page-coherent.h 3.7 KB

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