Olav Haugan | e6d01ef | 2013-01-25 16:55:44 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved. |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/list.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/iommu.h> |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 24 | #include <linux/clk.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 25 | #include <linux/scatterlist.h> |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 26 | |
| 27 | #include <asm/cacheflush.h> |
| 28 | #include <asm/sizes.h> |
| 29 | |
Olav Haugan | c599314 | 2013-02-04 13:59:39 -0800 | [diff] [blame] | 30 | #include <mach/iommu_perfmon.h> |
Olav Haugan | e6d01ef | 2013-01-25 16:55:44 -0800 | [diff] [blame] | 31 | #include <mach/iommu_hw-v0.h> |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 32 | #include <mach/msm_iommu_priv.h> |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 33 | #include <mach/iommu.h> |
Olav Haugan | 65209cd | 2012-11-07 15:02:56 -0800 | [diff] [blame] | 34 | #include <mach/msm_smsm.h> |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 35 | |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 36 | #define MRC(reg, processor, op1, crn, crm, op2) \ |
| 37 | __asm__ __volatile__ ( \ |
| 38 | " mrc " #processor "," #op1 ", %0," #crn "," #crm "," #op2 "\n" \ |
| 39 | : "=r" (reg)) |
| 40 | |
| 41 | #define RCP15_PRRR(reg) MRC(reg, p15, 0, c10, c2, 0) |
| 42 | #define RCP15_NMRR(reg) MRC(reg, p15, 0, c10, c2, 1) |
| 43 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 44 | /* Sharability attributes of MSM IOMMU mappings */ |
| 45 | #define MSM_IOMMU_ATTR_NON_SH 0x0 |
| 46 | #define MSM_IOMMU_ATTR_SH 0x4 |
| 47 | |
| 48 | /* Cacheability attributes of MSM IOMMU mappings */ |
| 49 | #define MSM_IOMMU_ATTR_NONCACHED 0x0 |
| 50 | #define MSM_IOMMU_ATTR_CACHED_WB_WA 0x1 |
| 51 | #define MSM_IOMMU_ATTR_CACHED_WB_NWA 0x2 |
| 52 | #define MSM_IOMMU_ATTR_CACHED_WT 0x3 |
| 53 | |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 54 | struct bus_type msm_iommu_sec_bus_type = { |
| 55 | .name = "msm_iommu_sec_bus", |
| 56 | }; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 57 | |
| 58 | static inline void clean_pte(unsigned long *start, unsigned long *end, |
| 59 | int redirect) |
| 60 | { |
| 61 | if (!redirect) |
| 62 | dmac_flush_range(start, end); |
| 63 | } |
| 64 | |
Ohad Ben-Cohen | 8342727 | 2011-11-10 11:32:28 +0200 | [diff] [blame] | 65 | /* bitmap of the page sizes currently supported */ |
| 66 | #define MSM_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M) |
| 67 | |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 68 | static int msm_iommu_tex_class[4]; |
| 69 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 70 | DEFINE_MUTEX(msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 71 | |
Olav Haugan | 65209cd | 2012-11-07 15:02:56 -0800 | [diff] [blame] | 72 | /** |
| 73 | * Remote spinlock implementation based on Peterson's algorithm to be used |
| 74 | * to synchronize IOMMU config port access between CPU and GPU. |
| 75 | * This implements Process 0 of the spin lock algorithm. GPU implements |
| 76 | * Process 1. Flag and turn is stored in shared memory to allow GPU to |
| 77 | * access these. |
| 78 | */ |
| 79 | struct msm_iommu_remote_lock { |
| 80 | int initialized; |
| 81 | struct remote_iommu_petersons_spinlock *lock; |
| 82 | }; |
| 83 | |
| 84 | static struct msm_iommu_remote_lock msm_iommu_remote_lock; |
| 85 | |
| 86 | #ifdef CONFIG_MSM_IOMMU_GPU_SYNC |
| 87 | static void _msm_iommu_remote_spin_lock_init(void) |
| 88 | { |
| 89 | msm_iommu_remote_lock.lock = smem_alloc(SMEM_SPINLOCK_ARRAY, 32); |
| 90 | memset(msm_iommu_remote_lock.lock, 0, |
| 91 | sizeof(*msm_iommu_remote_lock.lock)); |
| 92 | } |
| 93 | |
| 94 | void msm_iommu_remote_p0_spin_lock(void) |
| 95 | { |
| 96 | msm_iommu_remote_lock.lock->flag[PROC_APPS] = 1; |
| 97 | msm_iommu_remote_lock.lock->turn = 1; |
| 98 | |
| 99 | smp_mb(); |
| 100 | |
| 101 | while (msm_iommu_remote_lock.lock->flag[PROC_GPU] == 1 && |
| 102 | msm_iommu_remote_lock.lock->turn == 1) |
| 103 | cpu_relax(); |
| 104 | } |
| 105 | |
| 106 | void msm_iommu_remote_p0_spin_unlock(void) |
| 107 | { |
| 108 | smp_mb(); |
| 109 | |
| 110 | msm_iommu_remote_lock.lock->flag[PROC_APPS] = 0; |
| 111 | } |
| 112 | #endif |
| 113 | |
| 114 | inline void msm_iommu_mutex_lock(void) |
| 115 | { |
| 116 | mutex_lock(&msm_iommu_lock); |
| 117 | } |
| 118 | |
| 119 | inline void msm_iommu_mutex_unlock(void) |
| 120 | { |
| 121 | mutex_unlock(&msm_iommu_lock); |
| 122 | } |
| 123 | |
| 124 | void *msm_iommu_lock_initialize(void) |
| 125 | { |
| 126 | mutex_lock(&msm_iommu_lock); |
| 127 | if (!msm_iommu_remote_lock.initialized) { |
| 128 | msm_iommu_remote_lock_init(); |
| 129 | msm_iommu_remote_lock.initialized = 1; |
| 130 | } |
| 131 | mutex_unlock(&msm_iommu_lock); |
| 132 | return msm_iommu_remote_lock.lock; |
| 133 | } |
| 134 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 135 | static int __enable_clocks(struct msm_iommu_drvdata *drvdata) |
| 136 | { |
| 137 | int ret; |
| 138 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 139 | ret = clk_prepare_enable(drvdata->pclk); |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 140 | if (ret) |
| 141 | goto fail; |
| 142 | |
| 143 | if (drvdata->clk) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 144 | ret = clk_prepare_enable(drvdata->clk); |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 145 | if (ret) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 146 | clk_disable_unprepare(drvdata->pclk); |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 147 | } |
| 148 | fail: |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | static void __disable_clocks(struct msm_iommu_drvdata *drvdata) |
| 153 | { |
| 154 | if (drvdata->clk) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 155 | clk_disable_unprepare(drvdata->clk); |
| 156 | clk_disable_unprepare(drvdata->pclk); |
| 157 | } |
| 158 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 159 | static int __enable_regulators(struct msm_iommu_drvdata *drvdata) |
Olav Haugan | c599314 | 2013-02-04 13:59:39 -0800 | [diff] [blame] | 160 | { |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 161 | /* No need to do anything. IOMMUv0 is always on. */ |
| 162 | return 0; |
Olav Haugan | c599314 | 2013-02-04 13:59:39 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 165 | static void __disable_regulators(struct msm_iommu_drvdata *drvdata) |
Olav Haugan | c599314 | 2013-02-04 13:59:39 -0800 | [diff] [blame] | 166 | { |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 167 | /* No need to do anything. IOMMUv0 is always on. */ |
Olav Haugan | c599314 | 2013-02-04 13:59:39 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static void _iommu_lock_acquire(void) |
| 171 | { |
| 172 | msm_iommu_lock(); |
| 173 | } |
| 174 | |
| 175 | static void _iommu_lock_release(void) |
| 176 | { |
| 177 | msm_iommu_unlock(); |
| 178 | } |
| 179 | |
| 180 | struct iommu_access_ops iommu_access_ops_v0 = { |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 181 | .iommu_power_on = __enable_regulators, |
| 182 | .iommu_power_off = __disable_regulators, |
| 183 | .iommu_clk_on = __enable_clocks, |
| 184 | .iommu_clk_off = __disable_clocks, |
Olav Haugan | c599314 | 2013-02-04 13:59:39 -0800 | [diff] [blame] | 185 | .iommu_lock_acquire = _iommu_lock_acquire, |
| 186 | .iommu_lock_release = _iommu_lock_release, |
| 187 | }; |
| 188 | EXPORT_SYMBOL(iommu_access_ops_v0); |
| 189 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 190 | static int __flush_iotlb_va(struct iommu_domain *domain, unsigned int va) |
| 191 | { |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 192 | struct msm_iommu_priv *priv = domain->priv; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 193 | struct msm_iommu_drvdata *iommu_drvdata; |
| 194 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 195 | int ret = 0; |
| 196 | int asid; |
| 197 | |
| 198 | list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) { |
| 199 | if (!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent) |
| 200 | BUG(); |
| 201 | |
| 202 | iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent); |
| 203 | if (!iommu_drvdata) |
| 204 | BUG(); |
| 205 | |
| 206 | ret = __enable_clocks(iommu_drvdata); |
| 207 | if (ret) |
| 208 | goto fail; |
| 209 | |
Olav Haugan | 65209cd | 2012-11-07 15:02:56 -0800 | [diff] [blame] | 210 | msm_iommu_remote_spin_lock(); |
| 211 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 212 | asid = GET_CONTEXTIDR_ASID(iommu_drvdata->base, |
| 213 | ctx_drvdata->num); |
| 214 | |
| 215 | SET_TLBIVA(iommu_drvdata->base, ctx_drvdata->num, |
| 216 | asid | (va & TLBIVA_VA)); |
| 217 | mb(); |
Olav Haugan | 65209cd | 2012-11-07 15:02:56 -0800 | [diff] [blame] | 218 | |
| 219 | msm_iommu_remote_spin_unlock(); |
| 220 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 221 | __disable_clocks(iommu_drvdata); |
| 222 | } |
| 223 | fail: |
| 224 | return ret; |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Stepan Moskovchenko | 3306973 | 2010-11-12 19:30:00 -0800 | [diff] [blame] | 227 | static int __flush_iotlb(struct iommu_domain *domain) |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 228 | { |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 229 | struct msm_iommu_priv *priv = domain->priv; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 230 | struct msm_iommu_drvdata *iommu_drvdata; |
| 231 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
Stepan Moskovchenko | 3306973 | 2010-11-12 19:30:00 -0800 | [diff] [blame] | 232 | int ret = 0; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 233 | int asid; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 234 | |
| 235 | list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) { |
| 236 | if (!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent) |
| 237 | BUG(); |
| 238 | |
| 239 | iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 240 | if (!iommu_drvdata) |
| 241 | BUG(); |
Stepan Moskovchenko | 3306973 | 2010-11-12 19:30:00 -0800 | [diff] [blame] | 242 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 243 | ret = __enable_clocks(iommu_drvdata); |
| 244 | if (ret) |
| 245 | goto fail; |
| 246 | |
Olav Haugan | 65209cd | 2012-11-07 15:02:56 -0800 | [diff] [blame] | 247 | msm_iommu_remote_spin_lock(); |
| 248 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 249 | asid = GET_CONTEXTIDR_ASID(iommu_drvdata->base, |
| 250 | ctx_drvdata->num); |
| 251 | |
| 252 | SET_TLBIASID(iommu_drvdata->base, ctx_drvdata->num, asid); |
| 253 | mb(); |
Olav Haugan | 65209cd | 2012-11-07 15:02:56 -0800 | [diff] [blame] | 254 | |
| 255 | msm_iommu_remote_spin_unlock(); |
| 256 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 257 | __disable_clocks(iommu_drvdata); |
| 258 | } |
| 259 | fail: |
Stepan Moskovchenko | 3306973 | 2010-11-12 19:30:00 -0800 | [diff] [blame] | 260 | return ret; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 263 | static void __reset_context(void __iomem *base, void __iomem *glb_base, int ctx) |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 264 | { |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 265 | SET_BPRCOSH(glb_base, ctx, 0); |
| 266 | SET_BPRCISH(glb_base, ctx, 0); |
| 267 | SET_BPRCNSH(glb_base, ctx, 0); |
| 268 | SET_BPSHCFG(glb_base, ctx, 0); |
| 269 | SET_BPMTCFG(glb_base, ctx, 0); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 270 | SET_ACTLR(base, ctx, 0); |
| 271 | SET_SCTLR(base, ctx, 0); |
| 272 | SET_FSRRESTORE(base, ctx, 0); |
| 273 | SET_TTBR0(base, ctx, 0); |
| 274 | SET_TTBR1(base, ctx, 0); |
| 275 | SET_TTBCR(base, ctx, 0); |
| 276 | SET_BFBCR(base, ctx, 0); |
| 277 | SET_PAR(base, ctx, 0); |
| 278 | SET_FAR(base, ctx, 0); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 279 | SET_TLBFLPTER(base, ctx, 0); |
| 280 | SET_TLBSLPTER(base, ctx, 0); |
| 281 | SET_TLBLKCR(base, ctx, 0); |
| 282 | SET_PRRR(base, ctx, 0); |
| 283 | SET_NMRR(base, ctx, 0); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 284 | mb(); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 287 | static void __program_context(void __iomem *base, void __iomem *glb_base, |
| 288 | int ctx, int ncb, phys_addr_t pgtable, |
| 289 | int redirect, int ttbr_split) |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 290 | { |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 291 | unsigned int prrr, nmrr; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 292 | int i, j, found; |
Olav Haugan | 65209cd | 2012-11-07 15:02:56 -0800 | [diff] [blame] | 293 | msm_iommu_remote_spin_lock(); |
| 294 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 295 | __reset_context(base, glb_base, ctx); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 296 | |
| 297 | /* Set up HTW mode */ |
| 298 | /* TLB miss configuration: perform HTW on miss */ |
| 299 | SET_TLBMCFG(base, ctx, 0x3); |
| 300 | |
| 301 | /* V2P configuration: HTW for access */ |
| 302 | SET_V2PCFG(base, ctx, 0x3); |
| 303 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 304 | SET_TTBCR(base, ctx, ttbr_split); |
| 305 | SET_TTBR0_PA(base, ctx, (pgtable >> TTBR0_PA_SHIFT)); |
| 306 | if (ttbr_split) |
| 307 | SET_TTBR1_PA(base, ctx, (pgtable >> TTBR1_PA_SHIFT)); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 308 | |
| 309 | /* Enable context fault interrupt */ |
| 310 | SET_CFEIE(base, ctx, 1); |
| 311 | |
| 312 | /* Stall access on a context fault and let the handler deal with it */ |
| 313 | SET_CFCFG(base, ctx, 1); |
| 314 | |
| 315 | /* Redirect all cacheable requests to L2 slave port. */ |
| 316 | SET_RCISH(base, ctx, 1); |
| 317 | SET_RCOSH(base, ctx, 1); |
| 318 | SET_RCNSH(base, ctx, 1); |
| 319 | |
| 320 | /* Turn on TEX Remap */ |
| 321 | SET_TRE(base, ctx, 1); |
| 322 | |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 323 | /* Set TEX remap attributes */ |
| 324 | RCP15_PRRR(prrr); |
| 325 | RCP15_NMRR(nmrr); |
| 326 | SET_PRRR(base, ctx, prrr); |
| 327 | SET_NMRR(base, ctx, nmrr); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 328 | |
| 329 | /* Turn on BFB prefetch */ |
| 330 | SET_BFBDFE(base, ctx, 1); |
| 331 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 332 | /* Configure page tables as inner-cacheable and shareable to reduce |
| 333 | * the TLB miss penalty. |
| 334 | */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 335 | if (redirect) { |
| 336 | SET_TTBR0_SH(base, ctx, 1); |
| 337 | SET_TTBR1_SH(base, ctx, 1); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 338 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 339 | SET_TTBR0_NOS(base, ctx, 1); |
| 340 | SET_TTBR1_NOS(base, ctx, 1); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 341 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 342 | SET_TTBR0_IRGNH(base, ctx, 0); /* WB, WA */ |
| 343 | SET_TTBR0_IRGNL(base, ctx, 1); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 344 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 345 | SET_TTBR1_IRGNH(base, ctx, 0); /* WB, WA */ |
| 346 | SET_TTBR1_IRGNL(base, ctx, 1); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 347 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 348 | SET_TTBR0_ORGN(base, ctx, 1); /* WB, WA */ |
| 349 | SET_TTBR1_ORGN(base, ctx, 1); /* WB, WA */ |
| 350 | } |
| 351 | |
| 352 | /* Find if this page table is used elsewhere, and re-use ASID */ |
| 353 | found = 0; |
| 354 | for (i = 0; i < ncb; i++) |
| 355 | if (GET_TTBR0_PA(base, i) == (pgtable >> TTBR0_PA_SHIFT) && |
| 356 | i != ctx) { |
| 357 | SET_CONTEXTIDR_ASID(base, ctx, \ |
| 358 | GET_CONTEXTIDR_ASID(base, i)); |
| 359 | found = 1; |
| 360 | break; |
| 361 | } |
| 362 | |
| 363 | /* If page table is new, find an unused ASID */ |
| 364 | if (!found) { |
| 365 | for (i = 0; i < ncb; i++) { |
| 366 | found = 0; |
| 367 | for (j = 0; j < ncb; j++) { |
| 368 | if (GET_CONTEXTIDR_ASID(base, j) == i && |
| 369 | j != ctx) |
| 370 | found = 1; |
| 371 | } |
| 372 | |
| 373 | if (!found) { |
| 374 | SET_CONTEXTIDR_ASID(base, ctx, i); |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | BUG_ON(found); |
| 379 | } |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 380 | |
| 381 | /* Enable the MMU */ |
| 382 | SET_M(base, ctx, 1); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 383 | mb(); |
Olav Haugan | 65209cd | 2012-11-07 15:02:56 -0800 | [diff] [blame] | 384 | |
| 385 | msm_iommu_remote_spin_unlock(); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 388 | static int msm_iommu_domain_init(struct iommu_domain *domain, int flags) |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 389 | { |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 390 | struct msm_iommu_priv *priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 391 | |
| 392 | if (!priv) |
| 393 | goto fail_nomem; |
| 394 | |
| 395 | INIT_LIST_HEAD(&priv->list_attached); |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 396 | priv->pt.fl_table = (unsigned long *)__get_free_pages(GFP_KERNEL, |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 397 | get_order(SZ_16K)); |
| 398 | |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 399 | if (!priv->pt.fl_table) |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 400 | goto fail_nomem; |
| 401 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 402 | #ifdef CONFIG_IOMMU_PGTABLES_L2 |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 403 | priv->pt.redirect = flags & MSM_IOMMU_DOMAIN_PT_CACHEABLE; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 404 | #endif |
| 405 | |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 406 | memset(priv->pt.fl_table, 0, SZ_16K); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 407 | domain->priv = priv; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 408 | |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 409 | clean_pte(priv->pt.fl_table, priv->pt.fl_table + NUM_FL_PTE, |
| 410 | priv->pt.redirect); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 411 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 412 | return 0; |
| 413 | |
| 414 | fail_nomem: |
| 415 | kfree(priv); |
| 416 | return -ENOMEM; |
| 417 | } |
| 418 | |
| 419 | static void msm_iommu_domain_destroy(struct iommu_domain *domain) |
| 420 | { |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 421 | struct msm_iommu_priv *priv; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 422 | unsigned long *fl_table; |
| 423 | int i; |
| 424 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 425 | mutex_lock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 426 | priv = domain->priv; |
| 427 | domain->priv = NULL; |
| 428 | |
| 429 | if (priv) { |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 430 | fl_table = priv->pt.fl_table; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 431 | |
| 432 | for (i = 0; i < NUM_FL_PTE; i++) |
| 433 | if ((fl_table[i] & 0x03) == FL_TYPE_TABLE) |
| 434 | free_page((unsigned long) __va(((fl_table[i]) & |
| 435 | FL_BASE_MASK))); |
| 436 | |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 437 | free_pages((unsigned long)priv->pt.fl_table, get_order(SZ_16K)); |
| 438 | priv->pt.fl_table = NULL; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | kfree(priv); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 442 | mutex_unlock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev) |
| 446 | { |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 447 | struct msm_iommu_priv *priv; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 448 | struct msm_iommu_drvdata *iommu_drvdata; |
| 449 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 450 | struct msm_iommu_ctx_drvdata *tmp_drvdata; |
| 451 | int ret = 0; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 452 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 453 | mutex_lock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 454 | |
| 455 | priv = domain->priv; |
| 456 | |
| 457 | if (!priv || !dev) { |
| 458 | ret = -EINVAL; |
Olav Haugan | e99ee7e | 2012-12-11 15:02:02 -0800 | [diff] [blame] | 459 | goto unlock; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | iommu_drvdata = dev_get_drvdata(dev->parent); |
| 463 | ctx_drvdata = dev_get_drvdata(dev); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 464 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 465 | if (!iommu_drvdata || !ctx_drvdata) { |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 466 | ret = -EINVAL; |
Olav Haugan | e99ee7e | 2012-12-11 15:02:02 -0800 | [diff] [blame] | 467 | goto unlock; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Olav Haugan | e99ee7e | 2012-12-11 15:02:02 -0800 | [diff] [blame] | 470 | ++ctx_drvdata->attach_count; |
| 471 | |
| 472 | if (ctx_drvdata->attach_count > 1) |
| 473 | goto unlock; |
| 474 | |
Stepan Moskovchenko | 00d4b2b | 2010-11-12 19:29:56 -0800 | [diff] [blame] | 475 | if (!list_empty(&ctx_drvdata->attached_elm)) { |
| 476 | ret = -EBUSY; |
Olav Haugan | e99ee7e | 2012-12-11 15:02:02 -0800 | [diff] [blame] | 477 | goto unlock; |
Stepan Moskovchenko | 00d4b2b | 2010-11-12 19:29:56 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 480 | list_for_each_entry(tmp_drvdata, &priv->list_attached, attached_elm) |
| 481 | if (tmp_drvdata == ctx_drvdata) { |
| 482 | ret = -EBUSY; |
Olav Haugan | e99ee7e | 2012-12-11 15:02:02 -0800 | [diff] [blame] | 483 | goto unlock; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 486 | ret = __enable_clocks(iommu_drvdata); |
| 487 | if (ret) |
Olav Haugan | e99ee7e | 2012-12-11 15:02:02 -0800 | [diff] [blame] | 488 | goto unlock; |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 489 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 490 | __program_context(iommu_drvdata->base, iommu_drvdata->glb_base, |
| 491 | ctx_drvdata->num, iommu_drvdata->ncb, |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 492 | __pa(priv->pt.fl_table), priv->pt.redirect, |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 493 | iommu_drvdata->ttbr_split); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 494 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 495 | __disable_clocks(iommu_drvdata); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 496 | list_add(&(ctx_drvdata->attached_elm), &priv->list_attached); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 497 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 498 | ctx_drvdata->attached_domain = domain; |
Olav Haugan | c599314 | 2013-02-04 13:59:39 -0800 | [diff] [blame] | 499 | |
| 500 | mutex_unlock(&msm_iommu_lock); |
| 501 | |
| 502 | msm_iommu_attached(dev->parent); |
| 503 | return ret; |
Olav Haugan | e99ee7e | 2012-12-11 15:02:02 -0800 | [diff] [blame] | 504 | unlock: |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 505 | mutex_unlock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | static void msm_iommu_detach_dev(struct iommu_domain *domain, |
| 510 | struct device *dev) |
| 511 | { |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 512 | struct msm_iommu_priv *priv; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 513 | struct msm_iommu_drvdata *iommu_drvdata; |
| 514 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
Stepan Moskovchenko | 3306973 | 2010-11-12 19:30:00 -0800 | [diff] [blame] | 515 | int ret; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 516 | |
Olav Haugan | c599314 | 2013-02-04 13:59:39 -0800 | [diff] [blame] | 517 | msm_iommu_detached(dev->parent); |
| 518 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 519 | mutex_lock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 520 | priv = domain->priv; |
| 521 | |
| 522 | if (!priv || !dev) |
Olav Haugan | e99ee7e | 2012-12-11 15:02:02 -0800 | [diff] [blame] | 523 | goto unlock; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 524 | |
| 525 | iommu_drvdata = dev_get_drvdata(dev->parent); |
| 526 | ctx_drvdata = dev_get_drvdata(dev); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 527 | |
Olav Haugan | 35deadc | 2012-12-10 18:28:27 -0800 | [diff] [blame] | 528 | if (!iommu_drvdata || !ctx_drvdata) |
Olav Haugan | e99ee7e | 2012-12-11 15:02:02 -0800 | [diff] [blame] | 529 | goto unlock; |
| 530 | |
| 531 | --ctx_drvdata->attach_count; |
| 532 | BUG_ON(ctx_drvdata->attach_count < 0); |
| 533 | |
| 534 | if (ctx_drvdata->attach_count > 0) |
| 535 | goto unlock; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 536 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 537 | ret = __enable_clocks(iommu_drvdata); |
| 538 | if (ret) |
Olav Haugan | e99ee7e | 2012-12-11 15:02:02 -0800 | [diff] [blame] | 539 | goto unlock; |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 540 | |
Olav Haugan | 65209cd | 2012-11-07 15:02:56 -0800 | [diff] [blame] | 541 | msm_iommu_remote_spin_lock(); |
| 542 | |
Olav Haugan | 35deadc | 2012-12-10 18:28:27 -0800 | [diff] [blame] | 543 | SET_TLBIASID(iommu_drvdata->base, ctx_drvdata->num, |
| 544 | GET_CONTEXTIDR_ASID(iommu_drvdata->base, ctx_drvdata->num)); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 545 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 546 | __reset_context(iommu_drvdata->base, iommu_drvdata->glb_base, |
Olav Haugan | 35deadc | 2012-12-10 18:28:27 -0800 | [diff] [blame] | 547 | ctx_drvdata->num); |
Olav Haugan | 65209cd | 2012-11-07 15:02:56 -0800 | [diff] [blame] | 548 | |
| 549 | msm_iommu_remote_spin_unlock(); |
| 550 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 551 | __disable_clocks(iommu_drvdata); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 552 | list_del_init(&ctx_drvdata->attached_elm); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 553 | ctx_drvdata->attached_domain = NULL; |
Olav Haugan | e99ee7e | 2012-12-11 15:02:02 -0800 | [diff] [blame] | 554 | unlock: |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 555 | mutex_unlock(&msm_iommu_lock); |
| 556 | } |
| 557 | |
| 558 | static int __get_pgprot(int prot, int len) |
| 559 | { |
| 560 | unsigned int pgprot; |
| 561 | int tex; |
| 562 | |
| 563 | if (!(prot & (IOMMU_READ | IOMMU_WRITE))) { |
| 564 | prot |= IOMMU_READ | IOMMU_WRITE; |
| 565 | WARN_ONCE(1, "No attributes in iommu mapping; assuming RW\n"); |
| 566 | } |
| 567 | |
| 568 | if ((prot & IOMMU_WRITE) && !(prot & IOMMU_READ)) { |
| 569 | prot |= IOMMU_READ; |
| 570 | WARN_ONCE(1, "Write-only iommu mappings unsupported; falling back to RW\n"); |
| 571 | } |
| 572 | |
| 573 | if (prot & IOMMU_CACHE) |
| 574 | tex = (pgprot_kernel >> 2) & 0x07; |
| 575 | else |
| 576 | tex = msm_iommu_tex_class[MSM_IOMMU_ATTR_NONCACHED]; |
| 577 | |
| 578 | if (tex < 0 || tex > NUM_TEX_CLASS - 1) |
| 579 | return 0; |
| 580 | |
| 581 | if (len == SZ_16M || len == SZ_1M) { |
| 582 | pgprot = FL_SHARED; |
| 583 | pgprot |= tex & 0x01 ? FL_BUFFERABLE : 0; |
| 584 | pgprot |= tex & 0x02 ? FL_CACHEABLE : 0; |
| 585 | pgprot |= tex & 0x04 ? FL_TEX0 : 0; |
| 586 | pgprot |= FL_AP0 | FL_AP1; |
| 587 | pgprot |= prot & IOMMU_WRITE ? 0 : FL_AP2; |
| 588 | } else { |
| 589 | pgprot = SL_SHARED; |
| 590 | pgprot |= tex & 0x01 ? SL_BUFFERABLE : 0; |
| 591 | pgprot |= tex & 0x02 ? SL_CACHEABLE : 0; |
| 592 | pgprot |= tex & 0x04 ? SL_TEX0 : 0; |
| 593 | pgprot |= SL_AP0 | SL_AP1; |
| 594 | pgprot |= prot & IOMMU_WRITE ? 0 : SL_AP2; |
| 595 | } |
| 596 | |
| 597 | return pgprot; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 600 | static unsigned long *make_second_level(struct msm_iommu_priv *priv, |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 601 | unsigned long *fl_pte) |
| 602 | { |
| 603 | unsigned long *sl; |
| 604 | sl = (unsigned long *) __get_free_pages(GFP_KERNEL, |
| 605 | get_order(SZ_4K)); |
| 606 | |
| 607 | if (!sl) { |
| 608 | pr_debug("Could not allocate second level table\n"); |
| 609 | goto fail; |
| 610 | } |
| 611 | memset(sl, 0, SZ_4K); |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 612 | clean_pte(sl, sl + NUM_SL_PTE, priv->pt.redirect); |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 613 | |
| 614 | *fl_pte = ((((int)__pa(sl)) & FL_BASE_MASK) | \ |
| 615 | FL_TYPE_TABLE); |
| 616 | |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 617 | clean_pte(fl_pte, fl_pte + 1, priv->pt.redirect); |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 618 | fail: |
| 619 | return sl; |
| 620 | } |
| 621 | |
| 622 | static int sl_4k(unsigned long *sl_pte, phys_addr_t pa, unsigned int pgprot) |
| 623 | { |
| 624 | int ret = 0; |
| 625 | |
| 626 | if (*sl_pte) { |
| 627 | ret = -EBUSY; |
| 628 | goto fail; |
| 629 | } |
| 630 | |
| 631 | *sl_pte = (pa & SL_BASE_MASK_SMALL) | SL_NG | SL_SHARED |
| 632 | | SL_TYPE_SMALL | pgprot; |
| 633 | fail: |
| 634 | return ret; |
| 635 | } |
| 636 | |
| 637 | static int sl_64k(unsigned long *sl_pte, phys_addr_t pa, unsigned int pgprot) |
| 638 | { |
| 639 | int ret = 0; |
| 640 | |
| 641 | int i; |
| 642 | |
| 643 | for (i = 0; i < 16; i++) |
| 644 | if (*(sl_pte+i)) { |
| 645 | ret = -EBUSY; |
| 646 | goto fail; |
| 647 | } |
| 648 | |
| 649 | for (i = 0; i < 16; i++) |
| 650 | *(sl_pte+i) = (pa & SL_BASE_MASK_LARGE) | SL_NG |
| 651 | | SL_SHARED | SL_TYPE_LARGE | pgprot; |
| 652 | |
| 653 | fail: |
| 654 | return ret; |
| 655 | } |
| 656 | |
| 657 | |
| 658 | static inline int fl_1m(unsigned long *fl_pte, phys_addr_t pa, int pgprot) |
| 659 | { |
| 660 | if (*fl_pte) |
| 661 | return -EBUSY; |
| 662 | |
| 663 | *fl_pte = (pa & 0xFFF00000) | FL_NG | FL_TYPE_SECT | FL_SHARED |
| 664 | | pgprot; |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | |
| 670 | static inline int fl_16m(unsigned long *fl_pte, phys_addr_t pa, int pgprot) |
| 671 | { |
| 672 | int i; |
| 673 | int ret = 0; |
| 674 | for (i = 0; i < 16; i++) |
| 675 | if (*(fl_pte+i)) { |
| 676 | ret = -EBUSY; |
| 677 | goto fail; |
| 678 | } |
| 679 | for (i = 0; i < 16; i++) |
| 680 | *(fl_pte+i) = (pa & 0xFF000000) | FL_SUPERSECTION |
| 681 | | FL_TYPE_SECT | FL_SHARED | FL_NG | pgprot; |
| 682 | fail: |
| 683 | return ret; |
| 684 | } |
| 685 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 686 | static int msm_iommu_map(struct iommu_domain *domain, unsigned long va, |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 687 | phys_addr_t pa, size_t len, int prot) |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 688 | { |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 689 | struct msm_iommu_priv *priv; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 690 | unsigned long *fl_table; |
| 691 | unsigned long *fl_pte; |
| 692 | unsigned long fl_offset; |
| 693 | unsigned long *sl_table; |
| 694 | unsigned long *sl_pte; |
| 695 | unsigned long sl_offset; |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 696 | unsigned int pgprot; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 697 | int ret = 0; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 698 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 699 | mutex_lock(&msm_iommu_lock); |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 700 | |
| 701 | priv = domain->priv; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 702 | if (!priv) { |
| 703 | ret = -EINVAL; |
| 704 | goto fail; |
| 705 | } |
| 706 | |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 707 | fl_table = priv->pt.fl_table; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 708 | |
| 709 | if (len != SZ_16M && len != SZ_1M && |
| 710 | len != SZ_64K && len != SZ_4K) { |
| 711 | pr_debug("Bad size: %d\n", len); |
| 712 | ret = -EINVAL; |
| 713 | goto fail; |
| 714 | } |
| 715 | |
| 716 | if (!fl_table) { |
| 717 | pr_debug("Null page table\n"); |
| 718 | ret = -EINVAL; |
| 719 | goto fail; |
| 720 | } |
| 721 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 722 | pgprot = __get_pgprot(prot, len); |
| 723 | |
| 724 | if (!pgprot) { |
| 725 | ret = -EINVAL; |
| 726 | goto fail; |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 727 | } |
| 728 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 729 | fl_offset = FL_OFFSET(va); /* Upper 12 bits */ |
| 730 | fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */ |
| 731 | |
| 732 | if (len == SZ_16M) { |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 733 | ret = fl_16m(fl_pte, pa, pgprot); |
| 734 | if (ret) |
| 735 | goto fail; |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 736 | clean_pte(fl_pte, fl_pte + 16, priv->pt.redirect); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 739 | if (len == SZ_1M) { |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 740 | ret = fl_1m(fl_pte, pa, pgprot); |
| 741 | if (ret) |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 742 | goto fail; |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 743 | clean_pte(fl_pte, fl_pte + 1, priv->pt.redirect); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* Need a 2nd level table */ |
| 747 | if (len == SZ_4K || len == SZ_64K) { |
| 748 | |
| 749 | if (*fl_pte == 0) { |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 750 | if (make_second_level(priv, fl_pte) == NULL) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 751 | ret = -ENOMEM; |
| 752 | goto fail; |
| 753 | } |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | if (!(*fl_pte & FL_TYPE_TABLE)) { |
| 757 | ret = -EBUSY; |
| 758 | goto fail; |
| 759 | } |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK)); |
| 763 | sl_offset = SL_OFFSET(va); |
| 764 | sl_pte = sl_table + sl_offset; |
| 765 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 766 | if (len == SZ_4K) { |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 767 | ret = sl_4k(sl_pte, pa, pgprot); |
| 768 | if (ret) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 769 | goto fail; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 770 | |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 771 | clean_pte(sl_pte, sl_pte + 1, priv->pt.redirect); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 772 | } |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 773 | |
| 774 | if (len == SZ_64K) { |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 775 | ret = sl_64k(sl_pte, pa, pgprot); |
| 776 | if (ret) |
| 777 | goto fail; |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 778 | clean_pte(sl_pte, sl_pte + 16, priv->pt.redirect); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 781 | ret = __flush_iotlb_va(domain, va); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 782 | fail: |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 783 | mutex_unlock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 784 | return ret; |
| 785 | } |
| 786 | |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 787 | static size_t msm_iommu_unmap(struct iommu_domain *domain, unsigned long va, |
| 788 | size_t len) |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 789 | { |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 790 | struct msm_iommu_priv *priv; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 791 | unsigned long *fl_table; |
| 792 | unsigned long *fl_pte; |
| 793 | unsigned long fl_offset; |
| 794 | unsigned long *sl_table; |
| 795 | unsigned long *sl_pte; |
| 796 | unsigned long sl_offset; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 797 | int i, ret = 0; |
| 798 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 799 | mutex_lock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 800 | |
| 801 | priv = domain->priv; |
| 802 | |
Joerg Roedel | 05df1f3 | 2012-01-26 18:25:37 +0100 | [diff] [blame] | 803 | if (!priv) |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 804 | goto fail; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 805 | |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 806 | fl_table = priv->pt.fl_table; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 807 | |
| 808 | if (len != SZ_16M && len != SZ_1M && |
| 809 | len != SZ_64K && len != SZ_4K) { |
| 810 | pr_debug("Bad length: %d\n", len); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 811 | goto fail; |
| 812 | } |
| 813 | |
| 814 | if (!fl_table) { |
| 815 | pr_debug("Null page table\n"); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 816 | goto fail; |
| 817 | } |
| 818 | |
| 819 | fl_offset = FL_OFFSET(va); /* Upper 12 bits */ |
| 820 | fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */ |
| 821 | |
| 822 | if (*fl_pte == 0) { |
| 823 | pr_debug("First level PTE is 0\n"); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 824 | goto fail; |
| 825 | } |
| 826 | |
| 827 | /* Unmap supersection */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 828 | if (len == SZ_16M) { |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 829 | for (i = 0; i < 16; i++) |
| 830 | *(fl_pte+i) = 0; |
| 831 | |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 832 | clean_pte(fl_pte, fl_pte + 16, priv->pt.redirect); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | if (len == SZ_1M) { |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 836 | *fl_pte = 0; |
| 837 | |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 838 | clean_pte(fl_pte, fl_pte + 1, priv->pt.redirect); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 841 | sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK)); |
| 842 | sl_offset = SL_OFFSET(va); |
| 843 | sl_pte = sl_table + sl_offset; |
| 844 | |
| 845 | if (len == SZ_64K) { |
| 846 | for (i = 0; i < 16; i++) |
| 847 | *(sl_pte+i) = 0; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 848 | |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 849 | clean_pte(sl_pte, sl_pte + 16, priv->pt.redirect); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 852 | if (len == SZ_4K) { |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 853 | *sl_pte = 0; |
| 854 | |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 855 | clean_pte(sl_pte, sl_pte + 1, priv->pt.redirect); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 858 | if (len == SZ_4K || len == SZ_64K) { |
| 859 | int used = 0; |
| 860 | |
| 861 | for (i = 0; i < NUM_SL_PTE; i++) |
| 862 | if (sl_table[i]) |
| 863 | used = 1; |
| 864 | if (!used) { |
| 865 | free_page((unsigned long)sl_table); |
| 866 | *fl_pte = 0; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 867 | |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 868 | clean_pte(fl_pte, fl_pte + 1, priv->pt.redirect); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 872 | ret = __flush_iotlb_va(domain, va); |
Ohad Ben-Cohen | 9e28547 | 2011-09-02 13:32:34 -0400 | [diff] [blame] | 873 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 874 | fail: |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 875 | mutex_unlock(&msm_iommu_lock); |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 876 | |
| 877 | /* the IOMMU API requires us to return how many bytes were unmapped */ |
| 878 | len = ret ? 0 : len; |
| 879 | return len; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 882 | static unsigned int get_phys_addr(struct scatterlist *sg) |
| 883 | { |
| 884 | /* |
| 885 | * Try sg_dma_address first so that we can |
| 886 | * map carveout regions that do not have a |
| 887 | * struct page associated with them. |
| 888 | */ |
| 889 | unsigned int pa = sg_dma_address(sg); |
| 890 | if (pa == 0) |
| 891 | pa = sg_phys(sg); |
| 892 | return pa; |
| 893 | } |
| 894 | |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 895 | static inline int is_fully_aligned(unsigned int va, phys_addr_t pa, size_t len, |
| 896 | int align) |
| 897 | { |
| 898 | return IS_ALIGNED(va, align) && IS_ALIGNED(pa, align) |
| 899 | && (len >= align); |
| 900 | } |
| 901 | |
Jeremy Gebben | 8c5e2f7 | 2012-10-05 14:03:45 -0600 | [diff] [blame] | 902 | static int check_range(unsigned long *fl_table, unsigned int va, |
| 903 | unsigned int len) |
| 904 | { |
| 905 | unsigned int offset = 0; |
| 906 | unsigned long *fl_pte; |
| 907 | unsigned long fl_offset; |
| 908 | unsigned long *sl_table; |
| 909 | unsigned long sl_start, sl_end; |
| 910 | int i; |
| 911 | |
| 912 | fl_offset = FL_OFFSET(va); /* Upper 12 bits */ |
| 913 | fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */ |
| 914 | |
| 915 | while (offset < len) { |
| 916 | if (*fl_pte & FL_TYPE_TABLE) { |
| 917 | sl_start = SL_OFFSET(va); |
| 918 | sl_table = __va(((*fl_pte) & FL_BASE_MASK)); |
| 919 | sl_end = ((len - offset) / SZ_4K) + sl_start; |
| 920 | |
| 921 | if (sl_end > NUM_SL_PTE) |
| 922 | sl_end = NUM_SL_PTE; |
| 923 | |
| 924 | for (i = sl_start; i < sl_end; i++) { |
| 925 | if (sl_table[i] != 0) { |
| 926 | pr_err("%08x - %08x already mapped\n", |
| 927 | va, va + SZ_4K); |
| 928 | return -EBUSY; |
| 929 | } |
| 930 | offset += SZ_4K; |
| 931 | va += SZ_4K; |
| 932 | } |
| 933 | |
| 934 | |
| 935 | sl_start = 0; |
| 936 | } else { |
| 937 | if (*fl_pte != 0) { |
| 938 | pr_err("%08x - %08x already mapped\n", |
| 939 | va, va + SZ_1M); |
| 940 | return -EBUSY; |
| 941 | } |
| 942 | va += SZ_1M; |
| 943 | offset += SZ_1M; |
| 944 | sl_start = 0; |
| 945 | } |
| 946 | fl_pte++; |
| 947 | } |
| 948 | return 0; |
| 949 | } |
| 950 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 951 | static int msm_iommu_map_range(struct iommu_domain *domain, unsigned int va, |
| 952 | struct scatterlist *sg, unsigned int len, |
| 953 | int prot) |
| 954 | { |
| 955 | unsigned int pa; |
| 956 | unsigned int offset = 0; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 957 | unsigned long *fl_table; |
| 958 | unsigned long *fl_pte; |
| 959 | unsigned long fl_offset; |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 960 | unsigned long *sl_table = NULL; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 961 | unsigned long sl_offset, sl_start; |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 962 | unsigned int chunk_size, chunk_offset = 0; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 963 | int ret = 0; |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 964 | struct msm_iommu_priv *priv; |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 965 | unsigned int pgprot4k, pgprot64k, pgprot1m, pgprot16m; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 966 | |
| 967 | mutex_lock(&msm_iommu_lock); |
| 968 | |
| 969 | BUG_ON(len & (SZ_4K - 1)); |
| 970 | |
| 971 | priv = domain->priv; |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 972 | fl_table = priv->pt.fl_table; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 973 | |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 974 | pgprot4k = __get_pgprot(prot, SZ_4K); |
| 975 | pgprot64k = __get_pgprot(prot, SZ_64K); |
| 976 | pgprot1m = __get_pgprot(prot, SZ_1M); |
| 977 | pgprot16m = __get_pgprot(prot, SZ_16M); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 978 | |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 979 | if (!pgprot4k || !pgprot64k || !pgprot1m || !pgprot16m) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 980 | ret = -EINVAL; |
| 981 | goto fail; |
| 982 | } |
Jeremy Gebben | 8c5e2f7 | 2012-10-05 14:03:45 -0600 | [diff] [blame] | 983 | ret = check_range(fl_table, va, len); |
| 984 | if (ret) |
| 985 | goto fail; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 986 | |
| 987 | fl_offset = FL_OFFSET(va); /* Upper 12 bits */ |
| 988 | fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */ |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 989 | pa = get_phys_addr(sg); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 990 | |
| 991 | while (offset < len) { |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 992 | chunk_size = SZ_4K; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 993 | |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 994 | if (is_fully_aligned(va, pa, sg->length - chunk_offset, |
| 995 | SZ_16M)) |
| 996 | chunk_size = SZ_16M; |
| 997 | else if (is_fully_aligned(va, pa, sg->length - chunk_offset, |
| 998 | SZ_1M)) |
| 999 | chunk_size = SZ_1M; |
| 1000 | /* 64k or 4k determined later */ |
| 1001 | |
| 1002 | /* for 1M and 16M, only first level entries are required */ |
| 1003 | if (chunk_size >= SZ_1M) { |
| 1004 | if (chunk_size == SZ_16M) { |
| 1005 | ret = fl_16m(fl_pte, pa, pgprot16m); |
| 1006 | if (ret) |
| 1007 | goto fail; |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 1008 | clean_pte(fl_pte, fl_pte + 16, |
| 1009 | priv->pt.redirect); |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 1010 | fl_pte += 16; |
| 1011 | } else if (chunk_size == SZ_1M) { |
| 1012 | ret = fl_1m(fl_pte, pa, pgprot1m); |
| 1013 | if (ret) |
| 1014 | goto fail; |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 1015 | clean_pte(fl_pte, fl_pte + 1, |
| 1016 | priv->pt.redirect); |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 1017 | fl_pte++; |
| 1018 | } |
| 1019 | |
| 1020 | offset += chunk_size; |
| 1021 | chunk_offset += chunk_size; |
| 1022 | va += chunk_size; |
| 1023 | pa += chunk_size; |
| 1024 | |
| 1025 | if (chunk_offset >= sg->length && offset < len) { |
| 1026 | chunk_offset = 0; |
| 1027 | sg = sg_next(sg); |
| 1028 | pa = get_phys_addr(sg); |
| 1029 | if (pa == 0) { |
| 1030 | pr_debug("No dma address for sg %p\n", |
| 1031 | sg); |
| 1032 | ret = -EINVAL; |
| 1033 | goto fail; |
| 1034 | } |
| 1035 | } |
| 1036 | continue; |
| 1037 | } |
| 1038 | /* for 4K or 64K, make sure there is a second level table */ |
| 1039 | if (*fl_pte == 0) { |
| 1040 | if (!make_second_level(priv, fl_pte)) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1041 | ret = -ENOMEM; |
| 1042 | goto fail; |
| 1043 | } |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 1044 | } |
| 1045 | if (!(*fl_pte & FL_TYPE_TABLE)) { |
| 1046 | ret = -EBUSY; |
| 1047 | goto fail; |
| 1048 | } |
| 1049 | sl_table = __va(((*fl_pte) & FL_BASE_MASK)); |
| 1050 | sl_offset = SL_OFFSET(va); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1051 | /* Keep track of initial position so we |
| 1052 | * don't clean more than we have to |
| 1053 | */ |
| 1054 | sl_start = sl_offset; |
| 1055 | |
| 1056 | /* Build the 2nd level page table */ |
| 1057 | while (offset < len && sl_offset < NUM_SL_PTE) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1058 | |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 1059 | /* Map a large 64K page if the chunk is large enough and |
| 1060 | * the pa and va are aligned |
| 1061 | */ |
| 1062 | |
| 1063 | if (is_fully_aligned(va, pa, sg->length - chunk_offset, |
| 1064 | SZ_64K)) |
| 1065 | chunk_size = SZ_64K; |
| 1066 | else |
| 1067 | chunk_size = SZ_4K; |
| 1068 | |
| 1069 | if (chunk_size == SZ_4K) { |
| 1070 | sl_4k(&sl_table[sl_offset], pa, pgprot4k); |
| 1071 | sl_offset++; |
| 1072 | } else { |
| 1073 | BUG_ON(sl_offset + 16 > NUM_SL_PTE); |
| 1074 | sl_64k(&sl_table[sl_offset], pa, pgprot64k); |
| 1075 | sl_offset += 16; |
| 1076 | } |
| 1077 | |
| 1078 | |
| 1079 | offset += chunk_size; |
| 1080 | chunk_offset += chunk_size; |
| 1081 | va += chunk_size; |
| 1082 | pa += chunk_size; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1083 | |
| 1084 | if (chunk_offset >= sg->length && offset < len) { |
| 1085 | chunk_offset = 0; |
| 1086 | sg = sg_next(sg); |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 1087 | pa = get_phys_addr(sg); |
| 1088 | if (pa == 0) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1089 | pr_debug("No dma address for sg %p\n", |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 1090 | sg); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1091 | ret = -EINVAL; |
| 1092 | goto fail; |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | clean_pte(sl_table + sl_start, sl_table + sl_offset, |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 1098 | priv->pt.redirect); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1099 | |
| 1100 | fl_pte++; |
| 1101 | sl_offset = 0; |
| 1102 | } |
| 1103 | __flush_iotlb(domain); |
| 1104 | fail: |
| 1105 | mutex_unlock(&msm_iommu_lock); |
| 1106 | return ret; |
| 1107 | } |
| 1108 | |
| 1109 | |
| 1110 | static int msm_iommu_unmap_range(struct iommu_domain *domain, unsigned int va, |
| 1111 | unsigned int len) |
| 1112 | { |
| 1113 | unsigned int offset = 0; |
| 1114 | unsigned long *fl_table; |
| 1115 | unsigned long *fl_pte; |
| 1116 | unsigned long fl_offset; |
| 1117 | unsigned long *sl_table; |
| 1118 | unsigned long sl_start, sl_end; |
| 1119 | int used, i; |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 1120 | struct msm_iommu_priv *priv; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1121 | |
| 1122 | mutex_lock(&msm_iommu_lock); |
| 1123 | |
| 1124 | BUG_ON(len & (SZ_4K - 1)); |
| 1125 | |
| 1126 | priv = domain->priv; |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 1127 | fl_table = priv->pt.fl_table; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1128 | |
| 1129 | fl_offset = FL_OFFSET(va); /* Upper 12 bits */ |
| 1130 | fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */ |
| 1131 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1132 | while (offset < len) { |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 1133 | if (*fl_pte & FL_TYPE_TABLE) { |
| 1134 | sl_start = SL_OFFSET(va); |
| 1135 | sl_table = __va(((*fl_pte) & FL_BASE_MASK)); |
| 1136 | sl_end = ((len - offset) / SZ_4K) + sl_start; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1137 | |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 1138 | if (sl_end > NUM_SL_PTE) |
| 1139 | sl_end = NUM_SL_PTE; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1140 | |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 1141 | memset(sl_table + sl_start, 0, (sl_end - sl_start) * 4); |
| 1142 | clean_pte(sl_table + sl_start, sl_table + sl_end, |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 1143 | priv->pt.redirect); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1144 | |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 1145 | offset += (sl_end - sl_start) * SZ_4K; |
| 1146 | va += (sl_end - sl_start) * SZ_4K; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1147 | |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 1148 | /* Unmap and free the 2nd level table if all mappings |
| 1149 | * in it were removed. This saves memory, but the table |
| 1150 | * will need to be re-allocated the next time someone |
| 1151 | * tries to map these VAs. |
| 1152 | */ |
| 1153 | used = 0; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1154 | |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 1155 | /* If we just unmapped the whole table, don't bother |
| 1156 | * seeing if there are still used entries left. |
| 1157 | */ |
| 1158 | if (sl_end - sl_start != NUM_SL_PTE) |
| 1159 | for (i = 0; i < NUM_SL_PTE; i++) |
| 1160 | if (sl_table[i]) { |
| 1161 | used = 1; |
| 1162 | break; |
| 1163 | } |
| 1164 | if (!used) { |
| 1165 | free_page((unsigned long)sl_table); |
| 1166 | *fl_pte = 0; |
| 1167 | |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 1168 | clean_pte(fl_pte, fl_pte + 1, |
| 1169 | priv->pt.redirect); |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | sl_start = 0; |
| 1173 | } else { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1174 | *fl_pte = 0; |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 1175 | clean_pte(fl_pte, fl_pte + 1, priv->pt.redirect); |
Jordan Crouse | 8d8ee1a | 2012-07-09 13:27:07 -0600 | [diff] [blame] | 1176 | va += SZ_1M; |
| 1177 | offset += SZ_1M; |
| 1178 | sl_start = 0; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1179 | } |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1180 | fl_pte++; |
| 1181 | } |
| 1182 | |
| 1183 | __flush_iotlb(domain); |
| 1184 | mutex_unlock(&msm_iommu_lock); |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1188 | static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain, |
| 1189 | unsigned long va) |
| 1190 | { |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 1191 | struct msm_iommu_priv *priv; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1192 | struct msm_iommu_drvdata *iommu_drvdata; |
| 1193 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 1194 | unsigned int par; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1195 | void __iomem *base; |
| 1196 | phys_addr_t ret = 0; |
| 1197 | int ctx; |
| 1198 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1199 | mutex_lock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1200 | |
| 1201 | priv = domain->priv; |
| 1202 | if (list_empty(&priv->list_attached)) |
| 1203 | goto fail; |
| 1204 | |
| 1205 | ctx_drvdata = list_entry(priv->list_attached.next, |
| 1206 | struct msm_iommu_ctx_drvdata, attached_elm); |
| 1207 | iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent); |
| 1208 | |
| 1209 | base = iommu_drvdata->base; |
| 1210 | ctx = ctx_drvdata->num; |
| 1211 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 1212 | ret = __enable_clocks(iommu_drvdata); |
| 1213 | if (ret) |
| 1214 | goto fail; |
| 1215 | |
Olav Haugan | 65209cd | 2012-11-07 15:02:56 -0800 | [diff] [blame] | 1216 | msm_iommu_remote_spin_lock(); |
| 1217 | |
Stepan Moskovchenko | b0e7808 | 2011-02-28 16:04:55 -0800 | [diff] [blame] | 1218 | SET_V2PPR(base, ctx, va & V2Pxx_VA); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1219 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1220 | mb(); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1221 | par = GET_PAR(base, ctx); |
| 1222 | |
| 1223 | /* We are dealing with a supersection */ |
| 1224 | if (GET_NOFAULT_SS(base, ctx)) |
| 1225 | ret = (par & 0xFF000000) | (va & 0x00FFFFFF); |
| 1226 | else /* Upper 20 bits from PAR, lower 12 from VA */ |
| 1227 | ret = (par & 0xFFFFF000) | (va & 0x00000FFF); |
| 1228 | |
Stepan Moskovchenko | 3306973 | 2010-11-12 19:30:00 -0800 | [diff] [blame] | 1229 | if (GET_FAULT(base, ctx)) |
| 1230 | ret = 0; |
| 1231 | |
Olav Haugan | 65209cd | 2012-11-07 15:02:56 -0800 | [diff] [blame] | 1232 | msm_iommu_remote_spin_unlock(); |
| 1233 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 1234 | __disable_clocks(iommu_drvdata); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1235 | fail: |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1236 | mutex_unlock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1237 | return ret; |
| 1238 | } |
| 1239 | |
| 1240 | static int msm_iommu_domain_has_cap(struct iommu_domain *domain, |
| 1241 | unsigned long cap) |
| 1242 | { |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | static void print_ctx_regs(void __iomem *base, int ctx) |
| 1247 | { |
| 1248 | unsigned int fsr = GET_FSR(base, ctx); |
| 1249 | pr_err("FAR = %08x PAR = %08x\n", |
| 1250 | GET_FAR(base, ctx), GET_PAR(base, ctx)); |
| 1251 | pr_err("FSR = %08x [%s%s%s%s%s%s%s%s%s%s]\n", fsr, |
| 1252 | (fsr & 0x02) ? "TF " : "", |
| 1253 | (fsr & 0x04) ? "AFF " : "", |
| 1254 | (fsr & 0x08) ? "APF " : "", |
| 1255 | (fsr & 0x10) ? "TLBMF " : "", |
| 1256 | (fsr & 0x20) ? "HTWDEEF " : "", |
| 1257 | (fsr & 0x40) ? "HTWSEEF " : "", |
| 1258 | (fsr & 0x80) ? "MHF " : "", |
| 1259 | (fsr & 0x10000) ? "SL " : "", |
| 1260 | (fsr & 0x40000000) ? "SS " : "", |
| 1261 | (fsr & 0x80000000) ? "MULTI " : ""); |
| 1262 | |
| 1263 | pr_err("FSYNR0 = %08x FSYNR1 = %08x\n", |
| 1264 | GET_FSYNR0(base, ctx), GET_FSYNR1(base, ctx)); |
| 1265 | pr_err("TTBR0 = %08x TTBR1 = %08x\n", |
| 1266 | GET_TTBR0(base, ctx), GET_TTBR1(base, ctx)); |
| 1267 | pr_err("SCTLR = %08x ACTLR = %08x\n", |
| 1268 | GET_SCTLR(base, ctx), GET_ACTLR(base, ctx)); |
| 1269 | pr_err("PRRR = %08x NMRR = %08x\n", |
| 1270 | GET_PRRR(base, ctx), GET_NMRR(base, ctx)); |
| 1271 | } |
| 1272 | |
| 1273 | irqreturn_t msm_iommu_fault_handler(int irq, void *dev_id) |
| 1274 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1275 | struct msm_iommu_ctx_drvdata *ctx_drvdata = dev_id; |
| 1276 | struct msm_iommu_drvdata *drvdata; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1277 | void __iomem *base; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1278 | unsigned int fsr, num; |
| 1279 | int ret; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1280 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1281 | mutex_lock(&msm_iommu_lock); |
| 1282 | BUG_ON(!ctx_drvdata); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1283 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1284 | drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent); |
| 1285 | BUG_ON(!drvdata); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1286 | |
| 1287 | base = drvdata->base; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1288 | num = ctx_drvdata->num; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1289 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 1290 | ret = __enable_clocks(drvdata); |
| 1291 | if (ret) |
| 1292 | goto fail; |
| 1293 | |
Olav Haugan | 65209cd | 2012-11-07 15:02:56 -0800 | [diff] [blame] | 1294 | msm_iommu_remote_spin_lock(); |
| 1295 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1296 | fsr = GET_FSR(base, num); |
| 1297 | |
| 1298 | if (fsr) { |
| 1299 | if (!ctx_drvdata->attached_domain) { |
| 1300 | pr_err("Bad domain in interrupt handler\n"); |
| 1301 | ret = -ENOSYS; |
| 1302 | } else |
| 1303 | ret = report_iommu_fault(ctx_drvdata->attached_domain, |
| 1304 | &ctx_drvdata->pdev->dev, |
| 1305 | GET_FAR(base, num), 0); |
| 1306 | |
| 1307 | if (ret == -ENOSYS) { |
| 1308 | pr_err("Unexpected IOMMU page fault!\n"); |
| 1309 | pr_err("name = %s\n", drvdata->name); |
| 1310 | pr_err("context = %s (%d)\n", ctx_drvdata->name, num); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1311 | pr_err("Interesting registers:\n"); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1312 | print_ctx_regs(base, num); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1313 | } |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1314 | |
| 1315 | SET_FSR(base, num, fsr); |
Shubhraprakash Das | 52f50c4 | 2012-10-09 16:14:28 -0700 | [diff] [blame] | 1316 | /* |
| 1317 | * Only resume fetches if the registered fault handler |
| 1318 | * allows it |
| 1319 | */ |
| 1320 | if (ret != -EBUSY) |
| 1321 | SET_RESUME(base, num, 1); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1322 | |
| 1323 | ret = IRQ_HANDLED; |
| 1324 | } else |
| 1325 | ret = IRQ_NONE; |
| 1326 | |
Olav Haugan | 65209cd | 2012-11-07 15:02:56 -0800 | [diff] [blame] | 1327 | msm_iommu_remote_spin_unlock(); |
| 1328 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 1329 | __disable_clocks(drvdata); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1330 | fail: |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1331 | mutex_unlock(&msm_iommu_lock); |
| 1332 | return ret; |
| 1333 | } |
| 1334 | |
| 1335 | static phys_addr_t msm_iommu_get_pt_base_addr(struct iommu_domain *domain) |
| 1336 | { |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 1337 | struct msm_iommu_priv *priv = domain->priv; |
| 1338 | return __pa(priv->pt.fl_table); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | static struct iommu_ops msm_iommu_ops = { |
| 1342 | .domain_init = msm_iommu_domain_init, |
| 1343 | .domain_destroy = msm_iommu_domain_destroy, |
| 1344 | .attach_dev = msm_iommu_attach_dev, |
| 1345 | .detach_dev = msm_iommu_detach_dev, |
| 1346 | .map = msm_iommu_map, |
| 1347 | .unmap = msm_iommu_unmap, |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1348 | .map_range = msm_iommu_map_range, |
| 1349 | .unmap_range = msm_iommu_unmap_range, |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1350 | .iova_to_phys = msm_iommu_iova_to_phys, |
Ohad Ben-Cohen | 8342727 | 2011-11-10 11:32:28 +0200 | [diff] [blame] | 1351 | .domain_has_cap = msm_iommu_domain_has_cap, |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1352 | .get_pt_base_addr = msm_iommu_get_pt_base_addr, |
Ohad Ben-Cohen | 8342727 | 2011-11-10 11:32:28 +0200 | [diff] [blame] | 1353 | .pgsize_bitmap = MSM_IOMMU_PGSIZES, |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1354 | }; |
| 1355 | |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 1356 | static int __init get_tex_class(int icp, int ocp, int mt, int nos) |
| 1357 | { |
| 1358 | int i = 0; |
| 1359 | unsigned int prrr = 0; |
| 1360 | unsigned int nmrr = 0; |
| 1361 | int c_icp, c_ocp, c_mt, c_nos; |
| 1362 | |
| 1363 | RCP15_PRRR(prrr); |
| 1364 | RCP15_NMRR(nmrr); |
| 1365 | |
| 1366 | for (i = 0; i < NUM_TEX_CLASS; i++) { |
| 1367 | c_nos = PRRR_NOS(prrr, i); |
| 1368 | c_mt = PRRR_MT(prrr, i); |
| 1369 | c_icp = NMRR_ICP(nmrr, i); |
| 1370 | c_ocp = NMRR_OCP(nmrr, i); |
| 1371 | |
| 1372 | if (icp == c_icp && ocp == c_ocp && c_mt == mt && c_nos == nos) |
| 1373 | return i; |
| 1374 | } |
| 1375 | |
| 1376 | return -ENODEV; |
| 1377 | } |
| 1378 | |
| 1379 | static void __init setup_iommu_tex_classes(void) |
| 1380 | { |
| 1381 | msm_iommu_tex_class[MSM_IOMMU_ATTR_NONCACHED] = |
| 1382 | get_tex_class(CP_NONCACHED, CP_NONCACHED, MT_NORMAL, 1); |
| 1383 | |
| 1384 | msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WB_WA] = |
| 1385 | get_tex_class(CP_WB_WA, CP_WB_WA, MT_NORMAL, 1); |
| 1386 | |
| 1387 | msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WB_NWA] = |
| 1388 | get_tex_class(CP_WB_NWA, CP_WB_NWA, MT_NORMAL, 1); |
| 1389 | |
| 1390 | msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WT] = |
| 1391 | get_tex_class(CP_WT, CP_WT, MT_NORMAL, 1); |
| 1392 | } |
| 1393 | |
Stepan Moskovchenko | 516cbc7 | 2010-11-12 19:29:53 -0800 | [diff] [blame] | 1394 | static int __init msm_iommu_init(void) |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1395 | { |
Olav Haugan | 0e22c48 | 2013-01-28 17:39:36 -0800 | [diff] [blame] | 1396 | if (!msm_soc_version_supports_iommu_v0()) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1397 | return -ENODEV; |
| 1398 | |
Olav Haugan | 65209cd | 2012-11-07 15:02:56 -0800 | [diff] [blame] | 1399 | msm_iommu_lock_initialize(); |
| 1400 | |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 1401 | setup_iommu_tex_classes(); |
Joerg Roedel | 85eebbc | 2011-09-06 17:56:07 +0200 | [diff] [blame] | 1402 | bus_set_iommu(&platform_bus_type, &msm_iommu_ops); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | subsys_initcall(msm_iommu_init); |
| 1407 | |
| 1408 | MODULE_LICENSE("GPL v2"); |
| 1409 | MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>"); |