[PATCH 5/6] drm/exynos/iommu: replace preprocessor conditionals with C conditionals

Andrzej Hajda a.hajda at samsung.com
Tue Sep 25 08:23:07 UTC 2018


Using C conditionals is preferred solution - it provides better code
coverage, makes code more clear.

Signed-off-by: Andrzej Hajda <a.hajda at samsung.com>
---
 drivers/gpu/drm/exynos/exynos_drm_iommu.c | 114 +++++++++-------------
 1 file changed, 47 insertions(+), 67 deletions(-)

diff --git a/drivers/gpu/drm/exynos/exynos_drm_iommu.c b/drivers/gpu/drm/exynos/exynos_drm_iommu.c
index 96df5819fd52..54ac262a95aa 100644
--- a/drivers/gpu/drm/exynos/exynos_drm_iommu.c
+++ b/drivers/gpu/drm/exynos/exynos_drm_iommu.c
@@ -12,59 +12,39 @@
 #include <drm/drmP.h>
 #include <drm/exynos_drm.h>
 
+#include <linux/dma-iommu.h>
 #include <linux/dma-mapping.h>
 #include <linux/iommu.h>
 
 #include "exynos_drm_drv.h"
 #include "exynos_drm_iommu.h"
 
-#define EXYNOS_DEV_ADDR_START	0x20000000
-#define EXYNOS_DEV_ADDR_SIZE	0x40000000
-
 #if defined(CONFIG_ARM_DMA_USE_IOMMU)
 #include <asm/dma-iommu.h>
+#else
+#define arm_iommu_create_mapping(...)	({ NULL; })
+#define arm_iommu_attach_device(...)	({ -ENODEV; })
+#define arm_iommu_release_mapping(...)	({ })
+#define arm_iommu_detach_device(...)	({ })
+#define to_dma_iommu_mapping(dev) NULL
+#endif
 
-static inline int __exynos_iommu_create_mapping(struct exynos_drm_private *priv,
-					unsigned long start, unsigned long size)
-{
-	priv->mapping = arm_iommu_create_mapping(&platform_bus_type, start,
-						 size);
-	return IS_ERR(priv->mapping);
-}
-
-static inline void
-__exynos_iommu_release_mapping(struct exynos_drm_private *priv)
-{
-	arm_iommu_release_mapping(priv->mapping);
-}
-
-static inline int __exynos_iommu_attach(struct exynos_drm_private *priv,
-					struct device *dev)
-{
-	if (dev->archdata.mapping)
-		arm_iommu_detach_device(dev);
-
-	return arm_iommu_attach_device(dev, priv->mapping);
-}
-
-static inline void __exynos_iommu_detach(struct exynos_drm_private *priv,
-					 struct device *dev)
-{
-	arm_iommu_detach_device(dev);
-}
+#if !defined(CONFIG_IOMMU_DMA)
+#define iommu_dma_init_domain(...) ({ -EINVAL; })
+#endif
 
-#elif defined(CONFIG_IOMMU_DMA)
-#include <linux/dma-iommu.h>
+#define EXYNOS_DEV_ADDR_START	0x20000000
+#define EXYNOS_DEV_ADDR_SIZE	0x40000000
 
-static inline int __exynos_iommu_create_mapping(struct exynos_drm_private *priv,
-					unsigned long start, unsigned long size)
+static void *iommu_dma_create_mapping(struct exynos_drm_private *priv,
+				      unsigned long start, unsigned long size)
 {
 	struct iommu_domain *domain;
 	int ret;
 
 	domain = iommu_domain_alloc(priv->dma_dev->bus);
 	if (!domain)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
 	ret = iommu_get_dma_cookie(domain);
 	if (ret)
@@ -74,43 +54,20 @@ static inline int __exynos_iommu_create_mapping(struct exynos_drm_private *priv,
 	if (ret)
 		goto put_cookie;
 
-	priv->mapping = domain;
-	return 0;
+	return domain;
 
 put_cookie:
 	iommu_put_dma_cookie(domain);
 free_domain:
 	iommu_domain_free(domain);
-	return ret;
+	return ERR_PTR(ret);
 }
 
-static inline void __exynos_iommu_release_mapping(struct exynos_drm_private *priv)
+static inline void iommu_dma_release_mapping(struct iommu_domain *domain)
 {
-	struct iommu_domain *domain = priv->mapping;
-
 	iommu_put_dma_cookie(domain);
 	iommu_domain_free(domain);
-	priv->mapping = NULL;
-}
-
-static inline int __exynos_iommu_attach(struct exynos_drm_private *priv,
-					struct device *dev)
-{
-	struct iommu_domain *domain = priv->mapping;
-
-	return iommu_attach_device(domain, dev);
-}
-
-static inline void __exynos_iommu_detach(struct exynos_drm_private *priv,
-					 struct device *dev)
-{
-	struct iommu_domain *domain = priv->mapping;
-
-	iommu_detach_device(domain, dev);
 }
-#else
-#error Unsupported architecture and IOMMU/DMA-mapping glue code
-#endif
 
 static inline int configure_dma_max_seg_size(struct device *dev)
 {
@@ -138,8 +95,14 @@ int drm_create_iommu_mapping(struct drm_device *drm_dev)
 {
 	struct exynos_drm_private *priv = drm_dev->dev_private;
 
-	return __exynos_iommu_create_mapping(priv, EXYNOS_DEV_ADDR_START,
-					     EXYNOS_DEV_ADDR_SIZE);
+	if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))
+		priv->mapping = arm_iommu_create_mapping(&platform_bus_type,
+			EXYNOS_DEV_ADDR_START, EXYNOS_DEV_ADDR_SIZE);
+	else if (IS_ENABLED(CONFIG_IOMMU_DMA))
+		priv->mapping = iommu_dma_create_mapping(priv,
+			EXYNOS_DEV_ADDR_START, EXYNOS_DEV_ADDR_SIZE);
+
+	return IS_ERR(priv->mapping);
 }
 
 /*
@@ -151,7 +114,12 @@ void drm_release_iommu_mapping(struct drm_device *drm_dev)
 {
 	struct exynos_drm_private *priv = drm_dev->dev_private;
 
-	__exynos_iommu_release_mapping(priv);
+	if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))
+		arm_iommu_release_mapping(priv->mapping);
+	else if (IS_ENABLED(CONFIG_IOMMU_DMA))
+		iommu_dma_release_mapping(priv->mapping);
+
+	priv->mapping = NULL;
 }
 
 /*
@@ -179,7 +147,15 @@ int drm_iommu_attach_device(struct drm_device *drm_dev,
 	if (ret)
 		return ret;
 
-	ret = __exynos_iommu_attach(priv, subdrv_dev);
+	if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) {
+		if (to_dma_iommu_mapping(subdrv_dev))
+			arm_iommu_detach_device(subdrv_dev);
+
+		ret = arm_iommu_attach_device(subdrv_dev, priv->mapping);
+	} else if (IS_ENABLED(CONFIG_IOMMU_DMA)) {
+		ret = iommu_attach_device(priv->mapping, subdrv_dev);
+	}
+
 	if (ret)
 		clear_dma_max_seg_size(subdrv_dev);
 
@@ -200,6 +176,10 @@ void drm_iommu_detach_device(struct drm_device *drm_dev,
 {
 	struct exynos_drm_private *priv = drm_dev->dev_private;
 
-	__exynos_iommu_detach(priv, subdrv_dev);
+	if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))
+		arm_iommu_detach_device(subdrv_dev);
+	else if (IS_ENABLED(CONFIG_IOMMU_DMA))
+		iommu_detach_device(priv->mapping, subdrv_dev);
+
 	clear_dma_max_seg_size(subdrv_dev);
 }
-- 
2.18.0



More information about the dri-devel mailing list