Merge tag 'drm-fixes-for-v4.13-rc3' of git://people.freedesktop.org/~airlied/linux
[linux/fpc-iii.git] / drivers / iommu / omap-iommu.c
blob641e035cf86661b5da9239f8a60e28c252fa2f7e
1 /*
2 * omap iommu: tlb and pagetable primitives
4 * Copyright (C) 2008-2010 Nokia Corporation
6 * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>,
7 * Paul Mundt and Toshihiro Kobayashi
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/interrupt.h>
17 #include <linux/ioport.h>
18 #include <linux/platform_device.h>
19 #include <linux/iommu.h>
20 #include <linux/omap-iommu.h>
21 #include <linux/mutex.h>
22 #include <linux/spinlock.h>
23 #include <linux/io.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/of.h>
26 #include <linux/of_iommu.h>
27 #include <linux/of_irq.h>
28 #include <linux/of_platform.h>
29 #include <linux/regmap.h>
30 #include <linux/mfd/syscon.h>
32 #include <asm/cacheflush.h>
34 #include <linux/platform_data/iommu-omap.h>
36 #include "omap-iopgtable.h"
37 #include "omap-iommu.h"
39 static const struct iommu_ops omap_iommu_ops;
41 #define to_iommu(dev) \
42 ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
44 /* bitmap of the page sizes currently supported */
45 #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
47 #define MMU_LOCK_BASE_SHIFT 10
48 #define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
49 #define MMU_LOCK_BASE(x) \
50 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
52 #define MMU_LOCK_VICT_SHIFT 4
53 #define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
54 #define MMU_LOCK_VICT(x) \
55 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
57 static struct platform_driver omap_iommu_driver;
58 static struct kmem_cache *iopte_cachep;
60 /**
61 * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
62 * @dom: generic iommu domain handle
63 **/
64 static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
66 return container_of(dom, struct omap_iommu_domain, domain);
69 /**
70 * omap_iommu_save_ctx - Save registers for pm off-mode support
71 * @dev: client device
72 **/
73 void omap_iommu_save_ctx(struct device *dev)
75 struct omap_iommu *obj = dev_to_omap_iommu(dev);
76 u32 *p = obj->ctx;
77 int i;
79 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
80 p[i] = iommu_read_reg(obj, i * sizeof(u32));
81 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
84 EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
86 /**
87 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
88 * @dev: client device
89 **/
90 void omap_iommu_restore_ctx(struct device *dev)
92 struct omap_iommu *obj = dev_to_omap_iommu(dev);
93 u32 *p = obj->ctx;
94 int i;
96 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
97 iommu_write_reg(obj, p[i], i * sizeof(u32));
98 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
101 EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
103 static void dra7_cfg_dspsys_mmu(struct omap_iommu *obj, bool enable)
105 u32 val, mask;
107 if (!obj->syscfg)
108 return;
110 mask = (1 << (obj->id * DSP_SYS_MMU_CONFIG_EN_SHIFT));
111 val = enable ? mask : 0;
112 regmap_update_bits(obj->syscfg, DSP_SYS_MMU_CONFIG, mask, val);
115 static void __iommu_set_twl(struct omap_iommu *obj, bool on)
117 u32 l = iommu_read_reg(obj, MMU_CNTL);
119 if (on)
120 iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
121 else
122 iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
124 l &= ~MMU_CNTL_MASK;
125 if (on)
126 l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
127 else
128 l |= (MMU_CNTL_MMU_EN);
130 iommu_write_reg(obj, l, MMU_CNTL);
133 static int omap2_iommu_enable(struct omap_iommu *obj)
135 u32 l, pa;
137 if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
138 return -EINVAL;
140 pa = virt_to_phys(obj->iopgd);
141 if (!IS_ALIGNED(pa, SZ_16K))
142 return -EINVAL;
144 l = iommu_read_reg(obj, MMU_REVISION);
145 dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
146 (l >> 4) & 0xf, l & 0xf);
148 iommu_write_reg(obj, pa, MMU_TTB);
150 dra7_cfg_dspsys_mmu(obj, true);
152 if (obj->has_bus_err_back)
153 iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
155 __iommu_set_twl(obj, true);
157 return 0;
160 static void omap2_iommu_disable(struct omap_iommu *obj)
162 u32 l = iommu_read_reg(obj, MMU_CNTL);
164 l &= ~MMU_CNTL_MASK;
165 iommu_write_reg(obj, l, MMU_CNTL);
166 dra7_cfg_dspsys_mmu(obj, false);
168 dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
171 static int iommu_enable(struct omap_iommu *obj)
173 int err;
174 struct platform_device *pdev = to_platform_device(obj->dev);
175 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
177 if (pdata && pdata->deassert_reset) {
178 err = pdata->deassert_reset(pdev, pdata->reset_name);
179 if (err) {
180 dev_err(obj->dev, "deassert_reset failed: %d\n", err);
181 return err;
185 pm_runtime_get_sync(obj->dev);
187 err = omap2_iommu_enable(obj);
189 return err;
192 static void iommu_disable(struct omap_iommu *obj)
194 struct platform_device *pdev = to_platform_device(obj->dev);
195 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
197 omap2_iommu_disable(obj);
199 pm_runtime_put_sync(obj->dev);
201 if (pdata && pdata->assert_reset)
202 pdata->assert_reset(pdev, pdata->reset_name);
206 * TLB operations
208 static u32 iotlb_cr_to_virt(struct cr_regs *cr)
210 u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
211 u32 mask = get_cam_va_mask(cr->cam & page_size);
213 return cr->cam & mask;
216 static u32 get_iopte_attr(struct iotlb_entry *e)
218 u32 attr;
220 attr = e->mixed << 5;
221 attr |= e->endian;
222 attr |= e->elsz >> 3;
223 attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
224 (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
225 return attr;
228 static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
230 u32 status, fault_addr;
232 status = iommu_read_reg(obj, MMU_IRQSTATUS);
233 status &= MMU_IRQ_MASK;
234 if (!status) {
235 *da = 0;
236 return 0;
239 fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
240 *da = fault_addr;
242 iommu_write_reg(obj, status, MMU_IRQSTATUS);
244 return status;
247 void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
249 u32 val;
251 val = iommu_read_reg(obj, MMU_LOCK);
253 l->base = MMU_LOCK_BASE(val);
254 l->vict = MMU_LOCK_VICT(val);
257 void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
259 u32 val;
261 val = (l->base << MMU_LOCK_BASE_SHIFT);
262 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
264 iommu_write_reg(obj, val, MMU_LOCK);
267 static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
269 cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
270 cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
273 static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
275 iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
276 iommu_write_reg(obj, cr->ram, MMU_RAM);
278 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
279 iommu_write_reg(obj, 1, MMU_LD_TLB);
282 /* only used in iotlb iteration for-loop */
283 struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
285 struct cr_regs cr;
286 struct iotlb_lock l;
288 iotlb_lock_get(obj, &l);
289 l.vict = n;
290 iotlb_lock_set(obj, &l);
291 iotlb_read_cr(obj, &cr);
293 return cr;
296 #ifdef PREFETCH_IOTLB
297 static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
298 struct iotlb_entry *e)
300 struct cr_regs *cr;
302 if (!e)
303 return NULL;
305 if (e->da & ~(get_cam_va_mask(e->pgsz))) {
306 dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
307 e->da);
308 return ERR_PTR(-EINVAL);
311 cr = kmalloc(sizeof(*cr), GFP_KERNEL);
312 if (!cr)
313 return ERR_PTR(-ENOMEM);
315 cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
316 cr->ram = e->pa | e->endian | e->elsz | e->mixed;
318 return cr;
322 * load_iotlb_entry - Set an iommu tlb entry
323 * @obj: target iommu
324 * @e: an iommu tlb entry info
326 static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
328 int err = 0;
329 struct iotlb_lock l;
330 struct cr_regs *cr;
332 if (!obj || !obj->nr_tlb_entries || !e)
333 return -EINVAL;
335 pm_runtime_get_sync(obj->dev);
337 iotlb_lock_get(obj, &l);
338 if (l.base == obj->nr_tlb_entries) {
339 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
340 err = -EBUSY;
341 goto out;
343 if (!e->prsvd) {
344 int i;
345 struct cr_regs tmp;
347 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
348 if (!iotlb_cr_valid(&tmp))
349 break;
351 if (i == obj->nr_tlb_entries) {
352 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
353 err = -EBUSY;
354 goto out;
357 iotlb_lock_get(obj, &l);
358 } else {
359 l.vict = l.base;
360 iotlb_lock_set(obj, &l);
363 cr = iotlb_alloc_cr(obj, e);
364 if (IS_ERR(cr)) {
365 pm_runtime_put_sync(obj->dev);
366 return PTR_ERR(cr);
369 iotlb_load_cr(obj, cr);
370 kfree(cr);
372 if (e->prsvd)
373 l.base++;
374 /* increment victim for next tlb load */
375 if (++l.vict == obj->nr_tlb_entries)
376 l.vict = l.base;
377 iotlb_lock_set(obj, &l);
378 out:
379 pm_runtime_put_sync(obj->dev);
380 return err;
383 #else /* !PREFETCH_IOTLB */
385 static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
387 return 0;
390 #endif /* !PREFETCH_IOTLB */
392 static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
394 return load_iotlb_entry(obj, e);
398 * flush_iotlb_page - Clear an iommu tlb entry
399 * @obj: target iommu
400 * @da: iommu device virtual address
402 * Clear an iommu tlb entry which includes 'da' address.
404 static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
406 int i;
407 struct cr_regs cr;
409 pm_runtime_get_sync(obj->dev);
411 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
412 u32 start;
413 size_t bytes;
415 if (!iotlb_cr_valid(&cr))
416 continue;
418 start = iotlb_cr_to_virt(&cr);
419 bytes = iopgsz_to_bytes(cr.cam & 3);
421 if ((start <= da) && (da < start + bytes)) {
422 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
423 __func__, start, da, bytes);
424 iotlb_load_cr(obj, &cr);
425 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
426 break;
429 pm_runtime_put_sync(obj->dev);
431 if (i == obj->nr_tlb_entries)
432 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
436 * flush_iotlb_all - Clear all iommu tlb entries
437 * @obj: target iommu
439 static void flush_iotlb_all(struct omap_iommu *obj)
441 struct iotlb_lock l;
443 pm_runtime_get_sync(obj->dev);
445 l.base = 0;
446 l.vict = 0;
447 iotlb_lock_set(obj, &l);
449 iommu_write_reg(obj, 1, MMU_GFLUSH);
451 pm_runtime_put_sync(obj->dev);
455 * H/W pagetable operations
457 static void flush_iopgd_range(u32 *first, u32 *last)
459 /* FIXME: L2 cache should be taken care of if it exists */
460 do {
461 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
462 : : "r" (first));
463 first += L1_CACHE_BYTES / sizeof(*first);
464 } while (first <= last);
467 static void flush_iopte_range(u32 *first, u32 *last)
469 /* FIXME: L2 cache should be taken care of if it exists */
470 do {
471 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
472 : : "r" (first));
473 first += L1_CACHE_BYTES / sizeof(*first);
474 } while (first <= last);
477 static void iopte_free(u32 *iopte)
479 /* Note: freed iopte's must be clean ready for re-use */
480 if (iopte)
481 kmem_cache_free(iopte_cachep, iopte);
484 static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
486 u32 *iopte;
488 /* a table has already existed */
489 if (*iopgd)
490 goto pte_ready;
493 * do the allocation outside the page table lock
495 spin_unlock(&obj->page_table_lock);
496 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
497 spin_lock(&obj->page_table_lock);
499 if (!*iopgd) {
500 if (!iopte)
501 return ERR_PTR(-ENOMEM);
503 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
504 flush_iopgd_range(iopgd, iopgd);
506 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
507 } else {
508 /* We raced, free the reduniovant table */
509 iopte_free(iopte);
512 pte_ready:
513 iopte = iopte_offset(iopgd, da);
515 dev_vdbg(obj->dev,
516 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
517 __func__, da, iopgd, *iopgd, iopte, *iopte);
519 return iopte;
522 static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
524 u32 *iopgd = iopgd_offset(obj, da);
526 if ((da | pa) & ~IOSECTION_MASK) {
527 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
528 __func__, da, pa, IOSECTION_SIZE);
529 return -EINVAL;
532 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
533 flush_iopgd_range(iopgd, iopgd);
534 return 0;
537 static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
539 u32 *iopgd = iopgd_offset(obj, da);
540 int i;
542 if ((da | pa) & ~IOSUPER_MASK) {
543 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
544 __func__, da, pa, IOSUPER_SIZE);
545 return -EINVAL;
548 for (i = 0; i < 16; i++)
549 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
550 flush_iopgd_range(iopgd, iopgd + 15);
551 return 0;
554 static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
556 u32 *iopgd = iopgd_offset(obj, da);
557 u32 *iopte = iopte_alloc(obj, iopgd, da);
559 if (IS_ERR(iopte))
560 return PTR_ERR(iopte);
562 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
563 flush_iopte_range(iopte, iopte);
565 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
566 __func__, da, pa, iopte, *iopte);
568 return 0;
571 static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
573 u32 *iopgd = iopgd_offset(obj, da);
574 u32 *iopte = iopte_alloc(obj, iopgd, da);
575 int i;
577 if ((da | pa) & ~IOLARGE_MASK) {
578 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
579 __func__, da, pa, IOLARGE_SIZE);
580 return -EINVAL;
583 if (IS_ERR(iopte))
584 return PTR_ERR(iopte);
586 for (i = 0; i < 16; i++)
587 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
588 flush_iopte_range(iopte, iopte + 15);
589 return 0;
592 static int
593 iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
595 int (*fn)(struct omap_iommu *, u32, u32, u32);
596 u32 prot;
597 int err;
599 if (!obj || !e)
600 return -EINVAL;
602 switch (e->pgsz) {
603 case MMU_CAM_PGSZ_16M:
604 fn = iopgd_alloc_super;
605 break;
606 case MMU_CAM_PGSZ_1M:
607 fn = iopgd_alloc_section;
608 break;
609 case MMU_CAM_PGSZ_64K:
610 fn = iopte_alloc_large;
611 break;
612 case MMU_CAM_PGSZ_4K:
613 fn = iopte_alloc_page;
614 break;
615 default:
616 fn = NULL;
617 break;
620 if (WARN_ON(!fn))
621 return -EINVAL;
623 prot = get_iopte_attr(e);
625 spin_lock(&obj->page_table_lock);
626 err = fn(obj, e->da, e->pa, prot);
627 spin_unlock(&obj->page_table_lock);
629 return err;
633 * omap_iopgtable_store_entry - Make an iommu pte entry
634 * @obj: target iommu
635 * @e: an iommu tlb entry info
637 static int
638 omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
640 int err;
642 flush_iotlb_page(obj, e->da);
643 err = iopgtable_store_entry_core(obj, e);
644 if (!err)
645 prefetch_iotlb_entry(obj, e);
646 return err;
650 * iopgtable_lookup_entry - Lookup an iommu pte entry
651 * @obj: target iommu
652 * @da: iommu device virtual address
653 * @ppgd: iommu pgd entry pointer to be returned
654 * @ppte: iommu pte entry pointer to be returned
656 static void
657 iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
659 u32 *iopgd, *iopte = NULL;
661 iopgd = iopgd_offset(obj, da);
662 if (!*iopgd)
663 goto out;
665 if (iopgd_is_table(*iopgd))
666 iopte = iopte_offset(iopgd, da);
667 out:
668 *ppgd = iopgd;
669 *ppte = iopte;
672 static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
674 size_t bytes;
675 u32 *iopgd = iopgd_offset(obj, da);
676 int nent = 1;
678 if (!*iopgd)
679 return 0;
681 if (iopgd_is_table(*iopgd)) {
682 int i;
683 u32 *iopte = iopte_offset(iopgd, da);
685 bytes = IOPTE_SIZE;
686 if (*iopte & IOPTE_LARGE) {
687 nent *= 16;
688 /* rewind to the 1st entry */
689 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
691 bytes *= nent;
692 memset(iopte, 0, nent * sizeof(*iopte));
693 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
696 * do table walk to check if this table is necessary or not
698 iopte = iopte_offset(iopgd, 0);
699 for (i = 0; i < PTRS_PER_IOPTE; i++)
700 if (iopte[i])
701 goto out;
703 iopte_free(iopte);
704 nent = 1; /* for the next L1 entry */
705 } else {
706 bytes = IOPGD_SIZE;
707 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
708 nent *= 16;
709 /* rewind to the 1st entry */
710 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
712 bytes *= nent;
714 memset(iopgd, 0, nent * sizeof(*iopgd));
715 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
716 out:
717 return bytes;
721 * iopgtable_clear_entry - Remove an iommu pte entry
722 * @obj: target iommu
723 * @da: iommu device virtual address
725 static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
727 size_t bytes;
729 spin_lock(&obj->page_table_lock);
731 bytes = iopgtable_clear_entry_core(obj, da);
732 flush_iotlb_page(obj, da);
734 spin_unlock(&obj->page_table_lock);
736 return bytes;
739 static void iopgtable_clear_entry_all(struct omap_iommu *obj)
741 int i;
743 spin_lock(&obj->page_table_lock);
745 for (i = 0; i < PTRS_PER_IOPGD; i++) {
746 u32 da;
747 u32 *iopgd;
749 da = i << IOPGD_SHIFT;
750 iopgd = iopgd_offset(obj, da);
752 if (!*iopgd)
753 continue;
755 if (iopgd_is_table(*iopgd))
756 iopte_free(iopte_offset(iopgd, 0));
758 *iopgd = 0;
759 flush_iopgd_range(iopgd, iopgd);
762 flush_iotlb_all(obj);
764 spin_unlock(&obj->page_table_lock);
768 * Device IOMMU generic operations
770 static irqreturn_t iommu_fault_handler(int irq, void *data)
772 u32 da, errs;
773 u32 *iopgd, *iopte;
774 struct omap_iommu *obj = data;
775 struct iommu_domain *domain = obj->domain;
776 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
778 if (!omap_domain->iommu_dev)
779 return IRQ_NONE;
781 errs = iommu_report_fault(obj, &da);
782 if (errs == 0)
783 return IRQ_HANDLED;
785 /* Fault callback or TLB/PTE Dynamic loading */
786 if (!report_iommu_fault(domain, obj->dev, da, 0))
787 return IRQ_HANDLED;
789 iommu_disable(obj);
791 iopgd = iopgd_offset(obj, da);
793 if (!iopgd_is_table(*iopgd)) {
794 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
795 obj->name, errs, da, iopgd, *iopgd);
796 return IRQ_NONE;
799 iopte = iopte_offset(iopgd, da);
801 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
802 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
804 return IRQ_NONE;
808 * omap_iommu_attach() - attach iommu device to an iommu domain
809 * @obj: target omap iommu device
810 * @iopgd: page table
812 static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd)
814 int err;
816 spin_lock(&obj->iommu_lock);
818 obj->iopgd = iopgd;
819 err = iommu_enable(obj);
820 if (err)
821 goto err_enable;
822 flush_iotlb_all(obj);
824 spin_unlock(&obj->iommu_lock);
826 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
828 return 0;
830 err_enable:
831 spin_unlock(&obj->iommu_lock);
833 return err;
837 * omap_iommu_detach - release iommu device
838 * @obj: target iommu
840 static void omap_iommu_detach(struct omap_iommu *obj)
842 if (!obj || IS_ERR(obj))
843 return;
845 spin_lock(&obj->iommu_lock);
847 iommu_disable(obj);
848 obj->iopgd = NULL;
850 spin_unlock(&obj->iommu_lock);
852 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
855 static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device *pdev,
856 struct omap_iommu *obj)
858 struct device_node *np = pdev->dev.of_node;
859 int ret;
861 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
862 return 0;
864 if (!of_property_read_bool(np, "ti,syscon-mmuconfig")) {
865 dev_err(&pdev->dev, "ti,syscon-mmuconfig property is missing\n");
866 return -EINVAL;
869 obj->syscfg =
870 syscon_regmap_lookup_by_phandle(np, "ti,syscon-mmuconfig");
871 if (IS_ERR(obj->syscfg)) {
872 /* can fail with -EPROBE_DEFER */
873 ret = PTR_ERR(obj->syscfg);
874 return ret;
877 if (of_property_read_u32_index(np, "ti,syscon-mmuconfig", 1,
878 &obj->id)) {
879 dev_err(&pdev->dev, "couldn't get the IOMMU instance id within subsystem\n");
880 return -EINVAL;
883 if (obj->id != 0 && obj->id != 1) {
884 dev_err(&pdev->dev, "invalid IOMMU instance id\n");
885 return -EINVAL;
888 return 0;
892 * OMAP Device MMU(IOMMU) detection
894 static int omap_iommu_probe(struct platform_device *pdev)
896 int err = -ENODEV;
897 int irq;
898 struct omap_iommu *obj;
899 struct resource *res;
900 struct device_node *of = pdev->dev.of_node;
902 if (!of) {
903 pr_err("%s: only DT-based devices are supported\n", __func__);
904 return -ENODEV;
907 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
908 if (!obj)
909 return -ENOMEM;
911 obj->name = dev_name(&pdev->dev);
912 obj->nr_tlb_entries = 32;
913 err = of_property_read_u32(of, "ti,#tlb-entries", &obj->nr_tlb_entries);
914 if (err && err != -EINVAL)
915 return err;
916 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
917 return -EINVAL;
918 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
919 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
921 obj->dev = &pdev->dev;
922 obj->ctx = (void *)obj + sizeof(*obj);
924 spin_lock_init(&obj->iommu_lock);
925 spin_lock_init(&obj->page_table_lock);
927 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
928 obj->regbase = devm_ioremap_resource(obj->dev, res);
929 if (IS_ERR(obj->regbase))
930 return PTR_ERR(obj->regbase);
932 err = omap_iommu_dra7_get_dsp_system_cfg(pdev, obj);
933 if (err)
934 return err;
936 irq = platform_get_irq(pdev, 0);
937 if (irq < 0)
938 return -ENODEV;
940 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
941 dev_name(obj->dev), obj);
942 if (err < 0)
943 return err;
944 platform_set_drvdata(pdev, obj);
946 obj->group = iommu_group_alloc();
947 if (IS_ERR(obj->group))
948 return PTR_ERR(obj->group);
950 err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL, obj->name);
951 if (err)
952 goto out_group;
954 iommu_device_set_ops(&obj->iommu, &omap_iommu_ops);
956 err = iommu_device_register(&obj->iommu);
957 if (err)
958 goto out_sysfs;
960 pm_runtime_irq_safe(obj->dev);
961 pm_runtime_enable(obj->dev);
963 omap_iommu_debugfs_add(obj);
965 dev_info(&pdev->dev, "%s registered\n", obj->name);
967 return 0;
969 out_sysfs:
970 iommu_device_sysfs_remove(&obj->iommu);
971 out_group:
972 iommu_group_put(obj->group);
973 return err;
976 static int omap_iommu_remove(struct platform_device *pdev)
978 struct omap_iommu *obj = platform_get_drvdata(pdev);
980 iommu_group_put(obj->group);
981 obj->group = NULL;
983 iommu_device_sysfs_remove(&obj->iommu);
984 iommu_device_unregister(&obj->iommu);
986 omap_iommu_debugfs_remove(obj);
988 pm_runtime_disable(obj->dev);
990 dev_info(&pdev->dev, "%s removed\n", obj->name);
991 return 0;
994 static const struct of_device_id omap_iommu_of_match[] = {
995 { .compatible = "ti,omap2-iommu" },
996 { .compatible = "ti,omap4-iommu" },
997 { .compatible = "ti,dra7-iommu" },
998 { .compatible = "ti,dra7-dsp-iommu" },
1002 static struct platform_driver omap_iommu_driver = {
1003 .probe = omap_iommu_probe,
1004 .remove = omap_iommu_remove,
1005 .driver = {
1006 .name = "omap-iommu",
1007 .of_match_table = of_match_ptr(omap_iommu_of_match),
1011 static void iopte_cachep_ctor(void *iopte)
1013 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1016 static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
1018 memset(e, 0, sizeof(*e));
1020 e->da = da;
1021 e->pa = pa;
1022 e->valid = MMU_CAM_V;
1023 e->pgsz = pgsz;
1024 e->endian = MMU_RAM_ENDIAN_LITTLE;
1025 e->elsz = MMU_RAM_ELSZ_8;
1026 e->mixed = 0;
1028 return iopgsz_to_bytes(e->pgsz);
1031 static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
1032 phys_addr_t pa, size_t bytes, int prot)
1034 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1035 struct omap_iommu *oiommu = omap_domain->iommu_dev;
1036 struct device *dev = oiommu->dev;
1037 struct iotlb_entry e;
1038 int omap_pgsz;
1039 u32 ret;
1041 omap_pgsz = bytes_to_iopgsz(bytes);
1042 if (omap_pgsz < 0) {
1043 dev_err(dev, "invalid size to map: %d\n", bytes);
1044 return -EINVAL;
1047 dev_dbg(dev, "mapping da 0x%lx to pa %pa size 0x%x\n", da, &pa, bytes);
1049 iotlb_init_entry(&e, da, pa, omap_pgsz);
1051 ret = omap_iopgtable_store_entry(oiommu, &e);
1052 if (ret)
1053 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
1055 return ret;
1058 static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1059 size_t size)
1061 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1062 struct omap_iommu *oiommu = omap_domain->iommu_dev;
1063 struct device *dev = oiommu->dev;
1065 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
1067 return iopgtable_clear_entry(oiommu, da);
1070 static int
1071 omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1073 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1074 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1075 struct omap_iommu *oiommu;
1076 int ret = 0;
1078 if (!arch_data || !arch_data->iommu_dev) {
1079 dev_err(dev, "device doesn't have an associated iommu\n");
1080 return -EINVAL;
1083 spin_lock(&omap_domain->lock);
1085 /* only a single device is supported per domain for now */
1086 if (omap_domain->iommu_dev) {
1087 dev_err(dev, "iommu domain is already attached\n");
1088 ret = -EBUSY;
1089 goto out;
1092 oiommu = arch_data->iommu_dev;
1094 /* get a handle to and enable the omap iommu */
1095 ret = omap_iommu_attach(oiommu, omap_domain->pgtable);
1096 if (ret) {
1097 dev_err(dev, "can't get omap iommu: %d\n", ret);
1098 goto out;
1101 omap_domain->iommu_dev = oiommu;
1102 omap_domain->dev = dev;
1103 oiommu->domain = domain;
1105 out:
1106 spin_unlock(&omap_domain->lock);
1107 return ret;
1110 static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
1111 struct device *dev)
1113 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
1115 /* only a single device is supported per domain for now */
1116 if (omap_domain->iommu_dev != oiommu) {
1117 dev_err(dev, "invalid iommu device\n");
1118 return;
1121 iopgtable_clear_entry_all(oiommu);
1123 omap_iommu_detach(oiommu);
1125 omap_domain->iommu_dev = NULL;
1126 omap_domain->dev = NULL;
1127 oiommu->domain = NULL;
1130 static void omap_iommu_detach_dev(struct iommu_domain *domain,
1131 struct device *dev)
1133 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1135 spin_lock(&omap_domain->lock);
1136 _omap_iommu_detach_dev(omap_domain, dev);
1137 spin_unlock(&omap_domain->lock);
1140 static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
1142 struct omap_iommu_domain *omap_domain;
1144 if (type != IOMMU_DOMAIN_UNMANAGED)
1145 return NULL;
1147 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1148 if (!omap_domain)
1149 goto out;
1151 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1152 if (!omap_domain->pgtable)
1153 goto fail_nomem;
1156 * should never fail, but please keep this around to ensure
1157 * we keep the hardware happy
1159 if (WARN_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE)))
1160 goto fail_align;
1162 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1163 spin_lock_init(&omap_domain->lock);
1165 omap_domain->domain.geometry.aperture_start = 0;
1166 omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
1167 omap_domain->domain.geometry.force_aperture = true;
1169 return &omap_domain->domain;
1171 fail_align:
1172 kfree(omap_domain->pgtable);
1173 fail_nomem:
1174 kfree(omap_domain);
1175 out:
1176 return NULL;
1179 static void omap_iommu_domain_free(struct iommu_domain *domain)
1181 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1184 * An iommu device is still attached
1185 * (currently, only one device can be attached) ?
1187 if (omap_domain->iommu_dev)
1188 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1190 kfree(omap_domain->pgtable);
1191 kfree(omap_domain);
1194 static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
1195 dma_addr_t da)
1197 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1198 struct omap_iommu *oiommu = omap_domain->iommu_dev;
1199 struct device *dev = oiommu->dev;
1200 u32 *pgd, *pte;
1201 phys_addr_t ret = 0;
1203 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1205 if (pte) {
1206 if (iopte_is_small(*pte))
1207 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1208 else if (iopte_is_large(*pte))
1209 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1210 else
1211 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
1212 (unsigned long long)da);
1213 } else {
1214 if (iopgd_is_section(*pgd))
1215 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1216 else if (iopgd_is_super(*pgd))
1217 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1218 else
1219 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
1220 (unsigned long long)da);
1223 return ret;
1226 static int omap_iommu_add_device(struct device *dev)
1228 struct omap_iommu_arch_data *arch_data;
1229 struct omap_iommu *oiommu;
1230 struct iommu_group *group;
1231 struct device_node *np;
1232 struct platform_device *pdev;
1233 int ret;
1236 * Allocate the archdata iommu structure for DT-based devices.
1238 * TODO: Simplify this when removing non-DT support completely from the
1239 * IOMMU users.
1241 if (!dev->of_node)
1242 return 0;
1244 np = of_parse_phandle(dev->of_node, "iommus", 0);
1245 if (!np)
1246 return 0;
1248 pdev = of_find_device_by_node(np);
1249 if (WARN_ON(!pdev)) {
1250 of_node_put(np);
1251 return -EINVAL;
1254 oiommu = platform_get_drvdata(pdev);
1255 if (!oiommu) {
1256 of_node_put(np);
1257 return -EINVAL;
1260 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
1261 if (!arch_data) {
1262 of_node_put(np);
1263 return -ENOMEM;
1266 ret = iommu_device_link(&oiommu->iommu, dev);
1267 if (ret) {
1268 kfree(arch_data);
1269 of_node_put(np);
1270 return ret;
1273 arch_data->iommu_dev = oiommu;
1274 dev->archdata.iommu = arch_data;
1277 * IOMMU group initialization calls into omap_iommu_device_group, which
1278 * needs a valid dev->archdata.iommu pointer
1280 group = iommu_group_get_for_dev(dev);
1281 if (IS_ERR(group)) {
1282 iommu_device_unlink(&oiommu->iommu, dev);
1283 dev->archdata.iommu = NULL;
1284 kfree(arch_data);
1285 return PTR_ERR(group);
1287 iommu_group_put(group);
1289 of_node_put(np);
1291 return 0;
1294 static void omap_iommu_remove_device(struct device *dev)
1296 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1298 if (!dev->of_node || !arch_data)
1299 return;
1301 iommu_device_unlink(&arch_data->iommu_dev->iommu, dev);
1302 iommu_group_remove_device(dev);
1304 dev->archdata.iommu = NULL;
1305 kfree(arch_data);
1309 static struct iommu_group *omap_iommu_device_group(struct device *dev)
1311 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1312 struct iommu_group *group = ERR_PTR(-EINVAL);
1314 if (arch_data->iommu_dev)
1315 group = arch_data->iommu_dev->group;
1317 return group;
1320 static const struct iommu_ops omap_iommu_ops = {
1321 .domain_alloc = omap_iommu_domain_alloc,
1322 .domain_free = omap_iommu_domain_free,
1323 .attach_dev = omap_iommu_attach_dev,
1324 .detach_dev = omap_iommu_detach_dev,
1325 .map = omap_iommu_map,
1326 .unmap = omap_iommu_unmap,
1327 .map_sg = default_iommu_map_sg,
1328 .iova_to_phys = omap_iommu_iova_to_phys,
1329 .add_device = omap_iommu_add_device,
1330 .remove_device = omap_iommu_remove_device,
1331 .device_group = omap_iommu_device_group,
1332 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
1335 static int __init omap_iommu_init(void)
1337 struct kmem_cache *p;
1338 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1339 size_t align = 1 << 10; /* L2 pagetable alignement */
1340 struct device_node *np;
1341 int ret;
1343 np = of_find_matching_node(NULL, omap_iommu_of_match);
1344 if (!np)
1345 return 0;
1347 of_node_put(np);
1349 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1350 iopte_cachep_ctor);
1351 if (!p)
1352 return -ENOMEM;
1353 iopte_cachep = p;
1355 omap_iommu_debugfs_init();
1357 ret = platform_driver_register(&omap_iommu_driver);
1358 if (ret) {
1359 pr_err("%s: failed to register driver\n", __func__);
1360 goto fail_driver;
1363 ret = bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
1364 if (ret)
1365 goto fail_bus;
1367 return 0;
1369 fail_bus:
1370 platform_driver_unregister(&omap_iommu_driver);
1371 fail_driver:
1372 kmem_cache_destroy(iopte_cachep);
1373 return ret;
1375 subsys_initcall(omap_iommu_init);
1376 /* must be ready before omap3isp is probed */