]> Git Repo - linux.git/blob - drivers/gpu/drm/exynos/exynos_drm_dma.c
Merge tag 'arm64-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux
[linux.git] / drivers / gpu / drm / exynos / exynos_drm_dma.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Author: Inki Dae <[email protected]>
5 // Author: Andrzej Hajda <[email protected]>
6
7 #include <linux/dma-map-ops.h>
8 #include <linux/iommu.h>
9 #include <linux/platform_device.h>
10
11 #include <drm/drm_print.h>
12 #include <drm/exynos_drm.h>
13
14 #include "exynos_drm_drv.h"
15
16 #if defined(CONFIG_ARM_DMA_USE_IOMMU)
17 #include <asm/dma-iommu.h>
18 #else
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
24 #endif
25
26 #if !defined(CONFIG_IOMMU_DMA)
27 #define iommu_dma_init_domain(...) ({ -EINVAL; })
28 #endif
29
30 #define EXYNOS_DEV_ADDR_START   0x20000000
31 #define EXYNOS_DEV_ADDR_SIZE    0x40000000
32
33 /*
34  * drm_iommu_attach_device- attach device to iommu mapping
35  *
36  * @drm_dev: DRM device
37  * @subdrv_dev: device to be attach
38  *
39  * This function should be called by sub drivers to attach it to iommu
40  * mapping.
41  */
42 static int drm_iommu_attach_device(struct drm_device *drm_dev,
43                                 struct device *subdrv_dev, void **dma_priv)
44 {
45         struct exynos_drm_private *priv = drm_dev->dev_private;
46         int ret = 0;
47
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));
51                 return -EINVAL;
52         }
53
54         dma_set_max_seg_size(subdrv_dev, DMA_BIT_MASK(32));
55         if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) {
56                 /*
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)
61                  */
62                 *dma_priv = to_dma_iommu_mapping(subdrv_dev);
63                 if (*dma_priv)
64                         arm_iommu_detach_device(subdrv_dev);
65
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);
69         }
70
71         return ret;
72 }
73
74 /*
75  * drm_iommu_detach_device -detach device address space mapping from device
76  *
77  * @drm_dev: DRM device
78  * @subdrv_dev: device to be detached
79  *
80  * This function should be called by sub drivers to detach it from iommu
81  * mapping
82  */
83 static void drm_iommu_detach_device(struct drm_device *drm_dev,
84                                     struct device *subdrv_dev, void **dma_priv)
85 {
86         struct exynos_drm_private *priv = drm_dev->dev_private;
87
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);
93 }
94
95 int exynos_drm_register_dma(struct drm_device *drm, struct device *dev,
96                             void **dma_priv)
97 {
98         struct exynos_drm_private *priv = drm->dev_private;
99
100         if (!priv->dma_dev) {
101                 priv->dma_dev = dev;
102                 DRM_INFO("Exynos DRM: using %s device for DMA mapping operations\n",
103                          dev_name(dev));
104         }
105
106         if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU))
107                 return 0;
108
109         if (!priv->mapping) {
110                 void *mapping;
111
112                 if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))
113                         mapping = arm_iommu_create_mapping(&platform_bus_type,
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);
117                 else
118                         mapping = ERR_PTR(-ENODEV);
119
120                 if (IS_ERR(mapping))
121                         return PTR_ERR(mapping);
122                 priv->mapping = mapping;
123         }
124
125         return drm_iommu_attach_device(drm, dev, dma_priv);
126 }
127
128 void exynos_drm_unregister_dma(struct drm_device *drm, struct device *dev,
129                                void **dma_priv)
130 {
131         if (IS_ENABLED(CONFIG_EXYNOS_IOMMU))
132                 drm_iommu_detach_device(drm, dev, dma_priv);
133 }
134
135 void exynos_drm_cleanup_dma(struct drm_device *drm)
136 {
137         struct exynos_drm_private *priv = drm->dev_private;
138
139         if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU))
140                 return;
141
142         arm_iommu_release_mapping(priv->mapping);
143         priv->mapping = NULL;
144         priv->dma_dev = NULL;
145 }
This page took 0.036699 seconds and 4 git commands to generate.