Lines Matching +full:device +full:- +full:addr

1 // SPDX-License-Identifier: GPL-2.0
3 * arch-independent dma-mapping routines
10 #include <linux/dma-map-ops.h>
13 #include <linux/iommu-dma.h>
40 static void dmam_release(struct device *dev, void *res) in dmam_release()
44 dma_free_attrs(dev, this->size, this->vaddr, this->dma_handle, in dmam_release()
45 this->attrs); in dmam_release()
48 static int dmam_match(struct device *dev, void *res, void *match_data) in dmam_match()
52 if (this->vaddr == match->vaddr) { in dmam_match()
53 WARN_ON(this->size != match->size || in dmam_match()
54 this->dma_handle != match->dma_handle); in dmam_match()
61 * dmam_free_coherent - Managed dma_free_coherent()
62 * @dev: Device to free coherent memory for
69 void dmam_free_coherent(struct device *dev, size_t size, void *vaddr, in dmam_free_coherent()
80 * dmam_alloc_attrs - Managed dma_alloc_attrs()
81 * @dev: Device to allocate non_coherent memory for
93 void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle, in dmam_alloc_attrs()
109 dr->vaddr = vaddr; in dmam_alloc_attrs()
110 dr->dma_handle = *dma_handle; in dmam_alloc_attrs()
111 dr->size = size; in dmam_alloc_attrs()
112 dr->attrs = attrs; in dmam_alloc_attrs()
120 static bool dma_go_direct(struct device *dev, dma_addr_t mask, in dma_go_direct()
130 if (dev->dma_ops_bypass) in dma_go_direct()
131 return min_not_zero(mask, dev->bus_dma_limit) >= in dma_go_direct()
143 static inline bool dma_alloc_direct(struct device *dev, in dma_alloc_direct()
146 return dma_go_direct(dev, dev->coherent_dma_mask, ops); in dma_alloc_direct()
149 static inline bool dma_map_direct(struct device *dev, in dma_map_direct()
152 return dma_go_direct(dev, *dev->dma_mask, ops); in dma_map_direct()
155 dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page, in dma_map_page_attrs()
160 dma_addr_t addr; in dma_map_page_attrs() local
164 if (WARN_ON_ONCE(!dev->dma_mask)) in dma_map_page_attrs()
169 addr = dma_direct_map_page(dev, page, offset, size, dir, attrs); in dma_map_page_attrs()
171 addr = iommu_dma_map_page(dev, page, offset, size, dir, attrs); in dma_map_page_attrs()
173 addr = ops->map_page(dev, page, offset, size, dir, attrs); in dma_map_page_attrs()
175 trace_dma_map_page(dev, page_to_phys(page) + offset, addr, size, dir, in dma_map_page_attrs()
177 debug_dma_map_page(dev, page, offset, size, dir, addr, attrs); in dma_map_page_attrs()
179 return addr; in dma_map_page_attrs()
183 void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr, size_t size, in dma_unmap_page_attrs() argument
190 arch_dma_unmap_page_direct(dev, addr + size)) in dma_unmap_page_attrs()
191 dma_direct_unmap_page(dev, addr, size, dir, attrs); in dma_unmap_page_attrs()
193 iommu_dma_unmap_page(dev, addr, size, dir, attrs); in dma_unmap_page_attrs()
195 ops->unmap_page(dev, addr, size, dir, attrs); in dma_unmap_page_attrs()
196 trace_dma_unmap_page(dev, addr, size, dir, attrs); in dma_unmap_page_attrs()
197 debug_dma_unmap_page(dev, addr, size, dir); in dma_unmap_page_attrs()
201 static int __dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, in __dma_map_sg_attrs()
209 if (WARN_ON_ONCE(!dev->dma_mask)) in __dma_map_sg_attrs()
218 ents = ops->map_sg(dev, sg, nents, dir, attrs); in __dma_map_sg_attrs()
224 } else if (WARN_ON_ONCE(ents != -EINVAL && ents != -ENOMEM && in __dma_map_sg_attrs()
225 ents != -EIO && ents != -EREMOTEIO)) { in __dma_map_sg_attrs()
226 return -EIO; in __dma_map_sg_attrs()
233 * dma_map_sg_attrs - Map the given buffer for DMA
234 * @dev: The device for which to perform the DMA operation
241 * nents segments for the @dir DMA operation by the @dev device.
249 unsigned int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, in dma_map_sg_attrs()
262 * dma_map_sgtable - Map the given buffer for DMA
263 * @dev: The device for which to perform the DMA operation
269 * object for the @dir DMA operation by the @dev device. After success, the
278 * -EINVAL An invalid argument, unaligned access or other error
280 * -ENOMEM Insufficient resources (like memory or IOVA space) to
282 * -EIO Legacy error code with an unknown meaning. eg. this is
285 * -EREMOTEIO The DMA device cannot access P2PDMA memory specified
288 int dma_map_sgtable(struct device *dev, struct sg_table *sgt, in dma_map_sgtable()
293 nents = __dma_map_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs); in dma_map_sgtable()
296 sgt->nents = nents; in dma_map_sgtable()
301 void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg, in dma_unmap_sg_attrs()
315 else if (ops->unmap_sg) in dma_unmap_sg_attrs()
316 ops->unmap_sg(dev, sg, nents, dir, attrs); in dma_unmap_sg_attrs()
320 dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr, in dma_map_resource()
324 dma_addr_t addr = DMA_MAPPING_ERROR; in dma_map_resource() local
328 if (WARN_ON_ONCE(!dev->dma_mask)) in dma_map_resource()
332 addr = dma_direct_map_resource(dev, phys_addr, size, dir, attrs); in dma_map_resource()
334 addr = iommu_dma_map_resource(dev, phys_addr, size, dir, attrs); in dma_map_resource()
335 else if (ops->map_resource) in dma_map_resource()
336 addr = ops->map_resource(dev, phys_addr, size, dir, attrs); in dma_map_resource()
338 trace_dma_map_resource(dev, phys_addr, addr, size, dir, attrs); in dma_map_resource()
339 debug_dma_map_resource(dev, phys_addr, size, dir, addr, attrs); in dma_map_resource()
340 return addr; in dma_map_resource()
344 void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size, in dma_unmap_resource() argument
353 iommu_dma_unmap_resource(dev, addr, size, dir, attrs); in dma_unmap_resource()
354 else if (ops->unmap_resource) in dma_unmap_resource()
355 ops->unmap_resource(dev, addr, size, dir, attrs); in dma_unmap_resource()
356 trace_dma_unmap_resource(dev, addr, size, dir, attrs); in dma_unmap_resource()
357 debug_dma_unmap_resource(dev, addr, size, dir); in dma_unmap_resource()
362 void __dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size, in __dma_sync_single_for_cpu() argument
369 dma_direct_sync_single_for_cpu(dev, addr, size, dir); in __dma_sync_single_for_cpu()
371 iommu_dma_sync_single_for_cpu(dev, addr, size, dir); in __dma_sync_single_for_cpu()
372 else if (ops->sync_single_for_cpu) in __dma_sync_single_for_cpu()
373 ops->sync_single_for_cpu(dev, addr, size, dir); in __dma_sync_single_for_cpu()
374 trace_dma_sync_single_for_cpu(dev, addr, size, dir); in __dma_sync_single_for_cpu()
375 debug_dma_sync_single_for_cpu(dev, addr, size, dir); in __dma_sync_single_for_cpu()
379 void __dma_sync_single_for_device(struct device *dev, dma_addr_t addr, in __dma_sync_single_for_device() argument
386 dma_direct_sync_single_for_device(dev, addr, size, dir); in __dma_sync_single_for_device()
388 iommu_dma_sync_single_for_device(dev, addr, size, dir); in __dma_sync_single_for_device()
389 else if (ops->sync_single_for_device) in __dma_sync_single_for_device()
390 ops->sync_single_for_device(dev, addr, size, dir); in __dma_sync_single_for_device()
391 trace_dma_sync_single_for_device(dev, addr, size, dir); in __dma_sync_single_for_device()
392 debug_dma_sync_single_for_device(dev, addr, size, dir); in __dma_sync_single_for_device()
396 void __dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, in __dma_sync_sg_for_cpu()
406 else if (ops->sync_sg_for_cpu) in __dma_sync_sg_for_cpu()
407 ops->sync_sg_for_cpu(dev, sg, nelems, dir); in __dma_sync_sg_for_cpu()
413 void __dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, in __dma_sync_sg_for_device()
423 else if (ops->sync_sg_for_device) in __dma_sync_sg_for_device()
424 ops->sync_sg_for_device(dev, sg, nelems, dir); in __dma_sync_sg_for_device()
430 bool __dma_need_sync(struct device *dev, dma_addr_t dma_addr) in __dma_need_sync()
445 static void dma_setup_need_sync(struct device *dev) in dma_setup_need_sync()
452 * mapping, if any. During the device initialization, it's in dma_setup_need_sync()
455 dev->dma_skip_sync = dev_is_dma_coherent(dev); in dma_setup_need_sync()
456 else if (!ops->sync_single_for_device && !ops->sync_single_for_cpu && in dma_setup_need_sync()
457 !ops->sync_sg_for_device && !ops->sync_sg_for_cpu) in dma_setup_need_sync()
462 dev->dma_skip_sync = true; in dma_setup_need_sync()
464 dev->dma_skip_sync = false; in dma_setup_need_sync()
467 static inline void dma_setup_need_sync(struct device *dev) { } in dma_setup_need_sync()
471 * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems
481 int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, in dma_get_sgtable_attrs()
493 if (!ops->get_sgtable) in dma_get_sgtable_attrs()
494 return -ENXIO; in dma_get_sgtable_attrs()
495 return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size, attrs); in dma_get_sgtable_attrs()
504 pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs) in dma_pgprot()
517 * dma_can_mmap - check if a given device supports dma_mmap_*
518 * @dev: device to check
523 bool dma_can_mmap(struct device *dev) in dma_can_mmap()
531 return ops->mmap != NULL; in dma_can_mmap()
536 * dma_mmap_attrs - map a coherent DMA allocation into user space
537 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
539 * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
540 * @dma_addr: device-view address returned from dma_alloc_attrs
548 int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, in dma_mmap_attrs()
560 if (!ops->mmap) in dma_mmap_attrs()
561 return -ENXIO; in dma_mmap_attrs()
562 return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs); in dma_mmap_attrs()
566 u64 dma_get_required_mask(struct device *dev) in dma_get_required_mask()
576 if (ops->get_required_mask) in dma_get_required_mask()
577 return ops->get_required_mask(dev); in dma_get_required_mask()
580 * We require every DMA ops implementation to at least support a 32-bit in dma_get_required_mask()
583 * actually report an optimal mask we default to 32-bit here as that in dma_get_required_mask()
591 void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle, in dma_alloc_attrs()
597 WARN_ON_ONCE(!dev->coherent_dma_mask); in dma_alloc_attrs()
617 else if (ops->alloc) in dma_alloc_attrs()
618 cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs); in dma_alloc_attrs()
628 void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr, in dma_free_attrs()
636 * On non-coherent platforms which implement DMA-coherent buffers via in dma_free_attrs()
637 * non-cacheable remaps, ops->free() may call vunmap(). Thus getting in dma_free_attrs()
653 else if (ops->free) in dma_free_attrs()
654 ops->free(dev, size, cpu_addr, dma_handle, attrs); in dma_free_attrs()
658 static struct page *__dma_alloc_pages(struct device *dev, size_t size, in __dma_alloc_pages()
663 if (WARN_ON_ONCE(!dev->coherent_dma_mask)) in __dma_alloc_pages()
675 if (!ops->alloc_pages_op) in __dma_alloc_pages()
677 return ops->alloc_pages_op(dev, size, dma_handle, dir, gfp); in __dma_alloc_pages()
680 struct page *dma_alloc_pages(struct device *dev, size_t size, in dma_alloc_pages()
694 static void __dma_free_pages(struct device *dev, size_t size, struct page *page, in __dma_free_pages()
704 else if (ops->free_pages) in __dma_free_pages()
705 ops->free_pages(dev, size, page, dma_handle, dir); in __dma_free_pages()
708 void dma_free_pages(struct device *dev, size_t size, struct page *page, in dma_free_pages()
717 int dma_mmap_pages(struct device *dev, struct vm_area_struct *vma, in dma_mmap_pages()
722 if (vma->vm_pgoff >= count || vma_pages(vma) > count - vma->vm_pgoff) in dma_mmap_pages()
723 return -ENXIO; in dma_mmap_pages()
724 return remap_pfn_range(vma, vma->vm_start, in dma_mmap_pages()
725 page_to_pfn(page) + vma->vm_pgoff, in dma_mmap_pages()
726 vma_pages(vma) << PAGE_SHIFT, vma->vm_page_prot); in dma_mmap_pages()
730 static struct sg_table *alloc_single_sgt(struct device *dev, size_t size, in alloc_single_sgt()
741 page = __dma_alloc_pages(dev, size, &sgt->sgl->dma_address, dir, gfp); in alloc_single_sgt()
744 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0); in alloc_single_sgt()
745 sg_dma_len(sgt->sgl) = sgt->sgl->length; in alloc_single_sgt()
754 struct sg_table *dma_alloc_noncontiguous(struct device *dev, size_t size, in dma_alloc_noncontiguous()
770 sgt->nents = 1; in dma_alloc_noncontiguous()
771 trace_dma_map_sg(dev, sgt->sgl, sgt->orig_nents, 1, dir, attrs); in dma_alloc_noncontiguous()
772 debug_dma_map_sg(dev, sgt->sgl, sgt->orig_nents, 1, dir, attrs); in dma_alloc_noncontiguous()
778 static void free_single_sgt(struct device *dev, size_t size, in free_single_sgt()
781 __dma_free_pages(dev, size, sg_page(sgt->sgl), sgt->sgl->dma_address, in free_single_sgt()
787 void dma_free_noncontiguous(struct device *dev, size_t size, in dma_free_noncontiguous()
790 trace_dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir, 0); in dma_free_noncontiguous()
791 debug_dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); in dma_free_noncontiguous()
800 void *dma_vmap_noncontiguous(struct device *dev, size_t size, in dma_vmap_noncontiguous()
807 return page_address(sg_page(sgt->sgl)); in dma_vmap_noncontiguous()
811 void dma_vunmap_noncontiguous(struct device *dev, void *vaddr) in dma_vunmap_noncontiguous()
818 int dma_mmap_noncontiguous(struct device *dev, struct vm_area_struct *vma, in dma_mmap_noncontiguous()
823 return dma_mmap_pages(dev, vma, size, sg_page(sgt->sgl)); in dma_mmap_noncontiguous()
827 static int dma_supported(struct device *dev, u64 mask) in dma_supported()
838 * ->dma_supported sets and clears the bypass flag, so ignore it here in dma_supported()
842 if (!ops->dma_supported) in dma_supported()
844 return ops->dma_supported(dev, mask); in dma_supported()
850 bool dma_pci_p2pdma_supported(struct device *dev) in dma_pci_p2pdma_supported()
857 * if the specific device is bypassing them. in dma_pci_p2pdma_supported()
865 int dma_set_mask(struct device *dev, u64 mask) in dma_set_mask()
873 if (!dev->dma_mask || !dma_supported(dev, mask)) in dma_set_mask()
874 return -EIO; in dma_set_mask()
877 *dev->dma_mask = mask; in dma_set_mask()
884 int dma_set_coherent_mask(struct device *dev, u64 mask) in dma_set_coherent_mask()
893 return -EIO; in dma_set_coherent_mask()
895 dev->coherent_dma_mask = mask; in dma_set_coherent_mask()
901 * dma_addressing_limited - return if the device is addressing limited
902 * @dev: device to check
908 bool dma_addressing_limited(struct device *dev) in dma_addressing_limited()
912 if (min_not_zero(dma_get_mask(dev), dev->bus_dma_limit) < in dma_addressing_limited()
922 size_t dma_max_mapping_size(struct device *dev) in dma_max_mapping_size()
931 else if (ops && ops->max_mapping_size) in dma_max_mapping_size()
932 size = ops->max_mapping_size(dev); in dma_max_mapping_size()
938 size_t dma_opt_mapping_size(struct device *dev) in dma_opt_mapping_size()
945 else if (ops && ops->opt_mapping_size) in dma_opt_mapping_size()
946 size = ops->opt_mapping_size(); in dma_opt_mapping_size()
952 unsigned long dma_get_merge_boundary(struct device *dev) in dma_get_merge_boundary()
959 if (!ops || !ops->get_merge_boundary) in dma_get_merge_boundary()
962 return ops->get_merge_boundary(dev); in dma_get_merge_boundary()