blob: 70e96b01e837837466eba93b805e344e58ad25fb [file] [log] [blame]
Stepan Moskovchenko80acab92012-02-14 15:41:12 -08001/* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved.
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -07002 *
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.
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070011 */
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
25#include <mach/iommu_hw-8xxx.h>
26#include <mach/iommu.h>
27
28struct iommu_ctx_iter_data {
29 /* input */
30 const char *name;
31
32 /* output */
33 struct device *dev;
34};
35
36static struct platform_device *msm_iommu_root_dev;
37
38static int each_iommu_ctx(struct device *dev, void *data)
39{
40 struct iommu_ctx_iter_data *res = data;
41 struct msm_iommu_ctx_dev *c = dev->platform_data;
42
43 if (!res || !c || !c->name || !res->name)
44 return -EINVAL;
45
46 if (!strcmp(res->name, c->name)) {
47 res->dev = dev;
48 return 1;
49 }
50 return 0;
51}
52
53static int each_iommu(struct device *dev, void *data)
54{
55 return device_for_each_child(dev, data, each_iommu_ctx);
56}
57
58struct device *msm_iommu_get_ctx(const char *ctx_name)
59{
60 struct iommu_ctx_iter_data r;
61 int found;
62
63 if (!msm_iommu_root_dev) {
64 pr_err("No root IOMMU device.\n");
65 goto fail;
66 }
67
68 r.name = ctx_name;
69 found = device_for_each_child(&msm_iommu_root_dev->dev, &r, each_iommu);
70
Stepan Moskovchenko53b152f2011-10-31 18:24:59 -070071 if (!found || !dev_get_drvdata(r.dev)) {
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070072 pr_err("Could not find context <%s>\n", ctx_name);
73 goto fail;
74 }
75
76 return r.dev;
77fail:
78 return NULL;
79}
80EXPORT_SYMBOL(msm_iommu_get_ctx);
81
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -080082static void msm_iommu_reset(void __iomem *base, int ncb)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070083{
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -080084 int ctx;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070085
86 SET_RPUE(base, 0);
87 SET_RPUEIE(base, 0);
88 SET_ESRRESTORE(base, 0);
89 SET_TBE(base, 0);
90 SET_CR(base, 0);
91 SET_SPDMBE(base, 0);
92 SET_TESTBUSCR(base, 0);
93 SET_TLBRSW(base, 0);
94 SET_GLOBAL_TLBIALL(base, 0);
95 SET_RPU_ACR(base, 0);
96 SET_TLBLKCRWE(base, 1);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070097
98 for (ctx = 0; ctx < ncb; ctx++) {
99 SET_BPRCOSH(base, ctx, 0);
100 SET_BPRCISH(base, ctx, 0);
101 SET_BPRCNSH(base, ctx, 0);
102 SET_BPSHCFG(base, ctx, 0);
103 SET_BPMTCFG(base, ctx, 0);
104 SET_ACTLR(base, ctx, 0);
105 SET_SCTLR(base, ctx, 0);
106 SET_FSRRESTORE(base, ctx, 0);
107 SET_TTBR0(base, ctx, 0);
108 SET_TTBR1(base, ctx, 0);
109 SET_TTBCR(base, ctx, 0);
110 SET_BFBCR(base, ctx, 0);
111 SET_PAR(base, ctx, 0);
112 SET_FAR(base, ctx, 0);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700113 SET_TLBFLPTER(base, ctx, 0);
114 SET_TLBSLPTER(base, ctx, 0);
115 SET_TLBLKCR(base, ctx, 0);
Stepan Moskovchenko0272fd62011-09-26 15:06:40 -0700116 SET_CTX_TLBIALL(base, ctx, 0);
117 SET_TLBIVA(base, ctx, 0);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700118 SET_PRRR(base, ctx, 0);
119 SET_NMRR(base, ctx, 0);
120 SET_CONTEXTIDR(base, ctx, 0);
121 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700122 mb();
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700123}
124
125static int msm_iommu_probe(struct platform_device *pdev)
126{
Vasiliy Kulikova86c44d2010-10-17 18:51:37 +0400127 struct resource *r, *r2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700128 struct clk *iommu_clk = NULL;
129 struct clk *iommu_pclk = NULL;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700130 struct msm_iommu_drvdata *drvdata;
131 struct msm_iommu_dev *iommu_dev = pdev->dev.platform_data;
132 void __iomem *regs_base;
133 resource_size_t len;
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800134 int ret, irq, par;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700135
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800136 if (pdev->id == -1) {
137 msm_iommu_root_dev = pdev;
138 return 0;
139 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700140
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800141 drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700142
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800143 if (!drvdata) {
144 ret = -ENOMEM;
145 goto fail;
146 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700147
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800148 if (!iommu_dev) {
149 ret = -ENODEV;
150 goto fail;
151 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700152
Matt Wagantalle604d712011-10-21 15:38:18 -0700153 iommu_pclk = clk_get_sys("msm_iommu", "iface_clk");
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800154 if (IS_ERR(iommu_pclk)) {
155 ret = -ENODEV;
156 goto fail;
157 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700158
Stepan Moskovchenkobbf95e12012-02-14 15:42:27 -0800159 ret = clk_prepare_enable(iommu_pclk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800160 if (ret)
161 goto fail_enable;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700162
Matt Wagantalle604d712011-10-21 15:38:18 -0700163 iommu_clk = clk_get(&pdev->dev, "core_clk");
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700164
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800165 if (!IS_ERR(iommu_clk)) {
Matt Wagantall99858222011-11-08 14:31:27 -0800166 if (clk_get_rate(iommu_clk) == 0) {
167 ret = clk_round_rate(iommu_clk, 1);
168 clk_set_rate(iommu_clk, ret);
169 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700170
Stepan Moskovchenkobbf95e12012-02-14 15:42:27 -0800171 ret = clk_prepare_enable(iommu_clk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700172 if (ret) {
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800173 clk_put(iommu_clk);
174 goto fail_pclk;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700175 }
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800176 } else
177 iommu_clk = NULL;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700178
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800179 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "physbase");
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700180
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800181 if (!r) {
182 ret = -ENODEV;
183 goto fail_clk;
184 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700185
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800186 len = resource_size(r);
187
188 r2 = request_mem_region(r->start, len, r->name);
189 if (!r2) {
190 pr_err("Could not request memory region: start=%p, len=%d\n",
191 (void *) r->start, len);
192 ret = -EBUSY;
193 goto fail_clk;
194 }
195
196 regs_base = ioremap(r2->start, len);
197
198 if (!regs_base) {
199 pr_err("Could not ioremap: start=%p, len=%d\n",
200 (void *) r2->start, len);
201 ret = -EBUSY;
202 goto fail_mem;
203 }
204
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700205 irq = platform_get_irq_byname(pdev, "nonsecure_irq");
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800206 if (irq < 0) {
207 ret = -ENODEV;
208 goto fail_io;
209 }
210
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800211 msm_iommu_reset(regs_base, iommu_dev->ncb);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800212
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800213 SET_M(regs_base, 0, 1);
214 SET_PAR(regs_base, 0, 0);
215 SET_V2PCFG(regs_base, 0, 1);
216 SET_V2PPR(regs_base, 0, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217 mb();
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800218 par = GET_PAR(regs_base, 0);
219 SET_V2PCFG(regs_base, 0, 0);
220 SET_M(regs_base, 0, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700221 mb();
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800222
223 if (!par) {
224 pr_err("%s: Invalid PAR value detected\n", iommu_dev->name);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800225 ret = -ENODEV;
226 goto fail_io;
227 }
228
Stepan Moskovchenko80acab92012-02-14 15:41:12 -0800229 ret = request_threaded_irq(irq, NULL, msm_iommu_fault_handler,
230 IRQF_ONESHOT, "msm_iommu_secure_irpt_handler", drvdata);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800231 if (ret) {
232 pr_err("Request IRQ %d failed with ret=%d\n", irq, ret);
233 goto fail_io;
234 }
235
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800236
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800237 drvdata->pclk = iommu_pclk;
238 drvdata->clk = iommu_clk;
239 drvdata->base = regs_base;
240 drvdata->irq = irq;
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800241 drvdata->ncb = iommu_dev->ncb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700242 drvdata->name = iommu_dev->name;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800243
244 pr_info("device %s mapped at %p, irq %d with %d ctx banks\n",
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800245 iommu_dev->name, regs_base, irq, iommu_dev->ncb);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700246
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800247 platform_set_drvdata(pdev, drvdata);
248
249 if (iommu_clk)
Stepan Moskovchenkobbf95e12012-02-14 15:42:27 -0800250 clk_disable_unprepare(iommu_clk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800251
Stepan Moskovchenkobbf95e12012-02-14 15:42:27 -0800252 clk_disable_unprepare(iommu_pclk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700253
254 return 0;
Vasiliy Kulikova86c44d2010-10-17 18:51:37 +0400255fail_io:
256 iounmap(regs_base);
257fail_mem:
258 release_mem_region(r->start, len);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800259fail_clk:
260 if (iommu_clk) {
Stepan Moskovchenkobbf95e12012-02-14 15:42:27 -0800261 clk_disable_unprepare(iommu_clk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800262 clk_put(iommu_clk);
263 }
264fail_pclk:
Stepan Moskovchenkobbf95e12012-02-14 15:42:27 -0800265 clk_disable_unprepare(iommu_pclk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800266fail_enable:
267 clk_put(iommu_pclk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700268fail:
269 kfree(drvdata);
270 return ret;
271}
272
273static int msm_iommu_remove(struct platform_device *pdev)
274{
275 struct msm_iommu_drvdata *drv = NULL;
276
277 drv = platform_get_drvdata(pdev);
278 if (drv) {
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800279 if (drv->clk)
280 clk_put(drv->clk);
281 clk_put(drv->pclk);
282 memset(drv, 0, sizeof(*drv));
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700283 kfree(drv);
284 platform_set_drvdata(pdev, NULL);
285 }
286 return 0;
287}
288
289static int msm_iommu_ctx_probe(struct platform_device *pdev)
290{
291 struct msm_iommu_ctx_dev *c = pdev->dev.platform_data;
292 struct msm_iommu_drvdata *drvdata;
293 struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800294 int i, ret;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700295 if (!c || !pdev->dev.parent) {
296 ret = -EINVAL;
297 goto fail;
298 }
299
300 drvdata = dev_get_drvdata(pdev->dev.parent);
301
302 if (!drvdata) {
303 ret = -ENODEV;
304 goto fail;
305 }
306
307 ctx_drvdata = kzalloc(sizeof(*ctx_drvdata), GFP_KERNEL);
308 if (!ctx_drvdata) {
309 ret = -ENOMEM;
310 goto fail;
311 }
312 ctx_drvdata->num = c->num;
313 ctx_drvdata->pdev = pdev;
314
315 INIT_LIST_HEAD(&ctx_drvdata->attached_elm);
316 platform_set_drvdata(pdev, ctx_drvdata);
317
Stepan Moskovchenkobbf95e12012-02-14 15:42:27 -0800318 ret = clk_prepare_enable(drvdata->pclk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800319 if (ret)
320 goto fail;
321
322 if (drvdata->clk) {
Stepan Moskovchenkobbf95e12012-02-14 15:42:27 -0800323 ret = clk_prepare_enable(drvdata->clk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800324 if (ret) {
Stepan Moskovchenkobbf95e12012-02-14 15:42:27 -0800325 clk_disable_unprepare(drvdata->pclk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800326 goto fail;
327 }
328 }
329
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700330 /* Program the M2V tables for this context */
331 for (i = 0; i < MAX_NUM_MIDS; i++) {
332 int mid = c->mids[i];
333 if (mid == -1)
334 break;
335
336 SET_M2VCBR_N(drvdata->base, mid, 0);
337 SET_CBACR_N(drvdata->base, c->num, 0);
338
Stepan Moskovchenko01720e72011-10-21 14:27:29 -0700339 /* Route page faults to the non-secure interrupt */
340 SET_IRPTNDX(drvdata->base, c->num, 1);
341
Stepan Moskovchenko2e8c8ba2011-02-24 18:00:41 -0800342 /* Set VMID = 0 */
343 SET_VMID(drvdata->base, mid, 0);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700344
345 /* Set the context number for that MID to this context */
346 SET_CBNDX(drvdata->base, mid, c->num);
347
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348 /* Set MID associated with this context bank to 0 */
Stepan Moskovchenko2e8c8ba2011-02-24 18:00:41 -0800349 SET_CBVMID(drvdata->base, c->num, 0);
350
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700351 /* Set the ASID for TLB tagging for this context to 0 */
352 SET_CONTEXTIDR_ASID(drvdata->base, c->num, 0);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700353
354 /* Set security bit override to be Non-secure */
355 SET_NSCFG(drvdata->base, mid, 3);
356 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700357 mb();
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700358
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800359 if (drvdata->clk)
Stepan Moskovchenkobbf95e12012-02-14 15:42:27 -0800360 clk_disable_unprepare(drvdata->clk);
361 clk_disable_unprepare(drvdata->pclk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700362
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800363 dev_info(&pdev->dev, "context %s using bank %d\n", c->name, c->num);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700364 return 0;
365fail:
366 kfree(ctx_drvdata);
367 return ret;
368}
369
370static int msm_iommu_ctx_remove(struct platform_device *pdev)
371{
372 struct msm_iommu_ctx_drvdata *drv = NULL;
373 drv = platform_get_drvdata(pdev);
374 if (drv) {
375 memset(drv, 0, sizeof(struct msm_iommu_ctx_drvdata));
376 kfree(drv);
377 platform_set_drvdata(pdev, NULL);
378 }
379 return 0;
380}
381
382static struct platform_driver msm_iommu_driver = {
383 .driver = {
384 .name = "msm_iommu",
385 },
386 .probe = msm_iommu_probe,
387 .remove = msm_iommu_remove,
388};
389
390static struct platform_driver msm_iommu_ctx_driver = {
391 .driver = {
392 .name = "msm_iommu_ctx",
393 },
394 .probe = msm_iommu_ctx_probe,
395 .remove = msm_iommu_ctx_remove,
396};
397
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -0800398static int __init msm_iommu_driver_init(void)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700399{
400 int ret;
401 ret = platform_driver_register(&msm_iommu_driver);
402 if (ret != 0) {
403 pr_err("Failed to register IOMMU driver\n");
404 goto error;
405 }
406
407 ret = platform_driver_register(&msm_iommu_ctx_driver);
408 if (ret != 0) {
409 pr_err("Failed to register IOMMU context driver\n");
410 goto error;
411 }
412
413error:
414 return ret;
415}
416
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -0800417static void __exit msm_iommu_driver_exit(void)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700418{
419 platform_driver_unregister(&msm_iommu_ctx_driver);
420 platform_driver_unregister(&msm_iommu_driver);
421}
422
423subsys_initcall(msm_iommu_driver_init);
424module_exit(msm_iommu_driver_exit);
425
426MODULE_LICENSE("GPL v2");
427MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");