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/clk.h>
20 #include <linux/platform_device.h>
21 #include <linux/iommu.h>
22 #include <linux/mutex.h>
23 #include <linux/spinlock.h>
25 #include <asm/cacheflush.h>
27 #include <plat/iommu.h>
29 #include <plat/iopgtable.h>
31 #define for_each_iotlb_cr(obj, n, __i, cr) \
33 (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \
36 /* bitmap of the page sizes currently supported */
37 #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
40 * struct omap_iommu_domain - omap iommu domain
41 * @pgtable: the page table
42 * @iommu_dev: an omap iommu device attached to this domain. only a single
43 * iommu device can be attached for now.
44 * @lock: domain lock, should be taken when attaching/detaching
46 struct omap_iommu_domain
{
48 struct omap_iommu
*iommu_dev
;
52 /* accommodate the difference between omap1 and omap2/3 */
53 static const struct iommu_functions
*arch_iommu
;
55 static struct platform_driver omap_iommu_driver
;
56 static struct kmem_cache
*iopte_cachep
;
59 * omap_install_iommu_arch - Install archtecure specific iommu functions
60 * @ops: a pointer to architecture specific iommu functions
62 * There are several kind of iommu algorithm(tlb, pagetable) among
63 * omap series. This interface installs such an iommu algorighm.
65 int omap_install_iommu_arch(const struct iommu_functions
*ops
)
73 EXPORT_SYMBOL_GPL(omap_install_iommu_arch
);
76 * omap_uninstall_iommu_arch - Uninstall archtecure specific iommu functions
77 * @ops: a pointer to architecture specific iommu functions
79 * This interface uninstalls the iommu algorighm installed previously.
81 void omap_uninstall_iommu_arch(const struct iommu_functions
*ops
)
83 if (arch_iommu
!= ops
)
84 pr_err("%s: not your arch\n", __func__
);
88 EXPORT_SYMBOL_GPL(omap_uninstall_iommu_arch
);
91 * omap_iommu_save_ctx - Save registers for pm off-mode support
94 void omap_iommu_save_ctx(struct device
*dev
)
96 struct omap_iommu
*obj
= dev_to_omap_iommu(dev
);
98 arch_iommu
->save_ctx(obj
);
100 EXPORT_SYMBOL_GPL(omap_iommu_save_ctx
);
103 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
104 * @dev: client device
106 void omap_iommu_restore_ctx(struct device
*dev
)
108 struct omap_iommu
*obj
= dev_to_omap_iommu(dev
);
110 arch_iommu
->restore_ctx(obj
);
112 EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx
);
115 * omap_iommu_arch_version - Return running iommu arch version
117 u32
omap_iommu_arch_version(void)
119 return arch_iommu
->version
;
121 EXPORT_SYMBOL_GPL(omap_iommu_arch_version
);
123 static int iommu_enable(struct omap_iommu
*obj
)
133 clk_enable(obj
->clk
);
135 err
= arch_iommu
->enable(obj
);
137 clk_disable(obj
->clk
);
141 static void iommu_disable(struct omap_iommu
*obj
)
146 clk_enable(obj
->clk
);
148 arch_iommu
->disable(obj
);
150 clk_disable(obj
->clk
);
156 void omap_iotlb_cr_to_e(struct cr_regs
*cr
, struct iotlb_entry
*e
)
160 arch_iommu
->cr_to_e(cr
, e
);
162 EXPORT_SYMBOL_GPL(omap_iotlb_cr_to_e
);
164 static inline int iotlb_cr_valid(struct cr_regs
*cr
)
169 return arch_iommu
->cr_valid(cr
);
172 static inline struct cr_regs
*iotlb_alloc_cr(struct omap_iommu
*obj
,
173 struct iotlb_entry
*e
)
178 return arch_iommu
->alloc_cr(obj
, e
);
181 static u32
iotlb_cr_to_virt(struct cr_regs
*cr
)
183 return arch_iommu
->cr_to_virt(cr
);
186 static u32
get_iopte_attr(struct iotlb_entry
*e
)
188 return arch_iommu
->get_pte_attr(e
);
191 static u32
iommu_report_fault(struct omap_iommu
*obj
, u32
*da
)
193 return arch_iommu
->fault_isr(obj
, da
);
196 static void iotlb_lock_get(struct omap_iommu
*obj
, struct iotlb_lock
*l
)
200 val
= iommu_read_reg(obj
, MMU_LOCK
);
202 l
->base
= MMU_LOCK_BASE(val
);
203 l
->vict
= MMU_LOCK_VICT(val
);
207 static void iotlb_lock_set(struct omap_iommu
*obj
, struct iotlb_lock
*l
)
211 val
= (l
->base
<< MMU_LOCK_BASE_SHIFT
);
212 val
|= (l
->vict
<< MMU_LOCK_VICT_SHIFT
);
214 iommu_write_reg(obj
, val
, MMU_LOCK
);
217 static void iotlb_read_cr(struct omap_iommu
*obj
, struct cr_regs
*cr
)
219 arch_iommu
->tlb_read_cr(obj
, cr
);
222 static void iotlb_load_cr(struct omap_iommu
*obj
, struct cr_regs
*cr
)
224 arch_iommu
->tlb_load_cr(obj
, cr
);
226 iommu_write_reg(obj
, 1, MMU_FLUSH_ENTRY
);
227 iommu_write_reg(obj
, 1, MMU_LD_TLB
);
231 * iotlb_dump_cr - Dump an iommu tlb entry into buf
233 * @cr: contents of cam and ram register
234 * @buf: output buffer
236 static inline ssize_t
iotlb_dump_cr(struct omap_iommu
*obj
, struct cr_regs
*cr
,
241 return arch_iommu
->dump_cr(obj
, cr
, buf
);
244 /* only used in iotlb iteration for-loop */
245 static struct cr_regs
__iotlb_read_cr(struct omap_iommu
*obj
, int n
)
250 iotlb_lock_get(obj
, &l
);
252 iotlb_lock_set(obj
, &l
);
253 iotlb_read_cr(obj
, &cr
);
259 * load_iotlb_entry - Set an iommu tlb entry
261 * @e: an iommu tlb entry info
263 #ifdef PREFETCH_IOTLB
264 static int load_iotlb_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
270 if (!obj
|| !obj
->nr_tlb_entries
|| !e
)
273 clk_enable(obj
->clk
);
275 iotlb_lock_get(obj
, &l
);
276 if (l
.base
== obj
->nr_tlb_entries
) {
277 dev_warn(obj
->dev
, "%s: preserve entries full\n", __func__
);
285 for_each_iotlb_cr(obj
, obj
->nr_tlb_entries
, i
, tmp
)
286 if (!iotlb_cr_valid(&tmp
))
289 if (i
== obj
->nr_tlb_entries
) {
290 dev_dbg(obj
->dev
, "%s: full: no entry\n", __func__
);
295 iotlb_lock_get(obj
, &l
);
298 iotlb_lock_set(obj
, &l
);
301 cr
= iotlb_alloc_cr(obj
, e
);
303 clk_disable(obj
->clk
);
307 iotlb_load_cr(obj
, cr
);
312 /* increment victim for next tlb load */
313 if (++l
.vict
== obj
->nr_tlb_entries
)
315 iotlb_lock_set(obj
, &l
);
317 clk_disable(obj
->clk
);
321 #else /* !PREFETCH_IOTLB */
323 static int load_iotlb_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
328 #endif /* !PREFETCH_IOTLB */
330 static int prefetch_iotlb_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
332 return load_iotlb_entry(obj
, e
);
336 * flush_iotlb_page - Clear an iommu tlb entry
338 * @da: iommu device virtual address
340 * Clear an iommu tlb entry which includes 'da' address.
342 static void flush_iotlb_page(struct omap_iommu
*obj
, u32 da
)
347 clk_enable(obj
->clk
);
349 for_each_iotlb_cr(obj
, obj
->nr_tlb_entries
, i
, cr
) {
353 if (!iotlb_cr_valid(&cr
))
356 start
= iotlb_cr_to_virt(&cr
);
357 bytes
= iopgsz_to_bytes(cr
.cam
& 3);
359 if ((start
<= da
) && (da
< start
+ bytes
)) {
360 dev_dbg(obj
->dev
, "%s: %08x<=%08x(%x)\n",
361 __func__
, start
, da
, bytes
);
362 iotlb_load_cr(obj
, &cr
);
363 iommu_write_reg(obj
, 1, MMU_FLUSH_ENTRY
);
366 clk_disable(obj
->clk
);
368 if (i
== obj
->nr_tlb_entries
)
369 dev_dbg(obj
->dev
, "%s: no page for %08x\n", __func__
, da
);
373 * flush_iotlb_all - Clear all iommu tlb entries
376 static void flush_iotlb_all(struct omap_iommu
*obj
)
380 clk_enable(obj
->clk
);
384 iotlb_lock_set(obj
, &l
);
386 iommu_write_reg(obj
, 1, MMU_GFLUSH
);
388 clk_disable(obj
->clk
);
391 #if defined(CONFIG_OMAP_IOMMU_DEBUG) || defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
393 ssize_t
omap_iommu_dump_ctx(struct omap_iommu
*obj
, char *buf
, ssize_t bytes
)
398 clk_enable(obj
->clk
);
400 bytes
= arch_iommu
->dump_ctx(obj
, buf
, bytes
);
402 clk_disable(obj
->clk
);
406 EXPORT_SYMBOL_GPL(omap_iommu_dump_ctx
);
409 __dump_tlb_entries(struct omap_iommu
*obj
, struct cr_regs
*crs
, int num
)
412 struct iotlb_lock saved
;
414 struct cr_regs
*p
= crs
;
416 clk_enable(obj
->clk
);
417 iotlb_lock_get(obj
, &saved
);
419 for_each_iotlb_cr(obj
, num
, i
, tmp
) {
420 if (!iotlb_cr_valid(&tmp
))
425 iotlb_lock_set(obj
, &saved
);
426 clk_disable(obj
->clk
);
432 * omap_dump_tlb_entries - dump cr arrays to given buffer
434 * @buf: output buffer
436 size_t omap_dump_tlb_entries(struct omap_iommu
*obj
, char *buf
, ssize_t bytes
)
442 num
= bytes
/ sizeof(*cr
);
443 num
= min(obj
->nr_tlb_entries
, num
);
445 cr
= kcalloc(num
, sizeof(*cr
), GFP_KERNEL
);
449 num
= __dump_tlb_entries(obj
, cr
, num
);
450 for (i
= 0; i
< num
; i
++)
451 p
+= iotlb_dump_cr(obj
, cr
+ i
, p
);
456 EXPORT_SYMBOL_GPL(omap_dump_tlb_entries
);
458 int omap_foreach_iommu_device(void *data
, int (*fn
)(struct device
*, void *))
460 return driver_for_each_device(&omap_iommu_driver
.driver
,
463 EXPORT_SYMBOL_GPL(omap_foreach_iommu_device
);
465 #endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
468 * H/W pagetable operations
470 static void flush_iopgd_range(u32
*first
, u32
*last
)
472 /* FIXME: L2 cache should be taken care of if it exists */
474 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
476 first
+= L1_CACHE_BYTES
/ sizeof(*first
);
477 } while (first
<= last
);
480 static void flush_iopte_range(u32
*first
, u32
*last
)
482 /* FIXME: L2 cache should be taken care of if it exists */
484 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
486 first
+= L1_CACHE_BYTES
/ sizeof(*first
);
487 } while (first
<= last
);
490 static void iopte_free(u32
*iopte
)
492 /* Note: freed iopte's must be clean ready for re-use */
493 kmem_cache_free(iopte_cachep
, iopte
);
496 static u32
*iopte_alloc(struct omap_iommu
*obj
, u32
*iopgd
, u32 da
)
500 /* a table has already existed */
505 * do the allocation outside the page table lock
507 spin_unlock(&obj
->page_table_lock
);
508 iopte
= kmem_cache_zalloc(iopte_cachep
, GFP_KERNEL
);
509 spin_lock(&obj
->page_table_lock
);
513 return ERR_PTR(-ENOMEM
);
515 *iopgd
= virt_to_phys(iopte
) | IOPGD_TABLE
;
516 flush_iopgd_range(iopgd
, iopgd
);
518 dev_vdbg(obj
->dev
, "%s: a new pte:%p\n", __func__
, iopte
);
520 /* We raced, free the reduniovant table */
525 iopte
= iopte_offset(iopgd
, da
);
528 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
529 __func__
, da
, iopgd
, *iopgd
, iopte
, *iopte
);
534 static int iopgd_alloc_section(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
536 u32
*iopgd
= iopgd_offset(obj
, da
);
538 if ((da
| pa
) & ~IOSECTION_MASK
) {
539 dev_err(obj
->dev
, "%s: %08x:%08x should aligned on %08lx\n",
540 __func__
, da
, pa
, IOSECTION_SIZE
);
544 *iopgd
= (pa
& IOSECTION_MASK
) | prot
| IOPGD_SECTION
;
545 flush_iopgd_range(iopgd
, iopgd
);
549 static int iopgd_alloc_super(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
551 u32
*iopgd
= iopgd_offset(obj
, da
);
554 if ((da
| pa
) & ~IOSUPER_MASK
) {
555 dev_err(obj
->dev
, "%s: %08x:%08x should aligned on %08lx\n",
556 __func__
, da
, pa
, IOSUPER_SIZE
);
560 for (i
= 0; i
< 16; i
++)
561 *(iopgd
+ i
) = (pa
& IOSUPER_MASK
) | prot
| IOPGD_SUPER
;
562 flush_iopgd_range(iopgd
, iopgd
+ 15);
566 static int iopte_alloc_page(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
568 u32
*iopgd
= iopgd_offset(obj
, da
);
569 u32
*iopte
= iopte_alloc(obj
, iopgd
, da
);
572 return PTR_ERR(iopte
);
574 *iopte
= (pa
& IOPAGE_MASK
) | prot
| IOPTE_SMALL
;
575 flush_iopte_range(iopte
, iopte
);
577 dev_vdbg(obj
->dev
, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
578 __func__
, da
, pa
, iopte
, *iopte
);
583 static int iopte_alloc_large(struct omap_iommu
*obj
, u32 da
, u32 pa
, u32 prot
)
585 u32
*iopgd
= iopgd_offset(obj
, da
);
586 u32
*iopte
= iopte_alloc(obj
, iopgd
, da
);
589 if ((da
| pa
) & ~IOLARGE_MASK
) {
590 dev_err(obj
->dev
, "%s: %08x:%08x should aligned on %08lx\n",
591 __func__
, da
, pa
, IOLARGE_SIZE
);
596 return PTR_ERR(iopte
);
598 for (i
= 0; i
< 16; i
++)
599 *(iopte
+ i
) = (pa
& IOLARGE_MASK
) | prot
| IOPTE_LARGE
;
600 flush_iopte_range(iopte
, iopte
+ 15);
605 iopgtable_store_entry_core(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
607 int (*fn
)(struct omap_iommu
*, u32
, u32
, u32
);
615 case MMU_CAM_PGSZ_16M
:
616 fn
= iopgd_alloc_super
;
618 case MMU_CAM_PGSZ_1M
:
619 fn
= iopgd_alloc_section
;
621 case MMU_CAM_PGSZ_64K
:
622 fn
= iopte_alloc_large
;
624 case MMU_CAM_PGSZ_4K
:
625 fn
= iopte_alloc_page
;
633 prot
= get_iopte_attr(e
);
635 spin_lock(&obj
->page_table_lock
);
636 err
= fn(obj
, e
->da
, e
->pa
, prot
);
637 spin_unlock(&obj
->page_table_lock
);
643 * omap_iopgtable_store_entry - Make an iommu pte entry
645 * @e: an iommu tlb entry info
647 int omap_iopgtable_store_entry(struct omap_iommu
*obj
, struct iotlb_entry
*e
)
651 flush_iotlb_page(obj
, e
->da
);
652 err
= iopgtable_store_entry_core(obj
, e
);
654 prefetch_iotlb_entry(obj
, e
);
657 EXPORT_SYMBOL_GPL(omap_iopgtable_store_entry
);
660 * iopgtable_lookup_entry - Lookup an iommu pte entry
662 * @da: iommu device virtual address
663 * @ppgd: iommu pgd entry pointer to be returned
664 * @ppte: iommu pte entry pointer to be returned
667 iopgtable_lookup_entry(struct omap_iommu
*obj
, u32 da
, u32
**ppgd
, u32
**ppte
)
669 u32
*iopgd
, *iopte
= NULL
;
671 iopgd
= iopgd_offset(obj
, da
);
675 if (iopgd_is_table(*iopgd
))
676 iopte
= iopte_offset(iopgd
, da
);
682 static size_t iopgtable_clear_entry_core(struct omap_iommu
*obj
, u32 da
)
685 u32
*iopgd
= iopgd_offset(obj
, da
);
691 if (iopgd_is_table(*iopgd
)) {
693 u32
*iopte
= iopte_offset(iopgd
, da
);
696 if (*iopte
& IOPTE_LARGE
) {
698 /* rewind to the 1st entry */
699 iopte
= iopte_offset(iopgd
, (da
& IOLARGE_MASK
));
702 memset(iopte
, 0, nent
* sizeof(*iopte
));
703 flush_iopte_range(iopte
, iopte
+ (nent
- 1) * sizeof(*iopte
));
706 * do table walk to check if this table is necessary or not
708 iopte
= iopte_offset(iopgd
, 0);
709 for (i
= 0; i
< PTRS_PER_IOPTE
; i
++)
714 nent
= 1; /* for the next L1 entry */
717 if ((*iopgd
& IOPGD_SUPER
) == IOPGD_SUPER
) {
719 /* rewind to the 1st entry */
720 iopgd
= iopgd_offset(obj
, (da
& IOSUPER_MASK
));
724 memset(iopgd
, 0, nent
* sizeof(*iopgd
));
725 flush_iopgd_range(iopgd
, iopgd
+ (nent
- 1) * sizeof(*iopgd
));
731 * iopgtable_clear_entry - Remove an iommu pte entry
733 * @da: iommu device virtual address
735 static size_t iopgtable_clear_entry(struct omap_iommu
*obj
, u32 da
)
739 spin_lock(&obj
->page_table_lock
);
741 bytes
= iopgtable_clear_entry_core(obj
, da
);
742 flush_iotlb_page(obj
, da
);
744 spin_unlock(&obj
->page_table_lock
);
749 static void iopgtable_clear_entry_all(struct omap_iommu
*obj
)
753 spin_lock(&obj
->page_table_lock
);
755 for (i
= 0; i
< PTRS_PER_IOPGD
; i
++) {
759 da
= i
<< IOPGD_SHIFT
;
760 iopgd
= iopgd_offset(obj
, da
);
765 if (iopgd_is_table(*iopgd
))
766 iopte_free(iopte_offset(iopgd
, 0));
769 flush_iopgd_range(iopgd
, iopgd
);
772 flush_iotlb_all(obj
);
774 spin_unlock(&obj
->page_table_lock
);
778 * Device IOMMU generic operations
780 static irqreturn_t
iommu_fault_handler(int irq
, void *data
)
784 struct omap_iommu
*obj
= data
;
785 struct iommu_domain
*domain
= obj
->domain
;
790 clk_enable(obj
->clk
);
791 errs
= iommu_report_fault(obj
, &da
);
792 clk_disable(obj
->clk
);
796 /* Fault callback or TLB/PTE Dynamic loading */
797 if (!report_iommu_fault(domain
, obj
->dev
, da
, 0))
802 iopgd
= iopgd_offset(obj
, da
);
804 if (!iopgd_is_table(*iopgd
)) {
805 dev_err(obj
->dev
, "%s: errs:0x%08x da:0x%08x pgd:0x%p "
806 "*pgd:px%08x\n", obj
->name
, errs
, da
, iopgd
, *iopgd
);
810 iopte
= iopte_offset(iopgd
, da
);
812 dev_err(obj
->dev
, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x "
813 "pte:0x%p *pte:0x%08x\n", obj
->name
, errs
, da
, iopgd
, *iopgd
,
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
,
842 device_match_by_alias
);
848 spin_lock(&obj
->iommu_lock
);
850 /* an iommu device can only be attached once */
851 if (++obj
->refcount
> 1) {
852 dev_err(dev
, "%s: already attached!\n", obj
->name
);
858 err
= iommu_enable(obj
);
861 flush_iotlb_all(obj
);
863 if (!try_module_get(obj
->owner
))
866 spin_unlock(&obj
->iommu_lock
);
868 dev_dbg(obj
->dev
, "%s: %s\n", __func__
, obj
->name
);
872 if (obj
->refcount
== 1)
876 spin_unlock(&obj
->iommu_lock
);
881 * omap_iommu_detach - release iommu device
884 static void omap_iommu_detach(struct omap_iommu
*obj
)
886 if (!obj
|| IS_ERR(obj
))
889 spin_lock(&obj
->iommu_lock
);
891 if (--obj
->refcount
== 0)
894 module_put(obj
->owner
);
898 spin_unlock(&obj
->iommu_lock
);
900 dev_dbg(obj
->dev
, "%s: %s\n", __func__
, obj
->name
);
904 * OMAP Device MMU(IOMMU) detection
906 static int __devinit
omap_iommu_probe(struct platform_device
*pdev
)
910 struct omap_iommu
*obj
;
911 struct resource
*res
;
912 struct iommu_platform_data
*pdata
= pdev
->dev
.platform_data
;
914 if (pdev
->num_resources
!= 2)
917 obj
= kzalloc(sizeof(*obj
) + MMU_REG_SIZE
, GFP_KERNEL
);
921 obj
->clk
= clk_get(&pdev
->dev
, pdata
->clk_name
);
922 if (IS_ERR(obj
->clk
))
925 obj
->nr_tlb_entries
= pdata
->nr_tlb_entries
;
926 obj
->name
= pdata
->name
;
927 obj
->dev
= &pdev
->dev
;
928 obj
->ctx
= (void *)obj
+ sizeof(*obj
);
929 obj
->da_start
= pdata
->da_start
;
930 obj
->da_end
= pdata
->da_end
;
932 spin_lock_init(&obj
->iommu_lock
);
933 mutex_init(&obj
->mmap_lock
);
934 spin_lock_init(&obj
->page_table_lock
);
935 INIT_LIST_HEAD(&obj
->mmap
);
937 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
943 res
= request_mem_region(res
->start
, resource_size(res
),
944 dev_name(&pdev
->dev
));
950 obj
->regbase
= ioremap(res
->start
, resource_size(res
));
956 irq
= platform_get_irq(pdev
, 0);
961 err
= request_irq(irq
, iommu_fault_handler
, IRQF_SHARED
,
962 dev_name(&pdev
->dev
), obj
);
965 platform_set_drvdata(pdev
, obj
);
967 dev_info(&pdev
->dev
, "%s registered\n", obj
->name
);
971 iounmap(obj
->regbase
);
973 release_mem_region(res
->start
, resource_size(res
));
981 static int __devexit
omap_iommu_remove(struct platform_device
*pdev
)
984 struct resource
*res
;
985 struct omap_iommu
*obj
= platform_get_drvdata(pdev
);
987 platform_set_drvdata(pdev
, NULL
);
989 iopgtable_clear_entry_all(obj
);
991 irq
= platform_get_irq(pdev
, 0);
993 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
994 release_mem_region(res
->start
, resource_size(res
));
995 iounmap(obj
->regbase
);
998 dev_info(&pdev
->dev
, "%s removed\n", obj
->name
);
1003 static struct platform_driver omap_iommu_driver
= {
1004 .probe
= omap_iommu_probe
,
1005 .remove
= __devexit_p(omap_iommu_remove
),
1007 .name
= "omap-iommu",
1011 static void iopte_cachep_ctor(void *iopte
)
1013 clean_dcache_area(iopte
, IOPTE_TABLE_SIZE
);
1016 static int omap_iommu_map(struct iommu_domain
*domain
, unsigned long da
,
1017 phys_addr_t pa
, size_t bytes
, int prot
)
1019 struct omap_iommu_domain
*omap_domain
= domain
->priv
;
1020 struct omap_iommu
*oiommu
= omap_domain
->iommu_dev
;
1021 struct device
*dev
= oiommu
->dev
;
1022 struct iotlb_entry e
;
1026 /* we only support mapping a single iommu page for now */
1027 omap_pgsz
= bytes_to_iopgsz(bytes
);
1028 if (omap_pgsz
< 0) {
1029 dev_err(dev
, "invalid size to map: %d\n", bytes
);
1033 dev_dbg(dev
, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da
, pa
, bytes
);
1035 flags
= omap_pgsz
| prot
;
1037 iotlb_init_entry(&e
, da
, pa
, flags
);
1039 ret
= omap_iopgtable_store_entry(oiommu
, &e
);
1041 dev_err(dev
, "omap_iopgtable_store_entry failed: %d\n", ret
);
1046 static size_t omap_iommu_unmap(struct iommu_domain
*domain
, unsigned long da
,
1049 struct omap_iommu_domain
*omap_domain
= domain
->priv
;
1050 struct omap_iommu
*oiommu
= omap_domain
->iommu_dev
;
1051 struct device
*dev
= oiommu
->dev
;
1053 dev_dbg(dev
, "unmapping da 0x%lx size %u\n", da
, size
);
1055 return iopgtable_clear_entry(oiommu
, da
);
1059 omap_iommu_attach_dev(struct iommu_domain
*domain
, struct device
*dev
)
1061 struct omap_iommu_domain
*omap_domain
= domain
->priv
;
1062 struct omap_iommu
*oiommu
;
1063 struct omap_iommu_arch_data
*arch_data
= dev
->archdata
.iommu
;
1066 spin_lock(&omap_domain
->lock
);
1068 /* only a single device is supported per domain for now */
1069 if (omap_domain
->iommu_dev
) {
1070 dev_err(dev
, "iommu domain is already attached\n");
1075 /* get a handle to and enable the omap iommu */
1076 oiommu
= omap_iommu_attach(arch_data
->name
, omap_domain
->pgtable
);
1077 if (IS_ERR(oiommu
)) {
1078 ret
= PTR_ERR(oiommu
);
1079 dev_err(dev
, "can't get omap iommu: %d\n", ret
);
1083 omap_domain
->iommu_dev
= arch_data
->iommu_dev
= oiommu
;
1084 oiommu
->domain
= domain
;
1087 spin_unlock(&omap_domain
->lock
);
1091 static void omap_iommu_detach_dev(struct iommu_domain
*domain
,
1094 struct omap_iommu_domain
*omap_domain
= domain
->priv
;
1095 struct omap_iommu_arch_data
*arch_data
= dev
->archdata
.iommu
;
1096 struct omap_iommu
*oiommu
= dev_to_omap_iommu(dev
);
1098 spin_lock(&omap_domain
->lock
);
1100 /* only a single device is supported per domain for now */
1101 if (omap_domain
->iommu_dev
!= oiommu
) {
1102 dev_err(dev
, "invalid iommu device\n");
1106 iopgtable_clear_entry_all(oiommu
);
1108 omap_iommu_detach(oiommu
);
1110 omap_domain
->iommu_dev
= arch_data
->iommu_dev
= NULL
;
1113 spin_unlock(&omap_domain
->lock
);
1116 static int omap_iommu_domain_init(struct iommu_domain
*domain
)
1118 struct omap_iommu_domain
*omap_domain
;
1120 omap_domain
= kzalloc(sizeof(*omap_domain
), GFP_KERNEL
);
1122 pr_err("kzalloc failed\n");
1126 omap_domain
->pgtable
= kzalloc(IOPGD_TABLE_SIZE
, GFP_KERNEL
);
1127 if (!omap_domain
->pgtable
) {
1128 pr_err("kzalloc failed\n");
1133 * should never fail, but please keep this around to ensure
1134 * we keep the hardware happy
1136 BUG_ON(!IS_ALIGNED((long)omap_domain
->pgtable
, IOPGD_TABLE_SIZE
));
1138 clean_dcache_area(omap_domain
->pgtable
, IOPGD_TABLE_SIZE
);
1139 spin_lock_init(&omap_domain
->lock
);
1141 domain
->priv
= omap_domain
;
1151 /* assume device was already detached */
1152 static void omap_iommu_domain_destroy(struct iommu_domain
*domain
)
1154 struct omap_iommu_domain
*omap_domain
= domain
->priv
;
1156 domain
->priv
= NULL
;
1158 kfree(omap_domain
->pgtable
);
1162 static phys_addr_t
omap_iommu_iova_to_phys(struct iommu_domain
*domain
,
1165 struct omap_iommu_domain
*omap_domain
= domain
->priv
;
1166 struct omap_iommu
*oiommu
= omap_domain
->iommu_dev
;
1167 struct device
*dev
= oiommu
->dev
;
1169 phys_addr_t ret
= 0;
1171 iopgtable_lookup_entry(oiommu
, da
, &pgd
, &pte
);
1174 if (iopte_is_small(*pte
))
1175 ret
= omap_iommu_translate(*pte
, da
, IOPTE_MASK
);
1176 else if (iopte_is_large(*pte
))
1177 ret
= omap_iommu_translate(*pte
, da
, IOLARGE_MASK
);
1179 dev_err(dev
, "bogus pte 0x%x, da 0x%lx", *pte
, da
);
1181 if (iopgd_is_section(*pgd
))
1182 ret
= omap_iommu_translate(*pgd
, da
, IOSECTION_MASK
);
1183 else if (iopgd_is_super(*pgd
))
1184 ret
= omap_iommu_translate(*pgd
, da
, IOSUPER_MASK
);
1186 dev_err(dev
, "bogus pgd 0x%x, da 0x%lx", *pgd
, da
);
1192 static int omap_iommu_domain_has_cap(struct iommu_domain
*domain
,
1198 static struct iommu_ops omap_iommu_ops
= {
1199 .domain_init
= omap_iommu_domain_init
,
1200 .domain_destroy
= omap_iommu_domain_destroy
,
1201 .attach_dev
= omap_iommu_attach_dev
,
1202 .detach_dev
= omap_iommu_detach_dev
,
1203 .map
= omap_iommu_map
,
1204 .unmap
= omap_iommu_unmap
,
1205 .iova_to_phys
= omap_iommu_iova_to_phys
,
1206 .domain_has_cap
= omap_iommu_domain_has_cap
,
1207 .pgsize_bitmap
= OMAP_IOMMU_PGSIZES
,
1210 static int __init
omap_iommu_init(void)
1212 struct kmem_cache
*p
;
1213 const unsigned long flags
= SLAB_HWCACHE_ALIGN
;
1214 size_t align
= 1 << 10; /* L2 pagetable alignement */
1216 p
= kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE
, align
, flags
,
1222 bus_set_iommu(&platform_bus_type
, &omap_iommu_ops
);
1224 return platform_driver_register(&omap_iommu_driver
);
1226 /* must be ready before omap3isp is probed */
1227 subsys_initcall(omap_iommu_init
);
1229 static void __exit
omap_iommu_exit(void)
1231 kmem_cache_destroy(iopte_cachep
);
1233 platform_driver_unregister(&omap_iommu_driver
);
1235 module_exit(omap_iommu_exit
);
1237 MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1238 MODULE_ALIAS("platform:omap-iommu");
1239 MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1240 MODULE_LICENSE("GPL v2");