blob: 641e035cf86661b5da9239f8a60e28c252fa2f7e [file] [log] [blame]
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001/*
2 * omap iommu: tlb and pagetable primitives
3 *
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -08004 * Copyright (C) 2008-2010 Nokia Corporation
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02005 *
6 * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>,
7 * Paul Mundt and Toshihiro Kobayashi
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020016#include <linux/interrupt.h>
17#include <linux/ioport.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020018#include <linux/platform_device.h>
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030019#include <linux/iommu.h>
Tony Lindgrenc8d35c82012-11-02 12:24:03 -070020#include <linux/omap-iommu.h>
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030021#include <linux/mutex.h>
22#include <linux/spinlock.h>
Tony Lindgrened1c7de2012-11-02 12:24:06 -070023#include <linux/io.h>
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -060024#include <linux/pm_runtime.h>
Florian Vaussard3c927482014-02-28 14:42:36 -060025#include <linux/of.h>
26#include <linux/of_iommu.h>
27#include <linux/of_irq.h>
Suman Anna7d682772014-09-04 17:27:30 -050028#include <linux/of_platform.h>
Suman Anna3ca92992015-10-02 18:02:44 -050029#include <linux/regmap.h>
30#include <linux/mfd/syscon.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020031
32#include <asm/cacheflush.h>
33
Tony Lindgren2ab7c842012-11-02 12:24:14 -070034#include <linux/platform_data/iommu-omap.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020035
Ido Yariv2f7702a2012-11-02 12:24:00 -070036#include "omap-iopgtable.h"
Tony Lindgrened1c7de2012-11-02 12:24:06 -070037#include "omap-iommu.h"
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020038
Joerg Roedel01611fe2017-04-12 00:21:30 -050039static const struct iommu_ops omap_iommu_ops;
40
Suman Anna5acc97d2014-03-17 20:31:34 -050041#define to_iommu(dev) \
42 ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
43
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +020044/* bitmap of the page sizes currently supported */
45#define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
46
Ido Yariv7bd9e252012-11-02 12:24:09 -070047#define MMU_LOCK_BASE_SHIFT 10
48#define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
49#define MMU_LOCK_BASE(x) \
50 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
51
52#define MMU_LOCK_VICT_SHIFT 4
53#define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
54#define MMU_LOCK_VICT(x) \
55 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
56
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020057static struct platform_driver omap_iommu_driver;
58static struct kmem_cache *iopte_cachep;
59
60/**
Joerg Roedel8cf851e2015-03-26 13:43:09 +010061 * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
62 * @dom: generic iommu domain handle
63 **/
64static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
65{
66 return container_of(dom, struct omap_iommu_domain, domain);
67}
68
69/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030070 * omap_iommu_save_ctx - Save registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020071 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020072 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020073void omap_iommu_save_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020074{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020075 struct omap_iommu *obj = dev_to_omap_iommu(dev);
Suman Annabd4396f2014-10-22 17:22:27 -050076 u32 *p = obj->ctx;
77 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020078
Suman Annabd4396f2014-10-22 17:22:27 -050079 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
80 p[i] = iommu_read_reg(obj, i * sizeof(u32));
81 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
82 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020083}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030084EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020085
86/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030087 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020088 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020089 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020090void omap_iommu_restore_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020091{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020092 struct omap_iommu *obj = dev_to_omap_iommu(dev);
Suman Annabd4396f2014-10-22 17:22:27 -050093 u32 *p = obj->ctx;
94 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020095
Suman Annabd4396f2014-10-22 17:22:27 -050096 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
97 iommu_write_reg(obj, p[i], i * sizeof(u32));
98 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
99 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200100}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300101EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200102
Suman Anna3ca92992015-10-02 18:02:44 -0500103static void dra7_cfg_dspsys_mmu(struct omap_iommu *obj, bool enable)
104{
105 u32 val, mask;
106
107 if (!obj->syscfg)
108 return;
109
110 mask = (1 << (obj->id * DSP_SYS_MMU_CONFIG_EN_SHIFT));
111 val = enable ? mask : 0;
112 regmap_update_bits(obj->syscfg, DSP_SYS_MMU_CONFIG, mask, val);
113}
114
Suman Annabd4396f2014-10-22 17:22:27 -0500115static void __iommu_set_twl(struct omap_iommu *obj, bool on)
116{
117 u32 l = iommu_read_reg(obj, MMU_CNTL);
118
119 if (on)
120 iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
121 else
122 iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
123
124 l &= ~MMU_CNTL_MASK;
125 if (on)
126 l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
127 else
128 l |= (MMU_CNTL_MMU_EN);
129
130 iommu_write_reg(obj, l, MMU_CNTL);
131}
132
133static int omap2_iommu_enable(struct omap_iommu *obj)
134{
135 u32 l, pa;
136
137 if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
138 return -EINVAL;
139
140 pa = virt_to_phys(obj->iopgd);
141 if (!IS_ALIGNED(pa, SZ_16K))
142 return -EINVAL;
143
144 l = iommu_read_reg(obj, MMU_REVISION);
145 dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
146 (l >> 4) & 0xf, l & 0xf);
147
148 iommu_write_reg(obj, pa, MMU_TTB);
149
Suman Anna3ca92992015-10-02 18:02:44 -0500150 dra7_cfg_dspsys_mmu(obj, true);
151
Suman Annabd4396f2014-10-22 17:22:27 -0500152 if (obj->has_bus_err_back)
153 iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
154
155 __iommu_set_twl(obj, true);
156
157 return 0;
158}
159
160static void omap2_iommu_disable(struct omap_iommu *obj)
161{
162 u32 l = iommu_read_reg(obj, MMU_CNTL);
163
164 l &= ~MMU_CNTL_MASK;
165 iommu_write_reg(obj, l, MMU_CNTL);
Suman Anna3ca92992015-10-02 18:02:44 -0500166 dra7_cfg_dspsys_mmu(obj, false);
Suman Annabd4396f2014-10-22 17:22:27 -0500167
168 dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
169}
170
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300171static int iommu_enable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200172{
173 int err;
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600174 struct platform_device *pdev = to_platform_device(obj->dev);
Kiran Padwal99cb9ae2014-10-30 11:59:47 +0530175 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200176
Florian Vaussard90e569c2014-02-28 14:42:34 -0600177 if (pdata && pdata->deassert_reset) {
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600178 err = pdata->deassert_reset(pdev, pdata->reset_name);
179 if (err) {
180 dev_err(obj->dev, "deassert_reset failed: %d\n", err);
181 return err;
182 }
183 }
184
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600185 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200186
Suman Annabd4396f2014-10-22 17:22:27 -0500187 err = omap2_iommu_enable(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200188
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200189 return err;
190}
191
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300192static void iommu_disable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200193{
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600194 struct platform_device *pdev = to_platform_device(obj->dev);
Kiran Padwal99cb9ae2014-10-30 11:59:47 +0530195 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600196
Suman Annabd4396f2014-10-22 17:22:27 -0500197 omap2_iommu_disable(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200198
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600199 pm_runtime_put_sync(obj->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600200
Florian Vaussard90e569c2014-02-28 14:42:34 -0600201 if (pdata && pdata->assert_reset)
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600202 pdata->assert_reset(pdev, pdata->reset_name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200203}
204
205/*
206 * TLB operations
207 */
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300208static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200209{
Suman Annabd4396f2014-10-22 17:22:27 -0500210 u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
211 u32 mask = get_cam_va_mask(cr->cam & page_size);
212
213 return cr->cam & mask;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200214}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200215
216static u32 get_iopte_attr(struct iotlb_entry *e)
217{
Suman Annabd4396f2014-10-22 17:22:27 -0500218 u32 attr;
219
220 attr = e->mixed << 5;
221 attr |= e->endian;
222 attr |= e->elsz >> 3;
223 attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
224 (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
225 return attr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200226}
227
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300228static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200229{
Suman Annabd4396f2014-10-22 17:22:27 -0500230 u32 status, fault_addr;
231
232 status = iommu_read_reg(obj, MMU_IRQSTATUS);
233 status &= MMU_IRQ_MASK;
234 if (!status) {
235 *da = 0;
236 return 0;
237 }
238
239 fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
240 *da = fault_addr;
241
242 iommu_write_reg(obj, status, MMU_IRQSTATUS);
243
244 return status;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200245}
246
Suman Anna69c2c192015-07-20 17:33:25 -0500247void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200248{
249 u32 val;
250
251 val = iommu_read_reg(obj, MMU_LOCK);
252
253 l->base = MMU_LOCK_BASE(val);
254 l->vict = MMU_LOCK_VICT(val);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200255}
256
Suman Anna69c2c192015-07-20 17:33:25 -0500257void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200258{
259 u32 val;
260
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200261 val = (l->base << MMU_LOCK_BASE_SHIFT);
262 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
263
264 iommu_write_reg(obj, val, MMU_LOCK);
265}
266
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300267static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200268{
Suman Annabd4396f2014-10-22 17:22:27 -0500269 cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
270 cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200271}
272
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300273static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200274{
Suman Annabd4396f2014-10-22 17:22:27 -0500275 iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
276 iommu_write_reg(obj, cr->ram, MMU_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200277
278 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
279 iommu_write_reg(obj, 1, MMU_LD_TLB);
280}
281
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000282/* only used in iotlb iteration for-loop */
Suman Anna69c2c192015-07-20 17:33:25 -0500283struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000284{
285 struct cr_regs cr;
286 struct iotlb_lock l;
287
288 iotlb_lock_get(obj, &l);
289 l.vict = n;
290 iotlb_lock_set(obj, &l);
291 iotlb_read_cr(obj, &cr);
292
293 return cr;
294}
295
Suman Annabd4396f2014-10-22 17:22:27 -0500296#ifdef PREFETCH_IOTLB
297static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
298 struct iotlb_entry *e)
299{
300 struct cr_regs *cr;
301
302 if (!e)
303 return NULL;
304
305 if (e->da & ~(get_cam_va_mask(e->pgsz))) {
306 dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
307 e->da);
308 return ERR_PTR(-EINVAL);
309 }
310
311 cr = kmalloc(sizeof(*cr), GFP_KERNEL);
312 if (!cr)
313 return ERR_PTR(-ENOMEM);
314
315 cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
316 cr->ram = e->pa | e->endian | e->elsz | e->mixed;
317
318 return cr;
319}
320
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200321/**
322 * load_iotlb_entry - Set an iommu tlb entry
323 * @obj: target iommu
324 * @e: an iommu tlb entry info
325 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300326static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200327{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200328 int err = 0;
329 struct iotlb_lock l;
330 struct cr_regs *cr;
331
332 if (!obj || !obj->nr_tlb_entries || !e)
333 return -EINVAL;
334
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600335 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200336
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000337 iotlb_lock_get(obj, &l);
338 if (l.base == obj->nr_tlb_entries) {
339 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200340 err = -EBUSY;
341 goto out;
342 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000343 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000344 int i;
345 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000346
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000347 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000348 if (!iotlb_cr_valid(&tmp))
349 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000350
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000351 if (i == obj->nr_tlb_entries) {
352 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
353 err = -EBUSY;
354 goto out;
355 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000356
357 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000358 } else {
359 l.vict = l.base;
360 iotlb_lock_set(obj, &l);
361 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200362
363 cr = iotlb_alloc_cr(obj, e);
364 if (IS_ERR(cr)) {
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600365 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200366 return PTR_ERR(cr);
367 }
368
369 iotlb_load_cr(obj, cr);
370 kfree(cr);
371
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000372 if (e->prsvd)
373 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200374 /* increment victim for next tlb load */
375 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000376 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200377 iotlb_lock_set(obj, &l);
378out:
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600379 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200380 return err;
381}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200382
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300383#else /* !PREFETCH_IOTLB */
384
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300385static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300386{
387 return 0;
388}
389
390#endif /* !PREFETCH_IOTLB */
391
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300392static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300393{
394 return load_iotlb_entry(obj, e);
395}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200396
397/**
398 * flush_iotlb_page - Clear an iommu tlb entry
399 * @obj: target iommu
400 * @da: iommu device virtual address
401 *
402 * Clear an iommu tlb entry which includes 'da' address.
403 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300404static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200405{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200406 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000407 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200408
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600409 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200410
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000411 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200412 u32 start;
413 size_t bytes;
414
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200415 if (!iotlb_cr_valid(&cr))
416 continue;
417
418 start = iotlb_cr_to_virt(&cr);
419 bytes = iopgsz_to_bytes(cr.cam & 3);
420
421 if ((start <= da) && (da < start + bytes)) {
422 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
423 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000424 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200425 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
Laurent Pinchartf7129a02014-03-07 23:47:03 +0100426 break;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200427 }
428 }
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600429 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200430
431 if (i == obj->nr_tlb_entries)
432 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
433}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200434
435/**
436 * flush_iotlb_all - Clear all iommu tlb entries
437 * @obj: target iommu
438 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300439static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200440{
441 struct iotlb_lock l;
442
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600443 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200444
445 l.base = 0;
446 l.vict = 0;
447 iotlb_lock_set(obj, &l);
448
449 iommu_write_reg(obj, 1, MMU_GFLUSH);
450
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600451 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200452}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200453
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200454/*
455 * H/W pagetable operations
456 */
457static void flush_iopgd_range(u32 *first, u32 *last)
458{
459 /* FIXME: L2 cache should be taken care of if it exists */
460 do {
461 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
462 : : "r" (first));
463 first += L1_CACHE_BYTES / sizeof(*first);
464 } while (first <= last);
465}
466
467static void flush_iopte_range(u32 *first, u32 *last)
468{
469 /* FIXME: L2 cache should be taken care of if it exists */
470 do {
471 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
472 : : "r" (first));
473 first += L1_CACHE_BYTES / sizeof(*first);
474 } while (first <= last);
475}
476
477static void iopte_free(u32 *iopte)
478{
479 /* Note: freed iopte's must be clean ready for re-use */
Zhouyi Zhoue28045a2014-03-05 18:20:19 +0800480 if (iopte)
481 kmem_cache_free(iopte_cachep, iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200482}
483
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300484static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200485{
486 u32 *iopte;
487
488 /* a table has already existed */
489 if (*iopgd)
490 goto pte_ready;
491
492 /*
493 * do the allocation outside the page table lock
494 */
495 spin_unlock(&obj->page_table_lock);
496 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
497 spin_lock(&obj->page_table_lock);
498
499 if (!*iopgd) {
500 if (!iopte)
501 return ERR_PTR(-ENOMEM);
502
503 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
504 flush_iopgd_range(iopgd, iopgd);
505
506 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
507 } else {
508 /* We raced, free the reduniovant table */
509 iopte_free(iopte);
510 }
511
512pte_ready:
513 iopte = iopte_offset(iopgd, da);
514
515 dev_vdbg(obj->dev,
516 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
517 __func__, da, iopgd, *iopgd, iopte, *iopte);
518
519 return iopte;
520}
521
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300522static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200523{
524 u32 *iopgd = iopgd_offset(obj, da);
525
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300526 if ((da | pa) & ~IOSECTION_MASK) {
527 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
528 __func__, da, pa, IOSECTION_SIZE);
529 return -EINVAL;
530 }
531
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200532 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
533 flush_iopgd_range(iopgd, iopgd);
534 return 0;
535}
536
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300537static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200538{
539 u32 *iopgd = iopgd_offset(obj, da);
540 int i;
541
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300542 if ((da | pa) & ~IOSUPER_MASK) {
543 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
544 __func__, da, pa, IOSUPER_SIZE);
545 return -EINVAL;
546 }
547
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200548 for (i = 0; i < 16; i++)
549 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
550 flush_iopgd_range(iopgd, iopgd + 15);
551 return 0;
552}
553
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300554static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200555{
556 u32 *iopgd = iopgd_offset(obj, da);
557 u32 *iopte = iopte_alloc(obj, iopgd, da);
558
559 if (IS_ERR(iopte))
560 return PTR_ERR(iopte);
561
562 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
563 flush_iopte_range(iopte, iopte);
564
565 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
566 __func__, da, pa, iopte, *iopte);
567
568 return 0;
569}
570
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300571static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200572{
573 u32 *iopgd = iopgd_offset(obj, da);
574 u32 *iopte = iopte_alloc(obj, iopgd, da);
575 int i;
576
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300577 if ((da | pa) & ~IOLARGE_MASK) {
578 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
579 __func__, da, pa, IOLARGE_SIZE);
580 return -EINVAL;
581 }
582
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200583 if (IS_ERR(iopte))
584 return PTR_ERR(iopte);
585
586 for (i = 0; i < 16; i++)
587 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
588 flush_iopte_range(iopte, iopte + 15);
589 return 0;
590}
591
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300592static int
593iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200594{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300595 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200596 u32 prot;
597 int err;
598
599 if (!obj || !e)
600 return -EINVAL;
601
602 switch (e->pgsz) {
603 case MMU_CAM_PGSZ_16M:
604 fn = iopgd_alloc_super;
605 break;
606 case MMU_CAM_PGSZ_1M:
607 fn = iopgd_alloc_section;
608 break;
609 case MMU_CAM_PGSZ_64K:
610 fn = iopte_alloc_large;
611 break;
612 case MMU_CAM_PGSZ_4K:
613 fn = iopte_alloc_page;
614 break;
615 default:
616 fn = NULL;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200617 break;
618 }
619
Suman Anna7c1ab602016-04-04 17:46:19 -0500620 if (WARN_ON(!fn))
621 return -EINVAL;
622
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200623 prot = get_iopte_attr(e);
624
625 spin_lock(&obj->page_table_lock);
626 err = fn(obj, e->da, e->pa, prot);
627 spin_unlock(&obj->page_table_lock);
628
629 return err;
630}
631
632/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300633 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200634 * @obj: target iommu
635 * @e: an iommu tlb entry info
636 **/
Suman Anna4899a562014-10-22 17:22:32 -0500637static int
638omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200639{
640 int err;
641
642 flush_iotlb_page(obj, e->da);
643 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200644 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300645 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200646 return err;
647}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200648
649/**
650 * iopgtable_lookup_entry - Lookup an iommu pte entry
651 * @obj: target iommu
652 * @da: iommu device virtual address
653 * @ppgd: iommu pgd entry pointer to be returned
654 * @ppte: iommu pte entry pointer to be returned
655 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300656static void
657iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200658{
659 u32 *iopgd, *iopte = NULL;
660
661 iopgd = iopgd_offset(obj, da);
662 if (!*iopgd)
663 goto out;
664
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300665 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200666 iopte = iopte_offset(iopgd, da);
667out:
668 *ppgd = iopgd;
669 *ppte = iopte;
670}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200671
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300672static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200673{
674 size_t bytes;
675 u32 *iopgd = iopgd_offset(obj, da);
676 int nent = 1;
677
678 if (!*iopgd)
679 return 0;
680
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300681 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200682 int i;
683 u32 *iopte = iopte_offset(iopgd, da);
684
685 bytes = IOPTE_SIZE;
686 if (*iopte & IOPTE_LARGE) {
687 nent *= 16;
688 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800689 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200690 }
691 bytes *= nent;
692 memset(iopte, 0, nent * sizeof(*iopte));
693 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
694
695 /*
696 * do table walk to check if this table is necessary or not
697 */
698 iopte = iopte_offset(iopgd, 0);
699 for (i = 0; i < PTRS_PER_IOPTE; i++)
700 if (iopte[i])
701 goto out;
702
703 iopte_free(iopte);
704 nent = 1; /* for the next L1 entry */
705 } else {
706 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700707 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200708 nent *= 16;
709 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800710 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200711 }
712 bytes *= nent;
713 }
714 memset(iopgd, 0, nent * sizeof(*iopgd));
715 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
716out:
717 return bytes;
718}
719
720/**
721 * iopgtable_clear_entry - Remove an iommu pte entry
722 * @obj: target iommu
723 * @da: iommu device virtual address
724 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300725static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200726{
727 size_t bytes;
728
729 spin_lock(&obj->page_table_lock);
730
731 bytes = iopgtable_clear_entry_core(obj, da);
732 flush_iotlb_page(obj, da);
733
734 spin_unlock(&obj->page_table_lock);
735
736 return bytes;
737}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200738
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300739static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200740{
741 int i;
742
743 spin_lock(&obj->page_table_lock);
744
745 for (i = 0; i < PTRS_PER_IOPGD; i++) {
746 u32 da;
747 u32 *iopgd;
748
749 da = i << IOPGD_SHIFT;
750 iopgd = iopgd_offset(obj, da);
751
752 if (!*iopgd)
753 continue;
754
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300755 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200756 iopte_free(iopte_offset(iopgd, 0));
757
758 *iopgd = 0;
759 flush_iopgd_range(iopgd, iopgd);
760 }
761
762 flush_iotlb_all(obj);
763
764 spin_unlock(&obj->page_table_lock);
765}
766
767/*
768 * Device IOMMU generic operations
769 */
770static irqreturn_t iommu_fault_handler(int irq, void *data)
771{
David Cohend594f1f2011-02-16 19:35:51 +0000772 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200773 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300774 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400775 struct iommu_domain *domain = obj->domain;
Joerg Roedel8cf851e2015-03-26 13:43:09 +0100776 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200777
Suman Anna2088ecb2014-10-22 17:22:19 -0500778 if (!omap_domain->iommu_dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200779 return IRQ_NONE;
780
David Cohend594f1f2011-02-16 19:35:51 +0000781 errs = iommu_report_fault(obj, &da);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200782 if (errs == 0)
783 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000784
785 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400786 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200787 return IRQ_HANDLED;
788
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000789 iommu_disable(obj);
790
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200791 iopgd = iopgd_offset(obj, da);
792
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300793 if (!iopgd_is_table(*iopgd)) {
Suman Annab6c2e092013-05-30 18:10:59 -0500794 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 -0500795 obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200796 return IRQ_NONE;
797 }
798
799 iopte = iopte_offset(iopgd, da);
800
Suman Annab6c2e092013-05-30 18:10:59 -0500801 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 -0500802 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200803
804 return IRQ_NONE;
805}
806
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200807/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300808 * omap_iommu_attach() - attach iommu device to an iommu domain
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500809 * @obj: target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300810 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200811 **/
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500812static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200813{
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600814 int err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200815
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300816 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200817
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300818 obj->iopgd = iopgd;
819 err = iommu_enable(obj);
820 if (err)
821 goto err_enable;
822 flush_iotlb_all(obj);
823
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300824 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200825
826 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500827
828 return 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200829
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200830err_enable:
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300831 spin_unlock(&obj->iommu_lock);
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500832
833 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200834}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200835
836/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300837 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200838 * @obj: target iommu
839 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300840static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200841{
Roel Kluinacf9d462010-01-08 10:29:05 -0800842 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200843 return;
844
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300845 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200846
Suman Anna2088ecb2014-10-22 17:22:19 -0500847 iommu_disable(obj);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300848 obj->iopgd = NULL;
849
850 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200851
852 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
853}
David Cohend594f1f2011-02-16 19:35:51 +0000854
Suman Anna3ca92992015-10-02 18:02:44 -0500855static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device *pdev,
856 struct omap_iommu *obj)
857{
858 struct device_node *np = pdev->dev.of_node;
859 int ret;
860
861 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
862 return 0;
863
864 if (!of_property_read_bool(np, "ti,syscon-mmuconfig")) {
865 dev_err(&pdev->dev, "ti,syscon-mmuconfig property is missing\n");
866 return -EINVAL;
867 }
868
869 obj->syscfg =
870 syscon_regmap_lookup_by_phandle(np, "ti,syscon-mmuconfig");
871 if (IS_ERR(obj->syscfg)) {
872 /* can fail with -EPROBE_DEFER */
873 ret = PTR_ERR(obj->syscfg);
874 return ret;
875 }
876
877 if (of_property_read_u32_index(np, "ti,syscon-mmuconfig", 1,
878 &obj->id)) {
879 dev_err(&pdev->dev, "couldn't get the IOMMU instance id within subsystem\n");
880 return -EINVAL;
881 }
882
883 if (obj->id != 0 && obj->id != 1) {
884 dev_err(&pdev->dev, "invalid IOMMU instance id\n");
885 return -EINVAL;
886 }
887
888 return 0;
889}
890
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200891/*
892 * OMAP Device MMU(IOMMU) detection
893 */
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -0800894static int omap_iommu_probe(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200895{
896 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200897 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300898 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200899 struct resource *res;
Florian Vaussard3c927482014-02-28 14:42:36 -0600900 struct device_node *of = pdev->dev.of_node;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200901
Suman Anna49a57ef2017-04-12 00:21:27 -0500902 if (!of) {
903 pr_err("%s: only DT-based devices are supported\n", __func__);
904 return -ENODEV;
905 }
906
Suman Annaf129b3d2014-02-28 14:42:32 -0600907 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200908 if (!obj)
909 return -ENOMEM;
910
Suman Anna49a57ef2017-04-12 00:21:27 -0500911 obj->name = dev_name(&pdev->dev);
912 obj->nr_tlb_entries = 32;
913 err = of_property_read_u32(of, "ti,#tlb-entries", &obj->nr_tlb_entries);
914 if (err && err != -EINVAL)
915 return err;
916 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
917 return -EINVAL;
918 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
919 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
Florian Vaussard3c927482014-02-28 14:42:36 -0600920
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200921 obj->dev = &pdev->dev;
922 obj->ctx = (void *)obj + sizeof(*obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200923
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300924 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200925 spin_lock_init(&obj->page_table_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200926
927 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -0600928 obj->regbase = devm_ioremap_resource(obj->dev, res);
929 if (IS_ERR(obj->regbase))
930 return PTR_ERR(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +0000931
Suman Anna3ca92992015-10-02 18:02:44 -0500932 err = omap_iommu_dra7_get_dsp_system_cfg(pdev, obj);
933 if (err)
934 return err;
935
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200936 irq = platform_get_irq(pdev, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -0600937 if (irq < 0)
938 return -ENODEV;
939
940 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
941 dev_name(obj->dev), obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200942 if (err < 0)
Suman Annaf129b3d2014-02-28 14:42:32 -0600943 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200944 platform_set_drvdata(pdev, obj);
945
Joerg Roedel28ae1e32017-04-12 00:21:31 -0500946 obj->group = iommu_group_alloc();
947 if (IS_ERR(obj->group))
948 return PTR_ERR(obj->group);
949
Joerg Roedel01611fe2017-04-12 00:21:30 -0500950 err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL, obj->name);
951 if (err)
Joerg Roedel28ae1e32017-04-12 00:21:31 -0500952 goto out_group;
Joerg Roedel01611fe2017-04-12 00:21:30 -0500953
954 iommu_device_set_ops(&obj->iommu, &omap_iommu_ops);
955
956 err = iommu_device_register(&obj->iommu);
957 if (err)
958 goto out_sysfs;
959
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600960 pm_runtime_irq_safe(obj->dev);
961 pm_runtime_enable(obj->dev);
962
Suman Anna61c75352014-10-22 17:22:30 -0500963 omap_iommu_debugfs_add(obj);
964
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200965 dev_info(&pdev->dev, "%s registered\n", obj->name);
Joerg Roedel28ae1e32017-04-12 00:21:31 -0500966
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200967 return 0;
Joerg Roedel01611fe2017-04-12 00:21:30 -0500968
969out_sysfs:
970 iommu_device_sysfs_remove(&obj->iommu);
Joerg Roedel28ae1e32017-04-12 00:21:31 -0500971out_group:
972 iommu_group_put(obj->group);
Joerg Roedel01611fe2017-04-12 00:21:30 -0500973 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200974}
975
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -0800976static int omap_iommu_remove(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200977{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300978 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200979
Joerg Roedel28ae1e32017-04-12 00:21:31 -0500980 iommu_group_put(obj->group);
981 obj->group = NULL;
982
Joerg Roedel01611fe2017-04-12 00:21:30 -0500983 iommu_device_sysfs_remove(&obj->iommu);
984 iommu_device_unregister(&obj->iommu);
985
Suman Anna61c75352014-10-22 17:22:30 -0500986 omap_iommu_debugfs_remove(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200987
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600988 pm_runtime_disable(obj->dev);
989
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200990 dev_info(&pdev->dev, "%s removed\n", obj->name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200991 return 0;
992}
993
Kiran Padwald943b0f2014-09-11 19:07:36 +0530994static const struct of_device_id omap_iommu_of_match[] = {
Florian Vaussard3c927482014-02-28 14:42:36 -0600995 { .compatible = "ti,omap2-iommu" },
996 { .compatible = "ti,omap4-iommu" },
997 { .compatible = "ti,dra7-iommu" },
Suman Anna3ca92992015-10-02 18:02:44 -0500998 { .compatible = "ti,dra7-dsp-iommu" },
Florian Vaussard3c927482014-02-28 14:42:36 -0600999 {},
1000};
Florian Vaussard3c927482014-02-28 14:42:36 -06001001
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001002static struct platform_driver omap_iommu_driver = {
1003 .probe = omap_iommu_probe,
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001004 .remove = omap_iommu_remove,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001005 .driver = {
1006 .name = "omap-iommu",
Florian Vaussard3c927482014-02-28 14:42:36 -06001007 .of_match_table = of_match_ptr(omap_iommu_of_match),
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001008 },
1009};
1010
1011static void iopte_cachep_ctor(void *iopte)
1012{
1013 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
1014}
1015
Laurent Pinchart286f6002014-03-08 00:44:38 +01001016static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001017{
1018 memset(e, 0, sizeof(*e));
1019
1020 e->da = da;
1021 e->pa = pa;
Suman Annad760e3e2014-03-17 20:31:32 -05001022 e->valid = MMU_CAM_V;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001023 e->pgsz = pgsz;
1024 e->endian = MMU_RAM_ENDIAN_LITTLE;
1025 e->elsz = MMU_RAM_ELSZ_8;
1026 e->mixed = 0;
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001027
1028 return iopgsz_to_bytes(e->pgsz);
1029}
1030
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001031static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Suman Anna5835b6a2015-07-20 17:33:32 -05001032 phys_addr_t pa, size_t bytes, int prot)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001033{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001034 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001035 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001036 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001037 struct iotlb_entry e;
1038 int omap_pgsz;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001039 u32 ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001040
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001041 omap_pgsz = bytes_to_iopgsz(bytes);
1042 if (omap_pgsz < 0) {
1043 dev_err(dev, "invalid size to map: %d\n", bytes);
1044 return -EINVAL;
1045 }
1046
Joerg Roedel1d7f4492015-01-22 14:42:06 +01001047 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 +03001048
Laurent Pinchart286f6002014-03-08 00:44:38 +01001049 iotlb_init_entry(&e, da, pa, omap_pgsz);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001050
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001051 ret = omap_iopgtable_store_entry(oiommu, &e);
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001052 if (ret)
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001053 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001054
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001055 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001056}
1057
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001058static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
Suman Anna5835b6a2015-07-20 17:33:32 -05001059 size_t size)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001060{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001061 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001062 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001063 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001064
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001065 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001066
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001067 return iopgtable_clear_entry(oiommu, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001068}
1069
1070static int
1071omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1072{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001073 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001074 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001075 struct omap_iommu *oiommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001076 int ret = 0;
1077
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001078 if (!arch_data || !arch_data->iommu_dev) {
Suman Annae3f595b2014-09-04 17:27:29 -05001079 dev_err(dev, "device doesn't have an associated iommu\n");
1080 return -EINVAL;
1081 }
1082
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001083 spin_lock(&omap_domain->lock);
1084
1085 /* only a single device is supported per domain for now */
1086 if (omap_domain->iommu_dev) {
1087 dev_err(dev, "iommu domain is already attached\n");
1088 ret = -EBUSY;
1089 goto out;
1090 }
1091
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001092 oiommu = arch_data->iommu_dev;
1093
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001094 /* get a handle to and enable the omap iommu */
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001095 ret = omap_iommu_attach(oiommu, omap_domain->pgtable);
1096 if (ret) {
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001097 dev_err(dev, "can't get omap iommu: %d\n", ret);
1098 goto out;
1099 }
1100
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001101 omap_domain->iommu_dev = oiommu;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001102 omap_domain->dev = dev;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -04001103 oiommu->domain = domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001104
1105out:
1106 spin_unlock(&omap_domain->lock);
1107 return ret;
1108}
1109
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001110static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001111 struct device *dev)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001112{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001113 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001114
1115 /* only a single device is supported per domain for now */
1116 if (omap_domain->iommu_dev != oiommu) {
1117 dev_err(dev, "invalid iommu device\n");
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001118 return;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001119 }
1120
1121 iopgtable_clear_entry_all(oiommu);
1122
1123 omap_iommu_detach(oiommu);
1124
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001125 omap_domain->iommu_dev = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001126 omap_domain->dev = NULL;
Suman Annaf24d9ad2014-10-22 17:22:33 -05001127 oiommu->domain = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001128}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001129
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001130static void omap_iommu_detach_dev(struct iommu_domain *domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001131 struct device *dev)
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001132{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001133 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001134
1135 spin_lock(&omap_domain->lock);
1136 _omap_iommu_detach_dev(omap_domain, dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001137 spin_unlock(&omap_domain->lock);
1138}
1139
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001140static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001141{
1142 struct omap_iommu_domain *omap_domain;
1143
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001144 if (type != IOMMU_DOMAIN_UNMANAGED)
1145 return NULL;
1146
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001147 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
Suman Anna99ee98d2015-07-20 17:33:29 -05001148 if (!omap_domain)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001149 goto out;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001150
1151 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
Suman Anna99ee98d2015-07-20 17:33:29 -05001152 if (!omap_domain->pgtable)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001153 goto fail_nomem;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001154
1155 /*
1156 * should never fail, but please keep this around to ensure
1157 * we keep the hardware happy
1158 */
Suman Anna433c4342016-04-04 17:46:20 -05001159 if (WARN_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE)))
1160 goto fail_align;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001161
1162 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1163 spin_lock_init(&omap_domain->lock);
1164
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001165 omap_domain->domain.geometry.aperture_start = 0;
1166 omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
1167 omap_domain->domain.geometry.force_aperture = true;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001168
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001169 return &omap_domain->domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001170
Suman Anna433c4342016-04-04 17:46:20 -05001171fail_align:
1172 kfree(omap_domain->pgtable);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001173fail_nomem:
1174 kfree(omap_domain);
1175out:
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001176 return NULL;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001177}
1178
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001179static void omap_iommu_domain_free(struct iommu_domain *domain)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001180{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001181 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001182
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001183 /*
1184 * An iommu device is still attached
1185 * (currently, only one device can be attached) ?
1186 */
1187 if (omap_domain->iommu_dev)
1188 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1189
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001190 kfree(omap_domain->pgtable);
1191 kfree(omap_domain);
1192}
1193
1194static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001195 dma_addr_t da)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001196{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001197 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001198 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001199 struct device *dev = oiommu->dev;
1200 u32 *pgd, *pte;
1201 phys_addr_t ret = 0;
1202
1203 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1204
1205 if (pte) {
1206 if (iopte_is_small(*pte))
1207 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1208 else if (iopte_is_large(*pte))
1209 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1210 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001211 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
Suman Anna5835b6a2015-07-20 17:33:32 -05001212 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001213 } else {
1214 if (iopgd_is_section(*pgd))
1215 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1216 else if (iopgd_is_super(*pgd))
1217 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1218 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001219 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
Suman Anna5835b6a2015-07-20 17:33:32 -05001220 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001221 }
1222
1223 return ret;
1224}
1225
Laurent Pinchart07a02032014-02-28 14:42:38 -06001226static int omap_iommu_add_device(struct device *dev)
1227{
1228 struct omap_iommu_arch_data *arch_data;
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001229 struct omap_iommu *oiommu;
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001230 struct iommu_group *group;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001231 struct device_node *np;
Suman Anna7d682772014-09-04 17:27:30 -05001232 struct platform_device *pdev;
Joerg Roedel01611fe2017-04-12 00:21:30 -05001233 int ret;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001234
1235 /*
1236 * Allocate the archdata iommu structure for DT-based devices.
1237 *
1238 * TODO: Simplify this when removing non-DT support completely from the
1239 * IOMMU users.
1240 */
1241 if (!dev->of_node)
1242 return 0;
1243
1244 np = of_parse_phandle(dev->of_node, "iommus", 0);
1245 if (!np)
1246 return 0;
1247
Suman Anna7d682772014-09-04 17:27:30 -05001248 pdev = of_find_device_by_node(np);
1249 if (WARN_ON(!pdev)) {
1250 of_node_put(np);
1251 return -EINVAL;
1252 }
1253
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001254 oiommu = platform_get_drvdata(pdev);
1255 if (!oiommu) {
1256 of_node_put(np);
1257 return -EINVAL;
1258 }
1259
Laurent Pinchart07a02032014-02-28 14:42:38 -06001260 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
1261 if (!arch_data) {
1262 of_node_put(np);
1263 return -ENOMEM;
1264 }
1265
Joerg Roedel01611fe2017-04-12 00:21:30 -05001266 ret = iommu_device_link(&oiommu->iommu, dev);
1267 if (ret) {
1268 kfree(arch_data);
1269 of_node_put(np);
1270 return ret;
1271 }
1272
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001273 arch_data->iommu_dev = oiommu;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001274 dev->archdata.iommu = arch_data;
1275
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001276 /*
1277 * IOMMU group initialization calls into omap_iommu_device_group, which
1278 * needs a valid dev->archdata.iommu pointer
1279 */
1280 group = iommu_group_get_for_dev(dev);
1281 if (IS_ERR(group)) {
1282 iommu_device_unlink(&oiommu->iommu, dev);
1283 dev->archdata.iommu = NULL;
1284 kfree(arch_data);
1285 return PTR_ERR(group);
1286 }
1287 iommu_group_put(group);
1288
Laurent Pinchart07a02032014-02-28 14:42:38 -06001289 of_node_put(np);
1290
1291 return 0;
1292}
1293
1294static void omap_iommu_remove_device(struct device *dev)
1295{
1296 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1297
1298 if (!dev->of_node || !arch_data)
1299 return;
1300
Joerg Roedel01611fe2017-04-12 00:21:30 -05001301 iommu_device_unlink(&arch_data->iommu_dev->iommu, dev);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001302 iommu_group_remove_device(dev);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001303
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001304 dev->archdata.iommu = NULL;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001305 kfree(arch_data);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001306
Laurent Pinchart07a02032014-02-28 14:42:38 -06001307}
1308
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001309static struct iommu_group *omap_iommu_device_group(struct device *dev)
1310{
1311 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Joerg Roedel8faf5e52017-06-28 12:50:16 +02001312 struct iommu_group *group = ERR_PTR(-EINVAL);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001313
1314 if (arch_data->iommu_dev)
1315 group = arch_data->iommu_dev->group;
1316
1317 return group;
1318}
1319
Thierry Redingb22f6432014-06-27 09:03:12 +02001320static const struct iommu_ops omap_iommu_ops = {
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001321 .domain_alloc = omap_iommu_domain_alloc,
1322 .domain_free = omap_iommu_domain_free,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001323 .attach_dev = omap_iommu_attach_dev,
1324 .detach_dev = omap_iommu_detach_dev,
1325 .map = omap_iommu_map,
1326 .unmap = omap_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07001327 .map_sg = default_iommu_map_sg,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001328 .iova_to_phys = omap_iommu_iova_to_phys,
Laurent Pinchart07a02032014-02-28 14:42:38 -06001329 .add_device = omap_iommu_add_device,
1330 .remove_device = omap_iommu_remove_device,
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001331 .device_group = omap_iommu_device_group,
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +02001332 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001333};
1334
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001335static int __init omap_iommu_init(void)
1336{
1337 struct kmem_cache *p;
1338 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1339 size_t align = 1 << 10; /* L2 pagetable alignement */
Thierry Redingf938aab2015-02-06 11:44:06 +01001340 struct device_node *np;
Suman Annaabaa7e52017-04-12 00:21:26 -05001341 int ret;
Thierry Redingf938aab2015-02-06 11:44:06 +01001342
1343 np = of_find_matching_node(NULL, omap_iommu_of_match);
1344 if (!np)
1345 return 0;
1346
1347 of_node_put(np);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001348
1349 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1350 iopte_cachep_ctor);
1351 if (!p)
1352 return -ENOMEM;
1353 iopte_cachep = p;
1354
Suman Anna61c75352014-10-22 17:22:30 -05001355 omap_iommu_debugfs_init();
1356
Suman Annaabaa7e52017-04-12 00:21:26 -05001357 ret = platform_driver_register(&omap_iommu_driver);
1358 if (ret) {
1359 pr_err("%s: failed to register driver\n", __func__);
1360 goto fail_driver;
1361 }
1362
1363 ret = bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
1364 if (ret)
1365 goto fail_bus;
1366
1367 return 0;
1368
1369fail_bus:
1370 platform_driver_unregister(&omap_iommu_driver);
1371fail_driver:
1372 kmem_cache_destroy(iopte_cachep);
1373 return ret;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001374}
Ohad Ben-Cohen435792d2012-02-26 12:14:14 +02001375subsys_initcall(omap_iommu_init);
Suman Anna0cdbf722015-07-20 17:33:24 -05001376/* must be ready before omap3isp is probed */