blob: 78f32d13dd66361b24d3d6e2cafa96b6cd63dc6c [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>
Florian Vaussard3c927482014-02-28 14:42:36 -060026#include <linux/of.h>
27#include <linux/of_iommu.h>
28#include <linux/of_irq.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020029
30#include <asm/cacheflush.h>
31
Tony Lindgren2ab7c842012-11-02 12:24:14 -070032#include <linux/platform_data/iommu-omap.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020033
Ido Yariv2f7702a2012-11-02 12:24:00 -070034#include "omap-iopgtable.h"
Tony Lindgrened1c7de2012-11-02 12:24:06 -070035#include "omap-iommu.h"
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020036
Suman Anna5acc97d2014-03-17 20:31:34 -050037#define to_iommu(dev) \
38 ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
39
Hiroshi DOYU37c28362010-04-27 05:37:12 +000040#define for_each_iotlb_cr(obj, n, __i, cr) \
41 for (__i = 0; \
42 (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \
43 __i++)
44
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +020045/* bitmap of the page sizes currently supported */
46#define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
47
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030048/**
49 * struct omap_iommu_domain - omap iommu domain
50 * @pgtable: the page table
51 * @iommu_dev: an omap iommu device attached to this domain. only a single
52 * iommu device can be attached for now.
Omar Ramirez Luna803b5272012-04-18 13:09:41 -050053 * @dev: Device using this domain.
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030054 * @lock: domain lock, should be taken when attaching/detaching
55 */
56struct omap_iommu_domain {
57 u32 *pgtable;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030058 struct omap_iommu *iommu_dev;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -050059 struct device *dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030060 spinlock_t lock;
61};
62
Ido Yariv7bd9e252012-11-02 12:24:09 -070063#define MMU_LOCK_BASE_SHIFT 10
64#define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
65#define MMU_LOCK_BASE(x) \
66 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
67
68#define MMU_LOCK_VICT_SHIFT 4
69#define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
70#define MMU_LOCK_VICT(x) \
71 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
72
73struct iotlb_lock {
74 short base;
75 short vict;
76};
77
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020078/* accommodate the difference between omap1 and omap2/3 */
79static const struct iommu_functions *arch_iommu;
80
81static struct platform_driver omap_iommu_driver;
82static struct kmem_cache *iopte_cachep;
83
84/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030085 * omap_install_iommu_arch - Install archtecure specific iommu functions
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020086 * @ops: a pointer to architecture specific iommu functions
87 *
88 * There are several kind of iommu algorithm(tlb, pagetable) among
89 * omap series. This interface installs such an iommu algorighm.
90 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030091int omap_install_iommu_arch(const struct iommu_functions *ops)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020092{
93 if (arch_iommu)
94 return -EBUSY;
95
96 arch_iommu = ops;
97 return 0;
98}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030099EXPORT_SYMBOL_GPL(omap_install_iommu_arch);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200100
101/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300102 * omap_uninstall_iommu_arch - Uninstall archtecure specific iommu functions
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200103 * @ops: a pointer to architecture specific iommu functions
104 *
105 * This interface uninstalls the iommu algorighm installed previously.
106 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300107void omap_uninstall_iommu_arch(const struct iommu_functions *ops)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200108{
109 if (arch_iommu != ops)
110 pr_err("%s: not your arch\n", __func__);
111
112 arch_iommu = NULL;
113}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300114EXPORT_SYMBOL_GPL(omap_uninstall_iommu_arch);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200115
116/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300117 * omap_iommu_save_ctx - Save registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200118 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200119 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200120void omap_iommu_save_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200121{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200122 struct omap_iommu *obj = dev_to_omap_iommu(dev);
123
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200124 arch_iommu->save_ctx(obj);
125}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300126EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200127
128/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300129 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200130 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200131 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200132void omap_iommu_restore_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200133{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200134 struct omap_iommu *obj = dev_to_omap_iommu(dev);
135
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200136 arch_iommu->restore_ctx(obj);
137}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300138EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200139
140/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300141 * omap_iommu_arch_version - Return running iommu arch version
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200142 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300143u32 omap_iommu_arch_version(void)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200144{
145 return arch_iommu->version;
146}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300147EXPORT_SYMBOL_GPL(omap_iommu_arch_version);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200148
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300149static int iommu_enable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200150{
151 int err;
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600152 struct platform_device *pdev = to_platform_device(obj->dev);
153 struct iommu_platform_data *pdata = pdev->dev.platform_data;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200154
Martin Hostettleref4815a2011-02-24 12:51:31 -0800155 if (!arch_iommu)
156 return -ENODEV;
157
Florian Vaussard90e569c2014-02-28 14:42:34 -0600158 if (pdata && pdata->deassert_reset) {
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600159 err = pdata->deassert_reset(pdev, pdata->reset_name);
160 if (err) {
161 dev_err(obj->dev, "deassert_reset failed: %d\n", err);
162 return err;
163 }
164 }
165
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600166 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200167
168 err = arch_iommu->enable(obj);
169
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200170 return err;
171}
172
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300173static void iommu_disable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200174{
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600175 struct platform_device *pdev = to_platform_device(obj->dev);
176 struct iommu_platform_data *pdata = pdev->dev.platform_data;
177
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200178 arch_iommu->disable(obj);
179
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600180 pm_runtime_put_sync(obj->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600181
Florian Vaussard90e569c2014-02-28 14:42:34 -0600182 if (pdata && pdata->assert_reset)
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600183 pdata->assert_reset(pdev, pdata->reset_name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200184}
185
186/*
187 * TLB operations
188 */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300189void omap_iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200190{
191 BUG_ON(!cr || !e);
192
193 arch_iommu->cr_to_e(cr, e);
194}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300195EXPORT_SYMBOL_GPL(omap_iotlb_cr_to_e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200196
197static inline int iotlb_cr_valid(struct cr_regs *cr)
198{
199 if (!cr)
200 return -EINVAL;
201
202 return arch_iommu->cr_valid(cr);
203}
204
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300205static inline struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200206 struct iotlb_entry *e)
207{
208 if (!e)
209 return NULL;
210
211 return arch_iommu->alloc_cr(obj, e);
212}
213
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300214static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200215{
216 return arch_iommu->cr_to_virt(cr);
217}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200218
219static u32 get_iopte_attr(struct iotlb_entry *e)
220{
221 return arch_iommu->get_pte_attr(e);
222}
223
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300224static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200225{
226 return arch_iommu->fault_isr(obj, da);
227}
228
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300229static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200230{
231 u32 val;
232
233 val = iommu_read_reg(obj, MMU_LOCK);
234
235 l->base = MMU_LOCK_BASE(val);
236 l->vict = MMU_LOCK_VICT(val);
237
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200238}
239
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300240static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200241{
242 u32 val;
243
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200244 val = (l->base << MMU_LOCK_BASE_SHIFT);
245 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
246
247 iommu_write_reg(obj, val, MMU_LOCK);
248}
249
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300250static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200251{
252 arch_iommu->tlb_read_cr(obj, cr);
253}
254
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300255static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200256{
257 arch_iommu->tlb_load_cr(obj, cr);
258
259 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
260 iommu_write_reg(obj, 1, MMU_LD_TLB);
261}
262
263/**
264 * iotlb_dump_cr - Dump an iommu tlb entry into buf
265 * @obj: target iommu
266 * @cr: contents of cam and ram register
267 * @buf: output buffer
268 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300269static inline ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200270 char *buf)
271{
272 BUG_ON(!cr || !buf);
273
274 return arch_iommu->dump_cr(obj, cr, buf);
275}
276
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000277/* only used in iotlb iteration for-loop */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300278static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000279{
280 struct cr_regs cr;
281 struct iotlb_lock l;
282
283 iotlb_lock_get(obj, &l);
284 l.vict = n;
285 iotlb_lock_set(obj, &l);
286 iotlb_read_cr(obj, &cr);
287
288 return cr;
289}
290
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200291/**
292 * load_iotlb_entry - Set an iommu tlb entry
293 * @obj: target iommu
294 * @e: an iommu tlb entry info
295 **/
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300296#ifdef PREFETCH_IOTLB
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300297static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200298{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200299 int err = 0;
300 struct iotlb_lock l;
301 struct cr_regs *cr;
302
303 if (!obj || !obj->nr_tlb_entries || !e)
304 return -EINVAL;
305
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600306 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200307
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000308 iotlb_lock_get(obj, &l);
309 if (l.base == obj->nr_tlb_entries) {
310 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200311 err = -EBUSY;
312 goto out;
313 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000314 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000315 int i;
316 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000317
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000318 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000319 if (!iotlb_cr_valid(&tmp))
320 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000321
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000322 if (i == obj->nr_tlb_entries) {
323 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
324 err = -EBUSY;
325 goto out;
326 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000327
328 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000329 } else {
330 l.vict = l.base;
331 iotlb_lock_set(obj, &l);
332 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200333
334 cr = iotlb_alloc_cr(obj, e);
335 if (IS_ERR(cr)) {
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600336 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200337 return PTR_ERR(cr);
338 }
339
340 iotlb_load_cr(obj, cr);
341 kfree(cr);
342
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000343 if (e->prsvd)
344 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200345 /* increment victim for next tlb load */
346 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000347 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200348 iotlb_lock_set(obj, &l);
349out:
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600350 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200351 return err;
352}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200353
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300354#else /* !PREFETCH_IOTLB */
355
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300356static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300357{
358 return 0;
359}
360
361#endif /* !PREFETCH_IOTLB */
362
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300363static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300364{
365 return load_iotlb_entry(obj, e);
366}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200367
368/**
369 * flush_iotlb_page - Clear an iommu tlb entry
370 * @obj: target iommu
371 * @da: iommu device virtual address
372 *
373 * Clear an iommu tlb entry which includes 'da' address.
374 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300375static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200376{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200377 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000378 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200379
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600380 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200381
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000382 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200383 u32 start;
384 size_t bytes;
385
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200386 if (!iotlb_cr_valid(&cr))
387 continue;
388
389 start = iotlb_cr_to_virt(&cr);
390 bytes = iopgsz_to_bytes(cr.cam & 3);
391
392 if ((start <= da) && (da < start + bytes)) {
393 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
394 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000395 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200396 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
397 }
398 }
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600399 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200400
401 if (i == obj->nr_tlb_entries)
402 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
403}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200404
405/**
406 * flush_iotlb_all - Clear all iommu tlb entries
407 * @obj: target iommu
408 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300409static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200410{
411 struct iotlb_lock l;
412
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600413 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200414
415 l.base = 0;
416 l.vict = 0;
417 iotlb_lock_set(obj, &l);
418
419 iommu_write_reg(obj, 1, MMU_GFLUSH);
420
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600421 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200422}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200423
Arnd Bergmanne4efd942011-10-02 14:34:05 -0400424#if defined(CONFIG_OMAP_IOMMU_DEBUG) || defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
Kanigeri, Hariddfa9752010-05-24 02:01:51 +0000425
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300426ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200427{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200428 if (!obj || !buf)
429 return -EINVAL;
430
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600431 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200432
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700433 bytes = arch_iommu->dump_ctx(obj, buf, bytes);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200434
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600435 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200436
437 return bytes;
438}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300439EXPORT_SYMBOL_GPL(omap_iommu_dump_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200440
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300441static int
442__dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200443{
444 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000445 struct iotlb_lock saved;
446 struct cr_regs tmp;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200447 struct cr_regs *p = crs;
448
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600449 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200450 iotlb_lock_get(obj, &saved);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200451
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000452 for_each_iotlb_cr(obj, num, i, tmp) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200453 if (!iotlb_cr_valid(&tmp))
454 continue;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200455 *p++ = tmp;
456 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000457
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200458 iotlb_lock_set(obj, &saved);
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600459 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200460
461 return p - crs;
462}
463
464/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300465 * omap_dump_tlb_entries - dump cr arrays to given buffer
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200466 * @obj: target iommu
467 * @buf: output buffer
468 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300469size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200470{
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700471 int i, num;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200472 struct cr_regs *cr;
473 char *p = buf;
474
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700475 num = bytes / sizeof(*cr);
476 num = min(obj->nr_tlb_entries, num);
477
478 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200479 if (!cr)
480 return 0;
481
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700482 num = __dump_tlb_entries(obj, cr, num);
483 for (i = 0; i < num; i++)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200484 p += iotlb_dump_cr(obj, cr + i, p);
485 kfree(cr);
486
487 return p - buf;
488}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300489EXPORT_SYMBOL_GPL(omap_dump_tlb_entries);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200490
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300491int omap_foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200492{
493 return driver_for_each_device(&omap_iommu_driver.driver,
494 NULL, data, fn);
495}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300496EXPORT_SYMBOL_GPL(omap_foreach_iommu_device);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200497
498#endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
499
500/*
501 * H/W pagetable operations
502 */
503static void flush_iopgd_range(u32 *first, u32 *last)
504{
505 /* FIXME: L2 cache should be taken care of if it exists */
506 do {
507 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
508 : : "r" (first));
509 first += L1_CACHE_BYTES / sizeof(*first);
510 } while (first <= last);
511}
512
513static void flush_iopte_range(u32 *first, u32 *last)
514{
515 /* FIXME: L2 cache should be taken care of if it exists */
516 do {
517 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
518 : : "r" (first));
519 first += L1_CACHE_BYTES / sizeof(*first);
520 } while (first <= last);
521}
522
523static void iopte_free(u32 *iopte)
524{
525 /* Note: freed iopte's must be clean ready for re-use */
Zhouyi Zhoue28045a2014-03-05 18:20:19 +0800526 if (iopte)
527 kmem_cache_free(iopte_cachep, iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200528}
529
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300530static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200531{
532 u32 *iopte;
533
534 /* a table has already existed */
535 if (*iopgd)
536 goto pte_ready;
537
538 /*
539 * do the allocation outside the page table lock
540 */
541 spin_unlock(&obj->page_table_lock);
542 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
543 spin_lock(&obj->page_table_lock);
544
545 if (!*iopgd) {
546 if (!iopte)
547 return ERR_PTR(-ENOMEM);
548
549 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
550 flush_iopgd_range(iopgd, iopgd);
551
552 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
553 } else {
554 /* We raced, free the reduniovant table */
555 iopte_free(iopte);
556 }
557
558pte_ready:
559 iopte = iopte_offset(iopgd, da);
560
561 dev_vdbg(obj->dev,
562 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
563 __func__, da, iopgd, *iopgd, iopte, *iopte);
564
565 return iopte;
566}
567
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300568static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200569{
570 u32 *iopgd = iopgd_offset(obj, da);
571
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300572 if ((da | pa) & ~IOSECTION_MASK) {
573 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
574 __func__, da, pa, IOSECTION_SIZE);
575 return -EINVAL;
576 }
577
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200578 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
579 flush_iopgd_range(iopgd, iopgd);
580 return 0;
581}
582
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300583static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200584{
585 u32 *iopgd = iopgd_offset(obj, da);
586 int i;
587
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300588 if ((da | pa) & ~IOSUPER_MASK) {
589 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
590 __func__, da, pa, IOSUPER_SIZE);
591 return -EINVAL;
592 }
593
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200594 for (i = 0; i < 16; i++)
595 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
596 flush_iopgd_range(iopgd, iopgd + 15);
597 return 0;
598}
599
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300600static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200601{
602 u32 *iopgd = iopgd_offset(obj, da);
603 u32 *iopte = iopte_alloc(obj, iopgd, da);
604
605 if (IS_ERR(iopte))
606 return PTR_ERR(iopte);
607
608 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
609 flush_iopte_range(iopte, iopte);
610
611 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
612 __func__, da, pa, iopte, *iopte);
613
614 return 0;
615}
616
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300617static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200618{
619 u32 *iopgd = iopgd_offset(obj, da);
620 u32 *iopte = iopte_alloc(obj, iopgd, da);
621 int i;
622
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300623 if ((da | pa) & ~IOLARGE_MASK) {
624 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
625 __func__, da, pa, IOLARGE_SIZE);
626 return -EINVAL;
627 }
628
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200629 if (IS_ERR(iopte))
630 return PTR_ERR(iopte);
631
632 for (i = 0; i < 16; i++)
633 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
634 flush_iopte_range(iopte, iopte + 15);
635 return 0;
636}
637
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300638static int
639iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200640{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300641 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200642 u32 prot;
643 int err;
644
645 if (!obj || !e)
646 return -EINVAL;
647
648 switch (e->pgsz) {
649 case MMU_CAM_PGSZ_16M:
650 fn = iopgd_alloc_super;
651 break;
652 case MMU_CAM_PGSZ_1M:
653 fn = iopgd_alloc_section;
654 break;
655 case MMU_CAM_PGSZ_64K:
656 fn = iopte_alloc_large;
657 break;
658 case MMU_CAM_PGSZ_4K:
659 fn = iopte_alloc_page;
660 break;
661 default:
662 fn = NULL;
663 BUG();
664 break;
665 }
666
667 prot = get_iopte_attr(e);
668
669 spin_lock(&obj->page_table_lock);
670 err = fn(obj, e->da, e->pa, prot);
671 spin_unlock(&obj->page_table_lock);
672
673 return err;
674}
675
676/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300677 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200678 * @obj: target iommu
679 * @e: an iommu tlb entry info
680 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300681int omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200682{
683 int err;
684
685 flush_iotlb_page(obj, e->da);
686 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200687 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300688 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200689 return err;
690}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300691EXPORT_SYMBOL_GPL(omap_iopgtable_store_entry);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200692
693/**
694 * iopgtable_lookup_entry - Lookup an iommu pte entry
695 * @obj: target iommu
696 * @da: iommu device virtual address
697 * @ppgd: iommu pgd entry pointer to be returned
698 * @ppte: iommu pte entry pointer to be returned
699 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300700static void
701iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200702{
703 u32 *iopgd, *iopte = NULL;
704
705 iopgd = iopgd_offset(obj, da);
706 if (!*iopgd)
707 goto out;
708
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300709 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200710 iopte = iopte_offset(iopgd, da);
711out:
712 *ppgd = iopgd;
713 *ppte = iopte;
714}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200715
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300716static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200717{
718 size_t bytes;
719 u32 *iopgd = iopgd_offset(obj, da);
720 int nent = 1;
721
722 if (!*iopgd)
723 return 0;
724
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300725 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200726 int i;
727 u32 *iopte = iopte_offset(iopgd, da);
728
729 bytes = IOPTE_SIZE;
730 if (*iopte & IOPTE_LARGE) {
731 nent *= 16;
732 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800733 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200734 }
735 bytes *= nent;
736 memset(iopte, 0, nent * sizeof(*iopte));
737 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
738
739 /*
740 * do table walk to check if this table is necessary or not
741 */
742 iopte = iopte_offset(iopgd, 0);
743 for (i = 0; i < PTRS_PER_IOPTE; i++)
744 if (iopte[i])
745 goto out;
746
747 iopte_free(iopte);
748 nent = 1; /* for the next L1 entry */
749 } else {
750 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700751 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200752 nent *= 16;
753 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800754 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200755 }
756 bytes *= nent;
757 }
758 memset(iopgd, 0, nent * sizeof(*iopgd));
759 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
760out:
761 return bytes;
762}
763
764/**
765 * iopgtable_clear_entry - Remove an iommu pte entry
766 * @obj: target iommu
767 * @da: iommu device virtual address
768 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300769static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200770{
771 size_t bytes;
772
773 spin_lock(&obj->page_table_lock);
774
775 bytes = iopgtable_clear_entry_core(obj, da);
776 flush_iotlb_page(obj, da);
777
778 spin_unlock(&obj->page_table_lock);
779
780 return bytes;
781}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200782
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300783static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200784{
785 int i;
786
787 spin_lock(&obj->page_table_lock);
788
789 for (i = 0; i < PTRS_PER_IOPGD; i++) {
790 u32 da;
791 u32 *iopgd;
792
793 da = i << IOPGD_SHIFT;
794 iopgd = iopgd_offset(obj, da);
795
796 if (!*iopgd)
797 continue;
798
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300799 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200800 iopte_free(iopte_offset(iopgd, 0));
801
802 *iopgd = 0;
803 flush_iopgd_range(iopgd, iopgd);
804 }
805
806 flush_iotlb_all(obj);
807
808 spin_unlock(&obj->page_table_lock);
809}
810
811/*
812 * Device IOMMU generic operations
813 */
814static irqreturn_t iommu_fault_handler(int irq, void *data)
815{
David Cohend594f1f2011-02-16 19:35:51 +0000816 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200817 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300818 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400819 struct iommu_domain *domain = obj->domain;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200820
821 if (!obj->refcount)
822 return IRQ_NONE;
823
David Cohend594f1f2011-02-16 19:35:51 +0000824 errs = iommu_report_fault(obj, &da);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200825 if (errs == 0)
826 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000827
828 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400829 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200830 return IRQ_HANDLED;
831
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000832 iommu_disable(obj);
833
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200834 iopgd = iopgd_offset(obj, da);
835
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300836 if (!iopgd_is_table(*iopgd)) {
Suman Annab6c2e092013-05-30 18:10:59 -0500837 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
838 obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200839 return IRQ_NONE;
840 }
841
842 iopte = iopte_offset(iopgd, da);
843
Suman Annab6c2e092013-05-30 18:10:59 -0500844 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
845 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200846
847 return IRQ_NONE;
848}
849
850static int device_match_by_alias(struct device *dev, void *data)
851{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300852 struct omap_iommu *obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200853 const char *name = data;
854
855 pr_debug("%s: %s %s\n", __func__, obj->name, name);
856
857 return strcmp(obj->name, name) == 0;
858}
859
860/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300861 * omap_iommu_attach() - attach iommu device to an iommu domain
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200862 * @name: name of target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300863 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200864 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200865static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200866{
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600867 int err;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200868 struct device *dev;
869 struct omap_iommu *obj;
870
871 dev = driver_find_device(&omap_iommu_driver.driver, NULL,
872 (void *)name,
873 device_match_by_alias);
874 if (!dev)
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600875 return ERR_PTR(-ENODEV);
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200876
877 obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200878
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300879 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200880
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300881 /* an iommu device can only be attached once */
882 if (++obj->refcount > 1) {
883 dev_err(dev, "%s: already attached!\n", obj->name);
884 err = -EBUSY;
885 goto err_enable;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200886 }
887
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300888 obj->iopgd = iopgd;
889 err = iommu_enable(obj);
890 if (err)
891 goto err_enable;
892 flush_iotlb_all(obj);
893
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600894 if (!try_module_get(obj->owner)) {
895 err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200896 goto err_module;
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600897 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200898
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300899 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200900
901 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
902 return obj;
903
904err_module:
905 if (obj->refcount == 1)
906 iommu_disable(obj);
907err_enable:
908 obj->refcount--;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300909 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200910 return ERR_PTR(err);
911}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200912
913/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300914 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200915 * @obj: target iommu
916 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300917static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200918{
Roel Kluinacf9d462010-01-08 10:29:05 -0800919 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200920 return;
921
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300922 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200923
924 if (--obj->refcount == 0)
925 iommu_disable(obj);
926
927 module_put(obj->owner);
928
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300929 obj->iopgd = NULL;
930
931 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200932
933 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
934}
David Cohend594f1f2011-02-16 19:35:51 +0000935
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200936/*
937 * OMAP Device MMU(IOMMU) detection
938 */
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -0800939static int omap_iommu_probe(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200940{
941 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200942 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300943 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200944 struct resource *res;
945 struct iommu_platform_data *pdata = pdev->dev.platform_data;
Florian Vaussard3c927482014-02-28 14:42:36 -0600946 struct device_node *of = pdev->dev.of_node;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200947
Suman Annaf129b3d2014-02-28 14:42:32 -0600948 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200949 if (!obj)
950 return -ENOMEM;
951
Florian Vaussard3c927482014-02-28 14:42:36 -0600952 if (of) {
953 obj->name = dev_name(&pdev->dev);
954 obj->nr_tlb_entries = 32;
955 err = of_property_read_u32(of, "ti,#tlb-entries",
956 &obj->nr_tlb_entries);
957 if (err && err != -EINVAL)
958 return err;
959 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
960 return -EINVAL;
961 /*
962 * da_start and da_end are needed for omap-iovmm, so hardcode
963 * these values as used by OMAP3 ISP - the only user for
964 * omap-iovmm
965 */
966 obj->da_start = 0;
967 obj->da_end = 0xfffff000;
Suman Annab148d5f2014-02-28 14:42:37 -0600968 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
969 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
Florian Vaussard3c927482014-02-28 14:42:36 -0600970 } else {
971 obj->nr_tlb_entries = pdata->nr_tlb_entries;
972 obj->name = pdata->name;
973 obj->da_start = pdata->da_start;
974 obj->da_end = pdata->da_end;
975 }
976 if (obj->da_end <= obj->da_start)
977 return -EINVAL;
978
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200979 obj->dev = &pdev->dev;
980 obj->ctx = (void *)obj + sizeof(*obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200981
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300982 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200983 mutex_init(&obj->mmap_lock);
984 spin_lock_init(&obj->page_table_lock);
985 INIT_LIST_HEAD(&obj->mmap);
986
987 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -0600988 obj->regbase = devm_ioremap_resource(obj->dev, res);
989 if (IS_ERR(obj->regbase))
990 return PTR_ERR(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +0000991
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200992 irq = platform_get_irq(pdev, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -0600993 if (irq < 0)
994 return -ENODEV;
995
996 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
997 dev_name(obj->dev), obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200998 if (err < 0)
Suman Annaf129b3d2014-02-28 14:42:32 -0600999 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001000 platform_set_drvdata(pdev, obj);
1001
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001002 pm_runtime_irq_safe(obj->dev);
1003 pm_runtime_enable(obj->dev);
1004
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001005 dev_info(&pdev->dev, "%s registered\n", obj->name);
1006 return 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001007}
1008
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001009static int omap_iommu_remove(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001010{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001011 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001012
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001013 iopgtable_clear_entry_all(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001014
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001015 pm_runtime_disable(obj->dev);
1016
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001017 dev_info(&pdev->dev, "%s removed\n", obj->name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001018 return 0;
1019}
1020
Florian Vaussard3c927482014-02-28 14:42:36 -06001021static struct of_device_id omap_iommu_of_match[] = {
1022 { .compatible = "ti,omap2-iommu" },
1023 { .compatible = "ti,omap4-iommu" },
1024 { .compatible = "ti,dra7-iommu" },
1025 {},
1026};
1027MODULE_DEVICE_TABLE(of, omap_iommu_of_match);
1028
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001029static struct platform_driver omap_iommu_driver = {
1030 .probe = omap_iommu_probe,
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001031 .remove = omap_iommu_remove,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001032 .driver = {
1033 .name = "omap-iommu",
Florian Vaussard3c927482014-02-28 14:42:36 -06001034 .of_match_table = of_match_ptr(omap_iommu_of_match),
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001035 },
1036};
1037
1038static void iopte_cachep_ctor(void *iopte)
1039{
1040 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1041}
1042
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001043static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa,
1044 u32 flags)
1045{
1046 memset(e, 0, sizeof(*e));
1047
1048 e->da = da;
1049 e->pa = pa;
Suman Annad760e3e2014-03-17 20:31:32 -05001050 e->valid = MMU_CAM_V;
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001051 /* FIXME: add OMAP1 support */
1052 e->pgsz = flags & MMU_CAM_PGSZ_MASK;
1053 e->endian = flags & MMU_RAM_ENDIAN_MASK;
1054 e->elsz = flags & MMU_RAM_ELSZ_MASK;
1055 e->mixed = flags & MMU_RAM_MIXED_MASK;
1056
1057 return iopgsz_to_bytes(e->pgsz);
1058}
1059
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001060static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001061 phys_addr_t pa, size_t bytes, int prot)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001062{
1063 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001064 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001065 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001066 struct iotlb_entry e;
1067 int omap_pgsz;
1068 u32 ret, flags;
1069
1070 /* we only support mapping a single iommu page for now */
1071 omap_pgsz = bytes_to_iopgsz(bytes);
1072 if (omap_pgsz < 0) {
1073 dev_err(dev, "invalid size to map: %d\n", bytes);
1074 return -EINVAL;
1075 }
1076
1077 dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes);
1078
1079 flags = omap_pgsz | prot;
1080
1081 iotlb_init_entry(&e, da, pa, flags);
1082
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001083 ret = omap_iopgtable_store_entry(oiommu, &e);
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001084 if (ret)
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001085 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001086
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001087 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001088}
1089
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001090static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1091 size_t size)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001092{
1093 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001094 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001095 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001096
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001097 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001098
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001099 return iopgtable_clear_entry(oiommu, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001100}
1101
1102static int
1103omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1104{
1105 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001106 struct omap_iommu *oiommu;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001107 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001108 int ret = 0;
1109
1110 spin_lock(&omap_domain->lock);
1111
1112 /* only a single device is supported per domain for now */
1113 if (omap_domain->iommu_dev) {
1114 dev_err(dev, "iommu domain is already attached\n");
1115 ret = -EBUSY;
1116 goto out;
1117 }
1118
1119 /* get a handle to and enable the omap iommu */
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001120 oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001121 if (IS_ERR(oiommu)) {
1122 ret = PTR_ERR(oiommu);
1123 dev_err(dev, "can't get omap iommu: %d\n", ret);
1124 goto out;
1125 }
1126
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001127 omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001128 omap_domain->dev = dev;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -04001129 oiommu->domain = domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001130
1131out:
1132 spin_unlock(&omap_domain->lock);
1133 return ret;
1134}
1135
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001136static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
1137 struct device *dev)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001138{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001139 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001140 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001141
1142 /* only a single device is supported per domain for now */
1143 if (omap_domain->iommu_dev != oiommu) {
1144 dev_err(dev, "invalid iommu device\n");
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001145 return;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001146 }
1147
1148 iopgtable_clear_entry_all(oiommu);
1149
1150 omap_iommu_detach(oiommu);
1151
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001152 omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001153 omap_domain->dev = NULL;
1154}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001155
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001156static void omap_iommu_detach_dev(struct iommu_domain *domain,
1157 struct device *dev)
1158{
1159 struct omap_iommu_domain *omap_domain = domain->priv;
1160
1161 spin_lock(&omap_domain->lock);
1162 _omap_iommu_detach_dev(omap_domain, dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001163 spin_unlock(&omap_domain->lock);
1164}
1165
1166static int omap_iommu_domain_init(struct iommu_domain *domain)
1167{
1168 struct omap_iommu_domain *omap_domain;
1169
1170 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1171 if (!omap_domain) {
1172 pr_err("kzalloc failed\n");
1173 goto out;
1174 }
1175
1176 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1177 if (!omap_domain->pgtable) {
1178 pr_err("kzalloc failed\n");
1179 goto fail_nomem;
1180 }
1181
1182 /*
1183 * should never fail, but please keep this around to ensure
1184 * we keep the hardware happy
1185 */
1186 BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
1187
1188 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1189 spin_lock_init(&omap_domain->lock);
1190
1191 domain->priv = omap_domain;
1192
Joerg Roedel2c6edb02012-01-26 19:40:55 +01001193 domain->geometry.aperture_start = 0;
1194 domain->geometry.aperture_end = (1ULL << 32) - 1;
1195 domain->geometry.force_aperture = true;
1196
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001197 return 0;
1198
1199fail_nomem:
1200 kfree(omap_domain);
1201out:
1202 return -ENOMEM;
1203}
1204
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001205static void omap_iommu_domain_destroy(struct iommu_domain *domain)
1206{
1207 struct omap_iommu_domain *omap_domain = domain->priv;
1208
1209 domain->priv = NULL;
1210
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001211 /*
1212 * An iommu device is still attached
1213 * (currently, only one device can be attached) ?
1214 */
1215 if (omap_domain->iommu_dev)
1216 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1217
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001218 kfree(omap_domain->pgtable);
1219 kfree(omap_domain);
1220}
1221
1222static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547ac2013-03-29 01:23:58 +05301223 dma_addr_t da)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001224{
1225 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001226 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001227 struct device *dev = oiommu->dev;
1228 u32 *pgd, *pte;
1229 phys_addr_t ret = 0;
1230
1231 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1232
1233 if (pte) {
1234 if (iopte_is_small(*pte))
1235 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1236 else if (iopte_is_large(*pte))
1237 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1238 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001239 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
1240 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001241 } else {
1242 if (iopgd_is_section(*pgd))
1243 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1244 else if (iopgd_is_super(*pgd))
1245 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1246 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001247 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
1248 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001249 }
1250
1251 return ret;
1252}
1253
Laurent Pinchart07a02032014-02-28 14:42:38 -06001254static int omap_iommu_add_device(struct device *dev)
1255{
1256 struct omap_iommu_arch_data *arch_data;
1257 struct device_node *np;
1258
1259 /*
1260 * Allocate the archdata iommu structure for DT-based devices.
1261 *
1262 * TODO: Simplify this when removing non-DT support completely from the
1263 * IOMMU users.
1264 */
1265 if (!dev->of_node)
1266 return 0;
1267
1268 np = of_parse_phandle(dev->of_node, "iommus", 0);
1269 if (!np)
1270 return 0;
1271
1272 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
1273 if (!arch_data) {
1274 of_node_put(np);
1275 return -ENOMEM;
1276 }
1277
1278 arch_data->name = kstrdup(dev_name(dev), GFP_KERNEL);
1279 dev->archdata.iommu = arch_data;
1280
1281 of_node_put(np);
1282
1283 return 0;
1284}
1285
1286static void omap_iommu_remove_device(struct device *dev)
1287{
1288 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1289
1290 if (!dev->of_node || !arch_data)
1291 return;
1292
1293 kfree(arch_data->name);
1294 kfree(arch_data);
1295}
1296
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001297static struct iommu_ops omap_iommu_ops = {
1298 .domain_init = omap_iommu_domain_init,
1299 .domain_destroy = omap_iommu_domain_destroy,
1300 .attach_dev = omap_iommu_attach_dev,
1301 .detach_dev = omap_iommu_detach_dev,
1302 .map = omap_iommu_map,
1303 .unmap = omap_iommu_unmap,
1304 .iova_to_phys = omap_iommu_iova_to_phys,
Laurent Pinchart07a02032014-02-28 14:42:38 -06001305 .add_device = omap_iommu_add_device,
1306 .remove_device = omap_iommu_remove_device,
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +02001307 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001308};
1309
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001310static int __init omap_iommu_init(void)
1311{
1312 struct kmem_cache *p;
1313 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1314 size_t align = 1 << 10; /* L2 pagetable alignement */
1315
1316 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1317 iopte_cachep_ctor);
1318 if (!p)
1319 return -ENOMEM;
1320 iopte_cachep = p;
1321
Joerg Roedela65bc642011-09-06 17:56:07 +02001322 bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001323
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001324 return platform_driver_register(&omap_iommu_driver);
1325}
Ohad Ben-Cohen435792d2012-02-26 12:14:14 +02001326/* must be ready before omap3isp is probed */
1327subsys_initcall(omap_iommu_init);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001328
1329static void __exit omap_iommu_exit(void)
1330{
1331 kmem_cache_destroy(iopte_cachep);
1332
1333 platform_driver_unregister(&omap_iommu_driver);
1334}
1335module_exit(omap_iommu_exit);
1336
1337MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1338MODULE_ALIAS("platform:omap-iommu");
1339MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1340MODULE_LICENSE("GPL v2");