blob: eeecfc4073aff6fdcf2f6cb1a729946e835b2466 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020016#include <linux/interrupt.h>
17#include <linux/ioport.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020018#include <linux/platform_device.h>
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030019#include <linux/iommu.h>
Tony Lindgrenc8d35c82012-11-02 12:24:03 -070020#include <linux/omap-iommu.h>
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030021#include <linux/mutex.h>
22#include <linux/spinlock.h>
Tony Lindgrened1c7de2012-11-02 12:24:06 -070023#include <linux/io.h>
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -060024#include <linux/pm_runtime.h>
Florian Vaussard3c927482014-02-28 14:42:36 -060025#include <linux/of.h>
26#include <linux/of_iommu.h>
27#include <linux/of_irq.h>
Suman Anna7d682772014-09-04 17:27:30 -050028#include <linux/of_platform.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020029
30#include <asm/cacheflush.h>
31
Tony Lindgren2ab7c842012-11-02 12:24:14 -070032#include <linux/platform_data/iommu-omap.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020033
Ido Yariv2f7702a2012-11-02 12:24:00 -070034#include "omap-iopgtable.h"
Tony Lindgrened1c7de2012-11-02 12:24:06 -070035#include "omap-iommu.h"
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020036
Suman Anna5acc97d2014-03-17 20:31:34 -050037#define to_iommu(dev) \
38 ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
39
Hiroshi DOYU37c28362010-04-27 05:37:12 +000040#define for_each_iotlb_cr(obj, n, __i, cr) \
41 for (__i = 0; \
42 (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \
43 __i++)
44
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +020045/* bitmap of the page sizes currently supported */
46#define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
47
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030048/**
49 * struct omap_iommu_domain - omap iommu domain
50 * @pgtable: the page table
51 * @iommu_dev: an omap iommu device attached to this domain. only a single
52 * iommu device can be attached for now.
Omar Ramirez Luna803b5272012-04-18 13:09:41 -050053 * @dev: Device using this domain.
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030054 * @lock: domain lock, should be taken when attaching/detaching
55 */
56struct omap_iommu_domain {
57 u32 *pgtable;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030058 struct omap_iommu *iommu_dev;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -050059 struct device *dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030060 spinlock_t lock;
Joerg Roedel8cf851e2015-03-26 13:43:09 +010061 struct iommu_domain domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030062};
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/**
Joerg Roedel8cf851e2015-03-26 13:43:09 +010083 * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
84 * @dom: generic iommu domain handle
85 **/
86static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
87{
88 return container_of(dom, struct omap_iommu_domain, domain);
89}
90
91/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030092 * omap_iommu_save_ctx - Save registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020093 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020094 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020095void omap_iommu_save_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020096{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020097 struct omap_iommu *obj = dev_to_omap_iommu(dev);
Suman Annabd4396f2014-10-22 17:22:27 -050098 u32 *p = obj->ctx;
99 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200100
Suman Annabd4396f2014-10-22 17:22:27 -0500101 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
102 p[i] = iommu_read_reg(obj, i * sizeof(u32));
103 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
104 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200105}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300106EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200107
108/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300109 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200110 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200111 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200112void omap_iommu_restore_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200113{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200114 struct omap_iommu *obj = dev_to_omap_iommu(dev);
Suman Annabd4396f2014-10-22 17:22:27 -0500115 u32 *p = obj->ctx;
116 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200117
Suman Annabd4396f2014-10-22 17:22:27 -0500118 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
119 iommu_write_reg(obj, p[i], i * sizeof(u32));
120 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
121 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200122}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300123EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200124
Suman Annabd4396f2014-10-22 17:22:27 -0500125static void __iommu_set_twl(struct omap_iommu *obj, bool on)
126{
127 u32 l = iommu_read_reg(obj, MMU_CNTL);
128
129 if (on)
130 iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
131 else
132 iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
133
134 l &= ~MMU_CNTL_MASK;
135 if (on)
136 l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
137 else
138 l |= (MMU_CNTL_MMU_EN);
139
140 iommu_write_reg(obj, l, MMU_CNTL);
141}
142
143static int omap2_iommu_enable(struct omap_iommu *obj)
144{
145 u32 l, pa;
146
147 if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
148 return -EINVAL;
149
150 pa = virt_to_phys(obj->iopgd);
151 if (!IS_ALIGNED(pa, SZ_16K))
152 return -EINVAL;
153
154 l = iommu_read_reg(obj, MMU_REVISION);
155 dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
156 (l >> 4) & 0xf, l & 0xf);
157
158 iommu_write_reg(obj, pa, MMU_TTB);
159
160 if (obj->has_bus_err_back)
161 iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
162
163 __iommu_set_twl(obj, true);
164
165 return 0;
166}
167
168static void omap2_iommu_disable(struct omap_iommu *obj)
169{
170 u32 l = iommu_read_reg(obj, MMU_CNTL);
171
172 l &= ~MMU_CNTL_MASK;
173 iommu_write_reg(obj, l, MMU_CNTL);
174
175 dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
176}
177
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300178static int iommu_enable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200179{
180 int err;
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600181 struct platform_device *pdev = to_platform_device(obj->dev);
Kiran Padwal99cb9ae2014-10-30 11:59:47 +0530182 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200183
Florian Vaussard90e569c2014-02-28 14:42:34 -0600184 if (pdata && pdata->deassert_reset) {
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600185 err = pdata->deassert_reset(pdev, pdata->reset_name);
186 if (err) {
187 dev_err(obj->dev, "deassert_reset failed: %d\n", err);
188 return err;
189 }
190 }
191
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600192 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200193
Suman Annabd4396f2014-10-22 17:22:27 -0500194 err = omap2_iommu_enable(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200195
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200196 return err;
197}
198
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300199static void iommu_disable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200200{
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600201 struct platform_device *pdev = to_platform_device(obj->dev);
Kiran Padwal99cb9ae2014-10-30 11:59:47 +0530202 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600203
Suman Annabd4396f2014-10-22 17:22:27 -0500204 omap2_iommu_disable(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200205
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600206 pm_runtime_put_sync(obj->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600207
Florian Vaussard90e569c2014-02-28 14:42:34 -0600208 if (pdata && pdata->assert_reset)
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600209 pdata->assert_reset(pdev, pdata->reset_name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200210}
211
212/*
213 * TLB operations
214 */
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200215static inline int iotlb_cr_valid(struct cr_regs *cr)
216{
217 if (!cr)
218 return -EINVAL;
219
Suman Annabd4396f2014-10-22 17:22:27 -0500220 return cr->cam & MMU_CAM_V;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200221}
222
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300223static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200224{
Suman Annabd4396f2014-10-22 17:22:27 -0500225 u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
226 u32 mask = get_cam_va_mask(cr->cam & page_size);
227
228 return cr->cam & mask;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200229}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200230
231static u32 get_iopte_attr(struct iotlb_entry *e)
232{
Suman Annabd4396f2014-10-22 17:22:27 -0500233 u32 attr;
234
235 attr = e->mixed << 5;
236 attr |= e->endian;
237 attr |= e->elsz >> 3;
238 attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
239 (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
240 return attr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200241}
242
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300243static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200244{
Suman Annabd4396f2014-10-22 17:22:27 -0500245 u32 status, fault_addr;
246
247 status = iommu_read_reg(obj, MMU_IRQSTATUS);
248 status &= MMU_IRQ_MASK;
249 if (!status) {
250 *da = 0;
251 return 0;
252 }
253
254 fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
255 *da = fault_addr;
256
257 iommu_write_reg(obj, status, MMU_IRQSTATUS);
258
259 return status;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200260}
261
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300262static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200263{
264 u32 val;
265
266 val = iommu_read_reg(obj, MMU_LOCK);
267
268 l->base = MMU_LOCK_BASE(val);
269 l->vict = MMU_LOCK_VICT(val);
270
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200271}
272
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300273static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200274{
275 u32 val;
276
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200277 val = (l->base << MMU_LOCK_BASE_SHIFT);
278 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
279
280 iommu_write_reg(obj, val, MMU_LOCK);
281}
282
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300283static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200284{
Suman Annabd4396f2014-10-22 17:22:27 -0500285 cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
286 cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200287}
288
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300289static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200290{
Suman Annabd4396f2014-10-22 17:22:27 -0500291 iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
292 iommu_write_reg(obj, cr->ram, MMU_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200293
294 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
295 iommu_write_reg(obj, 1, MMU_LD_TLB);
296}
297
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000298/* only used in iotlb iteration for-loop */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300299static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000300{
301 struct cr_regs cr;
302 struct iotlb_lock l;
303
304 iotlb_lock_get(obj, &l);
305 l.vict = n;
306 iotlb_lock_set(obj, &l);
307 iotlb_read_cr(obj, &cr);
308
309 return cr;
310}
311
Suman Annabd4396f2014-10-22 17:22:27 -0500312#ifdef PREFETCH_IOTLB
313static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
314 struct iotlb_entry *e)
315{
316 struct cr_regs *cr;
317
318 if (!e)
319 return NULL;
320
321 if (e->da & ~(get_cam_va_mask(e->pgsz))) {
322 dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
323 e->da);
324 return ERR_PTR(-EINVAL);
325 }
326
327 cr = kmalloc(sizeof(*cr), GFP_KERNEL);
328 if (!cr)
329 return ERR_PTR(-ENOMEM);
330
331 cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
332 cr->ram = e->pa | e->endian | e->elsz | e->mixed;
333
334 return cr;
335}
336
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200337/**
338 * load_iotlb_entry - Set an iommu tlb entry
339 * @obj: target iommu
340 * @e: an iommu tlb entry info
341 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300342static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200343{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200344 int err = 0;
345 struct iotlb_lock l;
346 struct cr_regs *cr;
347
348 if (!obj || !obj->nr_tlb_entries || !e)
349 return -EINVAL;
350
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600351 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200352
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000353 iotlb_lock_get(obj, &l);
354 if (l.base == obj->nr_tlb_entries) {
355 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200356 err = -EBUSY;
357 goto out;
358 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000359 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000360 int i;
361 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000362
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000363 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000364 if (!iotlb_cr_valid(&tmp))
365 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000366
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000367 if (i == obj->nr_tlb_entries) {
368 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
369 err = -EBUSY;
370 goto out;
371 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000372
373 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000374 } else {
375 l.vict = l.base;
376 iotlb_lock_set(obj, &l);
377 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200378
379 cr = iotlb_alloc_cr(obj, e);
380 if (IS_ERR(cr)) {
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600381 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200382 return PTR_ERR(cr);
383 }
384
385 iotlb_load_cr(obj, cr);
386 kfree(cr);
387
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000388 if (e->prsvd)
389 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200390 /* increment victim for next tlb load */
391 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000392 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200393 iotlb_lock_set(obj, &l);
394out:
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600395 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200396 return err;
397}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200398
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300399#else /* !PREFETCH_IOTLB */
400
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300401static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300402{
403 return 0;
404}
405
406#endif /* !PREFETCH_IOTLB */
407
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300408static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300409{
410 return load_iotlb_entry(obj, e);
411}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200412
413/**
414 * flush_iotlb_page - Clear an iommu tlb entry
415 * @obj: target iommu
416 * @da: iommu device virtual address
417 *
418 * Clear an iommu tlb entry which includes 'da' address.
419 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300420static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200421{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200422 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000423 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200424
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600425 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200426
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000427 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200428 u32 start;
429 size_t bytes;
430
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200431 if (!iotlb_cr_valid(&cr))
432 continue;
433
434 start = iotlb_cr_to_virt(&cr);
435 bytes = iopgsz_to_bytes(cr.cam & 3);
436
437 if ((start <= da) && (da < start + bytes)) {
438 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
439 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000440 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200441 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
Laurent Pinchartf7129a02014-03-07 23:47:03 +0100442 break;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200443 }
444 }
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600445 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200446
447 if (i == obj->nr_tlb_entries)
448 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
449}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200450
451/**
452 * flush_iotlb_all - Clear all iommu tlb entries
453 * @obj: target iommu
454 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300455static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200456{
457 struct iotlb_lock l;
458
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600459 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200460
461 l.base = 0;
462 l.vict = 0;
463 iotlb_lock_set(obj, &l);
464
465 iommu_write_reg(obj, 1, MMU_GFLUSH);
466
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600467 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200468}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200469
Suman Anna61c75352014-10-22 17:22:30 -0500470#ifdef CONFIG_OMAP_IOMMU_DEBUG
Kanigeri, Hariddfa9752010-05-24 02:01:51 +0000471
Suman Annabd4396f2014-10-22 17:22:27 -0500472#define pr_reg(name) \
473 do { \
474 ssize_t bytes; \
475 const char *str = "%20s: %08x\n"; \
476 const int maxcol = 32; \
477 bytes = snprintf(p, maxcol, str, __stringify(name), \
478 iommu_read_reg(obj, MMU_##name)); \
479 p += bytes; \
480 len -= bytes; \
481 if (len < maxcol) \
482 goto out; \
483 } while (0)
484
485static ssize_t
486omap2_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len)
487{
488 char *p = buf;
489
490 pr_reg(REVISION);
491 pr_reg(IRQSTATUS);
492 pr_reg(IRQENABLE);
493 pr_reg(WALKING_ST);
494 pr_reg(CNTL);
495 pr_reg(FAULT_AD);
496 pr_reg(TTB);
497 pr_reg(LOCK);
498 pr_reg(LD_TLB);
499 pr_reg(CAM);
500 pr_reg(RAM);
501 pr_reg(GFLUSH);
502 pr_reg(FLUSH_ENTRY);
503 pr_reg(READ_CAM);
504 pr_reg(READ_RAM);
505 pr_reg(EMU_FAULT_AD);
506out:
507 return p - buf;
508}
509
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300510ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200511{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200512 if (!obj || !buf)
513 return -EINVAL;
514
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600515 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200516
Suman Annabd4396f2014-10-22 17:22:27 -0500517 bytes = omap2_iommu_dump_ctx(obj, buf, bytes);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200518
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600519 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200520
521 return bytes;
522}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200523
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300524static int
525__dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200526{
527 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000528 struct iotlb_lock saved;
529 struct cr_regs tmp;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200530 struct cr_regs *p = crs;
531
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600532 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200533 iotlb_lock_get(obj, &saved);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200534
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000535 for_each_iotlb_cr(obj, num, i, tmp) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200536 if (!iotlb_cr_valid(&tmp))
537 continue;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200538 *p++ = tmp;
539 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000540
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200541 iotlb_lock_set(obj, &saved);
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600542 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200543
544 return p - crs;
545}
546
547/**
Suman Annabd4396f2014-10-22 17:22:27 -0500548 * iotlb_dump_cr - Dump an iommu tlb entry into buf
549 * @obj: target iommu
550 * @cr: contents of cam and ram register
551 * @buf: output buffer
552 **/
553static ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
554 char *buf)
555{
556 char *p = buf;
557
558 /* FIXME: Need more detail analysis of cam/ram */
559 p += sprintf(p, "%08x %08x %01x\n", cr->cam, cr->ram,
560 (cr->cam & MMU_CAM_P) ? 1 : 0);
561
562 return p - buf;
563}
564
565/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300566 * omap_dump_tlb_entries - dump cr arrays to given buffer
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200567 * @obj: target iommu
568 * @buf: output buffer
569 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300570size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200571{
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700572 int i, num;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200573 struct cr_regs *cr;
574 char *p = buf;
575
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700576 num = bytes / sizeof(*cr);
577 num = min(obj->nr_tlb_entries, num);
578
579 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200580 if (!cr)
581 return 0;
582
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700583 num = __dump_tlb_entries(obj, cr, num);
584 for (i = 0; i < num; i++)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200585 p += iotlb_dump_cr(obj, cr + i, p);
586 kfree(cr);
587
588 return p - buf;
589}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200590
Suman Anna61c75352014-10-22 17:22:30 -0500591#endif /* CONFIG_OMAP_IOMMU_DEBUG */
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200592
593/*
594 * H/W pagetable operations
595 */
596static void flush_iopgd_range(u32 *first, u32 *last)
597{
598 /* FIXME: L2 cache should be taken care of if it exists */
599 do {
600 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
601 : : "r" (first));
602 first += L1_CACHE_BYTES / sizeof(*first);
603 } while (first <= last);
604}
605
606static void flush_iopte_range(u32 *first, u32 *last)
607{
608 /* FIXME: L2 cache should be taken care of if it exists */
609 do {
610 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
611 : : "r" (first));
612 first += L1_CACHE_BYTES / sizeof(*first);
613 } while (first <= last);
614}
615
616static void iopte_free(u32 *iopte)
617{
618 /* Note: freed iopte's must be clean ready for re-use */
Zhouyi Zhoue28045a2014-03-05 18:20:19 +0800619 if (iopte)
620 kmem_cache_free(iopte_cachep, iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200621}
622
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300623static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200624{
625 u32 *iopte;
626
627 /* a table has already existed */
628 if (*iopgd)
629 goto pte_ready;
630
631 /*
632 * do the allocation outside the page table lock
633 */
634 spin_unlock(&obj->page_table_lock);
635 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
636 spin_lock(&obj->page_table_lock);
637
638 if (!*iopgd) {
639 if (!iopte)
640 return ERR_PTR(-ENOMEM);
641
642 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
643 flush_iopgd_range(iopgd, iopgd);
644
645 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
646 } else {
647 /* We raced, free the reduniovant table */
648 iopte_free(iopte);
649 }
650
651pte_ready:
652 iopte = iopte_offset(iopgd, da);
653
654 dev_vdbg(obj->dev,
655 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
656 __func__, da, iopgd, *iopgd, iopte, *iopte);
657
658 return iopte;
659}
660
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300661static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200662{
663 u32 *iopgd = iopgd_offset(obj, da);
664
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300665 if ((da | pa) & ~IOSECTION_MASK) {
666 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
667 __func__, da, pa, IOSECTION_SIZE);
668 return -EINVAL;
669 }
670
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200671 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
672 flush_iopgd_range(iopgd, iopgd);
673 return 0;
674}
675
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300676static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200677{
678 u32 *iopgd = iopgd_offset(obj, da);
679 int i;
680
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300681 if ((da | pa) & ~IOSUPER_MASK) {
682 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
683 __func__, da, pa, IOSUPER_SIZE);
684 return -EINVAL;
685 }
686
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200687 for (i = 0; i < 16; i++)
688 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
689 flush_iopgd_range(iopgd, iopgd + 15);
690 return 0;
691}
692
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300693static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200694{
695 u32 *iopgd = iopgd_offset(obj, da);
696 u32 *iopte = iopte_alloc(obj, iopgd, da);
697
698 if (IS_ERR(iopte))
699 return PTR_ERR(iopte);
700
701 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
702 flush_iopte_range(iopte, iopte);
703
704 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
705 __func__, da, pa, iopte, *iopte);
706
707 return 0;
708}
709
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300710static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200711{
712 u32 *iopgd = iopgd_offset(obj, da);
713 u32 *iopte = iopte_alloc(obj, iopgd, da);
714 int i;
715
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300716 if ((da | pa) & ~IOLARGE_MASK) {
717 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
718 __func__, da, pa, IOLARGE_SIZE);
719 return -EINVAL;
720 }
721
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200722 if (IS_ERR(iopte))
723 return PTR_ERR(iopte);
724
725 for (i = 0; i < 16; i++)
726 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
727 flush_iopte_range(iopte, iopte + 15);
728 return 0;
729}
730
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300731static int
732iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200733{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300734 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200735 u32 prot;
736 int err;
737
738 if (!obj || !e)
739 return -EINVAL;
740
741 switch (e->pgsz) {
742 case MMU_CAM_PGSZ_16M:
743 fn = iopgd_alloc_super;
744 break;
745 case MMU_CAM_PGSZ_1M:
746 fn = iopgd_alloc_section;
747 break;
748 case MMU_CAM_PGSZ_64K:
749 fn = iopte_alloc_large;
750 break;
751 case MMU_CAM_PGSZ_4K:
752 fn = iopte_alloc_page;
753 break;
754 default:
755 fn = NULL;
756 BUG();
757 break;
758 }
759
760 prot = get_iopte_attr(e);
761
762 spin_lock(&obj->page_table_lock);
763 err = fn(obj, e->da, e->pa, prot);
764 spin_unlock(&obj->page_table_lock);
765
766 return err;
767}
768
769/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300770 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200771 * @obj: target iommu
772 * @e: an iommu tlb entry info
773 **/
Suman Anna4899a562014-10-22 17:22:32 -0500774static int
775omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200776{
777 int err;
778
779 flush_iotlb_page(obj, e->da);
780 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200781 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300782 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200783 return err;
784}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200785
786/**
787 * iopgtable_lookup_entry - Lookup an iommu pte entry
788 * @obj: target iommu
789 * @da: iommu device virtual address
790 * @ppgd: iommu pgd entry pointer to be returned
791 * @ppte: iommu pte entry pointer to be returned
792 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300793static void
794iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200795{
796 u32 *iopgd, *iopte = NULL;
797
798 iopgd = iopgd_offset(obj, da);
799 if (!*iopgd)
800 goto out;
801
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300802 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200803 iopte = iopte_offset(iopgd, da);
804out:
805 *ppgd = iopgd;
806 *ppte = iopte;
807}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200808
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300809static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200810{
811 size_t bytes;
812 u32 *iopgd = iopgd_offset(obj, da);
813 int nent = 1;
814
815 if (!*iopgd)
816 return 0;
817
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300818 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200819 int i;
820 u32 *iopte = iopte_offset(iopgd, da);
821
822 bytes = IOPTE_SIZE;
823 if (*iopte & IOPTE_LARGE) {
824 nent *= 16;
825 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800826 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200827 }
828 bytes *= nent;
829 memset(iopte, 0, nent * sizeof(*iopte));
830 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
831
832 /*
833 * do table walk to check if this table is necessary or not
834 */
835 iopte = iopte_offset(iopgd, 0);
836 for (i = 0; i < PTRS_PER_IOPTE; i++)
837 if (iopte[i])
838 goto out;
839
840 iopte_free(iopte);
841 nent = 1; /* for the next L1 entry */
842 } else {
843 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700844 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200845 nent *= 16;
846 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800847 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200848 }
849 bytes *= nent;
850 }
851 memset(iopgd, 0, nent * sizeof(*iopgd));
852 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
853out:
854 return bytes;
855}
856
857/**
858 * iopgtable_clear_entry - Remove an iommu pte entry
859 * @obj: target iommu
860 * @da: iommu device virtual address
861 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300862static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200863{
864 size_t bytes;
865
866 spin_lock(&obj->page_table_lock);
867
868 bytes = iopgtable_clear_entry_core(obj, da);
869 flush_iotlb_page(obj, da);
870
871 spin_unlock(&obj->page_table_lock);
872
873 return bytes;
874}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200875
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300876static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200877{
878 int i;
879
880 spin_lock(&obj->page_table_lock);
881
882 for (i = 0; i < PTRS_PER_IOPGD; i++) {
883 u32 da;
884 u32 *iopgd;
885
886 da = i << IOPGD_SHIFT;
887 iopgd = iopgd_offset(obj, da);
888
889 if (!*iopgd)
890 continue;
891
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300892 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200893 iopte_free(iopte_offset(iopgd, 0));
894
895 *iopgd = 0;
896 flush_iopgd_range(iopgd, iopgd);
897 }
898
899 flush_iotlb_all(obj);
900
901 spin_unlock(&obj->page_table_lock);
902}
903
904/*
905 * Device IOMMU generic operations
906 */
907static irqreturn_t iommu_fault_handler(int irq, void *data)
908{
David Cohend594f1f2011-02-16 19:35:51 +0000909 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200910 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300911 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400912 struct iommu_domain *domain = obj->domain;
Joerg Roedel8cf851e2015-03-26 13:43:09 +0100913 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200914
Suman Anna2088ecb2014-10-22 17:22:19 -0500915 if (!omap_domain->iommu_dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200916 return IRQ_NONE;
917
David Cohend594f1f2011-02-16 19:35:51 +0000918 errs = iommu_report_fault(obj, &da);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200919 if (errs == 0)
920 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000921
922 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400923 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200924 return IRQ_HANDLED;
925
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000926 iommu_disable(obj);
927
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200928 iopgd = iopgd_offset(obj, da);
929
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300930 if (!iopgd_is_table(*iopgd)) {
Suman Annab6c2e092013-05-30 18:10:59 -0500931 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
932 obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200933 return IRQ_NONE;
934 }
935
936 iopte = iopte_offset(iopgd, da);
937
Suman Annab6c2e092013-05-30 18:10:59 -0500938 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
939 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200940
941 return IRQ_NONE;
942}
943
944static int device_match_by_alias(struct device *dev, void *data)
945{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300946 struct omap_iommu *obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200947 const char *name = data;
948
949 pr_debug("%s: %s %s\n", __func__, obj->name, name);
950
951 return strcmp(obj->name, name) == 0;
952}
953
954/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300955 * omap_iommu_attach() - attach iommu device to an iommu domain
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200956 * @name: name of target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300957 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200958 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200959static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200960{
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600961 int err;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200962 struct device *dev;
963 struct omap_iommu *obj;
964
965 dev = driver_find_device(&omap_iommu_driver.driver, NULL,
966 (void *)name,
967 device_match_by_alias);
968 if (!dev)
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600969 return ERR_PTR(-ENODEV);
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200970
971 obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200972
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300973 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200974
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300975 obj->iopgd = iopgd;
976 err = iommu_enable(obj);
977 if (err)
978 goto err_enable;
979 flush_iotlb_all(obj);
980
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300981 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200982
983 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
984 return obj;
985
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200986err_enable:
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300987 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200988 return ERR_PTR(err);
989}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200990
991/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300992 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200993 * @obj: target iommu
994 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300995static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200996{
Roel Kluinacf9d462010-01-08 10:29:05 -0800997 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200998 return;
999
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001000 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001001
Suman Anna2088ecb2014-10-22 17:22:19 -05001002 iommu_disable(obj);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001003 obj->iopgd = NULL;
1004
1005 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001006
1007 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
1008}
David Cohend594f1f2011-02-16 19:35:51 +00001009
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001010/*
1011 * OMAP Device MMU(IOMMU) detection
1012 */
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001013static int omap_iommu_probe(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001014{
1015 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001016 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001017 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001018 struct resource *res;
Kiran Padwal99cb9ae2014-10-30 11:59:47 +05301019 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Florian Vaussard3c927482014-02-28 14:42:36 -06001020 struct device_node *of = pdev->dev.of_node;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001021
Suman Annaf129b3d2014-02-28 14:42:32 -06001022 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001023 if (!obj)
1024 return -ENOMEM;
1025
Florian Vaussard3c927482014-02-28 14:42:36 -06001026 if (of) {
1027 obj->name = dev_name(&pdev->dev);
1028 obj->nr_tlb_entries = 32;
1029 err = of_property_read_u32(of, "ti,#tlb-entries",
1030 &obj->nr_tlb_entries);
1031 if (err && err != -EINVAL)
1032 return err;
1033 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
1034 return -EINVAL;
Suman Annab148d5f2014-02-28 14:42:37 -06001035 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
1036 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
Florian Vaussard3c927482014-02-28 14:42:36 -06001037 } else {
1038 obj->nr_tlb_entries = pdata->nr_tlb_entries;
1039 obj->name = pdata->name;
Florian Vaussard3c927482014-02-28 14:42:36 -06001040 }
Florian Vaussard3c927482014-02-28 14:42:36 -06001041
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001042 obj->dev = &pdev->dev;
1043 obj->ctx = (void *)obj + sizeof(*obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001044
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001045 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001046 spin_lock_init(&obj->page_table_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001047
1048 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001049 obj->regbase = devm_ioremap_resource(obj->dev, res);
1050 if (IS_ERR(obj->regbase))
1051 return PTR_ERR(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +00001052
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001053 irq = platform_get_irq(pdev, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001054 if (irq < 0)
1055 return -ENODEV;
1056
1057 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
1058 dev_name(obj->dev), obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001059 if (err < 0)
Suman Annaf129b3d2014-02-28 14:42:32 -06001060 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001061 platform_set_drvdata(pdev, obj);
1062
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001063 pm_runtime_irq_safe(obj->dev);
1064 pm_runtime_enable(obj->dev);
1065
Suman Anna61c75352014-10-22 17:22:30 -05001066 omap_iommu_debugfs_add(obj);
1067
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001068 dev_info(&pdev->dev, "%s registered\n", obj->name);
1069 return 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001070}
1071
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001072static int omap_iommu_remove(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001073{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001074 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001075
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001076 iopgtable_clear_entry_all(obj);
Suman Anna61c75352014-10-22 17:22:30 -05001077 omap_iommu_debugfs_remove(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001078
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001079 pm_runtime_disable(obj->dev);
1080
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001081 dev_info(&pdev->dev, "%s removed\n", obj->name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001082 return 0;
1083}
1084
Kiran Padwald943b0f2014-09-11 19:07:36 +05301085static const struct of_device_id omap_iommu_of_match[] = {
Florian Vaussard3c927482014-02-28 14:42:36 -06001086 { .compatible = "ti,omap2-iommu" },
1087 { .compatible = "ti,omap4-iommu" },
1088 { .compatible = "ti,dra7-iommu" },
1089 {},
1090};
Florian Vaussard3c927482014-02-28 14:42:36 -06001091
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001092static struct platform_driver omap_iommu_driver = {
1093 .probe = omap_iommu_probe,
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001094 .remove = omap_iommu_remove,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001095 .driver = {
1096 .name = "omap-iommu",
Florian Vaussard3c927482014-02-28 14:42:36 -06001097 .of_match_table = of_match_ptr(omap_iommu_of_match),
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001098 },
1099};
1100
1101static void iopte_cachep_ctor(void *iopte)
1102{
1103 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1104}
1105
Laurent Pinchart286f6002014-03-08 00:44:38 +01001106static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001107{
1108 memset(e, 0, sizeof(*e));
1109
1110 e->da = da;
1111 e->pa = pa;
Suman Annad760e3e2014-03-17 20:31:32 -05001112 e->valid = MMU_CAM_V;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001113 e->pgsz = pgsz;
1114 e->endian = MMU_RAM_ENDIAN_LITTLE;
1115 e->elsz = MMU_RAM_ELSZ_8;
1116 e->mixed = 0;
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001117
1118 return iopgsz_to_bytes(e->pgsz);
1119}
1120
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001121static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001122 phys_addr_t pa, size_t bytes, int prot)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001123{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001124 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001125 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001126 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001127 struct iotlb_entry e;
1128 int omap_pgsz;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001129 u32 ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001130
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001131 omap_pgsz = bytes_to_iopgsz(bytes);
1132 if (omap_pgsz < 0) {
1133 dev_err(dev, "invalid size to map: %d\n", bytes);
1134 return -EINVAL;
1135 }
1136
Joerg Roedel1d7f4492015-01-22 14:42:06 +01001137 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 +03001138
Laurent Pinchart286f6002014-03-08 00:44:38 +01001139 iotlb_init_entry(&e, da, pa, omap_pgsz);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001140
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001141 ret = omap_iopgtable_store_entry(oiommu, &e);
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001142 if (ret)
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001143 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001144
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001145 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001146}
1147
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001148static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1149 size_t size)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001150{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001151 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001152 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001153 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001154
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001155 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001156
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001157 return iopgtable_clear_entry(oiommu, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001158}
1159
1160static int
1161omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1162{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001163 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001164 struct omap_iommu *oiommu;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001165 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001166 int ret = 0;
1167
Suman Annae3f595b2014-09-04 17:27:29 -05001168 if (!arch_data || !arch_data->name) {
1169 dev_err(dev, "device doesn't have an associated iommu\n");
1170 return -EINVAL;
1171 }
1172
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001173 spin_lock(&omap_domain->lock);
1174
1175 /* only a single device is supported per domain for now */
1176 if (omap_domain->iommu_dev) {
1177 dev_err(dev, "iommu domain is already attached\n");
1178 ret = -EBUSY;
1179 goto out;
1180 }
1181
1182 /* get a handle to and enable the omap iommu */
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001183 oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001184 if (IS_ERR(oiommu)) {
1185 ret = PTR_ERR(oiommu);
1186 dev_err(dev, "can't get omap iommu: %d\n", ret);
1187 goto out;
1188 }
1189
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001190 omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001191 omap_domain->dev = dev;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -04001192 oiommu->domain = domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001193
1194out:
1195 spin_unlock(&omap_domain->lock);
1196 return ret;
1197}
1198
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001199static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
1200 struct device *dev)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001201{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001202 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001203 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001204
1205 /* only a single device is supported per domain for now */
1206 if (omap_domain->iommu_dev != oiommu) {
1207 dev_err(dev, "invalid iommu device\n");
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001208 return;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001209 }
1210
1211 iopgtable_clear_entry_all(oiommu);
1212
1213 omap_iommu_detach(oiommu);
1214
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001215 omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001216 omap_domain->dev = NULL;
Suman Annaf24d9ad2014-10-22 17:22:33 -05001217 oiommu->domain = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001218}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001219
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001220static void omap_iommu_detach_dev(struct iommu_domain *domain,
1221 struct device *dev)
1222{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001223 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001224
1225 spin_lock(&omap_domain->lock);
1226 _omap_iommu_detach_dev(omap_domain, dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001227 spin_unlock(&omap_domain->lock);
1228}
1229
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001230static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001231{
1232 struct omap_iommu_domain *omap_domain;
1233
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001234 if (type != IOMMU_DOMAIN_UNMANAGED)
1235 return NULL;
1236
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001237 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1238 if (!omap_domain) {
1239 pr_err("kzalloc failed\n");
1240 goto out;
1241 }
1242
1243 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1244 if (!omap_domain->pgtable) {
1245 pr_err("kzalloc failed\n");
1246 goto fail_nomem;
1247 }
1248
1249 /*
1250 * should never fail, but please keep this around to ensure
1251 * we keep the hardware happy
1252 */
1253 BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
1254
1255 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1256 spin_lock_init(&omap_domain->lock);
1257
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001258 omap_domain->domain.geometry.aperture_start = 0;
1259 omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
1260 omap_domain->domain.geometry.force_aperture = true;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001261
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001262 return &omap_domain->domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001263
1264fail_nomem:
1265 kfree(omap_domain);
1266out:
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001267 return NULL;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001268}
1269
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001270static void omap_iommu_domain_free(struct iommu_domain *domain)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001271{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001272 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001273
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001274 /*
1275 * An iommu device is still attached
1276 * (currently, only one device can be attached) ?
1277 */
1278 if (omap_domain->iommu_dev)
1279 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1280
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001281 kfree(omap_domain->pgtable);
1282 kfree(omap_domain);
1283}
1284
1285static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547ac2013-03-29 01:23:58 +05301286 dma_addr_t da)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001287{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001288 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001289 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001290 struct device *dev = oiommu->dev;
1291 u32 *pgd, *pte;
1292 phys_addr_t ret = 0;
1293
1294 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1295
1296 if (pte) {
1297 if (iopte_is_small(*pte))
1298 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1299 else if (iopte_is_large(*pte))
1300 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1301 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001302 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
1303 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001304 } else {
1305 if (iopgd_is_section(*pgd))
1306 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1307 else if (iopgd_is_super(*pgd))
1308 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1309 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001310 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
1311 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001312 }
1313
1314 return ret;
1315}
1316
Laurent Pinchart07a02032014-02-28 14:42:38 -06001317static int omap_iommu_add_device(struct device *dev)
1318{
1319 struct omap_iommu_arch_data *arch_data;
1320 struct device_node *np;
Suman Anna7d682772014-09-04 17:27:30 -05001321 struct platform_device *pdev;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001322
1323 /*
1324 * Allocate the archdata iommu structure for DT-based devices.
1325 *
1326 * TODO: Simplify this when removing non-DT support completely from the
1327 * IOMMU users.
1328 */
1329 if (!dev->of_node)
1330 return 0;
1331
1332 np = of_parse_phandle(dev->of_node, "iommus", 0);
1333 if (!np)
1334 return 0;
1335
Suman Anna7d682772014-09-04 17:27:30 -05001336 pdev = of_find_device_by_node(np);
1337 if (WARN_ON(!pdev)) {
1338 of_node_put(np);
1339 return -EINVAL;
1340 }
1341
Laurent Pinchart07a02032014-02-28 14:42:38 -06001342 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
1343 if (!arch_data) {
1344 of_node_put(np);
1345 return -ENOMEM;
1346 }
1347
Suman Anna7d682772014-09-04 17:27:30 -05001348 arch_data->name = kstrdup(dev_name(&pdev->dev), GFP_KERNEL);
Laurent Pinchart07a02032014-02-28 14:42:38 -06001349 dev->archdata.iommu = arch_data;
1350
1351 of_node_put(np);
1352
1353 return 0;
1354}
1355
1356static void omap_iommu_remove_device(struct device *dev)
1357{
1358 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1359
1360 if (!dev->of_node || !arch_data)
1361 return;
1362
1363 kfree(arch_data->name);
1364 kfree(arch_data);
1365}
1366
Thierry Redingb22f6432014-06-27 09:03:12 +02001367static const struct iommu_ops omap_iommu_ops = {
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001368 .domain_alloc = omap_iommu_domain_alloc,
1369 .domain_free = omap_iommu_domain_free,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001370 .attach_dev = omap_iommu_attach_dev,
1371 .detach_dev = omap_iommu_detach_dev,
1372 .map = omap_iommu_map,
1373 .unmap = omap_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07001374 .map_sg = default_iommu_map_sg,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001375 .iova_to_phys = omap_iommu_iova_to_phys,
Laurent Pinchart07a02032014-02-28 14:42:38 -06001376 .add_device = omap_iommu_add_device,
1377 .remove_device = omap_iommu_remove_device,
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +02001378 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001379};
1380
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001381static int __init omap_iommu_init(void)
1382{
1383 struct kmem_cache *p;
1384 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1385 size_t align = 1 << 10; /* L2 pagetable alignement */
Thierry Redingf938aab2015-02-06 11:44:06 +01001386 struct device_node *np;
1387
1388 np = of_find_matching_node(NULL, omap_iommu_of_match);
1389 if (!np)
1390 return 0;
1391
1392 of_node_put(np);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001393
1394 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1395 iopte_cachep_ctor);
1396 if (!p)
1397 return -ENOMEM;
1398 iopte_cachep = p;
1399
Joerg Roedela65bc642011-09-06 17:56:07 +02001400 bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001401
Suman Anna61c75352014-10-22 17:22:30 -05001402 omap_iommu_debugfs_init();
1403
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001404 return platform_driver_register(&omap_iommu_driver);
1405}
Ohad Ben-Cohen435792d2012-02-26 12:14:14 +02001406subsys_initcall(omap_iommu_init);
Suman Anna0cdbf722015-07-20 17:33:24 -05001407/* must be ready before omap3isp is probed */