Skip to content

Commit 393cf70

Browse files
rleonmszyprow
authored andcommitted
dma-mapping: Provide an interface to allow allocate IOVA
The existing .map_pages() callback provides both allocating of IOVA and linking DMA pages. That combination works great for most of the callers who use it in control paths, but is less effective in fast paths where there may be multiple calls to map_page(). These advanced callers already manage their data in some sort of database and can perform IOVA allocation in advance, leaving range linkage operation to be in fast path. Provide an interface to allocate/deallocate IOVA and next patch link/unlink DMA ranges to that specific IOVA. In the new API a DMA mapping transaction is identified by a struct dma_iova_state, which holds some recomputed information for the transaction which does not change for each page being mapped, so add a check if IOVA can be used for the specific transaction. The API is exported from dma-iommu as it is the only implementation supported, the namespace is clearly different from iommu_* functions which are not allowed to be used. This code layout allows us to save function call per API call used in datapath as well as a lot of boilerplate code. Reviewed-by: Christoph Hellwig <hch@lst.de> Tested-by: Jens Axboe <axboe@kernel.dk> Reviewed-by: Luis Chamberlain <mcgrof@kernel.org> Signed-off-by: Leon Romanovsky <leonro@nvidia.com> Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
1 parent dc2e692 commit 393cf70

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

drivers/iommu/dma-iommu.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,92 @@ size_t iommu_dma_max_mapping_size(struct device *dev)
17231723
return SIZE_MAX;
17241724
}
17251725

1726+
/**
1727+
* dma_iova_try_alloc - Try to allocate an IOVA space
1728+
* @dev: Device to allocate the IOVA space for
1729+
* @state: IOVA state
1730+
* @phys: physical address
1731+
* @size: IOVA size
1732+
*
1733+
* Check if @dev supports the IOVA-based DMA API, and if yes allocate IOVA space
1734+
* for the given base address and size.
1735+
*
1736+
* Note: @phys is only used to calculate the IOVA alignment. Callers that always
1737+
* do PAGE_SIZE aligned transfers can safely pass 0 here.
1738+
*
1739+
* Returns %true if the IOVA-based DMA API can be used and IOVA space has been
1740+
* allocated, or %false if the regular DMA API should be used.
1741+
*/
1742+
bool dma_iova_try_alloc(struct device *dev, struct dma_iova_state *state,
1743+
phys_addr_t phys, size_t size)
1744+
{
1745+
struct iommu_dma_cookie *cookie;
1746+
struct iommu_domain *domain;
1747+
struct iova_domain *iovad;
1748+
size_t iova_off;
1749+
dma_addr_t addr;
1750+
1751+
memset(state, 0, sizeof(*state));
1752+
if (!use_dma_iommu(dev))
1753+
return false;
1754+
1755+
domain = iommu_get_dma_domain(dev);
1756+
cookie = domain->iova_cookie;
1757+
iovad = &cookie->iovad;
1758+
iova_off = iova_offset(iovad, phys);
1759+
1760+
if (static_branch_unlikely(&iommu_deferred_attach_enabled) &&
1761+
iommu_deferred_attach(dev, iommu_get_domain_for_dev(dev)))
1762+
return false;
1763+
1764+
if (WARN_ON_ONCE(!size))
1765+
return false;
1766+
1767+
/*
1768+
* DMA_IOVA_USE_SWIOTLB is flag which is set by dma-iommu
1769+
* internals, make sure that caller didn't set it and/or
1770+
* didn't use this interface to map SIZE_MAX.
1771+
*/
1772+
if (WARN_ON_ONCE((u64)size & DMA_IOVA_USE_SWIOTLB))
1773+
return false;
1774+
1775+
addr = iommu_dma_alloc_iova(domain,
1776+
iova_align(iovad, size + iova_off),
1777+
dma_get_mask(dev), dev);
1778+
if (!addr)
1779+
return false;
1780+
1781+
state->addr = addr + iova_off;
1782+
state->__size = size;
1783+
return true;
1784+
}
1785+
EXPORT_SYMBOL_GPL(dma_iova_try_alloc);
1786+
1787+
/**
1788+
* dma_iova_free - Free an IOVA space
1789+
* @dev: Device to free the IOVA space for
1790+
* @state: IOVA state
1791+
*
1792+
* Undoes a successful dma_try_iova_alloc().
1793+
*
1794+
* Note that all dma_iova_link() calls need to be undone first. For callers
1795+
* that never call dma_iova_unlink(), dma_iova_destroy() can be used instead
1796+
* which unlinks all ranges and frees the IOVA space in a single efficient
1797+
* operation.
1798+
*/
1799+
void dma_iova_free(struct device *dev, struct dma_iova_state *state)
1800+
{
1801+
struct iommu_domain *domain = iommu_get_dma_domain(dev);
1802+
struct iommu_dma_cookie *cookie = domain->iova_cookie;
1803+
struct iova_domain *iovad = &cookie->iovad;
1804+
size_t iova_start_pad = iova_offset(iovad, state->addr);
1805+
size_t size = dma_iova_size(state);
1806+
1807+
iommu_dma_free_iova(domain, state->addr - iova_start_pad,
1808+
iova_align(iovad, size + iova_start_pad), NULL);
1809+
}
1810+
EXPORT_SYMBOL_GPL(dma_iova_free);
1811+
17261812
void iommu_setup_dma_ops(struct device *dev)
17271813
{
17281814
struct iommu_domain *domain = iommu_get_domain_for_dev(dev);

include/linux/dma-mapping.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@
7272

7373
#define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
7474

75+
struct dma_iova_state {
76+
dma_addr_t addr;
77+
u64 __size;
78+
};
79+
80+
/*
81+
* Use the high bit to mark if we used swiotlb for one or more ranges.
82+
*/
83+
#define DMA_IOVA_USE_SWIOTLB (1ULL << 63)
84+
85+
static inline size_t dma_iova_size(struct dma_iova_state *state)
86+
{
87+
/* Casting is needed for 32-bits systems */
88+
return (size_t)(state->__size & ~DMA_IOVA_USE_SWIOTLB);
89+
}
90+
7591
#ifdef CONFIG_DMA_API_DEBUG
7692
void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr);
7793
void debug_dma_map_single(struct device *dev, const void *addr,
@@ -277,6 +293,38 @@ static inline int dma_mmap_noncontiguous(struct device *dev,
277293
}
278294
#endif /* CONFIG_HAS_DMA */
279295

296+
#ifdef CONFIG_IOMMU_DMA
297+
/**
298+
* dma_use_iova - check if the IOVA API is used for this state
299+
* @state: IOVA state
300+
*
301+
* Return %true if the DMA transfers uses the dma_iova_*() calls or %false if
302+
* they can't be used.
303+
*/
304+
static inline bool dma_use_iova(struct dma_iova_state *state)
305+
{
306+
return state->__size != 0;
307+
}
308+
309+
bool dma_iova_try_alloc(struct device *dev, struct dma_iova_state *state,
310+
phys_addr_t phys, size_t size);
311+
void dma_iova_free(struct device *dev, struct dma_iova_state *state);
312+
#else /* CONFIG_IOMMU_DMA */
313+
static inline bool dma_use_iova(struct dma_iova_state *state)
314+
{
315+
return false;
316+
}
317+
static inline bool dma_iova_try_alloc(struct device *dev,
318+
struct dma_iova_state *state, phys_addr_t phys, size_t size)
319+
{
320+
return false;
321+
}
322+
static inline void dma_iova_free(struct device *dev,
323+
struct dma_iova_state *state)
324+
{
325+
}
326+
#endif /* CONFIG_IOMMU_DMA */
327+
280328
#if defined(CONFIG_HAS_DMA) && defined(CONFIG_DMA_NEED_SYNC)
281329
void __dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
282330
enum dma_data_direction dir);

0 commit comments

Comments
 (0)