1cf65a0f6SChristoph Hellwig // SPDX-License-Identifier: GPL-2.0 2cf65a0f6SChristoph Hellwig /* 3cf65a0f6SChristoph Hellwig * arch-independent dma-mapping routines 4cf65a0f6SChristoph Hellwig * 5cf65a0f6SChristoph Hellwig * Copyright (c) 2006 SUSE Linux Products GmbH 6cf65a0f6SChristoph Hellwig * Copyright (c) 2006 Tejun Heo <teheo@suse.de> 7cf65a0f6SChristoph Hellwig */ 805887cb6SChristoph Hellwig #include <linux/memblock.h> /* for max_pfn */ 9cf65a0f6SChristoph Hellwig #include <linux/acpi.h> 10356da6d0SChristoph Hellwig #include <linux/dma-direct.h> 1158b04406SChristoph Hellwig #include <linux/dma-noncoherent.h> 12cf65a0f6SChristoph Hellwig #include <linux/export.h> 13cf65a0f6SChristoph Hellwig #include <linux/gfp.h> 14cf65a0f6SChristoph Hellwig #include <linux/of_device.h> 15cf65a0f6SChristoph Hellwig #include <linux/slab.h> 16cf65a0f6SChristoph Hellwig #include <linux/vmalloc.h> 17cf65a0f6SChristoph Hellwig 18cf65a0f6SChristoph Hellwig /* 19cf65a0f6SChristoph Hellwig * Managed DMA API 20cf65a0f6SChristoph Hellwig */ 21cf65a0f6SChristoph Hellwig struct dma_devres { 22cf65a0f6SChristoph Hellwig size_t size; 23cf65a0f6SChristoph Hellwig void *vaddr; 24cf65a0f6SChristoph Hellwig dma_addr_t dma_handle; 25cf65a0f6SChristoph Hellwig unsigned long attrs; 26cf65a0f6SChristoph Hellwig }; 27cf65a0f6SChristoph Hellwig 28cf65a0f6SChristoph Hellwig static void dmam_release(struct device *dev, void *res) 29cf65a0f6SChristoph Hellwig { 30cf65a0f6SChristoph Hellwig struct dma_devres *this = res; 31cf65a0f6SChristoph Hellwig 32cf65a0f6SChristoph Hellwig dma_free_attrs(dev, this->size, this->vaddr, this->dma_handle, 33cf65a0f6SChristoph Hellwig this->attrs); 34cf65a0f6SChristoph Hellwig } 35cf65a0f6SChristoph Hellwig 36cf65a0f6SChristoph Hellwig static int dmam_match(struct device *dev, void *res, void *match_data) 37cf65a0f6SChristoph Hellwig { 38cf65a0f6SChristoph Hellwig struct dma_devres *this = res, *match = match_data; 39cf65a0f6SChristoph Hellwig 40cf65a0f6SChristoph Hellwig if (this->vaddr == match->vaddr) { 41cf65a0f6SChristoph Hellwig WARN_ON(this->size != match->size || 42cf65a0f6SChristoph Hellwig this->dma_handle != match->dma_handle); 43cf65a0f6SChristoph Hellwig return 1; 44cf65a0f6SChristoph Hellwig } 45cf65a0f6SChristoph Hellwig return 0; 46cf65a0f6SChristoph Hellwig } 47cf65a0f6SChristoph Hellwig 48cf65a0f6SChristoph Hellwig /** 49cf65a0f6SChristoph Hellwig * dmam_free_coherent - Managed dma_free_coherent() 50cf65a0f6SChristoph Hellwig * @dev: Device to free coherent memory for 51cf65a0f6SChristoph Hellwig * @size: Size of allocation 52cf65a0f6SChristoph Hellwig * @vaddr: Virtual address of the memory to free 53cf65a0f6SChristoph Hellwig * @dma_handle: DMA handle of the memory to free 54cf65a0f6SChristoph Hellwig * 55cf65a0f6SChristoph Hellwig * Managed dma_free_coherent(). 56cf65a0f6SChristoph Hellwig */ 57cf65a0f6SChristoph Hellwig void dmam_free_coherent(struct device *dev, size_t size, void *vaddr, 58cf65a0f6SChristoph Hellwig dma_addr_t dma_handle) 59cf65a0f6SChristoph Hellwig { 60cf65a0f6SChristoph Hellwig struct dma_devres match_data = { size, vaddr, dma_handle }; 61cf65a0f6SChristoph Hellwig 62cf65a0f6SChristoph Hellwig dma_free_coherent(dev, size, vaddr, dma_handle); 63cf65a0f6SChristoph Hellwig WARN_ON(devres_destroy(dev, dmam_release, dmam_match, &match_data)); 64cf65a0f6SChristoph Hellwig } 65cf65a0f6SChristoph Hellwig EXPORT_SYMBOL(dmam_free_coherent); 66cf65a0f6SChristoph Hellwig 67cf65a0f6SChristoph Hellwig /** 68cf65a0f6SChristoph Hellwig * dmam_alloc_attrs - Managed dma_alloc_attrs() 69cf65a0f6SChristoph Hellwig * @dev: Device to allocate non_coherent memory for 70cf65a0f6SChristoph Hellwig * @size: Size of allocation 71cf65a0f6SChristoph Hellwig * @dma_handle: Out argument for allocated DMA handle 72cf65a0f6SChristoph Hellwig * @gfp: Allocation flags 73cf65a0f6SChristoph Hellwig * @attrs: Flags in the DMA_ATTR_* namespace. 74cf65a0f6SChristoph Hellwig * 75cf65a0f6SChristoph Hellwig * Managed dma_alloc_attrs(). Memory allocated using this function will be 76cf65a0f6SChristoph Hellwig * automatically released on driver detach. 77cf65a0f6SChristoph Hellwig * 78cf65a0f6SChristoph Hellwig * RETURNS: 79cf65a0f6SChristoph Hellwig * Pointer to allocated memory on success, NULL on failure. 80cf65a0f6SChristoph Hellwig */ 81cf65a0f6SChristoph Hellwig void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle, 82cf65a0f6SChristoph Hellwig gfp_t gfp, unsigned long attrs) 83cf65a0f6SChristoph Hellwig { 84cf65a0f6SChristoph Hellwig struct dma_devres *dr; 85cf65a0f6SChristoph Hellwig void *vaddr; 86cf65a0f6SChristoph Hellwig 87cf65a0f6SChristoph Hellwig dr = devres_alloc(dmam_release, sizeof(*dr), gfp); 88cf65a0f6SChristoph Hellwig if (!dr) 89cf65a0f6SChristoph Hellwig return NULL; 90cf65a0f6SChristoph Hellwig 91cf65a0f6SChristoph Hellwig vaddr = dma_alloc_attrs(dev, size, dma_handle, gfp, attrs); 92cf65a0f6SChristoph Hellwig if (!vaddr) { 93cf65a0f6SChristoph Hellwig devres_free(dr); 94cf65a0f6SChristoph Hellwig return NULL; 95cf65a0f6SChristoph Hellwig } 96cf65a0f6SChristoph Hellwig 97cf65a0f6SChristoph Hellwig dr->vaddr = vaddr; 98cf65a0f6SChristoph Hellwig dr->dma_handle = *dma_handle; 99cf65a0f6SChristoph Hellwig dr->size = size; 100cf65a0f6SChristoph Hellwig dr->attrs = attrs; 101cf65a0f6SChristoph Hellwig 102cf65a0f6SChristoph Hellwig devres_add(dev, dr); 103cf65a0f6SChristoph Hellwig 104cf65a0f6SChristoph Hellwig return vaddr; 105cf65a0f6SChristoph Hellwig } 106cf65a0f6SChristoph Hellwig EXPORT_SYMBOL(dmam_alloc_attrs); 107cf65a0f6SChristoph Hellwig 108cf65a0f6SChristoph Hellwig /* 109cf65a0f6SChristoph Hellwig * Create scatter-list for the already allocated DMA buffer. 110cf65a0f6SChristoph Hellwig */ 111cf65a0f6SChristoph Hellwig int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt, 1129406a49fSChristoph Hellwig void *cpu_addr, dma_addr_t dma_addr, size_t size, 1139406a49fSChristoph Hellwig unsigned long attrs) 114cf65a0f6SChristoph Hellwig { 1159406a49fSChristoph Hellwig struct page *page; 116cf65a0f6SChristoph Hellwig int ret; 117cf65a0f6SChristoph Hellwig 1189406a49fSChristoph Hellwig if (!dev_is_dma_coherent(dev)) { 1199406a49fSChristoph Hellwig if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_COHERENT_TO_PFN)) 1209406a49fSChristoph Hellwig return -ENXIO; 121cf65a0f6SChristoph Hellwig 1229406a49fSChristoph Hellwig page = pfn_to_page(arch_dma_coherent_to_pfn(dev, cpu_addr, 1239406a49fSChristoph Hellwig dma_addr)); 1249406a49fSChristoph Hellwig } else { 1259406a49fSChristoph Hellwig page = virt_to_page(cpu_addr); 1269406a49fSChristoph Hellwig } 1279406a49fSChristoph Hellwig 1289406a49fSChristoph Hellwig ret = sg_alloc_table(sgt, 1, GFP_KERNEL); 1299406a49fSChristoph Hellwig if (!ret) 130cf65a0f6SChristoph Hellwig sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0); 1319406a49fSChristoph Hellwig return ret; 132cf65a0f6SChristoph Hellwig } 1337249c1a5SChristoph Hellwig 1347249c1a5SChristoph Hellwig int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, 1357249c1a5SChristoph Hellwig void *cpu_addr, dma_addr_t dma_addr, size_t size, 1367249c1a5SChristoph Hellwig unsigned long attrs) 1377249c1a5SChristoph Hellwig { 1387249c1a5SChristoph Hellwig const struct dma_map_ops *ops = get_dma_ops(dev); 139356da6d0SChristoph Hellwig 140356da6d0SChristoph Hellwig if (!dma_is_direct(ops) && ops->get_sgtable) 1417249c1a5SChristoph Hellwig return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size, 1427249c1a5SChristoph Hellwig attrs); 1437249c1a5SChristoph Hellwig return dma_common_get_sgtable(dev, sgt, cpu_addr, dma_addr, size, 1447249c1a5SChristoph Hellwig attrs); 1457249c1a5SChristoph Hellwig } 1467249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_get_sgtable_attrs); 147cf65a0f6SChristoph Hellwig 148cf65a0f6SChristoph Hellwig /* 149cf65a0f6SChristoph Hellwig * Create userspace mapping for the DMA-coherent memory. 150cf65a0f6SChristoph Hellwig */ 151cf65a0f6SChristoph Hellwig int dma_common_mmap(struct device *dev, struct vm_area_struct *vma, 15258b04406SChristoph Hellwig void *cpu_addr, dma_addr_t dma_addr, size_t size, 15358b04406SChristoph Hellwig unsigned long attrs) 154cf65a0f6SChristoph Hellwig { 155cf65a0f6SChristoph Hellwig #ifndef CONFIG_ARCH_NO_COHERENT_DMA_MMAP 156cf65a0f6SChristoph Hellwig unsigned long user_count = vma_pages(vma); 157cf65a0f6SChristoph Hellwig unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT; 158cf65a0f6SChristoph Hellwig unsigned long off = vma->vm_pgoff; 15958b04406SChristoph Hellwig unsigned long pfn; 16058b04406SChristoph Hellwig int ret = -ENXIO; 161cf65a0f6SChristoph Hellwig 16258b04406SChristoph Hellwig vma->vm_page_prot = arch_dma_mmap_pgprot(dev, vma->vm_page_prot, attrs); 163cf65a0f6SChristoph Hellwig 164cf65a0f6SChristoph Hellwig if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret)) 165cf65a0f6SChristoph Hellwig return ret; 166cf65a0f6SChristoph Hellwig 16758b04406SChristoph Hellwig if (off >= count || user_count > count - off) 16858b04406SChristoph Hellwig return -ENXIO; 169cf65a0f6SChristoph Hellwig 17058b04406SChristoph Hellwig if (!dev_is_dma_coherent(dev)) { 17158b04406SChristoph Hellwig if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_COHERENT_TO_PFN)) 17258b04406SChristoph Hellwig return -ENXIO; 17358b04406SChristoph Hellwig pfn = arch_dma_coherent_to_pfn(dev, cpu_addr, dma_addr); 17458b04406SChristoph Hellwig } else { 17558b04406SChristoph Hellwig pfn = page_to_pfn(virt_to_page(cpu_addr)); 17658b04406SChristoph Hellwig } 17758b04406SChristoph Hellwig 17858b04406SChristoph Hellwig return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff, 17958b04406SChristoph Hellwig user_count << PAGE_SHIFT, vma->vm_page_prot); 18058b04406SChristoph Hellwig #else 18158b04406SChristoph Hellwig return -ENXIO; 18258b04406SChristoph Hellwig #endif /* !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */ 183cf65a0f6SChristoph Hellwig } 1847249c1a5SChristoph Hellwig 1857249c1a5SChristoph Hellwig /** 1867249c1a5SChristoph Hellwig * dma_mmap_attrs - map a coherent DMA allocation into user space 1877249c1a5SChristoph Hellwig * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices 1887249c1a5SChristoph Hellwig * @vma: vm_area_struct describing requested user mapping 1897249c1a5SChristoph Hellwig * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs 1907249c1a5SChristoph Hellwig * @dma_addr: device-view address returned from dma_alloc_attrs 1917249c1a5SChristoph Hellwig * @size: size of memory originally requested in dma_alloc_attrs 1927249c1a5SChristoph Hellwig * @attrs: attributes of mapping properties requested in dma_alloc_attrs 1937249c1a5SChristoph Hellwig * 1947249c1a5SChristoph Hellwig * Map a coherent DMA buffer previously allocated by dma_alloc_attrs into user 1957249c1a5SChristoph Hellwig * space. The coherent DMA buffer must not be freed by the driver until the 1967249c1a5SChristoph Hellwig * user space mapping has been released. 1977249c1a5SChristoph Hellwig */ 1987249c1a5SChristoph Hellwig int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, 1997249c1a5SChristoph Hellwig void *cpu_addr, dma_addr_t dma_addr, size_t size, 2007249c1a5SChristoph Hellwig unsigned long attrs) 2017249c1a5SChristoph Hellwig { 2027249c1a5SChristoph Hellwig const struct dma_map_ops *ops = get_dma_ops(dev); 203356da6d0SChristoph Hellwig 204356da6d0SChristoph Hellwig if (!dma_is_direct(ops) && ops->mmap) 2057249c1a5SChristoph Hellwig return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs); 2067249c1a5SChristoph Hellwig return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size, attrs); 2077249c1a5SChristoph Hellwig } 2087249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_mmap_attrs); 20905887cb6SChristoph Hellwig 21005887cb6SChristoph Hellwig static u64 dma_default_get_required_mask(struct device *dev) 21105887cb6SChristoph Hellwig { 21205887cb6SChristoph Hellwig u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT); 21305887cb6SChristoph Hellwig u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT)); 21405887cb6SChristoph Hellwig u64 mask; 21505887cb6SChristoph Hellwig 21605887cb6SChristoph Hellwig if (!high_totalram) { 21705887cb6SChristoph Hellwig /* convert to mask just covering totalram */ 21805887cb6SChristoph Hellwig low_totalram = (1 << (fls(low_totalram) - 1)); 21905887cb6SChristoph Hellwig low_totalram += low_totalram - 1; 22005887cb6SChristoph Hellwig mask = low_totalram; 22105887cb6SChristoph Hellwig } else { 22205887cb6SChristoph Hellwig high_totalram = (1 << (fls(high_totalram) - 1)); 22305887cb6SChristoph Hellwig high_totalram += high_totalram - 1; 22405887cb6SChristoph Hellwig mask = (((u64)high_totalram) << 32) + 0xffffffff; 22505887cb6SChristoph Hellwig } 22605887cb6SChristoph Hellwig return mask; 22705887cb6SChristoph Hellwig } 22805887cb6SChristoph Hellwig 22905887cb6SChristoph Hellwig u64 dma_get_required_mask(struct device *dev) 23005887cb6SChristoph Hellwig { 23105887cb6SChristoph Hellwig const struct dma_map_ops *ops = get_dma_ops(dev); 23205887cb6SChristoph Hellwig 233356da6d0SChristoph Hellwig if (dma_is_direct(ops)) 234356da6d0SChristoph Hellwig return dma_direct_get_required_mask(dev); 23505887cb6SChristoph Hellwig if (ops->get_required_mask) 23605887cb6SChristoph Hellwig return ops->get_required_mask(dev); 23705887cb6SChristoph Hellwig return dma_default_get_required_mask(dev); 23805887cb6SChristoph Hellwig } 23905887cb6SChristoph Hellwig EXPORT_SYMBOL_GPL(dma_get_required_mask); 24005887cb6SChristoph Hellwig 2417249c1a5SChristoph Hellwig void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle, 2427249c1a5SChristoph Hellwig gfp_t flag, unsigned long attrs) 2437249c1a5SChristoph Hellwig { 2447249c1a5SChristoph Hellwig const struct dma_map_ops *ops = get_dma_ops(dev); 2457249c1a5SChristoph Hellwig void *cpu_addr; 2467249c1a5SChristoph Hellwig 247148a97d5SDan Carpenter WARN_ON_ONCE(!dev->coherent_dma_mask); 2487249c1a5SChristoph Hellwig 2497249c1a5SChristoph Hellwig if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr)) 2507249c1a5SChristoph Hellwig return cpu_addr; 2517249c1a5SChristoph Hellwig 2527249c1a5SChristoph Hellwig /* let the implementation decide on the zone to allocate from: */ 2537249c1a5SChristoph Hellwig flag &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM); 2547249c1a5SChristoph Hellwig 255356da6d0SChristoph Hellwig if (dma_is_direct(ops)) 256356da6d0SChristoph Hellwig cpu_addr = dma_direct_alloc(dev, size, dma_handle, flag, attrs); 257356da6d0SChristoph Hellwig else if (ops->alloc) 258356da6d0SChristoph Hellwig cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs); 259356da6d0SChristoph Hellwig else 2607249c1a5SChristoph Hellwig return NULL; 2617249c1a5SChristoph Hellwig 2627249c1a5SChristoph Hellwig debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr); 2637249c1a5SChristoph Hellwig return cpu_addr; 2647249c1a5SChristoph Hellwig } 2657249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_alloc_attrs); 2667249c1a5SChristoph Hellwig 2677249c1a5SChristoph Hellwig void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr, 2687249c1a5SChristoph Hellwig dma_addr_t dma_handle, unsigned long attrs) 2697249c1a5SChristoph Hellwig { 2707249c1a5SChristoph Hellwig const struct dma_map_ops *ops = get_dma_ops(dev); 2717249c1a5SChristoph Hellwig 2727249c1a5SChristoph Hellwig if (dma_release_from_dev_coherent(dev, get_order(size), cpu_addr)) 2737249c1a5SChristoph Hellwig return; 2747249c1a5SChristoph Hellwig /* 2757249c1a5SChristoph Hellwig * On non-coherent platforms which implement DMA-coherent buffers via 2767249c1a5SChristoph Hellwig * non-cacheable remaps, ops->free() may call vunmap(). Thus getting 2777249c1a5SChristoph Hellwig * this far in IRQ context is a) at risk of a BUG_ON() or trying to 2787249c1a5SChristoph Hellwig * sleep on some machines, and b) an indication that the driver is 2797249c1a5SChristoph Hellwig * probably misusing the coherent API anyway. 2807249c1a5SChristoph Hellwig */ 2817249c1a5SChristoph Hellwig WARN_ON(irqs_disabled()); 2827249c1a5SChristoph Hellwig 283356da6d0SChristoph Hellwig if (!cpu_addr) 2847249c1a5SChristoph Hellwig return; 2857249c1a5SChristoph Hellwig 2867249c1a5SChristoph Hellwig debug_dma_free_coherent(dev, size, cpu_addr, dma_handle); 287356da6d0SChristoph Hellwig if (dma_is_direct(ops)) 288356da6d0SChristoph Hellwig dma_direct_free(dev, size, cpu_addr, dma_handle, attrs); 289356da6d0SChristoph Hellwig else if (ops->free) 2907249c1a5SChristoph Hellwig ops->free(dev, size, cpu_addr, dma_handle, attrs); 2917249c1a5SChristoph Hellwig } 2927249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_free_attrs); 2937249c1a5SChristoph Hellwig 2947249c1a5SChristoph Hellwig static inline void dma_check_mask(struct device *dev, u64 mask) 2957249c1a5SChristoph Hellwig { 2967249c1a5SChristoph Hellwig if (sme_active() && (mask < (((u64)sme_get_me_mask() << 1) - 1))) 2977249c1a5SChristoph Hellwig dev_warn(dev, "SME is active, device will require DMA bounce buffers\n"); 2987249c1a5SChristoph Hellwig } 2997249c1a5SChristoph Hellwig 3007249c1a5SChristoph Hellwig int dma_supported(struct device *dev, u64 mask) 3017249c1a5SChristoph Hellwig { 3027249c1a5SChristoph Hellwig const struct dma_map_ops *ops = get_dma_ops(dev); 3037249c1a5SChristoph Hellwig 304356da6d0SChristoph Hellwig if (dma_is_direct(ops)) 305356da6d0SChristoph Hellwig return dma_direct_supported(dev, mask); 3068b1cce9fSThierry Reding if (!ops->dma_supported) 3077249c1a5SChristoph Hellwig return 1; 3087249c1a5SChristoph Hellwig return ops->dma_supported(dev, mask); 3097249c1a5SChristoph Hellwig } 3107249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_supported); 3117249c1a5SChristoph Hellwig 31211ddce15SChristoph Hellwig #ifdef CONFIG_ARCH_HAS_DMA_SET_MASK 31311ddce15SChristoph Hellwig void arch_dma_set_mask(struct device *dev, u64 mask); 31411ddce15SChristoph Hellwig #else 31511ddce15SChristoph Hellwig #define arch_dma_set_mask(dev, mask) do { } while (0) 31611ddce15SChristoph Hellwig #endif 31711ddce15SChristoph Hellwig 3187249c1a5SChristoph Hellwig int dma_set_mask(struct device *dev, u64 mask) 3197249c1a5SChristoph Hellwig { 320*4a54d16fSChristoph Hellwig /* 321*4a54d16fSChristoph Hellwig * Truncate the mask to the actually supported dma_addr_t width to 322*4a54d16fSChristoph Hellwig * avoid generating unsupportable addresses. 323*4a54d16fSChristoph Hellwig */ 324*4a54d16fSChristoph Hellwig mask = (dma_addr_t)mask; 325*4a54d16fSChristoph Hellwig 3267249c1a5SChristoph Hellwig if (!dev->dma_mask || !dma_supported(dev, mask)) 3277249c1a5SChristoph Hellwig return -EIO; 3287249c1a5SChristoph Hellwig 32911ddce15SChristoph Hellwig arch_dma_set_mask(dev, mask); 3307249c1a5SChristoph Hellwig dma_check_mask(dev, mask); 3317249c1a5SChristoph Hellwig *dev->dma_mask = mask; 3327249c1a5SChristoph Hellwig return 0; 3337249c1a5SChristoph Hellwig } 3347249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_set_mask); 3357249c1a5SChristoph Hellwig 3367249c1a5SChristoph Hellwig #ifndef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK 3377249c1a5SChristoph Hellwig int dma_set_coherent_mask(struct device *dev, u64 mask) 3387249c1a5SChristoph Hellwig { 339*4a54d16fSChristoph Hellwig /* 340*4a54d16fSChristoph Hellwig * Truncate the mask to the actually supported dma_addr_t width to 341*4a54d16fSChristoph Hellwig * avoid generating unsupportable addresses. 342*4a54d16fSChristoph Hellwig */ 343*4a54d16fSChristoph Hellwig mask = (dma_addr_t)mask; 344*4a54d16fSChristoph Hellwig 3457249c1a5SChristoph Hellwig if (!dma_supported(dev, mask)) 3467249c1a5SChristoph Hellwig return -EIO; 3477249c1a5SChristoph Hellwig 3487249c1a5SChristoph Hellwig dma_check_mask(dev, mask); 3497249c1a5SChristoph Hellwig dev->coherent_dma_mask = mask; 3507249c1a5SChristoph Hellwig return 0; 3517249c1a5SChristoph Hellwig } 3527249c1a5SChristoph Hellwig EXPORT_SYMBOL(dma_set_coherent_mask); 3537249c1a5SChristoph Hellwig #endif 3548ddbe594SChristoph Hellwig 3558ddbe594SChristoph Hellwig void dma_cache_sync(struct device *dev, void *vaddr, size_t size, 3568ddbe594SChristoph Hellwig enum dma_data_direction dir) 3578ddbe594SChristoph Hellwig { 3588ddbe594SChristoph Hellwig const struct dma_map_ops *ops = get_dma_ops(dev); 3598ddbe594SChristoph Hellwig 3608ddbe594SChristoph Hellwig BUG_ON(!valid_dma_direction(dir)); 361356da6d0SChristoph Hellwig 362356da6d0SChristoph Hellwig if (dma_is_direct(ops)) 363356da6d0SChristoph Hellwig arch_dma_cache_sync(dev, vaddr, size, dir); 364356da6d0SChristoph Hellwig else if (ops->cache_sync) 3658ddbe594SChristoph Hellwig ops->cache_sync(dev, vaddr, size, dir); 3668ddbe594SChristoph Hellwig } 3678ddbe594SChristoph Hellwig EXPORT_SYMBOL(dma_cache_sync); 368133d624bSJoerg Roedel 369133d624bSJoerg Roedel size_t dma_max_mapping_size(struct device *dev) 370133d624bSJoerg Roedel { 371133d624bSJoerg Roedel const struct dma_map_ops *ops = get_dma_ops(dev); 372133d624bSJoerg Roedel size_t size = SIZE_MAX; 373133d624bSJoerg Roedel 374133d624bSJoerg Roedel if (dma_is_direct(ops)) 375133d624bSJoerg Roedel size = dma_direct_max_mapping_size(dev); 376133d624bSJoerg Roedel else if (ops && ops->max_mapping_size) 377133d624bSJoerg Roedel size = ops->max_mapping_size(dev); 378133d624bSJoerg Roedel 379133d624bSJoerg Roedel return size; 380133d624bSJoerg Roedel } 381133d624bSJoerg Roedel EXPORT_SYMBOL_GPL(dma_max_mapping_size); 382