mm.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <linux/cpu.h>
  2. #include <linux/dma-mapping.h>
  3. #include <linux/bootmem.h>
  4. #include <linux/gfp.h>
  5. #include <linux/highmem.h>
  6. #include <linux/export.h>
  7. #include <linux/memblock.h>
  8. #include <linux/of_address.h>
  9. #include <linux/slab.h>
  10. #include <linux/types.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/swiotlb.h>
  14. #include <xen/xen.h>
  15. #include <xen/interface/grant_table.h>
  16. #include <xen/interface/memory.h>
  17. #include <xen/page.h>
  18. #include <xen/swiotlb-xen.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/xen/hypercall.h>
  21. #include <asm/xen/interface.h>
  22. unsigned long xen_get_swiotlb_free_pages(unsigned int order)
  23. {
  24. struct memblock_region *reg;
  25. gfp_t flags = __GFP_NOWARN|__GFP_KSWAPD_RECLAIM;
  26. for_each_memblock(memory, reg) {
  27. if (reg->base < (phys_addr_t)0xffffffff) {
  28. flags |= __GFP_DMA;
  29. break;
  30. }
  31. }
  32. return __get_free_pages(flags, order);
  33. }
  34. enum dma_cache_op {
  35. DMA_UNMAP,
  36. DMA_MAP,
  37. };
  38. static bool hypercall_cflush = false;
  39. /* functions called by SWIOTLB */
  40. static void dma_cache_maint(dma_addr_t handle, unsigned long offset,
  41. size_t size, enum dma_data_direction dir, enum dma_cache_op op)
  42. {
  43. struct gnttab_cache_flush cflush;
  44. unsigned long xen_pfn;
  45. size_t left = size;
  46. xen_pfn = (handle >> XEN_PAGE_SHIFT) + offset / XEN_PAGE_SIZE;
  47. offset %= XEN_PAGE_SIZE;
  48. do {
  49. size_t len = left;
  50. /* buffers in highmem or foreign pages cannot cross page
  51. * boundaries */
  52. if (len + offset > XEN_PAGE_SIZE)
  53. len = XEN_PAGE_SIZE - offset;
  54. cflush.op = 0;
  55. cflush.a.dev_bus_addr = xen_pfn << XEN_PAGE_SHIFT;
  56. cflush.offset = offset;
  57. cflush.length = len;
  58. if (op == DMA_UNMAP && dir != DMA_TO_DEVICE)
  59. cflush.op = GNTTAB_CACHE_INVAL;
  60. if (op == DMA_MAP) {
  61. if (dir == DMA_FROM_DEVICE)
  62. cflush.op = GNTTAB_CACHE_INVAL;
  63. else
  64. cflush.op = GNTTAB_CACHE_CLEAN;
  65. }
  66. if (cflush.op)
  67. HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1);
  68. offset = 0;
  69. xen_pfn++;
  70. left -= len;
  71. } while (left);
  72. }
  73. static void __xen_dma_page_dev_to_cpu(struct device *hwdev, dma_addr_t handle,
  74. size_t size, enum dma_data_direction dir)
  75. {
  76. dma_cache_maint(handle & PAGE_MASK, handle & ~PAGE_MASK, size, dir, DMA_UNMAP);
  77. }
  78. static void __xen_dma_page_cpu_to_dev(struct device *hwdev, dma_addr_t handle,
  79. size_t size, enum dma_data_direction dir)
  80. {
  81. dma_cache_maint(handle & PAGE_MASK, handle & ~PAGE_MASK, size, dir, DMA_MAP);
  82. }
  83. void __xen_dma_map_page(struct device *hwdev, struct page *page,
  84. dma_addr_t dev_addr, unsigned long offset, size_t size,
  85. enum dma_data_direction dir, unsigned long attrs)
  86. {
  87. if (is_device_dma_coherent(hwdev))
  88. return;
  89. if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
  90. return;
  91. __xen_dma_page_cpu_to_dev(hwdev, dev_addr, size, dir);
  92. }
  93. void __xen_dma_unmap_page(struct device *hwdev, dma_addr_t handle,
  94. size_t size, enum dma_data_direction dir,
  95. unsigned long attrs)
  96. {
  97. if (is_device_dma_coherent(hwdev))
  98. return;
  99. if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
  100. return;
  101. __xen_dma_page_dev_to_cpu(hwdev, handle, size, dir);
  102. }
  103. void __xen_dma_sync_single_for_cpu(struct device *hwdev,
  104. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  105. {
  106. if (is_device_dma_coherent(hwdev))
  107. return;
  108. __xen_dma_page_dev_to_cpu(hwdev, handle, size, dir);
  109. }
  110. void __xen_dma_sync_single_for_device(struct device *hwdev,
  111. dma_addr_t handle, size_t size, enum dma_data_direction dir)
  112. {
  113. if (is_device_dma_coherent(hwdev))
  114. return;
  115. __xen_dma_page_cpu_to_dev(hwdev, handle, size, dir);
  116. }
  117. bool xen_arch_need_swiotlb(struct device *dev,
  118. phys_addr_t phys,
  119. dma_addr_t dev_addr)
  120. {
  121. unsigned int xen_pfn = XEN_PFN_DOWN(phys);
  122. unsigned int bfn = XEN_PFN_DOWN(dev_addr);
  123. /*
  124. * The swiotlb buffer should be used if
  125. * - Xen doesn't have the cache flush hypercall
  126. * - The Linux page refers to foreign memory
  127. * - The device doesn't support coherent DMA request
  128. *
  129. * The Linux page may be spanned acrros multiple Xen page, although
  130. * it's not possible to have a mix of local and foreign Xen page.
  131. * Furthermore, range_straddles_page_boundary is already checking
  132. * if buffer is physically contiguous in the host RAM.
  133. *
  134. * Therefore we only need to check the first Xen page to know if we
  135. * require a bounce buffer because the device doesn't support coherent
  136. * memory and we are not able to flush the cache.
  137. */
  138. return (!hypercall_cflush && (xen_pfn != bfn) &&
  139. !is_device_dma_coherent(dev));
  140. }
  141. int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order,
  142. unsigned int address_bits,
  143. dma_addr_t *dma_handle)
  144. {
  145. if (!xen_initial_domain())
  146. return -EINVAL;
  147. /* we assume that dom0 is mapped 1:1 for now */
  148. *dma_handle = pstart;
  149. return 0;
  150. }
  151. EXPORT_SYMBOL_GPL(xen_create_contiguous_region);
  152. void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order)
  153. {
  154. return;
  155. }
  156. EXPORT_SYMBOL_GPL(xen_destroy_contiguous_region);
  157. const struct dma_map_ops *xen_dma_ops;
  158. EXPORT_SYMBOL(xen_dma_ops);
  159. int __init xen_mm_init(void)
  160. {
  161. struct gnttab_cache_flush cflush;
  162. if (!xen_initial_domain())
  163. return 0;
  164. xen_swiotlb_init(1, false);
  165. xen_dma_ops = &xen_swiotlb_dma_ops;
  166. cflush.op = 0;
  167. cflush.a.dev_bus_addr = 0;
  168. cflush.offset = 0;
  169. cflush.length = 0;
  170. if (HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1) != -ENOSYS)
  171. hypercall_cflush = true;
  172. return 0;
  173. }
  174. arch_initcall(xen_mm_init);