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/dma-mapping.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/ioport.h>
19 #include <linux/platform_device.h>
20 #include <linux/iommu.h>
21 #include <linux/omap-iommu.h>
22 #include <linux/mutex.h>
23 #include <linux/spinlock.h>
25 #include <linux/pm_runtime.h>
27 #include <linux/of_iommu.h>
28 #include <linux/of_irq.h>
29 #include <linux/of_platform.h>
30 #include <linux/regmap.h>
31 #include <linux/mfd/syscon.h>
33 #include <linux/platform_data/iommu-omap.h>
35 #include "omap-iopgtable.h"
36 #include "omap-iommu.h"
38 static const struct iommu_ops omap_iommu_ops;
40 #define to_iommu(dev) \
41 ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
43 /* bitmap of the page sizes currently supported */
44 #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
46 #define MMU_LOCK_BASE_SHIFT 10
47 #define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
48 #define MMU_LOCK_BASE(x) \
49 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
51 #define MMU_LOCK_VICT_SHIFT 4
52 #define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
53 #define MMU_LOCK_VICT(x) \
54 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
56 static struct platform_driver omap_iommu_driver;
57 static struct kmem_cache *iopte_cachep;
60 * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
61 * @dom: generic iommu domain handle
63 static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
65 return container_of(dom, struct omap_iommu_domain, domain);
69 * omap_iommu_save_ctx - Save registers for pm off-mode support
72 void omap_iommu_save_ctx(struct device *dev)
74 struct omap_iommu *obj = dev_to_omap_iommu(dev);
78 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
79 p[i] = iommu_read_reg(obj, i * sizeof(u32));
80 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
83 EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
86 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
89 void omap_iommu_restore_ctx(struct device *dev)
91 struct omap_iommu *obj = dev_to_omap_iommu(dev);
95 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
96 iommu_write_reg(obj, p[i], i * sizeof(u32));
97 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
100 EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
102 static void dra7_cfg_dspsys_mmu(struct omap_iommu *obj, bool enable)
109 mask = (1 << (obj->id * DSP_SYS_MMU_CONFIG_EN_SHIFT));
110 val = enable ? mask : 0;
111 regmap_update_bits(obj->syscfg, DSP_SYS_MMU_CONFIG, mask, val);
114 static void __iommu_set_twl(struct omap_iommu *obj, bool on)
116 u32 l = iommu_read_reg(obj, MMU_CNTL);
119 iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
121 iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
125 l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
127 l |= (MMU_CNTL_MMU_EN);
129 iommu_write_reg(obj, l, MMU_CNTL);
132 static int omap2_iommu_enable(struct omap_iommu *obj)
136 if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
139 pa = virt_to_phys(obj->iopgd);
140 if (!IS_ALIGNED(pa, SZ_16K))
143 l = iommu_read_reg(obj, MMU_REVISION);
144 dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
145 (l >> 4) & 0xf, l & 0xf);
147 iommu_write_reg(obj, pa, MMU_TTB);
149 dra7_cfg_dspsys_mmu(obj, true);
151 if (obj->has_bus_err_back)
152 iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
154 __iommu_set_twl(obj, true);
159 static void omap2_iommu_disable(struct omap_iommu *obj)
161 u32 l = iommu_read_reg(obj, MMU_CNTL);
164 iommu_write_reg(obj, l, MMU_CNTL);
165 dra7_cfg_dspsys_mmu(obj, false);
167 dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
170 static int iommu_enable(struct omap_iommu *obj)
173 struct platform_device *pdev = to_platform_device(obj->dev);
174 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
176 if (pdata && pdata->deassert_reset) {
177 err = pdata->deassert_reset(pdev, pdata->reset_name);
179 dev_err(obj->dev, "deassert_reset failed: %d\n", err);
184 pm_runtime_get_sync(obj->dev);
186 err = omap2_iommu_enable(obj);
191 static void iommu_disable(struct omap_iommu *obj)
193 struct platform_device *pdev = to_platform_device(obj->dev);
194 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
196 omap2_iommu_disable(obj);
198 pm_runtime_put_sync(obj->dev);
200 if (pdata && pdata->assert_reset)
201 pdata->assert_reset(pdev, pdata->reset_name);
207 static u32 iotlb_cr_to_virt(struct cr_regs *cr)
209 u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
210 u32 mask = get_cam_va_mask(cr->cam & page_size);
212 return cr->cam & mask;
215 static u32 get_iopte_attr(struct iotlb_entry *e)
219 attr = e->mixed << 5;
221 attr |= e->elsz >> 3;
222 attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
223 (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
227 static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
229 u32 status, fault_addr;
231 status = iommu_read_reg(obj, MMU_IRQSTATUS);
232 status &= MMU_IRQ_MASK;
238 fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
241 iommu_write_reg(obj, status, MMU_IRQSTATUS);
246 void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
250 val = iommu_read_reg(obj, MMU_LOCK);
252 l->base = MMU_LOCK_BASE(val);
253 l->vict = MMU_LOCK_VICT(val);
256 void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
260 val = (l->base << MMU_LOCK_BASE_SHIFT);
261 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
263 iommu_write_reg(obj, val, MMU_LOCK);
266 static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
268 cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
269 cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
272 static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
274 iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
275 iommu_write_reg(obj, cr->ram, MMU_RAM);
277 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
278 iommu_write_reg(obj, 1, MMU_LD_TLB);
281 /* only used in iotlb iteration for-loop */
282 struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
287 iotlb_lock_get(obj, &l);
289 iotlb_lock_set(obj, &l);
290 iotlb_read_cr(obj, &cr);
295 #ifdef PREFETCH_IOTLB
296 static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
297 struct iotlb_entry *e)
304 if (e->da & ~(get_cam_va_mask(e->pgsz))) {
305 dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
307 return ERR_PTR(-EINVAL);
310 cr = kmalloc(sizeof(*cr), GFP_KERNEL);
312 return ERR_PTR(-ENOMEM);
314 cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
315 cr->ram = e->pa | e->endian | e->elsz | e->mixed;
321 * load_iotlb_entry - Set an iommu tlb entry
323 * @e: an iommu tlb entry info
325 static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
331 if (!obj || !obj->nr_tlb_entries || !e)
334 pm_runtime_get_sync(obj->dev);
336 iotlb_lock_get(obj, &l);
337 if (l.base == obj->nr_tlb_entries) {
338 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
346 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
347 if (!iotlb_cr_valid(&tmp))
350 if (i == obj->nr_tlb_entries) {
351 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
356 iotlb_lock_get(obj, &l);
359 iotlb_lock_set(obj, &l);
362 cr = iotlb_alloc_cr(obj, e);
364 pm_runtime_put_sync(obj->dev);
368 iotlb_load_cr(obj, cr);
373 /* increment victim for next tlb load */
374 if (++l.vict == obj->nr_tlb_entries)
376 iotlb_lock_set(obj, &l);
378 pm_runtime_put_sync(obj->dev);
382 #else /* !PREFETCH_IOTLB */
384 static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
389 #endif /* !PREFETCH_IOTLB */
391 static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
393 return load_iotlb_entry(obj, e);
397 * flush_iotlb_page - Clear an iommu tlb entry
399 * @da: iommu device virtual address
401 * Clear an iommu tlb entry which includes 'da' address.
403 static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
408 pm_runtime_get_sync(obj->dev);
410 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
414 if (!iotlb_cr_valid(&cr))
417 start = iotlb_cr_to_virt(&cr);
418 bytes = iopgsz_to_bytes(cr.cam & 3);
420 if ((start <= da) && (da < start + bytes)) {
421 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
422 __func__, start, da, bytes);
423 iotlb_load_cr(obj, &cr);
424 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
428 pm_runtime_put_sync(obj->dev);
430 if (i == obj->nr_tlb_entries)
431 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
435 * flush_iotlb_all - Clear all iommu tlb entries
438 static void flush_iotlb_all(struct omap_iommu *obj)
442 pm_runtime_get_sync(obj->dev);
446 iotlb_lock_set(obj, &l);
448 iommu_write_reg(obj, 1, MMU_GFLUSH);
450 pm_runtime_put_sync(obj->dev);
454 * H/W pagetable operations
456 static void flush_iopte_range(struct device *dev, dma_addr_t dma,
457 unsigned long offset, int num_entries)
459 size_t size = num_entries * sizeof(u32);
461 dma_sync_single_range_for_device(dev, dma, offset, size, DMA_TO_DEVICE);
464 static void iopte_free(struct omap_iommu *obj, u32 *iopte, bool dma_valid)
468 /* Note: freed iopte's must be clean ready for re-use */
471 pt_dma = virt_to_phys(iopte);
472 dma_unmap_single(obj->dev, pt_dma, IOPTE_TABLE_SIZE,
476 kmem_cache_free(iopte_cachep, iopte);
480 static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd,
481 dma_addr_t *pt_dma, u32 da)
484 unsigned long offset = iopgd_index(da) * sizeof(da);
486 /* a table has already existed */
491 * do the allocation outside the page table lock
493 spin_unlock(&obj->page_table_lock);
494 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
495 spin_lock(&obj->page_table_lock);
499 return ERR_PTR(-ENOMEM);
501 *pt_dma = dma_map_single(obj->dev, iopte, IOPTE_TABLE_SIZE,
503 if (dma_mapping_error(obj->dev, *pt_dma)) {
504 dev_err(obj->dev, "DMA map error for L2 table\n");
505 iopte_free(obj, iopte, false);
506 return ERR_PTR(-ENOMEM);
510 * we rely on dma address and the physical address to be
511 * the same for mapping the L2 table
513 if (WARN_ON(*pt_dma != virt_to_phys(iopte))) {
514 dev_err(obj->dev, "DMA translation error for L2 table\n");
515 dma_unmap_single(obj->dev, *pt_dma, IOPTE_TABLE_SIZE,
517 iopte_free(obj, iopte, false);
518 return ERR_PTR(-ENOMEM);
521 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
523 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
524 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
526 /* We raced, free the reduniovant table */
527 iopte_free(obj, iopte, false);
531 iopte = iopte_offset(iopgd, da);
532 *pt_dma = virt_to_phys(iopte);
534 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
535 __func__, da, iopgd, *iopgd, iopte, *iopte);
540 static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
542 u32 *iopgd = iopgd_offset(obj, da);
543 unsigned long offset = iopgd_index(da) * sizeof(da);
545 if ((da | pa) & ~IOSECTION_MASK) {
546 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
547 __func__, da, pa, IOSECTION_SIZE);
551 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
552 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
556 static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
558 u32 *iopgd = iopgd_offset(obj, da);
559 unsigned long offset = iopgd_index(da) * sizeof(da);
562 if ((da | pa) & ~IOSUPER_MASK) {
563 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
564 __func__, da, pa, IOSUPER_SIZE);
568 for (i = 0; i < 16; i++)
569 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
570 flush_iopte_range(obj->dev, obj->pd_dma, offset, 16);
574 static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
576 u32 *iopgd = iopgd_offset(obj, da);
578 u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
579 unsigned long offset = iopte_index(da) * sizeof(da);
582 return PTR_ERR(iopte);
584 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
585 flush_iopte_range(obj->dev, pt_dma, offset, 1);
587 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
588 __func__, da, pa, iopte, *iopte);
593 static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
595 u32 *iopgd = iopgd_offset(obj, da);
597 u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
598 unsigned long offset = iopte_index(da) * sizeof(da);
601 if ((da | pa) & ~IOLARGE_MASK) {
602 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
603 __func__, da, pa, IOLARGE_SIZE);
608 return PTR_ERR(iopte);
610 for (i = 0; i < 16; i++)
611 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
612 flush_iopte_range(obj->dev, pt_dma, offset, 16);
617 iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
619 int (*fn)(struct omap_iommu *, u32, u32, u32);
627 case MMU_CAM_PGSZ_16M:
628 fn = iopgd_alloc_super;
630 case MMU_CAM_PGSZ_1M:
631 fn = iopgd_alloc_section;
633 case MMU_CAM_PGSZ_64K:
634 fn = iopte_alloc_large;
636 case MMU_CAM_PGSZ_4K:
637 fn = iopte_alloc_page;
647 prot = get_iopte_attr(e);
649 spin_lock(&obj->page_table_lock);
650 err = fn(obj, e->da, e->pa, prot);
651 spin_unlock(&obj->page_table_lock);
657 * omap_iopgtable_store_entry - Make an iommu pte entry
659 * @e: an iommu tlb entry info
662 omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
666 flush_iotlb_page(obj, e->da);
667 err = iopgtable_store_entry_core(obj, e);
669 prefetch_iotlb_entry(obj, e);
674 * iopgtable_lookup_entry - Lookup an iommu pte entry
676 * @da: iommu device virtual address
677 * @ppgd: iommu pgd entry pointer to be returned
678 * @ppte: iommu pte entry pointer to be returned
681 iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
683 u32 *iopgd, *iopte = NULL;
685 iopgd = iopgd_offset(obj, da);
689 if (iopgd_is_table(*iopgd))
690 iopte = iopte_offset(iopgd, da);
696 static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
699 u32 *iopgd = iopgd_offset(obj, da);
702 unsigned long pd_offset = iopgd_index(da) * sizeof(da);
703 unsigned long pt_offset = iopte_index(da) * sizeof(da);
708 if (iopgd_is_table(*iopgd)) {
710 u32 *iopte = iopte_offset(iopgd, da);
713 if (*iopte & IOPTE_LARGE) {
715 /* rewind to the 1st entry */
716 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
719 memset(iopte, 0, nent * sizeof(*iopte));
720 pt_dma = virt_to_phys(iopte);
721 flush_iopte_range(obj->dev, pt_dma, pt_offset, nent);
724 * do table walk to check if this table is necessary or not
726 iopte = iopte_offset(iopgd, 0);
727 for (i = 0; i < PTRS_PER_IOPTE; i++)
731 iopte_free(obj, iopte, true);
732 nent = 1; /* for the next L1 entry */
735 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
737 /* rewind to the 1st entry */
738 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
742 memset(iopgd, 0, nent * sizeof(*iopgd));
743 flush_iopte_range(obj->dev, obj->pd_dma, pd_offset, nent);
749 * iopgtable_clear_entry - Remove an iommu pte entry
751 * @da: iommu device virtual address
753 static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
757 spin_lock(&obj->page_table_lock);
759 bytes = iopgtable_clear_entry_core(obj, da);
760 flush_iotlb_page(obj, da);
762 spin_unlock(&obj->page_table_lock);
767 static void iopgtable_clear_entry_all(struct omap_iommu *obj)
769 unsigned long offset;
772 spin_lock(&obj->page_table_lock);
774 for (i = 0; i < PTRS_PER_IOPGD; i++) {
778 da = i << IOPGD_SHIFT;
779 iopgd = iopgd_offset(obj, da);
780 offset = iopgd_index(da) * sizeof(da);
785 if (iopgd_is_table(*iopgd))
786 iopte_free(obj, iopte_offset(iopgd, 0), true);
789 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
792 flush_iotlb_all(obj);
794 spin_unlock(&obj->page_table_lock);
798 * Device IOMMU generic operations
800 static irqreturn_t iommu_fault_handler(int irq, void *data)
804 struct omap_iommu *obj = data;
805 struct iommu_domain *domain = obj->domain;
806 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
808 if (!omap_domain->iommu_dev)
811 errs = iommu_report_fault(obj, &da);
815 /* Fault callback or TLB/PTE Dynamic loading */
816 if (!report_iommu_fault(domain, obj->dev, da, 0))
819 iommu_write_reg(obj, 0, MMU_IRQENABLE);
821 iopgd = iopgd_offset(obj, da);
823 if (!iopgd_is_table(*iopgd)) {
824 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
825 obj->name, errs, da, iopgd, *iopgd);
829 iopte = iopte_offset(iopgd, da);
831 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
832 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
838 * omap_iommu_attach() - attach iommu device to an iommu domain
839 * @obj: target omap iommu device
842 static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd)
846 spin_lock(&obj->iommu_lock);
848 obj->pd_dma = dma_map_single(obj->dev, iopgd, IOPGD_TABLE_SIZE,
850 if (dma_mapping_error(obj->dev, obj->pd_dma)) {
851 dev_err(obj->dev, "DMA map error for L1 table\n");
857 err = iommu_enable(obj);
860 flush_iotlb_all(obj);
862 spin_unlock(&obj->iommu_lock);
864 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
869 spin_unlock(&obj->iommu_lock);
875 * omap_iommu_detach - release iommu device
878 static void omap_iommu_detach(struct omap_iommu *obj)
880 if (!obj || IS_ERR(obj))
883 spin_lock(&obj->iommu_lock);
885 dma_unmap_single(obj->dev, obj->pd_dma, IOPGD_TABLE_SIZE,
891 spin_unlock(&obj->iommu_lock);
893 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
896 static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device *pdev,
897 struct omap_iommu *obj)
899 struct device_node *np = pdev->dev.of_node;
902 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
905 if (!of_property_read_bool(np, "ti,syscon-mmuconfig")) {
906 dev_err(&pdev->dev, "ti,syscon-mmuconfig property is missing\n");
911 syscon_regmap_lookup_by_phandle(np, "ti,syscon-mmuconfig");
912 if (IS_ERR(obj->syscfg)) {
913 /* can fail with -EPROBE_DEFER */
914 ret = PTR_ERR(obj->syscfg);
918 if (of_property_read_u32_index(np, "ti,syscon-mmuconfig", 1,
920 dev_err(&pdev->dev, "couldn't get the IOMMU instance id within subsystem\n");
924 if (obj->id != 0 && obj->id != 1) {
925 dev_err(&pdev->dev, "invalid IOMMU instance id\n");
933 * OMAP Device MMU(IOMMU) detection
935 static int omap_iommu_probe(struct platform_device *pdev)
939 struct omap_iommu *obj;
940 struct resource *res;
941 struct device_node *of = pdev->dev.of_node;
944 pr_err("%s: only DT-based devices are supported\n", __func__);
948 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
952 obj->name = dev_name(&pdev->dev);
953 obj->nr_tlb_entries = 32;
954 err = of_property_read_u32(of, "ti,#tlb-entries", &obj->nr_tlb_entries);
955 if (err && err != -EINVAL)
957 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
959 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
960 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
962 obj->dev = &pdev->dev;
963 obj->ctx = (void *)obj + sizeof(*obj);
965 spin_lock_init(&obj->iommu_lock);
966 spin_lock_init(&obj->page_table_lock);
968 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
969 obj->regbase = devm_ioremap_resource(obj->dev, res);
970 if (IS_ERR(obj->regbase))
971 return PTR_ERR(obj->regbase);
973 err = omap_iommu_dra7_get_dsp_system_cfg(pdev, obj);
977 irq = platform_get_irq(pdev, 0);
981 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
982 dev_name(obj->dev), obj);
985 platform_set_drvdata(pdev, obj);
987 obj->group = iommu_group_alloc();
988 if (IS_ERR(obj->group))
989 return PTR_ERR(obj->group);
991 err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL, obj->name);
995 iommu_device_set_ops(&obj->iommu, &omap_iommu_ops);
997 err = iommu_device_register(&obj->iommu);
1001 pm_runtime_irq_safe(obj->dev);
1002 pm_runtime_enable(obj->dev);
1004 omap_iommu_debugfs_add(obj);
1006 dev_info(&pdev->dev, "%s registered\n", obj->name);
1011 iommu_device_sysfs_remove(&obj->iommu);
1013 iommu_group_put(obj->group);
1017 static int omap_iommu_remove(struct platform_device *pdev)
1019 struct omap_iommu *obj = platform_get_drvdata(pdev);
1021 iommu_group_put(obj->group);
1024 iommu_device_sysfs_remove(&obj->iommu);
1025 iommu_device_unregister(&obj->iommu);
1027 omap_iommu_debugfs_remove(obj);
1029 pm_runtime_disable(obj->dev);
1031 dev_info(&pdev->dev, "%s removed\n", obj->name);
1035 static const struct of_device_id omap_iommu_of_match[] = {
1036 { .compatible = "ti,omap2-iommu" },
1037 { .compatible = "ti,omap4-iommu" },
1038 { .compatible = "ti,dra7-iommu" },
1039 { .compatible = "ti,dra7-dsp-iommu" },
1043 static struct platform_driver omap_iommu_driver = {
1044 .probe = omap_iommu_probe,
1045 .remove = omap_iommu_remove,
1047 .name = "omap-iommu",
1048 .of_match_table = of_match_ptr(omap_iommu_of_match),
1052 static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
1054 memset(e, 0, sizeof(*e));
1058 e->valid = MMU_CAM_V;
1060 e->endian = MMU_RAM_ENDIAN_LITTLE;
1061 e->elsz = MMU_RAM_ELSZ_8;
1064 return iopgsz_to_bytes(e->pgsz);
1067 static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
1068 phys_addr_t pa, size_t bytes, int prot)
1070 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1071 struct omap_iommu *oiommu = omap_domain->iommu_dev;
1072 struct device *dev = oiommu->dev;
1073 struct iotlb_entry e;
1077 omap_pgsz = bytes_to_iopgsz(bytes);
1078 if (omap_pgsz < 0) {
1079 dev_err(dev, "invalid size to map: %d\n", bytes);
1083 dev_dbg(dev, "mapping da 0x%lx to pa %pa size 0x%x\n", da, &pa, bytes);
1085 iotlb_init_entry(&e, da, pa, omap_pgsz);
1087 ret = omap_iopgtable_store_entry(oiommu, &e);
1089 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
1094 static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1097 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1098 struct omap_iommu *oiommu = omap_domain->iommu_dev;
1099 struct device *dev = oiommu->dev;
1101 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
1103 return iopgtable_clear_entry(oiommu, da);
1107 omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1109 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1110 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1111 struct omap_iommu *oiommu;
1114 if (!arch_data || !arch_data->iommu_dev) {
1115 dev_err(dev, "device doesn't have an associated iommu\n");
1119 spin_lock(&omap_domain->lock);
1121 /* only a single device is supported per domain for now */
1122 if (omap_domain->iommu_dev) {
1123 dev_err(dev, "iommu domain is already attached\n");
1128 oiommu = arch_data->iommu_dev;
1130 /* get a handle to and enable the omap iommu */
1131 ret = omap_iommu_attach(oiommu, omap_domain->pgtable);
1133 dev_err(dev, "can't get omap iommu: %d\n", ret);
1137 omap_domain->iommu_dev = oiommu;
1138 omap_domain->dev = dev;
1139 oiommu->domain = domain;
1142 spin_unlock(&omap_domain->lock);
1146 static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
1149 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
1151 /* only a single device is supported per domain for now */
1152 if (omap_domain->iommu_dev != oiommu) {
1153 dev_err(dev, "invalid iommu device\n");
1157 iopgtable_clear_entry_all(oiommu);
1159 omap_iommu_detach(oiommu);
1161 omap_domain->iommu_dev = NULL;
1162 omap_domain->dev = NULL;
1163 oiommu->domain = NULL;
1166 static void omap_iommu_detach_dev(struct iommu_domain *domain,
1169 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1171 spin_lock(&omap_domain->lock);
1172 _omap_iommu_detach_dev(omap_domain, dev);
1173 spin_unlock(&omap_domain->lock);
1176 static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
1178 struct omap_iommu_domain *omap_domain;
1180 if (type != IOMMU_DOMAIN_UNMANAGED)
1183 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1187 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1188 if (!omap_domain->pgtable)
1192 * should never fail, but please keep this around to ensure
1193 * we keep the hardware happy
1195 if (WARN_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE)))
1198 spin_lock_init(&omap_domain->lock);
1200 omap_domain->domain.geometry.aperture_start = 0;
1201 omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
1202 omap_domain->domain.geometry.force_aperture = true;
1204 return &omap_domain->domain;
1207 kfree(omap_domain->pgtable);
1214 static void omap_iommu_domain_free(struct iommu_domain *domain)
1216 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1219 * An iommu device is still attached
1220 * (currently, only one device can be attached) ?
1222 if (omap_domain->iommu_dev)
1223 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1225 kfree(omap_domain->pgtable);
1229 static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
1232 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1233 struct omap_iommu *oiommu = omap_domain->iommu_dev;
1234 struct device *dev = oiommu->dev;
1236 phys_addr_t ret = 0;
1238 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1241 if (iopte_is_small(*pte))
1242 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1243 else if (iopte_is_large(*pte))
1244 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1246 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
1247 (unsigned long long)da);
1249 if (iopgd_is_section(*pgd))
1250 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1251 else if (iopgd_is_super(*pgd))
1252 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1254 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
1255 (unsigned long long)da);
1261 static int omap_iommu_add_device(struct device *dev)
1263 struct omap_iommu_arch_data *arch_data;
1264 struct omap_iommu *oiommu;
1265 struct iommu_group *group;
1266 struct device_node *np;
1267 struct platform_device *pdev;
1271 * Allocate the archdata iommu structure for DT-based devices.
1273 * TODO: Simplify this when removing non-DT support completely from the
1279 np = of_parse_phandle(dev->of_node, "iommus", 0);
1283 pdev = of_find_device_by_node(np);
1284 if (WARN_ON(!pdev)) {
1289 oiommu = platform_get_drvdata(pdev);
1295 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
1301 ret = iommu_device_link(&oiommu->iommu, dev);
1308 arch_data->iommu_dev = oiommu;
1309 dev->archdata.iommu = arch_data;
1312 * IOMMU group initialization calls into omap_iommu_device_group, which
1313 * needs a valid dev->archdata.iommu pointer
1315 group = iommu_group_get_for_dev(dev);
1316 if (IS_ERR(group)) {
1317 iommu_device_unlink(&oiommu->iommu, dev);
1318 dev->archdata.iommu = NULL;
1320 return PTR_ERR(group);
1322 iommu_group_put(group);
1329 static void omap_iommu_remove_device(struct device *dev)
1331 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1333 if (!dev->of_node || !arch_data)
1336 iommu_device_unlink(&arch_data->iommu_dev->iommu, dev);
1337 iommu_group_remove_device(dev);
1339 dev->archdata.iommu = NULL;
1344 static struct iommu_group *omap_iommu_device_group(struct device *dev)
1346 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1347 struct iommu_group *group = ERR_PTR(-EINVAL);
1349 if (arch_data->iommu_dev)
1350 group = arch_data->iommu_dev->group;
1355 static const struct iommu_ops omap_iommu_ops = {
1356 .domain_alloc = omap_iommu_domain_alloc,
1357 .domain_free = omap_iommu_domain_free,
1358 .attach_dev = omap_iommu_attach_dev,
1359 .detach_dev = omap_iommu_detach_dev,
1360 .map = omap_iommu_map,
1361 .unmap = omap_iommu_unmap,
1362 .map_sg = default_iommu_map_sg,
1363 .iova_to_phys = omap_iommu_iova_to_phys,
1364 .add_device = omap_iommu_add_device,
1365 .remove_device = omap_iommu_remove_device,
1366 .device_group = omap_iommu_device_group,
1367 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
1370 static int __init omap_iommu_init(void)
1372 struct kmem_cache *p;
1373 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1374 size_t align = 1 << 10; /* L2 pagetable alignement */
1375 struct device_node *np;
1378 np = of_find_matching_node(NULL, omap_iommu_of_match);
1384 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1390 omap_iommu_debugfs_init();
1392 ret = platform_driver_register(&omap_iommu_driver);
1394 pr_err("%s: failed to register driver\n", __func__);
1398 ret = bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
1405 platform_driver_unregister(&omap_iommu_driver);
1407 kmem_cache_destroy(iopte_cachep);
1410 subsys_initcall(omap_iommu_init);
1411 /* must be ready before omap3isp is probed */