blob: 217952b993e1e7df8d382655dbe4b84059501709 [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>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020019#include <linux/platform_device.h>
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030020#include <linux/iommu.h>
Tony Lindgrenc8d35c82012-11-02 12:24:03 -070021#include <linux/omap-iommu.h>
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030022#include <linux/mutex.h>
23#include <linux/spinlock.h>
Tony Lindgrened1c7de2012-11-02 12:24:06 -070024#include <linux/io.h>
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -060025#include <linux/pm_runtime.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020026
27#include <asm/cacheflush.h>
28
Tony Lindgren2ab7c842012-11-02 12:24:14 -070029#include <linux/platform_data/iommu-omap.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
Ido Yariv7bd9e252012-11-02 12:24:09 -070057#define MMU_LOCK_BASE_SHIFT 10
58#define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
59#define MMU_LOCK_BASE(x) \
60 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
61
62#define MMU_LOCK_VICT_SHIFT 4
63#define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
64#define MMU_LOCK_VICT(x) \
65 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
66
67struct iotlb_lock {
68 short base;
69 short vict;
70};
71
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020072/* accommodate the difference between omap1 and omap2/3 */
73static const struct iommu_functions *arch_iommu;
74
75static struct platform_driver omap_iommu_driver;
76static struct kmem_cache *iopte_cachep;
77
78/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030079 * omap_install_iommu_arch - Install archtecure specific iommu functions
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020080 * @ops: a pointer to architecture specific iommu functions
81 *
82 * There are several kind of iommu algorithm(tlb, pagetable) among
83 * omap series. This interface installs such an iommu algorighm.
84 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030085int omap_install_iommu_arch(const struct iommu_functions *ops)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020086{
87 if (arch_iommu)
88 return -EBUSY;
89
90 arch_iommu = ops;
91 return 0;
92}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030093EXPORT_SYMBOL_GPL(omap_install_iommu_arch);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020094
95/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030096 * omap_uninstall_iommu_arch - Uninstall archtecure specific iommu functions
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020097 * @ops: a pointer to architecture specific iommu functions
98 *
99 * This interface uninstalls the iommu algorighm installed previously.
100 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300101void omap_uninstall_iommu_arch(const struct iommu_functions *ops)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200102{
103 if (arch_iommu != ops)
104 pr_err("%s: not your arch\n", __func__);
105
106 arch_iommu = NULL;
107}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300108EXPORT_SYMBOL_GPL(omap_uninstall_iommu_arch);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200109
110/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300111 * omap_iommu_save_ctx - Save registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200112 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200113 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200114void omap_iommu_save_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200115{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200116 struct omap_iommu *obj = dev_to_omap_iommu(dev);
117
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200118 arch_iommu->save_ctx(obj);
119}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300120EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200121
122/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300123 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200124 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200125 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200126void omap_iommu_restore_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200127{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200128 struct omap_iommu *obj = dev_to_omap_iommu(dev);
129
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200130 arch_iommu->restore_ctx(obj);
131}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300132EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200133
134/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300135 * omap_iommu_arch_version - Return running iommu arch version
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200136 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300137u32 omap_iommu_arch_version(void)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200138{
139 return arch_iommu->version;
140}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300141EXPORT_SYMBOL_GPL(omap_iommu_arch_version);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200142
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300143static int iommu_enable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200144{
145 int err;
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600146 struct platform_device *pdev = to_platform_device(obj->dev);
147 struct iommu_platform_data *pdata = pdev->dev.platform_data;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200148
Martin Hostettleref4815a2011-02-24 12:51:31 -0800149 if (!arch_iommu)
150 return -ENODEV;
151
Florian Vaussard90e569c2014-02-28 14:42:34 -0600152 if (pdata && pdata->deassert_reset) {
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600153 err = pdata->deassert_reset(pdev, pdata->reset_name);
154 if (err) {
155 dev_err(obj->dev, "deassert_reset failed: %d\n", err);
156 return err;
157 }
158 }
159
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600160 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200161
162 err = arch_iommu->enable(obj);
163
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200164 return err;
165}
166
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300167static void iommu_disable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200168{
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600169 struct platform_device *pdev = to_platform_device(obj->dev);
170 struct iommu_platform_data *pdata = pdev->dev.platform_data;
171
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200172 arch_iommu->disable(obj);
173
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600174 pm_runtime_put_sync(obj->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600175
Florian Vaussard90e569c2014-02-28 14:42:34 -0600176 if (pdata && pdata->assert_reset)
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600177 pdata->assert_reset(pdev, pdata->reset_name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200178}
179
180/*
181 * TLB operations
182 */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300183void omap_iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200184{
185 BUG_ON(!cr || !e);
186
187 arch_iommu->cr_to_e(cr, e);
188}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300189EXPORT_SYMBOL_GPL(omap_iotlb_cr_to_e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200190
191static inline int iotlb_cr_valid(struct cr_regs *cr)
192{
193 if (!cr)
194 return -EINVAL;
195
196 return arch_iommu->cr_valid(cr);
197}
198
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300199static inline struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200200 struct iotlb_entry *e)
201{
202 if (!e)
203 return NULL;
204
205 return arch_iommu->alloc_cr(obj, e);
206}
207
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300208static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200209{
210 return arch_iommu->cr_to_virt(cr);
211}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200212
213static u32 get_iopte_attr(struct iotlb_entry *e)
214{
215 return arch_iommu->get_pte_attr(e);
216}
217
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300218static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200219{
220 return arch_iommu->fault_isr(obj, da);
221}
222
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300223static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200224{
225 u32 val;
226
227 val = iommu_read_reg(obj, MMU_LOCK);
228
229 l->base = MMU_LOCK_BASE(val);
230 l->vict = MMU_LOCK_VICT(val);
231
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200232}
233
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300234static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200235{
236 u32 val;
237
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200238 val = (l->base << MMU_LOCK_BASE_SHIFT);
239 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
240
241 iommu_write_reg(obj, val, MMU_LOCK);
242}
243
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300244static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200245{
246 arch_iommu->tlb_read_cr(obj, cr);
247}
248
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300249static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200250{
251 arch_iommu->tlb_load_cr(obj, cr);
252
253 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
254 iommu_write_reg(obj, 1, MMU_LD_TLB);
255}
256
257/**
258 * iotlb_dump_cr - Dump an iommu tlb entry into buf
259 * @obj: target iommu
260 * @cr: contents of cam and ram register
261 * @buf: output buffer
262 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300263static inline ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200264 char *buf)
265{
266 BUG_ON(!cr || !buf);
267
268 return arch_iommu->dump_cr(obj, cr, buf);
269}
270
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000271/* only used in iotlb iteration for-loop */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300272static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000273{
274 struct cr_regs cr;
275 struct iotlb_lock l;
276
277 iotlb_lock_get(obj, &l);
278 l.vict = n;
279 iotlb_lock_set(obj, &l);
280 iotlb_read_cr(obj, &cr);
281
282 return cr;
283}
284
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200285/**
286 * load_iotlb_entry - Set an iommu tlb entry
287 * @obj: target iommu
288 * @e: an iommu tlb entry info
289 **/
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300290#ifdef PREFETCH_IOTLB
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300291static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200292{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200293 int err = 0;
294 struct iotlb_lock l;
295 struct cr_regs *cr;
296
297 if (!obj || !obj->nr_tlb_entries || !e)
298 return -EINVAL;
299
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600300 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200301
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000302 iotlb_lock_get(obj, &l);
303 if (l.base == obj->nr_tlb_entries) {
304 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200305 err = -EBUSY;
306 goto out;
307 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000308 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000309 int i;
310 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000311
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000312 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000313 if (!iotlb_cr_valid(&tmp))
314 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000315
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000316 if (i == obj->nr_tlb_entries) {
317 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
318 err = -EBUSY;
319 goto out;
320 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000321
322 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000323 } else {
324 l.vict = l.base;
325 iotlb_lock_set(obj, &l);
326 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200327
328 cr = iotlb_alloc_cr(obj, e);
329 if (IS_ERR(cr)) {
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600330 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200331 return PTR_ERR(cr);
332 }
333
334 iotlb_load_cr(obj, cr);
335 kfree(cr);
336
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000337 if (e->prsvd)
338 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200339 /* increment victim for next tlb load */
340 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000341 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200342 iotlb_lock_set(obj, &l);
343out:
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600344 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200345 return err;
346}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200347
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300348#else /* !PREFETCH_IOTLB */
349
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300350static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300351{
352 return 0;
353}
354
355#endif /* !PREFETCH_IOTLB */
356
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300357static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300358{
359 return load_iotlb_entry(obj, e);
360}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200361
362/**
363 * flush_iotlb_page - Clear an iommu tlb entry
364 * @obj: target iommu
365 * @da: iommu device virtual address
366 *
367 * Clear an iommu tlb entry which includes 'da' address.
368 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300369static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200370{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200371 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000372 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200373
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600374 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200375
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000376 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200377 u32 start;
378 size_t bytes;
379
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200380 if (!iotlb_cr_valid(&cr))
381 continue;
382
383 start = iotlb_cr_to_virt(&cr);
384 bytes = iopgsz_to_bytes(cr.cam & 3);
385
386 if ((start <= da) && (da < start + bytes)) {
387 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
388 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000389 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200390 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
391 }
392 }
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600393 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200394
395 if (i == obj->nr_tlb_entries)
396 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
397}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200398
399/**
400 * flush_iotlb_all - Clear all iommu tlb entries
401 * @obj: target iommu
402 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300403static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200404{
405 struct iotlb_lock l;
406
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600407 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200408
409 l.base = 0;
410 l.vict = 0;
411 iotlb_lock_set(obj, &l);
412
413 iommu_write_reg(obj, 1, MMU_GFLUSH);
414
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600415 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200416}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200417
Arnd Bergmanne4efd942011-10-02 14:34:05 -0400418#if defined(CONFIG_OMAP_IOMMU_DEBUG) || defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
Kanigeri, Hariddfa9752010-05-24 02:01:51 +0000419
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300420ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200421{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200422 if (!obj || !buf)
423 return -EINVAL;
424
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600425 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200426
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700427 bytes = arch_iommu->dump_ctx(obj, buf, bytes);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200428
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600429 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200430
431 return bytes;
432}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300433EXPORT_SYMBOL_GPL(omap_iommu_dump_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200434
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300435static int
436__dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200437{
438 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000439 struct iotlb_lock saved;
440 struct cr_regs tmp;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200441 struct cr_regs *p = crs;
442
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600443 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200444 iotlb_lock_get(obj, &saved);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200445
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000446 for_each_iotlb_cr(obj, num, i, tmp) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200447 if (!iotlb_cr_valid(&tmp))
448 continue;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200449 *p++ = tmp;
450 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000451
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200452 iotlb_lock_set(obj, &saved);
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600453 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200454
455 return p - crs;
456}
457
458/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300459 * omap_dump_tlb_entries - dump cr arrays to given buffer
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200460 * @obj: target iommu
461 * @buf: output buffer
462 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300463size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200464{
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700465 int i, num;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200466 struct cr_regs *cr;
467 char *p = buf;
468
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700469 num = bytes / sizeof(*cr);
470 num = min(obj->nr_tlb_entries, num);
471
472 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200473 if (!cr)
474 return 0;
475
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700476 num = __dump_tlb_entries(obj, cr, num);
477 for (i = 0; i < num; i++)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200478 p += iotlb_dump_cr(obj, cr + i, p);
479 kfree(cr);
480
481 return p - buf;
482}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300483EXPORT_SYMBOL_GPL(omap_dump_tlb_entries);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200484
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300485int omap_foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200486{
487 return driver_for_each_device(&omap_iommu_driver.driver,
488 NULL, data, fn);
489}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300490EXPORT_SYMBOL_GPL(omap_foreach_iommu_device);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200491
492#endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
493
494/*
495 * H/W pagetable operations
496 */
497static void flush_iopgd_range(u32 *first, u32 *last)
498{
499 /* FIXME: L2 cache should be taken care of if it exists */
500 do {
501 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
502 : : "r" (first));
503 first += L1_CACHE_BYTES / sizeof(*first);
504 } while (first <= last);
505}
506
507static void flush_iopte_range(u32 *first, u32 *last)
508{
509 /* FIXME: L2 cache should be taken care of if it exists */
510 do {
511 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
512 : : "r" (first));
513 first += L1_CACHE_BYTES / sizeof(*first);
514 } while (first <= last);
515}
516
517static void iopte_free(u32 *iopte)
518{
519 /* Note: freed iopte's must be clean ready for re-use */
520 kmem_cache_free(iopte_cachep, iopte);
521}
522
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300523static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200524{
525 u32 *iopte;
526
527 /* a table has already existed */
528 if (*iopgd)
529 goto pte_ready;
530
531 /*
532 * do the allocation outside the page table lock
533 */
534 spin_unlock(&obj->page_table_lock);
535 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
536 spin_lock(&obj->page_table_lock);
537
538 if (!*iopgd) {
539 if (!iopte)
540 return ERR_PTR(-ENOMEM);
541
542 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
543 flush_iopgd_range(iopgd, iopgd);
544
545 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
546 } else {
547 /* We raced, free the reduniovant table */
548 iopte_free(iopte);
549 }
550
551pte_ready:
552 iopte = iopte_offset(iopgd, da);
553
554 dev_vdbg(obj->dev,
555 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
556 __func__, da, iopgd, *iopgd, iopte, *iopte);
557
558 return iopte;
559}
560
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300561static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200562{
563 u32 *iopgd = iopgd_offset(obj, da);
564
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300565 if ((da | pa) & ~IOSECTION_MASK) {
566 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
567 __func__, da, pa, IOSECTION_SIZE);
568 return -EINVAL;
569 }
570
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200571 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
572 flush_iopgd_range(iopgd, iopgd);
573 return 0;
574}
575
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300576static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200577{
578 u32 *iopgd = iopgd_offset(obj, da);
579 int i;
580
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300581 if ((da | pa) & ~IOSUPER_MASK) {
582 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
583 __func__, da, pa, IOSUPER_SIZE);
584 return -EINVAL;
585 }
586
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200587 for (i = 0; i < 16; i++)
588 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
589 flush_iopgd_range(iopgd, iopgd + 15);
590 return 0;
591}
592
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300593static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200594{
595 u32 *iopgd = iopgd_offset(obj, da);
596 u32 *iopte = iopte_alloc(obj, iopgd, da);
597
598 if (IS_ERR(iopte))
599 return PTR_ERR(iopte);
600
601 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
602 flush_iopte_range(iopte, iopte);
603
604 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
605 __func__, da, pa, iopte, *iopte);
606
607 return 0;
608}
609
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300610static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200611{
612 u32 *iopgd = iopgd_offset(obj, da);
613 u32 *iopte = iopte_alloc(obj, iopgd, da);
614 int i;
615
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300616 if ((da | pa) & ~IOLARGE_MASK) {
617 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
618 __func__, da, pa, IOLARGE_SIZE);
619 return -EINVAL;
620 }
621
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200622 if (IS_ERR(iopte))
623 return PTR_ERR(iopte);
624
625 for (i = 0; i < 16; i++)
626 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
627 flush_iopte_range(iopte, iopte + 15);
628 return 0;
629}
630
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300631static int
632iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200633{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300634 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200635 u32 prot;
636 int err;
637
638 if (!obj || !e)
639 return -EINVAL;
640
641 switch (e->pgsz) {
642 case MMU_CAM_PGSZ_16M:
643 fn = iopgd_alloc_super;
644 break;
645 case MMU_CAM_PGSZ_1M:
646 fn = iopgd_alloc_section;
647 break;
648 case MMU_CAM_PGSZ_64K:
649 fn = iopte_alloc_large;
650 break;
651 case MMU_CAM_PGSZ_4K:
652 fn = iopte_alloc_page;
653 break;
654 default:
655 fn = NULL;
656 BUG();
657 break;
658 }
659
660 prot = get_iopte_attr(e);
661
662 spin_lock(&obj->page_table_lock);
663 err = fn(obj, e->da, e->pa, prot);
664 spin_unlock(&obj->page_table_lock);
665
666 return err;
667}
668
669/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300670 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200671 * @obj: target iommu
672 * @e: an iommu tlb entry info
673 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300674int omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200675{
676 int err;
677
678 flush_iotlb_page(obj, e->da);
679 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200680 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300681 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200682 return err;
683}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300684EXPORT_SYMBOL_GPL(omap_iopgtable_store_entry);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200685
686/**
687 * iopgtable_lookup_entry - Lookup an iommu pte entry
688 * @obj: target iommu
689 * @da: iommu device virtual address
690 * @ppgd: iommu pgd entry pointer to be returned
691 * @ppte: iommu pte entry pointer to be returned
692 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300693static void
694iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200695{
696 u32 *iopgd, *iopte = NULL;
697
698 iopgd = iopgd_offset(obj, da);
699 if (!*iopgd)
700 goto out;
701
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300702 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200703 iopte = iopte_offset(iopgd, da);
704out:
705 *ppgd = iopgd;
706 *ppte = iopte;
707}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200708
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300709static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200710{
711 size_t bytes;
712 u32 *iopgd = iopgd_offset(obj, da);
713 int nent = 1;
714
715 if (!*iopgd)
716 return 0;
717
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300718 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200719 int i;
720 u32 *iopte = iopte_offset(iopgd, da);
721
722 bytes = IOPTE_SIZE;
723 if (*iopte & IOPTE_LARGE) {
724 nent *= 16;
725 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800726 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200727 }
728 bytes *= nent;
729 memset(iopte, 0, nent * sizeof(*iopte));
730 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
731
732 /*
733 * do table walk to check if this table is necessary or not
734 */
735 iopte = iopte_offset(iopgd, 0);
736 for (i = 0; i < PTRS_PER_IOPTE; i++)
737 if (iopte[i])
738 goto out;
739
740 iopte_free(iopte);
741 nent = 1; /* for the next L1 entry */
742 } else {
743 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700744 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200745 nent *= 16;
746 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800747 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200748 }
749 bytes *= nent;
750 }
751 memset(iopgd, 0, nent * sizeof(*iopgd));
752 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
753out:
754 return bytes;
755}
756
757/**
758 * iopgtable_clear_entry - Remove an iommu pte entry
759 * @obj: target iommu
760 * @da: iommu device virtual address
761 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300762static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200763{
764 size_t bytes;
765
766 spin_lock(&obj->page_table_lock);
767
768 bytes = iopgtable_clear_entry_core(obj, da);
769 flush_iotlb_page(obj, da);
770
771 spin_unlock(&obj->page_table_lock);
772
773 return bytes;
774}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200775
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300776static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200777{
778 int i;
779
780 spin_lock(&obj->page_table_lock);
781
782 for (i = 0; i < PTRS_PER_IOPGD; i++) {
783 u32 da;
784 u32 *iopgd;
785
786 da = i << IOPGD_SHIFT;
787 iopgd = iopgd_offset(obj, da);
788
789 if (!*iopgd)
790 continue;
791
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300792 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200793 iopte_free(iopte_offset(iopgd, 0));
794
795 *iopgd = 0;
796 flush_iopgd_range(iopgd, iopgd);
797 }
798
799 flush_iotlb_all(obj);
800
801 spin_unlock(&obj->page_table_lock);
802}
803
804/*
805 * Device IOMMU generic operations
806 */
807static irqreturn_t iommu_fault_handler(int irq, void *data)
808{
David Cohend594f1f2011-02-16 19:35:51 +0000809 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200810 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300811 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400812 struct iommu_domain *domain = obj->domain;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200813
814 if (!obj->refcount)
815 return IRQ_NONE;
816
David Cohend594f1f2011-02-16 19:35:51 +0000817 errs = iommu_report_fault(obj, &da);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200818 if (errs == 0)
819 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000820
821 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400822 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200823 return IRQ_HANDLED;
824
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000825 iommu_disable(obj);
826
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200827 iopgd = iopgd_offset(obj, da);
828
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300829 if (!iopgd_is_table(*iopgd)) {
Suman Annab6c2e092013-05-30 18:10:59 -0500830 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
831 obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200832 return IRQ_NONE;
833 }
834
835 iopte = iopte_offset(iopgd, da);
836
Suman Annab6c2e092013-05-30 18:10:59 -0500837 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
838 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200839
840 return IRQ_NONE;
841}
842
843static int device_match_by_alias(struct device *dev, void *data)
844{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300845 struct omap_iommu *obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200846 const char *name = data;
847
848 pr_debug("%s: %s %s\n", __func__, obj->name, name);
849
850 return strcmp(obj->name, name) == 0;
851}
852
853/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300854 * omap_iommu_attach() - attach iommu device to an iommu domain
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200855 * @name: name of target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300856 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200857 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200858static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200859{
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600860 int err;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200861 struct device *dev;
862 struct omap_iommu *obj;
863
864 dev = driver_find_device(&omap_iommu_driver.driver, NULL,
865 (void *)name,
866 device_match_by_alias);
867 if (!dev)
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600868 return ERR_PTR(-ENODEV);
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200869
870 obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200871
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300872 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200873
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300874 /* an iommu device can only be attached once */
875 if (++obj->refcount > 1) {
876 dev_err(dev, "%s: already attached!\n", obj->name);
877 err = -EBUSY;
878 goto err_enable;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200879 }
880
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300881 obj->iopgd = iopgd;
882 err = iommu_enable(obj);
883 if (err)
884 goto err_enable;
885 flush_iotlb_all(obj);
886
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600887 if (!try_module_get(obj->owner)) {
888 err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200889 goto err_module;
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600890 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200891
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300892 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200893
894 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
895 return obj;
896
897err_module:
898 if (obj->refcount == 1)
899 iommu_disable(obj);
900err_enable:
901 obj->refcount--;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300902 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200903 return ERR_PTR(err);
904}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200905
906/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300907 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200908 * @obj: target iommu
909 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300910static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200911{
Roel Kluinacf9d462010-01-08 10:29:05 -0800912 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200913 return;
914
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300915 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200916
917 if (--obj->refcount == 0)
918 iommu_disable(obj);
919
920 module_put(obj->owner);
921
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300922 obj->iopgd = NULL;
923
924 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200925
926 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
927}
David Cohend594f1f2011-02-16 19:35:51 +0000928
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200929/*
930 * OMAP Device MMU(IOMMU) detection
931 */
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -0800932static int omap_iommu_probe(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200933{
934 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200935 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300936 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200937 struct resource *res;
938 struct iommu_platform_data *pdata = pdev->dev.platform_data;
939
Suman Annaf129b3d2014-02-28 14:42:32 -0600940 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200941 if (!obj)
942 return -ENOMEM;
943
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200944 obj->nr_tlb_entries = pdata->nr_tlb_entries;
945 obj->name = pdata->name;
946 obj->dev = &pdev->dev;
947 obj->ctx = (void *)obj + sizeof(*obj);
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +0000948 obj->da_start = pdata->da_start;
949 obj->da_end = pdata->da_end;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200950
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300951 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200952 mutex_init(&obj->mmap_lock);
953 spin_lock_init(&obj->page_table_lock);
954 INIT_LIST_HEAD(&obj->mmap);
955
956 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -0600957 obj->regbase = devm_ioremap_resource(obj->dev, res);
958 if (IS_ERR(obj->regbase))
959 return PTR_ERR(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +0000960
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200961 irq = platform_get_irq(pdev, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -0600962 if (irq < 0)
963 return -ENODEV;
964
965 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
966 dev_name(obj->dev), obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200967 if (err < 0)
Suman Annaf129b3d2014-02-28 14:42:32 -0600968 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200969 platform_set_drvdata(pdev, obj);
970
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600971 pm_runtime_irq_safe(obj->dev);
972 pm_runtime_enable(obj->dev);
973
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200974 dev_info(&pdev->dev, "%s registered\n", obj->name);
975 return 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200976}
977
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -0800978static int omap_iommu_remove(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200979{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300980 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200981
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200982 iopgtable_clear_entry_all(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200983
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600984 pm_runtime_disable(obj->dev);
985
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200986 dev_info(&pdev->dev, "%s removed\n", obj->name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200987 return 0;
988}
989
990static struct platform_driver omap_iommu_driver = {
991 .probe = omap_iommu_probe,
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -0800992 .remove = omap_iommu_remove,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200993 .driver = {
994 .name = "omap-iommu",
995 },
996};
997
998static void iopte_cachep_ctor(void *iopte)
999{
1000 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1001}
1002
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001003static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa,
1004 u32 flags)
1005{
1006 memset(e, 0, sizeof(*e));
1007
1008 e->da = da;
1009 e->pa = pa;
1010 e->valid = 1;
1011 /* FIXME: add OMAP1 support */
1012 e->pgsz = flags & MMU_CAM_PGSZ_MASK;
1013 e->endian = flags & MMU_RAM_ENDIAN_MASK;
1014 e->elsz = flags & MMU_RAM_ELSZ_MASK;
1015 e->mixed = flags & MMU_RAM_MIXED_MASK;
1016
1017 return iopgsz_to_bytes(e->pgsz);
1018}
1019
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001020static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001021 phys_addr_t pa, size_t bytes, int prot)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001022{
1023 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001024 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001025 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001026 struct iotlb_entry e;
1027 int omap_pgsz;
1028 u32 ret, flags;
1029
1030 /* we only support mapping a single iommu page for now */
1031 omap_pgsz = bytes_to_iopgsz(bytes);
1032 if (omap_pgsz < 0) {
1033 dev_err(dev, "invalid size to map: %d\n", bytes);
1034 return -EINVAL;
1035 }
1036
1037 dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes);
1038
1039 flags = omap_pgsz | prot;
1040
1041 iotlb_init_entry(&e, da, pa, flags);
1042
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001043 ret = omap_iopgtable_store_entry(oiommu, &e);
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001044 if (ret)
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001045 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001046
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001047 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001048}
1049
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001050static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1051 size_t size)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001052{
1053 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001054 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001055 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001056
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001057 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001058
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001059 return iopgtable_clear_entry(oiommu, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001060}
1061
1062static int
1063omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1064{
1065 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001066 struct omap_iommu *oiommu;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001067 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001068 int ret = 0;
1069
1070 spin_lock(&omap_domain->lock);
1071
1072 /* only a single device is supported per domain for now */
1073 if (omap_domain->iommu_dev) {
1074 dev_err(dev, "iommu domain is already attached\n");
1075 ret = -EBUSY;
1076 goto out;
1077 }
1078
1079 /* get a handle to and enable the omap iommu */
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001080 oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001081 if (IS_ERR(oiommu)) {
1082 ret = PTR_ERR(oiommu);
1083 dev_err(dev, "can't get omap iommu: %d\n", ret);
1084 goto out;
1085 }
1086
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001087 omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001088 omap_domain->dev = dev;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -04001089 oiommu->domain = domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001090
1091out:
1092 spin_unlock(&omap_domain->lock);
1093 return ret;
1094}
1095
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001096static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
1097 struct device *dev)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001098{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001099 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001100 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001101
1102 /* only a single device is supported per domain for now */
1103 if (omap_domain->iommu_dev != oiommu) {
1104 dev_err(dev, "invalid iommu device\n");
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001105 return;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001106 }
1107
1108 iopgtable_clear_entry_all(oiommu);
1109
1110 omap_iommu_detach(oiommu);
1111
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001112 omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001113 omap_domain->dev = NULL;
1114}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001115
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001116static void omap_iommu_detach_dev(struct iommu_domain *domain,
1117 struct device *dev)
1118{
1119 struct omap_iommu_domain *omap_domain = domain->priv;
1120
1121 spin_lock(&omap_domain->lock);
1122 _omap_iommu_detach_dev(omap_domain, dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001123 spin_unlock(&omap_domain->lock);
1124}
1125
1126static int omap_iommu_domain_init(struct iommu_domain *domain)
1127{
1128 struct omap_iommu_domain *omap_domain;
1129
1130 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1131 if (!omap_domain) {
1132 pr_err("kzalloc failed\n");
1133 goto out;
1134 }
1135
1136 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1137 if (!omap_domain->pgtable) {
1138 pr_err("kzalloc failed\n");
1139 goto fail_nomem;
1140 }
1141
1142 /*
1143 * should never fail, but please keep this around to ensure
1144 * we keep the hardware happy
1145 */
1146 BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
1147
1148 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1149 spin_lock_init(&omap_domain->lock);
1150
1151 domain->priv = omap_domain;
1152
Joerg Roedel2c6edb02012-01-26 19:40:55 +01001153 domain->geometry.aperture_start = 0;
1154 domain->geometry.aperture_end = (1ULL << 32) - 1;
1155 domain->geometry.force_aperture = true;
1156
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001157 return 0;
1158
1159fail_nomem:
1160 kfree(omap_domain);
1161out:
1162 return -ENOMEM;
1163}
1164
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001165static void omap_iommu_domain_destroy(struct iommu_domain *domain)
1166{
1167 struct omap_iommu_domain *omap_domain = domain->priv;
1168
1169 domain->priv = NULL;
1170
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001171 /*
1172 * An iommu device is still attached
1173 * (currently, only one device can be attached) ?
1174 */
1175 if (omap_domain->iommu_dev)
1176 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1177
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001178 kfree(omap_domain->pgtable);
1179 kfree(omap_domain);
1180}
1181
1182static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547ac2013-03-29 01:23:58 +05301183 dma_addr_t da)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001184{
1185 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001186 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001187 struct device *dev = oiommu->dev;
1188 u32 *pgd, *pte;
1189 phys_addr_t ret = 0;
1190
1191 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1192
1193 if (pte) {
1194 if (iopte_is_small(*pte))
1195 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1196 else if (iopte_is_large(*pte))
1197 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1198 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001199 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
1200 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001201 } else {
1202 if (iopgd_is_section(*pgd))
1203 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1204 else if (iopgd_is_super(*pgd))
1205 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1206 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001207 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
1208 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001209 }
1210
1211 return ret;
1212}
1213
1214static int omap_iommu_domain_has_cap(struct iommu_domain *domain,
1215 unsigned long cap)
1216{
1217 return 0;
1218}
1219
1220static struct iommu_ops omap_iommu_ops = {
1221 .domain_init = omap_iommu_domain_init,
1222 .domain_destroy = omap_iommu_domain_destroy,
1223 .attach_dev = omap_iommu_attach_dev,
1224 .detach_dev = omap_iommu_detach_dev,
1225 .map = omap_iommu_map,
1226 .unmap = omap_iommu_unmap,
1227 .iova_to_phys = omap_iommu_iova_to_phys,
1228 .domain_has_cap = omap_iommu_domain_has_cap,
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +02001229 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001230};
1231
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001232static int __init omap_iommu_init(void)
1233{
1234 struct kmem_cache *p;
1235 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1236 size_t align = 1 << 10; /* L2 pagetable alignement */
1237
1238 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1239 iopte_cachep_ctor);
1240 if (!p)
1241 return -ENOMEM;
1242 iopte_cachep = p;
1243
Joerg Roedela65bc642011-09-06 17:56:07 +02001244 bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001245
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001246 return platform_driver_register(&omap_iommu_driver);
1247}
Ohad Ben-Cohen435792d2012-02-26 12:14:14 +02001248/* must be ready before omap3isp is probed */
1249subsys_initcall(omap_iommu_init);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001250
1251static void __exit omap_iommu_exit(void)
1252{
1253 kmem_cache_destroy(iopte_cachep);
1254
1255 platform_driver_unregister(&omap_iommu_driver);
1256}
1257module_exit(omap_iommu_exit);
1258
1259MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1260MODULE_ALIAS("platform:omap-iommu");
1261MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1262MODULE_LICENSE("GPL v2");