Olav Haugan | 3c7fb38 | 2013-01-02 17:32:25 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -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. |
| 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/mutex.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/iommu.h> |
| 24 | #include <linux/clk.h> |
| 25 | #include <linux/scatterlist.h> |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 26 | #include <linux/of.h> |
| 27 | #include <linux/of_device.h> |
Stepan Moskovchenko | 6751acc | 2012-06-21 17:36:47 -0700 | [diff] [blame] | 28 | #include <linux/regulator/consumer.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 29 | #include <asm/sizes.h> |
| 30 | |
Olav Haugan | e6d01ef | 2013-01-25 16:55:44 -0800 | [diff] [blame] | 31 | #include <mach/iommu_hw-v1.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 32 | #include <mach/iommu.h> |
Olav Haugan | 5ebfbc6 | 2013-01-07 17:49:10 -0800 | [diff] [blame] | 33 | #include <mach/iommu_perfmon.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 34 | #include "msm_iommu_pagetable.h" |
| 35 | |
| 36 | /* bitmap of the page sizes currently supported */ |
| 37 | #define MSM_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M) |
| 38 | |
| 39 | static DEFINE_MUTEX(msm_iommu_lock); |
| 40 | |
| 41 | struct msm_priv { |
| 42 | struct iommu_pt pt; |
| 43 | struct list_head list_attached; |
| 44 | }; |
| 45 | |
Olav Haugan | 2648d97 | 2013-01-07 17:32:31 -0800 | [diff] [blame] | 46 | static int __enable_regulators(struct msm_iommu_drvdata *drvdata) |
| 47 | { |
| 48 | int ret = regulator_enable(drvdata->gdsc); |
| 49 | if (ret) |
| 50 | goto fail; |
| 51 | |
| 52 | if (drvdata->alt_gdsc) |
| 53 | ret = regulator_enable(drvdata->alt_gdsc); |
| 54 | |
| 55 | if (ret) |
| 56 | regulator_disable(drvdata->gdsc); |
| 57 | fail: |
| 58 | return ret; |
| 59 | } |
| 60 | |
| 61 | static void __disable_regulators(struct msm_iommu_drvdata *drvdata) |
| 62 | { |
| 63 | if (drvdata->alt_gdsc) |
| 64 | regulator_disable(drvdata->alt_gdsc); |
| 65 | |
| 66 | regulator_disable(drvdata->gdsc); |
| 67 | } |
| 68 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 69 | static int __enable_clocks(struct msm_iommu_drvdata *drvdata) |
| 70 | { |
| 71 | int ret; |
| 72 | |
| 73 | ret = clk_prepare_enable(drvdata->pclk); |
| 74 | if (ret) |
| 75 | goto fail; |
| 76 | |
Stepan Moskovchenko | 17ae71e | 2012-07-24 19:24:14 -0700 | [diff] [blame] | 77 | ret = clk_prepare_enable(drvdata->clk); |
| 78 | if (ret) |
| 79 | clk_disable_unprepare(drvdata->pclk); |
| 80 | |
| 81 | if (drvdata->aclk) { |
| 82 | ret = clk_prepare_enable(drvdata->aclk); |
| 83 | if (ret) { |
| 84 | clk_disable_unprepare(drvdata->clk); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 85 | clk_disable_unprepare(drvdata->pclk); |
Stepan Moskovchenko | 17ae71e | 2012-07-24 19:24:14 -0700 | [diff] [blame] | 86 | } |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 87 | } |
Olav Haugan | 3c7fb38 | 2013-01-02 17:32:25 -0800 | [diff] [blame] | 88 | |
| 89 | if (drvdata->clk_reg_virt) { |
| 90 | unsigned int value; |
| 91 | |
| 92 | value = readl_relaxed(drvdata->clk_reg_virt); |
| 93 | value &= ~0x1; |
| 94 | writel_relaxed(value, drvdata->clk_reg_virt); |
Olav Haugan | af4eb0b | 2013-02-06 09:51:48 -0800 | [diff] [blame] | 95 | /* Ensure clock is on before continuing */ |
| 96 | mb(); |
Olav Haugan | 3c7fb38 | 2013-01-02 17:32:25 -0800 | [diff] [blame] | 97 | } |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 98 | fail: |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | static void __disable_clocks(struct msm_iommu_drvdata *drvdata) |
| 103 | { |
Stepan Moskovchenko | 17ae71e | 2012-07-24 19:24:14 -0700 | [diff] [blame] | 104 | if (drvdata->aclk) |
| 105 | clk_disable_unprepare(drvdata->aclk); |
| 106 | clk_disable_unprepare(drvdata->clk); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 107 | clk_disable_unprepare(drvdata->pclk); |
| 108 | } |
| 109 | |
Olav Haugan | 5ebfbc6 | 2013-01-07 17:49:10 -0800 | [diff] [blame] | 110 | static int _iommu_power_off(void *data) |
| 111 | { |
| 112 | struct msm_iommu_drvdata *drvdata; |
| 113 | |
| 114 | drvdata = (struct msm_iommu_drvdata *)data; |
| 115 | __disable_clocks(drvdata); |
| 116 | __disable_regulators(drvdata); |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static int _iommu_power_on(void *data) |
| 121 | { |
| 122 | int ret; |
| 123 | struct msm_iommu_drvdata *drvdata; |
| 124 | |
| 125 | drvdata = (struct msm_iommu_drvdata *)data; |
| 126 | ret = __enable_regulators(drvdata); |
| 127 | if (ret) |
| 128 | goto fail; |
| 129 | |
| 130 | ret = __enable_clocks(drvdata); |
| 131 | if (ret) { |
| 132 | __disable_regulators(drvdata); |
| 133 | goto fail; |
| 134 | } |
| 135 | return 0; |
| 136 | fail: |
| 137 | return -EIO; |
| 138 | } |
| 139 | |
| 140 | static void _iommu_lock_acquire(void) |
| 141 | { |
| 142 | mutex_lock(&msm_iommu_lock); |
| 143 | } |
| 144 | |
| 145 | static void _iommu_lock_release(void) |
| 146 | { |
| 147 | mutex_unlock(&msm_iommu_lock); |
| 148 | } |
| 149 | |
Olav Haugan | ef69e89 | 2013-02-04 13:47:08 -0800 | [diff] [blame] | 150 | struct iommu_access_ops iommu_access_ops_v1 = { |
Olav Haugan | 5ebfbc6 | 2013-01-07 17:49:10 -0800 | [diff] [blame] | 151 | .iommu_power_on = _iommu_power_on, |
| 152 | .iommu_power_off = _iommu_power_off, |
| 153 | .iommu_lock_acquire = _iommu_lock_acquire, |
| 154 | .iommu_lock_release = _iommu_lock_release, |
| 155 | }; |
Olav Haugan | ef69e89 | 2013-02-04 13:47:08 -0800 | [diff] [blame] | 156 | EXPORT_SYMBOL(iommu_access_ops_v1); |
Olav Haugan | 5ebfbc6 | 2013-01-07 17:49:10 -0800 | [diff] [blame] | 157 | |
Olav Haugan | cd93219 | 2013-01-31 18:30:15 -0800 | [diff] [blame] | 158 | void iommu_halt(const struct msm_iommu_drvdata *iommu_drvdata) |
| 159 | { |
| 160 | if (iommu_drvdata->halt_enabled) { |
| 161 | SET_MICRO_MMU_CTRL_HALT_REQ(iommu_drvdata->base, 1); |
| 162 | |
| 163 | while (GET_MICRO_MMU_CTRL_IDLE(iommu_drvdata->base) == 0) |
| 164 | cpu_relax(); |
| 165 | /* Ensure device is idle before continuing */ |
| 166 | mb(); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void iommu_resume(const struct msm_iommu_drvdata *iommu_drvdata) |
| 171 | { |
| 172 | if (iommu_drvdata->halt_enabled) { |
| 173 | /* |
| 174 | * Ensure transactions have completed before releasing |
| 175 | * the halt |
| 176 | */ |
| 177 | mb(); |
| 178 | SET_MICRO_MMU_CTRL_HALT_REQ(iommu_drvdata->base, 0); |
Olav Haugan | a142c98 | 2013-02-07 12:33:05 -0800 | [diff] [blame] | 179 | /* |
| 180 | * Ensure write is complete before continuing to ensure |
| 181 | * we don't turn off clocks while transaction is still |
| 182 | * pending. |
| 183 | */ |
| 184 | mb(); |
Olav Haugan | cd93219 | 2013-01-31 18:30:15 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Stepan Moskovchenko | 22d32c6 | 2012-07-11 18:00:06 -0700 | [diff] [blame] | 188 | static void __sync_tlb(void __iomem *base, int ctx) |
| 189 | { |
| 190 | SET_TLBSYNC(base, ctx, 0); |
| 191 | |
| 192 | /* No barrier needed due to register proximity */ |
| 193 | while (GET_CB_TLBSTATUS_SACTIVE(base, ctx)) |
| 194 | cpu_relax(); |
| 195 | |
| 196 | /* No barrier needed due to read dependency */ |
| 197 | } |
| 198 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 199 | static int __flush_iotlb_va(struct iommu_domain *domain, unsigned int va) |
| 200 | { |
| 201 | struct msm_priv *priv = domain->priv; |
| 202 | struct msm_iommu_drvdata *iommu_drvdata; |
| 203 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 204 | int ret = 0; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 205 | |
| 206 | list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) { |
| 207 | BUG_ON(!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent); |
| 208 | |
| 209 | iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent); |
| 210 | BUG_ON(!iommu_drvdata); |
| 211 | |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 212 | |
| 213 | ret = __enable_clocks(iommu_drvdata); |
| 214 | if (ret) |
| 215 | goto fail; |
| 216 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 217 | SET_TLBIVA(iommu_drvdata->base, ctx_drvdata->num, |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 218 | ctx_drvdata->asid | (va & CB_TLBIVA_VA)); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 219 | mb(); |
Stepan Moskovchenko | 22d32c6 | 2012-07-11 18:00:06 -0700 | [diff] [blame] | 220 | __sync_tlb(iommu_drvdata->base, ctx_drvdata->num); |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 221 | __disable_clocks(iommu_drvdata); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 222 | } |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 223 | fail: |
| 224 | return ret; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static int __flush_iotlb(struct iommu_domain *domain) |
| 228 | { |
| 229 | struct msm_priv *priv = domain->priv; |
| 230 | struct msm_iommu_drvdata *iommu_drvdata; |
| 231 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 232 | int ret = 0; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 233 | |
| 234 | list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) { |
| 235 | BUG_ON(!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent); |
| 236 | |
| 237 | iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent); |
| 238 | BUG_ON(!iommu_drvdata); |
| 239 | |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 240 | ret = __enable_clocks(iommu_drvdata); |
| 241 | if (ret) |
| 242 | goto fail; |
| 243 | |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 244 | SET_TLBIASID(iommu_drvdata->base, ctx_drvdata->num, |
| 245 | ctx_drvdata->asid); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 246 | mb(); |
Stepan Moskovchenko | 22d32c6 | 2012-07-11 18:00:06 -0700 | [diff] [blame] | 247 | __sync_tlb(iommu_drvdata->base, ctx_drvdata->num); |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 248 | __disable_clocks(iommu_drvdata); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 251 | fail: |
| 252 | return ret; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 255 | /* |
| 256 | * May only be called for non-secure iommus |
| 257 | */ |
Stepan Moskovchenko | 00f0cac | 2012-10-05 23:56:05 -0700 | [diff] [blame] | 258 | static void __reset_iommu(void __iomem *base) |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 259 | { |
Stepan Moskovchenko | 00f0cac | 2012-10-05 23:56:05 -0700 | [diff] [blame] | 260 | int i, smt_size; |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 261 | |
| 262 | SET_ACR(base, 0); |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 263 | SET_CR2(base, 0); |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 264 | SET_GFAR(base, 0); |
| 265 | SET_GFSRRESTORE(base, 0); |
| 266 | SET_TLBIALLNSNH(base, 0); |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 267 | SET_SCR1(base, 0); |
| 268 | SET_SSDR_N(base, 0, 0); |
Stepan Moskovchenko | 00f0cac | 2012-10-05 23:56:05 -0700 | [diff] [blame] | 269 | smt_size = GET_IDR0_NUMSMRG(base); |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 270 | |
Stepan Moskovchenko | 518ca10 | 2012-06-27 15:15:26 -0700 | [diff] [blame] | 271 | for (i = 0; i < smt_size; i++) |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 272 | SET_SMR_VALID(base, i, 0); |
| 273 | |
| 274 | mb(); |
| 275 | } |
| 276 | |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 277 | /* |
| 278 | * May only be called for non-secure iommus |
| 279 | */ |
Olav Haugan | f378273 | 2013-01-11 11:23:30 -0800 | [diff] [blame] | 280 | static void __program_iommu(void __iomem *base) |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 281 | { |
Stepan Moskovchenko | 00f0cac | 2012-10-05 23:56:05 -0700 | [diff] [blame] | 282 | __reset_iommu(base); |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 283 | |
| 284 | SET_CR0_SMCFCFG(base, 1); |
| 285 | SET_CR0_USFCFG(base, 1); |
| 286 | SET_CR0_STALLD(base, 1); |
| 287 | SET_CR0_GCFGFIE(base, 1); |
| 288 | SET_CR0_GCFGFRE(base, 1); |
| 289 | SET_CR0_GFIE(base, 1); |
| 290 | SET_CR0_GFRE(base, 1); |
| 291 | SET_CR0_CLIENTPD(base, 0); |
Stepan Moskovchenko | 880a318 | 2012-10-01 12:35:24 -0700 | [diff] [blame] | 292 | |
Olav Haugan | f378273 | 2013-01-11 11:23:30 -0800 | [diff] [blame] | 293 | mb(); /* Make sure writes complete before returning */ |
| 294 | } |
| 295 | |
| 296 | void program_iommu_bfb_settings(void __iomem *base, |
| 297 | const struct msm_iommu_bfb_settings *bfb_settings) |
| 298 | { |
| 299 | unsigned int i; |
Stepan Moskovchenko | 880a318 | 2012-10-01 12:35:24 -0700 | [diff] [blame] | 300 | if (bfb_settings) |
| 301 | for (i = 0; i < bfb_settings->length; i++) |
| 302 | SET_GLOBAL_REG(base, bfb_settings->regs[i], |
| 303 | bfb_settings->data[i]); |
| 304 | |
Olav Haugan | f378273 | 2013-01-11 11:23:30 -0800 | [diff] [blame] | 305 | mb(); /* Make sure writes complete before returning */ |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 308 | static void __reset_context(void __iomem *base, int ctx) |
| 309 | { |
| 310 | SET_ACTLR(base, ctx, 0); |
| 311 | SET_FAR(base, ctx, 0); |
| 312 | SET_FSRRESTORE(base, ctx, 0); |
| 313 | SET_NMRR(base, ctx, 0); |
| 314 | SET_PAR(base, ctx, 0); |
| 315 | SET_PRRR(base, ctx, 0); |
| 316 | SET_SCTLR(base, ctx, 0); |
| 317 | SET_TLBIALL(base, ctx, 0); |
| 318 | SET_TTBCR(base, ctx, 0); |
| 319 | SET_TTBR0(base, ctx, 0); |
| 320 | SET_TTBR1(base, ctx, 0); |
| 321 | mb(); |
| 322 | } |
| 323 | |
Stepan Moskovchenko | 00f0cac | 2012-10-05 23:56:05 -0700 | [diff] [blame] | 324 | static void __release_smg(void __iomem *base, int ctx) |
Stepan Moskovchenko | ce74935 | 2012-10-04 19:02:03 -0700 | [diff] [blame] | 325 | { |
Stepan Moskovchenko | 00f0cac | 2012-10-05 23:56:05 -0700 | [diff] [blame] | 326 | int i, smt_size; |
| 327 | smt_size = GET_IDR0_NUMSMRG(base); |
Stepan Moskovchenko | ce74935 | 2012-10-04 19:02:03 -0700 | [diff] [blame] | 328 | |
| 329 | /* Invalidate any SMGs associated with this context */ |
| 330 | for (i = 0; i < smt_size; i++) |
| 331 | if (GET_SMR_VALID(base, i) && |
| 332 | GET_S2CR_CBNDX(base, i) == ctx) |
| 333 | SET_SMR_VALID(base, i, 0); |
| 334 | } |
| 335 | |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 336 | static void msm_iommu_assign_ASID(const struct msm_iommu_drvdata *iommu_drvdata, |
| 337 | struct msm_iommu_ctx_drvdata *curr_ctx, |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 338 | struct msm_priv *priv) |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 339 | { |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 340 | unsigned int found = 0; |
| 341 | void __iomem *base = iommu_drvdata->base; |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 342 | unsigned int i; |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 343 | unsigned int ncb = iommu_drvdata->ncb; |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 344 | struct msm_iommu_ctx_drvdata *tmp_drvdata; |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 345 | |
| 346 | /* Find if this page table is used elsewhere, and re-use ASID */ |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 347 | if (!list_empty(&priv->list_attached)) { |
| 348 | tmp_drvdata = list_first_entry(&priv->list_attached, |
| 349 | struct msm_iommu_ctx_drvdata, attached_elm); |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 350 | |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 351 | ++iommu_drvdata->asid[tmp_drvdata->asid - 1]; |
| 352 | curr_ctx->asid = tmp_drvdata->asid; |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 353 | |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 354 | SET_CB_CONTEXTIDR_ASID(base, curr_ctx->num, curr_ctx->asid); |
| 355 | found = 1; |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* If page table is new, find an unused ASID */ |
| 359 | if (!found) { |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 360 | for (i = 0; i < ncb; ++i) { |
| 361 | if (iommu_drvdata->asid[i] == 0) { |
| 362 | ++iommu_drvdata->asid[i]; |
| 363 | curr_ctx->asid = i + 1; |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 364 | |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 365 | SET_CB_CONTEXTIDR_ASID(base, curr_ctx->num, |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 366 | curr_ctx->asid); |
| 367 | found = 1; |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 368 | break; |
| 369 | } |
| 370 | } |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 371 | BUG_ON(!found); |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | static void __program_context(struct msm_iommu_drvdata *iommu_drvdata, |
| 376 | struct msm_iommu_ctx_drvdata *ctx_drvdata, |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 377 | struct msm_priv *priv, bool is_secure) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 378 | { |
| 379 | unsigned int prrr, nmrr; |
| 380 | unsigned int pn; |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 381 | int num = 0, i, smt_size; |
| 382 | void __iomem *base = iommu_drvdata->base; |
| 383 | unsigned int ctx = ctx_drvdata->num; |
| 384 | u32 *sids = ctx_drvdata->sids; |
| 385 | int len = ctx_drvdata->nsid; |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 386 | phys_addr_t pgtable = __pa(priv->pt.fl_table); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 387 | |
| 388 | __reset_context(base, ctx); |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 389 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 390 | pn = pgtable >> CB_TTBR0_ADDR_SHIFT; |
| 391 | SET_TTBCR(base, ctx, 0); |
| 392 | SET_CB_TTBR0_ADDR(base, ctx, pn); |
| 393 | |
| 394 | /* Enable context fault interrupt */ |
| 395 | SET_CB_SCTLR_CFIE(base, ctx, 1); |
| 396 | |
| 397 | /* Redirect all cacheable requests to L2 slave port. */ |
| 398 | SET_CB_ACTLR_BPRCISH(base, ctx, 1); |
| 399 | SET_CB_ACTLR_BPRCOSH(base, ctx, 1); |
| 400 | SET_CB_ACTLR_BPRCNSH(base, ctx, 1); |
| 401 | |
| 402 | /* Turn on TEX Remap */ |
| 403 | SET_CB_SCTLR_TRE(base, ctx, 1); |
| 404 | |
| 405 | /* Enable private ASID namespace */ |
| 406 | SET_CB_SCTLR_ASIDPNE(base, ctx, 1); |
| 407 | |
| 408 | /* Set TEX remap attributes */ |
| 409 | RCP15_PRRR(prrr); |
| 410 | RCP15_NMRR(nmrr); |
| 411 | SET_PRRR(base, ctx, prrr); |
| 412 | SET_NMRR(base, ctx, nmrr); |
| 413 | |
| 414 | /* Configure page tables as inner-cacheable and shareable to reduce |
| 415 | * the TLB miss penalty. |
| 416 | */ |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 417 | if (priv->pt.redirect) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 418 | SET_CB_TTBR0_S(base, ctx, 1); |
| 419 | SET_CB_TTBR0_NOS(base, ctx, 1); |
| 420 | SET_CB_TTBR0_IRGN1(base, ctx, 0); /* WB, WA */ |
| 421 | SET_CB_TTBR0_IRGN0(base, ctx, 1); |
| 422 | SET_CB_TTBR0_RGN(base, ctx, 1); /* WB, WA */ |
| 423 | } |
| 424 | |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 425 | if (!is_secure) { |
| 426 | smt_size = GET_IDR0_NUMSMRG(base); |
| 427 | /* Program the M2V tables for this context */ |
| 428 | for (i = 0; i < len / sizeof(*sids); i++) { |
| 429 | for (; num < smt_size; num++) |
| 430 | if (GET_SMR_VALID(base, num) == 0) |
| 431 | break; |
| 432 | BUG_ON(num >= smt_size); |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 433 | |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 434 | SET_SMR_VALID(base, num, 1); |
| 435 | SET_SMR_MASK(base, num, 0); |
| 436 | SET_SMR_ID(base, num, sids[i]); |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 437 | |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 438 | SET_S2CR_N(base, num, 0); |
| 439 | SET_S2CR_CBNDX(base, num, ctx); |
| 440 | SET_S2CR_MEMATTR(base, num, 0x0A); |
| 441 | /* Set security bit override to be Non-secure */ |
| 442 | SET_S2CR_NSCFG(base, num, 3); |
| 443 | } |
| 444 | SET_CBAR_N(base, ctx, 0); |
| 445 | |
| 446 | /* Stage 1 Context with Stage 2 bypass */ |
| 447 | SET_CBAR_TYPE(base, ctx, 1); |
| 448 | |
| 449 | /* Route page faults to the non-secure interrupt */ |
| 450 | SET_CBAR_IRPTNDX(base, ctx, 1); |
| 451 | |
| 452 | /* Set VMID to non-secure HLOS */ |
| 453 | SET_CBAR_VMID(base, ctx, 3); |
| 454 | |
| 455 | /* Bypass is treated as inner-shareable */ |
| 456 | SET_CBAR_BPSHCFG(base, ctx, 2); |
| 457 | |
| 458 | /* Do not downgrade memory attributes */ |
| 459 | SET_CBAR_MEMATTR(base, ctx, 0x0A); |
| 460 | |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 463 | msm_iommu_assign_ASID(iommu_drvdata, ctx_drvdata, priv); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 464 | |
| 465 | /* Enable the MMU */ |
| 466 | SET_CB_SCTLR_M(base, ctx, 1); |
| 467 | mb(); |
| 468 | } |
| 469 | |
| 470 | static int msm_iommu_domain_init(struct iommu_domain *domain, int flags) |
| 471 | { |
| 472 | struct msm_priv *priv; |
| 473 | |
| 474 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 475 | if (!priv) |
| 476 | goto fail_nomem; |
| 477 | |
| 478 | #ifdef CONFIG_IOMMU_PGTABLES_L2 |
| 479 | priv->pt.redirect = flags & MSM_IOMMU_DOMAIN_PT_CACHEABLE; |
| 480 | #endif |
| 481 | |
| 482 | INIT_LIST_HEAD(&priv->list_attached); |
| 483 | if (msm_iommu_pagetable_alloc(&priv->pt)) |
| 484 | goto fail_nomem; |
| 485 | |
| 486 | domain->priv = priv; |
| 487 | return 0; |
| 488 | |
| 489 | fail_nomem: |
| 490 | kfree(priv); |
| 491 | return -ENOMEM; |
| 492 | } |
| 493 | |
| 494 | static void msm_iommu_domain_destroy(struct iommu_domain *domain) |
| 495 | { |
| 496 | struct msm_priv *priv; |
| 497 | |
| 498 | mutex_lock(&msm_iommu_lock); |
| 499 | priv = domain->priv; |
| 500 | domain->priv = NULL; |
| 501 | |
| 502 | if (priv) |
| 503 | msm_iommu_pagetable_free(&priv->pt); |
| 504 | |
| 505 | kfree(priv); |
| 506 | mutex_unlock(&msm_iommu_lock); |
| 507 | } |
| 508 | |
| 509 | static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev) |
| 510 | { |
| 511 | struct msm_priv *priv; |
| 512 | struct msm_iommu_drvdata *iommu_drvdata; |
| 513 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 514 | struct msm_iommu_ctx_drvdata *tmp_drvdata; |
Stepan Moskovchenko | 4575bdd | 2012-06-28 14:59:00 -0700 | [diff] [blame] | 515 | int ret; |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 516 | int is_secure; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 517 | |
| 518 | mutex_lock(&msm_iommu_lock); |
| 519 | |
| 520 | priv = domain->priv; |
| 521 | if (!priv || !dev) { |
| 522 | ret = -EINVAL; |
| 523 | goto fail; |
| 524 | } |
| 525 | |
| 526 | iommu_drvdata = dev_get_drvdata(dev->parent); |
| 527 | ctx_drvdata = dev_get_drvdata(dev); |
| 528 | if (!iommu_drvdata || !ctx_drvdata) { |
| 529 | ret = -EINVAL; |
| 530 | goto fail; |
| 531 | } |
| 532 | |
| 533 | if (!list_empty(&ctx_drvdata->attached_elm)) { |
| 534 | ret = -EBUSY; |
| 535 | goto fail; |
| 536 | } |
| 537 | |
| 538 | list_for_each_entry(tmp_drvdata, &priv->list_attached, attached_elm) |
| 539 | if (tmp_drvdata == ctx_drvdata) { |
| 540 | ret = -EBUSY; |
| 541 | goto fail; |
| 542 | } |
| 543 | |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 544 | is_secure = iommu_drvdata->sec_id != -1; |
| 545 | |
Olav Haugan | 2648d97 | 2013-01-07 17:32:31 -0800 | [diff] [blame] | 546 | ret = __enable_regulators(iommu_drvdata); |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 547 | if (ret) |
| 548 | goto fail; |
| 549 | |
Stepan Moskovchenko | 6751acc | 2012-06-21 17:36:47 -0700 | [diff] [blame] | 550 | ret = __enable_clocks(iommu_drvdata); |
| 551 | if (ret) { |
Olav Haugan | 2648d97 | 2013-01-07 17:32:31 -0800 | [diff] [blame] | 552 | __disable_regulators(iommu_drvdata); |
Stepan Moskovchenko | 6751acc | 2012-06-21 17:36:47 -0700 | [diff] [blame] | 553 | goto fail; |
| 554 | } |
| 555 | |
Olav Haugan | e388539 | 2013-03-06 16:22:53 -0800 | [diff] [blame] | 556 | /* We can only do this once */ |
| 557 | if (!iommu_drvdata->ctx_attach_count) { |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 558 | if (!is_secure) { |
Olav Haugan | ce2eab9 | 2013-02-07 12:59:18 -0800 | [diff] [blame] | 559 | iommu_halt(iommu_drvdata); |
Olav Haugan | f378273 | 2013-01-11 11:23:30 -0800 | [diff] [blame] | 560 | __program_iommu(iommu_drvdata->base); |
Olav Haugan | ce2eab9 | 2013-02-07 12:59:18 -0800 | [diff] [blame] | 561 | iommu_resume(iommu_drvdata); |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 562 | } else { |
| 563 | ret = msm_iommu_sec_program_iommu( |
| 564 | iommu_drvdata->sec_id); |
| 565 | if (ret) { |
Olav Haugan | 2648d97 | 2013-01-07 17:32:31 -0800 | [diff] [blame] | 566 | __disable_regulators(iommu_drvdata); |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 567 | __disable_clocks(iommu_drvdata); |
| 568 | goto fail; |
| 569 | } |
| 570 | } |
Olav Haugan | f378273 | 2013-01-11 11:23:30 -0800 | [diff] [blame] | 571 | program_iommu_bfb_settings(iommu_drvdata->base, |
| 572 | iommu_drvdata->bfb_settings); |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 573 | } |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 574 | |
Olav Haugan | ce2eab9 | 2013-02-07 12:59:18 -0800 | [diff] [blame] | 575 | iommu_halt(iommu_drvdata); |
| 576 | |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 577 | __program_context(iommu_drvdata, ctx_drvdata, priv, is_secure); |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 578 | |
Olav Haugan | cd93219 | 2013-01-31 18:30:15 -0800 | [diff] [blame] | 579 | iommu_resume(iommu_drvdata); |
| 580 | |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 581 | __disable_clocks(iommu_drvdata); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 582 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 583 | list_add(&(ctx_drvdata->attached_elm), &priv->list_attached); |
| 584 | ctx_drvdata->attached_domain = domain; |
Olav Haugan | e388539 | 2013-03-06 16:22:53 -0800 | [diff] [blame] | 585 | ++iommu_drvdata->ctx_attach_count; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 586 | |
Olav Haugan | 5ebfbc6 | 2013-01-07 17:49:10 -0800 | [diff] [blame] | 587 | mutex_unlock(&msm_iommu_lock); |
| 588 | |
| 589 | msm_iommu_attached(dev->parent); |
| 590 | return ret; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 591 | fail: |
| 592 | mutex_unlock(&msm_iommu_lock); |
| 593 | return ret; |
| 594 | } |
| 595 | |
| 596 | static void msm_iommu_detach_dev(struct iommu_domain *domain, |
| 597 | struct device *dev) |
| 598 | { |
| 599 | struct msm_priv *priv; |
| 600 | struct msm_iommu_drvdata *iommu_drvdata; |
| 601 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 602 | int ret; |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 603 | int is_secure; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 604 | |
Olav Haugan | 5ebfbc6 | 2013-01-07 17:49:10 -0800 | [diff] [blame] | 605 | msm_iommu_detached(dev->parent); |
| 606 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 607 | mutex_lock(&msm_iommu_lock); |
| 608 | priv = domain->priv; |
| 609 | if (!priv || !dev) |
| 610 | goto fail; |
| 611 | |
| 612 | iommu_drvdata = dev_get_drvdata(dev->parent); |
| 613 | ctx_drvdata = dev_get_drvdata(dev); |
| 614 | if (!iommu_drvdata || !ctx_drvdata || !ctx_drvdata->attached_domain) |
| 615 | goto fail; |
| 616 | |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 617 | ret = __enable_clocks(iommu_drvdata); |
| 618 | if (ret) |
| 619 | goto fail; |
| 620 | |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 621 | is_secure = iommu_drvdata->sec_id != -1; |
| 622 | |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 623 | SET_TLBIASID(iommu_drvdata->base, ctx_drvdata->num, ctx_drvdata->asid); |
Olav Haugan | 4e315c4 | 2013-03-06 10:14:28 -0800 | [diff] [blame] | 624 | |
| 625 | BUG_ON(iommu_drvdata->asid[ctx_drvdata->asid - 1] == 0); |
| 626 | iommu_drvdata->asid[ctx_drvdata->asid - 1]--; |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 627 | ctx_drvdata->asid = -1; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 628 | |
Olav Haugan | cd93219 | 2013-01-31 18:30:15 -0800 | [diff] [blame] | 629 | iommu_halt(iommu_drvdata); |
| 630 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 631 | __reset_context(iommu_drvdata->base, ctx_drvdata->num); |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 632 | if (!is_secure) |
| 633 | __release_smg(iommu_drvdata->base, ctx_drvdata->num); |
Stepan Moskovchenko | ce74935 | 2012-10-04 19:02:03 -0700 | [diff] [blame] | 634 | |
Olav Haugan | cd93219 | 2013-01-31 18:30:15 -0800 | [diff] [blame] | 635 | iommu_resume(iommu_drvdata); |
| 636 | |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 637 | __disable_clocks(iommu_drvdata); |
| 638 | |
Olav Haugan | 2648d97 | 2013-01-07 17:32:31 -0800 | [diff] [blame] | 639 | __disable_regulators(iommu_drvdata); |
Stepan Moskovchenko | 6751acc | 2012-06-21 17:36:47 -0700 | [diff] [blame] | 640 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 641 | list_del_init(&ctx_drvdata->attached_elm); |
| 642 | ctx_drvdata->attached_domain = NULL; |
Olav Haugan | e388539 | 2013-03-06 16:22:53 -0800 | [diff] [blame] | 643 | BUG_ON(iommu_drvdata->ctx_attach_count == 0); |
| 644 | --iommu_drvdata->ctx_attach_count; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 645 | fail: |
| 646 | mutex_unlock(&msm_iommu_lock); |
| 647 | } |
| 648 | |
| 649 | static int msm_iommu_map(struct iommu_domain *domain, unsigned long va, |
| 650 | phys_addr_t pa, size_t len, int prot) |
| 651 | { |
| 652 | struct msm_priv *priv; |
| 653 | int ret = 0; |
| 654 | |
| 655 | mutex_lock(&msm_iommu_lock); |
| 656 | |
| 657 | priv = domain->priv; |
| 658 | if (!priv) { |
| 659 | ret = -EINVAL; |
| 660 | goto fail; |
| 661 | } |
| 662 | |
| 663 | ret = msm_iommu_pagetable_map(&priv->pt, va, pa, len, prot); |
| 664 | if (ret) |
| 665 | goto fail; |
| 666 | |
| 667 | ret = __flush_iotlb_va(domain, va); |
| 668 | fail: |
| 669 | mutex_unlock(&msm_iommu_lock); |
| 670 | return ret; |
| 671 | } |
| 672 | |
| 673 | static size_t msm_iommu_unmap(struct iommu_domain *domain, unsigned long va, |
| 674 | size_t len) |
| 675 | { |
| 676 | struct msm_priv *priv; |
| 677 | int ret = -ENODEV; |
| 678 | |
| 679 | mutex_lock(&msm_iommu_lock); |
| 680 | |
| 681 | priv = domain->priv; |
| 682 | if (!priv) |
| 683 | goto fail; |
| 684 | |
| 685 | ret = msm_iommu_pagetable_unmap(&priv->pt, va, len); |
| 686 | if (ret < 0) |
| 687 | goto fail; |
| 688 | |
| 689 | ret = __flush_iotlb_va(domain, va); |
| 690 | fail: |
| 691 | mutex_unlock(&msm_iommu_lock); |
| 692 | |
| 693 | /* the IOMMU API requires us to return how many bytes were unmapped */ |
| 694 | len = ret ? 0 : len; |
| 695 | return len; |
| 696 | } |
| 697 | |
| 698 | static int msm_iommu_map_range(struct iommu_domain *domain, unsigned int va, |
| 699 | struct scatterlist *sg, unsigned int len, |
| 700 | int prot) |
| 701 | { |
| 702 | int ret; |
| 703 | struct msm_priv *priv; |
| 704 | |
| 705 | mutex_lock(&msm_iommu_lock); |
| 706 | |
| 707 | priv = domain->priv; |
| 708 | if (!priv) { |
| 709 | ret = -EINVAL; |
| 710 | goto fail; |
| 711 | } |
| 712 | |
| 713 | ret = msm_iommu_pagetable_map_range(&priv->pt, va, sg, len, prot); |
| 714 | if (ret) |
| 715 | goto fail; |
| 716 | |
| 717 | __flush_iotlb(domain); |
| 718 | fail: |
| 719 | mutex_unlock(&msm_iommu_lock); |
| 720 | return ret; |
| 721 | } |
| 722 | |
| 723 | |
| 724 | static int msm_iommu_unmap_range(struct iommu_domain *domain, unsigned int va, |
| 725 | unsigned int len) |
| 726 | { |
| 727 | struct msm_priv *priv; |
| 728 | |
| 729 | mutex_lock(&msm_iommu_lock); |
| 730 | |
| 731 | priv = domain->priv; |
| 732 | msm_iommu_pagetable_unmap_range(&priv->pt, va, len); |
| 733 | |
| 734 | __flush_iotlb(domain); |
| 735 | mutex_unlock(&msm_iommu_lock); |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain, |
| 740 | unsigned long va) |
| 741 | { |
| 742 | struct msm_priv *priv; |
| 743 | struct msm_iommu_drvdata *iommu_drvdata; |
| 744 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 745 | unsigned int par; |
| 746 | void __iomem *base; |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 747 | phys_addr_t ret = 0; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 748 | int ctx; |
| 749 | |
| 750 | mutex_lock(&msm_iommu_lock); |
| 751 | |
| 752 | priv = domain->priv; |
| 753 | if (list_empty(&priv->list_attached)) |
| 754 | goto fail; |
| 755 | |
| 756 | ctx_drvdata = list_entry(priv->list_attached.next, |
| 757 | struct msm_iommu_ctx_drvdata, attached_elm); |
| 758 | iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent); |
| 759 | |
| 760 | base = iommu_drvdata->base; |
| 761 | ctx = ctx_drvdata->num; |
| 762 | |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 763 | ret = __enable_clocks(iommu_drvdata); |
| 764 | if (ret) { |
| 765 | ret = 0; /* 0 indicates translation failed */ |
| 766 | goto fail; |
| 767 | } |
| 768 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 769 | SET_ATS1PR(base, ctx, va & CB_ATS1PR_ADDR); |
| 770 | mb(); |
| 771 | while (GET_CB_ATSR_ACTIVE(base, ctx)) |
| 772 | cpu_relax(); |
| 773 | |
| 774 | par = GET_PAR(base, ctx); |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 775 | __disable_clocks(iommu_drvdata); |
| 776 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 777 | if (par & CB_PAR_F) { |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 778 | ret = 0; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 779 | } else { |
| 780 | /* We are dealing with a supersection */ |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 781 | if (ret & CB_PAR_SS) |
| 782 | ret = (par & 0xFF000000) | (va & 0x00FFFFFF); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 783 | else /* Upper 20 bits from PAR, lower 12 from VA */ |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 784 | ret = (par & 0xFFFFF000) | (va & 0x00000FFF); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | fail: |
| 788 | mutex_unlock(&msm_iommu_lock); |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 789 | return ret; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | static int msm_iommu_domain_has_cap(struct iommu_domain *domain, |
| 793 | unsigned long cap) |
| 794 | { |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | static void print_ctx_regs(void __iomem *base, int ctx, unsigned int fsr) |
| 799 | { |
| 800 | pr_err("FAR = %08x PAR = %08x\n", |
| 801 | GET_FAR(base, ctx), GET_PAR(base, ctx)); |
| 802 | pr_err("FSR = %08x [%s%s%s%s%s%s%s%s%s]\n", fsr, |
| 803 | (fsr & 0x02) ? "TF " : "", |
| 804 | (fsr & 0x04) ? "AFF " : "", |
| 805 | (fsr & 0x08) ? "PF " : "", |
| 806 | (fsr & 0x10) ? "EF " : "", |
| 807 | (fsr & 0x20) ? "TLBMCF " : "", |
| 808 | (fsr & 0x40) ? "TLBLKF " : "", |
| 809 | (fsr & 0x80) ? "MHF " : "", |
| 810 | (fsr & 0x40000000) ? "SS " : "", |
| 811 | (fsr & 0x80000000) ? "MULTI " : ""); |
| 812 | |
| 813 | pr_err("FSYNR0 = %08x FSYNR1 = %08x\n", |
| 814 | GET_FSYNR0(base, ctx), GET_FSYNR1(base, ctx)); |
| 815 | pr_err("TTBR0 = %08x TTBR1 = %08x\n", |
| 816 | GET_TTBR0(base, ctx), GET_TTBR1(base, ctx)); |
| 817 | pr_err("SCTLR = %08x ACTLR = %08x\n", |
| 818 | GET_SCTLR(base, ctx), GET_ACTLR(base, ctx)); |
| 819 | pr_err("PRRR = %08x NMRR = %08x\n", |
| 820 | GET_PRRR(base, ctx), GET_NMRR(base, ctx)); |
| 821 | } |
| 822 | |
| 823 | irqreturn_t msm_iommu_fault_handler_v2(int irq, void *dev_id) |
| 824 | { |
| 825 | struct platform_device *pdev = dev_id; |
| 826 | struct msm_iommu_drvdata *drvdata; |
| 827 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 828 | unsigned int fsr; |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 829 | int ret; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 830 | |
| 831 | mutex_lock(&msm_iommu_lock); |
| 832 | |
| 833 | BUG_ON(!pdev); |
| 834 | |
| 835 | drvdata = dev_get_drvdata(pdev->dev.parent); |
| 836 | BUG_ON(!drvdata); |
| 837 | |
| 838 | ctx_drvdata = dev_get_drvdata(&pdev->dev); |
| 839 | BUG_ON(!ctx_drvdata); |
| 840 | |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 841 | ret = __enable_clocks(drvdata); |
| 842 | if (ret) { |
| 843 | ret = IRQ_NONE; |
| 844 | goto fail; |
| 845 | } |
| 846 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 847 | fsr = GET_FSR(drvdata->base, ctx_drvdata->num); |
| 848 | if (fsr) { |
| 849 | if (!ctx_drvdata->attached_domain) { |
| 850 | pr_err("Bad domain in interrupt handler\n"); |
| 851 | ret = -ENOSYS; |
| 852 | } else |
| 853 | ret = report_iommu_fault(ctx_drvdata->attached_domain, |
| 854 | &ctx_drvdata->pdev->dev, |
| 855 | GET_FAR(drvdata->base, ctx_drvdata->num), 0); |
| 856 | |
| 857 | if (ret == -ENOSYS) { |
| 858 | pr_err("Unexpected IOMMU page fault!\n"); |
| 859 | pr_err("name = %s\n", drvdata->name); |
| 860 | pr_err("context = %s (%d)\n", ctx_drvdata->name, |
| 861 | ctx_drvdata->num); |
| 862 | pr_err("Interesting registers:\n"); |
| 863 | print_ctx_regs(drvdata->base, ctx_drvdata->num, fsr); |
| 864 | } |
| 865 | |
| 866 | SET_FSR(drvdata->base, ctx_drvdata->num, fsr); |
| 867 | ret = IRQ_HANDLED; |
| 868 | } else |
| 869 | ret = IRQ_NONE; |
| 870 | |
Stepan Moskovchenko | 0bab748 | 2012-06-21 17:15:01 -0700 | [diff] [blame] | 871 | __disable_clocks(drvdata); |
| 872 | fail: |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 873 | mutex_unlock(&msm_iommu_lock); |
| 874 | return ret; |
| 875 | } |
| 876 | |
| 877 | static phys_addr_t msm_iommu_get_pt_base_addr(struct iommu_domain *domain) |
| 878 | { |
| 879 | struct msm_priv *priv = domain->priv; |
| 880 | return __pa(priv->pt.fl_table); |
| 881 | } |
| 882 | |
| 883 | static struct iommu_ops msm_iommu_ops = { |
| 884 | .domain_init = msm_iommu_domain_init, |
| 885 | .domain_destroy = msm_iommu_domain_destroy, |
| 886 | .attach_dev = msm_iommu_attach_dev, |
| 887 | .detach_dev = msm_iommu_detach_dev, |
| 888 | .map = msm_iommu_map, |
| 889 | .unmap = msm_iommu_unmap, |
| 890 | .map_range = msm_iommu_map_range, |
| 891 | .unmap_range = msm_iommu_unmap_range, |
| 892 | .iova_to_phys = msm_iommu_iova_to_phys, |
| 893 | .domain_has_cap = msm_iommu_domain_has_cap, |
| 894 | .get_pt_base_addr = msm_iommu_get_pt_base_addr, |
| 895 | .pgsize_bitmap = MSM_IOMMU_PGSIZES, |
| 896 | }; |
| 897 | |
| 898 | static int __init msm_iommu_init(void) |
| 899 | { |
| 900 | msm_iommu_pagetable_init(); |
| 901 | bus_set_iommu(&platform_bus_type, &msm_iommu_ops); |
| 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | subsys_initcall(msm_iommu_init); |
| 906 | |
| 907 | MODULE_LICENSE("GPL v2"); |
| 908 | MODULE_DESCRIPTION("MSM SMMU v2 Driver"); |