Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
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 /* buffers in highmem or foreign pages cannot cross page boundaries */
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  * Dom0 is mapped 1:1, and while the Linux page can span across multiple Xen
0072  * pages, it is not possible for it to contain a mix of local and foreign Xen
0073  * pages.  Calling pfn_valid on a foreign mfn will always return false, so if
0074  * pfn_valid returns true the pages is local and we can use the native
0075  * dma-direct functions, otherwise we call the Xen specific version.
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      * The swiotlb buffer should be used if
0102      *  - Xen doesn't have the cache flush hypercall
0103      *  - The Linux page refers to foreign memory
0104      *  - The device doesn't support coherent DMA request
0105      *
0106      * The Linux page may be spanned acrros multiple Xen page, although
0107      * it's not possible to have a mix of local and foreign Xen page.
0108      * Furthermore, range_straddles_page_boundary is already checking
0109      * if buffer is physically contiguous in the host RAM.
0110      *
0111      * Therefore we only need to check the first Xen page to know if we
0112      * require a bounce buffer because the device doesn't support coherent
0113      * memory and we are not able to flush the cache.
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     /* we can work with the default swiotlb */
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);