blob: 91262fa2a351139ca62afa48d82af370576d158c [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 +020079static struct platform_driver omap_iommu_driver;
80static struct kmem_cache *iopte_cachep;
81
82/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030083 * omap_iommu_save_ctx - Save registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020084 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020085 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020086void omap_iommu_save_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020087{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020088 struct omap_iommu *obj = dev_to_omap_iommu(dev);
Suman Annabd4396f2014-10-22 17:22:27 -050089 u32 *p = obj->ctx;
90 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020091
Suman Annabd4396f2014-10-22 17:22:27 -050092 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
93 p[i] = iommu_read_reg(obj, i * sizeof(u32));
94 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
95 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020096}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030097EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020098
99/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300100 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200101 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200102 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200103void omap_iommu_restore_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200104{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200105 struct omap_iommu *obj = dev_to_omap_iommu(dev);
Suman Annabd4396f2014-10-22 17:22:27 -0500106 u32 *p = obj->ctx;
107 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200108
Suman Annabd4396f2014-10-22 17:22:27 -0500109 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
110 iommu_write_reg(obj, p[i], i * sizeof(u32));
111 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
112 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200113}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300114EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200115
Suman Annabd4396f2014-10-22 17:22:27 -0500116static void __iommu_set_twl(struct omap_iommu *obj, bool on)
117{
118 u32 l = iommu_read_reg(obj, MMU_CNTL);
119
120 if (on)
121 iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
122 else
123 iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
124
125 l &= ~MMU_CNTL_MASK;
126 if (on)
127 l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
128 else
129 l |= (MMU_CNTL_MMU_EN);
130
131 iommu_write_reg(obj, l, MMU_CNTL);
132}
133
134static int omap2_iommu_enable(struct omap_iommu *obj)
135{
136 u32 l, pa;
137
138 if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
139 return -EINVAL;
140
141 pa = virt_to_phys(obj->iopgd);
142 if (!IS_ALIGNED(pa, SZ_16K))
143 return -EINVAL;
144
145 l = iommu_read_reg(obj, MMU_REVISION);
146 dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
147 (l >> 4) & 0xf, l & 0xf);
148
149 iommu_write_reg(obj, pa, MMU_TTB);
150
151 if (obj->has_bus_err_back)
152 iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
153
154 __iommu_set_twl(obj, true);
155
156 return 0;
157}
158
159static void omap2_iommu_disable(struct omap_iommu *obj)
160{
161 u32 l = iommu_read_reg(obj, MMU_CNTL);
162
163 l &= ~MMU_CNTL_MASK;
164 iommu_write_reg(obj, l, MMU_CNTL);
165
166 dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
167}
168
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300169static int iommu_enable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200170{
171 int err;
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600172 struct platform_device *pdev = to_platform_device(obj->dev);
173 struct iommu_platform_data *pdata = pdev->dev.platform_data;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200174
Florian Vaussard90e569c2014-02-28 14:42:34 -0600175 if (pdata && pdata->deassert_reset) {
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600176 err = pdata->deassert_reset(pdev, pdata->reset_name);
177 if (err) {
178 dev_err(obj->dev, "deassert_reset failed: %d\n", err);
179 return err;
180 }
181 }
182
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600183 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200184
Suman Annabd4396f2014-10-22 17:22:27 -0500185 err = omap2_iommu_enable(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200186
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200187 return err;
188}
189
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300190static void iommu_disable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200191{
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600192 struct platform_device *pdev = to_platform_device(obj->dev);
193 struct iommu_platform_data *pdata = pdev->dev.platform_data;
194
Suman Annabd4396f2014-10-22 17:22:27 -0500195 omap2_iommu_disable(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200196
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600197 pm_runtime_put_sync(obj->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600198
Florian Vaussard90e569c2014-02-28 14:42:34 -0600199 if (pdata && pdata->assert_reset)
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600200 pdata->assert_reset(pdev, pdata->reset_name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200201}
202
203/*
204 * TLB operations
205 */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300206void omap_iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200207{
208 BUG_ON(!cr || !e);
209
Suman Annabd4396f2014-10-22 17:22:27 -0500210 e->da = cr->cam & MMU_CAM_VATAG_MASK;
211 e->pa = cr->ram & MMU_RAM_PADDR_MASK;
212 e->valid = cr->cam & MMU_CAM_V;
213 e->pgsz = cr->cam & MMU_CAM_PGSZ_MASK;
214 e->endian = cr->ram & MMU_RAM_ENDIAN_MASK;
215 e->elsz = cr->ram & MMU_RAM_ELSZ_MASK;
216 e->mixed = cr->ram & MMU_RAM_MIXED;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200217}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300218EXPORT_SYMBOL_GPL(omap_iotlb_cr_to_e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200219
220static inline int iotlb_cr_valid(struct cr_regs *cr)
221{
222 if (!cr)
223 return -EINVAL;
224
Suman Annabd4396f2014-10-22 17:22:27 -0500225 return cr->cam & MMU_CAM_V;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200226}
227
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300228static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200229{
Suman Annabd4396f2014-10-22 17:22:27 -0500230 u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
231 u32 mask = get_cam_va_mask(cr->cam & page_size);
232
233 return cr->cam & mask;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200234}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200235
236static u32 get_iopte_attr(struct iotlb_entry *e)
237{
Suman Annabd4396f2014-10-22 17:22:27 -0500238 u32 attr;
239
240 attr = e->mixed << 5;
241 attr |= e->endian;
242 attr |= e->elsz >> 3;
243 attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
244 (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
245 return attr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200246}
247
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300248static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200249{
Suman Annabd4396f2014-10-22 17:22:27 -0500250 u32 status, fault_addr;
251
252 status = iommu_read_reg(obj, MMU_IRQSTATUS);
253 status &= MMU_IRQ_MASK;
254 if (!status) {
255 *da = 0;
256 return 0;
257 }
258
259 fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
260 *da = fault_addr;
261
262 iommu_write_reg(obj, status, MMU_IRQSTATUS);
263
264 return status;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200265}
266
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300267static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200268{
269 u32 val;
270
271 val = iommu_read_reg(obj, MMU_LOCK);
272
273 l->base = MMU_LOCK_BASE(val);
274 l->vict = MMU_LOCK_VICT(val);
275
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200276}
277
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300278static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200279{
280 u32 val;
281
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200282 val = (l->base << MMU_LOCK_BASE_SHIFT);
283 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
284
285 iommu_write_reg(obj, val, MMU_LOCK);
286}
287
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300288static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200289{
Suman Annabd4396f2014-10-22 17:22:27 -0500290 cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
291 cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200292}
293
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300294static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200295{
Suman Annabd4396f2014-10-22 17:22:27 -0500296 iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
297 iommu_write_reg(obj, cr->ram, MMU_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200298
299 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
300 iommu_write_reg(obj, 1, MMU_LD_TLB);
301}
302
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000303/* only used in iotlb iteration for-loop */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300304static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000305{
306 struct cr_regs cr;
307 struct iotlb_lock l;
308
309 iotlb_lock_get(obj, &l);
310 l.vict = n;
311 iotlb_lock_set(obj, &l);
312 iotlb_read_cr(obj, &cr);
313
314 return cr;
315}
316
Suman Annabd4396f2014-10-22 17:22:27 -0500317#ifdef PREFETCH_IOTLB
318static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
319 struct iotlb_entry *e)
320{
321 struct cr_regs *cr;
322
323 if (!e)
324 return NULL;
325
326 if (e->da & ~(get_cam_va_mask(e->pgsz))) {
327 dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
328 e->da);
329 return ERR_PTR(-EINVAL);
330 }
331
332 cr = kmalloc(sizeof(*cr), GFP_KERNEL);
333 if (!cr)
334 return ERR_PTR(-ENOMEM);
335
336 cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
337 cr->ram = e->pa | e->endian | e->elsz | e->mixed;
338
339 return cr;
340}
341
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200342/**
343 * load_iotlb_entry - Set an iommu tlb entry
344 * @obj: target iommu
345 * @e: an iommu tlb entry info
346 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300347static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200348{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200349 int err = 0;
350 struct iotlb_lock l;
351 struct cr_regs *cr;
352
353 if (!obj || !obj->nr_tlb_entries || !e)
354 return -EINVAL;
355
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600356 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200357
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000358 iotlb_lock_get(obj, &l);
359 if (l.base == obj->nr_tlb_entries) {
360 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200361 err = -EBUSY;
362 goto out;
363 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000364 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000365 int i;
366 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000367
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000368 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000369 if (!iotlb_cr_valid(&tmp))
370 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000371
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000372 if (i == obj->nr_tlb_entries) {
373 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
374 err = -EBUSY;
375 goto out;
376 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000377
378 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000379 } else {
380 l.vict = l.base;
381 iotlb_lock_set(obj, &l);
382 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200383
384 cr = iotlb_alloc_cr(obj, e);
385 if (IS_ERR(cr)) {
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600386 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200387 return PTR_ERR(cr);
388 }
389
390 iotlb_load_cr(obj, cr);
391 kfree(cr);
392
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000393 if (e->prsvd)
394 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200395 /* increment victim for next tlb load */
396 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000397 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200398 iotlb_lock_set(obj, &l);
399out:
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600400 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200401 return err;
402}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200403
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300404#else /* !PREFETCH_IOTLB */
405
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300406static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300407{
408 return 0;
409}
410
411#endif /* !PREFETCH_IOTLB */
412
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300413static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300414{
415 return load_iotlb_entry(obj, e);
416}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200417
418/**
419 * flush_iotlb_page - Clear an iommu tlb entry
420 * @obj: target iommu
421 * @da: iommu device virtual address
422 *
423 * Clear an iommu tlb entry which includes 'da' address.
424 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300425static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200426{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200427 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000428 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200429
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600430 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200431
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000432 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200433 u32 start;
434 size_t bytes;
435
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200436 if (!iotlb_cr_valid(&cr))
437 continue;
438
439 start = iotlb_cr_to_virt(&cr);
440 bytes = iopgsz_to_bytes(cr.cam & 3);
441
442 if ((start <= da) && (da < start + bytes)) {
443 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
444 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000445 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200446 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
Laurent Pinchartf7129a02014-03-07 23:47:03 +0100447 break;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200448 }
449 }
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600450 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200451
452 if (i == obj->nr_tlb_entries)
453 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
454}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200455
456/**
457 * flush_iotlb_all - Clear all iommu tlb entries
458 * @obj: target iommu
459 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300460static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200461{
462 struct iotlb_lock l;
463
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600464 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200465
466 l.base = 0;
467 l.vict = 0;
468 iotlb_lock_set(obj, &l);
469
470 iommu_write_reg(obj, 1, MMU_GFLUSH);
471
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600472 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200473}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200474
Arnd Bergmanne4efd942011-10-02 14:34:05 -0400475#if defined(CONFIG_OMAP_IOMMU_DEBUG) || defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
Kanigeri, Hariddfa9752010-05-24 02:01:51 +0000476
Suman Annabd4396f2014-10-22 17:22:27 -0500477#define pr_reg(name) \
478 do { \
479 ssize_t bytes; \
480 const char *str = "%20s: %08x\n"; \
481 const int maxcol = 32; \
482 bytes = snprintf(p, maxcol, str, __stringify(name), \
483 iommu_read_reg(obj, MMU_##name)); \
484 p += bytes; \
485 len -= bytes; \
486 if (len < maxcol) \
487 goto out; \
488 } while (0)
489
490static ssize_t
491omap2_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len)
492{
493 char *p = buf;
494
495 pr_reg(REVISION);
496 pr_reg(IRQSTATUS);
497 pr_reg(IRQENABLE);
498 pr_reg(WALKING_ST);
499 pr_reg(CNTL);
500 pr_reg(FAULT_AD);
501 pr_reg(TTB);
502 pr_reg(LOCK);
503 pr_reg(LD_TLB);
504 pr_reg(CAM);
505 pr_reg(RAM);
506 pr_reg(GFLUSH);
507 pr_reg(FLUSH_ENTRY);
508 pr_reg(READ_CAM);
509 pr_reg(READ_RAM);
510 pr_reg(EMU_FAULT_AD);
511out:
512 return p - buf;
513}
514
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300515ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200516{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200517 if (!obj || !buf)
518 return -EINVAL;
519
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600520 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200521
Suman Annabd4396f2014-10-22 17:22:27 -0500522 bytes = omap2_iommu_dump_ctx(obj, buf, bytes);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200523
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600524 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200525
526 return bytes;
527}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300528EXPORT_SYMBOL_GPL(omap_iommu_dump_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200529
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300530static int
531__dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200532{
533 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000534 struct iotlb_lock saved;
535 struct cr_regs tmp;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200536 struct cr_regs *p = crs;
537
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600538 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200539 iotlb_lock_get(obj, &saved);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200540
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000541 for_each_iotlb_cr(obj, num, i, tmp) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200542 if (!iotlb_cr_valid(&tmp))
543 continue;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200544 *p++ = tmp;
545 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000546
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200547 iotlb_lock_set(obj, &saved);
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600548 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200549
550 return p - crs;
551}
552
553/**
Suman Annabd4396f2014-10-22 17:22:27 -0500554 * iotlb_dump_cr - Dump an iommu tlb entry into buf
555 * @obj: target iommu
556 * @cr: contents of cam and ram register
557 * @buf: output buffer
558 **/
559static ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
560 char *buf)
561{
562 char *p = buf;
563
564 /* FIXME: Need more detail analysis of cam/ram */
565 p += sprintf(p, "%08x %08x %01x\n", cr->cam, cr->ram,
566 (cr->cam & MMU_CAM_P) ? 1 : 0);
567
568 return p - buf;
569}
570
571/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300572 * omap_dump_tlb_entries - dump cr arrays to given buffer
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200573 * @obj: target iommu
574 * @buf: output buffer
575 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300576size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200577{
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700578 int i, num;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200579 struct cr_regs *cr;
580 char *p = buf;
581
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700582 num = bytes / sizeof(*cr);
583 num = min(obj->nr_tlb_entries, num);
584
585 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200586 if (!cr)
587 return 0;
588
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700589 num = __dump_tlb_entries(obj, cr, num);
590 for (i = 0; i < num; i++)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200591 p += iotlb_dump_cr(obj, cr + i, p);
592 kfree(cr);
593
594 return p - buf;
595}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300596EXPORT_SYMBOL_GPL(omap_dump_tlb_entries);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200597
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300598int omap_foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200599{
600 return driver_for_each_device(&omap_iommu_driver.driver,
601 NULL, data, fn);
602}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300603EXPORT_SYMBOL_GPL(omap_foreach_iommu_device);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200604
605#endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
606
607/*
608 * H/W pagetable operations
609 */
610static void flush_iopgd_range(u32 *first, u32 *last)
611{
612 /* FIXME: L2 cache should be taken care of if it exists */
613 do {
614 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
615 : : "r" (first));
616 first += L1_CACHE_BYTES / sizeof(*first);
617 } while (first <= last);
618}
619
620static void flush_iopte_range(u32 *first, u32 *last)
621{
622 /* FIXME: L2 cache should be taken care of if it exists */
623 do {
624 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
625 : : "r" (first));
626 first += L1_CACHE_BYTES / sizeof(*first);
627 } while (first <= last);
628}
629
630static void iopte_free(u32 *iopte)
631{
632 /* Note: freed iopte's must be clean ready for re-use */
Zhouyi Zhoue28045a2014-03-05 18:20:19 +0800633 if (iopte)
634 kmem_cache_free(iopte_cachep, iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200635}
636
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300637static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200638{
639 u32 *iopte;
640
641 /* a table has already existed */
642 if (*iopgd)
643 goto pte_ready;
644
645 /*
646 * do the allocation outside the page table lock
647 */
648 spin_unlock(&obj->page_table_lock);
649 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
650 spin_lock(&obj->page_table_lock);
651
652 if (!*iopgd) {
653 if (!iopte)
654 return ERR_PTR(-ENOMEM);
655
656 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
657 flush_iopgd_range(iopgd, iopgd);
658
659 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
660 } else {
661 /* We raced, free the reduniovant table */
662 iopte_free(iopte);
663 }
664
665pte_ready:
666 iopte = iopte_offset(iopgd, da);
667
668 dev_vdbg(obj->dev,
669 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
670 __func__, da, iopgd, *iopgd, iopte, *iopte);
671
672 return iopte;
673}
674
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300675static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200676{
677 u32 *iopgd = iopgd_offset(obj, da);
678
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300679 if ((da | pa) & ~IOSECTION_MASK) {
680 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
681 __func__, da, pa, IOSECTION_SIZE);
682 return -EINVAL;
683 }
684
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200685 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
686 flush_iopgd_range(iopgd, iopgd);
687 return 0;
688}
689
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300690static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200691{
692 u32 *iopgd = iopgd_offset(obj, da);
693 int i;
694
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300695 if ((da | pa) & ~IOSUPER_MASK) {
696 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
697 __func__, da, pa, IOSUPER_SIZE);
698 return -EINVAL;
699 }
700
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200701 for (i = 0; i < 16; i++)
702 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
703 flush_iopgd_range(iopgd, iopgd + 15);
704 return 0;
705}
706
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300707static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200708{
709 u32 *iopgd = iopgd_offset(obj, da);
710 u32 *iopte = iopte_alloc(obj, iopgd, da);
711
712 if (IS_ERR(iopte))
713 return PTR_ERR(iopte);
714
715 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
716 flush_iopte_range(iopte, iopte);
717
718 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
719 __func__, da, pa, iopte, *iopte);
720
721 return 0;
722}
723
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300724static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200725{
726 u32 *iopgd = iopgd_offset(obj, da);
727 u32 *iopte = iopte_alloc(obj, iopgd, da);
728 int i;
729
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300730 if ((da | pa) & ~IOLARGE_MASK) {
731 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
732 __func__, da, pa, IOLARGE_SIZE);
733 return -EINVAL;
734 }
735
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200736 if (IS_ERR(iopte))
737 return PTR_ERR(iopte);
738
739 for (i = 0; i < 16; i++)
740 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
741 flush_iopte_range(iopte, iopte + 15);
742 return 0;
743}
744
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300745static int
746iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200747{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300748 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200749 u32 prot;
750 int err;
751
752 if (!obj || !e)
753 return -EINVAL;
754
755 switch (e->pgsz) {
756 case MMU_CAM_PGSZ_16M:
757 fn = iopgd_alloc_super;
758 break;
759 case MMU_CAM_PGSZ_1M:
760 fn = iopgd_alloc_section;
761 break;
762 case MMU_CAM_PGSZ_64K:
763 fn = iopte_alloc_large;
764 break;
765 case MMU_CAM_PGSZ_4K:
766 fn = iopte_alloc_page;
767 break;
768 default:
769 fn = NULL;
770 BUG();
771 break;
772 }
773
774 prot = get_iopte_attr(e);
775
776 spin_lock(&obj->page_table_lock);
777 err = fn(obj, e->da, e->pa, prot);
778 spin_unlock(&obj->page_table_lock);
779
780 return err;
781}
782
783/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300784 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200785 * @obj: target iommu
786 * @e: an iommu tlb entry info
787 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300788int omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200789{
790 int err;
791
792 flush_iotlb_page(obj, e->da);
793 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200794 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300795 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200796 return err;
797}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300798EXPORT_SYMBOL_GPL(omap_iopgtable_store_entry);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200799
800/**
801 * iopgtable_lookup_entry - Lookup an iommu pte entry
802 * @obj: target iommu
803 * @da: iommu device virtual address
804 * @ppgd: iommu pgd entry pointer to be returned
805 * @ppte: iommu pte entry pointer to be returned
806 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300807static void
808iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200809{
810 u32 *iopgd, *iopte = NULL;
811
812 iopgd = iopgd_offset(obj, da);
813 if (!*iopgd)
814 goto out;
815
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300816 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200817 iopte = iopte_offset(iopgd, da);
818out:
819 *ppgd = iopgd;
820 *ppte = iopte;
821}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200822
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300823static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200824{
825 size_t bytes;
826 u32 *iopgd = iopgd_offset(obj, da);
827 int nent = 1;
828
829 if (!*iopgd)
830 return 0;
831
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300832 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200833 int i;
834 u32 *iopte = iopte_offset(iopgd, da);
835
836 bytes = IOPTE_SIZE;
837 if (*iopte & IOPTE_LARGE) {
838 nent *= 16;
839 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800840 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200841 }
842 bytes *= nent;
843 memset(iopte, 0, nent * sizeof(*iopte));
844 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
845
846 /*
847 * do table walk to check if this table is necessary or not
848 */
849 iopte = iopte_offset(iopgd, 0);
850 for (i = 0; i < PTRS_PER_IOPTE; i++)
851 if (iopte[i])
852 goto out;
853
854 iopte_free(iopte);
855 nent = 1; /* for the next L1 entry */
856 } else {
857 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700858 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200859 nent *= 16;
860 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800861 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200862 }
863 bytes *= nent;
864 }
865 memset(iopgd, 0, nent * sizeof(*iopgd));
866 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
867out:
868 return bytes;
869}
870
871/**
872 * iopgtable_clear_entry - Remove an iommu pte entry
873 * @obj: target iommu
874 * @da: iommu device virtual address
875 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300876static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200877{
878 size_t bytes;
879
880 spin_lock(&obj->page_table_lock);
881
882 bytes = iopgtable_clear_entry_core(obj, da);
883 flush_iotlb_page(obj, da);
884
885 spin_unlock(&obj->page_table_lock);
886
887 return bytes;
888}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200889
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300890static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200891{
892 int i;
893
894 spin_lock(&obj->page_table_lock);
895
896 for (i = 0; i < PTRS_PER_IOPGD; i++) {
897 u32 da;
898 u32 *iopgd;
899
900 da = i << IOPGD_SHIFT;
901 iopgd = iopgd_offset(obj, da);
902
903 if (!*iopgd)
904 continue;
905
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300906 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200907 iopte_free(iopte_offset(iopgd, 0));
908
909 *iopgd = 0;
910 flush_iopgd_range(iopgd, iopgd);
911 }
912
913 flush_iotlb_all(obj);
914
915 spin_unlock(&obj->page_table_lock);
916}
917
918/*
919 * Device IOMMU generic operations
920 */
921static irqreturn_t iommu_fault_handler(int irq, void *data)
922{
David Cohend594f1f2011-02-16 19:35:51 +0000923 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200924 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300925 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400926 struct iommu_domain *domain = obj->domain;
Suman Anna2088ecb2014-10-22 17:22:19 -0500927 struct omap_iommu_domain *omap_domain = domain->priv;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200928
Suman Anna2088ecb2014-10-22 17:22:19 -0500929 if (!omap_domain->iommu_dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200930 return IRQ_NONE;
931
David Cohend594f1f2011-02-16 19:35:51 +0000932 errs = iommu_report_fault(obj, &da);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200933 if (errs == 0)
934 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000935
936 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400937 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200938 return IRQ_HANDLED;
939
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000940 iommu_disable(obj);
941
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200942 iopgd = iopgd_offset(obj, da);
943
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300944 if (!iopgd_is_table(*iopgd)) {
Suman Annab6c2e092013-05-30 18:10:59 -0500945 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
946 obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200947 return IRQ_NONE;
948 }
949
950 iopte = iopte_offset(iopgd, da);
951
Suman Annab6c2e092013-05-30 18:10:59 -0500952 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
953 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200954
955 return IRQ_NONE;
956}
957
958static int device_match_by_alias(struct device *dev, void *data)
959{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300960 struct omap_iommu *obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200961 const char *name = data;
962
963 pr_debug("%s: %s %s\n", __func__, obj->name, name);
964
965 return strcmp(obj->name, name) == 0;
966}
967
968/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300969 * omap_iommu_attach() - attach iommu device to an iommu domain
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200970 * @name: name of target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300971 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200972 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200973static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200974{
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600975 int err;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200976 struct device *dev;
977 struct omap_iommu *obj;
978
979 dev = driver_find_device(&omap_iommu_driver.driver, NULL,
980 (void *)name,
981 device_match_by_alias);
982 if (!dev)
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600983 return ERR_PTR(-ENODEV);
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200984
985 obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200986
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300987 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200988
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300989 obj->iopgd = iopgd;
990 err = iommu_enable(obj);
991 if (err)
992 goto err_enable;
993 flush_iotlb_all(obj);
994
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300995 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200996
997 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
998 return obj;
999
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001000err_enable:
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001001 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001002 return ERR_PTR(err);
1003}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001004
1005/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001006 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001007 * @obj: target iommu
1008 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001009static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001010{
Roel Kluinacf9d462010-01-08 10:29:05 -08001011 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001012 return;
1013
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001014 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001015
Suman Anna2088ecb2014-10-22 17:22:19 -05001016 iommu_disable(obj);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001017 obj->iopgd = NULL;
1018
1019 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001020
1021 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
1022}
David Cohend594f1f2011-02-16 19:35:51 +00001023
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001024/*
1025 * OMAP Device MMU(IOMMU) detection
1026 */
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001027static int omap_iommu_probe(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001028{
1029 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001030 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001031 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001032 struct resource *res;
1033 struct iommu_platform_data *pdata = pdev->dev.platform_data;
Florian Vaussard3c927482014-02-28 14:42:36 -06001034 struct device_node *of = pdev->dev.of_node;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001035
Suman Annaf129b3d2014-02-28 14:42:32 -06001036 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001037 if (!obj)
1038 return -ENOMEM;
1039
Florian Vaussard3c927482014-02-28 14:42:36 -06001040 if (of) {
1041 obj->name = dev_name(&pdev->dev);
1042 obj->nr_tlb_entries = 32;
1043 err = of_property_read_u32(of, "ti,#tlb-entries",
1044 &obj->nr_tlb_entries);
1045 if (err && err != -EINVAL)
1046 return err;
1047 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
1048 return -EINVAL;
Suman Annab148d5f2014-02-28 14:42:37 -06001049 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
1050 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
Florian Vaussard3c927482014-02-28 14:42:36 -06001051 } else {
1052 obj->nr_tlb_entries = pdata->nr_tlb_entries;
1053 obj->name = pdata->name;
Florian Vaussard3c927482014-02-28 14:42:36 -06001054 }
Florian Vaussard3c927482014-02-28 14:42:36 -06001055
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001056 obj->dev = &pdev->dev;
1057 obj->ctx = (void *)obj + sizeof(*obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001058
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001059 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001060 spin_lock_init(&obj->page_table_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001061
1062 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001063 obj->regbase = devm_ioremap_resource(obj->dev, res);
1064 if (IS_ERR(obj->regbase))
1065 return PTR_ERR(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +00001066
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001067 irq = platform_get_irq(pdev, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001068 if (irq < 0)
1069 return -ENODEV;
1070
1071 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
1072 dev_name(obj->dev), obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001073 if (err < 0)
Suman Annaf129b3d2014-02-28 14:42:32 -06001074 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001075 platform_set_drvdata(pdev, obj);
1076
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001077 pm_runtime_irq_safe(obj->dev);
1078 pm_runtime_enable(obj->dev);
1079
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001080 dev_info(&pdev->dev, "%s registered\n", obj->name);
1081 return 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001082}
1083
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001084static int omap_iommu_remove(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001085{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001086 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001087
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001088 iopgtable_clear_entry_all(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001089
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001090 pm_runtime_disable(obj->dev);
1091
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001092 dev_info(&pdev->dev, "%s removed\n", obj->name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001093 return 0;
1094}
1095
Kiran Padwald943b0f2014-09-11 19:07:36 +05301096static const struct of_device_id omap_iommu_of_match[] = {
Florian Vaussard3c927482014-02-28 14:42:36 -06001097 { .compatible = "ti,omap2-iommu" },
1098 { .compatible = "ti,omap4-iommu" },
1099 { .compatible = "ti,dra7-iommu" },
1100 {},
1101};
1102MODULE_DEVICE_TABLE(of, omap_iommu_of_match);
1103
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001104static struct platform_driver omap_iommu_driver = {
1105 .probe = omap_iommu_probe,
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001106 .remove = omap_iommu_remove,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001107 .driver = {
1108 .name = "omap-iommu",
Florian Vaussard3c927482014-02-28 14:42:36 -06001109 .of_match_table = of_match_ptr(omap_iommu_of_match),
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001110 },
1111};
1112
1113static void iopte_cachep_ctor(void *iopte)
1114{
1115 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1116}
1117
Laurent Pinchart286f6002014-03-08 00:44:38 +01001118static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001119{
1120 memset(e, 0, sizeof(*e));
1121
1122 e->da = da;
1123 e->pa = pa;
Suman Annad760e3e2014-03-17 20:31:32 -05001124 e->valid = MMU_CAM_V;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001125 e->pgsz = pgsz;
1126 e->endian = MMU_RAM_ENDIAN_LITTLE;
1127 e->elsz = MMU_RAM_ELSZ_8;
1128 e->mixed = 0;
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001129
1130 return iopgsz_to_bytes(e->pgsz);
1131}
1132
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001133static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001134 phys_addr_t pa, size_t bytes, int prot)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001135{
1136 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001137 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001138 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001139 struct iotlb_entry e;
1140 int omap_pgsz;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001141 u32 ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001142
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001143 omap_pgsz = bytes_to_iopgsz(bytes);
1144 if (omap_pgsz < 0) {
1145 dev_err(dev, "invalid size to map: %d\n", bytes);
1146 return -EINVAL;
1147 }
1148
1149 dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes);
1150
Laurent Pinchart286f6002014-03-08 00:44:38 +01001151 iotlb_init_entry(&e, da, pa, omap_pgsz);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001152
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001153 ret = omap_iopgtable_store_entry(oiommu, &e);
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001154 if (ret)
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001155 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001156
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001157 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001158}
1159
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001160static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1161 size_t size)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001162{
1163 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001164 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001165 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001166
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001167 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001168
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001169 return iopgtable_clear_entry(oiommu, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001170}
1171
1172static int
1173omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1174{
1175 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001176 struct omap_iommu *oiommu;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001177 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001178 int ret = 0;
1179
Suman Annae3f595b2014-09-04 17:27:29 -05001180 if (!arch_data || !arch_data->name) {
1181 dev_err(dev, "device doesn't have an associated iommu\n");
1182 return -EINVAL;
1183 }
1184
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001185 spin_lock(&omap_domain->lock);
1186
1187 /* only a single device is supported per domain for now */
1188 if (omap_domain->iommu_dev) {
1189 dev_err(dev, "iommu domain is already attached\n");
1190 ret = -EBUSY;
1191 goto out;
1192 }
1193
1194 /* get a handle to and enable the omap iommu */
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001195 oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001196 if (IS_ERR(oiommu)) {
1197 ret = PTR_ERR(oiommu);
1198 dev_err(dev, "can't get omap iommu: %d\n", ret);
1199 goto out;
1200 }
1201
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001202 omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001203 omap_domain->dev = dev;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -04001204 oiommu->domain = domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001205
1206out:
1207 spin_unlock(&omap_domain->lock);
1208 return ret;
1209}
1210
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001211static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
1212 struct device *dev)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001213{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001214 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001215 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001216
1217 /* only a single device is supported per domain for now */
1218 if (omap_domain->iommu_dev != oiommu) {
1219 dev_err(dev, "invalid iommu device\n");
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001220 return;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001221 }
1222
1223 iopgtable_clear_entry_all(oiommu);
1224
1225 omap_iommu_detach(oiommu);
1226
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001227 omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001228 omap_domain->dev = NULL;
1229}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001230
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001231static void omap_iommu_detach_dev(struct iommu_domain *domain,
1232 struct device *dev)
1233{
1234 struct omap_iommu_domain *omap_domain = domain->priv;
1235
1236 spin_lock(&omap_domain->lock);
1237 _omap_iommu_detach_dev(omap_domain, dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001238 spin_unlock(&omap_domain->lock);
1239}
1240
1241static int omap_iommu_domain_init(struct iommu_domain *domain)
1242{
1243 struct omap_iommu_domain *omap_domain;
1244
1245 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1246 if (!omap_domain) {
1247 pr_err("kzalloc failed\n");
1248 goto out;
1249 }
1250
1251 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1252 if (!omap_domain->pgtable) {
1253 pr_err("kzalloc failed\n");
1254 goto fail_nomem;
1255 }
1256
1257 /*
1258 * should never fail, but please keep this around to ensure
1259 * we keep the hardware happy
1260 */
1261 BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
1262
1263 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1264 spin_lock_init(&omap_domain->lock);
1265
1266 domain->priv = omap_domain;
1267
Joerg Roedel2c6edb02012-01-26 19:40:55 +01001268 domain->geometry.aperture_start = 0;
1269 domain->geometry.aperture_end = (1ULL << 32) - 1;
1270 domain->geometry.force_aperture = true;
1271
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001272 return 0;
1273
1274fail_nomem:
1275 kfree(omap_domain);
1276out:
1277 return -ENOMEM;
1278}
1279
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001280static void omap_iommu_domain_destroy(struct iommu_domain *domain)
1281{
1282 struct omap_iommu_domain *omap_domain = domain->priv;
1283
1284 domain->priv = NULL;
1285
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001286 /*
1287 * An iommu device is still attached
1288 * (currently, only one device can be attached) ?
1289 */
1290 if (omap_domain->iommu_dev)
1291 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1292
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001293 kfree(omap_domain->pgtable);
1294 kfree(omap_domain);
1295}
1296
1297static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547ac2013-03-29 01:23:58 +05301298 dma_addr_t da)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001299{
1300 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001301 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001302 struct device *dev = oiommu->dev;
1303 u32 *pgd, *pte;
1304 phys_addr_t ret = 0;
1305
1306 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1307
1308 if (pte) {
1309 if (iopte_is_small(*pte))
1310 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1311 else if (iopte_is_large(*pte))
1312 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1313 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001314 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
1315 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001316 } else {
1317 if (iopgd_is_section(*pgd))
1318 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1319 else if (iopgd_is_super(*pgd))
1320 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1321 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001322 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
1323 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001324 }
1325
1326 return ret;
1327}
1328
Laurent Pinchart07a02032014-02-28 14:42:38 -06001329static int omap_iommu_add_device(struct device *dev)
1330{
1331 struct omap_iommu_arch_data *arch_data;
1332 struct device_node *np;
Suman Anna7d682772014-09-04 17:27:30 -05001333 struct platform_device *pdev;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001334
1335 /*
1336 * Allocate the archdata iommu structure for DT-based devices.
1337 *
1338 * TODO: Simplify this when removing non-DT support completely from the
1339 * IOMMU users.
1340 */
1341 if (!dev->of_node)
1342 return 0;
1343
1344 np = of_parse_phandle(dev->of_node, "iommus", 0);
1345 if (!np)
1346 return 0;
1347
Suman Anna7d682772014-09-04 17:27:30 -05001348 pdev = of_find_device_by_node(np);
1349 if (WARN_ON(!pdev)) {
1350 of_node_put(np);
1351 return -EINVAL;
1352 }
1353
Laurent Pinchart07a02032014-02-28 14:42:38 -06001354 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
1355 if (!arch_data) {
1356 of_node_put(np);
1357 return -ENOMEM;
1358 }
1359
Suman Anna7d682772014-09-04 17:27:30 -05001360 arch_data->name = kstrdup(dev_name(&pdev->dev), GFP_KERNEL);
Laurent Pinchart07a02032014-02-28 14:42:38 -06001361 dev->archdata.iommu = arch_data;
1362
1363 of_node_put(np);
1364
1365 return 0;
1366}
1367
1368static void omap_iommu_remove_device(struct device *dev)
1369{
1370 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1371
1372 if (!dev->of_node || !arch_data)
1373 return;
1374
1375 kfree(arch_data->name);
1376 kfree(arch_data);
1377}
1378
Thierry Redingb22f6432014-06-27 09:03:12 +02001379static const struct iommu_ops omap_iommu_ops = {
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001380 .domain_init = omap_iommu_domain_init,
1381 .domain_destroy = omap_iommu_domain_destroy,
1382 .attach_dev = omap_iommu_attach_dev,
1383 .detach_dev = omap_iommu_detach_dev,
1384 .map = omap_iommu_map,
1385 .unmap = omap_iommu_unmap,
1386 .iova_to_phys = omap_iommu_iova_to_phys,
Laurent Pinchart07a02032014-02-28 14:42:38 -06001387 .add_device = omap_iommu_add_device,
1388 .remove_device = omap_iommu_remove_device,
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +02001389 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001390};
1391
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001392static int __init omap_iommu_init(void)
1393{
1394 struct kmem_cache *p;
1395 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1396 size_t align = 1 << 10; /* L2 pagetable alignement */
1397
1398 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1399 iopte_cachep_ctor);
1400 if (!p)
1401 return -ENOMEM;
1402 iopte_cachep = p;
1403
Joerg Roedela65bc642011-09-06 17:56:07 +02001404 bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001405
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001406 return platform_driver_register(&omap_iommu_driver);
1407}
Ohad Ben-Cohen435792d2012-02-26 12:14:14 +02001408/* must be ready before omap3isp is probed */
1409subsys_initcall(omap_iommu_init);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001410
1411static void __exit omap_iommu_exit(void)
1412{
1413 kmem_cache_destroy(iopte_cachep);
1414
1415 platform_driver_unregister(&omap_iommu_driver);
1416}
1417module_exit(omap_iommu_exit);
1418
1419MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1420MODULE_ALIAS("platform:omap-iommu");
1421MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1422MODULE_LICENSE("GPL v2");