Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 1 | /* |
| 2 | * omap iommu: tlb and pagetable primitives |
| 3 | * |
Hiroshi DOYU | c127c7d | 2010-02-15 10:03:32 -0800 | [diff] [blame] | 4 | * Copyright (C) 2008-2010 Nokia Corporation |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 5 | * |
| 6 | * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>, |
| 7 | * Paul Mundt and Toshihiro Kobayashi |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/ioport.h> |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 19 | #include <linux/iommu.h> |
Tony Lindgren | c8d35c8 | 2012-11-02 12:24:03 -0700 | [diff] [blame] | 20 | #include <linux/omap-iommu.h> |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 21 | #include <linux/mutex.h> |
| 22 | #include <linux/spinlock.h> |
Tony Lindgren | ed1c7de | 2012-11-02 12:24:06 -0700 | [diff] [blame] | 23 | #include <linux/io.h> |
Omar Ramirez Luna | ebf7cda | 2012-11-19 19:05:51 -0600 | [diff] [blame] | 24 | #include <linux/pm_runtime.h> |
Florian Vaussard | 3c92748 | 2014-02-28 14:42:36 -0600 | [diff] [blame] | 25 | #include <linux/of.h> |
| 26 | #include <linux/of_iommu.h> |
| 27 | #include <linux/of_irq.h> |
Suman Anna | 7d68277 | 2014-09-04 17:27:30 -0500 | [diff] [blame] | 28 | #include <linux/of_platform.h> |
Suman Anna | 3ca9299 | 2015-10-02 18:02:44 -0500 | [diff] [blame] | 29 | #include <linux/regmap.h> |
| 30 | #include <linux/mfd/syscon.h> |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 31 | |
| 32 | #include <asm/cacheflush.h> |
| 33 | |
Tony Lindgren | 2ab7c84 | 2012-11-02 12:24:14 -0700 | [diff] [blame] | 34 | #include <linux/platform_data/iommu-omap.h> |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 35 | |
Ido Yariv | 2f7702a | 2012-11-02 12:24:00 -0700 | [diff] [blame] | 36 | #include "omap-iopgtable.h" |
Tony Lindgren | ed1c7de | 2012-11-02 12:24:06 -0700 | [diff] [blame] | 37 | #include "omap-iommu.h" |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 38 | |
Joerg Roedel | 01611fe | 2017-04-12 00:21:30 -0500 | [diff] [blame] | 39 | static const struct iommu_ops omap_iommu_ops; |
| 40 | |
Suman Anna | 5acc97d | 2014-03-17 20:31:34 -0500 | [diff] [blame] | 41 | #define to_iommu(dev) \ |
| 42 | ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev))) |
| 43 | |
Ohad Ben-Cohen | 66bc8cf | 2011-11-10 11:32:27 +0200 | [diff] [blame] | 44 | /* bitmap of the page sizes currently supported */ |
| 45 | #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M) |
| 46 | |
Ido Yariv | 7bd9e25 | 2012-11-02 12:24:09 -0700 | [diff] [blame] | 47 | #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 DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 57 | static struct platform_driver omap_iommu_driver; |
| 58 | static struct kmem_cache *iopte_cachep; |
| 59 | |
| 60 | /** |
Joerg Roedel | 8cf851e | 2015-03-26 13:43:09 +0100 | [diff] [blame] | 61 | * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain |
| 62 | * @dom: generic iommu domain handle |
| 63 | **/ |
| 64 | static 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-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 70 | * omap_iommu_save_ctx - Save registers for pm off-mode support |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 71 | * @dev: client device |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 72 | **/ |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 73 | void omap_iommu_save_ctx(struct device *dev) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 74 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 75 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
Suman Anna | bd4396f | 2014-10-22 17:22:27 -0500 | [diff] [blame] | 76 | u32 *p = obj->ctx; |
| 77 | int i; |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 78 | |
Suman Anna | bd4396f | 2014-10-22 17:22:27 -0500 | [diff] [blame] | 79 | for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) { |
| 80 | p[i] = iommu_read_reg(obj, i * sizeof(u32)); |
| 81 | dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]); |
| 82 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 83 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 84 | EXPORT_SYMBOL_GPL(omap_iommu_save_ctx); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 85 | |
| 86 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 87 | * omap_iommu_restore_ctx - Restore registers for pm off-mode support |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 88 | * @dev: client device |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 89 | **/ |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 90 | void omap_iommu_restore_ctx(struct device *dev) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 91 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 92 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
Suman Anna | bd4396f | 2014-10-22 17:22:27 -0500 | [diff] [blame] | 93 | u32 *p = obj->ctx; |
| 94 | int i; |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 95 | |
Suman Anna | bd4396f | 2014-10-22 17:22:27 -0500 | [diff] [blame] | 96 | for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) { |
| 97 | iommu_write_reg(obj, p[i], i * sizeof(u32)); |
| 98 | dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]); |
| 99 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 100 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 101 | EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 102 | |
Suman Anna | 3ca9299 | 2015-10-02 18:02:44 -0500 | [diff] [blame] | 103 | static void dra7_cfg_dspsys_mmu(struct omap_iommu *obj, bool enable) |
| 104 | { |
| 105 | u32 val, mask; |
| 106 | |
| 107 | if (!obj->syscfg) |
| 108 | return; |
| 109 | |
| 110 | mask = (1 << (obj->id * DSP_SYS_MMU_CONFIG_EN_SHIFT)); |
| 111 | val = enable ? mask : 0; |
| 112 | regmap_update_bits(obj->syscfg, DSP_SYS_MMU_CONFIG, mask, val); |
| 113 | } |
| 114 | |
Suman Anna | bd4396f | 2014-10-22 17:22:27 -0500 | [diff] [blame] | 115 | static void __iommu_set_twl(struct omap_iommu *obj, bool on) |
| 116 | { |
| 117 | u32 l = iommu_read_reg(obj, MMU_CNTL); |
| 118 | |
| 119 | if (on) |
| 120 | iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE); |
| 121 | else |
| 122 | iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE); |
| 123 | |
| 124 | l &= ~MMU_CNTL_MASK; |
| 125 | if (on) |
| 126 | l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN); |
| 127 | else |
| 128 | l |= (MMU_CNTL_MMU_EN); |
| 129 | |
| 130 | iommu_write_reg(obj, l, MMU_CNTL); |
| 131 | } |
| 132 | |
| 133 | static int omap2_iommu_enable(struct omap_iommu *obj) |
| 134 | { |
| 135 | u32 l, pa; |
| 136 | |
| 137 | if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K)) |
| 138 | return -EINVAL; |
| 139 | |
| 140 | pa = virt_to_phys(obj->iopgd); |
| 141 | if (!IS_ALIGNED(pa, SZ_16K)) |
| 142 | return -EINVAL; |
| 143 | |
| 144 | l = iommu_read_reg(obj, MMU_REVISION); |
| 145 | dev_info(obj->dev, "%s: version %d.%d\n", obj->name, |
| 146 | (l >> 4) & 0xf, l & 0xf); |
| 147 | |
| 148 | iommu_write_reg(obj, pa, MMU_TTB); |
| 149 | |
Suman Anna | 3ca9299 | 2015-10-02 18:02:44 -0500 | [diff] [blame] | 150 | dra7_cfg_dspsys_mmu(obj, true); |
| 151 | |
Suman Anna | bd4396f | 2014-10-22 17:22:27 -0500 | [diff] [blame] | 152 | if (obj->has_bus_err_back) |
| 153 | iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG); |
| 154 | |
| 155 | __iommu_set_twl(obj, true); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static void omap2_iommu_disable(struct omap_iommu *obj) |
| 161 | { |
| 162 | u32 l = iommu_read_reg(obj, MMU_CNTL); |
| 163 | |
| 164 | l &= ~MMU_CNTL_MASK; |
| 165 | iommu_write_reg(obj, l, MMU_CNTL); |
Suman Anna | 3ca9299 | 2015-10-02 18:02:44 -0500 | [diff] [blame] | 166 | dra7_cfg_dspsys_mmu(obj, false); |
Suman Anna | bd4396f | 2014-10-22 17:22:27 -0500 | [diff] [blame] | 167 | |
| 168 | dev_dbg(obj->dev, "%s is shutting down\n", obj->name); |
| 169 | } |
| 170 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 171 | static int iommu_enable(struct omap_iommu *obj) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 172 | { |
| 173 | int err; |
Omar Ramirez Luna | 72b15b6 | 2012-11-19 19:05:50 -0600 | [diff] [blame] | 174 | struct platform_device *pdev = to_platform_device(obj->dev); |
Kiran Padwal | 99cb9ae | 2014-10-30 11:59:47 +0530 | [diff] [blame] | 175 | struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 176 | |
Florian Vaussard | 90e569c | 2014-02-28 14:42:34 -0600 | [diff] [blame] | 177 | if (pdata && pdata->deassert_reset) { |
Omar Ramirez Luna | 72b15b6 | 2012-11-19 19:05:50 -0600 | [diff] [blame] | 178 | err = pdata->deassert_reset(pdev, pdata->reset_name); |
| 179 | if (err) { |
| 180 | dev_err(obj->dev, "deassert_reset failed: %d\n", err); |
| 181 | return err; |
| 182 | } |
| 183 | } |
| 184 | |
Omar Ramirez Luna | ebf7cda | 2012-11-19 19:05:51 -0600 | [diff] [blame] | 185 | pm_runtime_get_sync(obj->dev); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 186 | |
Suman Anna | bd4396f | 2014-10-22 17:22:27 -0500 | [diff] [blame] | 187 | err = omap2_iommu_enable(obj); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 188 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 189 | return err; |
| 190 | } |
| 191 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 192 | static void iommu_disable(struct omap_iommu *obj) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 193 | { |
Omar Ramirez Luna | 72b15b6 | 2012-11-19 19:05:50 -0600 | [diff] [blame] | 194 | struct platform_device *pdev = to_platform_device(obj->dev); |
Kiran Padwal | 99cb9ae | 2014-10-30 11:59:47 +0530 | [diff] [blame] | 195 | struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Omar Ramirez Luna | 72b15b6 | 2012-11-19 19:05:50 -0600 | [diff] [blame] | 196 | |
Suman Anna | bd4396f | 2014-10-22 17:22:27 -0500 | [diff] [blame] | 197 | omap2_iommu_disable(obj); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 198 | |
Omar Ramirez Luna | ebf7cda | 2012-11-19 19:05:51 -0600 | [diff] [blame] | 199 | pm_runtime_put_sync(obj->dev); |
Omar Ramirez Luna | 72b15b6 | 2012-11-19 19:05:50 -0600 | [diff] [blame] | 200 | |
Florian Vaussard | 90e569c | 2014-02-28 14:42:34 -0600 | [diff] [blame] | 201 | if (pdata && pdata->assert_reset) |
Omar Ramirez Luna | 72b15b6 | 2012-11-19 19:05:50 -0600 | [diff] [blame] | 202 | pdata->assert_reset(pdev, pdata->reset_name); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* |
| 206 | * TLB operations |
| 207 | */ |
Ohad Ben-Cohen | e1f2381 | 2011-08-16 14:58:14 +0300 | [diff] [blame] | 208 | static u32 iotlb_cr_to_virt(struct cr_regs *cr) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 209 | { |
Suman Anna | bd4396f | 2014-10-22 17:22:27 -0500 | [diff] [blame] | 210 | u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK; |
| 211 | u32 mask = get_cam_va_mask(cr->cam & page_size); |
| 212 | |
| 213 | return cr->cam & mask; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 214 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 215 | |
| 216 | static u32 get_iopte_attr(struct iotlb_entry *e) |
| 217 | { |
Suman Anna | bd4396f | 2014-10-22 17:22:27 -0500 | [diff] [blame] | 218 | u32 attr; |
| 219 | |
| 220 | attr = e->mixed << 5; |
| 221 | attr |= e->endian; |
| 222 | attr |= e->elsz >> 3; |
| 223 | attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) || |
| 224 | (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6); |
| 225 | return attr; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 228 | static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 229 | { |
Suman Anna | bd4396f | 2014-10-22 17:22:27 -0500 | [diff] [blame] | 230 | u32 status, fault_addr; |
| 231 | |
| 232 | status = iommu_read_reg(obj, MMU_IRQSTATUS); |
| 233 | status &= MMU_IRQ_MASK; |
| 234 | if (!status) { |
| 235 | *da = 0; |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | fault_addr = iommu_read_reg(obj, MMU_FAULT_AD); |
| 240 | *da = fault_addr; |
| 241 | |
| 242 | iommu_write_reg(obj, status, MMU_IRQSTATUS); |
| 243 | |
| 244 | return status; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Suman Anna | 69c2c19 | 2015-07-20 17:33:25 -0500 | [diff] [blame] | 247 | void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 248 | { |
| 249 | u32 val; |
| 250 | |
| 251 | val = iommu_read_reg(obj, MMU_LOCK); |
| 252 | |
| 253 | l->base = MMU_LOCK_BASE(val); |
| 254 | l->vict = MMU_LOCK_VICT(val); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Suman Anna | 69c2c19 | 2015-07-20 17:33:25 -0500 | [diff] [blame] | 257 | void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 258 | { |
| 259 | u32 val; |
| 260 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 261 | val = (l->base << MMU_LOCK_BASE_SHIFT); |
| 262 | val |= (l->vict << MMU_LOCK_VICT_SHIFT); |
| 263 | |
| 264 | iommu_write_reg(obj, val, MMU_LOCK); |
| 265 | } |
| 266 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 267 | static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 268 | { |
Suman Anna | bd4396f | 2014-10-22 17:22:27 -0500 | [diff] [blame] | 269 | cr->cam = iommu_read_reg(obj, MMU_READ_CAM); |
| 270 | cr->ram = iommu_read_reg(obj, MMU_READ_RAM); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 273 | static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 274 | { |
Suman Anna | bd4396f | 2014-10-22 17:22:27 -0500 | [diff] [blame] | 275 | iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM); |
| 276 | iommu_write_reg(obj, cr->ram, MMU_RAM); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 277 | |
| 278 | iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY); |
| 279 | iommu_write_reg(obj, 1, MMU_LD_TLB); |
| 280 | } |
| 281 | |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 282 | /* only used in iotlb iteration for-loop */ |
Suman Anna | 69c2c19 | 2015-07-20 17:33:25 -0500 | [diff] [blame] | 283 | struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n) |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 284 | { |
| 285 | struct cr_regs cr; |
| 286 | struct iotlb_lock l; |
| 287 | |
| 288 | iotlb_lock_get(obj, &l); |
| 289 | l.vict = n; |
| 290 | iotlb_lock_set(obj, &l); |
| 291 | iotlb_read_cr(obj, &cr); |
| 292 | |
| 293 | return cr; |
| 294 | } |
| 295 | |
Suman Anna | bd4396f | 2014-10-22 17:22:27 -0500 | [diff] [blame] | 296 | #ifdef PREFETCH_IOTLB |
| 297 | static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj, |
| 298 | struct iotlb_entry *e) |
| 299 | { |
| 300 | struct cr_regs *cr; |
| 301 | |
| 302 | if (!e) |
| 303 | return NULL; |
| 304 | |
| 305 | if (e->da & ~(get_cam_va_mask(e->pgsz))) { |
| 306 | dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__, |
| 307 | e->da); |
| 308 | return ERR_PTR(-EINVAL); |
| 309 | } |
| 310 | |
| 311 | cr = kmalloc(sizeof(*cr), GFP_KERNEL); |
| 312 | if (!cr) |
| 313 | return ERR_PTR(-ENOMEM); |
| 314 | |
| 315 | cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid; |
| 316 | cr->ram = e->pa | e->endian | e->elsz | e->mixed; |
| 317 | |
| 318 | return cr; |
| 319 | } |
| 320 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 321 | /** |
| 322 | * load_iotlb_entry - Set an iommu tlb entry |
| 323 | * @obj: target iommu |
| 324 | * @e: an iommu tlb entry info |
| 325 | **/ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 326 | static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 327 | { |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 328 | int err = 0; |
| 329 | struct iotlb_lock l; |
| 330 | struct cr_regs *cr; |
| 331 | |
| 332 | if (!obj || !obj->nr_tlb_entries || !e) |
| 333 | return -EINVAL; |
| 334 | |
Omar Ramirez Luna | ebf7cda | 2012-11-19 19:05:51 -0600 | [diff] [blame] | 335 | pm_runtime_get_sync(obj->dev); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 336 | |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 337 | iotlb_lock_get(obj, &l); |
| 338 | if (l.base == obj->nr_tlb_entries) { |
| 339 | dev_warn(obj->dev, "%s: preserve entries full\n", __func__); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 340 | err = -EBUSY; |
| 341 | goto out; |
| 342 | } |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 343 | if (!e->prsvd) { |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 344 | int i; |
| 345 | struct cr_regs tmp; |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 346 | |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 347 | for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp) |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 348 | if (!iotlb_cr_valid(&tmp)) |
| 349 | break; |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 350 | |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 351 | if (i == obj->nr_tlb_entries) { |
| 352 | dev_dbg(obj->dev, "%s: full: no entry\n", __func__); |
| 353 | err = -EBUSY; |
| 354 | goto out; |
| 355 | } |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 356 | |
| 357 | iotlb_lock_get(obj, &l); |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 358 | } else { |
| 359 | l.vict = l.base; |
| 360 | iotlb_lock_set(obj, &l); |
| 361 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 362 | |
| 363 | cr = iotlb_alloc_cr(obj, e); |
| 364 | if (IS_ERR(cr)) { |
Omar Ramirez Luna | ebf7cda | 2012-11-19 19:05:51 -0600 | [diff] [blame] | 365 | pm_runtime_put_sync(obj->dev); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 366 | return PTR_ERR(cr); |
| 367 | } |
| 368 | |
| 369 | iotlb_load_cr(obj, cr); |
| 370 | kfree(cr); |
| 371 | |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 372 | if (e->prsvd) |
| 373 | l.base++; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 374 | /* increment victim for next tlb load */ |
| 375 | if (++l.vict == obj->nr_tlb_entries) |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 376 | l.vict = l.base; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 377 | iotlb_lock_set(obj, &l); |
| 378 | out: |
Omar Ramirez Luna | ebf7cda | 2012-11-19 19:05:51 -0600 | [diff] [blame] | 379 | pm_runtime_put_sync(obj->dev); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 380 | return err; |
| 381 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 382 | |
Ohad Ben-Cohen | 5da14a4 | 2011-08-16 15:19:10 +0300 | [diff] [blame] | 383 | #else /* !PREFETCH_IOTLB */ |
| 384 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 385 | static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e) |
Ohad Ben-Cohen | 5da14a4 | 2011-08-16 15:19:10 +0300 | [diff] [blame] | 386 | { |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | #endif /* !PREFETCH_IOTLB */ |
| 391 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 392 | static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e) |
Ohad Ben-Cohen | 5da14a4 | 2011-08-16 15:19:10 +0300 | [diff] [blame] | 393 | { |
| 394 | return load_iotlb_entry(obj, e); |
| 395 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 396 | |
| 397 | /** |
| 398 | * flush_iotlb_page - Clear an iommu tlb entry |
| 399 | * @obj: target iommu |
| 400 | * @da: iommu device virtual address |
| 401 | * |
| 402 | * Clear an iommu tlb entry which includes 'da' address. |
| 403 | **/ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 404 | static void flush_iotlb_page(struct omap_iommu *obj, u32 da) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 405 | { |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 406 | int i; |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 407 | struct cr_regs cr; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 408 | |
Omar Ramirez Luna | ebf7cda | 2012-11-19 19:05:51 -0600 | [diff] [blame] | 409 | pm_runtime_get_sync(obj->dev); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 410 | |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 411 | for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) { |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 412 | u32 start; |
| 413 | size_t bytes; |
| 414 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 415 | if (!iotlb_cr_valid(&cr)) |
| 416 | continue; |
| 417 | |
| 418 | start = iotlb_cr_to_virt(&cr); |
| 419 | bytes = iopgsz_to_bytes(cr.cam & 3); |
| 420 | |
| 421 | if ((start <= da) && (da < start + bytes)) { |
| 422 | dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n", |
| 423 | __func__, start, da, bytes); |
Hari Kanigeri | 0fa035e | 2010-08-20 13:50:18 +0000 | [diff] [blame] | 424 | iotlb_load_cr(obj, &cr); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 425 | iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY); |
Laurent Pinchart | f7129a0 | 2014-03-07 23:47:03 +0100 | [diff] [blame] | 426 | break; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 427 | } |
| 428 | } |
Omar Ramirez Luna | ebf7cda | 2012-11-19 19:05:51 -0600 | [diff] [blame] | 429 | pm_runtime_put_sync(obj->dev); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 430 | |
| 431 | if (i == obj->nr_tlb_entries) |
| 432 | dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da); |
| 433 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 434 | |
| 435 | /** |
| 436 | * flush_iotlb_all - Clear all iommu tlb entries |
| 437 | * @obj: target iommu |
| 438 | **/ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 439 | static void flush_iotlb_all(struct omap_iommu *obj) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 440 | { |
| 441 | struct iotlb_lock l; |
| 442 | |
Omar Ramirez Luna | ebf7cda | 2012-11-19 19:05:51 -0600 | [diff] [blame] | 443 | pm_runtime_get_sync(obj->dev); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 444 | |
| 445 | l.base = 0; |
| 446 | l.vict = 0; |
| 447 | iotlb_lock_set(obj, &l); |
| 448 | |
| 449 | iommu_write_reg(obj, 1, MMU_GFLUSH); |
| 450 | |
Omar Ramirez Luna | ebf7cda | 2012-11-19 19:05:51 -0600 | [diff] [blame] | 451 | pm_runtime_put_sync(obj->dev); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 452 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 453 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 454 | /* |
| 455 | * H/W pagetable operations |
| 456 | */ |
| 457 | static void flush_iopgd_range(u32 *first, u32 *last) |
| 458 | { |
| 459 | /* FIXME: L2 cache should be taken care of if it exists */ |
| 460 | do { |
| 461 | asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd" |
| 462 | : : "r" (first)); |
| 463 | first += L1_CACHE_BYTES / sizeof(*first); |
| 464 | } while (first <= last); |
| 465 | } |
| 466 | |
| 467 | static void flush_iopte_range(u32 *first, u32 *last) |
| 468 | { |
| 469 | /* FIXME: L2 cache should be taken care of if it exists */ |
| 470 | do { |
| 471 | asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte" |
| 472 | : : "r" (first)); |
| 473 | first += L1_CACHE_BYTES / sizeof(*first); |
| 474 | } while (first <= last); |
| 475 | } |
| 476 | |
| 477 | static void iopte_free(u32 *iopte) |
| 478 | { |
| 479 | /* Note: freed iopte's must be clean ready for re-use */ |
Zhouyi Zhou | e28045a | 2014-03-05 18:20:19 +0800 | [diff] [blame] | 480 | if (iopte) |
| 481 | kmem_cache_free(iopte_cachep, iopte); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 482 | } |
| 483 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 484 | static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 485 | { |
| 486 | u32 *iopte; |
| 487 | |
| 488 | /* a table has already existed */ |
| 489 | if (*iopgd) |
| 490 | goto pte_ready; |
| 491 | |
| 492 | /* |
| 493 | * do the allocation outside the page table lock |
| 494 | */ |
| 495 | spin_unlock(&obj->page_table_lock); |
| 496 | iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL); |
| 497 | spin_lock(&obj->page_table_lock); |
| 498 | |
| 499 | if (!*iopgd) { |
| 500 | if (!iopte) |
| 501 | return ERR_PTR(-ENOMEM); |
| 502 | |
| 503 | *iopgd = virt_to_phys(iopte) | IOPGD_TABLE; |
| 504 | flush_iopgd_range(iopgd, iopgd); |
| 505 | |
| 506 | dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte); |
| 507 | } else { |
| 508 | /* We raced, free the reduniovant table */ |
| 509 | iopte_free(iopte); |
| 510 | } |
| 511 | |
| 512 | pte_ready: |
| 513 | iopte = iopte_offset(iopgd, da); |
| 514 | |
| 515 | dev_vdbg(obj->dev, |
| 516 | "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n", |
| 517 | __func__, da, iopgd, *iopgd, iopte, *iopte); |
| 518 | |
| 519 | return iopte; |
| 520 | } |
| 521 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 522 | static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 523 | { |
| 524 | u32 *iopgd = iopgd_offset(obj, da); |
| 525 | |
Hiroshi DOYU | 4abb761 | 2010-05-06 18:24:04 +0300 | [diff] [blame] | 526 | if ((da | pa) & ~IOSECTION_MASK) { |
| 527 | dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n", |
| 528 | __func__, da, pa, IOSECTION_SIZE); |
| 529 | return -EINVAL; |
| 530 | } |
| 531 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 532 | *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION; |
| 533 | flush_iopgd_range(iopgd, iopgd); |
| 534 | return 0; |
| 535 | } |
| 536 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 537 | static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 538 | { |
| 539 | u32 *iopgd = iopgd_offset(obj, da); |
| 540 | int i; |
| 541 | |
Hiroshi DOYU | 4abb761 | 2010-05-06 18:24:04 +0300 | [diff] [blame] | 542 | if ((da | pa) & ~IOSUPER_MASK) { |
| 543 | dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n", |
| 544 | __func__, da, pa, IOSUPER_SIZE); |
| 545 | return -EINVAL; |
| 546 | } |
| 547 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 548 | for (i = 0; i < 16; i++) |
| 549 | *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER; |
| 550 | flush_iopgd_range(iopgd, iopgd + 15); |
| 551 | return 0; |
| 552 | } |
| 553 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 554 | static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 555 | { |
| 556 | u32 *iopgd = iopgd_offset(obj, da); |
| 557 | u32 *iopte = iopte_alloc(obj, iopgd, da); |
| 558 | |
| 559 | if (IS_ERR(iopte)) |
| 560 | return PTR_ERR(iopte); |
| 561 | |
| 562 | *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL; |
| 563 | flush_iopte_range(iopte, iopte); |
| 564 | |
| 565 | dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n", |
| 566 | __func__, da, pa, iopte, *iopte); |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 571 | static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 572 | { |
| 573 | u32 *iopgd = iopgd_offset(obj, da); |
| 574 | u32 *iopte = iopte_alloc(obj, iopgd, da); |
| 575 | int i; |
| 576 | |
Hiroshi DOYU | 4abb761 | 2010-05-06 18:24:04 +0300 | [diff] [blame] | 577 | if ((da | pa) & ~IOLARGE_MASK) { |
| 578 | dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n", |
| 579 | __func__, da, pa, IOLARGE_SIZE); |
| 580 | return -EINVAL; |
| 581 | } |
| 582 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 583 | if (IS_ERR(iopte)) |
| 584 | return PTR_ERR(iopte); |
| 585 | |
| 586 | for (i = 0; i < 16; i++) |
| 587 | *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE; |
| 588 | flush_iopte_range(iopte, iopte + 15); |
| 589 | return 0; |
| 590 | } |
| 591 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 592 | static int |
| 593 | iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 594 | { |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 595 | int (*fn)(struct omap_iommu *, u32, u32, u32); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 596 | u32 prot; |
| 597 | int err; |
| 598 | |
| 599 | if (!obj || !e) |
| 600 | return -EINVAL; |
| 601 | |
| 602 | switch (e->pgsz) { |
| 603 | case MMU_CAM_PGSZ_16M: |
| 604 | fn = iopgd_alloc_super; |
| 605 | break; |
| 606 | case MMU_CAM_PGSZ_1M: |
| 607 | fn = iopgd_alloc_section; |
| 608 | break; |
| 609 | case MMU_CAM_PGSZ_64K: |
| 610 | fn = iopte_alloc_large; |
| 611 | break; |
| 612 | case MMU_CAM_PGSZ_4K: |
| 613 | fn = iopte_alloc_page; |
| 614 | break; |
| 615 | default: |
| 616 | fn = NULL; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 617 | break; |
| 618 | } |
| 619 | |
Suman Anna | 7c1ab60 | 2016-04-04 17:46:19 -0500 | [diff] [blame] | 620 | if (WARN_ON(!fn)) |
| 621 | return -EINVAL; |
| 622 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 623 | prot = get_iopte_attr(e); |
| 624 | |
| 625 | spin_lock(&obj->page_table_lock); |
| 626 | err = fn(obj, e->da, e->pa, prot); |
| 627 | spin_unlock(&obj->page_table_lock); |
| 628 | |
| 629 | return err; |
| 630 | } |
| 631 | |
| 632 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 633 | * omap_iopgtable_store_entry - Make an iommu pte entry |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 634 | * @obj: target iommu |
| 635 | * @e: an iommu tlb entry info |
| 636 | **/ |
Suman Anna | 4899a56 | 2014-10-22 17:22:32 -0500 | [diff] [blame] | 637 | static int |
| 638 | omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 639 | { |
| 640 | int err; |
| 641 | |
| 642 | flush_iotlb_page(obj, e->da); |
| 643 | err = iopgtable_store_entry_core(obj, e); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 644 | if (!err) |
Ohad Ben-Cohen | 5da14a4 | 2011-08-16 15:19:10 +0300 | [diff] [blame] | 645 | prefetch_iotlb_entry(obj, e); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 646 | return err; |
| 647 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 648 | |
| 649 | /** |
| 650 | * iopgtable_lookup_entry - Lookup an iommu pte entry |
| 651 | * @obj: target iommu |
| 652 | * @da: iommu device virtual address |
| 653 | * @ppgd: iommu pgd entry pointer to be returned |
| 654 | * @ppte: iommu pte entry pointer to be returned |
| 655 | **/ |
Ohad Ben-Cohen | e1f2381 | 2011-08-16 14:58:14 +0300 | [diff] [blame] | 656 | static void |
| 657 | iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 658 | { |
| 659 | u32 *iopgd, *iopte = NULL; |
| 660 | |
| 661 | iopgd = iopgd_offset(obj, da); |
| 662 | if (!*iopgd) |
| 663 | goto out; |
| 664 | |
Hiroshi DOYU | a1a5445 | 2010-05-13 09:45:35 +0300 | [diff] [blame] | 665 | if (iopgd_is_table(*iopgd)) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 666 | iopte = iopte_offset(iopgd, da); |
| 667 | out: |
| 668 | *ppgd = iopgd; |
| 669 | *ppte = iopte; |
| 670 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 671 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 672 | static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 673 | { |
| 674 | size_t bytes; |
| 675 | u32 *iopgd = iopgd_offset(obj, da); |
| 676 | int nent = 1; |
| 677 | |
| 678 | if (!*iopgd) |
| 679 | return 0; |
| 680 | |
Hiroshi DOYU | a1a5445 | 2010-05-13 09:45:35 +0300 | [diff] [blame] | 681 | if (iopgd_is_table(*iopgd)) { |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 682 | int i; |
| 683 | u32 *iopte = iopte_offset(iopgd, da); |
| 684 | |
| 685 | bytes = IOPTE_SIZE; |
| 686 | if (*iopte & IOPTE_LARGE) { |
| 687 | nent *= 16; |
| 688 | /* rewind to the 1st entry */ |
Hiroshi DOYU | c127c7d | 2010-02-15 10:03:32 -0800 | [diff] [blame] | 689 | iopte = iopte_offset(iopgd, (da & IOLARGE_MASK)); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 690 | } |
| 691 | bytes *= nent; |
| 692 | memset(iopte, 0, nent * sizeof(*iopte)); |
| 693 | flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte)); |
| 694 | |
| 695 | /* |
| 696 | * do table walk to check if this table is necessary or not |
| 697 | */ |
| 698 | iopte = iopte_offset(iopgd, 0); |
| 699 | for (i = 0; i < PTRS_PER_IOPTE; i++) |
| 700 | if (iopte[i]) |
| 701 | goto out; |
| 702 | |
| 703 | iopte_free(iopte); |
| 704 | nent = 1; /* for the next L1 entry */ |
| 705 | } else { |
| 706 | bytes = IOPGD_SIZE; |
Hiroshi DOYU | dcc730d | 2009-10-22 14:46:32 -0700 | [diff] [blame] | 707 | if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) { |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 708 | nent *= 16; |
| 709 | /* rewind to the 1st entry */ |
Hiroshi DOYU | 8d33ea5 | 2010-02-15 10:03:32 -0800 | [diff] [blame] | 710 | iopgd = iopgd_offset(obj, (da & IOSUPER_MASK)); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 711 | } |
| 712 | bytes *= nent; |
| 713 | } |
| 714 | memset(iopgd, 0, nent * sizeof(*iopgd)); |
| 715 | flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd)); |
| 716 | out: |
| 717 | return bytes; |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * iopgtable_clear_entry - Remove an iommu pte entry |
| 722 | * @obj: target iommu |
| 723 | * @da: iommu device virtual address |
| 724 | **/ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 725 | static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 726 | { |
| 727 | size_t bytes; |
| 728 | |
| 729 | spin_lock(&obj->page_table_lock); |
| 730 | |
| 731 | bytes = iopgtable_clear_entry_core(obj, da); |
| 732 | flush_iotlb_page(obj, da); |
| 733 | |
| 734 | spin_unlock(&obj->page_table_lock); |
| 735 | |
| 736 | return bytes; |
| 737 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 738 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 739 | static void iopgtable_clear_entry_all(struct omap_iommu *obj) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 740 | { |
| 741 | int i; |
| 742 | |
| 743 | spin_lock(&obj->page_table_lock); |
| 744 | |
| 745 | for (i = 0; i < PTRS_PER_IOPGD; i++) { |
| 746 | u32 da; |
| 747 | u32 *iopgd; |
| 748 | |
| 749 | da = i << IOPGD_SHIFT; |
| 750 | iopgd = iopgd_offset(obj, da); |
| 751 | |
| 752 | if (!*iopgd) |
| 753 | continue; |
| 754 | |
Hiroshi DOYU | a1a5445 | 2010-05-13 09:45:35 +0300 | [diff] [blame] | 755 | if (iopgd_is_table(*iopgd)) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 756 | iopte_free(iopte_offset(iopgd, 0)); |
| 757 | |
| 758 | *iopgd = 0; |
| 759 | flush_iopgd_range(iopgd, iopgd); |
| 760 | } |
| 761 | |
| 762 | flush_iotlb_all(obj); |
| 763 | |
| 764 | spin_unlock(&obj->page_table_lock); |
| 765 | } |
| 766 | |
| 767 | /* |
| 768 | * Device IOMMU generic operations |
| 769 | */ |
| 770 | static irqreturn_t iommu_fault_handler(int irq, void *data) |
| 771 | { |
David Cohen | d594f1f | 2011-02-16 19:35:51 +0000 | [diff] [blame] | 772 | u32 da, errs; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 773 | u32 *iopgd, *iopte; |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 774 | struct omap_iommu *obj = data; |
Ohad Ben-Cohen | e7f10f0 | 2011-09-13 15:26:29 -0400 | [diff] [blame] | 775 | struct iommu_domain *domain = obj->domain; |
Joerg Roedel | 8cf851e | 2015-03-26 13:43:09 +0100 | [diff] [blame] | 776 | struct omap_iommu_domain *omap_domain = to_omap_domain(domain); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 777 | |
Suman Anna | 2088ecb | 2014-10-22 17:22:19 -0500 | [diff] [blame] | 778 | if (!omap_domain->iommu_dev) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 779 | return IRQ_NONE; |
| 780 | |
David Cohen | d594f1f | 2011-02-16 19:35:51 +0000 | [diff] [blame] | 781 | errs = iommu_report_fault(obj, &da); |
Laurent Pinchart | c56b2dd | 2011-05-10 16:56:46 +0200 | [diff] [blame] | 782 | if (errs == 0) |
| 783 | return IRQ_HANDLED; |
David Cohen | d594f1f | 2011-02-16 19:35:51 +0000 | [diff] [blame] | 784 | |
| 785 | /* Fault callback or TLB/PTE Dynamic loading */ |
Ohad Ben-Cohen | e7f10f0 | 2011-09-13 15:26:29 -0400 | [diff] [blame] | 786 | if (!report_iommu_fault(domain, obj->dev, da, 0)) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 787 | return IRQ_HANDLED; |
| 788 | |
Hiroshi DOYU | 37b2981 | 2010-05-24 02:01:52 +0000 | [diff] [blame] | 789 | iommu_disable(obj); |
| 790 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 791 | iopgd = iopgd_offset(obj, da); |
| 792 | |
Hiroshi DOYU | a1a5445 | 2010-05-13 09:45:35 +0300 | [diff] [blame] | 793 | if (!iopgd_is_table(*iopgd)) { |
Suman Anna | b6c2e09 | 2013-05-30 18:10:59 -0500 | [diff] [blame] | 794 | dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n", |
Suman Anna | 5835b6a | 2015-07-20 17:33:32 -0500 | [diff] [blame] | 795 | obj->name, errs, da, iopgd, *iopgd); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 796 | return IRQ_NONE; |
| 797 | } |
| 798 | |
| 799 | iopte = iopte_offset(iopgd, da); |
| 800 | |
Suman Anna | b6c2e09 | 2013-05-30 18:10:59 -0500 | [diff] [blame] | 801 | 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 Anna | 5835b6a | 2015-07-20 17:33:32 -0500 | [diff] [blame] | 802 | obj->name, errs, da, iopgd, *iopgd, iopte, *iopte); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 803 | |
| 804 | return IRQ_NONE; |
| 805 | } |
| 806 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 807 | /** |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 808 | * omap_iommu_attach() - attach iommu device to an iommu domain |
Joerg Roedel | ede1c2e | 2017-04-12 00:21:29 -0500 | [diff] [blame] | 809 | * @obj: target omap iommu device |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 810 | * @iopgd: page table |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 811 | **/ |
Joerg Roedel | ede1c2e | 2017-04-12 00:21:29 -0500 | [diff] [blame] | 812 | static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 813 | { |
Suman Anna | 7ee08b9e | 2014-02-28 14:42:33 -0600 | [diff] [blame] | 814 | int err; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 815 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 816 | spin_lock(&obj->iommu_lock); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 817 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 818 | obj->iopgd = iopgd; |
| 819 | err = iommu_enable(obj); |
| 820 | if (err) |
| 821 | goto err_enable; |
| 822 | flush_iotlb_all(obj); |
| 823 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 824 | spin_unlock(&obj->iommu_lock); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 825 | |
| 826 | dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name); |
Joerg Roedel | ede1c2e | 2017-04-12 00:21:29 -0500 | [diff] [blame] | 827 | |
| 828 | return 0; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 829 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 830 | err_enable: |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 831 | spin_unlock(&obj->iommu_lock); |
Joerg Roedel | ede1c2e | 2017-04-12 00:21:29 -0500 | [diff] [blame] | 832 | |
| 833 | return err; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 834 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 835 | |
| 836 | /** |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 837 | * omap_iommu_detach - release iommu device |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 838 | * @obj: target iommu |
| 839 | **/ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 840 | static void omap_iommu_detach(struct omap_iommu *obj) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 841 | { |
Roel Kluin | acf9d46 | 2010-01-08 10:29:05 -0800 | [diff] [blame] | 842 | if (!obj || IS_ERR(obj)) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 843 | return; |
| 844 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 845 | spin_lock(&obj->iommu_lock); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 846 | |
Suman Anna | 2088ecb | 2014-10-22 17:22:19 -0500 | [diff] [blame] | 847 | iommu_disable(obj); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 848 | obj->iopgd = NULL; |
| 849 | |
| 850 | spin_unlock(&obj->iommu_lock); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 851 | |
| 852 | dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name); |
| 853 | } |
David Cohen | d594f1f | 2011-02-16 19:35:51 +0000 | [diff] [blame] | 854 | |
Suman Anna | 3ca9299 | 2015-10-02 18:02:44 -0500 | [diff] [blame] | 855 | static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device *pdev, |
| 856 | struct omap_iommu *obj) |
| 857 | { |
| 858 | struct device_node *np = pdev->dev.of_node; |
| 859 | int ret; |
| 860 | |
| 861 | if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu")) |
| 862 | return 0; |
| 863 | |
| 864 | if (!of_property_read_bool(np, "ti,syscon-mmuconfig")) { |
| 865 | dev_err(&pdev->dev, "ti,syscon-mmuconfig property is missing\n"); |
| 866 | return -EINVAL; |
| 867 | } |
| 868 | |
| 869 | obj->syscfg = |
| 870 | syscon_regmap_lookup_by_phandle(np, "ti,syscon-mmuconfig"); |
| 871 | if (IS_ERR(obj->syscfg)) { |
| 872 | /* can fail with -EPROBE_DEFER */ |
| 873 | ret = PTR_ERR(obj->syscfg); |
| 874 | return ret; |
| 875 | } |
| 876 | |
| 877 | if (of_property_read_u32_index(np, "ti,syscon-mmuconfig", 1, |
| 878 | &obj->id)) { |
| 879 | dev_err(&pdev->dev, "couldn't get the IOMMU instance id within subsystem\n"); |
| 880 | return -EINVAL; |
| 881 | } |
| 882 | |
| 883 | if (obj->id != 0 && obj->id != 1) { |
| 884 | dev_err(&pdev->dev, "invalid IOMMU instance id\n"); |
| 885 | return -EINVAL; |
| 886 | } |
| 887 | |
| 888 | return 0; |
| 889 | } |
| 890 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 891 | /* |
| 892 | * OMAP Device MMU(IOMMU) detection |
| 893 | */ |
Greg Kroah-Hartman | d34d651 | 2012-12-21 15:05:21 -0800 | [diff] [blame] | 894 | static int omap_iommu_probe(struct platform_device *pdev) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 895 | { |
| 896 | int err = -ENODEV; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 897 | int irq; |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 898 | struct omap_iommu *obj; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 899 | struct resource *res; |
Florian Vaussard | 3c92748 | 2014-02-28 14:42:36 -0600 | [diff] [blame] | 900 | struct device_node *of = pdev->dev.of_node; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 901 | |
Suman Anna | 49a57ef | 2017-04-12 00:21:27 -0500 | [diff] [blame] | 902 | if (!of) { |
| 903 | pr_err("%s: only DT-based devices are supported\n", __func__); |
| 904 | return -ENODEV; |
| 905 | } |
| 906 | |
Suman Anna | f129b3d | 2014-02-28 14:42:32 -0600 | [diff] [blame] | 907 | obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 908 | if (!obj) |
| 909 | return -ENOMEM; |
| 910 | |
Suman Anna | 49a57ef | 2017-04-12 00:21:27 -0500 | [diff] [blame] | 911 | obj->name = dev_name(&pdev->dev); |
| 912 | obj->nr_tlb_entries = 32; |
| 913 | err = of_property_read_u32(of, "ti,#tlb-entries", &obj->nr_tlb_entries); |
| 914 | if (err && err != -EINVAL) |
| 915 | return err; |
| 916 | if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8) |
| 917 | return -EINVAL; |
| 918 | if (of_find_property(of, "ti,iommu-bus-err-back", NULL)) |
| 919 | obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN; |
Florian Vaussard | 3c92748 | 2014-02-28 14:42:36 -0600 | [diff] [blame] | 920 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 921 | obj->dev = &pdev->dev; |
| 922 | obj->ctx = (void *)obj + sizeof(*obj); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 923 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 924 | spin_lock_init(&obj->iommu_lock); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 925 | spin_lock_init(&obj->page_table_lock); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 926 | |
| 927 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Suman Anna | f129b3d | 2014-02-28 14:42:32 -0600 | [diff] [blame] | 928 | obj->regbase = devm_ioremap_resource(obj->dev, res); |
| 929 | if (IS_ERR(obj->regbase)) |
| 930 | return PTR_ERR(obj->regbase); |
Aaro Koskinen | da4a0f7 | 2011-03-14 12:28:32 +0000 | [diff] [blame] | 931 | |
Suman Anna | 3ca9299 | 2015-10-02 18:02:44 -0500 | [diff] [blame] | 932 | err = omap_iommu_dra7_get_dsp_system_cfg(pdev, obj); |
| 933 | if (err) |
| 934 | return err; |
| 935 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 936 | irq = platform_get_irq(pdev, 0); |
Suman Anna | f129b3d | 2014-02-28 14:42:32 -0600 | [diff] [blame] | 937 | if (irq < 0) |
| 938 | return -ENODEV; |
| 939 | |
| 940 | err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED, |
| 941 | dev_name(obj->dev), obj); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 942 | if (err < 0) |
Suman Anna | f129b3d | 2014-02-28 14:42:32 -0600 | [diff] [blame] | 943 | return err; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 944 | platform_set_drvdata(pdev, obj); |
| 945 | |
Joerg Roedel | 28ae1e3 | 2017-04-12 00:21:31 -0500 | [diff] [blame] | 946 | obj->group = iommu_group_alloc(); |
| 947 | if (IS_ERR(obj->group)) |
| 948 | return PTR_ERR(obj->group); |
| 949 | |
Joerg Roedel | 01611fe | 2017-04-12 00:21:30 -0500 | [diff] [blame] | 950 | err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL, obj->name); |
| 951 | if (err) |
Joerg Roedel | 28ae1e3 | 2017-04-12 00:21:31 -0500 | [diff] [blame] | 952 | goto out_group; |
Joerg Roedel | 01611fe | 2017-04-12 00:21:30 -0500 | [diff] [blame] | 953 | |
| 954 | iommu_device_set_ops(&obj->iommu, &omap_iommu_ops); |
| 955 | |
| 956 | err = iommu_device_register(&obj->iommu); |
| 957 | if (err) |
| 958 | goto out_sysfs; |
| 959 | |
Omar Ramirez Luna | ebf7cda | 2012-11-19 19:05:51 -0600 | [diff] [blame] | 960 | pm_runtime_irq_safe(obj->dev); |
| 961 | pm_runtime_enable(obj->dev); |
| 962 | |
Suman Anna | 61c7535 | 2014-10-22 17:22:30 -0500 | [diff] [blame] | 963 | omap_iommu_debugfs_add(obj); |
| 964 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 965 | dev_info(&pdev->dev, "%s registered\n", obj->name); |
Joerg Roedel | 28ae1e3 | 2017-04-12 00:21:31 -0500 | [diff] [blame] | 966 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 967 | return 0; |
Joerg Roedel | 01611fe | 2017-04-12 00:21:30 -0500 | [diff] [blame] | 968 | |
| 969 | out_sysfs: |
| 970 | iommu_device_sysfs_remove(&obj->iommu); |
Joerg Roedel | 28ae1e3 | 2017-04-12 00:21:31 -0500 | [diff] [blame] | 971 | out_group: |
| 972 | iommu_group_put(obj->group); |
Joerg Roedel | 01611fe | 2017-04-12 00:21:30 -0500 | [diff] [blame] | 973 | return err; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 974 | } |
| 975 | |
Greg Kroah-Hartman | d34d651 | 2012-12-21 15:05:21 -0800 | [diff] [blame] | 976 | static int omap_iommu_remove(struct platform_device *pdev) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 977 | { |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 978 | struct omap_iommu *obj = platform_get_drvdata(pdev); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 979 | |
Joerg Roedel | 28ae1e3 | 2017-04-12 00:21:31 -0500 | [diff] [blame] | 980 | iommu_group_put(obj->group); |
| 981 | obj->group = NULL; |
| 982 | |
Joerg Roedel | 01611fe | 2017-04-12 00:21:30 -0500 | [diff] [blame] | 983 | iommu_device_sysfs_remove(&obj->iommu); |
| 984 | iommu_device_unregister(&obj->iommu); |
| 985 | |
Suman Anna | 61c7535 | 2014-10-22 17:22:30 -0500 | [diff] [blame] | 986 | omap_iommu_debugfs_remove(obj); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 987 | |
Omar Ramirez Luna | ebf7cda | 2012-11-19 19:05:51 -0600 | [diff] [blame] | 988 | pm_runtime_disable(obj->dev); |
| 989 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 990 | dev_info(&pdev->dev, "%s removed\n", obj->name); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 991 | return 0; |
| 992 | } |
| 993 | |
Kiran Padwal | d943b0f | 2014-09-11 19:07:36 +0530 | [diff] [blame] | 994 | static const struct of_device_id omap_iommu_of_match[] = { |
Florian Vaussard | 3c92748 | 2014-02-28 14:42:36 -0600 | [diff] [blame] | 995 | { .compatible = "ti,omap2-iommu" }, |
| 996 | { .compatible = "ti,omap4-iommu" }, |
| 997 | { .compatible = "ti,dra7-iommu" }, |
Suman Anna | 3ca9299 | 2015-10-02 18:02:44 -0500 | [diff] [blame] | 998 | { .compatible = "ti,dra7-dsp-iommu" }, |
Florian Vaussard | 3c92748 | 2014-02-28 14:42:36 -0600 | [diff] [blame] | 999 | {}, |
| 1000 | }; |
Florian Vaussard | 3c92748 | 2014-02-28 14:42:36 -0600 | [diff] [blame] | 1001 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 1002 | static struct platform_driver omap_iommu_driver = { |
| 1003 | .probe = omap_iommu_probe, |
Greg Kroah-Hartman | d34d651 | 2012-12-21 15:05:21 -0800 | [diff] [blame] | 1004 | .remove = omap_iommu_remove, |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 1005 | .driver = { |
| 1006 | .name = "omap-iommu", |
Florian Vaussard | 3c92748 | 2014-02-28 14:42:36 -0600 | [diff] [blame] | 1007 | .of_match_table = of_match_ptr(omap_iommu_of_match), |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 1008 | }, |
| 1009 | }; |
| 1010 | |
| 1011 | static void iopte_cachep_ctor(void *iopte) |
| 1012 | { |
| 1013 | clean_dcache_area(iopte, IOPTE_TABLE_SIZE); |
| 1014 | } |
| 1015 | |
Laurent Pinchart | 286f600 | 2014-03-08 00:44:38 +0100 | [diff] [blame] | 1016 | static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz) |
Tony Lindgren | ed1c7de | 2012-11-02 12:24:06 -0700 | [diff] [blame] | 1017 | { |
| 1018 | memset(e, 0, sizeof(*e)); |
| 1019 | |
| 1020 | e->da = da; |
| 1021 | e->pa = pa; |
Suman Anna | d760e3e | 2014-03-17 20:31:32 -0500 | [diff] [blame] | 1022 | e->valid = MMU_CAM_V; |
Laurent Pinchart | 286f600 | 2014-03-08 00:44:38 +0100 | [diff] [blame] | 1023 | e->pgsz = pgsz; |
| 1024 | e->endian = MMU_RAM_ENDIAN_LITTLE; |
| 1025 | e->elsz = MMU_RAM_ELSZ_8; |
| 1026 | e->mixed = 0; |
Tony Lindgren | ed1c7de | 2012-11-02 12:24:06 -0700 | [diff] [blame] | 1027 | |
| 1028 | return iopgsz_to_bytes(e->pgsz); |
| 1029 | } |
| 1030 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1031 | static int omap_iommu_map(struct iommu_domain *domain, unsigned long da, |
Suman Anna | 5835b6a | 2015-07-20 17:33:32 -0500 | [diff] [blame] | 1032 | phys_addr_t pa, size_t bytes, int prot) |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1033 | { |
Joerg Roedel | 8cf851e | 2015-03-26 13:43:09 +0100 | [diff] [blame] | 1034 | struct omap_iommu_domain *omap_domain = to_omap_domain(domain); |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 1035 | struct omap_iommu *oiommu = omap_domain->iommu_dev; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1036 | struct device *dev = oiommu->dev; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1037 | struct iotlb_entry e; |
| 1038 | int omap_pgsz; |
Laurent Pinchart | 286f600 | 2014-03-08 00:44:38 +0100 | [diff] [blame] | 1039 | u32 ret; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1040 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1041 | omap_pgsz = bytes_to_iopgsz(bytes); |
| 1042 | if (omap_pgsz < 0) { |
| 1043 | dev_err(dev, "invalid size to map: %d\n", bytes); |
| 1044 | return -EINVAL; |
| 1045 | } |
| 1046 | |
Joerg Roedel | 1d7f449 | 2015-01-22 14:42:06 +0100 | [diff] [blame] | 1047 | dev_dbg(dev, "mapping da 0x%lx to pa %pa size 0x%x\n", da, &pa, bytes); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1048 | |
Laurent Pinchart | 286f600 | 2014-03-08 00:44:38 +0100 | [diff] [blame] | 1049 | iotlb_init_entry(&e, da, pa, omap_pgsz); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1050 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 1051 | ret = omap_iopgtable_store_entry(oiommu, &e); |
Ohad Ben-Cohen | b4550d4 | 2011-09-02 13:32:31 -0400 | [diff] [blame] | 1052 | if (ret) |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 1053 | dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1054 | |
Ohad Ben-Cohen | b4550d4 | 2011-09-02 13:32:31 -0400 | [diff] [blame] | 1055 | return ret; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1056 | } |
| 1057 | |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 1058 | static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da, |
Suman Anna | 5835b6a | 2015-07-20 17:33:32 -0500 | [diff] [blame] | 1059 | size_t size) |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1060 | { |
Joerg Roedel | 8cf851e | 2015-03-26 13:43:09 +0100 | [diff] [blame] | 1061 | struct omap_iommu_domain *omap_domain = to_omap_domain(domain); |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 1062 | struct omap_iommu *oiommu = omap_domain->iommu_dev; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1063 | struct device *dev = oiommu->dev; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1064 | |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 1065 | dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1066 | |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 1067 | return iopgtable_clear_entry(oiommu, da); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | static int |
| 1071 | omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev) |
| 1072 | { |
Joerg Roedel | 8cf851e | 2015-03-26 13:43:09 +0100 | [diff] [blame] | 1073 | struct omap_iommu_domain *omap_domain = to_omap_domain(domain); |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 1074 | struct omap_iommu_arch_data *arch_data = dev->archdata.iommu; |
Joerg Roedel | ede1c2e | 2017-04-12 00:21:29 -0500 | [diff] [blame] | 1075 | struct omap_iommu *oiommu; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1076 | int ret = 0; |
| 1077 | |
Joerg Roedel | ede1c2e | 2017-04-12 00:21:29 -0500 | [diff] [blame] | 1078 | if (!arch_data || !arch_data->iommu_dev) { |
Suman Anna | e3f595b | 2014-09-04 17:27:29 -0500 | [diff] [blame] | 1079 | dev_err(dev, "device doesn't have an associated iommu\n"); |
| 1080 | return -EINVAL; |
| 1081 | } |
| 1082 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1083 | spin_lock(&omap_domain->lock); |
| 1084 | |
| 1085 | /* only a single device is supported per domain for now */ |
| 1086 | if (omap_domain->iommu_dev) { |
| 1087 | dev_err(dev, "iommu domain is already attached\n"); |
| 1088 | ret = -EBUSY; |
| 1089 | goto out; |
| 1090 | } |
| 1091 | |
Joerg Roedel | ede1c2e | 2017-04-12 00:21:29 -0500 | [diff] [blame] | 1092 | oiommu = arch_data->iommu_dev; |
| 1093 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1094 | /* get a handle to and enable the omap iommu */ |
Joerg Roedel | ede1c2e | 2017-04-12 00:21:29 -0500 | [diff] [blame] | 1095 | ret = omap_iommu_attach(oiommu, omap_domain->pgtable); |
| 1096 | if (ret) { |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1097 | dev_err(dev, "can't get omap iommu: %d\n", ret); |
| 1098 | goto out; |
| 1099 | } |
| 1100 | |
Joerg Roedel | ede1c2e | 2017-04-12 00:21:29 -0500 | [diff] [blame] | 1101 | omap_domain->iommu_dev = oiommu; |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 1102 | omap_domain->dev = dev; |
Ohad Ben-Cohen | e7f10f0 | 2011-09-13 15:26:29 -0400 | [diff] [blame] | 1103 | oiommu->domain = domain; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1104 | |
| 1105 | out: |
| 1106 | spin_unlock(&omap_domain->lock); |
| 1107 | return ret; |
| 1108 | } |
| 1109 | |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 1110 | static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain, |
Suman Anna | 5835b6a | 2015-07-20 17:33:32 -0500 | [diff] [blame] | 1111 | struct device *dev) |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1112 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 1113 | struct omap_iommu *oiommu = dev_to_omap_iommu(dev); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1114 | |
| 1115 | /* only a single device is supported per domain for now */ |
| 1116 | if (omap_domain->iommu_dev != oiommu) { |
| 1117 | dev_err(dev, "invalid iommu device\n"); |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 1118 | return; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | iopgtable_clear_entry_all(oiommu); |
| 1122 | |
| 1123 | omap_iommu_detach(oiommu); |
| 1124 | |
Joerg Roedel | ede1c2e | 2017-04-12 00:21:29 -0500 | [diff] [blame] | 1125 | omap_domain->iommu_dev = NULL; |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 1126 | omap_domain->dev = NULL; |
Suman Anna | f24d9ad | 2014-10-22 17:22:33 -0500 | [diff] [blame] | 1127 | oiommu->domain = NULL; |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 1128 | } |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1129 | |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 1130 | static void omap_iommu_detach_dev(struct iommu_domain *domain, |
Suman Anna | 5835b6a | 2015-07-20 17:33:32 -0500 | [diff] [blame] | 1131 | struct device *dev) |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 1132 | { |
Joerg Roedel | 8cf851e | 2015-03-26 13:43:09 +0100 | [diff] [blame] | 1133 | struct omap_iommu_domain *omap_domain = to_omap_domain(domain); |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 1134 | |
| 1135 | spin_lock(&omap_domain->lock); |
| 1136 | _omap_iommu_detach_dev(omap_domain, dev); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1137 | spin_unlock(&omap_domain->lock); |
| 1138 | } |
| 1139 | |
Joerg Roedel | 8cf851e | 2015-03-26 13:43:09 +0100 | [diff] [blame] | 1140 | static struct iommu_domain *omap_iommu_domain_alloc(unsigned type) |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1141 | { |
| 1142 | struct omap_iommu_domain *omap_domain; |
| 1143 | |
Joerg Roedel | 8cf851e | 2015-03-26 13:43:09 +0100 | [diff] [blame] | 1144 | if (type != IOMMU_DOMAIN_UNMANAGED) |
| 1145 | return NULL; |
| 1146 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1147 | omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL); |
Suman Anna | 99ee98d | 2015-07-20 17:33:29 -0500 | [diff] [blame] | 1148 | if (!omap_domain) |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1149 | goto out; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1150 | |
| 1151 | omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL); |
Suman Anna | 99ee98d | 2015-07-20 17:33:29 -0500 | [diff] [blame] | 1152 | if (!omap_domain->pgtable) |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1153 | goto fail_nomem; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1154 | |
| 1155 | /* |
| 1156 | * should never fail, but please keep this around to ensure |
| 1157 | * we keep the hardware happy |
| 1158 | */ |
Suman Anna | 433c434 | 2016-04-04 17:46:20 -0500 | [diff] [blame] | 1159 | if (WARN_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE))) |
| 1160 | goto fail_align; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1161 | |
| 1162 | clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE); |
| 1163 | spin_lock_init(&omap_domain->lock); |
| 1164 | |
Joerg Roedel | 8cf851e | 2015-03-26 13:43:09 +0100 | [diff] [blame] | 1165 | omap_domain->domain.geometry.aperture_start = 0; |
| 1166 | omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1; |
| 1167 | omap_domain->domain.geometry.force_aperture = true; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1168 | |
Joerg Roedel | 8cf851e | 2015-03-26 13:43:09 +0100 | [diff] [blame] | 1169 | return &omap_domain->domain; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1170 | |
Suman Anna | 433c434 | 2016-04-04 17:46:20 -0500 | [diff] [blame] | 1171 | fail_align: |
| 1172 | kfree(omap_domain->pgtable); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1173 | fail_nomem: |
| 1174 | kfree(omap_domain); |
| 1175 | out: |
Joerg Roedel | 8cf851e | 2015-03-26 13:43:09 +0100 | [diff] [blame] | 1176 | return NULL; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1177 | } |
| 1178 | |
Joerg Roedel | 8cf851e | 2015-03-26 13:43:09 +0100 | [diff] [blame] | 1179 | static void omap_iommu_domain_free(struct iommu_domain *domain) |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1180 | { |
Joerg Roedel | 8cf851e | 2015-03-26 13:43:09 +0100 | [diff] [blame] | 1181 | struct omap_iommu_domain *omap_domain = to_omap_domain(domain); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1182 | |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 1183 | /* |
| 1184 | * An iommu device is still attached |
| 1185 | * (currently, only one device can be attached) ? |
| 1186 | */ |
| 1187 | if (omap_domain->iommu_dev) |
| 1188 | _omap_iommu_detach_dev(omap_domain, omap_domain->dev); |
| 1189 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1190 | kfree(omap_domain->pgtable); |
| 1191 | kfree(omap_domain); |
| 1192 | } |
| 1193 | |
| 1194 | static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain, |
Suman Anna | 5835b6a | 2015-07-20 17:33:32 -0500 | [diff] [blame] | 1195 | dma_addr_t da) |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1196 | { |
Joerg Roedel | 8cf851e | 2015-03-26 13:43:09 +0100 | [diff] [blame] | 1197 | struct omap_iommu_domain *omap_domain = to_omap_domain(domain); |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 1198 | struct omap_iommu *oiommu = omap_domain->iommu_dev; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1199 | struct device *dev = oiommu->dev; |
| 1200 | u32 *pgd, *pte; |
| 1201 | phys_addr_t ret = 0; |
| 1202 | |
| 1203 | iopgtable_lookup_entry(oiommu, da, &pgd, &pte); |
| 1204 | |
| 1205 | if (pte) { |
| 1206 | if (iopte_is_small(*pte)) |
| 1207 | ret = omap_iommu_translate(*pte, da, IOPTE_MASK); |
| 1208 | else if (iopte_is_large(*pte)) |
| 1209 | ret = omap_iommu_translate(*pte, da, IOLARGE_MASK); |
| 1210 | else |
Suman Anna | 2abfcfb | 2013-05-30 18:10:38 -0500 | [diff] [blame] | 1211 | dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte, |
Suman Anna | 5835b6a | 2015-07-20 17:33:32 -0500 | [diff] [blame] | 1212 | (unsigned long long)da); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1213 | } else { |
| 1214 | if (iopgd_is_section(*pgd)) |
| 1215 | ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK); |
| 1216 | else if (iopgd_is_super(*pgd)) |
| 1217 | ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK); |
| 1218 | else |
Suman Anna | 2abfcfb | 2013-05-30 18:10:38 -0500 | [diff] [blame] | 1219 | dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd, |
Suman Anna | 5835b6a | 2015-07-20 17:33:32 -0500 | [diff] [blame] | 1220 | (unsigned long long)da); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | return ret; |
| 1224 | } |
| 1225 | |
Laurent Pinchart | 07a0203 | 2014-02-28 14:42:38 -0600 | [diff] [blame] | 1226 | static int omap_iommu_add_device(struct device *dev) |
| 1227 | { |
| 1228 | struct omap_iommu_arch_data *arch_data; |
Joerg Roedel | ede1c2e | 2017-04-12 00:21:29 -0500 | [diff] [blame] | 1229 | struct omap_iommu *oiommu; |
Joerg Roedel | 28ae1e3 | 2017-04-12 00:21:31 -0500 | [diff] [blame] | 1230 | struct iommu_group *group; |
Laurent Pinchart | 07a0203 | 2014-02-28 14:42:38 -0600 | [diff] [blame] | 1231 | struct device_node *np; |
Suman Anna | 7d68277 | 2014-09-04 17:27:30 -0500 | [diff] [blame] | 1232 | struct platform_device *pdev; |
Joerg Roedel | 01611fe | 2017-04-12 00:21:30 -0500 | [diff] [blame] | 1233 | int ret; |
Laurent Pinchart | 07a0203 | 2014-02-28 14:42:38 -0600 | [diff] [blame] | 1234 | |
| 1235 | /* |
| 1236 | * Allocate the archdata iommu structure for DT-based devices. |
| 1237 | * |
| 1238 | * TODO: Simplify this when removing non-DT support completely from the |
| 1239 | * IOMMU users. |
| 1240 | */ |
| 1241 | if (!dev->of_node) |
| 1242 | return 0; |
| 1243 | |
| 1244 | np = of_parse_phandle(dev->of_node, "iommus", 0); |
| 1245 | if (!np) |
| 1246 | return 0; |
| 1247 | |
Suman Anna | 7d68277 | 2014-09-04 17:27:30 -0500 | [diff] [blame] | 1248 | pdev = of_find_device_by_node(np); |
| 1249 | if (WARN_ON(!pdev)) { |
| 1250 | of_node_put(np); |
| 1251 | return -EINVAL; |
| 1252 | } |
| 1253 | |
Joerg Roedel | ede1c2e | 2017-04-12 00:21:29 -0500 | [diff] [blame] | 1254 | oiommu = platform_get_drvdata(pdev); |
| 1255 | if (!oiommu) { |
| 1256 | of_node_put(np); |
| 1257 | return -EINVAL; |
| 1258 | } |
| 1259 | |
Laurent Pinchart | 07a0203 | 2014-02-28 14:42:38 -0600 | [diff] [blame] | 1260 | arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL); |
| 1261 | if (!arch_data) { |
| 1262 | of_node_put(np); |
| 1263 | return -ENOMEM; |
| 1264 | } |
| 1265 | |
Joerg Roedel | 01611fe | 2017-04-12 00:21:30 -0500 | [diff] [blame] | 1266 | ret = iommu_device_link(&oiommu->iommu, dev); |
| 1267 | if (ret) { |
| 1268 | kfree(arch_data); |
| 1269 | of_node_put(np); |
| 1270 | return ret; |
| 1271 | } |
| 1272 | |
Joerg Roedel | ede1c2e | 2017-04-12 00:21:29 -0500 | [diff] [blame] | 1273 | arch_data->iommu_dev = oiommu; |
Laurent Pinchart | 07a0203 | 2014-02-28 14:42:38 -0600 | [diff] [blame] | 1274 | dev->archdata.iommu = arch_data; |
| 1275 | |
Joerg Roedel | 28ae1e3 | 2017-04-12 00:21:31 -0500 | [diff] [blame] | 1276 | /* |
| 1277 | * IOMMU group initialization calls into omap_iommu_device_group, which |
| 1278 | * needs a valid dev->archdata.iommu pointer |
| 1279 | */ |
| 1280 | group = iommu_group_get_for_dev(dev); |
| 1281 | if (IS_ERR(group)) { |
| 1282 | iommu_device_unlink(&oiommu->iommu, dev); |
| 1283 | dev->archdata.iommu = NULL; |
| 1284 | kfree(arch_data); |
| 1285 | return PTR_ERR(group); |
| 1286 | } |
| 1287 | iommu_group_put(group); |
| 1288 | |
Laurent Pinchart | 07a0203 | 2014-02-28 14:42:38 -0600 | [diff] [blame] | 1289 | of_node_put(np); |
| 1290 | |
| 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | static void omap_iommu_remove_device(struct device *dev) |
| 1295 | { |
| 1296 | struct omap_iommu_arch_data *arch_data = dev->archdata.iommu; |
| 1297 | |
| 1298 | if (!dev->of_node || !arch_data) |
| 1299 | return; |
| 1300 | |
Joerg Roedel | 01611fe | 2017-04-12 00:21:30 -0500 | [diff] [blame] | 1301 | iommu_device_unlink(&arch_data->iommu_dev->iommu, dev); |
Joerg Roedel | 28ae1e3 | 2017-04-12 00:21:31 -0500 | [diff] [blame] | 1302 | iommu_group_remove_device(dev); |
Joerg Roedel | 01611fe | 2017-04-12 00:21:30 -0500 | [diff] [blame] | 1303 | |
Joerg Roedel | ede1c2e | 2017-04-12 00:21:29 -0500 | [diff] [blame] | 1304 | dev->archdata.iommu = NULL; |
Laurent Pinchart | 07a0203 | 2014-02-28 14:42:38 -0600 | [diff] [blame] | 1305 | kfree(arch_data); |
Joerg Roedel | 01611fe | 2017-04-12 00:21:30 -0500 | [diff] [blame] | 1306 | |
Laurent Pinchart | 07a0203 | 2014-02-28 14:42:38 -0600 | [diff] [blame] | 1307 | } |
| 1308 | |
Joerg Roedel | 28ae1e3 | 2017-04-12 00:21:31 -0500 | [diff] [blame] | 1309 | static struct iommu_group *omap_iommu_device_group(struct device *dev) |
| 1310 | { |
| 1311 | struct omap_iommu_arch_data *arch_data = dev->archdata.iommu; |
| 1312 | struct iommu_group *group = NULL; |
| 1313 | |
| 1314 | if (arch_data->iommu_dev) |
| 1315 | group = arch_data->iommu_dev->group; |
| 1316 | |
| 1317 | return group; |
| 1318 | } |
| 1319 | |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 1320 | static const struct iommu_ops omap_iommu_ops = { |
Joerg Roedel | 8cf851e | 2015-03-26 13:43:09 +0100 | [diff] [blame] | 1321 | .domain_alloc = omap_iommu_domain_alloc, |
| 1322 | .domain_free = omap_iommu_domain_free, |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1323 | .attach_dev = omap_iommu_attach_dev, |
| 1324 | .detach_dev = omap_iommu_detach_dev, |
| 1325 | .map = omap_iommu_map, |
| 1326 | .unmap = omap_iommu_unmap, |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 1327 | .map_sg = default_iommu_map_sg, |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1328 | .iova_to_phys = omap_iommu_iova_to_phys, |
Laurent Pinchart | 07a0203 | 2014-02-28 14:42:38 -0600 | [diff] [blame] | 1329 | .add_device = omap_iommu_add_device, |
| 1330 | .remove_device = omap_iommu_remove_device, |
Joerg Roedel | 28ae1e3 | 2017-04-12 00:21:31 -0500 | [diff] [blame] | 1331 | .device_group = omap_iommu_device_group, |
Ohad Ben-Cohen | 66bc8cf | 2011-11-10 11:32:27 +0200 | [diff] [blame] | 1332 | .pgsize_bitmap = OMAP_IOMMU_PGSIZES, |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1333 | }; |
| 1334 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 1335 | static int __init omap_iommu_init(void) |
| 1336 | { |
| 1337 | struct kmem_cache *p; |
| 1338 | const unsigned long flags = SLAB_HWCACHE_ALIGN; |
| 1339 | size_t align = 1 << 10; /* L2 pagetable alignement */ |
Thierry Reding | f938aab | 2015-02-06 11:44:06 +0100 | [diff] [blame] | 1340 | struct device_node *np; |
Suman Anna | abaa7e5 | 2017-04-12 00:21:26 -0500 | [diff] [blame] | 1341 | int ret; |
Thierry Reding | f938aab | 2015-02-06 11:44:06 +0100 | [diff] [blame] | 1342 | |
| 1343 | np = of_find_matching_node(NULL, omap_iommu_of_match); |
| 1344 | if (!np) |
| 1345 | return 0; |
| 1346 | |
| 1347 | of_node_put(np); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 1348 | |
| 1349 | p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags, |
| 1350 | iopte_cachep_ctor); |
| 1351 | if (!p) |
| 1352 | return -ENOMEM; |
| 1353 | iopte_cachep = p; |
| 1354 | |
Suman Anna | 61c7535 | 2014-10-22 17:22:30 -0500 | [diff] [blame] | 1355 | omap_iommu_debugfs_init(); |
| 1356 | |
Suman Anna | abaa7e5 | 2017-04-12 00:21:26 -0500 | [diff] [blame] | 1357 | ret = platform_driver_register(&omap_iommu_driver); |
| 1358 | if (ret) { |
| 1359 | pr_err("%s: failed to register driver\n", __func__); |
| 1360 | goto fail_driver; |
| 1361 | } |
| 1362 | |
| 1363 | ret = bus_set_iommu(&platform_bus_type, &omap_iommu_ops); |
| 1364 | if (ret) |
| 1365 | goto fail_bus; |
| 1366 | |
| 1367 | return 0; |
| 1368 | |
| 1369 | fail_bus: |
| 1370 | platform_driver_unregister(&omap_iommu_driver); |
| 1371 | fail_driver: |
| 1372 | kmem_cache_destroy(iopte_cachep); |
| 1373 | return ret; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 1374 | } |
Ohad Ben-Cohen | 435792d | 2012-02-26 12:14:14 +0200 | [diff] [blame] | 1375 | subsys_initcall(omap_iommu_init); |
Suman Anna | 0cdbf72 | 2015-07-20 17:33:24 -0500 | [diff] [blame] | 1376 | /* must be ready before omap3isp is probed */ |