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