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