2 * omap iommu: tlb and pagetable primitives
4 * Copyright (C) 2008-2010 Nokia Corporation
5 * Copyright (C) 2013-2017 Texas Instruments Incorporated - http://www.ti.com/
8 * Paul Mundt and Toshihiro Kobayashi
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/dma-mapping.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/ioport.h>
20 #include <linux/platform_device.h>
21 #include <linux/iommu.h>
22 #include <linux/omap-iommu.h>
23 #include <linux/mutex.h>
24 #include <linux/spinlock.h>
26 #include <linux/pm_runtime.h>
28 #include <linux/of_iommu.h>
29 #include <linux/of_irq.h>
30 #include <linux/of_platform.h>
31 #include <linux/regmap.h>
32 #include <linux/mfd/syscon.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;
61 * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
62 * @dom: generic iommu domain handle
64 static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
66 return container_of(dom, struct omap_iommu_domain, domain);
70 * omap_iommu_save_ctx - Save registers for pm off-mode support
73 void omap_iommu_save_ctx(struct device *dev)
75 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
76 struct omap_iommu *obj;
83 while (arch_data->iommu_dev) {
84 obj = arch_data->iommu_dev;
86 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
87 p[i] = iommu_read_reg(obj, i * sizeof(u32));
88 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i,
94 EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
97 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
100 void omap_iommu_restore_ctx(struct device *dev)
102 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
103 struct omap_iommu *obj;
110 while (arch_data->iommu_dev) {
111 obj = arch_data->iommu_dev;
113 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
114 iommu_write_reg(obj, p[i], i * sizeof(u32));
115 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i,
121 EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
123 static void dra7_cfg_dspsys_mmu(struct omap_iommu *obj, bool enable)
130 mask = (1 << (obj->id * DSP_SYS_MMU_CONFIG_EN_SHIFT));
131 val = enable ? mask : 0;
132 regmap_update_bits(obj->syscfg, DSP_SYS_MMU_CONFIG, mask, val);
135 static void __iommu_set_twl(struct omap_iommu *obj, bool on)
137 u32 l = iommu_read_reg(obj, MMU_CNTL);
140 iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
142 iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
146 l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
148 l |= (MMU_CNTL_MMU_EN);
150 iommu_write_reg(obj, l, MMU_CNTL);
153 static int omap2_iommu_enable(struct omap_iommu *obj)
157 if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
160 pa = virt_to_phys(obj->iopgd);
161 if (!IS_ALIGNED(pa, SZ_16K))
164 l = iommu_read_reg(obj, MMU_REVISION);
165 dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
166 (l >> 4) & 0xf, l & 0xf);
168 iommu_write_reg(obj, pa, MMU_TTB);
170 dra7_cfg_dspsys_mmu(obj, true);
172 if (obj->has_bus_err_back)
173 iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
175 __iommu_set_twl(obj, true);
180 static void omap2_iommu_disable(struct omap_iommu *obj)
182 u32 l = iommu_read_reg(obj, MMU_CNTL);
185 iommu_write_reg(obj, l, MMU_CNTL);
186 dra7_cfg_dspsys_mmu(obj, false);
188 dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
191 static int iommu_enable(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 if (pdata && pdata->deassert_reset) {
198 err = pdata->deassert_reset(pdev, pdata->reset_name);
200 dev_err(obj->dev, "deassert_reset failed: %d\n", err);
205 pm_runtime_get_sync(obj->dev);
207 err = omap2_iommu_enable(obj);
212 static void iommu_disable(struct omap_iommu *obj)
214 struct platform_device *pdev = to_platform_device(obj->dev);
215 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
217 omap2_iommu_disable(obj);
219 pm_runtime_put_sync(obj->dev);
221 if (pdata && pdata->assert_reset)
222 pdata->assert_reset(pdev, pdata->reset_name);
228 static u32 iotlb_cr_to_virt(struct cr_regs *cr)
230 u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
231 u32 mask = get_cam_va_mask(cr->cam & page_size);
233 return cr->cam & mask;
236 static u32 get_iopte_attr(struct iotlb_entry *e)
240 attr = e->mixed << 5;
242 attr |= e->elsz >> 3;
243 attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
244 (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
248 static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
250 u32 status, fault_addr;
252 status = iommu_read_reg(obj, MMU_IRQSTATUS);
253 status &= MMU_IRQ_MASK;
259 fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
262 iommu_write_reg(obj, status, MMU_IRQSTATUS);
267 void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
271 val = iommu_read_reg(obj, MMU_LOCK);
273 l->base = MMU_LOCK_BASE(val);
274 l->vict = MMU_LOCK_VICT(val);
277 void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
281 val = (l->base << MMU_LOCK_BASE_SHIFT);
282 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
284 iommu_write_reg(obj, val, MMU_LOCK);
287 static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
289 cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
290 cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
293 static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
295 iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
296 iommu_write_reg(obj, cr->ram, MMU_RAM);
298 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
299 iommu_write_reg(obj, 1, MMU_LD_TLB);
302 /* only used in iotlb iteration for-loop */
303 struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
308 iotlb_lock_get(obj, &l);
310 iotlb_lock_set(obj, &l);
311 iotlb_read_cr(obj, &cr);
316 #ifdef PREFETCH_IOTLB
317 static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
318 struct iotlb_entry *e)
325 if (e->da & ~(get_cam_va_mask(e->pgsz))) {
326 dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
328 return ERR_PTR(-EINVAL);
331 cr = kmalloc(sizeof(*cr), GFP_KERNEL);
333 return ERR_PTR(-ENOMEM);
335 cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
336 cr->ram = e->pa | e->endian | e->elsz | e->mixed;
342 * load_iotlb_entry - Set an iommu tlb entry
344 * @e: an iommu tlb entry info
346 static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
352 if (!obj || !obj->nr_tlb_entries || !e)
355 pm_runtime_get_sync(obj->dev);
357 iotlb_lock_get(obj, &l);
358 if (l.base == obj->nr_tlb_entries) {
359 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
367 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
368 if (!iotlb_cr_valid(&tmp))
371 if (i == obj->nr_tlb_entries) {
372 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
377 iotlb_lock_get(obj, &l);
380 iotlb_lock_set(obj, &l);
383 cr = iotlb_alloc_cr(obj, e);
385 pm_runtime_put_sync(obj->dev);
389 iotlb_load_cr(obj, cr);
394 /* increment victim for next tlb load */
395 if (++l.vict == obj->nr_tlb_entries)
397 iotlb_lock_set(obj, &l);
399 pm_runtime_put_sync(obj->dev);
403 #else /* !PREFETCH_IOTLB */
405 static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
410 #endif /* !PREFETCH_IOTLB */
412 static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
414 return load_iotlb_entry(obj, e);
418 * flush_iotlb_page - Clear an iommu tlb entry
420 * @da: iommu device virtual address
422 * Clear an iommu tlb entry which includes 'da' address.
424 static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
429 pm_runtime_get_sync(obj->dev);
431 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
435 if (!iotlb_cr_valid(&cr))
438 start = iotlb_cr_to_virt(&cr);
439 bytes = iopgsz_to_bytes(cr.cam & 3);
441 if ((start <= da) && (da < start + bytes)) {
442 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
443 __func__, start, da, bytes);
444 iotlb_load_cr(obj, &cr);
445 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
449 pm_runtime_put_sync(obj->dev);
451 if (i == obj->nr_tlb_entries)
452 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
456 * flush_iotlb_all - Clear all iommu tlb entries
459 static void flush_iotlb_all(struct omap_iommu *obj)
463 pm_runtime_get_sync(obj->dev);
467 iotlb_lock_set(obj, &l);
469 iommu_write_reg(obj, 1, MMU_GFLUSH);
471 pm_runtime_put_sync(obj->dev);
475 * H/W pagetable operations
477 static void flush_iopte_range(struct device *dev, dma_addr_t dma,
478 unsigned long offset, int num_entries)
480 size_t size = num_entries * sizeof(u32);
482 dma_sync_single_range_for_device(dev, dma, offset, size, DMA_TO_DEVICE);
485 static void iopte_free(struct omap_iommu *obj, u32 *iopte, bool dma_valid)
489 /* Note: freed iopte's must be clean ready for re-use */
492 pt_dma = virt_to_phys(iopte);
493 dma_unmap_single(obj->dev, pt_dma, IOPTE_TABLE_SIZE,
497 kmem_cache_free(iopte_cachep, iopte);
501 static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd,
502 dma_addr_t *pt_dma, u32 da)
505 unsigned long offset = iopgd_index(da) * sizeof(da);
507 /* a table has already existed */
512 * do the allocation outside the page table lock
514 spin_unlock(&obj->page_table_lock);
515 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
516 spin_lock(&obj->page_table_lock);
520 return ERR_PTR(-ENOMEM);
522 *pt_dma = dma_map_single(obj->dev, iopte, IOPTE_TABLE_SIZE,
524 if (dma_mapping_error(obj->dev, *pt_dma)) {
525 dev_err(obj->dev, "DMA map error for L2 table\n");
526 iopte_free(obj, iopte, false);
527 return ERR_PTR(-ENOMEM);
531 * we rely on dma address and the physical address to be
532 * the same for mapping the L2 table
534 if (WARN_ON(*pt_dma != virt_to_phys(iopte))) {
535 dev_err(obj->dev, "DMA translation error for L2 table\n");
536 dma_unmap_single(obj->dev, *pt_dma, IOPTE_TABLE_SIZE,
538 iopte_free(obj, iopte, false);
539 return ERR_PTR(-ENOMEM);
542 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
544 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
545 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
547 /* We raced, free the reduniovant table */
548 iopte_free(obj, iopte, false);
552 iopte = iopte_offset(iopgd, da);
553 *pt_dma = iopgd_page_paddr(iopgd);
555 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
556 __func__, da, iopgd, *iopgd, iopte, *iopte);
561 static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
563 u32 *iopgd = iopgd_offset(obj, da);
564 unsigned long offset = iopgd_index(da) * sizeof(da);
566 if ((da | pa) & ~IOSECTION_MASK) {
567 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
568 __func__, da, pa, IOSECTION_SIZE);
572 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
573 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
577 static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
579 u32 *iopgd = iopgd_offset(obj, da);
580 unsigned long offset = iopgd_index(da) * sizeof(da);
583 if ((da | pa) & ~IOSUPER_MASK) {
584 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
585 __func__, da, pa, IOSUPER_SIZE);
589 for (i = 0; i < 16; i++)
590 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
591 flush_iopte_range(obj->dev, obj->pd_dma, offset, 16);
595 static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
597 u32 *iopgd = iopgd_offset(obj, da);
599 u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
600 unsigned long offset = iopte_index(da) * sizeof(da);
603 return PTR_ERR(iopte);
605 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
606 flush_iopte_range(obj->dev, pt_dma, offset, 1);
608 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
609 __func__, da, pa, iopte, *iopte);
614 static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
616 u32 *iopgd = iopgd_offset(obj, da);
618 u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
619 unsigned long offset = iopte_index(da) * sizeof(da);
622 if ((da | pa) & ~IOLARGE_MASK) {
623 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
624 __func__, da, pa, IOLARGE_SIZE);
629 return PTR_ERR(iopte);
631 for (i = 0; i < 16; i++)
632 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
633 flush_iopte_range(obj->dev, pt_dma, offset, 16);
638 iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
640 int (*fn)(struct omap_iommu *, u32, u32, u32);
648 case MMU_CAM_PGSZ_16M:
649 fn = iopgd_alloc_super;
651 case MMU_CAM_PGSZ_1M:
652 fn = iopgd_alloc_section;
654 case MMU_CAM_PGSZ_64K:
655 fn = iopte_alloc_large;
657 case MMU_CAM_PGSZ_4K:
658 fn = iopte_alloc_page;
668 prot = get_iopte_attr(e);
670 spin_lock(&obj->page_table_lock);
671 err = fn(obj, e->da, e->pa, prot);
672 spin_unlock(&obj->page_table_lock);
678 * omap_iopgtable_store_entry - Make an iommu pte entry
680 * @e: an iommu tlb entry info
683 omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
687 flush_iotlb_page(obj, e->da);
688 err = iopgtable_store_entry_core(obj, e);
690 prefetch_iotlb_entry(obj, e);
695 * iopgtable_lookup_entry - Lookup an iommu pte entry
697 * @da: iommu device virtual address
698 * @ppgd: iommu pgd entry pointer to be returned
699 * @ppte: iommu pte entry pointer to be returned
702 iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
704 u32 *iopgd, *iopte = NULL;
706 iopgd = iopgd_offset(obj, da);
710 if (iopgd_is_table(*iopgd))
711 iopte = iopte_offset(iopgd, da);
717 static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
720 u32 *iopgd = iopgd_offset(obj, da);
723 unsigned long pd_offset = iopgd_index(da) * sizeof(da);
724 unsigned long pt_offset = iopte_index(da) * sizeof(da);
729 if (iopgd_is_table(*iopgd)) {
731 u32 *iopte = iopte_offset(iopgd, da);
734 if (*iopte & IOPTE_LARGE) {
736 /* rewind to the 1st entry */
737 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
740 memset(iopte, 0, nent * sizeof(*iopte));
741 pt_dma = iopgd_page_paddr(iopgd);
742 flush_iopte_range(obj->dev, pt_dma, pt_offset, nent);
745 * do table walk to check if this table is necessary or not
747 iopte = iopte_offset(iopgd, 0);
748 for (i = 0; i < PTRS_PER_IOPTE; i++)
752 iopte_free(obj, iopte, true);
753 nent = 1; /* for the next L1 entry */
756 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
758 /* rewind to the 1st entry */
759 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
763 memset(iopgd, 0, nent * sizeof(*iopgd));
764 flush_iopte_range(obj->dev, obj->pd_dma, pd_offset, nent);
770 * iopgtable_clear_entry - Remove an iommu pte entry
772 * @da: iommu device virtual address
774 static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
778 spin_lock(&obj->page_table_lock);
780 bytes = iopgtable_clear_entry_core(obj, da);
781 flush_iotlb_page(obj, da);
783 spin_unlock(&obj->page_table_lock);
788 static void iopgtable_clear_entry_all(struct omap_iommu *obj)
790 unsigned long offset;
793 spin_lock(&obj->page_table_lock);
795 for (i = 0; i < PTRS_PER_IOPGD; i++) {
799 da = i << IOPGD_SHIFT;
800 iopgd = iopgd_offset(obj, da);
801 offset = iopgd_index(da) * sizeof(da);
806 if (iopgd_is_table(*iopgd))
807 iopte_free(obj, iopte_offset(iopgd, 0), true);
810 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
813 flush_iotlb_all(obj);
815 spin_unlock(&obj->page_table_lock);
819 * Device IOMMU generic operations
821 static irqreturn_t iommu_fault_handler(int irq, void *data)
825 struct omap_iommu *obj = data;
826 struct iommu_domain *domain = obj->domain;
827 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
829 if (!omap_domain->dev)
832 errs = iommu_report_fault(obj, &da);
836 /* Fault callback or TLB/PTE Dynamic loading */
837 if (!report_iommu_fault(domain, obj->dev, da, 0))
840 iommu_write_reg(obj, 0, MMU_IRQENABLE);
842 iopgd = iopgd_offset(obj, da);
844 if (!iopgd_is_table(*iopgd)) {
845 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
846 obj->name, errs, da, iopgd, *iopgd);
850 iopte = iopte_offset(iopgd, da);
852 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
853 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
859 * omap_iommu_attach() - attach iommu device to an iommu domain
860 * @obj: target omap iommu device
863 static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd)
867 spin_lock(&obj->iommu_lock);
869 obj->pd_dma = dma_map_single(obj->dev, iopgd, IOPGD_TABLE_SIZE,
871 if (dma_mapping_error(obj->dev, obj->pd_dma)) {
872 dev_err(obj->dev, "DMA map error for L1 table\n");
878 err = iommu_enable(obj);
881 flush_iotlb_all(obj);
883 spin_unlock(&obj->iommu_lock);
885 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
890 spin_unlock(&obj->iommu_lock);
896 * omap_iommu_detach - release iommu device
899 static void omap_iommu_detach(struct omap_iommu *obj)
901 if (!obj || IS_ERR(obj))
904 spin_lock(&obj->iommu_lock);
906 dma_unmap_single(obj->dev, obj->pd_dma, IOPGD_TABLE_SIZE,
912 spin_unlock(&obj->iommu_lock);
914 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
917 static bool omap_iommu_can_register(struct platform_device *pdev)
919 struct device_node *np = pdev->dev.of_node;
921 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
925 * restrict IOMMU core registration only for processor-port MDMA MMUs
928 if ((!strcmp(dev_name(&pdev->dev), "40d01000.mmu")) ||
929 (!strcmp(dev_name(&pdev->dev), "41501000.mmu")))
935 static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device *pdev,
936 struct omap_iommu *obj)
938 struct device_node *np = pdev->dev.of_node;
941 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
944 if (!of_property_read_bool(np, "ti,syscon-mmuconfig")) {
945 dev_err(&pdev->dev, "ti,syscon-mmuconfig property is missing\n");
950 syscon_regmap_lookup_by_phandle(np, "ti,syscon-mmuconfig");
951 if (IS_ERR(obj->syscfg)) {
952 /* can fail with -EPROBE_DEFER */
953 ret = PTR_ERR(obj->syscfg);
957 if (of_property_read_u32_index(np, "ti,syscon-mmuconfig", 1,
959 dev_err(&pdev->dev, "couldn't get the IOMMU instance id within subsystem\n");
963 if (obj->id != 0 && obj->id != 1) {
964 dev_err(&pdev->dev, "invalid IOMMU instance id\n");
972 * OMAP Device MMU(IOMMU) detection
974 static int omap_iommu_probe(struct platform_device *pdev)
978 struct omap_iommu *obj;
979 struct resource *res;
980 struct device_node *of = pdev->dev.of_node;
983 pr_err("%s: only DT-based devices are supported\n", __func__);
987 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
991 obj->name = dev_name(&pdev->dev);
992 obj->nr_tlb_entries = 32;
993 err = of_property_read_u32(of, "ti,#tlb-entries", &obj->nr_tlb_entries);
994 if (err && err != -EINVAL)
996 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
998 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
999 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
1001 obj->dev = &pdev->dev;
1002 obj->ctx = (void *)obj + sizeof(*obj);
1004 spin_lock_init(&obj->iommu_lock);
1005 spin_lock_init(&obj->page_table_lock);
1007 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1008 obj->regbase = devm_ioremap_resource(obj->dev, res);
1009 if (IS_ERR(obj->regbase))
1010 return PTR_ERR(obj->regbase);
1012 err = omap_iommu_dra7_get_dsp_system_cfg(pdev, obj);
1016 irq = platform_get_irq(pdev, 0);
1020 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
1021 dev_name(obj->dev), obj);
1024 platform_set_drvdata(pdev, obj);
1026 if (omap_iommu_can_register(pdev)) {
1027 obj->group = iommu_group_alloc();
1028 if (IS_ERR(obj->group))
1029 return PTR_ERR(obj->group);
1031 err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL,
1036 iommu_device_set_ops(&obj->iommu, &omap_iommu_ops);
1038 err = iommu_device_register(&obj->iommu);
1043 pm_runtime_irq_safe(obj->dev);
1044 pm_runtime_enable(obj->dev);
1046 omap_iommu_debugfs_add(obj);
1048 dev_info(&pdev->dev, "%s registered\n", obj->name);
1053 iommu_device_sysfs_remove(&obj->iommu);
1055 iommu_group_put(obj->group);
1059 static int omap_iommu_remove(struct platform_device *pdev)
1061 struct omap_iommu *obj = platform_get_drvdata(pdev);
1064 iommu_group_put(obj->group);
1067 iommu_device_sysfs_remove(&obj->iommu);
1068 iommu_device_unregister(&obj->iommu);
1071 omap_iommu_debugfs_remove(obj);
1073 pm_runtime_disable(obj->dev);
1075 dev_info(&pdev->dev, "%s removed\n", obj->name);
1079 static const struct of_device_id omap_iommu_of_match[] = {
1080 { .compatible = "ti,omap2-iommu" },
1081 { .compatible = "ti,omap4-iommu" },
1082 { .compatible = "ti,dra7-iommu" },
1083 { .compatible = "ti,dra7-dsp-iommu" },
1087 static struct platform_driver omap_iommu_driver = {
1088 .probe = omap_iommu_probe,
1089 .remove = omap_iommu_remove,
1091 .name = "omap-iommu",
1092 .of_match_table = of_match_ptr(omap_iommu_of_match),
1096 static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
1098 memset(e, 0, sizeof(*e));
1102 e->valid = MMU_CAM_V;
1104 e->endian = MMU_RAM_ENDIAN_LITTLE;
1105 e->elsz = MMU_RAM_ELSZ_8;
1108 return iopgsz_to_bytes(e->pgsz);
1111 static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
1112 phys_addr_t pa, size_t bytes, int prot)
1114 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1115 struct device *dev = omap_domain->dev;
1116 struct omap_iommu_device *iommu;
1117 struct omap_iommu *oiommu;
1118 struct iotlb_entry e;
1123 omap_pgsz = bytes_to_iopgsz(bytes);
1124 if (omap_pgsz < 0) {
1125 dev_err(dev, "invalid size to map: %d\n", bytes);
1129 dev_dbg(dev, "mapping da 0x%lx to pa %pa size 0x%x\n", da, &pa, bytes);
1131 iotlb_init_entry(&e, da, pa, omap_pgsz);
1133 iommu = omap_domain->iommus;
1134 for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
1135 oiommu = iommu->iommu_dev;
1136 ret = omap_iopgtable_store_entry(oiommu, &e);
1138 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n",
1147 oiommu = iommu->iommu_dev;
1148 iopgtable_clear_entry(oiommu, da);
1155 static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1158 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1159 struct device *dev = omap_domain->dev;
1160 struct omap_iommu_device *iommu;
1161 struct omap_iommu *oiommu;
1166 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
1168 iommu = omap_domain->iommus;
1169 for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
1170 oiommu = iommu->iommu_dev;
1171 bytes = iopgtable_clear_entry(oiommu, da);
1177 * simplify return - we are only checking if any of the iommus
1178 * reported an error, but not if all of them are unmapping the
1179 * same number of entries. This should not occur due to the
1180 * mirror programming.
1182 return error ? 0 : bytes;
1185 static int omap_iommu_count(struct device *dev)
1187 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1190 while (arch_data->iommu_dev) {
1198 /* caller should call cleanup if this function fails */
1199 static int omap_iommu_attach_init(struct device *dev,
1200 struct omap_iommu_domain *odomain)
1202 struct omap_iommu_device *iommu;
1205 odomain->num_iommus = omap_iommu_count(dev);
1206 if (!odomain->num_iommus)
1209 odomain->iommus = kcalloc(odomain->num_iommus, sizeof(*iommu),
1211 if (!odomain->iommus)
1214 iommu = odomain->iommus;
1215 for (i = 0; i < odomain->num_iommus; i++, iommu++) {
1216 iommu->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_ATOMIC);
1217 if (!iommu->pgtable)
1221 * should never fail, but please keep this around to ensure
1222 * we keep the hardware happy
1224 if (WARN_ON(!IS_ALIGNED((long)iommu->pgtable,
1232 static void omap_iommu_detach_fini(struct omap_iommu_domain *odomain)
1235 struct omap_iommu_device *iommu = odomain->iommus;
1237 for (i = 0; iommu && i < odomain->num_iommus; i++, iommu++)
1238 kfree(iommu->pgtable);
1240 kfree(odomain->iommus);
1241 odomain->num_iommus = 0;
1242 odomain->iommus = NULL;
1246 omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1248 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1249 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1250 struct omap_iommu_device *iommu;
1251 struct omap_iommu *oiommu;
1255 if (!arch_data || !arch_data->iommu_dev) {
1256 dev_err(dev, "device doesn't have an associated iommu\n");
1260 spin_lock(&omap_domain->lock);
1262 /* only a single client device can be attached to a domain */
1263 if (omap_domain->dev) {
1264 dev_err(dev, "iommu domain is already attached\n");
1269 ret = omap_iommu_attach_init(dev, omap_domain);
1271 dev_err(dev, "failed to allocate required iommu data %d\n",
1276 iommu = omap_domain->iommus;
1277 for (i = 0; i < omap_domain->num_iommus; i++, iommu++, arch_data++) {
1278 /* configure and enable the omap iommu */
1279 oiommu = arch_data->iommu_dev;
1280 ret = omap_iommu_attach(oiommu, iommu->pgtable);
1282 dev_err(dev, "can't get omap iommu: %d\n", ret);
1286 oiommu->domain = domain;
1287 iommu->iommu_dev = oiommu;
1290 omap_domain->dev = dev;
1298 oiommu = iommu->iommu_dev;
1299 omap_iommu_detach(oiommu);
1300 iommu->iommu_dev = NULL;
1301 oiommu->domain = NULL;
1304 omap_iommu_detach_fini(omap_domain);
1306 spin_unlock(&omap_domain->lock);
1310 static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
1313 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1314 struct omap_iommu_device *iommu = omap_domain->iommus;
1315 struct omap_iommu *oiommu;
1318 if (!omap_domain->dev) {
1319 dev_err(dev, "domain has no attached device\n");
1323 /* only a single device is supported per domain for now */
1324 if (omap_domain->dev != dev) {
1325 dev_err(dev, "invalid attached device\n");
1330 * cleanup in the reverse order of attachment - this addresses
1331 * any h/w dependencies between multiple instances, if any
1333 iommu += (omap_domain->num_iommus - 1);
1334 arch_data += (omap_domain->num_iommus - 1);
1335 for (i = 0; i < omap_domain->num_iommus; i++, iommu--, arch_data--) {
1336 oiommu = iommu->iommu_dev;
1337 iopgtable_clear_entry_all(oiommu);
1339 omap_iommu_detach(oiommu);
1340 iommu->iommu_dev = NULL;
1341 oiommu->domain = NULL;
1344 omap_iommu_detach_fini(omap_domain);
1346 omap_domain->dev = NULL;
1349 static void omap_iommu_detach_dev(struct iommu_domain *domain,
1352 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1354 spin_lock(&omap_domain->lock);
1355 _omap_iommu_detach_dev(omap_domain, dev);
1356 spin_unlock(&omap_domain->lock);
1359 static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
1361 struct omap_iommu_domain *omap_domain;
1363 if (type != IOMMU_DOMAIN_UNMANAGED)
1366 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1370 spin_lock_init(&omap_domain->lock);
1372 omap_domain->domain.geometry.aperture_start = 0;
1373 omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
1374 omap_domain->domain.geometry.force_aperture = true;
1376 return &omap_domain->domain;
1379 static void omap_iommu_domain_free(struct iommu_domain *domain)
1381 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1384 * An iommu device is still attached
1385 * (currently, only one device can be attached) ?
1387 if (omap_domain->dev)
1388 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1393 static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
1396 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1397 struct omap_iommu_device *iommu = omap_domain->iommus;
1398 struct omap_iommu *oiommu = iommu->iommu_dev;
1399 struct device *dev = oiommu->dev;
1401 phys_addr_t ret = 0;
1404 * all the iommus within the domain will have identical programming,
1405 * so perform the lookup using just the first iommu
1407 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1410 if (iopte_is_small(*pte))
1411 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1412 else if (iopte_is_large(*pte))
1413 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1415 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
1416 (unsigned long long)da);
1418 if (iopgd_is_section(*pgd))
1419 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1420 else if (iopgd_is_super(*pgd))
1421 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1423 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
1424 (unsigned long long)da);
1430 static int omap_iommu_add_device(struct device *dev)
1432 struct omap_iommu_arch_data *arch_data, *tmp;
1433 struct omap_iommu *oiommu;
1434 struct iommu_group *group;
1435 struct device_node *np;
1436 struct platform_device *pdev;
1441 * Allocate the archdata iommu structure for DT-based devices.
1443 * TODO: Simplify this when removing non-DT support completely from the
1450 * retrieve the count of IOMMU nodes using phandle size as element size
1451 * since #iommu-cells = 0 for OMAP
1453 num_iommus = of_property_count_elems_of_size(dev->of_node, "iommus",
1458 arch_data = kcalloc(num_iommus + 1, sizeof(*arch_data), GFP_KERNEL);
1462 for (i = 0, tmp = arch_data; i < num_iommus; i++, tmp++) {
1463 np = of_parse_phandle(dev->of_node, "iommus", i);
1469 pdev = of_find_device_by_node(np);
1470 if (WARN_ON(!pdev)) {
1476 oiommu = platform_get_drvdata(pdev);
1483 tmp->iommu_dev = oiommu;
1489 * use the first IOMMU alone for the sysfs device linking.
1490 * TODO: Evaluate if a single iommu_group needs to be
1491 * maintained for both IOMMUs
1493 oiommu = arch_data->iommu_dev;
1494 ret = iommu_device_link(&oiommu->iommu, dev);
1500 dev->archdata.iommu = arch_data;
1503 * IOMMU group initialization calls into omap_iommu_device_group, which
1504 * needs a valid dev->archdata.iommu pointer
1506 group = iommu_group_get_for_dev(dev);
1507 if (IS_ERR(group)) {
1508 iommu_device_unlink(&oiommu->iommu, dev);
1509 dev->archdata.iommu = NULL;
1511 return PTR_ERR(group);
1513 iommu_group_put(group);
1518 static void omap_iommu_remove_device(struct device *dev)
1520 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1522 if (!dev->of_node || !arch_data)
1525 iommu_device_unlink(&arch_data->iommu_dev->iommu, dev);
1526 iommu_group_remove_device(dev);
1528 dev->archdata.iommu = NULL;
1533 static struct iommu_group *omap_iommu_device_group(struct device *dev)
1535 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1536 struct iommu_group *group = ERR_PTR(-EINVAL);
1538 if (arch_data->iommu_dev)
1539 group = iommu_group_ref_get(arch_data->iommu_dev->group);
1544 static const struct iommu_ops omap_iommu_ops = {
1545 .domain_alloc = omap_iommu_domain_alloc,
1546 .domain_free = omap_iommu_domain_free,
1547 .attach_dev = omap_iommu_attach_dev,
1548 .detach_dev = omap_iommu_detach_dev,
1549 .map = omap_iommu_map,
1550 .unmap = omap_iommu_unmap,
1551 .iova_to_phys = omap_iommu_iova_to_phys,
1552 .add_device = omap_iommu_add_device,
1553 .remove_device = omap_iommu_remove_device,
1554 .device_group = omap_iommu_device_group,
1555 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
1558 static int __init omap_iommu_init(void)
1560 struct kmem_cache *p;
1561 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1562 size_t align = 1 << 10; /* L2 pagetable alignement */
1563 struct device_node *np;
1566 np = of_find_matching_node(NULL, omap_iommu_of_match);
1572 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1578 omap_iommu_debugfs_init();
1580 ret = platform_driver_register(&omap_iommu_driver);
1582 pr_err("%s: failed to register driver\n", __func__);
1586 ret = bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
1593 platform_driver_unregister(&omap_iommu_driver);
1595 kmem_cache_destroy(iopte_cachep);
1598 subsys_initcall(omap_iommu_init);
1599 /* must be ready before omap3isp is probed */