Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2012, Code Aurora Forum. 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> |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -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 | |
| 30 | #include <mach/iommu_hw-8xxx.h> |
| 31 | #include <mach/iommu.h> |
| 32 | |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 33 | #define MRC(reg, processor, op1, crn, crm, op2) \ |
| 34 | __asm__ __volatile__ ( \ |
| 35 | " mrc " #processor "," #op1 ", %0," #crn "," #crm "," #op2 "\n" \ |
| 36 | : "=r" (reg)) |
| 37 | |
| 38 | #define RCP15_PRRR(reg) MRC(reg, p15, 0, c10, c2, 0) |
| 39 | #define RCP15_NMRR(reg) MRC(reg, p15, 0, c10, c2, 1) |
| 40 | |
Stepan Moskovchenko | 6ee3be8 | 2011-11-08 15:24:53 -0800 | [diff] [blame] | 41 | /* Sharability attributes of MSM IOMMU mappings */ |
| 42 | #define MSM_IOMMU_ATTR_NON_SH 0x0 |
| 43 | #define MSM_IOMMU_ATTR_SH 0x4 |
| 44 | |
| 45 | /* Cacheability attributes of MSM IOMMU mappings */ |
| 46 | #define MSM_IOMMU_ATTR_NONCACHED 0x0 |
| 47 | #define MSM_IOMMU_ATTR_CACHED_WB_WA 0x1 |
| 48 | #define MSM_IOMMU_ATTR_CACHED_WB_NWA 0x2 |
| 49 | #define MSM_IOMMU_ATTR_CACHED_WT 0x3 |
| 50 | |
| 51 | |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 52 | static inline void clean_pte(unsigned long *start, unsigned long *end, |
| 53 | int redirect) |
Stepan Moskovchenko | 094475d | 2011-08-03 13:38:29 -0700 | [diff] [blame] | 54 | { |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 55 | if (!redirect) |
| 56 | dmac_flush_range(start, end); |
Stepan Moskovchenko | 094475d | 2011-08-03 13:38:29 -0700 | [diff] [blame] | 57 | } |
Stepan Moskovchenko | 094475d | 2011-08-03 13:38:29 -0700 | [diff] [blame] | 58 | |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 59 | static int msm_iommu_tex_class[4]; |
| 60 | |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 61 | DEFINE_MUTEX(msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 62 | |
| 63 | struct msm_priv { |
| 64 | unsigned long *pgtable; |
Stepan Moskovchenko | b243889 | 2011-08-31 17:16:19 -0700 | [diff] [blame] | 65 | int redirect; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 66 | struct list_head list_attached; |
| 67 | }; |
| 68 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 69 | static int __enable_clocks(struct msm_iommu_drvdata *drvdata) |
| 70 | { |
| 71 | int ret; |
| 72 | |
Stepan Moskovchenko | bbf95e1 | 2012-02-14 15:42:27 -0800 | [diff] [blame] | 73 | ret = clk_prepare_enable(drvdata->pclk); |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 74 | if (ret) |
| 75 | goto fail; |
| 76 | |
| 77 | if (drvdata->clk) { |
Stepan Moskovchenko | bbf95e1 | 2012-02-14 15:42:27 -0800 | [diff] [blame] | 78 | ret = clk_prepare_enable(drvdata->clk); |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 79 | if (ret) |
Stepan Moskovchenko | bbf95e1 | 2012-02-14 15:42:27 -0800 | [diff] [blame] | 80 | clk_disable_unprepare(drvdata->pclk); |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 81 | } |
| 82 | fail: |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | static void __disable_clocks(struct msm_iommu_drvdata *drvdata) |
| 87 | { |
| 88 | if (drvdata->clk) |
Stepan Moskovchenko | bbf95e1 | 2012-02-14 15:42:27 -0800 | [diff] [blame] | 89 | clk_disable_unprepare(drvdata->clk); |
| 90 | clk_disable_unprepare(drvdata->pclk); |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Stepan Moskovchenko | bd1ad61 | 2011-08-03 16:24:54 -0700 | [diff] [blame] | 93 | static int __flush_iotlb_va(struct iommu_domain *domain, unsigned int va) |
| 94 | { |
| 95 | struct msm_priv *priv = domain->priv; |
| 96 | struct msm_iommu_drvdata *iommu_drvdata; |
| 97 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 98 | int ret = 0; |
| 99 | int asid; |
| 100 | |
| 101 | list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) { |
| 102 | if (!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent) |
| 103 | BUG(); |
| 104 | |
| 105 | iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent); |
| 106 | if (!iommu_drvdata) |
| 107 | BUG(); |
| 108 | |
| 109 | ret = __enable_clocks(iommu_drvdata); |
| 110 | if (ret) |
| 111 | goto fail; |
| 112 | |
| 113 | asid = GET_CONTEXTIDR_ASID(iommu_drvdata->base, |
| 114 | ctx_drvdata->num); |
| 115 | |
| 116 | SET_TLBIVA(iommu_drvdata->base, ctx_drvdata->num, |
| 117 | asid | (va & TLBIVA_VA)); |
| 118 | mb(); |
| 119 | __disable_clocks(iommu_drvdata); |
| 120 | } |
| 121 | fail: |
| 122 | return ret; |
| 123 | } |
| 124 | |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 125 | static int __flush_iotlb(struct iommu_domain *domain) |
| 126 | { |
| 127 | struct msm_priv *priv = domain->priv; |
| 128 | struct msm_iommu_drvdata *iommu_drvdata; |
| 129 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 130 | int ret = 0; |
| 131 | int asid; |
| 132 | |
| 133 | list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) { |
| 134 | if (!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent) |
| 135 | BUG(); |
| 136 | |
| 137 | iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent); |
| 138 | if (!iommu_drvdata) |
| 139 | BUG(); |
| 140 | |
| 141 | ret = __enable_clocks(iommu_drvdata); |
| 142 | if (ret) |
| 143 | goto fail; |
| 144 | |
| 145 | asid = GET_CONTEXTIDR_ASID(iommu_drvdata->base, |
| 146 | ctx_drvdata->num); |
| 147 | |
| 148 | SET_TLBIASID(iommu_drvdata->base, ctx_drvdata->num, asid); |
| 149 | mb(); |
| 150 | __disable_clocks(iommu_drvdata); |
| 151 | } |
| 152 | fail: |
| 153 | return ret; |
| 154 | } |
| 155 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 156 | static void __reset_context(void __iomem *base, int ctx) |
| 157 | { |
| 158 | SET_BPRCOSH(base, ctx, 0); |
| 159 | SET_BPRCISH(base, ctx, 0); |
| 160 | SET_BPRCNSH(base, ctx, 0); |
| 161 | SET_BPSHCFG(base, ctx, 0); |
| 162 | SET_BPMTCFG(base, ctx, 0); |
| 163 | SET_ACTLR(base, ctx, 0); |
| 164 | SET_SCTLR(base, ctx, 0); |
| 165 | SET_FSRRESTORE(base, ctx, 0); |
| 166 | SET_TTBR0(base, ctx, 0); |
| 167 | SET_TTBR1(base, ctx, 0); |
| 168 | SET_TTBCR(base, ctx, 0); |
| 169 | SET_BFBCR(base, ctx, 0); |
| 170 | SET_PAR(base, ctx, 0); |
| 171 | SET_FAR(base, ctx, 0); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 172 | SET_TLBFLPTER(base, ctx, 0); |
| 173 | SET_TLBSLPTER(base, ctx, 0); |
| 174 | SET_TLBLKCR(base, ctx, 0); |
| 175 | SET_PRRR(base, ctx, 0); |
| 176 | SET_NMRR(base, ctx, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 177 | mb(); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 180 | static void __program_context(void __iomem *base, int ctx, int ncb, |
Shubhraprakash Das | 935e6a5 | 2012-04-05 14:47:30 -0600 | [diff] [blame] | 181 | phys_addr_t pgtable, int redirect, |
| 182 | int ttbr_split) |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 183 | { |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 184 | unsigned int prrr, nmrr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 185 | int i, j, found; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 186 | __reset_context(base, ctx); |
| 187 | |
| 188 | /* Set up HTW mode */ |
| 189 | /* TLB miss configuration: perform HTW on miss */ |
| 190 | SET_TLBMCFG(base, ctx, 0x3); |
| 191 | |
| 192 | /* V2P configuration: HTW for access */ |
| 193 | SET_V2PCFG(base, ctx, 0x3); |
| 194 | |
Shubhraprakash Das | 935e6a5 | 2012-04-05 14:47:30 -0600 | [diff] [blame] | 195 | SET_TTBCR(base, ctx, ttbr_split); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 196 | SET_TTBR0_PA(base, ctx, (pgtable >> TTBR0_PA_SHIFT)); |
Shubhraprakash Das | 935e6a5 | 2012-04-05 14:47:30 -0600 | [diff] [blame] | 197 | if (ttbr_split) |
| 198 | SET_TTBR1_PA(base, ctx, (pgtable >> TTBR1_PA_SHIFT)); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 199 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 200 | /* Enable context fault interrupt */ |
| 201 | SET_CFEIE(base, ctx, 1); |
| 202 | |
| 203 | /* Stall access on a context fault and let the handler deal with it */ |
| 204 | SET_CFCFG(base, ctx, 1); |
| 205 | |
| 206 | /* Redirect all cacheable requests to L2 slave port. */ |
| 207 | SET_RCISH(base, ctx, 1); |
| 208 | SET_RCOSH(base, ctx, 1); |
| 209 | SET_RCNSH(base, ctx, 1); |
| 210 | |
| 211 | /* Turn on TEX Remap */ |
| 212 | SET_TRE(base, ctx, 1); |
| 213 | |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 214 | /* Set TEX remap attributes */ |
| 215 | RCP15_PRRR(prrr); |
| 216 | RCP15_NMRR(nmrr); |
| 217 | SET_PRRR(base, ctx, prrr); |
| 218 | SET_NMRR(base, ctx, nmrr); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 219 | |
| 220 | /* Turn on BFB prefetch */ |
| 221 | SET_BFBDFE(base, ctx, 1); |
| 222 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 223 | /* Configure page tables as inner-cacheable and shareable to reduce |
| 224 | * the TLB miss penalty. |
| 225 | */ |
Stepan Moskovchenko | b243889 | 2011-08-31 17:16:19 -0700 | [diff] [blame] | 226 | if (redirect) { |
| 227 | SET_TTBR0_SH(base, ctx, 1); |
| 228 | SET_TTBR1_SH(base, ctx, 1); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 229 | |
Stepan Moskovchenko | b243889 | 2011-08-31 17:16:19 -0700 | [diff] [blame] | 230 | SET_TTBR0_NOS(base, ctx, 1); |
| 231 | SET_TTBR1_NOS(base, ctx, 1); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 232 | |
Stepan Moskovchenko | b243889 | 2011-08-31 17:16:19 -0700 | [diff] [blame] | 233 | SET_TTBR0_IRGNH(base, ctx, 0); /* WB, WA */ |
| 234 | SET_TTBR0_IRGNL(base, ctx, 1); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 235 | |
Stepan Moskovchenko | b243889 | 2011-08-31 17:16:19 -0700 | [diff] [blame] | 236 | SET_TTBR1_IRGNH(base, ctx, 0); /* WB, WA */ |
| 237 | SET_TTBR1_IRGNL(base, ctx, 1); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 238 | |
Stepan Moskovchenko | b243889 | 2011-08-31 17:16:19 -0700 | [diff] [blame] | 239 | SET_TTBR0_ORGN(base, ctx, 1); /* WB, WA */ |
| 240 | SET_TTBR1_ORGN(base, ctx, 1); /* WB, WA */ |
| 241 | } |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 242 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 243 | /* Find if this page table is used elsewhere, and re-use ASID */ |
| 244 | found = 0; |
| 245 | for (i = 0; i < ncb; i++) |
| 246 | if (GET_TTBR0_PA(base, i) == (pgtable >> TTBR0_PA_SHIFT) && |
| 247 | i != ctx) { |
| 248 | SET_CONTEXTIDR_ASID(base, ctx, \ |
| 249 | GET_CONTEXTIDR_ASID(base, i)); |
| 250 | found = 1; |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | /* If page table is new, find an unused ASID */ |
| 255 | if (!found) { |
| 256 | for (i = 0; i < ncb; i++) { |
| 257 | found = 0; |
| 258 | for (j = 0; j < ncb; j++) { |
| 259 | if (GET_CONTEXTIDR_ASID(base, j) == i && |
| 260 | j != ctx) |
| 261 | found = 1; |
| 262 | } |
| 263 | |
| 264 | if (!found) { |
| 265 | SET_CONTEXTIDR_ASID(base, ctx, i); |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | BUG_ON(found); |
| 270 | } |
| 271 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 272 | /* Enable the MMU */ |
| 273 | SET_M(base, ctx, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 274 | mb(); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Stepan Moskovchenko | ff2d366 | 2011-08-31 17:13:32 -0700 | [diff] [blame] | 277 | static int msm_iommu_domain_init(struct iommu_domain *domain, int flags) |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 278 | { |
| 279 | struct msm_priv *priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 280 | |
| 281 | if (!priv) |
| 282 | goto fail_nomem; |
| 283 | |
| 284 | INIT_LIST_HEAD(&priv->list_attached); |
| 285 | priv->pgtable = (unsigned long *)__get_free_pages(GFP_KERNEL, |
| 286 | get_order(SZ_16K)); |
| 287 | |
| 288 | if (!priv->pgtable) |
| 289 | goto fail_nomem; |
| 290 | |
Stepan Moskovchenko | b243889 | 2011-08-31 17:16:19 -0700 | [diff] [blame] | 291 | #ifdef CONFIG_IOMMU_PGTABLES_L2 |
| 292 | priv->redirect = flags & MSM_IOMMU_DOMAIN_PT_CACHEABLE; |
| 293 | #endif |
| 294 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 295 | memset(priv->pgtable, 0, SZ_16K); |
| 296 | domain->priv = priv; |
Stepan Moskovchenko | 561b50d | 2012-05-11 17:30:08 -0700 | [diff] [blame] | 297 | |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 298 | clean_pte(priv->pgtable, priv->pgtable + NUM_FL_PTE, priv->redirect); |
Stepan Moskovchenko | 561b50d | 2012-05-11 17:30:08 -0700 | [diff] [blame] | 299 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 300 | return 0; |
| 301 | |
| 302 | fail_nomem: |
| 303 | kfree(priv); |
| 304 | return -ENOMEM; |
| 305 | } |
| 306 | |
| 307 | static void msm_iommu_domain_destroy(struct iommu_domain *domain) |
| 308 | { |
| 309 | struct msm_priv *priv; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 310 | unsigned long *fl_table; |
| 311 | int i; |
| 312 | |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 313 | mutex_lock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 314 | priv = domain->priv; |
| 315 | domain->priv = NULL; |
| 316 | |
| 317 | if (priv) { |
| 318 | fl_table = priv->pgtable; |
| 319 | |
| 320 | for (i = 0; i < NUM_FL_PTE; i++) |
| 321 | if ((fl_table[i] & 0x03) == FL_TYPE_TABLE) |
| 322 | free_page((unsigned long) __va(((fl_table[i]) & |
| 323 | FL_BASE_MASK))); |
| 324 | |
| 325 | free_pages((unsigned long)priv->pgtable, get_order(SZ_16K)); |
| 326 | priv->pgtable = NULL; |
| 327 | } |
| 328 | |
| 329 | kfree(priv); |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 330 | mutex_unlock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev) |
| 334 | { |
| 335 | struct msm_priv *priv; |
| 336 | struct msm_iommu_ctx_dev *ctx_dev; |
| 337 | struct msm_iommu_drvdata *iommu_drvdata; |
| 338 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 339 | struct msm_iommu_ctx_drvdata *tmp_drvdata; |
| 340 | int ret = 0; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 341 | |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 342 | mutex_lock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 343 | |
| 344 | priv = domain->priv; |
| 345 | |
| 346 | if (!priv || !dev) { |
| 347 | ret = -EINVAL; |
| 348 | goto fail; |
| 349 | } |
| 350 | |
| 351 | iommu_drvdata = dev_get_drvdata(dev->parent); |
| 352 | ctx_drvdata = dev_get_drvdata(dev); |
| 353 | ctx_dev = dev->platform_data; |
| 354 | |
| 355 | if (!iommu_drvdata || !ctx_drvdata || !ctx_dev) { |
| 356 | ret = -EINVAL; |
| 357 | goto fail; |
| 358 | } |
| 359 | |
Stepan Moskovchenko | 00d4b2b | 2010-11-12 19:29:56 -0800 | [diff] [blame] | 360 | if (!list_empty(&ctx_drvdata->attached_elm)) { |
| 361 | ret = -EBUSY; |
| 362 | goto fail; |
| 363 | } |
| 364 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 365 | list_for_each_entry(tmp_drvdata, &priv->list_attached, attached_elm) |
| 366 | if (tmp_drvdata == ctx_drvdata) { |
| 367 | ret = -EBUSY; |
| 368 | goto fail; |
| 369 | } |
| 370 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 371 | ret = __enable_clocks(iommu_drvdata); |
| 372 | if (ret) |
| 373 | goto fail; |
| 374 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 375 | __program_context(iommu_drvdata->base, ctx_dev->num, iommu_drvdata->ncb, |
Shubhraprakash Das | 935e6a5 | 2012-04-05 14:47:30 -0600 | [diff] [blame] | 376 | __pa(priv->pgtable), priv->redirect, |
| 377 | iommu_drvdata->ttbr_split); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 378 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 379 | __disable_clocks(iommu_drvdata); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 380 | list_add(&(ctx_drvdata->attached_elm), &priv->list_attached); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 381 | |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 382 | ctx_drvdata->attached_domain = domain; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 383 | fail: |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 384 | mutex_unlock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 385 | return ret; |
| 386 | } |
| 387 | |
| 388 | static void msm_iommu_detach_dev(struct iommu_domain *domain, |
| 389 | struct device *dev) |
| 390 | { |
| 391 | struct msm_priv *priv; |
| 392 | struct msm_iommu_ctx_dev *ctx_dev; |
| 393 | struct msm_iommu_drvdata *iommu_drvdata; |
| 394 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
Stepan Moskovchenko | 3306973 | 2010-11-12 19:30:00 -0800 | [diff] [blame] | 395 | int ret; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 396 | |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 397 | mutex_lock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 398 | priv = domain->priv; |
| 399 | |
| 400 | if (!priv || !dev) |
| 401 | goto fail; |
| 402 | |
| 403 | iommu_drvdata = dev_get_drvdata(dev->parent); |
| 404 | ctx_drvdata = dev_get_drvdata(dev); |
| 405 | ctx_dev = dev->platform_data; |
| 406 | |
| 407 | if (!iommu_drvdata || !ctx_drvdata || !ctx_dev) |
| 408 | goto fail; |
| 409 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 410 | ret = __enable_clocks(iommu_drvdata); |
| 411 | if (ret) |
| 412 | goto fail; |
| 413 | |
Stepan Moskovchenko | f17c16c | 2011-08-05 12:16:39 -0700 | [diff] [blame] | 414 | SET_TLBIASID(iommu_drvdata->base, ctx_dev->num, |
| 415 | GET_CONTEXTIDR_ASID(iommu_drvdata->base, ctx_dev->num)); |
| 416 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 417 | __reset_context(iommu_drvdata->base, ctx_dev->num); |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 418 | __disable_clocks(iommu_drvdata); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 419 | list_del_init(&ctx_drvdata->attached_elm); |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 420 | ctx_drvdata->attached_domain = NULL; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 421 | fail: |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 422 | mutex_unlock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 425 | static int __get_pgprot(int prot, int len) |
| 426 | { |
| 427 | unsigned int pgprot; |
Stepan Moskovchenko | 6ee3be8 | 2011-11-08 15:24:53 -0800 | [diff] [blame] | 428 | int tex; |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 429 | |
Stepan Moskovchenko | 3c8cdf8 | 2012-03-15 15:23:48 -0700 | [diff] [blame] | 430 | if (!(prot & (IOMMU_READ | IOMMU_WRITE))) { |
| 431 | prot |= IOMMU_READ | IOMMU_WRITE; |
| 432 | WARN_ONCE(1, "No attributes in iommu mapping; assuming RW\n"); |
| 433 | } |
| 434 | |
| 435 | if ((prot & IOMMU_WRITE) && !(prot & IOMMU_READ)) { |
| 436 | prot |= IOMMU_READ; |
| 437 | WARN_ONCE(1, "Write-only iommu mappings unsupported; falling back to RW\n"); |
| 438 | } |
| 439 | |
Stepan Moskovchenko | 6ee3be8 | 2011-11-08 15:24:53 -0800 | [diff] [blame] | 440 | if (prot & IOMMU_CACHE) |
| 441 | tex = (pgprot_kernel >> 2) & 0x07; |
| 442 | else |
| 443 | tex = msm_iommu_tex_class[MSM_IOMMU_ATTR_NONCACHED]; |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 444 | |
| 445 | if (tex < 0 || tex > NUM_TEX_CLASS - 1) |
| 446 | return 0; |
| 447 | |
| 448 | if (len == SZ_16M || len == SZ_1M) { |
Stepan Moskovchenko | 6ee3be8 | 2011-11-08 15:24:53 -0800 | [diff] [blame] | 449 | pgprot = FL_SHARED; |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 450 | pgprot |= tex & 0x01 ? FL_BUFFERABLE : 0; |
| 451 | pgprot |= tex & 0x02 ? FL_CACHEABLE : 0; |
| 452 | pgprot |= tex & 0x04 ? FL_TEX0 : 0; |
Stepan Moskovchenko | 3c8cdf8 | 2012-03-15 15:23:48 -0700 | [diff] [blame] | 453 | pgprot |= FL_AP0 | FL_AP1; |
| 454 | pgprot |= prot & IOMMU_WRITE ? 0 : FL_AP2; |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 455 | } else { |
Stepan Moskovchenko | 6ee3be8 | 2011-11-08 15:24:53 -0800 | [diff] [blame] | 456 | pgprot = SL_SHARED; |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 457 | pgprot |= tex & 0x01 ? SL_BUFFERABLE : 0; |
| 458 | pgprot |= tex & 0x02 ? SL_CACHEABLE : 0; |
| 459 | pgprot |= tex & 0x04 ? SL_TEX0 : 0; |
Stepan Moskovchenko | 3c8cdf8 | 2012-03-15 15:23:48 -0700 | [diff] [blame] | 460 | pgprot |= SL_AP0 | SL_AP1; |
| 461 | pgprot |= prot & IOMMU_WRITE ? 0 : SL_AP2; |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | return pgprot; |
| 465 | } |
| 466 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 467 | static int msm_iommu_map(struct iommu_domain *domain, unsigned long va, |
| 468 | phys_addr_t pa, int order, int prot) |
| 469 | { |
| 470 | struct msm_priv *priv; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 471 | unsigned long *fl_table; |
| 472 | unsigned long *fl_pte; |
| 473 | unsigned long fl_offset; |
| 474 | unsigned long *sl_table; |
| 475 | unsigned long *sl_pte; |
| 476 | unsigned long sl_offset; |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 477 | unsigned int pgprot; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 478 | size_t len = 0x1000UL << order; |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 479 | int ret = 0; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 480 | |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 481 | mutex_lock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 482 | |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 483 | priv = domain->priv; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 484 | if (!priv) { |
| 485 | ret = -EINVAL; |
| 486 | goto fail; |
| 487 | } |
| 488 | |
| 489 | fl_table = priv->pgtable; |
| 490 | |
| 491 | if (len != SZ_16M && len != SZ_1M && |
| 492 | len != SZ_64K && len != SZ_4K) { |
| 493 | pr_debug("Bad size: %d\n", len); |
| 494 | ret = -EINVAL; |
| 495 | goto fail; |
| 496 | } |
| 497 | |
| 498 | if (!fl_table) { |
| 499 | pr_debug("Null page table\n"); |
| 500 | ret = -EINVAL; |
| 501 | goto fail; |
| 502 | } |
| 503 | |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 504 | pgprot = __get_pgprot(prot, len); |
| 505 | |
| 506 | if (!pgprot) { |
| 507 | ret = -EINVAL; |
| 508 | goto fail; |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 511 | fl_offset = FL_OFFSET(va); /* Upper 12 bits */ |
| 512 | fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */ |
| 513 | |
| 514 | if (len == SZ_16M) { |
| 515 | int i = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 516 | |
| 517 | for (i = 0; i < 16; i++) |
| 518 | if (*(fl_pte+i)) { |
| 519 | ret = -EBUSY; |
| 520 | goto fail; |
| 521 | } |
| 522 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 523 | for (i = 0; i < 16; i++) |
Stepan Moskovchenko | 3c8cdf8 | 2012-03-15 15:23:48 -0700 | [diff] [blame] | 524 | *(fl_pte+i) = (pa & 0xFF000000) | FL_SUPERSECTION |
| 525 | | FL_TYPE_SECT | FL_SHARED | FL_NG | pgprot; |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 526 | clean_pte(fl_pte, fl_pte + 16, priv->redirect); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 529 | if (len == SZ_1M) { |
| 530 | if (*fl_pte) { |
| 531 | ret = -EBUSY; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 532 | goto fail; |
| 533 | } |
| 534 | |
Stepan Moskovchenko | 3c8cdf8 | 2012-03-15 15:23:48 -0700 | [diff] [blame] | 535 | *fl_pte = (pa & 0xFFF00000) | FL_NG | FL_TYPE_SECT | FL_SHARED |
| 536 | | pgprot; |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 537 | clean_pte(fl_pte, fl_pte + 1, priv->redirect); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /* Need a 2nd level table */ |
| 541 | if (len == SZ_4K || len == SZ_64K) { |
| 542 | |
| 543 | if (*fl_pte == 0) { |
| 544 | unsigned long *sl; |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 545 | sl = (unsigned long *) __get_free_pages(GFP_KERNEL, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 546 | get_order(SZ_4K)); |
| 547 | |
| 548 | if (!sl) { |
| 549 | pr_debug("Could not allocate second level table\n"); |
| 550 | ret = -ENOMEM; |
| 551 | goto fail; |
| 552 | } |
| 553 | memset(sl, 0, SZ_4K); |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 554 | clean_pte(sl, sl + NUM_SL_PTE, priv->redirect); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 555 | |
| 556 | *fl_pte = ((((int)__pa(sl)) & FL_BASE_MASK) | \ |
| 557 | FL_TYPE_TABLE); |
Stepan Moskovchenko | 094475d | 2011-08-03 13:38:29 -0700 | [diff] [blame] | 558 | |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 559 | clean_pte(fl_pte, fl_pte + 1, priv->redirect); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | if (!(*fl_pte & FL_TYPE_TABLE)) { |
| 563 | ret = -EBUSY; |
| 564 | goto fail; |
| 565 | } |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK)); |
| 569 | sl_offset = SL_OFFSET(va); |
| 570 | sl_pte = sl_table + sl_offset; |
| 571 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 572 | if (len == SZ_4K) { |
| 573 | if (*sl_pte) { |
| 574 | ret = -EBUSY; |
| 575 | goto fail; |
| 576 | } |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 577 | |
Stepan Moskovchenko | 3c8cdf8 | 2012-03-15 15:23:48 -0700 | [diff] [blame] | 578 | *sl_pte = (pa & SL_BASE_MASK_SMALL) | SL_NG | SL_SHARED |
| 579 | | SL_TYPE_SMALL | pgprot; |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 580 | clean_pte(sl_pte, sl_pte + 1, priv->redirect); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 581 | } |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 582 | |
| 583 | if (len == SZ_64K) { |
| 584 | int i; |
| 585 | |
| 586 | for (i = 0; i < 16; i++) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 587 | if (*(sl_pte+i)) { |
| 588 | ret = -EBUSY; |
| 589 | goto fail; |
| 590 | } |
| 591 | |
| 592 | for (i = 0; i < 16; i++) |
Stepan Moskovchenko | 3c8cdf8 | 2012-03-15 15:23:48 -0700 | [diff] [blame] | 593 | *(sl_pte+i) = (pa & SL_BASE_MASK_LARGE) | SL_NG |
| 594 | | SL_SHARED | SL_TYPE_LARGE | pgprot; |
Stepan Moskovchenko | 094475d | 2011-08-03 13:38:29 -0700 | [diff] [blame] | 595 | |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 596 | clean_pte(sl_pte, sl_pte + 16, priv->redirect); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Stepan Moskovchenko | bd1ad61 | 2011-08-03 16:24:54 -0700 | [diff] [blame] | 599 | ret = __flush_iotlb_va(domain, va); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 600 | fail: |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 601 | mutex_unlock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | static int msm_iommu_unmap(struct iommu_domain *domain, unsigned long va, |
| 606 | int order) |
| 607 | { |
| 608 | struct msm_priv *priv; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 609 | unsigned long *fl_table; |
| 610 | unsigned long *fl_pte; |
| 611 | unsigned long fl_offset; |
| 612 | unsigned long *sl_table; |
| 613 | unsigned long *sl_pte; |
| 614 | unsigned long sl_offset; |
| 615 | size_t len = 0x1000UL << order; |
| 616 | int i, ret = 0; |
| 617 | |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 618 | mutex_lock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 619 | |
| 620 | priv = domain->priv; |
| 621 | |
| 622 | if (!priv) { |
| 623 | ret = -ENODEV; |
| 624 | goto fail; |
| 625 | } |
| 626 | |
| 627 | fl_table = priv->pgtable; |
| 628 | |
| 629 | if (len != SZ_16M && len != SZ_1M && |
| 630 | len != SZ_64K && len != SZ_4K) { |
| 631 | pr_debug("Bad length: %d\n", len); |
| 632 | ret = -EINVAL; |
| 633 | goto fail; |
| 634 | } |
| 635 | |
| 636 | if (!fl_table) { |
| 637 | pr_debug("Null page table\n"); |
| 638 | ret = -EINVAL; |
| 639 | goto fail; |
| 640 | } |
| 641 | |
| 642 | fl_offset = FL_OFFSET(va); /* Upper 12 bits */ |
| 643 | fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */ |
| 644 | |
| 645 | if (*fl_pte == 0) { |
| 646 | pr_debug("First level PTE is 0\n"); |
| 647 | ret = -ENODEV; |
| 648 | goto fail; |
| 649 | } |
| 650 | |
| 651 | /* Unmap supersection */ |
Stepan Moskovchenko | 094475d | 2011-08-03 13:38:29 -0700 | [diff] [blame] | 652 | if (len == SZ_16M) { |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 653 | for (i = 0; i < 16; i++) |
| 654 | *(fl_pte+i) = 0; |
| 655 | |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 656 | clean_pte(fl_pte, fl_pte + 16, priv->redirect); |
Stepan Moskovchenko | 094475d | 2011-08-03 13:38:29 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | if (len == SZ_1M) { |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 660 | *fl_pte = 0; |
| 661 | |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 662 | clean_pte(fl_pte, fl_pte + 1, priv->redirect); |
Stepan Moskovchenko | 094475d | 2011-08-03 13:38:29 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 665 | sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK)); |
| 666 | sl_offset = SL_OFFSET(va); |
| 667 | sl_pte = sl_table + sl_offset; |
| 668 | |
| 669 | if (len == SZ_64K) { |
| 670 | for (i = 0; i < 16; i++) |
| 671 | *(sl_pte+i) = 0; |
Stepan Moskovchenko | 094475d | 2011-08-03 13:38:29 -0700 | [diff] [blame] | 672 | |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 673 | clean_pte(sl_pte, sl_pte + 16, priv->redirect); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Stepan Moskovchenko | 094475d | 2011-08-03 13:38:29 -0700 | [diff] [blame] | 676 | if (len == SZ_4K) { |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 677 | *sl_pte = 0; |
| 678 | |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 679 | clean_pte(sl_pte, sl_pte + 1, priv->redirect); |
Stepan Moskovchenko | 094475d | 2011-08-03 13:38:29 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 682 | if (len == SZ_4K || len == SZ_64K) { |
| 683 | int used = 0; |
| 684 | |
| 685 | for (i = 0; i < NUM_SL_PTE; i++) |
| 686 | if (sl_table[i]) |
| 687 | used = 1; |
| 688 | if (!used) { |
| 689 | free_page((unsigned long)sl_table); |
| 690 | *fl_pte = 0; |
Stepan Moskovchenko | 094475d | 2011-08-03 13:38:29 -0700 | [diff] [blame] | 691 | |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 692 | clean_pte(fl_pte, fl_pte + 1, priv->redirect); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
Stepan Moskovchenko | bd1ad61 | 2011-08-03 16:24:54 -0700 | [diff] [blame] | 696 | ret = __flush_iotlb_va(domain, va); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 697 | fail: |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 698 | mutex_unlock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 699 | return ret; |
| 700 | } |
| 701 | |
Jeremy Gebben | 74e57d4 | 2012-03-23 10:26:11 -0600 | [diff] [blame] | 702 | static unsigned int get_phys_addr(struct scatterlist *sg) |
| 703 | { |
| 704 | /* |
| 705 | * Try sg_dma_address first so that we can |
| 706 | * map carveout regions that do not have a |
| 707 | * struct page associated with them. |
| 708 | */ |
| 709 | unsigned int pa = sg_dma_address(sg); |
| 710 | if (pa == 0) |
| 711 | pa = sg_phys(sg); |
| 712 | return pa; |
| 713 | } |
| 714 | |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 715 | static int msm_iommu_map_range(struct iommu_domain *domain, unsigned int va, |
| 716 | struct scatterlist *sg, unsigned int len, |
| 717 | int prot) |
| 718 | { |
| 719 | unsigned int pa; |
| 720 | unsigned int offset = 0; |
| 721 | unsigned int pgprot; |
| 722 | unsigned long *fl_table; |
| 723 | unsigned long *fl_pte; |
| 724 | unsigned long fl_offset; |
| 725 | unsigned long *sl_table; |
| 726 | unsigned long sl_offset, sl_start; |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 727 | unsigned int chunk_offset = 0; |
| 728 | unsigned int chunk_pa; |
| 729 | int ret = 0; |
| 730 | struct msm_priv *priv; |
| 731 | |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 732 | mutex_lock(&msm_iommu_lock); |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 733 | |
| 734 | BUG_ON(len & (SZ_4K - 1)); |
| 735 | |
| 736 | priv = domain->priv; |
| 737 | fl_table = priv->pgtable; |
| 738 | |
| 739 | pgprot = __get_pgprot(prot, SZ_4K); |
| 740 | |
| 741 | if (!pgprot) { |
| 742 | ret = -EINVAL; |
| 743 | goto fail; |
| 744 | } |
| 745 | |
| 746 | fl_offset = FL_OFFSET(va); /* Upper 12 bits */ |
| 747 | fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */ |
| 748 | |
| 749 | sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK)); |
| 750 | sl_offset = SL_OFFSET(va); |
| 751 | |
Jeremy Gebben | 74e57d4 | 2012-03-23 10:26:11 -0600 | [diff] [blame] | 752 | chunk_pa = get_phys_addr(sg); |
| 753 | if (chunk_pa == 0) { |
| 754 | pr_debug("No dma address for sg %p\n", sg); |
| 755 | ret = -EINVAL; |
| 756 | goto fail; |
| 757 | } |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 758 | |
| 759 | while (offset < len) { |
| 760 | /* Set up a 2nd level page table if one doesn't exist */ |
| 761 | if (*fl_pte == 0) { |
| 762 | sl_table = (unsigned long *) |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 763 | __get_free_pages(GFP_KERNEL, get_order(SZ_4K)); |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 764 | |
| 765 | if (!sl_table) { |
| 766 | pr_debug("Could not allocate second level table\n"); |
| 767 | ret = -ENOMEM; |
| 768 | goto fail; |
| 769 | } |
| 770 | |
| 771 | memset(sl_table, 0, SZ_4K); |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 772 | clean_pte(sl_table, sl_table + NUM_SL_PTE, |
| 773 | priv->redirect); |
Stepan Moskovchenko | 561b50d | 2012-05-11 17:30:08 -0700 | [diff] [blame] | 774 | |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 775 | *fl_pte = ((((int)__pa(sl_table)) & FL_BASE_MASK) | |
| 776 | FL_TYPE_TABLE); |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 777 | clean_pte(fl_pte, fl_pte + 1, priv->redirect); |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 778 | } else |
| 779 | sl_table = (unsigned long *) |
| 780 | __va(((*fl_pte) & FL_BASE_MASK)); |
| 781 | |
| 782 | /* Keep track of initial position so we |
| 783 | * don't clean more than we have to |
| 784 | */ |
| 785 | sl_start = sl_offset; |
| 786 | |
| 787 | /* Build the 2nd level page table */ |
| 788 | while (offset < len && sl_offset < NUM_SL_PTE) { |
| 789 | pa = chunk_pa + chunk_offset; |
| 790 | sl_table[sl_offset] = (pa & SL_BASE_MASK_SMALL) | |
Stepan Moskovchenko | 3c8cdf8 | 2012-03-15 15:23:48 -0700 | [diff] [blame] | 791 | pgprot | SL_NG | SL_SHARED | SL_TYPE_SMALL; |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 792 | sl_offset++; |
| 793 | offset += SZ_4K; |
| 794 | |
| 795 | chunk_offset += SZ_4K; |
| 796 | |
| 797 | if (chunk_offset >= sg->length && offset < len) { |
| 798 | chunk_offset = 0; |
| 799 | sg = sg_next(sg); |
Jeremy Gebben | 74e57d4 | 2012-03-23 10:26:11 -0600 | [diff] [blame] | 800 | chunk_pa = get_phys_addr(sg); |
| 801 | if (chunk_pa == 0) { |
| 802 | pr_debug("No dma address for sg %p\n", |
| 803 | sg); |
| 804 | ret = -EINVAL; |
| 805 | goto fail; |
| 806 | } |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 810 | clean_pte(sl_table + sl_start, sl_table + sl_offset, |
| 811 | priv->redirect); |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 812 | |
| 813 | fl_pte++; |
| 814 | sl_offset = 0; |
| 815 | } |
| 816 | __flush_iotlb(domain); |
| 817 | fail: |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 818 | mutex_unlock(&msm_iommu_lock); |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 819 | return ret; |
| 820 | } |
| 821 | |
| 822 | |
| 823 | static int msm_iommu_unmap_range(struct iommu_domain *domain, unsigned int va, |
| 824 | unsigned int len) |
| 825 | { |
| 826 | unsigned int offset = 0; |
| 827 | unsigned long *fl_table; |
| 828 | unsigned long *fl_pte; |
| 829 | unsigned long fl_offset; |
| 830 | unsigned long *sl_table; |
| 831 | unsigned long sl_start, sl_end; |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 832 | int used, i; |
| 833 | struct msm_priv *priv; |
| 834 | |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 835 | mutex_lock(&msm_iommu_lock); |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 836 | |
| 837 | BUG_ON(len & (SZ_4K - 1)); |
| 838 | |
| 839 | priv = domain->priv; |
| 840 | fl_table = priv->pgtable; |
| 841 | |
| 842 | fl_offset = FL_OFFSET(va); /* Upper 12 bits */ |
| 843 | fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */ |
| 844 | |
| 845 | sl_start = SL_OFFSET(va); |
| 846 | |
| 847 | while (offset < len) { |
| 848 | sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK)); |
| 849 | sl_end = ((len - offset) / SZ_4K) + sl_start; |
| 850 | |
| 851 | if (sl_end > NUM_SL_PTE) |
| 852 | sl_end = NUM_SL_PTE; |
| 853 | |
| 854 | memset(sl_table + sl_start, 0, (sl_end - sl_start) * 4); |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 855 | clean_pte(sl_table + sl_start, sl_table + sl_end, |
| 856 | priv->redirect); |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 857 | |
| 858 | offset += (sl_end - sl_start) * SZ_4K; |
| 859 | |
| 860 | /* Unmap and free the 2nd level table if all mappings in it |
| 861 | * were removed. This saves memory, but the table will need |
| 862 | * to be re-allocated the next time someone tries to map these |
| 863 | * VAs. |
| 864 | */ |
| 865 | used = 0; |
| 866 | |
| 867 | /* If we just unmapped the whole table, don't bother |
| 868 | * seeing if there are still used entries left. |
| 869 | */ |
| 870 | if (sl_end - sl_start != NUM_SL_PTE) |
| 871 | for (i = 0; i < NUM_SL_PTE; i++) |
| 872 | if (sl_table[i]) { |
| 873 | used = 1; |
| 874 | break; |
| 875 | } |
| 876 | if (!used) { |
| 877 | free_page((unsigned long)sl_table); |
| 878 | *fl_pte = 0; |
| 879 | |
Stepan Moskovchenko | 70dddc9 | 2012-05-11 18:43:02 -0700 | [diff] [blame] | 880 | clean_pte(fl_pte, fl_pte + 1, priv->redirect); |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | sl_start = 0; |
| 884 | fl_pte++; |
| 885 | } |
| 886 | |
| 887 | __flush_iotlb(domain); |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 888 | mutex_unlock(&msm_iommu_lock); |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 889 | return 0; |
| 890 | } |
| 891 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 892 | static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain, |
| 893 | unsigned long va) |
| 894 | { |
| 895 | struct msm_priv *priv; |
| 896 | struct msm_iommu_drvdata *iommu_drvdata; |
| 897 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 898 | unsigned int par; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 899 | void __iomem *base; |
| 900 | phys_addr_t ret = 0; |
| 901 | int ctx; |
| 902 | |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 903 | mutex_lock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 904 | |
| 905 | priv = domain->priv; |
| 906 | if (list_empty(&priv->list_attached)) |
| 907 | goto fail; |
| 908 | |
| 909 | ctx_drvdata = list_entry(priv->list_attached.next, |
| 910 | struct msm_iommu_ctx_drvdata, attached_elm); |
| 911 | iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent); |
| 912 | |
| 913 | base = iommu_drvdata->base; |
| 914 | ctx = ctx_drvdata->num; |
| 915 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 916 | ret = __enable_clocks(iommu_drvdata); |
| 917 | if (ret) |
| 918 | goto fail; |
| 919 | |
Stepan Moskovchenko | b0e7808 | 2011-02-28 16:04:55 -0800 | [diff] [blame] | 920 | SET_V2PPR(base, ctx, va & V2Pxx_VA); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 921 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 922 | mb(); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 923 | par = GET_PAR(base, ctx); |
| 924 | |
| 925 | /* We are dealing with a supersection */ |
| 926 | if (GET_NOFAULT_SS(base, ctx)) |
| 927 | ret = (par & 0xFF000000) | (va & 0x00FFFFFF); |
| 928 | else /* Upper 20 bits from PAR, lower 12 from VA */ |
| 929 | ret = (par & 0xFFFFF000) | (va & 0x00000FFF); |
| 930 | |
Stepan Moskovchenko | 3306973 | 2010-11-12 19:30:00 -0800 | [diff] [blame] | 931 | if (GET_FAULT(base, ctx)) |
| 932 | ret = 0; |
| 933 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 934 | __disable_clocks(iommu_drvdata); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 935 | fail: |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 936 | mutex_unlock(&msm_iommu_lock); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 937 | return ret; |
| 938 | } |
| 939 | |
| 940 | static int msm_iommu_domain_has_cap(struct iommu_domain *domain, |
| 941 | unsigned long cap) |
| 942 | { |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | static void print_ctx_regs(void __iomem *base, int ctx) |
| 947 | { |
| 948 | unsigned int fsr = GET_FSR(base, ctx); |
| 949 | pr_err("FAR = %08x PAR = %08x\n", |
| 950 | GET_FAR(base, ctx), GET_PAR(base, ctx)); |
| 951 | pr_err("FSR = %08x [%s%s%s%s%s%s%s%s%s%s]\n", fsr, |
| 952 | (fsr & 0x02) ? "TF " : "", |
| 953 | (fsr & 0x04) ? "AFF " : "", |
| 954 | (fsr & 0x08) ? "APF " : "", |
| 955 | (fsr & 0x10) ? "TLBMF " : "", |
| 956 | (fsr & 0x20) ? "HTWDEEF " : "", |
| 957 | (fsr & 0x40) ? "HTWSEEF " : "", |
| 958 | (fsr & 0x80) ? "MHF " : "", |
| 959 | (fsr & 0x10000) ? "SL " : "", |
| 960 | (fsr & 0x40000000) ? "SS " : "", |
| 961 | (fsr & 0x80000000) ? "MULTI " : ""); |
| 962 | |
| 963 | pr_err("FSYNR0 = %08x FSYNR1 = %08x\n", |
| 964 | GET_FSYNR0(base, ctx), GET_FSYNR1(base, ctx)); |
| 965 | pr_err("TTBR0 = %08x TTBR1 = %08x\n", |
| 966 | GET_TTBR0(base, ctx), GET_TTBR1(base, ctx)); |
| 967 | pr_err("SCTLR = %08x ACTLR = %08x\n", |
| 968 | GET_SCTLR(base, ctx), GET_ACTLR(base, ctx)); |
| 969 | pr_err("PRRR = %08x NMRR = %08x\n", |
| 970 | GET_PRRR(base, ctx), GET_NMRR(base, ctx)); |
| 971 | } |
| 972 | |
| 973 | irqreturn_t msm_iommu_fault_handler(int irq, void *dev_id) |
| 974 | { |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 975 | struct msm_iommu_ctx_drvdata *ctx_drvdata = dev_id; |
| 976 | struct msm_iommu_drvdata *drvdata; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 977 | void __iomem *base; |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 978 | unsigned int fsr, num; |
| 979 | int ret; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 980 | |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 981 | mutex_lock(&msm_iommu_lock); |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 982 | BUG_ON(!ctx_drvdata); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 983 | |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 984 | drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent); |
| 985 | BUG_ON(!drvdata); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 986 | |
| 987 | base = drvdata->base; |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 988 | num = ctx_drvdata->num; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 989 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 990 | ret = __enable_clocks(drvdata); |
| 991 | if (ret) |
| 992 | goto fail; |
| 993 | |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 994 | fsr = GET_FSR(base, num); |
| 995 | |
| 996 | if (fsr) { |
| 997 | if (!ctx_drvdata->attached_domain) { |
| 998 | pr_err("Bad domain in interrupt handler\n"); |
| 999 | ret = -ENOSYS; |
| 1000 | } else |
| 1001 | ret = report_iommu_fault(ctx_drvdata->attached_domain, |
| 1002 | &ctx_drvdata->pdev->dev, |
| 1003 | GET_FAR(base, num), 0); |
| 1004 | |
| 1005 | if (ret == -ENOSYS) { |
| 1006 | pr_err("Unexpected IOMMU page fault!\n"); |
| 1007 | pr_err("name = %s\n", drvdata->name); |
| 1008 | pr_err("context = %s (%d)\n", ctx_drvdata->name, num); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1009 | pr_err("Interesting registers:\n"); |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 1010 | print_ctx_regs(base, num); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1011 | } |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 1012 | |
| 1013 | SET_FSR(base, num, fsr); |
| 1014 | SET_RESUME(base, num, 1); |
| 1015 | |
| 1016 | ret = IRQ_HANDLED; |
| 1017 | } else |
| 1018 | ret = IRQ_NONE; |
| 1019 | |
Stepan Moskovchenko | 41f3f51 | 2011-02-24 18:00:39 -0800 | [diff] [blame] | 1020 | __disable_clocks(drvdata); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1021 | fail: |
Stepan Moskovchenko | c5888f2 | 2012-02-14 15:42:05 -0800 | [diff] [blame] | 1022 | mutex_unlock(&msm_iommu_lock); |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 1023 | return ret; |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
Shubhraprakash Das | 4c436f2 | 2011-12-02 18:01:57 -0700 | [diff] [blame] | 1026 | static phys_addr_t msm_iommu_get_pt_base_addr(struct iommu_domain *domain) |
| 1027 | { |
| 1028 | struct msm_priv *priv = domain->priv; |
| 1029 | return __pa(priv->pgtable); |
| 1030 | } |
| 1031 | |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1032 | static struct iommu_ops msm_iommu_ops = { |
| 1033 | .domain_init = msm_iommu_domain_init, |
| 1034 | .domain_destroy = msm_iommu_domain_destroy, |
| 1035 | .attach_dev = msm_iommu_attach_dev, |
| 1036 | .detach_dev = msm_iommu_detach_dev, |
| 1037 | .map = msm_iommu_map, |
| 1038 | .unmap = msm_iommu_unmap, |
Stepan Moskovchenko | 04255ee | 2011-08-11 19:45:23 -0700 | [diff] [blame] | 1039 | .map_range = msm_iommu_map_range, |
| 1040 | .unmap_range = msm_iommu_unmap_range, |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1041 | .iova_to_phys = msm_iommu_iova_to_phys, |
Shubhraprakash Das | 4c436f2 | 2011-12-02 18:01:57 -0700 | [diff] [blame] | 1042 | .domain_has_cap = msm_iommu_domain_has_cap, |
| 1043 | .get_pt_base_addr = msm_iommu_get_pt_base_addr |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1044 | }; |
| 1045 | |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 1046 | static int __init get_tex_class(int icp, int ocp, int mt, int nos) |
| 1047 | { |
| 1048 | int i = 0; |
| 1049 | unsigned int prrr = 0; |
| 1050 | unsigned int nmrr = 0; |
| 1051 | int c_icp, c_ocp, c_mt, c_nos; |
| 1052 | |
| 1053 | RCP15_PRRR(prrr); |
| 1054 | RCP15_NMRR(nmrr); |
| 1055 | |
| 1056 | for (i = 0; i < NUM_TEX_CLASS; i++) { |
| 1057 | c_nos = PRRR_NOS(prrr, i); |
| 1058 | c_mt = PRRR_MT(prrr, i); |
| 1059 | c_icp = NMRR_ICP(nmrr, i); |
| 1060 | c_ocp = NMRR_OCP(nmrr, i); |
| 1061 | |
| 1062 | if (icp == c_icp && ocp == c_ocp && c_mt == mt && c_nos == nos) |
| 1063 | return i; |
| 1064 | } |
| 1065 | |
| 1066 | return -ENODEV; |
| 1067 | } |
| 1068 | |
| 1069 | static void __init setup_iommu_tex_classes(void) |
| 1070 | { |
| 1071 | msm_iommu_tex_class[MSM_IOMMU_ATTR_NONCACHED] = |
| 1072 | get_tex_class(CP_NONCACHED, CP_NONCACHED, MT_NORMAL, 1); |
| 1073 | |
| 1074 | msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WB_WA] = |
| 1075 | get_tex_class(CP_WB_WA, CP_WB_WA, MT_NORMAL, 1); |
| 1076 | |
| 1077 | msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WB_NWA] = |
| 1078 | get_tex_class(CP_WB_NWA, CP_WB_NWA, MT_NORMAL, 1); |
| 1079 | |
| 1080 | msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WT] = |
| 1081 | get_tex_class(CP_WT, CP_WT, MT_NORMAL, 1); |
| 1082 | } |
| 1083 | |
Stepan Moskovchenko | 516cbc7 | 2010-11-12 19:29:53 -0800 | [diff] [blame] | 1084 | static int __init msm_iommu_init(void) |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1085 | { |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 1086 | if (!msm_soc_version_supports_iommu_v1()) |
Stepan Moskovchenko | 15f209c | 2011-10-31 15:32:44 -0700 | [diff] [blame] | 1087 | return -ENODEV; |
| 1088 | |
Stepan Moskovchenko | 100832c | 2010-11-15 18:20:08 -0800 | [diff] [blame] | 1089 | setup_iommu_tex_classes(); |
Stepan Moskovchenko | 0720d1f | 2010-08-24 18:31:10 -0700 | [diff] [blame] | 1090 | register_iommu(&msm_iommu_ops); |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | subsys_initcall(msm_iommu_init); |
| 1095 | |
| 1096 | MODULE_LICENSE("GPL v2"); |
| 1097 | MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>"); |