Merge branches 'arm/rockchip', 'arm/exynos', 'arm/smmu', 'x86/vt-d', 'x86/amd', ...
[sfrench/cifs-2.6.git] / drivers / iommu / iommu.c
index ffad1eaf450dbf909749fe410f16feed8325b970..49e7542510d15caac5622cdb01fdcf8b77bb80e8 100644 (file)
@@ -788,15 +788,16 @@ static struct iommu_group *iommu_group_get_for_pci_dev(struct pci_dev *pdev)
 
        /* No shared group found, allocate new */
        group = iommu_group_alloc();
-       if (group) {
-               /*
-                * Try to allocate a default domain - needs support from the
-                * IOMMU driver.
-                */
-               group->default_domain = __iommu_domain_alloc(pdev->dev.bus,
-                                                            IOMMU_DOMAIN_DMA);
-               group->domain = group->default_domain;
-       }
+       if (IS_ERR(group))
+               return NULL;
+
+       /*
+        * Try to allocate a default domain - needs support from the
+        * IOMMU driver.
+        */
+       group->default_domain = __iommu_domain_alloc(pdev->dev.bus,
+                                                    IOMMU_DOMAIN_DMA);
+       group->domain = group->default_domain;
 
        return group;
 }
@@ -837,6 +838,11 @@ struct iommu_group *iommu_group_get_for_dev(struct device *dev)
        return group;
 }
 
+struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
+{
+       return group->default_domain;
+}
+
 static int add_iommu_group(struct device *dev, void *data)
 {
        struct iommu_callback_data *cb = data;
@@ -1451,7 +1457,7 @@ static int __init iommu_init(void)
 
        return 0;
 }
-arch_initcall(iommu_init);
+core_initcall(iommu_init);
 
 int iommu_domain_get_attr(struct iommu_domain *domain,
                          enum iommu_attr attr, void *data)
@@ -1533,3 +1539,56 @@ void iommu_put_dm_regions(struct device *dev, struct list_head *list)
        if (ops && ops->put_dm_regions)
                ops->put_dm_regions(dev, list);
 }
+
+/* Request that a device is direct mapped by the IOMMU */
+int iommu_request_dm_for_dev(struct device *dev)
+{
+       struct iommu_domain *dm_domain;
+       struct iommu_group *group;
+       int ret;
+
+       /* Device must already be in a group before calling this function */
+       group = iommu_group_get_for_dev(dev);
+       if (IS_ERR(group))
+               return PTR_ERR(group);
+
+       mutex_lock(&group->mutex);
+
+       /* Check if the default domain is already direct mapped */
+       ret = 0;
+       if (group->default_domain &&
+           group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
+               goto out;
+
+       /* Don't change mappings of existing devices */
+       ret = -EBUSY;
+       if (iommu_group_device_count(group) != 1)
+               goto out;
+
+       /* Allocate a direct mapped domain */
+       ret = -ENOMEM;
+       dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
+       if (!dm_domain)
+               goto out;
+
+       /* Attach the device to the domain */
+       ret = __iommu_attach_group(dm_domain, group);
+       if (ret) {
+               iommu_domain_free(dm_domain);
+               goto out;
+       }
+
+       /* Make the direct mapped domain the default for this group */
+       if (group->default_domain)
+               iommu_domain_free(group->default_domain);
+       group->default_domain = dm_domain;
+
+       pr_info("Using direct mapping for device %s\n", dev_name(dev));
+
+       ret = 0;
+out:
+       mutex_unlock(&group->mutex);
+       iommu_group_put(group);
+
+       return ret;
+}