eadcfde757ee78b9f7672786ca54edda47e9270d
[firefly-linux-kernel-4.4.55.git] / drivers / iommu / 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 #include <linux/iommu.h>
22 #include <linux/omap-iommu.h>
23 #include <linux/mutex.h>
24 #include <linux/spinlock.h>
25
26 #include <asm/cacheflush.h>
27
28 #include <plat/iommu.h>
29
30 #include "omap-iopgtable.h"
31
32 #define for_each_iotlb_cr(obj, n, __i, cr)                              \
33         for (__i = 0;                                                   \
34              (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true);   \
35              __i++)
36
37 /* bitmap of the page sizes currently supported */
38 #define OMAP_IOMMU_PGSIZES      (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
39
40 /**
41  * struct omap_iommu_domain - omap iommu domain
42  * @pgtable:    the page table
43  * @iommu_dev:  an omap iommu device attached to this domain. only a single
44  *              iommu device can be attached for now.
45  * @dev:        Device using this domain.
46  * @lock:       domain lock, should be taken when attaching/detaching
47  */
48 struct omap_iommu_domain {
49         u32 *pgtable;
50         struct omap_iommu *iommu_dev;
51         struct device *dev;
52         spinlock_t lock;
53 };
54
55 /* accommodate the difference between omap1 and omap2/3 */
56 static const struct iommu_functions *arch_iommu;
57
58 static struct platform_driver omap_iommu_driver;
59 static struct kmem_cache *iopte_cachep;
60
61 /**
62  * omap_install_iommu_arch - Install archtecure specific iommu functions
63  * @ops:        a pointer to architecture specific iommu functions
64  *
65  * There are several kind of iommu algorithm(tlb, pagetable) among
66  * omap series. This interface installs such an iommu algorighm.
67  **/
68 int omap_install_iommu_arch(const struct iommu_functions *ops)
69 {
70         if (arch_iommu)
71                 return -EBUSY;
72
73         arch_iommu = ops;
74         return 0;
75 }
76 EXPORT_SYMBOL_GPL(omap_install_iommu_arch);
77
78 /**
79  * omap_uninstall_iommu_arch - Uninstall archtecure specific iommu functions
80  * @ops:        a pointer to architecture specific iommu functions
81  *
82  * This interface uninstalls the iommu algorighm installed previously.
83  **/
84 void omap_uninstall_iommu_arch(const struct iommu_functions *ops)
85 {
86         if (arch_iommu != ops)
87                 pr_err("%s: not your arch\n", __func__);
88
89         arch_iommu = NULL;
90 }
91 EXPORT_SYMBOL_GPL(omap_uninstall_iommu_arch);
92
93 /**
94  * omap_iommu_save_ctx - Save registers for pm off-mode support
95  * @dev:        client device
96  **/
97 void omap_iommu_save_ctx(struct device *dev)
98 {
99         struct omap_iommu *obj = dev_to_omap_iommu(dev);
100
101         arch_iommu->save_ctx(obj);
102 }
103 EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
104
105 /**
106  * omap_iommu_restore_ctx - Restore registers for pm off-mode support
107  * @dev:        client device
108  **/
109 void omap_iommu_restore_ctx(struct device *dev)
110 {
111         struct omap_iommu *obj = dev_to_omap_iommu(dev);
112
113         arch_iommu->restore_ctx(obj);
114 }
115 EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
116
117 /**
118  * omap_iommu_arch_version - Return running iommu arch version
119  **/
120 u32 omap_iommu_arch_version(void)
121 {
122         return arch_iommu->version;
123 }
124 EXPORT_SYMBOL_GPL(omap_iommu_arch_version);
125
126 static int iommu_enable(struct omap_iommu *obj)
127 {
128         int err;
129
130         if (!obj)
131                 return -EINVAL;
132
133         if (!arch_iommu)
134                 return -ENODEV;
135
136         clk_enable(obj->clk);
137
138         err = arch_iommu->enable(obj);
139
140         clk_disable(obj->clk);
141         return err;
142 }
143
144 static void iommu_disable(struct omap_iommu *obj)
145 {
146         if (!obj)
147                 return;
148
149         clk_enable(obj->clk);
150
151         arch_iommu->disable(obj);
152
153         clk_disable(obj->clk);
154 }
155
156 /*
157  *      TLB operations
158  */
159 void omap_iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
160 {
161         BUG_ON(!cr || !e);
162
163         arch_iommu->cr_to_e(cr, e);
164 }
165 EXPORT_SYMBOL_GPL(omap_iotlb_cr_to_e);
166
167 static inline int iotlb_cr_valid(struct cr_regs *cr)
168 {
169         if (!cr)
170                 return -EINVAL;
171
172         return arch_iommu->cr_valid(cr);
173 }
174
175 static inline struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
176                                              struct iotlb_entry *e)
177 {
178         if (!e)
179                 return NULL;
180
181         return arch_iommu->alloc_cr(obj, e);
182 }
183
184 static u32 iotlb_cr_to_virt(struct cr_regs *cr)
185 {
186         return arch_iommu->cr_to_virt(cr);
187 }
188
189 static u32 get_iopte_attr(struct iotlb_entry *e)
190 {
191         return arch_iommu->get_pte_attr(e);
192 }
193
194 static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
195 {
196         return arch_iommu->fault_isr(obj, da);
197 }
198
199 static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
200 {
201         u32 val;
202
203         val = iommu_read_reg(obj, MMU_LOCK);
204
205         l->base = MMU_LOCK_BASE(val);
206         l->vict = MMU_LOCK_VICT(val);
207
208 }
209
210 static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
211 {
212         u32 val;
213
214         val = (l->base << MMU_LOCK_BASE_SHIFT);
215         val |= (l->vict << MMU_LOCK_VICT_SHIFT);
216
217         iommu_write_reg(obj, val, MMU_LOCK);
218 }
219
220 static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
221 {
222         arch_iommu->tlb_read_cr(obj, cr);
223 }
224
225 static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
226 {
227         arch_iommu->tlb_load_cr(obj, cr);
228
229         iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
230         iommu_write_reg(obj, 1, MMU_LD_TLB);
231 }
232
233 /**
234  * iotlb_dump_cr - Dump an iommu tlb entry into buf
235  * @obj:        target iommu
236  * @cr:         contents of cam and ram register
237  * @buf:        output buffer
238  **/
239 static inline ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
240                                     char *buf)
241 {
242         BUG_ON(!cr || !buf);
243
244         return arch_iommu->dump_cr(obj, cr, buf);
245 }
246
247 /* only used in iotlb iteration for-loop */
248 static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
249 {
250         struct cr_regs cr;
251         struct iotlb_lock l;
252
253         iotlb_lock_get(obj, &l);
254         l.vict = n;
255         iotlb_lock_set(obj, &l);
256         iotlb_read_cr(obj, &cr);
257
258         return cr;
259 }
260
261 /**
262  * load_iotlb_entry - Set an iommu tlb entry
263  * @obj:        target iommu
264  * @e:          an iommu tlb entry info
265  **/
266 #ifdef PREFETCH_IOTLB
267 static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
268 {
269         int err = 0;
270         struct iotlb_lock l;
271         struct cr_regs *cr;
272
273         if (!obj || !obj->nr_tlb_entries || !e)
274                 return -EINVAL;
275
276         clk_enable(obj->clk);
277
278         iotlb_lock_get(obj, &l);
279         if (l.base == obj->nr_tlb_entries) {
280                 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
281                 err = -EBUSY;
282                 goto out;
283         }
284         if (!e->prsvd) {
285                 int i;
286                 struct cr_regs tmp;
287
288                 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
289                         if (!iotlb_cr_valid(&tmp))
290                                 break;
291
292                 if (i == obj->nr_tlb_entries) {
293                         dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
294                         err = -EBUSY;
295                         goto out;
296                 }
297
298                 iotlb_lock_get(obj, &l);
299         } else {
300                 l.vict = l.base;
301                 iotlb_lock_set(obj, &l);
302         }
303
304         cr = iotlb_alloc_cr(obj, e);
305         if (IS_ERR(cr)) {
306                 clk_disable(obj->clk);
307                 return PTR_ERR(cr);
308         }
309
310         iotlb_load_cr(obj, cr);
311         kfree(cr);
312
313         if (e->prsvd)
314                 l.base++;
315         /* increment victim for next tlb load */
316         if (++l.vict == obj->nr_tlb_entries)
317                 l.vict = l.base;
318         iotlb_lock_set(obj, &l);
319 out:
320         clk_disable(obj->clk);
321         return err;
322 }
323
324 #else /* !PREFETCH_IOTLB */
325
326 static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
327 {
328         return 0;
329 }
330
331 #endif /* !PREFETCH_IOTLB */
332
333 static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
334 {
335         return load_iotlb_entry(obj, e);
336 }
337
338 /**
339  * flush_iotlb_page - Clear an iommu tlb entry
340  * @obj:        target iommu
341  * @da:         iommu device virtual address
342  *
343  * Clear an iommu tlb entry which includes 'da' address.
344  **/
345 static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
346 {
347         int i;
348         struct cr_regs cr;
349
350         clk_enable(obj->clk);
351
352         for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
353                 u32 start;
354                 size_t bytes;
355
356                 if (!iotlb_cr_valid(&cr))
357                         continue;
358
359                 start = iotlb_cr_to_virt(&cr);
360                 bytes = iopgsz_to_bytes(cr.cam & 3);
361
362                 if ((start <= da) && (da < start + bytes)) {
363                         dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
364                                 __func__, start, da, bytes);
365                         iotlb_load_cr(obj, &cr);
366                         iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
367                 }
368         }
369         clk_disable(obj->clk);
370
371         if (i == obj->nr_tlb_entries)
372                 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
373 }
374
375 /**
376  * flush_iotlb_all - Clear all iommu tlb entries
377  * @obj:        target iommu
378  **/
379 static void flush_iotlb_all(struct omap_iommu *obj)
380 {
381         struct iotlb_lock l;
382
383         clk_enable(obj->clk);
384
385         l.base = 0;
386         l.vict = 0;
387         iotlb_lock_set(obj, &l);
388
389         iommu_write_reg(obj, 1, MMU_GFLUSH);
390
391         clk_disable(obj->clk);
392 }
393
394 #if defined(CONFIG_OMAP_IOMMU_DEBUG) || defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
395
396 ssize_t omap_iommu_dump_ctx(struct omap_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(omap_iommu_dump_ctx);
410
411 static int
412 __dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
413 {
414         int i;
415         struct iotlb_lock saved;
416         struct cr_regs tmp;
417         struct cr_regs *p = crs;
418
419         clk_enable(obj->clk);
420         iotlb_lock_get(obj, &saved);
421
422         for_each_iotlb_cr(obj, num, i, tmp) {
423                 if (!iotlb_cr_valid(&tmp))
424                         continue;
425                 *p++ = tmp;
426         }
427
428         iotlb_lock_set(obj, &saved);
429         clk_disable(obj->clk);
430
431         return  p - crs;
432 }
433
434 /**
435  * omap_dump_tlb_entries - dump cr arrays to given buffer
436  * @obj:        target iommu
437  * @buf:        output buffer
438  **/
439 size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
440 {
441         int i, num;
442         struct cr_regs *cr;
443         char *p = buf;
444
445         num = bytes / sizeof(*cr);
446         num = min(obj->nr_tlb_entries, num);
447
448         cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
449         if (!cr)
450                 return 0;
451
452         num = __dump_tlb_entries(obj, cr, num);
453         for (i = 0; i < num; i++)
454                 p += iotlb_dump_cr(obj, cr + i, p);
455         kfree(cr);
456
457         return p - buf;
458 }
459 EXPORT_SYMBOL_GPL(omap_dump_tlb_entries);
460
461 int omap_foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
462 {
463         return driver_for_each_device(&omap_iommu_driver.driver,
464                                       NULL, data, fn);
465 }
466 EXPORT_SYMBOL_GPL(omap_foreach_iommu_device);
467
468 #endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
469
470 /*
471  *      H/W pagetable operations
472  */
473 static void flush_iopgd_range(u32 *first, u32 *last)
474 {
475         /* FIXME: L2 cache should be taken care of if it exists */
476         do {
477                 asm("mcr        p15, 0, %0, c7, c10, 1 @ flush_pgd"
478                     : : "r" (first));
479                 first += L1_CACHE_BYTES / sizeof(*first);
480         } while (first <= last);
481 }
482
483 static void flush_iopte_range(u32 *first, u32 *last)
484 {
485         /* FIXME: L2 cache should be taken care of if it exists */
486         do {
487                 asm("mcr        p15, 0, %0, c7, c10, 1 @ flush_pte"
488                     : : "r" (first));
489                 first += L1_CACHE_BYTES / sizeof(*first);
490         } while (first <= last);
491 }
492
493 static void iopte_free(u32 *iopte)
494 {
495         /* Note: freed iopte's must be clean ready for re-use */
496         kmem_cache_free(iopte_cachep, iopte);
497 }
498
499 static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
500 {
501         u32 *iopte;
502
503         /* a table has already existed */
504         if (*iopgd)
505                 goto pte_ready;
506
507         /*
508          * do the allocation outside the page table lock
509          */
510         spin_unlock(&obj->page_table_lock);
511         iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
512         spin_lock(&obj->page_table_lock);
513
514         if (!*iopgd) {
515                 if (!iopte)
516                         return ERR_PTR(-ENOMEM);
517
518                 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
519                 flush_iopgd_range(iopgd, iopgd);
520
521                 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
522         } else {
523                 /* We raced, free the reduniovant table */
524                 iopte_free(iopte);
525         }
526
527 pte_ready:
528         iopte = iopte_offset(iopgd, da);
529
530         dev_vdbg(obj->dev,
531                  "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
532                  __func__, da, iopgd, *iopgd, iopte, *iopte);
533
534         return iopte;
535 }
536
537 static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
538 {
539         u32 *iopgd = iopgd_offset(obj, da);
540
541         if ((da | pa) & ~IOSECTION_MASK) {
542                 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
543                         __func__, da, pa, IOSECTION_SIZE);
544                 return -EINVAL;
545         }
546
547         *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
548         flush_iopgd_range(iopgd, iopgd);
549         return 0;
550 }
551
552 static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
553 {
554         u32 *iopgd = iopgd_offset(obj, da);
555         int i;
556
557         if ((da | pa) & ~IOSUPER_MASK) {
558                 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
559                         __func__, da, pa, IOSUPER_SIZE);
560                 return -EINVAL;
561         }
562
563         for (i = 0; i < 16; i++)
564                 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
565         flush_iopgd_range(iopgd, iopgd + 15);
566         return 0;
567 }
568
569 static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
570 {
571         u32 *iopgd = iopgd_offset(obj, da);
572         u32 *iopte = iopte_alloc(obj, iopgd, da);
573
574         if (IS_ERR(iopte))
575                 return PTR_ERR(iopte);
576
577         *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
578         flush_iopte_range(iopte, iopte);
579
580         dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
581                  __func__, da, pa, iopte, *iopte);
582
583         return 0;
584 }
585
586 static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
587 {
588         u32 *iopgd = iopgd_offset(obj, da);
589         u32 *iopte = iopte_alloc(obj, iopgd, da);
590         int i;
591
592         if ((da | pa) & ~IOLARGE_MASK) {
593                 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
594                         __func__, da, pa, IOLARGE_SIZE);
595                 return -EINVAL;
596         }
597
598         if (IS_ERR(iopte))
599                 return PTR_ERR(iopte);
600
601         for (i = 0; i < 16; i++)
602                 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
603         flush_iopte_range(iopte, iopte + 15);
604         return 0;
605 }
606
607 static int
608 iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
609 {
610         int (*fn)(struct omap_iommu *, u32, u32, u32);
611         u32 prot;
612         int err;
613
614         if (!obj || !e)
615                 return -EINVAL;
616
617         switch (e->pgsz) {
618         case MMU_CAM_PGSZ_16M:
619                 fn = iopgd_alloc_super;
620                 break;
621         case MMU_CAM_PGSZ_1M:
622                 fn = iopgd_alloc_section;
623                 break;
624         case MMU_CAM_PGSZ_64K:
625                 fn = iopte_alloc_large;
626                 break;
627         case MMU_CAM_PGSZ_4K:
628                 fn = iopte_alloc_page;
629                 break;
630         default:
631                 fn = NULL;
632                 BUG();
633                 break;
634         }
635
636         prot = get_iopte_attr(e);
637
638         spin_lock(&obj->page_table_lock);
639         err = fn(obj, e->da, e->pa, prot);
640         spin_unlock(&obj->page_table_lock);
641
642         return err;
643 }
644
645 /**
646  * omap_iopgtable_store_entry - Make an iommu pte entry
647  * @obj:        target iommu
648  * @e:          an iommu tlb entry info
649  **/
650 int omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
651 {
652         int err;
653
654         flush_iotlb_page(obj, e->da);
655         err = iopgtable_store_entry_core(obj, e);
656         if (!err)
657                 prefetch_iotlb_entry(obj, e);
658         return err;
659 }
660 EXPORT_SYMBOL_GPL(omap_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 static void
670 iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
671 {
672         u32 *iopgd, *iopte = NULL;
673
674         iopgd = iopgd_offset(obj, da);
675         if (!*iopgd)
676                 goto out;
677
678         if (iopgd_is_table(*iopgd))
679                 iopte = iopte_offset(iopgd, da);
680 out:
681         *ppgd = iopgd;
682         *ppte = iopte;
683 }
684
685 static size_t iopgtable_clear_entry_core(struct omap_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 static size_t iopgtable_clear_entry(struct omap_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
752 static void iopgtable_clear_entry_all(struct omap_iommu *obj)
753 {
754         int i;
755
756         spin_lock(&obj->page_table_lock);
757
758         for (i = 0; i < PTRS_PER_IOPGD; i++) {
759                 u32 da;
760                 u32 *iopgd;
761
762                 da = i << IOPGD_SHIFT;
763                 iopgd = iopgd_offset(obj, da);
764
765                 if (!*iopgd)
766                         continue;
767
768                 if (iopgd_is_table(*iopgd))
769                         iopte_free(iopte_offset(iopgd, 0));
770
771                 *iopgd = 0;
772                 flush_iopgd_range(iopgd, iopgd);
773         }
774
775         flush_iotlb_all(obj);
776
777         spin_unlock(&obj->page_table_lock);
778 }
779
780 /*
781  *      Device IOMMU generic operations
782  */
783 static irqreturn_t iommu_fault_handler(int irq, void *data)
784 {
785         u32 da, errs;
786         u32 *iopgd, *iopte;
787         struct omap_iommu *obj = data;
788         struct iommu_domain *domain = obj->domain;
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         if (errs == 0)
797                 return IRQ_HANDLED;
798
799         /* Fault callback or TLB/PTE Dynamic loading */
800         if (!report_iommu_fault(domain, obj->dev, da, 0))
801                 return IRQ_HANDLED;
802
803         iommu_disable(obj);
804
805         iopgd = iopgd_offset(obj, da);
806
807         if (!iopgd_is_table(*iopgd)) {
808                 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p "
809                         "*pgd:px%08x\n", obj->name, errs, da, iopgd, *iopgd);
810                 return IRQ_NONE;
811         }
812
813         iopte = iopte_offset(iopgd, da);
814
815         dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x "
816                 "pte:0x%p *pte:0x%08x\n", obj->name, errs, da, iopgd, *iopgd,
817                 iopte, *iopte);
818
819         return IRQ_NONE;
820 }
821
822 static int device_match_by_alias(struct device *dev, void *data)
823 {
824         struct omap_iommu *obj = to_iommu(dev);
825         const char *name = data;
826
827         pr_debug("%s: %s %s\n", __func__, obj->name, name);
828
829         return strcmp(obj->name, name) == 0;
830 }
831
832 /**
833  * omap_iommu_attach() - attach iommu device to an iommu domain
834  * @name:       name of target omap iommu device
835  * @iopgd:      page table
836  **/
837 static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
838 {
839         int err = -ENOMEM;
840         struct device *dev;
841         struct omap_iommu *obj;
842
843         dev = driver_find_device(&omap_iommu_driver.driver, NULL,
844                                 (void *)name,
845                                 device_match_by_alias);
846         if (!dev)
847                 return NULL;
848
849         obj = to_iommu(dev);
850
851         spin_lock(&obj->iommu_lock);
852
853         /* an iommu device can only be attached once */
854         if (++obj->refcount > 1) {
855                 dev_err(dev, "%s: already attached!\n", obj->name);
856                 err = -EBUSY;
857                 goto err_enable;
858         }
859
860         obj->iopgd = iopgd;
861         err = iommu_enable(obj);
862         if (err)
863                 goto err_enable;
864         flush_iotlb_all(obj);
865
866         if (!try_module_get(obj->owner))
867                 goto err_module;
868
869         spin_unlock(&obj->iommu_lock);
870
871         dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
872         return obj;
873
874 err_module:
875         if (obj->refcount == 1)
876                 iommu_disable(obj);
877 err_enable:
878         obj->refcount--;
879         spin_unlock(&obj->iommu_lock);
880         return ERR_PTR(err);
881 }
882
883 /**
884  * omap_iommu_detach - release iommu device
885  * @obj:        target iommu
886  **/
887 static void omap_iommu_detach(struct omap_iommu *obj)
888 {
889         if (!obj || IS_ERR(obj))
890                 return;
891
892         spin_lock(&obj->iommu_lock);
893
894         if (--obj->refcount == 0)
895                 iommu_disable(obj);
896
897         module_put(obj->owner);
898
899         obj->iopgd = NULL;
900
901         spin_unlock(&obj->iommu_lock);
902
903         dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
904 }
905
906 /*
907  *      OMAP Device MMU(IOMMU) detection
908  */
909 static int __devinit omap_iommu_probe(struct platform_device *pdev)
910 {
911         int err = -ENODEV;
912         int irq;
913         struct omap_iommu *obj;
914         struct resource *res;
915         struct iommu_platform_data *pdata = pdev->dev.platform_data;
916
917         if (pdev->num_resources != 2)
918                 return -EINVAL;
919
920         obj = kzalloc(sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
921         if (!obj)
922                 return -ENOMEM;
923
924         obj->clk = clk_get(&pdev->dev, pdata->clk_name);
925         if (IS_ERR(obj->clk))
926                 goto err_clk;
927
928         obj->nr_tlb_entries = pdata->nr_tlb_entries;
929         obj->name = pdata->name;
930         obj->dev = &pdev->dev;
931         obj->ctx = (void *)obj + sizeof(*obj);
932         obj->da_start = pdata->da_start;
933         obj->da_end = pdata->da_end;
934
935         spin_lock_init(&obj->iommu_lock);
936         mutex_init(&obj->mmap_lock);
937         spin_lock_init(&obj->page_table_lock);
938         INIT_LIST_HEAD(&obj->mmap);
939
940         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
941         if (!res) {
942                 err = -ENODEV;
943                 goto err_mem;
944         }
945
946         res = request_mem_region(res->start, resource_size(res),
947                                  dev_name(&pdev->dev));
948         if (!res) {
949                 err = -EIO;
950                 goto err_mem;
951         }
952
953         obj->regbase = ioremap(res->start, resource_size(res));
954         if (!obj->regbase) {
955                 err = -ENOMEM;
956                 goto err_ioremap;
957         }
958
959         irq = platform_get_irq(pdev, 0);
960         if (irq < 0) {
961                 err = -ENODEV;
962                 goto err_irq;
963         }
964         err = request_irq(irq, iommu_fault_handler, IRQF_SHARED,
965                           dev_name(&pdev->dev), obj);
966         if (err < 0)
967                 goto err_irq;
968         platform_set_drvdata(pdev, obj);
969
970         dev_info(&pdev->dev, "%s registered\n", obj->name);
971         return 0;
972
973 err_irq:
974         iounmap(obj->regbase);
975 err_ioremap:
976         release_mem_region(res->start, resource_size(res));
977 err_mem:
978         clk_put(obj->clk);
979 err_clk:
980         kfree(obj);
981         return err;
982 }
983
984 static int __devexit omap_iommu_remove(struct platform_device *pdev)
985 {
986         int irq;
987         struct resource *res;
988         struct omap_iommu *obj = platform_get_drvdata(pdev);
989
990         platform_set_drvdata(pdev, NULL);
991
992         iopgtable_clear_entry_all(obj);
993
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);
999
1000         clk_put(obj->clk);
1001         dev_info(&pdev->dev, "%s removed\n", obj->name);
1002         kfree(obj);
1003         return 0;
1004 }
1005
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",
1011         },
1012 };
1013
1014 static void iopte_cachep_ctor(void *iopte)
1015 {
1016         clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1017 }
1018
1019 static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
1020                          phys_addr_t pa, size_t bytes, int prot)
1021 {
1022         struct omap_iommu_domain *omap_domain = domain->priv;
1023         struct omap_iommu *oiommu = omap_domain->iommu_dev;
1024         struct device *dev = oiommu->dev;
1025         struct iotlb_entry e;
1026         int omap_pgsz;
1027         u32 ret, flags;
1028
1029         /* we only support mapping a single iommu page for now */
1030         omap_pgsz = bytes_to_iopgsz(bytes);
1031         if (omap_pgsz < 0) {
1032                 dev_err(dev, "invalid size to map: %d\n", bytes);
1033                 return -EINVAL;
1034         }
1035
1036         dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes);
1037
1038         flags = omap_pgsz | prot;
1039
1040         iotlb_init_entry(&e, da, pa, flags);
1041
1042         ret = omap_iopgtable_store_entry(oiommu, &e);
1043         if (ret)
1044                 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
1045
1046         return ret;
1047 }
1048
1049 static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1050                             size_t size)
1051 {
1052         struct omap_iommu_domain *omap_domain = domain->priv;
1053         struct omap_iommu *oiommu = omap_domain->iommu_dev;
1054         struct device *dev = oiommu->dev;
1055
1056         dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
1057
1058         return iopgtable_clear_entry(oiommu, da);
1059 }
1060
1061 static int
1062 omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1063 {
1064         struct omap_iommu_domain *omap_domain = domain->priv;
1065         struct omap_iommu *oiommu;
1066         struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1067         int ret = 0;
1068
1069         spin_lock(&omap_domain->lock);
1070
1071         /* only a single device is supported per domain for now */
1072         if (omap_domain->iommu_dev) {
1073                 dev_err(dev, "iommu domain is already attached\n");
1074                 ret = -EBUSY;
1075                 goto out;
1076         }
1077
1078         /* get a handle to and enable the omap iommu */
1079         oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
1080         if (IS_ERR(oiommu)) {
1081                 ret = PTR_ERR(oiommu);
1082                 dev_err(dev, "can't get omap iommu: %d\n", ret);
1083                 goto out;
1084         }
1085
1086         omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
1087         omap_domain->dev = dev;
1088         oiommu->domain = domain;
1089
1090 out:
1091         spin_unlock(&omap_domain->lock);
1092         return ret;
1093 }
1094
1095 static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
1096                         struct device *dev)
1097 {
1098         struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
1099         struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1100
1101         /* only a single device is supported per domain for now */
1102         if (omap_domain->iommu_dev != oiommu) {
1103                 dev_err(dev, "invalid iommu device\n");
1104                 return;
1105         }
1106
1107         iopgtable_clear_entry_all(oiommu);
1108
1109         omap_iommu_detach(oiommu);
1110
1111         omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
1112         omap_domain->dev = NULL;
1113 }
1114
1115 static void omap_iommu_detach_dev(struct iommu_domain *domain,
1116                                  struct device *dev)
1117 {
1118         struct omap_iommu_domain *omap_domain = domain->priv;
1119
1120         spin_lock(&omap_domain->lock);
1121         _omap_iommu_detach_dev(omap_domain, dev);
1122         spin_unlock(&omap_domain->lock);
1123 }
1124
1125 static int omap_iommu_domain_init(struct iommu_domain *domain)
1126 {
1127         struct omap_iommu_domain *omap_domain;
1128
1129         omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1130         if (!omap_domain) {
1131                 pr_err("kzalloc failed\n");
1132                 goto out;
1133         }
1134
1135         omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1136         if (!omap_domain->pgtable) {
1137                 pr_err("kzalloc failed\n");
1138                 goto fail_nomem;
1139         }
1140
1141         /*
1142          * should never fail, but please keep this around to ensure
1143          * we keep the hardware happy
1144          */
1145         BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
1146
1147         clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1148         spin_lock_init(&omap_domain->lock);
1149
1150         domain->priv = omap_domain;
1151
1152         domain->geometry.aperture_start = 0;
1153         domain->geometry.aperture_end   = (1ULL << 32) - 1;
1154         domain->geometry.force_aperture = true;
1155
1156         return 0;
1157
1158 fail_nomem:
1159         kfree(omap_domain);
1160 out:
1161         return -ENOMEM;
1162 }
1163
1164 static void omap_iommu_domain_destroy(struct iommu_domain *domain)
1165 {
1166         struct omap_iommu_domain *omap_domain = domain->priv;
1167
1168         domain->priv = NULL;
1169
1170         /*
1171          * An iommu device is still attached
1172          * (currently, only one device can be attached) ?
1173          */
1174         if (omap_domain->iommu_dev)
1175                 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1176
1177         kfree(omap_domain->pgtable);
1178         kfree(omap_domain);
1179 }
1180
1181 static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
1182                                           unsigned long da)
1183 {
1184         struct omap_iommu_domain *omap_domain = domain->priv;
1185         struct omap_iommu *oiommu = omap_domain->iommu_dev;
1186         struct device *dev = oiommu->dev;
1187         u32 *pgd, *pte;
1188         phys_addr_t ret = 0;
1189
1190         iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1191
1192         if (pte) {
1193                 if (iopte_is_small(*pte))
1194                         ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1195                 else if (iopte_is_large(*pte))
1196                         ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1197                 else
1198                         dev_err(dev, "bogus pte 0x%x, da 0x%lx", *pte, da);
1199         } else {
1200                 if (iopgd_is_section(*pgd))
1201                         ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1202                 else if (iopgd_is_super(*pgd))
1203                         ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1204                 else
1205                         dev_err(dev, "bogus pgd 0x%x, da 0x%lx", *pgd, da);
1206         }
1207
1208         return ret;
1209 }
1210
1211 static int omap_iommu_domain_has_cap(struct iommu_domain *domain,
1212                                     unsigned long cap)
1213 {
1214         return 0;
1215 }
1216
1217 static struct iommu_ops omap_iommu_ops = {
1218         .domain_init    = omap_iommu_domain_init,
1219         .domain_destroy = omap_iommu_domain_destroy,
1220         .attach_dev     = omap_iommu_attach_dev,
1221         .detach_dev     = omap_iommu_detach_dev,
1222         .map            = omap_iommu_map,
1223         .unmap          = omap_iommu_unmap,
1224         .iova_to_phys   = omap_iommu_iova_to_phys,
1225         .domain_has_cap = omap_iommu_domain_has_cap,
1226         .pgsize_bitmap  = OMAP_IOMMU_PGSIZES,
1227 };
1228
1229 static int __init omap_iommu_init(void)
1230 {
1231         struct kmem_cache *p;
1232         const unsigned long flags = SLAB_HWCACHE_ALIGN;
1233         size_t align = 1 << 10; /* L2 pagetable alignement */
1234
1235         p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1236                               iopte_cachep_ctor);
1237         if (!p)
1238                 return -ENOMEM;
1239         iopte_cachep = p;
1240
1241         bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
1242
1243         return platform_driver_register(&omap_iommu_driver);
1244 }
1245 /* must be ready before omap3isp is probed */
1246 subsys_initcall(omap_iommu_init);
1247
1248 static void __exit omap_iommu_exit(void)
1249 {
1250         kmem_cache_destroy(iopte_cachep);
1251
1252         platform_driver_unregister(&omap_iommu_driver);
1253 }
1254 module_exit(omap_iommu_exit);
1255
1256 MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1257 MODULE_ALIAS("platform:omap-iommu");
1258 MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1259 MODULE_LICENSE("GPL v2");