mm.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. unsigned long xen_get_swiotlb_free_pages(unsigned int order)
  24. {
  25. phys_addr_t base;
  26. gfp_t flags = __GFP_NOWARN|__GFP_KSWAPD_RECLAIM;
  27. u64 i;
  28. for_each_mem_range(i, &base, NULL) {
  29. if (base < (phys_addr_t)0xffffffff) {
  30. if (IS_ENABLED(CONFIG_ZONE_DMA32))
  31. flags |= __GFP_DMA32;
  32. else
  33. flags |= __GFP_DMA;
  34. break;
  35. }
  36. }
  37. return __get_free_pages(flags, order);
  38. }
  39. static bool hypercall_cflush = false;
  40. /* buffers in highmem or foreign pages cannot cross page boundaries */
  41. static void dma_cache_maint(struct device *dev, dma_addr_t handle,
  42. size_t size, u32 op)
  43. {
  44. struct gnttab_cache_flush cflush;
  45. cflush.offset = xen_offset_in_page(handle);
  46. cflush.op = op;
  47. handle &= XEN_PAGE_MASK;
  48. do {
  49. cflush.a.dev_bus_addr = dma_to_phys(dev, handle);
  50. if (size + cflush.offset > XEN_PAGE_SIZE)
  51. cflush.length = XEN_PAGE_SIZE - cflush.offset;
  52. else
  53. cflush.length = size;
  54. HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1);
  55. cflush.offset = 0;
  56. handle += cflush.length;
  57. size -= cflush.length;
  58. } while (size);
  59. }
  60. /*
  61. * Dom0 is mapped 1:1, and while the Linux page can span across multiple Xen
  62. * pages, it is not possible for it to contain a mix of local and foreign Xen
  63. * pages. Calling pfn_valid on a foreign mfn will always return false, so if
  64. * pfn_valid returns true the pages is local and we can use the native
  65. * dma-direct functions, otherwise we call the Xen specific version.
  66. */
  67. void xen_dma_sync_for_cpu(struct device *dev, dma_addr_t handle,
  68. size_t size, enum dma_data_direction dir)
  69. {
  70. if (dir != DMA_TO_DEVICE)
  71. dma_cache_maint(dev, handle, size, GNTTAB_CACHE_INVAL);
  72. }
  73. void xen_dma_sync_for_device(struct device *dev, dma_addr_t handle,
  74. size_t size, enum dma_data_direction dir)
  75. {
  76. if (dir == DMA_FROM_DEVICE)
  77. dma_cache_maint(dev, handle, size, GNTTAB_CACHE_INVAL);
  78. else
  79. dma_cache_maint(dev, handle, size, GNTTAB_CACHE_CLEAN);
  80. }
  81. bool xen_arch_need_swiotlb(struct device *dev,
  82. phys_addr_t phys,
  83. dma_addr_t dev_addr)
  84. {
  85. unsigned int xen_pfn = XEN_PFN_DOWN(phys);
  86. unsigned int bfn = XEN_PFN_DOWN(dma_to_phys(dev, dev_addr));
  87. /*
  88. * The swiotlb buffer should be used if
  89. * - Xen doesn't have the cache flush hypercall
  90. * - The Linux page refers to foreign memory
  91. * - The device doesn't support coherent DMA request
  92. *
  93. * The Linux page may be spanned acrros multiple Xen page, although
  94. * it's not possible to have a mix of local and foreign Xen page.
  95. * Furthermore, range_straddles_page_boundary is already checking
  96. * if buffer is physically contiguous in the host RAM.
  97. *
  98. * Therefore we only need to check the first Xen page to know if we
  99. * require a bounce buffer because the device doesn't support coherent
  100. * memory and we are not able to flush the cache.
  101. */
  102. return (!hypercall_cflush && (xen_pfn != bfn) &&
  103. !dev_is_dma_coherent(dev));
  104. }
  105. int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order,
  106. unsigned int address_bits,
  107. dma_addr_t *dma_handle)
  108. {
  109. if (!xen_initial_domain())
  110. return -EINVAL;
  111. /* we assume that dom0 is mapped 1:1 for now */
  112. *dma_handle = pstart;
  113. return 0;
  114. }
  115. void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order)
  116. {
  117. return;
  118. }
  119. static int __init xen_mm_init(void)
  120. {
  121. struct gnttab_cache_flush cflush;
  122. if (!xen_initial_domain())
  123. return 0;
  124. xen_swiotlb_init(1, false);
  125. cflush.op = 0;
  126. cflush.a.dev_bus_addr = 0;
  127. cflush.offset = 0;
  128. cflush.length = 0;
  129. if (HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1) != -ENOSYS)
  130. hypercall_cflush = true;
  131. return 0;
  132. }
  133. arch_initcall(xen_mm_init);