mm.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/cpu.h>
  3. #include <linux/dma-direct.h>
  4. #include <linux/dma-map-ops.h>
  5. #include <linux/gfp.h>
  6. #include <linux/highmem.h>
  7. #include <linux/export.h>
  8. #include <linux/memblock.h>
  9. #include <linux/of_address.h>
  10. #include <linux/slab.h>
  11. #include <linux/types.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/xen-ops.h>
  19. #include <xen/swiotlb-xen.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/xen/hypercall.h>
  22. #include <asm/xen/interface.h>
  23. static gfp_t xen_swiotlb_gfp(void)
  24. {
  25. phys_addr_t base;
  26. u64 i;
  27. for_each_mem_range(i, &base, NULL) {
  28. if (base < (phys_addr_t)0xffffffff) {
  29. if (IS_ENABLED(CONFIG_ZONE_DMA32))
  30. return __GFP_DMA32;
  31. return __GFP_DMA;
  32. }
  33. }
  34. return GFP_KERNEL;
  35. }
  36. static bool hypercall_cflush = false;
  37. /* buffers in highmem or foreign pages cannot cross page boundaries */
  38. static void dma_cache_maint(struct device *dev, dma_addr_t handle,
  39. size_t size, u32 op)
  40. {
  41. struct gnttab_cache_flush cflush;
  42. cflush.offset = xen_offset_in_page(handle);
  43. cflush.op = op;
  44. handle &= XEN_PAGE_MASK;
  45. do {
  46. cflush.a.dev_bus_addr = dma_to_phys(dev, handle);
  47. if (size + cflush.offset > XEN_PAGE_SIZE)
  48. cflush.length = XEN_PAGE_SIZE - cflush.offset;
  49. else
  50. cflush.length = size;
  51. HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1);
  52. cflush.offset = 0;
  53. handle += cflush.length;
  54. size -= cflush.length;
  55. } while (size);
  56. }
  57. /*
  58. * Dom0 is mapped 1:1, and while the Linux page can span across multiple Xen
  59. * pages, it is not possible for it to contain a mix of local and foreign Xen
  60. * pages. Calling pfn_valid on a foreign mfn will always return false, so if
  61. * pfn_valid returns true the pages is local and we can use the native
  62. * dma-direct functions, otherwise we call the Xen specific version.
  63. */
  64. void xen_dma_sync_for_cpu(struct device *dev, dma_addr_t handle,
  65. size_t size, enum dma_data_direction dir)
  66. {
  67. if (dir != DMA_TO_DEVICE)
  68. dma_cache_maint(dev, handle, size, GNTTAB_CACHE_INVAL);
  69. }
  70. void xen_dma_sync_for_device(struct device *dev, dma_addr_t handle,
  71. size_t size, enum dma_data_direction dir)
  72. {
  73. if (dir == DMA_FROM_DEVICE)
  74. dma_cache_maint(dev, handle, size, GNTTAB_CACHE_INVAL);
  75. else
  76. dma_cache_maint(dev, handle, size, GNTTAB_CACHE_CLEAN);
  77. }
  78. bool xen_arch_need_swiotlb(struct device *dev,
  79. phys_addr_t phys,
  80. dma_addr_t dev_addr)
  81. {
  82. unsigned int xen_pfn = XEN_PFN_DOWN(phys);
  83. unsigned int bfn = XEN_PFN_DOWN(dma_to_phys(dev, dev_addr));
  84. /*
  85. * The swiotlb buffer should be used if
  86. * - Xen doesn't have the cache flush hypercall
  87. * - The Linux page refers to foreign memory
  88. * - The device doesn't support coherent DMA request
  89. *
  90. * The Linux page may be spanned acrros multiple Xen page, although
  91. * it's not possible to have a mix of local and foreign Xen page.
  92. * Furthermore, range_straddles_page_boundary is already checking
  93. * if buffer is physically contiguous in the host RAM.
  94. *
  95. * Therefore we only need to check the first Xen page to know if we
  96. * require a bounce buffer because the device doesn't support coherent
  97. * memory and we are not able to flush the cache.
  98. */
  99. return (!hypercall_cflush && (xen_pfn != bfn) &&
  100. !dev_is_dma_coherent(dev));
  101. }
  102. static int __init xen_mm_init(void)
  103. {
  104. struct gnttab_cache_flush cflush;
  105. int rc;
  106. if (!xen_swiotlb_detect())
  107. return 0;
  108. /* we can work with the default swiotlb */
  109. if (!io_tlb_default_mem.nslabs) {
  110. rc = swiotlb_init_late(swiotlb_size_or_default(),
  111. xen_swiotlb_gfp(), NULL);
  112. if (rc < 0)
  113. return rc;
  114. }
  115. cflush.op = 0;
  116. cflush.a.dev_bus_addr = 0;
  117. cflush.offset = 0;
  118. cflush.length = 0;
  119. if (HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1) != -ENOSYS)
  120. hypercall_cflush = true;
  121. return 0;
  122. }
  123. arch_initcall(xen_mm_init);