Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[sfrench/cifs-2.6.git] / arch / arm / include / asm / xen / page-coherent.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_ARM_XEN_PAGE_COHERENT_H
3 #define _ASM_ARM_XEN_PAGE_COHERENT_H
4
5 #include <linux/dma-mapping.h>
6 #include <asm/page.h>
7 #include <xen/arm/page-coherent.h>
8
9 static inline const struct dma_map_ops *xen_get_dma_ops(struct device *dev)
10 {
11         if (dev && dev->archdata.dev_dma_ops)
12                 return dev->archdata.dev_dma_ops;
13         return get_arch_dma_ops(NULL);
14 }
15
16 static inline void *xen_alloc_coherent_pages(struct device *hwdev, size_t size,
17                 dma_addr_t *dma_handle, gfp_t flags, unsigned long attrs)
18 {
19         return xen_get_dma_ops(hwdev)->alloc(hwdev, size, dma_handle, flags, attrs);
20 }
21
22 static inline void xen_free_coherent_pages(struct device *hwdev, size_t size,
23                 void *cpu_addr, dma_addr_t dma_handle, unsigned long attrs)
24 {
25         xen_get_dma_ops(hwdev)->free(hwdev, size, cpu_addr, dma_handle, attrs);
26 }
27
28 static inline void xen_dma_map_page(struct device *hwdev, struct page *page,
29              dma_addr_t dev_addr, unsigned long offset, size_t size,
30              enum dma_data_direction dir, unsigned long attrs)
31 {
32         unsigned long page_pfn = page_to_xen_pfn(page);
33         unsigned long dev_pfn = XEN_PFN_DOWN(dev_addr);
34         unsigned long compound_pages =
35                 (1<<compound_order(page)) * XEN_PFN_PER_PAGE;
36         bool local = (page_pfn <= dev_pfn) &&
37                 (dev_pfn - page_pfn < compound_pages);
38
39         /*
40          * Dom0 is mapped 1:1, while the Linux page can span across
41          * multiple Xen pages, it's not possible for it to contain a
42          * mix of local and foreign Xen pages. So if the first xen_pfn
43          * == mfn the page is local otherwise it's a foreign page
44          * grant-mapped in dom0. If the page is local we can safely
45          * call the native dma_ops function, otherwise we call the xen
46          * specific function.
47          */
48         if (local)
49                 xen_get_dma_ops(hwdev)->map_page(hwdev, page, offset, size, dir, attrs);
50         else
51                 __xen_dma_map_page(hwdev, page, dev_addr, offset, size, dir, attrs);
52 }
53
54 static inline void xen_dma_unmap_page(struct device *hwdev, dma_addr_t handle,
55                 size_t size, enum dma_data_direction dir, unsigned long attrs)
56 {
57         unsigned long pfn = PFN_DOWN(handle);
58         /*
59          * Dom0 is mapped 1:1, while the Linux page can be spanned accross
60          * multiple Xen page, it's not possible to have a mix of local and
61          * foreign Xen page. Dom0 is mapped 1:1, so calling pfn_valid on a
62          * foreign mfn will always return false. If the page is local we can
63          * safely call the native dma_ops function, otherwise we call the xen
64          * specific function.
65          */
66         if (pfn_valid(pfn)) {
67                 if (xen_get_dma_ops(hwdev)->unmap_page)
68                         xen_get_dma_ops(hwdev)->unmap_page(hwdev, handle, size, dir, attrs);
69         } else
70                 __xen_dma_unmap_page(hwdev, handle, size, dir, attrs);
71 }
72
73 static inline void xen_dma_sync_single_for_cpu(struct device *hwdev,
74                 dma_addr_t handle, size_t size, enum dma_data_direction dir)
75 {
76         unsigned long pfn = PFN_DOWN(handle);
77         if (pfn_valid(pfn)) {
78                 if (xen_get_dma_ops(hwdev)->sync_single_for_cpu)
79                         xen_get_dma_ops(hwdev)->sync_single_for_cpu(hwdev, handle, size, dir);
80         } else
81                 __xen_dma_sync_single_for_cpu(hwdev, handle, size, dir);
82 }
83
84 static inline void xen_dma_sync_single_for_device(struct device *hwdev,
85                 dma_addr_t handle, size_t size, enum dma_data_direction dir)
86 {
87         unsigned long pfn = PFN_DOWN(handle);
88         if (pfn_valid(pfn)) {
89                 if (xen_get_dma_ops(hwdev)->sync_single_for_device)
90                         xen_get_dma_ops(hwdev)->sync_single_for_device(hwdev, handle, size, dir);
91         } else
92                 __xen_dma_sync_single_for_device(hwdev, handle, size, dir);
93 }
94
95 #endif /* _ASM_ARM_XEN_PAGE_COHERENT_H */