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/module.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>
31 #include <asm/cacheflush.h>
33 #include <linux/platform_data/iommu-omap.h>
35 #include "omap-iopgtable.h"
36 #include "omap-iommu.h"
38 #define to_iommu(dev) \
39 ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
41 #define for_each_iotlb_cr(obj, n, __i, cr) \
43 (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \
46 /* bitmap of the page sizes currently supported */
47 #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
50 * struct omap_iommu_domain - omap iommu domain
51 * @pgtable: the page table
52 * @iommu_dev: an omap iommu device attached to this domain. only a single
53 * iommu device can be attached for now.
54 * @dev: Device using this domain.
55 * @lock: domain lock, should be taken when attaching/detaching
57 struct omap_iommu_domain
{
59 struct omap_iommu
*iommu_dev
;
64 #define MMU_LOCK_BASE_SHIFT 10
65 #define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
66 #define MMU_LOCK_BASE(x) \
67 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
69 #define MMU_LOCK_VICT_SHIFT 4
70 #define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
71 #define MMU_LOCK_VICT(x) \
72 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
79 static struct platform_driver omap_iommu_driver
;
80 static struct kmem_cache
*iopte_cachep
;
83 * omap_iommu_save_ctx - Save registers for pm off-mode support
86 void omap_iommu_save_ctx(struct device
*dev
)
88 struct omap_iommu
*obj
= dev_to_omap_iommu(dev
);
92 for (i
= 0; i
< (MMU_REG_SIZE
/ sizeof(u32
)); i
++) {
93 p
[i
] = iommu_read_reg(obj
, i
* sizeof(u32
));
94 dev_dbg(obj
->dev
, "%s\t[%02d] %08x\n", __func__
, i
, p
[i
]);
97 EXPORT_SYMBOL_GPL(omap_iommu_save_ctx
);
100 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
101 * @dev: client device
103 void omap_iommu_restore_ctx(struct device
*dev
)
105 struct omap_iommu
*obj
= dev_to_omap_iommu(dev
);
109 for (i
= 0; i
< (MMU_REG_SIZE
/ sizeof(u32
)); i
++) {
110 iommu_write_reg(obj
, p
[i
], i
* sizeof(u32
));
111 dev_dbg(obj
->dev
, "%s\t[%02d] %08x\n", __func__
, i
, p
[i
]);
114 EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx
);
116 static void __iommu_set_twl(struct omap_iommu
*obj
, bool on
)
118 u32 l
= iommu_read_reg(obj
, MMU_CNTL
);
121 iommu_write_reg(obj
, MMU_IRQ_TWL_MASK
, MMU_IRQENABLE
);
123 iommu_write_reg(obj
, MMU_IRQ_TLB_MISS_MASK
, MMU_IRQENABLE
);
127 l
|= (MMU_CNTL_MMU_EN
| MMU_CNTL_TWL_EN
);
129 l
|= (MMU_CNTL_MMU_EN
);
131 iommu_write_reg(obj
, l
, MMU_CNTL
);
134 static int omap2_iommu_enable(struct omap_iommu
*obj
)
138 if (!obj
->iopgd
|| !IS_ALIGNED((u32
)obj
->iopgd
, SZ_16K
))
141 pa
= virt_to_phys(obj
->iopgd
);
142 if (!IS_ALIGNED(pa
, SZ_16K
))
145 l
= iommu_read_reg(obj
, MMU_REVISION
);
146 dev_info(obj
->dev
, "%s: version %d.%d\n", obj
->name
,
147 (l
>> 4) & 0xf, l
& 0xf);
149 iommu_write_reg(obj
, pa
, MMU_TTB
);
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
);
166 dev_dbg(obj
->dev
, "%s is shutting down\n", obj
->name
);
169 static int iommu_enable(struct omap_iommu
*obj
)
172 struct platform_device
*pdev
= to_platform_device(obj
->dev
);
173 struct iommu_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
175 if (pdata
&& pdata
->deassert_reset
) {
176 err
= pdata
->deassert_reset(pdev
, pdata
->reset_name
);
178 dev_err(obj
->dev
, "deassert_reset failed: %d\n", err
);
183 pm_runtime_get_sync(obj
->dev
);
185 err
= omap2_iommu_enable(obj
);
190 static void iommu_disable(struct omap_iommu
*obj
)
192 struct platform_device
*pdev
= to_platform_device(obj
->dev
);
193 struct iommu_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
195 omap2_iommu_disable(obj
);
197 pm_runtime_put_sync(obj
->dev
);
199 if (pdata
&& pdata
->assert_reset
)
200 pdata
->assert_reset(pdev
, pdata
->reset_name
);
206 static inline int iotlb_cr_valid(struct cr_regs
*cr
)
211 return cr
->cam
& MMU_CAM_V
;
214 static u32
iotlb_cr_to_virt(struct cr_regs
*cr
)
216 u32 page_size
= cr
->cam
& MMU_CAM_PGSZ_MASK
;
217 u32 mask
= get_cam_va_mask(cr
->cam
& page_size
);
219 return cr
->cam
& mask
;
222 static u32
get_iopte_attr(struct iotlb_entry
*e
)
226 attr
= e
->mixed
<< 5;
228 attr
|= e
->elsz
>> 3;
229 attr
<<= (((e
->pgsz
== MMU_CAM_PGSZ_4K
) ||
230 (e
->pgsz
== MMU_CAM_PGSZ_64K
)) ? 0 : 6);
234 static u32
iommu_report_fault(struct omap_iommu
*obj
, u32
*da
)
236 u32 status
, fault_addr
;
238 status
= iommu_read_reg(obj
, MMU_IRQSTATUS
);
239 status
&= MMU_IRQ_MASK
;
245 fault_addr
= iommu_read_reg(obj
, MMU_FAULT_AD
);
248 iommu_write_reg(obj
, status
, MMU_IRQSTATUS
);
253 static void iotlb_lock_get(struct omap_iommu
*obj
, struct iotlb_lock
*l
)
257 val
= iommu_read_reg(obj
, MMU_LOCK
);
259 l
->base
= MMU_LOCK_BASE(val
);
260 l
->vict
= MMU_LOCK_VICT(val
);
264 static void iotlb_lock_set(struct omap_iommu
*obj
, struct iotlb_lock
*l
)
268 val
= (l
->base
<< MMU_LOCK_BASE_SHIFT
);
269 val
|= (l
->vict
<< MMU_LOCK_VICT_SHIFT
);
271 iommu_write_reg(obj
, val
, MMU_LOCK
);
274 static void iotlb_read_cr(struct omap_iommu
*obj
, struct cr_regs
*cr
)
276 cr
->cam
= iommu_read_reg(obj
, MMU_READ_CAM
);
277 cr
->ram
= iommu_read_reg(obj
, MMU_READ_RAM
);
280 static void iotlb_load_cr(struct omap_iommu
*obj
, struct cr_regs
*cr
)
282 iommu_write_reg(obj
, cr
->cam
| MMU_CAM_V
, MMU_CAM
);
283 iommu_write_reg(obj
, cr
->ram
, MMU_RAM
);
285 iommu_write_reg(obj
, 1, MMU_FLUSH_ENTRY
);
286 iommu_write_reg(obj
, 1, MMU_LD_TLB
);
289 /* only used in iotlb iteration for-loop */
290 static struct cr_regs
__iotlb_read_cr(struct omap_iommu
*obj
, int n
)
295 iotlb_lock_get(obj
, &l
);
297 iotlb_lock_set(obj
, &l
);
298 iotlb_read_cr(obj
, &cr
);
303 #ifdef PREFETCH_IOTLB
304 static struct cr_regs
*iotlb_alloc_cr(struct omap_iommu
*obj
,
305 struct iotlb_entry
*e
)
312 if (e
->da
& ~(get_cam_va_mask(e
->pgsz
))) {
313 dev_err(obj
->dev
, "%s:\twrong alignment: %08x\n", __func__
,
315 return ERR_PTR(-EINVAL
);
318 cr
= kmalloc(sizeof(*cr
), GFP_KERNEL
);
320 return ERR_PTR(-ENOMEM
);
322 cr
->cam
= (e
->da
& MMU_CAM_VATAG_MASK
) | e
->prsvd
| e
->pgsz
| e
->valid
;
323 cr
->ram
= e
->pa
| e
->endian
| e
->elsz
| e
->mixed
;
329 * load_iotlb_entry - Set an iommu tlb entry
331 * @e: an iommu tlb entry info
333 static int load_iotlb_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
339 if (!obj
|| !obj
->nr_tlb_entries
|| !e
)
342 pm_runtime_get_sync(obj
->dev
);
344 iotlb_lock_get(obj
, &l
);
345 if (l
.base
== obj
->nr_tlb_entries
) {
346 dev_warn(obj
->dev
, "%s: preserve entries full\n", __func__
);
354 for_each_iotlb_cr(obj
, obj
->nr_tlb_entries
, i
, tmp
)
355 if (!iotlb_cr_valid(&tmp
))
358 if (i
== obj
->nr_tlb_entries
) {
359 dev_dbg(obj
->dev
, "%s: full: no entry\n", __func__
);
364 iotlb_lock_get(obj
, &l
);
367 iotlb_lock_set(obj
, &l
);
370 cr
= iotlb_alloc_cr(obj
, e
);
372 pm_runtime_put_sync(obj
->dev
);
376 iotlb_load_cr(obj
, cr
);
381 /* increment victim for next tlb load */
382 if (++l
.vict
== obj
->nr_tlb_entries
)
384 iotlb_lock_set(obj
, &l
);
386 pm_runtime_put_sync(obj
->dev
);
390 #else /* !PREFETCH_IOTLB */
392 static int load_iotlb_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
397 #endif /* !PREFETCH_IOTLB */
399 static int prefetch_iotlb_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
401 return load_iotlb_entry(obj
, e
);
405 * flush_iotlb_page - Clear an iommu tlb entry
407 * @da: iommu device virtual address
409 * Clear an iommu tlb entry which includes 'da' address.
411 static void flush_iotlb_page(struct omap_iommu
*obj
, u32 da
)
416 pm_runtime_get_sync(obj
->dev
);
418 for_each_iotlb_cr(obj
, obj
->nr_tlb_entries
, i
, cr
) {
422 if (!iotlb_cr_valid(&cr
))
425 start
= iotlb_cr_to_virt(&cr
);
426 bytes
= iopgsz_to_bytes(cr
.cam
& 3);
428 if ((start
<= da
) && (da
< start
+ bytes
)) {
429 dev_dbg(obj
->dev
, "%s: %08x<=%08x(%x)\n",
430 __func__
, start
, da
, bytes
);
431 iotlb_load_cr(obj
, &cr
);
432 iommu_write_reg(obj
, 1, MMU_FLUSH_ENTRY
);
436 pm_runtime_put_sync(obj
->dev
);
438 if (i
== obj
->nr_tlb_entries
)
439 dev_dbg(obj
->dev
, "%s: no page for %08x\n", __func__
, da
);
443 * flush_iotlb_all - Clear all iommu tlb entries
446 static void flush_iotlb_all(struct omap_iommu
*obj
)
450 pm_runtime_get_sync(obj
->dev
);
454 iotlb_lock_set(obj
, &l
);
456 iommu_write_reg(obj
, 1, MMU_GFLUSH
);
458 pm_runtime_put_sync(obj
->dev
);
461 #ifdef CONFIG_OMAP_IOMMU_DEBUG
463 #define pr_reg(name) \
466 const char *str = "%20s: %08x\n"; \
467 const int maxcol = 32; \
468 bytes = snprintf(p, maxcol, str, __stringify(name), \
469 iommu_read_reg(obj, MMU_##name)); \
477 omap2_iommu_dump_ctx(struct omap_iommu
*obj
, char *buf
, ssize_t len
)
496 pr_reg(EMU_FAULT_AD
);
501 ssize_t
omap_iommu_dump_ctx(struct omap_iommu
*obj
, char *buf
, ssize_t bytes
)
506 pm_runtime_get_sync(obj
->dev
);
508 bytes
= omap2_iommu_dump_ctx(obj
, buf
, bytes
);
510 pm_runtime_put_sync(obj
->dev
);
516 __dump_tlb_entries(struct omap_iommu
*obj
, struct cr_regs
*crs
, int num
)
519 struct iotlb_lock saved
;
521 struct cr_regs
*p
= crs
;
523 pm_runtime_get_sync(obj
->dev
);
524 iotlb_lock_get(obj
, &saved
);
526 for_each_iotlb_cr(obj
, num
, i
, tmp
) {
527 if (!iotlb_cr_valid(&tmp
))
532 iotlb_lock_set(obj
, &saved
);
533 pm_runtime_put_sync(obj
->dev
);
539 * iotlb_dump_cr - Dump an iommu tlb entry into buf
541 * @cr: contents of cam and ram register
542 * @buf: output buffer
544 static ssize_t
iotlb_dump_cr(struct omap_iommu
*obj
, struct cr_regs
*cr
,
549 /* FIXME: Need more detail analysis of cam/ram */
550 p
+= sprintf(p
, "%08x %08x %01x\n", cr
->cam
, cr
->ram
,
551 (cr
->cam
& MMU_CAM_P
) ? 1 : 0);
557 * omap_dump_tlb_entries - dump cr arrays to given buffer
559 * @buf: output buffer
561 size_t omap_dump_tlb_entries(struct omap_iommu
*obj
, char *buf
, ssize_t bytes
)
567 num
= bytes
/ sizeof(*cr
);
568 num
= min(obj
->nr_tlb_entries
, num
);
570 cr
= kcalloc(num
, sizeof(*cr
), GFP_KERNEL
);
574 num
= __dump_tlb_entries(obj
, cr
, num
);
575 for (i
= 0; i
< num
; i
++)
576 p
+= iotlb_dump_cr(obj
, cr
+ i
, p
);
582 #endif /* CONFIG_OMAP_IOMMU_DEBUG */
585 * H/W pagetable operations
587 static void flush_iopgd_range(u32
*first
, u32
*last
)
589 /* FIXME: L2 cache should be taken care of if it exists */
591 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
593 first
+= L1_CACHE_BYTES
/ sizeof(*first
);
594 } while (first
<= last
);
597 static void flush_iopte_range(u32
*first
, u32
*last
)
599 /* FIXME: L2 cache should be taken care of if it exists */
601 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
603 first
+= L1_CACHE_BYTES
/ sizeof(*first
);
604 } while (first
<= last
);
607 static void iopte_free(u32
*iopte
)
609 /* Note: freed iopte's must be clean ready for re-use */
611 kmem_cache_free(iopte_cachep
, iopte
);
614 static u32
*iopte_alloc(struct omap_iommu
*obj
, u32
*iopgd
, u32 da
)
618 /* a table has already existed */
623 * do the allocation outside the page table lock
625 spin_unlock(&obj
->page_table_lock
);
626 iopte
= kmem_cache_zalloc(iopte_cachep
, GFP_KERNEL
);
627 spin_lock(&obj
->page_table_lock
);
631 return ERR_PTR(-ENOMEM
);
633 *iopgd
= virt_to_phys(iopte
) | IOPGD_TABLE
;
634 flush_iopgd_range(iopgd
, iopgd
);
636 dev_vdbg(obj
->dev
, "%s: a new pte:%p\n", __func__
, iopte
);
638 /* We raced, free the reduniovant table */
643 iopte
= iopte_offset(iopgd
, da
);
646 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
647 __func__
, da
, iopgd
, *iopgd
, iopte
, *iopte
);
652 static int iopgd_alloc_section(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
654 u32
*iopgd
= iopgd_offset(obj
, da
);
656 if ((da
| pa
) & ~IOSECTION_MASK
) {
657 dev_err(obj
->dev
, "%s: %08x:%08x should aligned on %08lx\n",
658 __func__
, da
, pa
, IOSECTION_SIZE
);
662 *iopgd
= (pa
& IOSECTION_MASK
) | prot
| IOPGD_SECTION
;
663 flush_iopgd_range(iopgd
, iopgd
);
667 static int iopgd_alloc_super(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
669 u32
*iopgd
= iopgd_offset(obj
, da
);
672 if ((da
| pa
) & ~IOSUPER_MASK
) {
673 dev_err(obj
->dev
, "%s: %08x:%08x should aligned on %08lx\n",
674 __func__
, da
, pa
, IOSUPER_SIZE
);
678 for (i
= 0; i
< 16; i
++)
679 *(iopgd
+ i
) = (pa
& IOSUPER_MASK
) | prot
| IOPGD_SUPER
;
680 flush_iopgd_range(iopgd
, iopgd
+ 15);
684 static int iopte_alloc_page(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
686 u32
*iopgd
= iopgd_offset(obj
, da
);
687 u32
*iopte
= iopte_alloc(obj
, iopgd
, da
);
690 return PTR_ERR(iopte
);
692 *iopte
= (pa
& IOPAGE_MASK
) | prot
| IOPTE_SMALL
;
693 flush_iopte_range(iopte
, iopte
);
695 dev_vdbg(obj
->dev
, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
696 __func__
, da
, pa
, iopte
, *iopte
);
701 static int iopte_alloc_large(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
703 u32
*iopgd
= iopgd_offset(obj
, da
);
704 u32
*iopte
= iopte_alloc(obj
, iopgd
, da
);
707 if ((da
| pa
) & ~IOLARGE_MASK
) {
708 dev_err(obj
->dev
, "%s: %08x:%08x should aligned on %08lx\n",
709 __func__
, da
, pa
, IOLARGE_SIZE
);
714 return PTR_ERR(iopte
);
716 for (i
= 0; i
< 16; i
++)
717 *(iopte
+ i
) = (pa
& IOLARGE_MASK
) | prot
| IOPTE_LARGE
;
718 flush_iopte_range(iopte
, iopte
+ 15);
723 iopgtable_store_entry_core(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
725 int (*fn
)(struct omap_iommu
*, u32
, u32
, u32
);
733 case MMU_CAM_PGSZ_16M
:
734 fn
= iopgd_alloc_super
;
736 case MMU_CAM_PGSZ_1M
:
737 fn
= iopgd_alloc_section
;
739 case MMU_CAM_PGSZ_64K
:
740 fn
= iopte_alloc_large
;
742 case MMU_CAM_PGSZ_4K
:
743 fn
= iopte_alloc_page
;
751 prot
= get_iopte_attr(e
);
753 spin_lock(&obj
->page_table_lock
);
754 err
= fn(obj
, e
->da
, e
->pa
, prot
);
755 spin_unlock(&obj
->page_table_lock
);
761 * omap_iopgtable_store_entry - Make an iommu pte entry
763 * @e: an iommu tlb entry info
766 omap_iopgtable_store_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
770 flush_iotlb_page(obj
, e
->da
);
771 err
= iopgtable_store_entry_core(obj
, e
);
773 prefetch_iotlb_entry(obj
, e
);
778 * iopgtable_lookup_entry - Lookup an iommu pte entry
780 * @da: iommu device virtual address
781 * @ppgd: iommu pgd entry pointer to be returned
782 * @ppte: iommu pte entry pointer to be returned
785 iopgtable_lookup_entry(struct omap_iommu
*obj
, u32 da
, u32
**ppgd
, u32
**ppte
)
787 u32
*iopgd
, *iopte
= NULL
;
789 iopgd
= iopgd_offset(obj
, da
);
793 if (iopgd_is_table(*iopgd
))
794 iopte
= iopte_offset(iopgd
, da
);
800 static size_t iopgtable_clear_entry_core(struct omap_iommu
*obj
, u32 da
)
803 u32
*iopgd
= iopgd_offset(obj
, da
);
809 if (iopgd_is_table(*iopgd
)) {
811 u32
*iopte
= iopte_offset(iopgd
, da
);
814 if (*iopte
& IOPTE_LARGE
) {
816 /* rewind to the 1st entry */
817 iopte
= iopte_offset(iopgd
, (da
& IOLARGE_MASK
));
820 memset(iopte
, 0, nent
* sizeof(*iopte
));
821 flush_iopte_range(iopte
, iopte
+ (nent
- 1) * sizeof(*iopte
));
824 * do table walk to check if this table is necessary or not
826 iopte
= iopte_offset(iopgd
, 0);
827 for (i
= 0; i
< PTRS_PER_IOPTE
; i
++)
832 nent
= 1; /* for the next L1 entry */
835 if ((*iopgd
& IOPGD_SUPER
) == IOPGD_SUPER
) {
837 /* rewind to the 1st entry */
838 iopgd
= iopgd_offset(obj
, (da
& IOSUPER_MASK
));
842 memset(iopgd
, 0, nent
* sizeof(*iopgd
));
843 flush_iopgd_range(iopgd
, iopgd
+ (nent
- 1) * sizeof(*iopgd
));
849 * iopgtable_clear_entry - Remove an iommu pte entry
851 * @da: iommu device virtual address
853 static size_t iopgtable_clear_entry(struct omap_iommu
*obj
, u32 da
)
857 spin_lock(&obj
->page_table_lock
);
859 bytes
= iopgtable_clear_entry_core(obj
, da
);
860 flush_iotlb_page(obj
, da
);
862 spin_unlock(&obj
->page_table_lock
);
867 static void iopgtable_clear_entry_all(struct omap_iommu
*obj
)
871 spin_lock(&obj
->page_table_lock
);
873 for (i
= 0; i
< PTRS_PER_IOPGD
; i
++) {
877 da
= i
<< IOPGD_SHIFT
;
878 iopgd
= iopgd_offset(obj
, da
);
883 if (iopgd_is_table(*iopgd
))
884 iopte_free(iopte_offset(iopgd
, 0));
887 flush_iopgd_range(iopgd
, iopgd
);
890 flush_iotlb_all(obj
);
892 spin_unlock(&obj
->page_table_lock
);
896 * Device IOMMU generic operations
898 static irqreturn_t
iommu_fault_handler(int irq
, void *data
)
902 struct omap_iommu
*obj
= data
;
903 struct iommu_domain
*domain
= obj
->domain
;
904 struct omap_iommu_domain
*omap_domain
= domain
->priv
;
906 if (!omap_domain
->iommu_dev
)
909 errs
= iommu_report_fault(obj
, &da
);
913 /* Fault callback or TLB/PTE Dynamic loading */
914 if (!report_iommu_fault(domain
, obj
->dev
, da
, 0))
919 iopgd
= iopgd_offset(obj
, da
);
921 if (!iopgd_is_table(*iopgd
)) {
922 dev_err(obj
->dev
, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
923 obj
->name
, errs
, da
, iopgd
, *iopgd
);
927 iopte
= iopte_offset(iopgd
, da
);
929 dev_err(obj
->dev
, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
930 obj
->name
, errs
, da
, iopgd
, *iopgd
, iopte
, *iopte
);
935 static int device_match_by_alias(struct device
*dev
, void *data
)
937 struct omap_iommu
*obj
= to_iommu(dev
);
938 const char *name
= data
;
940 pr_debug("%s: %s %s\n", __func__
, obj
->name
, name
);
942 return strcmp(obj
->name
, name
) == 0;
946 * omap_iommu_attach() - attach iommu device to an iommu domain
947 * @name: name of target omap iommu device
950 static struct omap_iommu
*omap_iommu_attach(const char *name
, u32
*iopgd
)
954 struct omap_iommu
*obj
;
956 dev
= driver_find_device(&omap_iommu_driver
.driver
, NULL
,
958 device_match_by_alias
);
960 return ERR_PTR(-ENODEV
);
964 spin_lock(&obj
->iommu_lock
);
967 err
= iommu_enable(obj
);
970 flush_iotlb_all(obj
);
972 spin_unlock(&obj
->iommu_lock
);
974 dev_dbg(obj
->dev
, "%s: %s\n", __func__
, obj
->name
);
978 spin_unlock(&obj
->iommu_lock
);
983 * omap_iommu_detach - release iommu device
986 static void omap_iommu_detach(struct omap_iommu
*obj
)
988 if (!obj
|| IS_ERR(obj
))
991 spin_lock(&obj
->iommu_lock
);
996 spin_unlock(&obj
->iommu_lock
);
998 dev_dbg(obj
->dev
, "%s: %s\n", __func__
, obj
->name
);
1002 * OMAP Device MMU(IOMMU) detection
1004 static int omap_iommu_probe(struct platform_device
*pdev
)
1008 struct omap_iommu
*obj
;
1009 struct resource
*res
;
1010 struct iommu_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
1011 struct device_node
*of
= pdev
->dev
.of_node
;
1013 obj
= devm_kzalloc(&pdev
->dev
, sizeof(*obj
) + MMU_REG_SIZE
, GFP_KERNEL
);
1018 obj
->name
= dev_name(&pdev
->dev
);
1019 obj
->nr_tlb_entries
= 32;
1020 err
= of_property_read_u32(of
, "ti,#tlb-entries",
1021 &obj
->nr_tlb_entries
);
1022 if (err
&& err
!= -EINVAL
)
1024 if (obj
->nr_tlb_entries
!= 32 && obj
->nr_tlb_entries
!= 8)
1026 if (of_find_property(of
, "ti,iommu-bus-err-back", NULL
))
1027 obj
->has_bus_err_back
= MMU_GP_REG_BUS_ERR_BACK_EN
;
1029 obj
->nr_tlb_entries
= pdata
->nr_tlb_entries
;
1030 obj
->name
= pdata
->name
;
1033 obj
->dev
= &pdev
->dev
;
1034 obj
->ctx
= (void *)obj
+ sizeof(*obj
);
1036 spin_lock_init(&obj
->iommu_lock
);
1037 spin_lock_init(&obj
->page_table_lock
);
1039 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1040 obj
->regbase
= devm_ioremap_resource(obj
->dev
, res
);
1041 if (IS_ERR(obj
->regbase
))
1042 return PTR_ERR(obj
->regbase
);
1044 irq
= platform_get_irq(pdev
, 0);
1048 err
= devm_request_irq(obj
->dev
, irq
, iommu_fault_handler
, IRQF_SHARED
,
1049 dev_name(obj
->dev
), obj
);
1052 platform_set_drvdata(pdev
, obj
);
1054 pm_runtime_irq_safe(obj
->dev
);
1055 pm_runtime_enable(obj
->dev
);
1057 omap_iommu_debugfs_add(obj
);
1059 dev_info(&pdev
->dev
, "%s registered\n", obj
->name
);
1063 static int omap_iommu_remove(struct platform_device
*pdev
)
1065 struct omap_iommu
*obj
= platform_get_drvdata(pdev
);
1067 iopgtable_clear_entry_all(obj
);
1068 omap_iommu_debugfs_remove(obj
);
1070 pm_runtime_disable(obj
->dev
);
1072 dev_info(&pdev
->dev
, "%s removed\n", obj
->name
);
1076 static const struct of_device_id omap_iommu_of_match
[] = {
1077 { .compatible
= "ti,omap2-iommu" },
1078 { .compatible
= "ti,omap4-iommu" },
1079 { .compatible
= "ti,dra7-iommu" },
1082 MODULE_DEVICE_TABLE(of
, omap_iommu_of_match
);
1084 static struct platform_driver omap_iommu_driver
= {
1085 .probe
= omap_iommu_probe
,
1086 .remove
= omap_iommu_remove
,
1088 .name
= "omap-iommu",
1089 .of_match_table
= of_match_ptr(omap_iommu_of_match
),
1093 static void iopte_cachep_ctor(void *iopte
)
1095 clean_dcache_area(iopte
, IOPTE_TABLE_SIZE
);
1098 static u32
iotlb_init_entry(struct iotlb_entry
*e
, u32 da
, u32 pa
, int pgsz
)
1100 memset(e
, 0, sizeof(*e
));
1104 e
->valid
= MMU_CAM_V
;
1106 e
->endian
= MMU_RAM_ENDIAN_LITTLE
;
1107 e
->elsz
= MMU_RAM_ELSZ_8
;
1110 return iopgsz_to_bytes(e
->pgsz
);
1113 static int omap_iommu_map(struct iommu_domain
*domain
, unsigned long da
,
1114 phys_addr_t pa
, size_t bytes
, int prot
)
1116 struct omap_iommu_domain
*omap_domain
= domain
->priv
;
1117 struct omap_iommu
*oiommu
= omap_domain
->iommu_dev
;
1118 struct device
*dev
= oiommu
->dev
;
1119 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 0x%x size 0x%x\n", da
, pa
, bytes
);
1131 iotlb_init_entry(&e
, da
, pa
, omap_pgsz
);
1133 ret
= omap_iopgtable_store_entry(oiommu
, &e
);
1135 dev_err(dev
, "omap_iopgtable_store_entry failed: %d\n", ret
);
1140 static size_t omap_iommu_unmap(struct iommu_domain
*domain
, unsigned long da
,
1143 struct omap_iommu_domain
*omap_domain
= domain
->priv
;
1144 struct omap_iommu
*oiommu
= omap_domain
->iommu_dev
;
1145 struct device
*dev
= oiommu
->dev
;
1147 dev_dbg(dev
, "unmapping da 0x%lx size %u\n", da
, size
);
1149 return iopgtable_clear_entry(oiommu
, da
);
1153 omap_iommu_attach_dev(struct iommu_domain
*domain
, struct device
*dev
)
1155 struct omap_iommu_domain
*omap_domain
= domain
->priv
;
1156 struct omap_iommu
*oiommu
;
1157 struct omap_iommu_arch_data
*arch_data
= dev
->archdata
.iommu
;
1160 if (!arch_data
|| !arch_data
->name
) {
1161 dev_err(dev
, "device doesn't have an associated iommu\n");
1165 spin_lock(&omap_domain
->lock
);
1167 /* only a single device is supported per domain for now */
1168 if (omap_domain
->iommu_dev
) {
1169 dev_err(dev
, "iommu domain is already attached\n");
1174 /* get a handle to and enable the omap iommu */
1175 oiommu
= omap_iommu_attach(arch_data
->name
, omap_domain
->pgtable
);
1176 if (IS_ERR(oiommu
)) {
1177 ret
= PTR_ERR(oiommu
);
1178 dev_err(dev
, "can't get omap iommu: %d\n", ret
);
1182 omap_domain
->iommu_dev
= arch_data
->iommu_dev
= oiommu
;
1183 omap_domain
->dev
= dev
;
1184 oiommu
->domain
= domain
;
1187 spin_unlock(&omap_domain
->lock
);
1191 static void _omap_iommu_detach_dev(struct omap_iommu_domain
*omap_domain
,
1194 struct omap_iommu
*oiommu
= dev_to_omap_iommu(dev
);
1195 struct omap_iommu_arch_data
*arch_data
= dev
->archdata
.iommu
;
1197 /* only a single device is supported per domain for now */
1198 if (omap_domain
->iommu_dev
!= oiommu
) {
1199 dev_err(dev
, "invalid iommu device\n");
1203 iopgtable_clear_entry_all(oiommu
);
1205 omap_iommu_detach(oiommu
);
1207 omap_domain
->iommu_dev
= arch_data
->iommu_dev
= NULL
;
1208 omap_domain
->dev
= NULL
;
1209 oiommu
->domain
= NULL
;
1212 static void omap_iommu_detach_dev(struct iommu_domain
*domain
,
1215 struct omap_iommu_domain
*omap_domain
= domain
->priv
;
1217 spin_lock(&omap_domain
->lock
);
1218 _omap_iommu_detach_dev(omap_domain
, dev
);
1219 spin_unlock(&omap_domain
->lock
);
1222 static int omap_iommu_domain_init(struct iommu_domain
*domain
)
1224 struct omap_iommu_domain
*omap_domain
;
1226 omap_domain
= kzalloc(sizeof(*omap_domain
), GFP_KERNEL
);
1228 pr_err("kzalloc failed\n");
1232 omap_domain
->pgtable
= kzalloc(IOPGD_TABLE_SIZE
, GFP_KERNEL
);
1233 if (!omap_domain
->pgtable
) {
1234 pr_err("kzalloc failed\n");
1239 * should never fail, but please keep this around to ensure
1240 * we keep the hardware happy
1242 BUG_ON(!IS_ALIGNED((long)omap_domain
->pgtable
, IOPGD_TABLE_SIZE
));
1244 clean_dcache_area(omap_domain
->pgtable
, IOPGD_TABLE_SIZE
);
1245 spin_lock_init(&omap_domain
->lock
);
1247 domain
->priv
= omap_domain
;
1249 domain
->geometry
.aperture_start
= 0;
1250 domain
->geometry
.aperture_end
= (1ULL << 32) - 1;
1251 domain
->geometry
.force_aperture
= true;
1261 static void omap_iommu_domain_destroy(struct iommu_domain
*domain
)
1263 struct omap_iommu_domain
*omap_domain
= domain
->priv
;
1265 domain
->priv
= NULL
;
1268 * An iommu device is still attached
1269 * (currently, only one device can be attached) ?
1271 if (omap_domain
->iommu_dev
)
1272 _omap_iommu_detach_dev(omap_domain
, omap_domain
->dev
);
1274 kfree(omap_domain
->pgtable
);
1278 static phys_addr_t
omap_iommu_iova_to_phys(struct iommu_domain
*domain
,
1281 struct omap_iommu_domain
*omap_domain
= domain
->priv
;
1282 struct omap_iommu
*oiommu
= omap_domain
->iommu_dev
;
1283 struct device
*dev
= oiommu
->dev
;
1285 phys_addr_t ret
= 0;
1287 iopgtable_lookup_entry(oiommu
, da
, &pgd
, &pte
);
1290 if (iopte_is_small(*pte
))
1291 ret
= omap_iommu_translate(*pte
, da
, IOPTE_MASK
);
1292 else if (iopte_is_large(*pte
))
1293 ret
= omap_iommu_translate(*pte
, da
, IOLARGE_MASK
);
1295 dev_err(dev
, "bogus pte 0x%x, da 0x%llx", *pte
,
1296 (unsigned long long)da
);
1298 if (iopgd_is_section(*pgd
))
1299 ret
= omap_iommu_translate(*pgd
, da
, IOSECTION_MASK
);
1300 else if (iopgd_is_super(*pgd
))
1301 ret
= omap_iommu_translate(*pgd
, da
, IOSUPER_MASK
);
1303 dev_err(dev
, "bogus pgd 0x%x, da 0x%llx", *pgd
,
1304 (unsigned long long)da
);
1310 static int omap_iommu_add_device(struct device
*dev
)
1312 struct omap_iommu_arch_data
*arch_data
;
1313 struct device_node
*np
;
1314 struct platform_device
*pdev
;
1317 * Allocate the archdata iommu structure for DT-based devices.
1319 * TODO: Simplify this when removing non-DT support completely from the
1325 np
= of_parse_phandle(dev
->of_node
, "iommus", 0);
1329 pdev
= of_find_device_by_node(np
);
1330 if (WARN_ON(!pdev
)) {
1335 arch_data
= kzalloc(sizeof(*arch_data
), GFP_KERNEL
);
1341 arch_data
->name
= kstrdup(dev_name(&pdev
->dev
), GFP_KERNEL
);
1342 dev
->archdata
.iommu
= arch_data
;
1349 static void omap_iommu_remove_device(struct device
*dev
)
1351 struct omap_iommu_arch_data
*arch_data
= dev
->archdata
.iommu
;
1353 if (!dev
->of_node
|| !arch_data
)
1356 kfree(arch_data
->name
);
1360 static const struct iommu_ops omap_iommu_ops
= {
1361 .domain_init
= omap_iommu_domain_init
,
1362 .domain_destroy
= omap_iommu_domain_destroy
,
1363 .attach_dev
= omap_iommu_attach_dev
,
1364 .detach_dev
= omap_iommu_detach_dev
,
1365 .map
= omap_iommu_map
,
1366 .unmap
= omap_iommu_unmap
,
1367 .map_sg
= default_iommu_map_sg
,
1368 .iova_to_phys
= omap_iommu_iova_to_phys
,
1369 .add_device
= omap_iommu_add_device
,
1370 .remove_device
= omap_iommu_remove_device
,
1371 .pgsize_bitmap
= OMAP_IOMMU_PGSIZES
,
1374 static int __init
omap_iommu_init(void)
1376 struct kmem_cache
*p
;
1377 const unsigned long flags
= SLAB_HWCACHE_ALIGN
;
1378 size_t align
= 1 << 10; /* L2 pagetable alignement */
1380 p
= kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE
, align
, flags
,
1386 bus_set_iommu(&platform_bus_type
, &omap_iommu_ops
);
1388 omap_iommu_debugfs_init();
1390 return platform_driver_register(&omap_iommu_driver
);
1392 /* must be ready before omap3isp is probed */
1393 subsys_initcall(omap_iommu_init
);
1395 static void __exit
omap_iommu_exit(void)
1397 kmem_cache_destroy(iopte_cachep
);
1399 platform_driver_unregister(&omap_iommu_driver
);
1401 omap_iommu_debugfs_exit();
1403 module_exit(omap_iommu_exit
);
1405 MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1406 MODULE_ALIAS("platform:omap-iommu");
1407 MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1408 MODULE_LICENSE("GPL v2");