1 // SPDX-License-Identifier: GPL-2.0-only
4 * by Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
6 * This code provides a IOMMU for Xen PV guests with PCI passthrough.
8 * PV guests under Xen are running in an non-contiguous memory architecture.
10 * When PCI pass-through is utilized, this necessitates an IOMMU for
11 * translating bus (DMA) to virtual and vice-versa and also providing a
12 * mechanism to have contiguous pages for device drivers operations (say DMA
15 * Specifically, under Xen the Linux idea of pages is an illusion. It
16 * assumes that pages start at zero and go up to the available memory. To
17 * help with that, the Linux Xen MMU provides a lookup mechanism to
18 * translate the page frame numbers (PFN) to machine frame numbers (MFN)
19 * and vice-versa. The MFN are the "real" frame numbers. Furthermore
20 * memory is not contiguous. Xen hypervisor stitches memory for guests
21 * from different pools, which means there is no guarantee that PFN==MFN
22 * and PFN+1==MFN+1. Lastly with Xen 4.0, pages (in debug mode) are
23 * allocated in descending order (high to low), meaning the guest might
24 * never get any MFN's under the 4GB mark.
27 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
29 #include <linux/memblock.h>
30 #include <linux/dma-map-ops.h>
31 #include <linux/dma-direct.h>
32 #include <linux/dma-noncoherent.h>
33 #include <linux/export.h>
34 #include <xen/swiotlb-xen.h>
36 #include <xen/xen-ops.h>
37 #include <xen/hvc-console.h>
39 #include <asm/dma-mapping.h>
40 #include <asm/xen/page-coherent.h>
42 #include <trace/events/swiotlb.h>
43 #define MAX_DMA_BITS 32
45 * Used to do a quick range check in swiotlb_tbl_unmap_single and
46 * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this
50 static char *xen_io_tlb_start, *xen_io_tlb_end;
51 static unsigned long xen_io_tlb_nslabs;
53 * Quick lookup value of the bus address of the IOTLB.
56 static inline phys_addr_t xen_phys_to_bus(struct device *dev, phys_addr_t paddr)
58 unsigned long bfn = pfn_to_bfn(XEN_PFN_DOWN(paddr));
59 phys_addr_t baddr = (phys_addr_t)bfn << XEN_PAGE_SHIFT;
61 baddr |= paddr & ~XEN_PAGE_MASK;
65 static inline dma_addr_t xen_phys_to_dma(struct device *dev, phys_addr_t paddr)
67 return phys_to_dma(dev, xen_phys_to_bus(dev, paddr));
70 static inline phys_addr_t xen_bus_to_phys(struct device *dev,
73 unsigned long xen_pfn = bfn_to_pfn(XEN_PFN_DOWN(baddr));
74 phys_addr_t paddr = (xen_pfn << XEN_PAGE_SHIFT) |
75 (baddr & ~XEN_PAGE_MASK);
80 static inline phys_addr_t xen_dma_to_phys(struct device *dev,
83 return xen_bus_to_phys(dev, dma_to_phys(dev, dma_addr));
86 static inline dma_addr_t xen_virt_to_bus(struct device *dev, void *address)
88 return xen_phys_to_dma(dev, virt_to_phys(address));
91 static inline int range_straddles_page_boundary(phys_addr_t p, size_t size)
93 unsigned long next_bfn, xen_pfn = XEN_PFN_DOWN(p);
94 unsigned int i, nr_pages = XEN_PFN_UP(xen_offset_in_page(p) + size);
96 next_bfn = pfn_to_bfn(xen_pfn);
98 for (i = 1; i < nr_pages; i++)
99 if (pfn_to_bfn(++xen_pfn) != ++next_bfn)
105 static int is_xen_swiotlb_buffer(struct device *dev, dma_addr_t dma_addr)
107 unsigned long bfn = XEN_PFN_DOWN(dma_to_phys(dev, dma_addr));
108 unsigned long xen_pfn = bfn_to_local_pfn(bfn);
109 phys_addr_t paddr = (phys_addr_t)xen_pfn << XEN_PAGE_SHIFT;
111 /* If the address is outside our domain, it CAN
112 * have the same virtual address as another address
113 * in our domain. Therefore _only_ check address within our domain.
115 if (pfn_valid(PFN_DOWN(paddr))) {
116 return paddr >= virt_to_phys(xen_io_tlb_start) &&
117 paddr < virt_to_phys(xen_io_tlb_end);
123 xen_swiotlb_fixup(void *buf, size_t size, unsigned long nslabs)
127 dma_addr_t dma_handle;
128 phys_addr_t p = virt_to_phys(buf);
130 dma_bits = get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT) + PAGE_SHIFT;
134 int slabs = min(nslabs - i, (unsigned long)IO_TLB_SEGSIZE);
137 rc = xen_create_contiguous_region(
138 p + (i << IO_TLB_SHIFT),
139 get_order(slabs << IO_TLB_SHIFT),
140 dma_bits, &dma_handle);
141 } while (rc && dma_bits++ < MAX_DMA_BITS);
146 } while (i < nslabs);
149 static unsigned long xen_set_nslabs(unsigned long nr_tbl)
152 xen_io_tlb_nslabs = (64 * 1024 * 1024 >> IO_TLB_SHIFT);
153 xen_io_tlb_nslabs = ALIGN(xen_io_tlb_nslabs, IO_TLB_SEGSIZE);
155 xen_io_tlb_nslabs = nr_tbl;
157 return xen_io_tlb_nslabs << IO_TLB_SHIFT;
160 enum xen_swiotlb_err {
161 XEN_SWIOTLB_UNKNOWN = 0,
166 static const char *xen_swiotlb_error(enum xen_swiotlb_err err)
169 case XEN_SWIOTLB_ENOMEM:
170 return "Cannot allocate Xen-SWIOTLB buffer\n";
171 case XEN_SWIOTLB_EFIXUP:
172 return "Failed to get contiguous memory for DMA from Xen!\n"\
173 "You either: don't have the permissions, do not have"\
174 " enough free memory under 4GB, or the hypervisor memory"\
175 " is too fragmented!";
181 int __ref xen_swiotlb_init(int verbose, bool early)
183 unsigned long bytes, order;
185 enum xen_swiotlb_err m_ret = XEN_SWIOTLB_UNKNOWN;
186 unsigned int repeat = 3;
188 xen_io_tlb_nslabs = swiotlb_nr_tbl();
190 bytes = xen_set_nslabs(xen_io_tlb_nslabs);
191 order = get_order(xen_io_tlb_nslabs << IO_TLB_SHIFT);
194 * IO TLB memory already allocated. Just use it.
196 if (io_tlb_start != 0) {
197 xen_io_tlb_start = phys_to_virt(io_tlb_start);
202 * Get IO TLB memory from any location.
205 xen_io_tlb_start = memblock_alloc(PAGE_ALIGN(bytes),
207 if (!xen_io_tlb_start)
208 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
209 __func__, PAGE_ALIGN(bytes), PAGE_SIZE);
211 #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
212 #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
213 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
214 xen_io_tlb_start = (void *)xen_get_swiotlb_free_pages(order);
215 if (xen_io_tlb_start)
219 if (order != get_order(bytes)) {
220 pr_warn("Warning: only able to allocate %ld MB for software IO TLB\n",
221 (PAGE_SIZE << order) >> 20);
222 xen_io_tlb_nslabs = SLABS_PER_PAGE << order;
223 bytes = xen_io_tlb_nslabs << IO_TLB_SHIFT;
226 if (!xen_io_tlb_start) {
227 m_ret = XEN_SWIOTLB_ENOMEM;
231 * And replace that memory with pages under 4GB.
233 rc = xen_swiotlb_fixup(xen_io_tlb_start,
238 memblock_free(__pa(xen_io_tlb_start),
241 free_pages((unsigned long)xen_io_tlb_start, order);
242 xen_io_tlb_start = NULL;
244 m_ret = XEN_SWIOTLB_EFIXUP;
248 if (swiotlb_init_with_tbl(xen_io_tlb_start, xen_io_tlb_nslabs,
250 panic("Cannot allocate SWIOTLB buffer");
253 rc = swiotlb_late_init_with_tbl(xen_io_tlb_start, xen_io_tlb_nslabs);
256 xen_io_tlb_end = xen_io_tlb_start + bytes;
258 swiotlb_set_max_segment(PAGE_SIZE);
263 xen_io_tlb_nslabs = max(1024UL, /* Min is 2MB */
264 (xen_io_tlb_nslabs >> 1));
265 pr_info("Lowering to %luMB\n",
266 (xen_io_tlb_nslabs << IO_TLB_SHIFT) >> 20);
269 pr_err("%s (rc:%d)\n", xen_swiotlb_error(m_ret), rc);
271 panic("%s (rc:%d)", xen_swiotlb_error(m_ret), rc);
273 free_pages((unsigned long)xen_io_tlb_start, order);
278 xen_swiotlb_alloc_coherent(struct device *hwdev, size_t size,
279 dma_addr_t *dma_handle, gfp_t flags,
283 int order = get_order(size);
284 u64 dma_mask = DMA_BIT_MASK(32);
289 * Ignore region specifiers - the kernel's ideas of
290 * pseudo-phys memory layout has nothing to do with the
291 * machine physical layout. We can't allocate highmem
292 * because we can't return a pointer to it.
294 flags &= ~(__GFP_DMA | __GFP_HIGHMEM);
296 /* Convert the size to actually allocated. */
297 size = 1UL << (order + XEN_PAGE_SHIFT);
299 /* On ARM this function returns an ioremap'ped virtual address for
300 * which virt_to_phys doesn't return the corresponding physical
301 * address. In fact on ARM virt_to_phys only works for kernel direct
302 * mapped RAM memory. Also see comment below.
304 ret = xen_alloc_coherent_pages(hwdev, size, dma_handle, flags, attrs);
309 if (hwdev && hwdev->coherent_dma_mask)
310 dma_mask = hwdev->coherent_dma_mask;
312 /* At this point dma_handle is the dma address, next we are
313 * going to set it to the machine address.
314 * Do not use virt_to_phys(ret) because on ARM it doesn't correspond
316 phys = dma_to_phys(hwdev, *dma_handle);
317 dev_addr = xen_phys_to_dma(hwdev, phys);
318 if (((dev_addr + size - 1 <= dma_mask)) &&
319 !range_straddles_page_boundary(phys, size))
320 *dma_handle = dev_addr;
322 if (xen_create_contiguous_region(phys, order,
323 fls64(dma_mask), dma_handle) != 0) {
324 xen_free_coherent_pages(hwdev, size, ret, (dma_addr_t)phys, attrs);
327 *dma_handle = phys_to_dma(hwdev, *dma_handle);
328 SetPageXenRemapped(virt_to_page(ret));
330 memset(ret, 0, size);
335 xen_swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
336 dma_addr_t dev_addr, unsigned long attrs)
338 int order = get_order(size);
340 u64 dma_mask = DMA_BIT_MASK(32);
343 if (hwdev && hwdev->coherent_dma_mask)
344 dma_mask = hwdev->coherent_dma_mask;
346 /* do not use virt_to_phys because on ARM it doesn't return you the
347 * physical address */
348 phys = xen_dma_to_phys(hwdev, dev_addr);
350 /* Convert the size to actually allocated. */
351 size = 1UL << (order + XEN_PAGE_SHIFT);
353 if (is_vmalloc_addr(vaddr))
354 page = vmalloc_to_page(vaddr);
356 page = virt_to_page(vaddr);
358 if (!WARN_ON((dev_addr + size - 1 > dma_mask) ||
359 range_straddles_page_boundary(phys, size)) &&
360 TestClearPageXenRemapped(page))
361 xen_destroy_contiguous_region(phys, order);
363 xen_free_coherent_pages(hwdev, size, vaddr, phys_to_dma(hwdev, phys),
368 * Map a single buffer of the indicated size for DMA in streaming mode. The
369 * physical address to use is returned.
371 * Once the device is given the dma address, the device owns this memory until
372 * either xen_swiotlb_unmap_page or xen_swiotlb_dma_sync_single is performed.
374 static dma_addr_t xen_swiotlb_map_page(struct device *dev, struct page *page,
375 unsigned long offset, size_t size,
376 enum dma_data_direction dir,
379 phys_addr_t map, phys = page_to_phys(page) + offset;
380 dma_addr_t dev_addr = xen_phys_to_dma(dev, phys);
382 BUG_ON(dir == DMA_NONE);
384 * If the address happens to be in the device's DMA window,
385 * we can safely return the device addr and not worry about bounce
388 if (dma_capable(dev, dev_addr, size, true) &&
389 !range_straddles_page_boundary(phys, size) &&
390 !xen_arch_need_swiotlb(dev, phys, dev_addr) &&
391 swiotlb_force != SWIOTLB_FORCE)
395 * Oh well, have to allocate and map a bounce buffer.
397 trace_swiotlb_bounced(dev, dev_addr, size, swiotlb_force);
399 map = swiotlb_tbl_map_single(dev, virt_to_phys(xen_io_tlb_start),
400 phys, size, size, dir, attrs);
401 if (map == (phys_addr_t)DMA_MAPPING_ERROR)
402 return DMA_MAPPING_ERROR;
405 dev_addr = xen_phys_to_dma(dev, map);
408 * Ensure that the address returned is DMA'ble
410 if (unlikely(!dma_capable(dev, dev_addr, size, true))) {
411 swiotlb_tbl_unmap_single(dev, map, size, size, dir,
412 attrs | DMA_ATTR_SKIP_CPU_SYNC);
413 return DMA_MAPPING_ERROR;
417 if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
418 if (pfn_valid(PFN_DOWN(dma_to_phys(dev, dev_addr))))
419 arch_sync_dma_for_device(phys, size, dir);
421 xen_dma_sync_for_device(dev, dev_addr, size, dir);
427 * Unmap a single streaming mode DMA translation. The dma_addr and size must
428 * match what was provided for in a previous xen_swiotlb_map_page call. All
429 * other usages are undefined.
431 * After this call, reads by the cpu to the buffer are guaranteed to see
432 * whatever the device wrote there.
434 static void xen_swiotlb_unmap_page(struct device *hwdev, dma_addr_t dev_addr,
435 size_t size, enum dma_data_direction dir, unsigned long attrs)
437 phys_addr_t paddr = xen_dma_to_phys(hwdev, dev_addr);
439 BUG_ON(dir == DMA_NONE);
441 if (!dev_is_dma_coherent(hwdev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
442 if (pfn_valid(PFN_DOWN(dma_to_phys(hwdev, dev_addr))))
443 arch_sync_dma_for_cpu(paddr, size, dir);
445 xen_dma_sync_for_cpu(hwdev, dev_addr, size, dir);
448 /* NOTE: We use dev_addr here, not paddr! */
449 if (is_xen_swiotlb_buffer(hwdev, dev_addr))
450 swiotlb_tbl_unmap_single(hwdev, paddr, size, size, dir, attrs);
454 xen_swiotlb_sync_single_for_cpu(struct device *dev, dma_addr_t dma_addr,
455 size_t size, enum dma_data_direction dir)
457 phys_addr_t paddr = xen_dma_to_phys(dev, dma_addr);
459 if (!dev_is_dma_coherent(dev)) {
460 if (pfn_valid(PFN_DOWN(dma_to_phys(dev, dma_addr))))
461 arch_sync_dma_for_cpu(paddr, size, dir);
463 xen_dma_sync_for_cpu(dev, dma_addr, size, dir);
466 if (is_xen_swiotlb_buffer(dev, dma_addr))
467 swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_CPU);
471 xen_swiotlb_sync_single_for_device(struct device *dev, dma_addr_t dma_addr,
472 size_t size, enum dma_data_direction dir)
474 phys_addr_t paddr = xen_dma_to_phys(dev, dma_addr);
476 if (is_xen_swiotlb_buffer(dev, dma_addr))
477 swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_DEVICE);
479 if (!dev_is_dma_coherent(dev)) {
480 if (pfn_valid(PFN_DOWN(dma_to_phys(dev, dma_addr))))
481 arch_sync_dma_for_device(paddr, size, dir);
483 xen_dma_sync_for_device(dev, dma_addr, size, dir);
488 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
489 * concerning calls here are the same as for swiotlb_unmap_page() above.
492 xen_swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
493 enum dma_data_direction dir, unsigned long attrs)
495 struct scatterlist *sg;
498 BUG_ON(dir == DMA_NONE);
500 for_each_sg(sgl, sg, nelems, i)
501 xen_swiotlb_unmap_page(hwdev, sg->dma_address, sg_dma_len(sg),
507 xen_swiotlb_map_sg(struct device *dev, struct scatterlist *sgl, int nelems,
508 enum dma_data_direction dir, unsigned long attrs)
510 struct scatterlist *sg;
513 BUG_ON(dir == DMA_NONE);
515 for_each_sg(sgl, sg, nelems, i) {
516 sg->dma_address = xen_swiotlb_map_page(dev, sg_page(sg),
517 sg->offset, sg->length, dir, attrs);
518 if (sg->dma_address == DMA_MAPPING_ERROR)
520 sg_dma_len(sg) = sg->length;
525 xen_swiotlb_unmap_sg(dev, sgl, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC);
531 xen_swiotlb_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl,
532 int nelems, enum dma_data_direction dir)
534 struct scatterlist *sg;
537 for_each_sg(sgl, sg, nelems, i) {
538 xen_swiotlb_sync_single_for_cpu(dev, sg->dma_address,
544 xen_swiotlb_sync_sg_for_device(struct device *dev, struct scatterlist *sgl,
545 int nelems, enum dma_data_direction dir)
547 struct scatterlist *sg;
550 for_each_sg(sgl, sg, nelems, i) {
551 xen_swiotlb_sync_single_for_device(dev, sg->dma_address,
557 * Return whether the given device DMA address mask can be supported
558 * properly. For example, if your device can only drive the low 24-bits
559 * during bus mastering, then you would pass 0x00ffffff as the mask to
563 xen_swiotlb_dma_supported(struct device *hwdev, u64 mask)
565 return xen_virt_to_bus(hwdev, xen_io_tlb_end - 1) <= mask;
568 const struct dma_map_ops xen_swiotlb_dma_ops = {
569 .alloc = xen_swiotlb_alloc_coherent,
570 .free = xen_swiotlb_free_coherent,
571 .sync_single_for_cpu = xen_swiotlb_sync_single_for_cpu,
572 .sync_single_for_device = xen_swiotlb_sync_single_for_device,
573 .sync_sg_for_cpu = xen_swiotlb_sync_sg_for_cpu,
574 .sync_sg_for_device = xen_swiotlb_sync_sg_for_device,
575 .map_sg = xen_swiotlb_map_sg,
576 .unmap_sg = xen_swiotlb_unmap_sg,
577 .map_page = xen_swiotlb_map_page,
578 .unmap_page = xen_swiotlb_unmap_page,
579 .dma_supported = xen_swiotlb_dma_supported,
580 .mmap = dma_common_mmap,
581 .get_sgtable = dma_common_get_sgtable,
582 .alloc_pages = dma_common_alloc_pages,
583 .free_pages = dma_common_free_pages,