blob: 4db86e12c20031407955c4bd973b43ad8b9b44b1 [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>
Tony Lindgrenc8d35c82012-11-02 12:24:03 -070022#include <linux/omap-iommu.h>
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030023#include <linux/mutex.h>
24#include <linux/spinlock.h>
Tony Lindgrened1c7de2012-11-02 12:24:06 -070025#include <linux/io.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020026
27#include <asm/cacheflush.h>
28
Tony Lindgrence491cf2009-10-20 09:40:47 -070029#include <plat/iommu.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020030
Ido Yariv2f7702a2012-11-02 12:24:00 -070031#include "omap-iopgtable.h"
Tony Lindgrened1c7de2012-11-02 12:24:06 -070032#include "omap-iommu.h"
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020033
Hiroshi DOYU37c28362010-04-27 05:37:12 +000034#define for_each_iotlb_cr(obj, n, __i, cr) \
35 for (__i = 0; \
36 (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \
37 __i++)
38
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +020039/* bitmap of the page sizes currently supported */
40#define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
41
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030042/**
43 * struct omap_iommu_domain - omap iommu domain
44 * @pgtable: the page table
45 * @iommu_dev: an omap iommu device attached to this domain. only a single
46 * iommu device can be attached for now.
Omar Ramirez Luna803b5272012-04-18 13:09:41 -050047 * @dev: Device using this domain.
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030048 * @lock: domain lock, should be taken when attaching/detaching
49 */
50struct omap_iommu_domain {
51 u32 *pgtable;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030052 struct omap_iommu *iommu_dev;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -050053 struct device *dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030054 spinlock_t lock;
55};
56
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020057/* accommodate the difference between omap1 and omap2/3 */
58static const struct iommu_functions *arch_iommu;
59
60static struct platform_driver omap_iommu_driver;
61static struct kmem_cache *iopte_cachep;
62
63/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030064 * omap_install_iommu_arch - Install archtecure specific iommu functions
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020065 * @ops: a pointer to architecture specific iommu functions
66 *
67 * There are several kind of iommu algorithm(tlb, pagetable) among
68 * omap series. This interface installs such an iommu algorighm.
69 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030070int omap_install_iommu_arch(const struct iommu_functions *ops)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020071{
72 if (arch_iommu)
73 return -EBUSY;
74
75 arch_iommu = ops;
76 return 0;
77}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030078EXPORT_SYMBOL_GPL(omap_install_iommu_arch);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020079
80/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030081 * omap_uninstall_iommu_arch - Uninstall archtecure specific iommu functions
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020082 * @ops: a pointer to architecture specific iommu functions
83 *
84 * This interface uninstalls the iommu algorighm installed previously.
85 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030086void omap_uninstall_iommu_arch(const struct iommu_functions *ops)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020087{
88 if (arch_iommu != ops)
89 pr_err("%s: not your arch\n", __func__);
90
91 arch_iommu = NULL;
92}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030093EXPORT_SYMBOL_GPL(omap_uninstall_iommu_arch);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020094
95/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030096 * omap_iommu_save_ctx - Save registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020097 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020098 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020099void omap_iommu_save_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200100{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200101 struct omap_iommu *obj = dev_to_omap_iommu(dev);
102
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200103 arch_iommu->save_ctx(obj);
104}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300105EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200106
107/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300108 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200109 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200110 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200111void omap_iommu_restore_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200112{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200113 struct omap_iommu *obj = dev_to_omap_iommu(dev);
114
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200115 arch_iommu->restore_ctx(obj);
116}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300117EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200118
119/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300120 * omap_iommu_arch_version - Return running iommu arch version
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200121 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300122u32 omap_iommu_arch_version(void)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200123{
124 return arch_iommu->version;
125}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300126EXPORT_SYMBOL_GPL(omap_iommu_arch_version);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200127
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300128static int iommu_enable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200129{
130 int err;
131
132 if (!obj)
133 return -EINVAL;
134
Martin Hostettleref4815a2011-02-24 12:51:31 -0800135 if (!arch_iommu)
136 return -ENODEV;
137
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200138 clk_enable(obj->clk);
139
140 err = arch_iommu->enable(obj);
141
142 clk_disable(obj->clk);
143 return err;
144}
145
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300146static void iommu_disable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200147{
148 if (!obj)
149 return;
150
151 clk_enable(obj->clk);
152
153 arch_iommu->disable(obj);
154
155 clk_disable(obj->clk);
156}
157
158/*
159 * TLB operations
160 */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300161void omap_iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200162{
163 BUG_ON(!cr || !e);
164
165 arch_iommu->cr_to_e(cr, e);
166}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300167EXPORT_SYMBOL_GPL(omap_iotlb_cr_to_e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200168
169static inline int iotlb_cr_valid(struct cr_regs *cr)
170{
171 if (!cr)
172 return -EINVAL;
173
174 return arch_iommu->cr_valid(cr);
175}
176
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300177static inline struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200178 struct iotlb_entry *e)
179{
180 if (!e)
181 return NULL;
182
183 return arch_iommu->alloc_cr(obj, e);
184}
185
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300186static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200187{
188 return arch_iommu->cr_to_virt(cr);
189}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200190
191static u32 get_iopte_attr(struct iotlb_entry *e)
192{
193 return arch_iommu->get_pte_attr(e);
194}
195
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300196static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200197{
198 return arch_iommu->fault_isr(obj, da);
199}
200
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300201static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200202{
203 u32 val;
204
205 val = iommu_read_reg(obj, MMU_LOCK);
206
207 l->base = MMU_LOCK_BASE(val);
208 l->vict = MMU_LOCK_VICT(val);
209
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200210}
211
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300212static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200213{
214 u32 val;
215
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200216 val = (l->base << MMU_LOCK_BASE_SHIFT);
217 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
218
219 iommu_write_reg(obj, val, MMU_LOCK);
220}
221
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300222static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200223{
224 arch_iommu->tlb_read_cr(obj, cr);
225}
226
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300227static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200228{
229 arch_iommu->tlb_load_cr(obj, cr);
230
231 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
232 iommu_write_reg(obj, 1, MMU_LD_TLB);
233}
234
235/**
236 * iotlb_dump_cr - Dump an iommu tlb entry into buf
237 * @obj: target iommu
238 * @cr: contents of cam and ram register
239 * @buf: output buffer
240 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300241static inline ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200242 char *buf)
243{
244 BUG_ON(!cr || !buf);
245
246 return arch_iommu->dump_cr(obj, cr, buf);
247}
248
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000249/* only used in iotlb iteration for-loop */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300250static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000251{
252 struct cr_regs cr;
253 struct iotlb_lock l;
254
255 iotlb_lock_get(obj, &l);
256 l.vict = n;
257 iotlb_lock_set(obj, &l);
258 iotlb_read_cr(obj, &cr);
259
260 return cr;
261}
262
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200263/**
264 * load_iotlb_entry - Set an iommu tlb entry
265 * @obj: target iommu
266 * @e: an iommu tlb entry info
267 **/
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300268#ifdef PREFETCH_IOTLB
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300269static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200270{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200271 int err = 0;
272 struct iotlb_lock l;
273 struct cr_regs *cr;
274
275 if (!obj || !obj->nr_tlb_entries || !e)
276 return -EINVAL;
277
278 clk_enable(obj->clk);
279
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000280 iotlb_lock_get(obj, &l);
281 if (l.base == obj->nr_tlb_entries) {
282 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200283 err = -EBUSY;
284 goto out;
285 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000286 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000287 int i;
288 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000289
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000290 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000291 if (!iotlb_cr_valid(&tmp))
292 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000293
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000294 if (i == obj->nr_tlb_entries) {
295 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
296 err = -EBUSY;
297 goto out;
298 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000299
300 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000301 } else {
302 l.vict = l.base;
303 iotlb_lock_set(obj, &l);
304 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200305
306 cr = iotlb_alloc_cr(obj, e);
307 if (IS_ERR(cr)) {
308 clk_disable(obj->clk);
309 return PTR_ERR(cr);
310 }
311
312 iotlb_load_cr(obj, cr);
313 kfree(cr);
314
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000315 if (e->prsvd)
316 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200317 /* increment victim for next tlb load */
318 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000319 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200320 iotlb_lock_set(obj, &l);
321out:
322 clk_disable(obj->clk);
323 return err;
324}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200325
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300326#else /* !PREFETCH_IOTLB */
327
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300328static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300329{
330 return 0;
331}
332
333#endif /* !PREFETCH_IOTLB */
334
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300335static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300336{
337 return load_iotlb_entry(obj, e);
338}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200339
340/**
341 * flush_iotlb_page - Clear an iommu tlb entry
342 * @obj: target iommu
343 * @da: iommu device virtual address
344 *
345 * Clear an iommu tlb entry which includes 'da' address.
346 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300347static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200348{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200349 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000350 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200351
352 clk_enable(obj->clk);
353
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000354 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200355 u32 start;
356 size_t bytes;
357
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200358 if (!iotlb_cr_valid(&cr))
359 continue;
360
361 start = iotlb_cr_to_virt(&cr);
362 bytes = iopgsz_to_bytes(cr.cam & 3);
363
364 if ((start <= da) && (da < start + bytes)) {
365 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
366 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000367 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200368 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
369 }
370 }
371 clk_disable(obj->clk);
372
373 if (i == obj->nr_tlb_entries)
374 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
375}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200376
377/**
378 * flush_iotlb_all - Clear all iommu tlb entries
379 * @obj: target iommu
380 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300381static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200382{
383 struct iotlb_lock l;
384
385 clk_enable(obj->clk);
386
387 l.base = 0;
388 l.vict = 0;
389 iotlb_lock_set(obj, &l);
390
391 iommu_write_reg(obj, 1, MMU_GFLUSH);
392
393 clk_disable(obj->clk);
394}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200395
Arnd Bergmanne4efd942011-10-02 14:34:05 -0400396#if defined(CONFIG_OMAP_IOMMU_DEBUG) || defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
Kanigeri, Hariddfa9752010-05-24 02:01:51 +0000397
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300398ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200399{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200400 if (!obj || !buf)
401 return -EINVAL;
402
403 clk_enable(obj->clk);
404
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700405 bytes = arch_iommu->dump_ctx(obj, buf, bytes);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200406
407 clk_disable(obj->clk);
408
409 return bytes;
410}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300411EXPORT_SYMBOL_GPL(omap_iommu_dump_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200412
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300413static int
414__dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200415{
416 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000417 struct iotlb_lock saved;
418 struct cr_regs tmp;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200419 struct cr_regs *p = crs;
420
421 clk_enable(obj->clk);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200422 iotlb_lock_get(obj, &saved);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200423
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000424 for_each_iotlb_cr(obj, num, i, tmp) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200425 if (!iotlb_cr_valid(&tmp))
426 continue;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200427 *p++ = tmp;
428 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000429
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200430 iotlb_lock_set(obj, &saved);
431 clk_disable(obj->clk);
432
433 return p - crs;
434}
435
436/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300437 * omap_dump_tlb_entries - dump cr arrays to given buffer
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200438 * @obj: target iommu
439 * @buf: output buffer
440 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300441size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200442{
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700443 int i, num;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200444 struct cr_regs *cr;
445 char *p = buf;
446
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700447 num = bytes / sizeof(*cr);
448 num = min(obj->nr_tlb_entries, num);
449
450 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200451 if (!cr)
452 return 0;
453
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700454 num = __dump_tlb_entries(obj, cr, num);
455 for (i = 0; i < num; i++)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200456 p += iotlb_dump_cr(obj, cr + i, p);
457 kfree(cr);
458
459 return p - buf;
460}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300461EXPORT_SYMBOL_GPL(omap_dump_tlb_entries);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200462
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300463int omap_foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200464{
465 return driver_for_each_device(&omap_iommu_driver.driver,
466 NULL, data, fn);
467}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300468EXPORT_SYMBOL_GPL(omap_foreach_iommu_device);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200469
470#endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
471
472/*
473 * H/W pagetable operations
474 */
475static void flush_iopgd_range(u32 *first, u32 *last)
476{
477 /* FIXME: L2 cache should be taken care of if it exists */
478 do {
479 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
480 : : "r" (first));
481 first += L1_CACHE_BYTES / sizeof(*first);
482 } while (first <= last);
483}
484
485static void flush_iopte_range(u32 *first, u32 *last)
486{
487 /* FIXME: L2 cache should be taken care of if it exists */
488 do {
489 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
490 : : "r" (first));
491 first += L1_CACHE_BYTES / sizeof(*first);
492 } while (first <= last);
493}
494
495static void iopte_free(u32 *iopte)
496{
497 /* Note: freed iopte's must be clean ready for re-use */
498 kmem_cache_free(iopte_cachep, iopte);
499}
500
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300501static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200502{
503 u32 *iopte;
504
505 /* a table has already existed */
506 if (*iopgd)
507 goto pte_ready;
508
509 /*
510 * do the allocation outside the page table lock
511 */
512 spin_unlock(&obj->page_table_lock);
513 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
514 spin_lock(&obj->page_table_lock);
515
516 if (!*iopgd) {
517 if (!iopte)
518 return ERR_PTR(-ENOMEM);
519
520 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
521 flush_iopgd_range(iopgd, iopgd);
522
523 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
524 } else {
525 /* We raced, free the reduniovant table */
526 iopte_free(iopte);
527 }
528
529pte_ready:
530 iopte = iopte_offset(iopgd, da);
531
532 dev_vdbg(obj->dev,
533 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
534 __func__, da, iopgd, *iopgd, iopte, *iopte);
535
536 return iopte;
537}
538
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300539static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200540{
541 u32 *iopgd = iopgd_offset(obj, da);
542
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300543 if ((da | pa) & ~IOSECTION_MASK) {
544 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
545 __func__, da, pa, IOSECTION_SIZE);
546 return -EINVAL;
547 }
548
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200549 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
550 flush_iopgd_range(iopgd, iopgd);
551 return 0;
552}
553
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300554static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200555{
556 u32 *iopgd = iopgd_offset(obj, da);
557 int i;
558
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300559 if ((da | pa) & ~IOSUPER_MASK) {
560 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
561 __func__, da, pa, IOSUPER_SIZE);
562 return -EINVAL;
563 }
564
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200565 for (i = 0; i < 16; i++)
566 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
567 flush_iopgd_range(iopgd, iopgd + 15);
568 return 0;
569}
570
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300571static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200572{
573 u32 *iopgd = iopgd_offset(obj, da);
574 u32 *iopte = iopte_alloc(obj, iopgd, da);
575
576 if (IS_ERR(iopte))
577 return PTR_ERR(iopte);
578
579 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
580 flush_iopte_range(iopte, iopte);
581
582 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
583 __func__, da, pa, iopte, *iopte);
584
585 return 0;
586}
587
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300588static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200589{
590 u32 *iopgd = iopgd_offset(obj, da);
591 u32 *iopte = iopte_alloc(obj, iopgd, da);
592 int i;
593
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300594 if ((da | pa) & ~IOLARGE_MASK) {
595 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
596 __func__, da, pa, IOLARGE_SIZE);
597 return -EINVAL;
598 }
599
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200600 if (IS_ERR(iopte))
601 return PTR_ERR(iopte);
602
603 for (i = 0; i < 16; i++)
604 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
605 flush_iopte_range(iopte, iopte + 15);
606 return 0;
607}
608
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300609static int
610iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200611{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300612 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200613 u32 prot;
614 int err;
615
616 if (!obj || !e)
617 return -EINVAL;
618
619 switch (e->pgsz) {
620 case MMU_CAM_PGSZ_16M:
621 fn = iopgd_alloc_super;
622 break;
623 case MMU_CAM_PGSZ_1M:
624 fn = iopgd_alloc_section;
625 break;
626 case MMU_CAM_PGSZ_64K:
627 fn = iopte_alloc_large;
628 break;
629 case MMU_CAM_PGSZ_4K:
630 fn = iopte_alloc_page;
631 break;
632 default:
633 fn = NULL;
634 BUG();
635 break;
636 }
637
638 prot = get_iopte_attr(e);
639
640 spin_lock(&obj->page_table_lock);
641 err = fn(obj, e->da, e->pa, prot);
642 spin_unlock(&obj->page_table_lock);
643
644 return err;
645}
646
647/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300648 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200649 * @obj: target iommu
650 * @e: an iommu tlb entry info
651 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300652int omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200653{
654 int err;
655
656 flush_iotlb_page(obj, e->da);
657 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200658 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300659 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200660 return err;
661}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300662EXPORT_SYMBOL_GPL(omap_iopgtable_store_entry);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200663
664/**
665 * iopgtable_lookup_entry - Lookup an iommu pte entry
666 * @obj: target iommu
667 * @da: iommu device virtual address
668 * @ppgd: iommu pgd entry pointer to be returned
669 * @ppte: iommu pte entry pointer to be returned
670 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300671static void
672iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200673{
674 u32 *iopgd, *iopte = NULL;
675
676 iopgd = iopgd_offset(obj, da);
677 if (!*iopgd)
678 goto out;
679
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300680 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200681 iopte = iopte_offset(iopgd, da);
682out:
683 *ppgd = iopgd;
684 *ppte = iopte;
685}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200686
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300687static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200688{
689 size_t bytes;
690 u32 *iopgd = iopgd_offset(obj, da);
691 int nent = 1;
692
693 if (!*iopgd)
694 return 0;
695
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300696 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200697 int i;
698 u32 *iopte = iopte_offset(iopgd, da);
699
700 bytes = IOPTE_SIZE;
701 if (*iopte & IOPTE_LARGE) {
702 nent *= 16;
703 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800704 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200705 }
706 bytes *= nent;
707 memset(iopte, 0, nent * sizeof(*iopte));
708 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
709
710 /*
711 * do table walk to check if this table is necessary or not
712 */
713 iopte = iopte_offset(iopgd, 0);
714 for (i = 0; i < PTRS_PER_IOPTE; i++)
715 if (iopte[i])
716 goto out;
717
718 iopte_free(iopte);
719 nent = 1; /* for the next L1 entry */
720 } else {
721 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700722 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200723 nent *= 16;
724 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800725 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200726 }
727 bytes *= nent;
728 }
729 memset(iopgd, 0, nent * sizeof(*iopgd));
730 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
731out:
732 return bytes;
733}
734
735/**
736 * iopgtable_clear_entry - Remove an iommu pte entry
737 * @obj: target iommu
738 * @da: iommu device virtual address
739 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300740static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200741{
742 size_t bytes;
743
744 spin_lock(&obj->page_table_lock);
745
746 bytes = iopgtable_clear_entry_core(obj, da);
747 flush_iotlb_page(obj, da);
748
749 spin_unlock(&obj->page_table_lock);
750
751 return bytes;
752}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200753
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300754static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200755{
756 int i;
757
758 spin_lock(&obj->page_table_lock);
759
760 for (i = 0; i < PTRS_PER_IOPGD; i++) {
761 u32 da;
762 u32 *iopgd;
763
764 da = i << IOPGD_SHIFT;
765 iopgd = iopgd_offset(obj, da);
766
767 if (!*iopgd)
768 continue;
769
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300770 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200771 iopte_free(iopte_offset(iopgd, 0));
772
773 *iopgd = 0;
774 flush_iopgd_range(iopgd, iopgd);
775 }
776
777 flush_iotlb_all(obj);
778
779 spin_unlock(&obj->page_table_lock);
780}
781
782/*
783 * Device IOMMU generic operations
784 */
785static irqreturn_t iommu_fault_handler(int irq, void *data)
786{
David Cohend594f1f2011-02-16 19:35:51 +0000787 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200788 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300789 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400790 struct iommu_domain *domain = obj->domain;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200791
792 if (!obj->refcount)
793 return IRQ_NONE;
794
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200795 clk_enable(obj->clk);
David Cohend594f1f2011-02-16 19:35:51 +0000796 errs = iommu_report_fault(obj, &da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200797 clk_disable(obj->clk);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200798 if (errs == 0)
799 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000800
801 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400802 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200803 return IRQ_HANDLED;
804
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000805 iommu_disable(obj);
806
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200807 iopgd = iopgd_offset(obj, da);
808
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300809 if (!iopgd_is_table(*iopgd)) {
David Cohend594f1f2011-02-16 19:35:51 +0000810 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p "
811 "*pgd:px%08x\n", obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200812 return IRQ_NONE;
813 }
814
815 iopte = iopte_offset(iopgd, da);
816
David Cohend594f1f2011-02-16 19:35:51 +0000817 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x "
818 "pte:0x%p *pte:0x%08x\n", obj->name, errs, da, iopgd, *iopgd,
819 iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200820
821 return IRQ_NONE;
822}
823
824static int device_match_by_alias(struct device *dev, void *data)
825{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300826 struct omap_iommu *obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200827 const char *name = data;
828
829 pr_debug("%s: %s %s\n", __func__, obj->name, name);
830
831 return strcmp(obj->name, name) == 0;
832}
833
834/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300835 * omap_iommu_attach() - attach iommu device to an iommu domain
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200836 * @name: name of target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300837 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200838 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200839static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200840{
841 int err = -ENOMEM;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200842 struct device *dev;
843 struct omap_iommu *obj;
844
845 dev = driver_find_device(&omap_iommu_driver.driver, NULL,
846 (void *)name,
847 device_match_by_alias);
848 if (!dev)
849 return NULL;
850
851 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
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001021static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa,
1022 u32 flags)
1023{
1024 memset(e, 0, sizeof(*e));
1025
1026 e->da = da;
1027 e->pa = pa;
1028 e->valid = 1;
1029 /* FIXME: add OMAP1 support */
1030 e->pgsz = flags & MMU_CAM_PGSZ_MASK;
1031 e->endian = flags & MMU_RAM_ENDIAN_MASK;
1032 e->elsz = flags & MMU_RAM_ELSZ_MASK;
1033 e->mixed = flags & MMU_RAM_MIXED_MASK;
1034
1035 return iopgsz_to_bytes(e->pgsz);
1036}
1037
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001038static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001039 phys_addr_t pa, size_t bytes, int prot)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001040{
1041 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001042 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001043 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001044 struct iotlb_entry e;
1045 int omap_pgsz;
1046 u32 ret, flags;
1047
1048 /* we only support mapping a single iommu page for now */
1049 omap_pgsz = bytes_to_iopgsz(bytes);
1050 if (omap_pgsz < 0) {
1051 dev_err(dev, "invalid size to map: %d\n", bytes);
1052 return -EINVAL;
1053 }
1054
1055 dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes);
1056
1057 flags = omap_pgsz | prot;
1058
1059 iotlb_init_entry(&e, da, pa, flags);
1060
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001061 ret = omap_iopgtable_store_entry(oiommu, &e);
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001062 if (ret)
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001063 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001064
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001065 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001066}
1067
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001068static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1069 size_t size)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001070{
1071 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001072 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001073 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001074
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001075 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001076
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001077 return iopgtable_clear_entry(oiommu, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001078}
1079
1080static int
1081omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1082{
1083 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001084 struct omap_iommu *oiommu;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001085 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001086 int ret = 0;
1087
1088 spin_lock(&omap_domain->lock);
1089
1090 /* only a single device is supported per domain for now */
1091 if (omap_domain->iommu_dev) {
1092 dev_err(dev, "iommu domain is already attached\n");
1093 ret = -EBUSY;
1094 goto out;
1095 }
1096
1097 /* get a handle to and enable the omap iommu */
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001098 oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001099 if (IS_ERR(oiommu)) {
1100 ret = PTR_ERR(oiommu);
1101 dev_err(dev, "can't get omap iommu: %d\n", ret);
1102 goto out;
1103 }
1104
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001105 omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001106 omap_domain->dev = dev;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -04001107 oiommu->domain = domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001108
1109out:
1110 spin_unlock(&omap_domain->lock);
1111 return ret;
1112}
1113
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001114static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
1115 struct device *dev)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001116{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001117 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001118 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001119
1120 /* only a single device is supported per domain for now */
1121 if (omap_domain->iommu_dev != oiommu) {
1122 dev_err(dev, "invalid iommu device\n");
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001123 return;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001124 }
1125
1126 iopgtable_clear_entry_all(oiommu);
1127
1128 omap_iommu_detach(oiommu);
1129
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001130 omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001131 omap_domain->dev = NULL;
1132}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001133
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001134static void omap_iommu_detach_dev(struct iommu_domain *domain,
1135 struct device *dev)
1136{
1137 struct omap_iommu_domain *omap_domain = domain->priv;
1138
1139 spin_lock(&omap_domain->lock);
1140 _omap_iommu_detach_dev(omap_domain, dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001141 spin_unlock(&omap_domain->lock);
1142}
1143
1144static int omap_iommu_domain_init(struct iommu_domain *domain)
1145{
1146 struct omap_iommu_domain *omap_domain;
1147
1148 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1149 if (!omap_domain) {
1150 pr_err("kzalloc failed\n");
1151 goto out;
1152 }
1153
1154 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1155 if (!omap_domain->pgtable) {
1156 pr_err("kzalloc failed\n");
1157 goto fail_nomem;
1158 }
1159
1160 /*
1161 * should never fail, but please keep this around to ensure
1162 * we keep the hardware happy
1163 */
1164 BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
1165
1166 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1167 spin_lock_init(&omap_domain->lock);
1168
1169 domain->priv = omap_domain;
1170
Joerg Roedel2c6edb02012-01-26 19:40:55 +01001171 domain->geometry.aperture_start = 0;
1172 domain->geometry.aperture_end = (1ULL << 32) - 1;
1173 domain->geometry.force_aperture = true;
1174
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001175 return 0;
1176
1177fail_nomem:
1178 kfree(omap_domain);
1179out:
1180 return -ENOMEM;
1181}
1182
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001183static void omap_iommu_domain_destroy(struct iommu_domain *domain)
1184{
1185 struct omap_iommu_domain *omap_domain = domain->priv;
1186
1187 domain->priv = NULL;
1188
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001189 /*
1190 * An iommu device is still attached
1191 * (currently, only one device can be attached) ?
1192 */
1193 if (omap_domain->iommu_dev)
1194 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1195
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001196 kfree(omap_domain->pgtable);
1197 kfree(omap_domain);
1198}
1199
1200static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
1201 unsigned long da)
1202{
1203 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001204 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001205 struct device *dev = oiommu->dev;
1206 u32 *pgd, *pte;
1207 phys_addr_t ret = 0;
1208
1209 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1210
1211 if (pte) {
1212 if (iopte_is_small(*pte))
1213 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1214 else if (iopte_is_large(*pte))
1215 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1216 else
Ohad Ben-Cohen1a36ea82011-12-06 15:22:10 +02001217 dev_err(dev, "bogus pte 0x%x, da 0x%lx", *pte, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001218 } else {
1219 if (iopgd_is_section(*pgd))
1220 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1221 else if (iopgd_is_super(*pgd))
1222 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1223 else
Ohad Ben-Cohen1a36ea82011-12-06 15:22:10 +02001224 dev_err(dev, "bogus pgd 0x%x, da 0x%lx", *pgd, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001225 }
1226
1227 return ret;
1228}
1229
1230static int omap_iommu_domain_has_cap(struct iommu_domain *domain,
1231 unsigned long cap)
1232{
1233 return 0;
1234}
1235
1236static struct iommu_ops omap_iommu_ops = {
1237 .domain_init = omap_iommu_domain_init,
1238 .domain_destroy = omap_iommu_domain_destroy,
1239 .attach_dev = omap_iommu_attach_dev,
1240 .detach_dev = omap_iommu_detach_dev,
1241 .map = omap_iommu_map,
1242 .unmap = omap_iommu_unmap,
1243 .iova_to_phys = omap_iommu_iova_to_phys,
1244 .domain_has_cap = omap_iommu_domain_has_cap,
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +02001245 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001246};
1247
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001248static int __init omap_iommu_init(void)
1249{
1250 struct kmem_cache *p;
1251 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1252 size_t align = 1 << 10; /* L2 pagetable alignement */
1253
1254 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1255 iopte_cachep_ctor);
1256 if (!p)
1257 return -ENOMEM;
1258 iopte_cachep = p;
1259
Joerg Roedela65bc642011-09-06 17:56:07 +02001260 bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001261
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001262 return platform_driver_register(&omap_iommu_driver);
1263}
Ohad Ben-Cohen435792d2012-02-26 12:14:14 +02001264/* must be ready before omap3isp is probed */
1265subsys_initcall(omap_iommu_init);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001266
1267static void __exit omap_iommu_exit(void)
1268{
1269 kmem_cache_destroy(iopte_cachep);
1270
1271 platform_driver_unregister(&omap_iommu_driver);
1272}
1273module_exit(omap_iommu_exit);
1274
1275MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1276MODULE_ALIAS("platform:omap-iommu");
1277MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1278MODULE_LICENSE("GPL v2");