[PATCH 1/2] iommu: add support for drivers that manage iommu explicitly

Rob Clark
2019-07-02T20:31:14Z

From: Rob Clark <robdclark@chromium.org>

Avoid attaching any non-driver managed domain if the driver indicates
that it manages the iommu directly.

This solves a couple problems that drm/msm + arm-smmu has with the iommu
framework:

1) In some cases the bootloader takes the iommu out of bypass and
enables the display. This is in particular a problem on the aarch64
laptops that exist these days, and modern snapdragon android devices.
(Older devices also enabled the display in bootloader but did not
take the iommu out of bypass.) Attaching a DMA or IDENTITY domain
while scanout is active, before the driver has a chance to intervene,
makes things go *boom*

2) We are currently blocked on landing support for GPU per-context
pagetables because of the domain attached before driver's ->probe()
is called.

This solves both problems.

Signed-off-by: Rob Clark <robdclark@chromium.org>
---
drivers/iommu/iommu.c | 11 +++++++++++
include/linux/device.h | 3 ++-
2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 0c674d80c37f..efa0957f9772 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -1573,6 +1573,17 @@ static int __iommu_attach_device(struct iommu_domain *domain,
domain->ops->is_attach_deferred(domain, dev))
return 0;

+ /*
+ * If driver is going to manage iommu directly, then avoid
+ * attaching any non driver managed domain. There could
+ * be already active dma underway (ie. scanout in case of
+ * bootloader enabled display), and interfering with that
+ * will make things go *boom*
+ */
+ if ((domain->type != IOMMU_DOMAIN_UNMANAGED) &&
+ dev->driver && dev->driver->driver_manages_iommu)
+ return 0;
+
if (unlikely(domain->ops->attach_dev == NULL))
return -ENODEV;

diff --git a/include/linux/device.h b/include/linux/device.h
index e138baabe01e..d98aa4d3c8c3 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -282,7 +282,8 @@ struct device_driver {
struct module *owner;
const char *mod_name; /* used for built-in modules */

- bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
+ bool suppress_bind_attrs:1; /* disables bind/unbind via sysfs */
+ bool driver_manages_iommu:1; /* driver manages IOMMU explicitly */
enum probe_type probe_type;

const struct of_device_id *of_match_table;
--
2.20.1