1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Author: Inki Dae <inki.dae@samsung.com>
5 // Author: Andrzej Hajda <a.hajda@samsung.com>
6
7 #include <linux/dma-iommu.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/iommu.h>
10 #include <linux/platform_device.h>
11
12 #include <drm/drm_print.h>
13 #include <drm/exynos_drm.h>
14
15 #include "exynos_drm_drv.h"
16
17 #if defined(CONFIG_ARM_DMA_USE_IOMMU)
18 #include <asm/dma-iommu.h>
19 #else
20 #define arm_iommu_create_mapping(...) ({ NULL; })
21 #define arm_iommu_attach_device(...) ({ -ENODEV; })
22 #define arm_iommu_release_mapping(...) ({ })
23 #define arm_iommu_detach_device(...) ({ })
24 #define to_dma_iommu_mapping(dev) NULL
25 #endif
26
27 #if !defined(CONFIG_IOMMU_DMA)
28 #define iommu_dma_init_domain(...) ({ -EINVAL; })
29 #endif
30
31 #define EXYNOS_DEV_ADDR_START 0x20000000
32 #define EXYNOS_DEV_ADDR_SIZE 0x40000000
33
configure_dma_max_seg_size(struct device * dev)34 static inline int configure_dma_max_seg_size(struct device *dev)
35 {
36 if (!dev->dma_parms)
37 dev->dma_parms = kzalloc(sizeof(*dev->dma_parms), GFP_KERNEL);
38 if (!dev->dma_parms)
39 return -ENOMEM;
40
41 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
42 return 0;
43 }
44
clear_dma_max_seg_size(struct device * dev)45 static inline void clear_dma_max_seg_size(struct device *dev)
46 {
47 kfree(dev->dma_parms);
48 dev->dma_parms = NULL;
49 }
50
51 /*
52 * drm_iommu_attach_device- attach device to iommu mapping
53 *
54 * @drm_dev: DRM device
55 * @subdrv_dev: device to be attach
56 *
57 * This function should be called by sub drivers to attach it to iommu
58 * mapping.
59 */
drm_iommu_attach_device(struct drm_device * drm_dev,struct device * subdrv_dev)60 static int drm_iommu_attach_device(struct drm_device *drm_dev,
61 struct device *subdrv_dev)
62 {
63 struct exynos_drm_private *priv = drm_dev->dev_private;
64 int ret;
65
66 if (get_dma_ops(priv->dma_dev) != get_dma_ops(subdrv_dev)) {
67 DRM_DEV_ERROR(subdrv_dev, "Device %s lacks support for IOMMU\n",
68 dev_name(subdrv_dev));
69 return -EINVAL;
70 }
71
72 ret = configure_dma_max_seg_size(subdrv_dev);
73 if (ret)
74 return ret;
75
76 if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) {
77 if (to_dma_iommu_mapping(subdrv_dev))
78 arm_iommu_detach_device(subdrv_dev);
79
80 ret = arm_iommu_attach_device(subdrv_dev, priv->mapping);
81 } else if (IS_ENABLED(CONFIG_IOMMU_DMA)) {
82 ret = iommu_attach_device(priv->mapping, subdrv_dev);
83 }
84
85 if (ret)
86 clear_dma_max_seg_size(subdrv_dev);
87
88 return 0;
89 }
90
91 /*
92 * drm_iommu_detach_device -detach device address space mapping from device
93 *
94 * @drm_dev: DRM device
95 * @subdrv_dev: device to be detached
96 *
97 * This function should be called by sub drivers to detach it from iommu
98 * mapping
99 */
drm_iommu_detach_device(struct drm_device * drm_dev,struct device * subdrv_dev)100 static void drm_iommu_detach_device(struct drm_device *drm_dev,
101 struct device *subdrv_dev)
102 {
103 struct exynos_drm_private *priv = drm_dev->dev_private;
104
105 if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))
106 arm_iommu_detach_device(subdrv_dev);
107 else if (IS_ENABLED(CONFIG_IOMMU_DMA))
108 iommu_detach_device(priv->mapping, subdrv_dev);
109
110 clear_dma_max_seg_size(subdrv_dev);
111 }
112
exynos_drm_register_dma(struct drm_device * drm,struct device * dev)113 int exynos_drm_register_dma(struct drm_device *drm, struct device *dev)
114 {
115 struct exynos_drm_private *priv = drm->dev_private;
116
117 if (!priv->dma_dev) {
118 priv->dma_dev = dev;
119 DRM_INFO("Exynos DRM: using %s device for DMA mapping operations\n",
120 dev_name(dev));
121 }
122
123 if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU))
124 return 0;
125
126 if (!priv->mapping) {
127 void *mapping;
128
129 if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))
130 mapping = arm_iommu_create_mapping(&platform_bus_type,
131 EXYNOS_DEV_ADDR_START, EXYNOS_DEV_ADDR_SIZE);
132 else if (IS_ENABLED(CONFIG_IOMMU_DMA))
133 mapping = iommu_get_domain_for_dev(priv->dma_dev);
134
135 if (IS_ERR(mapping))
136 return PTR_ERR(mapping);
137 priv->mapping = mapping;
138 }
139
140 return drm_iommu_attach_device(drm, dev);
141 }
142
exynos_drm_unregister_dma(struct drm_device * drm,struct device * dev)143 void exynos_drm_unregister_dma(struct drm_device *drm, struct device *dev)
144 {
145 if (IS_ENABLED(CONFIG_EXYNOS_IOMMU))
146 drm_iommu_detach_device(drm, dev);
147 }
148
exynos_drm_cleanup_dma(struct drm_device * drm)149 void exynos_drm_cleanup_dma(struct drm_device *drm)
150 {
151 struct exynos_drm_private *priv = drm->dev_private;
152
153 if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU))
154 return;
155
156 arm_iommu_release_mapping(priv->mapping);
157 priv->mapping = NULL;
158 priv->dma_dev = NULL;
159 }
160