iommu: Add iommu_map_sg() function

Mapping and unmapping are more often than not in the critical path.
map_sg allows IOMMU driver implementations to optimize the process
of mapping buffers into the IOMMU page tables.

Instead of mapping a buffer one page at a time and requiring potentially
expensive TLB operations for each page, this function allows the driver
to map all pages in one go and defer TLB maintenance until after all
pages have been mapped.

Additionally, the mapping operation would be faster in general since
clients does not have to keep calling map API over and over again for
each physically contiguous chunk of memory that needs to be mapped to a
virtually contiguous region.

Change-Id: I1f3dd2c3cf67b3db40ee1793580d6af5fec1247d
Signed-off-by: Olav Haugan <ohaugan@codeaurora.org>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
Git-commit: 315786ebbf
Git-repo: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
[mitchelh: fix existing callers and implementations of
 iommu_{map,unmap}_range to match the new function names and APIs,
 maintaining stubs for the old API so that out-of-tree modules can
 continue to compile]
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>
This commit is contained in:
Olav Haugan 2014-10-25 09:55:16 -07:00 committed by David Keitel
parent 923ea2cbed
commit 64bfe1d2be
4 changed files with 64 additions and 3 deletions

View file

@ -1688,8 +1688,8 @@ int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
total_length += s->length;
iova = __alloc_iova(mapping, total_length);
ret = iommu_map_range(mapping->domain, iova, sg, total_length, prot);
if (ret) {
ret = iommu_map_sg(mapping->domain, iova, sg, nents, prot);
if (ret != total_length) {
__free_iova(mapping, iova, total_length);
return 0;
}
@ -1757,7 +1757,7 @@ void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
total_length = PAGE_ALIGN((iova & ~PAGE_MASK) + total_length);
iova &= PAGE_MASK;
iommu_unmap_range(mapping->domain, iova, total_length);
iommu_unmap(mapping->domain, iova, total_length);
__free_iova(mapping, iova, total_length);
}

View file

@ -2154,6 +2154,7 @@ static struct iommu_ops arm_smmu_ops = {
.detach_dev = arm_smmu_detach_dev,
.map = arm_smmu_map,
.unmap = arm_smmu_unmap,
.map_sg = default_iommu_map_sg,
.iova_to_phys = arm_smmu_iova_to_phys,
.add_device = arm_smmu_add_device,
.remove_device = arm_smmu_remove_device,

View file

@ -1124,6 +1124,45 @@ size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
}
EXPORT_SYMBOL_GPL(iommu_unmap);
size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
struct scatterlist *sg, unsigned int nents, int prot)
{
int ret;
size_t mapped = 0;
unsigned int i;
struct scatterlist *s;
for_each_sg(sg, s, nents, i) {
phys_addr_t phys = page_to_phys(sg_page(s));
size_t page_len = s->offset + s->length;
ret = iommu_map(domain, iova + mapped, phys, page_len, prot);
if (ret) {
/* undo mappings already done */
iommu_unmap(domain, iova, mapped);
mapped = 0;
break;
}
mapped += page_len;
}
return mapped;
}
EXPORT_SYMBOL_GPL(default_iommu_map_sg);
/* DEPRECATED */
int iommu_map_range(struct iommu_domain *domain, unsigned int iova,
struct scatterlist *sg, unsigned int len, int opt)
{
return -ENODEV;
}
/* DEPRECATED */
int iommu_unmap_range(struct iommu_domain *domain, unsigned int iova,
unsigned int len)
{
return -ENODEV;
}
int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
phys_addr_t paddr, u64 size, int prot)

View file

@ -98,6 +98,8 @@ enum iommu_attr {
* @detach_dev: detach device from an iommu domain
* @map: map a physically contiguous memory region to an iommu domain
* @unmap: unmap a physically contiguous memory region from an iommu domain
* @map_sg: map a scatter-gather list of physically contiguous memory chunks
* to an iommu domain
* @iova_to_phys: translate iova to physical address
* @add_device: add device to iommu grouping
* @remove_device: remove device from iommu grouping
@ -115,6 +117,8 @@ struct iommu_ops {
phys_addr_t paddr, size_t size, int prot);
size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
size_t size);
size_t (*map_sg)(struct iommu_domain *domain, unsigned long iova,
struct scatterlist *sg, unsigned int nents, int prot);
phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova);
int (*add_device)(struct device *dev);
void (*remove_device)(struct device *dev);
@ -157,6 +161,9 @@ extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
phys_addr_t paddr, size_t size, int prot);
extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
size_t size);
extern size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
struct scatterlist *sg,unsigned int nents,
int prot);
extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova);
extern void iommu_set_fault_handler(struct iommu_domain *domain,
iommu_fault_handler_t handler, void *token);
@ -242,6 +249,13 @@ static inline int report_iommu_fault(struct iommu_domain *domain,
return ret;
}
static inline size_t iommu_map_sg(struct iommu_domain *domain,
unsigned long iova, struct scatterlist *sg,
unsigned int nents, int prot)
{
return domain->ops->map_sg(domain, iova, sg, nents, prot);
}
#else /* CONFIG_IOMMU_API */
struct iommu_ops {};
@ -294,6 +308,13 @@ static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova,
return -ENODEV;
}
static inline size_t iommu_map_sg(struct iommu_domain *domain,
unsigned long iova, struct scatterlist *sg,
unsigned int nents, int prot)
{
return -ENODEV;
}
static inline int iommu_domain_window_enable(struct iommu_domain *domain,
u32 wnd_nr, phys_addr_t paddr,
u64 size, int prot)