0001
0002 #include <linux/cpu.h>
0003 #include <linux/dma-direct.h>
0004 #include <linux/dma-map-ops.h>
0005 #include <linux/gfp.h>
0006 #include <linux/highmem.h>
0007 #include <linux/export.h>
0008 #include <linux/memblock.h>
0009 #include <linux/of_address.h>
0010 #include <linux/slab.h>
0011 #include <linux/types.h>
0012 #include <linux/vmalloc.h>
0013 #include <linux/swiotlb.h>
0014
0015 #include <xen/xen.h>
0016 #include <xen/interface/grant_table.h>
0017 #include <xen/interface/memory.h>
0018 #include <xen/page.h>
0019 #include <xen/xen-ops.h>
0020 #include <xen/swiotlb-xen.h>
0021
0022 #include <asm/cacheflush.h>
0023 #include <asm/xen/hypercall.h>
0024 #include <asm/xen/interface.h>
0025
0026 static gfp_t xen_swiotlb_gfp(void)
0027 {
0028 phys_addr_t base;
0029 u64 i;
0030
0031 for_each_mem_range(i, &base, NULL) {
0032 if (base < (phys_addr_t)0xffffffff) {
0033 if (IS_ENABLED(CONFIG_ZONE_DMA32))
0034 return __GFP_DMA32;
0035 return __GFP_DMA;
0036 }
0037 }
0038
0039 return GFP_KERNEL;
0040 }
0041
0042 static bool hypercall_cflush = false;
0043
0044
0045 static void dma_cache_maint(struct device *dev, dma_addr_t handle,
0046 size_t size, u32 op)
0047 {
0048 struct gnttab_cache_flush cflush;
0049
0050 cflush.offset = xen_offset_in_page(handle);
0051 cflush.op = op;
0052 handle &= XEN_PAGE_MASK;
0053
0054 do {
0055 cflush.a.dev_bus_addr = dma_to_phys(dev, handle);
0056
0057 if (size + cflush.offset > XEN_PAGE_SIZE)
0058 cflush.length = XEN_PAGE_SIZE - cflush.offset;
0059 else
0060 cflush.length = size;
0061
0062 HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1);
0063
0064 cflush.offset = 0;
0065 handle += cflush.length;
0066 size -= cflush.length;
0067 } while (size);
0068 }
0069
0070
0071
0072
0073
0074
0075
0076
0077 void xen_dma_sync_for_cpu(struct device *dev, dma_addr_t handle,
0078 size_t size, enum dma_data_direction dir)
0079 {
0080 if (dir != DMA_TO_DEVICE)
0081 dma_cache_maint(dev, handle, size, GNTTAB_CACHE_INVAL);
0082 }
0083
0084 void xen_dma_sync_for_device(struct device *dev, dma_addr_t handle,
0085 size_t size, enum dma_data_direction dir)
0086 {
0087 if (dir == DMA_FROM_DEVICE)
0088 dma_cache_maint(dev, handle, size, GNTTAB_CACHE_INVAL);
0089 else
0090 dma_cache_maint(dev, handle, size, GNTTAB_CACHE_CLEAN);
0091 }
0092
0093 bool xen_arch_need_swiotlb(struct device *dev,
0094 phys_addr_t phys,
0095 dma_addr_t dev_addr)
0096 {
0097 unsigned int xen_pfn = XEN_PFN_DOWN(phys);
0098 unsigned int bfn = XEN_PFN_DOWN(dma_to_phys(dev, dev_addr));
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115 return (!hypercall_cflush && (xen_pfn != bfn) &&
0116 !dev_is_dma_coherent(dev));
0117 }
0118
0119 static int __init xen_mm_init(void)
0120 {
0121 struct gnttab_cache_flush cflush;
0122 int rc;
0123
0124 if (!xen_swiotlb_detect())
0125 return 0;
0126
0127
0128 if (!io_tlb_default_mem.nslabs) {
0129 rc = swiotlb_init_late(swiotlb_size_or_default(),
0130 xen_swiotlb_gfp(), NULL);
0131 if (rc < 0)
0132 return rc;
0133 }
0134
0135 cflush.op = 0;
0136 cflush.a.dev_bus_addr = 0;
0137 cflush.offset = 0;
0138 cflush.length = 0;
0139 if (HYPERVISOR_grant_table_op(GNTTABOP_cache_flush, &cflush, 1) != -ENOSYS)
0140 hypercall_cflush = true;
0141 return 0;
0142 }
0143 arch_initcall(xen_mm_init);