blob: a4ba851825c235b5bfa7ab93a8256adfca160ca7 [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);
Kiran Padwal99cb9ae2014-10-30 11:59:47 +0530173 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
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);
Kiran Padwal99cb9ae2014-10-30 11:59:47 +0530193 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600194
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}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200514
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300515static int
516__dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200517{
518 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000519 struct iotlb_lock saved;
520 struct cr_regs tmp;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200521 struct cr_regs *p = crs;
522
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600523 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200524 iotlb_lock_get(obj, &saved);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200525
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000526 for_each_iotlb_cr(obj, num, i, tmp) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200527 if (!iotlb_cr_valid(&tmp))
528 continue;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200529 *p++ = tmp;
530 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000531
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200532 iotlb_lock_set(obj, &saved);
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600533 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200534
535 return p - crs;
536}
537
538/**
Suman Annabd4396f2014-10-22 17:22:27 -0500539 * iotlb_dump_cr - Dump an iommu tlb entry into buf
540 * @obj: target iommu
541 * @cr: contents of cam and ram register
542 * @buf: output buffer
543 **/
544static ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
545 char *buf)
546{
547 char *p = buf;
548
549 /* FIXME: Need more detail analysis of cam/ram */
550 p += sprintf(p, "%08x %08x %01x\n", cr->cam, cr->ram,
551 (cr->cam & MMU_CAM_P) ? 1 : 0);
552
553 return p - buf;
554}
555
556/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300557 * omap_dump_tlb_entries - dump cr arrays to given buffer
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200558 * @obj: target iommu
559 * @buf: output buffer
560 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300561size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200562{
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700563 int i, num;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200564 struct cr_regs *cr;
565 char *p = buf;
566
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700567 num = bytes / sizeof(*cr);
568 num = min(obj->nr_tlb_entries, num);
569
570 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200571 if (!cr)
572 return 0;
573
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700574 num = __dump_tlb_entries(obj, cr, num);
575 for (i = 0; i < num; i++)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200576 p += iotlb_dump_cr(obj, cr + i, p);
577 kfree(cr);
578
579 return p - buf;
580}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200581
Suman Anna61c75352014-10-22 17:22:30 -0500582#endif /* CONFIG_OMAP_IOMMU_DEBUG */
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200583
584/*
585 * H/W pagetable operations
586 */
587static void flush_iopgd_range(u32 *first, u32 *last)
588{
589 /* FIXME: L2 cache should be taken care of if it exists */
590 do {
591 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
592 : : "r" (first));
593 first += L1_CACHE_BYTES / sizeof(*first);
594 } while (first <= last);
595}
596
597static void flush_iopte_range(u32 *first, u32 *last)
598{
599 /* FIXME: L2 cache should be taken care of if it exists */
600 do {
601 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
602 : : "r" (first));
603 first += L1_CACHE_BYTES / sizeof(*first);
604 } while (first <= last);
605}
606
607static void iopte_free(u32 *iopte)
608{
609 /* Note: freed iopte's must be clean ready for re-use */
Zhouyi Zhoue28045a2014-03-05 18:20:19 +0800610 if (iopte)
611 kmem_cache_free(iopte_cachep, iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200612}
613
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300614static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200615{
616 u32 *iopte;
617
618 /* a table has already existed */
619 if (*iopgd)
620 goto pte_ready;
621
622 /*
623 * do the allocation outside the page table lock
624 */
625 spin_unlock(&obj->page_table_lock);
626 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
627 spin_lock(&obj->page_table_lock);
628
629 if (!*iopgd) {
630 if (!iopte)
631 return ERR_PTR(-ENOMEM);
632
633 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
634 flush_iopgd_range(iopgd, iopgd);
635
636 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
637 } else {
638 /* We raced, free the reduniovant table */
639 iopte_free(iopte);
640 }
641
642pte_ready:
643 iopte = iopte_offset(iopgd, da);
644
645 dev_vdbg(obj->dev,
646 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
647 __func__, da, iopgd, *iopgd, iopte, *iopte);
648
649 return iopte;
650}
651
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300652static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200653{
654 u32 *iopgd = iopgd_offset(obj, da);
655
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300656 if ((da | pa) & ~IOSECTION_MASK) {
657 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
658 __func__, da, pa, IOSECTION_SIZE);
659 return -EINVAL;
660 }
661
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200662 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
663 flush_iopgd_range(iopgd, iopgd);
664 return 0;
665}
666
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300667static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200668{
669 u32 *iopgd = iopgd_offset(obj, da);
670 int i;
671
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300672 if ((da | pa) & ~IOSUPER_MASK) {
673 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
674 __func__, da, pa, IOSUPER_SIZE);
675 return -EINVAL;
676 }
677
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200678 for (i = 0; i < 16; i++)
679 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
680 flush_iopgd_range(iopgd, iopgd + 15);
681 return 0;
682}
683
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300684static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200685{
686 u32 *iopgd = iopgd_offset(obj, da);
687 u32 *iopte = iopte_alloc(obj, iopgd, da);
688
689 if (IS_ERR(iopte))
690 return PTR_ERR(iopte);
691
692 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
693 flush_iopte_range(iopte, iopte);
694
695 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
696 __func__, da, pa, iopte, *iopte);
697
698 return 0;
699}
700
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300701static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200702{
703 u32 *iopgd = iopgd_offset(obj, da);
704 u32 *iopte = iopte_alloc(obj, iopgd, da);
705 int i;
706
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300707 if ((da | pa) & ~IOLARGE_MASK) {
708 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
709 __func__, da, pa, IOLARGE_SIZE);
710 return -EINVAL;
711 }
712
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200713 if (IS_ERR(iopte))
714 return PTR_ERR(iopte);
715
716 for (i = 0; i < 16; i++)
717 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
718 flush_iopte_range(iopte, iopte + 15);
719 return 0;
720}
721
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300722static int
723iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200724{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300725 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200726 u32 prot;
727 int err;
728
729 if (!obj || !e)
730 return -EINVAL;
731
732 switch (e->pgsz) {
733 case MMU_CAM_PGSZ_16M:
734 fn = iopgd_alloc_super;
735 break;
736 case MMU_CAM_PGSZ_1M:
737 fn = iopgd_alloc_section;
738 break;
739 case MMU_CAM_PGSZ_64K:
740 fn = iopte_alloc_large;
741 break;
742 case MMU_CAM_PGSZ_4K:
743 fn = iopte_alloc_page;
744 break;
745 default:
746 fn = NULL;
747 BUG();
748 break;
749 }
750
751 prot = get_iopte_attr(e);
752
753 spin_lock(&obj->page_table_lock);
754 err = fn(obj, e->da, e->pa, prot);
755 spin_unlock(&obj->page_table_lock);
756
757 return err;
758}
759
760/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300761 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200762 * @obj: target iommu
763 * @e: an iommu tlb entry info
764 **/
Suman Anna4899a562014-10-22 17:22:32 -0500765static int
766omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200767{
768 int err;
769
770 flush_iotlb_page(obj, e->da);
771 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200772 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300773 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200774 return err;
775}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200776
777/**
778 * iopgtable_lookup_entry - Lookup an iommu pte entry
779 * @obj: target iommu
780 * @da: iommu device virtual address
781 * @ppgd: iommu pgd entry pointer to be returned
782 * @ppte: iommu pte entry pointer to be returned
783 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300784static void
785iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200786{
787 u32 *iopgd, *iopte = NULL;
788
789 iopgd = iopgd_offset(obj, da);
790 if (!*iopgd)
791 goto out;
792
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300793 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200794 iopte = iopte_offset(iopgd, da);
795out:
796 *ppgd = iopgd;
797 *ppte = iopte;
798}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200799
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300800static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200801{
802 size_t bytes;
803 u32 *iopgd = iopgd_offset(obj, da);
804 int nent = 1;
805
806 if (!*iopgd)
807 return 0;
808
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300809 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200810 int i;
811 u32 *iopte = iopte_offset(iopgd, da);
812
813 bytes = IOPTE_SIZE;
814 if (*iopte & IOPTE_LARGE) {
815 nent *= 16;
816 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800817 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200818 }
819 bytes *= nent;
820 memset(iopte, 0, nent * sizeof(*iopte));
821 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
822
823 /*
824 * do table walk to check if this table is necessary or not
825 */
826 iopte = iopte_offset(iopgd, 0);
827 for (i = 0; i < PTRS_PER_IOPTE; i++)
828 if (iopte[i])
829 goto out;
830
831 iopte_free(iopte);
832 nent = 1; /* for the next L1 entry */
833 } else {
834 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700835 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200836 nent *= 16;
837 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800838 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200839 }
840 bytes *= nent;
841 }
842 memset(iopgd, 0, nent * sizeof(*iopgd));
843 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
844out:
845 return bytes;
846}
847
848/**
849 * iopgtable_clear_entry - Remove an iommu pte entry
850 * @obj: target iommu
851 * @da: iommu device virtual address
852 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300853static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200854{
855 size_t bytes;
856
857 spin_lock(&obj->page_table_lock);
858
859 bytes = iopgtable_clear_entry_core(obj, da);
860 flush_iotlb_page(obj, da);
861
862 spin_unlock(&obj->page_table_lock);
863
864 return bytes;
865}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200866
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300867static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200868{
869 int i;
870
871 spin_lock(&obj->page_table_lock);
872
873 for (i = 0; i < PTRS_PER_IOPGD; i++) {
874 u32 da;
875 u32 *iopgd;
876
877 da = i << IOPGD_SHIFT;
878 iopgd = iopgd_offset(obj, da);
879
880 if (!*iopgd)
881 continue;
882
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300883 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200884 iopte_free(iopte_offset(iopgd, 0));
885
886 *iopgd = 0;
887 flush_iopgd_range(iopgd, iopgd);
888 }
889
890 flush_iotlb_all(obj);
891
892 spin_unlock(&obj->page_table_lock);
893}
894
895/*
896 * Device IOMMU generic operations
897 */
898static irqreturn_t iommu_fault_handler(int irq, void *data)
899{
David Cohend594f1f2011-02-16 19:35:51 +0000900 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200901 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300902 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400903 struct iommu_domain *domain = obj->domain;
Suman Anna2088ecb2014-10-22 17:22:19 -0500904 struct omap_iommu_domain *omap_domain = domain->priv;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200905
Suman Anna2088ecb2014-10-22 17:22:19 -0500906 if (!omap_domain->iommu_dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200907 return IRQ_NONE;
908
David Cohend594f1f2011-02-16 19:35:51 +0000909 errs = iommu_report_fault(obj, &da);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200910 if (errs == 0)
911 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000912
913 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400914 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200915 return IRQ_HANDLED;
916
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000917 iommu_disable(obj);
918
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200919 iopgd = iopgd_offset(obj, da);
920
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300921 if (!iopgd_is_table(*iopgd)) {
Suman Annab6c2e092013-05-30 18:10:59 -0500922 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
923 obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200924 return IRQ_NONE;
925 }
926
927 iopte = iopte_offset(iopgd, da);
928
Suman Annab6c2e092013-05-30 18:10:59 -0500929 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
930 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200931
932 return IRQ_NONE;
933}
934
935static int device_match_by_alias(struct device *dev, void *data)
936{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300937 struct omap_iommu *obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200938 const char *name = data;
939
940 pr_debug("%s: %s %s\n", __func__, obj->name, name);
941
942 return strcmp(obj->name, name) == 0;
943}
944
945/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300946 * omap_iommu_attach() - attach iommu device to an iommu domain
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200947 * @name: name of target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300948 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200949 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200950static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200951{
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600952 int err;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200953 struct device *dev;
954 struct omap_iommu *obj;
955
956 dev = driver_find_device(&omap_iommu_driver.driver, NULL,
957 (void *)name,
958 device_match_by_alias);
959 if (!dev)
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600960 return ERR_PTR(-ENODEV);
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200961
962 obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200963
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300964 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200965
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300966 obj->iopgd = iopgd;
967 err = iommu_enable(obj);
968 if (err)
969 goto err_enable;
970 flush_iotlb_all(obj);
971
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300972 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200973
974 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
975 return obj;
976
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200977err_enable:
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300978 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200979 return ERR_PTR(err);
980}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200981
982/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300983 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200984 * @obj: target iommu
985 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300986static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200987{
Roel Kluinacf9d462010-01-08 10:29:05 -0800988 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200989 return;
990
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300991 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200992
Suman Anna2088ecb2014-10-22 17:22:19 -0500993 iommu_disable(obj);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300994 obj->iopgd = NULL;
995
996 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200997
998 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
999}
David Cohend594f1f2011-02-16 19:35:51 +00001000
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001001/*
1002 * OMAP Device MMU(IOMMU) detection
1003 */
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001004static int omap_iommu_probe(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001005{
1006 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001007 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001008 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001009 struct resource *res;
Kiran Padwal99cb9ae2014-10-30 11:59:47 +05301010 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Florian Vaussard3c927482014-02-28 14:42:36 -06001011 struct device_node *of = pdev->dev.of_node;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001012
Suman Annaf129b3d2014-02-28 14:42:32 -06001013 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001014 if (!obj)
1015 return -ENOMEM;
1016
Florian Vaussard3c927482014-02-28 14:42:36 -06001017 if (of) {
1018 obj->name = dev_name(&pdev->dev);
1019 obj->nr_tlb_entries = 32;
1020 err = of_property_read_u32(of, "ti,#tlb-entries",
1021 &obj->nr_tlb_entries);
1022 if (err && err != -EINVAL)
1023 return err;
1024 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
1025 return -EINVAL;
Suman Annab148d5f2014-02-28 14:42:37 -06001026 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
1027 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
Florian Vaussard3c927482014-02-28 14:42:36 -06001028 } else {
1029 obj->nr_tlb_entries = pdata->nr_tlb_entries;
1030 obj->name = pdata->name;
Florian Vaussard3c927482014-02-28 14:42:36 -06001031 }
Florian Vaussard3c927482014-02-28 14:42:36 -06001032
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001033 obj->dev = &pdev->dev;
1034 obj->ctx = (void *)obj + sizeof(*obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001035
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001036 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001037 spin_lock_init(&obj->page_table_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001038
1039 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001040 obj->regbase = devm_ioremap_resource(obj->dev, res);
1041 if (IS_ERR(obj->regbase))
1042 return PTR_ERR(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +00001043
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001044 irq = platform_get_irq(pdev, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001045 if (irq < 0)
1046 return -ENODEV;
1047
1048 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
1049 dev_name(obj->dev), obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001050 if (err < 0)
Suman Annaf129b3d2014-02-28 14:42:32 -06001051 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001052 platform_set_drvdata(pdev, obj);
1053
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001054 pm_runtime_irq_safe(obj->dev);
1055 pm_runtime_enable(obj->dev);
1056
Suman Anna61c75352014-10-22 17:22:30 -05001057 omap_iommu_debugfs_add(obj);
1058
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001059 dev_info(&pdev->dev, "%s registered\n", obj->name);
1060 return 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001061}
1062
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001063static int omap_iommu_remove(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001064{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001065 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001066
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001067 iopgtable_clear_entry_all(obj);
Suman Anna61c75352014-10-22 17:22:30 -05001068 omap_iommu_debugfs_remove(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001069
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001070 pm_runtime_disable(obj->dev);
1071
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001072 dev_info(&pdev->dev, "%s removed\n", obj->name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001073 return 0;
1074}
1075
Kiran Padwald943b0f2014-09-11 19:07:36 +05301076static const struct of_device_id omap_iommu_of_match[] = {
Florian Vaussard3c927482014-02-28 14:42:36 -06001077 { .compatible = "ti,omap2-iommu" },
1078 { .compatible = "ti,omap4-iommu" },
1079 { .compatible = "ti,dra7-iommu" },
1080 {},
1081};
1082MODULE_DEVICE_TABLE(of, omap_iommu_of_match);
1083
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001084static struct platform_driver omap_iommu_driver = {
1085 .probe = omap_iommu_probe,
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001086 .remove = omap_iommu_remove,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001087 .driver = {
1088 .name = "omap-iommu",
Florian Vaussard3c927482014-02-28 14:42:36 -06001089 .of_match_table = of_match_ptr(omap_iommu_of_match),
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001090 },
1091};
1092
1093static void iopte_cachep_ctor(void *iopte)
1094{
1095 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1096}
1097
Laurent Pinchart286f6002014-03-08 00:44:38 +01001098static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001099{
1100 memset(e, 0, sizeof(*e));
1101
1102 e->da = da;
1103 e->pa = pa;
Suman Annad760e3e2014-03-17 20:31:32 -05001104 e->valid = MMU_CAM_V;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001105 e->pgsz = pgsz;
1106 e->endian = MMU_RAM_ENDIAN_LITTLE;
1107 e->elsz = MMU_RAM_ELSZ_8;
1108 e->mixed = 0;
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001109
1110 return iopgsz_to_bytes(e->pgsz);
1111}
1112
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001113static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001114 phys_addr_t pa, size_t bytes, int prot)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001115{
1116 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001117 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001118 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001119 struct iotlb_entry e;
1120 int omap_pgsz;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001121 u32 ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001122
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001123 omap_pgsz = bytes_to_iopgsz(bytes);
1124 if (omap_pgsz < 0) {
1125 dev_err(dev, "invalid size to map: %d\n", bytes);
1126 return -EINVAL;
1127 }
1128
Joerg Roedel1d7f4492015-01-22 14:42:06 +01001129 dev_dbg(dev, "mapping da 0x%lx to pa %pa size 0x%x\n", da, &pa, bytes);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001130
Laurent Pinchart286f6002014-03-08 00:44:38 +01001131 iotlb_init_entry(&e, da, pa, omap_pgsz);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001132
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001133 ret = omap_iopgtable_store_entry(oiommu, &e);
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001134 if (ret)
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001135 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001136
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001137 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001138}
1139
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001140static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1141 size_t size)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001142{
1143 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001144 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001145 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001146
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001147 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001148
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001149 return iopgtable_clear_entry(oiommu, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001150}
1151
1152static int
1153omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1154{
1155 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001156 struct omap_iommu *oiommu;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001157 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001158 int ret = 0;
1159
Suman Annae3f595b2014-09-04 17:27:29 -05001160 if (!arch_data || !arch_data->name) {
1161 dev_err(dev, "device doesn't have an associated iommu\n");
1162 return -EINVAL;
1163 }
1164
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001165 spin_lock(&omap_domain->lock);
1166
1167 /* only a single device is supported per domain for now */
1168 if (omap_domain->iommu_dev) {
1169 dev_err(dev, "iommu domain is already attached\n");
1170 ret = -EBUSY;
1171 goto out;
1172 }
1173
1174 /* get a handle to and enable the omap iommu */
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001175 oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001176 if (IS_ERR(oiommu)) {
1177 ret = PTR_ERR(oiommu);
1178 dev_err(dev, "can't get omap iommu: %d\n", ret);
1179 goto out;
1180 }
1181
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001182 omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001183 omap_domain->dev = dev;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -04001184 oiommu->domain = domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001185
1186out:
1187 spin_unlock(&omap_domain->lock);
1188 return ret;
1189}
1190
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001191static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
1192 struct device *dev)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001193{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001194 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001195 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001196
1197 /* only a single device is supported per domain for now */
1198 if (omap_domain->iommu_dev != oiommu) {
1199 dev_err(dev, "invalid iommu device\n");
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001200 return;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001201 }
1202
1203 iopgtable_clear_entry_all(oiommu);
1204
1205 omap_iommu_detach(oiommu);
1206
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001207 omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001208 omap_domain->dev = NULL;
Suman Annaf24d9ad2014-10-22 17:22:33 -05001209 oiommu->domain = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001210}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001211
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001212static void omap_iommu_detach_dev(struct iommu_domain *domain,
1213 struct device *dev)
1214{
1215 struct omap_iommu_domain *omap_domain = domain->priv;
1216
1217 spin_lock(&omap_domain->lock);
1218 _omap_iommu_detach_dev(omap_domain, dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001219 spin_unlock(&omap_domain->lock);
1220}
1221
1222static int omap_iommu_domain_init(struct iommu_domain *domain)
1223{
1224 struct omap_iommu_domain *omap_domain;
1225
1226 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1227 if (!omap_domain) {
1228 pr_err("kzalloc failed\n");
1229 goto out;
1230 }
1231
1232 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1233 if (!omap_domain->pgtable) {
1234 pr_err("kzalloc failed\n");
1235 goto fail_nomem;
1236 }
1237
1238 /*
1239 * should never fail, but please keep this around to ensure
1240 * we keep the hardware happy
1241 */
1242 BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
1243
1244 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1245 spin_lock_init(&omap_domain->lock);
1246
1247 domain->priv = omap_domain;
1248
Joerg Roedel2c6edb02012-01-26 19:40:55 +01001249 domain->geometry.aperture_start = 0;
1250 domain->geometry.aperture_end = (1ULL << 32) - 1;
1251 domain->geometry.force_aperture = true;
1252
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001253 return 0;
1254
1255fail_nomem:
1256 kfree(omap_domain);
1257out:
1258 return -ENOMEM;
1259}
1260
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001261static void omap_iommu_domain_destroy(struct iommu_domain *domain)
1262{
1263 struct omap_iommu_domain *omap_domain = domain->priv;
1264
1265 domain->priv = NULL;
1266
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001267 /*
1268 * An iommu device is still attached
1269 * (currently, only one device can be attached) ?
1270 */
1271 if (omap_domain->iommu_dev)
1272 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1273
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001274 kfree(omap_domain->pgtable);
1275 kfree(omap_domain);
1276}
1277
1278static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547ac2013-03-29 01:23:58 +05301279 dma_addr_t da)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001280{
1281 struct omap_iommu_domain *omap_domain = domain->priv;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001282 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001283 struct device *dev = oiommu->dev;
1284 u32 *pgd, *pte;
1285 phys_addr_t ret = 0;
1286
1287 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1288
1289 if (pte) {
1290 if (iopte_is_small(*pte))
1291 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1292 else if (iopte_is_large(*pte))
1293 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1294 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001295 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
1296 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001297 } else {
1298 if (iopgd_is_section(*pgd))
1299 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1300 else if (iopgd_is_super(*pgd))
1301 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1302 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001303 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
1304 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001305 }
1306
1307 return ret;
1308}
1309
Laurent Pinchart07a02032014-02-28 14:42:38 -06001310static int omap_iommu_add_device(struct device *dev)
1311{
1312 struct omap_iommu_arch_data *arch_data;
1313 struct device_node *np;
Suman Anna7d682772014-09-04 17:27:30 -05001314 struct platform_device *pdev;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001315
1316 /*
1317 * Allocate the archdata iommu structure for DT-based devices.
1318 *
1319 * TODO: Simplify this when removing non-DT support completely from the
1320 * IOMMU users.
1321 */
1322 if (!dev->of_node)
1323 return 0;
1324
1325 np = of_parse_phandle(dev->of_node, "iommus", 0);
1326 if (!np)
1327 return 0;
1328
Suman Anna7d682772014-09-04 17:27:30 -05001329 pdev = of_find_device_by_node(np);
1330 if (WARN_ON(!pdev)) {
1331 of_node_put(np);
1332 return -EINVAL;
1333 }
1334
Laurent Pinchart07a02032014-02-28 14:42:38 -06001335 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
1336 if (!arch_data) {
1337 of_node_put(np);
1338 return -ENOMEM;
1339 }
1340
Suman Anna7d682772014-09-04 17:27:30 -05001341 arch_data->name = kstrdup(dev_name(&pdev->dev), GFP_KERNEL);
Laurent Pinchart07a02032014-02-28 14:42:38 -06001342 dev->archdata.iommu = arch_data;
1343
1344 of_node_put(np);
1345
1346 return 0;
1347}
1348
1349static void omap_iommu_remove_device(struct device *dev)
1350{
1351 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1352
1353 if (!dev->of_node || !arch_data)
1354 return;
1355
1356 kfree(arch_data->name);
1357 kfree(arch_data);
1358}
1359
Thierry Redingb22f6432014-06-27 09:03:12 +02001360static const struct iommu_ops omap_iommu_ops = {
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001361 .domain_init = omap_iommu_domain_init,
1362 .domain_destroy = omap_iommu_domain_destroy,
1363 .attach_dev = omap_iommu_attach_dev,
1364 .detach_dev = omap_iommu_detach_dev,
1365 .map = omap_iommu_map,
1366 .unmap = omap_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07001367 .map_sg = default_iommu_map_sg,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001368 .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 */
Thierry Redingf938aab2015-02-06 11:44:06 +01001379 struct device_node *np;
1380
1381 np = of_find_matching_node(NULL, omap_iommu_of_match);
1382 if (!np)
1383 return 0;
1384
1385 of_node_put(np);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001386
1387 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1388 iopte_cachep_ctor);
1389 if (!p)
1390 return -ENOMEM;
1391 iopte_cachep = p;
1392
Joerg Roedela65bc642011-09-06 17:56:07 +02001393 bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001394
Suman Anna61c75352014-10-22 17:22:30 -05001395 omap_iommu_debugfs_init();
1396
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001397 return platform_driver_register(&omap_iommu_driver);
1398}
Ohad Ben-Cohen435792d2012-02-26 12:14:14 +02001399/* must be ready before omap3isp is probed */
1400subsys_initcall(omap_iommu_init);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001401
1402static void __exit omap_iommu_exit(void)
1403{
1404 kmem_cache_destroy(iopte_cachep);
1405
1406 platform_driver_unregister(&omap_iommu_driver);
Suman Anna61c75352014-10-22 17:22:30 -05001407
1408 omap_iommu_debugfs_exit();
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001409}
1410module_exit(omap_iommu_exit);
1411
1412MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1413MODULE_ALIAS("platform:omap-iommu");
1414MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1415MODULE_LICENSE("GPL v2");