blob: 3799d5c2b5df746590b2952ed1a93fce2910e085 [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;
39 if (!priv->da_space_order)
40 priv->da_space_order = EXYNOS_DEV_ADDR_ORDER;
41
42 mapping = arm_iommu_create_mapping(&platform_bus_type, priv->da_start,
43 priv->da_space_size,
44 priv->da_space_order);
Wei Yongjuna0e41b52012-12-10 02:11:13 -050045 if (IS_ERR(mapping))
46 return PTR_ERR(mapping);
Inki Dae0519f9a2012-10-20 07:53:42 -070047
48 dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
49 GFP_KERNEL);
50 dma_set_max_seg_size(dev, 0xffffffffu);
51 dev->archdata.mapping = mapping;
52
53 return 0;
54}
55
56/*
57 * drm_release_iommu_mapping - release iommu mapping structure
58 *
59 * @drm_dev: DRM device
60 *
61 * if mapping->kref becomes 0 then all things related to iommu mapping
62 * will be released
63 */
64void drm_release_iommu_mapping(struct drm_device *drm_dev)
65{
66 struct device *dev = drm_dev->dev;
67
68 arm_iommu_release_mapping(dev->archdata.mapping);
69}
70
71/*
72 * drm_iommu_attach_device- attach device to iommu mapping
73 *
74 * @drm_dev: DRM device
75 * @subdrv_dev: device to be attach
76 *
77 * This function should be called by sub drivers to attach it to iommu
78 * mapping.
79 */
80int drm_iommu_attach_device(struct drm_device *drm_dev,
81 struct device *subdrv_dev)
82{
83 struct device *dev = drm_dev->dev;
84 int ret;
85
86 if (!dev->archdata.mapping) {
87 DRM_ERROR("iommu_mapping is null.\n");
88 return -EFAULT;
89 }
90
91 subdrv_dev->dma_parms = devm_kzalloc(subdrv_dev,
92 sizeof(*subdrv_dev->dma_parms),
93 GFP_KERNEL);
94 dma_set_max_seg_size(subdrv_dev, 0xffffffffu);
95
96 ret = arm_iommu_attach_device(subdrv_dev, dev->archdata.mapping);
97 if (ret < 0) {
98 DRM_DEBUG_KMS("failed iommu attach.\n");
99 return ret;
100 }
101
102 /*
103 * Set dma_ops to drm_device just one time.
104 *
105 * The dma mapping api needs device object and the api is used
106 * to allocate physial memory and map it with iommu table.
107 * If iommu attach succeeded, the sub driver would have dma_ops
108 * for iommu and also all sub drivers have same dma_ops.
109 */
110 if (!dev->archdata.dma_ops)
111 dev->archdata.dma_ops = subdrv_dev->archdata.dma_ops;
112
113 return 0;
114}
115
116/*
117 * drm_iommu_detach_device -detach device address space mapping from device
118 *
119 * @drm_dev: DRM device
120 * @subdrv_dev: device to be detached
121 *
122 * This function should be called by sub drivers to detach it from iommu
123 * mapping
124 */
125void drm_iommu_detach_device(struct drm_device *drm_dev,
126 struct device *subdrv_dev)
127{
128 struct device *dev = drm_dev->dev;
129 struct dma_iommu_mapping *mapping = dev->archdata.mapping;
130
131 if (!mapping || !mapping->domain)
132 return;
133
134 iommu_detach_device(mapping->domain, subdrv_dev);
135 drm_release_iommu_mapping(drm_dev);
136}