Skip to content

Commit 6f2dc3d

Browse files
committed
Merge tag 'dma-mapping-5.8-2' of git://git.infradead.org/users/hch/dma-mapping
Pull dma-mapping helpers from Christoph Hellwig: "These were in a separate stable branch so that various media and drm trees could pull the in for bug fixes, but looking at linux-next that hasn't actually happened yet. Still sending the APIs to you in the hope that these bug fixes get picked up for 5.8 in one way or another. Summary: - add DMA mapping helpers for struct sg_table (Marek Szyprowski)" * tag 'dma-mapping-5.8-2' of git://git.infradead.org/users/hch/dma-mapping: iommu: add generic helper for mapping sgtable objects scatterlist: add generic wrappers for iterating over sgtable objects dma-mapping: add generic helpers for mapping sgtable objects
2 parents 1ee18de + 48530d9 commit 6f2dc3d

3 files changed

Lines changed: 143 additions & 3 deletions

File tree

‎include/linux/dma-mapping.h‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,86 @@ static inline void dma_sync_single_range_for_device(struct device *dev,
609609
return dma_sync_single_for_device(dev, addr + offset, size, dir);
610610
}
611611

612+
/**
613+
* dma_map_sgtable - Map the given buffer for DMA
614+
* @dev: The device for which to perform the DMA operation
615+
* @sgt: The sg_table object describing the buffer
616+
* @dir: DMA direction
617+
* @attrs: Optional DMA attributes for the map operation
618+
*
619+
* Maps a buffer described by a scatterlist stored in the given sg_table
620+
* object for the @dir DMA operation by the @dev device. After success the
621+
* ownership for the buffer is transferred to the DMA domain. One has to
622+
* call dma_sync_sgtable_for_cpu() or dma_unmap_sgtable() to move the
623+
* ownership of the buffer back to the CPU domain before touching the
624+
* buffer by the CPU.
625+
*
626+
* Returns 0 on success or -EINVAL on error during mapping the buffer.
627+
*/
628+
static inline int dma_map_sgtable(struct device *dev, struct sg_table *sgt,
629+
enum dma_data_direction dir, unsigned long attrs)
630+
{
631+
int nents;
632+
633+
nents = dma_map_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs);
634+
if (nents <= 0)
635+
return -EINVAL;
636+
sgt->nents = nents;
637+
return 0;
638+
}
639+
640+
/**
641+
* dma_unmap_sgtable - Unmap the given buffer for DMA
642+
* @dev: The device for which to perform the DMA operation
643+
* @sgt: The sg_table object describing the buffer
644+
* @dir: DMA direction
645+
* @attrs: Optional DMA attributes for the unmap operation
646+
*
647+
* Unmaps a buffer described by a scatterlist stored in the given sg_table
648+
* object for the @dir DMA operation by the @dev device. After this function
649+
* the ownership of the buffer is transferred back to the CPU domain.
650+
*/
651+
static inline void dma_unmap_sgtable(struct device *dev, struct sg_table *sgt,
652+
enum dma_data_direction dir, unsigned long attrs)
653+
{
654+
dma_unmap_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs);
655+
}
656+
657+
/**
658+
* dma_sync_sgtable_for_cpu - Synchronize the given buffer for CPU access
659+
* @dev: The device for which to perform the DMA operation
660+
* @sgt: The sg_table object describing the buffer
661+
* @dir: DMA direction
662+
*
663+
* Performs the needed cache synchronization and moves the ownership of the
664+
* buffer back to the CPU domain, so it is safe to perform any access to it
665+
* by the CPU. Before doing any further DMA operations, one has to transfer
666+
* the ownership of the buffer back to the DMA domain by calling the
667+
* dma_sync_sgtable_for_device().
668+
*/
669+
static inline void dma_sync_sgtable_for_cpu(struct device *dev,
670+
struct sg_table *sgt, enum dma_data_direction dir)
671+
{
672+
dma_sync_sg_for_cpu(dev, sgt->sgl, sgt->orig_nents, dir);
673+
}
674+
675+
/**
676+
* dma_sync_sgtable_for_device - Synchronize the given buffer for DMA
677+
* @dev: The device for which to perform the DMA operation
678+
* @sgt: The sg_table object describing the buffer
679+
* @dir: DMA direction
680+
*
681+
* Performs the needed cache synchronization and moves the ownership of the
682+
* buffer back to the DMA domain, so it is safe to perform the DMA operation.
683+
* Once finished, one has to call dma_sync_sgtable_for_cpu() or
684+
* dma_unmap_sgtable().
685+
*/
686+
static inline void dma_sync_sgtable_for_device(struct device *dev,
687+
struct sg_table *sgt, enum dma_data_direction dir)
688+
{
689+
dma_sync_sg_for_device(dev, sgt->sgl, sgt->orig_nents, dir);
690+
}
691+
612692
#define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
613693
#define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
614694
#define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)

‎include/linux/iommu.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,22 @@ extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t io
466466
extern void iommu_set_fault_handler(struct iommu_domain *domain,
467467
iommu_fault_handler_t handler, void *token);
468468

469+
/**
470+
* iommu_map_sgtable - Map the given buffer to the IOMMU domain
471+
* @domain: The IOMMU domain to perform the mapping
472+
* @iova: The start address to map the buffer
473+
* @sgt: The sg_table object describing the buffer
474+
* @prot: IOMMU protection bits
475+
*
476+
* Creates a mapping at @iova for the buffer described by a scatterlist
477+
* stored in the given sg_table object in the provided IOMMU domain.
478+
*/
479+
static inline size_t iommu_map_sgtable(struct iommu_domain *domain,
480+
unsigned long iova, struct sg_table *sgt, int prot)
481+
{
482+
return iommu_map_sg(domain, iova, sgt->sgl, sgt->orig_nents, prot);
483+
}
484+
469485
extern void iommu_get_resv_regions(struct device *dev, struct list_head *list);
470486
extern void iommu_put_resv_regions(struct device *dev, struct list_head *list);
471487
extern void generic_iommu_put_resv_regions(struct device *dev,

‎include/linux/scatterlist.h‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
151151
#define for_each_sg(sglist, sg, nr, __i) \
152152
for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
153153

154+
/*
155+
* Loop over each sg element in the given sg_table object.
156+
*/
157+
#define for_each_sgtable_sg(sgt, sg, i) \
158+
for_each_sg(sgt->sgl, sg, sgt->orig_nents, i)
159+
160+
/*
161+
* Loop over each sg element in the given *DMA mapped* sg_table object.
162+
* Please use sg_dma_address(sg) and sg_dma_len(sg) to extract DMA addresses
163+
* of the each element.
164+
*/
165+
#define for_each_sgtable_dma_sg(sgt, sg, i) \
166+
for_each_sg(sgt->sgl, sg, sgt->nents, i)
167+
154168
/**
155169
* sg_chain - Chain two sglists together
156170
* @prv: First scatterlist
@@ -401,9 +415,10 @@ sg_page_iter_dma_address(struct sg_dma_page_iter *dma_iter)
401415
* @sglist: sglist to iterate over
402416
* @piter: page iterator to hold current page, sg, sg_pgoffset
403417
* @nents: maximum number of sg entries to iterate over
404-
* @pgoffset: starting page offset
418+
* @pgoffset: starting page offset (in pages)
405419
*
406420
* Callers may use sg_page_iter_page() to get each page pointer.
421+
* In each loop it operates on PAGE_SIZE unit.
407422
*/
408423
#define for_each_sg_page(sglist, piter, nents, pgoffset) \
409424
for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \
@@ -412,18 +427,47 @@ sg_page_iter_dma_address(struct sg_dma_page_iter *dma_iter)
412427
/**
413428
* for_each_sg_dma_page - iterate over the pages of the given sg list
414429
* @sglist: sglist to iterate over
415-
* @dma_iter: page iterator to hold current page
430+
* @dma_iter: DMA page iterator to hold current page
416431
* @dma_nents: maximum number of sg entries to iterate over, this is the value
417432
* returned from dma_map_sg
418-
* @pgoffset: starting page offset
433+
* @pgoffset: starting page offset (in pages)
419434
*
420435
* Callers may use sg_page_iter_dma_address() to get each page's DMA address.
436+
* In each loop it operates on PAGE_SIZE unit.
421437
*/
422438
#define for_each_sg_dma_page(sglist, dma_iter, dma_nents, pgoffset) \
423439
for (__sg_page_iter_start(&(dma_iter)->base, sglist, dma_nents, \
424440
pgoffset); \
425441
__sg_page_iter_dma_next(dma_iter);)
426442

443+
/**
444+
* for_each_sgtable_page - iterate over all pages in the sg_table object
445+
* @sgt: sg_table object to iterate over
446+
* @piter: page iterator to hold current page
447+
* @pgoffset: starting page offset (in pages)
448+
*
449+
* Iterates over the all memory pages in the buffer described by
450+
* a scatterlist stored in the given sg_table object.
451+
* See also for_each_sg_page(). In each loop it operates on PAGE_SIZE unit.
452+
*/
453+
#define for_each_sgtable_page(sgt, piter, pgoffset) \
454+
for_each_sg_page(sgt->sgl, piter, sgt->orig_nents, pgoffset)
455+
456+
/**
457+
* for_each_sgtable_dma_page - iterate over the DMA mapped sg_table object
458+
* @sgt: sg_table object to iterate over
459+
* @dma_iter: DMA page iterator to hold current page
460+
* @pgoffset: starting page offset (in pages)
461+
*
462+
* Iterates over the all DMA mapped pages in the buffer described by
463+
* a scatterlist stored in the given sg_table object.
464+
* See also for_each_sg_dma_page(). In each loop it operates on PAGE_SIZE
465+
* unit.
466+
*/
467+
#define for_each_sgtable_dma_page(sgt, dma_iter, pgoffset) \
468+
for_each_sg_dma_page(sgt->sgl, dma_iter, sgt->nents, pgoffset)
469+
470+
427471
/*
428472
* Mapping sg iterator
429473
*

0 commit comments

Comments
 (0)