blob: 67e8f531b7af413e518f0072f52df5a1ea8b043e [file] [log] [blame]
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001/* Copyright (c) 2010, 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 * 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#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
22#include <linux/errno.h>
23#include <linux/io.h>
24#include <linux/interrupt.h>
25#include <linux/list.h>
26#include <linux/spinlock.h>
27#include <linux/slab.h>
28#include <linux/iommu.h>
29
30#include <asm/cacheflush.h>
31#include <asm/sizes.h>
32
33#include <mach/iommu_hw-8xxx.h>
34#include <mach/iommu.h>
35
36DEFINE_SPINLOCK(msm_iommu_lock);
37
38struct msm_priv {
39 unsigned long *pgtable;
40 struct list_head list_attached;
41};
42
43static void __flush_iotlb(struct iommu_domain *domain)
44{
45 struct msm_priv *priv = domain->priv;
46 struct msm_iommu_drvdata *iommu_drvdata;
47 struct msm_iommu_ctx_drvdata *ctx_drvdata;
48
49#ifndef CONFIG_IOMMU_PGTABLES_L2
50 unsigned long *fl_table = priv->pgtable;
51 int i;
52
Stepan Moskovchenkof6f41eb2010-11-12 19:29:54 -080053 if (!list_empty(&priv->list_attached)) {
54 dmac_flush_range(fl_table, fl_table + SZ_16K);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -070055
Stepan Moskovchenkof6f41eb2010-11-12 19:29:54 -080056 for (i = 0; i < NUM_FL_PTE; i++)
57 if ((fl_table[i] & 0x03) == FL_TYPE_TABLE) {
58 void *sl_table = __va(fl_table[i] &
59 FL_BASE_MASK);
60 dmac_flush_range(sl_table, sl_table + SZ_4K);
61 }
62 }
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -070063#endif
64
65 list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) {
66 if (!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent)
67 BUG();
68
69 iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
70 SET_CTX_TLBIALL(iommu_drvdata->base, ctx_drvdata->num, 0);
71 }
72}
73
74static void __reset_context(void __iomem *base, int ctx)
75{
76 SET_BPRCOSH(base, ctx, 0);
77 SET_BPRCISH(base, ctx, 0);
78 SET_BPRCNSH(base, ctx, 0);
79 SET_BPSHCFG(base, ctx, 0);
80 SET_BPMTCFG(base, ctx, 0);
81 SET_ACTLR(base, ctx, 0);
82 SET_SCTLR(base, ctx, 0);
83 SET_FSRRESTORE(base, ctx, 0);
84 SET_TTBR0(base, ctx, 0);
85 SET_TTBR1(base, ctx, 0);
86 SET_TTBCR(base, ctx, 0);
87 SET_BFBCR(base, ctx, 0);
88 SET_PAR(base, ctx, 0);
89 SET_FAR(base, ctx, 0);
90 SET_CTX_TLBIALL(base, ctx, 0);
91 SET_TLBFLPTER(base, ctx, 0);
92 SET_TLBSLPTER(base, ctx, 0);
93 SET_TLBLKCR(base, ctx, 0);
94 SET_PRRR(base, ctx, 0);
95 SET_NMRR(base, ctx, 0);
96 SET_CONTEXTIDR(base, ctx, 0);
97}
98
99static void __program_context(void __iomem *base, int ctx, phys_addr_t pgtable)
100{
101 __reset_context(base, ctx);
102
103 /* Set up HTW mode */
104 /* TLB miss configuration: perform HTW on miss */
105 SET_TLBMCFG(base, ctx, 0x3);
106
107 /* V2P configuration: HTW for access */
108 SET_V2PCFG(base, ctx, 0x3);
109
110 SET_TTBCR(base, ctx, 0);
111 SET_TTBR0_PA(base, ctx, (pgtable >> 14));
112
113 /* Invalidate the TLB for this context */
114 SET_CTX_TLBIALL(base, ctx, 0);
115
116 /* Set interrupt number to "secure" interrupt */
117 SET_IRPTNDX(base, ctx, 0);
118
119 /* Enable context fault interrupt */
120 SET_CFEIE(base, ctx, 1);
121
122 /* Stall access on a context fault and let the handler deal with it */
123 SET_CFCFG(base, ctx, 1);
124
125 /* Redirect all cacheable requests to L2 slave port. */
126 SET_RCISH(base, ctx, 1);
127 SET_RCOSH(base, ctx, 1);
128 SET_RCNSH(base, ctx, 1);
129
130 /* Turn on TEX Remap */
131 SET_TRE(base, ctx, 1);
132
133 /* Do not configure PRRR / NMRR on the IOMMU for now. We will assume
134 * TEX class 0 for everything until attributes are properly worked out
135 */
136 SET_PRRR(base, ctx, 0);
137 SET_NMRR(base, ctx, 0);
138
139 /* Turn on BFB prefetch */
140 SET_BFBDFE(base, ctx, 1);
141
142#ifdef CONFIG_IOMMU_PGTABLES_L2
143 /* Configure page tables as inner-cacheable and shareable to reduce
144 * the TLB miss penalty.
145 */
146 SET_TTBR0_SH(base, ctx, 1);
147 SET_TTBR1_SH(base, ctx, 1);
148
149 SET_TTBR0_NOS(base, ctx, 1);
150 SET_TTBR1_NOS(base, ctx, 1);
151
152 SET_TTBR0_IRGNH(base, ctx, 0); /* WB, WA */
153 SET_TTBR0_IRGNL(base, ctx, 1);
154
155 SET_TTBR1_IRGNH(base, ctx, 0); /* WB, WA */
156 SET_TTBR1_IRGNL(base, ctx, 1);
157
158 SET_TTBR0_ORGN(base, ctx, 1); /* WB, WA */
159 SET_TTBR1_ORGN(base, ctx, 1); /* WB, WA */
160#endif
161
162 /* Enable the MMU */
163 SET_M(base, ctx, 1);
164}
165
166static int msm_iommu_domain_init(struct iommu_domain *domain)
167{
168 struct msm_priv *priv = kzalloc(sizeof(*priv), GFP_KERNEL);
169
170 if (!priv)
171 goto fail_nomem;
172
173 INIT_LIST_HEAD(&priv->list_attached);
174 priv->pgtable = (unsigned long *)__get_free_pages(GFP_KERNEL,
175 get_order(SZ_16K));
176
177 if (!priv->pgtable)
178 goto fail_nomem;
179
180 memset(priv->pgtable, 0, SZ_16K);
181 domain->priv = priv;
182 return 0;
183
184fail_nomem:
185 kfree(priv);
186 return -ENOMEM;
187}
188
189static void msm_iommu_domain_destroy(struct iommu_domain *domain)
190{
191 struct msm_priv *priv;
192 unsigned long flags;
193 unsigned long *fl_table;
194 int i;
195
196 spin_lock_irqsave(&msm_iommu_lock, flags);
197 priv = domain->priv;
198 domain->priv = NULL;
199
200 if (priv) {
201 fl_table = priv->pgtable;
202
203 for (i = 0; i < NUM_FL_PTE; i++)
204 if ((fl_table[i] & 0x03) == FL_TYPE_TABLE)
205 free_page((unsigned long) __va(((fl_table[i]) &
206 FL_BASE_MASK)));
207
208 free_pages((unsigned long)priv->pgtable, get_order(SZ_16K));
209 priv->pgtable = NULL;
210 }
211
212 kfree(priv);
213 spin_unlock_irqrestore(&msm_iommu_lock, flags);
214}
215
216static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
217{
218 struct msm_priv *priv;
219 struct msm_iommu_ctx_dev *ctx_dev;
220 struct msm_iommu_drvdata *iommu_drvdata;
221 struct msm_iommu_ctx_drvdata *ctx_drvdata;
222 struct msm_iommu_ctx_drvdata *tmp_drvdata;
223 int ret = 0;
224 unsigned long flags;
225
226 spin_lock_irqsave(&msm_iommu_lock, flags);
227
228 priv = domain->priv;
229
230 if (!priv || !dev) {
231 ret = -EINVAL;
232 goto fail;
233 }
234
235 iommu_drvdata = dev_get_drvdata(dev->parent);
236 ctx_drvdata = dev_get_drvdata(dev);
237 ctx_dev = dev->platform_data;
238
239 if (!iommu_drvdata || !ctx_drvdata || !ctx_dev) {
240 ret = -EINVAL;
241 goto fail;
242 }
243
Stepan Moskovchenko00d4b2b2010-11-12 19:29:56 -0800244 if (!list_empty(&ctx_drvdata->attached_elm)) {
245 ret = -EBUSY;
246 goto fail;
247 }
248
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700249 list_for_each_entry(tmp_drvdata, &priv->list_attached, attached_elm)
250 if (tmp_drvdata == ctx_drvdata) {
251 ret = -EBUSY;
252 goto fail;
253 }
254
255 __program_context(iommu_drvdata->base, ctx_dev->num,
256 __pa(priv->pgtable));
257
258 list_add(&(ctx_drvdata->attached_elm), &priv->list_attached);
259 __flush_iotlb(domain);
260
261fail:
262 spin_unlock_irqrestore(&msm_iommu_lock, flags);
263 return ret;
264}
265
266static void msm_iommu_detach_dev(struct iommu_domain *domain,
267 struct device *dev)
268{
269 struct msm_priv *priv;
270 struct msm_iommu_ctx_dev *ctx_dev;
271 struct msm_iommu_drvdata *iommu_drvdata;
272 struct msm_iommu_ctx_drvdata *ctx_drvdata;
273 unsigned long flags;
274
275 spin_lock_irqsave(&msm_iommu_lock, flags);
276 priv = domain->priv;
277
278 if (!priv || !dev)
279 goto fail;
280
281 iommu_drvdata = dev_get_drvdata(dev->parent);
282 ctx_drvdata = dev_get_drvdata(dev);
283 ctx_dev = dev->platform_data;
284
285 if (!iommu_drvdata || !ctx_drvdata || !ctx_dev)
286 goto fail;
287
288 __flush_iotlb(domain);
289 __reset_context(iommu_drvdata->base, ctx_dev->num);
290 list_del_init(&ctx_drvdata->attached_elm);
291
292fail:
293 spin_unlock_irqrestore(&msm_iommu_lock, flags);
294}
295
296static int msm_iommu_map(struct iommu_domain *domain, unsigned long va,
297 phys_addr_t pa, int order, int prot)
298{
299 struct msm_priv *priv;
300 unsigned long flags;
301 unsigned long *fl_table;
302 unsigned long *fl_pte;
303 unsigned long fl_offset;
304 unsigned long *sl_table;
305 unsigned long *sl_pte;
306 unsigned long sl_offset;
307 size_t len = 0x1000UL << order;
308 int ret = 0;
309
310 spin_lock_irqsave(&msm_iommu_lock, flags);
311 priv = domain->priv;
312
313 if (!priv) {
314 ret = -EINVAL;
315 goto fail;
316 }
317
318 fl_table = priv->pgtable;
319
320 if (len != SZ_16M && len != SZ_1M &&
321 len != SZ_64K && len != SZ_4K) {
322 pr_debug("Bad size: %d\n", len);
323 ret = -EINVAL;
324 goto fail;
325 }
326
327 if (!fl_table) {
328 pr_debug("Null page table\n");
329 ret = -EINVAL;
330 goto fail;
331 }
332
333 fl_offset = FL_OFFSET(va); /* Upper 12 bits */
334 fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */
335
336 if (len == SZ_16M) {
337 int i = 0;
338 for (i = 0; i < 16; i++)
339 *(fl_pte+i) = (pa & 0xFF000000) | FL_SUPERSECTION |
340 FL_AP_READ | FL_AP_WRITE | FL_TYPE_SECT |
341 FL_SHARED;
342 }
343
344 if (len == SZ_1M)
345 *fl_pte = (pa & 0xFFF00000) | FL_AP_READ | FL_AP_WRITE |
346 FL_TYPE_SECT | FL_SHARED;
347
348 /* Need a 2nd level table */
349 if ((len == SZ_4K || len == SZ_64K) && (*fl_pte) == 0) {
350 unsigned long *sl;
351 sl = (unsigned long *) __get_free_pages(GFP_KERNEL,
352 get_order(SZ_4K));
353
354 if (!sl) {
355 pr_debug("Could not allocate second level table\n");
356 ret = -ENOMEM;
357 goto fail;
358 }
359
360 memset(sl, 0, SZ_4K);
361 *fl_pte = ((((int)__pa(sl)) & FL_BASE_MASK) | FL_TYPE_TABLE);
362 }
363
364 sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK));
365 sl_offset = SL_OFFSET(va);
366 sl_pte = sl_table + sl_offset;
367
368
369 if (len == SZ_4K)
370 *sl_pte = (pa & SL_BASE_MASK_SMALL) | SL_AP0 | SL_AP1 |
371 SL_SHARED | SL_TYPE_SMALL;
372
373 if (len == SZ_64K) {
374 int i;
375
376 for (i = 0; i < 16; i++)
377 *(sl_pte+i) = (pa & SL_BASE_MASK_LARGE) | SL_AP0 |
378 SL_AP1 | SL_SHARED | SL_TYPE_LARGE;
379 }
380
381 __flush_iotlb(domain);
382fail:
383 spin_unlock_irqrestore(&msm_iommu_lock, flags);
384 return ret;
385}
386
387static int msm_iommu_unmap(struct iommu_domain *domain, unsigned long va,
388 int order)
389{
390 struct msm_priv *priv;
391 unsigned long flags;
392 unsigned long *fl_table;
393 unsigned long *fl_pte;
394 unsigned long fl_offset;
395 unsigned long *sl_table;
396 unsigned long *sl_pte;
397 unsigned long sl_offset;
398 size_t len = 0x1000UL << order;
399 int i, ret = 0;
400
401 spin_lock_irqsave(&msm_iommu_lock, flags);
402
403 priv = domain->priv;
404
405 if (!priv) {
406 ret = -ENODEV;
407 goto fail;
408 }
409
410 fl_table = priv->pgtable;
411
412 if (len != SZ_16M && len != SZ_1M &&
413 len != SZ_64K && len != SZ_4K) {
414 pr_debug("Bad length: %d\n", len);
415 ret = -EINVAL;
416 goto fail;
417 }
418
419 if (!fl_table) {
420 pr_debug("Null page table\n");
421 ret = -EINVAL;
422 goto fail;
423 }
424
425 fl_offset = FL_OFFSET(va); /* Upper 12 bits */
426 fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */
427
428 if (*fl_pte == 0) {
429 pr_debug("First level PTE is 0\n");
430 ret = -ENODEV;
431 goto fail;
432 }
433
434 /* Unmap supersection */
435 if (len == SZ_16M)
436 for (i = 0; i < 16; i++)
437 *(fl_pte+i) = 0;
438
439 if (len == SZ_1M)
440 *fl_pte = 0;
441
442 sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK));
443 sl_offset = SL_OFFSET(va);
444 sl_pte = sl_table + sl_offset;
445
446 if (len == SZ_64K) {
447 for (i = 0; i < 16; i++)
448 *(sl_pte+i) = 0;
449 }
450
451 if (len == SZ_4K)
452 *sl_pte = 0;
453
454 if (len == SZ_4K || len == SZ_64K) {
455 int used = 0;
456
457 for (i = 0; i < NUM_SL_PTE; i++)
458 if (sl_table[i])
459 used = 1;
460 if (!used) {
461 free_page((unsigned long)sl_table);
462 *fl_pte = 0;
463 }
464 }
465
466 __flush_iotlb(domain);
467fail:
468 spin_unlock_irqrestore(&msm_iommu_lock, flags);
469 return ret;
470}
471
472static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain,
473 unsigned long va)
474{
475 struct msm_priv *priv;
476 struct msm_iommu_drvdata *iommu_drvdata;
477 struct msm_iommu_ctx_drvdata *ctx_drvdata;
478 unsigned int par;
479 unsigned long flags;
480 void __iomem *base;
481 phys_addr_t ret = 0;
482 int ctx;
483
484 spin_lock_irqsave(&msm_iommu_lock, flags);
485
486 priv = domain->priv;
487 if (list_empty(&priv->list_attached))
488 goto fail;
489
490 ctx_drvdata = list_entry(priv->list_attached.next,
491 struct msm_iommu_ctx_drvdata, attached_elm);
492 iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
493
494 base = iommu_drvdata->base;
495 ctx = ctx_drvdata->num;
496
497 /* Invalidate context TLB */
498 SET_CTX_TLBIALL(base, ctx, 0);
499 SET_V2PPR_VA(base, ctx, va >> V2Pxx_VA_SHIFT);
500
501 if (GET_FAULT(base, ctx))
502 goto fail;
503
504 par = GET_PAR(base, ctx);
505
506 /* We are dealing with a supersection */
507 if (GET_NOFAULT_SS(base, ctx))
508 ret = (par & 0xFF000000) | (va & 0x00FFFFFF);
509 else /* Upper 20 bits from PAR, lower 12 from VA */
510 ret = (par & 0xFFFFF000) | (va & 0x00000FFF);
511
512fail:
513 spin_unlock_irqrestore(&msm_iommu_lock, flags);
514 return ret;
515}
516
517static int msm_iommu_domain_has_cap(struct iommu_domain *domain,
518 unsigned long cap)
519{
520 return 0;
521}
522
523static void print_ctx_regs(void __iomem *base, int ctx)
524{
525 unsigned int fsr = GET_FSR(base, ctx);
526 pr_err("FAR = %08x PAR = %08x\n",
527 GET_FAR(base, ctx), GET_PAR(base, ctx));
528 pr_err("FSR = %08x [%s%s%s%s%s%s%s%s%s%s]\n", fsr,
529 (fsr & 0x02) ? "TF " : "",
530 (fsr & 0x04) ? "AFF " : "",
531 (fsr & 0x08) ? "APF " : "",
532 (fsr & 0x10) ? "TLBMF " : "",
533 (fsr & 0x20) ? "HTWDEEF " : "",
534 (fsr & 0x40) ? "HTWSEEF " : "",
535 (fsr & 0x80) ? "MHF " : "",
536 (fsr & 0x10000) ? "SL " : "",
537 (fsr & 0x40000000) ? "SS " : "",
538 (fsr & 0x80000000) ? "MULTI " : "");
539
540 pr_err("FSYNR0 = %08x FSYNR1 = %08x\n",
541 GET_FSYNR0(base, ctx), GET_FSYNR1(base, ctx));
542 pr_err("TTBR0 = %08x TTBR1 = %08x\n",
543 GET_TTBR0(base, ctx), GET_TTBR1(base, ctx));
544 pr_err("SCTLR = %08x ACTLR = %08x\n",
545 GET_SCTLR(base, ctx), GET_ACTLR(base, ctx));
546 pr_err("PRRR = %08x NMRR = %08x\n",
547 GET_PRRR(base, ctx), GET_NMRR(base, ctx));
548}
549
550irqreturn_t msm_iommu_fault_handler(int irq, void *dev_id)
551{
552 struct msm_iommu_drvdata *drvdata = dev_id;
553 void __iomem *base;
554 unsigned int fsr = 0;
555 int ncb = 0, i = 0;
556
557 spin_lock(&msm_iommu_lock);
558
559 if (!drvdata) {
560 pr_err("Invalid device ID in context interrupt handler\n");
561 goto fail;
562 }
563
564 base = drvdata->base;
565
566 pr_err("===== WOAH! =====\n");
567 pr_err("Unexpected IOMMU page fault!\n");
568 pr_err("base = %08x\n", (unsigned int) base);
569
570 ncb = GET_NCB(base)+1;
571 for (i = 0; i < ncb; i++) {
572 fsr = GET_FSR(base, i);
573 if (fsr) {
574 pr_err("Fault occurred in context %d.\n", i);
575 pr_err("Interesting registers:\n");
576 print_ctx_regs(base, i);
577 SET_FSR(base, i, 0x4000000F);
578 }
579 }
580fail:
581 spin_unlock(&msm_iommu_lock);
582 return 0;
583}
584
585static struct iommu_ops msm_iommu_ops = {
586 .domain_init = msm_iommu_domain_init,
587 .domain_destroy = msm_iommu_domain_destroy,
588 .attach_dev = msm_iommu_attach_dev,
589 .detach_dev = msm_iommu_detach_dev,
590 .map = msm_iommu_map,
591 .unmap = msm_iommu_unmap,
592 .iova_to_phys = msm_iommu_iova_to_phys,
593 .domain_has_cap = msm_iommu_domain_has_cap
594};
595
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -0800596static int __init msm_iommu_init(void)
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700597{
598 register_iommu(&msm_iommu_ops);
599 return 0;
600}
601
602subsys_initcall(msm_iommu_init);
603
604MODULE_LICENSE("GPL v2");
605MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");