1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Author: Inki Dae <inki.dae@samsung.com>
5 // Author: Andrzej Hajda <a.hajda@samsung.com>
8 #include <drm/exynos_drm.h>
9 #include <linux/dma-iommu.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/iommu.h>
13 #include "exynos_drm_drv.h"
15 #if defined(CONFIG_ARM_DMA_USE_IOMMU)
16 #include <asm/dma-iommu.h>
18 #define arm_iommu_create_mapping(...) ({ NULL; })
19 #define arm_iommu_attach_device(...) ({ -ENODEV; })
20 #define arm_iommu_release_mapping(...) ({ })
21 #define arm_iommu_detach_device(...) ({ })
22 #define to_dma_iommu_mapping(dev) NULL
25 #if !defined(CONFIG_IOMMU_DMA)
26 #define iommu_dma_init_domain(...) ({ -EINVAL; })
29 #define EXYNOS_DEV_ADDR_START 0x20000000
30 #define EXYNOS_DEV_ADDR_SIZE 0x40000000
32 static inline int configure_dma_max_seg_size(struct device *dev)
35 dev->dma_parms = kzalloc(sizeof(*dev->dma_parms), GFP_KERNEL);
39 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
43 static inline void clear_dma_max_seg_size(struct device *dev)
45 kfree(dev->dma_parms);
46 dev->dma_parms = NULL;
50 * drm_iommu_attach_device- attach device to iommu mapping
52 * @drm_dev: DRM device
53 * @subdrv_dev: device to be attach
55 * This function should be called by sub drivers to attach it to iommu
58 static int drm_iommu_attach_device(struct drm_device *drm_dev,
59 struct device *subdrv_dev)
61 struct exynos_drm_private *priv = drm_dev->dev_private;
64 if (get_dma_ops(priv->dma_dev) != get_dma_ops(subdrv_dev)) {
65 DRM_DEV_ERROR(subdrv_dev, "Device %s lacks support for IOMMU\n",
66 dev_name(subdrv_dev));
70 ret = configure_dma_max_seg_size(subdrv_dev);
74 if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) {
75 if (to_dma_iommu_mapping(subdrv_dev))
76 arm_iommu_detach_device(subdrv_dev);
78 ret = arm_iommu_attach_device(subdrv_dev, priv->mapping);
79 } else if (IS_ENABLED(CONFIG_IOMMU_DMA)) {
80 ret = iommu_attach_device(priv->mapping, subdrv_dev);
84 clear_dma_max_seg_size(subdrv_dev);
90 * drm_iommu_detach_device -detach device address space mapping from device
92 * @drm_dev: DRM device
93 * @subdrv_dev: device to be detached
95 * This function should be called by sub drivers to detach it from iommu
98 static void drm_iommu_detach_device(struct drm_device *drm_dev,
99 struct device *subdrv_dev)
101 struct exynos_drm_private *priv = drm_dev->dev_private;
103 if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))
104 arm_iommu_detach_device(subdrv_dev);
105 else if (IS_ENABLED(CONFIG_IOMMU_DMA))
106 iommu_detach_device(priv->mapping, subdrv_dev);
108 clear_dma_max_seg_size(subdrv_dev);
111 int exynos_drm_register_dma(struct drm_device *drm, struct device *dev)
113 struct exynos_drm_private *priv = drm->dev_private;
115 if (!priv->dma_dev) {
117 DRM_INFO("Exynos DRM: using %s device for DMA mapping operations\n",
121 if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU))
124 if (!priv->mapping) {
127 if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))
128 mapping = arm_iommu_create_mapping(&platform_bus_type,
129 EXYNOS_DEV_ADDR_START, EXYNOS_DEV_ADDR_SIZE);
130 else if (IS_ENABLED(CONFIG_IOMMU_DMA))
131 mapping = iommu_get_domain_for_dev(priv->dma_dev);
134 return PTR_ERR(mapping);
135 priv->mapping = mapping;
138 return drm_iommu_attach_device(drm, dev);
141 void exynos_drm_unregister_dma(struct drm_device *drm, struct device *dev)
143 if (IS_ENABLED(CONFIG_EXYNOS_IOMMU))
144 drm_iommu_detach_device(drm, dev);
147 void exynos_drm_cleanup_dma(struct drm_device *drm)
149 struct exynos_drm_private *priv = drm->dev_private;
151 if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU))
154 arm_iommu_release_mapping(priv->mapping);
155 priv->mapping = NULL;
156 priv->dma_dev = NULL;