blob: 79ade0b3b32644e4f653f60195f250f35498939d [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
88static void msm_iommu_reset(void __iomem *base)
89{
90 int ctx, ncb;
91
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);
103 ncb = GET_NCB(base)+1;
104
105 for (ctx = 0; ctx < ncb; ctx++) {
106 SET_BPRCOSH(base, ctx, 0);
107 SET_BPRCISH(base, ctx, 0);
108 SET_BPRCNSH(base, ctx, 0);
109 SET_BPSHCFG(base, ctx, 0);
110 SET_BPMTCFG(base, ctx, 0);
111 SET_ACTLR(base, ctx, 0);
112 SET_SCTLR(base, ctx, 0);
113 SET_FSRRESTORE(base, ctx, 0);
114 SET_TTBR0(base, ctx, 0);
115 SET_TTBR1(base, ctx, 0);
116 SET_TTBCR(base, ctx, 0);
117 SET_BFBCR(base, ctx, 0);
118 SET_PAR(base, ctx, 0);
119 SET_FAR(base, ctx, 0);
120 SET_CTX_TLBIALL(base, ctx, 0);
121 SET_TLBFLPTER(base, ctx, 0);
122 SET_TLBSLPTER(base, ctx, 0);
123 SET_TLBLKCR(base, ctx, 0);
124 SET_PRRR(base, ctx, 0);
125 SET_NMRR(base, ctx, 0);
126 SET_CONTEXTIDR(base, ctx, 0);
127 }
128}
129
130static int msm_iommu_probe(struct platform_device *pdev)
131{
Vasiliy Kulikova86c44d2010-10-17 18:51:37 +0400132 struct resource *r, *r2;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700133 struct clk *iommu_clk;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800134 struct clk *iommu_pclk;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700135 struct msm_iommu_drvdata *drvdata;
136 struct msm_iommu_dev *iommu_dev = pdev->dev.platform_data;
137 void __iomem *regs_base;
138 resource_size_t len;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800139 int ret, ncb, nm2v, irq;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700140
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800141 if (pdev->id == -1) {
142 msm_iommu_root_dev = pdev;
143 return 0;
144 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700145
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800146 drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700147
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800148 if (!drvdata) {
149 ret = -ENOMEM;
150 goto fail;
151 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700152
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800153 if (!iommu_dev) {
154 ret = -ENODEV;
155 goto fail;
156 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700157
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800158 iommu_pclk = clk_get(NULL, "smmu_pclk");
159 if (IS_ERR(iommu_pclk)) {
160 ret = -ENODEV;
161 goto fail;
162 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700163
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800164 ret = clk_enable(iommu_pclk);
165 if (ret)
166 goto fail_enable;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700167
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800168 iommu_clk = clk_get(&pdev->dev, "iommu_clk");
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700169
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800170 if (!IS_ERR(iommu_clk)) {
171 if (clk_get_rate(iommu_clk) == 0)
172 clk_set_min_rate(iommu_clk, 1);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700173
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800174 ret = clk_enable(iommu_clk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700175 if (ret) {
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800176 clk_put(iommu_clk);
177 goto fail_pclk;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700178 }
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800179 } else
180 iommu_clk = NULL;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700181
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800182 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "physbase");
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700183
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800184 if (!r) {
185 ret = -ENODEV;
186 goto fail_clk;
187 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700188
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800189 len = resource_size(r);
190
191 r2 = request_mem_region(r->start, len, r->name);
192 if (!r2) {
193 pr_err("Could not request memory region: start=%p, len=%d\n",
194 (void *) r->start, len);
195 ret = -EBUSY;
196 goto fail_clk;
197 }
198
199 regs_base = ioremap(r2->start, len);
200
201 if (!regs_base) {
202 pr_err("Could not ioremap: start=%p, len=%d\n",
203 (void *) r2->start, len);
204 ret = -EBUSY;
205 goto fail_mem;
206 }
207
208 irq = platform_get_irq_byname(pdev, "secure_irq");
209 if (irq < 0) {
210 ret = -ENODEV;
211 goto fail_io;
212 }
213
214 mb();
215
216 if (GET_IDR(regs_base) == 0) {
217 pr_err("Invalid IDR value detected\n");
218 ret = -ENODEV;
219 goto fail_io;
220 }
221
222 ret = request_irq(irq, msm_iommu_fault_handler, 0,
223 "msm_iommu_secure_irpt_handler", drvdata);
224 if (ret) {
225 pr_err("Request IRQ %d failed with ret=%d\n", irq, ret);
226 goto fail_io;
227 }
228
229 msm_iommu_reset(regs_base);
230 drvdata->pclk = iommu_pclk;
231 drvdata->clk = iommu_clk;
232 drvdata->base = regs_base;
233 drvdata->irq = irq;
234
235 nm2v = GET_NM2VCBMT((unsigned long) regs_base);
236 ncb = GET_NCB((unsigned long) regs_base);
237
238 pr_info("device %s mapped at %p, irq %d with %d ctx banks\n",
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700239 iommu_dev->name, regs_base, irq, ncb+1);
240
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800241 platform_set_drvdata(pdev, drvdata);
242
243 if (iommu_clk)
244 clk_disable(iommu_clk);
245
246 clk_disable(iommu_pclk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700247
248 return 0;
Vasiliy Kulikova86c44d2010-10-17 18:51:37 +0400249fail_io:
250 iounmap(regs_base);
251fail_mem:
252 release_mem_region(r->start, len);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800253fail_clk:
254 if (iommu_clk) {
255 clk_disable(iommu_clk);
256 clk_put(iommu_clk);
257 }
258fail_pclk:
259 clk_disable(iommu_pclk);
260fail_enable:
261 clk_put(iommu_pclk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700262fail:
263 kfree(drvdata);
264 return ret;
265}
266
267static int msm_iommu_remove(struct platform_device *pdev)
268{
269 struct msm_iommu_drvdata *drv = NULL;
270
271 drv = platform_get_drvdata(pdev);
272 if (drv) {
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800273 if (drv->clk)
274 clk_put(drv->clk);
275 clk_put(drv->pclk);
276 memset(drv, 0, sizeof(*drv));
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700277 kfree(drv);
278 platform_set_drvdata(pdev, NULL);
279 }
280 return 0;
281}
282
283static int msm_iommu_ctx_probe(struct platform_device *pdev)
284{
285 struct msm_iommu_ctx_dev *c = pdev->dev.platform_data;
286 struct msm_iommu_drvdata *drvdata;
287 struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800288 int i, ret;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700289 if (!c || !pdev->dev.parent) {
290 ret = -EINVAL;
291 goto fail;
292 }
293
294 drvdata = dev_get_drvdata(pdev->dev.parent);
295
296 if (!drvdata) {
297 ret = -ENODEV;
298 goto fail;
299 }
300
301 ctx_drvdata = kzalloc(sizeof(*ctx_drvdata), GFP_KERNEL);
302 if (!ctx_drvdata) {
303 ret = -ENOMEM;
304 goto fail;
305 }
306 ctx_drvdata->num = c->num;
307 ctx_drvdata->pdev = pdev;
308
309 INIT_LIST_HEAD(&ctx_drvdata->attached_elm);
310 platform_set_drvdata(pdev, ctx_drvdata);
311
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800312 ret = clk_enable(drvdata->pclk);
313 if (ret)
314 goto fail;
315
316 if (drvdata->clk) {
317 ret = clk_enable(drvdata->clk);
318 if (ret) {
319 clk_disable(drvdata->pclk);
320 goto fail;
321 }
322 }
323
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700324 /* Program the M2V tables for this context */
325 for (i = 0; i < MAX_NUM_MIDS; i++) {
326 int mid = c->mids[i];
327 if (mid == -1)
328 break;
329
330 SET_M2VCBR_N(drvdata->base, mid, 0);
331 SET_CBACR_N(drvdata->base, c->num, 0);
332
333 /* Set VMID = MID */
334 SET_VMID(drvdata->base, mid, mid);
335
336 /* Set the context number for that MID to this context */
337 SET_CBNDX(drvdata->base, mid, c->num);
338
339 /* Set MID associated with this context bank */
340 SET_CBVMID(drvdata->base, c->num, mid);
341
342 /* Set security bit override to be Non-secure */
343 SET_NSCFG(drvdata->base, mid, 3);
344 }
345
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800346 if (drvdata->clk)
347 clk_disable(drvdata->clk);
348 clk_disable(drvdata->pclk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700349
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800350 dev_info(&pdev->dev, "context %s using bank %d\n", c->name, c->num);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700351 return 0;
352fail:
353 kfree(ctx_drvdata);
354 return ret;
355}
356
357static int msm_iommu_ctx_remove(struct platform_device *pdev)
358{
359 struct msm_iommu_ctx_drvdata *drv = NULL;
360 drv = platform_get_drvdata(pdev);
361 if (drv) {
362 memset(drv, 0, sizeof(struct msm_iommu_ctx_drvdata));
363 kfree(drv);
364 platform_set_drvdata(pdev, NULL);
365 }
366 return 0;
367}
368
369static struct platform_driver msm_iommu_driver = {
370 .driver = {
371 .name = "msm_iommu",
372 },
373 .probe = msm_iommu_probe,
374 .remove = msm_iommu_remove,
375};
376
377static struct platform_driver msm_iommu_ctx_driver = {
378 .driver = {
379 .name = "msm_iommu_ctx",
380 },
381 .probe = msm_iommu_ctx_probe,
382 .remove = msm_iommu_ctx_remove,
383};
384
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -0800385static int __init msm_iommu_driver_init(void)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700386{
387 int ret;
388 ret = platform_driver_register(&msm_iommu_driver);
389 if (ret != 0) {
390 pr_err("Failed to register IOMMU driver\n");
391 goto error;
392 }
393
394 ret = platform_driver_register(&msm_iommu_ctx_driver);
395 if (ret != 0) {
396 pr_err("Failed to register IOMMU context driver\n");
397 goto error;
398 }
399
400error:
401 return ret;
402}
403
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -0800404static void __exit msm_iommu_driver_exit(void)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700405{
406 platform_driver_unregister(&msm_iommu_ctx_driver);
407 platform_driver_unregister(&msm_iommu_driver);
408}
409
410subsys_initcall(msm_iommu_driver_init);
411module_exit(msm_iommu_driver_exit);
412
413MODULE_LICENSE("GPL v2");
414MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");