blob: 0553b0381e2ab4a36ce26220f12991845e9e7049 [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
Suman Anna5acc97d2014-03-17 20:31:34 -050039#define to_iommu(dev) \
40 ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
41
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +020042/* bitmap of the page sizes currently supported */
43#define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
44
Ido Yariv7bd9e252012-11-02 12:24:09 -070045#define MMU_LOCK_BASE_SHIFT 10
46#define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
47#define MMU_LOCK_BASE(x) \
48 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
49
50#define MMU_LOCK_VICT_SHIFT 4
51#define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
52#define MMU_LOCK_VICT(x) \
53 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
54
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020055static struct platform_driver omap_iommu_driver;
56static struct kmem_cache *iopte_cachep;
57
58/**
Joerg Roedel8cf851e2015-03-26 13:43:09 +010059 * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
60 * @dom: generic iommu domain handle
61 **/
62static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
63{
64 return container_of(dom, struct omap_iommu_domain, domain);
65}
66
67/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030068 * omap_iommu_save_ctx - Save registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020069 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020070 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020071void omap_iommu_save_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020072{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020073 struct omap_iommu *obj = dev_to_omap_iommu(dev);
Suman Annabd4396f2014-10-22 17:22:27 -050074 u32 *p = obj->ctx;
75 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020076
Suman Annabd4396f2014-10-22 17:22:27 -050077 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
78 p[i] = iommu_read_reg(obj, i * sizeof(u32));
79 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
80 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020081}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030082EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020083
84/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030085 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020086 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020087 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020088void omap_iommu_restore_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020089{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020090 struct omap_iommu *obj = dev_to_omap_iommu(dev);
Suman Annabd4396f2014-10-22 17:22:27 -050091 u32 *p = obj->ctx;
92 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020093
Suman Annabd4396f2014-10-22 17:22:27 -050094 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
95 iommu_write_reg(obj, p[i], i * sizeof(u32));
96 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
97 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020098}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030099EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200100
Suman Anna3ca92992015-10-02 18:02:44 -0500101static void dra7_cfg_dspsys_mmu(struct omap_iommu *obj, bool enable)
102{
103 u32 val, mask;
104
105 if (!obj->syscfg)
106 return;
107
108 mask = (1 << (obj->id * DSP_SYS_MMU_CONFIG_EN_SHIFT));
109 val = enable ? mask : 0;
110 regmap_update_bits(obj->syscfg, DSP_SYS_MMU_CONFIG, mask, val);
111}
112
Suman Annabd4396f2014-10-22 17:22:27 -0500113static void __iommu_set_twl(struct omap_iommu *obj, bool on)
114{
115 u32 l = iommu_read_reg(obj, MMU_CNTL);
116
117 if (on)
118 iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
119 else
120 iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
121
122 l &= ~MMU_CNTL_MASK;
123 if (on)
124 l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
125 else
126 l |= (MMU_CNTL_MMU_EN);
127
128 iommu_write_reg(obj, l, MMU_CNTL);
129}
130
131static int omap2_iommu_enable(struct omap_iommu *obj)
132{
133 u32 l, pa;
134
135 if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
136 return -EINVAL;
137
138 pa = virt_to_phys(obj->iopgd);
139 if (!IS_ALIGNED(pa, SZ_16K))
140 return -EINVAL;
141
142 l = iommu_read_reg(obj, MMU_REVISION);
143 dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
144 (l >> 4) & 0xf, l & 0xf);
145
146 iommu_write_reg(obj, pa, MMU_TTB);
147
Suman Anna3ca92992015-10-02 18:02:44 -0500148 dra7_cfg_dspsys_mmu(obj, true);
149
Suman Annabd4396f2014-10-22 17:22:27 -0500150 if (obj->has_bus_err_back)
151 iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
152
153 __iommu_set_twl(obj, true);
154
155 return 0;
156}
157
158static void omap2_iommu_disable(struct omap_iommu *obj)
159{
160 u32 l = iommu_read_reg(obj, MMU_CNTL);
161
162 l &= ~MMU_CNTL_MASK;
163 iommu_write_reg(obj, l, MMU_CNTL);
Suman Anna3ca92992015-10-02 18:02:44 -0500164 dra7_cfg_dspsys_mmu(obj, false);
Suman Annabd4396f2014-10-22 17:22:27 -0500165
166 dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
167}
168
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300169static int iommu_enable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200170{
171 int err;
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600172 struct platform_device *pdev = to_platform_device(obj->dev);
Kiran Padwal99cb9ae2014-10-30 11:59:47 +0530173 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200174
Florian Vaussard90e569c2014-02-28 14:42:34 -0600175 if (pdata && pdata->deassert_reset) {
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600176 err = pdata->deassert_reset(pdev, pdata->reset_name);
177 if (err) {
178 dev_err(obj->dev, "deassert_reset failed: %d\n", err);
179 return err;
180 }
181 }
182
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600183 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200184
Suman Annabd4396f2014-10-22 17:22:27 -0500185 err = omap2_iommu_enable(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200186
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200187 return err;
188}
189
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300190static void iommu_disable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200191{
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600192 struct platform_device *pdev = to_platform_device(obj->dev);
Kiran Padwal99cb9ae2014-10-30 11:59:47 +0530193 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600194
Suman Annabd4396f2014-10-22 17:22:27 -0500195 omap2_iommu_disable(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200196
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600197 pm_runtime_put_sync(obj->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600198
Florian Vaussard90e569c2014-02-28 14:42:34 -0600199 if (pdata && pdata->assert_reset)
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600200 pdata->assert_reset(pdev, pdata->reset_name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200201}
202
203/*
204 * TLB operations
205 */
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300206static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200207{
Suman Annabd4396f2014-10-22 17:22:27 -0500208 u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
209 u32 mask = get_cam_va_mask(cr->cam & page_size);
210
211 return cr->cam & mask;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200212}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200213
214static u32 get_iopte_attr(struct iotlb_entry *e)
215{
Suman Annabd4396f2014-10-22 17:22:27 -0500216 u32 attr;
217
218 attr = e->mixed << 5;
219 attr |= e->endian;
220 attr |= e->elsz >> 3;
221 attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
222 (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
223 return attr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200224}
225
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300226static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200227{
Suman Annabd4396f2014-10-22 17:22:27 -0500228 u32 status, fault_addr;
229
230 status = iommu_read_reg(obj, MMU_IRQSTATUS);
231 status &= MMU_IRQ_MASK;
232 if (!status) {
233 *da = 0;
234 return 0;
235 }
236
237 fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
238 *da = fault_addr;
239
240 iommu_write_reg(obj, status, MMU_IRQSTATUS);
241
242 return status;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200243}
244
Suman Anna69c2c192015-07-20 17:33:25 -0500245void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200246{
247 u32 val;
248
249 val = iommu_read_reg(obj, MMU_LOCK);
250
251 l->base = MMU_LOCK_BASE(val);
252 l->vict = MMU_LOCK_VICT(val);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200253}
254
Suman Anna69c2c192015-07-20 17:33:25 -0500255void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200256{
257 u32 val;
258
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200259 val = (l->base << MMU_LOCK_BASE_SHIFT);
260 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
261
262 iommu_write_reg(obj, val, MMU_LOCK);
263}
264
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300265static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200266{
Suman Annabd4396f2014-10-22 17:22:27 -0500267 cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
268 cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200269}
270
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300271static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200272{
Suman Annabd4396f2014-10-22 17:22:27 -0500273 iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
274 iommu_write_reg(obj, cr->ram, MMU_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200275
276 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
277 iommu_write_reg(obj, 1, MMU_LD_TLB);
278}
279
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000280/* only used in iotlb iteration for-loop */
Suman Anna69c2c192015-07-20 17:33:25 -0500281struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000282{
283 struct cr_regs cr;
284 struct iotlb_lock l;
285
286 iotlb_lock_get(obj, &l);
287 l.vict = n;
288 iotlb_lock_set(obj, &l);
289 iotlb_read_cr(obj, &cr);
290
291 return cr;
292}
293
Suman Annabd4396f2014-10-22 17:22:27 -0500294#ifdef PREFETCH_IOTLB
295static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
296 struct iotlb_entry *e)
297{
298 struct cr_regs *cr;
299
300 if (!e)
301 return NULL;
302
303 if (e->da & ~(get_cam_va_mask(e->pgsz))) {
304 dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
305 e->da);
306 return ERR_PTR(-EINVAL);
307 }
308
309 cr = kmalloc(sizeof(*cr), GFP_KERNEL);
310 if (!cr)
311 return ERR_PTR(-ENOMEM);
312
313 cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
314 cr->ram = e->pa | e->endian | e->elsz | e->mixed;
315
316 return cr;
317}
318
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200319/**
320 * load_iotlb_entry - Set an iommu tlb entry
321 * @obj: target iommu
322 * @e: an iommu tlb entry info
323 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300324static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200325{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200326 int err = 0;
327 struct iotlb_lock l;
328 struct cr_regs *cr;
329
330 if (!obj || !obj->nr_tlb_entries || !e)
331 return -EINVAL;
332
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600333 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200334
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000335 iotlb_lock_get(obj, &l);
336 if (l.base == obj->nr_tlb_entries) {
337 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200338 err = -EBUSY;
339 goto out;
340 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000341 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000342 int i;
343 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000344
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000345 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000346 if (!iotlb_cr_valid(&tmp))
347 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000348
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000349 if (i == obj->nr_tlb_entries) {
350 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
351 err = -EBUSY;
352 goto out;
353 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000354
355 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000356 } else {
357 l.vict = l.base;
358 iotlb_lock_set(obj, &l);
359 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200360
361 cr = iotlb_alloc_cr(obj, e);
362 if (IS_ERR(cr)) {
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600363 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200364 return PTR_ERR(cr);
365 }
366
367 iotlb_load_cr(obj, cr);
368 kfree(cr);
369
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000370 if (e->prsvd)
371 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200372 /* increment victim for next tlb load */
373 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000374 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200375 iotlb_lock_set(obj, &l);
376out:
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600377 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200378 return err;
379}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200380
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300381#else /* !PREFETCH_IOTLB */
382
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300383static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300384{
385 return 0;
386}
387
388#endif /* !PREFETCH_IOTLB */
389
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300390static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300391{
392 return load_iotlb_entry(obj, e);
393}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200394
395/**
396 * flush_iotlb_page - Clear an iommu tlb entry
397 * @obj: target iommu
398 * @da: iommu device virtual address
399 *
400 * Clear an iommu tlb entry which includes 'da' address.
401 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300402static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200403{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200404 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000405 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200406
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600407 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200408
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000409 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200410 u32 start;
411 size_t bytes;
412
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200413 if (!iotlb_cr_valid(&cr))
414 continue;
415
416 start = iotlb_cr_to_virt(&cr);
417 bytes = iopgsz_to_bytes(cr.cam & 3);
418
419 if ((start <= da) && (da < start + bytes)) {
420 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
421 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000422 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200423 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
Laurent Pinchartf7129a02014-03-07 23:47:03 +0100424 break;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200425 }
426 }
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600427 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200428
429 if (i == obj->nr_tlb_entries)
430 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
431}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200432
433/**
434 * flush_iotlb_all - Clear all iommu tlb entries
435 * @obj: target iommu
436 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300437static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200438{
439 struct iotlb_lock l;
440
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600441 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200442
443 l.base = 0;
444 l.vict = 0;
445 iotlb_lock_set(obj, &l);
446
447 iommu_write_reg(obj, 1, MMU_GFLUSH);
448
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600449 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200450}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200451
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200452/*
453 * H/W pagetable operations
454 */
455static void flush_iopgd_range(u32 *first, u32 *last)
456{
457 /* FIXME: L2 cache should be taken care of if it exists */
458 do {
459 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
460 : : "r" (first));
461 first += L1_CACHE_BYTES / sizeof(*first);
462 } while (first <= last);
463}
464
465static void flush_iopte_range(u32 *first, u32 *last)
466{
467 /* FIXME: L2 cache should be taken care of if it exists */
468 do {
469 asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
470 : : "r" (first));
471 first += L1_CACHE_BYTES / sizeof(*first);
472 } while (first <= last);
473}
474
475static void iopte_free(u32 *iopte)
476{
477 /* Note: freed iopte's must be clean ready for re-use */
Zhouyi Zhoue28045a2014-03-05 18:20:19 +0800478 if (iopte)
479 kmem_cache_free(iopte_cachep, iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200480}
481
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300482static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200483{
484 u32 *iopte;
485
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
501 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
502 flush_iopgd_range(iopgd, iopgd);
503
504 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
505 } else {
506 /* We raced, free the reduniovant table */
507 iopte_free(iopte);
508 }
509
510pte_ready:
511 iopte = iopte_offset(iopgd, da);
512
513 dev_vdbg(obj->dev,
514 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
515 __func__, da, iopgd, *iopgd, iopte, *iopte);
516
517 return iopte;
518}
519
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300520static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200521{
522 u32 *iopgd = iopgd_offset(obj, da);
523
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300524 if ((da | pa) & ~IOSECTION_MASK) {
525 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
526 __func__, da, pa, IOSECTION_SIZE);
527 return -EINVAL;
528 }
529
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200530 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
531 flush_iopgd_range(iopgd, iopgd);
532 return 0;
533}
534
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300535static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200536{
537 u32 *iopgd = iopgd_offset(obj, da);
538 int i;
539
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300540 if ((da | pa) & ~IOSUPER_MASK) {
541 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
542 __func__, da, pa, IOSUPER_SIZE);
543 return -EINVAL;
544 }
545
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200546 for (i = 0; i < 16; i++)
547 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
548 flush_iopgd_range(iopgd, iopgd + 15);
549 return 0;
550}
551
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300552static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200553{
554 u32 *iopgd = iopgd_offset(obj, da);
555 u32 *iopte = iopte_alloc(obj, iopgd, da);
556
557 if (IS_ERR(iopte))
558 return PTR_ERR(iopte);
559
560 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
561 flush_iopte_range(iopte, iopte);
562
563 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
564 __func__, da, pa, iopte, *iopte);
565
566 return 0;
567}
568
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300569static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200570{
571 u32 *iopgd = iopgd_offset(obj, da);
572 u32 *iopte = iopte_alloc(obj, iopgd, da);
573 int i;
574
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300575 if ((da | pa) & ~IOLARGE_MASK) {
576 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
577 __func__, da, pa, IOLARGE_SIZE);
578 return -EINVAL;
579 }
580
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200581 if (IS_ERR(iopte))
582 return PTR_ERR(iopte);
583
584 for (i = 0; i < 16; i++)
585 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
586 flush_iopte_range(iopte, iopte + 15);
587 return 0;
588}
589
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300590static int
591iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200592{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300593 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200594 u32 prot;
595 int err;
596
597 if (!obj || !e)
598 return -EINVAL;
599
600 switch (e->pgsz) {
601 case MMU_CAM_PGSZ_16M:
602 fn = iopgd_alloc_super;
603 break;
604 case MMU_CAM_PGSZ_1M:
605 fn = iopgd_alloc_section;
606 break;
607 case MMU_CAM_PGSZ_64K:
608 fn = iopte_alloc_large;
609 break;
610 case MMU_CAM_PGSZ_4K:
611 fn = iopte_alloc_page;
612 break;
613 default:
614 fn = NULL;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200615 break;
616 }
617
Suman Anna7c1ab602016-04-04 17:46:19 -0500618 if (WARN_ON(!fn))
619 return -EINVAL;
620
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200621 prot = get_iopte_attr(e);
622
623 spin_lock(&obj->page_table_lock);
624 err = fn(obj, e->da, e->pa, prot);
625 spin_unlock(&obj->page_table_lock);
626
627 return err;
628}
629
630/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300631 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200632 * @obj: target iommu
633 * @e: an iommu tlb entry info
634 **/
Suman Anna4899a562014-10-22 17:22:32 -0500635static int
636omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200637{
638 int err;
639
640 flush_iotlb_page(obj, e->da);
641 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200642 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300643 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200644 return err;
645}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200646
647/**
648 * iopgtable_lookup_entry - Lookup an iommu pte entry
649 * @obj: target iommu
650 * @da: iommu device virtual address
651 * @ppgd: iommu pgd entry pointer to be returned
652 * @ppte: iommu pte entry pointer to be returned
653 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300654static void
655iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200656{
657 u32 *iopgd, *iopte = NULL;
658
659 iopgd = iopgd_offset(obj, da);
660 if (!*iopgd)
661 goto out;
662
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300663 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200664 iopte = iopte_offset(iopgd, da);
665out:
666 *ppgd = iopgd;
667 *ppte = iopte;
668}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200669
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300670static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200671{
672 size_t bytes;
673 u32 *iopgd = iopgd_offset(obj, da);
674 int nent = 1;
675
676 if (!*iopgd)
677 return 0;
678
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300679 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200680 int i;
681 u32 *iopte = iopte_offset(iopgd, da);
682
683 bytes = IOPTE_SIZE;
684 if (*iopte & IOPTE_LARGE) {
685 nent *= 16;
686 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800687 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200688 }
689 bytes *= nent;
690 memset(iopte, 0, nent * sizeof(*iopte));
691 flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
692
693 /*
694 * do table walk to check if this table is necessary or not
695 */
696 iopte = iopte_offset(iopgd, 0);
697 for (i = 0; i < PTRS_PER_IOPTE; i++)
698 if (iopte[i])
699 goto out;
700
701 iopte_free(iopte);
702 nent = 1; /* for the next L1 entry */
703 } else {
704 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700705 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200706 nent *= 16;
707 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800708 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200709 }
710 bytes *= nent;
711 }
712 memset(iopgd, 0, nent * sizeof(*iopgd));
713 flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
714out:
715 return bytes;
716}
717
718/**
719 * iopgtable_clear_entry - Remove an iommu pte entry
720 * @obj: target iommu
721 * @da: iommu device virtual address
722 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300723static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200724{
725 size_t bytes;
726
727 spin_lock(&obj->page_table_lock);
728
729 bytes = iopgtable_clear_entry_core(obj, da);
730 flush_iotlb_page(obj, da);
731
732 spin_unlock(&obj->page_table_lock);
733
734 return bytes;
735}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200736
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300737static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200738{
739 int i;
740
741 spin_lock(&obj->page_table_lock);
742
743 for (i = 0; i < PTRS_PER_IOPGD; i++) {
744 u32 da;
745 u32 *iopgd;
746
747 da = i << IOPGD_SHIFT;
748 iopgd = iopgd_offset(obj, da);
749
750 if (!*iopgd)
751 continue;
752
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300753 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200754 iopte_free(iopte_offset(iopgd, 0));
755
756 *iopgd = 0;
757 flush_iopgd_range(iopgd, iopgd);
758 }
759
760 flush_iotlb_all(obj);
761
762 spin_unlock(&obj->page_table_lock);
763}
764
765/*
766 * Device IOMMU generic operations
767 */
768static irqreturn_t iommu_fault_handler(int irq, void *data)
769{
David Cohend594f1f2011-02-16 19:35:51 +0000770 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200771 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300772 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400773 struct iommu_domain *domain = obj->domain;
Joerg Roedel8cf851e2015-03-26 13:43:09 +0100774 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200775
Suman Anna2088ecb2014-10-22 17:22:19 -0500776 if (!omap_domain->iommu_dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200777 return IRQ_NONE;
778
David Cohend594f1f2011-02-16 19:35:51 +0000779 errs = iommu_report_fault(obj, &da);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200780 if (errs == 0)
781 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000782
783 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400784 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200785 return IRQ_HANDLED;
786
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000787 iommu_disable(obj);
788
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200789 iopgd = iopgd_offset(obj, da);
790
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300791 if (!iopgd_is_table(*iopgd)) {
Suman Annab6c2e092013-05-30 18:10:59 -0500792 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 -0500793 obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200794 return IRQ_NONE;
795 }
796
797 iopte = iopte_offset(iopgd, da);
798
Suman Annab6c2e092013-05-30 18:10:59 -0500799 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 -0500800 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200801
802 return IRQ_NONE;
803}
804
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200805/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300806 * omap_iommu_attach() - attach iommu device to an iommu domain
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500807 * @obj: target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300808 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200809 **/
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500810static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200811{
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600812 int err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200813
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300814 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200815
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300816 obj->iopgd = iopgd;
817 err = iommu_enable(obj);
818 if (err)
819 goto err_enable;
820 flush_iotlb_all(obj);
821
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300822 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200823
824 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500825
826 return 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200827
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200828err_enable:
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300829 spin_unlock(&obj->iommu_lock);
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500830
831 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200832}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200833
834/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300835 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200836 * @obj: target iommu
837 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300838static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200839{
Roel Kluinacf9d462010-01-08 10:29:05 -0800840 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200841 return;
842
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300843 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200844
Suman Anna2088ecb2014-10-22 17:22:19 -0500845 iommu_disable(obj);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300846 obj->iopgd = NULL;
847
848 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200849
850 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
851}
David Cohend594f1f2011-02-16 19:35:51 +0000852
Suman Anna3ca92992015-10-02 18:02:44 -0500853static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device *pdev,
854 struct omap_iommu *obj)
855{
856 struct device_node *np = pdev->dev.of_node;
857 int ret;
858
859 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
860 return 0;
861
862 if (!of_property_read_bool(np, "ti,syscon-mmuconfig")) {
863 dev_err(&pdev->dev, "ti,syscon-mmuconfig property is missing\n");
864 return -EINVAL;
865 }
866
867 obj->syscfg =
868 syscon_regmap_lookup_by_phandle(np, "ti,syscon-mmuconfig");
869 if (IS_ERR(obj->syscfg)) {
870 /* can fail with -EPROBE_DEFER */
871 ret = PTR_ERR(obj->syscfg);
872 return ret;
873 }
874
875 if (of_property_read_u32_index(np, "ti,syscon-mmuconfig", 1,
876 &obj->id)) {
877 dev_err(&pdev->dev, "couldn't get the IOMMU instance id within subsystem\n");
878 return -EINVAL;
879 }
880
881 if (obj->id != 0 && obj->id != 1) {
882 dev_err(&pdev->dev, "invalid IOMMU instance id\n");
883 return -EINVAL;
884 }
885
886 return 0;
887}
888
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200889/*
890 * OMAP Device MMU(IOMMU) detection
891 */
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -0800892static int omap_iommu_probe(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200893{
894 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200895 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300896 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200897 struct resource *res;
Florian Vaussard3c927482014-02-28 14:42:36 -0600898 struct device_node *of = pdev->dev.of_node;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200899
Suman Anna49a57ef2017-04-12 00:21:27 -0500900 if (!of) {
901 pr_err("%s: only DT-based devices are supported\n", __func__);
902 return -ENODEV;
903 }
904
Suman Annaf129b3d2014-02-28 14:42:32 -0600905 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200906 if (!obj)
907 return -ENOMEM;
908
Suman Anna49a57ef2017-04-12 00:21:27 -0500909 obj->name = dev_name(&pdev->dev);
910 obj->nr_tlb_entries = 32;
911 err = of_property_read_u32(of, "ti,#tlb-entries", &obj->nr_tlb_entries);
912 if (err && err != -EINVAL)
913 return err;
914 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
915 return -EINVAL;
916 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
917 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
Florian Vaussard3c927482014-02-28 14:42:36 -0600918
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200919 obj->dev = &pdev->dev;
920 obj->ctx = (void *)obj + sizeof(*obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200921
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300922 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200923 spin_lock_init(&obj->page_table_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200924
925 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -0600926 obj->regbase = devm_ioremap_resource(obj->dev, res);
927 if (IS_ERR(obj->regbase))
928 return PTR_ERR(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +0000929
Suman Anna3ca92992015-10-02 18:02:44 -0500930 err = omap_iommu_dra7_get_dsp_system_cfg(pdev, obj);
931 if (err)
932 return err;
933
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200934 irq = platform_get_irq(pdev, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -0600935 if (irq < 0)
936 return -ENODEV;
937
938 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
939 dev_name(obj->dev), obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200940 if (err < 0)
Suman Annaf129b3d2014-02-28 14:42:32 -0600941 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200942 platform_set_drvdata(pdev, obj);
943
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600944 pm_runtime_irq_safe(obj->dev);
945 pm_runtime_enable(obj->dev);
946
Suman Anna61c75352014-10-22 17:22:30 -0500947 omap_iommu_debugfs_add(obj);
948
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200949 dev_info(&pdev->dev, "%s registered\n", obj->name);
950 return 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200951}
952
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -0800953static int omap_iommu_remove(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200954{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300955 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200956
Suman Anna61c75352014-10-22 17:22:30 -0500957 omap_iommu_debugfs_remove(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200958
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600959 pm_runtime_disable(obj->dev);
960
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200961 dev_info(&pdev->dev, "%s removed\n", obj->name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200962 return 0;
963}
964
Kiran Padwald943b0f2014-09-11 19:07:36 +0530965static const struct of_device_id omap_iommu_of_match[] = {
Florian Vaussard3c927482014-02-28 14:42:36 -0600966 { .compatible = "ti,omap2-iommu" },
967 { .compatible = "ti,omap4-iommu" },
968 { .compatible = "ti,dra7-iommu" },
Suman Anna3ca92992015-10-02 18:02:44 -0500969 { .compatible = "ti,dra7-dsp-iommu" },
Florian Vaussard3c927482014-02-28 14:42:36 -0600970 {},
971};
Florian Vaussard3c927482014-02-28 14:42:36 -0600972
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200973static struct platform_driver omap_iommu_driver = {
974 .probe = omap_iommu_probe,
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -0800975 .remove = omap_iommu_remove,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200976 .driver = {
977 .name = "omap-iommu",
Florian Vaussard3c927482014-02-28 14:42:36 -0600978 .of_match_table = of_match_ptr(omap_iommu_of_match),
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200979 },
980};
981
982static void iopte_cachep_ctor(void *iopte)
983{
984 clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
985}
986
Laurent Pinchart286f6002014-03-08 00:44:38 +0100987static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
Tony Lindgrened1c7de2012-11-02 12:24:06 -0700988{
989 memset(e, 0, sizeof(*e));
990
991 e->da = da;
992 e->pa = pa;
Suman Annad760e3e2014-03-17 20:31:32 -0500993 e->valid = MMU_CAM_V;
Laurent Pinchart286f6002014-03-08 00:44:38 +0100994 e->pgsz = pgsz;
995 e->endian = MMU_RAM_ENDIAN_LITTLE;
996 e->elsz = MMU_RAM_ELSZ_8;
997 e->mixed = 0;
Tony Lindgrened1c7de2012-11-02 12:24:06 -0700998
999 return iopgsz_to_bytes(e->pgsz);
1000}
1001
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001002static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Suman Anna5835b6a2015-07-20 17:33:32 -05001003 phys_addr_t pa, size_t bytes, int prot)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001004{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001005 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001006 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001007 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001008 struct iotlb_entry e;
1009 int omap_pgsz;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001010 u32 ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001011
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001012 omap_pgsz = bytes_to_iopgsz(bytes);
1013 if (omap_pgsz < 0) {
1014 dev_err(dev, "invalid size to map: %d\n", bytes);
1015 return -EINVAL;
1016 }
1017
Joerg Roedel1d7f4492015-01-22 14:42:06 +01001018 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 +03001019
Laurent Pinchart286f6002014-03-08 00:44:38 +01001020 iotlb_init_entry(&e, da, pa, omap_pgsz);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001021
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001022 ret = omap_iopgtable_store_entry(oiommu, &e);
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001023 if (ret)
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001024 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001025
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001026 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001027}
1028
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001029static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
Suman Anna5835b6a2015-07-20 17:33:32 -05001030 size_t size)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001031{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001032 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001033 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001034 struct device *dev = oiommu->dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001035
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001036 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001037
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001038 return iopgtable_clear_entry(oiommu, da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001039}
1040
1041static int
1042omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1043{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001044 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001045 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001046 struct omap_iommu *oiommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001047 int ret = 0;
1048
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001049 if (!arch_data || !arch_data->iommu_dev) {
Suman Annae3f595b2014-09-04 17:27:29 -05001050 dev_err(dev, "device doesn't have an associated iommu\n");
1051 return -EINVAL;
1052 }
1053
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001054 spin_lock(&omap_domain->lock);
1055
1056 /* only a single device is supported per domain for now */
1057 if (omap_domain->iommu_dev) {
1058 dev_err(dev, "iommu domain is already attached\n");
1059 ret = -EBUSY;
1060 goto out;
1061 }
1062
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001063 oiommu = arch_data->iommu_dev;
1064
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001065 /* get a handle to and enable the omap iommu */
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001066 ret = omap_iommu_attach(oiommu, omap_domain->pgtable);
1067 if (ret) {
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001068 dev_err(dev, "can't get omap iommu: %d\n", ret);
1069 goto out;
1070 }
1071
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001072 omap_domain->iommu_dev = oiommu;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001073 omap_domain->dev = dev;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -04001074 oiommu->domain = domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001075
1076out:
1077 spin_unlock(&omap_domain->lock);
1078 return ret;
1079}
1080
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001081static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001082 struct device *dev)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001083{
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001084 struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001085
1086 /* only a single device is supported per domain for now */
1087 if (omap_domain->iommu_dev != oiommu) {
1088 dev_err(dev, "invalid iommu device\n");
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001089 return;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001090 }
1091
1092 iopgtable_clear_entry_all(oiommu);
1093
1094 omap_iommu_detach(oiommu);
1095
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001096 omap_domain->iommu_dev = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001097 omap_domain->dev = NULL;
Suman Annaf24d9ad2014-10-22 17:22:33 -05001098 oiommu->domain = NULL;
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001099}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001100
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001101static void omap_iommu_detach_dev(struct iommu_domain *domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001102 struct device *dev)
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001103{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001104 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001105
1106 spin_lock(&omap_domain->lock);
1107 _omap_iommu_detach_dev(omap_domain, dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001108 spin_unlock(&omap_domain->lock);
1109}
1110
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001111static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001112{
1113 struct omap_iommu_domain *omap_domain;
1114
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001115 if (type != IOMMU_DOMAIN_UNMANAGED)
1116 return NULL;
1117
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001118 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
Suman Anna99ee98d2015-07-20 17:33:29 -05001119 if (!omap_domain)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001120 goto out;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001121
1122 omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
Suman Anna99ee98d2015-07-20 17:33:29 -05001123 if (!omap_domain->pgtable)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001124 goto fail_nomem;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001125
1126 /*
1127 * should never fail, but please keep this around to ensure
1128 * we keep the hardware happy
1129 */
Suman Anna433c4342016-04-04 17:46:20 -05001130 if (WARN_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE)))
1131 goto fail_align;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001132
1133 clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
1134 spin_lock_init(&omap_domain->lock);
1135
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001136 omap_domain->domain.geometry.aperture_start = 0;
1137 omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
1138 omap_domain->domain.geometry.force_aperture = true;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001139
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001140 return &omap_domain->domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001141
Suman Anna433c4342016-04-04 17:46:20 -05001142fail_align:
1143 kfree(omap_domain->pgtable);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001144fail_nomem:
1145 kfree(omap_domain);
1146out:
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001147 return NULL;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001148}
1149
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001150static void omap_iommu_domain_free(struct iommu_domain *domain)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001151{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001152 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001153
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001154 /*
1155 * An iommu device is still attached
1156 * (currently, only one device can be attached) ?
1157 */
1158 if (omap_domain->iommu_dev)
1159 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1160
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001161 kfree(omap_domain->pgtable);
1162 kfree(omap_domain);
1163}
1164
1165static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001166 dma_addr_t da)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001167{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001168 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001169 struct omap_iommu *oiommu = omap_domain->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001170 struct device *dev = oiommu->dev;
1171 u32 *pgd, *pte;
1172 phys_addr_t ret = 0;
1173
1174 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1175
1176 if (pte) {
1177 if (iopte_is_small(*pte))
1178 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1179 else if (iopte_is_large(*pte))
1180 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1181 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001182 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
Suman Anna5835b6a2015-07-20 17:33:32 -05001183 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001184 } else {
1185 if (iopgd_is_section(*pgd))
1186 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1187 else if (iopgd_is_super(*pgd))
1188 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1189 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001190 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
Suman Anna5835b6a2015-07-20 17:33:32 -05001191 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001192 }
1193
1194 return ret;
1195}
1196
Laurent Pinchart07a02032014-02-28 14:42:38 -06001197static int omap_iommu_add_device(struct device *dev)
1198{
1199 struct omap_iommu_arch_data *arch_data;
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001200 struct omap_iommu *oiommu;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001201 struct device_node *np;
Suman Anna7d682772014-09-04 17:27:30 -05001202 struct platform_device *pdev;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001203
1204 /*
1205 * Allocate the archdata iommu structure for DT-based devices.
1206 *
1207 * TODO: Simplify this when removing non-DT support completely from the
1208 * IOMMU users.
1209 */
1210 if (!dev->of_node)
1211 return 0;
1212
1213 np = of_parse_phandle(dev->of_node, "iommus", 0);
1214 if (!np)
1215 return 0;
1216
Suman Anna7d682772014-09-04 17:27:30 -05001217 pdev = of_find_device_by_node(np);
1218 if (WARN_ON(!pdev)) {
1219 of_node_put(np);
1220 return -EINVAL;
1221 }
1222
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001223 oiommu = platform_get_drvdata(pdev);
1224 if (!oiommu) {
1225 of_node_put(np);
1226 return -EINVAL;
1227 }
1228
Laurent Pinchart07a02032014-02-28 14:42:38 -06001229 arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
1230 if (!arch_data) {
1231 of_node_put(np);
1232 return -ENOMEM;
1233 }
1234
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001235 arch_data->iommu_dev = oiommu;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001236 dev->archdata.iommu = arch_data;
1237
1238 of_node_put(np);
1239
1240 return 0;
1241}
1242
1243static void omap_iommu_remove_device(struct device *dev)
1244{
1245 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1246
1247 if (!dev->of_node || !arch_data)
1248 return;
1249
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001250 dev->archdata.iommu = NULL;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001251 kfree(arch_data);
1252}
1253
Thierry Redingb22f6432014-06-27 09:03:12 +02001254static const struct iommu_ops omap_iommu_ops = {
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001255 .domain_alloc = omap_iommu_domain_alloc,
1256 .domain_free = omap_iommu_domain_free,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001257 .attach_dev = omap_iommu_attach_dev,
1258 .detach_dev = omap_iommu_detach_dev,
1259 .map = omap_iommu_map,
1260 .unmap = omap_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07001261 .map_sg = default_iommu_map_sg,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001262 .iova_to_phys = omap_iommu_iova_to_phys,
Laurent Pinchart07a02032014-02-28 14:42:38 -06001263 .add_device = omap_iommu_add_device,
1264 .remove_device = omap_iommu_remove_device,
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +02001265 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001266};
1267
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001268static int __init omap_iommu_init(void)
1269{
1270 struct kmem_cache *p;
1271 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1272 size_t align = 1 << 10; /* L2 pagetable alignement */
Thierry Redingf938aab2015-02-06 11:44:06 +01001273 struct device_node *np;
Suman Annaabaa7e52017-04-12 00:21:26 -05001274 int ret;
Thierry Redingf938aab2015-02-06 11:44:06 +01001275
1276 np = of_find_matching_node(NULL, omap_iommu_of_match);
1277 if (!np)
1278 return 0;
1279
1280 of_node_put(np);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001281
1282 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
1283 iopte_cachep_ctor);
1284 if (!p)
1285 return -ENOMEM;
1286 iopte_cachep = p;
1287
Suman Anna61c75352014-10-22 17:22:30 -05001288 omap_iommu_debugfs_init();
1289
Suman Annaabaa7e52017-04-12 00:21:26 -05001290 ret = platform_driver_register(&omap_iommu_driver);
1291 if (ret) {
1292 pr_err("%s: failed to register driver\n", __func__);
1293 goto fail_driver;
1294 }
1295
1296 ret = bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
1297 if (ret)
1298 goto fail_bus;
1299
1300 return 0;
1301
1302fail_bus:
1303 platform_driver_unregister(&omap_iommu_driver);
1304fail_driver:
1305 kmem_cache_destroy(iopte_cachep);
1306 return ret;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001307}
Ohad Ben-Cohen435792d2012-02-26 12:14:14 +02001308subsys_initcall(omap_iommu_init);
Suman Anna0cdbf722015-07-20 17:33:24 -05001309/* must be ready before omap3isp is probed */