Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012 The Linux Foundation. All rights reserved. |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [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 | |
| 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> |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 24 | #include <linux/of.h> |
| 25 | #include <linux/of_address.h> |
| 26 | #include <linux/of_device.h> |
| 27 | |
| 28 | #include <mach/iommu_hw-v2.h> |
| 29 | #include <mach/iommu.h> |
| 30 | |
Stepan Moskovchenko | 880a318 | 2012-10-01 12:35:24 -0700 | [diff] [blame] | 31 | static int msm_iommu_parse_bfb_settings(struct platform_device *pdev, |
| 32 | struct msm_iommu_drvdata *drvdata) |
| 33 | { |
| 34 | struct msm_iommu_bfb_settings *bfb_settings; |
| 35 | u32 nreg, nval; |
| 36 | int ret, i; |
| 37 | |
| 38 | /* |
| 39 | * It is not valid for a device to have the qcom,iommu-bfb-regs |
| 40 | * property but not the qcom,iommu-bfb-data property, and vice versa. |
| 41 | */ |
| 42 | if (!of_get_property(pdev->dev.of_node, "qcom,iommu-bfb-regs", &nreg)) { |
| 43 | if (of_get_property(pdev->dev.of_node, "qcom,iommu-bfb-data", |
| 44 | &nval)) |
| 45 | return -EINVAL; |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | if (!of_get_property(pdev->dev.of_node, "qcom,iommu-bfb-data", &nval)) |
| 50 | return -EINVAL; |
| 51 | |
| 52 | if (nreg >= sizeof(bfb_settings->regs)) |
| 53 | return -EINVAL; |
| 54 | |
| 55 | if (nval >= sizeof(bfb_settings->data)) |
| 56 | return -EINVAL; |
| 57 | |
| 58 | if (nval != nreg) |
| 59 | return -EINVAL; |
| 60 | |
| 61 | bfb_settings = devm_kzalloc(&pdev->dev, sizeof(*bfb_settings), |
| 62 | GFP_KERNEL); |
| 63 | if (!bfb_settings) |
| 64 | return -ENOMEM; |
| 65 | |
| 66 | ret = of_property_read_u32_array(pdev->dev.of_node, |
| 67 | "qcom,iommu-bfb-regs", |
| 68 | bfb_settings->regs, |
| 69 | nreg / sizeof(*bfb_settings->regs)); |
| 70 | if (ret) |
| 71 | return ret; |
| 72 | |
| 73 | ret = of_property_read_u32_array(pdev->dev.of_node, |
| 74 | "qcom,iommu-bfb-data", |
| 75 | bfb_settings->data, |
| 76 | nval / sizeof(*bfb_settings->data)); |
| 77 | if (ret) |
| 78 | return ret; |
| 79 | |
| 80 | bfb_settings->length = nreg / sizeof(*bfb_settings->regs); |
| 81 | |
| 82 | for (i = 0; i < bfb_settings->length; i++) |
| 83 | if (bfb_settings->regs[i] < IMPLDEF_OFFSET || |
| 84 | bfb_settings->regs[i] >= IMPLDEF_OFFSET + IMPLDEF_LENGTH) |
| 85 | return -EINVAL; |
| 86 | |
| 87 | drvdata->bfb_settings = bfb_settings; |
| 88 | return 0; |
| 89 | } |
| 90 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 91 | static int msm_iommu_parse_dt(struct platform_device *pdev, |
| 92 | struct msm_iommu_drvdata *drvdata) |
| 93 | { |
| 94 | struct device_node *child; |
Stepan Moskovchenko | 4575bdd | 2012-06-28 14:59:00 -0700 | [diff] [blame] | 95 | int ret = 0; |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 96 | |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 97 | drvdata->dev = &pdev->dev; |
| 98 | msm_iommu_add_drv(drvdata); |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 99 | |
Stepan Moskovchenko | 880a318 | 2012-10-01 12:35:24 -0700 | [diff] [blame] | 100 | ret = msm_iommu_parse_bfb_settings(pdev, drvdata); |
| 101 | if (ret) |
| 102 | goto fail; |
| 103 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 104 | for_each_child_of_node(pdev->dev.of_node, child) { |
| 105 | drvdata->ncb++; |
| 106 | if (!of_platform_device_create(child, NULL, &pdev->dev)) |
| 107 | pr_err("Failed to create %s device\n", child->name); |
| 108 | } |
| 109 | |
| 110 | drvdata->name = dev_name(&pdev->dev); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 111 | drvdata->sec_id = -1; |
| 112 | of_property_read_u32(pdev->dev.of_node, "qcom,iommu-secure-id", |
| 113 | &drvdata->sec_id); |
| 114 | return 0; |
Stepan Moskovchenko | 4575bdd | 2012-06-28 14:59:00 -0700 | [diff] [blame] | 115 | fail: |
| 116 | return ret; |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 119 | static int __devinit msm_iommu_probe(struct platform_device *pdev) |
| 120 | { |
| 121 | struct msm_iommu_drvdata *drvdata; |
| 122 | struct resource *r; |
Stepan Moskovchenko | 17ae71e | 2012-07-24 19:24:14 -0700 | [diff] [blame] | 123 | int ret, needs_alt_core_clk; |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 124 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 125 | drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL); |
| 126 | if (!drvdata) |
| 127 | return -ENOMEM; |
| 128 | |
| 129 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 130 | if (!r) |
| 131 | return -EINVAL; |
| 132 | |
| 133 | drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r)); |
| 134 | if (!drvdata->base) |
| 135 | return -ENOMEM; |
| 136 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 137 | drvdata->glb_base = drvdata->base; |
| 138 | |
Stepan Moskovchenko | 6751acc | 2012-06-21 17:36:47 -0700 | [diff] [blame] | 139 | drvdata->gdsc = devm_regulator_get(&pdev->dev, "vdd"); |
| 140 | if (IS_ERR(drvdata->gdsc)) |
| 141 | return -EINVAL; |
| 142 | |
Stepan Moskovchenko | 17ae71e | 2012-07-24 19:24:14 -0700 | [diff] [blame] | 143 | drvdata->pclk = devm_clk_get(&pdev->dev, "iface_clk"); |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 144 | if (IS_ERR(drvdata->pclk)) |
| 145 | return PTR_ERR(drvdata->pclk); |
| 146 | |
Stepan Moskovchenko | 17ae71e | 2012-07-24 19:24:14 -0700 | [diff] [blame] | 147 | drvdata->clk = devm_clk_get(&pdev->dev, "core_clk"); |
| 148 | if (IS_ERR(drvdata->clk)) |
| 149 | return PTR_ERR(drvdata->clk); |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 150 | |
Stepan Moskovchenko | 17ae71e | 2012-07-24 19:24:14 -0700 | [diff] [blame] | 151 | needs_alt_core_clk = of_property_read_bool(pdev->dev.of_node, |
| 152 | "qcom,needs-alt-core-clk"); |
| 153 | if (needs_alt_core_clk) { |
| 154 | drvdata->aclk = devm_clk_get(&pdev->dev, "alt_core_clk"); |
| 155 | if (IS_ERR(drvdata->aclk)) |
| 156 | return PTR_ERR(drvdata->aclk); |
| 157 | } |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 158 | |
Stepan Moskovchenko | 17ae71e | 2012-07-24 19:24:14 -0700 | [diff] [blame] | 159 | if (clk_get_rate(drvdata->clk) == 0) { |
| 160 | ret = clk_round_rate(drvdata->clk, 1); |
| 161 | clk_set_rate(drvdata->clk, ret); |
| 162 | } |
| 163 | |
| 164 | if (drvdata->aclk && clk_get_rate(drvdata->aclk) == 0) { |
| 165 | ret = clk_round_rate(drvdata->aclk, 1); |
| 166 | clk_set_rate(drvdata->aclk, ret); |
| 167 | } |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 168 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 169 | ret = msm_iommu_parse_dt(pdev, drvdata); |
| 170 | if (ret) |
Stepan Moskovchenko | 17ae71e | 2012-07-24 19:24:14 -0700 | [diff] [blame] | 171 | return ret; |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 172 | |
| 173 | pr_info("device %s mapped at %p, with %d ctx banks\n", |
| 174 | drvdata->name, drvdata->base, drvdata->ncb); |
| 175 | |
| 176 | platform_set_drvdata(pdev, drvdata); |
| 177 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 178 | return 0; |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static int __devexit msm_iommu_remove(struct platform_device *pdev) |
| 182 | { |
| 183 | struct msm_iommu_drvdata *drv = NULL; |
| 184 | |
| 185 | drv = platform_get_drvdata(pdev); |
| 186 | if (drv) { |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 187 | msm_iommu_remove_drv(drv); |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 188 | if (drv->clk) |
| 189 | clk_put(drv->clk); |
| 190 | clk_put(drv->pclk); |
| 191 | platform_set_drvdata(pdev, NULL); |
| 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int msm_iommu_ctx_parse_dt(struct platform_device *pdev, |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 197 | struct msm_iommu_ctx_drvdata *ctx_drvdata) |
| 198 | { |
| 199 | struct resource *r, rp; |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 200 | int irq, ret; |
Stepan Moskovchenko | 4575bdd | 2012-06-28 14:59:00 -0700 | [diff] [blame] | 201 | u32 nsid; |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 202 | |
| 203 | irq = platform_get_irq(pdev, 0); |
| 204 | if (irq > 0) { |
| 205 | ret = request_threaded_irq(irq, NULL, |
| 206 | msm_iommu_fault_handler_v2, |
| 207 | IRQF_ONESHOT | IRQF_SHARED, |
| 208 | "msm_iommu_nonsecure_irq", pdev); |
| 209 | if (ret) { |
| 210 | pr_err("Request IRQ %d failed with ret=%d\n", irq, ret); |
| 211 | return ret; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 216 | if (!r) |
| 217 | return -EINVAL; |
| 218 | |
| 219 | ret = of_address_to_resource(pdev->dev.parent->of_node, 0, &rp); |
| 220 | if (ret) |
| 221 | return -EINVAL; |
| 222 | |
| 223 | /* Calculate the context bank number using the base addresses. The |
| 224 | * first 8 pages belong to the global address space which is followed |
| 225 | * by the context banks, hence subtract by 8 to get the context bank |
| 226 | * number. |
| 227 | */ |
| 228 | ctx_drvdata->num = ((r->start - rp.start) >> CTX_SHIFT) - 8; |
| 229 | |
Stepan Moskovchenko | ce78fc2 | 2012-07-11 17:20:27 -0700 | [diff] [blame] | 230 | if (of_property_read_string(pdev->dev.of_node, "label", |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 231 | &ctx_drvdata->name)) |
| 232 | ctx_drvdata->name = dev_name(&pdev->dev); |
| 233 | |
Stepan Moskovchenko | 4575bdd | 2012-06-28 14:59:00 -0700 | [diff] [blame] | 234 | if (!of_get_property(pdev->dev.of_node, "qcom,iommu-ctx-sids", &nsid)) |
| 235 | return -EINVAL; |
| 236 | |
| 237 | if (nsid >= sizeof(ctx_drvdata->sids)) |
| 238 | return -EINVAL; |
| 239 | |
| 240 | if (of_property_read_u32_array(pdev->dev.of_node, "qcom,iommu-ctx-sids", |
| 241 | ctx_drvdata->sids, |
| 242 | nsid / sizeof(*ctx_drvdata->sids))) { |
| 243 | return -EINVAL; |
| 244 | } |
| 245 | ctx_drvdata->nsid = nsid; |
| 246 | |
Olav Haugan | 26ddd43 | 2012-12-07 11:39:21 -0800 | [diff] [blame] | 247 | ctx_drvdata->secure_context = of_property_read_bool(pdev->dev.of_node, |
| 248 | "qcom,secure-context"); |
| 249 | ctx_drvdata->asid = -1; |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int __devinit msm_iommu_ctx_probe(struct platform_device *pdev) |
| 254 | { |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 255 | struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL; |
| 256 | int ret; |
| 257 | |
| 258 | if (!pdev->dev.parent) |
| 259 | return -EINVAL; |
| 260 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 261 | ctx_drvdata = devm_kzalloc(&pdev->dev, sizeof(*ctx_drvdata), |
| 262 | GFP_KERNEL); |
| 263 | if (!ctx_drvdata) |
| 264 | return -ENOMEM; |
| 265 | |
| 266 | ctx_drvdata->pdev = pdev; |
| 267 | INIT_LIST_HEAD(&ctx_drvdata->attached_elm); |
| 268 | platform_set_drvdata(pdev, ctx_drvdata); |
| 269 | |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 270 | ret = msm_iommu_ctx_parse_dt(pdev, ctx_drvdata); |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 271 | if (!ret) |
| 272 | dev_info(&pdev->dev, "context %s using bank %d\n", |
Stepan Moskovchenko | e14ca5c | 2012-07-11 12:40:51 -0700 | [diff] [blame] | 273 | ctx_drvdata->name, ctx_drvdata->num); |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 274 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | static int __devexit msm_iommu_ctx_remove(struct platform_device *pdev) |
| 279 | { |
| 280 | platform_set_drvdata(pdev, NULL); |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | static struct of_device_id msm_iommu_match_table[] = { |
| 285 | { .compatible = "qcom,msm-smmu-v2", }, |
| 286 | {} |
| 287 | }; |
| 288 | |
| 289 | static struct platform_driver msm_iommu_driver = { |
| 290 | .driver = { |
| 291 | .name = "msm_iommu_v2", |
| 292 | .of_match_table = msm_iommu_match_table, |
| 293 | }, |
| 294 | .probe = msm_iommu_probe, |
| 295 | .remove = __devexit_p(msm_iommu_remove), |
| 296 | }; |
| 297 | |
| 298 | static struct of_device_id msm_iommu_ctx_match_table[] = { |
| 299 | { .name = "qcom,iommu-ctx", }, |
| 300 | {} |
| 301 | }; |
| 302 | |
| 303 | static struct platform_driver msm_iommu_ctx_driver = { |
| 304 | .driver = { |
| 305 | .name = "msm_iommu_ctx_v2", |
| 306 | .of_match_table = msm_iommu_ctx_match_table, |
| 307 | }, |
| 308 | .probe = msm_iommu_ctx_probe, |
| 309 | .remove = __devexit_p(msm_iommu_ctx_remove), |
| 310 | }; |
| 311 | |
| 312 | static int __init msm_iommu_driver_init(void) |
| 313 | { |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 314 | int ret; |
| 315 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 316 | ret = platform_driver_register(&msm_iommu_driver); |
| 317 | if (ret != 0) { |
| 318 | pr_err("Failed to register IOMMU driver\n"); |
| 319 | goto error; |
| 320 | } |
| 321 | |
| 322 | ret = platform_driver_register(&msm_iommu_ctx_driver); |
| 323 | if (ret != 0) { |
| 324 | pr_err("Failed to register IOMMU context driver\n"); |
| 325 | goto error; |
| 326 | } |
| 327 | |
| 328 | error: |
| 329 | return ret; |
| 330 | } |
| 331 | |
| 332 | static void __exit msm_iommu_driver_exit(void) |
| 333 | { |
| 334 | platform_driver_unregister(&msm_iommu_ctx_driver); |
| 335 | platform_driver_unregister(&msm_iommu_driver); |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | subsys_initcall(msm_iommu_driver_init); |
| 339 | module_exit(msm_iommu_driver_exit); |
| 340 | |
| 341 | MODULE_LICENSE("GPL v2"); |