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