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-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(dev: priv->dma_dev) != get_dma_ops(dev: 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(dev: 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(domain: priv->mapping, dev: 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(domain: priv->mapping, dev: 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 = NULL; |
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(dev: priv->dma_dev); |
117 | |
118 | if (!mapping) |
119 | return -ENODEV; |
120 | priv->mapping = mapping; |
121 | } |
122 | |
123 | return drm_iommu_attach_device(drm_dev: drm, subdrv_dev: dev, dma_priv); |
124 | } |
125 | |
126 | void exynos_drm_unregister_dma(struct drm_device *drm, struct device *dev, |
127 | void **dma_priv) |
128 | { |
129 | if (IS_ENABLED(CONFIG_EXYNOS_IOMMU)) |
130 | drm_iommu_detach_device(drm_dev: drm, subdrv_dev: dev, dma_priv); |
131 | } |
132 | |
133 | void exynos_drm_cleanup_dma(struct drm_device *drm) |
134 | { |
135 | struct exynos_drm_private *priv = drm->dev_private; |
136 | |
137 | if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU)) |
138 | return; |
139 | |
140 | arm_iommu_release_mapping(priv->mapping); |
141 | priv->mapping = NULL; |
142 | priv->dma_dev = NULL; |
143 | } |
144 | |