blob: d344f6ab8c12778ea4c419f29251535ca9e06da1 [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.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/io.h>
24#include <linux/clk.h>
25#include <linux/iommu.h>
26#include <linux/interrupt.h>
27#include <linux/err.h>
28#include <linux/slab.h>
29
30#include <mach/iommu_hw-8xxx.h>
31#include <mach/iommu.h>
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -080032#include <mach/clk.h>
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070033
34struct iommu_ctx_iter_data {
35 /* input */
36 const char *name;
37
38 /* output */
39 struct device *dev;
40};
41
42static struct platform_device *msm_iommu_root_dev;
43
44static int each_iommu_ctx(struct device *dev, void *data)
45{
46 struct iommu_ctx_iter_data *res = data;
47 struct msm_iommu_ctx_dev *c = dev->platform_data;
48
49 if (!res || !c || !c->name || !res->name)
50 return -EINVAL;
51
52 if (!strcmp(res->name, c->name)) {
53 res->dev = dev;
54 return 1;
55 }
56 return 0;
57}
58
59static int each_iommu(struct device *dev, void *data)
60{
61 return device_for_each_child(dev, data, each_iommu_ctx);
62}
63
64struct device *msm_iommu_get_ctx(const char *ctx_name)
65{
66 struct iommu_ctx_iter_data r;
67 int found;
68
69 if (!msm_iommu_root_dev) {
70 pr_err("No root IOMMU device.\n");
71 goto fail;
72 }
73
74 r.name = ctx_name;
75 found = device_for_each_child(&msm_iommu_root_dev->dev, &r, each_iommu);
76
77 if (!found) {
78 pr_err("Could not find context <%s>\n", ctx_name);
79 goto fail;
80 }
81
82 return r.dev;
83fail:
84 return NULL;
85}
86EXPORT_SYMBOL(msm_iommu_get_ctx);
87
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -080088static void msm_iommu_reset(void __iomem *base, int ncb)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070089{
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -080090 int ctx;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070091
92 SET_RPUE(base, 0);
93 SET_RPUEIE(base, 0);
94 SET_ESRRESTORE(base, 0);
95 SET_TBE(base, 0);
96 SET_CR(base, 0);
97 SET_SPDMBE(base, 0);
98 SET_TESTBUSCR(base, 0);
99 SET_TLBRSW(base, 0);
100 SET_GLOBAL_TLBIALL(base, 0);
101 SET_RPU_ACR(base, 0);
102 SET_TLBLKCRWE(base, 1);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700103
104 for (ctx = 0; ctx < ncb; ctx++) {
105 SET_BPRCOSH(base, ctx, 0);
106 SET_BPRCISH(base, ctx, 0);
107 SET_BPRCNSH(base, ctx, 0);
108 SET_BPSHCFG(base, ctx, 0);
109 SET_BPMTCFG(base, ctx, 0);
110 SET_ACTLR(base, ctx, 0);
111 SET_SCTLR(base, ctx, 0);
112 SET_FSRRESTORE(base, ctx, 0);
113 SET_TTBR0(base, ctx, 0);
114 SET_TTBR1(base, ctx, 0);
115 SET_TTBCR(base, ctx, 0);
116 SET_BFBCR(base, ctx, 0);
117 SET_PAR(base, ctx, 0);
118 SET_FAR(base, ctx, 0);
119 SET_CTX_TLBIALL(base, ctx, 0);
120 SET_TLBFLPTER(base, ctx, 0);
121 SET_TLBSLPTER(base, ctx, 0);
122 SET_TLBLKCR(base, ctx, 0);
123 SET_PRRR(base, ctx, 0);
124 SET_NMRR(base, ctx, 0);
125 SET_CONTEXTIDR(base, ctx, 0);
126 }
127}
128
129static int msm_iommu_probe(struct platform_device *pdev)
130{
Vasiliy Kulikova86c44d2010-10-17 18:51:37 +0400131 struct resource *r, *r2;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700132 struct clk *iommu_clk;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800133 struct clk *iommu_pclk;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700134 struct msm_iommu_drvdata *drvdata;
135 struct msm_iommu_dev *iommu_dev = pdev->dev.platform_data;
136 void __iomem *regs_base;
137 resource_size_t len;
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800138 int ret, irq, par;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700139
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800140 if (pdev->id == -1) {
141 msm_iommu_root_dev = pdev;
142 return 0;
143 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700144
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800145 drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700146
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800147 if (!drvdata) {
148 ret = -ENOMEM;
149 goto fail;
150 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700151
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800152 if (!iommu_dev) {
153 ret = -ENODEV;
154 goto fail;
155 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700156
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800157 iommu_pclk = clk_get(NULL, "smmu_pclk");
158 if (IS_ERR(iommu_pclk)) {
159 ret = -ENODEV;
160 goto fail;
161 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700162
Stephen Boyd1107b172013-06-17 10:43:12 -0700163 ret = clk_prepare_enable(iommu_pclk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800164 if (ret)
165 goto fail_enable;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700166
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800167 iommu_clk = clk_get(&pdev->dev, "iommu_clk");
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700168
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800169 if (!IS_ERR(iommu_clk)) {
170 if (clk_get_rate(iommu_clk) == 0)
171 clk_set_min_rate(iommu_clk, 1);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700172
Stephen Boyd1107b172013-06-17 10:43:12 -0700173 ret = clk_prepare_enable(iommu_clk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700174 if (ret) {
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800175 clk_put(iommu_clk);
176 goto fail_pclk;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700177 }
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800178 } else
179 iommu_clk = NULL;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700180
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800181 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "physbase");
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700182
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800183 if (!r) {
184 ret = -ENODEV;
185 goto fail_clk;
186 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700187
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800188 len = resource_size(r);
189
190 r2 = request_mem_region(r->start, len, r->name);
191 if (!r2) {
192 pr_err("Could not request memory region: start=%p, len=%d\n",
193 (void *) r->start, len);
194 ret = -EBUSY;
195 goto fail_clk;
196 }
197
198 regs_base = ioremap(r2->start, len);
199
200 if (!regs_base) {
201 pr_err("Could not ioremap: start=%p, len=%d\n",
202 (void *) r2->start, len);
203 ret = -EBUSY;
204 goto fail_mem;
205 }
206
207 irq = platform_get_irq_byname(pdev, "secure_irq");
208 if (irq < 0) {
209 ret = -ENODEV;
210 goto fail_io;
211 }
212
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800213 msm_iommu_reset(regs_base, iommu_dev->ncb);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800214
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800215 SET_M(regs_base, 0, 1);
216 SET_PAR(regs_base, 0, 0);
217 SET_V2PCFG(regs_base, 0, 1);
218 SET_V2PPR(regs_base, 0, 0);
219 par = GET_PAR(regs_base, 0);
220 SET_V2PCFG(regs_base, 0, 0);
221 SET_M(regs_base, 0, 0);
222
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
229 ret = request_irq(irq, msm_iommu_fault_handler, 0,
230 "msm_iommu_secure_irpt_handler", drvdata);
231 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;
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:
Stephen Boyd1107b172013-06-17 10:43:12 -0700264 clk_disable_unprepare(iommu_pclk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800265fail_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) {
Stephen Boyd1107b172013-06-17 10:43:12 -0700278 if (drv->clk) {
279 clk_unprepare(drv->clk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800280 clk_put(drv->clk);
Stephen Boyd1107b172013-06-17 10:43:12 -0700281 }
282 clk_unprepare(drv->pclk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800283 clk_put(drv->pclk);
284 memset(drv, 0, sizeof(*drv));
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700285 kfree(drv);
286 platform_set_drvdata(pdev, NULL);
287 }
288 return 0;
289}
290
291static int msm_iommu_ctx_probe(struct platform_device *pdev)
292{
293 struct msm_iommu_ctx_dev *c = pdev->dev.platform_data;
294 struct msm_iommu_drvdata *drvdata;
295 struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800296 int i, ret;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700297 if (!c || !pdev->dev.parent) {
298 ret = -EINVAL;
299 goto fail;
300 }
301
302 drvdata = dev_get_drvdata(pdev->dev.parent);
303
304 if (!drvdata) {
305 ret = -ENODEV;
306 goto fail;
307 }
308
309 ctx_drvdata = kzalloc(sizeof(*ctx_drvdata), GFP_KERNEL);
310 if (!ctx_drvdata) {
311 ret = -ENOMEM;
312 goto fail;
313 }
314 ctx_drvdata->num = c->num;
315 ctx_drvdata->pdev = pdev;
316
317 INIT_LIST_HEAD(&ctx_drvdata->attached_elm);
318 platform_set_drvdata(pdev, ctx_drvdata);
319
Stephen Boyd1107b172013-06-17 10:43:12 -0700320 ret = clk_prepare_enable(drvdata->pclk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800321 if (ret)
322 goto fail;
323
324 if (drvdata->clk) {
Stephen Boyd1107b172013-06-17 10:43:12 -0700325 ret = clk_prepare_enable(drvdata->clk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800326 if (ret) {
Stephen Boyd1107b172013-06-17 10:43:12 -0700327 clk_disable_unprepare(drvdata->pclk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800328 goto fail;
329 }
330 }
331
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700332 /* Program the M2V tables for this context */
333 for (i = 0; i < MAX_NUM_MIDS; i++) {
334 int mid = c->mids[i];
335 if (mid == -1)
336 break;
337
338 SET_M2VCBR_N(drvdata->base, mid, 0);
339 SET_CBACR_N(drvdata->base, c->num, 0);
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
Stepan Moskovchenko2e8c8ba2011-02-24 18:00:41 -0800347 /* Set MID associated with this context bank to 0*/
348 SET_CBVMID(drvdata->base, c->num, 0);
349
350 /* Set the ASID for TLB tagging for this context */
351 SET_CONTEXTIDR_ASID(drvdata->base, c->num, c->num);
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 }
356
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800357 if (drvdata->clk)
358 clk_disable(drvdata->clk);
359 clk_disable(drvdata->pclk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700360
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800361 dev_info(&pdev->dev, "context %s using bank %d\n", c->name, c->num);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700362 return 0;
363fail:
364 kfree(ctx_drvdata);
365 return ret;
366}
367
368static int msm_iommu_ctx_remove(struct platform_device *pdev)
369{
370 struct msm_iommu_ctx_drvdata *drv = NULL;
371 drv = platform_get_drvdata(pdev);
372 if (drv) {
373 memset(drv, 0, sizeof(struct msm_iommu_ctx_drvdata));
374 kfree(drv);
375 platform_set_drvdata(pdev, NULL);
376 }
377 return 0;
378}
379
380static struct platform_driver msm_iommu_driver = {
381 .driver = {
382 .name = "msm_iommu",
383 },
384 .probe = msm_iommu_probe,
385 .remove = msm_iommu_remove,
386};
387
388static struct platform_driver msm_iommu_ctx_driver = {
389 .driver = {
390 .name = "msm_iommu_ctx",
391 },
392 .probe = msm_iommu_ctx_probe,
393 .remove = msm_iommu_ctx_remove,
394};
395
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -0800396static int __init msm_iommu_driver_init(void)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700397{
398 int ret;
399 ret = platform_driver_register(&msm_iommu_driver);
400 if (ret != 0) {
401 pr_err("Failed to register IOMMU driver\n");
402 goto error;
403 }
404
405 ret = platform_driver_register(&msm_iommu_ctx_driver);
406 if (ret != 0) {
407 pr_err("Failed to register IOMMU context driver\n");
408 goto error;
409 }
410
411error:
412 return ret;
413}
414
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -0800415static void __exit msm_iommu_driver_exit(void)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700416{
417 platform_driver_unregister(&msm_iommu_ctx_driver);
418 platform_driver_unregister(&msm_iommu_driver);
419}
420
421subsys_initcall(msm_iommu_driver_init);
422module_exit(msm_iommu_driver_exit);
423
424MODULE_LICENSE("GPL v2");
425MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");