dma-mapping: split <linux/dma-mapping.h>
[linux-2.6-microblaze.git] / drivers / xen / swiotlb-xen.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Copyright 2010
4  *  by Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
5  *
6  * This code provides a IOMMU for Xen PV guests with PCI passthrough.
7  *
8  * PV guests under Xen are running in an non-contiguous memory architecture.
9  *
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
13  * operations).
14  *
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.
25  */
26
27 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
28
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>
35 #include <xen/page.h>
36 #include <xen/xen-ops.h>
37 #include <xen/hvc-console.h>
38
39 #include <asm/dma-mapping.h>
40 #include <asm/xen/page-coherent.h>
41
42 #include <trace/events/swiotlb.h>
43 #define MAX_DMA_BITS 32
44 /*
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
47  * API.
48  */
49
50 static char *xen_io_tlb_start, *xen_io_tlb_end;
51 static unsigned long xen_io_tlb_nslabs;
52 /*
53  * Quick lookup value of the bus address of the IOTLB.
54  */
55
56 static inline phys_addr_t xen_phys_to_bus(struct device *dev, phys_addr_t paddr)
57 {
58         unsigned long bfn = pfn_to_bfn(XEN_PFN_DOWN(paddr));
59         phys_addr_t baddr = (phys_addr_t)bfn << XEN_PAGE_SHIFT;
60
61         baddr |= paddr & ~XEN_PAGE_MASK;
62         return baddr;
63 }
64
65 static inline dma_addr_t xen_phys_to_dma(struct device *dev, phys_addr_t paddr)
66 {
67         return phys_to_dma(dev, xen_phys_to_bus(dev, paddr));
68 }
69
70 static inline phys_addr_t xen_bus_to_phys(struct device *dev,
71                                           phys_addr_t baddr)
72 {
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);
76
77         return paddr;
78 }
79
80 static inline phys_addr_t xen_dma_to_phys(struct device *dev,
81                                           dma_addr_t dma_addr)
82 {
83         return xen_bus_to_phys(dev, dma_to_phys(dev, dma_addr));
84 }
85
86 static inline dma_addr_t xen_virt_to_bus(struct device *dev, void *address)
87 {
88         return xen_phys_to_dma(dev, virt_to_phys(address));
89 }
90
91 static inline int range_straddles_page_boundary(phys_addr_t p, size_t size)
92 {
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);
95
96         next_bfn = pfn_to_bfn(xen_pfn);
97
98         for (i = 1; i < nr_pages; i++)
99                 if (pfn_to_bfn(++xen_pfn) != ++next_bfn)
100                         return 1;
101
102         return 0;
103 }
104
105 static int is_xen_swiotlb_buffer(struct device *dev, dma_addr_t dma_addr)
106 {
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;
110
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.
114          */
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);
118         }
119         return 0;
120 }
121
122 static int
123 xen_swiotlb_fixup(void *buf, size_t size, unsigned long nslabs)
124 {
125         int i, rc;
126         int dma_bits;
127         dma_addr_t dma_handle;
128         phys_addr_t p = virt_to_phys(buf);
129
130         dma_bits = get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT) + PAGE_SHIFT;
131
132         i = 0;
133         do {
134                 int slabs = min(nslabs - i, (unsigned long)IO_TLB_SEGSIZE);
135
136                 do {
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);
142                 if (rc)
143                         return rc;
144
145                 i += slabs;
146         } while (i < nslabs);
147         return 0;
148 }
149 static unsigned long xen_set_nslabs(unsigned long nr_tbl)
150 {
151         if (!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);
154         } else
155                 xen_io_tlb_nslabs = nr_tbl;
156
157         return xen_io_tlb_nslabs << IO_TLB_SHIFT;
158 }
159
160 enum xen_swiotlb_err {
161         XEN_SWIOTLB_UNKNOWN = 0,
162         XEN_SWIOTLB_ENOMEM,
163         XEN_SWIOTLB_EFIXUP
164 };
165
166 static const char *xen_swiotlb_error(enum xen_swiotlb_err err)
167 {
168         switch (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!";
176         default:
177                 break;
178         }
179         return "";
180 }
181 int __ref xen_swiotlb_init(int verbose, bool early)
182 {
183         unsigned long bytes, order;
184         int rc = -ENOMEM;
185         enum xen_swiotlb_err m_ret = XEN_SWIOTLB_UNKNOWN;
186         unsigned int repeat = 3;
187
188         xen_io_tlb_nslabs = swiotlb_nr_tbl();
189 retry:
190         bytes = xen_set_nslabs(xen_io_tlb_nslabs);
191         order = get_order(xen_io_tlb_nslabs << IO_TLB_SHIFT);
192
193         /*
194          * IO TLB memory already allocated. Just use it.
195          */
196         if (io_tlb_start != 0) {
197                 xen_io_tlb_start = phys_to_virt(io_tlb_start);
198                 goto end;
199         }
200
201         /*
202          * Get IO TLB memory from any location.
203          */
204         if (early) {
205                 xen_io_tlb_start = memblock_alloc(PAGE_ALIGN(bytes),
206                                                   PAGE_SIZE);
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);
210         } else {
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)
216                                 break;
217                         order--;
218                 }
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;
224                 }
225         }
226         if (!xen_io_tlb_start) {
227                 m_ret = XEN_SWIOTLB_ENOMEM;
228                 goto error;
229         }
230         /*
231          * And replace that memory with pages under 4GB.
232          */
233         rc = xen_swiotlb_fixup(xen_io_tlb_start,
234                                bytes,
235                                xen_io_tlb_nslabs);
236         if (rc) {
237                 if (early)
238                         memblock_free(__pa(xen_io_tlb_start),
239                                       PAGE_ALIGN(bytes));
240                 else {
241                         free_pages((unsigned long)xen_io_tlb_start, order);
242                         xen_io_tlb_start = NULL;
243                 }
244                 m_ret = XEN_SWIOTLB_EFIXUP;
245                 goto error;
246         }
247         if (early) {
248                 if (swiotlb_init_with_tbl(xen_io_tlb_start, xen_io_tlb_nslabs,
249                          verbose))
250                         panic("Cannot allocate SWIOTLB buffer");
251                 rc = 0;
252         } else
253                 rc = swiotlb_late_init_with_tbl(xen_io_tlb_start, xen_io_tlb_nslabs);
254
255 end:
256         xen_io_tlb_end = xen_io_tlb_start + bytes;
257         if (!rc)
258                 swiotlb_set_max_segment(PAGE_SIZE);
259
260         return rc;
261 error:
262         if (repeat--) {
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);
267                 goto retry;
268         }
269         pr_err("%s (rc:%d)\n", xen_swiotlb_error(m_ret), rc);
270         if (early)
271                 panic("%s (rc:%d)", xen_swiotlb_error(m_ret), rc);
272         else
273                 free_pages((unsigned long)xen_io_tlb_start, order);
274         return rc;
275 }
276
277 static void *
278 xen_swiotlb_alloc_coherent(struct device *hwdev, size_t size,
279                            dma_addr_t *dma_handle, gfp_t flags,
280                            unsigned long attrs)
281 {
282         void *ret;
283         int order = get_order(size);
284         u64 dma_mask = DMA_BIT_MASK(32);
285         phys_addr_t phys;
286         dma_addr_t dev_addr;
287
288         /*
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.
293         */
294         flags &= ~(__GFP_DMA | __GFP_HIGHMEM);
295
296         /* Convert the size to actually allocated. */
297         size = 1UL << (order + XEN_PAGE_SHIFT);
298
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.
303          */
304         ret = xen_alloc_coherent_pages(hwdev, size, dma_handle, flags, attrs);
305
306         if (!ret)
307                 return ret;
308
309         if (hwdev && hwdev->coherent_dma_mask)
310                 dma_mask = hwdev->coherent_dma_mask;
311
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
315          * to *dma_handle. */
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;
321         else {
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);
325                         return NULL;
326                 }
327                 *dma_handle = phys_to_dma(hwdev, *dma_handle);
328                 SetPageXenRemapped(virt_to_page(ret));
329         }
330         memset(ret, 0, size);
331         return ret;
332 }
333
334 static void
335 xen_swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
336                           dma_addr_t dev_addr, unsigned long attrs)
337 {
338         int order = get_order(size);
339         phys_addr_t phys;
340         u64 dma_mask = DMA_BIT_MASK(32);
341         struct page *page;
342
343         if (hwdev && hwdev->coherent_dma_mask)
344                 dma_mask = hwdev->coherent_dma_mask;
345
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);
349
350         /* Convert the size to actually allocated. */
351         size = 1UL << (order + XEN_PAGE_SHIFT);
352
353         if (is_vmalloc_addr(vaddr))
354                 page = vmalloc_to_page(vaddr);
355         else
356                 page = virt_to_page(vaddr);
357
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);
362
363         xen_free_coherent_pages(hwdev, size, vaddr, phys_to_dma(hwdev, phys),
364                                 attrs);
365 }
366
367 /*
368  * Map a single buffer of the indicated size for DMA in streaming mode.  The
369  * physical address to use is returned.
370  *
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.
373  */
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,
377                                 unsigned long attrs)
378 {
379         phys_addr_t map, phys = page_to_phys(page) + offset;
380         dma_addr_t dev_addr = xen_phys_to_dma(dev, phys);
381
382         BUG_ON(dir == DMA_NONE);
383         /*
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
386          * buffering it.
387          */
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)
392                 goto done;
393
394         /*
395          * Oh well, have to allocate and map a bounce buffer.
396          */
397         trace_swiotlb_bounced(dev, dev_addr, size, swiotlb_force);
398
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;
403
404         phys = map;
405         dev_addr = xen_phys_to_dma(dev, map);
406
407         /*
408          * Ensure that the address returned is DMA'ble
409          */
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;
414         }
415
416 done:
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);
420                 else
421                         xen_dma_sync_for_device(dev, dev_addr, size, dir);
422         }
423         return dev_addr;
424 }
425
426 /*
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.
430  *
431  * After this call, reads by the cpu to the buffer are guaranteed to see
432  * whatever the device wrote there.
433  */
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)
436 {
437         phys_addr_t paddr = xen_dma_to_phys(hwdev, dev_addr);
438
439         BUG_ON(dir == DMA_NONE);
440
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);
444                 else
445                         xen_dma_sync_for_cpu(hwdev, dev_addr, size, dir);
446         }
447
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);
451 }
452
453 static void
454 xen_swiotlb_sync_single_for_cpu(struct device *dev, dma_addr_t dma_addr,
455                 size_t size, enum dma_data_direction dir)
456 {
457         phys_addr_t paddr = xen_dma_to_phys(dev, dma_addr);
458
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);
462                 else
463                         xen_dma_sync_for_cpu(dev, dma_addr, size, dir);
464         }
465
466         if (is_xen_swiotlb_buffer(dev, dma_addr))
467                 swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_CPU);
468 }
469
470 static void
471 xen_swiotlb_sync_single_for_device(struct device *dev, dma_addr_t dma_addr,
472                 size_t size, enum dma_data_direction dir)
473 {
474         phys_addr_t paddr = xen_dma_to_phys(dev, dma_addr);
475
476         if (is_xen_swiotlb_buffer(dev, dma_addr))
477                 swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_DEVICE);
478
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);
482                 else
483                         xen_dma_sync_for_device(dev, dma_addr, size, dir);
484         }
485 }
486
487 /*
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.
490  */
491 static void
492 xen_swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
493                 enum dma_data_direction dir, unsigned long attrs)
494 {
495         struct scatterlist *sg;
496         int i;
497
498         BUG_ON(dir == DMA_NONE);
499
500         for_each_sg(sgl, sg, nelems, i)
501                 xen_swiotlb_unmap_page(hwdev, sg->dma_address, sg_dma_len(sg),
502                                 dir, attrs);
503
504 }
505
506 static int
507 xen_swiotlb_map_sg(struct device *dev, struct scatterlist *sgl, int nelems,
508                 enum dma_data_direction dir, unsigned long attrs)
509 {
510         struct scatterlist *sg;
511         int i;
512
513         BUG_ON(dir == DMA_NONE);
514
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)
519                         goto out_unmap;
520                 sg_dma_len(sg) = sg->length;
521         }
522
523         return nelems;
524 out_unmap:
525         xen_swiotlb_unmap_sg(dev, sgl, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC);
526         sg_dma_len(sgl) = 0;
527         return 0;
528 }
529
530 static void
531 xen_swiotlb_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl,
532                             int nelems, enum dma_data_direction dir)
533 {
534         struct scatterlist *sg;
535         int i;
536
537         for_each_sg(sgl, sg, nelems, i) {
538                 xen_swiotlb_sync_single_for_cpu(dev, sg->dma_address,
539                                 sg->length, dir);
540         }
541 }
542
543 static void
544 xen_swiotlb_sync_sg_for_device(struct device *dev, struct scatterlist *sgl,
545                                int nelems, enum dma_data_direction dir)
546 {
547         struct scatterlist *sg;
548         int i;
549
550         for_each_sg(sgl, sg, nelems, i) {
551                 xen_swiotlb_sync_single_for_device(dev, sg->dma_address,
552                                 sg->length, dir);
553         }
554 }
555
556 /*
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
560  * this function.
561  */
562 static int
563 xen_swiotlb_dma_supported(struct device *hwdev, u64 mask)
564 {
565         return xen_virt_to_bus(hwdev, xen_io_tlb_end - 1) <= mask;
566 }
567
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,
584 };