mm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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;
  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 pfn;
  45. size_t left = size;
  46. pfn = (handle >> PAGE_SHIFT) + offset / PAGE_SIZE;
  47. offset %= 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 > PAGE_SIZE)
  53. len = PAGE_SIZE - offset;
  54. cflush.op = 0;
  55. cflush.a.dev_bus_addr = pfn << 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. 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, struct dma_attrs *attrs)
  86. {
  87. if (is_device_dma_coherent(hwdev))
  88. return;
  89. if (dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  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. struct dma_attrs *attrs)
  96. {
  97. if (is_device_dma_coherent(hwdev))
  98. return;
  99. if (dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
  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. unsigned long pfn,
  119. unsigned long bfn)
  120. {
  121. return (!hypercall_cflush && (pfn != bfn) && !is_device_dma_coherent(dev));
  122. }
  123. int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order,
  124. unsigned int address_bits,
  125. dma_addr_t *dma_handle)
  126. {
  127. if (!xen_initial_domain())
  128. return -EINVAL;
  129. /* we assume that dom0 is mapped 1:1 for now */
  130. *dma_handle = pstart;
  131. return 0;
  132. }
  133. EXPORT_SYMBOL_GPL(xen_create_contiguous_region);
  134. void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order)
  135. {
  136. return;
  137. }
  138. EXPORT_SYMBOL_GPL(xen_destroy_contiguous_region);
  139. struct dma_map_ops *xen_dma_ops;
  140. EXPORT_SYMBOL(xen_dma_ops);
  141. static struct dma_map_ops xen_swiotlb_dma_ops = {
  142. .mapping_error = xen_swiotlb_dma_mapping_error,
  143. .alloc = xen_swiotlb_alloc_coherent,
  144. .free = xen_swiotlb_free_coherent,
  145. .sync_single_for_cpu = xen_swiotlb_sync_single_for_cpu,
  146. .sync_single_for_device = xen_swiotlb_sync_single_for_device,
  147. .sync_sg_for_cpu = xen_swiotlb_sync_sg_for_cpu,
  148. .sync_sg_for_device = xen_swiotlb_sync_sg_for_device,
  149. .map_sg = xen_swiotlb_map_sg_attrs,
  150. .unmap_sg = xen_swiotlb_unmap_sg_attrs,
  151. .map_page = xen_swiotlb_map_page,
  152. .unmap_page = xen_swiotlb_unmap_page,
  153. .dma_supported = xen_swiotlb_dma_supported,
  154. .set_dma_mask = xen_swiotlb_set_dma_mask,
  155. };
  156. int __init xen_mm_init(void)
  157. {
  158. struct gnttab_cache_flush cflush;
  159. if (!xen_initial_domain())
  160. return 0;
  161. xen_swiotlb_init(1, false);
  162. xen_dma_ops = &xen_swiotlb_dma_ops;
  163. cflush.op = 0;
  164. cflush.a.dev_bus_addr = 0;
  165. cflush.offset = 0;
  166. cflush.length = 0;
  167. if (HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1) != -ENOSYS)
  168. hypercall_cflush = true;
  169. return 0;
  170. }
  171. arch_initcall(xen_mm_init);