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 #define to_iommu(dev) \
40 ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
42 /* bitmap of the page sizes currently supported */
43 #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
46 * struct omap_iommu_domain - omap iommu domain
47 * @pgtable: the page table
48 * @iommu_dev: an omap iommu device attached to this domain. only a single
49 * iommu device can be attached for now.
50 * @dev: Device using this domain.
51 * @lock: domain lock, should be taken when attaching/detaching
53 struct omap_iommu_domain
{
55 struct omap_iommu
*iommu_dev
;
58 struct iommu_domain domain
;
61 #define MMU_LOCK_BASE_SHIFT 10
62 #define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
63 #define MMU_LOCK_BASE(x) \
64 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
66 #define MMU_LOCK_VICT_SHIFT 4
67 #define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
68 #define MMU_LOCK_VICT(x) \
69 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
71 static struct platform_driver omap_iommu_driver
;
72 static struct kmem_cache
*iopte_cachep
;
75 * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
76 * @dom: generic iommu domain handle
78 static struct omap_iommu_domain
*to_omap_domain(struct iommu_domain
*dom
)
80 return container_of(dom
, struct omap_iommu_domain
, domain
);
84 * omap_iommu_save_ctx - Save registers for pm off-mode support
87 void omap_iommu_save_ctx(struct device
*dev
)
89 struct omap_iommu
*obj
= dev_to_omap_iommu(dev
);
93 for (i
= 0; i
< (MMU_REG_SIZE
/ sizeof(u32
)); i
++) {
94 p
[i
] = iommu_read_reg(obj
, i
* sizeof(u32
));
95 dev_dbg(obj
->dev
, "%s\t[%02d] %08x\n", __func__
, i
, p
[i
]);
98 EXPORT_SYMBOL_GPL(omap_iommu_save_ctx
);
101 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
102 * @dev: client device
104 void omap_iommu_restore_ctx(struct device
*dev
)
106 struct omap_iommu
*obj
= dev_to_omap_iommu(dev
);
110 for (i
= 0; i
< (MMU_REG_SIZE
/ sizeof(u32
)); i
++) {
111 iommu_write_reg(obj
, p
[i
], i
* sizeof(u32
));
112 dev_dbg(obj
->dev
, "%s\t[%02d] %08x\n", __func__
, i
, p
[i
]);
115 EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx
);
117 static void dra7_cfg_dspsys_mmu(struct omap_iommu
*obj
, bool enable
)
124 mask
= (1 << (obj
->id
* DSP_SYS_MMU_CONFIG_EN_SHIFT
));
125 val
= enable
? mask
: 0;
126 regmap_update_bits(obj
->syscfg
, DSP_SYS_MMU_CONFIG
, mask
, val
);
129 static void __iommu_set_twl(struct omap_iommu
*obj
, bool on
)
131 u32 l
= iommu_read_reg(obj
, MMU_CNTL
);
134 iommu_write_reg(obj
, MMU_IRQ_TWL_MASK
, MMU_IRQENABLE
);
136 iommu_write_reg(obj
, MMU_IRQ_TLB_MISS_MASK
, MMU_IRQENABLE
);
140 l
|= (MMU_CNTL_MMU_EN
| MMU_CNTL_TWL_EN
);
142 l
|= (MMU_CNTL_MMU_EN
);
144 iommu_write_reg(obj
, l
, MMU_CNTL
);
147 static int omap2_iommu_enable(struct omap_iommu
*obj
)
151 if (!obj
->iopgd
|| !IS_ALIGNED((u32
)obj
->iopgd
, SZ_16K
))
154 pa
= virt_to_phys(obj
->iopgd
);
155 if (!IS_ALIGNED(pa
, SZ_16K
))
158 l
= iommu_read_reg(obj
, MMU_REVISION
);
159 dev_info(obj
->dev
, "%s: version %d.%d\n", obj
->name
,
160 (l
>> 4) & 0xf, l
& 0xf);
162 iommu_write_reg(obj
, pa
, MMU_TTB
);
164 dra7_cfg_dspsys_mmu(obj
, true);
166 if (obj
->has_bus_err_back
)
167 iommu_write_reg(obj
, MMU_GP_REG_BUS_ERR_BACK_EN
, MMU_GP_REG
);
169 __iommu_set_twl(obj
, true);
174 static void omap2_iommu_disable(struct omap_iommu
*obj
)
176 u32 l
= iommu_read_reg(obj
, MMU_CNTL
);
179 iommu_write_reg(obj
, l
, MMU_CNTL
);
180 dra7_cfg_dspsys_mmu(obj
, false);
182 dev_dbg(obj
->dev
, "%s is shutting down\n", obj
->name
);
185 static int iommu_enable(struct omap_iommu
*obj
)
188 struct platform_device
*pdev
= to_platform_device(obj
->dev
);
189 struct iommu_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
191 if (pdata
&& pdata
->deassert_reset
) {
192 err
= pdata
->deassert_reset(pdev
, pdata
->reset_name
);
194 dev_err(obj
->dev
, "deassert_reset failed: %d\n", err
);
199 pm_runtime_get_sync(obj
->dev
);
201 err
= omap2_iommu_enable(obj
);
206 static void iommu_disable(struct omap_iommu
*obj
)
208 struct platform_device
*pdev
= to_platform_device(obj
->dev
);
209 struct iommu_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
211 omap2_iommu_disable(obj
);
213 pm_runtime_put_sync(obj
->dev
);
215 if (pdata
&& pdata
->assert_reset
)
216 pdata
->assert_reset(pdev
, pdata
->reset_name
);
222 static u32
iotlb_cr_to_virt(struct cr_regs
*cr
)
224 u32 page_size
= cr
->cam
& MMU_CAM_PGSZ_MASK
;
225 u32 mask
= get_cam_va_mask(cr
->cam
& page_size
);
227 return cr
->cam
& mask
;
230 static u32
get_iopte_attr(struct iotlb_entry
*e
)
234 attr
= e
->mixed
<< 5;
236 attr
|= e
->elsz
>> 3;
237 attr
<<= (((e
->pgsz
== MMU_CAM_PGSZ_4K
) ||
238 (e
->pgsz
== MMU_CAM_PGSZ_64K
)) ? 0 : 6);
242 static u32
iommu_report_fault(struct omap_iommu
*obj
, u32
*da
)
244 u32 status
, fault_addr
;
246 status
= iommu_read_reg(obj
, MMU_IRQSTATUS
);
247 status
&= MMU_IRQ_MASK
;
253 fault_addr
= iommu_read_reg(obj
, MMU_FAULT_AD
);
256 iommu_write_reg(obj
, status
, MMU_IRQSTATUS
);
261 void iotlb_lock_get(struct omap_iommu
*obj
, struct iotlb_lock
*l
)
265 val
= iommu_read_reg(obj
, MMU_LOCK
);
267 l
->base
= MMU_LOCK_BASE(val
);
268 l
->vict
= MMU_LOCK_VICT(val
);
271 void iotlb_lock_set(struct omap_iommu
*obj
, struct iotlb_lock
*l
)
275 val
= (l
->base
<< MMU_LOCK_BASE_SHIFT
);
276 val
|= (l
->vict
<< MMU_LOCK_VICT_SHIFT
);
278 iommu_write_reg(obj
, val
, MMU_LOCK
);
281 static void iotlb_read_cr(struct omap_iommu
*obj
, struct cr_regs
*cr
)
283 cr
->cam
= iommu_read_reg(obj
, MMU_READ_CAM
);
284 cr
->ram
= iommu_read_reg(obj
, MMU_READ_RAM
);
287 static void iotlb_load_cr(struct omap_iommu
*obj
, struct cr_regs
*cr
)
289 iommu_write_reg(obj
, cr
->cam
| MMU_CAM_V
, MMU_CAM
);
290 iommu_write_reg(obj
, cr
->ram
, MMU_RAM
);
292 iommu_write_reg(obj
, 1, MMU_FLUSH_ENTRY
);
293 iommu_write_reg(obj
, 1, MMU_LD_TLB
);
296 /* only used in iotlb iteration for-loop */
297 struct cr_regs
__iotlb_read_cr(struct omap_iommu
*obj
, int n
)
302 iotlb_lock_get(obj
, &l
);
304 iotlb_lock_set(obj
, &l
);
305 iotlb_read_cr(obj
, &cr
);
310 #ifdef PREFETCH_IOTLB
311 static struct cr_regs
*iotlb_alloc_cr(struct omap_iommu
*obj
,
312 struct iotlb_entry
*e
)
319 if (e
->da
& ~(get_cam_va_mask(e
->pgsz
))) {
320 dev_err(obj
->dev
, "%s:\twrong alignment: %08x\n", __func__
,
322 return ERR_PTR(-EINVAL
);
325 cr
= kmalloc(sizeof(*cr
), GFP_KERNEL
);
327 return ERR_PTR(-ENOMEM
);
329 cr
->cam
= (e
->da
& MMU_CAM_VATAG_MASK
) | e
->prsvd
| e
->pgsz
| e
->valid
;
330 cr
->ram
= e
->pa
| e
->endian
| e
->elsz
| e
->mixed
;
336 * load_iotlb_entry - Set an iommu tlb entry
338 * @e: an iommu tlb entry info
340 static int load_iotlb_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
346 if (!obj
|| !obj
->nr_tlb_entries
|| !e
)
349 pm_runtime_get_sync(obj
->dev
);
351 iotlb_lock_get(obj
, &l
);
352 if (l
.base
== obj
->nr_tlb_entries
) {
353 dev_warn(obj
->dev
, "%s: preserve entries full\n", __func__
);
361 for_each_iotlb_cr(obj
, obj
->nr_tlb_entries
, i
, tmp
)
362 if (!iotlb_cr_valid(&tmp
))
365 if (i
== obj
->nr_tlb_entries
) {
366 dev_dbg(obj
->dev
, "%s: full: no entry\n", __func__
);
371 iotlb_lock_get(obj
, &l
);
374 iotlb_lock_set(obj
, &l
);
377 cr
= iotlb_alloc_cr(obj
, e
);
379 pm_runtime_put_sync(obj
->dev
);
383 iotlb_load_cr(obj
, cr
);
388 /* increment victim for next tlb load */
389 if (++l
.vict
== obj
->nr_tlb_entries
)
391 iotlb_lock_set(obj
, &l
);
393 pm_runtime_put_sync(obj
->dev
);
397 #else /* !PREFETCH_IOTLB */
399 static int load_iotlb_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
404 #endif /* !PREFETCH_IOTLB */
406 static int prefetch_iotlb_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
408 return load_iotlb_entry(obj
, e
);
412 * flush_iotlb_page - Clear an iommu tlb entry
414 * @da: iommu device virtual address
416 * Clear an iommu tlb entry which includes 'da' address.
418 static void flush_iotlb_page(struct omap_iommu
*obj
, u32 da
)
423 pm_runtime_get_sync(obj
->dev
);
425 for_each_iotlb_cr(obj
, obj
->nr_tlb_entries
, i
, cr
) {
429 if (!iotlb_cr_valid(&cr
))
432 start
= iotlb_cr_to_virt(&cr
);
433 bytes
= iopgsz_to_bytes(cr
.cam
& 3);
435 if ((start
<= da
) && (da
< start
+ bytes
)) {
436 dev_dbg(obj
->dev
, "%s: %08x<=%08x(%x)\n",
437 __func__
, start
, da
, bytes
);
438 iotlb_load_cr(obj
, &cr
);
439 iommu_write_reg(obj
, 1, MMU_FLUSH_ENTRY
);
443 pm_runtime_put_sync(obj
->dev
);
445 if (i
== obj
->nr_tlb_entries
)
446 dev_dbg(obj
->dev
, "%s: no page for %08x\n", __func__
, da
);
450 * flush_iotlb_all - Clear all iommu tlb entries
453 static void flush_iotlb_all(struct omap_iommu
*obj
)
457 pm_runtime_get_sync(obj
->dev
);
461 iotlb_lock_set(obj
, &l
);
463 iommu_write_reg(obj
, 1, MMU_GFLUSH
);
465 pm_runtime_put_sync(obj
->dev
);
469 * H/W pagetable operations
471 static void flush_iopgd_range(u32
*first
, u32
*last
)
473 /* FIXME: L2 cache should be taken care of if it exists */
475 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
477 first
+= L1_CACHE_BYTES
/ sizeof(*first
);
478 } while (first
<= last
);
481 static void flush_iopte_range(u32
*first
, u32
*last
)
483 /* FIXME: L2 cache should be taken care of if it exists */
485 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
487 first
+= L1_CACHE_BYTES
/ sizeof(*first
);
488 } while (first
<= last
);
491 static void iopte_free(u32
*iopte
)
493 /* Note: freed iopte's must be clean ready for re-use */
495 kmem_cache_free(iopte_cachep
, iopte
);
498 static u32
*iopte_alloc(struct omap_iommu
*obj
, u32
*iopgd
, u32 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 *iopgd
= virt_to_phys(iopte
) | IOPGD_TABLE
;
518 flush_iopgd_range(iopgd
, iopgd
);
520 dev_vdbg(obj
->dev
, "%s: a new pte:%p\n", __func__
, iopte
);
522 /* We raced, free the reduniovant table */
527 iopte
= iopte_offset(iopgd
, da
);
530 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
531 __func__
, da
, iopgd
, *iopgd
, iopte
, *iopte
);
536 static int iopgd_alloc_section(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
538 u32
*iopgd
= iopgd_offset(obj
, da
);
540 if ((da
| pa
) & ~IOSECTION_MASK
) {
541 dev_err(obj
->dev
, "%s: %08x:%08x should aligned on %08lx\n",
542 __func__
, da
, pa
, IOSECTION_SIZE
);
546 *iopgd
= (pa
& IOSECTION_MASK
) | prot
| IOPGD_SECTION
;
547 flush_iopgd_range(iopgd
, iopgd
);
551 static int iopgd_alloc_super(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
553 u32
*iopgd
= iopgd_offset(obj
, da
);
556 if ((da
| pa
) & ~IOSUPER_MASK
) {
557 dev_err(obj
->dev
, "%s: %08x:%08x should aligned on %08lx\n",
558 __func__
, da
, pa
, IOSUPER_SIZE
);
562 for (i
= 0; i
< 16; i
++)
563 *(iopgd
+ i
) = (pa
& IOSUPER_MASK
) | prot
| IOPGD_SUPER
;
564 flush_iopgd_range(iopgd
, iopgd
+ 15);
568 static int iopte_alloc_page(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
570 u32
*iopgd
= iopgd_offset(obj
, da
);
571 u32
*iopte
= iopte_alloc(obj
, iopgd
, da
);
574 return PTR_ERR(iopte
);
576 *iopte
= (pa
& IOPAGE_MASK
) | prot
| IOPTE_SMALL
;
577 flush_iopte_range(iopte
, iopte
);
579 dev_vdbg(obj
->dev
, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
580 __func__
, da
, pa
, iopte
, *iopte
);
585 static int iopte_alloc_large(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
587 u32
*iopgd
= iopgd_offset(obj
, da
);
588 u32
*iopte
= iopte_alloc(obj
, iopgd
, da
);
591 if ((da
| pa
) & ~IOLARGE_MASK
) {
592 dev_err(obj
->dev
, "%s: %08x:%08x should aligned on %08lx\n",
593 __func__
, da
, pa
, IOLARGE_SIZE
);
598 return PTR_ERR(iopte
);
600 for (i
= 0; i
< 16; i
++)
601 *(iopte
+ i
) = (pa
& IOLARGE_MASK
) | prot
| IOPTE_LARGE
;
602 flush_iopte_range(iopte
, iopte
+ 15);
607 iopgtable_store_entry_core(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
609 int (*fn
)(struct omap_iommu
*, u32
, u32
, u32
);
617 case MMU_CAM_PGSZ_16M
:
618 fn
= iopgd_alloc_super
;
620 case MMU_CAM_PGSZ_1M
:
621 fn
= iopgd_alloc_section
;
623 case MMU_CAM_PGSZ_64K
:
624 fn
= iopte_alloc_large
;
626 case MMU_CAM_PGSZ_4K
:
627 fn
= iopte_alloc_page
;
635 prot
= get_iopte_attr(e
);
637 spin_lock(&obj
->page_table_lock
);
638 err
= fn(obj
, e
->da
, e
->pa
, prot
);
639 spin_unlock(&obj
->page_table_lock
);
645 * omap_iopgtable_store_entry - Make an iommu pte entry
647 * @e: an iommu tlb entry info
650 omap_iopgtable_store_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
654 flush_iotlb_page(obj
, e
->da
);
655 err
= iopgtable_store_entry_core(obj
, e
);
657 prefetch_iotlb_entry(obj
, e
);
662 * iopgtable_lookup_entry - Lookup an iommu pte entry
664 * @da: iommu device virtual address
665 * @ppgd: iommu pgd entry pointer to be returned
666 * @ppte: iommu pte entry pointer to be returned
669 iopgtable_lookup_entry(struct omap_iommu
*obj
, u32 da
, u32
**ppgd
, u32
**ppte
)
671 u32
*iopgd
, *iopte
= NULL
;
673 iopgd
= iopgd_offset(obj
, da
);
677 if (iopgd_is_table(*iopgd
))
678 iopte
= iopte_offset(iopgd
, da
);
684 static size_t iopgtable_clear_entry_core(struct omap_iommu
*obj
, u32 da
)
687 u32
*iopgd
= iopgd_offset(obj
, da
);
693 if (iopgd_is_table(*iopgd
)) {
695 u32
*iopte
= iopte_offset(iopgd
, da
);
698 if (*iopte
& IOPTE_LARGE
) {
700 /* rewind to the 1st entry */
701 iopte
= iopte_offset(iopgd
, (da
& IOLARGE_MASK
));
704 memset(iopte
, 0, nent
* sizeof(*iopte
));
705 flush_iopte_range(iopte
, iopte
+ (nent
- 1) * sizeof(*iopte
));
708 * do table walk to check if this table is necessary or not
710 iopte
= iopte_offset(iopgd
, 0);
711 for (i
= 0; i
< PTRS_PER_IOPTE
; i
++)
716 nent
= 1; /* for the next L1 entry */
719 if ((*iopgd
& IOPGD_SUPER
) == IOPGD_SUPER
) {
721 /* rewind to the 1st entry */
722 iopgd
= iopgd_offset(obj
, (da
& IOSUPER_MASK
));
726 memset(iopgd
, 0, nent
* sizeof(*iopgd
));
727 flush_iopgd_range(iopgd
, iopgd
+ (nent
- 1) * sizeof(*iopgd
));
733 * iopgtable_clear_entry - Remove an iommu pte entry
735 * @da: iommu device virtual address
737 static size_t iopgtable_clear_entry(struct omap_iommu
*obj
, u32 da
)
741 spin_lock(&obj
->page_table_lock
);
743 bytes
= iopgtable_clear_entry_core(obj
, da
);
744 flush_iotlb_page(obj
, da
);
746 spin_unlock(&obj
->page_table_lock
);
751 static void iopgtable_clear_entry_all(struct omap_iommu
*obj
)
755 spin_lock(&obj
->page_table_lock
);
757 for (i
= 0; i
< PTRS_PER_IOPGD
; i
++) {
761 da
= i
<< IOPGD_SHIFT
;
762 iopgd
= iopgd_offset(obj
, da
);
767 if (iopgd_is_table(*iopgd
))
768 iopte_free(iopte_offset(iopgd
, 0));
771 flush_iopgd_range(iopgd
, iopgd
);
774 flush_iotlb_all(obj
);
776 spin_unlock(&obj
->page_table_lock
);
780 * Device IOMMU generic operations
782 static irqreturn_t
iommu_fault_handler(int irq
, void *data
)
786 struct omap_iommu
*obj
= data
;
787 struct iommu_domain
*domain
= obj
->domain
;
788 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
790 if (!omap_domain
->iommu_dev
)
793 errs
= iommu_report_fault(obj
, &da
);
797 /* Fault callback or TLB/PTE Dynamic loading */
798 if (!report_iommu_fault(domain
, obj
->dev
, da
, 0))
803 iopgd
= iopgd_offset(obj
, da
);
805 if (!iopgd_is_table(*iopgd
)) {
806 dev_err(obj
->dev
, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
807 obj
->name
, errs
, da
, iopgd
, *iopgd
);
811 iopte
= iopte_offset(iopgd
, da
);
813 dev_err(obj
->dev
, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
814 obj
->name
, errs
, da
, iopgd
, *iopgd
, iopte
, *iopte
);
819 static int device_match_by_alias(struct device
*dev
, void *data
)
821 struct omap_iommu
*obj
= to_iommu(dev
);
822 const char *name
= data
;
824 pr_debug("%s: %s %s\n", __func__
, obj
->name
, name
);
826 return strcmp(obj
->name
, name
) == 0;
830 * omap_iommu_attach() - attach iommu device to an iommu domain
831 * @name: name of target omap iommu device
834 static struct omap_iommu
*omap_iommu_attach(const char *name
, u32
*iopgd
)
838 struct omap_iommu
*obj
;
840 dev
= driver_find_device(&omap_iommu_driver
.driver
, NULL
, (void *)name
,
841 device_match_by_alias
);
843 return ERR_PTR(-ENODEV
);
847 spin_lock(&obj
->iommu_lock
);
850 err
= iommu_enable(obj
);
853 flush_iotlb_all(obj
);
855 spin_unlock(&obj
->iommu_lock
);
857 dev_dbg(obj
->dev
, "%s: %s\n", __func__
, obj
->name
);
861 spin_unlock(&obj
->iommu_lock
);
866 * omap_iommu_detach - release iommu device
869 static void omap_iommu_detach(struct omap_iommu
*obj
)
871 if (!obj
|| IS_ERR(obj
))
874 spin_lock(&obj
->iommu_lock
);
879 spin_unlock(&obj
->iommu_lock
);
881 dev_dbg(obj
->dev
, "%s: %s\n", __func__
, obj
->name
);
884 static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device
*pdev
,
885 struct omap_iommu
*obj
)
887 struct device_node
*np
= pdev
->dev
.of_node
;
890 if (!of_device_is_compatible(np
, "ti,dra7-dsp-iommu"))
893 if (!of_property_read_bool(np
, "ti,syscon-mmuconfig")) {
894 dev_err(&pdev
->dev
, "ti,syscon-mmuconfig property is missing\n");
899 syscon_regmap_lookup_by_phandle(np
, "ti,syscon-mmuconfig");
900 if (IS_ERR(obj
->syscfg
)) {
901 /* can fail with -EPROBE_DEFER */
902 ret
= PTR_ERR(obj
->syscfg
);
906 if (of_property_read_u32_index(np
, "ti,syscon-mmuconfig", 1,
908 dev_err(&pdev
->dev
, "couldn't get the IOMMU instance id within subsystem\n");
912 if (obj
->id
!= 0 && obj
->id
!= 1) {
913 dev_err(&pdev
->dev
, "invalid IOMMU instance id\n");
921 * OMAP Device MMU(IOMMU) detection
923 static int omap_iommu_probe(struct platform_device
*pdev
)
927 struct omap_iommu
*obj
;
928 struct resource
*res
;
929 struct iommu_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
930 struct device_node
*of
= pdev
->dev
.of_node
;
932 obj
= devm_kzalloc(&pdev
->dev
, sizeof(*obj
) + MMU_REG_SIZE
, GFP_KERNEL
);
937 obj
->name
= dev_name(&pdev
->dev
);
938 obj
->nr_tlb_entries
= 32;
939 err
= of_property_read_u32(of
, "ti,#tlb-entries",
940 &obj
->nr_tlb_entries
);
941 if (err
&& err
!= -EINVAL
)
943 if (obj
->nr_tlb_entries
!= 32 && obj
->nr_tlb_entries
!= 8)
945 if (of_find_property(of
, "ti,iommu-bus-err-back", NULL
))
946 obj
->has_bus_err_back
= MMU_GP_REG_BUS_ERR_BACK_EN
;
948 obj
->nr_tlb_entries
= pdata
->nr_tlb_entries
;
949 obj
->name
= pdata
->name
;
952 obj
->dev
= &pdev
->dev
;
953 obj
->ctx
= (void *)obj
+ sizeof(*obj
);
955 spin_lock_init(&obj
->iommu_lock
);
956 spin_lock_init(&obj
->page_table_lock
);
958 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
959 obj
->regbase
= devm_ioremap_resource(obj
->dev
, res
);
960 if (IS_ERR(obj
->regbase
))
961 return PTR_ERR(obj
->regbase
);
963 err
= omap_iommu_dra7_get_dsp_system_cfg(pdev
, obj
);
967 irq
= platform_get_irq(pdev
, 0);
971 err
= devm_request_irq(obj
->dev
, irq
, iommu_fault_handler
, IRQF_SHARED
,
972 dev_name(obj
->dev
), obj
);
975 platform_set_drvdata(pdev
, obj
);
977 pm_runtime_irq_safe(obj
->dev
);
978 pm_runtime_enable(obj
->dev
);
980 omap_iommu_debugfs_add(obj
);
982 dev_info(&pdev
->dev
, "%s registered\n", obj
->name
);
986 static int omap_iommu_remove(struct platform_device
*pdev
)
988 struct omap_iommu
*obj
= platform_get_drvdata(pdev
);
990 iopgtable_clear_entry_all(obj
);
991 omap_iommu_debugfs_remove(obj
);
993 pm_runtime_disable(obj
->dev
);
995 dev_info(&pdev
->dev
, "%s removed\n", obj
->name
);
999 static const struct of_device_id omap_iommu_of_match
[] = {
1000 { .compatible
= "ti,omap2-iommu" },
1001 { .compatible
= "ti,omap4-iommu" },
1002 { .compatible
= "ti,dra7-iommu" },
1003 { .compatible
= "ti,dra7-dsp-iommu" },
1007 static struct platform_driver omap_iommu_driver
= {
1008 .probe
= omap_iommu_probe
,
1009 .remove
= omap_iommu_remove
,
1011 .name
= "omap-iommu",
1012 .of_match_table
= of_match_ptr(omap_iommu_of_match
),
1016 static void iopte_cachep_ctor(void *iopte
)
1018 clean_dcache_area(iopte
, IOPTE_TABLE_SIZE
);
1021 static u32
iotlb_init_entry(struct iotlb_entry
*e
, u32 da
, u32 pa
, int pgsz
)
1023 memset(e
, 0, sizeof(*e
));
1027 e
->valid
= MMU_CAM_V
;
1029 e
->endian
= MMU_RAM_ENDIAN_LITTLE
;
1030 e
->elsz
= MMU_RAM_ELSZ_8
;
1033 return iopgsz_to_bytes(e
->pgsz
);
1036 static int omap_iommu_map(struct iommu_domain
*domain
, unsigned long da
,
1037 phys_addr_t pa
, size_t bytes
, int prot
)
1039 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
1040 struct omap_iommu
*oiommu
= omap_domain
->iommu_dev
;
1041 struct device
*dev
= oiommu
->dev
;
1042 struct iotlb_entry e
;
1046 omap_pgsz
= bytes_to_iopgsz(bytes
);
1047 if (omap_pgsz
< 0) {
1048 dev_err(dev
, "invalid size to map: %d\n", bytes
);
1052 dev_dbg(dev
, "mapping da 0x%lx to pa %pa size 0x%x\n", da
, &pa
, bytes
);
1054 iotlb_init_entry(&e
, da
, pa
, omap_pgsz
);
1056 ret
= omap_iopgtable_store_entry(oiommu
, &e
);
1058 dev_err(dev
, "omap_iopgtable_store_entry failed: %d\n", ret
);
1063 static size_t omap_iommu_unmap(struct iommu_domain
*domain
, unsigned long da
,
1066 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
1067 struct omap_iommu
*oiommu
= omap_domain
->iommu_dev
;
1068 struct device
*dev
= oiommu
->dev
;
1070 dev_dbg(dev
, "unmapping da 0x%lx size %u\n", da
, size
);
1072 return iopgtable_clear_entry(oiommu
, da
);
1076 omap_iommu_attach_dev(struct iommu_domain
*domain
, struct device
*dev
)
1078 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
1079 struct omap_iommu
*oiommu
;
1080 struct omap_iommu_arch_data
*arch_data
= dev
->archdata
.iommu
;
1083 if (!arch_data
|| !arch_data
->name
) {
1084 dev_err(dev
, "device doesn't have an associated iommu\n");
1088 spin_lock(&omap_domain
->lock
);
1090 /* only a single device is supported per domain for now */
1091 if (omap_domain
->iommu_dev
) {
1092 dev_err(dev
, "iommu domain is already attached\n");
1097 /* get a handle to and enable the omap iommu */
1098 oiommu
= omap_iommu_attach(arch_data
->name
, omap_domain
->pgtable
);
1099 if (IS_ERR(oiommu
)) {
1100 ret
= PTR_ERR(oiommu
);
1101 dev_err(dev
, "can't get omap iommu: %d\n", ret
);
1105 omap_domain
->iommu_dev
= arch_data
->iommu_dev
= oiommu
;
1106 omap_domain
->dev
= dev
;
1107 oiommu
->domain
= domain
;
1110 spin_unlock(&omap_domain
->lock
);
1114 static void _omap_iommu_detach_dev(struct omap_iommu_domain
*omap_domain
,
1117 struct omap_iommu
*oiommu
= dev_to_omap_iommu(dev
);
1118 struct omap_iommu_arch_data
*arch_data
= dev
->archdata
.iommu
;
1120 /* only a single device is supported per domain for now */
1121 if (omap_domain
->iommu_dev
!= oiommu
) {
1122 dev_err(dev
, "invalid iommu device\n");
1126 iopgtable_clear_entry_all(oiommu
);
1128 omap_iommu_detach(oiommu
);
1130 omap_domain
->iommu_dev
= arch_data
->iommu_dev
= NULL
;
1131 omap_domain
->dev
= NULL
;
1132 oiommu
->domain
= NULL
;
1135 static void omap_iommu_detach_dev(struct iommu_domain
*domain
,
1138 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
1140 spin_lock(&omap_domain
->lock
);
1141 _omap_iommu_detach_dev(omap_domain
, dev
);
1142 spin_unlock(&omap_domain
->lock
);
1145 static struct iommu_domain
*omap_iommu_domain_alloc(unsigned type
)
1147 struct omap_iommu_domain
*omap_domain
;
1149 if (type
!= IOMMU_DOMAIN_UNMANAGED
)
1152 omap_domain
= kzalloc(sizeof(*omap_domain
), GFP_KERNEL
);
1156 omap_domain
->pgtable
= kzalloc(IOPGD_TABLE_SIZE
, GFP_KERNEL
);
1157 if (!omap_domain
->pgtable
)
1161 * should never fail, but please keep this around to ensure
1162 * we keep the hardware happy
1164 BUG_ON(!IS_ALIGNED((long)omap_domain
->pgtable
, IOPGD_TABLE_SIZE
));
1166 clean_dcache_area(omap_domain
->pgtable
, IOPGD_TABLE_SIZE
);
1167 spin_lock_init(&omap_domain
->lock
);
1169 omap_domain
->domain
.geometry
.aperture_start
= 0;
1170 omap_domain
->domain
.geometry
.aperture_end
= (1ULL << 32) - 1;
1171 omap_domain
->domain
.geometry
.force_aperture
= true;
1173 return &omap_domain
->domain
;
1181 static void omap_iommu_domain_free(struct iommu_domain
*domain
)
1183 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
1186 * An iommu device is still attached
1187 * (currently, only one device can be attached) ?
1189 if (omap_domain
->iommu_dev
)
1190 _omap_iommu_detach_dev(omap_domain
, omap_domain
->dev
);
1192 kfree(omap_domain
->pgtable
);
1196 static phys_addr_t
omap_iommu_iova_to_phys(struct iommu_domain
*domain
,
1199 struct omap_iommu_domain
*omap_domain
= to_omap_domain(domain
);
1200 struct omap_iommu
*oiommu
= omap_domain
->iommu_dev
;
1201 struct device
*dev
= oiommu
->dev
;
1203 phys_addr_t ret
= 0;
1205 iopgtable_lookup_entry(oiommu
, da
, &pgd
, &pte
);
1208 if (iopte_is_small(*pte
))
1209 ret
= omap_iommu_translate(*pte
, da
, IOPTE_MASK
);
1210 else if (iopte_is_large(*pte
))
1211 ret
= omap_iommu_translate(*pte
, da
, IOLARGE_MASK
);
1213 dev_err(dev
, "bogus pte 0x%x, da 0x%llx", *pte
,
1214 (unsigned long long)da
);
1216 if (iopgd_is_section(*pgd
))
1217 ret
= omap_iommu_translate(*pgd
, da
, IOSECTION_MASK
);
1218 else if (iopgd_is_super(*pgd
))
1219 ret
= omap_iommu_translate(*pgd
, da
, IOSUPER_MASK
);
1221 dev_err(dev
, "bogus pgd 0x%x, da 0x%llx", *pgd
,
1222 (unsigned long long)da
);
1228 static int omap_iommu_add_device(struct device
*dev
)
1230 struct omap_iommu_arch_data
*arch_data
;
1231 struct device_node
*np
;
1232 struct platform_device
*pdev
;
1235 * Allocate the archdata iommu structure for DT-based devices.
1237 * TODO: Simplify this when removing non-DT support completely from the
1243 np
= of_parse_phandle(dev
->of_node
, "iommus", 0);
1247 pdev
= of_find_device_by_node(np
);
1248 if (WARN_ON(!pdev
)) {
1253 arch_data
= kzalloc(sizeof(*arch_data
), GFP_KERNEL
);
1259 arch_data
->name
= kstrdup(dev_name(&pdev
->dev
), GFP_KERNEL
);
1260 dev
->archdata
.iommu
= arch_data
;
1267 static void omap_iommu_remove_device(struct device
*dev
)
1269 struct omap_iommu_arch_data
*arch_data
= dev
->archdata
.iommu
;
1271 if (!dev
->of_node
|| !arch_data
)
1274 kfree(arch_data
->name
);
1278 static const struct iommu_ops omap_iommu_ops
= {
1279 .domain_alloc
= omap_iommu_domain_alloc
,
1280 .domain_free
= omap_iommu_domain_free
,
1281 .attach_dev
= omap_iommu_attach_dev
,
1282 .detach_dev
= omap_iommu_detach_dev
,
1283 .map
= omap_iommu_map
,
1284 .unmap
= omap_iommu_unmap
,
1285 .map_sg
= default_iommu_map_sg
,
1286 .iova_to_phys
= omap_iommu_iova_to_phys
,
1287 .add_device
= omap_iommu_add_device
,
1288 .remove_device
= omap_iommu_remove_device
,
1289 .pgsize_bitmap
= OMAP_IOMMU_PGSIZES
,
1292 static int __init
omap_iommu_init(void)
1294 struct kmem_cache
*p
;
1295 const unsigned long flags
= SLAB_HWCACHE_ALIGN
;
1296 size_t align
= 1 << 10; /* L2 pagetable alignement */
1297 struct device_node
*np
;
1299 np
= of_find_matching_node(NULL
, omap_iommu_of_match
);
1305 p
= kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE
, align
, flags
,
1311 bus_set_iommu(&platform_bus_type
, &omap_iommu_ops
);
1313 omap_iommu_debugfs_init();
1315 return platform_driver_register(&omap_iommu_driver
);
1317 subsys_initcall(omap_iommu_init
);
1318 /* must be ready before omap3isp is probed */