Commit | Line | Data |
---|---|---|
b61401ad | 1 | /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
c6a5951e SM |
2 | * |
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> | |
b61401ad | 32 | #include <mach/clk.h> |
c6a5951e SM |
33 | |
34 | struct iommu_ctx_iter_data { | |
35 | /* input */ | |
36 | const char *name; | |
37 | ||
38 | /* output */ | |
39 | struct device *dev; | |
40 | }; | |
41 | ||
42 | static struct platform_device *msm_iommu_root_dev; | |
43 | ||
44 | static 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 | ||
59 | static int each_iommu(struct device *dev, void *data) | |
60 | { | |
61 | return device_for_each_child(dev, data, each_iommu_ctx); | |
62 | } | |
63 | ||
64 | struct 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; | |
83 | fail: | |
84 | return NULL; | |
85 | } | |
86 | EXPORT_SYMBOL(msm_iommu_get_ctx); | |
87 | ||
88 | static 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 | ||
130 | static int msm_iommu_probe(struct platform_device *pdev) | |
131 | { | |
a86c44d4 | 132 | struct resource *r, *r2; |
c6a5951e | 133 | struct clk *iommu_clk; |
b61401ad | 134 | struct clk *iommu_pclk; |
c6a5951e SM |
135 | 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; | |
b61401ad | 139 | int ret, ncb, nm2v, irq; |
c6a5951e | 140 | |
b61401ad SM |
141 | if (pdev->id == -1) { |
142 | msm_iommu_root_dev = pdev; | |
143 | return 0; | |
144 | } | |
c6a5951e | 145 | |
b61401ad | 146 | drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL); |
c6a5951e | 147 | |
b61401ad SM |
148 | if (!drvdata) { |
149 | ret = -ENOMEM; | |
150 | goto fail; | |
151 | } | |
152 | ||
153 | if (!iommu_dev) { | |
154 | ret = -ENODEV; | |
155 | goto fail; | |
156 | } | |
c6a5951e | 157 | |
b61401ad SM |
158 | iommu_pclk = clk_get(NULL, "smmu_pclk"); |
159 | if (IS_ERR(iommu_pclk)) { | |
160 | ret = -ENODEV; | |
161 | goto fail; | |
162 | } | |
163 | ||
164 | ret = clk_enable(iommu_pclk); | |
165 | if (ret) | |
166 | goto fail_enable; | |
167 | ||
168 | iommu_clk = clk_get(&pdev->dev, "iommu_clk"); | |
169 | ||
170 | if (!IS_ERR(iommu_clk)) { | |
171 | if (clk_get_rate(iommu_clk) == 0) | |
172 | clk_set_min_rate(iommu_clk, 1); | |
173 | ||
174 | ret = clk_enable(iommu_clk); | |
175 | if (ret) { | |
c6a5951e | 176 | clk_put(iommu_clk); |
b61401ad | 177 | goto fail_pclk; |
c6a5951e | 178 | } |
b61401ad SM |
179 | } else |
180 | iommu_clk = NULL; | |
c6a5951e | 181 | |
b61401ad | 182 | r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "physbase"); |
c6a5951e | 183 | |
b61401ad SM |
184 | if (!r) { |
185 | ret = -ENODEV; | |
186 | goto fail_clk; | |
187 | } | |
c6a5951e | 188 | |
b61401ad | 189 | len = resource_size(r); |
c6a5951e | 190 | |
b61401ad SM |
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 | } | |
c6a5951e | 198 | |
b61401ad | 199 | regs_base = ioremap(r2->start, len); |
c6a5951e | 200 | |
b61401ad SM |
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 | } | |
c6a5951e | 213 | |
b61401ad | 214 | mb(); |
c6a5951e | 215 | |
b61401ad SM |
216 | if (GET_IDR(regs_base) == 0) { |
217 | pr_err("Invalid IDR value detected\n"); | |
218 | ret = -ENODEV; | |
219 | goto fail_io; | |
220 | } | |
c6a5951e | 221 | |
b61401ad SM |
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 | } | |
c6a5951e | 228 | |
b61401ad SM |
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; | |
c6a5951e | 234 | |
b61401ad SM |
235 | nm2v = GET_NM2VCBMT((unsigned long) regs_base); |
236 | ncb = GET_NCB((unsigned long) regs_base); | |
c6a5951e | 237 | |
b61401ad | 238 | pr_info("device %s mapped at %p, irq %d with %d ctx banks\n", |
c6a5951e SM |
239 | iommu_dev->name, regs_base, irq, ncb+1); |
240 | ||
b61401ad | 241 | platform_set_drvdata(pdev, drvdata); |
c6a5951e | 242 | |
b61401ad SM |
243 | if (iommu_clk) |
244 | clk_disable(iommu_clk); | |
245 | ||
246 | clk_disable(iommu_pclk); | |
c6a5951e | 247 | |
b61401ad | 248 | return 0; |
a86c44d4 VK |
249 | fail_io: |
250 | iounmap(regs_base); | |
251 | fail_mem: | |
252 | release_mem_region(r->start, len); | |
b61401ad SM |
253 | fail_clk: |
254 | if (iommu_clk) { | |
255 | clk_disable(iommu_clk); | |
256 | clk_put(iommu_clk); | |
257 | } | |
258 | fail_pclk: | |
259 | clk_disable(iommu_pclk); | |
260 | fail_enable: | |
261 | clk_put(iommu_pclk); | |
c6a5951e SM |
262 | fail: |
263 | kfree(drvdata); | |
264 | return ret; | |
265 | } | |
266 | ||
267 | static 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) { | |
b61401ad SM |
273 | if (drv->clk) |
274 | clk_put(drv->clk); | |
275 | clk_put(drv->pclk); | |
276 | memset(drv, 0, sizeof(*drv)); | |
c6a5951e SM |
277 | kfree(drv); |
278 | platform_set_drvdata(pdev, NULL); | |
279 | } | |
280 | return 0; | |
281 | } | |
282 | ||
283 | static 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; | |
b61401ad | 288 | int i, ret; |
c6a5951e SM |
289 | 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 | ||
b61401ad SM |
312 | 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 | ||
c6a5951e SM |
324 | /* 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 | ||
2e8c8ba9 SM |
333 | /* Set VMID = 0 */ |
334 | SET_VMID(drvdata->base, mid, 0); | |
c6a5951e SM |
335 | |
336 | /* Set the context number for that MID to this context */ | |
337 | SET_CBNDX(drvdata->base, mid, c->num); | |
338 | ||
2e8c8ba9 SM |
339 | /* Set MID associated with this context bank to 0*/ |
340 | SET_CBVMID(drvdata->base, c->num, 0); | |
341 | ||
342 | /* Set the ASID for TLB tagging for this context */ | |
343 | SET_CONTEXTIDR_ASID(drvdata->base, c->num, c->num); | |
c6a5951e SM |
344 | |
345 | /* Set security bit override to be Non-secure */ | |
346 | SET_NSCFG(drvdata->base, mid, 3); | |
347 | } | |
348 | ||
b61401ad SM |
349 | if (drvdata->clk) |
350 | clk_disable(drvdata->clk); | |
351 | clk_disable(drvdata->pclk); | |
c6a5951e | 352 | |
b61401ad | 353 | dev_info(&pdev->dev, "context %s using bank %d\n", c->name, c->num); |
c6a5951e SM |
354 | return 0; |
355 | fail: | |
356 | kfree(ctx_drvdata); | |
357 | return ret; | |
358 | } | |
359 | ||
360 | static int msm_iommu_ctx_remove(struct platform_device *pdev) | |
361 | { | |
362 | struct msm_iommu_ctx_drvdata *drv = NULL; | |
363 | drv = platform_get_drvdata(pdev); | |
364 | if (drv) { | |
365 | memset(drv, 0, sizeof(struct msm_iommu_ctx_drvdata)); | |
366 | kfree(drv); | |
367 | platform_set_drvdata(pdev, NULL); | |
368 | } | |
369 | return 0; | |
370 | } | |
371 | ||
372 | static struct platform_driver msm_iommu_driver = { | |
373 | .driver = { | |
374 | .name = "msm_iommu", | |
375 | }, | |
376 | .probe = msm_iommu_probe, | |
377 | .remove = msm_iommu_remove, | |
378 | }; | |
379 | ||
380 | static struct platform_driver msm_iommu_ctx_driver = { | |
381 | .driver = { | |
382 | .name = "msm_iommu_ctx", | |
383 | }, | |
384 | .probe = msm_iommu_ctx_probe, | |
385 | .remove = msm_iommu_ctx_remove, | |
386 | }; | |
387 | ||
516cbc79 | 388 | static int __init msm_iommu_driver_init(void) |
c6a5951e SM |
389 | { |
390 | int ret; | |
391 | ret = platform_driver_register(&msm_iommu_driver); | |
392 | if (ret != 0) { | |
393 | pr_err("Failed to register IOMMU driver\n"); | |
394 | goto error; | |
395 | } | |
396 | ||
397 | ret = platform_driver_register(&msm_iommu_ctx_driver); | |
398 | if (ret != 0) { | |
399 | pr_err("Failed to register IOMMU context driver\n"); | |
400 | goto error; | |
401 | } | |
402 | ||
403 | error: | |
404 | return ret; | |
405 | } | |
406 | ||
516cbc79 | 407 | static void __exit msm_iommu_driver_exit(void) |
c6a5951e SM |
408 | { |
409 | platform_driver_unregister(&msm_iommu_ctx_driver); | |
410 | platform_driver_unregister(&msm_iommu_driver); | |
411 | } | |
412 | ||
413 | subsys_initcall(msm_iommu_driver_init); | |
414 | module_exit(msm_iommu_driver_exit); | |
415 | ||
416 | MODULE_LICENSE("GPL v2"); | |
417 | MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>"); |