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