blob: ad80b1d0d099575d53482eaff5c56a76f905a6aa [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;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030045 struct omap_iommu *iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030046 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/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030056 * omap_install_iommu_arch - Install archtecure specific iommu functions
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020057 * @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 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030062int omap_install_iommu_arch(const struct iommu_functions *ops)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020063{
64 if (arch_iommu)
65 return -EBUSY;
66
67 arch_iommu = ops;
68 return 0;
69}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030070EXPORT_SYMBOL_GPL(omap_install_iommu_arch);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020071
72/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030073 * omap_uninstall_iommu_arch - Uninstall archtecure specific iommu functions
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020074 * @ops: a pointer to architecture specific iommu functions
75 *
76 * This interface uninstalls the iommu algorighm installed previously.
77 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030078void omap_uninstall_iommu_arch(const struct iommu_functions *ops)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020079{
80 if (arch_iommu != ops)
81 pr_err("%s: not your arch\n", __func__);
82
83 arch_iommu = NULL;
84}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030085EXPORT_SYMBOL_GPL(omap_uninstall_iommu_arch);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020086
87/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030088 * omap_iommu_save_ctx - Save registers for pm off-mode support
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020089 * @obj: target iommu
90 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030091void omap_iommu_save_ctx(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020092{
93 arch_iommu->save_ctx(obj);
94}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030095EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020096
97/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030098 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020099 * @obj: target iommu
100 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300101void omap_iommu_restore_ctx(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200102{
103 arch_iommu->restore_ctx(obj);
104}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300105EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200106
107/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300108 * omap_iommu_arch_version - Return running iommu arch version
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200109 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300110u32 omap_iommu_arch_version(void)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200111{
112 return arch_iommu->version;
113}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300114EXPORT_SYMBOL_GPL(omap_iommu_arch_version);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200115
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300116static int iommu_enable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200117{
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
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300134static void iommu_disable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200135{
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 */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300149void omap_iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200150{
151 BUG_ON(!cr || !e);
152
153 arch_iommu->cr_to_e(cr, e);
154}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300155EXPORT_SYMBOL_GPL(omap_iotlb_cr_to_e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200156
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
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300165static inline struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200166 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
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300184static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200185{
186 return arch_iommu->fault_isr(obj, da);
187}
188
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300189static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200190{
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
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300200static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200201{
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
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300210static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200211{
212 arch_iommu->tlb_read_cr(obj, cr);
213}
214
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300215static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200216{
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 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300229static inline ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200230 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 */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300238static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000239{
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-Cohen6c32df42011-08-17 22:57:56 +0300257static int load_iotlb_entry(struct omap_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
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300316static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300317{
318 return 0;
319}
320
321#endif /* !PREFETCH_IOTLB */
322
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300323static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300324{
325 return load_iotlb_entry(obj, e);
326}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200327
328/**
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-Cohen6c32df42011-08-17 22:57:56 +0300335static void flush_iotlb_page(struct omap_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_all - Clear all iommu tlb entries
367 * @obj: target iommu
368 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300369static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200370{
371 struct iotlb_lock l;
372
373 clk_enable(obj->clk);
374
375 l.base = 0;
376 l.vict = 0;
377 iotlb_lock_set(obj, &l);
378
379 iommu_write_reg(obj, 1, MMU_GFLUSH);
380
381 clk_disable(obj->clk);
382}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200383
Arnd Bergmanne4efd942011-10-02 14:34:05 -0400384#if defined(CONFIG_OMAP_IOMMU_DEBUG) || defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
Kanigeri, Hariddfa9752010-05-24 02:01:51 +0000385
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300386ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200387{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200388 if (!obj || !buf)
389 return -EINVAL;
390
391 clk_enable(obj->clk);
392
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700393 bytes = arch_iommu->dump_ctx(obj, buf, bytes);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200394
395 clk_disable(obj->clk);
396
397 return bytes;
398}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300399EXPORT_SYMBOL_GPL(omap_iommu_dump_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200400
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300401static int
402__dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200403{
404 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000405 struct iotlb_lock saved;
406 struct cr_regs tmp;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200407 struct cr_regs *p = crs;
408
409 clk_enable(obj->clk);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200410 iotlb_lock_get(obj, &saved);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200411
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000412 for_each_iotlb_cr(obj, num, i, tmp) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200413 if (!iotlb_cr_valid(&tmp))
414 continue;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200415 *p++ = tmp;
416 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000417
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200418 iotlb_lock_set(obj, &saved);
419 clk_disable(obj->clk);
420
421 return p - crs;
422}
423
424/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300425 * omap_dump_tlb_entries - dump cr arrays to given buffer
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200426 * @obj: target iommu
427 * @buf: output buffer
428 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300429size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200430{
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700431 int i, num;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200432 struct cr_regs *cr;
433 char *p = buf;
434
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700435 num = bytes / sizeof(*cr);
436 num = min(obj->nr_tlb_entries, num);
437
438 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200439 if (!cr)
440 return 0;
441
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700442 num = __dump_tlb_entries(obj, cr, num);
443 for (i = 0; i < num; i++)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200444 p += iotlb_dump_cr(obj, cr + i, p);
445 kfree(cr);
446
447 return p - buf;
448}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300449EXPORT_SYMBOL_GPL(omap_dump_tlb_entries);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200450
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300451int omap_foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200452{
453 return driver_for_each_device(&omap_iommu_driver.driver,
454 NULL, data, fn);
455}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300456EXPORT_SYMBOL_GPL(omap_foreach_iommu_device);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200457
458#endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
459
460/*
461 * H/W pagetable operations
462 */
463static void flush_iopgd_range(u32 *first, u32 *last)
464{
465 /* FIXME: L2 cache should be taken care of if it exists */
466 do {
467 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
468 : : "r" (first));
469 first += L1_CACHE_BYTES / sizeof(*first);
470 } while (first <= last);
471}
472
473static void flush_iopte_range(u32 *first, u32 *last)
474{
475 /* FIXME: L2 cache should be taken care of if it exists */
476 do {
477 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
478 : : "r" (first));
479 first += L1_CACHE_BYTES / sizeof(*first);
480 } while (first <= last);
481}
482
483static void iopte_free(u32 *iopte)
484{
485 /* Note: freed iopte's must be clean ready for re-use */
486 kmem_cache_free(iopte_cachep, iopte);
487}
488
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300489static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200490{
491 u32 *iopte;
492
493 /* a table has already existed */
494 if (*iopgd)
495 goto pte_ready;
496
497 /*
498 * do the allocation outside the page table lock
499 */
500 spin_unlock(&obj->page_table_lock);
501 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
502 spin_lock(&obj->page_table_lock);
503
504 if (!*iopgd) {
505 if (!iopte)
506 return ERR_PTR(-ENOMEM);
507
508 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
509 flush_iopgd_range(iopgd, iopgd);
510
511 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
512 } else {
513 /* We raced, free the reduniovant table */
514 iopte_free(iopte);
515 }
516
517pte_ready:
518 iopte = iopte_offset(iopgd, da);
519
520 dev_vdbg(obj->dev,
521 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
522 __func__, da, iopgd, *iopgd, iopte, *iopte);
523
524 return iopte;
525}
526
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300527static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200528{
529 u32 *iopgd = iopgd_offset(obj, da);
530
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300531 if ((da | pa) & ~IOSECTION_MASK) {
532 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
533 __func__, da, pa, IOSECTION_SIZE);
534 return -EINVAL;
535 }
536
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200537 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
538 flush_iopgd_range(iopgd, iopgd);
539 return 0;
540}
541
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300542static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200543{
544 u32 *iopgd = iopgd_offset(obj, da);
545 int i;
546
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300547 if ((da | pa) & ~IOSUPER_MASK) {
548 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
549 __func__, da, pa, IOSUPER_SIZE);
550 return -EINVAL;
551 }
552
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200553 for (i = 0; i < 16; i++)
554 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
555 flush_iopgd_range(iopgd, iopgd + 15);
556 return 0;
557}
558
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300559static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200560{
561 u32 *iopgd = iopgd_offset(obj, da);
562 u32 *iopte = iopte_alloc(obj, iopgd, da);
563
564 if (IS_ERR(iopte))
565 return PTR_ERR(iopte);
566
567 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
568 flush_iopte_range(iopte, iopte);
569
570 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
571 __func__, da, pa, iopte, *iopte);
572
573 return 0;
574}
575
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300576static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200577{
578 u32 *iopgd = iopgd_offset(obj, da);
579 u32 *iopte = iopte_alloc(obj, iopgd, da);
580 int i;
581
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300582 if ((da | pa) & ~IOLARGE_MASK) {
583 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
584 __func__, da, pa, IOLARGE_SIZE);
585 return -EINVAL;
586 }
587
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200588 if (IS_ERR(iopte))
589 return PTR_ERR(iopte);
590
591 for (i = 0; i < 16; i++)
592 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
593 flush_iopte_range(iopte, iopte + 15);
594 return 0;
595}
596
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300597static int
598iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200599{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300600 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200601 u32 prot;
602 int err;
603
604 if (!obj || !e)
605 return -EINVAL;
606
607 switch (e->pgsz) {
608 case MMU_CAM_PGSZ_16M:
609 fn = iopgd_alloc_super;
610 break;
611 case MMU_CAM_PGSZ_1M:
612 fn = iopgd_alloc_section;
613 break;
614 case MMU_CAM_PGSZ_64K:
615 fn = iopte_alloc_large;
616 break;
617 case MMU_CAM_PGSZ_4K:
618 fn = iopte_alloc_page;
619 break;
620 default:
621 fn = NULL;
622 BUG();
623 break;
624 }
625
626 prot = get_iopte_attr(e);
627
628 spin_lock(&obj->page_table_lock);
629 err = fn(obj, e->da, e->pa, prot);
630 spin_unlock(&obj->page_table_lock);
631
632 return err;
633}
634
635/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300636 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200637 * @obj: target iommu
638 * @e: an iommu tlb entry info
639 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300640int omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200641{
642 int err;
643
644 flush_iotlb_page(obj, e->da);
645 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200646 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300647 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200648 return err;
649}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300650EXPORT_SYMBOL_GPL(omap_iopgtable_store_entry);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200651
652/**
653 * iopgtable_lookup_entry - Lookup an iommu pte entry
654 * @obj: target iommu
655 * @da: iommu device virtual address
656 * @ppgd: iommu pgd entry pointer to be returned
657 * @ppte: iommu pte entry pointer to be returned
658 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300659static void
660iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200661{
662 u32 *iopgd, *iopte = NULL;
663
664 iopgd = iopgd_offset(obj, da);
665 if (!*iopgd)
666 goto out;
667
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300668 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200669 iopte = iopte_offset(iopgd, da);
670out:
671 *ppgd = iopgd;
672 *ppte = iopte;
673}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200674
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300675static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200676{
677 size_t bytes;
678 u32 *iopgd = iopgd_offset(obj, da);
679 int nent = 1;
680
681 if (!*iopgd)
682 return 0;
683
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300684 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200685 int i;
686 u32 *iopte = iopte_offset(iopgd, da);
687
688 bytes = IOPTE_SIZE;
689 if (*iopte & IOPTE_LARGE) {
690 nent *= 16;
691 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800692 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200693 }
694 bytes *= nent;
695 memset(iopte, 0, nent * sizeof(*iopte));
696 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
697
698 /*
699 * do table walk to check if this table is necessary or not
700 */
701 iopte = iopte_offset(iopgd, 0);
702 for (i = 0; i < PTRS_PER_IOPTE; i++)
703 if (iopte[i])
704 goto out;
705
706 iopte_free(iopte);
707 nent = 1; /* for the next L1 entry */
708 } else {
709 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700710 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200711 nent *= 16;
712 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800713 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200714 }
715 bytes *= nent;
716 }
717 memset(iopgd, 0, nent * sizeof(*iopgd));
718 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
719out:
720 return bytes;
721}
722
723/**
724 * iopgtable_clear_entry - Remove an iommu pte entry
725 * @obj: target iommu
726 * @da: iommu device virtual address
727 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300728static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200729{
730 size_t bytes;
731
732 spin_lock(&obj->page_table_lock);
733
734 bytes = iopgtable_clear_entry_core(obj, da);
735 flush_iotlb_page(obj, da);
736
737 spin_unlock(&obj->page_table_lock);
738
739 return bytes;
740}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200741
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300742static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200743{
744 int i;
745
746 spin_lock(&obj->page_table_lock);
747
748 for (i = 0; i < PTRS_PER_IOPGD; i++) {
749 u32 da;
750 u32 *iopgd;
751
752 da = i << IOPGD_SHIFT;
753 iopgd = iopgd_offset(obj, da);
754
755 if (!*iopgd)
756 continue;
757
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300758 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200759 iopte_free(iopte_offset(iopgd, 0));
760
761 *iopgd = 0;
762 flush_iopgd_range(iopgd, iopgd);
763 }
764
765 flush_iotlb_all(obj);
766
767 spin_unlock(&obj->page_table_lock);
768}
769
770/*
771 * Device IOMMU generic operations
772 */
773static irqreturn_t iommu_fault_handler(int irq, void *data)
774{
David Cohend594f1f2011-02-16 19:35:51 +0000775 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200776 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300777 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400778 struct iommu_domain *domain = obj->domain;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200779
780 if (!obj->refcount)
781 return IRQ_NONE;
782
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200783 clk_enable(obj->clk);
David Cohend594f1f2011-02-16 19:35:51 +0000784 errs = iommu_report_fault(obj, &da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200785 clk_disable(obj->clk);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200786 if (errs == 0)
787 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000788
789 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400790 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200791 return IRQ_HANDLED;
792
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000793 iommu_disable(obj);
794
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200795 iopgd = iopgd_offset(obj, da);
796
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300797 if (!iopgd_is_table(*iopgd)) {
David Cohend594f1f2011-02-16 19:35:51 +0000798 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p "
799 "*pgd:px%08x\n", obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200800 return IRQ_NONE;
801 }
802
803 iopte = iopte_offset(iopgd, da);
804
David Cohend594f1f2011-02-16 19:35:51 +0000805 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x "
806 "pte:0x%p *pte:0x%08x\n", obj->name, errs, da, iopgd, *iopgd,
807 iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200808
809 return IRQ_NONE;
810}
811
812static int device_match_by_alias(struct device *dev, void *data)
813{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300814 struct omap_iommu *obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200815 const char *name = data;
816
817 pr_debug("%s: %s %s\n", __func__, obj->name, name);
818
819 return strcmp(obj->name, name) == 0;
820}
821
822/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300823 * omap_find_iommu_device() - find an omap iommu device by name
824 * @name: name of the iommu device
825 *
826 * The generic iommu API requires the caller to provide the device
827 * he wishes to attach to a certain iommu domain.
828 *
829 * Drivers generally should not bother with this as it should just
830 * be taken care of by the DMA-API using dev_archdata.
831 *
832 * This function is provided as an interim solution until the latter
833 * materializes, and omap3isp is fully migrated to the DMA-API.
834 */
835struct device *omap_find_iommu_device(const char *name)
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +0000836{
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300837 return driver_find_device(&omap_iommu_driver.driver, NULL,
838 (void *)name,
839 device_match_by_alias);
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +0000840}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300841EXPORT_SYMBOL_GPL(omap_find_iommu_device);
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +0000842
843/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300844 * omap_iommu_attach() - attach iommu device to an iommu domain
845 * @dev: target omap iommu device
846 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200847 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300848static struct omap_iommu *omap_iommu_attach(struct device *dev, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200849{
850 int err = -ENOMEM;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300851 struct omap_iommu *obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200852
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300853 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200854
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300855 /* an iommu device can only be attached once */
856 if (++obj->refcount > 1) {
857 dev_err(dev, "%s: already attached!\n", obj->name);
858 err = -EBUSY;
859 goto err_enable;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200860 }
861
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300862 obj->iopgd = iopgd;
863 err = iommu_enable(obj);
864 if (err)
865 goto err_enable;
866 flush_iotlb_all(obj);
867
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200868 if (!try_module_get(obj->owner))
869 goto err_module;
870
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300871 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200872
873 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
874 return obj;
875
876err_module:
877 if (obj->refcount == 1)
878 iommu_disable(obj);
879err_enable:
880 obj->refcount--;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300881 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200882 return ERR_PTR(err);
883}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200884
885/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300886 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200887 * @obj: target iommu
888 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300889static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200890{
Roel Kluinacf9d462010-01-08 10:29:05 -0800891 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200892 return;
893
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300894 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200895
896 if (--obj->refcount == 0)
897 iommu_disable(obj);
898
899 module_put(obj->owner);
900
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300901 obj->iopgd = NULL;
902
903 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200904
905 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
906}
David Cohend594f1f2011-02-16 19:35:51 +0000907
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200908/*
909 * OMAP Device MMU(IOMMU) detection
910 */
911static int __devinit omap_iommu_probe(struct platform_device *pdev)
912{
913 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200914 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300915 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200916 struct resource *res;
917 struct iommu_platform_data *pdata = pdev->dev.platform_data;
918
919 if (pdev->num_resources != 2)
920 return -EINVAL;
921
922 obj = kzalloc(sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
923 if (!obj)
924 return -ENOMEM;
925
926 obj->clk = clk_get(&pdev->dev, pdata->clk_name);
927 if (IS_ERR(obj->clk))
928 goto err_clk;
929
930 obj->nr_tlb_entries = pdata->nr_tlb_entries;
931 obj->name = pdata->name;
932 obj->dev = &pdev->dev;
933 obj->ctx = (void *)obj + sizeof(*obj);
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +0000934 obj->da_start = pdata->da_start;
935 obj->da_end = pdata->da_end;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200936
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300937 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200938 mutex_init(&obj->mmap_lock);
939 spin_lock_init(&obj->page_table_lock);
940 INIT_LIST_HEAD(&obj->mmap);
941
942 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
943 if (!res) {
944 err = -ENODEV;
945 goto err_mem;
946 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200947
948 res = request_mem_region(res->start, resource_size(res),
949 dev_name(&pdev->dev));
950 if (!res) {
951 err = -EIO;
952 goto err_mem;
953 }
954
Aaro Koskinenda4a0f72011-03-14 12:28:32 +0000955 obj->regbase = ioremap(res->start, resource_size(res));
956 if (!obj->regbase) {
957 err = -ENOMEM;
958 goto err_ioremap;
959 }
960
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200961 irq = platform_get_irq(pdev, 0);
962 if (irq < 0) {
963 err = -ENODEV;
964 goto err_irq;
965 }
966 err = request_irq(irq, iommu_fault_handler, IRQF_SHARED,
967 dev_name(&pdev->dev), obj);
968 if (err < 0)
969 goto err_irq;
970 platform_set_drvdata(pdev, obj);
971
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200972 dev_info(&pdev->dev, "%s registered\n", obj->name);
973 return 0;
974
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200975err_irq:
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200976 iounmap(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +0000977err_ioremap:
978 release_mem_region(res->start, resource_size(res));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200979err_mem:
980 clk_put(obj->clk);
981err_clk:
982 kfree(obj);
983 return err;
984}
985
986static int __devexit omap_iommu_remove(struct platform_device *pdev)
987{
988 int irq;
989 struct resource *res;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300990 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200991
992 platform_set_drvdata(pdev, NULL);
993
994 iopgtable_clear_entry_all(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200995
996 irq = platform_get_irq(pdev, 0);
997 free_irq(irq, obj);
998 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
999 release_mem_region(res->start, resource_size(res));
1000 iounmap(obj->regbase);
1001
1002 clk_put(obj->clk);
1003 dev_info(&pdev->dev, "%s removed\n", obj->name);
1004 kfree(obj);
1005 return 0;
1006}
1007
1008static struct platform_driver omap_iommu_driver = {
1009 .probe = omap_iommu_probe,
1010 .remove = __devexit_p(omap_iommu_remove),
1011 .driver = {
1012 .name = "omap-iommu",
1013 },
1014};
1015
1016static void iopte_cachep_ctor(void *iopte)
1017{
1018 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1019}
1020
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001021static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001022 phys_addr_t pa, size_t bytes, int prot)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001023{
1024 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001025 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001026 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001027 struct iotlb_entry e;
1028 int omap_pgsz;
1029 u32 ret, flags;
1030
1031 /* we only support mapping a single iommu page for now */
1032 omap_pgsz = bytes_to_iopgsz(bytes);
1033 if (omap_pgsz < 0) {
1034 dev_err(dev, "invalid size to map: %d\n", bytes);
1035 return -EINVAL;
1036 }
1037
1038 dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes);
1039
1040 flags = omap_pgsz | prot;
1041
1042 iotlb_init_entry(&e, da, pa, flags);
1043
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001044 ret = omap_iopgtable_store_entry(oiommu, &e);
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001045 if (ret)
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001046 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001047
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001048 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001049}
1050
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001051static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1052 size_t size)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001053{
1054 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001055 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001056 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001057
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001058 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001059
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001060 return iopgtable_clear_entry(oiommu, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001061}
1062
1063static int
1064omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1065{
1066 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001067 struct omap_iommu *oiommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001068 int ret = 0;
1069
1070 spin_lock(&omap_domain->lock);
1071
1072 /* only a single device is supported per domain for now */
1073 if (omap_domain->iommu_dev) {
1074 dev_err(dev, "iommu domain is already attached\n");
1075 ret = -EBUSY;
1076 goto out;
1077 }
1078
1079 /* get a handle to and enable the omap iommu */
1080 oiommu = omap_iommu_attach(dev, omap_domain->pgtable);
1081 if (IS_ERR(oiommu)) {
1082 ret = PTR_ERR(oiommu);
1083 dev_err(dev, "can't get omap iommu: %d\n", ret);
1084 goto out;
1085 }
1086
1087 omap_domain->iommu_dev = oiommu;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -04001088 oiommu->domain = domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001089
1090out:
1091 spin_unlock(&omap_domain->lock);
1092 return ret;
1093}
1094
1095static void omap_iommu_detach_dev(struct iommu_domain *domain,
1096 struct device *dev)
1097{
1098 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001099 struct omap_iommu *oiommu = to_iommu(dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001100
1101 spin_lock(&omap_domain->lock);
1102
1103 /* only a single device is supported per domain for now */
1104 if (omap_domain->iommu_dev != oiommu) {
1105 dev_err(dev, "invalid iommu device\n");
1106 goto out;
1107 }
1108
1109 iopgtable_clear_entry_all(oiommu);
1110
1111 omap_iommu_detach(oiommu);
1112
1113 omap_domain->iommu_dev = NULL;
1114
1115out:
1116 spin_unlock(&omap_domain->lock);
1117}
1118
1119static int omap_iommu_domain_init(struct iommu_domain *domain)
1120{
1121 struct omap_iommu_domain *omap_domain;
1122
1123 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1124 if (!omap_domain) {
1125 pr_err("kzalloc failed\n");
1126 goto out;
1127 }
1128
1129 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1130 if (!omap_domain->pgtable) {
1131 pr_err("kzalloc failed\n");
1132 goto fail_nomem;
1133 }
1134
1135 /*
1136 * should never fail, but please keep this around to ensure
1137 * we keep the hardware happy
1138 */
1139 BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
1140
1141 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1142 spin_lock_init(&omap_domain->lock);
1143
1144 domain->priv = omap_domain;
1145
1146 return 0;
1147
1148fail_nomem:
1149 kfree(omap_domain);
1150out:
1151 return -ENOMEM;
1152}
1153
1154/* assume device was already detached */
1155static void omap_iommu_domain_destroy(struct iommu_domain *domain)
1156{
1157 struct omap_iommu_domain *omap_domain = domain->priv;
1158
1159 domain->priv = NULL;
1160
1161 kfree(omap_domain->pgtable);
1162 kfree(omap_domain);
1163}
1164
1165static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
1166 unsigned long da)
1167{
1168 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001169 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001170 struct device *dev = oiommu->dev;
1171 u32 *pgd, *pte;
1172 phys_addr_t ret = 0;
1173
1174 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1175
1176 if (pte) {
1177 if (iopte_is_small(*pte))
1178 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1179 else if (iopte_is_large(*pte))
1180 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1181 else
1182 dev_err(dev, "bogus pte 0x%x", *pte);
1183 } else {
1184 if (iopgd_is_section(*pgd))
1185 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1186 else if (iopgd_is_super(*pgd))
1187 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1188 else
1189 dev_err(dev, "bogus pgd 0x%x", *pgd);
1190 }
1191
1192 return ret;
1193}
1194
1195static int omap_iommu_domain_has_cap(struct iommu_domain *domain,
1196 unsigned long cap)
1197{
1198 return 0;
1199}
1200
1201static struct iommu_ops omap_iommu_ops = {
1202 .domain_init = omap_iommu_domain_init,
1203 .domain_destroy = omap_iommu_domain_destroy,
1204 .attach_dev = omap_iommu_attach_dev,
1205 .detach_dev = omap_iommu_detach_dev,
1206 .map = omap_iommu_map,
1207 .unmap = omap_iommu_unmap,
1208 .iova_to_phys = omap_iommu_iova_to_phys,
1209 .domain_has_cap = omap_iommu_domain_has_cap,
1210};
1211
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001212static int __init omap_iommu_init(void)
1213{
1214 struct kmem_cache *p;
1215 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1216 size_t align = 1 << 10; /* L2 pagetable alignement */
1217
1218 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1219 iopte_cachep_ctor);
1220 if (!p)
1221 return -ENOMEM;
1222 iopte_cachep = p;
1223
Joerg Roedela65bc642011-09-06 17:56:07 +02001224 bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001225
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001226 return platform_driver_register(&omap_iommu_driver);
1227}
1228module_init(omap_iommu_init);
1229
1230static void __exit omap_iommu_exit(void)
1231{
1232 kmem_cache_destroy(iopte_cachep);
1233
1234 platform_driver_unregister(&omap_iommu_driver);
1235}
1236module_exit(omap_iommu_exit);
1237
1238MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1239MODULE_ALIAS("platform:omap-iommu");
1240MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1241MODULE_LICENSE("GPL v2");