Loading...
1/* exynos_drm_iommu.c
2 *
3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 */
11
12#include <drm/drmP.h>
13#include <drm/exynos_drm.h>
14
15#include <linux/dma-mapping.h>
16#include <linux/iommu.h>
17#include <linux/kref.h>
18
19#include <asm/dma-iommu.h>
20
21#include "exynos_drm_drv.h"
22#include "exynos_drm_iommu.h"
23
24/*
25 * drm_create_iommu_mapping - create a mapping structure
26 *
27 * @drm_dev: DRM device
28 */
29int drm_create_iommu_mapping(struct drm_device *drm_dev)
30{
31 struct dma_iommu_mapping *mapping = NULL;
32 struct exynos_drm_private *priv = drm_dev->dev_private;
33
34 if (!priv->da_start)
35 priv->da_start = EXYNOS_DEV_ADDR_START;
36 if (!priv->da_space_size)
37 priv->da_space_size = EXYNOS_DEV_ADDR_SIZE;
38
39 mapping = arm_iommu_create_mapping(&platform_bus_type, priv->da_start,
40 priv->da_space_size);
41
42 if (IS_ERR(mapping))
43 return PTR_ERR(mapping);
44
45 priv->mapping = mapping;
46
47 return 0;
48}
49
50/*
51 * drm_release_iommu_mapping - release iommu mapping structure
52 *
53 * @drm_dev: DRM device
54 *
55 * if mapping->kref becomes 0 then all things related to iommu mapping
56 * will be released
57 */
58void drm_release_iommu_mapping(struct drm_device *drm_dev)
59{
60 struct exynos_drm_private *priv = drm_dev->dev_private;
61
62 arm_iommu_release_mapping(priv->mapping);
63}
64
65/*
66 * drm_iommu_attach_device- attach device to iommu mapping
67 *
68 * @drm_dev: DRM device
69 * @subdrv_dev: device to be attach
70 *
71 * This function should be called by sub drivers to attach it to iommu
72 * mapping.
73 */
74int drm_iommu_attach_device(struct drm_device *drm_dev,
75 struct device *subdrv_dev)
76{
77 struct exynos_drm_private *priv = drm_dev->dev_private;
78 int ret;
79
80 if (!priv->mapping)
81 return 0;
82
83 subdrv_dev->dma_parms = devm_kzalloc(subdrv_dev,
84 sizeof(*subdrv_dev->dma_parms),
85 GFP_KERNEL);
86 if (!subdrv_dev->dma_parms)
87 return -ENOMEM;
88
89 dma_set_max_seg_size(subdrv_dev, 0xffffffffu);
90
91 if (subdrv_dev->archdata.mapping)
92 arm_iommu_detach_device(subdrv_dev);
93
94 ret = arm_iommu_attach_device(subdrv_dev, priv->mapping);
95 if (ret < 0) {
96 DRM_DEBUG_KMS("failed iommu attach.\n");
97 return ret;
98 }
99
100 return 0;
101}
102
103/*
104 * drm_iommu_detach_device -detach device address space mapping from device
105 *
106 * @drm_dev: DRM device
107 * @subdrv_dev: device to be detached
108 *
109 * This function should be called by sub drivers to detach it from iommu
110 * mapping
111 */
112void drm_iommu_detach_device(struct drm_device *drm_dev,
113 struct device *subdrv_dev)
114{
115 struct exynos_drm_private *priv = drm_dev->dev_private;
116 struct dma_iommu_mapping *mapping = priv->mapping;
117
118 if (!mapping || !mapping->domain)
119 return;
120
121 arm_iommu_detach_device(subdrv_dev);
122}
1/* exynos_drm_iommu.c
2 *
3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 */
11
12#include <drm/drmP.h>
13#include <drm/exynos_drm.h>
14
15#include <linux/dma-mapping.h>
16#include <linux/iommu.h>
17
18#include "exynos_drm_drv.h"
19#include "exynos_drm_iommu.h"
20
21static inline int configure_dma_max_seg_size(struct device *dev)
22{
23 if (!dev->dma_parms)
24 dev->dma_parms = kzalloc(sizeof(*dev->dma_parms), GFP_KERNEL);
25 if (!dev->dma_parms)
26 return -ENOMEM;
27
28 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
29 return 0;
30}
31
32static inline void clear_dma_max_seg_size(struct device *dev)
33{
34 kfree(dev->dma_parms);
35 dev->dma_parms = NULL;
36}
37
38/*
39 * drm_create_iommu_mapping - create a mapping structure
40 *
41 * @drm_dev: DRM device
42 */
43int drm_create_iommu_mapping(struct drm_device *drm_dev)
44{
45 struct exynos_drm_private *priv = drm_dev->dev_private;
46
47 return __exynos_iommu_create_mapping(priv, EXYNOS_DEV_ADDR_START,
48 EXYNOS_DEV_ADDR_SIZE);
49}
50
51/*
52 * drm_release_iommu_mapping - release iommu mapping structure
53 *
54 * @drm_dev: DRM device
55 */
56void drm_release_iommu_mapping(struct drm_device *drm_dev)
57{
58 struct exynos_drm_private *priv = drm_dev->dev_private;
59
60 __exynos_iommu_release_mapping(priv);
61}
62
63/*
64 * drm_iommu_attach_device- attach device to iommu mapping
65 *
66 * @drm_dev: DRM device
67 * @subdrv_dev: device to be attach
68 *
69 * This function should be called by sub drivers to attach it to iommu
70 * mapping.
71 */
72int drm_iommu_attach_device(struct drm_device *drm_dev,
73 struct device *subdrv_dev)
74{
75 struct exynos_drm_private *priv = drm_dev->dev_private;
76 int ret;
77
78 if (get_dma_ops(priv->dma_dev) != get_dma_ops(subdrv_dev)) {
79 DRM_ERROR("Device %s lacks support for IOMMU\n",
80 dev_name(subdrv_dev));
81 return -EINVAL;
82 }
83
84 ret = configure_dma_max_seg_size(subdrv_dev);
85 if (ret)
86 return ret;
87
88 ret = __exynos_iommu_attach(priv, subdrv_dev);
89 if (ret)
90 clear_dma_max_seg_size(subdrv_dev);
91
92 return 0;
93}
94
95/*
96 * drm_iommu_detach_device -detach device address space mapping from device
97 *
98 * @drm_dev: DRM device
99 * @subdrv_dev: device to be detached
100 *
101 * This function should be called by sub drivers to detach it from iommu
102 * mapping
103 */
104void drm_iommu_detach_device(struct drm_device *drm_dev,
105 struct device *subdrv_dev)
106{
107 struct exynos_drm_private *priv = drm_dev->dev_private;
108
109 __exynos_iommu_detach(priv, subdrv_dev);
110 clear_dma_max_seg_size(subdrv_dev);
111}