page-coherent.h 3.9 KB

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