blob: 6e62e60493f45b1291731f07bec11fd0d728e6c6 [file] [log] [blame]
Steve Mucklef132c6c2012-06-06 18:30:57 -07001/* Copyright (c) 2012 Code Aurora Forum. All rights reserved.
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
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/platform_device.h>
17#include <linux/errno.h>
18#include <linux/io.h>
19#include <linux/interrupt.h>
20#include <linux/list.h>
21#include <linux/mutex.h>
22#include <linux/slab.h>
23#include <linux/iommu.h>
24#include <linux/clk.h>
25#include <linux/scatterlist.h>
Sathish Ambleycf045e62012-06-07 12:56:50 -070026#include <linux/of.h>
27#include <linux/of_device.h>
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -070028#include <linux/regulator/consumer.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070029#include <asm/sizes.h>
30
31#include <mach/iommu_hw-v2.h>
32#include <mach/iommu.h>
33
34#include "msm_iommu_pagetable.h"
35
36/* bitmap of the page sizes currently supported */
37#define MSM_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
38
39static DEFINE_MUTEX(msm_iommu_lock);
40
41struct msm_priv {
42 struct iommu_pt pt;
43 struct list_head list_attached;
44};
45
46static int __enable_clocks(struct msm_iommu_drvdata *drvdata)
47{
48 int ret;
49
50 ret = clk_prepare_enable(drvdata->pclk);
51 if (ret)
52 goto fail;
53
54 if (drvdata->clk) {
55 ret = clk_prepare_enable(drvdata->clk);
56 if (ret)
57 clk_disable_unprepare(drvdata->pclk);
58 }
59fail:
60 return ret;
61}
62
63static void __disable_clocks(struct msm_iommu_drvdata *drvdata)
64{
65 if (drvdata->clk)
66 clk_disable_unprepare(drvdata->clk);
67 clk_disable_unprepare(drvdata->pclk);
68}
69
70static int __flush_iotlb_va(struct iommu_domain *domain, unsigned int va)
71{
72 struct msm_priv *priv = domain->priv;
73 struct msm_iommu_drvdata *iommu_drvdata;
74 struct msm_iommu_ctx_drvdata *ctx_drvdata;
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -070075 int ret = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -070076 int asid;
77
78 list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) {
79 BUG_ON(!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent);
80
81 iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
82 BUG_ON(!iommu_drvdata);
83
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -070084
85 ret = __enable_clocks(iommu_drvdata);
86 if (ret)
87 goto fail;
88
Steve Mucklef132c6c2012-06-06 18:30:57 -070089 asid = GET_CB_CONTEXTIDR_ASID(iommu_drvdata->base,
90 ctx_drvdata->num);
91
92 SET_TLBIVA(iommu_drvdata->base, ctx_drvdata->num,
93 asid | (va & CB_TLBIVA_VA));
94 mb();
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -070095 __disable_clocks(iommu_drvdata);
Steve Mucklef132c6c2012-06-06 18:30:57 -070096 }
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -070097fail:
98 return ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -070099}
100
101static int __flush_iotlb(struct iommu_domain *domain)
102{
103 struct msm_priv *priv = domain->priv;
104 struct msm_iommu_drvdata *iommu_drvdata;
105 struct msm_iommu_ctx_drvdata *ctx_drvdata;
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700106 int ret = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700107 int asid;
108
109 list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) {
110 BUG_ON(!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent);
111
112 iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
113 BUG_ON(!iommu_drvdata);
114
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700115 ret = __enable_clocks(iommu_drvdata);
116 if (ret)
117 goto fail;
118
Steve Mucklef132c6c2012-06-06 18:30:57 -0700119 asid = GET_CB_CONTEXTIDR_ASID(iommu_drvdata->base,
120 ctx_drvdata->num);
121
122 SET_TLBIASID(iommu_drvdata->base, ctx_drvdata->num, asid);
123 mb();
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700124 __disable_clocks(iommu_drvdata);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700125 }
126
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700127fail:
128 return ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700129}
130
Sathish Ambleycf045e62012-06-07 12:56:50 -0700131static void __reset_iommu(void __iomem *base)
132{
133 int i;
134
135 SET_ACR(base, 0);
136 SET_NSACR(base, 0);
137 SET_CR2(base, 0);
138 SET_NSCR2(base, 0);
139 SET_GFAR(base, 0);
140 SET_GFSRRESTORE(base, 0);
141 SET_TLBIALLNSNH(base, 0);
142 SET_PMCR(base, 0);
143 SET_SCR1(base, 0);
144 SET_SSDR_N(base, 0, 0);
145
146 for (i = 0; i < MAX_NUM_SMR; i++)
147 SET_SMR_VALID(base, i, 0);
148
149 mb();
150}
151
152static void __program_iommu(void __iomem *base)
153{
154 __reset_iommu(base);
155
156 SET_CR0_SMCFCFG(base, 1);
157 SET_CR0_USFCFG(base, 1);
158 SET_CR0_STALLD(base, 1);
159 SET_CR0_GCFGFIE(base, 1);
160 SET_CR0_GCFGFRE(base, 1);
161 SET_CR0_GFIE(base, 1);
162 SET_CR0_GFRE(base, 1);
163 SET_CR0_CLIENTPD(base, 0);
164 mb(); /* Make sure writes complete before returning */
165}
166
Steve Mucklef132c6c2012-06-06 18:30:57 -0700167static void __reset_context(void __iomem *base, int ctx)
168{
169 SET_ACTLR(base, ctx, 0);
170 SET_FAR(base, ctx, 0);
171 SET_FSRRESTORE(base, ctx, 0);
172 SET_NMRR(base, ctx, 0);
173 SET_PAR(base, ctx, 0);
174 SET_PRRR(base, ctx, 0);
175 SET_SCTLR(base, ctx, 0);
176 SET_TLBIALL(base, ctx, 0);
177 SET_TTBCR(base, ctx, 0);
178 SET_TTBR0(base, ctx, 0);
179 SET_TTBR1(base, ctx, 0);
180 mb();
181}
182
183static void __program_context(void __iomem *base, int ctx, int ncb,
Sathish Ambleycf045e62012-06-07 12:56:50 -0700184 phys_addr_t pgtable, int redirect,
185 u32 *sids, int len)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700186{
187 unsigned int prrr, nmrr;
188 unsigned int pn;
Sathish Ambleycf045e62012-06-07 12:56:50 -0700189 int i, j, found, num = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700190
191 __reset_context(base, ctx);
192
193 pn = pgtable >> CB_TTBR0_ADDR_SHIFT;
194 SET_TTBCR(base, ctx, 0);
195 SET_CB_TTBR0_ADDR(base, ctx, pn);
196
197 /* Enable context fault interrupt */
198 SET_CB_SCTLR_CFIE(base, ctx, 1);
199
200 /* Redirect all cacheable requests to L2 slave port. */
201 SET_CB_ACTLR_BPRCISH(base, ctx, 1);
202 SET_CB_ACTLR_BPRCOSH(base, ctx, 1);
203 SET_CB_ACTLR_BPRCNSH(base, ctx, 1);
204
205 /* Turn on TEX Remap */
206 SET_CB_SCTLR_TRE(base, ctx, 1);
207
208 /* Enable private ASID namespace */
209 SET_CB_SCTLR_ASIDPNE(base, ctx, 1);
210
211 /* Set TEX remap attributes */
212 RCP15_PRRR(prrr);
213 RCP15_NMRR(nmrr);
214 SET_PRRR(base, ctx, prrr);
215 SET_NMRR(base, ctx, nmrr);
216
217 /* Configure page tables as inner-cacheable and shareable to reduce
218 * the TLB miss penalty.
219 */
220 if (redirect) {
221 SET_CB_TTBR0_S(base, ctx, 1);
222 SET_CB_TTBR0_NOS(base, ctx, 1);
223 SET_CB_TTBR0_IRGN1(base, ctx, 0); /* WB, WA */
224 SET_CB_TTBR0_IRGN0(base, ctx, 1);
225 SET_CB_TTBR0_RGN(base, ctx, 1); /* WB, WA */
226 }
227
Sathish Ambleycf045e62012-06-07 12:56:50 -0700228 /* Program the M2V tables for this context */
229 for (i = 0; i < len / sizeof(*sids); i++) {
230 for (; num < MAX_NUM_SMR; num++)
231 if (GET_SMR_VALID(base, num) == 0)
232 break;
233 BUG_ON(num >= MAX_NUM_SMR);
234
235 SET_SMR_VALID(base, num, 1);
236 SET_SMR_MASK(base, num, 0);
237 SET_SMR_ID(base, num, sids[i]);
238
239 /* Set VMID = 0 */
240 SET_S2CR_N(base, num, 0);
241 SET_S2CR_CBNDX(base, num, ctx);
242 /* Set security bit override to be Non-secure */
243 SET_S2CR_NSCFG(base, sids[i], 3);
244
245 SET_CBAR_N(base, ctx, 0);
246 /* Stage 1 Context with Stage 2 bypass */
247 SET_CBAR_TYPE(base, ctx, 1);
248 /* Route page faults to the non-secure interrupt */
249 SET_CBAR_IRPTNDX(base, ctx, 1);
250 }
251
Steve Mucklef132c6c2012-06-06 18:30:57 -0700252 /* Find if this page table is used elsewhere, and re-use ASID */
253 found = 0;
254 for (i = 0; i < ncb; i++)
255 if ((GET_CB_TTBR0_ADDR(base, i) == pn) && (i != ctx)) {
256 SET_CB_CONTEXTIDR_ASID(base, ctx, \
257 GET_CB_CONTEXTIDR_ASID(base, i));
258 found = 1;
259 break;
260 }
261
262 /* If page table is new, find an unused ASID */
263 if (!found) {
264 for (i = 0; i < ncb; i++) {
265 found = 0;
266 for (j = 0; j < ncb; j++) {
267 if (GET_CB_CONTEXTIDR_ASID(base, j) == i &&
268 j != ctx)
269 found = 1;
270 }
271
272 if (!found) {
273 SET_CB_CONTEXTIDR_ASID(base, ctx, i);
274 break;
275 }
276 }
277 BUG_ON(found);
278 }
279
280 /* Enable the MMU */
281 SET_CB_SCTLR_M(base, ctx, 1);
282 mb();
283}
284
285static int msm_iommu_domain_init(struct iommu_domain *domain, int flags)
286{
287 struct msm_priv *priv;
288
289 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
290 if (!priv)
291 goto fail_nomem;
292
293#ifdef CONFIG_IOMMU_PGTABLES_L2
294 priv->pt.redirect = flags & MSM_IOMMU_DOMAIN_PT_CACHEABLE;
295#endif
296
297 INIT_LIST_HEAD(&priv->list_attached);
298 if (msm_iommu_pagetable_alloc(&priv->pt))
299 goto fail_nomem;
300
301 domain->priv = priv;
302 return 0;
303
304fail_nomem:
305 kfree(priv);
306 return -ENOMEM;
307}
308
309static void msm_iommu_domain_destroy(struct iommu_domain *domain)
310{
311 struct msm_priv *priv;
312
313 mutex_lock(&msm_iommu_lock);
314 priv = domain->priv;
315 domain->priv = NULL;
316
317 if (priv)
318 msm_iommu_pagetable_free(&priv->pt);
319
320 kfree(priv);
321 mutex_unlock(&msm_iommu_lock);
322}
323
Sathish Ambleycf045e62012-06-07 12:56:50 -0700324static int msm_iommu_ctx_attached(struct device *dev)
325{
326 struct platform_device *pdev;
327 struct device_node *child;
328 struct msm_iommu_ctx_drvdata *ctx;
329
330 for_each_child_of_node(dev->of_node, child) {
331 pdev = of_find_device_by_node(child);
332
333 ctx = dev_get_drvdata(&pdev->dev);
334 if (ctx->attached_domain) {
335 of_node_put(child);
336 return 1;
337 }
338 }
339
340 return 0;
341}
342
Steve Mucklef132c6c2012-06-06 18:30:57 -0700343static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
344{
345 struct msm_priv *priv;
346 struct msm_iommu_drvdata *iommu_drvdata;
347 struct msm_iommu_ctx_drvdata *ctx_drvdata;
348 struct msm_iommu_ctx_drvdata *tmp_drvdata;
Sathish Ambleycf045e62012-06-07 12:56:50 -0700349 u32 sids[MAX_NUM_SMR];
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700350 int len = 0, ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700351
352 mutex_lock(&msm_iommu_lock);
353
354 priv = domain->priv;
355 if (!priv || !dev) {
356 ret = -EINVAL;
357 goto fail;
358 }
359
360 iommu_drvdata = dev_get_drvdata(dev->parent);
361 ctx_drvdata = dev_get_drvdata(dev);
362 if (!iommu_drvdata || !ctx_drvdata) {
363 ret = -EINVAL;
364 goto fail;
365 }
366
367 if (!list_empty(&ctx_drvdata->attached_elm)) {
368 ret = -EBUSY;
369 goto fail;
370 }
371
372 list_for_each_entry(tmp_drvdata, &priv->list_attached, attached_elm)
373 if (tmp_drvdata == ctx_drvdata) {
374 ret = -EBUSY;
375 goto fail;
376 }
377
Sathish Ambleycf045e62012-06-07 12:56:50 -0700378 of_get_property(dev->of_node, "qcom,iommu-ctx-sids", &len);
379 BUG_ON(len >= sizeof(sids));
380 if (of_property_read_u32_array(dev->of_node, "qcom,iommu-ctx-sids",
381 sids, len / sizeof(*sids))) {
382 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700383 goto fail;
Sathish Ambleycf045e62012-06-07 12:56:50 -0700384 }
385
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -0700386 ret = regulator_enable(iommu_drvdata->gdsc);
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700387 if (ret)
388 goto fail;
389
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -0700390 ret = __enable_clocks(iommu_drvdata);
391 if (ret) {
392 regulator_disable(iommu_drvdata->gdsc);
393 goto fail;
394 }
395
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700396 if (!msm_iommu_ctx_attached(dev->parent))
Sathish Ambleycf045e62012-06-07 12:56:50 -0700397 __program_iommu(iommu_drvdata->base);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700398
399 __program_context(iommu_drvdata->base, ctx_drvdata->num,
400 iommu_drvdata->ncb, __pa(priv->pt.fl_table),
Sathish Ambleycf045e62012-06-07 12:56:50 -0700401 priv->pt.redirect, sids, len);
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700402 __disable_clocks(iommu_drvdata);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700403
Steve Mucklef132c6c2012-06-06 18:30:57 -0700404 list_add(&(ctx_drvdata->attached_elm), &priv->list_attached);
405 ctx_drvdata->attached_domain = domain;
406
407fail:
408 mutex_unlock(&msm_iommu_lock);
409 return ret;
410}
411
412static void msm_iommu_detach_dev(struct iommu_domain *domain,
413 struct device *dev)
414{
415 struct msm_priv *priv;
416 struct msm_iommu_drvdata *iommu_drvdata;
417 struct msm_iommu_ctx_drvdata *ctx_drvdata;
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700418 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700419
420 mutex_lock(&msm_iommu_lock);
421 priv = domain->priv;
422 if (!priv || !dev)
423 goto fail;
424
425 iommu_drvdata = dev_get_drvdata(dev->parent);
426 ctx_drvdata = dev_get_drvdata(dev);
427 if (!iommu_drvdata || !ctx_drvdata || !ctx_drvdata->attached_domain)
428 goto fail;
429
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700430 ret = __enable_clocks(iommu_drvdata);
431 if (ret)
432 goto fail;
433
Steve Mucklef132c6c2012-06-06 18:30:57 -0700434 SET_TLBIASID(iommu_drvdata->base, ctx_drvdata->num,
435 GET_CB_CONTEXTIDR_ASID(iommu_drvdata->base, ctx_drvdata->num));
436
437 __reset_context(iommu_drvdata->base, ctx_drvdata->num);
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700438 __disable_clocks(iommu_drvdata);
439
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -0700440 regulator_disable(iommu_drvdata->gdsc);
441
Steve Mucklef132c6c2012-06-06 18:30:57 -0700442 list_del_init(&ctx_drvdata->attached_elm);
443 ctx_drvdata->attached_domain = NULL;
444
Steve Mucklef132c6c2012-06-06 18:30:57 -0700445fail:
446 mutex_unlock(&msm_iommu_lock);
447}
448
449static int msm_iommu_map(struct iommu_domain *domain, unsigned long va,
450 phys_addr_t pa, size_t len, int prot)
451{
452 struct msm_priv *priv;
453 int ret = 0;
454
455 mutex_lock(&msm_iommu_lock);
456
457 priv = domain->priv;
458 if (!priv) {
459 ret = -EINVAL;
460 goto fail;
461 }
462
463 ret = msm_iommu_pagetable_map(&priv->pt, va, pa, len, prot);
464 if (ret)
465 goto fail;
466
467 ret = __flush_iotlb_va(domain, va);
468fail:
469 mutex_unlock(&msm_iommu_lock);
470 return ret;
471}
472
473static size_t msm_iommu_unmap(struct iommu_domain *domain, unsigned long va,
474 size_t len)
475{
476 struct msm_priv *priv;
477 int ret = -ENODEV;
478
479 mutex_lock(&msm_iommu_lock);
480
481 priv = domain->priv;
482 if (!priv)
483 goto fail;
484
485 ret = msm_iommu_pagetable_unmap(&priv->pt, va, len);
486 if (ret < 0)
487 goto fail;
488
489 ret = __flush_iotlb_va(domain, va);
490fail:
491 mutex_unlock(&msm_iommu_lock);
492
493 /* the IOMMU API requires us to return how many bytes were unmapped */
494 len = ret ? 0 : len;
495 return len;
496}
497
498static int msm_iommu_map_range(struct iommu_domain *domain, unsigned int va,
499 struct scatterlist *sg, unsigned int len,
500 int prot)
501{
502 int ret;
503 struct msm_priv *priv;
504
505 mutex_lock(&msm_iommu_lock);
506
507 priv = domain->priv;
508 if (!priv) {
509 ret = -EINVAL;
510 goto fail;
511 }
512
513 ret = msm_iommu_pagetable_map_range(&priv->pt, va, sg, len, prot);
514 if (ret)
515 goto fail;
516
517 __flush_iotlb(domain);
518fail:
519 mutex_unlock(&msm_iommu_lock);
520 return ret;
521}
522
523
524static int msm_iommu_unmap_range(struct iommu_domain *domain, unsigned int va,
525 unsigned int len)
526{
527 struct msm_priv *priv;
528
529 mutex_lock(&msm_iommu_lock);
530
531 priv = domain->priv;
532 msm_iommu_pagetable_unmap_range(&priv->pt, va, len);
533
534 __flush_iotlb(domain);
535 mutex_unlock(&msm_iommu_lock);
536 return 0;
537}
538
539static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain,
540 unsigned long va)
541{
542 struct msm_priv *priv;
543 struct msm_iommu_drvdata *iommu_drvdata;
544 struct msm_iommu_ctx_drvdata *ctx_drvdata;
545 unsigned int par;
546 void __iomem *base;
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700547 phys_addr_t ret = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700548 int ctx;
549
550 mutex_lock(&msm_iommu_lock);
551
552 priv = domain->priv;
553 if (list_empty(&priv->list_attached))
554 goto fail;
555
556 ctx_drvdata = list_entry(priv->list_attached.next,
557 struct msm_iommu_ctx_drvdata, attached_elm);
558 iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
559
560 base = iommu_drvdata->base;
561 ctx = ctx_drvdata->num;
562
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700563 ret = __enable_clocks(iommu_drvdata);
564 if (ret) {
565 ret = 0; /* 0 indicates translation failed */
566 goto fail;
567 }
568
Steve Mucklef132c6c2012-06-06 18:30:57 -0700569 SET_ATS1PR(base, ctx, va & CB_ATS1PR_ADDR);
570 mb();
571 while (GET_CB_ATSR_ACTIVE(base, ctx))
572 cpu_relax();
573
574 par = GET_PAR(base, ctx);
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700575 __disable_clocks(iommu_drvdata);
576
Steve Mucklef132c6c2012-06-06 18:30:57 -0700577 if (par & CB_PAR_F) {
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700578 ret = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700579 } else {
580 /* We are dealing with a supersection */
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700581 if (ret & CB_PAR_SS)
582 ret = (par & 0xFF000000) | (va & 0x00FFFFFF);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700583 else /* Upper 20 bits from PAR, lower 12 from VA */
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700584 ret = (par & 0xFFFFF000) | (va & 0x00000FFF);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700585 }
586
587fail:
588 mutex_unlock(&msm_iommu_lock);
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700589 return ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700590}
591
592static int msm_iommu_domain_has_cap(struct iommu_domain *domain,
593 unsigned long cap)
594{
595 return 0;
596}
597
598static void print_ctx_regs(void __iomem *base, int ctx, unsigned int fsr)
599{
600 pr_err("FAR = %08x PAR = %08x\n",
601 GET_FAR(base, ctx), GET_PAR(base, ctx));
602 pr_err("FSR = %08x [%s%s%s%s%s%s%s%s%s]\n", fsr,
603 (fsr & 0x02) ? "TF " : "",
604 (fsr & 0x04) ? "AFF " : "",
605 (fsr & 0x08) ? "PF " : "",
606 (fsr & 0x10) ? "EF " : "",
607 (fsr & 0x20) ? "TLBMCF " : "",
608 (fsr & 0x40) ? "TLBLKF " : "",
609 (fsr & 0x80) ? "MHF " : "",
610 (fsr & 0x40000000) ? "SS " : "",
611 (fsr & 0x80000000) ? "MULTI " : "");
612
613 pr_err("FSYNR0 = %08x FSYNR1 = %08x\n",
614 GET_FSYNR0(base, ctx), GET_FSYNR1(base, ctx));
615 pr_err("TTBR0 = %08x TTBR1 = %08x\n",
616 GET_TTBR0(base, ctx), GET_TTBR1(base, ctx));
617 pr_err("SCTLR = %08x ACTLR = %08x\n",
618 GET_SCTLR(base, ctx), GET_ACTLR(base, ctx));
619 pr_err("PRRR = %08x NMRR = %08x\n",
620 GET_PRRR(base, ctx), GET_NMRR(base, ctx));
621}
622
623irqreturn_t msm_iommu_fault_handler_v2(int irq, void *dev_id)
624{
625 struct platform_device *pdev = dev_id;
626 struct msm_iommu_drvdata *drvdata;
627 struct msm_iommu_ctx_drvdata *ctx_drvdata;
628 unsigned int fsr;
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700629 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700630
631 mutex_lock(&msm_iommu_lock);
632
633 BUG_ON(!pdev);
634
635 drvdata = dev_get_drvdata(pdev->dev.parent);
636 BUG_ON(!drvdata);
637
638 ctx_drvdata = dev_get_drvdata(&pdev->dev);
639 BUG_ON(!ctx_drvdata);
640
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700641 ret = __enable_clocks(drvdata);
642 if (ret) {
643 ret = IRQ_NONE;
644 goto fail;
645 }
646
Steve Mucklef132c6c2012-06-06 18:30:57 -0700647 fsr = GET_FSR(drvdata->base, ctx_drvdata->num);
648 if (fsr) {
649 if (!ctx_drvdata->attached_domain) {
650 pr_err("Bad domain in interrupt handler\n");
651 ret = -ENOSYS;
652 } else
653 ret = report_iommu_fault(ctx_drvdata->attached_domain,
654 &ctx_drvdata->pdev->dev,
655 GET_FAR(drvdata->base, ctx_drvdata->num), 0);
656
657 if (ret == -ENOSYS) {
658 pr_err("Unexpected IOMMU page fault!\n");
659 pr_err("name = %s\n", drvdata->name);
660 pr_err("context = %s (%d)\n", ctx_drvdata->name,
661 ctx_drvdata->num);
662 pr_err("Interesting registers:\n");
663 print_ctx_regs(drvdata->base, ctx_drvdata->num, fsr);
664 }
665
666 SET_FSR(drvdata->base, ctx_drvdata->num, fsr);
667 ret = IRQ_HANDLED;
668 } else
669 ret = IRQ_NONE;
670
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700671 __disable_clocks(drvdata);
672fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700673 mutex_unlock(&msm_iommu_lock);
674 return ret;
675}
676
677static phys_addr_t msm_iommu_get_pt_base_addr(struct iommu_domain *domain)
678{
679 struct msm_priv *priv = domain->priv;
680 return __pa(priv->pt.fl_table);
681}
682
683static struct iommu_ops msm_iommu_ops = {
684 .domain_init = msm_iommu_domain_init,
685 .domain_destroy = msm_iommu_domain_destroy,
686 .attach_dev = msm_iommu_attach_dev,
687 .detach_dev = msm_iommu_detach_dev,
688 .map = msm_iommu_map,
689 .unmap = msm_iommu_unmap,
690 .map_range = msm_iommu_map_range,
691 .unmap_range = msm_iommu_unmap_range,
692 .iova_to_phys = msm_iommu_iova_to_phys,
693 .domain_has_cap = msm_iommu_domain_has_cap,
694 .get_pt_base_addr = msm_iommu_get_pt_base_addr,
695 .pgsize_bitmap = MSM_IOMMU_PGSIZES,
696};
697
698static int __init msm_iommu_init(void)
699{
700 msm_iommu_pagetable_init();
701 bus_set_iommu(&platform_bus_type, &msm_iommu_ops);
702 return 0;
703}
704
705subsys_initcall(msm_iommu_init);
706
707MODULE_LICENSE("GPL v2");
708MODULE_DESCRIPTION("MSM SMMU v2 Driver");