Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame^] | 1 | /* Copyright (c) 2012 Code Aurora Forum. All rights reserved. |
| 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 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/clk.h> |
| 20 | #include <linux/iommu.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/atomic.h> |
| 25 | #include <linux/of.h> |
| 26 | #include <linux/of_address.h> |
| 27 | #include <linux/of_device.h> |
| 28 | |
| 29 | #include <mach/iommu_hw-v2.h> |
| 30 | #include <mach/iommu.h> |
| 31 | |
| 32 | static void msm_iommu_reset(void __iomem *base) |
| 33 | { |
| 34 | int i; |
| 35 | |
| 36 | SET_ACR(base, 0); |
| 37 | SET_NSACR(base, 0); |
| 38 | SET_CR2(base, 0); |
| 39 | SET_NSCR2(base, 0); |
| 40 | SET_GFAR(base, 0); |
| 41 | SET_GFSRRESTORE(base, 0); |
| 42 | SET_TLBIALLNSNH(base, 0); |
| 43 | SET_PMCR(base, 0); |
| 44 | SET_SCR1(base, 0); |
| 45 | SET_SSDR_N(base, 0, 0); |
| 46 | |
| 47 | for (i = 0; i < MAX_NUM_SMR; i++) |
| 48 | SET_SMR_VALID(base, i, 0); |
| 49 | |
| 50 | mb(); |
| 51 | } |
| 52 | |
| 53 | static int msm_iommu_parse_dt(struct platform_device *pdev, |
| 54 | struct msm_iommu_drvdata *drvdata) |
| 55 | { |
| 56 | struct device_node *child; |
| 57 | int ret; |
| 58 | |
| 59 | ret = device_move(&pdev->dev, &msm_iommu_root_dev->dev, DPM_ORDER_NONE); |
| 60 | if (ret) |
| 61 | return ret; |
| 62 | |
| 63 | for_each_child_of_node(pdev->dev.of_node, child) { |
| 64 | drvdata->ncb++; |
| 65 | if (!of_platform_device_create(child, NULL, &pdev->dev)) |
| 66 | pr_err("Failed to create %s device\n", child->name); |
| 67 | } |
| 68 | |
| 69 | drvdata->name = dev_name(&pdev->dev); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static atomic_t msm_iommu_next_id = ATOMIC_INIT(-1); |
| 74 | |
| 75 | static int __devinit msm_iommu_probe(struct platform_device *pdev) |
| 76 | { |
| 77 | struct msm_iommu_drvdata *drvdata; |
| 78 | struct resource *r; |
| 79 | int ret; |
| 80 | |
| 81 | if (msm_iommu_root_dev == pdev) |
| 82 | return 0; |
| 83 | |
| 84 | if (pdev->id == -1) |
| 85 | pdev->id = atomic_inc_return(&msm_iommu_next_id) - 1; |
| 86 | |
| 87 | drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL); |
| 88 | if (!drvdata) |
| 89 | return -ENOMEM; |
| 90 | |
| 91 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 92 | if (!r) |
| 93 | return -EINVAL; |
| 94 | |
| 95 | drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r)); |
| 96 | if (!drvdata->base) |
| 97 | return -ENOMEM; |
| 98 | |
| 99 | drvdata->pclk = clk_get(&pdev->dev, "iface_clk"); |
| 100 | if (IS_ERR(drvdata->pclk)) |
| 101 | return PTR_ERR(drvdata->pclk); |
| 102 | |
| 103 | ret = clk_prepare_enable(drvdata->pclk); |
| 104 | if (ret) |
| 105 | goto fail_enable; |
| 106 | |
| 107 | drvdata->clk = clk_get(&pdev->dev, "core_clk"); |
| 108 | if (!IS_ERR(drvdata->clk)) { |
| 109 | if (clk_get_rate(drvdata->clk) == 0) { |
| 110 | ret = clk_round_rate(drvdata->clk, 1); |
| 111 | clk_set_rate(drvdata->clk, ret); |
| 112 | } |
| 113 | |
| 114 | ret = clk_prepare_enable(drvdata->clk); |
| 115 | if (ret) { |
| 116 | clk_put(drvdata->clk); |
| 117 | goto fail_pclk; |
| 118 | } |
| 119 | } else |
| 120 | drvdata->clk = NULL; |
| 121 | |
| 122 | msm_iommu_reset(drvdata->base); |
| 123 | |
| 124 | SET_CR0_SMCFCFG(drvdata->base, 1); |
| 125 | SET_CR0_USFCFG(drvdata->base, 1); |
| 126 | SET_CR0_STALLD(drvdata->base, 1); |
| 127 | SET_CR0_GCFGFIE(drvdata->base, 1); |
| 128 | SET_CR0_GCFGFRE(drvdata->base, 1); |
| 129 | SET_CR0_GFIE(drvdata->base, 1); |
| 130 | SET_CR0_GFRE(drvdata->base, 1); |
| 131 | SET_CR0_CLIENTPD(drvdata->base, 0); |
| 132 | |
| 133 | ret = msm_iommu_parse_dt(pdev, drvdata); |
| 134 | if (ret) |
| 135 | goto fail_clk; |
| 136 | |
| 137 | pr_info("device %s mapped at %p, with %d ctx banks\n", |
| 138 | drvdata->name, drvdata->base, drvdata->ncb); |
| 139 | |
| 140 | platform_set_drvdata(pdev, drvdata); |
| 141 | |
| 142 | if (drvdata->clk) |
| 143 | clk_disable_unprepare(drvdata->clk); |
| 144 | |
| 145 | clk_disable_unprepare(drvdata->pclk); |
| 146 | |
| 147 | return 0; |
| 148 | |
| 149 | fail_clk: |
| 150 | if (drvdata->clk) { |
| 151 | clk_disable_unprepare(drvdata->clk); |
| 152 | clk_put(drvdata->clk); |
| 153 | } |
| 154 | fail_pclk: |
| 155 | clk_disable_unprepare(drvdata->pclk); |
| 156 | fail_enable: |
| 157 | clk_put(drvdata->pclk); |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | static int __devexit msm_iommu_remove(struct platform_device *pdev) |
| 162 | { |
| 163 | struct msm_iommu_drvdata *drv = NULL; |
| 164 | |
| 165 | drv = platform_get_drvdata(pdev); |
| 166 | if (drv) { |
| 167 | if (drv->clk) |
| 168 | clk_put(drv->clk); |
| 169 | clk_put(drv->pclk); |
| 170 | platform_set_drvdata(pdev, NULL); |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int msm_iommu_ctx_parse_dt(struct platform_device *pdev, |
| 176 | struct msm_iommu_drvdata *drvdata, |
| 177 | struct msm_iommu_ctx_drvdata *ctx_drvdata) |
| 178 | { |
| 179 | struct resource *r, rp; |
| 180 | u32 sids[MAX_NUM_SMR]; |
| 181 | int num = 0; |
| 182 | int irq, i, ret, len = 0; |
| 183 | |
| 184 | irq = platform_get_irq(pdev, 0); |
| 185 | if (irq > 0) { |
| 186 | ret = request_threaded_irq(irq, NULL, |
| 187 | msm_iommu_fault_handler_v2, |
| 188 | IRQF_ONESHOT | IRQF_SHARED, |
| 189 | "msm_iommu_nonsecure_irq", pdev); |
| 190 | if (ret) { |
| 191 | pr_err("Request IRQ %d failed with ret=%d\n", irq, ret); |
| 192 | return ret; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 197 | if (!r) |
| 198 | return -EINVAL; |
| 199 | |
| 200 | ret = of_address_to_resource(pdev->dev.parent->of_node, 0, &rp); |
| 201 | if (ret) |
| 202 | return -EINVAL; |
| 203 | |
| 204 | /* Calculate the context bank number using the base addresses. The |
| 205 | * first 8 pages belong to the global address space which is followed |
| 206 | * by the context banks, hence subtract by 8 to get the context bank |
| 207 | * number. |
| 208 | */ |
| 209 | ctx_drvdata->num = ((r->start - rp.start) >> CTX_SHIFT) - 8; |
| 210 | |
| 211 | if (of_property_read_string(pdev->dev.of_node, "qcom,iommu-ctx-name", |
| 212 | &ctx_drvdata->name)) |
| 213 | ctx_drvdata->name = dev_name(&pdev->dev); |
| 214 | |
| 215 | of_get_property(pdev->dev.of_node, "qcom,iommu-ctx-sids", &len); |
| 216 | BUG_ON(len >= sizeof(sids)); |
| 217 | if (of_property_read_u32_array(pdev->dev.of_node, "qcom,iommu-ctx-sids", |
| 218 | sids, len / sizeof(*sids))) |
| 219 | return -EINVAL; |
| 220 | |
| 221 | /* Program the M2V tables for this context */ |
| 222 | for (i = 0; i < len / sizeof(*sids); i++) { |
| 223 | for (; num < MAX_NUM_SMR; num++) |
| 224 | if (GET_SMR_VALID(drvdata->base, num) == 0) |
| 225 | break; |
| 226 | BUG_ON(num >= MAX_NUM_SMR); |
| 227 | |
| 228 | SET_SMR_VALID(drvdata->base, num, 1); |
| 229 | SET_SMR_MASK(drvdata->base, num, 0); |
| 230 | SET_SMR_ID(drvdata->base, num, sids[i]); |
| 231 | |
| 232 | /* Set VMID = 0 */ |
| 233 | SET_S2CR_N(drvdata->base, num, 0); |
| 234 | SET_S2CR_CBNDX(drvdata->base, num, ctx_drvdata->num); |
| 235 | /* Set security bit override to be Non-secure */ |
| 236 | SET_S2CR_NSCFG(drvdata->base, sids[i], 3); |
| 237 | |
| 238 | SET_CBAR_N(drvdata->base, ctx_drvdata->num, 0); |
| 239 | /* Stage 1 Context with Stage 2 bypass */ |
| 240 | SET_CBAR_TYPE(drvdata->base, ctx_drvdata->num, 1); |
| 241 | /* Route page faults to the non-secure interrupt */ |
| 242 | SET_CBAR_IRPTNDX(drvdata->base, ctx_drvdata->num, 1); |
| 243 | } |
| 244 | mb(); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int __devinit msm_iommu_ctx_probe(struct platform_device *pdev) |
| 250 | { |
| 251 | struct msm_iommu_drvdata *drvdata; |
| 252 | struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL; |
| 253 | int ret; |
| 254 | |
| 255 | if (!pdev->dev.parent) |
| 256 | return -EINVAL; |
| 257 | |
| 258 | drvdata = dev_get_drvdata(pdev->dev.parent); |
| 259 | if (!drvdata) |
| 260 | return -ENODEV; |
| 261 | |
| 262 | ctx_drvdata = devm_kzalloc(&pdev->dev, sizeof(*ctx_drvdata), |
| 263 | GFP_KERNEL); |
| 264 | if (!ctx_drvdata) |
| 265 | return -ENOMEM; |
| 266 | |
| 267 | ctx_drvdata->pdev = pdev; |
| 268 | INIT_LIST_HEAD(&ctx_drvdata->attached_elm); |
| 269 | platform_set_drvdata(pdev, ctx_drvdata); |
| 270 | |
| 271 | ret = clk_prepare_enable(drvdata->pclk); |
| 272 | if (ret) |
| 273 | return ret; |
| 274 | |
| 275 | if (drvdata->clk) { |
| 276 | ret = clk_prepare_enable(drvdata->clk); |
| 277 | if (ret) { |
| 278 | clk_disable_unprepare(drvdata->pclk); |
| 279 | return ret; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | ret = msm_iommu_ctx_parse_dt(pdev, drvdata, ctx_drvdata); |
| 284 | if (!ret) |
| 285 | dev_info(&pdev->dev, "context %s using bank %d\n", |
| 286 | dev_name(&pdev->dev), ctx_drvdata->num); |
| 287 | |
| 288 | if (drvdata->clk) |
| 289 | clk_disable_unprepare(drvdata->clk); |
| 290 | clk_disable_unprepare(drvdata->pclk); |
| 291 | |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | static int __devexit msm_iommu_ctx_remove(struct platform_device *pdev) |
| 296 | { |
| 297 | platform_set_drvdata(pdev, NULL); |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static struct of_device_id msm_iommu_match_table[] = { |
| 302 | { .compatible = "qcom,msm-smmu-v2", }, |
| 303 | {} |
| 304 | }; |
| 305 | |
| 306 | static struct platform_driver msm_iommu_driver = { |
| 307 | .driver = { |
| 308 | .name = "msm_iommu_v2", |
| 309 | .of_match_table = msm_iommu_match_table, |
| 310 | }, |
| 311 | .probe = msm_iommu_probe, |
| 312 | .remove = __devexit_p(msm_iommu_remove), |
| 313 | }; |
| 314 | |
| 315 | static struct of_device_id msm_iommu_ctx_match_table[] = { |
| 316 | { .name = "qcom,iommu-ctx", }, |
| 317 | {} |
| 318 | }; |
| 319 | |
| 320 | static struct platform_driver msm_iommu_ctx_driver = { |
| 321 | .driver = { |
| 322 | .name = "msm_iommu_ctx_v2", |
| 323 | .of_match_table = msm_iommu_ctx_match_table, |
| 324 | }, |
| 325 | .probe = msm_iommu_ctx_probe, |
| 326 | .remove = __devexit_p(msm_iommu_ctx_remove), |
| 327 | }; |
| 328 | |
| 329 | static int __init msm_iommu_driver_init(void) |
| 330 | { |
| 331 | struct device_node *node; |
| 332 | int ret; |
| 333 | |
| 334 | node = of_find_compatible_node(NULL, NULL, "qcom,msm-smmu-v2"); |
| 335 | if (!node) |
| 336 | return -ENODEV; |
| 337 | |
| 338 | of_node_put(node); |
| 339 | |
| 340 | msm_iommu_root_dev = platform_device_register_simple( |
| 341 | "msm_iommu", -1, 0, 0); |
| 342 | if (!msm_iommu_root_dev) { |
| 343 | pr_err("Failed to create root IOMMU device\n"); |
| 344 | ret = -ENODEV; |
| 345 | goto error; |
| 346 | } |
| 347 | |
| 348 | atomic_inc(&msm_iommu_next_id); |
| 349 | |
| 350 | ret = platform_driver_register(&msm_iommu_driver); |
| 351 | if (ret != 0) { |
| 352 | pr_err("Failed to register IOMMU driver\n"); |
| 353 | goto error; |
| 354 | } |
| 355 | |
| 356 | ret = platform_driver_register(&msm_iommu_ctx_driver); |
| 357 | if (ret != 0) { |
| 358 | pr_err("Failed to register IOMMU context driver\n"); |
| 359 | goto error; |
| 360 | } |
| 361 | |
| 362 | error: |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | static void __exit msm_iommu_driver_exit(void) |
| 367 | { |
| 368 | platform_driver_unregister(&msm_iommu_ctx_driver); |
| 369 | platform_driver_unregister(&msm_iommu_driver); |
| 370 | platform_device_unregister(msm_iommu_root_dev); |
| 371 | } |
| 372 | |
| 373 | subsys_initcall(msm_iommu_driver_init); |
| 374 | module_exit(msm_iommu_driver_exit); |
| 375 | |
| 376 | MODULE_LICENSE("GPL v2"); |