iommu: Add API to request DMA domain for device

Normally during iommu probing a device, a default doamin will
be allocated and attached to the device. The domain type of
the default domain is statically defined, which results in a
situation where the allocated default domain isn't suitable
for the device due to some limitations. We already have API
iommu_request_dm_for_dev() to replace a DMA domain with an
identity one. This adds iommu_request_dma_domain_for_dev()
to request a dma domain if an allocated identity domain isn't
suitable for the device in question.

Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
This commit is contained in:
Lu Baolu 2019-05-25 13:41:22 +08:00 committed by Joerg Roedel
parent dd5142ca5d
commit 7423e01741
2 changed files with 31 additions and 11 deletions

View File

@ -1907,10 +1907,10 @@ struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
return region; return region;
} }
/* Request that a device is direct mapped by the IOMMU */ static int
int iommu_request_dm_for_dev(struct device *dev) request_default_domain_for_dev(struct device *dev, unsigned long type)
{ {
struct iommu_domain *dm_domain; struct iommu_domain *domain;
struct iommu_group *group; struct iommu_group *group;
int ret; int ret;
@ -1923,8 +1923,7 @@ int iommu_request_dm_for_dev(struct device *dev)
/* Check if the default domain is already direct mapped */ /* Check if the default domain is already direct mapped */
ret = 0; ret = 0;
if (group->default_domain && if (group->default_domain && group->default_domain->type == type)
group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
goto out; goto out;
/* Don't change mappings of existing devices */ /* Don't change mappings of existing devices */
@ -1934,23 +1933,26 @@ int iommu_request_dm_for_dev(struct device *dev)
/* Allocate a direct mapped domain */ /* Allocate a direct mapped domain */
ret = -ENOMEM; ret = -ENOMEM;
dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY); domain = __iommu_domain_alloc(dev->bus, type);
if (!dm_domain) if (!domain)
goto out; goto out;
/* Attach the device to the domain */ /* Attach the device to the domain */
ret = __iommu_attach_group(dm_domain, group); ret = __iommu_attach_group(domain, group);
if (ret) { if (ret) {
iommu_domain_free(dm_domain); iommu_domain_free(domain);
goto out; goto out;
} }
iommu_group_create_direct_mappings(group, dev);
/* Make the direct mapped domain the default for this group */ /* Make the direct mapped domain the default for this group */
if (group->default_domain) if (group->default_domain)
iommu_domain_free(group->default_domain); iommu_domain_free(group->default_domain);
group->default_domain = dm_domain; group->default_domain = domain;
dev_info(dev, "Using iommu direct mapping\n"); dev_info(dev, "Using iommu %s mapping\n",
type == IOMMU_DOMAIN_DMA ? "dma" : "direct");
ret = 0; ret = 0;
out: out:
@ -1960,6 +1962,18 @@ out:
return ret; return ret;
} }
/* Request that a device is direct mapped by the IOMMU */
int iommu_request_dm_for_dev(struct device *dev)
{
return request_default_domain_for_dev(dev, IOMMU_DOMAIN_IDENTITY);
}
/* Request that a device can't be direct mapped by the IOMMU */
int iommu_request_dma_domain_for_dev(struct device *dev)
{
return request_default_domain_for_dev(dev, IOMMU_DOMAIN_DMA);
}
const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode) const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
{ {
const struct iommu_ops *ops = NULL; const struct iommu_ops *ops = NULL;

View File

@ -362,6 +362,7 @@ extern void iommu_set_fault_handler(struct iommu_domain *domain,
extern void iommu_get_resv_regions(struct device *dev, struct list_head *list); extern void iommu_get_resv_regions(struct device *dev, struct list_head *list);
extern void iommu_put_resv_regions(struct device *dev, struct list_head *list); extern void iommu_put_resv_regions(struct device *dev, struct list_head *list);
extern int iommu_request_dm_for_dev(struct device *dev); extern int iommu_request_dm_for_dev(struct device *dev);
extern int iommu_request_dma_domain_for_dev(struct device *dev);
extern struct iommu_resv_region * extern struct iommu_resv_region *
iommu_alloc_resv_region(phys_addr_t start, size_t length, int prot, iommu_alloc_resv_region(phys_addr_t start, size_t length, int prot,
enum iommu_resv_type type); enum iommu_resv_type type);
@ -626,6 +627,11 @@ static inline int iommu_request_dm_for_dev(struct device *dev)
return -ENODEV; return -ENODEV;
} }
static inline int iommu_request_dma_domain_for_dev(struct device *dev)
{
return -ENODEV;
}
static inline int iommu_attach_group(struct iommu_domain *domain, static inline int iommu_attach_group(struct iommu_domain *domain,
struct iommu_group *group) struct iommu_group *group)
{ {