1 // SPDX-License-Identifier: GPL-2.0-only
3 * omap iommu: tlb and pagetable primitives
5 * Copyright (C) 2008-2010 Nokia Corporation
6 * Copyright (C) 2013-2017 Texas Instruments Incorporated - http://www.ti.com/
8 * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>,
9 * Paul Mundt and Toshihiro Kobayashi
12 #include <linux/dma-mapping.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
17 #include <linux/platform_device.h>
18 #include <linux/iommu.h>
19 #include <linux/omap-iommu.h>
20 #include <linux/mutex.h>
21 #include <linux/spinlock.h>
23 #include <linux/pm_runtime.h>
25 #include <linux/of_iommu.h>
26 #include <linux/of_irq.h>
27 #include <linux/of_platform.h>
28 #include <linux/regmap.h>
29 #include <linux/mfd/syscon.h>
31 #include <linux/platform_data/iommu-omap.h>
33 #include "omap-iopgtable.h"
34 #include "omap-iommu.h"
36 static const struct iommu_ops omap_iommu_ops
;
40 struct list_head node
;
43 static LIST_HEAD(orphan_dev_list
);
45 static DEFINE_SPINLOCK(orphan_lock
);
47 #define to_iommu(dev) ((struct omap_iommu *)dev_get_drvdata(dev))
49 /* bitmap of the page sizes currently supported */
50 #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
52 #define MMU_LOCK_BASE_SHIFT 10
53 #define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
54 #define MMU_LOCK_BASE(x) \
55 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
57 #define MMU_LOCK_VICT_SHIFT 4
58 #define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
59 #define MMU_LOCK_VICT(x) \
60 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
62 static struct platform_driver omap_iommu_driver
;
63 static struct kmem_cache
*iopte_cachep
;
65 static int _omap_iommu_add_device(struct device
*dev
);
68 * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
69 * @dom: generic iommu domain handle
71 static struct omap_iommu_domain
*to_omap_domain(struct iommu_domain
*dom
)
73 return container_of(dom
, struct omap_iommu_domain
, domain
);
77 * omap_iommu_save_ctx - Save registers for pm off-mode support
80 * This should be treated as an deprecated API. It is preserved only
81 * to maintain existing functionality for OMAP3 ISP driver.
83 void omap_iommu_save_ctx(struct device
*dev
)
85 struct omap_iommu_arch_data
*arch_data
= dev
->archdata
.iommu
;
86 struct omap_iommu
*obj
;
93 while (arch_data
->iommu_dev
) {
94 obj
= arch_data
->iommu_dev
;
96 for (i
= 0; i
< (MMU_REG_SIZE
/ sizeof(u32
)); i
++) {
97 p
[i
] = iommu_read_reg(obj
, i
* sizeof(u32
));
98 dev_dbg(obj
->dev
, "%s\t[%02d] %08x\n", __func__
, i
,
104 EXPORT_SYMBOL_GPL(omap_iommu_save_ctx
);
107 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
108 * @dev: client device
110 * This should be treated as an deprecated API. It is preserved only
111 * to maintain existing functionality for OMAP3 ISP driver.
113 void omap_iommu_restore_ctx(struct device
*dev
)
115 struct omap_iommu_arch_data
*arch_data
= dev
->archdata
.iommu
;
116 struct omap_iommu
*obj
;
123 while (arch_data
->iommu_dev
) {
124 obj
= arch_data
->iommu_dev
;
126 for (i
= 0; i
< (MMU_REG_SIZE
/ sizeof(u32
)); i
++) {
127 iommu_write_reg(obj
, p
[i
], i
* sizeof(u32
));
128 dev_dbg(obj
->dev
, "%s\t[%02d] %08x\n", __func__
, i
,
134 EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx
);
136 static void dra7_cfg_dspsys_mmu(struct omap_iommu
*obj
, bool enable
)
143 mask
= (1 << (obj
->id
* DSP_SYS_MMU_CONFIG_EN_SHIFT
));
144 val
= enable
? mask
: 0;
145 regmap_update_bits(obj
->syscfg
, DSP_SYS_MMU_CONFIG
, mask
, val
);
148 static void __iommu_set_twl(struct omap_iommu
*obj
, bool on
)
150 u32 l
= iommu_read_reg(obj
, MMU_CNTL
);
153 iommu_write_reg(obj
, MMU_IRQ_TWL_MASK
, MMU_IRQENABLE
);
155 iommu_write_reg(obj
, MMU_IRQ_TLB_MISS_MASK
, MMU_IRQENABLE
);
159 l
|= (MMU_CNTL_MMU_EN
| MMU_CNTL_TWL_EN
);
161 l
|= (MMU_CNTL_MMU_EN
);
163 iommu_write_reg(obj
, l
, MMU_CNTL
);
166 static int omap2_iommu_enable(struct omap_iommu
*obj
)
170 if (!obj
->iopgd
|| !IS_ALIGNED((u32
)obj
->iopgd
, SZ_16K
))
173 pa
= virt_to_phys(obj
->iopgd
);
174 if (!IS_ALIGNED(pa
, SZ_16K
))
177 l
= iommu_read_reg(obj
, MMU_REVISION
);
178 dev_info(obj
->dev
, "%s: version %d.%d\n", obj
->name
,
179 (l
>> 4) & 0xf, l
& 0xf);
181 iommu_write_reg(obj
, pa
, MMU_TTB
);
183 dra7_cfg_dspsys_mmu(obj
, true);
185 if (obj
->has_bus_err_back
)
186 iommu_write_reg(obj
, MMU_GP_REG_BUS_ERR_BACK_EN
, MMU_GP_REG
);
188 __iommu_set_twl(obj
, true);
193 static void omap2_iommu_disable(struct omap_iommu
*obj
)
195 u32 l
= iommu_read_reg(obj
, MMU_CNTL
);
198 iommu_write_reg(obj
, l
, MMU_CNTL
);
199 dra7_cfg_dspsys_mmu(obj
, false);
201 dev_dbg(obj
->dev
, "%s is shutting down\n", obj
->name
);
204 static int iommu_enable(struct omap_iommu
*obj
)
208 ret
= pm_runtime_get_sync(obj
->dev
);
210 pm_runtime_put_noidle(obj
->dev
);
212 return ret
< 0 ? ret
: 0;
215 static void iommu_disable(struct omap_iommu
*obj
)
217 pm_runtime_put_sync(obj
->dev
);
223 static u32
iotlb_cr_to_virt(struct cr_regs
*cr
)
225 u32 page_size
= cr
->cam
& MMU_CAM_PGSZ_MASK
;
226 u32 mask
= get_cam_va_mask(cr
->cam
& page_size
);
228 return cr
->cam
& mask
;
231 static u32
get_iopte_attr(struct iotlb_entry
*e
)
235 attr
= e
->mixed
<< 5;
237 attr
|= e
->elsz
>> 3;
238 attr
<<= (((e
->pgsz
== MMU_CAM_PGSZ_4K
) ||
239 (e
->pgsz
== MMU_CAM_PGSZ_64K
)) ? 0 : 6);
243 static u32
iommu_report_fault(struct omap_iommu
*obj
, u32
*da
)
245 u32 status
, fault_addr
;
247 status
= iommu_read_reg(obj
, MMU_IRQSTATUS
);
248 status
&= MMU_IRQ_MASK
;
254 fault_addr
= iommu_read_reg(obj
, MMU_FAULT_AD
);
257 iommu_write_reg(obj
, status
, MMU_IRQSTATUS
);
262 void iotlb_lock_get(struct omap_iommu
*obj
, struct iotlb_lock
*l
)
266 val
= iommu_read_reg(obj
, MMU_LOCK
);
268 l
->base
= MMU_LOCK_BASE(val
);
269 l
->vict
= MMU_LOCK_VICT(val
);
272 void iotlb_lock_set(struct omap_iommu
*obj
, struct iotlb_lock
*l
)
276 val
= (l
->base
<< MMU_LOCK_BASE_SHIFT
);
277 val
|= (l
->vict
<< MMU_LOCK_VICT_SHIFT
);
279 iommu_write_reg(obj
, val
, MMU_LOCK
);
282 static void iotlb_read_cr(struct omap_iommu
*obj
, struct cr_regs
*cr
)
284 cr
->cam
= iommu_read_reg(obj
, MMU_READ_CAM
);
285 cr
->ram
= iommu_read_reg(obj
, MMU_READ_RAM
);
288 static void iotlb_load_cr(struct omap_iommu
*obj
, struct cr_regs
*cr
)
290 iommu_write_reg(obj
, cr
->cam
| MMU_CAM_V
, MMU_CAM
);
291 iommu_write_reg(obj
, cr
->ram
, MMU_RAM
);
293 iommu_write_reg(obj
, 1, MMU_FLUSH_ENTRY
);
294 iommu_write_reg(obj
, 1, MMU_LD_TLB
);
297 /* only used in iotlb iteration for-loop */
298 struct cr_regs
__iotlb_read_cr(struct omap_iommu
*obj
, int n
)
303 iotlb_lock_get(obj
, &l
);
305 iotlb_lock_set(obj
, &l
);
306 iotlb_read_cr(obj
, &cr
);
311 #ifdef PREFETCH_IOTLB
312 static struct cr_regs
*iotlb_alloc_cr(struct omap_iommu
*obj
,
313 struct iotlb_entry
*e
)
320 if (e
->da
& ~(get_cam_va_mask(e
->pgsz
))) {
321 dev_err(obj
->dev
, "%s:\twrong alignment: %08x\n", __func__
,
323 return ERR_PTR(-EINVAL
);
326 cr
= kmalloc(sizeof(*cr
), GFP_KERNEL
);
328 return ERR_PTR(-ENOMEM
);
330 cr
->cam
= (e
->da
& MMU_CAM_VATAG_MASK
) | e
->prsvd
| e
->pgsz
| e
->valid
;
331 cr
->ram
= e
->pa
| e
->endian
| e
->elsz
| e
->mixed
;
337 * load_iotlb_entry - Set an iommu tlb entry
339 * @e: an iommu tlb entry info
341 static int load_iotlb_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
347 if (!obj
|| !obj
->nr_tlb_entries
|| !e
)
350 pm_runtime_get_sync(obj
->dev
);
352 iotlb_lock_get(obj
, &l
);
353 if (l
.base
== obj
->nr_tlb_entries
) {
354 dev_warn(obj
->dev
, "%s: preserve entries full\n", __func__
);
362 for_each_iotlb_cr(obj
, obj
->nr_tlb_entries
, i
, tmp
)
363 if (!iotlb_cr_valid(&tmp
))
366 if (i
== obj
->nr_tlb_entries
) {
367 dev_dbg(obj
->dev
, "%s: full: no entry\n", __func__
);
372 iotlb_lock_get(obj
, &l
);
375 iotlb_lock_set(obj
, &l
);
378 cr
= iotlb_alloc_cr(obj
, e
);
380 pm_runtime_put_sync(obj
->dev
);
384 iotlb_load_cr(obj
, cr
);
389 /* increment victim for next tlb load */
390 if (++l
.vict
== obj
->nr_tlb_entries
)
392 iotlb_lock_set(obj
, &l
);
394 pm_runtime_put_sync(obj
->dev
);
398 #else /* !PREFETCH_IOTLB */
400 static int load_iotlb_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
405 #endif /* !PREFETCH_IOTLB */
407 static int prefetch_iotlb_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
409 return load_iotlb_entry(obj
, e
);
413 * flush_iotlb_page - Clear an iommu tlb entry
415 * @da: iommu device virtual address
417 * Clear an iommu tlb entry which includes 'da' address.
419 static void flush_iotlb_page(struct omap_iommu
*obj
, u32 da
)
424 pm_runtime_get_sync(obj
->dev
);
426 for_each_iotlb_cr(obj
, obj
->nr_tlb_entries
, i
, cr
) {
430 if (!iotlb_cr_valid(&cr
))
433 start
= iotlb_cr_to_virt(&cr
);
434 bytes
= iopgsz_to_bytes(cr
.cam
& 3);
436 if ((start
<= da
) && (da
< start
+ bytes
)) {
437 dev_dbg(obj
->dev
, "%s: %08x<=%08x(%x)\n",
438 __func__
, start
, da
, bytes
);
439 iotlb_load_cr(obj
, &cr
);
440 iommu_write_reg(obj
, 1, MMU_FLUSH_ENTRY
);
444 pm_runtime_put_sync(obj
->dev
);
446 if (i
== obj
->nr_tlb_entries
)
447 dev_dbg(obj
->dev
, "%s: no page for %08x\n", __func__
, da
);
451 * flush_iotlb_all - Clear all iommu tlb entries
454 static void flush_iotlb_all(struct omap_iommu
*obj
)
458 pm_runtime_get_sync(obj
->dev
);
462 iotlb_lock_set(obj
, &l
);
464 iommu_write_reg(obj
, 1, MMU_GFLUSH
);
466 pm_runtime_put_sync(obj
->dev
);
470 * H/W pagetable operations
472 static void flush_iopte_range(struct device
*dev
, dma_addr_t dma
,
473 unsigned long offset
, int num_entries
)
475 size_t size
= num_entries
* sizeof(u32
);
477 dma_sync_single_range_for_device(dev
, dma
, offset
, size
, DMA_TO_DEVICE
);
480 static void iopte_free(struct omap_iommu
*obj
, u32
*iopte
, bool dma_valid
)
484 /* Note: freed iopte's must be clean ready for re-use */
487 pt_dma
= virt_to_phys(iopte
);
488 dma_unmap_single(obj
->dev
, pt_dma
, IOPTE_TABLE_SIZE
,
492 kmem_cache_free(iopte_cachep
, iopte
);
496 static u32
*iopte_alloc(struct omap_iommu
*obj
, u32
*iopgd
,
497 dma_addr_t
*pt_dma
, u32 da
)
500 unsigned long offset
= iopgd_index(da
) * sizeof(da
);
502 /* a table has already existed */
507 * do the allocation outside the page table lock
509 spin_unlock(&obj
->page_table_lock
);
510 iopte
= kmem_cache_zalloc(iopte_cachep
, GFP_KERNEL
);
511 spin_lock(&obj
->page_table_lock
);
515 return ERR_PTR(-ENOMEM
);
517 *pt_dma
= dma_map_single(obj
->dev
, iopte
, IOPTE_TABLE_SIZE
,
519 if (dma_mapping_error(obj
->dev
, *pt_dma
)) {
520 dev_err(obj
->dev
, "DMA map error for L2 table\n");
521 iopte_free(obj
, iopte
, false);
522 return ERR_PTR(-ENOMEM
);
526 * we rely on dma address and the physical address to be
527 * the same for mapping the L2 table
529 if (WARN_ON(*pt_dma
!= virt_to_phys(iopte
))) {
530 dev_err(obj
->dev
, "DMA translation error for L2 table\n");
531 dma_unmap_single(obj
->dev
, *pt_dma
, IOPTE_TABLE_SIZE
,
533 iopte_free(obj
, iopte
, false);
534 return ERR_PTR(-ENOMEM
);
537 *iopgd
= virt_to_phys(iopte
) | IOPGD_TABLE
;
539 flush_iopte_range(obj
->dev
, obj
->pd_dma
, offset
, 1);
540 dev_vdbg(obj
->dev
, "%s: a new pte:%p\n", __func__
, iopte
);
542 /* We raced, free the reduniovant table */
543 iopte_free(obj
, iopte
, false);
547 iopte
= iopte_offset(iopgd
, da
);
548 *pt_dma
= iopgd_page_paddr(iopgd
);
550 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
551 __func__
, da
, iopgd
, *iopgd
, iopte
, *iopte
);
556 static int iopgd_alloc_section(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
);
561 if ((da
| pa
) & ~IOSECTION_MASK
) {
562 dev_err(obj
->dev
, "%s: %08x:%08x should aligned on %08lx\n",
563 __func__
, da
, pa
, IOSECTION_SIZE
);
567 *iopgd
= (pa
& IOSECTION_MASK
) | prot
| IOPGD_SECTION
;
568 flush_iopte_range(obj
->dev
, obj
->pd_dma
, offset
, 1);
572 static int iopgd_alloc_super(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
574 u32
*iopgd
= iopgd_offset(obj
, da
);
575 unsigned long offset
= iopgd_index(da
) * sizeof(da
);
578 if ((da
| pa
) & ~IOSUPER_MASK
) {
579 dev_err(obj
->dev
, "%s: %08x:%08x should aligned on %08lx\n",
580 __func__
, da
, pa
, IOSUPER_SIZE
);
584 for (i
= 0; i
< 16; i
++)
585 *(iopgd
+ i
) = (pa
& IOSUPER_MASK
) | prot
| IOPGD_SUPER
;
586 flush_iopte_range(obj
->dev
, obj
->pd_dma
, offset
, 16);
590 static int iopte_alloc_page(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
592 u32
*iopgd
= iopgd_offset(obj
, da
);
594 u32
*iopte
= iopte_alloc(obj
, iopgd
, &pt_dma
, da
);
595 unsigned long offset
= iopte_index(da
) * sizeof(da
);
598 return PTR_ERR(iopte
);
600 *iopte
= (pa
& IOPAGE_MASK
) | prot
| IOPTE_SMALL
;
601 flush_iopte_range(obj
->dev
, pt_dma
, offset
, 1);
603 dev_vdbg(obj
->dev
, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
604 __func__
, da
, pa
, iopte
, *iopte
);
609 static int iopte_alloc_large(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
611 u32
*iopgd
= iopgd_offset(obj
, da
);
613 u32
*iopte
= iopte_alloc(obj
, iopgd
, &pt_dma
, da
);
614 unsigned long offset
= iopte_index(da
) * sizeof(da
);
617 if ((da
| pa
) & ~IOLARGE_MASK
) {
618 dev_err(obj
->dev
, "%s: %08x:%08x should aligned on %08lx\n",
619 __func__
, da
, pa
, IOLARGE_SIZE
);
624 return PTR_ERR(iopte
);
626 for (i
= 0; i
< 16; i
++)
627 *(iopte
+ i
) = (pa
& IOLARGE_MASK
) | prot
| IOPTE_LARGE
;
628 flush_iopte_range(obj
->dev
, pt_dma
, offset
, 16);
633 iopgtable_store_entry_core(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
635 int (*fn
)(struct omap_iommu
*, u32
, u32
, u32
);
643 case MMU_CAM_PGSZ_16M
:
644 fn
= iopgd_alloc_super
;
646 case MMU_CAM_PGSZ_1M
:
647 fn
= iopgd_alloc_section
;
649 case MMU_CAM_PGSZ_64K
:
650 fn
= iopte_alloc_large
;
652 case MMU_CAM_PGSZ_4K
:
653 fn
= iopte_alloc_page
;
663 prot
= get_iopte_attr(e
);
665 spin_lock(&obj
->page_table_lock
);
666 err
= fn(obj
, e
->da
, e
->pa
, prot
);
667 spin_unlock(&obj
->page_table_lock
);
673 * omap_iopgtable_store_entry - Make an iommu pte entry
675 * @e: an iommu tlb entry info
678 omap_iopgtable_store_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
682 flush_iotlb_page(obj
, e
->da
);
683 err
= iopgtable_store_entry_core(obj
, e
);
685 prefetch_iotlb_entry(obj
, e
);
690 * iopgtable_lookup_entry - Lookup an iommu pte entry
692 * @da: iommu device virtual address
693 * @ppgd: iommu pgd entry pointer to be returned
694 * @ppte: iommu pte entry pointer to be returned
697 iopgtable_lookup_entry(struct omap_iommu
*obj
, u32 da
, u32
**ppgd
, u32
**ppte
)
699 u32
*iopgd
, *iopte
= NULL
;
701 iopgd
= iopgd_offset(obj
, da
);
705 if (iopgd_is_table(*iopgd
))
706 iopte
= iopte_offset(iopgd
, da
);
712 static size_t iopgtable_clear_entry_core(struct omap_iommu
*obj
, u32 da
)
715 u32
*iopgd
= iopgd_offset(obj
, da
);
718 unsigned long pd_offset
= iopgd_index(da
) * sizeof(da
);
719 unsigned long pt_offset
= iopte_index(da
) * sizeof(da
);
724 if (iopgd_is_table(*iopgd
)) {
726 u32
*iopte
= iopte_offset(iopgd
, da
);
729 if (*iopte
& IOPTE_LARGE
) {
731 /* rewind to the 1st entry */
732 iopte
= iopte_offset(iopgd
, (da
& IOLARGE_MASK
));
735 memset(iopte
, 0, nent
* sizeof(*iopte
));
736 pt_dma
= iopgd_page_paddr(iopgd
);
737 flush_iopte_range(obj
->dev
, pt_dma
, pt_offset
, nent
);
740 * do table walk to check if this table is necessary or not
742 iopte
= iopte_offset(iopgd
, 0);
743 for (i
= 0; i
< PTRS_PER_IOPTE
; i
++)
747 iopte_free(obj
, iopte
, true);
748 nent
= 1; /* for the next L1 entry */
751 if ((*iopgd
& IOPGD_SUPER
) == IOPGD_SUPER
) {
753 /* rewind to the 1st entry */
754 iopgd
= iopgd_offset(obj
, (da
& IOSUPER_MASK
));
758 memset(iopgd
, 0, nent
* sizeof(*iopgd
));
759 flush_iopte_range(obj
->dev
, obj
->pd_dma
, pd_offset
, nent
);
765 * iopgtable_clear_entry - Remove an iommu pte entry
767 * @da: iommu device virtual address
769 static size_t iopgtable_clear_entry(struct omap_iommu
*obj
, u32 da
)
773 spin_lock(&obj
->page_table_lock
);
775 bytes
= iopgtable_clear_entry_core(obj
, da
);
776 flush_iotlb_page(obj
, da
);
778 spin_unlock(&obj
->page_table_lock
);
783 static void iopgtable_clear_entry_all(struct omap_iommu
*obj
)
785 unsigned long offset
;
788 spin_lock(&obj
->page_table_lock
);
790 for (i
= 0; i
< PTRS_PER_IOPGD
; i
++) {
794 da
= i
<< IOPGD_SHIFT
;
795 iopgd
= iopgd_offset(obj
, da
);
796 offset
= iopgd_index(da
) * sizeof(da
);
801 if (iopgd_is_table(*iopgd
))
802 iopte_free(obj
, iopte_offset(iopgd
, 0), true);
805 flush_iopte_range(obj
->dev
, obj
->pd_dma
, offset
, 1);
808 flush_iotlb_all(obj
);
810 spin_unlock(&obj
->page_table_lock
);
814 * Device IOMMU generic operations
816 static irqreturn_t
iommu_fault_handler(int irq
, void *data
)
820 struct omap_iommu
*obj
= data
;
821 struct iommu_domain
*domain
= obj
->domain
;
822 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
824 if (!omap_domain
->dev
)
827 errs
= iommu_report_fault(obj
, &da
);
831 /* Fault callback or TLB/PTE Dynamic loading */
832 if (!report_iommu_fault(domain
, obj
->dev
, da
, 0))
835 iommu_write_reg(obj
, 0, MMU_IRQENABLE
);
837 iopgd
= iopgd_offset(obj
, da
);
839 if (!iopgd_is_table(*iopgd
)) {
840 dev_err(obj
->dev
, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
841 obj
->name
, errs
, da
, iopgd
, *iopgd
);
845 iopte
= iopte_offset(iopgd
, da
);
847 dev_err(obj
->dev
, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
848 obj
->name
, errs
, da
, iopgd
, *iopgd
, iopte
, *iopte
);
854 * omap_iommu_attach() - attach iommu device to an iommu domain
855 * @obj: target omap iommu device
858 static int omap_iommu_attach(struct omap_iommu
*obj
, u32
*iopgd
)
862 spin_lock(&obj
->iommu_lock
);
864 obj
->pd_dma
= dma_map_single(obj
->dev
, iopgd
, IOPGD_TABLE_SIZE
,
866 if (dma_mapping_error(obj
->dev
, obj
->pd_dma
)) {
867 dev_err(obj
->dev
, "DMA map error for L1 table\n");
873 err
= iommu_enable(obj
);
876 flush_iotlb_all(obj
);
878 spin_unlock(&obj
->iommu_lock
);
880 dev_dbg(obj
->dev
, "%s: %s\n", __func__
, obj
->name
);
885 spin_unlock(&obj
->iommu_lock
);
891 * omap_iommu_detach - release iommu device
894 static void omap_iommu_detach(struct omap_iommu
*obj
)
896 if (!obj
|| IS_ERR(obj
))
899 spin_lock(&obj
->iommu_lock
);
901 dma_unmap_single(obj
->dev
, obj
->pd_dma
, IOPGD_TABLE_SIZE
,
907 spin_unlock(&obj
->iommu_lock
);
909 dev_dbg(obj
->dev
, "%s: %s\n", __func__
, obj
->name
);
912 static void omap_iommu_save_tlb_entries(struct omap_iommu
*obj
)
914 struct iotlb_lock lock
;
919 /* check if there are any locked tlbs to save */
920 iotlb_lock_get(obj
, &lock
);
921 obj
->num_cr_ctx
= lock
.base
;
922 if (!obj
->num_cr_ctx
)
926 for_each_iotlb_cr(obj
, obj
->num_cr_ctx
, i
, cr
)
930 static void omap_iommu_restore_tlb_entries(struct omap_iommu
*obj
)
936 /* no locked tlbs to restore */
937 if (!obj
->num_cr_ctx
)
942 for (i
= 0; i
< obj
->num_cr_ctx
; i
++, tmp
++) {
944 iotlb_lock_set(obj
, &l
);
945 iotlb_load_cr(obj
, tmp
);
947 l
.base
= obj
->num_cr_ctx
;
949 iotlb_lock_set(obj
, &l
);
953 * omap_iommu_domain_deactivate - deactivate attached iommu devices
954 * @domain: iommu domain attached to the target iommu device
956 * This API allows the client devices of IOMMU devices to suspend
957 * the IOMMUs they control at runtime, after they are idled and
958 * suspended all activity. System Suspend will leverage the PM
959 * driver late callbacks.
961 int omap_iommu_domain_deactivate(struct iommu_domain
*domain
)
963 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
964 struct omap_iommu_device
*iommu
;
965 struct omap_iommu
*oiommu
;
968 if (!omap_domain
->dev
)
971 iommu
= omap_domain
->iommus
;
972 iommu
+= (omap_domain
->num_iommus
- 1);
973 for (i
= 0; i
< omap_domain
->num_iommus
; i
++, iommu
--) {
974 oiommu
= iommu
->iommu_dev
;
975 pm_runtime_put_sync(oiommu
->dev
);
980 EXPORT_SYMBOL_GPL(omap_iommu_domain_deactivate
);
983 * omap_iommu_domain_activate - activate attached iommu devices
984 * @domain: iommu domain attached to the target iommu device
986 * This API allows the client devices of IOMMU devices to resume the
987 * IOMMUs they control at runtime, before they can resume operations.
988 * System Resume will leverage the PM driver late callbacks.
990 int omap_iommu_domain_activate(struct iommu_domain
*domain
)
992 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
993 struct omap_iommu_device
*iommu
;
994 struct omap_iommu
*oiommu
;
997 if (!omap_domain
->dev
)
1000 iommu
= omap_domain
->iommus
;
1001 for (i
= 0; i
< omap_domain
->num_iommus
; i
++, iommu
++) {
1002 oiommu
= iommu
->iommu_dev
;
1003 pm_runtime_get_sync(oiommu
->dev
);
1008 EXPORT_SYMBOL_GPL(omap_iommu_domain_activate
);
1011 * omap_iommu_runtime_suspend - disable an iommu device
1012 * @dev: iommu device
1014 * This function performs all that is necessary to disable an
1015 * IOMMU device, either during final detachment from a client
1016 * device, or during system/runtime suspend of the device. This
1017 * includes programming all the appropriate IOMMU registers, and
1018 * managing the associated omap_hwmod's state and the device's
1019 * reset line. This function also saves the context of any
1020 * locked TLBs if suspending.
1022 static __maybe_unused
int omap_iommu_runtime_suspend(struct device
*dev
)
1024 struct platform_device
*pdev
= to_platform_device(dev
);
1025 struct iommu_platform_data
*pdata
= dev_get_platdata(dev
);
1026 struct omap_iommu
*obj
= to_iommu(dev
);
1029 /* save the TLBs only during suspend, and not for power down */
1030 if (obj
->domain
&& obj
->iopgd
)
1031 omap_iommu_save_tlb_entries(obj
);
1033 omap2_iommu_disable(obj
);
1035 if (pdata
&& pdata
->device_idle
)
1036 pdata
->device_idle(pdev
);
1038 if (pdata
&& pdata
->assert_reset
)
1039 pdata
->assert_reset(pdev
, pdata
->reset_name
);
1041 if (pdata
&& pdata
->set_pwrdm_constraint
) {
1042 ret
= pdata
->set_pwrdm_constraint(pdev
, false, &obj
->pwrst
);
1044 dev_warn(obj
->dev
, "pwrdm_constraint failed to be reset, status = %d\n",
1053 * omap_iommu_runtime_resume - enable an iommu device
1054 * @dev: iommu device
1056 * This function performs all that is necessary to enable an
1057 * IOMMU device, either during initial attachment to a client
1058 * device, or during system/runtime resume of the device. This
1059 * includes programming all the appropriate IOMMU registers, and
1060 * managing the associated omap_hwmod's state and the device's
1061 * reset line. The function also restores any locked TLBs if
1062 * resuming after a suspend.
1064 static __maybe_unused
int omap_iommu_runtime_resume(struct device
*dev
)
1066 struct platform_device
*pdev
= to_platform_device(dev
);
1067 struct iommu_platform_data
*pdata
= dev_get_platdata(dev
);
1068 struct omap_iommu
*obj
= to_iommu(dev
);
1071 if (pdata
&& pdata
->set_pwrdm_constraint
) {
1072 ret
= pdata
->set_pwrdm_constraint(pdev
, true, &obj
->pwrst
);
1074 dev_warn(obj
->dev
, "pwrdm_constraint failed to be set, status = %d\n",
1079 if (pdata
&& pdata
->deassert_reset
) {
1080 ret
= pdata
->deassert_reset(pdev
, pdata
->reset_name
);
1082 dev_err(dev
, "deassert_reset failed: %d\n", ret
);
1087 if (pdata
&& pdata
->device_enable
)
1088 pdata
->device_enable(pdev
);
1090 /* restore the TLBs only during resume, and not for power up */
1092 omap_iommu_restore_tlb_entries(obj
);
1094 ret
= omap2_iommu_enable(obj
);
1100 * omap_iommu_suspend_prepare - prepare() dev_pm_ops implementation
1101 * @dev: iommu device
1103 * This function performs the necessary checks to determine if the IOMMU
1104 * device needs suspending or not. The function checks if the runtime_pm
1105 * status of the device is suspended, and returns 1 in that case. This
1106 * results in the PM core to skip invoking any of the Sleep PM callbacks
1107 * (suspend, suspend_late, resume, resume_early etc).
1109 static int omap_iommu_prepare(struct device
*dev
)
1111 if (pm_runtime_status_suspended(dev
))
1116 static bool omap_iommu_can_register(struct platform_device
*pdev
)
1118 struct device_node
*np
= pdev
->dev
.of_node
;
1120 if (!of_device_is_compatible(np
, "ti,dra7-dsp-iommu"))
1124 * restrict IOMMU core registration only for processor-port MDMA MMUs
1127 if ((!strcmp(dev_name(&pdev
->dev
), "40d01000.mmu")) ||
1128 (!strcmp(dev_name(&pdev
->dev
), "41501000.mmu")))
1134 static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device
*pdev
,
1135 struct omap_iommu
*obj
)
1137 struct device_node
*np
= pdev
->dev
.of_node
;
1140 if (!of_device_is_compatible(np
, "ti,dra7-dsp-iommu"))
1143 if (!of_property_read_bool(np
, "ti,syscon-mmuconfig")) {
1144 dev_err(&pdev
->dev
, "ti,syscon-mmuconfig property is missing\n");
1149 syscon_regmap_lookup_by_phandle(np
, "ti,syscon-mmuconfig");
1150 if (IS_ERR(obj
->syscfg
)) {
1151 /* can fail with -EPROBE_DEFER */
1152 ret
= PTR_ERR(obj
->syscfg
);
1156 if (of_property_read_u32_index(np
, "ti,syscon-mmuconfig", 1,
1158 dev_err(&pdev
->dev
, "couldn't get the IOMMU instance id within subsystem\n");
1162 if (obj
->id
!= 0 && obj
->id
!= 1) {
1163 dev_err(&pdev
->dev
, "invalid IOMMU instance id\n");
1171 * OMAP Device MMU(IOMMU) detection
1173 static int omap_iommu_probe(struct platform_device
*pdev
)
1177 struct omap_iommu
*obj
;
1178 struct resource
*res
;
1179 struct device_node
*of
= pdev
->dev
.of_node
;
1180 struct orphan_dev
*orphan_dev
, *tmp
;
1183 pr_err("%s: only DT-based devices are supported\n", __func__
);
1187 obj
= devm_kzalloc(&pdev
->dev
, sizeof(*obj
) + MMU_REG_SIZE
, GFP_KERNEL
);
1192 * self-manage the ordering dependencies between omap_device_enable/idle
1193 * and omap_device_assert/deassert_hardreset API
1195 if (pdev
->dev
.pm_domain
) {
1196 dev_dbg(&pdev
->dev
, "device pm_domain is being reset\n");
1197 pdev
->dev
.pm_domain
= NULL
;
1200 obj
->name
= dev_name(&pdev
->dev
);
1201 obj
->nr_tlb_entries
= 32;
1202 err
= of_property_read_u32(of
, "ti,#tlb-entries", &obj
->nr_tlb_entries
);
1203 if (err
&& err
!= -EINVAL
)
1205 if (obj
->nr_tlb_entries
!= 32 && obj
->nr_tlb_entries
!= 8)
1207 if (of_find_property(of
, "ti,iommu-bus-err-back", NULL
))
1208 obj
->has_bus_err_back
= MMU_GP_REG_BUS_ERR_BACK_EN
;
1210 obj
->dev
= &pdev
->dev
;
1211 obj
->ctx
= (void *)obj
+ sizeof(*obj
);
1212 obj
->cr_ctx
= devm_kzalloc(&pdev
->dev
,
1213 sizeof(*obj
->cr_ctx
) * obj
->nr_tlb_entries
,
1218 spin_lock_init(&obj
->iommu_lock
);
1219 spin_lock_init(&obj
->page_table_lock
);
1221 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1222 obj
->regbase
= devm_ioremap_resource(obj
->dev
, res
);
1223 if (IS_ERR(obj
->regbase
))
1224 return PTR_ERR(obj
->regbase
);
1226 err
= omap_iommu_dra7_get_dsp_system_cfg(pdev
, obj
);
1230 irq
= platform_get_irq(pdev
, 0);
1234 err
= devm_request_irq(obj
->dev
, irq
, iommu_fault_handler
, IRQF_SHARED
,
1235 dev_name(obj
->dev
), obj
);
1238 platform_set_drvdata(pdev
, obj
);
1240 if (omap_iommu_can_register(pdev
)) {
1241 obj
->group
= iommu_group_alloc();
1242 if (IS_ERR(obj
->group
))
1243 return PTR_ERR(obj
->group
);
1245 err
= iommu_device_sysfs_add(&obj
->iommu
, obj
->dev
, NULL
,
1250 iommu_device_set_ops(&obj
->iommu
, &omap_iommu_ops
);
1252 err
= iommu_device_register(&obj
->iommu
);
1257 pm_runtime_enable(obj
->dev
);
1259 omap_iommu_debugfs_add(obj
);
1261 dev_info(&pdev
->dev
, "%s registered\n", obj
->name
);
1263 list_for_each_entry_safe(orphan_dev
, tmp
, &orphan_dev_list
, node
) {
1264 err
= _omap_iommu_add_device(orphan_dev
->dev
);
1266 list_del(&orphan_dev
->node
);
1274 iommu_device_sysfs_remove(&obj
->iommu
);
1276 iommu_group_put(obj
->group
);
1280 static int omap_iommu_remove(struct platform_device
*pdev
)
1282 struct omap_iommu
*obj
= platform_get_drvdata(pdev
);
1285 iommu_group_put(obj
->group
);
1288 iommu_device_sysfs_remove(&obj
->iommu
);
1289 iommu_device_unregister(&obj
->iommu
);
1292 omap_iommu_debugfs_remove(obj
);
1294 pm_runtime_disable(obj
->dev
);
1296 dev_info(&pdev
->dev
, "%s removed\n", obj
->name
);
1300 static const struct dev_pm_ops omap_iommu_pm_ops
= {
1301 .prepare
= omap_iommu_prepare
,
1302 SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1303 pm_runtime_force_resume
)
1304 SET_RUNTIME_PM_OPS(omap_iommu_runtime_suspend
,
1305 omap_iommu_runtime_resume
, NULL
)
1308 static const struct of_device_id omap_iommu_of_match
[] = {
1309 { .compatible
= "ti,omap2-iommu" },
1310 { .compatible
= "ti,omap4-iommu" },
1311 { .compatible
= "ti,dra7-iommu" },
1312 { .compatible
= "ti,dra7-dsp-iommu" },
1316 static struct platform_driver omap_iommu_driver
= {
1317 .probe
= omap_iommu_probe
,
1318 .remove
= omap_iommu_remove
,
1320 .name
= "omap-iommu",
1321 .pm
= &omap_iommu_pm_ops
,
1322 .of_match_table
= of_match_ptr(omap_iommu_of_match
),
1326 static u32
iotlb_init_entry(struct iotlb_entry
*e
, u32 da
, u32 pa
, int pgsz
)
1328 memset(e
, 0, sizeof(*e
));
1332 e
->valid
= MMU_CAM_V
;
1334 e
->endian
= MMU_RAM_ENDIAN_LITTLE
;
1335 e
->elsz
= MMU_RAM_ELSZ_8
;
1338 return iopgsz_to_bytes(e
->pgsz
);
1341 static int omap_iommu_map(struct iommu_domain
*domain
, unsigned long da
,
1342 phys_addr_t pa
, size_t bytes
, int prot
)
1344 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
1345 struct device
*dev
= omap_domain
->dev
;
1346 struct omap_iommu_device
*iommu
;
1347 struct omap_iommu
*oiommu
;
1348 struct iotlb_entry e
;
1353 omap_pgsz
= bytes_to_iopgsz(bytes
);
1354 if (omap_pgsz
< 0) {
1355 dev_err(dev
, "invalid size to map: %d\n", bytes
);
1359 dev_dbg(dev
, "mapping da 0x%lx to pa %pa size 0x%x\n", da
, &pa
, bytes
);
1361 iotlb_init_entry(&e
, da
, pa
, omap_pgsz
);
1363 iommu
= omap_domain
->iommus
;
1364 for (i
= 0; i
< omap_domain
->num_iommus
; i
++, iommu
++) {
1365 oiommu
= iommu
->iommu_dev
;
1366 ret
= omap_iopgtable_store_entry(oiommu
, &e
);
1368 dev_err(dev
, "omap_iopgtable_store_entry failed: %d\n",
1377 oiommu
= iommu
->iommu_dev
;
1378 iopgtable_clear_entry(oiommu
, da
);
1385 static size_t omap_iommu_unmap(struct iommu_domain
*domain
, unsigned long da
,
1386 size_t size
, struct iommu_iotlb_gather
*gather
)
1388 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
1389 struct device
*dev
= omap_domain
->dev
;
1390 struct omap_iommu_device
*iommu
;
1391 struct omap_iommu
*oiommu
;
1396 dev_dbg(dev
, "unmapping da 0x%lx size %u\n", da
, size
);
1398 iommu
= omap_domain
->iommus
;
1399 for (i
= 0; i
< omap_domain
->num_iommus
; i
++, iommu
++) {
1400 oiommu
= iommu
->iommu_dev
;
1401 bytes
= iopgtable_clear_entry(oiommu
, da
);
1407 * simplify return - we are only checking if any of the iommus
1408 * reported an error, but not if all of them are unmapping the
1409 * same number of entries. This should not occur due to the
1410 * mirror programming.
1412 return error
? 0 : bytes
;
1415 static int omap_iommu_count(struct device
*dev
)
1417 struct omap_iommu_arch_data
*arch_data
= dev
->archdata
.iommu
;
1420 while (arch_data
->iommu_dev
) {
1428 /* caller should call cleanup if this function fails */
1429 static int omap_iommu_attach_init(struct device
*dev
,
1430 struct omap_iommu_domain
*odomain
)
1432 struct omap_iommu_device
*iommu
;
1435 odomain
->num_iommus
= omap_iommu_count(dev
);
1436 if (!odomain
->num_iommus
)
1439 odomain
->iommus
= kcalloc(odomain
->num_iommus
, sizeof(*iommu
),
1441 if (!odomain
->iommus
)
1444 iommu
= odomain
->iommus
;
1445 for (i
= 0; i
< odomain
->num_iommus
; i
++, iommu
++) {
1446 iommu
->pgtable
= kzalloc(IOPGD_TABLE_SIZE
, GFP_ATOMIC
);
1447 if (!iommu
->pgtable
)
1451 * should never fail, but please keep this around to ensure
1452 * we keep the hardware happy
1454 if (WARN_ON(!IS_ALIGNED((long)iommu
->pgtable
,
1462 static void omap_iommu_detach_fini(struct omap_iommu_domain
*odomain
)
1465 struct omap_iommu_device
*iommu
= odomain
->iommus
;
1467 for (i
= 0; iommu
&& i
< odomain
->num_iommus
; i
++, iommu
++)
1468 kfree(iommu
->pgtable
);
1470 kfree(odomain
->iommus
);
1471 odomain
->num_iommus
= 0;
1472 odomain
->iommus
= NULL
;
1476 omap_iommu_attach_dev(struct iommu_domain
*domain
, struct device
*dev
)
1478 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
1479 struct omap_iommu_arch_data
*arch_data
= dev
->archdata
.iommu
;
1480 struct omap_iommu_device
*iommu
;
1481 struct omap_iommu
*oiommu
;
1485 if (!arch_data
|| !arch_data
->iommu_dev
) {
1486 dev_err(dev
, "device doesn't have an associated iommu\n");
1490 spin_lock(&omap_domain
->lock
);
1492 /* only a single client device can be attached to a domain */
1493 if (omap_domain
->dev
) {
1494 dev_err(dev
, "iommu domain is already attached\n");
1499 ret
= omap_iommu_attach_init(dev
, omap_domain
);
1501 dev_err(dev
, "failed to allocate required iommu data %d\n",
1506 iommu
= omap_domain
->iommus
;
1507 for (i
= 0; i
< omap_domain
->num_iommus
; i
++, iommu
++, arch_data
++) {
1508 /* configure and enable the omap iommu */
1509 oiommu
= arch_data
->iommu_dev
;
1510 ret
= omap_iommu_attach(oiommu
, iommu
->pgtable
);
1512 dev_err(dev
, "can't get omap iommu: %d\n", ret
);
1516 oiommu
->domain
= domain
;
1517 iommu
->iommu_dev
= oiommu
;
1520 omap_domain
->dev
= dev
;
1528 oiommu
= iommu
->iommu_dev
;
1529 omap_iommu_detach(oiommu
);
1530 iommu
->iommu_dev
= NULL
;
1531 oiommu
->domain
= NULL
;
1534 omap_iommu_detach_fini(omap_domain
);
1536 spin_unlock(&omap_domain
->lock
);
1540 static void _omap_iommu_detach_dev(struct omap_iommu_domain
*omap_domain
,
1543 struct omap_iommu_arch_data
*arch_data
= dev
->archdata
.iommu
;
1544 struct omap_iommu_device
*iommu
= omap_domain
->iommus
;
1545 struct omap_iommu
*oiommu
;
1548 if (!omap_domain
->dev
) {
1549 dev_err(dev
, "domain has no attached device\n");
1553 /* only a single device is supported per domain for now */
1554 if (omap_domain
->dev
!= dev
) {
1555 dev_err(dev
, "invalid attached device\n");
1560 * cleanup in the reverse order of attachment - this addresses
1561 * any h/w dependencies between multiple instances, if any
1563 iommu
+= (omap_domain
->num_iommus
- 1);
1564 arch_data
+= (omap_domain
->num_iommus
- 1);
1565 for (i
= 0; i
< omap_domain
->num_iommus
; i
++, iommu
--, arch_data
--) {
1566 oiommu
= iommu
->iommu_dev
;
1567 iopgtable_clear_entry_all(oiommu
);
1569 omap_iommu_detach(oiommu
);
1570 iommu
->iommu_dev
= NULL
;
1571 oiommu
->domain
= NULL
;
1574 omap_iommu_detach_fini(omap_domain
);
1576 omap_domain
->dev
= NULL
;
1579 static void omap_iommu_detach_dev(struct iommu_domain
*domain
,
1582 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
1584 spin_lock(&omap_domain
->lock
);
1585 _omap_iommu_detach_dev(omap_domain
, dev
);
1586 spin_unlock(&omap_domain
->lock
);
1589 static struct iommu_domain
*omap_iommu_domain_alloc(unsigned type
)
1591 struct omap_iommu_domain
*omap_domain
;
1593 if (type
!= IOMMU_DOMAIN_UNMANAGED
)
1596 omap_domain
= kzalloc(sizeof(*omap_domain
), GFP_KERNEL
);
1600 spin_lock_init(&omap_domain
->lock
);
1602 omap_domain
->domain
.geometry
.aperture_start
= 0;
1603 omap_domain
->domain
.geometry
.aperture_end
= (1ULL << 32) - 1;
1604 omap_domain
->domain
.geometry
.force_aperture
= true;
1606 return &omap_domain
->domain
;
1609 static void omap_iommu_domain_free(struct iommu_domain
*domain
)
1611 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
1614 * An iommu device is still attached
1615 * (currently, only one device can be attached) ?
1617 if (omap_domain
->dev
)
1618 _omap_iommu_detach_dev(omap_domain
, omap_domain
->dev
);
1623 static phys_addr_t
omap_iommu_iova_to_phys(struct iommu_domain
*domain
,
1626 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
1627 struct omap_iommu_device
*iommu
= omap_domain
->iommus
;
1628 struct omap_iommu
*oiommu
= iommu
->iommu_dev
;
1629 struct device
*dev
= oiommu
->dev
;
1631 phys_addr_t ret
= 0;
1634 * all the iommus within the domain will have identical programming,
1635 * so perform the lookup using just the first iommu
1637 iopgtable_lookup_entry(oiommu
, da
, &pgd
, &pte
);
1640 if (iopte_is_small(*pte
))
1641 ret
= omap_iommu_translate(*pte
, da
, IOPTE_MASK
);
1642 else if (iopte_is_large(*pte
))
1643 ret
= omap_iommu_translate(*pte
, da
, IOLARGE_MASK
);
1645 dev_err(dev
, "bogus pte 0x%x, da 0x%llx", *pte
,
1646 (unsigned long long)da
);
1648 if (iopgd_is_section(*pgd
))
1649 ret
= omap_iommu_translate(*pgd
, da
, IOSECTION_MASK
);
1650 else if (iopgd_is_super(*pgd
))
1651 ret
= omap_iommu_translate(*pgd
, da
, IOSUPER_MASK
);
1653 dev_err(dev
, "bogus pgd 0x%x, da 0x%llx", *pgd
,
1654 (unsigned long long)da
);
1660 static int _omap_iommu_add_device(struct device
*dev
)
1662 struct omap_iommu_arch_data
*arch_data
, *tmp
;
1663 struct omap_iommu
*oiommu
;
1664 struct iommu_group
*group
;
1665 struct device_node
*np
;
1666 struct platform_device
*pdev
;
1669 struct orphan_dev
*orphan_dev
;
1670 unsigned long flags
;
1673 * Allocate the archdata iommu structure for DT-based devices.
1675 * TODO: Simplify this when removing non-DT support completely from the
1682 * retrieve the count of IOMMU nodes using phandle size as element size
1683 * since #iommu-cells = 0 for OMAP
1685 num_iommus
= of_property_count_elems_of_size(dev
->of_node
, "iommus",
1690 arch_data
= kcalloc(num_iommus
+ 1, sizeof(*arch_data
), GFP_KERNEL
);
1694 for (i
= 0, tmp
= arch_data
; i
< num_iommus
; i
++, tmp
++) {
1695 np
= of_parse_phandle(dev
->of_node
, "iommus", i
);
1701 pdev
= of_find_device_by_node(np
);
1705 spin_lock_irqsave(&orphan_lock
, flags
);
1706 list_for_each_entry(orphan_dev
, &orphan_dev_list
,
1708 if (orphan_dev
->dev
== dev
)
1711 spin_unlock_irqrestore(&orphan_lock
, flags
);
1713 if (orphan_dev
&& orphan_dev
->dev
== dev
)
1714 return -EPROBE_DEFER
;
1716 orphan_dev
= kzalloc(sizeof(*orphan_dev
), GFP_KERNEL
);
1717 orphan_dev
->dev
= dev
;
1718 spin_lock_irqsave(&orphan_lock
, flags
);
1719 list_add(&orphan_dev
->node
, &orphan_dev_list
);
1720 spin_unlock_irqrestore(&orphan_lock
, flags
);
1721 return -EPROBE_DEFER
;
1724 oiommu
= platform_get_drvdata(pdev
);
1731 tmp
->iommu_dev
= oiommu
;
1732 tmp
->dev
= &pdev
->dev
;
1738 * use the first IOMMU alone for the sysfs device linking.
1739 * TODO: Evaluate if a single iommu_group needs to be
1740 * maintained for both IOMMUs
1742 oiommu
= arch_data
->iommu_dev
;
1743 ret
= iommu_device_link(&oiommu
->iommu
, dev
);
1749 dev
->archdata
.iommu
= arch_data
;
1752 * IOMMU group initialization calls into omap_iommu_device_group, which
1753 * needs a valid dev->archdata.iommu pointer
1755 group
= iommu_group_get_for_dev(dev
);
1756 if (IS_ERR(group
)) {
1757 iommu_device_unlink(&oiommu
->iommu
, dev
);
1758 dev
->archdata
.iommu
= NULL
;
1760 return PTR_ERR(group
);
1762 iommu_group_put(group
);
1767 static int omap_iommu_add_device(struct device
*dev
)
1771 ret
= _omap_iommu_add_device(dev
);
1772 if (ret
== -EPROBE_DEFER
)
1778 static void omap_iommu_remove_device(struct device
*dev
)
1780 struct omap_iommu_arch_data
*arch_data
= dev
->archdata
.iommu
;
1782 if (!dev
->of_node
|| !arch_data
)
1785 iommu_device_unlink(&arch_data
->iommu_dev
->iommu
, dev
);
1786 iommu_group_remove_device(dev
);
1788 dev
->archdata
.iommu
= NULL
;
1793 static struct iommu_group
*omap_iommu_device_group(struct device
*dev
)
1795 struct omap_iommu_arch_data
*arch_data
= dev
->archdata
.iommu
;
1796 struct iommu_group
*group
= ERR_PTR(-EINVAL
);
1798 if (arch_data
->iommu_dev
)
1799 group
= iommu_group_ref_get(arch_data
->iommu_dev
->group
);
1804 static const struct iommu_ops omap_iommu_ops
= {
1805 .domain_alloc
= omap_iommu_domain_alloc
,
1806 .domain_free
= omap_iommu_domain_free
,
1807 .attach_dev
= omap_iommu_attach_dev
,
1808 .detach_dev
= omap_iommu_detach_dev
,
1809 .map
= omap_iommu_map
,
1810 .unmap
= omap_iommu_unmap
,
1811 .iova_to_phys
= omap_iommu_iova_to_phys
,
1812 .add_device
= omap_iommu_add_device
,
1813 .remove_device
= omap_iommu_remove_device
,
1814 .device_group
= omap_iommu_device_group
,
1815 .pgsize_bitmap
= OMAP_IOMMU_PGSIZES
,
1818 static int __init
omap_iommu_init(void)
1820 struct kmem_cache
*p
;
1821 const slab_flags_t flags
= SLAB_HWCACHE_ALIGN
;
1822 size_t align
= 1 << 10; /* L2 pagetable alignement */
1823 struct device_node
*np
;
1826 np
= of_find_matching_node(NULL
, omap_iommu_of_match
);
1832 p
= kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE
, align
, flags
,
1838 omap_iommu_debugfs_init();
1840 ret
= platform_driver_register(&omap_iommu_driver
);
1842 pr_err("%s: failed to register driver\n", __func__
);
1846 ret
= bus_set_iommu(&platform_bus_type
, &omap_iommu_ops
);
1853 platform_driver_unregister(&omap_iommu_driver
);
1855 kmem_cache_destroy(iopte_cachep
);
1858 subsys_initcall(omap_iommu_init
);
1859 /* must be ready before omap3isp is probed */