1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_ARM64_XEN_PAGE_COHERENT_H
3 #define _ASM_ARM64_XEN_PAGE_COHERENT_H
5 #include <linux/dma-mapping.h>
7 #include <xen/arm/page-coherent.h>
9 static inline void *xen_alloc_coherent_pages(struct device *hwdev, size_t size,
10 dma_addr_t *dma_handle, gfp_t flags, unsigned long attrs)
12 return dma_direct_alloc(hwdev, size, dma_handle, flags, attrs);
15 static inline void xen_free_coherent_pages(struct device *hwdev, size_t size,
16 void *cpu_addr, dma_addr_t dma_handle, unsigned long attrs)
18 dma_direct_free(hwdev, size, cpu_addr, dma_handle, attrs);
21 static inline void xen_dma_sync_single_for_cpu(struct device *hwdev,
22 dma_addr_t handle, size_t size, enum dma_data_direction dir)
24 unsigned long pfn = PFN_DOWN(handle);
27 dma_direct_sync_single_for_cpu(hwdev, handle, size, dir);
29 __xen_dma_sync_single_for_cpu(hwdev, handle, size, dir);
32 static inline void xen_dma_sync_single_for_device(struct device *hwdev,
33 dma_addr_t handle, size_t size, enum dma_data_direction dir)
35 unsigned long pfn = PFN_DOWN(handle);
37 dma_direct_sync_single_for_device(hwdev, handle, size, dir);
39 __xen_dma_sync_single_for_device(hwdev, handle, size, dir);
42 static inline void xen_dma_map_page(struct device *hwdev, struct page *page,
43 dma_addr_t dev_addr, unsigned long offset, size_t size,
44 enum dma_data_direction dir, unsigned long attrs)
46 unsigned long page_pfn = page_to_xen_pfn(page);
47 unsigned long dev_pfn = XEN_PFN_DOWN(dev_addr);
48 unsigned long compound_pages =
49 (1<<compound_order(page)) * XEN_PFN_PER_PAGE;
50 bool local = (page_pfn <= dev_pfn) &&
51 (dev_pfn - page_pfn < compound_pages);
54 dma_direct_map_page(hwdev, page, offset, size, dir, attrs);
56 __xen_dma_map_page(hwdev, page, dev_addr, offset, size, dir, attrs);
59 static inline void xen_dma_unmap_page(struct device *hwdev, dma_addr_t handle,
60 size_t size, enum dma_data_direction dir, unsigned long attrs)
62 unsigned long pfn = PFN_DOWN(handle);
64 * Dom0 is mapped 1:1, while the Linux page can be spanned accross
65 * multiple Xen page, it's not possible to have a mix of local and
66 * foreign Xen page. Dom0 is mapped 1:1, so calling pfn_valid on a
67 * foreign mfn will always return false. If the page is local we can
68 * safely call the native dma_ops function, otherwise we call the xen
72 dma_direct_unmap_page(hwdev, handle, size, dir, attrs);
74 __xen_dma_unmap_page(hwdev, handle, size, dir, attrs);
77 #endif /* _ASM_ARM64_XEN_PAGE_COHERENT_H */