Merge tag 'sched-urgent-2020-12-27' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / arch / arm / xen / mm.c
blob467fa225c3d0ed5c9793107f583c2be74d7a88c9
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/cpu.h>
3 #include <linux/dma-direct.h>
4 #include <linux/dma-map-ops.h>
5 #include <linux/gfp.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>
15 #include <xen/xen.h>
16 #include <xen/interface/grant_table.h>
17 #include <xen/interface/memory.h>
18 #include <xen/page.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)
28 phys_addr_t base;
29 gfp_t flags = __GFP_NOWARN|__GFP_KSWAPD_RECLAIM;
30 u64 i;
32 for_each_mem_range(i, &base, NULL) {
33 if (base < (phys_addr_t)0xffffffff) {
34 if (IS_ENABLED(CONFIG_ZONE_DMA32))
35 flags |= __GFP_DMA32;
36 else
37 flags |= __GFP_DMA;
38 break;
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,
48 size_t size, u32 op)
50 struct gnttab_cache_flush cflush;
52 cflush.offset = xen_offset_in_page(handle);
53 cflush.op = op;
54 handle &= XEN_PAGE_MASK;
56 do {
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;
61 else
62 cflush.length = size;
64 HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1);
66 cflush.offset = 0;
67 handle += cflush.length;
68 size -= cflush.length;
69 } while (size);
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);
91 else
92 dma_cache_maint(dev, handle, size, GNTTAB_CACHE_CLEAN);
95 bool xen_arch_need_swiotlb(struct device *dev,
96 phys_addr_t phys,
97 dma_addr_t dev_addr)
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())
126 return -EINVAL;
128 /* we assume that dom0 is mapped 1:1 for now */
129 *dma_handle = pstart;
130 return 0;
133 void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order)
135 return;
138 static int __init xen_mm_init(void)
140 struct gnttab_cache_flush cflush;
141 if (!xen_initial_domain())
142 return 0;
143 xen_swiotlb_init(1, false);
145 cflush.op = 0;
146 cflush.a.dev_bus_addr = 0;
147 cflush.offset = 0;
148 cflush.length = 0;
149 if (HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1) != -ENOSYS)
150 hypercall_cflush = true;
151 return 0;
153 arch_initcall(xen_mm_init);