blob: 966093a2fcf5db13c75ac6a73bc6e9d82b93d72a [file] [log] [blame]
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001/*
2 * omap iommu: tlb and pagetable primitives
3 *
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -08004 * Copyright (C) 2008-2010 Nokia Corporation
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02005 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020017#include <linux/interrupt.h>
18#include <linux/ioport.h>
19#include <linux/clk.h>
20#include <linux/platform_device.h>
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030021#include <linux/iommu.h>
22#include <linux/mutex.h>
23#include <linux/spinlock.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020024
25#include <asm/cacheflush.h>
26
Tony Lindgrence491cf2009-10-20 09:40:47 -070027#include <plat/iommu.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020028
Ohad Ben-Cohenfcf3a6e2011-08-15 23:21:41 +030029#include <plat/iopgtable.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020030
Hiroshi DOYU37c28362010-04-27 05:37:12 +000031#define for_each_iotlb_cr(obj, n, __i, cr) \
32 for (__i = 0; \
33 (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \
34 __i++)
35
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030036/**
37 * struct omap_iommu_domain - omap iommu domain
38 * @pgtable: the page table
39 * @iommu_dev: an omap iommu device attached to this domain. only a single
40 * iommu device can be attached for now.
41 * @lock: domain lock, should be taken when attaching/detaching
42 */
43struct omap_iommu_domain {
44 u32 *pgtable;
45 struct iommu *iommu_dev;
46 spinlock_t lock;
47};
48
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020049/* accommodate the difference between omap1 and omap2/3 */
50static const struct iommu_functions *arch_iommu;
51
52static struct platform_driver omap_iommu_driver;
53static struct kmem_cache *iopte_cachep;
54
55/**
56 * install_iommu_arch - Install archtecure specific iommu functions
57 * @ops: a pointer to architecture specific iommu functions
58 *
59 * There are several kind of iommu algorithm(tlb, pagetable) among
60 * omap series. This interface installs such an iommu algorighm.
61 **/
62int install_iommu_arch(const struct iommu_functions *ops)
63{
64 if (arch_iommu)
65 return -EBUSY;
66
67 arch_iommu = ops;
68 return 0;
69}
70EXPORT_SYMBOL_GPL(install_iommu_arch);
71
72/**
73 * uninstall_iommu_arch - Uninstall archtecure specific iommu functions
74 * @ops: a pointer to architecture specific iommu functions
75 *
76 * This interface uninstalls the iommu algorighm installed previously.
77 **/
78void uninstall_iommu_arch(const struct iommu_functions *ops)
79{
80 if (arch_iommu != ops)
81 pr_err("%s: not your arch\n", __func__);
82
83 arch_iommu = NULL;
84}
85EXPORT_SYMBOL_GPL(uninstall_iommu_arch);
86
87/**
88 * iommu_save_ctx - Save registers for pm off-mode support
89 * @obj: target iommu
90 **/
91void iommu_save_ctx(struct iommu *obj)
92{
93 arch_iommu->save_ctx(obj);
94}
95EXPORT_SYMBOL_GPL(iommu_save_ctx);
96
97/**
98 * iommu_restore_ctx - Restore registers for pm off-mode support
99 * @obj: target iommu
100 **/
101void iommu_restore_ctx(struct iommu *obj)
102{
103 arch_iommu->restore_ctx(obj);
104}
105EXPORT_SYMBOL_GPL(iommu_restore_ctx);
106
107/**
108 * iommu_arch_version - Return running iommu arch version
109 **/
110u32 iommu_arch_version(void)
111{
112 return arch_iommu->version;
113}
114EXPORT_SYMBOL_GPL(iommu_arch_version);
115
116static int iommu_enable(struct iommu *obj)
117{
118 int err;
119
120 if (!obj)
121 return -EINVAL;
122
Martin Hostettleref4815a2011-02-24 12:51:31 -0800123 if (!arch_iommu)
124 return -ENODEV;
125
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200126 clk_enable(obj->clk);
127
128 err = arch_iommu->enable(obj);
129
130 clk_disable(obj->clk);
131 return err;
132}
133
134static void iommu_disable(struct iommu *obj)
135{
136 if (!obj)
137 return;
138
139 clk_enable(obj->clk);
140
141 arch_iommu->disable(obj);
142
143 clk_disable(obj->clk);
144}
145
146/*
147 * TLB operations
148 */
149void iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
150{
151 BUG_ON(!cr || !e);
152
153 arch_iommu->cr_to_e(cr, e);
154}
155EXPORT_SYMBOL_GPL(iotlb_cr_to_e);
156
157static inline int iotlb_cr_valid(struct cr_regs *cr)
158{
159 if (!cr)
160 return -EINVAL;
161
162 return arch_iommu->cr_valid(cr);
163}
164
165static inline struct cr_regs *iotlb_alloc_cr(struct iommu *obj,
166 struct iotlb_entry *e)
167{
168 if (!e)
169 return NULL;
170
171 return arch_iommu->alloc_cr(obj, e);
172}
173
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300174static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200175{
176 return arch_iommu->cr_to_virt(cr);
177}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200178
179static u32 get_iopte_attr(struct iotlb_entry *e)
180{
181 return arch_iommu->get_pte_attr(e);
182}
183
184static u32 iommu_report_fault(struct iommu *obj, u32 *da)
185{
186 return arch_iommu->fault_isr(obj, da);
187}
188
189static void iotlb_lock_get(struct iommu *obj, struct iotlb_lock *l)
190{
191 u32 val;
192
193 val = iommu_read_reg(obj, MMU_LOCK);
194
195 l->base = MMU_LOCK_BASE(val);
196 l->vict = MMU_LOCK_VICT(val);
197
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200198}
199
200static void iotlb_lock_set(struct iommu *obj, struct iotlb_lock *l)
201{
202 u32 val;
203
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200204 val = (l->base << MMU_LOCK_BASE_SHIFT);
205 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
206
207 iommu_write_reg(obj, val, MMU_LOCK);
208}
209
210static void iotlb_read_cr(struct iommu *obj, struct cr_regs *cr)
211{
212 arch_iommu->tlb_read_cr(obj, cr);
213}
214
215static void iotlb_load_cr(struct iommu *obj, struct cr_regs *cr)
216{
217 arch_iommu->tlb_load_cr(obj, cr);
218
219 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
220 iommu_write_reg(obj, 1, MMU_LD_TLB);
221}
222
223/**
224 * iotlb_dump_cr - Dump an iommu tlb entry into buf
225 * @obj: target iommu
226 * @cr: contents of cam and ram register
227 * @buf: output buffer
228 **/
229static inline ssize_t iotlb_dump_cr(struct iommu *obj, struct cr_regs *cr,
230 char *buf)
231{
232 BUG_ON(!cr || !buf);
233
234 return arch_iommu->dump_cr(obj, cr, buf);
235}
236
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000237/* only used in iotlb iteration for-loop */
238static struct cr_regs __iotlb_read_cr(struct iommu *obj, int n)
239{
240 struct cr_regs cr;
241 struct iotlb_lock l;
242
243 iotlb_lock_get(obj, &l);
244 l.vict = n;
245 iotlb_lock_set(obj, &l);
246 iotlb_read_cr(obj, &cr);
247
248 return cr;
249}
250
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200251/**
252 * load_iotlb_entry - Set an iommu tlb entry
253 * @obj: target iommu
254 * @e: an iommu tlb entry info
255 **/
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300256#ifdef PREFETCH_IOTLB
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300257static int load_iotlb_entry(struct iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200258{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200259 int err = 0;
260 struct iotlb_lock l;
261 struct cr_regs *cr;
262
263 if (!obj || !obj->nr_tlb_entries || !e)
264 return -EINVAL;
265
266 clk_enable(obj->clk);
267
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000268 iotlb_lock_get(obj, &l);
269 if (l.base == obj->nr_tlb_entries) {
270 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200271 err = -EBUSY;
272 goto out;
273 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000274 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000275 int i;
276 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000277
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000278 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000279 if (!iotlb_cr_valid(&tmp))
280 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000281
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000282 if (i == obj->nr_tlb_entries) {
283 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
284 err = -EBUSY;
285 goto out;
286 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000287
288 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000289 } else {
290 l.vict = l.base;
291 iotlb_lock_set(obj, &l);
292 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200293
294 cr = iotlb_alloc_cr(obj, e);
295 if (IS_ERR(cr)) {
296 clk_disable(obj->clk);
297 return PTR_ERR(cr);
298 }
299
300 iotlb_load_cr(obj, cr);
301 kfree(cr);
302
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000303 if (e->prsvd)
304 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200305 /* increment victim for next tlb load */
306 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000307 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200308 iotlb_lock_set(obj, &l);
309out:
310 clk_disable(obj->clk);
311 return err;
312}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200313
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300314#else /* !PREFETCH_IOTLB */
315
316static int load_iotlb_entry(struct iommu *obj, struct iotlb_entry *e)
317{
318 return 0;
319}
320
321#endif /* !PREFETCH_IOTLB */
322
323static int prefetch_iotlb_entry(struct iommu *obj, struct iotlb_entry *e)
324{
325 return load_iotlb_entry(obj, e);
326}
327
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200328/**
329 * flush_iotlb_page - Clear an iommu tlb entry
330 * @obj: target iommu
331 * @da: iommu device virtual address
332 *
333 * Clear an iommu tlb entry which includes 'da' address.
334 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300335static void flush_iotlb_page(struct iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200336{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200337 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000338 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200339
340 clk_enable(obj->clk);
341
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000342 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200343 u32 start;
344 size_t bytes;
345
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200346 if (!iotlb_cr_valid(&cr))
347 continue;
348
349 start = iotlb_cr_to_virt(&cr);
350 bytes = iopgsz_to_bytes(cr.cam & 3);
351
352 if ((start <= da) && (da < start + bytes)) {
353 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
354 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000355 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200356 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
357 }
358 }
359 clk_disable(obj->clk);
360
361 if (i == obj->nr_tlb_entries)
362 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
363}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200364
365/**
366 * flush_iotlb_range - Clear an iommu tlb entries
367 * @obj: target iommu
368 * @start: iommu device virtual address(start)
369 * @end: iommu device virtual address(end)
370 *
371 * Clear an iommu tlb entry which includes 'da' address.
372 **/
373void flush_iotlb_range(struct iommu *obj, u32 start, u32 end)
374{
375 u32 da = start;
376
377 while (da < end) {
378 flush_iotlb_page(obj, da);
379 /* FIXME: Optimize for multiple page size */
380 da += IOPTE_SIZE;
381 }
382}
383EXPORT_SYMBOL_GPL(flush_iotlb_range);
384
385/**
386 * flush_iotlb_all - Clear all iommu tlb entries
387 * @obj: target iommu
388 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300389static void flush_iotlb_all(struct iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200390{
391 struct iotlb_lock l;
392
393 clk_enable(obj->clk);
394
395 l.base = 0;
396 l.vict = 0;
397 iotlb_lock_set(obj, &l);
398
399 iommu_write_reg(obj, 1, MMU_GFLUSH);
400
401 clk_disable(obj->clk);
402}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200403
Kanigeri, Hariddfa9752010-05-24 02:01:51 +0000404/**
405 * iommu_set_twl - enable/disable table walking logic
406 * @obj: target iommu
407 * @on: enable/disable
408 *
409 * Function used to enable/disable TWL. If one wants to work
410 * exclusively with locked TLB entries and receive notifications
411 * for TLB miss then call this function to disable TWL.
412 */
413void iommu_set_twl(struct iommu *obj, bool on)
414{
415 clk_enable(obj->clk);
416 arch_iommu->set_twl(obj, on);
417 clk_disable(obj->clk);
418}
419EXPORT_SYMBOL_GPL(iommu_set_twl);
420
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200421#if defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
422
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700423ssize_t iommu_dump_ctx(struct iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200424{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200425 if (!obj || !buf)
426 return -EINVAL;
427
428 clk_enable(obj->clk);
429
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700430 bytes = arch_iommu->dump_ctx(obj, buf, bytes);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200431
432 clk_disable(obj->clk);
433
434 return bytes;
435}
436EXPORT_SYMBOL_GPL(iommu_dump_ctx);
437
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700438static int __dump_tlb_entries(struct iommu *obj, struct cr_regs *crs, int num)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200439{
440 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000441 struct iotlb_lock saved;
442 struct cr_regs tmp;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200443 struct cr_regs *p = crs;
444
445 clk_enable(obj->clk);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200446 iotlb_lock_get(obj, &saved);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200447
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000448 for_each_iotlb_cr(obj, num, i, tmp) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200449 if (!iotlb_cr_valid(&tmp))
450 continue;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200451 *p++ = tmp;
452 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000453
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200454 iotlb_lock_set(obj, &saved);
455 clk_disable(obj->clk);
456
457 return p - crs;
458}
459
460/**
461 * dump_tlb_entries - dump cr arrays to given buffer
462 * @obj: target iommu
463 * @buf: output buffer
464 **/
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700465size_t dump_tlb_entries(struct iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200466{
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700467 int i, num;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200468 struct cr_regs *cr;
469 char *p = buf;
470
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700471 num = bytes / sizeof(*cr);
472 num = min(obj->nr_tlb_entries, num);
473
474 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200475 if (!cr)
476 return 0;
477
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700478 num = __dump_tlb_entries(obj, cr, num);
479 for (i = 0; i < num; i++)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200480 p += iotlb_dump_cr(obj, cr + i, p);
481 kfree(cr);
482
483 return p - buf;
484}
485EXPORT_SYMBOL_GPL(dump_tlb_entries);
486
487int foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
488{
489 return driver_for_each_device(&omap_iommu_driver.driver,
490 NULL, data, fn);
491}
492EXPORT_SYMBOL_GPL(foreach_iommu_device);
493
494#endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
495
496/*
497 * H/W pagetable operations
498 */
499static void flush_iopgd_range(u32 *first, u32 *last)
500{
501 /* FIXME: L2 cache should be taken care of if it exists */
502 do {
503 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
504 : : "r" (first));
505 first += L1_CACHE_BYTES / sizeof(*first);
506 } while (first <= last);
507}
508
509static void flush_iopte_range(u32 *first, u32 *last)
510{
511 /* FIXME: L2 cache should be taken care of if it exists */
512 do {
513 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
514 : : "r" (first));
515 first += L1_CACHE_BYTES / sizeof(*first);
516 } while (first <= last);
517}
518
519static void iopte_free(u32 *iopte)
520{
521 /* Note: freed iopte's must be clean ready for re-use */
522 kmem_cache_free(iopte_cachep, iopte);
523}
524
525static u32 *iopte_alloc(struct iommu *obj, u32 *iopgd, u32 da)
526{
527 u32 *iopte;
528
529 /* a table has already existed */
530 if (*iopgd)
531 goto pte_ready;
532
533 /*
534 * do the allocation outside the page table lock
535 */
536 spin_unlock(&obj->page_table_lock);
537 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
538 spin_lock(&obj->page_table_lock);
539
540 if (!*iopgd) {
541 if (!iopte)
542 return ERR_PTR(-ENOMEM);
543
544 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
545 flush_iopgd_range(iopgd, iopgd);
546
547 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
548 } else {
549 /* We raced, free the reduniovant table */
550 iopte_free(iopte);
551 }
552
553pte_ready:
554 iopte = iopte_offset(iopgd, da);
555
556 dev_vdbg(obj->dev,
557 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
558 __func__, da, iopgd, *iopgd, iopte, *iopte);
559
560 return iopte;
561}
562
563static int iopgd_alloc_section(struct iommu *obj, u32 da, u32 pa, u32 prot)
564{
565 u32 *iopgd = iopgd_offset(obj, da);
566
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300567 if ((da | pa) & ~IOSECTION_MASK) {
568 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
569 __func__, da, pa, IOSECTION_SIZE);
570 return -EINVAL;
571 }
572
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200573 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
574 flush_iopgd_range(iopgd, iopgd);
575 return 0;
576}
577
578static int iopgd_alloc_super(struct iommu *obj, u32 da, u32 pa, u32 prot)
579{
580 u32 *iopgd = iopgd_offset(obj, da);
581 int i;
582
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300583 if ((da | pa) & ~IOSUPER_MASK) {
584 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
585 __func__, da, pa, IOSUPER_SIZE);
586 return -EINVAL;
587 }
588
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200589 for (i = 0; i < 16; i++)
590 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
591 flush_iopgd_range(iopgd, iopgd + 15);
592 return 0;
593}
594
595static int iopte_alloc_page(struct iommu *obj, u32 da, u32 pa, u32 prot)
596{
597 u32 *iopgd = iopgd_offset(obj, da);
598 u32 *iopte = iopte_alloc(obj, iopgd, da);
599
600 if (IS_ERR(iopte))
601 return PTR_ERR(iopte);
602
603 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
604 flush_iopte_range(iopte, iopte);
605
606 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
607 __func__, da, pa, iopte, *iopte);
608
609 return 0;
610}
611
612static int iopte_alloc_large(struct iommu *obj, u32 da, u32 pa, u32 prot)
613{
614 u32 *iopgd = iopgd_offset(obj, da);
615 u32 *iopte = iopte_alloc(obj, iopgd, da);
616 int i;
617
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300618 if ((da | pa) & ~IOLARGE_MASK) {
619 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
620 __func__, da, pa, IOLARGE_SIZE);
621 return -EINVAL;
622 }
623
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200624 if (IS_ERR(iopte))
625 return PTR_ERR(iopte);
626
627 for (i = 0; i < 16; i++)
628 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
629 flush_iopte_range(iopte, iopte + 15);
630 return 0;
631}
632
633static int iopgtable_store_entry_core(struct iommu *obj, struct iotlb_entry *e)
634{
635 int (*fn)(struct iommu *, u32, u32, u32);
636 u32 prot;
637 int err;
638
639 if (!obj || !e)
640 return -EINVAL;
641
642 switch (e->pgsz) {
643 case MMU_CAM_PGSZ_16M:
644 fn = iopgd_alloc_super;
645 break;
646 case MMU_CAM_PGSZ_1M:
647 fn = iopgd_alloc_section;
648 break;
649 case MMU_CAM_PGSZ_64K:
650 fn = iopte_alloc_large;
651 break;
652 case MMU_CAM_PGSZ_4K:
653 fn = iopte_alloc_page;
654 break;
655 default:
656 fn = NULL;
657 BUG();
658 break;
659 }
660
661 prot = get_iopte_attr(e);
662
663 spin_lock(&obj->page_table_lock);
664 err = fn(obj, e->da, e->pa, prot);
665 spin_unlock(&obj->page_table_lock);
666
667 return err;
668}
669
670/**
671 * iopgtable_store_entry - Make an iommu pte entry
672 * @obj: target iommu
673 * @e: an iommu tlb entry info
674 **/
675int iopgtable_store_entry(struct iommu *obj, struct iotlb_entry *e)
676{
677 int err;
678
679 flush_iotlb_page(obj, e->da);
680 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200681 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300682 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200683 return err;
684}
685EXPORT_SYMBOL_GPL(iopgtable_store_entry);
686
687/**
688 * iopgtable_lookup_entry - Lookup an iommu pte entry
689 * @obj: target iommu
690 * @da: iommu device virtual address
691 * @ppgd: iommu pgd entry pointer to be returned
692 * @ppte: iommu pte entry pointer to be returned
693 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300694static void
695iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200696{
697 u32 *iopgd, *iopte = NULL;
698
699 iopgd = iopgd_offset(obj, da);
700 if (!*iopgd)
701 goto out;
702
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300703 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200704 iopte = iopte_offset(iopgd, da);
705out:
706 *ppgd = iopgd;
707 *ppte = iopte;
708}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200709
710static size_t iopgtable_clear_entry_core(struct iommu *obj, u32 da)
711{
712 size_t bytes;
713 u32 *iopgd = iopgd_offset(obj, da);
714 int nent = 1;
715
716 if (!*iopgd)
717 return 0;
718
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300719 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200720 int i;
721 u32 *iopte = iopte_offset(iopgd, da);
722
723 bytes = IOPTE_SIZE;
724 if (*iopte & IOPTE_LARGE) {
725 nent *= 16;
726 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800727 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200728 }
729 bytes *= nent;
730 memset(iopte, 0, nent * sizeof(*iopte));
731 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
732
733 /*
734 * do table walk to check if this table is necessary or not
735 */
736 iopte = iopte_offset(iopgd, 0);
737 for (i = 0; i < PTRS_PER_IOPTE; i++)
738 if (iopte[i])
739 goto out;
740
741 iopte_free(iopte);
742 nent = 1; /* for the next L1 entry */
743 } else {
744 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700745 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200746 nent *= 16;
747 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800748 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200749 }
750 bytes *= nent;
751 }
752 memset(iopgd, 0, nent * sizeof(*iopgd));
753 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
754out:
755 return bytes;
756}
757
758/**
759 * iopgtable_clear_entry - Remove an iommu pte entry
760 * @obj: target iommu
761 * @da: iommu device virtual address
762 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300763static size_t iopgtable_clear_entry(struct iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200764{
765 size_t bytes;
766
767 spin_lock(&obj->page_table_lock);
768
769 bytes = iopgtable_clear_entry_core(obj, da);
770 flush_iotlb_page(obj, da);
771
772 spin_unlock(&obj->page_table_lock);
773
774 return bytes;
775}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200776
777static void iopgtable_clear_entry_all(struct iommu *obj)
778{
779 int i;
780
781 spin_lock(&obj->page_table_lock);
782
783 for (i = 0; i < PTRS_PER_IOPGD; i++) {
784 u32 da;
785 u32 *iopgd;
786
787 da = i << IOPGD_SHIFT;
788 iopgd = iopgd_offset(obj, da);
789
790 if (!*iopgd)
791 continue;
792
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300793 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200794 iopte_free(iopte_offset(iopgd, 0));
795
796 *iopgd = 0;
797 flush_iopgd_range(iopgd, iopgd);
798 }
799
800 flush_iotlb_all(obj);
801
802 spin_unlock(&obj->page_table_lock);
803}
804
805/*
806 * Device IOMMU generic operations
807 */
808static irqreturn_t iommu_fault_handler(int irq, void *data)
809{
David Cohend594f1f2011-02-16 19:35:51 +0000810 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200811 u32 *iopgd, *iopte;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200812 struct iommu *obj = data;
813
814 if (!obj->refcount)
815 return IRQ_NONE;
816
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200817 clk_enable(obj->clk);
David Cohend594f1f2011-02-16 19:35:51 +0000818 errs = iommu_report_fault(obj, &da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200819 clk_disable(obj->clk);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200820 if (errs == 0)
821 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000822
823 /* Fault callback or TLB/PTE Dynamic loading */
824 if (obj->isr && !obj->isr(obj, da, errs, obj->isr_priv))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200825 return IRQ_HANDLED;
826
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000827 iommu_disable(obj);
828
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200829 iopgd = iopgd_offset(obj, da);
830
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300831 if (!iopgd_is_table(*iopgd)) {
David Cohend594f1f2011-02-16 19:35:51 +0000832 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p "
833 "*pgd:px%08x\n", obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200834 return IRQ_NONE;
835 }
836
837 iopte = iopte_offset(iopgd, da);
838
David Cohend594f1f2011-02-16 19:35:51 +0000839 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x "
840 "pte:0x%p *pte:0x%08x\n", obj->name, errs, da, iopgd, *iopgd,
841 iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200842
843 return IRQ_NONE;
844}
845
846static int device_match_by_alias(struct device *dev, void *data)
847{
848 struct iommu *obj = to_iommu(dev);
849 const char *name = data;
850
851 pr_debug("%s: %s %s\n", __func__, obj->name, name);
852
853 return strcmp(obj->name, name) == 0;
854}
855
856/**
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +0000857 * iommu_set_da_range - Set a valid device address range
858 * @obj: target iommu
859 * @start Start of valid range
860 * @end End of valid range
861 **/
862int iommu_set_da_range(struct iommu *obj, u32 start, u32 end)
863{
864
865 if (!obj)
866 return -EFAULT;
867
868 if (end < start || !PAGE_ALIGN(start | end))
869 return -EINVAL;
870
871 obj->da_start = start;
872 obj->da_end = end;
873
874 return 0;
875}
876EXPORT_SYMBOL_GPL(iommu_set_da_range);
877
878/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300879 * omap_find_iommu_device() - find an omap iommu device by name
880 * @name: name of the iommu device
881 *
882 * The generic iommu API requires the caller to provide the device
883 * he wishes to attach to a certain iommu domain.
884 *
885 * Drivers generally should not bother with this as it should just
886 * be taken care of by the DMA-API using dev_archdata.
887 *
888 * This function is provided as an interim solution until the latter
889 * materializes, and omap3isp is fully migrated to the DMA-API.
890 */
891struct device *omap_find_iommu_device(const char *name)
892{
893 return driver_find_device(&omap_iommu_driver.driver, NULL,
894 (void *)name,
895 device_match_by_alias);
896}
897EXPORT_SYMBOL_GPL(omap_find_iommu_device);
898
899/**
900 * omap_iommu_attach() - attach iommu device to an iommu domain
901 * @dev: target omap iommu device
902 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200903 **/
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300904static struct iommu *omap_iommu_attach(struct device *dev, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200905{
906 int err = -ENOMEM;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300907 struct iommu *obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200908
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300909 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200910
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300911 /* an iommu device can only be attached once */
912 if (++obj->refcount > 1) {
913 dev_err(dev, "%s: already attached!\n", obj->name);
914 err = -EBUSY;
915 goto err_enable;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200916 }
917
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300918 obj->iopgd = iopgd;
919 err = iommu_enable(obj);
920 if (err)
921 goto err_enable;
922 flush_iotlb_all(obj);
923
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200924 if (!try_module_get(obj->owner))
925 goto err_module;
926
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300927 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200928
929 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
930 return obj;
931
932err_module:
933 if (obj->refcount == 1)
934 iommu_disable(obj);
935err_enable:
936 obj->refcount--;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300937 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200938 return ERR_PTR(err);
939}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200940
941/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300942 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200943 * @obj: target iommu
944 **/
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300945static void omap_iommu_detach(struct iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200946{
Roel Kluinacf9d462010-01-08 10:29:05 -0800947 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200948 return;
949
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300950 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200951
952 if (--obj->refcount == 0)
953 iommu_disable(obj);
954
955 module_put(obj->owner);
956
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300957 obj->iopgd = NULL;
958
959 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200960
961 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
962}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200963
David Cohend594f1f2011-02-16 19:35:51 +0000964int iommu_set_isr(const char *name,
965 int (*isr)(struct iommu *obj, u32 da, u32 iommu_errs,
966 void *priv),
967 void *isr_priv)
968{
969 struct device *dev;
970 struct iommu *obj;
971
972 dev = driver_find_device(&omap_iommu_driver.driver, NULL, (void *)name,
973 device_match_by_alias);
974 if (!dev)
975 return -ENODEV;
976
977 obj = to_iommu(dev);
978 mutex_lock(&obj->iommu_lock);
979 if (obj->refcount != 0) {
980 mutex_unlock(&obj->iommu_lock);
981 return -EBUSY;
982 }
983 obj->isr = isr;
984 obj->isr_priv = isr_priv;
985 mutex_unlock(&obj->iommu_lock);
986
987 return 0;
988}
989EXPORT_SYMBOL_GPL(iommu_set_isr);
990
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200991/*
992 * OMAP Device MMU(IOMMU) detection
993 */
994static int __devinit omap_iommu_probe(struct platform_device *pdev)
995{
996 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200997 int irq;
998 struct iommu *obj;
999 struct resource *res;
1000 struct iommu_platform_data *pdata = pdev->dev.platform_data;
1001
1002 if (pdev->num_resources != 2)
1003 return -EINVAL;
1004
1005 obj = kzalloc(sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
1006 if (!obj)
1007 return -ENOMEM;
1008
1009 obj->clk = clk_get(&pdev->dev, pdata->clk_name);
1010 if (IS_ERR(obj->clk))
1011 goto err_clk;
1012
1013 obj->nr_tlb_entries = pdata->nr_tlb_entries;
1014 obj->name = pdata->name;
1015 obj->dev = &pdev->dev;
1016 obj->ctx = (void *)obj + sizeof(*obj);
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +00001017 obj->da_start = pdata->da_start;
1018 obj->da_end = pdata->da_end;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001019
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001020 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001021 mutex_init(&obj->mmap_lock);
1022 spin_lock_init(&obj->page_table_lock);
1023 INIT_LIST_HEAD(&obj->mmap);
1024
1025 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1026 if (!res) {
1027 err = -ENODEV;
1028 goto err_mem;
1029 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001030
1031 res = request_mem_region(res->start, resource_size(res),
1032 dev_name(&pdev->dev));
1033 if (!res) {
1034 err = -EIO;
1035 goto err_mem;
1036 }
1037
Aaro Koskinenda4a0f72011-03-14 12:28:32 +00001038 obj->regbase = ioremap(res->start, resource_size(res));
1039 if (!obj->regbase) {
1040 err = -ENOMEM;
1041 goto err_ioremap;
1042 }
1043
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001044 irq = platform_get_irq(pdev, 0);
1045 if (irq < 0) {
1046 err = -ENODEV;
1047 goto err_irq;
1048 }
1049 err = request_irq(irq, iommu_fault_handler, IRQF_SHARED,
1050 dev_name(&pdev->dev), obj);
1051 if (err < 0)
1052 goto err_irq;
1053 platform_set_drvdata(pdev, obj);
1054
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001055 dev_info(&pdev->dev, "%s registered\n", obj->name);
1056 return 0;
1057
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001058err_irq:
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001059 iounmap(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +00001060err_ioremap:
1061 release_mem_region(res->start, resource_size(res));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001062err_mem:
1063 clk_put(obj->clk);
1064err_clk:
1065 kfree(obj);
1066 return err;
1067}
1068
1069static int __devexit omap_iommu_remove(struct platform_device *pdev)
1070{
1071 int irq;
1072 struct resource *res;
1073 struct iommu *obj = platform_get_drvdata(pdev);
1074
1075 platform_set_drvdata(pdev, NULL);
1076
1077 iopgtable_clear_entry_all(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001078
1079 irq = platform_get_irq(pdev, 0);
1080 free_irq(irq, obj);
1081 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1082 release_mem_region(res->start, resource_size(res));
1083 iounmap(obj->regbase);
1084
1085 clk_put(obj->clk);
1086 dev_info(&pdev->dev, "%s removed\n", obj->name);
1087 kfree(obj);
1088 return 0;
1089}
1090
1091static struct platform_driver omap_iommu_driver = {
1092 .probe = omap_iommu_probe,
1093 .remove = __devexit_p(omap_iommu_remove),
1094 .driver = {
1095 .name = "omap-iommu",
1096 },
1097};
1098
1099static void iopte_cachep_ctor(void *iopte)
1100{
1101 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1102}
1103
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001104static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
1105 phys_addr_t pa, int order, int prot)
1106{
1107 struct omap_iommu_domain *omap_domain = domain->priv;
1108 struct iommu *oiommu = omap_domain->iommu_dev;
1109 struct device *dev = oiommu->dev;
1110 size_t bytes = PAGE_SIZE << order;
1111 struct iotlb_entry e;
1112 int omap_pgsz;
1113 u32 ret, flags;
1114
1115 /* we only support mapping a single iommu page for now */
1116 omap_pgsz = bytes_to_iopgsz(bytes);
1117 if (omap_pgsz < 0) {
1118 dev_err(dev, "invalid size to map: %d\n", bytes);
1119 return -EINVAL;
1120 }
1121
1122 dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes);
1123
1124 flags = omap_pgsz | prot;
1125
1126 iotlb_init_entry(&e, da, pa, flags);
1127
1128 ret = iopgtable_store_entry(oiommu, &e);
1129 if (ret) {
1130 dev_err(dev, "iopgtable_store_entry failed: %d\n", ret);
1131 return ret;
1132 }
1133
1134 return 0;
1135}
1136
1137static int omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1138 int order)
1139{
1140 struct omap_iommu_domain *omap_domain = domain->priv;
1141 struct iommu *oiommu = omap_domain->iommu_dev;
1142 struct device *dev = oiommu->dev;
1143 size_t bytes = PAGE_SIZE << order;
1144 size_t ret;
1145
1146 dev_dbg(dev, "unmapping da 0x%lx size 0x%x\n", da, bytes);
1147
1148 ret = iopgtable_clear_entry(oiommu, da);
1149 if (ret != bytes) {
1150 dev_err(dev, "entry @ 0x%lx was %d; not %d\n", da, ret, bytes);
1151 return -EINVAL;
1152 }
1153
1154 return 0;
1155}
1156
1157static int
1158omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1159{
1160 struct omap_iommu_domain *omap_domain = domain->priv;
1161 struct iommu *oiommu;
1162 int ret = 0;
1163
1164 spin_lock(&omap_domain->lock);
1165
1166 /* only a single device is supported per domain for now */
1167 if (omap_domain->iommu_dev) {
1168 dev_err(dev, "iommu domain is already attached\n");
1169 ret = -EBUSY;
1170 goto out;
1171 }
1172
1173 /* get a handle to and enable the omap iommu */
1174 oiommu = omap_iommu_attach(dev, omap_domain->pgtable);
1175 if (IS_ERR(oiommu)) {
1176 ret = PTR_ERR(oiommu);
1177 dev_err(dev, "can't get omap iommu: %d\n", ret);
1178 goto out;
1179 }
1180
1181 omap_domain->iommu_dev = oiommu;
1182
1183out:
1184 spin_unlock(&omap_domain->lock);
1185 return ret;
1186}
1187
1188static void omap_iommu_detach_dev(struct iommu_domain *domain,
1189 struct device *dev)
1190{
1191 struct omap_iommu_domain *omap_domain = domain->priv;
1192 struct iommu *oiommu = to_iommu(dev);
1193
1194 spin_lock(&omap_domain->lock);
1195
1196 /* only a single device is supported per domain for now */
1197 if (omap_domain->iommu_dev != oiommu) {
1198 dev_err(dev, "invalid iommu device\n");
1199 goto out;
1200 }
1201
1202 iopgtable_clear_entry_all(oiommu);
1203
1204 omap_iommu_detach(oiommu);
1205
1206 omap_domain->iommu_dev = NULL;
1207
1208out:
1209 spin_unlock(&omap_domain->lock);
1210}
1211
1212static int omap_iommu_domain_init(struct iommu_domain *domain)
1213{
1214 struct omap_iommu_domain *omap_domain;
1215
1216 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1217 if (!omap_domain) {
1218 pr_err("kzalloc failed\n");
1219 goto out;
1220 }
1221
1222 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1223 if (!omap_domain->pgtable) {
1224 pr_err("kzalloc failed\n");
1225 goto fail_nomem;
1226 }
1227
1228 /*
1229 * should never fail, but please keep this around to ensure
1230 * we keep the hardware happy
1231 */
1232 BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
1233
1234 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1235 spin_lock_init(&omap_domain->lock);
1236
1237 domain->priv = omap_domain;
1238
1239 return 0;
1240
1241fail_nomem:
1242 kfree(omap_domain);
1243out:
1244 return -ENOMEM;
1245}
1246
1247/* assume device was already detached */
1248static void omap_iommu_domain_destroy(struct iommu_domain *domain)
1249{
1250 struct omap_iommu_domain *omap_domain = domain->priv;
1251
1252 domain->priv = NULL;
1253
1254 kfree(omap_domain->pgtable);
1255 kfree(omap_domain);
1256}
1257
1258static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
1259 unsigned long da)
1260{
1261 struct omap_iommu_domain *omap_domain = domain->priv;
1262 struct iommu *oiommu = omap_domain->iommu_dev;
1263 struct device *dev = oiommu->dev;
1264 u32 *pgd, *pte;
1265 phys_addr_t ret = 0;
1266
1267 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1268
1269 if (pte) {
1270 if (iopte_is_small(*pte))
1271 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1272 else if (iopte_is_large(*pte))
1273 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1274 else
1275 dev_err(dev, "bogus pte 0x%x", *pte);
1276 } else {
1277 if (iopgd_is_section(*pgd))
1278 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1279 else if (iopgd_is_super(*pgd))
1280 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1281 else
1282 dev_err(dev, "bogus pgd 0x%x", *pgd);
1283 }
1284
1285 return ret;
1286}
1287
1288static int omap_iommu_domain_has_cap(struct iommu_domain *domain,
1289 unsigned long cap)
1290{
1291 return 0;
1292}
1293
1294static struct iommu_ops omap_iommu_ops = {
1295 .domain_init = omap_iommu_domain_init,
1296 .domain_destroy = omap_iommu_domain_destroy,
1297 .attach_dev = omap_iommu_attach_dev,
1298 .detach_dev = omap_iommu_detach_dev,
1299 .map = omap_iommu_map,
1300 .unmap = omap_iommu_unmap,
1301 .iova_to_phys = omap_iommu_iova_to_phys,
1302 .domain_has_cap = omap_iommu_domain_has_cap,
1303};
1304
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001305static int __init omap_iommu_init(void)
1306{
1307 struct kmem_cache *p;
1308 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1309 size_t align = 1 << 10; /* L2 pagetable alignement */
1310
1311 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1312 iopte_cachep_ctor);
1313 if (!p)
1314 return -ENOMEM;
1315 iopte_cachep = p;
1316
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001317 register_iommu(&omap_iommu_ops);
1318
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001319 return platform_driver_register(&omap_iommu_driver);
1320}
1321module_init(omap_iommu_init);
1322
1323static void __exit omap_iommu_exit(void)
1324{
1325 kmem_cache_destroy(iopte_cachep);
1326
1327 platform_driver_unregister(&omap_iommu_driver);
1328}
1329module_exit(omap_iommu_exit);
1330
1331MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1332MODULE_ALIAS("platform:omap-iommu");
1333MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1334MODULE_LICENSE("GPL v2");