blob: ca2ecc6efdb4ca297cb81425a3a0ac2b1081f9c9 [file] [log] [blame]
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -08001/* Copyright (c) 2010-2011, 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>
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -080027#include <mach/clk.h>
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070028
29struct iommu_ctx_iter_data {
30 /* input */
31 const char *name;
32
33 /* output */
34 struct device *dev;
35};
36
37static struct platform_device *msm_iommu_root_dev;
38
39static int each_iommu_ctx(struct device *dev, void *data)
40{
41 struct iommu_ctx_iter_data *res = data;
42 struct msm_iommu_ctx_dev *c = dev->platform_data;
43
44 if (!res || !c || !c->name || !res->name)
45 return -EINVAL;
46
47 if (!strcmp(res->name, c->name)) {
48 res->dev = dev;
49 return 1;
50 }
51 return 0;
52}
53
54static int each_iommu(struct device *dev, void *data)
55{
56 return device_for_each_child(dev, data, each_iommu_ctx);
57}
58
59struct device *msm_iommu_get_ctx(const char *ctx_name)
60{
61 struct iommu_ctx_iter_data r;
62 int found;
63
64 if (!msm_iommu_root_dev) {
65 pr_err("No root IOMMU device.\n");
66 goto fail;
67 }
68
69 r.name = ctx_name;
70 found = device_for_each_child(&msm_iommu_root_dev->dev, &r, each_iommu);
71
72 if (!found) {
73 pr_err("Could not find context <%s>\n", ctx_name);
74 goto fail;
75 }
76
77 return r.dev;
78fail:
79 return NULL;
80}
81EXPORT_SYMBOL(msm_iommu_get_ctx);
82
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -080083static void msm_iommu_reset(void __iomem *base, int ncb)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070084{
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -080085 int ctx;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070086
87 SET_RPUE(base, 0);
88 SET_RPUEIE(base, 0);
89 SET_ESRRESTORE(base, 0);
90 SET_TBE(base, 0);
91 SET_CR(base, 0);
92 SET_SPDMBE(base, 0);
93 SET_TESTBUSCR(base, 0);
94 SET_TLBRSW(base, 0);
95 SET_GLOBAL_TLBIALL(base, 0);
96 SET_RPU_ACR(base, 0);
97 SET_TLBLKCRWE(base, 1);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070098
99 for (ctx = 0; ctx < ncb; ctx++) {
100 SET_BPRCOSH(base, ctx, 0);
101 SET_BPRCISH(base, ctx, 0);
102 SET_BPRCNSH(base, ctx, 0);
103 SET_BPSHCFG(base, ctx, 0);
104 SET_BPMTCFG(base, ctx, 0);
105 SET_ACTLR(base, ctx, 0);
106 SET_SCTLR(base, ctx, 0);
107 SET_FSRRESTORE(base, ctx, 0);
108 SET_TTBR0(base, ctx, 0);
109 SET_TTBR1(base, ctx, 0);
110 SET_TTBCR(base, ctx, 0);
111 SET_BFBCR(base, ctx, 0);
112 SET_PAR(base, ctx, 0);
113 SET_FAR(base, ctx, 0);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700114 SET_TLBFLPTER(base, ctx, 0);
115 SET_TLBSLPTER(base, ctx, 0);
116 SET_TLBLKCR(base, ctx, 0);
Stepan Moskovchenko0272fd62011-09-26 15:06:40 -0700117 SET_CTX_TLBIALL(base, ctx, 0);
118 SET_TLBIVA(base, ctx, 0);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700119 SET_PRRR(base, ctx, 0);
120 SET_NMRR(base, ctx, 0);
121 SET_CONTEXTIDR(base, ctx, 0);
122 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700123 mb();
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700124}
125
126static int msm_iommu_probe(struct platform_device *pdev)
127{
Vasiliy Kulikova86c44d2010-10-17 18:51:37 +0400128 struct resource *r, *r2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700129 struct clk *iommu_clk = NULL;
130 struct clk *iommu_pclk = NULL;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700131 struct msm_iommu_drvdata *drvdata;
132 struct msm_iommu_dev *iommu_dev = pdev->dev.platform_data;
133 void __iomem *regs_base;
134 resource_size_t len;
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800135 int ret, irq, par;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700136
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800137 if (pdev->id == -1) {
138 msm_iommu_root_dev = pdev;
139 return 0;
140 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700141
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800142 drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700143
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800144 if (!drvdata) {
145 ret = -ENOMEM;
146 goto fail;
147 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700148
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800149 if (!iommu_dev) {
150 ret = -ENODEV;
151 goto fail;
152 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700153
Matt Wagantalle604d712011-10-21 15:38:18 -0700154 iommu_pclk = clk_get_sys("msm_iommu", "iface_clk");
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800155 if (IS_ERR(iommu_pclk)) {
156 ret = -ENODEV;
157 goto fail;
158 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700159
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800160 ret = clk_enable(iommu_pclk);
161 if (ret)
162 goto fail_enable;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700163
Matt Wagantalle604d712011-10-21 15:38:18 -0700164 iommu_clk = clk_get(&pdev->dev, "core_clk");
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700165
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800166 if (!IS_ERR(iommu_clk)) {
167 if (clk_get_rate(iommu_clk) == 0)
168 clk_set_min_rate(iommu_clk, 1);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700169
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800170 ret = clk_enable(iommu_clk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700171 if (ret) {
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800172 clk_put(iommu_clk);
173 goto fail_pclk;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700174 }
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800175 } else
176 iommu_clk = NULL;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700177
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800178 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "physbase");
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700179
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800180 if (!r) {
181 ret = -ENODEV;
182 goto fail_clk;
183 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700184
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800185 len = resource_size(r);
186
187 r2 = request_mem_region(r->start, len, r->name);
188 if (!r2) {
189 pr_err("Could not request memory region: start=%p, len=%d\n",
190 (void *) r->start, len);
191 ret = -EBUSY;
192 goto fail_clk;
193 }
194
195 regs_base = ioremap(r2->start, len);
196
197 if (!regs_base) {
198 pr_err("Could not ioremap: start=%p, len=%d\n",
199 (void *) r2->start, len);
200 ret = -EBUSY;
201 goto fail_mem;
202 }
203
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204 irq = platform_get_irq_byname(pdev, "nonsecure_irq");
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800205 if (irq < 0) {
206 ret = -ENODEV;
207 goto fail_io;
208 }
209
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800210 msm_iommu_reset(regs_base, iommu_dev->ncb);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800211
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800212 SET_M(regs_base, 0, 1);
213 SET_PAR(regs_base, 0, 0);
214 SET_V2PCFG(regs_base, 0, 1);
215 SET_V2PPR(regs_base, 0, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216 mb();
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800217 par = GET_PAR(regs_base, 0);
218 SET_V2PCFG(regs_base, 0, 0);
219 SET_M(regs_base, 0, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220 mb();
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800221
222 if (!par) {
223 pr_err("%s: Invalid PAR value detected\n", iommu_dev->name);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800224 ret = -ENODEV;
225 goto fail_io;
226 }
227
228 ret = request_irq(irq, msm_iommu_fault_handler, 0,
229 "msm_iommu_secure_irpt_handler", drvdata);
230 if (ret) {
231 pr_err("Request IRQ %d failed with ret=%d\n", irq, ret);
232 goto fail_io;
233 }
234
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800235
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800236 drvdata->pclk = iommu_pclk;
237 drvdata->clk = iommu_clk;
238 drvdata->base = regs_base;
239 drvdata->irq = irq;
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800240 drvdata->ncb = iommu_dev->ncb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700241 drvdata->name = iommu_dev->name;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800242
243 pr_info("device %s mapped at %p, irq %d with %d ctx banks\n",
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800244 iommu_dev->name, regs_base, irq, iommu_dev->ncb);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700245
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800246 platform_set_drvdata(pdev, drvdata);
247
248 if (iommu_clk)
249 clk_disable(iommu_clk);
250
251 clk_disable(iommu_pclk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700252
253 return 0;
Vasiliy Kulikova86c44d2010-10-17 18:51:37 +0400254fail_io:
255 iounmap(regs_base);
256fail_mem:
257 release_mem_region(r->start, len);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800258fail_clk:
259 if (iommu_clk) {
260 clk_disable(iommu_clk);
261 clk_put(iommu_clk);
262 }
263fail_pclk:
264 clk_disable(iommu_pclk);
265fail_enable:
266 clk_put(iommu_pclk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700267fail:
268 kfree(drvdata);
269 return ret;
270}
271
272static int msm_iommu_remove(struct platform_device *pdev)
273{
274 struct msm_iommu_drvdata *drv = NULL;
275
276 drv = platform_get_drvdata(pdev);
277 if (drv) {
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800278 if (drv->clk)
279 clk_put(drv->clk);
280 clk_put(drv->pclk);
281 memset(drv, 0, sizeof(*drv));
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700282 kfree(drv);
283 platform_set_drvdata(pdev, NULL);
284 }
285 return 0;
286}
287
288static int msm_iommu_ctx_probe(struct platform_device *pdev)
289{
290 struct msm_iommu_ctx_dev *c = pdev->dev.platform_data;
291 struct msm_iommu_drvdata *drvdata;
292 struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800293 int i, ret;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700294 if (!c || !pdev->dev.parent) {
295 ret = -EINVAL;
296 goto fail;
297 }
298
299 drvdata = dev_get_drvdata(pdev->dev.parent);
300
301 if (!drvdata) {
302 ret = -ENODEV;
303 goto fail;
304 }
305
306 ctx_drvdata = kzalloc(sizeof(*ctx_drvdata), GFP_KERNEL);
307 if (!ctx_drvdata) {
308 ret = -ENOMEM;
309 goto fail;
310 }
311 ctx_drvdata->num = c->num;
312 ctx_drvdata->pdev = pdev;
313
314 INIT_LIST_HEAD(&ctx_drvdata->attached_elm);
315 platform_set_drvdata(pdev, ctx_drvdata);
316
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800317 ret = clk_enable(drvdata->pclk);
318 if (ret)
319 goto fail;
320
321 if (drvdata->clk) {
322 ret = clk_enable(drvdata->clk);
323 if (ret) {
324 clk_disable(drvdata->pclk);
325 goto fail;
326 }
327 }
328
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700329 /* Program the M2V tables for this context */
330 for (i = 0; i < MAX_NUM_MIDS; i++) {
331 int mid = c->mids[i];
332 if (mid == -1)
333 break;
334
335 SET_M2VCBR_N(drvdata->base, mid, 0);
336 SET_CBACR_N(drvdata->base, c->num, 0);
337
Stepan Moskovchenko01720e72011-10-21 14:27:29 -0700338 /* Route page faults to the non-secure interrupt */
339 SET_IRPTNDX(drvdata->base, c->num, 1);
340
Stepan Moskovchenko2e8c8ba2011-02-24 18:00:41 -0800341 /* Set VMID = 0 */
342 SET_VMID(drvdata->base, mid, 0);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700343
344 /* Set the context number for that MID to this context */
345 SET_CBNDX(drvdata->base, mid, c->num);
346
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700347 /* Set MID associated with this context bank to 0 */
Stepan Moskovchenko2e8c8ba2011-02-24 18:00:41 -0800348 SET_CBVMID(drvdata->base, c->num, 0);
349
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350 /* Set the ASID for TLB tagging for this context to 0 */
351 SET_CONTEXTIDR_ASID(drvdata->base, c->num, 0);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700352
353 /* Set security bit override to be Non-secure */
354 SET_NSCFG(drvdata->base, mid, 3);
355 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356 mb();
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700357
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800358 if (drvdata->clk)
359 clk_disable(drvdata->clk);
360 clk_disable(drvdata->pclk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700361
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800362 dev_info(&pdev->dev, "context %s using bank %d\n", c->name, c->num);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700363 return 0;
364fail:
365 kfree(ctx_drvdata);
366 return ret;
367}
368
369static int msm_iommu_ctx_remove(struct platform_device *pdev)
370{
371 struct msm_iommu_ctx_drvdata *drv = NULL;
372 drv = platform_get_drvdata(pdev);
373 if (drv) {
374 memset(drv, 0, sizeof(struct msm_iommu_ctx_drvdata));
375 kfree(drv);
376 platform_set_drvdata(pdev, NULL);
377 }
378 return 0;
379}
380
381static struct platform_driver msm_iommu_driver = {
382 .driver = {
383 .name = "msm_iommu",
384 },
385 .probe = msm_iommu_probe,
386 .remove = msm_iommu_remove,
387};
388
389static struct platform_driver msm_iommu_ctx_driver = {
390 .driver = {
391 .name = "msm_iommu_ctx",
392 },
393 .probe = msm_iommu_ctx_probe,
394 .remove = msm_iommu_ctx_remove,
395};
396
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -0800397static int __init msm_iommu_driver_init(void)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700398{
399 int ret;
400 ret = platform_driver_register(&msm_iommu_driver);
401 if (ret != 0) {
402 pr_err("Failed to register IOMMU driver\n");
403 goto error;
404 }
405
406 ret = platform_driver_register(&msm_iommu_ctx_driver);
407 if (ret != 0) {
408 pr_err("Failed to register IOMMU context driver\n");
409 goto error;
410 }
411
412error:
413 return ret;
414}
415
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -0800416static void __exit msm_iommu_driver_exit(void)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700417{
418 platform_driver_unregister(&msm_iommu_ctx_driver);
419 platform_driver_unregister(&msm_iommu_driver);
420}
421
422subsys_initcall(msm_iommu_driver_init);
423module_exit(msm_iommu_driver_exit);
424
425MODULE_LICENSE("GPL v2");
426MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");