blob: b7f863d72c08420d45cfc6c533951bce085694e6 [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
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020089 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020090 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020091void omap_iommu_save_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020092{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020093 struct omap_iommu *obj = dev_to_omap_iommu(dev);
94
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020095 arch_iommu->save_ctx(obj);
96}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030097EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020098
99/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300100 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200101 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200102 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200103void omap_iommu_restore_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200104{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200105 struct omap_iommu *obj = dev_to_omap_iommu(dev);
106
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200107 arch_iommu->restore_ctx(obj);
108}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300109EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200110
111/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300112 * omap_iommu_arch_version - Return running iommu arch version
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200113 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300114u32 omap_iommu_arch_version(void)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200115{
116 return arch_iommu->version;
117}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300118EXPORT_SYMBOL_GPL(omap_iommu_arch_version);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200119
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300120static int iommu_enable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200121{
122 int err;
123
124 if (!obj)
125 return -EINVAL;
126
Martin Hostettleref4815a2011-02-24 12:51:31 -0800127 if (!arch_iommu)
128 return -ENODEV;
129
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200130 clk_enable(obj->clk);
131
132 err = arch_iommu->enable(obj);
133
134 clk_disable(obj->clk);
135 return err;
136}
137
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300138static void iommu_disable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200139{
140 if (!obj)
141 return;
142
143 clk_enable(obj->clk);
144
145 arch_iommu->disable(obj);
146
147 clk_disable(obj->clk);
148}
149
150/*
151 * TLB operations
152 */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300153void omap_iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200154{
155 BUG_ON(!cr || !e);
156
157 arch_iommu->cr_to_e(cr, e);
158}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300159EXPORT_SYMBOL_GPL(omap_iotlb_cr_to_e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200160
161static inline int iotlb_cr_valid(struct cr_regs *cr)
162{
163 if (!cr)
164 return -EINVAL;
165
166 return arch_iommu->cr_valid(cr);
167}
168
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300169static inline struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200170 struct iotlb_entry *e)
171{
172 if (!e)
173 return NULL;
174
175 return arch_iommu->alloc_cr(obj, e);
176}
177
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300178static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200179{
180 return arch_iommu->cr_to_virt(cr);
181}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200182
183static u32 get_iopte_attr(struct iotlb_entry *e)
184{
185 return arch_iommu->get_pte_attr(e);
186}
187
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300188static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200189{
190 return arch_iommu->fault_isr(obj, da);
191}
192
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300193static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200194{
195 u32 val;
196
197 val = iommu_read_reg(obj, MMU_LOCK);
198
199 l->base = MMU_LOCK_BASE(val);
200 l->vict = MMU_LOCK_VICT(val);
201
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200202}
203
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300204static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200205{
206 u32 val;
207
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200208 val = (l->base << MMU_LOCK_BASE_SHIFT);
209 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
210
211 iommu_write_reg(obj, val, MMU_LOCK);
212}
213
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300214static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200215{
216 arch_iommu->tlb_read_cr(obj, cr);
217}
218
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300219static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200220{
221 arch_iommu->tlb_load_cr(obj, cr);
222
223 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
224 iommu_write_reg(obj, 1, MMU_LD_TLB);
225}
226
227/**
228 * iotlb_dump_cr - Dump an iommu tlb entry into buf
229 * @obj: target iommu
230 * @cr: contents of cam and ram register
231 * @buf: output buffer
232 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300233static inline ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200234 char *buf)
235{
236 BUG_ON(!cr || !buf);
237
238 return arch_iommu->dump_cr(obj, cr, buf);
239}
240
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000241/* only used in iotlb iteration for-loop */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300242static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000243{
244 struct cr_regs cr;
245 struct iotlb_lock l;
246
247 iotlb_lock_get(obj, &l);
248 l.vict = n;
249 iotlb_lock_set(obj, &l);
250 iotlb_read_cr(obj, &cr);
251
252 return cr;
253}
254
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200255/**
256 * load_iotlb_entry - Set an iommu tlb entry
257 * @obj: target iommu
258 * @e: an iommu tlb entry info
259 **/
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300260#ifdef PREFETCH_IOTLB
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300261static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200262{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200263 int err = 0;
264 struct iotlb_lock l;
265 struct cr_regs *cr;
266
267 if (!obj || !obj->nr_tlb_entries || !e)
268 return -EINVAL;
269
270 clk_enable(obj->clk);
271
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000272 iotlb_lock_get(obj, &l);
273 if (l.base == obj->nr_tlb_entries) {
274 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200275 err = -EBUSY;
276 goto out;
277 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000278 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000279 int i;
280 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000281
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000282 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000283 if (!iotlb_cr_valid(&tmp))
284 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000285
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000286 if (i == obj->nr_tlb_entries) {
287 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
288 err = -EBUSY;
289 goto out;
290 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000291
292 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000293 } else {
294 l.vict = l.base;
295 iotlb_lock_set(obj, &l);
296 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200297
298 cr = iotlb_alloc_cr(obj, e);
299 if (IS_ERR(cr)) {
300 clk_disable(obj->clk);
301 return PTR_ERR(cr);
302 }
303
304 iotlb_load_cr(obj, cr);
305 kfree(cr);
306
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000307 if (e->prsvd)
308 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200309 /* increment victim for next tlb load */
310 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000311 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200312 iotlb_lock_set(obj, &l);
313out:
314 clk_disable(obj->clk);
315 return err;
316}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200317
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300318#else /* !PREFETCH_IOTLB */
319
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300320static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300321{
322 return 0;
323}
324
325#endif /* !PREFETCH_IOTLB */
326
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300327static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300328{
329 return load_iotlb_entry(obj, e);
330}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200331
332/**
333 * flush_iotlb_page - Clear an iommu tlb entry
334 * @obj: target iommu
335 * @da: iommu device virtual address
336 *
337 * Clear an iommu tlb entry which includes 'da' address.
338 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300339static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200340{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200341 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000342 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200343
344 clk_enable(obj->clk);
345
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000346 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200347 u32 start;
348 size_t bytes;
349
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200350 if (!iotlb_cr_valid(&cr))
351 continue;
352
353 start = iotlb_cr_to_virt(&cr);
354 bytes = iopgsz_to_bytes(cr.cam & 3);
355
356 if ((start <= da) && (da < start + bytes)) {
357 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
358 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000359 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200360 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
361 }
362 }
363 clk_disable(obj->clk);
364
365 if (i == obj->nr_tlb_entries)
366 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
367}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200368
369/**
370 * flush_iotlb_all - Clear all iommu tlb entries
371 * @obj: target iommu
372 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300373static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200374{
375 struct iotlb_lock l;
376
377 clk_enable(obj->clk);
378
379 l.base = 0;
380 l.vict = 0;
381 iotlb_lock_set(obj, &l);
382
383 iommu_write_reg(obj, 1, MMU_GFLUSH);
384
385 clk_disable(obj->clk);
386}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200387
Arnd Bergmanne4efd942011-10-02 14:34:05 -0400388#if defined(CONFIG_OMAP_IOMMU_DEBUG) || defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
Kanigeri, Hariddfa9752010-05-24 02:01:51 +0000389
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300390ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200391{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200392 if (!obj || !buf)
393 return -EINVAL;
394
395 clk_enable(obj->clk);
396
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700397 bytes = arch_iommu->dump_ctx(obj, buf, bytes);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200398
399 clk_disable(obj->clk);
400
401 return bytes;
402}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300403EXPORT_SYMBOL_GPL(omap_iommu_dump_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200404
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300405static int
406__dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200407{
408 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000409 struct iotlb_lock saved;
410 struct cr_regs tmp;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200411 struct cr_regs *p = crs;
412
413 clk_enable(obj->clk);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200414 iotlb_lock_get(obj, &saved);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200415
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000416 for_each_iotlb_cr(obj, num, i, tmp) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200417 if (!iotlb_cr_valid(&tmp))
418 continue;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200419 *p++ = tmp;
420 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000421
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200422 iotlb_lock_set(obj, &saved);
423 clk_disable(obj->clk);
424
425 return p - crs;
426}
427
428/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300429 * omap_dump_tlb_entries - dump cr arrays to given buffer
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200430 * @obj: target iommu
431 * @buf: output buffer
432 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300433size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200434{
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700435 int i, num;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200436 struct cr_regs *cr;
437 char *p = buf;
438
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700439 num = bytes / sizeof(*cr);
440 num = min(obj->nr_tlb_entries, num);
441
442 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200443 if (!cr)
444 return 0;
445
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700446 num = __dump_tlb_entries(obj, cr, num);
447 for (i = 0; i < num; i++)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200448 p += iotlb_dump_cr(obj, cr + i, p);
449 kfree(cr);
450
451 return p - buf;
452}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300453EXPORT_SYMBOL_GPL(omap_dump_tlb_entries);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200454
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300455int omap_foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200456{
457 return driver_for_each_device(&omap_iommu_driver.driver,
458 NULL, data, fn);
459}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300460EXPORT_SYMBOL_GPL(omap_foreach_iommu_device);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200461
462#endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
463
464/*
465 * H/W pagetable operations
466 */
467static void flush_iopgd_range(u32 *first, u32 *last)
468{
469 /* FIXME: L2 cache should be taken care of if it exists */
470 do {
471 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
472 : : "r" (first));
473 first += L1_CACHE_BYTES / sizeof(*first);
474 } while (first <= last);
475}
476
477static void flush_iopte_range(u32 *first, u32 *last)
478{
479 /* FIXME: L2 cache should be taken care of if it exists */
480 do {
481 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
482 : : "r" (first));
483 first += L1_CACHE_BYTES / sizeof(*first);
484 } while (first <= last);
485}
486
487static void iopte_free(u32 *iopte)
488{
489 /* Note: freed iopte's must be clean ready for re-use */
490 kmem_cache_free(iopte_cachep, iopte);
491}
492
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300493static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200494{
495 u32 *iopte;
496
497 /* a table has already existed */
498 if (*iopgd)
499 goto pte_ready;
500
501 /*
502 * do the allocation outside the page table lock
503 */
504 spin_unlock(&obj->page_table_lock);
505 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
506 spin_lock(&obj->page_table_lock);
507
508 if (!*iopgd) {
509 if (!iopte)
510 return ERR_PTR(-ENOMEM);
511
512 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
513 flush_iopgd_range(iopgd, iopgd);
514
515 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
516 } else {
517 /* We raced, free the reduniovant table */
518 iopte_free(iopte);
519 }
520
521pte_ready:
522 iopte = iopte_offset(iopgd, da);
523
524 dev_vdbg(obj->dev,
525 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
526 __func__, da, iopgd, *iopgd, iopte, *iopte);
527
528 return iopte;
529}
530
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300531static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200532{
533 u32 *iopgd = iopgd_offset(obj, da);
534
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300535 if ((da | pa) & ~IOSECTION_MASK) {
536 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
537 __func__, da, pa, IOSECTION_SIZE);
538 return -EINVAL;
539 }
540
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200541 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
542 flush_iopgd_range(iopgd, iopgd);
543 return 0;
544}
545
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300546static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200547{
548 u32 *iopgd = iopgd_offset(obj, da);
549 int i;
550
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300551 if ((da | pa) & ~IOSUPER_MASK) {
552 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
553 __func__, da, pa, IOSUPER_SIZE);
554 return -EINVAL;
555 }
556
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200557 for (i = 0; i < 16; i++)
558 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
559 flush_iopgd_range(iopgd, iopgd + 15);
560 return 0;
561}
562
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300563static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200564{
565 u32 *iopgd = iopgd_offset(obj, da);
566 u32 *iopte = iopte_alloc(obj, iopgd, da);
567
568 if (IS_ERR(iopte))
569 return PTR_ERR(iopte);
570
571 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
572 flush_iopte_range(iopte, iopte);
573
574 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
575 __func__, da, pa, iopte, *iopte);
576
577 return 0;
578}
579
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300580static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200581{
582 u32 *iopgd = iopgd_offset(obj, da);
583 u32 *iopte = iopte_alloc(obj, iopgd, da);
584 int i;
585
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300586 if ((da | pa) & ~IOLARGE_MASK) {
587 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
588 __func__, da, pa, IOLARGE_SIZE);
589 return -EINVAL;
590 }
591
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200592 if (IS_ERR(iopte))
593 return PTR_ERR(iopte);
594
595 for (i = 0; i < 16; i++)
596 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
597 flush_iopte_range(iopte, iopte + 15);
598 return 0;
599}
600
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300601static int
602iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200603{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300604 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200605 u32 prot;
606 int err;
607
608 if (!obj || !e)
609 return -EINVAL;
610
611 switch (e->pgsz) {
612 case MMU_CAM_PGSZ_16M:
613 fn = iopgd_alloc_super;
614 break;
615 case MMU_CAM_PGSZ_1M:
616 fn = iopgd_alloc_section;
617 break;
618 case MMU_CAM_PGSZ_64K:
619 fn = iopte_alloc_large;
620 break;
621 case MMU_CAM_PGSZ_4K:
622 fn = iopte_alloc_page;
623 break;
624 default:
625 fn = NULL;
626 BUG();
627 break;
628 }
629
630 prot = get_iopte_attr(e);
631
632 spin_lock(&obj->page_table_lock);
633 err = fn(obj, e->da, e->pa, prot);
634 spin_unlock(&obj->page_table_lock);
635
636 return err;
637}
638
639/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300640 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200641 * @obj: target iommu
642 * @e: an iommu tlb entry info
643 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300644int omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200645{
646 int err;
647
648 flush_iotlb_page(obj, e->da);
649 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200650 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300651 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200652 return err;
653}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300654EXPORT_SYMBOL_GPL(omap_iopgtable_store_entry);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200655
656/**
657 * iopgtable_lookup_entry - Lookup an iommu pte entry
658 * @obj: target iommu
659 * @da: iommu device virtual address
660 * @ppgd: iommu pgd entry pointer to be returned
661 * @ppte: iommu pte entry pointer to be returned
662 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300663static void
664iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200665{
666 u32 *iopgd, *iopte = NULL;
667
668 iopgd = iopgd_offset(obj, da);
669 if (!*iopgd)
670 goto out;
671
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300672 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200673 iopte = iopte_offset(iopgd, da);
674out:
675 *ppgd = iopgd;
676 *ppte = iopte;
677}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200678
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300679static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200680{
681 size_t bytes;
682 u32 *iopgd = iopgd_offset(obj, da);
683 int nent = 1;
684
685 if (!*iopgd)
686 return 0;
687
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300688 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200689 int i;
690 u32 *iopte = iopte_offset(iopgd, da);
691
692 bytes = IOPTE_SIZE;
693 if (*iopte & IOPTE_LARGE) {
694 nent *= 16;
695 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800696 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200697 }
698 bytes *= nent;
699 memset(iopte, 0, nent * sizeof(*iopte));
700 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
701
702 /*
703 * do table walk to check if this table is necessary or not
704 */
705 iopte = iopte_offset(iopgd, 0);
706 for (i = 0; i < PTRS_PER_IOPTE; i++)
707 if (iopte[i])
708 goto out;
709
710 iopte_free(iopte);
711 nent = 1; /* for the next L1 entry */
712 } else {
713 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700714 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200715 nent *= 16;
716 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800717 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200718 }
719 bytes *= nent;
720 }
721 memset(iopgd, 0, nent * sizeof(*iopgd));
722 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
723out:
724 return bytes;
725}
726
727/**
728 * iopgtable_clear_entry - Remove an iommu pte entry
729 * @obj: target iommu
730 * @da: iommu device virtual address
731 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300732static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200733{
734 size_t bytes;
735
736 spin_lock(&obj->page_table_lock);
737
738 bytes = iopgtable_clear_entry_core(obj, da);
739 flush_iotlb_page(obj, da);
740
741 spin_unlock(&obj->page_table_lock);
742
743 return bytes;
744}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200745
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300746static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200747{
748 int i;
749
750 spin_lock(&obj->page_table_lock);
751
752 for (i = 0; i < PTRS_PER_IOPGD; i++) {
753 u32 da;
754 u32 *iopgd;
755
756 da = i << IOPGD_SHIFT;
757 iopgd = iopgd_offset(obj, da);
758
759 if (!*iopgd)
760 continue;
761
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300762 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200763 iopte_free(iopte_offset(iopgd, 0));
764
765 *iopgd = 0;
766 flush_iopgd_range(iopgd, iopgd);
767 }
768
769 flush_iotlb_all(obj);
770
771 spin_unlock(&obj->page_table_lock);
772}
773
774/*
775 * Device IOMMU generic operations
776 */
777static irqreturn_t iommu_fault_handler(int irq, void *data)
778{
David Cohend594f1f2011-02-16 19:35:51 +0000779 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200780 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300781 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400782 struct iommu_domain *domain = obj->domain;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200783
784 if (!obj->refcount)
785 return IRQ_NONE;
786
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200787 clk_enable(obj->clk);
David Cohend594f1f2011-02-16 19:35:51 +0000788 errs = iommu_report_fault(obj, &da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200789 clk_disable(obj->clk);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200790 if (errs == 0)
791 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000792
793 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400794 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200795 return IRQ_HANDLED;
796
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000797 iommu_disable(obj);
798
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200799 iopgd = iopgd_offset(obj, da);
800
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300801 if (!iopgd_is_table(*iopgd)) {
David Cohend594f1f2011-02-16 19:35:51 +0000802 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p "
803 "*pgd:px%08x\n", obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200804 return IRQ_NONE;
805 }
806
807 iopte = iopte_offset(iopgd, da);
808
David Cohend594f1f2011-02-16 19:35:51 +0000809 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x "
810 "pte:0x%p *pte:0x%08x\n", obj->name, errs, da, iopgd, *iopgd,
811 iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200812
813 return IRQ_NONE;
814}
815
816static int device_match_by_alias(struct device *dev, void *data)
817{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300818 struct omap_iommu *obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200819 const char *name = data;
820
821 pr_debug("%s: %s %s\n", __func__, obj->name, name);
822
823 return strcmp(obj->name, name) == 0;
824}
825
826/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300827 * omap_iommu_attach() - attach iommu device to an iommu domain
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200828 * @name: name of target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300829 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200830 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200831static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200832{
833 int err = -ENOMEM;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200834 struct device *dev;
835 struct omap_iommu *obj;
836
837 dev = driver_find_device(&omap_iommu_driver.driver, NULL,
838 (void *)name,
839 device_match_by_alias);
840 if (!dev)
841 return NULL;
842
843 obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200844
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300845 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200846
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300847 /* an iommu device can only be attached once */
848 if (++obj->refcount > 1) {
849 dev_err(dev, "%s: already attached!\n", obj->name);
850 err = -EBUSY;
851 goto err_enable;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200852 }
853
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300854 obj->iopgd = iopgd;
855 err = iommu_enable(obj);
856 if (err)
857 goto err_enable;
858 flush_iotlb_all(obj);
859
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200860 if (!try_module_get(obj->owner))
861 goto err_module;
862
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300863 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200864
865 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
866 return obj;
867
868err_module:
869 if (obj->refcount == 1)
870 iommu_disable(obj);
871err_enable:
872 obj->refcount--;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300873 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200874 return ERR_PTR(err);
875}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200876
877/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300878 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200879 * @obj: target iommu
880 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300881static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200882{
Roel Kluinacf9d462010-01-08 10:29:05 -0800883 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200884 return;
885
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300886 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200887
888 if (--obj->refcount == 0)
889 iommu_disable(obj);
890
891 module_put(obj->owner);
892
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300893 obj->iopgd = NULL;
894
895 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200896
897 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
898}
David Cohend594f1f2011-02-16 19:35:51 +0000899
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200900/*
901 * OMAP Device MMU(IOMMU) detection
902 */
903static int __devinit omap_iommu_probe(struct platform_device *pdev)
904{
905 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200906 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300907 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200908 struct resource *res;
909 struct iommu_platform_data *pdata = pdev->dev.platform_data;
910
911 if (pdev->num_resources != 2)
912 return -EINVAL;
913
914 obj = kzalloc(sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
915 if (!obj)
916 return -ENOMEM;
917
918 obj->clk = clk_get(&pdev->dev, pdata->clk_name);
919 if (IS_ERR(obj->clk))
920 goto err_clk;
921
922 obj->nr_tlb_entries = pdata->nr_tlb_entries;
923 obj->name = pdata->name;
924 obj->dev = &pdev->dev;
925 obj->ctx = (void *)obj + sizeof(*obj);
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +0000926 obj->da_start = pdata->da_start;
927 obj->da_end = pdata->da_end;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200928
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300929 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200930 mutex_init(&obj->mmap_lock);
931 spin_lock_init(&obj->page_table_lock);
932 INIT_LIST_HEAD(&obj->mmap);
933
934 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
935 if (!res) {
936 err = -ENODEV;
937 goto err_mem;
938 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200939
940 res = request_mem_region(res->start, resource_size(res),
941 dev_name(&pdev->dev));
942 if (!res) {
943 err = -EIO;
944 goto err_mem;
945 }
946
Aaro Koskinenda4a0f72011-03-14 12:28:32 +0000947 obj->regbase = ioremap(res->start, resource_size(res));
948 if (!obj->regbase) {
949 err = -ENOMEM;
950 goto err_ioremap;
951 }
952
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200953 irq = platform_get_irq(pdev, 0);
954 if (irq < 0) {
955 err = -ENODEV;
956 goto err_irq;
957 }
958 err = request_irq(irq, iommu_fault_handler, IRQF_SHARED,
959 dev_name(&pdev->dev), obj);
960 if (err < 0)
961 goto err_irq;
962 platform_set_drvdata(pdev, obj);
963
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200964 dev_info(&pdev->dev, "%s registered\n", obj->name);
965 return 0;
966
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200967err_irq:
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200968 iounmap(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +0000969err_ioremap:
970 release_mem_region(res->start, resource_size(res));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200971err_mem:
972 clk_put(obj->clk);
973err_clk:
974 kfree(obj);
975 return err;
976}
977
978static int __devexit omap_iommu_remove(struct platform_device *pdev)
979{
980 int irq;
981 struct resource *res;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300982 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200983
984 platform_set_drvdata(pdev, NULL);
985
986 iopgtable_clear_entry_all(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200987
988 irq = platform_get_irq(pdev, 0);
989 free_irq(irq, obj);
990 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
991 release_mem_region(res->start, resource_size(res));
992 iounmap(obj->regbase);
993
994 clk_put(obj->clk);
995 dev_info(&pdev->dev, "%s removed\n", obj->name);
996 kfree(obj);
997 return 0;
998}
999
1000static struct platform_driver omap_iommu_driver = {
1001 .probe = omap_iommu_probe,
1002 .remove = __devexit_p(omap_iommu_remove),
1003 .driver = {
1004 .name = "omap-iommu",
1005 },
1006};
1007
1008static void iopte_cachep_ctor(void *iopte)
1009{
1010 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1011}
1012
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001013static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
1014 phys_addr_t pa, int order, int prot)
1015{
1016 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001017 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001018 struct device *dev = oiommu->dev;
1019 size_t bytes = PAGE_SIZE << order;
1020 struct iotlb_entry e;
1021 int omap_pgsz;
1022 u32 ret, flags;
1023
1024 /* we only support mapping a single iommu page for now */
1025 omap_pgsz = bytes_to_iopgsz(bytes);
1026 if (omap_pgsz < 0) {
1027 dev_err(dev, "invalid size to map: %d\n", bytes);
1028 return -EINVAL;
1029 }
1030
1031 dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes);
1032
1033 flags = omap_pgsz | prot;
1034
1035 iotlb_init_entry(&e, da, pa, flags);
1036
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001037 ret = omap_iopgtable_store_entry(oiommu, &e);
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001038 if (ret)
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001039 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001040
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001041 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001042}
1043
1044static int omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1045 int order)
1046{
1047 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001048 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001049 struct device *dev = oiommu->dev;
Ohad Ben-Cohen5e1b6122011-09-02 13:32:33 -04001050 size_t unmap_size;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001051
Ohad Ben-Cohen5e1b6122011-09-02 13:32:33 -04001052 dev_dbg(dev, "unmapping da 0x%lx order %d\n", da, order);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001053
Ohad Ben-Cohen5e1b6122011-09-02 13:32:33 -04001054 unmap_size = iopgtable_clear_entry(oiommu, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001055
Ohad Ben-Cohen5e1b6122011-09-02 13:32:33 -04001056 return unmap_size ? get_order(unmap_size) : -EINVAL;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001057}
1058
1059static int
1060omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1061{
1062 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001063 struct omap_iommu *oiommu;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001064 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001065 int ret = 0;
1066
1067 spin_lock(&omap_domain->lock);
1068
1069 /* only a single device is supported per domain for now */
1070 if (omap_domain->iommu_dev) {
1071 dev_err(dev, "iommu domain is already attached\n");
1072 ret = -EBUSY;
1073 goto out;
1074 }
1075
1076 /* get a handle to and enable the omap iommu */
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001077 oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001078 if (IS_ERR(oiommu)) {
1079 ret = PTR_ERR(oiommu);
1080 dev_err(dev, "can't get omap iommu: %d\n", ret);
1081 goto out;
1082 }
1083
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001084 omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -04001085 oiommu->domain = domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001086
1087out:
1088 spin_unlock(&omap_domain->lock);
1089 return ret;
1090}
1091
1092static void omap_iommu_detach_dev(struct iommu_domain *domain,
1093 struct device *dev)
1094{
1095 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001096 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1097 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001098
1099 spin_lock(&omap_domain->lock);
1100
1101 /* only a single device is supported per domain for now */
1102 if (omap_domain->iommu_dev != oiommu) {
1103 dev_err(dev, "invalid iommu device\n");
1104 goto out;
1105 }
1106
1107 iopgtable_clear_entry_all(oiommu);
1108
1109 omap_iommu_detach(oiommu);
1110
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001111 omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001112
1113out:
1114 spin_unlock(&omap_domain->lock);
1115}
1116
1117static int omap_iommu_domain_init(struct iommu_domain *domain)
1118{
1119 struct omap_iommu_domain *omap_domain;
1120
1121 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1122 if (!omap_domain) {
1123 pr_err("kzalloc failed\n");
1124 goto out;
1125 }
1126
1127 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1128 if (!omap_domain->pgtable) {
1129 pr_err("kzalloc failed\n");
1130 goto fail_nomem;
1131 }
1132
1133 /*
1134 * should never fail, but please keep this around to ensure
1135 * we keep the hardware happy
1136 */
1137 BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
1138
1139 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1140 spin_lock_init(&omap_domain->lock);
1141
1142 domain->priv = omap_domain;
1143
1144 return 0;
1145
1146fail_nomem:
1147 kfree(omap_domain);
1148out:
1149 return -ENOMEM;
1150}
1151
1152/* assume device was already detached */
1153static void omap_iommu_domain_destroy(struct iommu_domain *domain)
1154{
1155 struct omap_iommu_domain *omap_domain = domain->priv;
1156
1157 domain->priv = NULL;
1158
1159 kfree(omap_domain->pgtable);
1160 kfree(omap_domain);
1161}
1162
1163static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
1164 unsigned long da)
1165{
1166 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001167 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001168 struct device *dev = oiommu->dev;
1169 u32 *pgd, *pte;
1170 phys_addr_t ret = 0;
1171
1172 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1173
1174 if (pte) {
1175 if (iopte_is_small(*pte))
1176 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1177 else if (iopte_is_large(*pte))
1178 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1179 else
1180 dev_err(dev, "bogus pte 0x%x", *pte);
1181 } else {
1182 if (iopgd_is_section(*pgd))
1183 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1184 else if (iopgd_is_super(*pgd))
1185 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1186 else
1187 dev_err(dev, "bogus pgd 0x%x", *pgd);
1188 }
1189
1190 return ret;
1191}
1192
1193static int omap_iommu_domain_has_cap(struct iommu_domain *domain,
1194 unsigned long cap)
1195{
1196 return 0;
1197}
1198
1199static struct iommu_ops omap_iommu_ops = {
1200 .domain_init = omap_iommu_domain_init,
1201 .domain_destroy = omap_iommu_domain_destroy,
1202 .attach_dev = omap_iommu_attach_dev,
1203 .detach_dev = omap_iommu_detach_dev,
1204 .map = omap_iommu_map,
1205 .unmap = omap_iommu_unmap,
1206 .iova_to_phys = omap_iommu_iova_to_phys,
1207 .domain_has_cap = omap_iommu_domain_has_cap,
1208};
1209
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001210static int __init omap_iommu_init(void)
1211{
1212 struct kmem_cache *p;
1213 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1214 size_t align = 1 << 10; /* L2 pagetable alignement */
1215
1216 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1217 iopte_cachep_ctor);
1218 if (!p)
1219 return -ENOMEM;
1220 iopte_cachep = p;
1221
Joerg Roedela65bc642011-09-06 17:56:07 +02001222 bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001223
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001224 return platform_driver_register(&omap_iommu_driver);
1225}
1226module_init(omap_iommu_init);
1227
1228static void __exit omap_iommu_exit(void)
1229{
1230 kmem_cache_destroy(iopte_cachep);
1231
1232 platform_driver_unregister(&omap_iommu_driver);
1233}
1234module_exit(omap_iommu_exit);
1235
1236MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1237MODULE_ALIAS("platform:omap-iommu");
1238MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1239MODULE_LICENSE("GPL v2");