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>
24 #include <linux/pm_runtime.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
;
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
*obj
= dev_to_omap_iommu(dev
);
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
);
87 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
90 void omap_iommu_restore_ctx(struct device
*dev
)
92 struct omap_iommu
*obj
= dev_to_omap_iommu(dev
);
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
)
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
);
120 iommu_write_reg(obj
, MMU_IRQ_TWL_MASK
, MMU_IRQENABLE
);
122 iommu_write_reg(obj
, MMU_IRQ_TLB_MISS_MASK
, MMU_IRQENABLE
);
126 l
|= (MMU_CNTL_MMU_EN
| MMU_CNTL_TWL_EN
);
128 l
|= (MMU_CNTL_MMU_EN
);
130 iommu_write_reg(obj
, l
, MMU_CNTL
);
133 static int omap2_iommu_enable(struct omap_iommu
*obj
)
137 if (!obj
->iopgd
|| !IS_ALIGNED((u32
)obj
->iopgd
, SZ_16K
))
140 pa
= virt_to_phys(obj
->iopgd
);
141 if (!IS_ALIGNED(pa
, SZ_16K
))
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);
160 static void omap2_iommu_disable(struct omap_iommu
*obj
)
162 u32 l
= iommu_read_reg(obj
, MMU_CNTL
);
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
)
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
);
180 dev_err(obj
->dev
, "deassert_reset failed: %d\n", err
);
185 pm_runtime_get_sync(obj
->dev
);
187 err
= omap2_iommu_enable(obj
);
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
);
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
)
220 attr
= e
->mixed
<< 5;
222 attr
|= e
->elsz
>> 3;
223 attr
<<= (((e
->pgsz
== MMU_CAM_PGSZ_4K
) ||
224 (e
->pgsz
== MMU_CAM_PGSZ_64K
)) ? 0 : 6);
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
;
239 fault_addr
= iommu_read_reg(obj
, MMU_FAULT_AD
);
242 iommu_write_reg(obj
, status
, MMU_IRQSTATUS
);
247 void iotlb_lock_get(struct omap_iommu
*obj
, struct iotlb_lock
*l
)
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
)
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
)
288 iotlb_lock_get(obj
, &l
);
290 iotlb_lock_set(obj
, &l
);
291 iotlb_read_cr(obj
, &cr
);
296 #ifdef PREFETCH_IOTLB
297 static struct cr_regs
*iotlb_alloc_cr(struct omap_iommu
*obj
,
298 struct iotlb_entry
*e
)
305 if (e
->da
& ~(get_cam_va_mask(e
->pgsz
))) {
306 dev_err(obj
->dev
, "%s:\twrong alignment: %08x\n", __func__
,
308 return ERR_PTR(-EINVAL
);
311 cr
= kmalloc(sizeof(*cr
), GFP_KERNEL
);
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
;
322 * load_iotlb_entry - Set an iommu tlb entry
324 * @e: an iommu tlb entry info
326 static int load_iotlb_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
332 if (!obj
|| !obj
->nr_tlb_entries
|| !e
)
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__
);
347 for_each_iotlb_cr(obj
, obj
->nr_tlb_entries
, i
, tmp
)
348 if (!iotlb_cr_valid(&tmp
))
351 if (i
== obj
->nr_tlb_entries
) {
352 dev_dbg(obj
->dev
, "%s: full: no entry\n", __func__
);
357 iotlb_lock_get(obj
, &l
);
360 iotlb_lock_set(obj
, &l
);
363 cr
= iotlb_alloc_cr(obj
, e
);
365 pm_runtime_put_sync(obj
->dev
);
369 iotlb_load_cr(obj
, cr
);
374 /* increment victim for next tlb load */
375 if (++l
.vict
== obj
->nr_tlb_entries
)
377 iotlb_lock_set(obj
, &l
);
379 pm_runtime_put_sync(obj
->dev
);
383 #else /* !PREFETCH_IOTLB */
385 static int load_iotlb_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
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
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
)
409 pm_runtime_get_sync(obj
->dev
);
411 for_each_iotlb_cr(obj
, obj
->nr_tlb_entries
, i
, cr
) {
415 if (!iotlb_cr_valid(&cr
))
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
);
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
439 static void flush_iotlb_all(struct omap_iommu
*obj
)
443 pm_runtime_get_sync(obj
->dev
);
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 */
461 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
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 */
471 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
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 */
481 kmem_cache_free(iopte_cachep
, iopte
);
484 static u32
*iopte_alloc(struct omap_iommu
*obj
, u32
*iopgd
, u32 da
)
488 /* a table has already existed */
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
);
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
);
508 /* We raced, free the reduniovant table */
513 iopte
= iopte_offset(iopgd
, da
);
516 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
517 __func__
, da
, iopgd
, *iopgd
, iopte
, *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
);
532 *iopgd
= (pa
& IOSECTION_MASK
) | prot
| IOPGD_SECTION
;
533 flush_iopgd_range(iopgd
, iopgd
);
537 static int iopgd_alloc_super(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
539 u32
*iopgd
= iopgd_offset(obj
, da
);
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
);
548 for (i
= 0; i
< 16; i
++)
549 *(iopgd
+ i
) = (pa
& IOSUPER_MASK
) | prot
| IOPGD_SUPER
;
550 flush_iopgd_range(iopgd
, iopgd
+ 15);
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
);
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
);
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
);
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
);
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);
593 iopgtable_store_entry_core(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
595 int (*fn
)(struct omap_iommu
*, u32
, u32
, u32
);
603 case MMU_CAM_PGSZ_16M
:
604 fn
= iopgd_alloc_super
;
606 case MMU_CAM_PGSZ_1M
:
607 fn
= iopgd_alloc_section
;
609 case MMU_CAM_PGSZ_64K
:
610 fn
= iopte_alloc_large
;
612 case MMU_CAM_PGSZ_4K
:
613 fn
= iopte_alloc_page
;
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
);
633 * omap_iopgtable_store_entry - Make an iommu pte entry
635 * @e: an iommu tlb entry info
638 omap_iopgtable_store_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
642 flush_iotlb_page(obj
, e
->da
);
643 err
= iopgtable_store_entry_core(obj
, e
);
645 prefetch_iotlb_entry(obj
, e
);
650 * iopgtable_lookup_entry - Lookup an iommu pte entry
652 * @da: iommu device virtual address
653 * @ppgd: iommu pgd entry pointer to be returned
654 * @ppte: iommu pte entry pointer to be returned
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
);
665 if (iopgd_is_table(*iopgd
))
666 iopte
= iopte_offset(iopgd
, da
);
672 static size_t iopgtable_clear_entry_core(struct omap_iommu
*obj
, u32 da
)
675 u32
*iopgd
= iopgd_offset(obj
, da
);
681 if (iopgd_is_table(*iopgd
)) {
683 u32
*iopte
= iopte_offset(iopgd
, da
);
686 if (*iopte
& IOPTE_LARGE
) {
688 /* rewind to the 1st entry */
689 iopte
= iopte_offset(iopgd
, (da
& IOLARGE_MASK
));
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
++)
704 nent
= 1; /* for the next L1 entry */
707 if ((*iopgd
& IOPGD_SUPER
) == IOPGD_SUPER
) {
709 /* rewind to the 1st entry */
710 iopgd
= iopgd_offset(obj
, (da
& IOSUPER_MASK
));
714 memset(iopgd
, 0, nent
* sizeof(*iopgd
));
715 flush_iopgd_range(iopgd
, iopgd
+ (nent
- 1) * sizeof(*iopgd
));
721 * iopgtable_clear_entry - Remove an iommu pte entry
723 * @da: iommu device virtual address
725 static size_t iopgtable_clear_entry(struct omap_iommu
*obj
, u32 da
)
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
);
739 static void iopgtable_clear_entry_all(struct omap_iommu
*obj
)
743 spin_lock(&obj
->page_table_lock
);
745 for (i
= 0; i
< PTRS_PER_IOPGD
; i
++) {
749 da
= i
<< IOPGD_SHIFT
;
750 iopgd
= iopgd_offset(obj
, da
);
755 if (iopgd_is_table(*iopgd
))
756 iopte_free(iopte_offset(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
)
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
)
781 errs
= iommu_report_fault(obj
, &da
);
785 /* Fault callback or TLB/PTE Dynamic loading */
786 if (!report_iommu_fault(domain
, obj
->dev
, da
, 0))
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
);
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
);
808 * omap_iommu_attach() - attach iommu device to an iommu domain
809 * @obj: target omap iommu device
812 static int omap_iommu_attach(struct omap_iommu
*obj
, u32
*iopgd
)
816 spin_lock(&obj
->iommu_lock
);
819 err
= iommu_enable(obj
);
822 flush_iotlb_all(obj
);
824 spin_unlock(&obj
->iommu_lock
);
826 dev_dbg(obj
->dev
, "%s: %s\n", __func__
, obj
->name
);
831 spin_unlock(&obj
->iommu_lock
);
837 * omap_iommu_detach - release iommu device
840 static void omap_iommu_detach(struct omap_iommu
*obj
)
842 if (!obj
|| IS_ERR(obj
))
845 spin_lock(&obj
->iommu_lock
);
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
;
861 if (!of_device_is_compatible(np
, "ti,dra7-dsp-iommu"))
864 if (!of_property_read_bool(np
, "ti,syscon-mmuconfig")) {
865 dev_err(&pdev
->dev
, "ti,syscon-mmuconfig property is missing\n");
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
);
877 if (of_property_read_u32_index(np
, "ti,syscon-mmuconfig", 1,
879 dev_err(&pdev
->dev
, "couldn't get the IOMMU instance id within subsystem\n");
883 if (obj
->id
!= 0 && obj
->id
!= 1) {
884 dev_err(&pdev
->dev
, "invalid IOMMU instance id\n");
892 * OMAP Device MMU(IOMMU) detection
894 static int omap_iommu_probe(struct platform_device
*pdev
)
898 struct omap_iommu
*obj
;
899 struct resource
*res
;
900 struct device_node
*of
= pdev
->dev
.of_node
;
903 pr_err("%s: only DT-based devices are supported\n", __func__
);
907 obj
= devm_kzalloc(&pdev
->dev
, sizeof(*obj
) + MMU_REG_SIZE
, GFP_KERNEL
);
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
)
916 if (obj
->nr_tlb_entries
!= 32 && obj
->nr_tlb_entries
!= 8)
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
);
936 irq
= platform_get_irq(pdev
, 0);
940 err
= devm_request_irq(obj
->dev
, irq
, iommu_fault_handler
, IRQF_SHARED
,
941 dev_name(obj
->dev
), obj
);
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
);
954 iommu_device_set_ops(&obj
->iommu
, &omap_iommu_ops
);
956 err
= iommu_device_register(&obj
->iommu
);
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
);
970 iommu_device_sysfs_remove(&obj
->iommu
);
972 iommu_group_put(obj
->group
);
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
);
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
);
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
,
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
));
1022 e
->valid
= MMU_CAM_V
;
1024 e
->endian
= MMU_RAM_ENDIAN_LITTLE
;
1025 e
->elsz
= MMU_RAM_ELSZ_8
;
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
;
1041 omap_pgsz
= bytes_to_iopgsz(bytes
);
1042 if (omap_pgsz
< 0) {
1043 dev_err(dev
, "invalid size to map: %d\n", bytes
);
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
);
1053 dev_err(dev
, "omap_iopgtable_store_entry failed: %d\n", ret
);
1058 static size_t omap_iommu_unmap(struct iommu_domain
*domain
, unsigned long da
,
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
);
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
;
1078 if (!arch_data
|| !arch_data
->iommu_dev
) {
1079 dev_err(dev
, "device doesn't have an associated iommu\n");
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");
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
);
1097 dev_err(dev
, "can't get omap iommu: %d\n", ret
);
1101 omap_domain
->iommu_dev
= oiommu
;
1102 omap_domain
->dev
= dev
;
1103 oiommu
->domain
= domain
;
1106 spin_unlock(&omap_domain
->lock
);
1110 static void _omap_iommu_detach_dev(struct omap_iommu_domain
*omap_domain
,
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");
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
,
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
)
1147 omap_domain
= kzalloc(sizeof(*omap_domain
), GFP_KERNEL
);
1151 omap_domain
->pgtable
= kzalloc(IOPGD_TABLE_SIZE
, GFP_KERNEL
);
1152 if (!omap_domain
->pgtable
)
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
)))
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
;
1172 kfree(omap_domain
->pgtable
);
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
);
1194 static phys_addr_t
omap_iommu_iova_to_phys(struct iommu_domain
*domain
,
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
;
1201 phys_addr_t ret
= 0;
1203 iopgtable_lookup_entry(oiommu
, da
, &pgd
, &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
);
1211 dev_err(dev
, "bogus pte 0x%x, da 0x%llx", *pte
,
1212 (unsigned long long)da
);
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
);
1219 dev_err(dev
, "bogus pgd 0x%x, da 0x%llx", *pgd
,
1220 (unsigned long long)da
);
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
;
1236 * Allocate the archdata iommu structure for DT-based devices.
1238 * TODO: Simplify this when removing non-DT support completely from the
1244 np
= of_parse_phandle(dev
->of_node
, "iommus", 0);
1248 pdev
= of_find_device_by_node(np
);
1249 if (WARN_ON(!pdev
)) {
1254 oiommu
= platform_get_drvdata(pdev
);
1260 arch_data
= kzalloc(sizeof(*arch_data
), GFP_KERNEL
);
1266 ret
= iommu_device_link(&oiommu
->iommu
, dev
);
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
;
1285 return PTR_ERR(group
);
1287 iommu_group_put(group
);
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
)
1301 iommu_device_unlink(&arch_data
->iommu_dev
->iommu
, dev
);
1302 iommu_group_remove_device(dev
);
1304 dev
->archdata
.iommu
= NULL
;
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
;
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
;
1343 np
= of_find_matching_node(NULL
, omap_iommu_of_match
);
1349 p
= kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE
, align
, flags
,
1355 omap_iommu_debugfs_init();
1357 ret
= platform_driver_register(&omap_iommu_driver
);
1359 pr_err("%s: failed to register driver\n", __func__
);
1363 ret
= bus_set_iommu(&platform_bus_type
, &omap_iommu_ops
);
1370 platform_driver_unregister(&omap_iommu_driver
);
1372 kmem_cache_destroy(iopte_cachep
);
1375 subsys_initcall(omap_iommu_init
);
1376 /* must be ready before omap3isp is probed */