blob: af4a8e7fcd27453f160788c93c660f7cf39ff3ab [file] [log] [blame]
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001/*
2 * omap iommu: tlb and pagetable primitives
3 *
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -08004 * Copyright (C) 2008-2010 Nokia Corporation
Suman Anna9d5018d2017-09-05 17:56:18 -05005 * Copyright (C) 2013-2017 Texas Instruments Incorporated - http://www.ti.com/
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02006 *
7 * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>,
8 * Paul Mundt and Toshihiro Kobayashi
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Josue Albarranbfee0cf2017-07-28 15:49:14 -050015#include <linux/dma-mapping.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020016#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020018#include <linux/interrupt.h>
19#include <linux/ioport.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020020#include <linux/platform_device.h>
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030021#include <linux/iommu.h>
Tony Lindgrenc8d35c82012-11-02 12:24:03 -070022#include <linux/omap-iommu.h>
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030023#include <linux/mutex.h>
24#include <linux/spinlock.h>
Tony Lindgrened1c7de2012-11-02 12:24:06 -070025#include <linux/io.h>
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -060026#include <linux/pm_runtime.h>
Florian Vaussard3c927482014-02-28 14:42:36 -060027#include <linux/of.h>
28#include <linux/of_iommu.h>
29#include <linux/of_irq.h>
Suman Anna7d682772014-09-04 17:27:30 -050030#include <linux/of_platform.h>
Suman Anna3ca92992015-10-02 18:02:44 -050031#include <linux/regmap.h>
32#include <linux/mfd/syscon.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020033
Tony Lindgren2ab7c842012-11-02 12:24:14 -070034#include <linux/platform_data/iommu-omap.h>
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020035
Ido Yariv2f7702a2012-11-02 12:24:00 -070036#include "omap-iopgtable.h"
Tony Lindgrened1c7de2012-11-02 12:24:06 -070037#include "omap-iommu.h"
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020038
Joerg Roedel01611fe2017-04-12 00:21:30 -050039static const struct iommu_ops omap_iommu_ops;
40
Suman Anna5acc97d2014-03-17 20:31:34 -050041#define to_iommu(dev) \
42 ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
43
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +020044/* bitmap of the page sizes currently supported */
45#define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
46
Ido Yariv7bd9e252012-11-02 12:24:09 -070047#define MMU_LOCK_BASE_SHIFT 10
48#define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
49#define MMU_LOCK_BASE(x) \
50 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
51
52#define MMU_LOCK_VICT_SHIFT 4
53#define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
54#define MMU_LOCK_VICT(x) \
55 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
56
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020057static struct platform_driver omap_iommu_driver;
58static struct kmem_cache *iopte_cachep;
59
60/**
Joerg Roedel8cf851e2015-03-26 13:43:09 +010061 * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
62 * @dom: generic iommu domain handle
63 **/
64static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
65{
66 return container_of(dom, struct omap_iommu_domain, domain);
67}
68
69/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030070 * omap_iommu_save_ctx - Save registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020071 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020072 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020073void omap_iommu_save_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020074{
Suman Anna9d5018d2017-09-05 17:56:18 -050075 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
76 struct omap_iommu *obj;
77 u32 *p;
Suman Annabd4396f2014-10-22 17:22:27 -050078 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020079
Suman Anna9d5018d2017-09-05 17:56:18 -050080 if (!arch_data)
81 return;
82
83 while (arch_data->iommu_dev) {
84 obj = arch_data->iommu_dev;
85 p = obj->ctx;
86 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
87 p[i] = iommu_read_reg(obj, i * sizeof(u32));
88 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i,
89 p[i]);
90 }
91 arch_data++;
Suman Annabd4396f2014-10-22 17:22:27 -050092 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020093}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030094EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020095
96/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +030097 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +020098 * @dev: client device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +020099 **/
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200100void omap_iommu_restore_ctx(struct device *dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200101{
Suman Anna9d5018d2017-09-05 17:56:18 -0500102 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
103 struct omap_iommu *obj;
104 u32 *p;
Suman Annabd4396f2014-10-22 17:22:27 -0500105 int i;
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200106
Suman Anna9d5018d2017-09-05 17:56:18 -0500107 if (!arch_data)
108 return;
109
110 while (arch_data->iommu_dev) {
111 obj = arch_data->iommu_dev;
112 p = obj->ctx;
113 for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
114 iommu_write_reg(obj, p[i], i * sizeof(u32));
115 dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i,
116 p[i]);
117 }
118 arch_data++;
Suman Annabd4396f2014-10-22 17:22:27 -0500119 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200120}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300121EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200122
Suman Anna3ca92992015-10-02 18:02:44 -0500123static void dra7_cfg_dspsys_mmu(struct omap_iommu *obj, bool enable)
124{
125 u32 val, mask;
126
127 if (!obj->syscfg)
128 return;
129
130 mask = (1 << (obj->id * DSP_SYS_MMU_CONFIG_EN_SHIFT));
131 val = enable ? mask : 0;
132 regmap_update_bits(obj->syscfg, DSP_SYS_MMU_CONFIG, mask, val);
133}
134
Suman Annabd4396f2014-10-22 17:22:27 -0500135static void __iommu_set_twl(struct omap_iommu *obj, bool on)
136{
137 u32 l = iommu_read_reg(obj, MMU_CNTL);
138
139 if (on)
140 iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
141 else
142 iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
143
144 l &= ~MMU_CNTL_MASK;
145 if (on)
146 l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
147 else
148 l |= (MMU_CNTL_MMU_EN);
149
150 iommu_write_reg(obj, l, MMU_CNTL);
151}
152
153static int omap2_iommu_enable(struct omap_iommu *obj)
154{
155 u32 l, pa;
156
157 if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
158 return -EINVAL;
159
160 pa = virt_to_phys(obj->iopgd);
161 if (!IS_ALIGNED(pa, SZ_16K))
162 return -EINVAL;
163
164 l = iommu_read_reg(obj, MMU_REVISION);
165 dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
166 (l >> 4) & 0xf, l & 0xf);
167
168 iommu_write_reg(obj, pa, MMU_TTB);
169
Suman Anna3ca92992015-10-02 18:02:44 -0500170 dra7_cfg_dspsys_mmu(obj, true);
171
Suman Annabd4396f2014-10-22 17:22:27 -0500172 if (obj->has_bus_err_back)
173 iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
174
175 __iommu_set_twl(obj, true);
176
177 return 0;
178}
179
180static void omap2_iommu_disable(struct omap_iommu *obj)
181{
182 u32 l = iommu_read_reg(obj, MMU_CNTL);
183
184 l &= ~MMU_CNTL_MASK;
185 iommu_write_reg(obj, l, MMU_CNTL);
Suman Anna3ca92992015-10-02 18:02:44 -0500186 dra7_cfg_dspsys_mmu(obj, false);
Suman Annabd4396f2014-10-22 17:22:27 -0500187
188 dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
189}
190
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300191static int iommu_enable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200192{
193 int err;
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600194 struct platform_device *pdev = to_platform_device(obj->dev);
Kiran Padwal99cb9ae2014-10-30 11:59:47 +0530195 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200196
Florian Vaussard90e569c2014-02-28 14:42:34 -0600197 if (pdata && pdata->deassert_reset) {
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600198 err = pdata->deassert_reset(pdev, pdata->reset_name);
199 if (err) {
200 dev_err(obj->dev, "deassert_reset failed: %d\n", err);
201 return err;
202 }
203 }
204
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600205 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200206
Suman Annabd4396f2014-10-22 17:22:27 -0500207 err = omap2_iommu_enable(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200208
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200209 return err;
210}
211
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300212static void iommu_disable(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200213{
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600214 struct platform_device *pdev = to_platform_device(obj->dev);
Kiran Padwal99cb9ae2014-10-30 11:59:47 +0530215 struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600216
Suman Annabd4396f2014-10-22 17:22:27 -0500217 omap2_iommu_disable(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200218
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600219 pm_runtime_put_sync(obj->dev);
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600220
Florian Vaussard90e569c2014-02-28 14:42:34 -0600221 if (pdata && pdata->assert_reset)
Omar Ramirez Luna72b15b62012-11-19 19:05:50 -0600222 pdata->assert_reset(pdev, pdata->reset_name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200223}
224
225/*
226 * TLB operations
227 */
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300228static u32 iotlb_cr_to_virt(struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200229{
Suman Annabd4396f2014-10-22 17:22:27 -0500230 u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
231 u32 mask = get_cam_va_mask(cr->cam & page_size);
232
233 return cr->cam & mask;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200234}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200235
236static u32 get_iopte_attr(struct iotlb_entry *e)
237{
Suman Annabd4396f2014-10-22 17:22:27 -0500238 u32 attr;
239
240 attr = e->mixed << 5;
241 attr |= e->endian;
242 attr |= e->elsz >> 3;
243 attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
244 (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
245 return attr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200246}
247
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300248static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200249{
Suman Annabd4396f2014-10-22 17:22:27 -0500250 u32 status, fault_addr;
251
252 status = iommu_read_reg(obj, MMU_IRQSTATUS);
253 status &= MMU_IRQ_MASK;
254 if (!status) {
255 *da = 0;
256 return 0;
257 }
258
259 fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
260 *da = fault_addr;
261
262 iommu_write_reg(obj, status, MMU_IRQSTATUS);
263
264 return status;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200265}
266
Suman Anna69c2c192015-07-20 17:33:25 -0500267void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200268{
269 u32 val;
270
271 val = iommu_read_reg(obj, MMU_LOCK);
272
273 l->base = MMU_LOCK_BASE(val);
274 l->vict = MMU_LOCK_VICT(val);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200275}
276
Suman Anna69c2c192015-07-20 17:33:25 -0500277void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200278{
279 u32 val;
280
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200281 val = (l->base << MMU_LOCK_BASE_SHIFT);
282 val |= (l->vict << MMU_LOCK_VICT_SHIFT);
283
284 iommu_write_reg(obj, val, MMU_LOCK);
285}
286
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300287static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200288{
Suman Annabd4396f2014-10-22 17:22:27 -0500289 cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
290 cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200291}
292
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300293static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200294{
Suman Annabd4396f2014-10-22 17:22:27 -0500295 iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
296 iommu_write_reg(obj, cr->ram, MMU_RAM);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200297
298 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
299 iommu_write_reg(obj, 1, MMU_LD_TLB);
300}
301
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000302/* only used in iotlb iteration for-loop */
Suman Anna69c2c192015-07-20 17:33:25 -0500303struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000304{
305 struct cr_regs cr;
306 struct iotlb_lock l;
307
308 iotlb_lock_get(obj, &l);
309 l.vict = n;
310 iotlb_lock_set(obj, &l);
311 iotlb_read_cr(obj, &cr);
312
313 return cr;
314}
315
Suman Annabd4396f2014-10-22 17:22:27 -0500316#ifdef PREFETCH_IOTLB
317static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
318 struct iotlb_entry *e)
319{
320 struct cr_regs *cr;
321
322 if (!e)
323 return NULL;
324
325 if (e->da & ~(get_cam_va_mask(e->pgsz))) {
326 dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
327 e->da);
328 return ERR_PTR(-EINVAL);
329 }
330
331 cr = kmalloc(sizeof(*cr), GFP_KERNEL);
332 if (!cr)
333 return ERR_PTR(-ENOMEM);
334
335 cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
336 cr->ram = e->pa | e->endian | e->elsz | e->mixed;
337
338 return cr;
339}
340
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200341/**
342 * load_iotlb_entry - Set an iommu tlb entry
343 * @obj: target iommu
344 * @e: an iommu tlb entry info
345 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300346static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200347{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200348 int err = 0;
349 struct iotlb_lock l;
350 struct cr_regs *cr;
351
352 if (!obj || !obj->nr_tlb_entries || !e)
353 return -EINVAL;
354
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600355 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200356
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000357 iotlb_lock_get(obj, &l);
358 if (l.base == obj->nr_tlb_entries) {
359 dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200360 err = -EBUSY;
361 goto out;
362 }
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000363 if (!e->prsvd) {
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000364 int i;
365 struct cr_regs tmp;
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000366
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000367 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000368 if (!iotlb_cr_valid(&tmp))
369 break;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000370
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000371 if (i == obj->nr_tlb_entries) {
372 dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
373 err = -EBUSY;
374 goto out;
375 }
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000376
377 iotlb_lock_get(obj, &l);
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000378 } else {
379 l.vict = l.base;
380 iotlb_lock_set(obj, &l);
381 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200382
383 cr = iotlb_alloc_cr(obj, e);
384 if (IS_ERR(cr)) {
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600385 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200386 return PTR_ERR(cr);
387 }
388
389 iotlb_load_cr(obj, cr);
390 kfree(cr);
391
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000392 if (e->prsvd)
393 l.base++;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200394 /* increment victim for next tlb load */
395 if (++l.vict == obj->nr_tlb_entries)
Kanigeri, Haribe6d8022010-04-22 23:26:11 +0000396 l.vict = l.base;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200397 iotlb_lock_set(obj, &l);
398out:
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600399 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200400 return err;
401}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200402
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300403#else /* !PREFETCH_IOTLB */
404
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300405static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300406{
407 return 0;
408}
409
410#endif /* !PREFETCH_IOTLB */
411
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300412static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300413{
414 return load_iotlb_entry(obj, e);
415}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200416
417/**
418 * flush_iotlb_page - Clear an iommu tlb entry
419 * @obj: target iommu
420 * @da: iommu device virtual address
421 *
422 * Clear an iommu tlb entry which includes 'da' address.
423 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300424static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200425{
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200426 int i;
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000427 struct cr_regs cr;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200428
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600429 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200430
Hiroshi DOYU37c28362010-04-27 05:37:12 +0000431 for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200432 u32 start;
433 size_t bytes;
434
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200435 if (!iotlb_cr_valid(&cr))
436 continue;
437
438 start = iotlb_cr_to_virt(&cr);
439 bytes = iopgsz_to_bytes(cr.cam & 3);
440
441 if ((start <= da) && (da < start + bytes)) {
442 dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
443 __func__, start, da, bytes);
Hari Kanigeri0fa035e2010-08-20 13:50:18 +0000444 iotlb_load_cr(obj, &cr);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200445 iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
Laurent Pinchartf7129a02014-03-07 23:47:03 +0100446 break;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200447 }
448 }
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600449 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200450
451 if (i == obj->nr_tlb_entries)
452 dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
453}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200454
455/**
456 * flush_iotlb_all - Clear all iommu tlb entries
457 * @obj: target iommu
458 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300459static void flush_iotlb_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200460{
461 struct iotlb_lock l;
462
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600463 pm_runtime_get_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200464
465 l.base = 0;
466 l.vict = 0;
467 iotlb_lock_set(obj, &l);
468
469 iommu_write_reg(obj, 1, MMU_GFLUSH);
470
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -0600471 pm_runtime_put_sync(obj->dev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200472}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200473
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200474/*
475 * H/W pagetable operations
476 */
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500477static void flush_iopte_range(struct device *dev, dma_addr_t dma,
478 unsigned long offset, int num_entries)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200479{
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500480 size_t size = num_entries * sizeof(u32);
481
482 dma_sync_single_range_for_device(dev, dma, offset, size, DMA_TO_DEVICE);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200483}
484
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500485static void iopte_free(struct omap_iommu *obj, u32 *iopte, bool dma_valid)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200486{
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500487 dma_addr_t pt_dma;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200488
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200489 /* Note: freed iopte's must be clean ready for re-use */
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500490 if (iopte) {
491 if (dma_valid) {
492 pt_dma = virt_to_phys(iopte);
493 dma_unmap_single(obj->dev, pt_dma, IOPTE_TABLE_SIZE,
494 DMA_TO_DEVICE);
495 }
496
Zhouyi Zhoue28045a2014-03-05 18:20:19 +0800497 kmem_cache_free(iopte_cachep, iopte);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500498 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200499}
500
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500501static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd,
502 dma_addr_t *pt_dma, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200503{
504 u32 *iopte;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500505 unsigned long offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200506
507 /* a table has already existed */
508 if (*iopgd)
509 goto pte_ready;
510
511 /*
512 * do the allocation outside the page table lock
513 */
514 spin_unlock(&obj->page_table_lock);
515 iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
516 spin_lock(&obj->page_table_lock);
517
518 if (!*iopgd) {
519 if (!iopte)
520 return ERR_PTR(-ENOMEM);
521
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500522 *pt_dma = dma_map_single(obj->dev, iopte, IOPTE_TABLE_SIZE,
523 DMA_TO_DEVICE);
524 if (dma_mapping_error(obj->dev, *pt_dma)) {
525 dev_err(obj->dev, "DMA map error for L2 table\n");
526 iopte_free(obj, iopte, false);
527 return ERR_PTR(-ENOMEM);
528 }
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200529
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500530 /*
531 * we rely on dma address and the physical address to be
532 * the same for mapping the L2 table
533 */
534 if (WARN_ON(*pt_dma != virt_to_phys(iopte))) {
535 dev_err(obj->dev, "DMA translation error for L2 table\n");
536 dma_unmap_single(obj->dev, *pt_dma, IOPTE_TABLE_SIZE,
537 DMA_TO_DEVICE);
538 iopte_free(obj, iopte, false);
539 return ERR_PTR(-ENOMEM);
540 }
541
542 *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
543
544 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200545 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
546 } else {
547 /* We raced, free the reduniovant table */
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500548 iopte_free(obj, iopte, false);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200549 }
550
551pte_ready:
552 iopte = iopte_offset(iopgd, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500553 *pt_dma = virt_to_phys(iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200554 dev_vdbg(obj->dev,
555 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
556 __func__, da, iopgd, *iopgd, iopte, *iopte);
557
558 return iopte;
559}
560
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300561static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200562{
563 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500564 unsigned long offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200565
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300566 if ((da | pa) & ~IOSECTION_MASK) {
567 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
568 __func__, da, pa, IOSECTION_SIZE);
569 return -EINVAL;
570 }
571
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200572 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500573 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200574 return 0;
575}
576
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300577static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200578{
579 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500580 unsigned long offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200581 int i;
582
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300583 if ((da | pa) & ~IOSUPER_MASK) {
584 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
585 __func__, da, pa, IOSUPER_SIZE);
586 return -EINVAL;
587 }
588
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200589 for (i = 0; i < 16; i++)
590 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500591 flush_iopte_range(obj->dev, obj->pd_dma, offset, 16);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200592 return 0;
593}
594
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300595static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200596{
597 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500598 dma_addr_t pt_dma;
599 u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
600 unsigned long offset = iopte_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200601
602 if (IS_ERR(iopte))
603 return PTR_ERR(iopte);
604
605 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500606 flush_iopte_range(obj->dev, pt_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200607
608 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
609 __func__, da, pa, iopte, *iopte);
610
611 return 0;
612}
613
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300614static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200615{
616 u32 *iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500617 dma_addr_t pt_dma;
618 u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
619 unsigned long offset = iopte_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200620 int i;
621
Hiroshi DOYU4abb7612010-05-06 18:24:04 +0300622 if ((da | pa) & ~IOLARGE_MASK) {
623 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
624 __func__, da, pa, IOLARGE_SIZE);
625 return -EINVAL;
626 }
627
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200628 if (IS_ERR(iopte))
629 return PTR_ERR(iopte);
630
631 for (i = 0; i < 16; i++)
632 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500633 flush_iopte_range(obj->dev, pt_dma, offset, 16);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200634 return 0;
635}
636
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300637static int
638iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200639{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300640 int (*fn)(struct omap_iommu *, u32, u32, u32);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200641 u32 prot;
642 int err;
643
644 if (!obj || !e)
645 return -EINVAL;
646
647 switch (e->pgsz) {
648 case MMU_CAM_PGSZ_16M:
649 fn = iopgd_alloc_super;
650 break;
651 case MMU_CAM_PGSZ_1M:
652 fn = iopgd_alloc_section;
653 break;
654 case MMU_CAM_PGSZ_64K:
655 fn = iopte_alloc_large;
656 break;
657 case MMU_CAM_PGSZ_4K:
658 fn = iopte_alloc_page;
659 break;
660 default:
661 fn = NULL;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200662 break;
663 }
664
Suman Anna7c1ab602016-04-04 17:46:19 -0500665 if (WARN_ON(!fn))
666 return -EINVAL;
667
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200668 prot = get_iopte_attr(e);
669
670 spin_lock(&obj->page_table_lock);
671 err = fn(obj, e->da, e->pa, prot);
672 spin_unlock(&obj->page_table_lock);
673
674 return err;
675}
676
677/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300678 * omap_iopgtable_store_entry - Make an iommu pte entry
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200679 * @obj: target iommu
680 * @e: an iommu tlb entry info
681 **/
Suman Anna4899a562014-10-22 17:22:32 -0500682static int
683omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200684{
685 int err;
686
687 flush_iotlb_page(obj, e->da);
688 err = iopgtable_store_entry_core(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200689 if (!err)
Ohad Ben-Cohen5da14a42011-08-16 15:19:10 +0300690 prefetch_iotlb_entry(obj, e);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200691 return err;
692}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200693
694/**
695 * iopgtable_lookup_entry - Lookup an iommu pte entry
696 * @obj: target iommu
697 * @da: iommu device virtual address
698 * @ppgd: iommu pgd entry pointer to be returned
699 * @ppte: iommu pte entry pointer to be returned
700 **/
Ohad Ben-Cohene1f23812011-08-16 14:58:14 +0300701static void
702iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200703{
704 u32 *iopgd, *iopte = NULL;
705
706 iopgd = iopgd_offset(obj, da);
707 if (!*iopgd)
708 goto out;
709
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300710 if (iopgd_is_table(*iopgd))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200711 iopte = iopte_offset(iopgd, da);
712out:
713 *ppgd = iopgd;
714 *ppte = iopte;
715}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200716
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300717static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200718{
719 size_t bytes;
720 u32 *iopgd = iopgd_offset(obj, da);
721 int nent = 1;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500722 dma_addr_t pt_dma;
723 unsigned long pd_offset = iopgd_index(da) * sizeof(da);
724 unsigned long pt_offset = iopte_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200725
726 if (!*iopgd)
727 return 0;
728
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300729 if (iopgd_is_table(*iopgd)) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200730 int i;
731 u32 *iopte = iopte_offset(iopgd, da);
732
733 bytes = IOPTE_SIZE;
734 if (*iopte & IOPTE_LARGE) {
735 nent *= 16;
736 /* rewind to the 1st entry */
Hiroshi DOYUc127c7d2010-02-15 10:03:32 -0800737 iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200738 }
739 bytes *= nent;
740 memset(iopte, 0, nent * sizeof(*iopte));
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500741 pt_dma = virt_to_phys(iopte);
742 flush_iopte_range(obj->dev, pt_dma, pt_offset, nent);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200743
744 /*
745 * do table walk to check if this table is necessary or not
746 */
747 iopte = iopte_offset(iopgd, 0);
748 for (i = 0; i < PTRS_PER_IOPTE; i++)
749 if (iopte[i])
750 goto out;
751
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500752 iopte_free(obj, iopte, true);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200753 nent = 1; /* for the next L1 entry */
754 } else {
755 bytes = IOPGD_SIZE;
Hiroshi DOYUdcc730d2009-10-22 14:46:32 -0700756 if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200757 nent *= 16;
758 /* rewind to the 1st entry */
Hiroshi DOYU8d33ea52010-02-15 10:03:32 -0800759 iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200760 }
761 bytes *= nent;
762 }
763 memset(iopgd, 0, nent * sizeof(*iopgd));
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500764 flush_iopte_range(obj->dev, obj->pd_dma, pd_offset, nent);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200765out:
766 return bytes;
767}
768
769/**
770 * iopgtable_clear_entry - Remove an iommu pte entry
771 * @obj: target iommu
772 * @da: iommu device virtual address
773 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300774static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200775{
776 size_t bytes;
777
778 spin_lock(&obj->page_table_lock);
779
780 bytes = iopgtable_clear_entry_core(obj, da);
781 flush_iotlb_page(obj, da);
782
783 spin_unlock(&obj->page_table_lock);
784
785 return bytes;
786}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200787
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300788static void iopgtable_clear_entry_all(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200789{
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500790 unsigned long offset;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200791 int i;
792
793 spin_lock(&obj->page_table_lock);
794
795 for (i = 0; i < PTRS_PER_IOPGD; i++) {
796 u32 da;
797 u32 *iopgd;
798
799 da = i << IOPGD_SHIFT;
800 iopgd = iopgd_offset(obj, da);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500801 offset = iopgd_index(da) * sizeof(da);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200802
803 if (!*iopgd)
804 continue;
805
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300806 if (iopgd_is_table(*iopgd))
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500807 iopte_free(obj, iopte_offset(iopgd, 0), true);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200808
809 *iopgd = 0;
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500810 flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200811 }
812
813 flush_iotlb_all(obj);
814
815 spin_unlock(&obj->page_table_lock);
816}
817
818/*
819 * Device IOMMU generic operations
820 */
821static irqreturn_t iommu_fault_handler(int irq, void *data)
822{
David Cohend594f1f2011-02-16 19:35:51 +0000823 u32 da, errs;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200824 u32 *iopgd, *iopte;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300825 struct omap_iommu *obj = data;
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400826 struct iommu_domain *domain = obj->domain;
Joerg Roedel8cf851e2015-03-26 13:43:09 +0100827 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200828
Suman Anna0d364282017-09-05 17:56:17 -0500829 if (!omap_domain->dev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200830 return IRQ_NONE;
831
David Cohend594f1f2011-02-16 19:35:51 +0000832 errs = iommu_report_fault(obj, &da);
Laurent Pinchartc56b2dd2011-05-10 16:56:46 +0200833 if (errs == 0)
834 return IRQ_HANDLED;
David Cohend594f1f2011-02-16 19:35:51 +0000835
836 /* Fault callback or TLB/PTE Dynamic loading */
Ohad Ben-Cohene7f10f02011-09-13 15:26:29 -0400837 if (!report_iommu_fault(domain, obj->dev, da, 0))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200838 return IRQ_HANDLED;
839
Fernando Guzman Lugo159d3e32017-07-28 15:49:13 -0500840 iommu_write_reg(obj, 0, MMU_IRQENABLE);
Hiroshi DOYU37b29812010-05-24 02:01:52 +0000841
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200842 iopgd = iopgd_offset(obj, da);
843
Hiroshi DOYUa1a54452010-05-13 09:45:35 +0300844 if (!iopgd_is_table(*iopgd)) {
Suman Annab6c2e092013-05-30 18:10:59 -0500845 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 -0500846 obj->name, errs, da, iopgd, *iopgd);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200847 return IRQ_NONE;
848 }
849
850 iopte = iopte_offset(iopgd, da);
851
Suman Annab6c2e092013-05-30 18:10:59 -0500852 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 -0500853 obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200854
855 return IRQ_NONE;
856}
857
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200858/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300859 * omap_iommu_attach() - attach iommu device to an iommu domain
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500860 * @obj: target omap iommu device
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300861 * @iopgd: page table
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200862 **/
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500863static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200864{
Suman Anna7ee08b9e2014-02-28 14:42:33 -0600865 int err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200866
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300867 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200868
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500869 obj->pd_dma = dma_map_single(obj->dev, iopgd, IOPGD_TABLE_SIZE,
870 DMA_TO_DEVICE);
871 if (dma_mapping_error(obj->dev, obj->pd_dma)) {
872 dev_err(obj->dev, "DMA map error for L1 table\n");
873 err = -ENOMEM;
874 goto out_err;
875 }
876
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300877 obj->iopgd = iopgd;
878 err = iommu_enable(obj);
879 if (err)
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500880 goto out_err;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300881 flush_iotlb_all(obj);
882
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300883 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200884
885 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500886
887 return 0;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200888
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500889out_err:
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300890 spin_unlock(&obj->iommu_lock);
Joerg Roedelede1c2e2017-04-12 00:21:29 -0500891
892 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200893}
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200894
895/**
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300896 * omap_iommu_detach - release iommu device
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200897 * @obj: target iommu
898 **/
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300899static void omap_iommu_detach(struct omap_iommu *obj)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200900{
Roel Kluinacf9d462010-01-08 10:29:05 -0800901 if (!obj || IS_ERR(obj))
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200902 return;
903
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300904 spin_lock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200905
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500906 dma_unmap_single(obj->dev, obj->pd_dma, IOPGD_TABLE_SIZE,
907 DMA_TO_DEVICE);
Suman Anna2088ecb2014-10-22 17:22:19 -0500908 iommu_disable(obj);
Josue Albarranbfee0cf2017-07-28 15:49:14 -0500909 obj->pd_dma = 0;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300910 obj->iopgd = NULL;
911
912 spin_unlock(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200913
914 dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
915}
David Cohend594f1f2011-02-16 19:35:51 +0000916
Suman Anna9d5018d2017-09-05 17:56:18 -0500917static bool omap_iommu_can_register(struct platform_device *pdev)
918{
919 struct device_node *np = pdev->dev.of_node;
920
921 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
922 return true;
923
924 /*
925 * restrict IOMMU core registration only for processor-port MDMA MMUs
926 * on DRA7 DSPs
927 */
928 if ((!strcmp(dev_name(&pdev->dev), "40d01000.mmu")) ||
929 (!strcmp(dev_name(&pdev->dev), "41501000.mmu")))
930 return true;
931
932 return false;
933}
934
Suman Anna3ca92992015-10-02 18:02:44 -0500935static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device *pdev,
936 struct omap_iommu *obj)
937{
938 struct device_node *np = pdev->dev.of_node;
939 int ret;
940
941 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
942 return 0;
943
944 if (!of_property_read_bool(np, "ti,syscon-mmuconfig")) {
945 dev_err(&pdev->dev, "ti,syscon-mmuconfig property is missing\n");
946 return -EINVAL;
947 }
948
949 obj->syscfg =
950 syscon_regmap_lookup_by_phandle(np, "ti,syscon-mmuconfig");
951 if (IS_ERR(obj->syscfg)) {
952 /* can fail with -EPROBE_DEFER */
953 ret = PTR_ERR(obj->syscfg);
954 return ret;
955 }
956
957 if (of_property_read_u32_index(np, "ti,syscon-mmuconfig", 1,
958 &obj->id)) {
959 dev_err(&pdev->dev, "couldn't get the IOMMU instance id within subsystem\n");
960 return -EINVAL;
961 }
962
963 if (obj->id != 0 && obj->id != 1) {
964 dev_err(&pdev->dev, "invalid IOMMU instance id\n");
965 return -EINVAL;
966 }
967
968 return 0;
969}
970
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200971/*
972 * OMAP Device MMU(IOMMU) detection
973 */
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -0800974static int omap_iommu_probe(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200975{
976 int err = -ENODEV;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200977 int irq;
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300978 struct omap_iommu *obj;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200979 struct resource *res;
Florian Vaussard3c927482014-02-28 14:42:36 -0600980 struct device_node *of = pdev->dev.of_node;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200981
Suman Anna49a57ef2017-04-12 00:21:27 -0500982 if (!of) {
983 pr_err("%s: only DT-based devices are supported\n", __func__);
984 return -ENODEV;
985 }
986
Suman Annaf129b3d2014-02-28 14:42:32 -0600987 obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +0200988 if (!obj)
989 return -ENOMEM;
990
Suman Anna49a57ef2017-04-12 00:21:27 -0500991 obj->name = dev_name(&pdev->dev);
992 obj->nr_tlb_entries = 32;
993 err = of_property_read_u32(of, "ti,#tlb-entries", &obj->nr_tlb_entries);
994 if (err && err != -EINVAL)
995 return err;
996 if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
997 return -EINVAL;
998 if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
999 obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
Florian Vaussard3c927482014-02-28 14:42:36 -06001000
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001001 obj->dev = &pdev->dev;
1002 obj->ctx = (void *)obj + sizeof(*obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001003
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001004 spin_lock_init(&obj->iommu_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001005 spin_lock_init(&obj->page_table_lock);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001006
1007 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001008 obj->regbase = devm_ioremap_resource(obj->dev, res);
1009 if (IS_ERR(obj->regbase))
1010 return PTR_ERR(obj->regbase);
Aaro Koskinenda4a0f72011-03-14 12:28:32 +00001011
Suman Anna3ca92992015-10-02 18:02:44 -05001012 err = omap_iommu_dra7_get_dsp_system_cfg(pdev, obj);
1013 if (err)
1014 return err;
1015
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001016 irq = platform_get_irq(pdev, 0);
Suman Annaf129b3d2014-02-28 14:42:32 -06001017 if (irq < 0)
1018 return -ENODEV;
1019
1020 err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
1021 dev_name(obj->dev), obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001022 if (err < 0)
Suman Annaf129b3d2014-02-28 14:42:32 -06001023 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001024 platform_set_drvdata(pdev, obj);
1025
Suman Anna9d5018d2017-09-05 17:56:18 -05001026 if (omap_iommu_can_register(pdev)) {
1027 obj->group = iommu_group_alloc();
1028 if (IS_ERR(obj->group))
1029 return PTR_ERR(obj->group);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001030
Suman Anna9d5018d2017-09-05 17:56:18 -05001031 err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL,
1032 obj->name);
1033 if (err)
1034 goto out_group;
Joerg Roedel01611fe2017-04-12 00:21:30 -05001035
Suman Anna9d5018d2017-09-05 17:56:18 -05001036 iommu_device_set_ops(&obj->iommu, &omap_iommu_ops);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001037
Suman Anna9d5018d2017-09-05 17:56:18 -05001038 err = iommu_device_register(&obj->iommu);
1039 if (err)
1040 goto out_sysfs;
1041 }
Joerg Roedel01611fe2017-04-12 00:21:30 -05001042
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001043 pm_runtime_irq_safe(obj->dev);
1044 pm_runtime_enable(obj->dev);
1045
Suman Anna61c75352014-10-22 17:22:30 -05001046 omap_iommu_debugfs_add(obj);
1047
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001048 dev_info(&pdev->dev, "%s registered\n", obj->name);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001049
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001050 return 0;
Joerg Roedel01611fe2017-04-12 00:21:30 -05001051
1052out_sysfs:
1053 iommu_device_sysfs_remove(&obj->iommu);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001054out_group:
1055 iommu_group_put(obj->group);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001056 return err;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001057}
1058
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001059static int omap_iommu_remove(struct platform_device *pdev)
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001060{
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +03001061 struct omap_iommu *obj = platform_get_drvdata(pdev);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001062
Suman Anna9d5018d2017-09-05 17:56:18 -05001063 if (obj->group) {
1064 iommu_group_put(obj->group);
1065 obj->group = NULL;
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001066
Suman Anna9d5018d2017-09-05 17:56:18 -05001067 iommu_device_sysfs_remove(&obj->iommu);
1068 iommu_device_unregister(&obj->iommu);
1069 }
Joerg Roedel01611fe2017-04-12 00:21:30 -05001070
Suman Anna61c75352014-10-22 17:22:30 -05001071 omap_iommu_debugfs_remove(obj);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001072
Omar Ramirez Lunaebf7cda2012-11-19 19:05:51 -06001073 pm_runtime_disable(obj->dev);
1074
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001075 dev_info(&pdev->dev, "%s removed\n", obj->name);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001076 return 0;
1077}
1078
Kiran Padwald943b0f2014-09-11 19:07:36 +05301079static const struct of_device_id omap_iommu_of_match[] = {
Florian Vaussard3c927482014-02-28 14:42:36 -06001080 { .compatible = "ti,omap2-iommu" },
1081 { .compatible = "ti,omap4-iommu" },
1082 { .compatible = "ti,dra7-iommu" },
Suman Anna3ca92992015-10-02 18:02:44 -05001083 { .compatible = "ti,dra7-dsp-iommu" },
Florian Vaussard3c927482014-02-28 14:42:36 -06001084 {},
1085};
Florian Vaussard3c927482014-02-28 14:42:36 -06001086
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001087static struct platform_driver omap_iommu_driver = {
1088 .probe = omap_iommu_probe,
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001089 .remove = omap_iommu_remove,
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001090 .driver = {
1091 .name = "omap-iommu",
Florian Vaussard3c927482014-02-28 14:42:36 -06001092 .of_match_table = of_match_ptr(omap_iommu_of_match),
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001093 },
1094};
1095
Laurent Pinchart286f6002014-03-08 00:44:38 +01001096static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001097{
1098 memset(e, 0, sizeof(*e));
1099
1100 e->da = da;
1101 e->pa = pa;
Suman Annad760e3e2014-03-17 20:31:32 -05001102 e->valid = MMU_CAM_V;
Laurent Pinchart286f6002014-03-08 00:44:38 +01001103 e->pgsz = pgsz;
1104 e->endian = MMU_RAM_ENDIAN_LITTLE;
1105 e->elsz = MMU_RAM_ELSZ_8;
1106 e->mixed = 0;
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001107
1108 return iopgsz_to_bytes(e->pgsz);
1109}
1110
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001111static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
Suman Anna5835b6a2015-07-20 17:33:32 -05001112 phys_addr_t pa, size_t bytes, int prot)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001113{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001114 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Suman Anna9d5018d2017-09-05 17:56:18 -05001115 struct device *dev = omap_domain->dev;
1116 struct omap_iommu_device *iommu;
1117 struct omap_iommu *oiommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001118 struct iotlb_entry e;
1119 int omap_pgsz;
Suman Anna9d5018d2017-09-05 17:56:18 -05001120 u32 ret = -EINVAL;
1121 int i;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001122
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001123 omap_pgsz = bytes_to_iopgsz(bytes);
1124 if (omap_pgsz < 0) {
1125 dev_err(dev, "invalid size to map: %d\n", bytes);
1126 return -EINVAL;
1127 }
1128
Joerg Roedel1d7f4492015-01-22 14:42:06 +01001129 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 +03001130
Laurent Pinchart286f6002014-03-08 00:44:38 +01001131 iotlb_init_entry(&e, da, pa, omap_pgsz);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001132
Suman Anna9d5018d2017-09-05 17:56:18 -05001133 iommu = omap_domain->iommus;
1134 for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
1135 oiommu = iommu->iommu_dev;
1136 ret = omap_iopgtable_store_entry(oiommu, &e);
1137 if (ret) {
1138 dev_err(dev, "omap_iopgtable_store_entry failed: %d\n",
1139 ret);
1140 break;
1141 }
1142 }
1143
1144 if (ret) {
1145 while (i--) {
1146 iommu--;
1147 oiommu = iommu->iommu_dev;
1148 iopgtable_clear_entry(oiommu, da);
1149 }
1150 }
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001151
Ohad Ben-Cohenb4550d42011-09-02 13:32:31 -04001152 return ret;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001153}
1154
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001155static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
Suman Anna5835b6a2015-07-20 17:33:32 -05001156 size_t size)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001157{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001158 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Suman Anna9d5018d2017-09-05 17:56:18 -05001159 struct device *dev = omap_domain->dev;
1160 struct omap_iommu_device *iommu;
1161 struct omap_iommu *oiommu;
1162 bool error = false;
1163 size_t bytes = 0;
1164 int i;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001165
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001166 dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001167
Suman Anna9d5018d2017-09-05 17:56:18 -05001168 iommu = omap_domain->iommus;
1169 for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
1170 oiommu = iommu->iommu_dev;
1171 bytes = iopgtable_clear_entry(oiommu, da);
1172 if (!bytes)
1173 error = true;
1174 }
1175
1176 /*
1177 * simplify return - we are only checking if any of the iommus
1178 * reported an error, but not if all of them are unmapping the
1179 * same number of entries. This should not occur due to the
1180 * mirror programming.
1181 */
1182 return error ? 0 : bytes;
1183}
1184
1185static int omap_iommu_count(struct device *dev)
1186{
1187 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1188 int count = 0;
1189
1190 while (arch_data->iommu_dev) {
1191 count++;
1192 arch_data++;
1193 }
1194
1195 return count;
1196}
1197
1198/* caller should call cleanup if this function fails */
1199static int omap_iommu_attach_init(struct device *dev,
1200 struct omap_iommu_domain *odomain)
1201{
1202 struct omap_iommu_device *iommu;
1203 int i;
1204
1205 odomain->num_iommus = omap_iommu_count(dev);
1206 if (!odomain->num_iommus)
1207 return -EINVAL;
1208
1209 odomain->iommus = kcalloc(odomain->num_iommus, sizeof(*iommu),
1210 GFP_ATOMIC);
1211 if (!odomain->iommus)
1212 return -ENOMEM;
1213
1214 iommu = odomain->iommus;
1215 for (i = 0; i < odomain->num_iommus; i++, iommu++) {
1216 iommu->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_ATOMIC);
1217 if (!iommu->pgtable)
1218 return -ENOMEM;
1219
1220 /*
1221 * should never fail, but please keep this around to ensure
1222 * we keep the hardware happy
1223 */
1224 if (WARN_ON(!IS_ALIGNED((long)iommu->pgtable,
1225 IOPGD_TABLE_SIZE)))
1226 return -EINVAL;
1227 }
1228
1229 return 0;
1230}
1231
1232static void omap_iommu_detach_fini(struct omap_iommu_domain *odomain)
1233{
1234 int i;
1235 struct omap_iommu_device *iommu = odomain->iommus;
1236
1237 for (i = 0; iommu && i < odomain->num_iommus; i++, iommu++)
1238 kfree(iommu->pgtable);
1239
1240 kfree(odomain->iommus);
1241 odomain->num_iommus = 0;
1242 odomain->iommus = NULL;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001243}
1244
1245static int
1246omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
1247{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001248 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +02001249 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Suman Anna9d5018d2017-09-05 17:56:18 -05001250 struct omap_iommu_device *iommu;
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001251 struct omap_iommu *oiommu;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001252 int ret = 0;
Suman Anna9d5018d2017-09-05 17:56:18 -05001253 int i;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001254
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001255 if (!arch_data || !arch_data->iommu_dev) {
Suman Annae3f595b2014-09-04 17:27:29 -05001256 dev_err(dev, "device doesn't have an associated iommu\n");
1257 return -EINVAL;
1258 }
1259
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001260 spin_lock(&omap_domain->lock);
1261
Suman Anna0d364282017-09-05 17:56:17 -05001262 /* only a single client device can be attached to a domain */
1263 if (omap_domain->dev) {
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001264 dev_err(dev, "iommu domain is already attached\n");
1265 ret = -EBUSY;
1266 goto out;
1267 }
1268
Suman Anna9d5018d2017-09-05 17:56:18 -05001269 ret = omap_iommu_attach_init(dev, omap_domain);
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001270 if (ret) {
Suman Anna9d5018d2017-09-05 17:56:18 -05001271 dev_err(dev, "failed to allocate required iommu data %d\n",
1272 ret);
1273 goto init_fail;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001274 }
1275
Suman Anna9d5018d2017-09-05 17:56:18 -05001276 iommu = omap_domain->iommus;
1277 for (i = 0; i < omap_domain->num_iommus; i++, iommu++, arch_data++) {
1278 /* configure and enable the omap iommu */
1279 oiommu = arch_data->iommu_dev;
1280 ret = omap_iommu_attach(oiommu, iommu->pgtable);
1281 if (ret) {
1282 dev_err(dev, "can't get omap iommu: %d\n", ret);
1283 goto attach_fail;
1284 }
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001285
Suman Anna9d5018d2017-09-05 17:56:18 -05001286 oiommu->domain = domain;
1287 iommu->iommu_dev = oiommu;
1288 }
1289
1290 omap_domain->dev = dev;
1291
1292 goto out;
1293
1294attach_fail:
1295 while (i--) {
1296 iommu--;
1297 arch_data--;
1298 oiommu = iommu->iommu_dev;
1299 omap_iommu_detach(oiommu);
1300 iommu->iommu_dev = NULL;
1301 oiommu->domain = NULL;
1302 }
1303init_fail:
1304 omap_iommu_detach_fini(omap_domain);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001305out:
1306 spin_unlock(&omap_domain->lock);
1307 return ret;
1308}
1309
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001310static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001311 struct device *dev)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001312{
Suman Anna9d5018d2017-09-05 17:56:18 -05001313 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1314 struct omap_iommu_device *iommu = omap_domain->iommus;
1315 struct omap_iommu *oiommu;
1316 int i;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001317
Suman Anna0d364282017-09-05 17:56:17 -05001318 if (!omap_domain->dev) {
1319 dev_err(dev, "domain has no attached device\n");
1320 return;
1321 }
1322
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001323 /* only a single device is supported per domain for now */
Suman Anna0d364282017-09-05 17:56:17 -05001324 if (omap_domain->dev != dev) {
1325 dev_err(dev, "invalid attached device\n");
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001326 return;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001327 }
1328
Suman Anna9d5018d2017-09-05 17:56:18 -05001329 /*
1330 * cleanup in the reverse order of attachment - this addresses
1331 * any h/w dependencies between multiple instances, if any
1332 */
1333 iommu += (omap_domain->num_iommus - 1);
1334 arch_data += (omap_domain->num_iommus - 1);
1335 for (i = 0; i < omap_domain->num_iommus; i++, iommu--, arch_data--) {
1336 oiommu = iommu->iommu_dev;
1337 iopgtable_clear_entry_all(oiommu);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001338
Suman Anna9d5018d2017-09-05 17:56:18 -05001339 omap_iommu_detach(oiommu);
1340 iommu->iommu_dev = NULL;
1341 oiommu->domain = NULL;
1342 }
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001343
Suman Anna9d5018d2017-09-05 17:56:18 -05001344 omap_iommu_detach_fini(omap_domain);
1345
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001346 omap_domain->dev = NULL;
1347}
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001348
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001349static void omap_iommu_detach_dev(struct iommu_domain *domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001350 struct device *dev)
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001351{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001352 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001353
1354 spin_lock(&omap_domain->lock);
1355 _omap_iommu_detach_dev(omap_domain, dev);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001356 spin_unlock(&omap_domain->lock);
1357}
1358
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001359static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001360{
1361 struct omap_iommu_domain *omap_domain;
1362
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001363 if (type != IOMMU_DOMAIN_UNMANAGED)
1364 return NULL;
1365
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001366 omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
Suman Anna99ee98d2015-07-20 17:33:29 -05001367 if (!omap_domain)
Suman Anna9d5018d2017-09-05 17:56:18 -05001368 return NULL;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001369
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001370 spin_lock_init(&omap_domain->lock);
1371
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001372 omap_domain->domain.geometry.aperture_start = 0;
1373 omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
1374 omap_domain->domain.geometry.force_aperture = true;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001375
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001376 return &omap_domain->domain;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001377}
1378
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001379static void omap_iommu_domain_free(struct iommu_domain *domain)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001380{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001381 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001382
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001383 /*
1384 * An iommu device is still attached
1385 * (currently, only one device can be attached) ?
1386 */
Suman Anna0d364282017-09-05 17:56:17 -05001387 if (omap_domain->dev)
Omar Ramirez Luna803b5272012-04-18 13:09:41 -05001388 _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
1389
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001390 kfree(omap_domain);
1391}
1392
1393static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
Suman Anna5835b6a2015-07-20 17:33:32 -05001394 dma_addr_t da)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001395{
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001396 struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
Suman Anna9d5018d2017-09-05 17:56:18 -05001397 struct omap_iommu_device *iommu = omap_domain->iommus;
1398 struct omap_iommu *oiommu = iommu->iommu_dev;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001399 struct device *dev = oiommu->dev;
1400 u32 *pgd, *pte;
1401 phys_addr_t ret = 0;
1402
Suman Anna9d5018d2017-09-05 17:56:18 -05001403 /*
1404 * all the iommus within the domain will have identical programming,
1405 * so perform the lookup using just the first iommu
1406 */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001407 iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
1408
1409 if (pte) {
1410 if (iopte_is_small(*pte))
1411 ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
1412 else if (iopte_is_large(*pte))
1413 ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
1414 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001415 dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
Suman Anna5835b6a2015-07-20 17:33:32 -05001416 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001417 } else {
1418 if (iopgd_is_section(*pgd))
1419 ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
1420 else if (iopgd_is_super(*pgd))
1421 ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
1422 else
Suman Anna2abfcfb2013-05-30 18:10:38 -05001423 dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
Suman Anna5835b6a2015-07-20 17:33:32 -05001424 (unsigned long long)da);
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001425 }
1426
1427 return ret;
1428}
1429
Laurent Pinchart07a02032014-02-28 14:42:38 -06001430static int omap_iommu_add_device(struct device *dev)
1431{
Suman Anna9d5018d2017-09-05 17:56:18 -05001432 struct omap_iommu_arch_data *arch_data, *tmp;
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001433 struct omap_iommu *oiommu;
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001434 struct iommu_group *group;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001435 struct device_node *np;
Suman Anna7d682772014-09-04 17:27:30 -05001436 struct platform_device *pdev;
Suman Anna9d5018d2017-09-05 17:56:18 -05001437 int num_iommus, i;
Joerg Roedel01611fe2017-04-12 00:21:30 -05001438 int ret;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001439
1440 /*
1441 * Allocate the archdata iommu structure for DT-based devices.
1442 *
1443 * TODO: Simplify this when removing non-DT support completely from the
1444 * IOMMU users.
1445 */
1446 if (!dev->of_node)
1447 return 0;
1448
Suman Anna9d5018d2017-09-05 17:56:18 -05001449 /*
1450 * retrieve the count of IOMMU nodes using phandle size as element size
1451 * since #iommu-cells = 0 for OMAP
1452 */
1453 num_iommus = of_property_count_elems_of_size(dev->of_node, "iommus",
1454 sizeof(phandle));
1455 if (num_iommus < 0)
Laurent Pinchart07a02032014-02-28 14:42:38 -06001456 return 0;
1457
Kees Cook6396bb22018-06-12 14:03:40 -07001458 arch_data = kcalloc(num_iommus + 1, sizeof(*arch_data), GFP_KERNEL);
Suman Anna9d5018d2017-09-05 17:56:18 -05001459 if (!arch_data)
Laurent Pinchart07a02032014-02-28 14:42:38 -06001460 return -ENOMEM;
Suman Anna9d5018d2017-09-05 17:56:18 -05001461
1462 for (i = 0, tmp = arch_data; i < num_iommus; i++, tmp++) {
1463 np = of_parse_phandle(dev->of_node, "iommus", i);
1464 if (!np) {
1465 kfree(arch_data);
1466 return -EINVAL;
1467 }
1468
1469 pdev = of_find_device_by_node(np);
1470 if (WARN_ON(!pdev)) {
1471 of_node_put(np);
1472 kfree(arch_data);
1473 return -EINVAL;
1474 }
1475
1476 oiommu = platform_get_drvdata(pdev);
1477 if (!oiommu) {
1478 of_node_put(np);
1479 kfree(arch_data);
1480 return -EINVAL;
1481 }
1482
1483 tmp->iommu_dev = oiommu;
1484
1485 of_node_put(np);
Laurent Pinchart07a02032014-02-28 14:42:38 -06001486 }
1487
Suman Anna9d5018d2017-09-05 17:56:18 -05001488 /*
1489 * use the first IOMMU alone for the sysfs device linking.
1490 * TODO: Evaluate if a single iommu_group needs to be
1491 * maintained for both IOMMUs
1492 */
1493 oiommu = arch_data->iommu_dev;
Joerg Roedel01611fe2017-04-12 00:21:30 -05001494 ret = iommu_device_link(&oiommu->iommu, dev);
1495 if (ret) {
1496 kfree(arch_data);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001497 return ret;
1498 }
1499
Laurent Pinchart07a02032014-02-28 14:42:38 -06001500 dev->archdata.iommu = arch_data;
1501
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001502 /*
1503 * IOMMU group initialization calls into omap_iommu_device_group, which
1504 * needs a valid dev->archdata.iommu pointer
1505 */
1506 group = iommu_group_get_for_dev(dev);
1507 if (IS_ERR(group)) {
1508 iommu_device_unlink(&oiommu->iommu, dev);
1509 dev->archdata.iommu = NULL;
1510 kfree(arch_data);
1511 return PTR_ERR(group);
1512 }
1513 iommu_group_put(group);
1514
Laurent Pinchart07a02032014-02-28 14:42:38 -06001515 return 0;
1516}
1517
1518static void omap_iommu_remove_device(struct device *dev)
1519{
1520 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1521
1522 if (!dev->of_node || !arch_data)
1523 return;
1524
Joerg Roedel01611fe2017-04-12 00:21:30 -05001525 iommu_device_unlink(&arch_data->iommu_dev->iommu, dev);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001526 iommu_group_remove_device(dev);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001527
Joerg Roedelede1c2e2017-04-12 00:21:29 -05001528 dev->archdata.iommu = NULL;
Laurent Pinchart07a02032014-02-28 14:42:38 -06001529 kfree(arch_data);
Joerg Roedel01611fe2017-04-12 00:21:30 -05001530
Laurent Pinchart07a02032014-02-28 14:42:38 -06001531}
1532
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001533static struct iommu_group *omap_iommu_device_group(struct device *dev)
1534{
1535 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
Joerg Roedel8faf5e52017-06-28 12:50:16 +02001536 struct iommu_group *group = ERR_PTR(-EINVAL);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001537
1538 if (arch_data->iommu_dev)
Jeffy Chenb6d57f12018-03-01 19:22:08 +08001539 group = iommu_group_ref_get(arch_data->iommu_dev->group);
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001540
1541 return group;
1542}
1543
Thierry Redingb22f6432014-06-27 09:03:12 +02001544static const struct iommu_ops omap_iommu_ops = {
Joerg Roedel8cf851e2015-03-26 13:43:09 +01001545 .domain_alloc = omap_iommu_domain_alloc,
1546 .domain_free = omap_iommu_domain_free,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001547 .attach_dev = omap_iommu_attach_dev,
1548 .detach_dev = omap_iommu_detach_dev,
1549 .map = omap_iommu_map,
1550 .unmap = omap_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07001551 .map_sg = default_iommu_map_sg,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001552 .iova_to_phys = omap_iommu_iova_to_phys,
Laurent Pinchart07a02032014-02-28 14:42:38 -06001553 .add_device = omap_iommu_add_device,
1554 .remove_device = omap_iommu_remove_device,
Joerg Roedel28ae1e32017-04-12 00:21:31 -05001555 .device_group = omap_iommu_device_group,
Ohad Ben-Cohen66bc8cf2011-11-10 11:32:27 +02001556 .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +03001557};
1558
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001559static int __init omap_iommu_init(void)
1560{
1561 struct kmem_cache *p;
1562 const unsigned long flags = SLAB_HWCACHE_ALIGN;
1563 size_t align = 1 << 10; /* L2 pagetable alignement */
Thierry Redingf938aab2015-02-06 11:44:06 +01001564 struct device_node *np;
Suman Annaabaa7e52017-04-12 00:21:26 -05001565 int ret;
Thierry Redingf938aab2015-02-06 11:44:06 +01001566
1567 np = of_find_matching_node(NULL, omap_iommu_of_match);
1568 if (!np)
1569 return 0;
1570
1571 of_node_put(np);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001572
1573 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
Josue Albarranbfee0cf2017-07-28 15:49:14 -05001574 NULL);
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001575 if (!p)
1576 return -ENOMEM;
1577 iopte_cachep = p;
1578
Suman Anna61c75352014-10-22 17:22:30 -05001579 omap_iommu_debugfs_init();
1580
Suman Annaabaa7e52017-04-12 00:21:26 -05001581 ret = platform_driver_register(&omap_iommu_driver);
1582 if (ret) {
1583 pr_err("%s: failed to register driver\n", __func__);
1584 goto fail_driver;
1585 }
1586
1587 ret = bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
1588 if (ret)
1589 goto fail_bus;
1590
1591 return 0;
1592
1593fail_bus:
1594 platform_driver_unregister(&omap_iommu_driver);
1595fail_driver:
1596 kmem_cache_destroy(iopte_cachep);
1597 return ret;
Hiroshi DOYUa9dcad52009-01-26 15:13:40 +02001598}
Ohad Ben-Cohen435792d2012-02-26 12:14:14 +02001599subsys_initcall(omap_iommu_init);
Suman Anna0cdbf722015-07-20 17:33:24 -05001600/* must be ready before omap3isp is probed */