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