blob: a22c33d6a486c9dea9bfa820feea18a034623eec [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;
Joerg Roedel8cf851e2015-03-26 13:43:09 +010062 struct iommu_domain domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030063};
64
Ido Yariv7bd9e252012-11-02 12:24:09 -070065#define MMU_LOCK_BASE_SHIFT 10
66#define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
67#define MMU_LOCK_BASE(x) \
68 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
69
70#define MMU_LOCK_VICT_SHIFT 4
71#define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
72#define MMU_LOCK_VICT(x) \
73 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
74
75struct iotlb_lock {
76 short base;
77 short vict;
78};
79
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020080static struct platform_driver omap_iommu_driver;
81static struct kmem_cache *iopte_cachep;
82
83/**
Joerg Roedel8cf851e2015-03-26 13:43:09 +010084 * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
85 * @dom: generic iommu domain handle
86 **/
87static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
88{
89 return container_of(dom, struct omap_iommu_domain, domain);
90}
91
92/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030093 * omap_iommu_save_ctx - Save registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020094 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020095 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020096void omap_iommu_save_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020097{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020098 struct omap_iommu *obj = dev_to_omap_iommu(dev);
Suman Annabd4396f2014-10-22 17:22:27 -050099 u32 *p = obj->ctx;
100 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200101
Suman Annabd4396f2014-10-22 17:22:27 -0500102 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
103 p[i] = iommu_read_reg(obj, i * sizeof(u32));
104 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
105 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200106}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300107EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200108
109/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300110 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200111 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200112 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200113void omap_iommu_restore_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200114{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200115 struct omap_iommu *obj = dev_to_omap_iommu(dev);
Suman Annabd4396f2014-10-22 17:22:27 -0500116 u32 *p = obj->ctx;
117 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200118
Suman Annabd4396f2014-10-22 17:22:27 -0500119 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
120 iommu_write_reg(obj, p[i], i * sizeof(u32));
121 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
122 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200123}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300124EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200125
Suman Annabd4396f2014-10-22 17:22:27 -0500126static void __iommu_set_twl(struct omap_iommu *obj, bool on)
127{
128 u32 l = iommu_read_reg(obj, MMU_CNTL);
129
130 if (on)
131 iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
132 else
133 iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
134
135 l &= ~MMU_CNTL_MASK;
136 if (on)
137 l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
138 else
139 l |= (MMU_CNTL_MMU_EN);
140
141 iommu_write_reg(obj, l, MMU_CNTL);
142}
143
144static int omap2_iommu_enable(struct omap_iommu *obj)
145{
146 u32 l, pa;
147
148 if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
149 return -EINVAL;
150
151 pa = virt_to_phys(obj->iopgd);
152 if (!IS_ALIGNED(pa, SZ_16K))
153 return -EINVAL;
154
155 l = iommu_read_reg(obj, MMU_REVISION);
156 dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
157 (l >> 4) & 0xf, l & 0xf);
158
159 iommu_write_reg(obj, pa, MMU_TTB);
160
161 if (obj->has_bus_err_back)
162 iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
163
164 __iommu_set_twl(obj, true);
165
166 return 0;
167}
168
169static void omap2_iommu_disable(struct omap_iommu *obj)
170{
171 u32 l = iommu_read_reg(obj, MMU_CNTL);
172
173 l &= ~MMU_CNTL_MASK;
174 iommu_write_reg(obj, l, MMU_CNTL);
175
176 dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
177}
178
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300179static int iommu_enable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200180{
181 int err;
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600182 struct platform_device *pdev = to_platform_device(obj->dev);
Kiran Padwal99cb9ae2014-10-30 11:59:47 +0530183 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200184
Florian Vaussard90e569c2014-02-28 14:42:34 -0600185 if (pdata && pdata->deassert_reset) {
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600186 err = pdata->deassert_reset(pdev, pdata->reset_name);
187 if (err) {
188 dev_err(obj->dev, "deassert_reset failed: %d\n", err);
189 return err;
190 }
191 }
192
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600193 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200194
Suman Annabd4396f2014-10-22 17:22:27 -0500195 err = omap2_iommu_enable(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200196
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200197 return err;
198}
199
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300200static void iommu_disable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200201{
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600202 struct platform_device *pdev = to_platform_device(obj->dev);
Kiran Padwal99cb9ae2014-10-30 11:59:47 +0530203 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600204
Suman Annabd4396f2014-10-22 17:22:27 -0500205 omap2_iommu_disable(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200206
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600207 pm_runtime_put_sync(obj->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600208
Florian Vaussard90e569c2014-02-28 14:42:34 -0600209 if (pdata && pdata->assert_reset)
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600210 pdata->assert_reset(pdev, pdata->reset_name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200211}
212
213/*
214 * TLB operations
215 */
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200216static inline int iotlb_cr_valid(struct cr_regs *cr)
217{
218 if (!cr)
219 return -EINVAL;
220
Suman Annabd4396f2014-10-22 17:22:27 -0500221 return cr->cam & MMU_CAM_V;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200222}
223
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300224static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200225{
Suman Annabd4396f2014-10-22 17:22:27 -0500226 u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
227 u32 mask = get_cam_va_mask(cr->cam & page_size);
228
229 return cr->cam & mask;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200230}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200231
232static u32 get_iopte_attr(struct iotlb_entry *e)
233{
Suman Annabd4396f2014-10-22 17:22:27 -0500234 u32 attr;
235
236 attr = e->mixed << 5;
237 attr |= e->endian;
238 attr |= e->elsz >> 3;
239 attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
240 (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
241 return attr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200242}
243
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300244static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200245{
Suman Annabd4396f2014-10-22 17:22:27 -0500246 u32 status, fault_addr;
247
248 status = iommu_read_reg(obj, MMU_IRQSTATUS);
249 status &= MMU_IRQ_MASK;
250 if (!status) {
251 *da = 0;
252 return 0;
253 }
254
255 fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
256 *da = fault_addr;
257
258 iommu_write_reg(obj, status, MMU_IRQSTATUS);
259
260 return status;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200261}
262
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300263static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200264{
265 u32 val;
266
267 val = iommu_read_reg(obj, MMU_LOCK);
268
269 l->base = MMU_LOCK_BASE(val);
270 l->vict = MMU_LOCK_VICT(val);
271
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200272}
273
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300274static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200275{
276 u32 val;
277
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200278 val = (l->base << MMU_LOCK_BASE_SHIFT);
279 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
280
281 iommu_write_reg(obj, val, MMU_LOCK);
282}
283
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300284static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200285{
Suman Annabd4396f2014-10-22 17:22:27 -0500286 cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
287 cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200288}
289
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300290static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200291{
Suman Annabd4396f2014-10-22 17:22:27 -0500292 iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
293 iommu_write_reg(obj, cr->ram, MMU_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200294
295 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
296 iommu_write_reg(obj, 1, MMU_LD_TLB);
297}
298
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000299/* only used in iotlb iteration for-loop */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300300static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000301{
302 struct cr_regs cr;
303 struct iotlb_lock l;
304
305 iotlb_lock_get(obj, &l);
306 l.vict = n;
307 iotlb_lock_set(obj, &l);
308 iotlb_read_cr(obj, &cr);
309
310 return cr;
311}
312
Suman Annabd4396f2014-10-22 17:22:27 -0500313#ifdef PREFETCH_IOTLB
314static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
315 struct iotlb_entry *e)
316{
317 struct cr_regs *cr;
318
319 if (!e)
320 return NULL;
321
322 if (e->da & ~(get_cam_va_mask(e->pgsz))) {
323 dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
324 e->da);
325 return ERR_PTR(-EINVAL);
326 }
327
328 cr = kmalloc(sizeof(*cr), GFP_KERNEL);
329 if (!cr)
330 return ERR_PTR(-ENOMEM);
331
332 cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
333 cr->ram = e->pa | e->endian | e->elsz | e->mixed;
334
335 return cr;
336}
337
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200338/**
339 * load_iotlb_entry - Set an iommu tlb entry
340 * @obj: target iommu
341 * @e: an iommu tlb entry info
342 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300343static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200344{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200345 int err = 0;
346 struct iotlb_lock l;
347 struct cr_regs *cr;
348
349 if (!obj || !obj->nr_tlb_entries || !e)
350 return -EINVAL;
351
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600352 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200353
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000354 iotlb_lock_get(obj, &l);
355 if (l.base == obj->nr_tlb_entries) {
356 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200357 err = -EBUSY;
358 goto out;
359 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000360 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000361 int i;
362 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000363
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000364 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000365 if (!iotlb_cr_valid(&tmp))
366 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000367
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000368 if (i == obj->nr_tlb_entries) {
369 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
370 err = -EBUSY;
371 goto out;
372 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000373
374 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000375 } else {
376 l.vict = l.base;
377 iotlb_lock_set(obj, &l);
378 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200379
380 cr = iotlb_alloc_cr(obj, e);
381 if (IS_ERR(cr)) {
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600382 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200383 return PTR_ERR(cr);
384 }
385
386 iotlb_load_cr(obj, cr);
387 kfree(cr);
388
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000389 if (e->prsvd)
390 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200391 /* increment victim for next tlb load */
392 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000393 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200394 iotlb_lock_set(obj, &l);
395out:
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600396 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200397 return err;
398}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200399
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300400#else /* !PREFETCH_IOTLB */
401
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300402static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300403{
404 return 0;
405}
406
407#endif /* !PREFETCH_IOTLB */
408
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300409static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300410{
411 return load_iotlb_entry(obj, e);
412}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200413
414/**
415 * flush_iotlb_page - Clear an iommu tlb entry
416 * @obj: target iommu
417 * @da: iommu device virtual address
418 *
419 * Clear an iommu tlb entry which includes 'da' address.
420 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300421static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200422{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200423 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000424 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200425
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600426 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200427
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000428 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200429 u32 start;
430 size_t bytes;
431
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200432 if (!iotlb_cr_valid(&cr))
433 continue;
434
435 start = iotlb_cr_to_virt(&cr);
436 bytes = iopgsz_to_bytes(cr.cam & 3);
437
438 if ((start <= da) && (da < start + bytes)) {
439 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
440 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000441 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200442 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
Laurent Pinchartf7129a02014-03-07 23:47:03 +0100443 break;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200444 }
445 }
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600446 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200447
448 if (i == obj->nr_tlb_entries)
449 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
450}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200451
452/**
453 * flush_iotlb_all - Clear all iommu tlb entries
454 * @obj: target iommu
455 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300456static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200457{
458 struct iotlb_lock l;
459
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600460 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200461
462 l.base = 0;
463 l.vict = 0;
464 iotlb_lock_set(obj, &l);
465
466 iommu_write_reg(obj, 1, MMU_GFLUSH);
467
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600468 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200469}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200470
Suman Anna61c75352014-10-22 17:22:30 -0500471#ifdef CONFIG_OMAP_IOMMU_DEBUG
Kanigeri, Hariddfa9752010-05-24 02:01:51 +0000472
Suman Annabd4396f2014-10-22 17:22:27 -0500473#define pr_reg(name) \
474 do { \
475 ssize_t bytes; \
476 const char *str = "%20s: %08x\n"; \
477 const int maxcol = 32; \
478 bytes = snprintf(p, maxcol, str, __stringify(name), \
479 iommu_read_reg(obj, MMU_##name)); \
480 p += bytes; \
481 len -= bytes; \
482 if (len < maxcol) \
483 goto out; \
484 } while (0)
485
486static ssize_t
487omap2_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len)
488{
489 char *p = buf;
490
491 pr_reg(REVISION);
492 pr_reg(IRQSTATUS);
493 pr_reg(IRQENABLE);
494 pr_reg(WALKING_ST);
495 pr_reg(CNTL);
496 pr_reg(FAULT_AD);
497 pr_reg(TTB);
498 pr_reg(LOCK);
499 pr_reg(LD_TLB);
500 pr_reg(CAM);
501 pr_reg(RAM);
502 pr_reg(GFLUSH);
503 pr_reg(FLUSH_ENTRY);
504 pr_reg(READ_CAM);
505 pr_reg(READ_RAM);
506 pr_reg(EMU_FAULT_AD);
507out:
508 return p - buf;
509}
510
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300511ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200512{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200513 if (!obj || !buf)
514 return -EINVAL;
515
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600516 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200517
Suman Annabd4396f2014-10-22 17:22:27 -0500518 bytes = omap2_iommu_dump_ctx(obj, buf, bytes);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200519
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600520 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200521
522 return bytes;
523}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200524
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300525static int
526__dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200527{
528 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000529 struct iotlb_lock saved;
530 struct cr_regs tmp;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200531 struct cr_regs *p = crs;
532
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600533 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200534 iotlb_lock_get(obj, &saved);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200535
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000536 for_each_iotlb_cr(obj, num, i, tmp) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200537 if (!iotlb_cr_valid(&tmp))
538 continue;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200539 *p++ = tmp;
540 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000541
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200542 iotlb_lock_set(obj, &saved);
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600543 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200544
545 return p - crs;
546}
547
548/**
Suman Annabd4396f2014-10-22 17:22:27 -0500549 * iotlb_dump_cr - Dump an iommu tlb entry into buf
550 * @obj: target iommu
551 * @cr: contents of cam and ram register
552 * @buf: output buffer
553 **/
554static ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
555 char *buf)
556{
557 char *p = buf;
558
559 /* FIXME: Need more detail analysis of cam/ram */
560 p += sprintf(p, "%08x %08x %01x\n", cr->cam, cr->ram,
561 (cr->cam & MMU_CAM_P) ? 1 : 0);
562
563 return p - buf;
564}
565
566/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300567 * omap_dump_tlb_entries - dump cr arrays to given buffer
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200568 * @obj: target iommu
569 * @buf: output buffer
570 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300571size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200572{
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700573 int i, num;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200574 struct cr_regs *cr;
575 char *p = buf;
576
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700577 num = bytes / sizeof(*cr);
578 num = min(obj->nr_tlb_entries, num);
579
580 cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200581 if (!cr)
582 return 0;
583
Hiroshi DOYU14e0e672009-08-28 10:54:41 -0700584 num = __dump_tlb_entries(obj, cr, num);
585 for (i = 0; i < num; i++)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200586 p += iotlb_dump_cr(obj, cr + i, p);
587 kfree(cr);
588
589 return p - buf;
590}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200591
Suman Anna61c75352014-10-22 17:22:30 -0500592#endif /* CONFIG_OMAP_IOMMU_DEBUG */
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200593
594/*
595 * H/W pagetable operations
596 */
597static void flush_iopgd_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_pgd"
602 : : "r" (first));
603 first += L1_CACHE_BYTES / sizeof(*first);
604 } while (first <= last);
605}
606
607static void flush_iopte_range(u32 *first, u32 *last)
608{
609 /* FIXME: L2 cache should be taken care of if it exists */
610 do {
611 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
612 : : "r" (first));
613 first += L1_CACHE_BYTES / sizeof(*first);
614 } while (first <= last);
615}
616
617static void iopte_free(u32 *iopte)
618{
619 /* Note: freed iopte's must be clean ready for re-use */
Zhouyi Zhoue28045a2014-03-05 18:20:19 +0800620 if (iopte)
621 kmem_cache_free(iopte_cachep, iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200622}
623
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300624static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200625{
626 u32 *iopte;
627
628 /* a table has already existed */
629 if (*iopgd)
630 goto pte_ready;
631
632 /*
633 * do the allocation outside the page table lock
634 */
635 spin_unlock(&obj->page_table_lock);
636 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
637 spin_lock(&obj->page_table_lock);
638
639 if (!*iopgd) {
640 if (!iopte)
641 return ERR_PTR(-ENOMEM);
642
643 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
644 flush_iopgd_range(iopgd, iopgd);
645
646 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
647 } else {
648 /* We raced, free the reduniovant table */
649 iopte_free(iopte);
650 }
651
652pte_ready:
653 iopte = iopte_offset(iopgd, da);
654
655 dev_vdbg(obj->dev,
656 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
657 __func__, da, iopgd, *iopgd, iopte, *iopte);
658
659 return iopte;
660}
661
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300662static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200663{
664 u32 *iopgd = iopgd_offset(obj, da);
665
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300666 if ((da | pa) & ~IOSECTION_MASK) {
667 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
668 __func__, da, pa, IOSECTION_SIZE);
669 return -EINVAL;
670 }
671
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200672 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
673 flush_iopgd_range(iopgd, iopgd);
674 return 0;
675}
676
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300677static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200678{
679 u32 *iopgd = iopgd_offset(obj, da);
680 int i;
681
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300682 if ((da | pa) & ~IOSUPER_MASK) {
683 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
684 __func__, da, pa, IOSUPER_SIZE);
685 return -EINVAL;
686 }
687
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200688 for (i = 0; i < 16; i++)
689 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
690 flush_iopgd_range(iopgd, iopgd + 15);
691 return 0;
692}
693
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300694static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200695{
696 u32 *iopgd = iopgd_offset(obj, da);
697 u32 *iopte = iopte_alloc(obj, iopgd, da);
698
699 if (IS_ERR(iopte))
700 return PTR_ERR(iopte);
701
702 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
703 flush_iopte_range(iopte, iopte);
704
705 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
706 __func__, da, pa, iopte, *iopte);
707
708 return 0;
709}
710
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300711static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200712{
713 u32 *iopgd = iopgd_offset(obj, da);
714 u32 *iopte = iopte_alloc(obj, iopgd, da);
715 int i;
716
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300717 if ((da | pa) & ~IOLARGE_MASK) {
718 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
719 __func__, da, pa, IOLARGE_SIZE);
720 return -EINVAL;
721 }
722
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200723 if (IS_ERR(iopte))
724 return PTR_ERR(iopte);
725
726 for (i = 0; i < 16; i++)
727 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
728 flush_iopte_range(iopte, iopte + 15);
729 return 0;
730}
731
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300732static int
733iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200734{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300735 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200736 u32 prot;
737 int err;
738
739 if (!obj || !e)
740 return -EINVAL;
741
742 switch (e->pgsz) {
743 case MMU_CAM_PGSZ_16M:
744 fn = iopgd_alloc_super;
745 break;
746 case MMU_CAM_PGSZ_1M:
747 fn = iopgd_alloc_section;
748 break;
749 case MMU_CAM_PGSZ_64K:
750 fn = iopte_alloc_large;
751 break;
752 case MMU_CAM_PGSZ_4K:
753 fn = iopte_alloc_page;
754 break;
755 default:
756 fn = NULL;
757 BUG();
758 break;
759 }
760
761 prot = get_iopte_attr(e);
762
763 spin_lock(&obj->page_table_lock);
764 err = fn(obj, e->da, e->pa, prot);
765 spin_unlock(&obj->page_table_lock);
766
767 return err;
768}
769
770/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300771 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200772 * @obj: target iommu
773 * @e: an iommu tlb entry info
774 **/
Suman Anna4899a562014-10-22 17:22:32 -0500775static int
776omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200777{
778 int err;
779
780 flush_iotlb_page(obj, e->da);
781 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200782 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300783 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200784 return err;
785}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200786
787/**
788 * iopgtable_lookup_entry - Lookup an iommu pte entry
789 * @obj: target iommu
790 * @da: iommu device virtual address
791 * @ppgd: iommu pgd entry pointer to be returned
792 * @ppte: iommu pte entry pointer to be returned
793 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300794static void
795iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200796{
797 u32 *iopgd, *iopte = NULL;
798
799 iopgd = iopgd_offset(obj, da);
800 if (!*iopgd)
801 goto out;
802
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300803 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200804 iopte = iopte_offset(iopgd, da);
805out:
806 *ppgd = iopgd;
807 *ppte = iopte;
808}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200809
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300810static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200811{
812 size_t bytes;
813 u32 *iopgd = iopgd_offset(obj, da);
814 int nent = 1;
815
816 if (!*iopgd)
817 return 0;
818
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300819 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200820 int i;
821 u32 *iopte = iopte_offset(iopgd, da);
822
823 bytes = IOPTE_SIZE;
824 if (*iopte & IOPTE_LARGE) {
825 nent *= 16;
826 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800827 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200828 }
829 bytes *= nent;
830 memset(iopte, 0, nent * sizeof(*iopte));
831 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
832
833 /*
834 * do table walk to check if this table is necessary or not
835 */
836 iopte = iopte_offset(iopgd, 0);
837 for (i = 0; i < PTRS_PER_IOPTE; i++)
838 if (iopte[i])
839 goto out;
840
841 iopte_free(iopte);
842 nent = 1; /* for the next L1 entry */
843 } else {
844 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700845 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200846 nent *= 16;
847 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800848 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200849 }
850 bytes *= nent;
851 }
852 memset(iopgd, 0, nent * sizeof(*iopgd));
853 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
854out:
855 return bytes;
856}
857
858/**
859 * iopgtable_clear_entry - Remove an iommu pte entry
860 * @obj: target iommu
861 * @da: iommu device virtual address
862 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300863static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200864{
865 size_t bytes;
866
867 spin_lock(&obj->page_table_lock);
868
869 bytes = iopgtable_clear_entry_core(obj, da);
870 flush_iotlb_page(obj, da);
871
872 spin_unlock(&obj->page_table_lock);
873
874 return bytes;
875}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200876
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300877static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200878{
879 int i;
880
881 spin_lock(&obj->page_table_lock);
882
883 for (i = 0; i < PTRS_PER_IOPGD; i++) {
884 u32 da;
885 u32 *iopgd;
886
887 da = i << IOPGD_SHIFT;
888 iopgd = iopgd_offset(obj, da);
889
890 if (!*iopgd)
891 continue;
892
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300893 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200894 iopte_free(iopte_offset(iopgd, 0));
895
896 *iopgd = 0;
897 flush_iopgd_range(iopgd, iopgd);
898 }
899
900 flush_iotlb_all(obj);
901
902 spin_unlock(&obj->page_table_lock);
903}
904
905/*
906 * Device IOMMU generic operations
907 */
908static irqreturn_t iommu_fault_handler(int irq, void *data)
909{
David Cohend594f1f2011-02-16 19:35:51 +0000910 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200911 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300912 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400913 struct iommu_domain *domain = obj->domain;
Joerg Roedel8cf851e2015-03-26 13:43:09 +0100914 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200915
Suman Anna2088ecb2014-10-22 17:22:19 -0500916 if (!omap_domain->iommu_dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200917 return IRQ_NONE;
918
David Cohend594f1f2011-02-16 19:35:51 +0000919 errs = iommu_report_fault(obj, &da);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200920 if (errs == 0)
921 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000922
923 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400924 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200925 return IRQ_HANDLED;
926
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000927 iommu_disable(obj);
928
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200929 iopgd = iopgd_offset(obj, da);
930
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300931 if (!iopgd_is_table(*iopgd)) {
Suman Annab6c2e092013-05-30 18:10:59 -0500932 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
933 obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200934 return IRQ_NONE;
935 }
936
937 iopte = iopte_offset(iopgd, da);
938
Suman Annab6c2e092013-05-30 18:10:59 -0500939 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
940 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200941
942 return IRQ_NONE;
943}
944
945static int device_match_by_alias(struct device *dev, void *data)
946{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300947 struct omap_iommu *obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200948 const char *name = data;
949
950 pr_debug("%s: %s %s\n", __func__, obj->name, name);
951
952 return strcmp(obj->name, name) == 0;
953}
954
955/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300956 * omap_iommu_attach() - attach iommu device to an iommu domain
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200957 * @name: name of target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300958 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200959 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200960static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200961{
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600962 int err;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200963 struct device *dev;
964 struct omap_iommu *obj;
965
966 dev = driver_find_device(&omap_iommu_driver.driver, NULL,
967 (void *)name,
968 device_match_by_alias);
969 if (!dev)
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600970 return ERR_PTR(-ENODEV);
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200971
972 obj = to_iommu(dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200973
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300974 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200975
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300976 obj->iopgd = iopgd;
977 err = iommu_enable(obj);
978 if (err)
979 goto err_enable;
980 flush_iotlb_all(obj);
981
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300982 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200983
984 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
985 return obj;
986
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200987err_enable:
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300988 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200989 return ERR_PTR(err);
990}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200991
992/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300993 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200994 * @obj: target iommu
995 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300996static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200997{
Roel Kluinacf9d462010-01-08 10:29:05 -0800998 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200999 return;
1000
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001001 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001002
Suman Anna2088ecb2014-10-22 17:22:19 -05001003 iommu_disable(obj);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001004 obj->iopgd = NULL;
1005
1006 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001007
1008 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
1009}
David Cohend594f1f2011-02-16 19:35:51 +00001010
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001011/*
1012 * OMAP Device MMU(IOMMU) detection
1013 */
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001014static int omap_iommu_probe(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001015{
1016 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001017 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001018 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001019 struct resource *res;
Kiran Padwal99cb9ae2014-10-30 11:59:47 +05301020 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Florian Vaussard3c927482014-02-28 14:42:36 -06001021 struct device_node *of = pdev->dev.of_node;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001022
Suman Annaf129b3d2014-02-28 14:42:32 -06001023 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001024 if (!obj)
1025 return -ENOMEM;
1026
Florian Vaussard3c927482014-02-28 14:42:36 -06001027 if (of) {
1028 obj->name = dev_name(&pdev->dev);
1029 obj->nr_tlb_entries = 32;
1030 err = of_property_read_u32(of, "ti,#tlb-entries",
1031 &obj->nr_tlb_entries);
1032 if (err && err != -EINVAL)
1033 return err;
1034 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
1035 return -EINVAL;
Suman Annab148d5f2014-02-28 14:42:37 -06001036 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
1037 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
Florian Vaussard3c927482014-02-28 14:42:36 -06001038 } else {
1039 obj->nr_tlb_entries = pdata->nr_tlb_entries;
1040 obj->name = pdata->name;
Florian Vaussard3c927482014-02-28 14:42:36 -06001041 }
Florian Vaussard3c927482014-02-28 14:42:36 -06001042
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001043 obj->dev = &pdev->dev;
1044 obj->ctx = (void *)obj + sizeof(*obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001045
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001046 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001047 spin_lock_init(&obj->page_table_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001048
1049 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001050 obj->regbase = devm_ioremap_resource(obj->dev, res);
1051 if (IS_ERR(obj->regbase))
1052 return PTR_ERR(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +00001053
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001054 irq = platform_get_irq(pdev, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001055 if (irq < 0)
1056 return -ENODEV;
1057
1058 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
1059 dev_name(obj->dev), obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001060 if (err < 0)
Suman Annaf129b3d2014-02-28 14:42:32 -06001061 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001062 platform_set_drvdata(pdev, obj);
1063
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001064 pm_runtime_irq_safe(obj->dev);
1065 pm_runtime_enable(obj->dev);
1066
Suman Anna61c75352014-10-22 17:22:30 -05001067 omap_iommu_debugfs_add(obj);
1068
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001069 dev_info(&pdev->dev, "%s registered\n", obj->name);
1070 return 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001071}
1072
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001073static int omap_iommu_remove(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001074{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001075 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001076
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001077 iopgtable_clear_entry_all(obj);
Suman Anna61c75352014-10-22 17:22:30 -05001078 omap_iommu_debugfs_remove(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001079
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001080 pm_runtime_disable(obj->dev);
1081
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001082 dev_info(&pdev->dev, "%s removed\n", obj->name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001083 return 0;
1084}
1085
Kiran Padwald943b0f2014-09-11 19:07:36 +05301086static const struct of_device_id omap_iommu_of_match[] = {
Florian Vaussard3c927482014-02-28 14:42:36 -06001087 { .compatible = "ti,omap2-iommu" },
1088 { .compatible = "ti,omap4-iommu" },
1089 { .compatible = "ti,dra7-iommu" },
1090 {},
1091};
1092MODULE_DEVICE_TABLE(of, omap_iommu_of_match);
1093
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001094static struct platform_driver omap_iommu_driver = {
1095 .probe = omap_iommu_probe,
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001096 .remove = omap_iommu_remove,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001097 .driver = {
1098 .name = "omap-iommu",
Florian Vaussard3c927482014-02-28 14:42:36 -06001099 .of_match_table = of_match_ptr(omap_iommu_of_match),
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001100 },
1101};
1102
1103static void iopte_cachep_ctor(void *iopte)
1104{
1105 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1106}
1107
Laurent Pinchart286f6002014-03-08 00:44:38 +01001108static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001109{
1110 memset(e, 0, sizeof(*e));
1111
1112 e->da = da;
1113 e->pa = pa;
Suman Annad760e3e2014-03-17 20:31:32 -05001114 e->valid = MMU_CAM_V;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001115 e->pgsz = pgsz;
1116 e->endian = MMU_RAM_ENDIAN_LITTLE;
1117 e->elsz = MMU_RAM_ELSZ_8;
1118 e->mixed = 0;
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001119
1120 return iopgsz_to_bytes(e->pgsz);
1121}
1122
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001123static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001124 phys_addr_t pa, size_t bytes, int prot)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001125{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001126 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001127 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001128 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001129 struct iotlb_entry e;
1130 int omap_pgsz;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001131 u32 ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001132
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001133 omap_pgsz = bytes_to_iopgsz(bytes);
1134 if (omap_pgsz < 0) {
1135 dev_err(dev, "invalid size to map: %d\n", bytes);
1136 return -EINVAL;
1137 }
1138
Joerg Roedel1d7f4492015-01-22 14:42:06 +01001139 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 +03001140
Laurent Pinchart286f6002014-03-08 00:44:38 +01001141 iotlb_init_entry(&e, da, pa, omap_pgsz);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001142
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001143 ret = omap_iopgtable_store_entry(oiommu, &e);
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001144 if (ret)
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001145 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001146
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001147 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001148}
1149
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001150static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
1151 size_t size)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001152{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001153 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001154 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001155 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001156
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001157 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001158
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001159 return iopgtable_clear_entry(oiommu, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001160}
1161
1162static int
1163omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1164{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001165 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001166 struct omap_iommu *oiommu;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001167 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001168 int ret = 0;
1169
Suman Annae3f595b2014-09-04 17:27:29 -05001170 if (!arch_data || !arch_data->name) {
1171 dev_err(dev, "device doesn't have an associated iommu\n");
1172 return -EINVAL;
1173 }
1174
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001175 spin_lock(&omap_domain->lock);
1176
1177 /* only a single device is supported per domain for now */
1178 if (omap_domain->iommu_dev) {
1179 dev_err(dev, "iommu domain is already attached\n");
1180 ret = -EBUSY;
1181 goto out;
1182 }
1183
1184 /* get a handle to and enable the omap iommu */
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001185 oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001186 if (IS_ERR(oiommu)) {
1187 ret = PTR_ERR(oiommu);
1188 dev_err(dev, "can't get omap iommu: %d\n", ret);
1189 goto out;
1190 }
1191
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001192 omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001193 omap_domain->dev = dev;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -04001194 oiommu->domain = domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001195
1196out:
1197 spin_unlock(&omap_domain->lock);
1198 return ret;
1199}
1200
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001201static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
1202 struct device *dev)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001203{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001204 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001205 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001206
1207 /* only a single device is supported per domain for now */
1208 if (omap_domain->iommu_dev != oiommu) {
1209 dev_err(dev, "invalid iommu device\n");
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001210 return;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001211 }
1212
1213 iopgtable_clear_entry_all(oiommu);
1214
1215 omap_iommu_detach(oiommu);
1216
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001217 omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001218 omap_domain->dev = NULL;
Suman Annaf24d9ad2014-10-22 17:22:33 -05001219 oiommu->domain = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001220}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001221
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001222static void omap_iommu_detach_dev(struct iommu_domain *domain,
1223 struct device *dev)
1224{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001225 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001226
1227 spin_lock(&omap_domain->lock);
1228 _omap_iommu_detach_dev(omap_domain, dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001229 spin_unlock(&omap_domain->lock);
1230}
1231
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001232static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001233{
1234 struct omap_iommu_domain *omap_domain;
1235
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001236 if (type != IOMMU_DOMAIN_UNMANAGED)
1237 return NULL;
1238
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001239 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
1240 if (!omap_domain) {
1241 pr_err("kzalloc failed\n");
1242 goto out;
1243 }
1244
1245 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
1246 if (!omap_domain->pgtable) {
1247 pr_err("kzalloc failed\n");
1248 goto fail_nomem;
1249 }
1250
1251 /*
1252 * should never fail, but please keep this around to ensure
1253 * we keep the hardware happy
1254 */
1255 BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
1256
1257 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1258 spin_lock_init(&omap_domain->lock);
1259
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001260 omap_domain->domain.geometry.aperture_start = 0;
1261 omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
1262 omap_domain->domain.geometry.force_aperture = true;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001263
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001264 return &omap_domain->domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001265
1266fail_nomem:
1267 kfree(omap_domain);
1268out:
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001269 return NULL;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001270}
1271
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001272static void omap_iommu_domain_free(struct iommu_domain *domain)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001273{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001274 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001275
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001276 /*
1277 * An iommu device is still attached
1278 * (currently, only one device can be attached) ?
1279 */
1280 if (omap_domain->iommu_dev)
1281 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1282
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001283 kfree(omap_domain->pgtable);
1284 kfree(omap_domain);
1285}
1286
1287static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547ac2013-03-29 01:23:58 +05301288 dma_addr_t da)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001289{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001290 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001291 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001292 struct device *dev = oiommu->dev;
1293 u32 *pgd, *pte;
1294 phys_addr_t ret = 0;
1295
1296 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1297
1298 if (pte) {
1299 if (iopte_is_small(*pte))
1300 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1301 else if (iopte_is_large(*pte))
1302 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1303 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001304 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
1305 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001306 } else {
1307 if (iopgd_is_section(*pgd))
1308 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1309 else if (iopgd_is_super(*pgd))
1310 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1311 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001312 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
1313 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001314 }
1315
1316 return ret;
1317}
1318
Laurent Pinchart07a02032014-02-28 14:42:38 -06001319static int omap_iommu_add_device(struct device *dev)
1320{
1321 struct omap_iommu_arch_data *arch_data;
1322 struct device_node *np;
Suman Anna7d682772014-09-04 17:27:30 -05001323 struct platform_device *pdev;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001324
1325 /*
1326 * Allocate the archdata iommu structure for DT-based devices.
1327 *
1328 * TODO: Simplify this when removing non-DT support completely from the
1329 * IOMMU users.
1330 */
1331 if (!dev->of_node)
1332 return 0;
1333
1334 np = of_parse_phandle(dev->of_node, "iommus", 0);
1335 if (!np)
1336 return 0;
1337
Suman Anna7d682772014-09-04 17:27:30 -05001338 pdev = of_find_device_by_node(np);
1339 if (WARN_ON(!pdev)) {
1340 of_node_put(np);
1341 return -EINVAL;
1342 }
1343
Laurent Pinchart07a02032014-02-28 14:42:38 -06001344 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
1345 if (!arch_data) {
1346 of_node_put(np);
1347 return -ENOMEM;
1348 }
1349
Suman Anna7d682772014-09-04 17:27:30 -05001350 arch_data->name = kstrdup(dev_name(&pdev->dev), GFP_KERNEL);
Laurent Pinchart07a02032014-02-28 14:42:38 -06001351 dev->archdata.iommu = arch_data;
1352
1353 of_node_put(np);
1354
1355 return 0;
1356}
1357
1358static void omap_iommu_remove_device(struct device *dev)
1359{
1360 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1361
1362 if (!dev->of_node || !arch_data)
1363 return;
1364
1365 kfree(arch_data->name);
1366 kfree(arch_data);
1367}
1368
Thierry Redingb22f6432014-06-27 09:03:12 +02001369static const struct iommu_ops omap_iommu_ops = {
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001370 .domain_alloc = omap_iommu_domain_alloc,
1371 .domain_free = omap_iommu_domain_free,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001372 .attach_dev = omap_iommu_attach_dev,
1373 .detach_dev = omap_iommu_detach_dev,
1374 .map = omap_iommu_map,
1375 .unmap = omap_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07001376 .map_sg = default_iommu_map_sg,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001377 .iova_to_phys = omap_iommu_iova_to_phys,
Laurent Pinchart07a02032014-02-28 14:42:38 -06001378 .add_device = omap_iommu_add_device,
1379 .remove_device = omap_iommu_remove_device,
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +02001380 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001381};
1382
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001383static int __init omap_iommu_init(void)
1384{
1385 struct kmem_cache *p;
1386 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1387 size_t align = 1 << 10; /* L2 pagetable alignement */
Thierry Redingf938aab2015-02-06 11:44:06 +01001388 struct device_node *np;
1389
1390 np = of_find_matching_node(NULL, omap_iommu_of_match);
1391 if (!np)
1392 return 0;
1393
1394 of_node_put(np);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001395
1396 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1397 iopte_cachep_ctor);
1398 if (!p)
1399 return -ENOMEM;
1400 iopte_cachep = p;
1401
Joerg Roedela65bc642011-09-06 17:56:07 +02001402 bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001403
Suman Anna61c75352014-10-22 17:22:30 -05001404 omap_iommu_debugfs_init();
1405
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001406 return platform_driver_register(&omap_iommu_driver);
1407}
Ohad Ben-Cohen435792d2012-02-26 12:14:14 +02001408/* must be ready before omap3isp is probed */
1409subsys_initcall(omap_iommu_init);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001410
1411static void __exit omap_iommu_exit(void)
1412{
1413 kmem_cache_destroy(iopte_cachep);
1414
1415 platform_driver_unregister(&omap_iommu_driver);
Suman Anna61c75352014-10-22 17:22:30 -05001416
1417 omap_iommu_debugfs_exit();
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001418}
1419module_exit(omap_iommu_exit);
1420
1421MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
1422MODULE_ALIAS("platform:omap-iommu");
1423MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
1424MODULE_LICENSE("GPL v2");