blob: bd67e1b2c64eadf25f5b10d120cf1214b120b54f [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
Josue Albarranbfee0cf2017-07-28 15:49:14 -050014#include <linux/dma-mapping.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020015#include <linux/err.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>
Suman Anna3ca92992015-10-02 18:02:44 -050030#include <linux/regmap.h>
31#include <linux/mfd/syscon.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020032
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
Joerg Roedel01611fe2017-04-12 00:21:30 -050038static const struct iommu_ops omap_iommu_ops;
39
Suman Anna5acc97d2014-03-17 20:31:34 -050040#define to_iommu(dev) \
41 ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
42
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +020043/* bitmap of the page sizes currently supported */
44#define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
45
Ido Yariv7bd9e252012-11-02 12:24:09 -070046#define MMU_LOCK_BASE_SHIFT 10
47#define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
48#define MMU_LOCK_BASE(x) \
49 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
50
51#define MMU_LOCK_VICT_SHIFT 4
52#define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
53#define MMU_LOCK_VICT(x) \
54 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
55
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020056static struct platform_driver omap_iommu_driver;
57static struct kmem_cache *iopte_cachep;
58
59/**
Joerg Roedel8cf851e2015-03-26 13:43:09 +010060 * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
61 * @dom: generic iommu domain handle
62 **/
63static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
64{
65 return container_of(dom, struct omap_iommu_domain, domain);
66}
67
68/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030069 * omap_iommu_save_ctx - Save registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020070 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020071 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020072void omap_iommu_save_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020073{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020074 struct omap_iommu *obj = dev_to_omap_iommu(dev);
Suman Annabd4396f2014-10-22 17:22:27 -050075 u32 *p = obj->ctx;
76 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020077
Suman Annabd4396f2014-10-22 17:22:27 -050078 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
79 p[i] = iommu_read_reg(obj, i * sizeof(u32));
80 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
81 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020082}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030083EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020084
85/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030086 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020087 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020088 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020089void omap_iommu_restore_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020090{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020091 struct omap_iommu *obj = dev_to_omap_iommu(dev);
Suman Annabd4396f2014-10-22 17:22:27 -050092 u32 *p = obj->ctx;
93 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020094
Suman Annabd4396f2014-10-22 17:22:27 -050095 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
96 iommu_write_reg(obj, p[i], i * sizeof(u32));
97 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
98 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020099}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300100EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200101
Suman Anna3ca92992015-10-02 18:02:44 -0500102static void dra7_cfg_dspsys_mmu(struct omap_iommu *obj, bool enable)
103{
104 u32 val, mask;
105
106 if (!obj->syscfg)
107 return;
108
109 mask = (1 << (obj->id * DSP_SYS_MMU_CONFIG_EN_SHIFT));
110 val = enable ? mask : 0;
111 regmap_update_bits(obj->syscfg, DSP_SYS_MMU_CONFIG, mask, val);
112}
113
Suman Annabd4396f2014-10-22 17:22:27 -0500114static void __iommu_set_twl(struct omap_iommu *obj, bool on)
115{
116 u32 l = iommu_read_reg(obj, MMU_CNTL);
117
118 if (on)
119 iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
120 else
121 iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
122
123 l &= ~MMU_CNTL_MASK;
124 if (on)
125 l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
126 else
127 l |= (MMU_CNTL_MMU_EN);
128
129 iommu_write_reg(obj, l, MMU_CNTL);
130}
131
132static int omap2_iommu_enable(struct omap_iommu *obj)
133{
134 u32 l, pa;
135
136 if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
137 return -EINVAL;
138
139 pa = virt_to_phys(obj->iopgd);
140 if (!IS_ALIGNED(pa, SZ_16K))
141 return -EINVAL;
142
143 l = iommu_read_reg(obj, MMU_REVISION);
144 dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
145 (l >> 4) & 0xf, l & 0xf);
146
147 iommu_write_reg(obj, pa, MMU_TTB);
148
Suman Anna3ca92992015-10-02 18:02:44 -0500149 dra7_cfg_dspsys_mmu(obj, true);
150
Suman Annabd4396f2014-10-22 17:22:27 -0500151 if (obj->has_bus_err_back)
152 iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
153
154 __iommu_set_twl(obj, true);
155
156 return 0;
157}
158
159static void omap2_iommu_disable(struct omap_iommu *obj)
160{
161 u32 l = iommu_read_reg(obj, MMU_CNTL);
162
163 l &= ~MMU_CNTL_MASK;
164 iommu_write_reg(obj, l, MMU_CNTL);
Suman Anna3ca92992015-10-02 18:02:44 -0500165 dra7_cfg_dspsys_mmu(obj, false);
Suman Annabd4396f2014-10-22 17:22:27 -0500166
167 dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
168}
169
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300170static int iommu_enable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200171{
172 int err;
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600173 struct platform_device *pdev = to_platform_device(obj->dev);
Kiran Padwal99cb9ae2014-10-30 11:59:47 +0530174 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200175
Florian Vaussard90e569c2014-02-28 14:42:34 -0600176 if (pdata && pdata->deassert_reset) {
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600177 err = pdata->deassert_reset(pdev, pdata->reset_name);
178 if (err) {
179 dev_err(obj->dev, "deassert_reset failed: %d\n", err);
180 return err;
181 }
182 }
183
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600184 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200185
Suman Annabd4396f2014-10-22 17:22:27 -0500186 err = omap2_iommu_enable(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200187
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200188 return err;
189}
190
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300191static void iommu_disable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200192{
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600193 struct platform_device *pdev = to_platform_device(obj->dev);
Kiran Padwal99cb9ae2014-10-30 11:59:47 +0530194 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600195
Suman Annabd4396f2014-10-22 17:22:27 -0500196 omap2_iommu_disable(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200197
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600198 pm_runtime_put_sync(obj->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600199
Florian Vaussard90e569c2014-02-28 14:42:34 -0600200 if (pdata && pdata->assert_reset)
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600201 pdata->assert_reset(pdev, pdata->reset_name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200202}
203
204/*
205 * TLB operations
206 */
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300207static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200208{
Suman Annabd4396f2014-10-22 17:22:27 -0500209 u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
210 u32 mask = get_cam_va_mask(cr->cam & page_size);
211
212 return cr->cam & mask;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200213}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200214
215static u32 get_iopte_attr(struct iotlb_entry *e)
216{
Suman Annabd4396f2014-10-22 17:22:27 -0500217 u32 attr;
218
219 attr = e->mixed << 5;
220 attr |= e->endian;
221 attr |= e->elsz >> 3;
222 attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
223 (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
224 return attr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200225}
226
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300227static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200228{
Suman Annabd4396f2014-10-22 17:22:27 -0500229 u32 status, fault_addr;
230
231 status = iommu_read_reg(obj, MMU_IRQSTATUS);
232 status &= MMU_IRQ_MASK;
233 if (!status) {
234 *da = 0;
235 return 0;
236 }
237
238 fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
239 *da = fault_addr;
240
241 iommu_write_reg(obj, status, MMU_IRQSTATUS);
242
243 return status;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200244}
245
Suman Anna69c2c192015-07-20 17:33:25 -0500246void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200247{
248 u32 val;
249
250 val = iommu_read_reg(obj, MMU_LOCK);
251
252 l->base = MMU_LOCK_BASE(val);
253 l->vict = MMU_LOCK_VICT(val);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200254}
255
Suman Anna69c2c192015-07-20 17:33:25 -0500256void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200257{
258 u32 val;
259
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200260 val = (l->base << MMU_LOCK_BASE_SHIFT);
261 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
262
263 iommu_write_reg(obj, val, MMU_LOCK);
264}
265
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300266static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200267{
Suman Annabd4396f2014-10-22 17:22:27 -0500268 cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
269 cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200270}
271
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300272static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200273{
Suman Annabd4396f2014-10-22 17:22:27 -0500274 iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
275 iommu_write_reg(obj, cr->ram, MMU_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200276
277 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
278 iommu_write_reg(obj, 1, MMU_LD_TLB);
279}
280
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000281/* only used in iotlb iteration for-loop */
Suman Anna69c2c192015-07-20 17:33:25 -0500282struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000283{
284 struct cr_regs cr;
285 struct iotlb_lock l;
286
287 iotlb_lock_get(obj, &l);
288 l.vict = n;
289 iotlb_lock_set(obj, &l);
290 iotlb_read_cr(obj, &cr);
291
292 return cr;
293}
294
Suman Annabd4396f2014-10-22 17:22:27 -0500295#ifdef PREFETCH_IOTLB
296static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
297 struct iotlb_entry *e)
298{
299 struct cr_regs *cr;
300
301 if (!e)
302 return NULL;
303
304 if (e->da & ~(get_cam_va_mask(e->pgsz))) {
305 dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
306 e->da);
307 return ERR_PTR(-EINVAL);
308 }
309
310 cr = kmalloc(sizeof(*cr), GFP_KERNEL);
311 if (!cr)
312 return ERR_PTR(-ENOMEM);
313
314 cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
315 cr->ram = e->pa | e->endian | e->elsz | e->mixed;
316
317 return cr;
318}
319
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200320/**
321 * load_iotlb_entry - Set an iommu tlb entry
322 * @obj: target iommu
323 * @e: an iommu tlb entry info
324 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300325static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200326{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200327 int err = 0;
328 struct iotlb_lock l;
329 struct cr_regs *cr;
330
331 if (!obj || !obj->nr_tlb_entries || !e)
332 return -EINVAL;
333
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600334 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200335
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000336 iotlb_lock_get(obj, &l);
337 if (l.base == obj->nr_tlb_entries) {
338 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200339 err = -EBUSY;
340 goto out;
341 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000342 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000343 int i;
344 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000345
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000346 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000347 if (!iotlb_cr_valid(&tmp))
348 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000349
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000350 if (i == obj->nr_tlb_entries) {
351 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
352 err = -EBUSY;
353 goto out;
354 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000355
356 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000357 } else {
358 l.vict = l.base;
359 iotlb_lock_set(obj, &l);
360 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200361
362 cr = iotlb_alloc_cr(obj, e);
363 if (IS_ERR(cr)) {
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600364 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200365 return PTR_ERR(cr);
366 }
367
368 iotlb_load_cr(obj, cr);
369 kfree(cr);
370
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000371 if (e->prsvd)
372 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200373 /* increment victim for next tlb load */
374 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000375 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200376 iotlb_lock_set(obj, &l);
377out:
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600378 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200379 return err;
380}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200381
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300382#else /* !PREFETCH_IOTLB */
383
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300384static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300385{
386 return 0;
387}
388
389#endif /* !PREFETCH_IOTLB */
390
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300391static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300392{
393 return load_iotlb_entry(obj, e);
394}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200395
396/**
397 * flush_iotlb_page - Clear an iommu tlb entry
398 * @obj: target iommu
399 * @da: iommu device virtual address
400 *
401 * Clear an iommu tlb entry which includes 'da' address.
402 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300403static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200404{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200405 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000406 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200407
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600408 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200409
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000410 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200411 u32 start;
412 size_t bytes;
413
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200414 if (!iotlb_cr_valid(&cr))
415 continue;
416
417 start = iotlb_cr_to_virt(&cr);
418 bytes = iopgsz_to_bytes(cr.cam & 3);
419
420 if ((start <= da) && (da < start + bytes)) {
421 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
422 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000423 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200424 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
Laurent Pinchartf7129a02014-03-07 23:47:03 +0100425 break;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200426 }
427 }
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600428 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200429
430 if (i == obj->nr_tlb_entries)
431 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
432}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200433
434/**
435 * flush_iotlb_all - Clear all iommu tlb entries
436 * @obj: target iommu
437 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300438static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200439{
440 struct iotlb_lock l;
441
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600442 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200443
444 l.base = 0;
445 l.vict = 0;
446 iotlb_lock_set(obj, &l);
447
448 iommu_write_reg(obj, 1, MMU_GFLUSH);
449
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600450 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200451}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200452
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200453/*
454 * H/W pagetable operations
455 */
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500456static void flush_iopte_range(struct device *dev, dma_addr_t dma,
457 unsigned long offset, int num_entries)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200458{
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500459 size_t size = num_entries * sizeof(u32);
460
461 dma_sync_single_range_for_device(dev, dma, offset, size, DMA_TO_DEVICE);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200462}
463
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500464static void iopte_free(struct omap_iommu *obj, u32 *iopte, bool dma_valid)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200465{
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500466 dma_addr_t pt_dma;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200467
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200468 /* Note: freed iopte's must be clean ready for re-use */
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500469 if (iopte) {
470 if (dma_valid) {
471 pt_dma = virt_to_phys(iopte);
472 dma_unmap_single(obj->dev, pt_dma, IOPTE_TABLE_SIZE,
473 DMA_TO_DEVICE);
474 }
475
Zhouyi Zhoue28045a2014-03-05 18:20:19 +0800476 kmem_cache_free(iopte_cachep, iopte);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500477 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200478}
479
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500480static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd,
481 dma_addr_t *pt_dma, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200482{
483 u32 *iopte;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500484 unsigned long offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200485
486 /* a table has already existed */
487 if (*iopgd)
488 goto pte_ready;
489
490 /*
491 * do the allocation outside the page table lock
492 */
493 spin_unlock(&obj->page_table_lock);
494 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
495 spin_lock(&obj->page_table_lock);
496
497 if (!*iopgd) {
498 if (!iopte)
499 return ERR_PTR(-ENOMEM);
500
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500501 *pt_dma = dma_map_single(obj->dev, iopte, IOPTE_TABLE_SIZE,
502 DMA_TO_DEVICE);
503 if (dma_mapping_error(obj->dev, *pt_dma)) {
504 dev_err(obj->dev, "DMA map error for L2 table\n");
505 iopte_free(obj, iopte, false);
506 return ERR_PTR(-ENOMEM);
507 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200508
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500509 /*
510 * we rely on dma address and the physical address to be
511 * the same for mapping the L2 table
512 */
513 if (WARN_ON(*pt_dma != virt_to_phys(iopte))) {
514 dev_err(obj->dev, "DMA translation error for L2 table\n");
515 dma_unmap_single(obj->dev, *pt_dma, IOPTE_TABLE_SIZE,
516 DMA_TO_DEVICE);
517 iopte_free(obj, iopte, false);
518 return ERR_PTR(-ENOMEM);
519 }
520
521 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
522
523 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200524 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
525 } else {
526 /* We raced, free the reduniovant table */
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500527 iopte_free(obj, iopte, false);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200528 }
529
530pte_ready:
531 iopte = iopte_offset(iopgd, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500532 *pt_dma = virt_to_phys(iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200533 dev_vdbg(obj->dev,
534 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
535 __func__, da, iopgd, *iopgd, iopte, *iopte);
536
537 return iopte;
538}
539
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300540static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200541{
542 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500543 unsigned long offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200544
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300545 if ((da | pa) & ~IOSECTION_MASK) {
546 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
547 __func__, da, pa, IOSECTION_SIZE);
548 return -EINVAL;
549 }
550
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200551 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500552 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200553 return 0;
554}
555
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300556static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200557{
558 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500559 unsigned long offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200560 int i;
561
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300562 if ((da | pa) & ~IOSUPER_MASK) {
563 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
564 __func__, da, pa, IOSUPER_SIZE);
565 return -EINVAL;
566 }
567
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200568 for (i = 0; i < 16; i++)
569 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500570 flush_iopte_range(obj->dev, obj->pd_dma, offset, 16);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200571 return 0;
572}
573
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300574static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200575{
576 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500577 dma_addr_t pt_dma;
578 u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
579 unsigned long offset = iopte_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200580
581 if (IS_ERR(iopte))
582 return PTR_ERR(iopte);
583
584 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500585 flush_iopte_range(obj->dev, pt_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200586
587 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
588 __func__, da, pa, iopte, *iopte);
589
590 return 0;
591}
592
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300593static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200594{
595 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500596 dma_addr_t pt_dma;
597 u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
598 unsigned long offset = iopte_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200599 int i;
600
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300601 if ((da | pa) & ~IOLARGE_MASK) {
602 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
603 __func__, da, pa, IOLARGE_SIZE);
604 return -EINVAL;
605 }
606
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200607 if (IS_ERR(iopte))
608 return PTR_ERR(iopte);
609
610 for (i = 0; i < 16; i++)
611 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500612 flush_iopte_range(obj->dev, pt_dma, offset, 16);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200613 return 0;
614}
615
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300616static int
617iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200618{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300619 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200620 u32 prot;
621 int err;
622
623 if (!obj || !e)
624 return -EINVAL;
625
626 switch (e->pgsz) {
627 case MMU_CAM_PGSZ_16M:
628 fn = iopgd_alloc_super;
629 break;
630 case MMU_CAM_PGSZ_1M:
631 fn = iopgd_alloc_section;
632 break;
633 case MMU_CAM_PGSZ_64K:
634 fn = iopte_alloc_large;
635 break;
636 case MMU_CAM_PGSZ_4K:
637 fn = iopte_alloc_page;
638 break;
639 default:
640 fn = NULL;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200641 break;
642 }
643
Suman Anna7c1ab602016-04-04 17:46:19 -0500644 if (WARN_ON(!fn))
645 return -EINVAL;
646
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200647 prot = get_iopte_attr(e);
648
649 spin_lock(&obj->page_table_lock);
650 err = fn(obj, e->da, e->pa, prot);
651 spin_unlock(&obj->page_table_lock);
652
653 return err;
654}
655
656/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300657 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200658 * @obj: target iommu
659 * @e: an iommu tlb entry info
660 **/
Suman Anna4899a562014-10-22 17:22:32 -0500661static int
662omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200663{
664 int err;
665
666 flush_iotlb_page(obj, e->da);
667 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200668 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300669 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200670 return err;
671}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200672
673/**
674 * iopgtable_lookup_entry - Lookup an iommu pte entry
675 * @obj: target iommu
676 * @da: iommu device virtual address
677 * @ppgd: iommu pgd entry pointer to be returned
678 * @ppte: iommu pte entry pointer to be returned
679 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300680static void
681iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200682{
683 u32 *iopgd, *iopte = NULL;
684
685 iopgd = iopgd_offset(obj, da);
686 if (!*iopgd)
687 goto out;
688
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300689 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200690 iopte = iopte_offset(iopgd, da);
691out:
692 *ppgd = iopgd;
693 *ppte = iopte;
694}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200695
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300696static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200697{
698 size_t bytes;
699 u32 *iopgd = iopgd_offset(obj, da);
700 int nent = 1;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500701 dma_addr_t pt_dma;
702 unsigned long pd_offset = iopgd_index(da) * sizeof(da);
703 unsigned long pt_offset = iopte_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200704
705 if (!*iopgd)
706 return 0;
707
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300708 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200709 int i;
710 u32 *iopte = iopte_offset(iopgd, da);
711
712 bytes = IOPTE_SIZE;
713 if (*iopte & IOPTE_LARGE) {
714 nent *= 16;
715 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800716 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200717 }
718 bytes *= nent;
719 memset(iopte, 0, nent * sizeof(*iopte));
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500720 pt_dma = virt_to_phys(iopte);
721 flush_iopte_range(obj->dev, pt_dma, pt_offset, nent);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200722
723 /*
724 * do table walk to check if this table is necessary or not
725 */
726 iopte = iopte_offset(iopgd, 0);
727 for (i = 0; i < PTRS_PER_IOPTE; i++)
728 if (iopte[i])
729 goto out;
730
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500731 iopte_free(obj, iopte, true);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200732 nent = 1; /* for the next L1 entry */
733 } else {
734 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700735 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200736 nent *= 16;
737 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800738 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200739 }
740 bytes *= nent;
741 }
742 memset(iopgd, 0, nent * sizeof(*iopgd));
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500743 flush_iopte_range(obj->dev, obj->pd_dma, pd_offset, nent);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200744out:
745 return bytes;
746}
747
748/**
749 * iopgtable_clear_entry - Remove an iommu pte entry
750 * @obj: target iommu
751 * @da: iommu device virtual address
752 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300753static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200754{
755 size_t bytes;
756
757 spin_lock(&obj->page_table_lock);
758
759 bytes = iopgtable_clear_entry_core(obj, da);
760 flush_iotlb_page(obj, da);
761
762 spin_unlock(&obj->page_table_lock);
763
764 return bytes;
765}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200766
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300767static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200768{
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500769 unsigned long offset;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200770 int i;
771
772 spin_lock(&obj->page_table_lock);
773
774 for (i = 0; i < PTRS_PER_IOPGD; i++) {
775 u32 da;
776 u32 *iopgd;
777
778 da = i << IOPGD_SHIFT;
779 iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500780 offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200781
782 if (!*iopgd)
783 continue;
784
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300785 if (iopgd_is_table(*iopgd))
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500786 iopte_free(obj, iopte_offset(iopgd, 0), true);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200787
788 *iopgd = 0;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500789 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200790 }
791
792 flush_iotlb_all(obj);
793
794 spin_unlock(&obj->page_table_lock);
795}
796
797/*
798 * Device IOMMU generic operations
799 */
800static irqreturn_t iommu_fault_handler(int irq, void *data)
801{
David Cohend594f1f2011-02-16 19:35:51 +0000802 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200803 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300804 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400805 struct iommu_domain *domain = obj->domain;
Joerg Roedel8cf851e2015-03-26 13:43:09 +0100806 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200807
Suman Anna2088ecb2014-10-22 17:22:19 -0500808 if (!omap_domain->iommu_dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200809 return IRQ_NONE;
810
David Cohend594f1f2011-02-16 19:35:51 +0000811 errs = iommu_report_fault(obj, &da);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200812 if (errs == 0)
813 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000814
815 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400816 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200817 return IRQ_HANDLED;
818
Fernando Guzman Lugo159d3e32017-07-28 15:49:13 -0500819 iommu_write_reg(obj, 0, MMU_IRQENABLE);
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000820
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200821 iopgd = iopgd_offset(obj, da);
822
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300823 if (!iopgd_is_table(*iopgd)) {
Suman Annab6c2e092013-05-30 18:10:59 -0500824 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
Suman Anna5835b6a2015-07-20 17:33:32 -0500825 obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200826 return IRQ_NONE;
827 }
828
829 iopte = iopte_offset(iopgd, da);
830
Suman Annab6c2e092013-05-30 18:10:59 -0500831 dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
Suman Anna5835b6a2015-07-20 17:33:32 -0500832 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200833
834 return IRQ_NONE;
835}
836
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200837/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300838 * omap_iommu_attach() - attach iommu device to an iommu domain
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500839 * @obj: target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300840 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200841 **/
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500842static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200843{
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600844 int err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200845
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300846 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200847
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500848 obj->pd_dma = dma_map_single(obj->dev, iopgd, IOPGD_TABLE_SIZE,
849 DMA_TO_DEVICE);
850 if (dma_mapping_error(obj->dev, obj->pd_dma)) {
851 dev_err(obj->dev, "DMA map error for L1 table\n");
852 err = -ENOMEM;
853 goto out_err;
854 }
855
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300856 obj->iopgd = iopgd;
857 err = iommu_enable(obj);
858 if (err)
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500859 goto out_err;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300860 flush_iotlb_all(obj);
861
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300862 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200863
864 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500865
866 return 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200867
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500868out_err:
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300869 spin_unlock(&obj->iommu_lock);
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500870
871 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200872}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200873
874/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300875 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200876 * @obj: target iommu
877 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300878static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200879{
Roel Kluinacf9d462010-01-08 10:29:05 -0800880 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200881 return;
882
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300883 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200884
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500885 dma_unmap_single(obj->dev, obj->pd_dma, IOPGD_TABLE_SIZE,
886 DMA_TO_DEVICE);
Suman Anna2088ecb2014-10-22 17:22:19 -0500887 iommu_disable(obj);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500888 obj->pd_dma = 0;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300889 obj->iopgd = NULL;
890
891 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200892
893 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
894}
David Cohend594f1f2011-02-16 19:35:51 +0000895
Suman Anna3ca92992015-10-02 18:02:44 -0500896static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device *pdev,
897 struct omap_iommu *obj)
898{
899 struct device_node *np = pdev->dev.of_node;
900 int ret;
901
902 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
903 return 0;
904
905 if (!of_property_read_bool(np, "ti,syscon-mmuconfig")) {
906 dev_err(&pdev->dev, "ti,syscon-mmuconfig property is missing\n");
907 return -EINVAL;
908 }
909
910 obj->syscfg =
911 syscon_regmap_lookup_by_phandle(np, "ti,syscon-mmuconfig");
912 if (IS_ERR(obj->syscfg)) {
913 /* can fail with -EPROBE_DEFER */
914 ret = PTR_ERR(obj->syscfg);
915 return ret;
916 }
917
918 if (of_property_read_u32_index(np, "ti,syscon-mmuconfig", 1,
919 &obj->id)) {
920 dev_err(&pdev->dev, "couldn't get the IOMMU instance id within subsystem\n");
921 return -EINVAL;
922 }
923
924 if (obj->id != 0 && obj->id != 1) {
925 dev_err(&pdev->dev, "invalid IOMMU instance id\n");
926 return -EINVAL;
927 }
928
929 return 0;
930}
931
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200932/*
933 * OMAP Device MMU(IOMMU) detection
934 */
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -0800935static int omap_iommu_probe(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200936{
937 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200938 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300939 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200940 struct resource *res;
Florian Vaussard3c927482014-02-28 14:42:36 -0600941 struct device_node *of = pdev->dev.of_node;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200942
Suman Anna49a57ef2017-04-12 00:21:27 -0500943 if (!of) {
944 pr_err("%s: only DT-based devices are supported\n", __func__);
945 return -ENODEV;
946 }
947
Suman Annaf129b3d2014-02-28 14:42:32 -0600948 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200949 if (!obj)
950 return -ENOMEM;
951
Suman Anna49a57ef2017-04-12 00:21:27 -0500952 obj->name = dev_name(&pdev->dev);
953 obj->nr_tlb_entries = 32;
954 err = of_property_read_u32(of, "ti,#tlb-entries", &obj->nr_tlb_entries);
955 if (err && err != -EINVAL)
956 return err;
957 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
958 return -EINVAL;
959 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
960 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
Florian Vaussard3c927482014-02-28 14:42:36 -0600961
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200962 obj->dev = &pdev->dev;
963 obj->ctx = (void *)obj + sizeof(*obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200964
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300965 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200966 spin_lock_init(&obj->page_table_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200967
968 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -0600969 obj->regbase = devm_ioremap_resource(obj->dev, res);
970 if (IS_ERR(obj->regbase))
971 return PTR_ERR(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +0000972
Suman Anna3ca92992015-10-02 18:02:44 -0500973 err = omap_iommu_dra7_get_dsp_system_cfg(pdev, obj);
974 if (err)
975 return err;
976
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200977 irq = platform_get_irq(pdev, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -0600978 if (irq < 0)
979 return -ENODEV;
980
981 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
982 dev_name(obj->dev), obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200983 if (err < 0)
Suman Annaf129b3d2014-02-28 14:42:32 -0600984 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200985 platform_set_drvdata(pdev, obj);
986
Joerg Roedel28ae1e32017-04-12 00:21:31 -0500987 obj->group = iommu_group_alloc();
988 if (IS_ERR(obj->group))
989 return PTR_ERR(obj->group);
990
Joerg Roedel01611fe2017-04-12 00:21:30 -0500991 err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL, obj->name);
992 if (err)
Joerg Roedel28ae1e32017-04-12 00:21:31 -0500993 goto out_group;
Joerg Roedel01611fe2017-04-12 00:21:30 -0500994
995 iommu_device_set_ops(&obj->iommu, &omap_iommu_ops);
996
997 err = iommu_device_register(&obj->iommu);
998 if (err)
999 goto out_sysfs;
1000
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001001 pm_runtime_irq_safe(obj->dev);
1002 pm_runtime_enable(obj->dev);
1003
Suman Anna61c75352014-10-22 17:22:30 -05001004 omap_iommu_debugfs_add(obj);
1005
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001006 dev_info(&pdev->dev, "%s registered\n", obj->name);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001007
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001008 return 0;
Joerg Roedel01611fe2017-04-12 00:21:30 -05001009
1010out_sysfs:
1011 iommu_device_sysfs_remove(&obj->iommu);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001012out_group:
1013 iommu_group_put(obj->group);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001014 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001015}
1016
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001017static int omap_iommu_remove(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001018{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001019 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001020
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001021 iommu_group_put(obj->group);
1022 obj->group = NULL;
1023
Joerg Roedel01611fe2017-04-12 00:21:30 -05001024 iommu_device_sysfs_remove(&obj->iommu);
1025 iommu_device_unregister(&obj->iommu);
1026
Suman Anna61c75352014-10-22 17:22:30 -05001027 omap_iommu_debugfs_remove(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001028
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001029 pm_runtime_disable(obj->dev);
1030
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001031 dev_info(&pdev->dev, "%s removed\n", obj->name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001032 return 0;
1033}
1034
Kiran Padwald943b0f2014-09-11 19:07:36 +05301035static const struct of_device_id omap_iommu_of_match[] = {
Florian Vaussard3c927482014-02-28 14:42:36 -06001036 { .compatible = "ti,omap2-iommu" },
1037 { .compatible = "ti,omap4-iommu" },
1038 { .compatible = "ti,dra7-iommu" },
Suman Anna3ca92992015-10-02 18:02:44 -05001039 { .compatible = "ti,dra7-dsp-iommu" },
Florian Vaussard3c927482014-02-28 14:42:36 -06001040 {},
1041};
Florian Vaussard3c927482014-02-28 14:42:36 -06001042
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001043static struct platform_driver omap_iommu_driver = {
1044 .probe = omap_iommu_probe,
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001045 .remove = omap_iommu_remove,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001046 .driver = {
1047 .name = "omap-iommu",
Florian Vaussard3c927482014-02-28 14:42:36 -06001048 .of_match_table = of_match_ptr(omap_iommu_of_match),
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001049 },
1050};
1051
Laurent Pinchart286f6002014-03-08 00:44:38 +01001052static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001053{
1054 memset(e, 0, sizeof(*e));
1055
1056 e->da = da;
1057 e->pa = pa;
Suman Annad760e3e2014-03-17 20:31:32 -05001058 e->valid = MMU_CAM_V;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001059 e->pgsz = pgsz;
1060 e->endian = MMU_RAM_ENDIAN_LITTLE;
1061 e->elsz = MMU_RAM_ELSZ_8;
1062 e->mixed = 0;
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001063
1064 return iopgsz_to_bytes(e->pgsz);
1065}
1066
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001067static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Suman Anna5835b6a2015-07-20 17:33:32 -05001068 phys_addr_t pa, size_t bytes, int prot)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001069{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001070 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001071 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001072 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001073 struct iotlb_entry e;
1074 int omap_pgsz;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001075 u32 ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001076
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001077 omap_pgsz = bytes_to_iopgsz(bytes);
1078 if (omap_pgsz < 0) {
1079 dev_err(dev, "invalid size to map: %d\n", bytes);
1080 return -EINVAL;
1081 }
1082
Joerg Roedel1d7f4492015-01-22 14:42:06 +01001083 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 +03001084
Laurent Pinchart286f6002014-03-08 00:44:38 +01001085 iotlb_init_entry(&e, da, pa, omap_pgsz);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001086
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001087 ret = omap_iopgtable_store_entry(oiommu, &e);
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001088 if (ret)
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001089 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001090
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001091 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001092}
1093
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001094static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
Suman Anna5835b6a2015-07-20 17:33:32 -05001095 size_t size)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001096{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001097 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001098 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001099 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001100
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001101 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001102
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001103 return iopgtable_clear_entry(oiommu, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001104}
1105
1106static int
1107omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1108{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001109 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001110 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001111 struct omap_iommu *oiommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001112 int ret = 0;
1113
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001114 if (!arch_data || !arch_data->iommu_dev) {
Suman Annae3f595b2014-09-04 17:27:29 -05001115 dev_err(dev, "device doesn't have an associated iommu\n");
1116 return -EINVAL;
1117 }
1118
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001119 spin_lock(&omap_domain->lock);
1120
1121 /* only a single device is supported per domain for now */
1122 if (omap_domain->iommu_dev) {
1123 dev_err(dev, "iommu domain is already attached\n");
1124 ret = -EBUSY;
1125 goto out;
1126 }
1127
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001128 oiommu = arch_data->iommu_dev;
1129
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001130 /* get a handle to and enable the omap iommu */
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001131 ret = omap_iommu_attach(oiommu, omap_domain->pgtable);
1132 if (ret) {
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001133 dev_err(dev, "can't get omap iommu: %d\n", ret);
1134 goto out;
1135 }
1136
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001137 omap_domain->iommu_dev = oiommu;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001138 omap_domain->dev = dev;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -04001139 oiommu->domain = domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001140
1141out:
1142 spin_unlock(&omap_domain->lock);
1143 return ret;
1144}
1145
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001146static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001147 struct device *dev)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001148{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001149 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001150
1151 /* only a single device is supported per domain for now */
1152 if (omap_domain->iommu_dev != oiommu) {
1153 dev_err(dev, "invalid iommu device\n");
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001154 return;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001155 }
1156
1157 iopgtable_clear_entry_all(oiommu);
1158
1159 omap_iommu_detach(oiommu);
1160
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001161 omap_domain->iommu_dev = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001162 omap_domain->dev = NULL;
Suman Annaf24d9ad2014-10-22 17:22:33 -05001163 oiommu->domain = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001164}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001165
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001166static void omap_iommu_detach_dev(struct iommu_domain *domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001167 struct device *dev)
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001168{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001169 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001170
1171 spin_lock(&omap_domain->lock);
1172 _omap_iommu_detach_dev(omap_domain, dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001173 spin_unlock(&omap_domain->lock);
1174}
1175
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001176static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001177{
1178 struct omap_iommu_domain *omap_domain;
1179
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001180 if (type != IOMMU_DOMAIN_UNMANAGED)
1181 return NULL;
1182
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001183 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
Suman Anna99ee98d2015-07-20 17:33:29 -05001184 if (!omap_domain)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001185 goto out;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001186
1187 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
Suman Anna99ee98d2015-07-20 17:33:29 -05001188 if (!omap_domain->pgtable)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001189 goto fail_nomem;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001190
1191 /*
1192 * should never fail, but please keep this around to ensure
1193 * we keep the hardware happy
1194 */
Suman Anna433c4342016-04-04 17:46:20 -05001195 if (WARN_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE)))
1196 goto fail_align;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001197
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001198 spin_lock_init(&omap_domain->lock);
1199
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001200 omap_domain->domain.geometry.aperture_start = 0;
1201 omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
1202 omap_domain->domain.geometry.force_aperture = true;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001203
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001204 return &omap_domain->domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001205
Suman Anna433c4342016-04-04 17:46:20 -05001206fail_align:
1207 kfree(omap_domain->pgtable);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001208fail_nomem:
1209 kfree(omap_domain);
1210out:
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001211 return NULL;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001212}
1213
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001214static void omap_iommu_domain_free(struct iommu_domain *domain)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001215{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001216 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001217
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001218 /*
1219 * An iommu device is still attached
1220 * (currently, only one device can be attached) ?
1221 */
1222 if (omap_domain->iommu_dev)
1223 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1224
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001225 kfree(omap_domain->pgtable);
1226 kfree(omap_domain);
1227}
1228
1229static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001230 dma_addr_t da)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001231{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001232 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001233 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001234 struct device *dev = oiommu->dev;
1235 u32 *pgd, *pte;
1236 phys_addr_t ret = 0;
1237
1238 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1239
1240 if (pte) {
1241 if (iopte_is_small(*pte))
1242 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1243 else if (iopte_is_large(*pte))
1244 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1245 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001246 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
Suman Anna5835b6a2015-07-20 17:33:32 -05001247 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001248 } else {
1249 if (iopgd_is_section(*pgd))
1250 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1251 else if (iopgd_is_super(*pgd))
1252 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1253 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001254 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
Suman Anna5835b6a2015-07-20 17:33:32 -05001255 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001256 }
1257
1258 return ret;
1259}
1260
Laurent Pinchart07a02032014-02-28 14:42:38 -06001261static int omap_iommu_add_device(struct device *dev)
1262{
1263 struct omap_iommu_arch_data *arch_data;
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001264 struct omap_iommu *oiommu;
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001265 struct iommu_group *group;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001266 struct device_node *np;
Suman Anna7d682772014-09-04 17:27:30 -05001267 struct platform_device *pdev;
Joerg Roedel01611fe2017-04-12 00:21:30 -05001268 int ret;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001269
1270 /*
1271 * Allocate the archdata iommu structure for DT-based devices.
1272 *
1273 * TODO: Simplify this when removing non-DT support completely from the
1274 * IOMMU users.
1275 */
1276 if (!dev->of_node)
1277 return 0;
1278
1279 np = of_parse_phandle(dev->of_node, "iommus", 0);
1280 if (!np)
1281 return 0;
1282
Suman Anna7d682772014-09-04 17:27:30 -05001283 pdev = of_find_device_by_node(np);
1284 if (WARN_ON(!pdev)) {
1285 of_node_put(np);
1286 return -EINVAL;
1287 }
1288
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001289 oiommu = platform_get_drvdata(pdev);
1290 if (!oiommu) {
1291 of_node_put(np);
1292 return -EINVAL;
1293 }
1294
Laurent Pinchart07a02032014-02-28 14:42:38 -06001295 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
1296 if (!arch_data) {
1297 of_node_put(np);
1298 return -ENOMEM;
1299 }
1300
Joerg Roedel01611fe2017-04-12 00:21:30 -05001301 ret = iommu_device_link(&oiommu->iommu, dev);
1302 if (ret) {
1303 kfree(arch_data);
1304 of_node_put(np);
1305 return ret;
1306 }
1307
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001308 arch_data->iommu_dev = oiommu;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001309 dev->archdata.iommu = arch_data;
1310
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001311 /*
1312 * IOMMU group initialization calls into omap_iommu_device_group, which
1313 * needs a valid dev->archdata.iommu pointer
1314 */
1315 group = iommu_group_get_for_dev(dev);
1316 if (IS_ERR(group)) {
1317 iommu_device_unlink(&oiommu->iommu, dev);
1318 dev->archdata.iommu = NULL;
1319 kfree(arch_data);
1320 return PTR_ERR(group);
1321 }
1322 iommu_group_put(group);
1323
Laurent Pinchart07a02032014-02-28 14:42:38 -06001324 of_node_put(np);
1325
1326 return 0;
1327}
1328
1329static void omap_iommu_remove_device(struct device *dev)
1330{
1331 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1332
1333 if (!dev->of_node || !arch_data)
1334 return;
1335
Joerg Roedel01611fe2017-04-12 00:21:30 -05001336 iommu_device_unlink(&arch_data->iommu_dev->iommu, dev);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001337 iommu_group_remove_device(dev);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001338
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001339 dev->archdata.iommu = NULL;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001340 kfree(arch_data);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001341
Laurent Pinchart07a02032014-02-28 14:42:38 -06001342}
1343
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001344static struct iommu_group *omap_iommu_device_group(struct device *dev)
1345{
1346 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Joerg Roedel8faf5e52017-06-28 12:50:16 +02001347 struct iommu_group *group = ERR_PTR(-EINVAL);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001348
1349 if (arch_data->iommu_dev)
1350 group = arch_data->iommu_dev->group;
1351
1352 return group;
1353}
1354
Thierry Redingb22f6432014-06-27 09:03:12 +02001355static const struct iommu_ops omap_iommu_ops = {
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001356 .domain_alloc = omap_iommu_domain_alloc,
1357 .domain_free = omap_iommu_domain_free,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001358 .attach_dev = omap_iommu_attach_dev,
1359 .detach_dev = omap_iommu_detach_dev,
1360 .map = omap_iommu_map,
1361 .unmap = omap_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07001362 .map_sg = default_iommu_map_sg,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001363 .iova_to_phys = omap_iommu_iova_to_phys,
Laurent Pinchart07a02032014-02-28 14:42:38 -06001364 .add_device = omap_iommu_add_device,
1365 .remove_device = omap_iommu_remove_device,
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001366 .device_group = omap_iommu_device_group,
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +02001367 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001368};
1369
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001370static int __init omap_iommu_init(void)
1371{
1372 struct kmem_cache *p;
1373 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1374 size_t align = 1 << 10; /* L2 pagetable alignement */
Thierry Redingf938aab2015-02-06 11:44:06 +01001375 struct device_node *np;
Suman Annaabaa7e52017-04-12 00:21:26 -05001376 int ret;
Thierry Redingf938aab2015-02-06 11:44:06 +01001377
1378 np = of_find_matching_node(NULL, omap_iommu_of_match);
1379 if (!np)
1380 return 0;
1381
1382 of_node_put(np);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001383
1384 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
Josue Albarranbfee0cf2017-07-28 15:49:14 -05001385 NULL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001386 if (!p)
1387 return -ENOMEM;
1388 iopte_cachep = p;
1389
Suman Anna61c75352014-10-22 17:22:30 -05001390 omap_iommu_debugfs_init();
1391
Suman Annaabaa7e52017-04-12 00:21:26 -05001392 ret = platform_driver_register(&omap_iommu_driver);
1393 if (ret) {
1394 pr_err("%s: failed to register driver\n", __func__);
1395 goto fail_driver;
1396 }
1397
1398 ret = bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
1399 if (ret)
1400 goto fail_bus;
1401
1402 return 0;
1403
1404fail_bus:
1405 platform_driver_unregister(&omap_iommu_driver);
1406fail_driver:
1407 kmem_cache_destroy(iopte_cachep);
1408 return ret;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001409}
Ohad Ben-Cohen435792d2012-02-26 12:14:14 +02001410subsys_initcall(omap_iommu_init);
Suman Anna0cdbf722015-07-20 17:33:24 -05001411/* must be ready before omap3isp is probed */