1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/dma-direct.h>
4 #include <linux/dma-map-ops.h>
6 #include <linux/highmem.h>
7 #include <linux/export.h>
8 #include <linux/memblock.h>
9 #include <linux/of_address.h>
10 #include <linux/slab.h>
11 #include <linux/types.h>
12 #include <linux/vmalloc.h>
13 #include <linux/swiotlb.h>
16 #include <xen/interface/grant_table.h>
17 #include <xen/interface/memory.h>
19 #include <xen/xen-ops.h>
20 #include <xen/swiotlb-xen.h>
22 #include <asm/cacheflush.h>
23 #include <asm/xen/hypercall.h>
24 #include <asm/xen/interface.h>
26 unsigned long xen_get_swiotlb_free_pages(unsigned int order)
29 gfp_t flags = __GFP_NOWARN|__GFP_KSWAPD_RECLAIM;
32 for_each_mem_range(i, &base, NULL) {
33 if (base < (phys_addr_t)0xffffffff) {
34 if (IS_ENABLED(CONFIG_ZONE_DMA32))
41 return __get_free_pages(flags, order);
44 static bool hypercall_cflush = false;
46 /* buffers in highmem or foreign pages cannot cross page boundaries */
47 static void dma_cache_maint(struct device *dev, dma_addr_t handle,
50 struct gnttab_cache_flush cflush;
52 cflush.offset = xen_offset_in_page(handle);
54 handle &= XEN_PAGE_MASK;
57 cflush.a.dev_bus_addr = dma_to_phys(dev, handle);
59 if (size + cflush.offset > XEN_PAGE_SIZE)
60 cflush.length = XEN_PAGE_SIZE - cflush.offset;
64 HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1);
67 handle += cflush.length;
68 size -= cflush.length;
73 * Dom0 is mapped 1:1, and while the Linux page can span across multiple Xen
74 * pages, it is not possible for it to contain a mix of local and foreign Xen
75 * pages. Calling pfn_valid on a foreign mfn will always return false, so if
76 * pfn_valid returns true the pages is local and we can use the native
77 * dma-direct functions, otherwise we call the Xen specific version.
79 void xen_dma_sync_for_cpu(struct device *dev, dma_addr_t handle,
80 size_t size, enum dma_data_direction dir)
82 if (dir != DMA_TO_DEVICE)
83 dma_cache_maint(dev, handle, size, GNTTAB_CACHE_INVAL);
86 void xen_dma_sync_for_device(struct device *dev, dma_addr_t handle,
87 size_t size, enum dma_data_direction dir)
89 if (dir == DMA_FROM_DEVICE)
90 dma_cache_maint(dev, handle, size, GNTTAB_CACHE_INVAL);
92 dma_cache_maint(dev, handle, size, GNTTAB_CACHE_CLEAN);
95 bool xen_arch_need_swiotlb(struct device *dev,
99 unsigned int xen_pfn = XEN_PFN_DOWN(phys);
100 unsigned int bfn = XEN_PFN_DOWN(dma_to_phys(dev, dev_addr));
103 * The swiotlb buffer should be used if
104 * - Xen doesn't have the cache flush hypercall
105 * - The Linux page refers to foreign memory
106 * - The device doesn't support coherent DMA request
108 * The Linux page may be spanned acrros multiple Xen page, although
109 * it's not possible to have a mix of local and foreign Xen page.
110 * Furthermore, range_straddles_page_boundary is already checking
111 * if buffer is physically contiguous in the host RAM.
113 * Therefore we only need to check the first Xen page to know if we
114 * require a bounce buffer because the device doesn't support coherent
115 * memory and we are not able to flush the cache.
117 return (!hypercall_cflush && (xen_pfn != bfn) &&
118 !dev_is_dma_coherent(dev));
121 int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order,
122 unsigned int address_bits,
123 dma_addr_t *dma_handle)
125 if (!xen_initial_domain())
128 /* we assume that dom0 is mapped 1:1 for now */
129 *dma_handle = pstart;
133 void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order)
138 static int __init xen_mm_init(void)
140 struct gnttab_cache_flush cflush;
143 if (!xen_swiotlb_detect())
146 rc = xen_swiotlb_init();
147 /* we can work with the default swiotlb */
148 if (rc < 0 && rc != -EEXIST)
152 cflush.a.dev_bus_addr = 0;
155 if (HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1) != -ENOSYS)
156 hypercall_cflush = true;
159 arch_initcall(xen_mm_init);