Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[pandora-kernel.git] / arch / arm / plat-omap / iommu.c
1 /*
2  * omap iommu: tlb and pagetable primitives
3  *
4  * Copyright (C) 2008-2010 Nokia Corporation
5  *
6  * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>,
7  *              Paul Mundt and Toshihiro Kobayashi
8  *
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.
12  */
13
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
22 #include <asm/cacheflush.h>
23
24 #include <plat/iommu.h>
25
26 #include "iopgtable.h"
27
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++)
32
33 /* accommodate the difference between omap1 and omap2/3 */
34 static const struct iommu_functions *arch_iommu;
35
36 static struct platform_driver omap_iommu_driver;
37 static struct kmem_cache *iopte_cachep;
38
39 /**
40  * install_iommu_arch - Install archtecure specific iommu functions
41  * @ops:        a pointer to architecture specific iommu functions
42  *
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)
47 {
48         if (arch_iommu)
49                 return -EBUSY;
50
51         arch_iommu = ops;
52         return 0;
53 }
54 EXPORT_SYMBOL_GPL(install_iommu_arch);
55
56 /**
57  * uninstall_iommu_arch - Uninstall archtecure specific iommu functions
58  * @ops:        a pointer to architecture specific iommu functions
59  *
60  * This interface uninstalls the iommu algorighm installed previously.
61  **/
62 void uninstall_iommu_arch(const struct iommu_functions *ops)
63 {
64         if (arch_iommu != ops)
65                 pr_err("%s: not your arch\n", __func__);
66
67         arch_iommu = NULL;
68 }
69 EXPORT_SYMBOL_GPL(uninstall_iommu_arch);
70
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)
76 {
77         arch_iommu->save_ctx(obj);
78 }
79 EXPORT_SYMBOL_GPL(iommu_save_ctx);
80
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)
86 {
87         arch_iommu->restore_ctx(obj);
88 }
89 EXPORT_SYMBOL_GPL(iommu_restore_ctx);
90
91 /**
92  * iommu_arch_version - Return running iommu arch version
93  **/
94 u32 iommu_arch_version(void)
95 {
96         return arch_iommu->version;
97 }
98 EXPORT_SYMBOL_GPL(iommu_arch_version);
99
100 static int iommu_enable(struct iommu *obj)
101 {
102         int err;
103
104         if (!obj)
105                 return -EINVAL;
106
107         if (!arch_iommu)
108                 return -ENODEV;
109
110         clk_enable(obj->clk);
111
112         err = arch_iommu->enable(obj);
113
114         clk_disable(obj->clk);
115         return err;
116 }
117
118 static void iommu_disable(struct iommu *obj)
119 {
120         if (!obj)
121                 return;
122
123         clk_enable(obj->clk);
124
125         arch_iommu->disable(obj);
126
127         clk_disable(obj->clk);
128 }
129
130 /*
131  *      TLB operations
132  */
133 void iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
134 {
135         BUG_ON(!cr || !e);
136
137         arch_iommu->cr_to_e(cr, e);
138 }
139 EXPORT_SYMBOL_GPL(iotlb_cr_to_e);
140
141 static inline int iotlb_cr_valid(struct cr_regs *cr)
142 {
143         if (!cr)
144                 return -EINVAL;
145
146         return arch_iommu->cr_valid(cr);
147 }
148
149 static inline struct cr_regs *iotlb_alloc_cr(struct iommu *obj,
150                                              struct iotlb_entry *e)
151 {
152         if (!e)
153                 return NULL;
154
155         return arch_iommu->alloc_cr(obj, e);
156 }
157
158 u32 iotlb_cr_to_virt(struct cr_regs *cr)
159 {
160         return arch_iommu->cr_to_virt(cr);
161 }
162 EXPORT_SYMBOL_GPL(iotlb_cr_to_virt);
163
164 static u32 get_iopte_attr(struct iotlb_entry *e)
165 {
166         return arch_iommu->get_pte_attr(e);
167 }
168
169 static u32 iommu_report_fault(struct iommu *obj, u32 *da)
170 {
171         return arch_iommu->fault_isr(obj, da);
172 }
173
174 static void iotlb_lock_get(struct iommu *obj, struct iotlb_lock *l)
175 {
176         u32 val;
177
178         val = iommu_read_reg(obj, MMU_LOCK);
179
180         l->base = MMU_LOCK_BASE(val);
181         l->vict = MMU_LOCK_VICT(val);
182
183 }
184
185 static void iotlb_lock_set(struct iommu *obj, struct iotlb_lock *l)
186 {
187         u32 val;
188
189         val = (l->base << MMU_LOCK_BASE_SHIFT);
190         val |= (l->vict << MMU_LOCK_VICT_SHIFT);
191
192         iommu_write_reg(obj, val, MMU_LOCK);
193 }
194
195 static void iotlb_read_cr(struct iommu *obj, struct cr_regs *cr)
196 {
197         arch_iommu->tlb_read_cr(obj, cr);
198 }
199
200 static void iotlb_load_cr(struct iommu *obj, struct cr_regs *cr)
201 {
202         arch_iommu->tlb_load_cr(obj, cr);
203
204         iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
205         iommu_write_reg(obj, 1, MMU_LD_TLB);
206 }
207
208 /**
209  * iotlb_dump_cr - Dump an iommu tlb entry into buf
210  * @obj:        target iommu
211  * @cr:         contents of cam and ram register
212  * @buf:        output buffer
213  **/
214 static inline ssize_t iotlb_dump_cr(struct iommu *obj, struct cr_regs *cr,
215                                     char *buf)
216 {
217         BUG_ON(!cr || !buf);
218
219         return arch_iommu->dump_cr(obj, cr, buf);
220 }
221
222 /* only used in iotlb iteration for-loop */
223 static struct cr_regs __iotlb_read_cr(struct iommu *obj, int n)
224 {
225         struct cr_regs cr;
226         struct iotlb_lock l;
227
228         iotlb_lock_get(obj, &l);
229         l.vict = n;
230         iotlb_lock_set(obj, &l);
231         iotlb_read_cr(obj, &cr);
232
233         return cr;
234 }
235
236 /**
237  * load_iotlb_entry - Set an iommu tlb entry
238  * @obj:        target iommu
239  * @e:          an iommu tlb entry info
240  **/
241 int load_iotlb_entry(struct iommu *obj, struct iotlb_entry *e)
242 {
243         int err = 0;
244         struct iotlb_lock l;
245         struct cr_regs *cr;
246
247         if (!obj || !obj->nr_tlb_entries || !e)
248                 return -EINVAL;
249
250         clk_enable(obj->clk);
251
252         iotlb_lock_get(obj, &l);
253         if (l.base == obj->nr_tlb_entries) {
254                 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
255                 err = -EBUSY;
256                 goto out;
257         }
258         if (!e->prsvd) {
259                 int i;
260                 struct cr_regs tmp;
261
262                 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
263                         if (!iotlb_cr_valid(&tmp))
264                                 break;
265
266                 if (i == obj->nr_tlb_entries) {
267                         dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
268                         err = -EBUSY;
269                         goto out;
270                 }
271
272                 iotlb_lock_get(obj, &l);
273         } else {
274                 l.vict = l.base;
275                 iotlb_lock_set(obj, &l);
276         }
277
278         cr = iotlb_alloc_cr(obj, e);
279         if (IS_ERR(cr)) {
280                 clk_disable(obj->clk);
281                 return PTR_ERR(cr);
282         }
283
284         iotlb_load_cr(obj, cr);
285         kfree(cr);
286
287         if (e->prsvd)
288                 l.base++;
289         /* increment victim for next tlb load */
290         if (++l.vict == obj->nr_tlb_entries)
291                 l.vict = l.base;
292         iotlb_lock_set(obj, &l);
293 out:
294         clk_disable(obj->clk);
295         return err;
296 }
297 EXPORT_SYMBOL_GPL(load_iotlb_entry);
298
299 /**
300  * flush_iotlb_page - Clear an iommu tlb entry
301  * @obj:        target iommu
302  * @da:         iommu device virtual address
303  *
304  * Clear an iommu tlb entry which includes 'da' address.
305  **/
306 void flush_iotlb_page(struct iommu *obj, u32 da)
307 {
308         int i;
309         struct cr_regs cr;
310
311         clk_enable(obj->clk);
312
313         for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
314                 u32 start;
315                 size_t bytes;
316
317                 if (!iotlb_cr_valid(&cr))
318                         continue;
319
320                 start = iotlb_cr_to_virt(&cr);
321                 bytes = iopgsz_to_bytes(cr.cam & 3);
322
323                 if ((start <= da) && (da < start + bytes)) {
324                         dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
325                                 __func__, start, da, bytes);
326                         iotlb_load_cr(obj, &cr);
327                         iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
328                 }
329         }
330         clk_disable(obj->clk);
331
332         if (i == obj->nr_tlb_entries)
333                 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
334 }
335 EXPORT_SYMBOL_GPL(flush_iotlb_page);
336
337 /**
338  * flush_iotlb_range - Clear an iommu tlb entries
339  * @obj:        target iommu
340  * @start:      iommu device virtual address(start)
341  * @end:        iommu device virtual address(end)
342  *
343  * Clear an iommu tlb entry which includes 'da' address.
344  **/
345 void flush_iotlb_range(struct iommu *obj, u32 start, u32 end)
346 {
347         u32 da = start;
348
349         while (da < end) {
350                 flush_iotlb_page(obj, da);
351                 /* FIXME: Optimize for multiple page size */
352                 da += IOPTE_SIZE;
353         }
354 }
355 EXPORT_SYMBOL_GPL(flush_iotlb_range);
356
357 /**
358  * flush_iotlb_all - Clear all iommu tlb entries
359  * @obj:        target iommu
360  **/
361 void flush_iotlb_all(struct iommu *obj)
362 {
363         struct iotlb_lock l;
364
365         clk_enable(obj->clk);
366
367         l.base = 0;
368         l.vict = 0;
369         iotlb_lock_set(obj, &l);
370
371         iommu_write_reg(obj, 1, MMU_GFLUSH);
372
373         clk_disable(obj->clk);
374 }
375 EXPORT_SYMBOL_GPL(flush_iotlb_all);
376
377 /**
378  * iommu_set_twl - enable/disable table walking logic
379  * @obj:        target iommu
380  * @on:         enable/disable
381  *
382  * Function used to enable/disable TWL. If one wants to work
383  * exclusively with locked TLB entries and receive notifications
384  * for TLB miss then call this function to disable TWL.
385  */
386 void iommu_set_twl(struct iommu *obj, bool on)
387 {
388         clk_enable(obj->clk);
389         arch_iommu->set_twl(obj, on);
390         clk_disable(obj->clk);
391 }
392 EXPORT_SYMBOL_GPL(iommu_set_twl);
393
394 #if defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
395
396 ssize_t iommu_dump_ctx(struct iommu *obj, char *buf, ssize_t bytes)
397 {
398         if (!obj || !buf)
399                 return -EINVAL;
400
401         clk_enable(obj->clk);
402
403         bytes = arch_iommu->dump_ctx(obj, buf, bytes);
404
405         clk_disable(obj->clk);
406
407         return bytes;
408 }
409 EXPORT_SYMBOL_GPL(iommu_dump_ctx);
410
411 static int __dump_tlb_entries(struct iommu *obj, struct cr_regs *crs, int num)
412 {
413         int i;
414         struct iotlb_lock saved;
415         struct cr_regs tmp;
416         struct cr_regs *p = crs;
417
418         clk_enable(obj->clk);
419         iotlb_lock_get(obj, &saved);
420
421         for_each_iotlb_cr(obj, num, i, tmp) {
422                 if (!iotlb_cr_valid(&tmp))
423                         continue;
424                 *p++ = tmp;
425         }
426
427         iotlb_lock_set(obj, &saved);
428         clk_disable(obj->clk);
429
430         return  p - crs;
431 }
432
433 /**
434  * dump_tlb_entries - dump cr arrays to given buffer
435  * @obj:        target iommu
436  * @buf:        output buffer
437  **/
438 size_t dump_tlb_entries(struct iommu *obj, char *buf, ssize_t bytes)
439 {
440         int i, num;
441         struct cr_regs *cr;
442         char *p = buf;
443
444         num = bytes / sizeof(*cr);
445         num = min(obj->nr_tlb_entries, num);
446
447         cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
448         if (!cr)
449                 return 0;
450
451         num = __dump_tlb_entries(obj, cr, num);
452         for (i = 0; i < num; i++)
453                 p += iotlb_dump_cr(obj, cr + i, p);
454         kfree(cr);
455
456         return p - buf;
457 }
458 EXPORT_SYMBOL_GPL(dump_tlb_entries);
459
460 int foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
461 {
462         return driver_for_each_device(&omap_iommu_driver.driver,
463                                       NULL, data, fn);
464 }
465 EXPORT_SYMBOL_GPL(foreach_iommu_device);
466
467 #endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
468
469 /*
470  *      H/W pagetable operations
471  */
472 static void flush_iopgd_range(u32 *first, u32 *last)
473 {
474         /* FIXME: L2 cache should be taken care of if it exists */
475         do {
476                 asm("mcr        p15, 0, %0, c7, c10, 1 @ flush_pgd"
477                     : : "r" (first));
478                 first += L1_CACHE_BYTES / sizeof(*first);
479         } while (first <= last);
480 }
481
482 static void flush_iopte_range(u32 *first, u32 *last)
483 {
484         /* FIXME: L2 cache should be taken care of if it exists */
485         do {
486                 asm("mcr        p15, 0, %0, c7, c10, 1 @ flush_pte"
487                     : : "r" (first));
488                 first += L1_CACHE_BYTES / sizeof(*first);
489         } while (first <= last);
490 }
491
492 static void iopte_free(u32 *iopte)
493 {
494         /* Note: freed iopte's must be clean ready for re-use */
495         kmem_cache_free(iopte_cachep, iopte);
496 }
497
498 static u32 *iopte_alloc(struct iommu *obj, u32 *iopgd, u32 da)
499 {
500         u32 *iopte;
501
502         /* a table has already existed */
503         if (*iopgd)
504                 goto pte_ready;
505
506         /*
507          * do the allocation outside the page table lock
508          */
509         spin_unlock(&obj->page_table_lock);
510         iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
511         spin_lock(&obj->page_table_lock);
512
513         if (!*iopgd) {
514                 if (!iopte)
515                         return ERR_PTR(-ENOMEM);
516
517                 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
518                 flush_iopgd_range(iopgd, iopgd);
519
520                 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
521         } else {
522                 /* We raced, free the reduniovant table */
523                 iopte_free(iopte);
524         }
525
526 pte_ready:
527         iopte = iopte_offset(iopgd, da);
528
529         dev_vdbg(obj->dev,
530                  "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
531                  __func__, da, iopgd, *iopgd, iopte, *iopte);
532
533         return iopte;
534 }
535
536 static int iopgd_alloc_section(struct iommu *obj, u32 da, u32 pa, u32 prot)
537 {
538         u32 *iopgd = iopgd_offset(obj, da);
539
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);
543                 return -EINVAL;
544         }
545
546         *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
547         flush_iopgd_range(iopgd, iopgd);
548         return 0;
549 }
550
551 static int iopgd_alloc_super(struct iommu *obj, u32 da, u32 pa, u32 prot)
552 {
553         u32 *iopgd = iopgd_offset(obj, da);
554         int i;
555
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);
559                 return -EINVAL;
560         }
561
562         for (i = 0; i < 16; i++)
563                 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
564         flush_iopgd_range(iopgd, iopgd + 15);
565         return 0;
566 }
567
568 static int iopte_alloc_page(struct iommu *obj, u32 da, u32 pa, u32 prot)
569 {
570         u32 *iopgd = iopgd_offset(obj, da);
571         u32 *iopte = iopte_alloc(obj, iopgd, da);
572
573         if (IS_ERR(iopte))
574                 return PTR_ERR(iopte);
575
576         *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
577         flush_iopte_range(iopte, iopte);
578
579         dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
580                  __func__, da, pa, iopte, *iopte);
581
582         return 0;
583 }
584
585 static int iopte_alloc_large(struct iommu *obj, u32 da, u32 pa, u32 prot)
586 {
587         u32 *iopgd = iopgd_offset(obj, da);
588         u32 *iopte = iopte_alloc(obj, iopgd, da);
589         int i;
590
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);
594                 return -EINVAL;
595         }
596
597         if (IS_ERR(iopte))
598                 return PTR_ERR(iopte);
599
600         for (i = 0; i < 16; i++)
601                 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
602         flush_iopte_range(iopte, iopte + 15);
603         return 0;
604 }
605
606 static int iopgtable_store_entry_core(struct iommu *obj, struct iotlb_entry *e)
607 {
608         int (*fn)(struct iommu *, u32, u32, u32);
609         u32 prot;
610         int err;
611
612         if (!obj || !e)
613                 return -EINVAL;
614
615         switch (e->pgsz) {
616         case MMU_CAM_PGSZ_16M:
617                 fn = iopgd_alloc_super;
618                 break;
619         case MMU_CAM_PGSZ_1M:
620                 fn = iopgd_alloc_section;
621                 break;
622         case MMU_CAM_PGSZ_64K:
623                 fn = iopte_alloc_large;
624                 break;
625         case MMU_CAM_PGSZ_4K:
626                 fn = iopte_alloc_page;
627                 break;
628         default:
629                 fn = NULL;
630                 BUG();
631                 break;
632         }
633
634         prot = get_iopte_attr(e);
635
636         spin_lock(&obj->page_table_lock);
637         err = fn(obj, e->da, e->pa, prot);
638         spin_unlock(&obj->page_table_lock);
639
640         return err;
641 }
642
643 /**
644  * iopgtable_store_entry - Make an iommu pte entry
645  * @obj:        target iommu
646  * @e:          an iommu tlb entry info
647  **/
648 int iopgtable_store_entry(struct iommu *obj, struct iotlb_entry *e)
649 {
650         int err;
651
652         flush_iotlb_page(obj, e->da);
653         err = iopgtable_store_entry_core(obj, e);
654 #ifdef PREFETCH_IOTLB
655         if (!err)
656                 load_iotlb_entry(obj, e);
657 #endif
658         return err;
659 }
660 EXPORT_SYMBOL_GPL(iopgtable_store_entry);
661
662 /**
663  * iopgtable_lookup_entry - Lookup an iommu pte entry
664  * @obj:        target iommu
665  * @da:         iommu device virtual address
666  * @ppgd:       iommu pgd entry pointer to be returned
667  * @ppte:       iommu pte entry pointer to be returned
668  **/
669 void iopgtable_lookup_entry(struct iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
670 {
671         u32 *iopgd, *iopte = NULL;
672
673         iopgd = iopgd_offset(obj, da);
674         if (!*iopgd)
675                 goto out;
676
677         if (iopgd_is_table(*iopgd))
678                 iopte = iopte_offset(iopgd, da);
679 out:
680         *ppgd = iopgd;
681         *ppte = iopte;
682 }
683 EXPORT_SYMBOL_GPL(iopgtable_lookup_entry);
684
685 static size_t iopgtable_clear_entry_core(struct iommu *obj, u32 da)
686 {
687         size_t bytes;
688         u32 *iopgd = iopgd_offset(obj, da);
689         int nent = 1;
690
691         if (!*iopgd)
692                 return 0;
693
694         if (iopgd_is_table(*iopgd)) {
695                 int i;
696                 u32 *iopte = iopte_offset(iopgd, da);
697
698                 bytes = IOPTE_SIZE;
699                 if (*iopte & IOPTE_LARGE) {
700                         nent *= 16;
701                         /* rewind to the 1st entry */
702                         iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
703                 }
704                 bytes *= nent;
705                 memset(iopte, 0, nent * sizeof(*iopte));
706                 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
707
708                 /*
709                  * do table walk to check if this table is necessary or not
710                  */
711                 iopte = iopte_offset(iopgd, 0);
712                 for (i = 0; i < PTRS_PER_IOPTE; i++)
713                         if (iopte[i])
714                                 goto out;
715
716                 iopte_free(iopte);
717                 nent = 1; /* for the next L1 entry */
718         } else {
719                 bytes = IOPGD_SIZE;
720                 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
721                         nent *= 16;
722                         /* rewind to the 1st entry */
723                         iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
724                 }
725                 bytes *= nent;
726         }
727         memset(iopgd, 0, nent * sizeof(*iopgd));
728         flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
729 out:
730         return bytes;
731 }
732
733 /**
734  * iopgtable_clear_entry - Remove an iommu pte entry
735  * @obj:        target iommu
736  * @da:         iommu device virtual address
737  **/
738 size_t iopgtable_clear_entry(struct iommu *obj, u32 da)
739 {
740         size_t bytes;
741
742         spin_lock(&obj->page_table_lock);
743
744         bytes = iopgtable_clear_entry_core(obj, da);
745         flush_iotlb_page(obj, da);
746
747         spin_unlock(&obj->page_table_lock);
748
749         return bytes;
750 }
751 EXPORT_SYMBOL_GPL(iopgtable_clear_entry);
752
753 static void iopgtable_clear_entry_all(struct iommu *obj)
754 {
755         int i;
756
757         spin_lock(&obj->page_table_lock);
758
759         for (i = 0; i < PTRS_PER_IOPGD; i++) {
760                 u32 da;
761                 u32 *iopgd;
762
763                 da = i << IOPGD_SHIFT;
764                 iopgd = iopgd_offset(obj, da);
765
766                 if (!*iopgd)
767                         continue;
768
769                 if (iopgd_is_table(*iopgd))
770                         iopte_free(iopte_offset(iopgd, 0));
771
772                 *iopgd = 0;
773                 flush_iopgd_range(iopgd, iopgd);
774         }
775
776         flush_iotlb_all(obj);
777
778         spin_unlock(&obj->page_table_lock);
779 }
780
781 /*
782  *      Device IOMMU generic operations
783  */
784 static irqreturn_t iommu_fault_handler(int irq, void *data)
785 {
786         u32 da, errs;
787         u32 *iopgd, *iopte;
788         struct iommu *obj = data;
789
790         if (!obj->refcount)
791                 return IRQ_NONE;
792
793         clk_enable(obj->clk);
794         errs = iommu_report_fault(obj, &da);
795         clk_disable(obj->clk);
796
797         /* Fault callback or TLB/PTE Dynamic loading */
798         if (obj->isr && !obj->isr(obj, da, errs, obj->isr_priv))
799                 return IRQ_HANDLED;
800
801         iommu_disable(obj);
802
803         iopgd = iopgd_offset(obj, da);
804
805         if (!iopgd_is_table(*iopgd)) {
806                 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p "
807                         "*pgd:px%08x\n", obj->name, errs, da, iopgd, *iopgd);
808                 return IRQ_NONE;
809         }
810
811         iopte = iopte_offset(iopgd, da);
812
813         dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x "
814                 "pte:0x%p *pte:0x%08x\n", obj->name, errs, da, iopgd, *iopgd,
815                 iopte, *iopte);
816
817         return IRQ_NONE;
818 }
819
820 static int device_match_by_alias(struct device *dev, void *data)
821 {
822         struct iommu *obj = to_iommu(dev);
823         const char *name = data;
824
825         pr_debug("%s: %s %s\n", __func__, obj->name, name);
826
827         return strcmp(obj->name, name) == 0;
828 }
829
830 /**
831  * iommu_set_da_range - Set a valid device address range
832  * @obj:                target iommu
833  * @start               Start of valid range
834  * @end                 End of valid range
835  **/
836 int iommu_set_da_range(struct iommu *obj, u32 start, u32 end)
837 {
838
839         if (!obj)
840                 return -EFAULT;
841
842         if (end < start || !PAGE_ALIGN(start | end))
843                 return -EINVAL;
844
845         obj->da_start = start;
846         obj->da_end = end;
847
848         return 0;
849 }
850 EXPORT_SYMBOL_GPL(iommu_set_da_range);
851
852 /**
853  * iommu_get - Get iommu handler
854  * @name:       target iommu name
855  **/
856 struct iommu *iommu_get(const char *name)
857 {
858         int err = -ENOMEM;
859         struct device *dev;
860         struct iommu *obj;
861
862         dev = driver_find_device(&omap_iommu_driver.driver, NULL, (void *)name,
863                                  device_match_by_alias);
864         if (!dev)
865                 return ERR_PTR(-ENODEV);
866
867         obj = to_iommu(dev);
868
869         mutex_lock(&obj->iommu_lock);
870
871         if (obj->refcount++ == 0) {
872                 err = iommu_enable(obj);
873                 if (err)
874                         goto err_enable;
875                 flush_iotlb_all(obj);
876         }
877
878         if (!try_module_get(obj->owner))
879                 goto err_module;
880
881         mutex_unlock(&obj->iommu_lock);
882
883         dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
884         return obj;
885
886 err_module:
887         if (obj->refcount == 1)
888                 iommu_disable(obj);
889 err_enable:
890         obj->refcount--;
891         mutex_unlock(&obj->iommu_lock);
892         return ERR_PTR(err);
893 }
894 EXPORT_SYMBOL_GPL(iommu_get);
895
896 /**
897  * iommu_put - Put back iommu handler
898  * @obj:        target iommu
899  **/
900 void iommu_put(struct iommu *obj)
901 {
902         if (!obj || IS_ERR(obj))
903                 return;
904
905         mutex_lock(&obj->iommu_lock);
906
907         if (--obj->refcount == 0)
908                 iommu_disable(obj);
909
910         module_put(obj->owner);
911
912         mutex_unlock(&obj->iommu_lock);
913
914         dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
915 }
916 EXPORT_SYMBOL_GPL(iommu_put);
917
918 int iommu_set_isr(const char *name,
919                   int (*isr)(struct iommu *obj, u32 da, u32 iommu_errs,
920                              void *priv),
921                   void *isr_priv)
922 {
923         struct device *dev;
924         struct iommu *obj;
925
926         dev = driver_find_device(&omap_iommu_driver.driver, NULL, (void *)name,
927                                  device_match_by_alias);
928         if (!dev)
929                 return -ENODEV;
930
931         obj = to_iommu(dev);
932         mutex_lock(&obj->iommu_lock);
933         if (obj->refcount != 0) {
934                 mutex_unlock(&obj->iommu_lock);
935                 return -EBUSY;
936         }
937         obj->isr = isr;
938         obj->isr_priv = isr_priv;
939         mutex_unlock(&obj->iommu_lock);
940
941         return 0;
942 }
943 EXPORT_SYMBOL_GPL(iommu_set_isr);
944
945 /*
946  *      OMAP Device MMU(IOMMU) detection
947  */
948 static int __devinit omap_iommu_probe(struct platform_device *pdev)
949 {
950         int err = -ENODEV;
951         void *p;
952         int irq;
953         struct iommu *obj;
954         struct resource *res;
955         struct iommu_platform_data *pdata = pdev->dev.platform_data;
956
957         if (pdev->num_resources != 2)
958                 return -EINVAL;
959
960         obj = kzalloc(sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
961         if (!obj)
962                 return -ENOMEM;
963
964         obj->clk = clk_get(&pdev->dev, pdata->clk_name);
965         if (IS_ERR(obj->clk))
966                 goto err_clk;
967
968         obj->nr_tlb_entries = pdata->nr_tlb_entries;
969         obj->name = pdata->name;
970         obj->dev = &pdev->dev;
971         obj->ctx = (void *)obj + sizeof(*obj);
972         obj->da_start = pdata->da_start;
973         obj->da_end = pdata->da_end;
974
975         mutex_init(&obj->iommu_lock);
976         mutex_init(&obj->mmap_lock);
977         spin_lock_init(&obj->page_table_lock);
978         INIT_LIST_HEAD(&obj->mmap);
979
980         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
981         if (!res) {
982                 err = -ENODEV;
983                 goto err_mem;
984         }
985
986         res = request_mem_region(res->start, resource_size(res),
987                                  dev_name(&pdev->dev));
988         if (!res) {
989                 err = -EIO;
990                 goto err_mem;
991         }
992
993         obj->regbase = ioremap(res->start, resource_size(res));
994         if (!obj->regbase) {
995                 err = -ENOMEM;
996                 goto err_ioremap;
997         }
998
999         irq = platform_get_irq(pdev, 0);
1000         if (irq < 0) {
1001                 err = -ENODEV;
1002                 goto err_irq;
1003         }
1004         err = request_irq(irq, iommu_fault_handler, IRQF_SHARED,
1005                           dev_name(&pdev->dev), obj);
1006         if (err < 0)
1007                 goto err_irq;
1008         platform_set_drvdata(pdev, obj);
1009
1010         p = (void *)__get_free_pages(GFP_KERNEL, get_order(IOPGD_TABLE_SIZE));
1011         if (!p) {
1012                 err = -ENOMEM;
1013                 goto err_pgd;
1014         }
1015         memset(p, 0, IOPGD_TABLE_SIZE);
1016         clean_dcache_area(p, IOPGD_TABLE_SIZE);
1017         obj->iopgd = p;
1018
1019         BUG_ON(!IS_ALIGNED((unsigned long)obj->iopgd, IOPGD_TABLE_SIZE));
1020
1021         dev_info(&pdev->dev, "%s registered\n", obj->name);
1022         return 0;
1023
1024 err_pgd:
1025         free_irq(irq, obj);
1026 err_irq:
1027         iounmap(obj->regbase);
1028 err_ioremap:
1029         release_mem_region(res->start, resource_size(res));
1030 err_mem:
1031         clk_put(obj->clk);
1032 err_clk:
1033         kfree(obj);
1034         return err;
1035 }
1036
1037 static int __devexit omap_iommu_remove(struct platform_device *pdev)
1038 {
1039         int irq;
1040         struct resource *res;
1041         struct iommu *obj = platform_get_drvdata(pdev);
1042
1043         platform_set_drvdata(pdev, NULL);
1044
1045         iopgtable_clear_entry_all(obj);
1046         free_pages((unsigned long)obj->iopgd, get_order(IOPGD_TABLE_SIZE));
1047
1048         irq = platform_get_irq(pdev, 0);
1049         free_irq(irq, obj);
1050         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1051         release_mem_region(res->start, resource_size(res));
1052         iounmap(obj->regbase);
1053
1054         clk_put(obj->clk);
1055         dev_info(&pdev->dev, "%s removed\n", obj->name);
1056         kfree(obj);
1057         return 0;
1058 }
1059
1060 static struct platform_driver omap_iommu_driver = {
1061         .probe  = omap_iommu_probe,
1062         .remove = __devexit_p(omap_iommu_remove),
1063         .driver = {
1064                 .name   = "omap-iommu",
1065         },
1066 };
1067
1068 static void iopte_cachep_ctor(void *iopte)
1069 {
1070         clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1071 }
1072
1073 static int __init omap_iommu_init(void)
1074 {
1075         struct kmem_cache *p;
1076         const unsigned long flags = SLAB_HWCACHE_ALIGN;
1077         size_t align = 1 << 10; /* L2 pagetable alignement */
1078
1079         p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1080                               iopte_cachep_ctor);
1081         if (!p)
1082                 return -ENOMEM;
1083         iopte_cachep = p;
1084
1085         return platform_driver_register(&omap_iommu_driver);
1086 }
1087 module_init(omap_iommu_init);
1088
1089 static void __exit omap_iommu_exit(void)
1090 {
1091         kmem_cache_destroy(iopte_cachep);
1092
1093         platform_driver_unregister(&omap_iommu_driver);
1094 }
1095 module_exit(omap_iommu_exit);
1096
1097 MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1098 MODULE_ALIAS("platform:omap-iommu");
1099 MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1100 MODULE_LICENSE("GPL v2");