blob: 61def7cb52633c04b4ea764f4948de69d12656f4 [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
Stephen Boyd0b559df2013-07-24 13:54:33 -070030#include "msm_iommu_hw-8xxx.h"
31#include "msm_iommu.h"
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070032
33struct iommu_ctx_iter_data {
34 /* input */
35 const char *name;
36
37 /* output */
38 struct device *dev;
39};
40
41static struct platform_device *msm_iommu_root_dev;
42
43static int each_iommu_ctx(struct device *dev, void *data)
44{
45 struct iommu_ctx_iter_data *res = data;
46 struct msm_iommu_ctx_dev *c = dev->platform_data;
47
48 if (!res || !c || !c->name || !res->name)
49 return -EINVAL;
50
51 if (!strcmp(res->name, c->name)) {
52 res->dev = dev;
53 return 1;
54 }
55 return 0;
56}
57
58static int each_iommu(struct device *dev, void *data)
59{
60 return device_for_each_child(dev, data, each_iommu_ctx);
61}
62
63struct device *msm_iommu_get_ctx(const char *ctx_name)
64{
65 struct iommu_ctx_iter_data r;
66 int found;
67
68 if (!msm_iommu_root_dev) {
69 pr_err("No root IOMMU device.\n");
70 goto fail;
71 }
72
73 r.name = ctx_name;
74 found = device_for_each_child(&msm_iommu_root_dev->dev, &r, each_iommu);
75
76 if (!found) {
77 pr_err("Could not find context <%s>\n", ctx_name);
78 goto fail;
79 }
80
81 return r.dev;
82fail:
83 return NULL;
84}
85EXPORT_SYMBOL(msm_iommu_get_ctx);
86
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -080087static void msm_iommu_reset(void __iomem *base, int ncb)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070088{
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -080089 int ctx;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -070090
91 SET_RPUE(base, 0);
92 SET_RPUEIE(base, 0);
93 SET_ESRRESTORE(base, 0);
94 SET_TBE(base, 0);
95 SET_CR(base, 0);
96 SET_SPDMBE(base, 0);
97 SET_TESTBUSCR(base, 0);
98 SET_TLBRSW(base, 0);
99 SET_GLOBAL_TLBIALL(base, 0);
100 SET_RPU_ACR(base, 0);
101 SET_TLBLKCRWE(base, 1);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700102
103 for (ctx = 0; ctx < ncb; ctx++) {
104 SET_BPRCOSH(base, ctx, 0);
105 SET_BPRCISH(base, ctx, 0);
106 SET_BPRCNSH(base, ctx, 0);
107 SET_BPSHCFG(base, ctx, 0);
108 SET_BPMTCFG(base, ctx, 0);
109 SET_ACTLR(base, ctx, 0);
110 SET_SCTLR(base, ctx, 0);
111 SET_FSRRESTORE(base, ctx, 0);
112 SET_TTBR0(base, ctx, 0);
113 SET_TTBR1(base, ctx, 0);
114 SET_TTBCR(base, ctx, 0);
115 SET_BFBCR(base, ctx, 0);
116 SET_PAR(base, ctx, 0);
117 SET_FAR(base, ctx, 0);
118 SET_CTX_TLBIALL(base, ctx, 0);
119 SET_TLBFLPTER(base, ctx, 0);
120 SET_TLBSLPTER(base, ctx, 0);
121 SET_TLBLKCR(base, ctx, 0);
122 SET_PRRR(base, ctx, 0);
123 SET_NMRR(base, ctx, 0);
124 SET_CONTEXTIDR(base, ctx, 0);
125 }
126}
127
128static int msm_iommu_probe(struct platform_device *pdev)
129{
Kefeng Wangafba2562014-05-27 18:18:42 +0800130 struct resource *r;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700131 struct clk *iommu_clk;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800132 struct clk *iommu_pclk;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700133 struct msm_iommu_drvdata *drvdata;
134 struct msm_iommu_dev *iommu_dev = pdev->dev.platform_data;
135 void __iomem *regs_base;
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800136 int ret, irq, par;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700137
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800138 if (pdev->id == -1) {
139 msm_iommu_root_dev = pdev;
140 return 0;
141 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700142
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800143 drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700144
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800145 if (!drvdata) {
146 ret = -ENOMEM;
147 goto fail;
148 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700149
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800150 if (!iommu_dev) {
151 ret = -ENODEV;
152 goto fail;
153 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700154
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800155 iommu_pclk = clk_get(NULL, "smmu_pclk");
156 if (IS_ERR(iommu_pclk)) {
157 ret = -ENODEV;
158 goto fail;
159 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700160
Stephen Boyd1107b172013-06-17 10:43:12 -0700161 ret = clk_prepare_enable(iommu_pclk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800162 if (ret)
163 goto fail_enable;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700164
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800165 iommu_clk = clk_get(&pdev->dev, "iommu_clk");
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700166
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800167 if (!IS_ERR(iommu_clk)) {
168 if (clk_get_rate(iommu_clk) == 0)
Stephen Boyd8d214152013-06-17 10:43:13 -0700169 clk_set_rate(iommu_clk, 1);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700170
Stephen Boyd1107b172013-06-17 10:43:12 -0700171 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");
Kefeng Wangafba2562014-05-27 18:18:42 +0800180 regs_base = devm_ioremap_resource(&pdev->dev, r);
181 if (IS_ERR(regs_base)) {
182 ret = PTR_ERR(regs_base);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800183 goto fail_clk;
184 }
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700185
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800186 irq = platform_get_irq_byname(pdev, "secure_irq");
187 if (irq < 0) {
188 ret = -ENODEV;
Kefeng Wangafba2562014-05-27 18:18:42 +0800189 goto fail_clk;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800190 }
191
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800192 msm_iommu_reset(regs_base, iommu_dev->ncb);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800193
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800194 SET_M(regs_base, 0, 1);
195 SET_PAR(regs_base, 0, 0);
196 SET_V2PCFG(regs_base, 0, 1);
197 SET_V2PPR(regs_base, 0, 0);
198 par = GET_PAR(regs_base, 0);
199 SET_V2PCFG(regs_base, 0, 0);
200 SET_M(regs_base, 0, 0);
201
202 if (!par) {
203 pr_err("%s: Invalid PAR value detected\n", iommu_dev->name);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800204 ret = -ENODEV;
Kefeng Wangafba2562014-05-27 18:18:42 +0800205 goto fail_clk;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800206 }
207
208 ret = request_irq(irq, msm_iommu_fault_handler, 0,
209 "msm_iommu_secure_irpt_handler", drvdata);
210 if (ret) {
211 pr_err("Request IRQ %d failed with ret=%d\n", irq, ret);
Kefeng Wangafba2562014-05-27 18:18:42 +0800212 goto fail_clk;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800213 }
214
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800215
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800216 drvdata->pclk = iommu_pclk;
217 drvdata->clk = iommu_clk;
218 drvdata->base = regs_base;
219 drvdata->irq = irq;
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800220 drvdata->ncb = iommu_dev->ncb;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800221
222 pr_info("device %s mapped at %p, irq %d with %d ctx banks\n",
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800223 iommu_dev->name, regs_base, irq, iommu_dev->ncb);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700224
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800225 platform_set_drvdata(pdev, drvdata);
226
227 if (iommu_clk)
228 clk_disable(iommu_clk);
229
230 clk_disable(iommu_pclk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700231
232 return 0;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800233fail_clk:
234 if (iommu_clk) {
235 clk_disable(iommu_clk);
236 clk_put(iommu_clk);
237 }
238fail_pclk:
Stephen Boyd1107b172013-06-17 10:43:12 -0700239 clk_disable_unprepare(iommu_pclk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800240fail_enable:
241 clk_put(iommu_pclk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700242fail:
243 kfree(drvdata);
244 return ret;
245}
246
247static int msm_iommu_remove(struct platform_device *pdev)
248{
249 struct msm_iommu_drvdata *drv = NULL;
250
251 drv = platform_get_drvdata(pdev);
252 if (drv) {
Stephen Boyd1107b172013-06-17 10:43:12 -0700253 if (drv->clk) {
254 clk_unprepare(drv->clk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800255 clk_put(drv->clk);
Stephen Boyd1107b172013-06-17 10:43:12 -0700256 }
257 clk_unprepare(drv->pclk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800258 clk_put(drv->pclk);
259 memset(drv, 0, sizeof(*drv));
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700260 kfree(drv);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700261 }
262 return 0;
263}
264
265static int msm_iommu_ctx_probe(struct platform_device *pdev)
266{
267 struct msm_iommu_ctx_dev *c = pdev->dev.platform_data;
268 struct msm_iommu_drvdata *drvdata;
Libo Chen34e3a58c2013-07-08 15:59:47 -0700269 struct msm_iommu_ctx_drvdata *ctx_drvdata;
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800270 int i, ret;
Libo Chen34e3a58c2013-07-08 15:59:47 -0700271
272 if (!c || !pdev->dev.parent)
273 return -EINVAL;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700274
275 drvdata = dev_get_drvdata(pdev->dev.parent);
Libo Chen34e3a58c2013-07-08 15:59:47 -0700276 if (!drvdata)
277 return -ENODEV;
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700278
279 ctx_drvdata = kzalloc(sizeof(*ctx_drvdata), GFP_KERNEL);
Libo Chen34e3a58c2013-07-08 15:59:47 -0700280 if (!ctx_drvdata)
281 return -ENOMEM;
282
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700283 ctx_drvdata->num = c->num;
284 ctx_drvdata->pdev = pdev;
285
286 INIT_LIST_HEAD(&ctx_drvdata->attached_elm);
287 platform_set_drvdata(pdev, ctx_drvdata);
288
Stephen Boyd1107b172013-06-17 10:43:12 -0700289 ret = clk_prepare_enable(drvdata->pclk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800290 if (ret)
291 goto fail;
292
293 if (drvdata->clk) {
Stephen Boyd1107b172013-06-17 10:43:12 -0700294 ret = clk_prepare_enable(drvdata->clk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800295 if (ret) {
Stephen Boyd1107b172013-06-17 10:43:12 -0700296 clk_disable_unprepare(drvdata->pclk);
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800297 goto fail;
298 }
299 }
300
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700301 /* Program the M2V tables for this context */
302 for (i = 0; i < MAX_NUM_MIDS; i++) {
303 int mid = c->mids[i];
304 if (mid == -1)
305 break;
306
307 SET_M2VCBR_N(drvdata->base, mid, 0);
308 SET_CBACR_N(drvdata->base, c->num, 0);
309
Stepan Moskovchenko2e8c8ba2011-02-24 18:00:41 -0800310 /* Set VMID = 0 */
311 SET_VMID(drvdata->base, mid, 0);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700312
313 /* Set the context number for that MID to this context */
314 SET_CBNDX(drvdata->base, mid, c->num);
315
Stepan Moskovchenko2e8c8ba2011-02-24 18:00:41 -0800316 /* Set MID associated with this context bank to 0*/
317 SET_CBVMID(drvdata->base, c->num, 0);
318
319 /* Set the ASID for TLB tagging for this context */
320 SET_CONTEXTIDR_ASID(drvdata->base, c->num, c->num);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700321
322 /* Set security bit override to be Non-secure */
323 SET_NSCFG(drvdata->base, mid, 3);
324 }
325
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800326 if (drvdata->clk)
327 clk_disable(drvdata->clk);
328 clk_disable(drvdata->pclk);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700329
Stepan Moskovchenkob61401a2011-02-28 16:03:02 -0800330 dev_info(&pdev->dev, "context %s using bank %d\n", c->name, c->num);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700331 return 0;
332fail:
333 kfree(ctx_drvdata);
334 return ret;
335}
336
337static int msm_iommu_ctx_remove(struct platform_device *pdev)
338{
339 struct msm_iommu_ctx_drvdata *drv = NULL;
340 drv = platform_get_drvdata(pdev);
341 if (drv) {
342 memset(drv, 0, sizeof(struct msm_iommu_ctx_drvdata));
343 kfree(drv);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700344 }
345 return 0;
346}
347
348static struct platform_driver msm_iommu_driver = {
349 .driver = {
350 .name = "msm_iommu",
351 },
352 .probe = msm_iommu_probe,
353 .remove = msm_iommu_remove,
354};
355
356static struct platform_driver msm_iommu_ctx_driver = {
357 .driver = {
358 .name = "msm_iommu_ctx",
359 },
360 .probe = msm_iommu_ctx_probe,
361 .remove = msm_iommu_ctx_remove,
362};
363
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -0800364static int __init msm_iommu_driver_init(void)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700365{
366 int ret;
367 ret = platform_driver_register(&msm_iommu_driver);
368 if (ret != 0) {
369 pr_err("Failed to register IOMMU driver\n");
370 goto error;
371 }
372
373 ret = platform_driver_register(&msm_iommu_ctx_driver);
374 if (ret != 0) {
Libo Chen34e3a58c2013-07-08 15:59:47 -0700375 platform_driver_unregister(&msm_iommu_driver);
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700376 pr_err("Failed to register IOMMU context driver\n");
377 goto error;
378 }
379
380error:
381 return ret;
382}
383
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -0800384static void __exit msm_iommu_driver_exit(void)
Stepan Moskovchenkoc6a59512010-08-24 18:32:38 -0700385{
386 platform_driver_unregister(&msm_iommu_ctx_driver);
387 platform_driver_unregister(&msm_iommu_driver);
388}
389
390subsys_initcall(msm_iommu_driver_init);
391module_exit(msm_iommu_driver_exit);
392
393MODULE_LICENSE("GPL v2");
394MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");