blob: ea04e4d30bbfc44e039995ac0ccfb161eb49f956 [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>
Suman Anna7d682772014-09-04 17:27:30 -050029#include <linux/of_platform.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020030
31#include <asm/cacheflush.h>
32
Tony Lindgren2ab7c842012-11-02 12:24:14 -070033#include <linux/platform_data/iommu-omap.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020034
Ido Yariv2f7702a2012-11-02 12:24:00 -070035#include "omap-iopgtable.h"
Tony Lindgrened1c7de2012-11-02 12:24:06 -070036#include "omap-iommu.h"
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020037
Suman Anna5acc97d2014-03-17 20:31:34 -050038#define to_iommu(dev) \
39 ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
40
Hiroshi DOYU37c28362010-04-27 05:37:12 +000041#define for_each_iotlb_cr(obj, n, __i, cr) \
42 for (__i = 0; \
43 (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \
44 __i++)
45
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +020046/* bitmap of the page sizes currently supported */
47#define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
48
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030049/**
50 * struct omap_iommu_domain - omap iommu domain
51 * @pgtable: the page table
52 * @iommu_dev: an omap iommu device attached to this domain. only a single
53 * iommu device can be attached for now.
Omar Ramirez Luna803b5272012-04-18 13:09:41 -050054 * @dev: Device using this domain.
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030055 * @lock: domain lock, should be taken when attaching/detaching
56 */
57struct omap_iommu_domain {
58 u32 *pgtable;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030059 struct omap_iommu *iommu_dev;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -050060 struct device *dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030061 spinlock_t lock;
62};
63
Ido Yariv7bd9e252012-11-02 12:24:09 -070064#define MMU_LOCK_BASE_SHIFT 10
65#define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
66#define MMU_LOCK_BASE(x) \
67 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
68
69#define MMU_LOCK_VICT_SHIFT 4
70#define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
71#define MMU_LOCK_VICT(x) \
72 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
73
74struct iotlb_lock {
75 short base;
76 short vict;
77};
78
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020079/* accommodate the difference between omap1 and omap2/3 */
80static const struct iommu_functions *arch_iommu;
81
82static struct platform_driver omap_iommu_driver;
83static struct kmem_cache *iopte_cachep;
84
85/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030086 * omap_install_iommu_arch - Install archtecure specific iommu functions
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020087 * @ops: a pointer to architecture specific iommu functions
88 *
89 * There are several kind of iommu algorithm(tlb, pagetable) among
90 * omap series. This interface installs such an iommu algorighm.
91 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030092int omap_install_iommu_arch(const struct iommu_functions *ops)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020093{
94 if (arch_iommu)
95 return -EBUSY;
96
97 arch_iommu = ops;
98 return 0;
99}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300100EXPORT_SYMBOL_GPL(omap_install_iommu_arch);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200101
102/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300103 * omap_uninstall_iommu_arch - Uninstall archtecure specific iommu functions
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200104 * @ops: a pointer to architecture specific iommu functions
105 *
106 * This interface uninstalls the iommu algorighm installed previously.
107 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300108void omap_uninstall_iommu_arch(const struct iommu_functions *ops)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200109{
110 if (arch_iommu != ops)
111 pr_err("%s: not your arch\n", __func__);
112
113 arch_iommu = NULL;
114}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300115EXPORT_SYMBOL_GPL(omap_uninstall_iommu_arch);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200116
117/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300118 * omap_iommu_save_ctx - Save registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200119 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200120 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200121void omap_iommu_save_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200122{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200123 struct omap_iommu *obj = dev_to_omap_iommu(dev);
124
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200125 arch_iommu->save_ctx(obj);
126}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300127EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200128
129/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300130 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200131 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200132 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200133void omap_iommu_restore_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200134{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200135 struct omap_iommu *obj = dev_to_omap_iommu(dev);
136
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200137 arch_iommu->restore_ctx(obj);
138}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300139EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200140
141/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300142 * omap_iommu_arch_version - Return running iommu arch version
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200143 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300144u32 omap_iommu_arch_version(void)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200145{
146 return arch_iommu->version;
147}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300148EXPORT_SYMBOL_GPL(omap_iommu_arch_version);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200149
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300150static int iommu_enable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200151{
152 int err;
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600153 struct platform_device *pdev = to_platform_device(obj->dev);
154 struct iommu_platform_data *pdata = pdev->dev.platform_data;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200155
Martin Hostettleref4815a2011-02-24 12:51:31 -0800156 if (!arch_iommu)
157 return -ENODEV;
158
Florian Vaussard90e569c2014-02-28 14:42:34 -0600159 if (pdata && pdata->deassert_reset) {
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600160 err = pdata->deassert_reset(pdev, pdata->reset_name);
161 if (err) {
162 dev_err(obj->dev, "deassert_reset failed: %d\n", err);
163 return err;
164 }
165 }
166
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600167 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200168
169 err = arch_iommu->enable(obj);
170
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200171 return err;
172}
173
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300174static void iommu_disable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200175{
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600176 struct platform_device *pdev = to_platform_device(obj->dev);
177 struct iommu_platform_data *pdata = pdev->dev.platform_data;
178
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200179 arch_iommu->disable(obj);
180
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600181 pm_runtime_put_sync(obj->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600182
Florian Vaussard90e569c2014-02-28 14:42:34 -0600183 if (pdata && pdata->assert_reset)
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600184 pdata->assert_reset(pdev, pdata->reset_name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200185}
186
187/*
188 * TLB operations
189 */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300190void omap_iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200191{
192 BUG_ON(!cr || !e);
193
194 arch_iommu->cr_to_e(cr, e);
195}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300196EXPORT_SYMBOL_GPL(omap_iotlb_cr_to_e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200197
198static inline int iotlb_cr_valid(struct cr_regs *cr)
199{
200 if (!cr)
201 return -EINVAL;
202
203 return arch_iommu->cr_valid(cr);
204}
205
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300206static inline struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200207 struct iotlb_entry *e)
208{
209 if (!e)
210 return NULL;
211
212 return arch_iommu->alloc_cr(obj, e);
213}
214
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300215static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200216{
217 return arch_iommu->cr_to_virt(cr);
218}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200219
220static u32 get_iopte_attr(struct iotlb_entry *e)
221{
222 return arch_iommu->get_pte_attr(e);
223}
224
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300225static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200226{
227 return arch_iommu->fault_isr(obj, da);
228}
229
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300230static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200231{
232 u32 val;
233
234 val = iommu_read_reg(obj, MMU_LOCK);
235
236 l->base = MMU_LOCK_BASE(val);
237 l->vict = MMU_LOCK_VICT(val);
238
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200239}
240
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300241static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200242{
243 u32 val;
244
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200245 val = (l->base << MMU_LOCK_BASE_SHIFT);
246 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
247
248 iommu_write_reg(obj, val, MMU_LOCK);
249}
250
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300251static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200252{
253 arch_iommu->tlb_read_cr(obj, cr);
254}
255
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300256static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200257{
258 arch_iommu->tlb_load_cr(obj, cr);
259
260 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
261 iommu_write_reg(obj, 1, MMU_LD_TLB);
262}
263
264/**
265 * iotlb_dump_cr - Dump an iommu tlb entry into buf
266 * @obj: target iommu
267 * @cr: contents of cam and ram register
268 * @buf: output buffer
269 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300270static inline ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200271 char *buf)
272{
273 BUG_ON(!cr || !buf);
274
275 return arch_iommu->dump_cr(obj, cr, buf);
276}
277
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000278/* only used in iotlb iteration for-loop */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300279static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000280{
281 struct cr_regs cr;
282 struct iotlb_lock l;
283
284 iotlb_lock_get(obj, &l);
285 l.vict = n;
286 iotlb_lock_set(obj, &l);
287 iotlb_read_cr(obj, &cr);
288
289 return cr;
290}
291
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200292/**
293 * load_iotlb_entry - Set an iommu tlb entry
294 * @obj: target iommu
295 * @e: an iommu tlb entry info
296 **/
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300297#ifdef PREFETCH_IOTLB
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300298static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200299{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200300 int err = 0;
301 struct iotlb_lock l;
302 struct cr_regs *cr;
303
304 if (!obj || !obj->nr_tlb_entries || !e)
305 return -EINVAL;
306
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600307 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200308
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000309 iotlb_lock_get(obj, &l);
310 if (l.base == obj->nr_tlb_entries) {
311 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200312 err = -EBUSY;
313 goto out;
314 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000315 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000316 int i;
317 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000318
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000319 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000320 if (!iotlb_cr_valid(&tmp))
321 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000322
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000323 if (i == obj->nr_tlb_entries) {
324 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
325 err = -EBUSY;
326 goto out;
327 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000328
329 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000330 } else {
331 l.vict = l.base;
332 iotlb_lock_set(obj, &l);
333 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200334
335 cr = iotlb_alloc_cr(obj, e);
336 if (IS_ERR(cr)) {
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600337 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200338 return PTR_ERR(cr);
339 }
340
341 iotlb_load_cr(obj, cr);
342 kfree(cr);
343
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000344 if (e->prsvd)
345 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200346 /* increment victim for next tlb load */
347 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000348 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200349 iotlb_lock_set(obj, &l);
350out:
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600351 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200352 return err;
353}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200354
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300355#else /* !PREFETCH_IOTLB */
356
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300357static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300358{
359 return 0;
360}
361
362#endif /* !PREFETCH_IOTLB */
363
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300364static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300365{
366 return load_iotlb_entry(obj, e);
367}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200368
369/**
370 * flush_iotlb_page - Clear an iommu tlb entry
371 * @obj: target iommu
372 * @da: iommu device virtual address
373 *
374 * Clear an iommu tlb entry which includes 'da' address.
375 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300376static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200377{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200378 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000379 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200380
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600381 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200382
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000383 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200384 u32 start;
385 size_t bytes;
386
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200387 if (!iotlb_cr_valid(&cr))
388 continue;
389
390 start = iotlb_cr_to_virt(&cr);
391 bytes = iopgsz_to_bytes(cr.cam & 3);
392
393 if ((start <= da) && (da < start + bytes)) {
394 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
395 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000396 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200397 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
Laurent Pinchartf7129a02014-03-07 23:47:03 +0100398 break;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200399 }
400 }
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600401 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200402
403 if (i == obj->nr_tlb_entries)
404 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
405}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200406
407/**
408 * flush_iotlb_all - Clear all iommu tlb entries
409 * @obj: target iommu
410 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300411static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200412{
413 struct iotlb_lock l;
414
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600415 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200416
417 l.base = 0;
418 l.vict = 0;
419 iotlb_lock_set(obj, &l);
420
421 iommu_write_reg(obj, 1, MMU_GFLUSH);
422
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600423 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200424}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200425
Arnd Bergmanne4efd942011-10-02 14:34:05 -0400426#if defined(CONFIG_OMAP_IOMMU_DEBUG) || defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
Kanigeri, Hariddfa9752010-05-24 02:01:51 +0000427
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300428ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200429{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200430 if (!obj || !buf)
431 return -EINVAL;
432
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600433 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200434
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700435 bytes = arch_iommu->dump_ctx(obj, buf, bytes);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200436
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600437 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200438
439 return bytes;
440}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300441EXPORT_SYMBOL_GPL(omap_iommu_dump_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200442
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300443static int
444__dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200445{
446 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000447 struct iotlb_lock saved;
448 struct cr_regs tmp;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200449 struct cr_regs *p = crs;
450
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600451 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200452 iotlb_lock_get(obj, &saved);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200453
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000454 for_each_iotlb_cr(obj, num, i, tmp) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200455 if (!iotlb_cr_valid(&tmp))
456 continue;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200457 *p++ = tmp;
458 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000459
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200460 iotlb_lock_set(obj, &saved);
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600461 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200462
463 return p - crs;
464}
465
466/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300467 * omap_dump_tlb_entries - dump cr arrays to given buffer
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200468 * @obj: target iommu
469 * @buf: output buffer
470 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300471size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200472{
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700473 int i, num;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200474 struct cr_regs *cr;
475 char *p = buf;
476
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700477 num = bytes / sizeof(*cr);
478 num = min(obj->nr_tlb_entries, num);
479
480 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200481 if (!cr)
482 return 0;
483
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700484 num = __dump_tlb_entries(obj, cr, num);
485 for (i = 0; i < num; i++)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200486 p += iotlb_dump_cr(obj, cr + i, p);
487 kfree(cr);
488
489 return p - buf;
490}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300491EXPORT_SYMBOL_GPL(omap_dump_tlb_entries);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200492
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300493int omap_foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200494{
495 return driver_for_each_device(&omap_iommu_driver.driver,
496 NULL, data, fn);
497}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300498EXPORT_SYMBOL_GPL(omap_foreach_iommu_device);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200499
500#endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
501
502/*
503 * H/W pagetable operations
504 */
505static void flush_iopgd_range(u32 *first, u32 *last)
506{
507 /* FIXME: L2 cache should be taken care of if it exists */
508 do {
509 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
510 : : "r" (first));
511 first += L1_CACHE_BYTES / sizeof(*first);
512 } while (first <= last);
513}
514
515static void flush_iopte_range(u32 *first, u32 *last)
516{
517 /* FIXME: L2 cache should be taken care of if it exists */
518 do {
519 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
520 : : "r" (first));
521 first += L1_CACHE_BYTES / sizeof(*first);
522 } while (first <= last);
523}
524
525static void iopte_free(u32 *iopte)
526{
527 /* Note: freed iopte's must be clean ready for re-use */
Zhouyi Zhoue28045a2014-03-05 18:20:19 +0800528 if (iopte)
529 kmem_cache_free(iopte_cachep, iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200530}
531
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300532static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200533{
534 u32 *iopte;
535
536 /* a table has already existed */
537 if (*iopgd)
538 goto pte_ready;
539
540 /*
541 * do the allocation outside the page table lock
542 */
543 spin_unlock(&obj->page_table_lock);
544 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
545 spin_lock(&obj->page_table_lock);
546
547 if (!*iopgd) {
548 if (!iopte)
549 return ERR_PTR(-ENOMEM);
550
551 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
552 flush_iopgd_range(iopgd, iopgd);
553
554 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
555 } else {
556 /* We raced, free the reduniovant table */
557 iopte_free(iopte);
558 }
559
560pte_ready:
561 iopte = iopte_offset(iopgd, da);
562
563 dev_vdbg(obj->dev,
564 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
565 __func__, da, iopgd, *iopgd, iopte, *iopte);
566
567 return iopte;
568}
569
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300570static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200571{
572 u32 *iopgd = iopgd_offset(obj, da);
573
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300574 if ((da | pa) & ~IOSECTION_MASK) {
575 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
576 __func__, da, pa, IOSECTION_SIZE);
577 return -EINVAL;
578 }
579
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200580 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
581 flush_iopgd_range(iopgd, iopgd);
582 return 0;
583}
584
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300585static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200586{
587 u32 *iopgd = iopgd_offset(obj, da);
588 int i;
589
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300590 if ((da | pa) & ~IOSUPER_MASK) {
591 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
592 __func__, da, pa, IOSUPER_SIZE);
593 return -EINVAL;
594 }
595
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200596 for (i = 0; i < 16; i++)
597 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
598 flush_iopgd_range(iopgd, iopgd + 15);
599 return 0;
600}
601
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300602static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200603{
604 u32 *iopgd = iopgd_offset(obj, da);
605 u32 *iopte = iopte_alloc(obj, iopgd, da);
606
607 if (IS_ERR(iopte))
608 return PTR_ERR(iopte);
609
610 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
611 flush_iopte_range(iopte, iopte);
612
613 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
614 __func__, da, pa, iopte, *iopte);
615
616 return 0;
617}
618
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300619static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200620{
621 u32 *iopgd = iopgd_offset(obj, da);
622 u32 *iopte = iopte_alloc(obj, iopgd, da);
623 int i;
624
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300625 if ((da | pa) & ~IOLARGE_MASK) {
626 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
627 __func__, da, pa, IOLARGE_SIZE);
628 return -EINVAL;
629 }
630
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200631 if (IS_ERR(iopte))
632 return PTR_ERR(iopte);
633
634 for (i = 0; i < 16; i++)
635 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
636 flush_iopte_range(iopte, iopte + 15);
637 return 0;
638}
639
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300640static int
641iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200642{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300643 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200644 u32 prot;
645 int err;
646
647 if (!obj || !e)
648 return -EINVAL;
649
650 switch (e->pgsz) {
651 case MMU_CAM_PGSZ_16M:
652 fn = iopgd_alloc_super;
653 break;
654 case MMU_CAM_PGSZ_1M:
655 fn = iopgd_alloc_section;
656 break;
657 case MMU_CAM_PGSZ_64K:
658 fn = iopte_alloc_large;
659 break;
660 case MMU_CAM_PGSZ_4K:
661 fn = iopte_alloc_page;
662 break;
663 default:
664 fn = NULL;
665 BUG();
666 break;
667 }
668
669 prot = get_iopte_attr(e);
670
671 spin_lock(&obj->page_table_lock);
672 err = fn(obj, e->da, e->pa, prot);
673 spin_unlock(&obj->page_table_lock);
674
675 return err;
676}
677
678/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300679 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200680 * @obj: target iommu
681 * @e: an iommu tlb entry info
682 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300683int omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200684{
685 int err;
686
687 flush_iotlb_page(obj, e->da);
688 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200689 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300690 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200691 return err;
692}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300693EXPORT_SYMBOL_GPL(omap_iopgtable_store_entry);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200694
695/**
696 * iopgtable_lookup_entry - Lookup an iommu pte entry
697 * @obj: target iommu
698 * @da: iommu device virtual address
699 * @ppgd: iommu pgd entry pointer to be returned
700 * @ppte: iommu pte entry pointer to be returned
701 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300702static void
703iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200704{
705 u32 *iopgd, *iopte = NULL;
706
707 iopgd = iopgd_offset(obj, da);
708 if (!*iopgd)
709 goto out;
710
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300711 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200712 iopte = iopte_offset(iopgd, da);
713out:
714 *ppgd = iopgd;
715 *ppte = iopte;
716}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200717
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300718static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200719{
720 size_t bytes;
721 u32 *iopgd = iopgd_offset(obj, da);
722 int nent = 1;
723
724 if (!*iopgd)
725 return 0;
726
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300727 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200728 int i;
729 u32 *iopte = iopte_offset(iopgd, da);
730
731 bytes = IOPTE_SIZE;
732 if (*iopte & IOPTE_LARGE) {
733 nent *= 16;
734 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800735 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200736 }
737 bytes *= nent;
738 memset(iopte, 0, nent * sizeof(*iopte));
739 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
740
741 /*
742 * do table walk to check if this table is necessary or not
743 */
744 iopte = iopte_offset(iopgd, 0);
745 for (i = 0; i < PTRS_PER_IOPTE; i++)
746 if (iopte[i])
747 goto out;
748
749 iopte_free(iopte);
750 nent = 1; /* for the next L1 entry */
751 } else {
752 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700753 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200754 nent *= 16;
755 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800756 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200757 }
758 bytes *= nent;
759 }
760 memset(iopgd, 0, nent * sizeof(*iopgd));
761 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
762out:
763 return bytes;
764}
765
766/**
767 * iopgtable_clear_entry - Remove an iommu pte entry
768 * @obj: target iommu
769 * @da: iommu device virtual address
770 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300771static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200772{
773 size_t bytes;
774
775 spin_lock(&obj->page_table_lock);
776
777 bytes = iopgtable_clear_entry_core(obj, da);
778 flush_iotlb_page(obj, da);
779
780 spin_unlock(&obj->page_table_lock);
781
782 return bytes;
783}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200784
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300785static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200786{
787 int i;
788
789 spin_lock(&obj->page_table_lock);
790
791 for (i = 0; i < PTRS_PER_IOPGD; i++) {
792 u32 da;
793 u32 *iopgd;
794
795 da = i << IOPGD_SHIFT;
796 iopgd = iopgd_offset(obj, da);
797
798 if (!*iopgd)
799 continue;
800
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300801 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200802 iopte_free(iopte_offset(iopgd, 0));
803
804 *iopgd = 0;
805 flush_iopgd_range(iopgd, iopgd);
806 }
807
808 flush_iotlb_all(obj);
809
810 spin_unlock(&obj->page_table_lock);
811}
812
813/*
814 * Device IOMMU generic operations
815 */
816static irqreturn_t iommu_fault_handler(int irq, void *data)
817{
David Cohend594f1f2011-02-16 19:35:51 +0000818 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200819 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300820 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400821 struct iommu_domain *domain = obj->domain;
Suman Anna2088ecb2014-10-22 17:22:19 -0500822 struct omap_iommu_domain *omap_domain = domain->priv;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200823
Suman Anna2088ecb2014-10-22 17:22:19 -0500824 if (!omap_domain->iommu_dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200825 return IRQ_NONE;
826
David Cohend594f1f2011-02-16 19:35:51 +0000827 errs = iommu_report_fault(obj, &da);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200828 if (errs == 0)
829 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000830
831 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400832 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200833 return IRQ_HANDLED;
834
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000835 iommu_disable(obj);
836
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200837 iopgd = iopgd_offset(obj, da);
838
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300839 if (!iopgd_is_table(*iopgd)) {
Suman Annab6c2e092013-05-30 18:10:59 -0500840 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
841 obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200842 return IRQ_NONE;
843 }
844
845 iopte = iopte_offset(iopgd, da);
846
Suman Annab6c2e092013-05-30 18:10:59 -0500847 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
848 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200849
850 return IRQ_NONE;
851}
852
853static int device_match_by_alias(struct device *dev, void *data)
854{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300855 struct omap_iommu *obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200856 const char *name = data;
857
858 pr_debug("%s: %s %s\n", __func__, obj->name, name);
859
860 return strcmp(obj->name, name) == 0;
861}
862
863/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300864 * omap_iommu_attach() - attach iommu device to an iommu domain
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200865 * @name: name of target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300866 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200867 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200868static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200869{
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600870 int err;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200871 struct device *dev;
872 struct omap_iommu *obj;
873
874 dev = driver_find_device(&omap_iommu_driver.driver, NULL,
875 (void *)name,
876 device_match_by_alias);
877 if (!dev)
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600878 return ERR_PTR(-ENODEV);
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200879
880 obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200881
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300882 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200883
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300884 obj->iopgd = iopgd;
885 err = iommu_enable(obj);
886 if (err)
887 goto err_enable;
888 flush_iotlb_all(obj);
889
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300890 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200891
892 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
893 return obj;
894
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200895err_enable:
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300896 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200897 return ERR_PTR(err);
898}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200899
900/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300901 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200902 * @obj: target iommu
903 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300904static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200905{
Roel Kluinacf9d462010-01-08 10:29:05 -0800906 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200907 return;
908
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300909 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200910
Suman Anna2088ecb2014-10-22 17:22:19 -0500911 iommu_disable(obj);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300912 obj->iopgd = NULL;
913
914 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200915
916 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
917}
David Cohend594f1f2011-02-16 19:35:51 +0000918
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200919/*
920 * OMAP Device MMU(IOMMU) detection
921 */
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -0800922static int omap_iommu_probe(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200923{
924 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200925 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300926 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200927 struct resource *res;
928 struct iommu_platform_data *pdata = pdev->dev.platform_data;
Florian Vaussard3c927482014-02-28 14:42:36 -0600929 struct device_node *of = pdev->dev.of_node;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200930
Suman Annaf129b3d2014-02-28 14:42:32 -0600931 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200932 if (!obj)
933 return -ENOMEM;
934
Florian Vaussard3c927482014-02-28 14:42:36 -0600935 if (of) {
936 obj->name = dev_name(&pdev->dev);
937 obj->nr_tlb_entries = 32;
938 err = of_property_read_u32(of, "ti,#tlb-entries",
939 &obj->nr_tlb_entries);
940 if (err && err != -EINVAL)
941 return err;
942 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
943 return -EINVAL;
Suman Annab148d5f2014-02-28 14:42:37 -0600944 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
945 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
Florian Vaussard3c927482014-02-28 14:42:36 -0600946 } else {
947 obj->nr_tlb_entries = pdata->nr_tlb_entries;
948 obj->name = pdata->name;
Florian Vaussard3c927482014-02-28 14:42:36 -0600949 }
Florian Vaussard3c927482014-02-28 14:42:36 -0600950
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200951 obj->dev = &pdev->dev;
952 obj->ctx = (void *)obj + sizeof(*obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200953
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300954 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200955 spin_lock_init(&obj->page_table_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200956
957 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -0600958 obj->regbase = devm_ioremap_resource(obj->dev, res);
959 if (IS_ERR(obj->regbase))
960 return PTR_ERR(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +0000961
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200962 irq = platform_get_irq(pdev, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -0600963 if (irq < 0)
964 return -ENODEV;
965
966 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
967 dev_name(obj->dev), obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200968 if (err < 0)
Suman Annaf129b3d2014-02-28 14:42:32 -0600969 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200970 platform_set_drvdata(pdev, obj);
971
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600972 pm_runtime_irq_safe(obj->dev);
973 pm_runtime_enable(obj->dev);
974
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200975 dev_info(&pdev->dev, "%s registered\n", obj->name);
976 return 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200977}
978
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -0800979static int omap_iommu_remove(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200980{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300981 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200982
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200983 iopgtable_clear_entry_all(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200984
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600985 pm_runtime_disable(obj->dev);
986
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200987 dev_info(&pdev->dev, "%s removed\n", obj->name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200988 return 0;
989}
990
Kiran Padwald943b0f2014-09-11 19:07:36 +0530991static const struct of_device_id omap_iommu_of_match[] = {
Florian Vaussard3c927482014-02-28 14:42:36 -0600992 { .compatible = "ti,omap2-iommu" },
993 { .compatible = "ti,omap4-iommu" },
994 { .compatible = "ti,dra7-iommu" },
995 {},
996};
997MODULE_DEVICE_TABLE(of, omap_iommu_of_match);
998
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200999static struct platform_driver omap_iommu_driver = {
1000 .probe = omap_iommu_probe,
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001001 .remove = omap_iommu_remove,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001002 .driver = {
1003 .name = "omap-iommu",
Florian Vaussard3c927482014-02-28 14:42:36 -06001004 .of_match_table = of_match_ptr(omap_iommu_of_match),
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001005 },
1006};
1007
1008static void iopte_cachep_ctor(void *iopte)
1009{
1010 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1011}
1012
Laurent Pinchart286f6002014-03-08 00:44:38 +01001013static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001014{
1015 memset(e, 0, sizeof(*e));
1016
1017 e->da = da;
1018 e->pa = pa;
Suman Annad760e3e2014-03-17 20:31:32 -05001019 e->valid = MMU_CAM_V;
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001020 /* FIXME: add OMAP1 support */
Laurent Pinchart286f6002014-03-08 00:44:38 +01001021 e->pgsz = pgsz;
1022 e->endian = MMU_RAM_ENDIAN_LITTLE;
1023 e->elsz = MMU_RAM_ELSZ_8;
1024 e->mixed = 0;
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001025
1026 return iopgsz_to_bytes(e->pgsz);
1027}
1028
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001029static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001030 phys_addr_t pa, size_t bytes, int prot)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001031{
1032 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001033 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001034 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001035 struct iotlb_entry e;
1036 int omap_pgsz;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001037 u32 ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001038
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001039 omap_pgsz = bytes_to_iopgsz(bytes);
1040 if (omap_pgsz < 0) {
1041 dev_err(dev, "invalid size to map: %d\n", bytes);
1042 return -EINVAL;
1043 }
1044
1045 dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes);
1046
Laurent Pinchart286f6002014-03-08 00:44:38 +01001047 iotlb_init_entry(&e, da, pa, omap_pgsz);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001048
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001049 ret = omap_iopgtable_store_entry(oiommu, &e);
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001050 if (ret)
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001051 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001052
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001053 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001054}
1055
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001056static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1057 size_t size)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001058{
1059 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001060 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001061 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001062
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001063 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001064
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001065 return iopgtable_clear_entry(oiommu, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001066}
1067
1068static int
1069omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1070{
1071 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001072 struct omap_iommu *oiommu;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001073 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001074 int ret = 0;
1075
Suman Annae3f595b2014-09-04 17:27:29 -05001076 if (!arch_data || !arch_data->name) {
1077 dev_err(dev, "device doesn't have an associated iommu\n");
1078 return -EINVAL;
1079 }
1080
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001081 spin_lock(&omap_domain->lock);
1082
1083 /* only a single device is supported per domain for now */
1084 if (omap_domain->iommu_dev) {
1085 dev_err(dev, "iommu domain is already attached\n");
1086 ret = -EBUSY;
1087 goto out;
1088 }
1089
1090 /* get a handle to and enable the omap iommu */
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001091 oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001092 if (IS_ERR(oiommu)) {
1093 ret = PTR_ERR(oiommu);
1094 dev_err(dev, "can't get omap iommu: %d\n", ret);
1095 goto out;
1096 }
1097
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001098 omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001099 omap_domain->dev = dev;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -04001100 oiommu->domain = domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001101
1102out:
1103 spin_unlock(&omap_domain->lock);
1104 return ret;
1105}
1106
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001107static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
1108 struct device *dev)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001109{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001110 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001111 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001112
1113 /* only a single device is supported per domain for now */
1114 if (omap_domain->iommu_dev != oiommu) {
1115 dev_err(dev, "invalid iommu device\n");
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001116 return;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001117 }
1118
1119 iopgtable_clear_entry_all(oiommu);
1120
1121 omap_iommu_detach(oiommu);
1122
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001123 omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001124 omap_domain->dev = NULL;
1125}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001126
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001127static void omap_iommu_detach_dev(struct iommu_domain *domain,
1128 struct device *dev)
1129{
1130 struct omap_iommu_domain *omap_domain = domain->priv;
1131
1132 spin_lock(&omap_domain->lock);
1133 _omap_iommu_detach_dev(omap_domain, dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001134 spin_unlock(&omap_domain->lock);
1135}
1136
1137static int omap_iommu_domain_init(struct iommu_domain *domain)
1138{
1139 struct omap_iommu_domain *omap_domain;
1140
1141 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1142 if (!omap_domain) {
1143 pr_err("kzalloc failed\n");
1144 goto out;
1145 }
1146
1147 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1148 if (!omap_domain->pgtable) {
1149 pr_err("kzalloc failed\n");
1150 goto fail_nomem;
1151 }
1152
1153 /*
1154 * should never fail, but please keep this around to ensure
1155 * we keep the hardware happy
1156 */
1157 BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
1158
1159 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1160 spin_lock_init(&omap_domain->lock);
1161
1162 domain->priv = omap_domain;
1163
Joerg Roedel2c6edb02012-01-26 19:40:55 +01001164 domain->geometry.aperture_start = 0;
1165 domain->geometry.aperture_end = (1ULL << 32) - 1;
1166 domain->geometry.force_aperture = true;
1167
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001168 return 0;
1169
1170fail_nomem:
1171 kfree(omap_domain);
1172out:
1173 return -ENOMEM;
1174}
1175
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001176static void omap_iommu_domain_destroy(struct iommu_domain *domain)
1177{
1178 struct omap_iommu_domain *omap_domain = domain->priv;
1179
1180 domain->priv = NULL;
1181
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001182 /*
1183 * An iommu device is still attached
1184 * (currently, only one device can be attached) ?
1185 */
1186 if (omap_domain->iommu_dev)
1187 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1188
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001189 kfree(omap_domain->pgtable);
1190 kfree(omap_domain);
1191}
1192
1193static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547ac2013-03-29 01:23:58 +05301194 dma_addr_t da)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001195{
1196 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001197 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001198 struct device *dev = oiommu->dev;
1199 u32 *pgd, *pte;
1200 phys_addr_t ret = 0;
1201
1202 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1203
1204 if (pte) {
1205 if (iopte_is_small(*pte))
1206 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1207 else if (iopte_is_large(*pte))
1208 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1209 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001210 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
1211 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001212 } else {
1213 if (iopgd_is_section(*pgd))
1214 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1215 else if (iopgd_is_super(*pgd))
1216 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1217 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001218 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
1219 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001220 }
1221
1222 return ret;
1223}
1224
Laurent Pinchart07a02032014-02-28 14:42:38 -06001225static int omap_iommu_add_device(struct device *dev)
1226{
1227 struct omap_iommu_arch_data *arch_data;
1228 struct device_node *np;
Suman Anna7d682772014-09-04 17:27:30 -05001229 struct platform_device *pdev;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001230
1231 /*
1232 * Allocate the archdata iommu structure for DT-based devices.
1233 *
1234 * TODO: Simplify this when removing non-DT support completely from the
1235 * IOMMU users.
1236 */
1237 if (!dev->of_node)
1238 return 0;
1239
1240 np = of_parse_phandle(dev->of_node, "iommus", 0);
1241 if (!np)
1242 return 0;
1243
Suman Anna7d682772014-09-04 17:27:30 -05001244 pdev = of_find_device_by_node(np);
1245 if (WARN_ON(!pdev)) {
1246 of_node_put(np);
1247 return -EINVAL;
1248 }
1249
Laurent Pinchart07a02032014-02-28 14:42:38 -06001250 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
1251 if (!arch_data) {
1252 of_node_put(np);
1253 return -ENOMEM;
1254 }
1255
Suman Anna7d682772014-09-04 17:27:30 -05001256 arch_data->name = kstrdup(dev_name(&pdev->dev), GFP_KERNEL);
Laurent Pinchart07a02032014-02-28 14:42:38 -06001257 dev->archdata.iommu = arch_data;
1258
1259 of_node_put(np);
1260
1261 return 0;
1262}
1263
1264static void omap_iommu_remove_device(struct device *dev)
1265{
1266 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1267
1268 if (!dev->of_node || !arch_data)
1269 return;
1270
1271 kfree(arch_data->name);
1272 kfree(arch_data);
1273}
1274
Thierry Redingb22f6432014-06-27 09:03:12 +02001275static const struct iommu_ops omap_iommu_ops = {
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001276 .domain_init = omap_iommu_domain_init,
1277 .domain_destroy = omap_iommu_domain_destroy,
1278 .attach_dev = omap_iommu_attach_dev,
1279 .detach_dev = omap_iommu_detach_dev,
1280 .map = omap_iommu_map,
1281 .unmap = omap_iommu_unmap,
1282 .iova_to_phys = omap_iommu_iova_to_phys,
Laurent Pinchart07a02032014-02-28 14:42:38 -06001283 .add_device = omap_iommu_add_device,
1284 .remove_device = omap_iommu_remove_device,
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +02001285 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001286};
1287
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001288static int __init omap_iommu_init(void)
1289{
1290 struct kmem_cache *p;
1291 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1292 size_t align = 1 << 10; /* L2 pagetable alignement */
1293
1294 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1295 iopte_cachep_ctor);
1296 if (!p)
1297 return -ENOMEM;
1298 iopte_cachep = p;
1299
Joerg Roedela65bc642011-09-06 17:56:07 +02001300 bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001301
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001302 return platform_driver_register(&omap_iommu_driver);
1303}
Ohad Ben-Cohen435792d2012-02-26 12:14:14 +02001304/* must be ready before omap3isp is probed */
1305subsys_initcall(omap_iommu_init);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001306
1307static void __exit omap_iommu_exit(void)
1308{
1309 kmem_cache_destroy(iopte_cachep);
1310
1311 platform_driver_unregister(&omap_iommu_driver);
1312}
1313module_exit(omap_iommu_exit);
1314
1315MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1316MODULE_ALIAS("platform:omap-iommu");
1317MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1318MODULE_LICENSE("GPL v2");