blob: 91749654fd4909d0bfc1e7536908439eeefa7d60 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02002/*
3 * omap iommu: tlb and pagetable primitives
4 *
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -08005 * Copyright (C) 2008-2010 Nokia Corporation
Alexander A. Klimovf512eef2020-07-08 23:04:34 +02006 * Copyright (C) 2013-2017 Texas Instruments Incorporated - https://www.ti.com/
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02007 *
8 * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>,
9 * Paul Mundt and Toshihiro Kobayashi
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020010 */
11
Josue Albarranbfee0cf2017-07-28 15:49:14 -050012#include <linux/dma-mapping.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020013#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020015#include <linux/interrupt.h>
16#include <linux/ioport.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020017#include <linux/platform_device.h>
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030018#include <linux/iommu.h>
Tony Lindgrenc8d35c82012-11-02 12:24:03 -070019#include <linux/omap-iommu.h>
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030020#include <linux/mutex.h>
21#include <linux/spinlock.h>
Tony Lindgrened1c7de2012-11-02 12:24:06 -070022#include <linux/io.h>
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -060023#include <linux/pm_runtime.h>
Florian Vaussard3c927482014-02-28 14:42:36 -060024#include <linux/of.h>
Florian Vaussard3c927482014-02-28 14:42:36 -060025#include <linux/of_irq.h>
Suman Anna7d682772014-09-04 17:27:30 -050026#include <linux/of_platform.h>
Suman Anna3ca92992015-10-02 18:02:44 -050027#include <linux/regmap.h>
28#include <linux/mfd/syscon.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020029
Tony Lindgren2ab7c842012-11-02 12:24:14 -070030#include <linux/platform_data/iommu-omap.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020031
Ido Yariv2f7702a2012-11-02 12:24:00 -070032#include "omap-iopgtable.h"
Tony Lindgrened1c7de2012-11-02 12:24:06 -070033#include "omap-iommu.h"
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020034
Joerg Roedel01611fe2017-04-12 00:21:30 -050035static const struct iommu_ops omap_iommu_ops;
36
Kefeng Wang6e8b5662019-04-23 15:50:08 +080037#define to_iommu(dev) ((struct omap_iommu *)dev_get_drvdata(dev))
Suman Anna5acc97d2014-03-17 20:31:34 -050038
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +020039/* bitmap of the page sizes currently supported */
40#define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
41
Ido Yariv7bd9e252012-11-02 12:24:09 -070042#define MMU_LOCK_BASE_SHIFT 10
43#define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
44#define MMU_LOCK_BASE(x) \
45 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
46
47#define MMU_LOCK_VICT_SHIFT 4
48#define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
49#define MMU_LOCK_VICT(x) \
50 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
51
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020052static struct platform_driver omap_iommu_driver;
53static struct kmem_cache *iopte_cachep;
54
55/**
Joerg Roedel8cf851e2015-03-26 13:43:09 +010056 * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
57 * @dom: generic iommu domain handle
58 **/
59static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
60{
61 return container_of(dom, struct omap_iommu_domain, domain);
62}
63
64/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030065 * omap_iommu_save_ctx - Save registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020066 * @dev: client device
Suman Annac4206c42019-08-07 11:26:49 +030067 *
68 * This should be treated as an deprecated API. It is preserved only
69 * to maintain existing functionality for OMAP3 ISP driver.
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{
Joerg Roedel97ea1202020-06-25 15:08:27 +020073 struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
Suman Anna9d5018d2017-09-05 17:56:18 -050074 struct omap_iommu *obj;
75 u32 *p;
Suman Annabd4396f2014-10-22 17:22:27 -050076 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020077
Suman Anna9d5018d2017-09-05 17:56:18 -050078 if (!arch_data)
79 return;
80
81 while (arch_data->iommu_dev) {
82 obj = arch_data->iommu_dev;
83 p = obj->ctx;
84 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
85 p[i] = iommu_read_reg(obj, i * sizeof(u32));
86 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i,
87 p[i]);
88 }
89 arch_data++;
Suman Annabd4396f2014-10-22 17:22:27 -050090 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020091}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030092EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020093
94/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030095 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020096 * @dev: client device
Suman Annac4206c42019-08-07 11:26:49 +030097 *
98 * This should be treated as an deprecated API. It is preserved only
99 * to maintain existing functionality for OMAP3 ISP driver.
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200100 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200101void omap_iommu_restore_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200102{
Joerg Roedel97ea1202020-06-25 15:08:27 +0200103 struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
Suman Anna9d5018d2017-09-05 17:56:18 -0500104 struct omap_iommu *obj;
105 u32 *p;
Suman Annabd4396f2014-10-22 17:22:27 -0500106 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200107
Suman Anna9d5018d2017-09-05 17:56:18 -0500108 if (!arch_data)
109 return;
110
111 while (arch_data->iommu_dev) {
112 obj = arch_data->iommu_dev;
113 p = obj->ctx;
114 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
115 iommu_write_reg(obj, p[i], i * sizeof(u32));
116 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i,
117 p[i]);
118 }
119 arch_data++;
Suman Annabd4396f2014-10-22 17:22:27 -0500120 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200121}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300122EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200123
Suman Anna3ca92992015-10-02 18:02:44 -0500124static void dra7_cfg_dspsys_mmu(struct omap_iommu *obj, bool enable)
125{
126 u32 val, mask;
127
128 if (!obj->syscfg)
129 return;
130
131 mask = (1 << (obj->id * DSP_SYS_MMU_CONFIG_EN_SHIFT));
132 val = enable ? mask : 0;
133 regmap_update_bits(obj->syscfg, DSP_SYS_MMU_CONFIG, mask, val);
134}
135
Suman Annabd4396f2014-10-22 17:22:27 -0500136static void __iommu_set_twl(struct omap_iommu *obj, bool on)
137{
138 u32 l = iommu_read_reg(obj, MMU_CNTL);
139
140 if (on)
141 iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
142 else
143 iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
144
145 l &= ~MMU_CNTL_MASK;
146 if (on)
147 l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
148 else
149 l |= (MMU_CNTL_MMU_EN);
150
151 iommu_write_reg(obj, l, MMU_CNTL);
152}
153
154static int omap2_iommu_enable(struct omap_iommu *obj)
155{
156 u32 l, pa;
157
Krzysztof Kozlowskif2ce16c2020-03-03 21:27:48 +0100158 if (!obj->iopgd || !IS_ALIGNED((unsigned long)obj->iopgd, SZ_16K))
Suman Annabd4396f2014-10-22 17:22:27 -0500159 return -EINVAL;
160
161 pa = virt_to_phys(obj->iopgd);
162 if (!IS_ALIGNED(pa, SZ_16K))
163 return -EINVAL;
164
165 l = iommu_read_reg(obj, MMU_REVISION);
166 dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
167 (l >> 4) & 0xf, l & 0xf);
168
169 iommu_write_reg(obj, pa, MMU_TTB);
170
Suman Anna3ca92992015-10-02 18:02:44 -0500171 dra7_cfg_dspsys_mmu(obj, true);
172
Suman Annabd4396f2014-10-22 17:22:27 -0500173 if (obj->has_bus_err_back)
174 iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
175
176 __iommu_set_twl(obj, true);
177
178 return 0;
179}
180
181static void omap2_iommu_disable(struct omap_iommu *obj)
182{
183 u32 l = iommu_read_reg(obj, MMU_CNTL);
184
185 l &= ~MMU_CNTL_MASK;
186 iommu_write_reg(obj, l, MMU_CNTL);
Suman Anna3ca92992015-10-02 18:02:44 -0500187 dra7_cfg_dspsys_mmu(obj, false);
Suman Annabd4396f2014-10-22 17:22:27 -0500188
189 dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
190}
191
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300192static int iommu_enable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200193{
Suman Annadb8918f2019-08-07 11:26:47 +0300194 int ret;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200195
Suman Annadb8918f2019-08-07 11:26:47 +0300196 ret = pm_runtime_get_sync(obj->dev);
197 if (ret < 0)
198 pm_runtime_put_noidle(obj->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600199
Suman Annadb8918f2019-08-07 11:26:47 +0300200 return ret < 0 ? ret : 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200201}
202
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300203static void iommu_disable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200204{
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600205 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200206}
207
208/*
209 * TLB operations
210 */
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300211static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200212{
Suman Annabd4396f2014-10-22 17:22:27 -0500213 u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
214 u32 mask = get_cam_va_mask(cr->cam & page_size);
215
216 return cr->cam & mask;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200217}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200218
219static u32 get_iopte_attr(struct iotlb_entry *e)
220{
Suman Annabd4396f2014-10-22 17:22:27 -0500221 u32 attr;
222
223 attr = e->mixed << 5;
224 attr |= e->endian;
225 attr |= e->elsz >> 3;
226 attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
227 (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
228 return attr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200229}
230
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300231static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200232{
Suman Annabd4396f2014-10-22 17:22:27 -0500233 u32 status, fault_addr;
234
235 status = iommu_read_reg(obj, MMU_IRQSTATUS);
236 status &= MMU_IRQ_MASK;
237 if (!status) {
238 *da = 0;
239 return 0;
240 }
241
242 fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
243 *da = fault_addr;
244
245 iommu_write_reg(obj, status, MMU_IRQSTATUS);
246
247 return status;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200248}
249
Suman Anna69c2c192015-07-20 17:33:25 -0500250void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200251{
252 u32 val;
253
254 val = iommu_read_reg(obj, MMU_LOCK);
255
256 l->base = MMU_LOCK_BASE(val);
257 l->vict = MMU_LOCK_VICT(val);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200258}
259
Suman Anna69c2c192015-07-20 17:33:25 -0500260void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200261{
262 u32 val;
263
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200264 val = (l->base << MMU_LOCK_BASE_SHIFT);
265 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
266
267 iommu_write_reg(obj, val, MMU_LOCK);
268}
269
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300270static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200271{
Suman Annabd4396f2014-10-22 17:22:27 -0500272 cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
273 cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200274}
275
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300276static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200277{
Suman Annabd4396f2014-10-22 17:22:27 -0500278 iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
279 iommu_write_reg(obj, cr->ram, MMU_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200280
281 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
282 iommu_write_reg(obj, 1, MMU_LD_TLB);
283}
284
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000285/* only used in iotlb iteration for-loop */
Suman Anna69c2c192015-07-20 17:33:25 -0500286struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000287{
288 struct cr_regs cr;
289 struct iotlb_lock l;
290
291 iotlb_lock_get(obj, &l);
292 l.vict = n;
293 iotlb_lock_set(obj, &l);
294 iotlb_read_cr(obj, &cr);
295
296 return cr;
297}
298
Suman Annabd4396f2014-10-22 17:22:27 -0500299#ifdef PREFETCH_IOTLB
300static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
301 struct iotlb_entry *e)
302{
303 struct cr_regs *cr;
304
305 if (!e)
306 return NULL;
307
308 if (e->da & ~(get_cam_va_mask(e->pgsz))) {
309 dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
310 e->da);
311 return ERR_PTR(-EINVAL);
312 }
313
314 cr = kmalloc(sizeof(*cr), GFP_KERNEL);
315 if (!cr)
316 return ERR_PTR(-ENOMEM);
317
318 cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
319 cr->ram = e->pa | e->endian | e->elsz | e->mixed;
320
321 return cr;
322}
323
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200324/**
325 * load_iotlb_entry - Set an iommu tlb entry
326 * @obj: target iommu
327 * @e: an iommu tlb entry info
328 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300329static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200330{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200331 int err = 0;
332 struct iotlb_lock l;
333 struct cr_regs *cr;
334
335 if (!obj || !obj->nr_tlb_entries || !e)
336 return -EINVAL;
337
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600338 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200339
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000340 iotlb_lock_get(obj, &l);
341 if (l.base == obj->nr_tlb_entries) {
342 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200343 err = -EBUSY;
344 goto out;
345 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000346 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000347 int i;
348 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000349
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000350 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000351 if (!iotlb_cr_valid(&tmp))
352 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000353
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000354 if (i == obj->nr_tlb_entries) {
355 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
356 err = -EBUSY;
357 goto out;
358 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000359
360 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000361 } else {
362 l.vict = l.base;
363 iotlb_lock_set(obj, &l);
364 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200365
366 cr = iotlb_alloc_cr(obj, e);
367 if (IS_ERR(cr)) {
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600368 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200369 return PTR_ERR(cr);
370 }
371
372 iotlb_load_cr(obj, cr);
373 kfree(cr);
374
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000375 if (e->prsvd)
376 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200377 /* increment victim for next tlb load */
378 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000379 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200380 iotlb_lock_set(obj, &l);
381out:
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600382 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200383 return err;
384}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200385
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300386#else /* !PREFETCH_IOTLB */
387
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300388static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300389{
390 return 0;
391}
392
393#endif /* !PREFETCH_IOTLB */
394
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300395static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300396{
397 return load_iotlb_entry(obj, e);
398}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200399
400/**
401 * flush_iotlb_page - Clear an iommu tlb entry
402 * @obj: target iommu
403 * @da: iommu device virtual address
404 *
405 * Clear an iommu tlb entry which includes 'da' address.
406 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300407static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200408{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200409 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000410 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200411
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600412 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200413
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000414 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200415 u32 start;
416 size_t bytes;
417
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200418 if (!iotlb_cr_valid(&cr))
419 continue;
420
421 start = iotlb_cr_to_virt(&cr);
422 bytes = iopgsz_to_bytes(cr.cam & 3);
423
424 if ((start <= da) && (da < start + bytes)) {
Krzysztof Kozlowski6135a892020-03-03 21:27:49 +0100425 dev_dbg(obj->dev, "%s: %08x<=%08x(%zx)\n",
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200426 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000427 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200428 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
Laurent Pinchartf7129a02014-03-07 23:47:03 +0100429 break;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200430 }
431 }
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600432 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200433
434 if (i == obj->nr_tlb_entries)
435 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
436}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200437
438/**
439 * flush_iotlb_all - Clear all iommu tlb entries
440 * @obj: target iommu
441 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300442static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200443{
444 struct iotlb_lock l;
445
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600446 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200447
448 l.base = 0;
449 l.vict = 0;
450 iotlb_lock_set(obj, &l);
451
452 iommu_write_reg(obj, 1, MMU_GFLUSH);
453
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600454 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200455}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200456
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200457/*
458 * H/W pagetable operations
459 */
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500460static void flush_iopte_range(struct device *dev, dma_addr_t dma,
461 unsigned long offset, int num_entries)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200462{
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500463 size_t size = num_entries * sizeof(u32);
464
465 dma_sync_single_range_for_device(dev, dma, offset, size, DMA_TO_DEVICE);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200466}
467
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500468static void iopte_free(struct omap_iommu *obj, u32 *iopte, bool dma_valid)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200469{
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500470 dma_addr_t pt_dma;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200471
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200472 /* Note: freed iopte's must be clean ready for re-use */
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500473 if (iopte) {
474 if (dma_valid) {
475 pt_dma = virt_to_phys(iopte);
476 dma_unmap_single(obj->dev, pt_dma, IOPTE_TABLE_SIZE,
477 DMA_TO_DEVICE);
478 }
479
Zhouyi Zhoue28045a2014-03-05 18:20:19 +0800480 kmem_cache_free(iopte_cachep, iopte);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500481 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200482}
483
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500484static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd,
485 dma_addr_t *pt_dma, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200486{
487 u32 *iopte;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500488 unsigned long offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200489
490 /* a table has already existed */
491 if (*iopgd)
492 goto pte_ready;
493
494 /*
495 * do the allocation outside the page table lock
496 */
497 spin_unlock(&obj->page_table_lock);
498 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
499 spin_lock(&obj->page_table_lock);
500
501 if (!*iopgd) {
502 if (!iopte)
503 return ERR_PTR(-ENOMEM);
504
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500505 *pt_dma = dma_map_single(obj->dev, iopte, IOPTE_TABLE_SIZE,
506 DMA_TO_DEVICE);
507 if (dma_mapping_error(obj->dev, *pt_dma)) {
508 dev_err(obj->dev, "DMA map error for L2 table\n");
509 iopte_free(obj, iopte, false);
510 return ERR_PTR(-ENOMEM);
511 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200512
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500513 /*
514 * we rely on dma address and the physical address to be
515 * the same for mapping the L2 table
516 */
517 if (WARN_ON(*pt_dma != virt_to_phys(iopte))) {
518 dev_err(obj->dev, "DMA translation error for L2 table\n");
519 dma_unmap_single(obj->dev, *pt_dma, IOPTE_TABLE_SIZE,
520 DMA_TO_DEVICE);
521 iopte_free(obj, iopte, false);
522 return ERR_PTR(-ENOMEM);
523 }
524
525 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
526
527 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200528 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
529 } else {
530 /* We raced, free the reduniovant table */
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500531 iopte_free(obj, iopte, false);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200532 }
533
534pte_ready:
535 iopte = iopte_offset(iopgd, da);
Ralf Goebel04c532a2018-08-06 17:00:36 +0200536 *pt_dma = iopgd_page_paddr(iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200537 dev_vdbg(obj->dev,
538 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
539 __func__, da, iopgd, *iopgd, iopte, *iopte);
540
541 return iopte;
542}
543
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300544static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200545{
546 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500547 unsigned long offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200548
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300549 if ((da | pa) & ~IOSECTION_MASK) {
550 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
551 __func__, da, pa, IOSECTION_SIZE);
552 return -EINVAL;
553 }
554
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200555 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500556 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200557 return 0;
558}
559
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300560static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200561{
562 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500563 unsigned long offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200564 int i;
565
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300566 if ((da | pa) & ~IOSUPER_MASK) {
567 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
568 __func__, da, pa, IOSUPER_SIZE);
569 return -EINVAL;
570 }
571
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200572 for (i = 0; i < 16; i++)
573 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500574 flush_iopte_range(obj->dev, obj->pd_dma, offset, 16);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200575 return 0;
576}
577
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300578static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200579{
580 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500581 dma_addr_t pt_dma;
582 u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
583 unsigned long offset = iopte_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200584
585 if (IS_ERR(iopte))
586 return PTR_ERR(iopte);
587
588 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500589 flush_iopte_range(obj->dev, pt_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200590
591 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
592 __func__, da, pa, iopte, *iopte);
593
594 return 0;
595}
596
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300597static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200598{
599 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500600 dma_addr_t pt_dma;
601 u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
602 unsigned long offset = iopte_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200603 int i;
604
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300605 if ((da | pa) & ~IOLARGE_MASK) {
606 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
607 __func__, da, pa, IOLARGE_SIZE);
608 return -EINVAL;
609 }
610
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200611 if (IS_ERR(iopte))
612 return PTR_ERR(iopte);
613
614 for (i = 0; i < 16; i++)
615 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500616 flush_iopte_range(obj->dev, pt_dma, offset, 16);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200617 return 0;
618}
619
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300620static int
621iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200622{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300623 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200624 u32 prot;
625 int err;
626
627 if (!obj || !e)
628 return -EINVAL;
629
630 switch (e->pgsz) {
631 case MMU_CAM_PGSZ_16M:
632 fn = iopgd_alloc_super;
633 break;
634 case MMU_CAM_PGSZ_1M:
635 fn = iopgd_alloc_section;
636 break;
637 case MMU_CAM_PGSZ_64K:
638 fn = iopte_alloc_large;
639 break;
640 case MMU_CAM_PGSZ_4K:
641 fn = iopte_alloc_page;
642 break;
643 default:
644 fn = NULL;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200645 break;
646 }
647
Suman Anna7c1ab602016-04-04 17:46:19 -0500648 if (WARN_ON(!fn))
649 return -EINVAL;
650
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200651 prot = get_iopte_attr(e);
652
653 spin_lock(&obj->page_table_lock);
654 err = fn(obj, e->da, e->pa, prot);
655 spin_unlock(&obj->page_table_lock);
656
657 return err;
658}
659
660/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300661 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200662 * @obj: target iommu
663 * @e: an iommu tlb entry info
664 **/
Suman Anna4899a562014-10-22 17:22:32 -0500665static int
666omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200667{
668 int err;
669
670 flush_iotlb_page(obj, e->da);
671 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200672 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300673 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200674 return err;
675}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200676
677/**
678 * iopgtable_lookup_entry - Lookup an iommu pte entry
679 * @obj: target iommu
680 * @da: iommu device virtual address
681 * @ppgd: iommu pgd entry pointer to be returned
682 * @ppte: iommu pte entry pointer to be returned
683 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300684static void
685iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200686{
687 u32 *iopgd, *iopte = NULL;
688
689 iopgd = iopgd_offset(obj, da);
690 if (!*iopgd)
691 goto out;
692
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300693 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200694 iopte = iopte_offset(iopgd, da);
695out:
696 *ppgd = iopgd;
697 *ppte = iopte;
698}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200699
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300700static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200701{
702 size_t bytes;
703 u32 *iopgd = iopgd_offset(obj, da);
704 int nent = 1;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500705 dma_addr_t pt_dma;
706 unsigned long pd_offset = iopgd_index(da) * sizeof(da);
707 unsigned long pt_offset = iopte_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200708
709 if (!*iopgd)
710 return 0;
711
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300712 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200713 int i;
714 u32 *iopte = iopte_offset(iopgd, da);
715
716 bytes = IOPTE_SIZE;
717 if (*iopte & IOPTE_LARGE) {
718 nent *= 16;
719 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800720 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200721 }
722 bytes *= nent;
723 memset(iopte, 0, nent * sizeof(*iopte));
Ralf Goebel04c532a2018-08-06 17:00:36 +0200724 pt_dma = iopgd_page_paddr(iopgd);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500725 flush_iopte_range(obj->dev, pt_dma, pt_offset, nent);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200726
727 /*
728 * do table walk to check if this table is necessary or not
729 */
730 iopte = iopte_offset(iopgd, 0);
731 for (i = 0; i < PTRS_PER_IOPTE; i++)
732 if (iopte[i])
733 goto out;
734
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500735 iopte_free(obj, iopte, true);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200736 nent = 1; /* for the next L1 entry */
737 } else {
738 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700739 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200740 nent *= 16;
741 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800742 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200743 }
744 bytes *= nent;
745 }
746 memset(iopgd, 0, nent * sizeof(*iopgd));
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500747 flush_iopte_range(obj->dev, obj->pd_dma, pd_offset, nent);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200748out:
749 return bytes;
750}
751
752/**
753 * iopgtable_clear_entry - Remove an iommu pte entry
754 * @obj: target iommu
755 * @da: iommu device virtual address
756 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300757static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200758{
759 size_t bytes;
760
761 spin_lock(&obj->page_table_lock);
762
763 bytes = iopgtable_clear_entry_core(obj, da);
764 flush_iotlb_page(obj, da);
765
766 spin_unlock(&obj->page_table_lock);
767
768 return bytes;
769}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200770
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300771static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200772{
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500773 unsigned long offset;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200774 int i;
775
776 spin_lock(&obj->page_table_lock);
777
778 for (i = 0; i < PTRS_PER_IOPGD; i++) {
779 u32 da;
780 u32 *iopgd;
781
782 da = i << IOPGD_SHIFT;
783 iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500784 offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200785
786 if (!*iopgd)
787 continue;
788
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300789 if (iopgd_is_table(*iopgd))
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500790 iopte_free(obj, iopte_offset(iopgd, 0), true);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200791
792 *iopgd = 0;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500793 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200794 }
795
796 flush_iotlb_all(obj);
797
798 spin_unlock(&obj->page_table_lock);
799}
800
801/*
802 * Device IOMMU generic operations
803 */
804static irqreturn_t iommu_fault_handler(int irq, void *data)
805{
David Cohend594f1f2011-02-16 19:35:51 +0000806 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200807 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300808 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400809 struct iommu_domain *domain = obj->domain;
Joerg Roedel8cf851e2015-03-26 13:43:09 +0100810 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200811
Suman Anna0d364282017-09-05 17:56:17 -0500812 if (!omap_domain->dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200813 return IRQ_NONE;
814
David Cohend594f1f2011-02-16 19:35:51 +0000815 errs = iommu_report_fault(obj, &da);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200816 if (errs == 0)
817 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000818
819 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400820 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200821 return IRQ_HANDLED;
822
Fernando Guzman Lugo159d3e32017-07-28 15:49:13 -0500823 iommu_write_reg(obj, 0, MMU_IRQENABLE);
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000824
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200825 iopgd = iopgd_offset(obj, da);
826
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300827 if (!iopgd_is_table(*iopgd)) {
Suman Annab6c2e092013-05-30 18:10:59 -0500828 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 -0500829 obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200830 return IRQ_NONE;
831 }
832
833 iopte = iopte_offset(iopgd, da);
834
Suman Annab6c2e092013-05-30 18:10:59 -0500835 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 -0500836 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200837
838 return IRQ_NONE;
839}
840
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200841/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300842 * omap_iommu_attach() - attach iommu device to an iommu domain
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500843 * @obj: target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300844 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200845 **/
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500846static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200847{
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600848 int err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200849
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300850 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200851
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500852 obj->pd_dma = dma_map_single(obj->dev, iopgd, IOPGD_TABLE_SIZE,
853 DMA_TO_DEVICE);
854 if (dma_mapping_error(obj->dev, obj->pd_dma)) {
855 dev_err(obj->dev, "DMA map error for L1 table\n");
856 err = -ENOMEM;
857 goto out_err;
858 }
859
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300860 obj->iopgd = iopgd;
861 err = iommu_enable(obj);
862 if (err)
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500863 goto out_err;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300864 flush_iotlb_all(obj);
865
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300866 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200867
868 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500869
870 return 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200871
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500872out_err:
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300873 spin_unlock(&obj->iommu_lock);
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500874
875 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200876}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200877
878/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300879 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200880 * @obj: target iommu
881 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300882static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200883{
Roel Kluinacf9d462010-01-08 10:29:05 -0800884 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200885 return;
886
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300887 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200888
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500889 dma_unmap_single(obj->dev, obj->pd_dma, IOPGD_TABLE_SIZE,
890 DMA_TO_DEVICE);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500891 obj->pd_dma = 0;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300892 obj->iopgd = NULL;
Suman Annac3b44a02019-08-07 11:26:48 +0300893 iommu_disable(obj);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300894
895 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200896
897 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
898}
David Cohend594f1f2011-02-16 19:35:51 +0000899
Suman Annac3b44a02019-08-07 11:26:48 +0300900static void omap_iommu_save_tlb_entries(struct omap_iommu *obj)
901{
902 struct iotlb_lock lock;
903 struct cr_regs cr;
904 struct cr_regs *tmp;
905 int i;
906
907 /* check if there are any locked tlbs to save */
908 iotlb_lock_get(obj, &lock);
909 obj->num_cr_ctx = lock.base;
910 if (!obj->num_cr_ctx)
911 return;
912
913 tmp = obj->cr_ctx;
914 for_each_iotlb_cr(obj, obj->num_cr_ctx, i, cr)
915 * tmp++ = cr;
916}
917
918static void omap_iommu_restore_tlb_entries(struct omap_iommu *obj)
919{
920 struct iotlb_lock l;
921 struct cr_regs *tmp;
922 int i;
923
924 /* no locked tlbs to restore */
925 if (!obj->num_cr_ctx)
926 return;
927
928 l.base = 0;
929 tmp = obj->cr_ctx;
930 for (i = 0; i < obj->num_cr_ctx; i++, tmp++) {
931 l.vict = i;
932 iotlb_lock_set(obj, &l);
933 iotlb_load_cr(obj, tmp);
934 }
935 l.base = obj->num_cr_ctx;
936 l.vict = i;
937 iotlb_lock_set(obj, &l);
938}
939
Suman Annadb8918f2019-08-07 11:26:47 +0300940/**
Suman Annad9c4d8a2019-08-07 11:26:50 +0300941 * omap_iommu_domain_deactivate - deactivate attached iommu devices
942 * @domain: iommu domain attached to the target iommu device
943 *
944 * This API allows the client devices of IOMMU devices to suspend
945 * the IOMMUs they control at runtime, after they are idled and
946 * suspended all activity. System Suspend will leverage the PM
947 * driver late callbacks.
948 **/
949int omap_iommu_domain_deactivate(struct iommu_domain *domain)
950{
951 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
952 struct omap_iommu_device *iommu;
953 struct omap_iommu *oiommu;
954 int i;
955
956 if (!omap_domain->dev)
957 return 0;
958
959 iommu = omap_domain->iommus;
960 iommu += (omap_domain->num_iommus - 1);
961 for (i = 0; i < omap_domain->num_iommus; i++, iommu--) {
962 oiommu = iommu->iommu_dev;
963 pm_runtime_put_sync(oiommu->dev);
964 }
965
966 return 0;
967}
968EXPORT_SYMBOL_GPL(omap_iommu_domain_deactivate);
969
970/**
971 * omap_iommu_domain_activate - activate attached iommu devices
972 * @domain: iommu domain attached to the target iommu device
973 *
974 * This API allows the client devices of IOMMU devices to resume the
975 * IOMMUs they control at runtime, before they can resume operations.
976 * System Resume will leverage the PM driver late callbacks.
977 **/
978int omap_iommu_domain_activate(struct iommu_domain *domain)
979{
980 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
981 struct omap_iommu_device *iommu;
982 struct omap_iommu *oiommu;
983 int i;
984
985 if (!omap_domain->dev)
986 return 0;
987
988 iommu = omap_domain->iommus;
989 for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
990 oiommu = iommu->iommu_dev;
991 pm_runtime_get_sync(oiommu->dev);
992 }
993
994 return 0;
995}
996EXPORT_SYMBOL_GPL(omap_iommu_domain_activate);
997
998/**
Suman Annadb8918f2019-08-07 11:26:47 +0300999 * omap_iommu_runtime_suspend - disable an iommu device
1000 * @dev: iommu device
1001 *
1002 * This function performs all that is necessary to disable an
1003 * IOMMU device, either during final detachment from a client
1004 * device, or during system/runtime suspend of the device. This
1005 * includes programming all the appropriate IOMMU registers, and
1006 * managing the associated omap_hwmod's state and the device's
Suman Annac3b44a02019-08-07 11:26:48 +03001007 * reset line. This function also saves the context of any
1008 * locked TLBs if suspending.
Suman Annadb8918f2019-08-07 11:26:47 +03001009 **/
Arnd Bergmann96088a202019-09-06 17:15:38 +02001010static __maybe_unused int omap_iommu_runtime_suspend(struct device *dev)
Suman Annadb8918f2019-08-07 11:26:47 +03001011{
1012 struct platform_device *pdev = to_platform_device(dev);
1013 struct iommu_platform_data *pdata = dev_get_platdata(dev);
1014 struct omap_iommu *obj = to_iommu(dev);
1015 int ret;
1016
Suman Annac3b44a02019-08-07 11:26:48 +03001017 /* save the TLBs only during suspend, and not for power down */
1018 if (obj->domain && obj->iopgd)
1019 omap_iommu_save_tlb_entries(obj);
1020
Suman Annadb8918f2019-08-07 11:26:47 +03001021 omap2_iommu_disable(obj);
1022
1023 if (pdata && pdata->device_idle)
1024 pdata->device_idle(pdev);
1025
1026 if (pdata && pdata->assert_reset)
1027 pdata->assert_reset(pdev, pdata->reset_name);
1028
1029 if (pdata && pdata->set_pwrdm_constraint) {
1030 ret = pdata->set_pwrdm_constraint(pdev, false, &obj->pwrst);
1031 if (ret) {
1032 dev_warn(obj->dev, "pwrdm_constraint failed to be reset, status = %d\n",
1033 ret);
1034 }
1035 }
1036
1037 return 0;
1038}
1039
1040/**
1041 * omap_iommu_runtime_resume - enable an iommu device
1042 * @dev: iommu device
1043 *
1044 * This function performs all that is necessary to enable an
1045 * IOMMU device, either during initial attachment to a client
1046 * device, or during system/runtime resume of the device. This
1047 * includes programming all the appropriate IOMMU registers, and
1048 * managing the associated omap_hwmod's state and the device's
Suman Annac3b44a02019-08-07 11:26:48 +03001049 * reset line. The function also restores any locked TLBs if
1050 * resuming after a suspend.
Suman Annadb8918f2019-08-07 11:26:47 +03001051 **/
Arnd Bergmann96088a202019-09-06 17:15:38 +02001052static __maybe_unused int omap_iommu_runtime_resume(struct device *dev)
Suman Annadb8918f2019-08-07 11:26:47 +03001053{
1054 struct platform_device *pdev = to_platform_device(dev);
1055 struct iommu_platform_data *pdata = dev_get_platdata(dev);
1056 struct omap_iommu *obj = to_iommu(dev);
1057 int ret = 0;
1058
1059 if (pdata && pdata->set_pwrdm_constraint) {
1060 ret = pdata->set_pwrdm_constraint(pdev, true, &obj->pwrst);
1061 if (ret) {
1062 dev_warn(obj->dev, "pwrdm_constraint failed to be set, status = %d\n",
1063 ret);
1064 }
1065 }
1066
1067 if (pdata && pdata->deassert_reset) {
1068 ret = pdata->deassert_reset(pdev, pdata->reset_name);
1069 if (ret) {
1070 dev_err(dev, "deassert_reset failed: %d\n", ret);
1071 return ret;
1072 }
1073 }
1074
1075 if (pdata && pdata->device_enable)
1076 pdata->device_enable(pdev);
1077
Suman Annac3b44a02019-08-07 11:26:48 +03001078 /* restore the TLBs only during resume, and not for power up */
1079 if (obj->domain)
1080 omap_iommu_restore_tlb_entries(obj);
1081
Suman Annadb8918f2019-08-07 11:26:47 +03001082 ret = omap2_iommu_enable(obj);
1083
1084 return ret;
1085}
1086
Suman Annac4206c42019-08-07 11:26:49 +03001087/**
1088 * omap_iommu_suspend_prepare - prepare() dev_pm_ops implementation
1089 * @dev: iommu device
1090 *
1091 * This function performs the necessary checks to determine if the IOMMU
1092 * device needs suspending or not. The function checks if the runtime_pm
1093 * status of the device is suspended, and returns 1 in that case. This
1094 * results in the PM core to skip invoking any of the Sleep PM callbacks
1095 * (suspend, suspend_late, resume, resume_early etc).
1096 */
1097static int omap_iommu_prepare(struct device *dev)
1098{
1099 if (pm_runtime_status_suspended(dev))
1100 return 1;
1101 return 0;
1102}
1103
Suman Anna9d5018d2017-09-05 17:56:18 -05001104static bool omap_iommu_can_register(struct platform_device *pdev)
1105{
1106 struct device_node *np = pdev->dev.of_node;
1107
1108 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
1109 return true;
1110
1111 /*
1112 * restrict IOMMU core registration only for processor-port MDMA MMUs
1113 * on DRA7 DSPs
1114 */
1115 if ((!strcmp(dev_name(&pdev->dev), "40d01000.mmu")) ||
1116 (!strcmp(dev_name(&pdev->dev), "41501000.mmu")))
1117 return true;
1118
1119 return false;
1120}
1121
Suman Anna3ca92992015-10-02 18:02:44 -05001122static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device *pdev,
1123 struct omap_iommu *obj)
1124{
1125 struct device_node *np = pdev->dev.of_node;
1126 int ret;
1127
1128 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
1129 return 0;
1130
1131 if (!of_property_read_bool(np, "ti,syscon-mmuconfig")) {
1132 dev_err(&pdev->dev, "ti,syscon-mmuconfig property is missing\n");
1133 return -EINVAL;
1134 }
1135
1136 obj->syscfg =
1137 syscon_regmap_lookup_by_phandle(np, "ti,syscon-mmuconfig");
1138 if (IS_ERR(obj->syscfg)) {
1139 /* can fail with -EPROBE_DEFER */
1140 ret = PTR_ERR(obj->syscfg);
1141 return ret;
1142 }
1143
1144 if (of_property_read_u32_index(np, "ti,syscon-mmuconfig", 1,
1145 &obj->id)) {
1146 dev_err(&pdev->dev, "couldn't get the IOMMU instance id within subsystem\n");
1147 return -EINVAL;
1148 }
1149
1150 if (obj->id != 0 && obj->id != 1) {
1151 dev_err(&pdev->dev, "invalid IOMMU instance id\n");
1152 return -EINVAL;
1153 }
1154
1155 return 0;
1156}
1157
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001158/*
1159 * OMAP Device MMU(IOMMU) detection
1160 */
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001161static int omap_iommu_probe(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001162{
1163 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001164 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001165 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001166 struct resource *res;
Florian Vaussard3c927482014-02-28 14:42:36 -06001167 struct device_node *of = pdev->dev.of_node;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001168
Suman Anna49a57ef2017-04-12 00:21:27 -05001169 if (!of) {
1170 pr_err("%s: only DT-based devices are supported\n", __func__);
1171 return -ENODEV;
1172 }
1173
Suman Annaf129b3d2014-02-28 14:42:32 -06001174 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001175 if (!obj)
1176 return -ENOMEM;
1177
Suman Annadb8918f2019-08-07 11:26:47 +03001178 /*
1179 * self-manage the ordering dependencies between omap_device_enable/idle
1180 * and omap_device_assert/deassert_hardreset API
1181 */
1182 if (pdev->dev.pm_domain) {
1183 dev_dbg(&pdev->dev, "device pm_domain is being reset\n");
1184 pdev->dev.pm_domain = NULL;
1185 }
1186
Suman Anna49a57ef2017-04-12 00:21:27 -05001187 obj->name = dev_name(&pdev->dev);
1188 obj->nr_tlb_entries = 32;
1189 err = of_property_read_u32(of, "ti,#tlb-entries", &obj->nr_tlb_entries);
1190 if (err && err != -EINVAL)
1191 return err;
1192 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
1193 return -EINVAL;
1194 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
1195 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
Florian Vaussard3c927482014-02-28 14:42:36 -06001196
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001197 obj->dev = &pdev->dev;
1198 obj->ctx = (void *)obj + sizeof(*obj);
Suman Annac3b44a02019-08-07 11:26:48 +03001199 obj->cr_ctx = devm_kzalloc(&pdev->dev,
1200 sizeof(*obj->cr_ctx) * obj->nr_tlb_entries,
1201 GFP_KERNEL);
1202 if (!obj->cr_ctx)
1203 return -ENOMEM;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001204
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001205 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001206 spin_lock_init(&obj->page_table_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001207
1208 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001209 obj->regbase = devm_ioremap_resource(obj->dev, res);
1210 if (IS_ERR(obj->regbase))
1211 return PTR_ERR(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +00001212
Suman Anna3ca92992015-10-02 18:02:44 -05001213 err = omap_iommu_dra7_get_dsp_system_cfg(pdev, obj);
1214 if (err)
1215 return err;
1216
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001217 irq = platform_get_irq(pdev, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001218 if (irq < 0)
1219 return -ENODEV;
1220
1221 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
1222 dev_name(obj->dev), obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001223 if (err < 0)
Suman Annaf129b3d2014-02-28 14:42:32 -06001224 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001225 platform_set_drvdata(pdev, obj);
1226
Suman Anna9d5018d2017-09-05 17:56:18 -05001227 if (omap_iommu_can_register(pdev)) {
1228 obj->group = iommu_group_alloc();
1229 if (IS_ERR(obj->group))
1230 return PTR_ERR(obj->group);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001231
Suman Anna9d5018d2017-09-05 17:56:18 -05001232 err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL,
1233 obj->name);
1234 if (err)
1235 goto out_group;
Joerg Roedel01611fe2017-04-12 00:21:30 -05001236
Robin Murphy2d471b22021-04-01 14:56:26 +01001237 err = iommu_device_register(&obj->iommu, &omap_iommu_ops, &pdev->dev);
Suman Anna9d5018d2017-09-05 17:56:18 -05001238 if (err)
1239 goto out_sysfs;
1240 }
Joerg Roedel01611fe2017-04-12 00:21:30 -05001241
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001242 pm_runtime_enable(obj->dev);
1243
Suman Anna61c75352014-10-22 17:22:30 -05001244 omap_iommu_debugfs_add(obj);
1245
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001246 dev_info(&pdev->dev, "%s registered\n", obj->name);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001247
Joerg Roedelc822b372020-04-29 15:37:06 +02001248 /* Re-probe bus to probe device attached to this IOMMU */
1249 bus_iommu_probe(&platform_bus_type);
Tero Kristo604629b2019-08-07 11:26:51 +03001250
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001251 return 0;
Joerg Roedel01611fe2017-04-12 00:21:30 -05001252
1253out_sysfs:
1254 iommu_device_sysfs_remove(&obj->iommu);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001255out_group:
1256 iommu_group_put(obj->group);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001257 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001258}
1259
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001260static int omap_iommu_remove(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001261{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001262 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001263
Suman Anna9d5018d2017-09-05 17:56:18 -05001264 if (obj->group) {
1265 iommu_group_put(obj->group);
1266 obj->group = NULL;
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001267
Suman Anna9d5018d2017-09-05 17:56:18 -05001268 iommu_device_sysfs_remove(&obj->iommu);
1269 iommu_device_unregister(&obj->iommu);
1270 }
Joerg Roedel01611fe2017-04-12 00:21:30 -05001271
Suman Anna61c75352014-10-22 17:22:30 -05001272 omap_iommu_debugfs_remove(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001273
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001274 pm_runtime_disable(obj->dev);
1275
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001276 dev_info(&pdev->dev, "%s removed\n", obj->name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001277 return 0;
1278}
1279
Suman Annadb8918f2019-08-07 11:26:47 +03001280static const struct dev_pm_ops omap_iommu_pm_ops = {
Suman Annac4206c42019-08-07 11:26:49 +03001281 .prepare = omap_iommu_prepare,
1282 SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1283 pm_runtime_force_resume)
Suman Annadb8918f2019-08-07 11:26:47 +03001284 SET_RUNTIME_PM_OPS(omap_iommu_runtime_suspend,
1285 omap_iommu_runtime_resume, NULL)
1286};
1287
Kiran Padwald943b0f2014-09-11 19:07:36 +05301288static const struct of_device_id omap_iommu_of_match[] = {
Florian Vaussard3c927482014-02-28 14:42:36 -06001289 { .compatible = "ti,omap2-iommu" },
1290 { .compatible = "ti,omap4-iommu" },
1291 { .compatible = "ti,dra7-iommu" },
Suman Anna3ca92992015-10-02 18:02:44 -05001292 { .compatible = "ti,dra7-dsp-iommu" },
Florian Vaussard3c927482014-02-28 14:42:36 -06001293 {},
1294};
Florian Vaussard3c927482014-02-28 14:42:36 -06001295
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001296static struct platform_driver omap_iommu_driver = {
1297 .probe = omap_iommu_probe,
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001298 .remove = omap_iommu_remove,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001299 .driver = {
1300 .name = "omap-iommu",
Suman Annadb8918f2019-08-07 11:26:47 +03001301 .pm = &omap_iommu_pm_ops,
Florian Vaussard3c927482014-02-28 14:42:36 -06001302 .of_match_table = of_match_ptr(omap_iommu_of_match),
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001303 },
1304};
1305
Laurent Pinchart286f6002014-03-08 00:44:38 +01001306static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001307{
1308 memset(e, 0, sizeof(*e));
1309
1310 e->da = da;
1311 e->pa = pa;
Suman Annad760e3e2014-03-17 20:31:32 -05001312 e->valid = MMU_CAM_V;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001313 e->pgsz = pgsz;
1314 e->endian = MMU_RAM_ENDIAN_LITTLE;
1315 e->elsz = MMU_RAM_ELSZ_8;
1316 e->mixed = 0;
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001317
1318 return iopgsz_to_bytes(e->pgsz);
1319}
1320
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001321static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Tom Murphy781ca2d2019-09-08 09:56:38 -07001322 phys_addr_t pa, size_t bytes, int prot, gfp_t gfp)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001323{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001324 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Suman Anna9d5018d2017-09-05 17:56:18 -05001325 struct device *dev = omap_domain->dev;
1326 struct omap_iommu_device *iommu;
1327 struct omap_iommu *oiommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001328 struct iotlb_entry e;
1329 int omap_pgsz;
Suman Anna9d5018d2017-09-05 17:56:18 -05001330 u32 ret = -EINVAL;
1331 int i;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001332
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001333 omap_pgsz = bytes_to_iopgsz(bytes);
1334 if (omap_pgsz < 0) {
Krzysztof Kozlowski6135a892020-03-03 21:27:49 +01001335 dev_err(dev, "invalid size to map: %zu\n", bytes);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001336 return -EINVAL;
1337 }
1338
Krzysztof Kozlowski6135a892020-03-03 21:27:49 +01001339 dev_dbg(dev, "mapping da 0x%lx to pa %pa size 0x%zx\n", da, &pa, bytes);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001340
Laurent Pinchart286f6002014-03-08 00:44:38 +01001341 iotlb_init_entry(&e, da, pa, omap_pgsz);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001342
Suman Anna9d5018d2017-09-05 17:56:18 -05001343 iommu = omap_domain->iommus;
1344 for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
1345 oiommu = iommu->iommu_dev;
1346 ret = omap_iopgtable_store_entry(oiommu, &e);
1347 if (ret) {
1348 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n",
1349 ret);
1350 break;
1351 }
1352 }
1353
1354 if (ret) {
1355 while (i--) {
1356 iommu--;
1357 oiommu = iommu->iommu_dev;
1358 iopgtable_clear_entry(oiommu, da);
1359 }
1360 }
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001361
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001362 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001363}
1364
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001365static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
Will Deacon56f8af52019-07-02 16:44:06 +01001366 size_t size, struct iommu_iotlb_gather *gather)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001367{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001368 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Suman Anna9d5018d2017-09-05 17:56:18 -05001369 struct device *dev = omap_domain->dev;
1370 struct omap_iommu_device *iommu;
1371 struct omap_iommu *oiommu;
1372 bool error = false;
1373 size_t bytes = 0;
1374 int i;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001375
Krzysztof Kozlowski6135a892020-03-03 21:27:49 +01001376 dev_dbg(dev, "unmapping da 0x%lx size %zu\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001377
Suman Anna9d5018d2017-09-05 17:56:18 -05001378 iommu = omap_domain->iommus;
1379 for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
1380 oiommu = iommu->iommu_dev;
1381 bytes = iopgtable_clear_entry(oiommu, da);
1382 if (!bytes)
1383 error = true;
1384 }
1385
1386 /*
1387 * simplify return - we are only checking if any of the iommus
1388 * reported an error, but not if all of them are unmapping the
1389 * same number of entries. This should not occur due to the
1390 * mirror programming.
1391 */
1392 return error ? 0 : bytes;
1393}
1394
1395static int omap_iommu_count(struct device *dev)
1396{
Joerg Roedel97ea1202020-06-25 15:08:27 +02001397 struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
Suman Anna9d5018d2017-09-05 17:56:18 -05001398 int count = 0;
1399
1400 while (arch_data->iommu_dev) {
1401 count++;
1402 arch_data++;
1403 }
1404
1405 return count;
1406}
1407
1408/* caller should call cleanup if this function fails */
1409static int omap_iommu_attach_init(struct device *dev,
1410 struct omap_iommu_domain *odomain)
1411{
1412 struct omap_iommu_device *iommu;
1413 int i;
1414
1415 odomain->num_iommus = omap_iommu_count(dev);
1416 if (!odomain->num_iommus)
1417 return -EINVAL;
1418
1419 odomain->iommus = kcalloc(odomain->num_iommus, sizeof(*iommu),
1420 GFP_ATOMIC);
1421 if (!odomain->iommus)
1422 return -ENOMEM;
1423
1424 iommu = odomain->iommus;
1425 for (i = 0; i < odomain->num_iommus; i++, iommu++) {
1426 iommu->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_ATOMIC);
1427 if (!iommu->pgtable)
1428 return -ENOMEM;
1429
1430 /*
1431 * should never fail, but please keep this around to ensure
1432 * we keep the hardware happy
1433 */
1434 if (WARN_ON(!IS_ALIGNED((long)iommu->pgtable,
1435 IOPGD_TABLE_SIZE)))
1436 return -EINVAL;
1437 }
1438
1439 return 0;
1440}
1441
1442static void omap_iommu_detach_fini(struct omap_iommu_domain *odomain)
1443{
1444 int i;
1445 struct omap_iommu_device *iommu = odomain->iommus;
1446
1447 for (i = 0; iommu && i < odomain->num_iommus; i++, iommu++)
1448 kfree(iommu->pgtable);
1449
1450 kfree(odomain->iommus);
1451 odomain->num_iommus = 0;
1452 odomain->iommus = NULL;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001453}
1454
1455static int
1456omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1457{
Joerg Roedel97ea1202020-06-25 15:08:27 +02001458 struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001459 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Suman Anna9d5018d2017-09-05 17:56:18 -05001460 struct omap_iommu_device *iommu;
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001461 struct omap_iommu *oiommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001462 int ret = 0;
Suman Anna9d5018d2017-09-05 17:56:18 -05001463 int i;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001464
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001465 if (!arch_data || !arch_data->iommu_dev) {
Suman Annae3f595b2014-09-04 17:27:29 -05001466 dev_err(dev, "device doesn't have an associated iommu\n");
1467 return -EINVAL;
1468 }
1469
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001470 spin_lock(&omap_domain->lock);
1471
Suman Anna0d364282017-09-05 17:56:17 -05001472 /* only a single client device can be attached to a domain */
1473 if (omap_domain->dev) {
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001474 dev_err(dev, "iommu domain is already attached\n");
1475 ret = -EBUSY;
1476 goto out;
1477 }
1478
Suman Anna9d5018d2017-09-05 17:56:18 -05001479 ret = omap_iommu_attach_init(dev, omap_domain);
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001480 if (ret) {
Suman Anna9d5018d2017-09-05 17:56:18 -05001481 dev_err(dev, "failed to allocate required iommu data %d\n",
1482 ret);
1483 goto init_fail;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001484 }
1485
Suman Anna9d5018d2017-09-05 17:56:18 -05001486 iommu = omap_domain->iommus;
1487 for (i = 0; i < omap_domain->num_iommus; i++, iommu++, arch_data++) {
1488 /* configure and enable the omap iommu */
1489 oiommu = arch_data->iommu_dev;
1490 ret = omap_iommu_attach(oiommu, iommu->pgtable);
1491 if (ret) {
1492 dev_err(dev, "can't get omap iommu: %d\n", ret);
1493 goto attach_fail;
1494 }
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001495
Suman Anna9d5018d2017-09-05 17:56:18 -05001496 oiommu->domain = domain;
1497 iommu->iommu_dev = oiommu;
1498 }
1499
1500 omap_domain->dev = dev;
1501
1502 goto out;
1503
1504attach_fail:
1505 while (i--) {
1506 iommu--;
1507 arch_data--;
1508 oiommu = iommu->iommu_dev;
1509 omap_iommu_detach(oiommu);
1510 iommu->iommu_dev = NULL;
1511 oiommu->domain = NULL;
1512 }
1513init_fail:
1514 omap_iommu_detach_fini(omap_domain);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001515out:
1516 spin_unlock(&omap_domain->lock);
1517 return ret;
1518}
1519
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001520static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001521 struct device *dev)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001522{
Joerg Roedel97ea1202020-06-25 15:08:27 +02001523 struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
Suman Anna9d5018d2017-09-05 17:56:18 -05001524 struct omap_iommu_device *iommu = omap_domain->iommus;
1525 struct omap_iommu *oiommu;
1526 int i;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001527
Suman Anna0d364282017-09-05 17:56:17 -05001528 if (!omap_domain->dev) {
1529 dev_err(dev, "domain has no attached device\n");
1530 return;
1531 }
1532
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001533 /* only a single device is supported per domain for now */
Suman Anna0d364282017-09-05 17:56:17 -05001534 if (omap_domain->dev != dev) {
1535 dev_err(dev, "invalid attached device\n");
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001536 return;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001537 }
1538
Suman Anna9d5018d2017-09-05 17:56:18 -05001539 /*
1540 * cleanup in the reverse order of attachment - this addresses
1541 * any h/w dependencies between multiple instances, if any
1542 */
1543 iommu += (omap_domain->num_iommus - 1);
1544 arch_data += (omap_domain->num_iommus - 1);
1545 for (i = 0; i < omap_domain->num_iommus; i++, iommu--, arch_data--) {
1546 oiommu = iommu->iommu_dev;
1547 iopgtable_clear_entry_all(oiommu);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001548
Suman Anna9d5018d2017-09-05 17:56:18 -05001549 omap_iommu_detach(oiommu);
1550 iommu->iommu_dev = NULL;
1551 oiommu->domain = NULL;
1552 }
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001553
Suman Anna9d5018d2017-09-05 17:56:18 -05001554 omap_iommu_detach_fini(omap_domain);
1555
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001556 omap_domain->dev = NULL;
1557}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001558
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001559static void omap_iommu_detach_dev(struct iommu_domain *domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001560 struct device *dev)
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001561{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001562 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001563
1564 spin_lock(&omap_domain->lock);
1565 _omap_iommu_detach_dev(omap_domain, dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001566 spin_unlock(&omap_domain->lock);
1567}
1568
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001569static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001570{
1571 struct omap_iommu_domain *omap_domain;
1572
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001573 if (type != IOMMU_DOMAIN_UNMANAGED)
1574 return NULL;
1575
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001576 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
Suman Anna99ee98d2015-07-20 17:33:29 -05001577 if (!omap_domain)
Suman Anna9d5018d2017-09-05 17:56:18 -05001578 return NULL;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001579
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001580 spin_lock_init(&omap_domain->lock);
1581
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001582 omap_domain->domain.geometry.aperture_start = 0;
1583 omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
1584 omap_domain->domain.geometry.force_aperture = true;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001585
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001586 return &omap_domain->domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001587}
1588
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001589static void omap_iommu_domain_free(struct iommu_domain *domain)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001590{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001591 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001592
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001593 /*
1594 * An iommu device is still attached
1595 * (currently, only one device can be attached) ?
1596 */
Suman Anna0d364282017-09-05 17:56:17 -05001597 if (omap_domain->dev)
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001598 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1599
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001600 kfree(omap_domain);
1601}
1602
1603static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001604 dma_addr_t da)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001605{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001606 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Suman Anna9d5018d2017-09-05 17:56:18 -05001607 struct omap_iommu_device *iommu = omap_domain->iommus;
1608 struct omap_iommu *oiommu = iommu->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001609 struct device *dev = oiommu->dev;
1610 u32 *pgd, *pte;
1611 phys_addr_t ret = 0;
1612
Suman Anna9d5018d2017-09-05 17:56:18 -05001613 /*
1614 * all the iommus within the domain will have identical programming,
1615 * so perform the lookup using just the first iommu
1616 */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001617 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1618
1619 if (pte) {
1620 if (iopte_is_small(*pte))
1621 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1622 else if (iopte_is_large(*pte))
1623 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1624 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001625 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
Suman Anna5835b6a2015-07-20 17:33:32 -05001626 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001627 } else {
1628 if (iopgd_is_section(*pgd))
1629 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1630 else if (iopgd_is_super(*pgd))
1631 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1632 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001633 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
Suman Anna5835b6a2015-07-20 17:33:32 -05001634 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001635 }
1636
1637 return ret;
1638}
1639
Joerg Roedel6785eb92020-04-29 15:37:07 +02001640static struct iommu_device *omap_iommu_probe_device(struct device *dev)
Laurent Pinchart07a02032014-02-28 14:42:38 -06001641{
Suman Anna9d5018d2017-09-05 17:56:18 -05001642 struct omap_iommu_arch_data *arch_data, *tmp;
Suman Anna7d682772014-09-04 17:27:30 -05001643 struct platform_device *pdev;
Joerg Roedel6785eb92020-04-29 15:37:07 +02001644 struct omap_iommu *oiommu;
1645 struct device_node *np;
Suman Anna9d5018d2017-09-05 17:56:18 -05001646 int num_iommus, i;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001647
1648 /*
Joerg Roedel97ea1202020-06-25 15:08:27 +02001649 * Allocate the per-device iommu structure for DT-based devices.
Laurent Pinchart07a02032014-02-28 14:42:38 -06001650 *
1651 * TODO: Simplify this when removing non-DT support completely from the
1652 * IOMMU users.
1653 */
1654 if (!dev->of_node)
Joerg Roedel6785eb92020-04-29 15:37:07 +02001655 return ERR_PTR(-ENODEV);
Laurent Pinchart07a02032014-02-28 14:42:38 -06001656
Suman Anna9d5018d2017-09-05 17:56:18 -05001657 /*
1658 * retrieve the count of IOMMU nodes using phandle size as element size
1659 * since #iommu-cells = 0 for OMAP
1660 */
1661 num_iommus = of_property_count_elems_of_size(dev->of_node, "iommus",
1662 sizeof(phandle));
1663 if (num_iommus < 0)
Laurent Pinchart07a02032014-02-28 14:42:38 -06001664 return 0;
1665
Kees Cook6396bb22018-06-12 14:03:40 -07001666 arch_data = kcalloc(num_iommus + 1, sizeof(*arch_data), GFP_KERNEL);
Suman Anna9d5018d2017-09-05 17:56:18 -05001667 if (!arch_data)
Joerg Roedel6785eb92020-04-29 15:37:07 +02001668 return ERR_PTR(-ENOMEM);
Suman Anna9d5018d2017-09-05 17:56:18 -05001669
1670 for (i = 0, tmp = arch_data; i < num_iommus; i++, tmp++) {
1671 np = of_parse_phandle(dev->of_node, "iommus", i);
1672 if (!np) {
1673 kfree(arch_data);
Joerg Roedel6785eb92020-04-29 15:37:07 +02001674 return ERR_PTR(-EINVAL);
Suman Anna9d5018d2017-09-05 17:56:18 -05001675 }
1676
1677 pdev = of_find_device_by_node(np);
Tero Kristo604629b2019-08-07 11:26:51 +03001678 if (!pdev) {
Suman Anna9d5018d2017-09-05 17:56:18 -05001679 of_node_put(np);
1680 kfree(arch_data);
Joerg Roedel6785eb92020-04-29 15:37:07 +02001681 return ERR_PTR(-ENODEV);
Suman Anna9d5018d2017-09-05 17:56:18 -05001682 }
1683
1684 oiommu = platform_get_drvdata(pdev);
1685 if (!oiommu) {
1686 of_node_put(np);
1687 kfree(arch_data);
Joerg Roedel6785eb92020-04-29 15:37:07 +02001688 return ERR_PTR(-EINVAL);
Suman Anna9d5018d2017-09-05 17:56:18 -05001689 }
1690
1691 tmp->iommu_dev = oiommu;
Tero Kristo604629b2019-08-07 11:26:51 +03001692 tmp->dev = &pdev->dev;
Suman Anna9d5018d2017-09-05 17:56:18 -05001693
1694 of_node_put(np);
Laurent Pinchart07a02032014-02-28 14:42:38 -06001695 }
1696
Joerg Roedel97ea1202020-06-25 15:08:27 +02001697 dev_iommu_priv_set(dev, arch_data);
Joerg Roedel6785eb92020-04-29 15:37:07 +02001698
Suman Anna9d5018d2017-09-05 17:56:18 -05001699 /*
1700 * use the first IOMMU alone for the sysfs device linking.
1701 * TODO: Evaluate if a single iommu_group needs to be
1702 * maintained for both IOMMUs
1703 */
1704 oiommu = arch_data->iommu_dev;
Joerg Roedel01611fe2017-04-12 00:21:30 -05001705
Joerg Roedel6785eb92020-04-29 15:37:07 +02001706 return &oiommu->iommu;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001707}
1708
Joerg Roedel6785eb92020-04-29 15:37:07 +02001709static void omap_iommu_release_device(struct device *dev)
Laurent Pinchart07a02032014-02-28 14:42:38 -06001710{
Joerg Roedel97ea1202020-06-25 15:08:27 +02001711 struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
Laurent Pinchart07a02032014-02-28 14:42:38 -06001712
1713 if (!dev->of_node || !arch_data)
1714 return;
1715
Joerg Roedel97ea1202020-06-25 15:08:27 +02001716 dev_iommu_priv_set(dev, NULL);
Laurent Pinchart07a02032014-02-28 14:42:38 -06001717 kfree(arch_data);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001718
Laurent Pinchart07a02032014-02-28 14:42:38 -06001719}
1720
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001721static struct iommu_group *omap_iommu_device_group(struct device *dev)
1722{
Joerg Roedel97ea1202020-06-25 15:08:27 +02001723 struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
Joerg Roedel8faf5e52017-06-28 12:50:16 +02001724 struct iommu_group *group = ERR_PTR(-EINVAL);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001725
Tero Kristo via iommu46b14fc2020-05-18 14:10:57 +03001726 if (!arch_data)
1727 return ERR_PTR(-ENODEV);
1728
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001729 if (arch_data->iommu_dev)
Jeffy Chenb6d57f12018-03-01 19:22:08 +08001730 group = iommu_group_ref_get(arch_data->iommu_dev->group);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001731
1732 return group;
1733}
1734
Thierry Redingb22f6432014-06-27 09:03:12 +02001735static const struct iommu_ops omap_iommu_ops = {
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001736 .domain_alloc = omap_iommu_domain_alloc,
1737 .domain_free = omap_iommu_domain_free,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001738 .attach_dev = omap_iommu_attach_dev,
1739 .detach_dev = omap_iommu_detach_dev,
1740 .map = omap_iommu_map,
1741 .unmap = omap_iommu_unmap,
1742 .iova_to_phys = omap_iommu_iova_to_phys,
Joerg Roedel6785eb92020-04-29 15:37:07 +02001743 .probe_device = omap_iommu_probe_device,
1744 .release_device = omap_iommu_release_device,
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001745 .device_group = omap_iommu_device_group,
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +02001746 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001747};
1748
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001749static int __init omap_iommu_init(void)
1750{
1751 struct kmem_cache *p;
Suman Anna24ce0ba2019-08-16 17:58:37 -05001752 const slab_flags_t flags = SLAB_HWCACHE_ALIGN;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001753 size_t align = 1 << 10; /* L2 pagetable alignement */
Thierry Redingf938aab2015-02-06 11:44:06 +01001754 struct device_node *np;
Suman Annaabaa7e52017-04-12 00:21:26 -05001755 int ret;
Thierry Redingf938aab2015-02-06 11:44:06 +01001756
1757 np = of_find_matching_node(NULL, omap_iommu_of_match);
1758 if (!np)
1759 return 0;
1760
1761 of_node_put(np);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001762
1763 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
Josue Albarranbfee0cf2017-07-28 15:49:14 -05001764 NULL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001765 if (!p)
1766 return -ENOMEM;
1767 iopte_cachep = p;
1768
Suman Anna61c75352014-10-22 17:22:30 -05001769 omap_iommu_debugfs_init();
1770
Suman Annaabaa7e52017-04-12 00:21:26 -05001771 ret = platform_driver_register(&omap_iommu_driver);
1772 if (ret) {
1773 pr_err("%s: failed to register driver\n", __func__);
1774 goto fail_driver;
1775 }
1776
1777 ret = bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
1778 if (ret)
1779 goto fail_bus;
1780
1781 return 0;
1782
1783fail_bus:
1784 platform_driver_unregister(&omap_iommu_driver);
1785fail_driver:
1786 kmem_cache_destroy(iopte_cachep);
1787 return ret;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001788}
Ohad Ben-Cohen435792d2012-02-26 12:14:14 +02001789subsys_initcall(omap_iommu_init);
Suman Anna0cdbf722015-07-20 17:33:24 -05001790/* must be ready before omap3isp is probed */