blob: 116563a9f5a9801306c13c9c51eff8494ff69ddc [file] [log] [blame]
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -08001/* Copyright (c) 2012 Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/io.h>
19#include <linux/clk.h>
20#include <linux/iommu.h>
21#include <linux/interrupt.h>
22#include <linux/err.h>
23#include <linux/slab.h>
24#include <linux/atomic.h>
25#include <linux/of.h>
26#include <linux/of_address.h>
27#include <linux/of_device.h>
28
29#include <mach/iommu_hw-v2.h>
30#include <mach/iommu.h>
31
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080032static int msm_iommu_parse_dt(struct platform_device *pdev,
33 struct msm_iommu_drvdata *drvdata)
34{
35 struct device_node *child;
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -070036 int ret = 0;
37 u32 nsmr;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080038
39 ret = device_move(&pdev->dev, &msm_iommu_root_dev->dev, DPM_ORDER_NONE);
40 if (ret)
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -070041 goto fail;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080042
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -070043 ret = of_property_read_u32(pdev->dev.of_node, "qcom,iommu-smt-size",
44 &nsmr);
45 if (ret)
46 goto fail;
47
48 if (nsmr > MAX_NUM_SMR) {
49 pr_err("Invalid SMT size: %d\n", nsmr);
50 ret = -EINVAL;
51 goto fail;
52 }
53
54 drvdata->nsmr = nsmr;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080055 for_each_child_of_node(pdev->dev.of_node, child) {
56 drvdata->ncb++;
57 if (!of_platform_device_create(child, NULL, &pdev->dev))
58 pr_err("Failed to create %s device\n", child->name);
59 }
60
61 drvdata->name = dev_name(&pdev->dev);
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -070062fail:
63 return ret;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080064}
65
66static atomic_t msm_iommu_next_id = ATOMIC_INIT(-1);
67
68static int __devinit msm_iommu_probe(struct platform_device *pdev)
69{
70 struct msm_iommu_drvdata *drvdata;
71 struct resource *r;
72 int ret;
73
74 if (msm_iommu_root_dev == pdev)
75 return 0;
76
77 if (pdev->id == -1)
78 pdev->id = atomic_inc_return(&msm_iommu_next_id) - 1;
79
80 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
81 if (!drvdata)
82 return -ENOMEM;
83
84 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
85 if (!r)
86 return -EINVAL;
87
88 drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
89 if (!drvdata->base)
90 return -ENOMEM;
91
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -070092 drvdata->gdsc = devm_regulator_get(&pdev->dev, "vdd");
93 if (IS_ERR(drvdata->gdsc))
94 return -EINVAL;
95
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080096 drvdata->pclk = clk_get(&pdev->dev, "iface_clk");
97 if (IS_ERR(drvdata->pclk))
98 return PTR_ERR(drvdata->pclk);
99
100 ret = clk_prepare_enable(drvdata->pclk);
101 if (ret)
102 goto fail_enable;
103
104 drvdata->clk = clk_get(&pdev->dev, "core_clk");
105 if (!IS_ERR(drvdata->clk)) {
106 if (clk_get_rate(drvdata->clk) == 0) {
107 ret = clk_round_rate(drvdata->clk, 1);
108 clk_set_rate(drvdata->clk, ret);
109 }
110
111 ret = clk_prepare_enable(drvdata->clk);
112 if (ret) {
113 clk_put(drvdata->clk);
114 goto fail_pclk;
115 }
116 } else
117 drvdata->clk = NULL;
118
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800119 ret = msm_iommu_parse_dt(pdev, drvdata);
120 if (ret)
121 goto fail_clk;
122
123 pr_info("device %s mapped at %p, with %d ctx banks\n",
124 drvdata->name, drvdata->base, drvdata->ncb);
125
126 platform_set_drvdata(pdev, drvdata);
127
128 if (drvdata->clk)
129 clk_disable_unprepare(drvdata->clk);
130
131 clk_disable_unprepare(drvdata->pclk);
132
133 return 0;
134
135fail_clk:
136 if (drvdata->clk) {
137 clk_disable_unprepare(drvdata->clk);
138 clk_put(drvdata->clk);
139 }
140fail_pclk:
141 clk_disable_unprepare(drvdata->pclk);
142fail_enable:
143 clk_put(drvdata->pclk);
144 return ret;
145}
146
147static int __devexit msm_iommu_remove(struct platform_device *pdev)
148{
149 struct msm_iommu_drvdata *drv = NULL;
150
151 drv = platform_get_drvdata(pdev);
152 if (drv) {
153 if (drv->clk)
154 clk_put(drv->clk);
155 clk_put(drv->pclk);
156 platform_set_drvdata(pdev, NULL);
157 }
158 return 0;
159}
160
161static int msm_iommu_ctx_parse_dt(struct platform_device *pdev,
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800162 struct msm_iommu_ctx_drvdata *ctx_drvdata)
163{
164 struct resource *r, rp;
Sathish Ambleycf045e62012-06-07 12:56:50 -0700165 int irq, ret;
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -0700166 u32 nsid;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800167
168 irq = platform_get_irq(pdev, 0);
169 if (irq > 0) {
170 ret = request_threaded_irq(irq, NULL,
171 msm_iommu_fault_handler_v2,
172 IRQF_ONESHOT | IRQF_SHARED,
173 "msm_iommu_nonsecure_irq", pdev);
174 if (ret) {
175 pr_err("Request IRQ %d failed with ret=%d\n", irq, ret);
176 return ret;
177 }
178 }
179
180 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
181 if (!r)
182 return -EINVAL;
183
184 ret = of_address_to_resource(pdev->dev.parent->of_node, 0, &rp);
185 if (ret)
186 return -EINVAL;
187
188 /* Calculate the context bank number using the base addresses. The
189 * first 8 pages belong to the global address space which is followed
190 * by the context banks, hence subtract by 8 to get the context bank
191 * number.
192 */
193 ctx_drvdata->num = ((r->start - rp.start) >> CTX_SHIFT) - 8;
194
195 if (of_property_read_string(pdev->dev.of_node, "qcom,iommu-ctx-name",
196 &ctx_drvdata->name))
197 ctx_drvdata->name = dev_name(&pdev->dev);
198
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -0700199 if (!of_get_property(pdev->dev.of_node, "qcom,iommu-ctx-sids", &nsid))
200 return -EINVAL;
201
202 if (nsid >= sizeof(ctx_drvdata->sids))
203 return -EINVAL;
204
205 if (of_property_read_u32_array(pdev->dev.of_node, "qcom,iommu-ctx-sids",
206 ctx_drvdata->sids,
207 nsid / sizeof(*ctx_drvdata->sids))) {
208 return -EINVAL;
209 }
210 ctx_drvdata->nsid = nsid;
211
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800212 return 0;
213}
214
215static int __devinit msm_iommu_ctx_probe(struct platform_device *pdev)
216{
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800217 struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL;
218 int ret;
219
220 if (!pdev->dev.parent)
221 return -EINVAL;
222
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800223 ctx_drvdata = devm_kzalloc(&pdev->dev, sizeof(*ctx_drvdata),
224 GFP_KERNEL);
225 if (!ctx_drvdata)
226 return -ENOMEM;
227
228 ctx_drvdata->pdev = pdev;
229 INIT_LIST_HEAD(&ctx_drvdata->attached_elm);
230 platform_set_drvdata(pdev, ctx_drvdata);
231
Sathish Ambleycf045e62012-06-07 12:56:50 -0700232 ret = msm_iommu_ctx_parse_dt(pdev, ctx_drvdata);
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800233 if (!ret)
234 dev_info(&pdev->dev, "context %s using bank %d\n",
Stepan Moskovchenkoe14ca5c2012-07-11 12:40:51 -0700235 ctx_drvdata->name, ctx_drvdata->num);
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800236
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800237 return ret;
238}
239
240static int __devexit msm_iommu_ctx_remove(struct platform_device *pdev)
241{
242 platform_set_drvdata(pdev, NULL);
243 return 0;
244}
245
246static struct of_device_id msm_iommu_match_table[] = {
247 { .compatible = "qcom,msm-smmu-v2", },
248 {}
249};
250
251static struct platform_driver msm_iommu_driver = {
252 .driver = {
253 .name = "msm_iommu_v2",
254 .of_match_table = msm_iommu_match_table,
255 },
256 .probe = msm_iommu_probe,
257 .remove = __devexit_p(msm_iommu_remove),
258};
259
260static struct of_device_id msm_iommu_ctx_match_table[] = {
261 { .name = "qcom,iommu-ctx", },
262 {}
263};
264
265static struct platform_driver msm_iommu_ctx_driver = {
266 .driver = {
267 .name = "msm_iommu_ctx_v2",
268 .of_match_table = msm_iommu_ctx_match_table,
269 },
270 .probe = msm_iommu_ctx_probe,
271 .remove = __devexit_p(msm_iommu_ctx_remove),
272};
273
274static int __init msm_iommu_driver_init(void)
275{
276 struct device_node *node;
277 int ret;
278
279 node = of_find_compatible_node(NULL, NULL, "qcom,msm-smmu-v2");
280 if (!node)
281 return -ENODEV;
282
283 of_node_put(node);
284
285 msm_iommu_root_dev = platform_device_register_simple(
286 "msm_iommu", -1, 0, 0);
287 if (!msm_iommu_root_dev) {
288 pr_err("Failed to create root IOMMU device\n");
289 ret = -ENODEV;
290 goto error;
291 }
292
293 atomic_inc(&msm_iommu_next_id);
294
295 ret = platform_driver_register(&msm_iommu_driver);
296 if (ret != 0) {
297 pr_err("Failed to register IOMMU driver\n");
298 goto error;
299 }
300
301 ret = platform_driver_register(&msm_iommu_ctx_driver);
302 if (ret != 0) {
303 pr_err("Failed to register IOMMU context driver\n");
304 goto error;
305 }
306
307error:
308 return ret;
309}
310
311static void __exit msm_iommu_driver_exit(void)
312{
313 platform_driver_unregister(&msm_iommu_ctx_driver);
314 platform_driver_unregister(&msm_iommu_driver);
315 platform_device_unregister(msm_iommu_root_dev);
316}
317
318subsys_initcall(msm_iommu_driver_init);
319module_exit(msm_iommu_driver_exit);
320
321MODULE_LICENSE("GPL v2");