GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / arm / plat-omap / iommu.c
blob43fd6a2355d473695c218b38c8dbdf3d1e7f29f6
1 /*
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>
22 #include <asm/cacheflush.h>
24 #include <plat/iommu.h>
26 #include "iopgtable.h"
28 #define for_each_iotlb_cr(obj, n, __i, cr) \
29 for (__i = 0; \
30 (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \
31 __i++)
33 /* accommodate the difference between omap1 and omap2/3 */
34 static const struct iommu_functions *arch_iommu;
36 static struct platform_driver omap_iommu_driver;
37 static struct kmem_cache *iopte_cachep;
39 /**
40 * install_iommu_arch - Install archtecure specific iommu functions
41 * @ops: a pointer to architecture specific iommu functions
43 * There are several kind of iommu algorithm(tlb, pagetable) among
44 * omap series. This interface installs such an iommu algorighm.
45 **/
46 int install_iommu_arch(const struct iommu_functions *ops)
48 if (arch_iommu)
49 return -EBUSY;
51 arch_iommu = ops;
52 return 0;
54 EXPORT_SYMBOL_GPL(install_iommu_arch);
56 /**
57 * uninstall_iommu_arch - Uninstall archtecure specific iommu functions
58 * @ops: a pointer to architecture specific iommu functions
60 * This interface uninstalls the iommu algorighm installed previously.
61 **/
62 void uninstall_iommu_arch(const struct iommu_functions *ops)
64 if (arch_iommu != ops)
65 pr_err("%s: not your arch\n", __func__);
67 arch_iommu = NULL;
69 EXPORT_SYMBOL_GPL(uninstall_iommu_arch);
71 /**
72 * iommu_save_ctx - Save registers for pm off-mode support
73 * @obj: target iommu
74 **/
75 void iommu_save_ctx(struct iommu *obj)
77 arch_iommu->save_ctx(obj);
79 EXPORT_SYMBOL_GPL(iommu_save_ctx);
81 /**
82 * iommu_restore_ctx - Restore registers for pm off-mode support
83 * @obj: target iommu
84 **/
85 void iommu_restore_ctx(struct iommu *obj)
87 arch_iommu->restore_ctx(obj);
89 EXPORT_SYMBOL_GPL(iommu_restore_ctx);
91 /**
92 * iommu_arch_version - Return running iommu arch version
93 **/
94 u32 iommu_arch_version(void)
96 return arch_iommu->version;
98 EXPORT_SYMBOL_GPL(iommu_arch_version);
100 static int iommu_enable(struct iommu *obj)
102 int err;
104 if (!obj)
105 return -EINVAL;
107 clk_enable(obj->clk);
109 err = arch_iommu->enable(obj);
111 clk_disable(obj->clk);
112 return err;
115 static void iommu_disable(struct iommu *obj)
117 if (!obj)
118 return;
120 clk_enable(obj->clk);
122 arch_iommu->disable(obj);
124 clk_disable(obj->clk);
128 * TLB operations
130 void iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
132 BUG_ON(!cr || !e);
134 arch_iommu->cr_to_e(cr, e);
136 EXPORT_SYMBOL_GPL(iotlb_cr_to_e);
138 static inline int iotlb_cr_valid(struct cr_regs *cr)
140 if (!cr)
141 return -EINVAL;
143 return arch_iommu->cr_valid(cr);
146 static inline struct cr_regs *iotlb_alloc_cr(struct iommu *obj,
147 struct iotlb_entry *e)
149 if (!e)
150 return NULL;
152 return arch_iommu->alloc_cr(obj, e);
155 u32 iotlb_cr_to_virt(struct cr_regs *cr)
157 return arch_iommu->cr_to_virt(cr);
159 EXPORT_SYMBOL_GPL(iotlb_cr_to_virt);
161 static u32 get_iopte_attr(struct iotlb_entry *e)
163 return arch_iommu->get_pte_attr(e);
166 static u32 iommu_report_fault(struct iommu *obj, u32 *da)
168 return arch_iommu->fault_isr(obj, da);
171 static void iotlb_lock_get(struct iommu *obj, struct iotlb_lock *l)
173 u32 val;
175 val = iommu_read_reg(obj, MMU_LOCK);
177 l->base = MMU_LOCK_BASE(val);
178 l->vict = MMU_LOCK_VICT(val);
182 static void iotlb_lock_set(struct iommu *obj, struct iotlb_lock *l)
184 u32 val;
186 val = (l->base << MMU_LOCK_BASE_SHIFT);
187 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
189 iommu_write_reg(obj, val, MMU_LOCK);
192 static void iotlb_read_cr(struct iommu *obj, struct cr_regs *cr)
194 arch_iommu->tlb_read_cr(obj, cr);
197 static void iotlb_load_cr(struct iommu *obj, struct cr_regs *cr)
199 arch_iommu->tlb_load_cr(obj, cr);
201 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
202 iommu_write_reg(obj, 1, MMU_LD_TLB);
206 * iotlb_dump_cr - Dump an iommu tlb entry into buf
207 * @obj: target iommu
208 * @cr: contents of cam and ram register
209 * @buf: output buffer
211 static inline ssize_t iotlb_dump_cr(struct iommu *obj, struct cr_regs *cr,
212 char *buf)
214 BUG_ON(!cr || !buf);
216 return arch_iommu->dump_cr(obj, cr, buf);
219 /* only used in iotlb iteration for-loop */
220 static struct cr_regs __iotlb_read_cr(struct iommu *obj, int n)
222 struct cr_regs cr;
223 struct iotlb_lock l;
225 iotlb_lock_get(obj, &l);
226 l.vict = n;
227 iotlb_lock_set(obj, &l);
228 iotlb_read_cr(obj, &cr);
230 return cr;
234 * load_iotlb_entry - Set an iommu tlb entry
235 * @obj: target iommu
236 * @e: an iommu tlb entry info
238 int load_iotlb_entry(struct iommu *obj, struct iotlb_entry *e)
240 int err = 0;
241 struct iotlb_lock l;
242 struct cr_regs *cr;
244 if (!obj || !obj->nr_tlb_entries || !e)
245 return -EINVAL;
247 clk_enable(obj->clk);
249 iotlb_lock_get(obj, &l);
250 if (l.base == obj->nr_tlb_entries) {
251 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
252 err = -EBUSY;
253 goto out;
255 if (!e->prsvd) {
256 int i;
257 struct cr_regs tmp;
259 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
260 if (!iotlb_cr_valid(&tmp))
261 break;
263 if (i == obj->nr_tlb_entries) {
264 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
265 err = -EBUSY;
266 goto out;
269 iotlb_lock_get(obj, &l);
270 } else {
271 l.vict = l.base;
272 iotlb_lock_set(obj, &l);
275 cr = iotlb_alloc_cr(obj, e);
276 if (IS_ERR(cr)) {
277 clk_disable(obj->clk);
278 return PTR_ERR(cr);
281 iotlb_load_cr(obj, cr);
282 kfree(cr);
284 if (e->prsvd)
285 l.base++;
286 /* increment victim for next tlb load */
287 if (++l.vict == obj->nr_tlb_entries)
288 l.vict = l.base;
289 iotlb_lock_set(obj, &l);
290 out:
291 clk_disable(obj->clk);
292 return err;
294 EXPORT_SYMBOL_GPL(load_iotlb_entry);
297 * flush_iotlb_page - Clear an iommu tlb entry
298 * @obj: target iommu
299 * @da: iommu device virtual address
301 * Clear an iommu tlb entry which includes 'da' address.
303 void flush_iotlb_page(struct iommu *obj, u32 da)
305 int i;
306 struct cr_regs cr;
308 clk_enable(obj->clk);
310 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
311 u32 start;
312 size_t bytes;
314 if (!iotlb_cr_valid(&cr))
315 continue;
317 start = iotlb_cr_to_virt(&cr);
318 bytes = iopgsz_to_bytes(cr.cam & 3);
320 if ((start <= da) && (da < start + bytes)) {
321 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
322 __func__, start, da, bytes);
323 iotlb_load_cr(obj, &cr);
324 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
327 clk_disable(obj->clk);
329 if (i == obj->nr_tlb_entries)
330 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
332 EXPORT_SYMBOL_GPL(flush_iotlb_page);
335 * flush_iotlb_range - Clear an iommu tlb entries
336 * @obj: target iommu
337 * @start: iommu device virtual address(start)
338 * @end: iommu device virtual address(end)
340 * Clear an iommu tlb entry which includes 'da' address.
342 void flush_iotlb_range(struct iommu *obj, u32 start, u32 end)
344 u32 da = start;
346 while (da < end) {
347 flush_iotlb_page(obj, da);
348 da += IOPTE_SIZE;
351 EXPORT_SYMBOL_GPL(flush_iotlb_range);
354 * flush_iotlb_all - Clear all iommu tlb entries
355 * @obj: target iommu
357 void flush_iotlb_all(struct iommu *obj)
359 struct iotlb_lock l;
361 clk_enable(obj->clk);
363 l.base = 0;
364 l.vict = 0;
365 iotlb_lock_set(obj, &l);
367 iommu_write_reg(obj, 1, MMU_GFLUSH);
369 clk_disable(obj->clk);
371 EXPORT_SYMBOL_GPL(flush_iotlb_all);
374 * iommu_set_twl - enable/disable table walking logic
375 * @obj: target iommu
376 * @on: enable/disable
378 * Function used to enable/disable TWL. If one wants to work
379 * exclusively with locked TLB entries and receive notifications
380 * for TLB miss then call this function to disable TWL.
382 void iommu_set_twl(struct iommu *obj, bool on)
384 clk_enable(obj->clk);
385 arch_iommu->set_twl(obj, on);
386 clk_disable(obj->clk);
388 EXPORT_SYMBOL_GPL(iommu_set_twl);
390 #if defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
392 ssize_t iommu_dump_ctx(struct iommu *obj, char *buf, ssize_t bytes)
394 if (!obj || !buf)
395 return -EINVAL;
397 clk_enable(obj->clk);
399 bytes = arch_iommu->dump_ctx(obj, buf, bytes);
401 clk_disable(obj->clk);
403 return bytes;
405 EXPORT_SYMBOL_GPL(iommu_dump_ctx);
407 static int __dump_tlb_entries(struct iommu *obj, struct cr_regs *crs, int num)
409 int i;
410 struct iotlb_lock saved;
411 struct cr_regs tmp;
412 struct cr_regs *p = crs;
414 clk_enable(obj->clk);
415 iotlb_lock_get(obj, &saved);
417 for_each_iotlb_cr(obj, num, i, tmp) {
418 if (!iotlb_cr_valid(&tmp))
419 continue;
420 *p++ = tmp;
423 iotlb_lock_set(obj, &saved);
424 clk_disable(obj->clk);
426 return p - crs;
430 * dump_tlb_entries - dump cr arrays to given buffer
431 * @obj: target iommu
432 * @buf: output buffer
434 size_t dump_tlb_entries(struct iommu *obj, char *buf, ssize_t bytes)
436 int i, num;
437 struct cr_regs *cr;
438 char *p = buf;
440 num = bytes / sizeof(*cr);
441 num = min(obj->nr_tlb_entries, num);
443 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
444 if (!cr)
445 return 0;
447 num = __dump_tlb_entries(obj, cr, num);
448 for (i = 0; i < num; i++)
449 p += iotlb_dump_cr(obj, cr + i, p);
450 kfree(cr);
452 return p - buf;
454 EXPORT_SYMBOL_GPL(dump_tlb_entries);
456 int foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
458 return driver_for_each_device(&omap_iommu_driver.driver,
459 NULL, data, fn);
461 EXPORT_SYMBOL_GPL(foreach_iommu_device);
463 #endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
466 * H/W pagetable operations
468 static void flush_iopgd_range(u32 *first, u32 *last)
470 do {
471 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
472 : : "r" (first));
473 first += L1_CACHE_BYTES / sizeof(*first);
474 } while (first <= last);
477 static void flush_iopte_range(u32 *first, u32 *last)
479 do {
480 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
481 : : "r" (first));
482 first += L1_CACHE_BYTES / sizeof(*first);
483 } while (first <= last);
486 static void iopte_free(u32 *iopte)
488 /* Note: freed iopte's must be clean ready for re-use */
489 kmem_cache_free(iopte_cachep, iopte);
492 static u32 *iopte_alloc(struct iommu *obj, u32 *iopgd, u32 da)
494 u32 *iopte;
496 /* a table has already existed */
497 if (*iopgd)
498 goto pte_ready;
501 * do the allocation outside the page table lock
503 spin_unlock(&obj->page_table_lock);
504 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
505 spin_lock(&obj->page_table_lock);
507 if (!*iopgd) {
508 if (!iopte)
509 return ERR_PTR(-ENOMEM);
511 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
512 flush_iopgd_range(iopgd, iopgd);
514 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
515 } else {
516 /* We raced, free the reduniovant table */
517 iopte_free(iopte);
520 pte_ready:
521 iopte = iopte_offset(iopgd, da);
523 dev_vdbg(obj->dev,
524 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
525 __func__, da, iopgd, *iopgd, iopte, *iopte);
527 return iopte;
530 static int iopgd_alloc_section(struct iommu *obj, u32 da, u32 pa, u32 prot)
532 u32 *iopgd = iopgd_offset(obj, da);
534 if ((da | pa) & ~IOSECTION_MASK) {
535 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
536 __func__, da, pa, IOSECTION_SIZE);
537 return -EINVAL;
540 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
541 flush_iopgd_range(iopgd, iopgd);
542 return 0;
545 static int iopgd_alloc_super(struct iommu *obj, u32 da, u32 pa, u32 prot)
547 u32 *iopgd = iopgd_offset(obj, da);
548 int i;
550 if ((da | pa) & ~IOSUPER_MASK) {
551 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
552 __func__, da, pa, IOSUPER_SIZE);
553 return -EINVAL;
556 for (i = 0; i < 16; i++)
557 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
558 flush_iopgd_range(iopgd, iopgd + 15);
559 return 0;
562 static int iopte_alloc_page(struct iommu *obj, u32 da, u32 pa, u32 prot)
564 u32 *iopgd = iopgd_offset(obj, da);
565 u32 *iopte = iopte_alloc(obj, iopgd, da);
567 if (IS_ERR(iopte))
568 return PTR_ERR(iopte);
570 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
571 flush_iopte_range(iopte, iopte);
573 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
574 __func__, da, pa, iopte, *iopte);
576 return 0;
579 static int iopte_alloc_large(struct iommu *obj, u32 da, u32 pa, u32 prot)
581 u32 *iopgd = iopgd_offset(obj, da);
582 u32 *iopte = iopte_alloc(obj, iopgd, da);
583 int i;
585 if ((da | pa) & ~IOLARGE_MASK) {
586 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
587 __func__, da, pa, IOLARGE_SIZE);
588 return -EINVAL;
591 if (IS_ERR(iopte))
592 return PTR_ERR(iopte);
594 for (i = 0; i < 16; i++)
595 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
596 flush_iopte_range(iopte, iopte + 15);
597 return 0;
600 static int iopgtable_store_entry_core(struct iommu *obj, struct iotlb_entry *e)
602 int (*fn)(struct iommu *, u32, u32, u32);
603 u32 prot;
604 int err;
606 if (!obj || !e)
607 return -EINVAL;
609 switch (e->pgsz) {
610 case MMU_CAM_PGSZ_16M:
611 fn = iopgd_alloc_super;
612 break;
613 case MMU_CAM_PGSZ_1M:
614 fn = iopgd_alloc_section;
615 break;
616 case MMU_CAM_PGSZ_64K:
617 fn = iopte_alloc_large;
618 break;
619 case MMU_CAM_PGSZ_4K:
620 fn = iopte_alloc_page;
621 break;
622 default:
623 fn = NULL;
624 BUG();
625 break;
628 prot = get_iopte_attr(e);
630 spin_lock(&obj->page_table_lock);
631 err = fn(obj, e->da, e->pa, prot);
632 spin_unlock(&obj->page_table_lock);
634 return err;
638 * iopgtable_store_entry - Make an iommu pte entry
639 * @obj: target iommu
640 * @e: an iommu tlb entry info
642 int iopgtable_store_entry(struct iommu *obj, struct iotlb_entry *e)
644 int err;
646 flush_iotlb_page(obj, e->da);
647 err = iopgtable_store_entry_core(obj, e);
648 #ifdef PREFETCH_IOTLB
649 if (!err)
650 load_iotlb_entry(obj, e);
651 #endif
652 return err;
654 EXPORT_SYMBOL_GPL(iopgtable_store_entry);
657 * iopgtable_lookup_entry - Lookup an iommu pte entry
658 * @obj: target iommu
659 * @da: iommu device virtual address
660 * @ppgd: iommu pgd entry pointer to be returned
661 * @ppte: iommu pte entry pointer to be returned
663 void iopgtable_lookup_entry(struct iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
665 u32 *iopgd, *iopte = NULL;
667 iopgd = iopgd_offset(obj, da);
668 if (!*iopgd)
669 goto out;
671 if (iopgd_is_table(*iopgd))
672 iopte = iopte_offset(iopgd, da);
673 out:
674 *ppgd = iopgd;
675 *ppte = iopte;
677 EXPORT_SYMBOL_GPL(iopgtable_lookup_entry);
679 static size_t iopgtable_clear_entry_core(struct iommu *obj, u32 da)
681 size_t bytes;
682 u32 *iopgd = iopgd_offset(obj, da);
683 int nent = 1;
685 if (!*iopgd)
686 return 0;
688 if (iopgd_is_table(*iopgd)) {
689 int i;
690 u32 *iopte = iopte_offset(iopgd, da);
692 bytes = IOPTE_SIZE;
693 if (*iopte & IOPTE_LARGE) {
694 nent *= 16;
695 /* rewind to the 1st entry */
696 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
698 bytes *= nent;
699 memset(iopte, 0, nent * sizeof(*iopte));
700 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
703 * do table walk to check if this table is necessary or not
705 iopte = iopte_offset(iopgd, 0);
706 for (i = 0; i < PTRS_PER_IOPTE; i++)
707 if (iopte[i])
708 goto out;
710 iopte_free(iopte);
711 nent = 1; /* for the next L1 entry */
712 } else {
713 bytes = IOPGD_SIZE;
714 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
715 nent *= 16;
716 /* rewind to the 1st entry */
717 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
719 bytes *= nent;
721 memset(iopgd, 0, nent * sizeof(*iopgd));
722 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
723 out:
724 return bytes;
728 * iopgtable_clear_entry - Remove an iommu pte entry
729 * @obj: target iommu
730 * @da: iommu device virtual address
732 size_t iopgtable_clear_entry(struct iommu *obj, u32 da)
734 size_t bytes;
736 spin_lock(&obj->page_table_lock);
738 bytes = iopgtable_clear_entry_core(obj, da);
739 flush_iotlb_page(obj, da);
741 spin_unlock(&obj->page_table_lock);
743 return bytes;
745 EXPORT_SYMBOL_GPL(iopgtable_clear_entry);
747 static void iopgtable_clear_entry_all(struct iommu *obj)
749 int i;
751 spin_lock(&obj->page_table_lock);
753 for (i = 0; i < PTRS_PER_IOPGD; i++) {
754 u32 da;
755 u32 *iopgd;
757 da = i << IOPGD_SHIFT;
758 iopgd = iopgd_offset(obj, da);
760 if (!*iopgd)
761 continue;
763 if (iopgd_is_table(*iopgd))
764 iopte_free(iopte_offset(iopgd, 0));
766 *iopgd = 0;
767 flush_iopgd_range(iopgd, iopgd);
770 flush_iotlb_all(obj);
772 spin_unlock(&obj->page_table_lock);
776 * Device IOMMU generic operations
778 static irqreturn_t iommu_fault_handler(int irq, void *data)
780 u32 stat, da;
781 u32 *iopgd, *iopte;
782 int err = -EIO;
783 struct iommu *obj = data;
785 if (!obj->refcount)
786 return IRQ_NONE;
788 /* Dynamic loading TLB or PTE */
789 if (obj->isr)
790 err = obj->isr(obj);
792 if (!err)
793 return IRQ_HANDLED;
795 clk_enable(obj->clk);
796 stat = iommu_report_fault(obj, &da);
797 clk_disable(obj->clk);
798 if (!stat)
799 return IRQ_HANDLED;
801 iommu_disable(obj);
803 iopgd = iopgd_offset(obj, da);
805 if (!iopgd_is_table(*iopgd)) {
806 dev_err(obj->dev, "%s: da:%08x pgd:%p *pgd:%08x\n", __func__,
807 da, iopgd, *iopgd);
808 return IRQ_NONE;
811 iopte = iopte_offset(iopgd, da);
813 dev_err(obj->dev, "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
814 __func__, da, iopgd, *iopgd, iopte, *iopte);
816 return IRQ_NONE;
819 static int device_match_by_alias(struct device *dev, void *data)
821 struct 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 * iommu_get - Get iommu handler
831 * @name: target iommu name
833 struct iommu *iommu_get(const char *name)
835 int err = -ENOMEM;
836 struct device *dev;
837 struct iommu *obj;
839 dev = driver_find_device(&omap_iommu_driver.driver, NULL, (void *)name,
840 device_match_by_alias);
841 if (!dev)
842 return ERR_PTR(-ENODEV);
844 obj = to_iommu(dev);
846 mutex_lock(&obj->iommu_lock);
848 if (obj->refcount++ == 0) {
849 err = iommu_enable(obj);
850 if (err)
851 goto err_enable;
852 flush_iotlb_all(obj);
855 if (!try_module_get(obj->owner))
856 goto err_module;
858 mutex_unlock(&obj->iommu_lock);
860 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
861 return obj;
863 err_module:
864 if (obj->refcount == 1)
865 iommu_disable(obj);
866 err_enable:
867 obj->refcount--;
868 mutex_unlock(&obj->iommu_lock);
869 return ERR_PTR(err);
871 EXPORT_SYMBOL_GPL(iommu_get);
874 * iommu_put - Put back iommu handler
875 * @obj: target iommu
877 void iommu_put(struct iommu *obj)
879 if (!obj || IS_ERR(obj))
880 return;
882 mutex_lock(&obj->iommu_lock);
884 if (--obj->refcount == 0)
885 iommu_disable(obj);
887 module_put(obj->owner);
889 mutex_unlock(&obj->iommu_lock);
891 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
893 EXPORT_SYMBOL_GPL(iommu_put);
896 * OMAP Device MMU(IOMMU) detection
898 static int __devinit omap_iommu_probe(struct platform_device *pdev)
900 int err = -ENODEV;
901 void *p;
902 int irq;
903 struct iommu *obj;
904 struct resource *res;
905 struct iommu_platform_data *pdata = pdev->dev.platform_data;
907 if (pdev->num_resources != 2)
908 return -EINVAL;
910 obj = kzalloc(sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
911 if (!obj)
912 return -ENOMEM;
914 obj->clk = clk_get(&pdev->dev, pdata->clk_name);
915 if (IS_ERR(obj->clk))
916 goto err_clk;
918 obj->nr_tlb_entries = pdata->nr_tlb_entries;
919 obj->name = pdata->name;
920 obj->dev = &pdev->dev;
921 obj->ctx = (void *)obj + sizeof(*obj);
923 mutex_init(&obj->iommu_lock);
924 mutex_init(&obj->mmap_lock);
925 spin_lock_init(&obj->page_table_lock);
926 INIT_LIST_HEAD(&obj->mmap);
928 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
929 if (!res) {
930 err = -ENODEV;
931 goto err_mem;
933 obj->regbase = ioremap(res->start, resource_size(res));
934 if (!obj->regbase) {
935 err = -ENOMEM;
936 goto err_mem;
939 res = request_mem_region(res->start, resource_size(res),
940 dev_name(&pdev->dev));
941 if (!res) {
942 err = -EIO;
943 goto err_mem;
946 irq = platform_get_irq(pdev, 0);
947 if (irq < 0) {
948 err = -ENODEV;
949 goto err_irq;
951 err = request_irq(irq, iommu_fault_handler, IRQF_SHARED,
952 dev_name(&pdev->dev), obj);
953 if (err < 0)
954 goto err_irq;
955 platform_set_drvdata(pdev, obj);
957 p = (void *)__get_free_pages(GFP_KERNEL, get_order(IOPGD_TABLE_SIZE));
958 if (!p) {
959 err = -ENOMEM;
960 goto err_pgd;
962 memset(p, 0, IOPGD_TABLE_SIZE);
963 clean_dcache_area(p, IOPGD_TABLE_SIZE);
964 obj->iopgd = p;
966 BUG_ON(!IS_ALIGNED((unsigned long)obj->iopgd, IOPGD_TABLE_SIZE));
968 dev_info(&pdev->dev, "%s registered\n", obj->name);
969 return 0;
971 err_pgd:
972 free_irq(irq, obj);
973 err_irq:
974 release_mem_region(res->start, resource_size(res));
975 iounmap(obj->regbase);
976 err_mem:
977 clk_put(obj->clk);
978 err_clk:
979 kfree(obj);
980 return err;
983 static int __devexit omap_iommu_remove(struct platform_device *pdev)
985 int irq;
986 struct resource *res;
987 struct iommu *obj = platform_get_drvdata(pdev);
989 platform_set_drvdata(pdev, NULL);
991 iopgtable_clear_entry_all(obj);
992 free_pages((unsigned long)obj->iopgd, get_order(IOPGD_TABLE_SIZE));
994 irq = platform_get_irq(pdev, 0);
995 free_irq(irq, obj);
996 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
997 release_mem_region(res->start, resource_size(res));
998 iounmap(obj->regbase);
1000 clk_put(obj->clk);
1001 dev_info(&pdev->dev, "%s removed\n", obj->name);
1002 kfree(obj);
1003 return 0;
1006 static struct platform_driver omap_iommu_driver = {
1007 .probe = omap_iommu_probe,
1008 .remove = __devexit_p(omap_iommu_remove),
1009 .driver = {
1010 .name = "omap-iommu",
1014 static void iopte_cachep_ctor(void *iopte)
1016 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1019 static int __init omap_iommu_init(void)
1021 struct kmem_cache *p;
1022 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1023 size_t align = 1 << 10; /* L2 pagetable alignement */
1025 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1026 iopte_cachep_ctor);
1027 if (!p)
1028 return -ENOMEM;
1029 iopte_cachep = p;
1031 return platform_driver_register(&omap_iommu_driver);
1033 module_init(omap_iommu_init);
1035 static void __exit omap_iommu_exit(void)
1037 kmem_cache_destroy(iopte_cachep);
1039 platform_driver_unregister(&omap_iommu_driver);
1041 module_exit(omap_iommu_exit);
1043 MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1044 MODULE_ALIAS("platform:omap-iommu");
1045 MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1046 MODULE_LICENSE("GPL v2");