blob: 91711126cb25a8f3837dbb77cfbaf41c0b9855ea [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 */
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200206static inline int iotlb_cr_valid(struct cr_regs *cr)
207{
208 if (!cr)
209 return -EINVAL;
210
Suman Annabd4396f2014-10-22 17:22:27 -0500211 return cr->cam & MMU_CAM_V;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200212}
213
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300214static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200215{
Suman Annabd4396f2014-10-22 17:22:27 -0500216 u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
217 u32 mask = get_cam_va_mask(cr->cam & page_size);
218
219 return cr->cam & mask;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200220}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200221
222static u32 get_iopte_attr(struct iotlb_entry *e)
223{
Suman Annabd4396f2014-10-22 17:22:27 -0500224 u32 attr;
225
226 attr = e->mixed << 5;
227 attr |= e->endian;
228 attr |= e->elsz >> 3;
229 attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
230 (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
231 return attr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200232}
233
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300234static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200235{
Suman Annabd4396f2014-10-22 17:22:27 -0500236 u32 status, fault_addr;
237
238 status = iommu_read_reg(obj, MMU_IRQSTATUS);
239 status &= MMU_IRQ_MASK;
240 if (!status) {
241 *da = 0;
242 return 0;
243 }
244
245 fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
246 *da = fault_addr;
247
248 iommu_write_reg(obj, status, MMU_IRQSTATUS);
249
250 return status;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200251}
252
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300253static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200254{
255 u32 val;
256
257 val = iommu_read_reg(obj, MMU_LOCK);
258
259 l->base = MMU_LOCK_BASE(val);
260 l->vict = MMU_LOCK_VICT(val);
261
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200262}
263
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300264static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200265{
266 u32 val;
267
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200268 val = (l->base << MMU_LOCK_BASE_SHIFT);
269 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
270
271 iommu_write_reg(obj, val, MMU_LOCK);
272}
273
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300274static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200275{
Suman Annabd4396f2014-10-22 17:22:27 -0500276 cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
277 cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200278}
279
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300280static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200281{
Suman Annabd4396f2014-10-22 17:22:27 -0500282 iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
283 iommu_write_reg(obj, cr->ram, MMU_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200284
285 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
286 iommu_write_reg(obj, 1, MMU_LD_TLB);
287}
288
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000289/* only used in iotlb iteration for-loop */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300290static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000291{
292 struct cr_regs cr;
293 struct iotlb_lock l;
294
295 iotlb_lock_get(obj, &l);
296 l.vict = n;
297 iotlb_lock_set(obj, &l);
298 iotlb_read_cr(obj, &cr);
299
300 return cr;
301}
302
Suman Annabd4396f2014-10-22 17:22:27 -0500303#ifdef PREFETCH_IOTLB
304static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
305 struct iotlb_entry *e)
306{
307 struct cr_regs *cr;
308
309 if (!e)
310 return NULL;
311
312 if (e->da & ~(get_cam_va_mask(e->pgsz))) {
313 dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
314 e->da);
315 return ERR_PTR(-EINVAL);
316 }
317
318 cr = kmalloc(sizeof(*cr), GFP_KERNEL);
319 if (!cr)
320 return ERR_PTR(-ENOMEM);
321
322 cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
323 cr->ram = e->pa | e->endian | e->elsz | e->mixed;
324
325 return cr;
326}
327
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200328/**
329 * load_iotlb_entry - Set an iommu tlb entry
330 * @obj: target iommu
331 * @e: an iommu tlb entry info
332 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300333static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200334{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200335 int err = 0;
336 struct iotlb_lock l;
337 struct cr_regs *cr;
338
339 if (!obj || !obj->nr_tlb_entries || !e)
340 return -EINVAL;
341
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600342 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200343
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000344 iotlb_lock_get(obj, &l);
345 if (l.base == obj->nr_tlb_entries) {
346 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200347 err = -EBUSY;
348 goto out;
349 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000350 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000351 int i;
352 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000353
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000354 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000355 if (!iotlb_cr_valid(&tmp))
356 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000357
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000358 if (i == obj->nr_tlb_entries) {
359 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
360 err = -EBUSY;
361 goto out;
362 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000363
364 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000365 } else {
366 l.vict = l.base;
367 iotlb_lock_set(obj, &l);
368 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200369
370 cr = iotlb_alloc_cr(obj, e);
371 if (IS_ERR(cr)) {
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600372 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200373 return PTR_ERR(cr);
374 }
375
376 iotlb_load_cr(obj, cr);
377 kfree(cr);
378
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000379 if (e->prsvd)
380 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200381 /* increment victim for next tlb load */
382 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000383 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200384 iotlb_lock_set(obj, &l);
385out:
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600386 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200387 return err;
388}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200389
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300390#else /* !PREFETCH_IOTLB */
391
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300392static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300393{
394 return 0;
395}
396
397#endif /* !PREFETCH_IOTLB */
398
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300399static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300400{
401 return load_iotlb_entry(obj, e);
402}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200403
404/**
405 * flush_iotlb_page - Clear an iommu tlb entry
406 * @obj: target iommu
407 * @da: iommu device virtual address
408 *
409 * Clear an iommu tlb entry which includes 'da' address.
410 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300411static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200412{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200413 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000414 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200415
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600416 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200417
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000418 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200419 u32 start;
420 size_t bytes;
421
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200422 if (!iotlb_cr_valid(&cr))
423 continue;
424
425 start = iotlb_cr_to_virt(&cr);
426 bytes = iopgsz_to_bytes(cr.cam & 3);
427
428 if ((start <= da) && (da < start + bytes)) {
429 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
430 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000431 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200432 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
Laurent Pinchartf7129a02014-03-07 23:47:03 +0100433 break;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200434 }
435 }
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600436 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200437
438 if (i == obj->nr_tlb_entries)
439 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
440}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200441
442/**
443 * flush_iotlb_all - Clear all iommu tlb entries
444 * @obj: target iommu
445 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300446static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200447{
448 struct iotlb_lock l;
449
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600450 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200451
452 l.base = 0;
453 l.vict = 0;
454 iotlb_lock_set(obj, &l);
455
456 iommu_write_reg(obj, 1, MMU_GFLUSH);
457
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600458 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200459}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200460
Suman Anna61c75352014-10-22 17:22:30 -0500461#ifdef CONFIG_OMAP_IOMMU_DEBUG
Kanigeri, Hariddfa9752010-05-24 02:01:51 +0000462
Suman Annabd4396f2014-10-22 17:22:27 -0500463#define pr_reg(name) \
464 do { \
465 ssize_t bytes; \
466 const char *str = "%20s: %08x\n"; \
467 const int maxcol = 32; \
468 bytes = snprintf(p, maxcol, str, __stringify(name), \
469 iommu_read_reg(obj, MMU_##name)); \
470 p += bytes; \
471 len -= bytes; \
472 if (len < maxcol) \
473 goto out; \
474 } while (0)
475
476static ssize_t
477omap2_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len)
478{
479 char *p = buf;
480
481 pr_reg(REVISION);
482 pr_reg(IRQSTATUS);
483 pr_reg(IRQENABLE);
484 pr_reg(WALKING_ST);
485 pr_reg(CNTL);
486 pr_reg(FAULT_AD);
487 pr_reg(TTB);
488 pr_reg(LOCK);
489 pr_reg(LD_TLB);
490 pr_reg(CAM);
491 pr_reg(RAM);
492 pr_reg(GFLUSH);
493 pr_reg(FLUSH_ENTRY);
494 pr_reg(READ_CAM);
495 pr_reg(READ_RAM);
496 pr_reg(EMU_FAULT_AD);
497out:
498 return p - buf;
499}
500
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300501ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200502{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200503 if (!obj || !buf)
504 return -EINVAL;
505
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600506 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200507
Suman Annabd4396f2014-10-22 17:22:27 -0500508 bytes = omap2_iommu_dump_ctx(obj, buf, bytes);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200509
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600510 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200511
512 return bytes;
513}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300514EXPORT_SYMBOL_GPL(omap_iommu_dump_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200515
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300516static int
517__dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200518{
519 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000520 struct iotlb_lock saved;
521 struct cr_regs tmp;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200522 struct cr_regs *p = crs;
523
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600524 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200525 iotlb_lock_get(obj, &saved);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200526
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000527 for_each_iotlb_cr(obj, num, i, tmp) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200528 if (!iotlb_cr_valid(&tmp))
529 continue;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200530 *p++ = tmp;
531 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000532
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200533 iotlb_lock_set(obj, &saved);
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600534 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200535
536 return p - crs;
537}
538
539/**
Suman Annabd4396f2014-10-22 17:22:27 -0500540 * iotlb_dump_cr - Dump an iommu tlb entry into buf
541 * @obj: target iommu
542 * @cr: contents of cam and ram register
543 * @buf: output buffer
544 **/
545static ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
546 char *buf)
547{
548 char *p = buf;
549
550 /* FIXME: Need more detail analysis of cam/ram */
551 p += sprintf(p, "%08x %08x %01x\n", cr->cam, cr->ram,
552 (cr->cam & MMU_CAM_P) ? 1 : 0);
553
554 return p - buf;
555}
556
557/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300558 * omap_dump_tlb_entries - dump cr arrays to given buffer
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200559 * @obj: target iommu
560 * @buf: output buffer
561 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300562size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200563{
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700564 int i, num;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200565 struct cr_regs *cr;
566 char *p = buf;
567
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700568 num = bytes / sizeof(*cr);
569 num = min(obj->nr_tlb_entries, num);
570
571 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200572 if (!cr)
573 return 0;
574
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700575 num = __dump_tlb_entries(obj, cr, num);
576 for (i = 0; i < num; i++)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200577 p += iotlb_dump_cr(obj, cr + i, p);
578 kfree(cr);
579
580 return p - buf;
581}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300582EXPORT_SYMBOL_GPL(omap_dump_tlb_entries);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200583
Suman Anna61c75352014-10-22 17:22:30 -0500584#endif /* CONFIG_OMAP_IOMMU_DEBUG */
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200585
586/*
587 * H/W pagetable operations
588 */
589static void flush_iopgd_range(u32 *first, u32 *last)
590{
591 /* FIXME: L2 cache should be taken care of if it exists */
592 do {
593 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
594 : : "r" (first));
595 first += L1_CACHE_BYTES / sizeof(*first);
596 } while (first <= last);
597}
598
599static void flush_iopte_range(u32 *first, u32 *last)
600{
601 /* FIXME: L2 cache should be taken care of if it exists */
602 do {
603 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
604 : : "r" (first));
605 first += L1_CACHE_BYTES / sizeof(*first);
606 } while (first <= last);
607}
608
609static void iopte_free(u32 *iopte)
610{
611 /* Note: freed iopte's must be clean ready for re-use */
Zhouyi Zhoue28045a2014-03-05 18:20:19 +0800612 if (iopte)
613 kmem_cache_free(iopte_cachep, iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200614}
615
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300616static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200617{
618 u32 *iopte;
619
620 /* a table has already existed */
621 if (*iopgd)
622 goto pte_ready;
623
624 /*
625 * do the allocation outside the page table lock
626 */
627 spin_unlock(&obj->page_table_lock);
628 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
629 spin_lock(&obj->page_table_lock);
630
631 if (!*iopgd) {
632 if (!iopte)
633 return ERR_PTR(-ENOMEM);
634
635 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
636 flush_iopgd_range(iopgd, iopgd);
637
638 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
639 } else {
640 /* We raced, free the reduniovant table */
641 iopte_free(iopte);
642 }
643
644pte_ready:
645 iopte = iopte_offset(iopgd, da);
646
647 dev_vdbg(obj->dev,
648 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
649 __func__, da, iopgd, *iopgd, iopte, *iopte);
650
651 return iopte;
652}
653
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300654static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200655{
656 u32 *iopgd = iopgd_offset(obj, da);
657
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300658 if ((da | pa) & ~IOSECTION_MASK) {
659 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
660 __func__, da, pa, IOSECTION_SIZE);
661 return -EINVAL;
662 }
663
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200664 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
665 flush_iopgd_range(iopgd, iopgd);
666 return 0;
667}
668
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300669static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200670{
671 u32 *iopgd = iopgd_offset(obj, da);
672 int i;
673
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300674 if ((da | pa) & ~IOSUPER_MASK) {
675 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
676 __func__, da, pa, IOSUPER_SIZE);
677 return -EINVAL;
678 }
679
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200680 for (i = 0; i < 16; i++)
681 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
682 flush_iopgd_range(iopgd, iopgd + 15);
683 return 0;
684}
685
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300686static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200687{
688 u32 *iopgd = iopgd_offset(obj, da);
689 u32 *iopte = iopte_alloc(obj, iopgd, da);
690
691 if (IS_ERR(iopte))
692 return PTR_ERR(iopte);
693
694 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
695 flush_iopte_range(iopte, iopte);
696
697 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
698 __func__, da, pa, iopte, *iopte);
699
700 return 0;
701}
702
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300703static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200704{
705 u32 *iopgd = iopgd_offset(obj, da);
706 u32 *iopte = iopte_alloc(obj, iopgd, da);
707 int i;
708
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300709 if ((da | pa) & ~IOLARGE_MASK) {
710 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
711 __func__, da, pa, IOLARGE_SIZE);
712 return -EINVAL;
713 }
714
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200715 if (IS_ERR(iopte))
716 return PTR_ERR(iopte);
717
718 for (i = 0; i < 16; i++)
719 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
720 flush_iopte_range(iopte, iopte + 15);
721 return 0;
722}
723
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300724static int
725iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200726{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300727 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200728 u32 prot;
729 int err;
730
731 if (!obj || !e)
732 return -EINVAL;
733
734 switch (e->pgsz) {
735 case MMU_CAM_PGSZ_16M:
736 fn = iopgd_alloc_super;
737 break;
738 case MMU_CAM_PGSZ_1M:
739 fn = iopgd_alloc_section;
740 break;
741 case MMU_CAM_PGSZ_64K:
742 fn = iopte_alloc_large;
743 break;
744 case MMU_CAM_PGSZ_4K:
745 fn = iopte_alloc_page;
746 break;
747 default:
748 fn = NULL;
749 BUG();
750 break;
751 }
752
753 prot = get_iopte_attr(e);
754
755 spin_lock(&obj->page_table_lock);
756 err = fn(obj, e->da, e->pa, prot);
757 spin_unlock(&obj->page_table_lock);
758
759 return err;
760}
761
762/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300763 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200764 * @obj: target iommu
765 * @e: an iommu tlb entry info
766 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300767int omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200768{
769 int err;
770
771 flush_iotlb_page(obj, e->da);
772 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200773 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300774 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200775 return err;
776}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300777EXPORT_SYMBOL_GPL(omap_iopgtable_store_entry);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200778
779/**
780 * iopgtable_lookup_entry - Lookup an iommu pte entry
781 * @obj: target iommu
782 * @da: iommu device virtual address
783 * @ppgd: iommu pgd entry pointer to be returned
784 * @ppte: iommu pte entry pointer to be returned
785 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300786static void
787iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200788{
789 u32 *iopgd, *iopte = NULL;
790
791 iopgd = iopgd_offset(obj, da);
792 if (!*iopgd)
793 goto out;
794
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300795 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200796 iopte = iopte_offset(iopgd, da);
797out:
798 *ppgd = iopgd;
799 *ppte = iopte;
800}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200801
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300802static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200803{
804 size_t bytes;
805 u32 *iopgd = iopgd_offset(obj, da);
806 int nent = 1;
807
808 if (!*iopgd)
809 return 0;
810
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300811 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200812 int i;
813 u32 *iopte = iopte_offset(iopgd, da);
814
815 bytes = IOPTE_SIZE;
816 if (*iopte & IOPTE_LARGE) {
817 nent *= 16;
818 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800819 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200820 }
821 bytes *= nent;
822 memset(iopte, 0, nent * sizeof(*iopte));
823 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
824
825 /*
826 * do table walk to check if this table is necessary or not
827 */
828 iopte = iopte_offset(iopgd, 0);
829 for (i = 0; i < PTRS_PER_IOPTE; i++)
830 if (iopte[i])
831 goto out;
832
833 iopte_free(iopte);
834 nent = 1; /* for the next L1 entry */
835 } else {
836 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700837 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200838 nent *= 16;
839 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800840 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200841 }
842 bytes *= nent;
843 }
844 memset(iopgd, 0, nent * sizeof(*iopgd));
845 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
846out:
847 return bytes;
848}
849
850/**
851 * iopgtable_clear_entry - Remove an iommu pte entry
852 * @obj: target iommu
853 * @da: iommu device virtual address
854 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300855static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200856{
857 size_t bytes;
858
859 spin_lock(&obj->page_table_lock);
860
861 bytes = iopgtable_clear_entry_core(obj, da);
862 flush_iotlb_page(obj, da);
863
864 spin_unlock(&obj->page_table_lock);
865
866 return bytes;
867}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200868
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300869static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200870{
871 int i;
872
873 spin_lock(&obj->page_table_lock);
874
875 for (i = 0; i < PTRS_PER_IOPGD; i++) {
876 u32 da;
877 u32 *iopgd;
878
879 da = i << IOPGD_SHIFT;
880 iopgd = iopgd_offset(obj, da);
881
882 if (!*iopgd)
883 continue;
884
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300885 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200886 iopte_free(iopte_offset(iopgd, 0));
887
888 *iopgd = 0;
889 flush_iopgd_range(iopgd, iopgd);
890 }
891
892 flush_iotlb_all(obj);
893
894 spin_unlock(&obj->page_table_lock);
895}
896
897/*
898 * Device IOMMU generic operations
899 */
900static irqreturn_t iommu_fault_handler(int irq, void *data)
901{
David Cohend594f1f2011-02-16 19:35:51 +0000902 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200903 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300904 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400905 struct iommu_domain *domain = obj->domain;
Suman Anna2088ecb2014-10-22 17:22:19 -0500906 struct omap_iommu_domain *omap_domain = domain->priv;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200907
Suman Anna2088ecb2014-10-22 17:22:19 -0500908 if (!omap_domain->iommu_dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200909 return IRQ_NONE;
910
David Cohend594f1f2011-02-16 19:35:51 +0000911 errs = iommu_report_fault(obj, &da);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200912 if (errs == 0)
913 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000914
915 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400916 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200917 return IRQ_HANDLED;
918
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000919 iommu_disable(obj);
920
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200921 iopgd = iopgd_offset(obj, da);
922
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300923 if (!iopgd_is_table(*iopgd)) {
Suman Annab6c2e092013-05-30 18:10:59 -0500924 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
925 obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200926 return IRQ_NONE;
927 }
928
929 iopte = iopte_offset(iopgd, da);
930
Suman Annab6c2e092013-05-30 18:10:59 -0500931 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
932 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200933
934 return IRQ_NONE;
935}
936
937static int device_match_by_alias(struct device *dev, void *data)
938{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300939 struct omap_iommu *obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200940 const char *name = data;
941
942 pr_debug("%s: %s %s\n", __func__, obj->name, name);
943
944 return strcmp(obj->name, name) == 0;
945}
946
947/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300948 * omap_iommu_attach() - attach iommu device to an iommu domain
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200949 * @name: name of target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300950 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200951 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200952static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200953{
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600954 int err;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200955 struct device *dev;
956 struct omap_iommu *obj;
957
958 dev = driver_find_device(&omap_iommu_driver.driver, NULL,
959 (void *)name,
960 device_match_by_alias);
961 if (!dev)
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600962 return ERR_PTR(-ENODEV);
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200963
964 obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200965
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300966 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200967
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300968 obj->iopgd = iopgd;
969 err = iommu_enable(obj);
970 if (err)
971 goto err_enable;
972 flush_iotlb_all(obj);
973
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300974 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200975
976 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
977 return obj;
978
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200979err_enable:
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300980 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200981 return ERR_PTR(err);
982}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200983
984/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300985 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200986 * @obj: target iommu
987 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300988static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200989{
Roel Kluinacf9d462010-01-08 10:29:05 -0800990 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200991 return;
992
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300993 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200994
Suman Anna2088ecb2014-10-22 17:22:19 -0500995 iommu_disable(obj);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300996 obj->iopgd = NULL;
997
998 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200999
1000 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
1001}
David Cohend594f1f2011-02-16 19:35:51 +00001002
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001003/*
1004 * OMAP Device MMU(IOMMU) detection
1005 */
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001006static int omap_iommu_probe(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001007{
1008 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001009 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001010 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001011 struct resource *res;
1012 struct iommu_platform_data *pdata = pdev->dev.platform_data;
Florian Vaussard3c927482014-02-28 14:42:36 -06001013 struct device_node *of = pdev->dev.of_node;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001014
Suman Annaf129b3d2014-02-28 14:42:32 -06001015 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001016 if (!obj)
1017 return -ENOMEM;
1018
Florian Vaussard3c927482014-02-28 14:42:36 -06001019 if (of) {
1020 obj->name = dev_name(&pdev->dev);
1021 obj->nr_tlb_entries = 32;
1022 err = of_property_read_u32(of, "ti,#tlb-entries",
1023 &obj->nr_tlb_entries);
1024 if (err && err != -EINVAL)
1025 return err;
1026 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
1027 return -EINVAL;
Suman Annab148d5f2014-02-28 14:42:37 -06001028 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
1029 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
Florian Vaussard3c927482014-02-28 14:42:36 -06001030 } else {
1031 obj->nr_tlb_entries = pdata->nr_tlb_entries;
1032 obj->name = pdata->name;
Florian Vaussard3c927482014-02-28 14:42:36 -06001033 }
Florian Vaussard3c927482014-02-28 14:42:36 -06001034
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001035 obj->dev = &pdev->dev;
1036 obj->ctx = (void *)obj + sizeof(*obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001037
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001038 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001039 spin_lock_init(&obj->page_table_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001040
1041 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001042 obj->regbase = devm_ioremap_resource(obj->dev, res);
1043 if (IS_ERR(obj->regbase))
1044 return PTR_ERR(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +00001045
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001046 irq = platform_get_irq(pdev, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001047 if (irq < 0)
1048 return -ENODEV;
1049
1050 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
1051 dev_name(obj->dev), obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001052 if (err < 0)
Suman Annaf129b3d2014-02-28 14:42:32 -06001053 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001054 platform_set_drvdata(pdev, obj);
1055
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001056 pm_runtime_irq_safe(obj->dev);
1057 pm_runtime_enable(obj->dev);
1058
Suman Anna61c75352014-10-22 17:22:30 -05001059 omap_iommu_debugfs_add(obj);
1060
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001061 dev_info(&pdev->dev, "%s registered\n", obj->name);
1062 return 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001063}
1064
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001065static int omap_iommu_remove(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001066{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001067 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001068
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001069 iopgtable_clear_entry_all(obj);
Suman Anna61c75352014-10-22 17:22:30 -05001070 omap_iommu_debugfs_remove(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001071
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001072 pm_runtime_disable(obj->dev);
1073
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001074 dev_info(&pdev->dev, "%s removed\n", obj->name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001075 return 0;
1076}
1077
Kiran Padwald943b0f2014-09-11 19:07:36 +05301078static const struct of_device_id omap_iommu_of_match[] = {
Florian Vaussard3c927482014-02-28 14:42:36 -06001079 { .compatible = "ti,omap2-iommu" },
1080 { .compatible = "ti,omap4-iommu" },
1081 { .compatible = "ti,dra7-iommu" },
1082 {},
1083};
1084MODULE_DEVICE_TABLE(of, omap_iommu_of_match);
1085
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001086static struct platform_driver omap_iommu_driver = {
1087 .probe = omap_iommu_probe,
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001088 .remove = omap_iommu_remove,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001089 .driver = {
1090 .name = "omap-iommu",
Florian Vaussard3c927482014-02-28 14:42:36 -06001091 .of_match_table = of_match_ptr(omap_iommu_of_match),
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001092 },
1093};
1094
1095static void iopte_cachep_ctor(void *iopte)
1096{
1097 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1098}
1099
Laurent Pinchart286f6002014-03-08 00:44:38 +01001100static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001101{
1102 memset(e, 0, sizeof(*e));
1103
1104 e->da = da;
1105 e->pa = pa;
Suman Annad760e3e2014-03-17 20:31:32 -05001106 e->valid = MMU_CAM_V;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001107 e->pgsz = pgsz;
1108 e->endian = MMU_RAM_ENDIAN_LITTLE;
1109 e->elsz = MMU_RAM_ELSZ_8;
1110 e->mixed = 0;
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001111
1112 return iopgsz_to_bytes(e->pgsz);
1113}
1114
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001115static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001116 phys_addr_t pa, size_t bytes, int prot)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001117{
1118 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001119 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001120 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001121 struct iotlb_entry e;
1122 int omap_pgsz;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001123 u32 ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001124
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001125 omap_pgsz = bytes_to_iopgsz(bytes);
1126 if (omap_pgsz < 0) {
1127 dev_err(dev, "invalid size to map: %d\n", bytes);
1128 return -EINVAL;
1129 }
1130
1131 dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes);
1132
Laurent Pinchart286f6002014-03-08 00:44:38 +01001133 iotlb_init_entry(&e, da, pa, omap_pgsz);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001134
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001135 ret = omap_iopgtable_store_entry(oiommu, &e);
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001136 if (ret)
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001137 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001138
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001139 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001140}
1141
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001142static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1143 size_t size)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001144{
1145 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001146 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001147 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001148
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001149 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001150
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001151 return iopgtable_clear_entry(oiommu, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001152}
1153
1154static int
1155omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1156{
1157 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001158 struct omap_iommu *oiommu;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001159 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001160 int ret = 0;
1161
Suman Annae3f595b2014-09-04 17:27:29 -05001162 if (!arch_data || !arch_data->name) {
1163 dev_err(dev, "device doesn't have an associated iommu\n");
1164 return -EINVAL;
1165 }
1166
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001167 spin_lock(&omap_domain->lock);
1168
1169 /* only a single device is supported per domain for now */
1170 if (omap_domain->iommu_dev) {
1171 dev_err(dev, "iommu domain is already attached\n");
1172 ret = -EBUSY;
1173 goto out;
1174 }
1175
1176 /* get a handle to and enable the omap iommu */
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001177 oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001178 if (IS_ERR(oiommu)) {
1179 ret = PTR_ERR(oiommu);
1180 dev_err(dev, "can't get omap iommu: %d\n", ret);
1181 goto out;
1182 }
1183
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001184 omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001185 omap_domain->dev = dev;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -04001186 oiommu->domain = domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001187
1188out:
1189 spin_unlock(&omap_domain->lock);
1190 return ret;
1191}
1192
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001193static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
1194 struct device *dev)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001195{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001196 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001197 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001198
1199 /* only a single device is supported per domain for now */
1200 if (omap_domain->iommu_dev != oiommu) {
1201 dev_err(dev, "invalid iommu device\n");
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001202 return;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001203 }
1204
1205 iopgtable_clear_entry_all(oiommu);
1206
1207 omap_iommu_detach(oiommu);
1208
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001209 omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001210 omap_domain->dev = NULL;
1211}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001212
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001213static void omap_iommu_detach_dev(struct iommu_domain *domain,
1214 struct device *dev)
1215{
1216 struct omap_iommu_domain *omap_domain = domain->priv;
1217
1218 spin_lock(&omap_domain->lock);
1219 _omap_iommu_detach_dev(omap_domain, dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001220 spin_unlock(&omap_domain->lock);
1221}
1222
1223static int omap_iommu_domain_init(struct iommu_domain *domain)
1224{
1225 struct omap_iommu_domain *omap_domain;
1226
1227 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1228 if (!omap_domain) {
1229 pr_err("kzalloc failed\n");
1230 goto out;
1231 }
1232
1233 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1234 if (!omap_domain->pgtable) {
1235 pr_err("kzalloc failed\n");
1236 goto fail_nomem;
1237 }
1238
1239 /*
1240 * should never fail, but please keep this around to ensure
1241 * we keep the hardware happy
1242 */
1243 BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
1244
1245 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1246 spin_lock_init(&omap_domain->lock);
1247
1248 domain->priv = omap_domain;
1249
Joerg Roedel2c6edb02012-01-26 19:40:55 +01001250 domain->geometry.aperture_start = 0;
1251 domain->geometry.aperture_end = (1ULL << 32) - 1;
1252 domain->geometry.force_aperture = true;
1253
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001254 return 0;
1255
1256fail_nomem:
1257 kfree(omap_domain);
1258out:
1259 return -ENOMEM;
1260}
1261
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001262static void omap_iommu_domain_destroy(struct iommu_domain *domain)
1263{
1264 struct omap_iommu_domain *omap_domain = domain->priv;
1265
1266 domain->priv = NULL;
1267
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001268 /*
1269 * An iommu device is still attached
1270 * (currently, only one device can be attached) ?
1271 */
1272 if (omap_domain->iommu_dev)
1273 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1274
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001275 kfree(omap_domain->pgtable);
1276 kfree(omap_domain);
1277}
1278
1279static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547ac2013-03-29 01:23:58 +05301280 dma_addr_t da)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001281{
1282 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001283 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001284 struct device *dev = oiommu->dev;
1285 u32 *pgd, *pte;
1286 phys_addr_t ret = 0;
1287
1288 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1289
1290 if (pte) {
1291 if (iopte_is_small(*pte))
1292 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1293 else if (iopte_is_large(*pte))
1294 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1295 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001296 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
1297 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001298 } else {
1299 if (iopgd_is_section(*pgd))
1300 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1301 else if (iopgd_is_super(*pgd))
1302 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1303 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001304 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
1305 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001306 }
1307
1308 return ret;
1309}
1310
Laurent Pinchart07a02032014-02-28 14:42:38 -06001311static int omap_iommu_add_device(struct device *dev)
1312{
1313 struct omap_iommu_arch_data *arch_data;
1314 struct device_node *np;
Suman Anna7d682772014-09-04 17:27:30 -05001315 struct platform_device *pdev;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001316
1317 /*
1318 * Allocate the archdata iommu structure for DT-based devices.
1319 *
1320 * TODO: Simplify this when removing non-DT support completely from the
1321 * IOMMU users.
1322 */
1323 if (!dev->of_node)
1324 return 0;
1325
1326 np = of_parse_phandle(dev->of_node, "iommus", 0);
1327 if (!np)
1328 return 0;
1329
Suman Anna7d682772014-09-04 17:27:30 -05001330 pdev = of_find_device_by_node(np);
1331 if (WARN_ON(!pdev)) {
1332 of_node_put(np);
1333 return -EINVAL;
1334 }
1335
Laurent Pinchart07a02032014-02-28 14:42:38 -06001336 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
1337 if (!arch_data) {
1338 of_node_put(np);
1339 return -ENOMEM;
1340 }
1341
Suman Anna7d682772014-09-04 17:27:30 -05001342 arch_data->name = kstrdup(dev_name(&pdev->dev), GFP_KERNEL);
Laurent Pinchart07a02032014-02-28 14:42:38 -06001343 dev->archdata.iommu = arch_data;
1344
1345 of_node_put(np);
1346
1347 return 0;
1348}
1349
1350static void omap_iommu_remove_device(struct device *dev)
1351{
1352 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1353
1354 if (!dev->of_node || !arch_data)
1355 return;
1356
1357 kfree(arch_data->name);
1358 kfree(arch_data);
1359}
1360
Thierry Redingb22f6432014-06-27 09:03:12 +02001361static const struct iommu_ops omap_iommu_ops = {
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001362 .domain_init = omap_iommu_domain_init,
1363 .domain_destroy = omap_iommu_domain_destroy,
1364 .attach_dev = omap_iommu_attach_dev,
1365 .detach_dev = omap_iommu_detach_dev,
1366 .map = omap_iommu_map,
1367 .unmap = omap_iommu_unmap,
1368 .iova_to_phys = omap_iommu_iova_to_phys,
Laurent Pinchart07a02032014-02-28 14:42:38 -06001369 .add_device = omap_iommu_add_device,
1370 .remove_device = omap_iommu_remove_device,
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +02001371 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001372};
1373
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001374static int __init omap_iommu_init(void)
1375{
1376 struct kmem_cache *p;
1377 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1378 size_t align = 1 << 10; /* L2 pagetable alignement */
1379
1380 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1381 iopte_cachep_ctor);
1382 if (!p)
1383 return -ENOMEM;
1384 iopte_cachep = p;
1385
Joerg Roedela65bc642011-09-06 17:56:07 +02001386 bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001387
Suman Anna61c75352014-10-22 17:22:30 -05001388 omap_iommu_debugfs_init();
1389
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001390 return platform_driver_register(&omap_iommu_driver);
1391}
Ohad Ben-Cohen435792d2012-02-26 12:14:14 +02001392/* must be ready before omap3isp is probed */
1393subsys_initcall(omap_iommu_init);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001394
1395static void __exit omap_iommu_exit(void)
1396{
1397 kmem_cache_destroy(iopte_cachep);
1398
1399 platform_driver_unregister(&omap_iommu_driver);
Suman Anna61c75352014-10-22 17:22:30 -05001400
1401 omap_iommu_debugfs_exit();
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001402}
1403module_exit(omap_iommu_exit);
1404
1405MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1406MODULE_ALIAS("platform:omap-iommu");
1407MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1408MODULE_LICENSE("GPL v2");