iommu/omap: Move to_iommu definition from omap-iopgtable.h
[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
HD
396 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
397 }
398 }
ebf7cda0 399 pm_runtime_put_sync(obj->dev);
a9dcad5e
HD
400
401 if (i == obj->nr_tlb_entries)
402 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
403}
a9dcad5e
HD
404
405/**
406 * flush_iotlb_all - Clear all iommu tlb entries
407 * @obj: target iommu
408 **/
6c32df43 409static void flush_iotlb_all(struct omap_iommu *obj)
a9dcad5e
HD
410{
411 struct iotlb_lock l;
412
ebf7cda0 413 pm_runtime_get_sync(obj->dev);
a9dcad5e
HD
414
415 l.base = 0;
416 l.vict = 0;
417 iotlb_lock_set(obj, &l);
418
419 iommu_write_reg(obj, 1, MMU_GFLUSH);
420
ebf7cda0 421 pm_runtime_put_sync(obj->dev);
a9dcad5e 422}
ddfa975a 423
e4efd94b 424#if defined(CONFIG_OMAP_IOMMU_DEBUG) || defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
a9dcad5e 425
6c32df43 426ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes)
a9dcad5e 427{
a9dcad5e
HD
428 if (!obj || !buf)
429 return -EINVAL;
430
ebf7cda0 431 pm_runtime_get_sync(obj->dev);
a9dcad5e 432
14e0e679 433 bytes = arch_iommu->dump_ctx(obj, buf, bytes);
a9dcad5e 434
ebf7cda0 435 pm_runtime_put_sync(obj->dev);
a9dcad5e
HD
436
437 return bytes;
438}
6c32df43 439EXPORT_SYMBOL_GPL(omap_iommu_dump_ctx);
a9dcad5e 440
6c32df43
OBC
441static int
442__dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
a9dcad5e
HD
443{
444 int i;
37c2836c
HD
445 struct iotlb_lock saved;
446 struct cr_regs tmp;
a9dcad5e
HD
447 struct cr_regs *p = crs;
448
ebf7cda0 449 pm_runtime_get_sync(obj->dev);
a9dcad5e 450 iotlb_lock_get(obj, &saved);
a9dcad5e 451
37c2836c 452 for_each_iotlb_cr(obj, num, i, tmp) {
a9dcad5e
HD
453 if (!iotlb_cr_valid(&tmp))
454 continue;
a9dcad5e
HD
455 *p++ = tmp;
456 }
37c2836c 457
a9dcad5e 458 iotlb_lock_set(obj, &saved);
ebf7cda0 459 pm_runtime_put_sync(obj->dev);
a9dcad5e
HD
460
461 return p - crs;
462}
463
464/**
6c32df43 465 * omap_dump_tlb_entries - dump cr arrays to given buffer
a9dcad5e
HD
466 * @obj: target iommu
467 * @buf: output buffer
468 **/
6c32df43 469size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
a9dcad5e 470{
14e0e679 471 int i, num;
a9dcad5e
HD
472 struct cr_regs *cr;
473 char *p = buf;
474
14e0e679
HD
475 num = bytes / sizeof(*cr);
476 num = min(obj->nr_tlb_entries, num);
477
478 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
a9dcad5e
HD
479 if (!cr)
480 return 0;
481
14e0e679
HD
482 num = __dump_tlb_entries(obj, cr, num);
483 for (i = 0; i < num; i++)
a9dcad5e
HD
484 p += iotlb_dump_cr(obj, cr + i, p);
485 kfree(cr);
486
487 return p - buf;
488}
6c32df43 489EXPORT_SYMBOL_GPL(omap_dump_tlb_entries);
a9dcad5e 490
6c32df43 491int omap_foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
a9dcad5e
HD
492{
493 return driver_for_each_device(&omap_iommu_driver.driver,
494 NULL, data, fn);
495}
6c32df43 496EXPORT_SYMBOL_GPL(omap_foreach_iommu_device);
a9dcad5e
HD
497
498#endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
499
500/*
501 * H/W pagetable operations
502 */
503static void flush_iopgd_range(u32 *first, u32 *last)
504{
505 /* FIXME: L2 cache should be taken care of if it exists */
506 do {
507 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
508 : : "r" (first));
509 first += L1_CACHE_BYTES / sizeof(*first);
510 } while (first <= last);
511}
512
513static void flush_iopte_range(u32 *first, u32 *last)
514{
515 /* FIXME: L2 cache should be taken care of if it exists */
516 do {
517 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
518 : : "r" (first));
519 first += L1_CACHE_BYTES / sizeof(*first);
520 } while (first <= last);
521}
522
523static void iopte_free(u32 *iopte)
524{
525 /* Note: freed iopte's must be clean ready for re-use */
e28045ab
ZZ
526 if (iopte)
527 kmem_cache_free(iopte_cachep, iopte);
a9dcad5e
HD
528}
529
6c32df43 530static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
a9dcad5e
HD
531{
532 u32 *iopte;
533
534 /* a table has already existed */
535 if (*iopgd)
536 goto pte_ready;
537
538 /*
539 * do the allocation outside the page table lock
540 */
541 spin_unlock(&obj->page_table_lock);
542 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
543 spin_lock(&obj->page_table_lock);
544
545 if (!*iopgd) {
546 if (!iopte)
547 return ERR_PTR(-ENOMEM);
548
549 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
550 flush_iopgd_range(iopgd, iopgd);
551
552 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
553 } else {
554 /* We raced, free the reduniovant table */
555 iopte_free(iopte);
556 }
557
558pte_ready:
559 iopte = iopte_offset(iopgd, da);
560
561 dev_vdbg(obj->dev,
562 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
563 __func__, da, iopgd, *iopgd, iopte, *iopte);
564
565 return iopte;
566}
567
6c32df43 568static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
a9dcad5e
HD
569{
570 u32 *iopgd = iopgd_offset(obj, da);
571
4abb7617
HD
572 if ((da | pa) & ~IOSECTION_MASK) {
573 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
574 __func__, da, pa, IOSECTION_SIZE);
575 return -EINVAL;
576 }
577
a9dcad5e
HD
578 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
579 flush_iopgd_range(iopgd, iopgd);
580 return 0;
581}
582
6c32df43 583static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
a9dcad5e
HD
584{
585 u32 *iopgd = iopgd_offset(obj, da);
586 int i;
587
4abb7617
HD
588 if ((da | pa) & ~IOSUPER_MASK) {
589 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
590 __func__, da, pa, IOSUPER_SIZE);
591 return -EINVAL;
592 }
593
a9dcad5e
HD
594 for (i = 0; i < 16; i++)
595 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
596 flush_iopgd_range(iopgd, iopgd + 15);
597 return 0;
598}
599
6c32df43 600static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
a9dcad5e
HD
601{
602 u32 *iopgd = iopgd_offset(obj, da);
603 u32 *iopte = iopte_alloc(obj, iopgd, da);
604
605 if (IS_ERR(iopte))
606 return PTR_ERR(iopte);
607
608 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
609 flush_iopte_range(iopte, iopte);
610
611 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
612 __func__, da, pa, iopte, *iopte);
613
614 return 0;
615}
616
6c32df43 617static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
a9dcad5e
HD
618{
619 u32 *iopgd = iopgd_offset(obj, da);
620 u32 *iopte = iopte_alloc(obj, iopgd, da);
621 int i;
622
4abb7617
HD
623 if ((da | pa) & ~IOLARGE_MASK) {
624 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
625 __func__, da, pa, IOLARGE_SIZE);
626 return -EINVAL;
627 }
628
a9dcad5e
HD
629 if (IS_ERR(iopte))
630 return PTR_ERR(iopte);
631
632 for (i = 0; i < 16; i++)
633 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
634 flush_iopte_range(iopte, iopte + 15);
635 return 0;
636}
637
6c32df43
OBC
638static int
639iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
a9dcad5e 640{
6c32df43 641 int (*fn)(struct omap_iommu *, u32, u32, u32);
a9dcad5e
HD
642 u32 prot;
643 int err;
644
645 if (!obj || !e)
646 return -EINVAL;
647
648 switch (e->pgsz) {
649 case MMU_CAM_PGSZ_16M:
650 fn = iopgd_alloc_super;
651 break;
652 case MMU_CAM_PGSZ_1M:
653 fn = iopgd_alloc_section;
654 break;
655 case MMU_CAM_PGSZ_64K:
656 fn = iopte_alloc_large;
657 break;
658 case MMU_CAM_PGSZ_4K:
659 fn = iopte_alloc_page;
660 break;
661 default:
662 fn = NULL;
663 BUG();
664 break;
665 }
666
667 prot = get_iopte_attr(e);
668
669 spin_lock(&obj->page_table_lock);
670 err = fn(obj, e->da, e->pa, prot);
671 spin_unlock(&obj->page_table_lock);
672
673 return err;
674}
675
676/**
6c32df43 677 * omap_iopgtable_store_entry - Make an iommu pte entry
a9dcad5e
HD
678 * @obj: target iommu
679 * @e: an iommu tlb entry info
680 **/
6c32df43 681int omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
a9dcad5e
HD
682{
683 int err;
684
685 flush_iotlb_page(obj, e->da);
686 err = iopgtable_store_entry_core(obj, e);
a9dcad5e 687 if (!err)
5da14a47 688 prefetch_iotlb_entry(obj, e);
a9dcad5e
HD
689 return err;
690}
6c32df43 691EXPORT_SYMBOL_GPL(omap_iopgtable_store_entry);
a9dcad5e
HD
692
693/**
694 * iopgtable_lookup_entry - Lookup an iommu pte entry
695 * @obj: target iommu
696 * @da: iommu device virtual address
697 * @ppgd: iommu pgd entry pointer to be returned
698 * @ppte: iommu pte entry pointer to be returned
699 **/
e1f23813
OBC
700static void
701iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
a9dcad5e
HD
702{
703 u32 *iopgd, *iopte = NULL;
704
705 iopgd = iopgd_offset(obj, da);
706 if (!*iopgd)
707 goto out;
708
a1a54456 709 if (iopgd_is_table(*iopgd))
a9dcad5e
HD
710 iopte = iopte_offset(iopgd, da);
711out:
712 *ppgd = iopgd;
713 *ppte = iopte;
714}
a9dcad5e 715
6c32df43 716static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
a9dcad5e
HD
717{
718 size_t bytes;
719 u32 *iopgd = iopgd_offset(obj, da);
720 int nent = 1;
721
722 if (!*iopgd)
723 return 0;
724
a1a54456 725 if (iopgd_is_table(*iopgd)) {
a9dcad5e
HD
726 int i;
727 u32 *iopte = iopte_offset(iopgd, da);
728
729 bytes = IOPTE_SIZE;
730 if (*iopte & IOPTE_LARGE) {
731 nent *= 16;
732 /* rewind to the 1st entry */
c127c7dc 733 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
a9dcad5e
HD
734 }
735 bytes *= nent;
736 memset(iopte, 0, nent * sizeof(*iopte));
737 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
738
739 /*
740 * do table walk to check if this table is necessary or not
741 */
742 iopte = iopte_offset(iopgd, 0);
743 for (i = 0; i < PTRS_PER_IOPTE; i++)
744 if (iopte[i])
745 goto out;
746
747 iopte_free(iopte);
748 nent = 1; /* for the next L1 entry */
749 } else {
750 bytes = IOPGD_SIZE;
dcc730dc 751 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
a9dcad5e
HD
752 nent *= 16;
753 /* rewind to the 1st entry */
8d33ea58 754 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
a9dcad5e
HD
755 }
756 bytes *= nent;
757 }
758 memset(iopgd, 0, nent * sizeof(*iopgd));
759 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
760out:
761 return bytes;
762}
763
764/**
765 * iopgtable_clear_entry - Remove an iommu pte entry
766 * @obj: target iommu
767 * @da: iommu device virtual address
768 **/
6c32df43 769static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
a9dcad5e
HD
770{
771 size_t bytes;
772
773 spin_lock(&obj->page_table_lock);
774
775 bytes = iopgtable_clear_entry_core(obj, da);
776 flush_iotlb_page(obj, da);
777
778 spin_unlock(&obj->page_table_lock);
779
780 return bytes;
781}
a9dcad5e 782
6c32df43 783static void iopgtable_clear_entry_all(struct omap_iommu *obj)
a9dcad5e
HD
784{
785 int i;
786
787 spin_lock(&obj->page_table_lock);
788
789 for (i = 0; i < PTRS_PER_IOPGD; i++) {
790 u32 da;
791 u32 *iopgd;
792
793 da = i << IOPGD_SHIFT;
794 iopgd = iopgd_offset(obj, da);
795
796 if (!*iopgd)
797 continue;
798
a1a54456 799 if (iopgd_is_table(*iopgd))
a9dcad5e
HD
800 iopte_free(iopte_offset(iopgd, 0));
801
802 *iopgd = 0;
803 flush_iopgd_range(iopgd, iopgd);
804 }
805
806 flush_iotlb_all(obj);
807
808 spin_unlock(&obj->page_table_lock);
809}
810
811/*
812 * Device IOMMU generic operations
813 */
814static irqreturn_t iommu_fault_handler(int irq, void *data)
815{
d594f1f3 816 u32 da, errs;
a9dcad5e 817 u32 *iopgd, *iopte;
6c32df43 818 struct omap_iommu *obj = data;
e7f10f02 819 struct iommu_domain *domain = obj->domain;
a9dcad5e
HD
820
821 if (!obj->refcount)
822 return IRQ_NONE;
823
d594f1f3 824 errs = iommu_report_fault(obj, &da);
c56b2ddd
LP
825 if (errs == 0)
826 return IRQ_HANDLED;
d594f1f3
DC
827
828 /* Fault callback or TLB/PTE Dynamic loading */
e7f10f02 829 if (!report_iommu_fault(domain, obj->dev, da, 0))
a9dcad5e
HD
830 return IRQ_HANDLED;
831
37b29810
HD
832 iommu_disable(obj);
833
a9dcad5e
HD
834 iopgd = iopgd_offset(obj, da);
835
a1a54456 836 if (!iopgd_is_table(*iopgd)) {
b6c2e09f
SA
837 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
838 obj->name, errs, da, iopgd, *iopgd);
a9dcad5e
HD
839 return IRQ_NONE;
840 }
841
842 iopte = iopte_offset(iopgd, da);
843
b6c2e09f
SA
844 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
845 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
a9dcad5e
HD
846
847 return IRQ_NONE;
848}
849
850static int device_match_by_alias(struct device *dev, void *data)
851{
6c32df43 852 struct omap_iommu *obj = to_iommu(dev);
a9dcad5e
HD
853 const char *name = data;
854
855 pr_debug("%s: %s %s\n", __func__, obj->name, name);
856
857 return strcmp(obj->name, name) == 0;
858}
859
860/**
f626b52d 861 * omap_iommu_attach() - attach iommu device to an iommu domain
fabdbca8 862 * @name: name of target omap iommu device
f626b52d 863 * @iopgd: page table
a9dcad5e 864 **/
fabdbca8 865static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
a9dcad5e 866{
7ee08b9e 867 int err;
fabdbca8
OBC
868 struct device *dev;
869 struct omap_iommu *obj;
870
871 dev = driver_find_device(&omap_iommu_driver.driver, NULL,
872 (void *)name,
873 device_match_by_alias);
874 if (!dev)
7ee08b9e 875 return ERR_PTR(-ENODEV);
fabdbca8
OBC
876
877 obj = to_iommu(dev);
a9dcad5e 878
f626b52d 879 spin_lock(&obj->iommu_lock);
a9dcad5e 880
f626b52d
OBC
881 /* an iommu device can only be attached once */
882 if (++obj->refcount > 1) {
883 dev_err(dev, "%s: already attached!\n", obj->name);
884 err = -EBUSY;
885 goto err_enable;
a9dcad5e
HD
886 }
887
f626b52d
OBC
888 obj->iopgd = iopgd;
889 err = iommu_enable(obj);
890 if (err)
891 goto err_enable;
892 flush_iotlb_all(obj);
893
7ee08b9e
SA
894 if (!try_module_get(obj->owner)) {
895 err = -ENODEV;
a9dcad5e 896 goto err_module;
7ee08b9e 897 }
a9dcad5e 898
f626b52d 899 spin_unlock(&obj->iommu_lock);
a9dcad5e
HD
900
901 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
902 return obj;
903
904err_module:
905 if (obj->refcount == 1)
906 iommu_disable(obj);
907err_enable:
908 obj->refcount--;
f626b52d 909 spin_unlock(&obj->iommu_lock);
a9dcad5e
HD
910 return ERR_PTR(err);
911}
a9dcad5e
HD
912
913/**
f626b52d 914 * omap_iommu_detach - release iommu device
a9dcad5e
HD
915 * @obj: target iommu
916 **/
6c32df43 917static void omap_iommu_detach(struct omap_iommu *obj)
a9dcad5e 918{
acf9d467 919 if (!obj || IS_ERR(obj))
a9dcad5e
HD
920 return;
921
f626b52d 922 spin_lock(&obj->iommu_lock);
a9dcad5e
HD
923
924 if (--obj->refcount == 0)
925 iommu_disable(obj);
926
927 module_put(obj->owner);
928
f626b52d 929 obj->iopgd = NULL;
d594f1f3 930
f626b52d 931 spin_unlock(&obj->iommu_lock);
d594f1f3 932
a9dcad5e 933 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
d594f1f3 934}
d594f1f3 935
a9dcad5e
HD
936/*
937 * OMAP Device MMU(IOMMU) detection
938 */
d34d6517 939static int omap_iommu_probe(struct platform_device *pdev)
a9dcad5e
HD
940{
941 int err = -ENODEV;
a9dcad5e 942 int irq;
6c32df43 943 struct omap_iommu *obj;
a9dcad5e
HD
944 struct resource *res;
945 struct iommu_platform_data *pdata = pdev->dev.platform_data;
3c92748d 946 struct device_node *of = pdev->dev.of_node;
a9dcad5e 947
f129b3df 948 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
a9dcad5e
HD
949 if (!obj)
950 return -ENOMEM;
951
3c92748d
FV
952 if (of) {
953 obj->name = dev_name(&pdev->dev);
954 obj->nr_tlb_entries = 32;
955 err = of_property_read_u32(of, "ti,#tlb-entries",
956 &obj->nr_tlb_entries);
957 if (err && err != -EINVAL)
958 return err;
959 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
960 return -EINVAL;
961 /*
962 * da_start and da_end are needed for omap-iovmm, so hardcode
963 * these values as used by OMAP3 ISP - the only user for
964 * omap-iovmm
965 */
966 obj->da_start = 0;
967 obj->da_end = 0xfffff000;
b148d5fb
SA
968 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
969 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
3c92748d
FV
970 } else {
971 obj->nr_tlb_entries = pdata->nr_tlb_entries;
972 obj->name = pdata->name;
973 obj->da_start = pdata->da_start;
974 obj->da_end = pdata->da_end;
975 }
976 if (obj->da_end <= obj->da_start)
977 return -EINVAL;
978
a9dcad5e
HD
979 obj->dev = &pdev->dev;
980 obj->ctx = (void *)obj + sizeof(*obj);
981
f626b52d 982 spin_lock_init(&obj->iommu_lock);
a9dcad5e
HD
983 mutex_init(&obj->mmap_lock);
984 spin_lock_init(&obj->page_table_lock);
985 INIT_LIST_HEAD(&obj->mmap);
986
987 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
f129b3df
SA
988 obj->regbase = devm_ioremap_resource(obj->dev, res);
989 if (IS_ERR(obj->regbase))
990 return PTR_ERR(obj->regbase);
da4a0f76 991
a9dcad5e 992 irq = platform_get_irq(pdev, 0);
f129b3df
SA
993 if (irq < 0)
994 return -ENODEV;
995
996 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
997 dev_name(obj->dev), obj);
a9dcad5e 998 if (err < 0)
f129b3df 999 return err;
a9dcad5e
HD
1000 platform_set_drvdata(pdev, obj);
1001
ebf7cda0
ORL
1002 pm_runtime_irq_safe(obj->dev);
1003 pm_runtime_enable(obj->dev);
1004
a9dcad5e
HD
1005 dev_info(&pdev->dev, "%s registered\n", obj->name);
1006 return 0;
a9dcad5e
HD
1007}
1008
d34d6517 1009static int omap_iommu_remove(struct platform_device *pdev)
a9dcad5e 1010{
6c32df43 1011 struct omap_iommu *obj = platform_get_drvdata(pdev);
a9dcad5e 1012
a9dcad5e 1013 iopgtable_clear_entry_all(obj);
a9dcad5e 1014
ebf7cda0
ORL
1015 pm_runtime_disable(obj->dev);
1016
a9dcad5e 1017 dev_info(&pdev->dev, "%s removed\n", obj->name);
a9dcad5e
HD
1018 return 0;
1019}
1020
3c92748d
FV
1021static struct of_device_id omap_iommu_of_match[] = {
1022 { .compatible = "ti,omap2-iommu" },
1023 { .compatible = "ti,omap4-iommu" },
1024 { .compatible = "ti,dra7-iommu" },
1025 {},
1026};
1027MODULE_DEVICE_TABLE(of, omap_iommu_of_match);
1028
a9dcad5e
HD
1029static struct platform_driver omap_iommu_driver = {
1030 .probe = omap_iommu_probe,
d34d6517 1031 .remove = omap_iommu_remove,
a9dcad5e
HD
1032 .driver = {
1033 .name = "omap-iommu",
3c92748d 1034 .of_match_table = of_match_ptr(omap_iommu_of_match),
a9dcad5e
HD
1035 },
1036};
1037
1038static void iopte_cachep_ctor(void *iopte)
1039{
1040 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1041}
1042
ed1c7de2
TL
1043static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa,
1044 u32 flags)
1045{
1046 memset(e, 0, sizeof(*e));
1047
1048 e->da = da;
1049 e->pa = pa;
d760e3e0 1050 e->valid = MMU_CAM_V;
ed1c7de2
TL
1051 /* FIXME: add OMAP1 support */
1052 e->pgsz = flags & MMU_CAM_PGSZ_MASK;
1053 e->endian = flags & MMU_RAM_ENDIAN_MASK;
1054 e->elsz = flags & MMU_RAM_ELSZ_MASK;
1055 e->mixed = flags & MMU_RAM_MIXED_MASK;
1056
1057 return iopgsz_to_bytes(e->pgsz);
1058}
1059
f626b52d 1060static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
5009065d 1061 phys_addr_t pa, size_t bytes, int prot)
f626b52d
OBC
1062{
1063 struct omap_iommu_domain *omap_domain = domain->priv;
6c32df43 1064 struct omap_iommu *oiommu = omap_domain->iommu_dev;
f626b52d 1065 struct device *dev = oiommu->dev;
f626b52d
OBC
1066 struct iotlb_entry e;
1067 int omap_pgsz;
1068 u32 ret, flags;
1069
1070 /* we only support mapping a single iommu page for now */
1071 omap_pgsz = bytes_to_iopgsz(bytes);
1072 if (omap_pgsz < 0) {
1073 dev_err(dev, "invalid size to map: %d\n", bytes);
1074 return -EINVAL;
1075 }
1076
1077 dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes);
1078
1079 flags = omap_pgsz | prot;
1080
1081 iotlb_init_entry(&e, da, pa, flags);
1082
6c32df43 1083 ret = omap_iopgtable_store_entry(oiommu, &e);
b4550d41 1084 if (ret)
6c32df43 1085 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
f626b52d 1086
b4550d41 1087 return ret;
f626b52d
OBC
1088}
1089
5009065d
OBC
1090static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1091 size_t size)
f626b52d
OBC
1092{
1093 struct omap_iommu_domain *omap_domain = domain->priv;
6c32df43 1094 struct omap_iommu *oiommu = omap_domain->iommu_dev;
f626b52d 1095 struct device *dev = oiommu->dev;
f626b52d 1096
5009065d 1097 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
f626b52d 1098
5009065d 1099 return iopgtable_clear_entry(oiommu, da);
f626b52d
OBC
1100}
1101
1102static int
1103omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1104{
1105 struct omap_iommu_domain *omap_domain = domain->priv;
6c32df43 1106 struct omap_iommu *oiommu;
fabdbca8 1107 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
f626b52d
OBC
1108 int ret = 0;
1109
1110 spin_lock(&omap_domain->lock);
1111
1112 /* only a single device is supported per domain for now */
1113 if (omap_domain->iommu_dev) {
1114 dev_err(dev, "iommu domain is already attached\n");
1115 ret = -EBUSY;
1116 goto out;
1117 }
1118
1119 /* get a handle to and enable the omap iommu */
fabdbca8 1120 oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
f626b52d
OBC
1121 if (IS_ERR(oiommu)) {
1122 ret = PTR_ERR(oiommu);
1123 dev_err(dev, "can't get omap iommu: %d\n", ret);
1124 goto out;
1125 }
1126
fabdbca8 1127 omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
803b5277 1128 omap_domain->dev = dev;
e7f10f02 1129 oiommu->domain = domain;
f626b52d
OBC
1130
1131out:
1132 spin_unlock(&omap_domain->lock);
1133 return ret;
1134}
1135
803b5277
ORL
1136static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
1137 struct device *dev)
f626b52d 1138{
fabdbca8 1139 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
803b5277 1140 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
f626b52d
OBC
1141
1142 /* only a single device is supported per domain for now */
1143 if (omap_domain->iommu_dev != oiommu) {
1144 dev_err(dev, "invalid iommu device\n");
803b5277 1145 return;
f626b52d
OBC
1146 }
1147
1148 iopgtable_clear_entry_all(oiommu);
1149
1150 omap_iommu_detach(oiommu);
1151
fabdbca8 1152 omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
803b5277
ORL
1153 omap_domain->dev = NULL;
1154}
f626b52d 1155
803b5277
ORL
1156static void omap_iommu_detach_dev(struct iommu_domain *domain,
1157 struct device *dev)
1158{
1159 struct omap_iommu_domain *omap_domain = domain->priv;
1160
1161 spin_lock(&omap_domain->lock);
1162 _omap_iommu_detach_dev(omap_domain, dev);
f626b52d
OBC
1163 spin_unlock(&omap_domain->lock);
1164}
1165
1166static int omap_iommu_domain_init(struct iommu_domain *domain)
1167{
1168 struct omap_iommu_domain *omap_domain;
1169
1170 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1171 if (!omap_domain) {
1172 pr_err("kzalloc failed\n");
1173 goto out;
1174 }
1175
1176 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1177 if (!omap_domain->pgtable) {
1178 pr_err("kzalloc failed\n");
1179 goto fail_nomem;
1180 }
1181
1182 /*
1183 * should never fail, but please keep this around to ensure
1184 * we keep the hardware happy
1185 */
1186 BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
1187
1188 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1189 spin_lock_init(&omap_domain->lock);
1190
1191 domain->priv = omap_domain;
1192
2c6edb0c
JR
1193 domain->geometry.aperture_start = 0;
1194 domain->geometry.aperture_end = (1ULL << 32) - 1;
1195 domain->geometry.force_aperture = true;
1196
f626b52d
OBC
1197 return 0;
1198
1199fail_nomem:
1200 kfree(omap_domain);
1201out:
1202 return -ENOMEM;
1203}
1204
f626b52d
OBC
1205static void omap_iommu_domain_destroy(struct iommu_domain *domain)
1206{
1207 struct omap_iommu_domain *omap_domain = domain->priv;
1208
1209 domain->priv = NULL;
1210
803b5277
ORL
1211 /*
1212 * An iommu device is still attached
1213 * (currently, only one device can be attached) ?
1214 */
1215 if (omap_domain->iommu_dev)
1216 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1217
f626b52d
OBC
1218 kfree(omap_domain->pgtable);
1219 kfree(omap_domain);
1220}
1221
1222static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
bb5547ac 1223 dma_addr_t da)
f626b52d
OBC
1224{
1225 struct omap_iommu_domain *omap_domain = domain->priv;
6c32df43 1226 struct omap_iommu *oiommu = omap_domain->iommu_dev;
f626b52d
OBC
1227 struct device *dev = oiommu->dev;
1228 u32 *pgd, *pte;
1229 phys_addr_t ret = 0;
1230
1231 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1232
1233 if (pte) {
1234 if (iopte_is_small(*pte))
1235 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1236 else if (iopte_is_large(*pte))
1237 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1238 else
2abfcfbc
SA
1239 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
1240 (unsigned long long)da);
f626b52d
OBC
1241 } else {
1242 if (iopgd_is_section(*pgd))
1243 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1244 else if (iopgd_is_super(*pgd))
1245 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1246 else
2abfcfbc
SA
1247 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
1248 (unsigned long long)da);
f626b52d
OBC
1249 }
1250
1251 return ret;
1252}
1253
07a02030
LP
1254static int omap_iommu_add_device(struct device *dev)
1255{
1256 struct omap_iommu_arch_data *arch_data;
1257 struct device_node *np;
1258
1259 /*
1260 * Allocate the archdata iommu structure for DT-based devices.
1261 *
1262 * TODO: Simplify this when removing non-DT support completely from the
1263 * IOMMU users.
1264 */
1265 if (!dev->of_node)
1266 return 0;
1267
1268 np = of_parse_phandle(dev->of_node, "iommus", 0);
1269 if (!np)
1270 return 0;
1271
1272 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
1273 if (!arch_data) {
1274 of_node_put(np);
1275 return -ENOMEM;
1276 }
1277
1278 arch_data->name = kstrdup(dev_name(dev), GFP_KERNEL);
1279 dev->archdata.iommu = arch_data;
1280
1281 of_node_put(np);
1282
1283 return 0;
1284}
1285
1286static void omap_iommu_remove_device(struct device *dev)
1287{
1288 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1289
1290 if (!dev->of_node || !arch_data)
1291 return;
1292
1293 kfree(arch_data->name);
1294 kfree(arch_data);
1295}
1296
f626b52d
OBC
1297static struct iommu_ops omap_iommu_ops = {
1298 .domain_init = omap_iommu_domain_init,
1299 .domain_destroy = omap_iommu_domain_destroy,
1300 .attach_dev = omap_iommu_attach_dev,
1301 .detach_dev = omap_iommu_detach_dev,
1302 .map = omap_iommu_map,
1303 .unmap = omap_iommu_unmap,
1304 .iova_to_phys = omap_iommu_iova_to_phys,
07a02030
LP
1305 .add_device = omap_iommu_add_device,
1306 .remove_device = omap_iommu_remove_device,
66bc8cf3 1307 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
f626b52d
OBC
1308};
1309
a9dcad5e
HD
1310static int __init omap_iommu_init(void)
1311{
1312 struct kmem_cache *p;
1313 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1314 size_t align = 1 << 10; /* L2 pagetable alignement */
1315
1316 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1317 iopte_cachep_ctor);
1318 if (!p)
1319 return -ENOMEM;
1320 iopte_cachep = p;
1321
a65bc64f 1322 bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
f626b52d 1323
a9dcad5e
HD
1324 return platform_driver_register(&omap_iommu_driver);
1325}
435792d9
OBC
1326/* must be ready before omap3isp is probed */
1327subsys_initcall(omap_iommu_init);
a9dcad5e
HD
1328
1329static void __exit omap_iommu_exit(void)
1330{
1331 kmem_cache_destroy(iopte_cachep);
1332
1333 platform_driver_unregister(&omap_iommu_driver);
1334}
1335module_exit(omap_iommu_exit);
1336
1337MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1338MODULE_ALIAS("platform:omap-iommu");
1339MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1340MODULE_LICENSE("GPL v2");
This page took 0.532908 seconds and 5 git commands to generate.