1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
7 #include <linux/dma-map-ops.h>
8 #include <linux/iommu.h>
9 #include <linux/platform_device.h>
11 #include <drm/drm_print.h>
12 #include <drm/exynos_drm.h>
14 #include "exynos_drm_drv.h"
16 #if defined(CONFIG_ARM_DMA_USE_IOMMU)
17 #include <asm/dma-iommu.h>
19 #define arm_iommu_create_mapping(...) ({ NULL; })
20 #define arm_iommu_attach_device(...) ({ -ENODEV; })
21 #define arm_iommu_release_mapping(...) ({ })
22 #define arm_iommu_detach_device(...) ({ })
23 #define to_dma_iommu_mapping(dev) NULL
26 #if !defined(CONFIG_IOMMU_DMA)
27 #define iommu_dma_init_domain(...) ({ -EINVAL; })
30 #define EXYNOS_DEV_ADDR_START 0x20000000
31 #define EXYNOS_DEV_ADDR_SIZE 0x40000000
34 * drm_iommu_attach_device- attach device to iommu mapping
36 * @drm_dev: DRM device
37 * @subdrv_dev: device to be attach
39 * This function should be called by sub drivers to attach it to iommu
42 static int drm_iommu_attach_device(struct drm_device *drm_dev,
43 struct device *subdrv_dev, void **dma_priv)
45 struct exynos_drm_private *priv = drm_dev->dev_private;
48 if (get_dma_ops(priv->dma_dev) != get_dma_ops(subdrv_dev)) {
49 DRM_DEV_ERROR(subdrv_dev, "Device %s lacks support for IOMMU\n",
50 dev_name(subdrv_dev));
54 dma_set_max_seg_size(subdrv_dev, DMA_BIT_MASK(32));
55 if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) {
57 * Keep the original DMA mapping of the sub-device and
58 * restore it on Exynos DRM detach, otherwise the DMA
59 * framework considers it as IOMMU-less during the next
60 * probe (in case of deferred probe or modular build)
62 *dma_priv = to_dma_iommu_mapping(subdrv_dev);
64 arm_iommu_detach_device(subdrv_dev);
66 ret = arm_iommu_attach_device(subdrv_dev, priv->mapping);
67 } else if (IS_ENABLED(CONFIG_IOMMU_DMA)) {
68 ret = iommu_attach_device(priv->mapping, subdrv_dev);
75 * drm_iommu_detach_device -detach device address space mapping from device
77 * @drm_dev: DRM device
78 * @subdrv_dev: device to be detached
80 * This function should be called by sub drivers to detach it from iommu
83 static void drm_iommu_detach_device(struct drm_device *drm_dev,
84 struct device *subdrv_dev, void **dma_priv)
86 struct exynos_drm_private *priv = drm_dev->dev_private;
88 if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) {
89 arm_iommu_detach_device(subdrv_dev);
90 arm_iommu_attach_device(subdrv_dev, *dma_priv);
91 } else if (IS_ENABLED(CONFIG_IOMMU_DMA))
92 iommu_detach_device(priv->mapping, subdrv_dev);
95 int exynos_drm_register_dma(struct drm_device *drm, struct device *dev,
98 struct exynos_drm_private *priv = drm->dev_private;
100 if (!priv->dma_dev) {
102 DRM_INFO("Exynos DRM: using %s device for DMA mapping operations\n",
106 if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU))
109 if (!priv->mapping) {
110 void *mapping = NULL;
112 if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))
113 mapping = arm_iommu_create_mapping(dev,
114 EXYNOS_DEV_ADDR_START, EXYNOS_DEV_ADDR_SIZE);
115 else if (IS_ENABLED(CONFIG_IOMMU_DMA))
116 mapping = iommu_get_domain_for_dev(priv->dma_dev);
120 priv->mapping = mapping;
123 return drm_iommu_attach_device(drm, dev, dma_priv);
126 void exynos_drm_unregister_dma(struct drm_device *drm, struct device *dev,
129 if (IS_ENABLED(CONFIG_EXYNOS_IOMMU))
130 drm_iommu_detach_device(drm, dev, dma_priv);
133 void exynos_drm_cleanup_dma(struct drm_device *drm)
135 struct exynos_drm_private *priv = drm->dev_private;
137 if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU))
140 arm_iommu_release_mapping(priv->mapping);
141 priv->mapping = NULL;
142 priv->dma_dev = NULL;