blob: 8645e9b175a3e4d3e44558e84e79efa0097202e5 [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
Suman Anna9d5018d2017-09-05 17:56:18 -05006 * Copyright (C) 2013-2017 Texas Instruments Incorporated - http://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>
25#include <linux/of_iommu.h>
26#include <linux/of_irq.h>
Suman Anna7d682772014-09-04 17:27:30 -050027#include <linux/of_platform.h>
Suman Anna3ca92992015-10-02 18:02:44 -050028#include <linux/regmap.h>
29#include <linux/mfd/syscon.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020030
Tony Lindgren2ab7c842012-11-02 12:24:14 -070031#include <linux/platform_data/iommu-omap.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020032
Ido Yariv2f7702a2012-11-02 12:24:00 -070033#include "omap-iopgtable.h"
Tony Lindgrened1c7de2012-11-02 12:24:06 -070034#include "omap-iommu.h"
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020035
Joerg Roedel01611fe2017-04-12 00:21:30 -050036static const struct iommu_ops omap_iommu_ops;
37
Tero Kristo604629b2019-08-07 11:26:51 +030038struct orphan_dev {
39 struct device *dev;
40 struct list_head node;
41};
42
43static LIST_HEAD(orphan_dev_list);
44
45static DEFINE_SPINLOCK(orphan_lock);
46
Kefeng Wang6e8b5662019-04-23 15:50:08 +080047#define to_iommu(dev) ((struct omap_iommu *)dev_get_drvdata(dev))
Suman Anna5acc97d2014-03-17 20:31:34 -050048
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +020049/* bitmap of the page sizes currently supported */
50#define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
51
Ido Yariv7bd9e252012-11-02 12:24:09 -070052#define MMU_LOCK_BASE_SHIFT 10
53#define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
54#define MMU_LOCK_BASE(x) \
55 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
56
57#define MMU_LOCK_VICT_SHIFT 4
58#define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
59#define MMU_LOCK_VICT(x) \
60 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
61
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020062static struct platform_driver omap_iommu_driver;
63static struct kmem_cache *iopte_cachep;
64
Tero Kristo604629b2019-08-07 11:26:51 +030065static int _omap_iommu_add_device(struct device *dev);
66
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020067/**
Joerg Roedel8cf851e2015-03-26 13:43:09 +010068 * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
69 * @dom: generic iommu domain handle
70 **/
71static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
72{
73 return container_of(dom, struct omap_iommu_domain, domain);
74}
75
76/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030077 * omap_iommu_save_ctx - Save registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020078 * @dev: client device
Suman Annac4206c42019-08-07 11:26:49 +030079 *
80 * This should be treated as an deprecated API. It is preserved only
81 * to maintain existing functionality for OMAP3 ISP driver.
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020082 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020083void omap_iommu_save_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020084{
Suman Anna9d5018d2017-09-05 17:56:18 -050085 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
86 struct omap_iommu *obj;
87 u32 *p;
Suman Annabd4396f2014-10-22 17:22:27 -050088 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020089
Suman Anna9d5018d2017-09-05 17:56:18 -050090 if (!arch_data)
91 return;
92
93 while (arch_data->iommu_dev) {
94 obj = arch_data->iommu_dev;
95 p = obj->ctx;
96 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
97 p[i] = iommu_read_reg(obj, i * sizeof(u32));
98 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i,
99 p[i]);
100 }
101 arch_data++;
Suman Annabd4396f2014-10-22 17:22:27 -0500102 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200103}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300104EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200105
106/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300107 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200108 * @dev: client device
Suman Annac4206c42019-08-07 11:26:49 +0300109 *
110 * This should be treated as an deprecated API. It is preserved only
111 * to maintain existing functionality for OMAP3 ISP driver.
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200112 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200113void omap_iommu_restore_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200114{
Suman Anna9d5018d2017-09-05 17:56:18 -0500115 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
116 struct omap_iommu *obj;
117 u32 *p;
Suman Annabd4396f2014-10-22 17:22:27 -0500118 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200119
Suman Anna9d5018d2017-09-05 17:56:18 -0500120 if (!arch_data)
121 return;
122
123 while (arch_data->iommu_dev) {
124 obj = arch_data->iommu_dev;
125 p = obj->ctx;
126 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
127 iommu_write_reg(obj, p[i], i * sizeof(u32));
128 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i,
129 p[i]);
130 }
131 arch_data++;
Suman Annabd4396f2014-10-22 17:22:27 -0500132 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200133}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300134EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200135
Suman Anna3ca92992015-10-02 18:02:44 -0500136static void dra7_cfg_dspsys_mmu(struct omap_iommu *obj, bool enable)
137{
138 u32 val, mask;
139
140 if (!obj->syscfg)
141 return;
142
143 mask = (1 << (obj->id * DSP_SYS_MMU_CONFIG_EN_SHIFT));
144 val = enable ? mask : 0;
145 regmap_update_bits(obj->syscfg, DSP_SYS_MMU_CONFIG, mask, val);
146}
147
Suman Annabd4396f2014-10-22 17:22:27 -0500148static void __iommu_set_twl(struct omap_iommu *obj, bool on)
149{
150 u32 l = iommu_read_reg(obj, MMU_CNTL);
151
152 if (on)
153 iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
154 else
155 iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
156
157 l &= ~MMU_CNTL_MASK;
158 if (on)
159 l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
160 else
161 l |= (MMU_CNTL_MMU_EN);
162
163 iommu_write_reg(obj, l, MMU_CNTL);
164}
165
166static int omap2_iommu_enable(struct omap_iommu *obj)
167{
168 u32 l, pa;
169
170 if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
171 return -EINVAL;
172
173 pa = virt_to_phys(obj->iopgd);
174 if (!IS_ALIGNED(pa, SZ_16K))
175 return -EINVAL;
176
177 l = iommu_read_reg(obj, MMU_REVISION);
178 dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
179 (l >> 4) & 0xf, l & 0xf);
180
181 iommu_write_reg(obj, pa, MMU_TTB);
182
Suman Anna3ca92992015-10-02 18:02:44 -0500183 dra7_cfg_dspsys_mmu(obj, true);
184
Suman Annabd4396f2014-10-22 17:22:27 -0500185 if (obj->has_bus_err_back)
186 iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
187
188 __iommu_set_twl(obj, true);
189
190 return 0;
191}
192
193static void omap2_iommu_disable(struct omap_iommu *obj)
194{
195 u32 l = iommu_read_reg(obj, MMU_CNTL);
196
197 l &= ~MMU_CNTL_MASK;
198 iommu_write_reg(obj, l, MMU_CNTL);
Suman Anna3ca92992015-10-02 18:02:44 -0500199 dra7_cfg_dspsys_mmu(obj, false);
Suman Annabd4396f2014-10-22 17:22:27 -0500200
201 dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
202}
203
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300204static int iommu_enable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200205{
Suman Annadb8918f2019-08-07 11:26:47 +0300206 int ret;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200207
Suman Annadb8918f2019-08-07 11:26:47 +0300208 ret = pm_runtime_get_sync(obj->dev);
209 if (ret < 0)
210 pm_runtime_put_noidle(obj->dev);
Suman Anna3846a3b92019-08-07 11:26:45 +0300211
Suman Annadb8918f2019-08-07 11:26:47 +0300212 return ret < 0 ? ret : 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200213}
214
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300215static void iommu_disable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200216{
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600217 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200218}
219
220/*
221 * TLB operations
222 */
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300223static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200224{
Suman Annabd4396f2014-10-22 17:22:27 -0500225 u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
226 u32 mask = get_cam_va_mask(cr->cam & page_size);
227
228 return cr->cam & mask;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200229}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200230
231static u32 get_iopte_attr(struct iotlb_entry *e)
232{
Suman Annabd4396f2014-10-22 17:22:27 -0500233 u32 attr;
234
235 attr = e->mixed << 5;
236 attr |= e->endian;
237 attr |= e->elsz >> 3;
238 attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
239 (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
240 return attr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200241}
242
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300243static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200244{
Suman Annabd4396f2014-10-22 17:22:27 -0500245 u32 status, fault_addr;
246
247 status = iommu_read_reg(obj, MMU_IRQSTATUS);
248 status &= MMU_IRQ_MASK;
249 if (!status) {
250 *da = 0;
251 return 0;
252 }
253
254 fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
255 *da = fault_addr;
256
257 iommu_write_reg(obj, status, MMU_IRQSTATUS);
258
259 return status;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200260}
261
Suman Anna69c2c192015-07-20 17:33:25 -0500262void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200263{
264 u32 val;
265
266 val = iommu_read_reg(obj, MMU_LOCK);
267
268 l->base = MMU_LOCK_BASE(val);
269 l->vict = MMU_LOCK_VICT(val);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200270}
271
Suman Anna69c2c192015-07-20 17:33:25 -0500272void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200273{
274 u32 val;
275
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200276 val = (l->base << MMU_LOCK_BASE_SHIFT);
277 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
278
279 iommu_write_reg(obj, val, MMU_LOCK);
280}
281
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300282static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200283{
Suman Annabd4396f2014-10-22 17:22:27 -0500284 cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
285 cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200286}
287
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300288static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200289{
Suman Annabd4396f2014-10-22 17:22:27 -0500290 iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
291 iommu_write_reg(obj, cr->ram, MMU_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200292
293 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
294 iommu_write_reg(obj, 1, MMU_LD_TLB);
295}
296
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000297/* only used in iotlb iteration for-loop */
Suman Anna69c2c192015-07-20 17:33:25 -0500298struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000299{
300 struct cr_regs cr;
301 struct iotlb_lock l;
302
303 iotlb_lock_get(obj, &l);
304 l.vict = n;
305 iotlb_lock_set(obj, &l);
306 iotlb_read_cr(obj, &cr);
307
308 return cr;
309}
310
Suman Annabd4396f2014-10-22 17:22:27 -0500311#ifdef PREFETCH_IOTLB
312static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
313 struct iotlb_entry *e)
314{
315 struct cr_regs *cr;
316
317 if (!e)
318 return NULL;
319
320 if (e->da & ~(get_cam_va_mask(e->pgsz))) {
321 dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
322 e->da);
323 return ERR_PTR(-EINVAL);
324 }
325
326 cr = kmalloc(sizeof(*cr), GFP_KERNEL);
327 if (!cr)
328 return ERR_PTR(-ENOMEM);
329
330 cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
331 cr->ram = e->pa | e->endian | e->elsz | e->mixed;
332
333 return cr;
334}
335
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200336/**
337 * load_iotlb_entry - Set an iommu tlb entry
338 * @obj: target iommu
339 * @e: an iommu tlb entry info
340 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300341static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200342{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200343 int err = 0;
344 struct iotlb_lock l;
345 struct cr_regs *cr;
346
347 if (!obj || !obj->nr_tlb_entries || !e)
348 return -EINVAL;
349
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600350 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200351
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000352 iotlb_lock_get(obj, &l);
353 if (l.base == obj->nr_tlb_entries) {
354 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200355 err = -EBUSY;
356 goto out;
357 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000358 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000359 int i;
360 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000361
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000362 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000363 if (!iotlb_cr_valid(&tmp))
364 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000365
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000366 if (i == obj->nr_tlb_entries) {
367 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
368 err = -EBUSY;
369 goto out;
370 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000371
372 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000373 } else {
374 l.vict = l.base;
375 iotlb_lock_set(obj, &l);
376 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200377
378 cr = iotlb_alloc_cr(obj, e);
379 if (IS_ERR(cr)) {
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600380 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200381 return PTR_ERR(cr);
382 }
383
384 iotlb_load_cr(obj, cr);
385 kfree(cr);
386
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000387 if (e->prsvd)
388 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200389 /* increment victim for next tlb load */
390 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000391 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200392 iotlb_lock_set(obj, &l);
393out:
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600394 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200395 return err;
396}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200397
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300398#else /* !PREFETCH_IOTLB */
399
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300400static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300401{
402 return 0;
403}
404
405#endif /* !PREFETCH_IOTLB */
406
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300407static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300408{
409 return load_iotlb_entry(obj, e);
410}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200411
412/**
413 * flush_iotlb_page - Clear an iommu tlb entry
414 * @obj: target iommu
415 * @da: iommu device virtual address
416 *
417 * Clear an iommu tlb entry which includes 'da' address.
418 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300419static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200420{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200421 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000422 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200423
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600424 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200425
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000426 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200427 u32 start;
428 size_t bytes;
429
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200430 if (!iotlb_cr_valid(&cr))
431 continue;
432
433 start = iotlb_cr_to_virt(&cr);
434 bytes = iopgsz_to_bytes(cr.cam & 3);
435
436 if ((start <= da) && (da < start + bytes)) {
437 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
438 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000439 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200440 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
Laurent Pinchartf7129a02014-03-07 23:47:03 +0100441 break;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200442 }
443 }
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600444 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200445
446 if (i == obj->nr_tlb_entries)
447 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
448}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200449
450/**
451 * flush_iotlb_all - Clear all iommu tlb entries
452 * @obj: target iommu
453 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300454static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200455{
456 struct iotlb_lock l;
457
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600458 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200459
460 l.base = 0;
461 l.vict = 0;
462 iotlb_lock_set(obj, &l);
463
464 iommu_write_reg(obj, 1, MMU_GFLUSH);
465
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600466 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200467}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200468
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200469/*
470 * H/W pagetable operations
471 */
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500472static void flush_iopte_range(struct device *dev, dma_addr_t dma,
473 unsigned long offset, int num_entries)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200474{
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500475 size_t size = num_entries * sizeof(u32);
476
477 dma_sync_single_range_for_device(dev, dma, offset, size, DMA_TO_DEVICE);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200478}
479
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500480static void iopte_free(struct omap_iommu *obj, u32 *iopte, bool dma_valid)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200481{
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500482 dma_addr_t pt_dma;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200483
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200484 /* Note: freed iopte's must be clean ready for re-use */
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500485 if (iopte) {
486 if (dma_valid) {
487 pt_dma = virt_to_phys(iopte);
488 dma_unmap_single(obj->dev, pt_dma, IOPTE_TABLE_SIZE,
489 DMA_TO_DEVICE);
490 }
491
Zhouyi Zhoue28045a2014-03-05 18:20:19 +0800492 kmem_cache_free(iopte_cachep, iopte);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500493 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200494}
495
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500496static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd,
497 dma_addr_t *pt_dma, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200498{
499 u32 *iopte;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500500 unsigned long offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200501
502 /* a table has already existed */
503 if (*iopgd)
504 goto pte_ready;
505
506 /*
507 * do the allocation outside the page table lock
508 */
509 spin_unlock(&obj->page_table_lock);
510 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
511 spin_lock(&obj->page_table_lock);
512
513 if (!*iopgd) {
514 if (!iopte)
515 return ERR_PTR(-ENOMEM);
516
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500517 *pt_dma = dma_map_single(obj->dev, iopte, IOPTE_TABLE_SIZE,
518 DMA_TO_DEVICE);
519 if (dma_mapping_error(obj->dev, *pt_dma)) {
520 dev_err(obj->dev, "DMA map error for L2 table\n");
521 iopte_free(obj, iopte, false);
522 return ERR_PTR(-ENOMEM);
523 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200524
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500525 /*
526 * we rely on dma address and the physical address to be
527 * the same for mapping the L2 table
528 */
529 if (WARN_ON(*pt_dma != virt_to_phys(iopte))) {
530 dev_err(obj->dev, "DMA translation error for L2 table\n");
531 dma_unmap_single(obj->dev, *pt_dma, IOPTE_TABLE_SIZE,
532 DMA_TO_DEVICE);
533 iopte_free(obj, iopte, false);
534 return ERR_PTR(-ENOMEM);
535 }
536
537 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
538
539 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200540 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
541 } else {
542 /* We raced, free the reduniovant table */
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500543 iopte_free(obj, iopte, false);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200544 }
545
546pte_ready:
547 iopte = iopte_offset(iopgd, da);
Ralf Goebel04c532a2018-08-06 17:00:36 +0200548 *pt_dma = iopgd_page_paddr(iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200549 dev_vdbg(obj->dev,
550 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
551 __func__, da, iopgd, *iopgd, iopte, *iopte);
552
553 return iopte;
554}
555
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300556static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200557{
558 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500559 unsigned long offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200560
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300561 if ((da | pa) & ~IOSECTION_MASK) {
562 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
563 __func__, da, pa, IOSECTION_SIZE);
564 return -EINVAL;
565 }
566
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200567 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500568 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200569 return 0;
570}
571
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300572static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200573{
574 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500575 unsigned long offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200576 int i;
577
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300578 if ((da | pa) & ~IOSUPER_MASK) {
579 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
580 __func__, da, pa, IOSUPER_SIZE);
581 return -EINVAL;
582 }
583
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200584 for (i = 0; i < 16; i++)
585 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500586 flush_iopte_range(obj->dev, obj->pd_dma, offset, 16);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200587 return 0;
588}
589
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300590static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200591{
592 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500593 dma_addr_t pt_dma;
594 u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
595 unsigned long offset = iopte_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200596
597 if (IS_ERR(iopte))
598 return PTR_ERR(iopte);
599
600 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500601 flush_iopte_range(obj->dev, pt_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200602
603 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
604 __func__, da, pa, iopte, *iopte);
605
606 return 0;
607}
608
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300609static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200610{
611 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500612 dma_addr_t pt_dma;
613 u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
614 unsigned long offset = iopte_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200615 int i;
616
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300617 if ((da | pa) & ~IOLARGE_MASK) {
618 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
619 __func__, da, pa, IOLARGE_SIZE);
620 return -EINVAL;
621 }
622
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200623 if (IS_ERR(iopte))
624 return PTR_ERR(iopte);
625
626 for (i = 0; i < 16; i++)
627 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500628 flush_iopte_range(obj->dev, pt_dma, offset, 16);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200629 return 0;
630}
631
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300632static int
633iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200634{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300635 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200636 u32 prot;
637 int err;
638
639 if (!obj || !e)
640 return -EINVAL;
641
642 switch (e->pgsz) {
643 case MMU_CAM_PGSZ_16M:
644 fn = iopgd_alloc_super;
645 break;
646 case MMU_CAM_PGSZ_1M:
647 fn = iopgd_alloc_section;
648 break;
649 case MMU_CAM_PGSZ_64K:
650 fn = iopte_alloc_large;
651 break;
652 case MMU_CAM_PGSZ_4K:
653 fn = iopte_alloc_page;
654 break;
655 default:
656 fn = NULL;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200657 break;
658 }
659
Suman Anna7c1ab602016-04-04 17:46:19 -0500660 if (WARN_ON(!fn))
661 return -EINVAL;
662
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200663 prot = get_iopte_attr(e);
664
665 spin_lock(&obj->page_table_lock);
666 err = fn(obj, e->da, e->pa, prot);
667 spin_unlock(&obj->page_table_lock);
668
669 return err;
670}
671
672/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300673 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200674 * @obj: target iommu
675 * @e: an iommu tlb entry info
676 **/
Suman Anna4899a562014-10-22 17:22:32 -0500677static int
678omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200679{
680 int err;
681
682 flush_iotlb_page(obj, e->da);
683 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200684 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300685 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200686 return err;
687}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200688
689/**
690 * iopgtable_lookup_entry - Lookup an iommu pte entry
691 * @obj: target iommu
692 * @da: iommu device virtual address
693 * @ppgd: iommu pgd entry pointer to be returned
694 * @ppte: iommu pte entry pointer to be returned
695 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300696static void
697iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200698{
699 u32 *iopgd, *iopte = NULL;
700
701 iopgd = iopgd_offset(obj, da);
702 if (!*iopgd)
703 goto out;
704
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300705 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200706 iopte = iopte_offset(iopgd, da);
707out:
708 *ppgd = iopgd;
709 *ppte = iopte;
710}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200711
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300712static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200713{
714 size_t bytes;
715 u32 *iopgd = iopgd_offset(obj, da);
716 int nent = 1;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500717 dma_addr_t pt_dma;
718 unsigned long pd_offset = iopgd_index(da) * sizeof(da);
719 unsigned long pt_offset = iopte_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200720
721 if (!*iopgd)
722 return 0;
723
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300724 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200725 int i;
726 u32 *iopte = iopte_offset(iopgd, da);
727
728 bytes = IOPTE_SIZE;
729 if (*iopte & IOPTE_LARGE) {
730 nent *= 16;
731 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800732 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200733 }
734 bytes *= nent;
735 memset(iopte, 0, nent * sizeof(*iopte));
Ralf Goebel04c532a2018-08-06 17:00:36 +0200736 pt_dma = iopgd_page_paddr(iopgd);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500737 flush_iopte_range(obj->dev, pt_dma, pt_offset, nent);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200738
739 /*
740 * do table walk to check if this table is necessary or not
741 */
742 iopte = iopte_offset(iopgd, 0);
743 for (i = 0; i < PTRS_PER_IOPTE; i++)
744 if (iopte[i])
745 goto out;
746
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500747 iopte_free(obj, iopte, true);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200748 nent = 1; /* for the next L1 entry */
749 } else {
750 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700751 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200752 nent *= 16;
753 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800754 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200755 }
756 bytes *= nent;
757 }
758 memset(iopgd, 0, nent * sizeof(*iopgd));
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500759 flush_iopte_range(obj->dev, obj->pd_dma, pd_offset, nent);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200760out:
761 return bytes;
762}
763
764/**
765 * iopgtable_clear_entry - Remove an iommu pte entry
766 * @obj: target iommu
767 * @da: iommu device virtual address
768 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300769static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200770{
771 size_t bytes;
772
773 spin_lock(&obj->page_table_lock);
774
775 bytes = iopgtable_clear_entry_core(obj, da);
776 flush_iotlb_page(obj, da);
777
778 spin_unlock(&obj->page_table_lock);
779
780 return bytes;
781}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200782
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300783static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200784{
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500785 unsigned long offset;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200786 int i;
787
788 spin_lock(&obj->page_table_lock);
789
790 for (i = 0; i < PTRS_PER_IOPGD; i++) {
791 u32 da;
792 u32 *iopgd;
793
794 da = i << IOPGD_SHIFT;
795 iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500796 offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200797
798 if (!*iopgd)
799 continue;
800
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300801 if (iopgd_is_table(*iopgd))
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500802 iopte_free(obj, iopte_offset(iopgd, 0), true);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200803
804 *iopgd = 0;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500805 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200806 }
807
808 flush_iotlb_all(obj);
809
810 spin_unlock(&obj->page_table_lock);
811}
812
813/*
814 * Device IOMMU generic operations
815 */
816static irqreturn_t iommu_fault_handler(int irq, void *data)
817{
David Cohend594f1f2011-02-16 19:35:51 +0000818 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200819 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300820 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400821 struct iommu_domain *domain = obj->domain;
Joerg Roedel8cf851e2015-03-26 13:43:09 +0100822 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200823
Suman Anna0d364282017-09-05 17:56:17 -0500824 if (!omap_domain->dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200825 return IRQ_NONE;
826
David Cohend594f1f2011-02-16 19:35:51 +0000827 errs = iommu_report_fault(obj, &da);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200828 if (errs == 0)
829 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000830
831 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400832 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200833 return IRQ_HANDLED;
834
Fernando Guzman Lugo159d3e32017-07-28 15:49:13 -0500835 iommu_write_reg(obj, 0, MMU_IRQENABLE);
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000836
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200837 iopgd = iopgd_offset(obj, da);
838
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300839 if (!iopgd_is_table(*iopgd)) {
Suman Annab6c2e092013-05-30 18:10:59 -0500840 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 -0500841 obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200842 return IRQ_NONE;
843 }
844
845 iopte = iopte_offset(iopgd, da);
846
Suman Annab6c2e092013-05-30 18:10:59 -0500847 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 -0500848 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200849
850 return IRQ_NONE;
851}
852
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200853/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300854 * omap_iommu_attach() - attach iommu device to an iommu domain
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500855 * @obj: target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300856 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200857 **/
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500858static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200859{
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600860 int err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200861
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300862 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200863
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500864 obj->pd_dma = dma_map_single(obj->dev, iopgd, IOPGD_TABLE_SIZE,
865 DMA_TO_DEVICE);
866 if (dma_mapping_error(obj->dev, obj->pd_dma)) {
867 dev_err(obj->dev, "DMA map error for L1 table\n");
868 err = -ENOMEM;
869 goto out_err;
870 }
871
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300872 obj->iopgd = iopgd;
873 err = iommu_enable(obj);
874 if (err)
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500875 goto out_err;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300876 flush_iotlb_all(obj);
877
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300878 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200879
880 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500881
882 return 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200883
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500884out_err:
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300885 spin_unlock(&obj->iommu_lock);
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500886
887 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200888}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200889
890/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300891 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200892 * @obj: target iommu
893 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300894static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200895{
Roel Kluinacf9d462010-01-08 10:29:05 -0800896 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200897 return;
898
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300899 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200900
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500901 dma_unmap_single(obj->dev, obj->pd_dma, IOPGD_TABLE_SIZE,
902 DMA_TO_DEVICE);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500903 obj->pd_dma = 0;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300904 obj->iopgd = NULL;
Suman Annac3b44a02019-08-07 11:26:48 +0300905 iommu_disable(obj);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300906
907 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200908
909 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
910}
David Cohend594f1f2011-02-16 19:35:51 +0000911
Suman Annac3b44a02019-08-07 11:26:48 +0300912static void omap_iommu_save_tlb_entries(struct omap_iommu *obj)
913{
914 struct iotlb_lock lock;
915 struct cr_regs cr;
916 struct cr_regs *tmp;
917 int i;
918
919 /* check if there are any locked tlbs to save */
920 iotlb_lock_get(obj, &lock);
921 obj->num_cr_ctx = lock.base;
922 if (!obj->num_cr_ctx)
923 return;
924
925 tmp = obj->cr_ctx;
926 for_each_iotlb_cr(obj, obj->num_cr_ctx, i, cr)
927 * tmp++ = cr;
928}
929
930static void omap_iommu_restore_tlb_entries(struct omap_iommu *obj)
931{
932 struct iotlb_lock l;
933 struct cr_regs *tmp;
934 int i;
935
936 /* no locked tlbs to restore */
937 if (!obj->num_cr_ctx)
938 return;
939
940 l.base = 0;
941 tmp = obj->cr_ctx;
942 for (i = 0; i < obj->num_cr_ctx; i++, tmp++) {
943 l.vict = i;
944 iotlb_lock_set(obj, &l);
945 iotlb_load_cr(obj, tmp);
946 }
947 l.base = obj->num_cr_ctx;
948 l.vict = i;
949 iotlb_lock_set(obj, &l);
950}
951
Suman Annadb8918f2019-08-07 11:26:47 +0300952/**
Suman Annad9c4d8a2019-08-07 11:26:50 +0300953 * omap_iommu_domain_deactivate - deactivate attached iommu devices
954 * @domain: iommu domain attached to the target iommu device
955 *
956 * This API allows the client devices of IOMMU devices to suspend
957 * the IOMMUs they control at runtime, after they are idled and
958 * suspended all activity. System Suspend will leverage the PM
959 * driver late callbacks.
960 **/
961int omap_iommu_domain_deactivate(struct iommu_domain *domain)
962{
963 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
964 struct omap_iommu_device *iommu;
965 struct omap_iommu *oiommu;
966 int i;
967
968 if (!omap_domain->dev)
969 return 0;
970
971 iommu = omap_domain->iommus;
972 iommu += (omap_domain->num_iommus - 1);
973 for (i = 0; i < omap_domain->num_iommus; i++, iommu--) {
974 oiommu = iommu->iommu_dev;
975 pm_runtime_put_sync(oiommu->dev);
976 }
977
978 return 0;
979}
980EXPORT_SYMBOL_GPL(omap_iommu_domain_deactivate);
981
982/**
983 * omap_iommu_domain_activate - activate attached iommu devices
984 * @domain: iommu domain attached to the target iommu device
985 *
986 * This API allows the client devices of IOMMU devices to resume the
987 * IOMMUs they control at runtime, before they can resume operations.
988 * System Resume will leverage the PM driver late callbacks.
989 **/
990int omap_iommu_domain_activate(struct iommu_domain *domain)
991{
992 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
993 struct omap_iommu_device *iommu;
994 struct omap_iommu *oiommu;
995 int i;
996
997 if (!omap_domain->dev)
998 return 0;
999
1000 iommu = omap_domain->iommus;
1001 for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
1002 oiommu = iommu->iommu_dev;
1003 pm_runtime_get_sync(oiommu->dev);
1004 }
1005
1006 return 0;
1007}
1008EXPORT_SYMBOL_GPL(omap_iommu_domain_activate);
1009
1010/**
Suman Annadb8918f2019-08-07 11:26:47 +03001011 * omap_iommu_runtime_suspend - disable an iommu device
1012 * @dev: iommu device
1013 *
1014 * This function performs all that is necessary to disable an
1015 * IOMMU device, either during final detachment from a client
1016 * device, or during system/runtime suspend of the device. This
1017 * includes programming all the appropriate IOMMU registers, and
1018 * managing the associated omap_hwmod's state and the device's
Suman Annac3b44a02019-08-07 11:26:48 +03001019 * reset line. This function also saves the context of any
1020 * locked TLBs if suspending.
Suman Annadb8918f2019-08-07 11:26:47 +03001021 **/
1022static int omap_iommu_runtime_suspend(struct device *dev)
1023{
1024 struct platform_device *pdev = to_platform_device(dev);
1025 struct iommu_platform_data *pdata = dev_get_platdata(dev);
1026 struct omap_iommu *obj = to_iommu(dev);
1027 int ret;
1028
Suman Annac3b44a02019-08-07 11:26:48 +03001029 /* save the TLBs only during suspend, and not for power down */
1030 if (obj->domain && obj->iopgd)
1031 omap_iommu_save_tlb_entries(obj);
1032
Suman Annadb8918f2019-08-07 11:26:47 +03001033 omap2_iommu_disable(obj);
1034
1035 if (pdata && pdata->device_idle)
1036 pdata->device_idle(pdev);
1037
1038 if (pdata && pdata->assert_reset)
1039 pdata->assert_reset(pdev, pdata->reset_name);
1040
1041 if (pdata && pdata->set_pwrdm_constraint) {
1042 ret = pdata->set_pwrdm_constraint(pdev, false, &obj->pwrst);
1043 if (ret) {
1044 dev_warn(obj->dev, "pwrdm_constraint failed to be reset, status = %d\n",
1045 ret);
1046 }
1047 }
1048
1049 return 0;
1050}
1051
1052/**
1053 * omap_iommu_runtime_resume - enable an iommu device
1054 * @dev: iommu device
1055 *
1056 * This function performs all that is necessary to enable an
1057 * IOMMU device, either during initial attachment to a client
1058 * device, or during system/runtime resume of the device. This
1059 * includes programming all the appropriate IOMMU registers, and
1060 * managing the associated omap_hwmod's state and the device's
Suman Annac3b44a02019-08-07 11:26:48 +03001061 * reset line. The function also restores any locked TLBs if
1062 * resuming after a suspend.
Suman Annadb8918f2019-08-07 11:26:47 +03001063 **/
1064static int omap_iommu_runtime_resume(struct device *dev)
1065{
1066 struct platform_device *pdev = to_platform_device(dev);
1067 struct iommu_platform_data *pdata = dev_get_platdata(dev);
1068 struct omap_iommu *obj = to_iommu(dev);
1069 int ret = 0;
1070
1071 if (pdata && pdata->set_pwrdm_constraint) {
1072 ret = pdata->set_pwrdm_constraint(pdev, true, &obj->pwrst);
1073 if (ret) {
1074 dev_warn(obj->dev, "pwrdm_constraint failed to be set, status = %d\n",
1075 ret);
1076 }
1077 }
1078
1079 if (pdata && pdata->deassert_reset) {
1080 ret = pdata->deassert_reset(pdev, pdata->reset_name);
1081 if (ret) {
1082 dev_err(dev, "deassert_reset failed: %d\n", ret);
1083 return ret;
1084 }
1085 }
1086
1087 if (pdata && pdata->device_enable)
1088 pdata->device_enable(pdev);
1089
Suman Annac3b44a02019-08-07 11:26:48 +03001090 /* restore the TLBs only during resume, and not for power up */
1091 if (obj->domain)
1092 omap_iommu_restore_tlb_entries(obj);
1093
Suman Annadb8918f2019-08-07 11:26:47 +03001094 ret = omap2_iommu_enable(obj);
1095
1096 return ret;
1097}
1098
Suman Annac4206c42019-08-07 11:26:49 +03001099/**
1100 * omap_iommu_suspend_prepare - prepare() dev_pm_ops implementation
1101 * @dev: iommu device
1102 *
1103 * This function performs the necessary checks to determine if the IOMMU
1104 * device needs suspending or not. The function checks if the runtime_pm
1105 * status of the device is suspended, and returns 1 in that case. This
1106 * results in the PM core to skip invoking any of the Sleep PM callbacks
1107 * (suspend, suspend_late, resume, resume_early etc).
1108 */
1109static int omap_iommu_prepare(struct device *dev)
1110{
1111 if (pm_runtime_status_suspended(dev))
1112 return 1;
1113 return 0;
1114}
1115
Suman Anna9d5018d2017-09-05 17:56:18 -05001116static bool omap_iommu_can_register(struct platform_device *pdev)
1117{
1118 struct device_node *np = pdev->dev.of_node;
1119
1120 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
1121 return true;
1122
1123 /*
1124 * restrict IOMMU core registration only for processor-port MDMA MMUs
1125 * on DRA7 DSPs
1126 */
1127 if ((!strcmp(dev_name(&pdev->dev), "40d01000.mmu")) ||
1128 (!strcmp(dev_name(&pdev->dev), "41501000.mmu")))
1129 return true;
1130
1131 return false;
1132}
1133
Suman Anna3ca92992015-10-02 18:02:44 -05001134static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device *pdev,
1135 struct omap_iommu *obj)
1136{
1137 struct device_node *np = pdev->dev.of_node;
1138 int ret;
1139
1140 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
1141 return 0;
1142
1143 if (!of_property_read_bool(np, "ti,syscon-mmuconfig")) {
1144 dev_err(&pdev->dev, "ti,syscon-mmuconfig property is missing\n");
1145 return -EINVAL;
1146 }
1147
1148 obj->syscfg =
1149 syscon_regmap_lookup_by_phandle(np, "ti,syscon-mmuconfig");
1150 if (IS_ERR(obj->syscfg)) {
1151 /* can fail with -EPROBE_DEFER */
1152 ret = PTR_ERR(obj->syscfg);
1153 return ret;
1154 }
1155
1156 if (of_property_read_u32_index(np, "ti,syscon-mmuconfig", 1,
1157 &obj->id)) {
1158 dev_err(&pdev->dev, "couldn't get the IOMMU instance id within subsystem\n");
1159 return -EINVAL;
1160 }
1161
1162 if (obj->id != 0 && obj->id != 1) {
1163 dev_err(&pdev->dev, "invalid IOMMU instance id\n");
1164 return -EINVAL;
1165 }
1166
1167 return 0;
1168}
1169
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001170/*
1171 * OMAP Device MMU(IOMMU) detection
1172 */
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001173static int omap_iommu_probe(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001174{
1175 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001176 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001177 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001178 struct resource *res;
Florian Vaussard3c927482014-02-28 14:42:36 -06001179 struct device_node *of = pdev->dev.of_node;
Tero Kristo604629b2019-08-07 11:26:51 +03001180 struct orphan_dev *orphan_dev, *tmp;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001181
Suman Anna49a57ef2017-04-12 00:21:27 -05001182 if (!of) {
1183 pr_err("%s: only DT-based devices are supported\n", __func__);
1184 return -ENODEV;
1185 }
1186
Suman Annaf129b3d2014-02-28 14:42:32 -06001187 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001188 if (!obj)
1189 return -ENOMEM;
1190
Suman Annadb8918f2019-08-07 11:26:47 +03001191 /*
1192 * self-manage the ordering dependencies between omap_device_enable/idle
1193 * and omap_device_assert/deassert_hardreset API
1194 */
1195 if (pdev->dev.pm_domain) {
1196 dev_dbg(&pdev->dev, "device pm_domain is being reset\n");
1197 pdev->dev.pm_domain = NULL;
1198 }
1199
Suman Anna49a57ef2017-04-12 00:21:27 -05001200 obj->name = dev_name(&pdev->dev);
1201 obj->nr_tlb_entries = 32;
1202 err = of_property_read_u32(of, "ti,#tlb-entries", &obj->nr_tlb_entries);
1203 if (err && err != -EINVAL)
1204 return err;
1205 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
1206 return -EINVAL;
1207 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
1208 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
Florian Vaussard3c927482014-02-28 14:42:36 -06001209
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001210 obj->dev = &pdev->dev;
1211 obj->ctx = (void *)obj + sizeof(*obj);
Suman Annac3b44a02019-08-07 11:26:48 +03001212 obj->cr_ctx = devm_kzalloc(&pdev->dev,
1213 sizeof(*obj->cr_ctx) * obj->nr_tlb_entries,
1214 GFP_KERNEL);
1215 if (!obj->cr_ctx)
1216 return -ENOMEM;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001217
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001218 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001219 spin_lock_init(&obj->page_table_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001220
1221 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001222 obj->regbase = devm_ioremap_resource(obj->dev, res);
1223 if (IS_ERR(obj->regbase))
1224 return PTR_ERR(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +00001225
Suman Anna3ca92992015-10-02 18:02:44 -05001226 err = omap_iommu_dra7_get_dsp_system_cfg(pdev, obj);
1227 if (err)
1228 return err;
1229
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001230 irq = platform_get_irq(pdev, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001231 if (irq < 0)
1232 return -ENODEV;
1233
1234 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
1235 dev_name(obj->dev), obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001236 if (err < 0)
Suman Annaf129b3d2014-02-28 14:42:32 -06001237 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001238 platform_set_drvdata(pdev, obj);
1239
Suman Anna9d5018d2017-09-05 17:56:18 -05001240 if (omap_iommu_can_register(pdev)) {
1241 obj->group = iommu_group_alloc();
1242 if (IS_ERR(obj->group))
1243 return PTR_ERR(obj->group);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001244
Suman Anna9d5018d2017-09-05 17:56:18 -05001245 err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL,
1246 obj->name);
1247 if (err)
1248 goto out_group;
Joerg Roedel01611fe2017-04-12 00:21:30 -05001249
Suman Anna9d5018d2017-09-05 17:56:18 -05001250 iommu_device_set_ops(&obj->iommu, &omap_iommu_ops);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001251
Suman Anna9d5018d2017-09-05 17:56:18 -05001252 err = iommu_device_register(&obj->iommu);
1253 if (err)
1254 goto out_sysfs;
1255 }
Joerg Roedel01611fe2017-04-12 00:21:30 -05001256
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001257 pm_runtime_enable(obj->dev);
1258
Suman Anna61c75352014-10-22 17:22:30 -05001259 omap_iommu_debugfs_add(obj);
1260
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001261 dev_info(&pdev->dev, "%s registered\n", obj->name);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001262
Tero Kristo604629b2019-08-07 11:26:51 +03001263 list_for_each_entry_safe(orphan_dev, tmp, &orphan_dev_list, node) {
1264 err = _omap_iommu_add_device(orphan_dev->dev);
1265 if (!err) {
1266 list_del(&orphan_dev->node);
1267 kfree(orphan_dev);
1268 }
1269 }
1270
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001271 return 0;
Joerg Roedel01611fe2017-04-12 00:21:30 -05001272
1273out_sysfs:
1274 iommu_device_sysfs_remove(&obj->iommu);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001275out_group:
1276 iommu_group_put(obj->group);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001277 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001278}
1279
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001280static int omap_iommu_remove(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001281{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001282 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001283
Suman Anna9d5018d2017-09-05 17:56:18 -05001284 if (obj->group) {
1285 iommu_group_put(obj->group);
1286 obj->group = NULL;
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001287
Suman Anna9d5018d2017-09-05 17:56:18 -05001288 iommu_device_sysfs_remove(&obj->iommu);
1289 iommu_device_unregister(&obj->iommu);
1290 }
Joerg Roedel01611fe2017-04-12 00:21:30 -05001291
Suman Anna61c75352014-10-22 17:22:30 -05001292 omap_iommu_debugfs_remove(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001293
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001294 pm_runtime_disable(obj->dev);
1295
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001296 dev_info(&pdev->dev, "%s removed\n", obj->name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001297 return 0;
1298}
1299
Suman Annadb8918f2019-08-07 11:26:47 +03001300static const struct dev_pm_ops omap_iommu_pm_ops = {
Suman Annac4206c42019-08-07 11:26:49 +03001301 .prepare = omap_iommu_prepare,
1302 SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1303 pm_runtime_force_resume)
Suman Annadb8918f2019-08-07 11:26:47 +03001304 SET_RUNTIME_PM_OPS(omap_iommu_runtime_suspend,
1305 omap_iommu_runtime_resume, NULL)
1306};
1307
Kiran Padwald943b0f2014-09-11 19:07:36 +05301308static const struct of_device_id omap_iommu_of_match[] = {
Florian Vaussard3c927482014-02-28 14:42:36 -06001309 { .compatible = "ti,omap2-iommu" },
1310 { .compatible = "ti,omap4-iommu" },
1311 { .compatible = "ti,dra7-iommu" },
Suman Anna3ca92992015-10-02 18:02:44 -05001312 { .compatible = "ti,dra7-dsp-iommu" },
Florian Vaussard3c927482014-02-28 14:42:36 -06001313 {},
1314};
Florian Vaussard3c927482014-02-28 14:42:36 -06001315
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001316static struct platform_driver omap_iommu_driver = {
1317 .probe = omap_iommu_probe,
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001318 .remove = omap_iommu_remove,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001319 .driver = {
1320 .name = "omap-iommu",
Suman Annadb8918f2019-08-07 11:26:47 +03001321 .pm = &omap_iommu_pm_ops,
Florian Vaussard3c927482014-02-28 14:42:36 -06001322 .of_match_table = of_match_ptr(omap_iommu_of_match),
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001323 },
1324};
1325
Laurent Pinchart286f6002014-03-08 00:44:38 +01001326static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001327{
1328 memset(e, 0, sizeof(*e));
1329
1330 e->da = da;
1331 e->pa = pa;
Suman Annad760e3e2014-03-17 20:31:32 -05001332 e->valid = MMU_CAM_V;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001333 e->pgsz = pgsz;
1334 e->endian = MMU_RAM_ENDIAN_LITTLE;
1335 e->elsz = MMU_RAM_ELSZ_8;
1336 e->mixed = 0;
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001337
1338 return iopgsz_to_bytes(e->pgsz);
1339}
1340
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001341static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Suman Anna5835b6a2015-07-20 17:33:32 -05001342 phys_addr_t pa, size_t bytes, int prot)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001343{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001344 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Suman Anna9d5018d2017-09-05 17:56:18 -05001345 struct device *dev = omap_domain->dev;
1346 struct omap_iommu_device *iommu;
1347 struct omap_iommu *oiommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001348 struct iotlb_entry e;
1349 int omap_pgsz;
Suman Anna9d5018d2017-09-05 17:56:18 -05001350 u32 ret = -EINVAL;
1351 int i;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001352
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001353 omap_pgsz = bytes_to_iopgsz(bytes);
1354 if (omap_pgsz < 0) {
1355 dev_err(dev, "invalid size to map: %d\n", bytes);
1356 return -EINVAL;
1357 }
1358
Joerg Roedel1d7f4492015-01-22 14:42:06 +01001359 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 +03001360
Laurent Pinchart286f6002014-03-08 00:44:38 +01001361 iotlb_init_entry(&e, da, pa, omap_pgsz);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001362
Suman Anna9d5018d2017-09-05 17:56:18 -05001363 iommu = omap_domain->iommus;
1364 for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
1365 oiommu = iommu->iommu_dev;
1366 ret = omap_iopgtable_store_entry(oiommu, &e);
1367 if (ret) {
1368 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n",
1369 ret);
1370 break;
1371 }
1372 }
1373
1374 if (ret) {
1375 while (i--) {
1376 iommu--;
1377 oiommu = iommu->iommu_dev;
1378 iopgtable_clear_entry(oiommu, da);
1379 }
1380 }
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001381
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001382 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001383}
1384
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001385static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
Suman Anna5835b6a2015-07-20 17:33:32 -05001386 size_t size)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001387{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001388 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Suman Anna9d5018d2017-09-05 17:56:18 -05001389 struct device *dev = omap_domain->dev;
1390 struct omap_iommu_device *iommu;
1391 struct omap_iommu *oiommu;
1392 bool error = false;
1393 size_t bytes = 0;
1394 int i;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001395
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001396 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001397
Suman Anna9d5018d2017-09-05 17:56:18 -05001398 iommu = omap_domain->iommus;
1399 for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
1400 oiommu = iommu->iommu_dev;
1401 bytes = iopgtable_clear_entry(oiommu, da);
1402 if (!bytes)
1403 error = true;
1404 }
1405
1406 /*
1407 * simplify return - we are only checking if any of the iommus
1408 * reported an error, but not if all of them are unmapping the
1409 * same number of entries. This should not occur due to the
1410 * mirror programming.
1411 */
1412 return error ? 0 : bytes;
1413}
1414
1415static int omap_iommu_count(struct device *dev)
1416{
1417 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1418 int count = 0;
1419
1420 while (arch_data->iommu_dev) {
1421 count++;
1422 arch_data++;
1423 }
1424
1425 return count;
1426}
1427
1428/* caller should call cleanup if this function fails */
1429static int omap_iommu_attach_init(struct device *dev,
1430 struct omap_iommu_domain *odomain)
1431{
1432 struct omap_iommu_device *iommu;
1433 int i;
1434
1435 odomain->num_iommus = omap_iommu_count(dev);
1436 if (!odomain->num_iommus)
1437 return -EINVAL;
1438
1439 odomain->iommus = kcalloc(odomain->num_iommus, sizeof(*iommu),
1440 GFP_ATOMIC);
1441 if (!odomain->iommus)
1442 return -ENOMEM;
1443
1444 iommu = odomain->iommus;
1445 for (i = 0; i < odomain->num_iommus; i++, iommu++) {
1446 iommu->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_ATOMIC);
1447 if (!iommu->pgtable)
1448 return -ENOMEM;
1449
1450 /*
1451 * should never fail, but please keep this around to ensure
1452 * we keep the hardware happy
1453 */
1454 if (WARN_ON(!IS_ALIGNED((long)iommu->pgtable,
1455 IOPGD_TABLE_SIZE)))
1456 return -EINVAL;
1457 }
1458
1459 return 0;
1460}
1461
1462static void omap_iommu_detach_fini(struct omap_iommu_domain *odomain)
1463{
1464 int i;
1465 struct omap_iommu_device *iommu = odomain->iommus;
1466
1467 for (i = 0; iommu && i < odomain->num_iommus; i++, iommu++)
1468 kfree(iommu->pgtable);
1469
1470 kfree(odomain->iommus);
1471 odomain->num_iommus = 0;
1472 odomain->iommus = NULL;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001473}
1474
1475static int
1476omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1477{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001478 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001479 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Suman Anna9d5018d2017-09-05 17:56:18 -05001480 struct omap_iommu_device *iommu;
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001481 struct omap_iommu *oiommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001482 int ret = 0;
Suman Anna9d5018d2017-09-05 17:56:18 -05001483 int i;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001484
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001485 if (!arch_data || !arch_data->iommu_dev) {
Suman Annae3f595b2014-09-04 17:27:29 -05001486 dev_err(dev, "device doesn't have an associated iommu\n");
1487 return -EINVAL;
1488 }
1489
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001490 spin_lock(&omap_domain->lock);
1491
Suman Anna0d364282017-09-05 17:56:17 -05001492 /* only a single client device can be attached to a domain */
1493 if (omap_domain->dev) {
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001494 dev_err(dev, "iommu domain is already attached\n");
1495 ret = -EBUSY;
1496 goto out;
1497 }
1498
Suman Anna9d5018d2017-09-05 17:56:18 -05001499 ret = omap_iommu_attach_init(dev, omap_domain);
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001500 if (ret) {
Suman Anna9d5018d2017-09-05 17:56:18 -05001501 dev_err(dev, "failed to allocate required iommu data %d\n",
1502 ret);
1503 goto init_fail;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001504 }
1505
Suman Anna9d5018d2017-09-05 17:56:18 -05001506 iommu = omap_domain->iommus;
1507 for (i = 0; i < omap_domain->num_iommus; i++, iommu++, arch_data++) {
1508 /* configure and enable the omap iommu */
1509 oiommu = arch_data->iommu_dev;
1510 ret = omap_iommu_attach(oiommu, iommu->pgtable);
1511 if (ret) {
1512 dev_err(dev, "can't get omap iommu: %d\n", ret);
1513 goto attach_fail;
1514 }
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001515
Suman Anna9d5018d2017-09-05 17:56:18 -05001516 oiommu->domain = domain;
1517 iommu->iommu_dev = oiommu;
1518 }
1519
1520 omap_domain->dev = dev;
1521
1522 goto out;
1523
1524attach_fail:
1525 while (i--) {
1526 iommu--;
1527 arch_data--;
1528 oiommu = iommu->iommu_dev;
1529 omap_iommu_detach(oiommu);
1530 iommu->iommu_dev = NULL;
1531 oiommu->domain = NULL;
1532 }
1533init_fail:
1534 omap_iommu_detach_fini(omap_domain);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001535out:
1536 spin_unlock(&omap_domain->lock);
1537 return ret;
1538}
1539
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001540static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001541 struct device *dev)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001542{
Suman Anna9d5018d2017-09-05 17:56:18 -05001543 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1544 struct omap_iommu_device *iommu = omap_domain->iommus;
1545 struct omap_iommu *oiommu;
1546 int i;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001547
Suman Anna0d364282017-09-05 17:56:17 -05001548 if (!omap_domain->dev) {
1549 dev_err(dev, "domain has no attached device\n");
1550 return;
1551 }
1552
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001553 /* only a single device is supported per domain for now */
Suman Anna0d364282017-09-05 17:56:17 -05001554 if (omap_domain->dev != dev) {
1555 dev_err(dev, "invalid attached device\n");
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001556 return;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001557 }
1558
Suman Anna9d5018d2017-09-05 17:56:18 -05001559 /*
1560 * cleanup in the reverse order of attachment - this addresses
1561 * any h/w dependencies between multiple instances, if any
1562 */
1563 iommu += (omap_domain->num_iommus - 1);
1564 arch_data += (omap_domain->num_iommus - 1);
1565 for (i = 0; i < omap_domain->num_iommus; i++, iommu--, arch_data--) {
1566 oiommu = iommu->iommu_dev;
1567 iopgtable_clear_entry_all(oiommu);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001568
Suman Anna9d5018d2017-09-05 17:56:18 -05001569 omap_iommu_detach(oiommu);
1570 iommu->iommu_dev = NULL;
1571 oiommu->domain = NULL;
1572 }
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001573
Suman Anna9d5018d2017-09-05 17:56:18 -05001574 omap_iommu_detach_fini(omap_domain);
1575
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001576 omap_domain->dev = NULL;
1577}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001578
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001579static void omap_iommu_detach_dev(struct iommu_domain *domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001580 struct device *dev)
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001581{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001582 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001583
1584 spin_lock(&omap_domain->lock);
1585 _omap_iommu_detach_dev(omap_domain, dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001586 spin_unlock(&omap_domain->lock);
1587}
1588
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001589static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001590{
1591 struct omap_iommu_domain *omap_domain;
1592
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001593 if (type != IOMMU_DOMAIN_UNMANAGED)
1594 return NULL;
1595
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001596 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
Suman Anna99ee98d2015-07-20 17:33:29 -05001597 if (!omap_domain)
Suman Anna9d5018d2017-09-05 17:56:18 -05001598 return NULL;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001599
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001600 spin_lock_init(&omap_domain->lock);
1601
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001602 omap_domain->domain.geometry.aperture_start = 0;
1603 omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
1604 omap_domain->domain.geometry.force_aperture = true;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001605
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001606 return &omap_domain->domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001607}
1608
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001609static void omap_iommu_domain_free(struct iommu_domain *domain)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001610{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001611 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001612
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001613 /*
1614 * An iommu device is still attached
1615 * (currently, only one device can be attached) ?
1616 */
Suman Anna0d364282017-09-05 17:56:17 -05001617 if (omap_domain->dev)
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001618 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1619
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001620 kfree(omap_domain);
1621}
1622
1623static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001624 dma_addr_t da)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001625{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001626 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Suman Anna9d5018d2017-09-05 17:56:18 -05001627 struct omap_iommu_device *iommu = omap_domain->iommus;
1628 struct omap_iommu *oiommu = iommu->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001629 struct device *dev = oiommu->dev;
1630 u32 *pgd, *pte;
1631 phys_addr_t ret = 0;
1632
Suman Anna9d5018d2017-09-05 17:56:18 -05001633 /*
1634 * all the iommus within the domain will have identical programming,
1635 * so perform the lookup using just the first iommu
1636 */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001637 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1638
1639 if (pte) {
1640 if (iopte_is_small(*pte))
1641 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1642 else if (iopte_is_large(*pte))
1643 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1644 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001645 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
Suman Anna5835b6a2015-07-20 17:33:32 -05001646 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001647 } else {
1648 if (iopgd_is_section(*pgd))
1649 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1650 else if (iopgd_is_super(*pgd))
1651 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1652 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001653 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
Suman Anna5835b6a2015-07-20 17:33:32 -05001654 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001655 }
1656
1657 return ret;
1658}
1659
Tero Kristo604629b2019-08-07 11:26:51 +03001660static int _omap_iommu_add_device(struct device *dev)
Laurent Pinchart07a02032014-02-28 14:42:38 -06001661{
Suman Anna9d5018d2017-09-05 17:56:18 -05001662 struct omap_iommu_arch_data *arch_data, *tmp;
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001663 struct omap_iommu *oiommu;
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001664 struct iommu_group *group;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001665 struct device_node *np;
Suman Anna7d682772014-09-04 17:27:30 -05001666 struct platform_device *pdev;
Suman Anna9d5018d2017-09-05 17:56:18 -05001667 int num_iommus, i;
Joerg Roedel01611fe2017-04-12 00:21:30 -05001668 int ret;
Tero Kristo604629b2019-08-07 11:26:51 +03001669 struct orphan_dev *orphan_dev;
1670 unsigned long flags;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001671
1672 /*
1673 * Allocate the archdata iommu structure for DT-based devices.
1674 *
1675 * TODO: Simplify this when removing non-DT support completely from the
1676 * IOMMU users.
1677 */
1678 if (!dev->of_node)
1679 return 0;
1680
Suman Anna9d5018d2017-09-05 17:56:18 -05001681 /*
1682 * retrieve the count of IOMMU nodes using phandle size as element size
1683 * since #iommu-cells = 0 for OMAP
1684 */
1685 num_iommus = of_property_count_elems_of_size(dev->of_node, "iommus",
1686 sizeof(phandle));
1687 if (num_iommus < 0)
Laurent Pinchart07a02032014-02-28 14:42:38 -06001688 return 0;
1689
Kees Cook6396bb22018-06-12 14:03:40 -07001690 arch_data = kcalloc(num_iommus + 1, sizeof(*arch_data), GFP_KERNEL);
Suman Anna9d5018d2017-09-05 17:56:18 -05001691 if (!arch_data)
Laurent Pinchart07a02032014-02-28 14:42:38 -06001692 return -ENOMEM;
Suman Anna9d5018d2017-09-05 17:56:18 -05001693
1694 for (i = 0, tmp = arch_data; i < num_iommus; i++, tmp++) {
1695 np = of_parse_phandle(dev->of_node, "iommus", i);
1696 if (!np) {
1697 kfree(arch_data);
1698 return -EINVAL;
1699 }
1700
1701 pdev = of_find_device_by_node(np);
Tero Kristo604629b2019-08-07 11:26:51 +03001702 if (!pdev) {
Suman Anna9d5018d2017-09-05 17:56:18 -05001703 of_node_put(np);
1704 kfree(arch_data);
Tero Kristo604629b2019-08-07 11:26:51 +03001705 spin_lock_irqsave(&orphan_lock, flags);
1706 list_for_each_entry(orphan_dev, &orphan_dev_list,
1707 node) {
1708 if (orphan_dev->dev == dev)
1709 break;
1710 }
1711 spin_unlock_irqrestore(&orphan_lock, flags);
1712
1713 if (orphan_dev && orphan_dev->dev == dev)
1714 return -EPROBE_DEFER;
1715
1716 orphan_dev = kzalloc(sizeof(*orphan_dev), GFP_KERNEL);
1717 orphan_dev->dev = dev;
1718 spin_lock_irqsave(&orphan_lock, flags);
1719 list_add(&orphan_dev->node, &orphan_dev_list);
1720 spin_unlock_irqrestore(&orphan_lock, flags);
1721 return -EPROBE_DEFER;
Suman Anna9d5018d2017-09-05 17:56:18 -05001722 }
1723
1724 oiommu = platform_get_drvdata(pdev);
1725 if (!oiommu) {
1726 of_node_put(np);
1727 kfree(arch_data);
1728 return -EINVAL;
1729 }
1730
1731 tmp->iommu_dev = oiommu;
Tero Kristo604629b2019-08-07 11:26:51 +03001732 tmp->dev = &pdev->dev;
Suman Anna9d5018d2017-09-05 17:56:18 -05001733
1734 of_node_put(np);
Laurent Pinchart07a02032014-02-28 14:42:38 -06001735 }
1736
Suman Anna9d5018d2017-09-05 17:56:18 -05001737 /*
1738 * use the first IOMMU alone for the sysfs device linking.
1739 * TODO: Evaluate if a single iommu_group needs to be
1740 * maintained for both IOMMUs
1741 */
1742 oiommu = arch_data->iommu_dev;
Joerg Roedel01611fe2017-04-12 00:21:30 -05001743 ret = iommu_device_link(&oiommu->iommu, dev);
1744 if (ret) {
1745 kfree(arch_data);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001746 return ret;
1747 }
1748
Laurent Pinchart07a02032014-02-28 14:42:38 -06001749 dev->archdata.iommu = arch_data;
1750
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001751 /*
1752 * IOMMU group initialization calls into omap_iommu_device_group, which
1753 * needs a valid dev->archdata.iommu pointer
1754 */
1755 group = iommu_group_get_for_dev(dev);
1756 if (IS_ERR(group)) {
1757 iommu_device_unlink(&oiommu->iommu, dev);
1758 dev->archdata.iommu = NULL;
1759 kfree(arch_data);
1760 return PTR_ERR(group);
1761 }
1762 iommu_group_put(group);
1763
Laurent Pinchart07a02032014-02-28 14:42:38 -06001764 return 0;
1765}
1766
Tero Kristo604629b2019-08-07 11:26:51 +03001767static int omap_iommu_add_device(struct device *dev)
1768{
1769 int ret;
1770
1771 ret = _omap_iommu_add_device(dev);
1772 if (ret == -EPROBE_DEFER)
1773 return 0;
1774
1775 return ret;
1776}
1777
Laurent Pinchart07a02032014-02-28 14:42:38 -06001778static void omap_iommu_remove_device(struct device *dev)
1779{
1780 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1781
1782 if (!dev->of_node || !arch_data)
1783 return;
1784
Joerg Roedel01611fe2017-04-12 00:21:30 -05001785 iommu_device_unlink(&arch_data->iommu_dev->iommu, dev);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001786 iommu_group_remove_device(dev);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001787
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001788 dev->archdata.iommu = NULL;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001789 kfree(arch_data);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001790
Laurent Pinchart07a02032014-02-28 14:42:38 -06001791}
1792
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001793static struct iommu_group *omap_iommu_device_group(struct device *dev)
1794{
1795 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Joerg Roedel8faf5e52017-06-28 12:50:16 +02001796 struct iommu_group *group = ERR_PTR(-EINVAL);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001797
1798 if (arch_data->iommu_dev)
Jeffy Chenb6d57f12018-03-01 19:22:08 +08001799 group = iommu_group_ref_get(arch_data->iommu_dev->group);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001800
1801 return group;
1802}
1803
Thierry Redingb22f6432014-06-27 09:03:12 +02001804static const struct iommu_ops omap_iommu_ops = {
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001805 .domain_alloc = omap_iommu_domain_alloc,
1806 .domain_free = omap_iommu_domain_free,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001807 .attach_dev = omap_iommu_attach_dev,
1808 .detach_dev = omap_iommu_detach_dev,
1809 .map = omap_iommu_map,
1810 .unmap = omap_iommu_unmap,
1811 .iova_to_phys = omap_iommu_iova_to_phys,
Laurent Pinchart07a02032014-02-28 14:42:38 -06001812 .add_device = omap_iommu_add_device,
1813 .remove_device = omap_iommu_remove_device,
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001814 .device_group = omap_iommu_device_group,
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +02001815 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001816};
1817
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001818static int __init omap_iommu_init(void)
1819{
1820 struct kmem_cache *p;
Suman Anna24ce0ba2019-08-16 17:58:37 -05001821 const slab_flags_t flags = SLAB_HWCACHE_ALIGN;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001822 size_t align = 1 << 10; /* L2 pagetable alignement */
Thierry Redingf938aab2015-02-06 11:44:06 +01001823 struct device_node *np;
Suman Annaabaa7e52017-04-12 00:21:26 -05001824 int ret;
Thierry Redingf938aab2015-02-06 11:44:06 +01001825
1826 np = of_find_matching_node(NULL, omap_iommu_of_match);
1827 if (!np)
1828 return 0;
1829
1830 of_node_put(np);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001831
1832 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
Josue Albarranbfee0cf2017-07-28 15:49:14 -05001833 NULL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001834 if (!p)
1835 return -ENOMEM;
1836 iopte_cachep = p;
1837
Suman Anna61c75352014-10-22 17:22:30 -05001838 omap_iommu_debugfs_init();
1839
Suman Annaabaa7e52017-04-12 00:21:26 -05001840 ret = platform_driver_register(&omap_iommu_driver);
1841 if (ret) {
1842 pr_err("%s: failed to register driver\n", __func__);
1843 goto fail_driver;
1844 }
1845
1846 ret = bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
1847 if (ret)
1848 goto fail_bus;
1849
1850 return 0;
1851
1852fail_bus:
1853 platform_driver_unregister(&omap_iommu_driver);
1854fail_driver:
1855 kmem_cache_destroy(iopte_cachep);
1856 return ret;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001857}
Ohad Ben-Cohen435792d2012-02-26 12:14:14 +02001858subsys_initcall(omap_iommu_init);
Suman Anna0cdbf722015-07-20 17:33:24 -05001859/* must be ready before omap3isp is probed */