blob: d73b9ad35b7a6a809082f2336493429e58c05c5a [file] [log] [blame]
Inki Dae0519f9a2012-10-20 07:53:42 -07001/* exynos_drm_iommu.c
2 *
3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
5 *
Inki Daed81aecb2012-12-18 02:30:17 +09006 * 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.
Inki Dae0519f9a2012-10-20 07:53:42 -070010 */
11
12#include <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 struct device *dev = drm_dev->dev;
34
35 if (!priv->da_start)
36 priv->da_start = EXYNOS_DEV_ADDR_START;
37 if (!priv->da_space_size)
38 priv->da_space_size = EXYNOS_DEV_ADDR_SIZE;
Inki Dae0519f9a2012-10-20 07:53:42 -070039
40 mapping = arm_iommu_create_mapping(&platform_bus_type, priv->da_start,
Marek Szyprowski68efd7d2014-02-25 13:01:09 +010041 priv->da_space_size);
42
Wei Yongjuna0e41b52012-12-10 02:11:13 -050043 if (IS_ERR(mapping))
44 return PTR_ERR(mapping);
Inki Dae0519f9a2012-10-20 07:53:42 -070045
46 dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
47 GFP_KERNEL);
Sachin Kamat4db7fcd2013-08-14 16:38:03 +053048 if (!dev->dma_parms)
49 goto error;
50
Inki Dae0519f9a2012-10-20 07:53:42 -070051 dma_set_max_seg_size(dev, 0xffffffffu);
52 dev->archdata.mapping = mapping;
53
54 return 0;
Sachin Kamat4db7fcd2013-08-14 16:38:03 +053055error:
56 arm_iommu_release_mapping(mapping);
57 return -ENOMEM;
Inki Dae0519f9a2012-10-20 07:53:42 -070058}
59
60/*
61 * drm_release_iommu_mapping - release iommu mapping structure
62 *
63 * @drm_dev: DRM device
64 *
65 * if mapping->kref becomes 0 then all things related to iommu mapping
66 * will be released
67 */
68void drm_release_iommu_mapping(struct drm_device *drm_dev)
69{
70 struct device *dev = drm_dev->dev;
71
72 arm_iommu_release_mapping(dev->archdata.mapping);
73}
74
75/*
76 * drm_iommu_attach_device- attach device to iommu mapping
77 *
78 * @drm_dev: DRM device
79 * @subdrv_dev: device to be attach
80 *
81 * This function should be called by sub drivers to attach it to iommu
82 * mapping.
83 */
84int drm_iommu_attach_device(struct drm_device *drm_dev,
85 struct device *subdrv_dev)
86{
87 struct device *dev = drm_dev->dev;
88 int ret;
89
Joonyoung Shimbf566082015-07-02 21:49:38 +090090 if (!dev->archdata.mapping)
91 return 0;
Inki Dae0519f9a2012-10-20 07:53:42 -070092
93 subdrv_dev->dma_parms = devm_kzalloc(subdrv_dev,
94 sizeof(*subdrv_dev->dma_parms),
95 GFP_KERNEL);
Sachin Kamat4db7fcd2013-08-14 16:38:03 +053096 if (!subdrv_dev->dma_parms)
97 return -ENOMEM;
98
Inki Dae0519f9a2012-10-20 07:53:42 -070099 dma_set_max_seg_size(subdrv_dev, 0xffffffffu);
100
Marek Szyprowski45286852015-06-03 10:26:23 +0200101 if (subdrv_dev->archdata.mapping)
102 arm_iommu_detach_device(subdrv_dev);
103
Inki Dae0519f9a2012-10-20 07:53:42 -0700104 ret = arm_iommu_attach_device(subdrv_dev, dev->archdata.mapping);
105 if (ret < 0) {
106 DRM_DEBUG_KMS("failed iommu attach.\n");
107 return ret;
108 }
109
110 /*
111 * Set dma_ops to drm_device just one time.
112 *
113 * The dma mapping api needs device object and the api is used
114 * to allocate physial memory and map it with iommu table.
115 * If iommu attach succeeded, the sub driver would have dma_ops
116 * for iommu and also all sub drivers have same dma_ops.
117 */
Marek Szyprowskibcfe4e22015-06-03 10:26:24 +0200118 if (get_dma_ops(dev) == get_dma_ops(NULL))
119 set_dma_ops(dev, get_dma_ops(subdrv_dev));
Inki Dae0519f9a2012-10-20 07:53:42 -0700120
121 return 0;
122}
123
124/*
125 * drm_iommu_detach_device -detach device address space mapping from device
126 *
127 * @drm_dev: DRM device
128 * @subdrv_dev: device to be detached
129 *
130 * This function should be called by sub drivers to detach it from iommu
131 * mapping
132 */
133void drm_iommu_detach_device(struct drm_device *drm_dev,
134 struct device *subdrv_dev)
135{
136 struct device *dev = drm_dev->dev;
137 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
138
139 if (!mapping || !mapping->domain)
140 return;
141
Joonyoung Shim4d91a852015-10-02 09:30:38 +0900142 arm_iommu_detach_device(subdrv_dev);
Inki Dae0519f9a2012-10-20 07:53:42 -0700143}