arm: xen: mm: use __GPF_DMA32 for arm64
[linux-2.6-microblaze.git] / arch / arm / xen / mm.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/cpu.h>
3 #include <linux/dma-mapping.h>
4 #include <linux/gfp.h>
5 #include <linux/highmem.h>
6 #include <linux/export.h>
7 #include <linux/memblock.h>
8 #include <linux/of_address.h>
9 #include <linux/slab.h>
10 #include <linux/types.h>
11 #include <linux/vmalloc.h>
12 #include <linux/swiotlb.h>
13
14 #include <xen/xen.h>
15 #include <xen/interface/grant_table.h>
16 #include <xen/interface/memory.h>
17 #include <xen/page.h>
18 #include <xen/swiotlb-xen.h>
19
20 #include <asm/cacheflush.h>
21 #include <asm/xen/hypercall.h>
22 #include <asm/xen/interface.h>
23
24 unsigned long xen_get_swiotlb_free_pages(unsigned int order)
25 {
26         struct memblock_region *reg;
27         gfp_t flags = __GFP_NOWARN|__GFP_KSWAPD_RECLAIM;
28
29         for_each_memblock(memory, reg) {
30                 if (reg->base < (phys_addr_t)0xffffffff) {
31                         if (IS_ENABLED(CONFIG_ZONE_DMA32))
32                                 flags |= __GFP_DMA32;
33                         else
34                                 flags |= __GFP_DMA;
35                         break;
36                 }
37         }
38         return __get_free_pages(flags, order);
39 }
40
41 enum dma_cache_op {
42        DMA_UNMAP,
43        DMA_MAP,
44 };
45 static bool hypercall_cflush = false;
46
47 /* functions called by SWIOTLB */
48
49 static void dma_cache_maint(dma_addr_t handle, unsigned long offset,
50         size_t size, enum dma_data_direction dir, enum dma_cache_op op)
51 {
52         struct gnttab_cache_flush cflush;
53         unsigned long xen_pfn;
54         size_t left = size;
55
56         xen_pfn = (handle >> XEN_PAGE_SHIFT) + offset / XEN_PAGE_SIZE;
57         offset %= XEN_PAGE_SIZE;
58
59         do {
60                 size_t len = left;
61         
62                 /* buffers in highmem or foreign pages cannot cross page
63                  * boundaries */
64                 if (len + offset > XEN_PAGE_SIZE)
65                         len = XEN_PAGE_SIZE - offset;
66
67                 cflush.op = 0;
68                 cflush.a.dev_bus_addr = xen_pfn << XEN_PAGE_SHIFT;
69                 cflush.offset = offset;
70                 cflush.length = len;
71
72                 if (op == DMA_UNMAP && dir != DMA_TO_DEVICE)
73                         cflush.op = GNTTAB_CACHE_INVAL;
74                 if (op == DMA_MAP) {
75                         if (dir == DMA_FROM_DEVICE)
76                                 cflush.op = GNTTAB_CACHE_INVAL;
77                         else
78                                 cflush.op = GNTTAB_CACHE_CLEAN;
79                 }
80                 if (cflush.op)
81                         HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1);
82
83                 offset = 0;
84                 xen_pfn++;
85                 left -= len;
86         } while (left);
87 }
88
89 static void __xen_dma_page_dev_to_cpu(struct device *hwdev, dma_addr_t handle,
90                 size_t size, enum dma_data_direction dir)
91 {
92         dma_cache_maint(handle & PAGE_MASK, handle & ~PAGE_MASK, size, dir, DMA_UNMAP);
93 }
94
95 static void __xen_dma_page_cpu_to_dev(struct device *hwdev, dma_addr_t handle,
96                 size_t size, enum dma_data_direction dir)
97 {
98         dma_cache_maint(handle & PAGE_MASK, handle & ~PAGE_MASK, size, dir, DMA_MAP);
99 }
100
101 void __xen_dma_map_page(struct device *hwdev, struct page *page,
102              dma_addr_t dev_addr, unsigned long offset, size_t size,
103              enum dma_data_direction dir, unsigned long attrs)
104 {
105         if (is_device_dma_coherent(hwdev))
106                 return;
107         if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
108                 return;
109
110         __xen_dma_page_cpu_to_dev(hwdev, dev_addr, size, dir);
111 }
112
113 void __xen_dma_unmap_page(struct device *hwdev, dma_addr_t handle,
114                 size_t size, enum dma_data_direction dir,
115                 unsigned long attrs)
116
117 {
118         if (is_device_dma_coherent(hwdev))
119                 return;
120         if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
121                 return;
122
123         __xen_dma_page_dev_to_cpu(hwdev, handle, size, dir);
124 }
125
126 void __xen_dma_sync_single_for_cpu(struct device *hwdev,
127                 dma_addr_t handle, size_t size, enum dma_data_direction dir)
128 {
129         if (is_device_dma_coherent(hwdev))
130                 return;
131         __xen_dma_page_dev_to_cpu(hwdev, handle, size, dir);
132 }
133
134 void __xen_dma_sync_single_for_device(struct device *hwdev,
135                 dma_addr_t handle, size_t size, enum dma_data_direction dir)
136 {
137         if (is_device_dma_coherent(hwdev))
138                 return;
139         __xen_dma_page_cpu_to_dev(hwdev, handle, size, dir);
140 }
141
142 bool xen_arch_need_swiotlb(struct device *dev,
143                            phys_addr_t phys,
144                            dma_addr_t dev_addr)
145 {
146         unsigned int xen_pfn = XEN_PFN_DOWN(phys);
147         unsigned int bfn = XEN_PFN_DOWN(dev_addr);
148
149         /*
150          * The swiotlb buffer should be used if
151          *      - Xen doesn't have the cache flush hypercall
152          *      - The Linux page refers to foreign memory
153          *      - The device doesn't support coherent DMA request
154          *
155          * The Linux page may be spanned acrros multiple Xen page, although
156          * it's not possible to have a mix of local and foreign Xen page.
157          * Furthermore, range_straddles_page_boundary is already checking
158          * if buffer is physically contiguous in the host RAM.
159          *
160          * Therefore we only need to check the first Xen page to know if we
161          * require a bounce buffer because the device doesn't support coherent
162          * memory and we are not able to flush the cache.
163          */
164         return (!hypercall_cflush && (xen_pfn != bfn) &&
165                 !is_device_dma_coherent(dev));
166 }
167
168 int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order,
169                                  unsigned int address_bits,
170                                  dma_addr_t *dma_handle)
171 {
172         if (!xen_initial_domain())
173                 return -EINVAL;
174
175         /* we assume that dom0 is mapped 1:1 for now */
176         *dma_handle = pstart;
177         return 0;
178 }
179 EXPORT_SYMBOL_GPL(xen_create_contiguous_region);
180
181 void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order)
182 {
183         return;
184 }
185 EXPORT_SYMBOL_GPL(xen_destroy_contiguous_region);
186
187 const struct dma_map_ops *xen_dma_ops;
188 EXPORT_SYMBOL(xen_dma_ops);
189
190 int __init xen_mm_init(void)
191 {
192         struct gnttab_cache_flush cflush;
193         if (!xen_initial_domain())
194                 return 0;
195         xen_swiotlb_init(1, false);
196         xen_dma_ops = &xen_swiotlb_dma_ops;
197
198         cflush.op = 0;
199         cflush.a.dev_bus_addr = 0;
200         cflush.offset = 0;
201         cflush.length = 0;
202         if (HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1) != -ENOSYS)
203                 hypercall_cflush = true;
204         return 0;
205 }
206 arch_initcall(xen_mm_init);