Duy Truong | e833aca | 2013-02-12 13:35:08 -0800 | [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> |
| 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 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 32 | static int msm_iommu_parse_dt(struct platform_device *pdev, |
| 33 | struct msm_iommu_drvdata *drvdata) |
| 34 | { |
| 35 | struct device_node *child; |
Stepan Moskovchenko | 4575bdd | 2012-06-28 14:59:00 -0700 | [diff] [blame] | 36 | int ret = 0; |
| 37 | u32 nsmr; |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 38 | |
| 39 | ret = device_move(&pdev->dev, &msm_iommu_root_dev->dev, DPM_ORDER_NONE); |
| 40 | if (ret) |
Stepan Moskovchenko | 4575bdd | 2012-06-28 14:59:00 -0700 | [diff] [blame] | 41 | goto fail; |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 42 | |
Stepan Moskovchenko | 4575bdd | 2012-06-28 14:59:00 -0700 | [diff] [blame] | 43 | ret = of_property_read_u32(pdev->dev.of_node, "qcom,iommu-smt-size", |
| 44 | &nsmr); |
| 45 | if (ret) |
| 46 | goto fail; |
| 47 | |
| 48 | if (nsmr > MAX_NUM_SMR) { |
| 49 | pr_err("Invalid SMT size: %d\n", nsmr); |
| 50 | ret = -EINVAL; |
| 51 | goto fail; |
| 52 | } |
| 53 | |
| 54 | drvdata->nsmr = nsmr; |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 55 | for_each_child_of_node(pdev->dev.of_node, child) { |
| 56 | drvdata->ncb++; |
| 57 | if (!of_platform_device_create(child, NULL, &pdev->dev)) |
| 58 | pr_err("Failed to create %s device\n", child->name); |
| 59 | } |
| 60 | |
| 61 | drvdata->name = dev_name(&pdev->dev); |
Stepan Moskovchenko | 4575bdd | 2012-06-28 14:59:00 -0700 | [diff] [blame] | 62 | fail: |
| 63 | return ret; |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static atomic_t msm_iommu_next_id = ATOMIC_INIT(-1); |
| 67 | |
| 68 | static int __devinit msm_iommu_probe(struct platform_device *pdev) |
| 69 | { |
| 70 | struct msm_iommu_drvdata *drvdata; |
| 71 | struct resource *r; |
Stepan Moskovchenko | 17ae71e | 2012-07-24 19:24:14 -0700 | [diff] [blame] | 72 | int ret, needs_alt_core_clk; |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 73 | |
| 74 | if (msm_iommu_root_dev == pdev) |
| 75 | return 0; |
| 76 | |
| 77 | if (pdev->id == -1) |
| 78 | pdev->id = atomic_inc_return(&msm_iommu_next_id) - 1; |
| 79 | |
| 80 | drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL); |
| 81 | if (!drvdata) |
| 82 | return -ENOMEM; |
| 83 | |
| 84 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 85 | if (!r) |
| 86 | return -EINVAL; |
| 87 | |
| 88 | drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r)); |
| 89 | if (!drvdata->base) |
| 90 | return -ENOMEM; |
| 91 | |
Stepan Moskovchenko | 6751acc | 2012-06-21 17:36:47 -0700 | [diff] [blame] | 92 | drvdata->gdsc = devm_regulator_get(&pdev->dev, "vdd"); |
| 93 | if (IS_ERR(drvdata->gdsc)) |
| 94 | return -EINVAL; |
| 95 | |
Stepan Moskovchenko | 17ae71e | 2012-07-24 19:24:14 -0700 | [diff] [blame] | 96 | drvdata->pclk = devm_clk_get(&pdev->dev, "iface_clk"); |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 97 | if (IS_ERR(drvdata->pclk)) |
| 98 | return PTR_ERR(drvdata->pclk); |
| 99 | |
Stepan Moskovchenko | 17ae71e | 2012-07-24 19:24:14 -0700 | [diff] [blame] | 100 | drvdata->clk = devm_clk_get(&pdev->dev, "core_clk"); |
| 101 | if (IS_ERR(drvdata->clk)) |
| 102 | return PTR_ERR(drvdata->clk); |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 103 | |
Stepan Moskovchenko | 17ae71e | 2012-07-24 19:24:14 -0700 | [diff] [blame] | 104 | needs_alt_core_clk = of_property_read_bool(pdev->dev.of_node, |
| 105 | "qcom,needs-alt-core-clk"); |
| 106 | if (needs_alt_core_clk) { |
| 107 | drvdata->aclk = devm_clk_get(&pdev->dev, "alt_core_clk"); |
| 108 | if (IS_ERR(drvdata->aclk)) |
| 109 | return PTR_ERR(drvdata->aclk); |
| 110 | } |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 111 | |
Stepan Moskovchenko | 17ae71e | 2012-07-24 19:24:14 -0700 | [diff] [blame] | 112 | if (clk_get_rate(drvdata->clk) == 0) { |
| 113 | ret = clk_round_rate(drvdata->clk, 1); |
| 114 | clk_set_rate(drvdata->clk, ret); |
| 115 | } |
| 116 | |
| 117 | if (drvdata->aclk && clk_get_rate(drvdata->aclk) == 0) { |
| 118 | ret = clk_round_rate(drvdata->aclk, 1); |
| 119 | clk_set_rate(drvdata->aclk, ret); |
| 120 | } |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 121 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 122 | ret = msm_iommu_parse_dt(pdev, drvdata); |
| 123 | if (ret) |
Stepan Moskovchenko | 17ae71e | 2012-07-24 19:24:14 -0700 | [diff] [blame] | 124 | return ret; |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 125 | |
| 126 | pr_info("device %s mapped at %p, with %d ctx banks\n", |
| 127 | drvdata->name, drvdata->base, drvdata->ncb); |
| 128 | |
| 129 | platform_set_drvdata(pdev, drvdata); |
| 130 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 131 | return 0; |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static int __devexit msm_iommu_remove(struct platform_device *pdev) |
| 135 | { |
| 136 | struct msm_iommu_drvdata *drv = NULL; |
| 137 | |
| 138 | drv = platform_get_drvdata(pdev); |
| 139 | if (drv) { |
| 140 | if (drv->clk) |
| 141 | clk_put(drv->clk); |
| 142 | clk_put(drv->pclk); |
| 143 | platform_set_drvdata(pdev, NULL); |
| 144 | } |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int msm_iommu_ctx_parse_dt(struct platform_device *pdev, |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 149 | struct msm_iommu_ctx_drvdata *ctx_drvdata) |
| 150 | { |
| 151 | struct resource *r, rp; |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 152 | int irq, ret; |
Stepan Moskovchenko | 4575bdd | 2012-06-28 14:59:00 -0700 | [diff] [blame] | 153 | u32 nsid; |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 154 | |
| 155 | irq = platform_get_irq(pdev, 0); |
| 156 | if (irq > 0) { |
| 157 | ret = request_threaded_irq(irq, NULL, |
| 158 | msm_iommu_fault_handler_v2, |
| 159 | IRQF_ONESHOT | IRQF_SHARED, |
| 160 | "msm_iommu_nonsecure_irq", pdev); |
| 161 | if (ret) { |
| 162 | pr_err("Request IRQ %d failed with ret=%d\n", irq, ret); |
| 163 | return ret; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 168 | if (!r) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | ret = of_address_to_resource(pdev->dev.parent->of_node, 0, &rp); |
| 172 | if (ret) |
| 173 | return -EINVAL; |
| 174 | |
| 175 | /* Calculate the context bank number using the base addresses. The |
| 176 | * first 8 pages belong to the global address space which is followed |
| 177 | * by the context banks, hence subtract by 8 to get the context bank |
| 178 | * number. |
| 179 | */ |
| 180 | ctx_drvdata->num = ((r->start - rp.start) >> CTX_SHIFT) - 8; |
| 181 | |
Stepan Moskovchenko | ce78fc2 | 2012-07-11 17:20:27 -0700 | [diff] [blame] | 182 | if (of_property_read_string(pdev->dev.of_node, "label", |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 183 | &ctx_drvdata->name)) |
| 184 | ctx_drvdata->name = dev_name(&pdev->dev); |
| 185 | |
Stepan Moskovchenko | 4575bdd | 2012-06-28 14:59:00 -0700 | [diff] [blame] | 186 | if (!of_get_property(pdev->dev.of_node, "qcom,iommu-ctx-sids", &nsid)) |
| 187 | return -EINVAL; |
| 188 | |
| 189 | if (nsid >= sizeof(ctx_drvdata->sids)) |
| 190 | return -EINVAL; |
| 191 | |
| 192 | if (of_property_read_u32_array(pdev->dev.of_node, "qcom,iommu-ctx-sids", |
| 193 | ctx_drvdata->sids, |
| 194 | nsid / sizeof(*ctx_drvdata->sids))) { |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | ctx_drvdata->nsid = nsid; |
| 198 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int __devinit msm_iommu_ctx_probe(struct platform_device *pdev) |
| 203 | { |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 204 | struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL; |
| 205 | int ret; |
| 206 | |
| 207 | if (!pdev->dev.parent) |
| 208 | return -EINVAL; |
| 209 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 210 | ctx_drvdata = devm_kzalloc(&pdev->dev, sizeof(*ctx_drvdata), |
| 211 | GFP_KERNEL); |
| 212 | if (!ctx_drvdata) |
| 213 | return -ENOMEM; |
| 214 | |
| 215 | ctx_drvdata->pdev = pdev; |
| 216 | INIT_LIST_HEAD(&ctx_drvdata->attached_elm); |
| 217 | platform_set_drvdata(pdev, ctx_drvdata); |
| 218 | |
Sathish Ambley | cf045e6 | 2012-06-07 12:56:50 -0700 | [diff] [blame] | 219 | ret = msm_iommu_ctx_parse_dt(pdev, ctx_drvdata); |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 220 | if (!ret) |
| 221 | dev_info(&pdev->dev, "context %s using bank %d\n", |
Stepan Moskovchenko | e14ca5c | 2012-07-11 12:40:51 -0700 | [diff] [blame] | 222 | ctx_drvdata->name, ctx_drvdata->num); |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 223 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | static int __devexit msm_iommu_ctx_remove(struct platform_device *pdev) |
| 228 | { |
| 229 | platform_set_drvdata(pdev, NULL); |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static struct of_device_id msm_iommu_match_table[] = { |
| 234 | { .compatible = "qcom,msm-smmu-v2", }, |
| 235 | {} |
| 236 | }; |
| 237 | |
| 238 | static struct platform_driver msm_iommu_driver = { |
| 239 | .driver = { |
| 240 | .name = "msm_iommu_v2", |
| 241 | .of_match_table = msm_iommu_match_table, |
| 242 | }, |
| 243 | .probe = msm_iommu_probe, |
| 244 | .remove = __devexit_p(msm_iommu_remove), |
| 245 | }; |
| 246 | |
| 247 | static struct of_device_id msm_iommu_ctx_match_table[] = { |
| 248 | { .name = "qcom,iommu-ctx", }, |
| 249 | {} |
| 250 | }; |
| 251 | |
| 252 | static struct platform_driver msm_iommu_ctx_driver = { |
| 253 | .driver = { |
| 254 | .name = "msm_iommu_ctx_v2", |
| 255 | .of_match_table = msm_iommu_ctx_match_table, |
| 256 | }, |
| 257 | .probe = msm_iommu_ctx_probe, |
| 258 | .remove = __devexit_p(msm_iommu_ctx_remove), |
| 259 | }; |
| 260 | |
| 261 | static int __init msm_iommu_driver_init(void) |
| 262 | { |
| 263 | struct device_node *node; |
| 264 | int ret; |
| 265 | |
| 266 | node = of_find_compatible_node(NULL, NULL, "qcom,msm-smmu-v2"); |
| 267 | if (!node) |
| 268 | return -ENODEV; |
| 269 | |
| 270 | of_node_put(node); |
| 271 | |
| 272 | msm_iommu_root_dev = platform_device_register_simple( |
| 273 | "msm_iommu", -1, 0, 0); |
| 274 | if (!msm_iommu_root_dev) { |
| 275 | pr_err("Failed to create root IOMMU device\n"); |
| 276 | ret = -ENODEV; |
| 277 | goto error; |
| 278 | } |
| 279 | |
| 280 | atomic_inc(&msm_iommu_next_id); |
| 281 | |
| 282 | ret = platform_driver_register(&msm_iommu_driver); |
| 283 | if (ret != 0) { |
| 284 | pr_err("Failed to register IOMMU driver\n"); |
| 285 | goto error; |
| 286 | } |
| 287 | |
| 288 | ret = platform_driver_register(&msm_iommu_ctx_driver); |
| 289 | if (ret != 0) { |
| 290 | pr_err("Failed to register IOMMU context driver\n"); |
| 291 | goto error; |
| 292 | } |
| 293 | |
| 294 | error: |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | static void __exit msm_iommu_driver_exit(void) |
| 299 | { |
| 300 | platform_driver_unregister(&msm_iommu_ctx_driver); |
| 301 | platform_driver_unregister(&msm_iommu_driver); |
| 302 | platform_device_unregister(msm_iommu_root_dev); |
| 303 | } |
| 304 | |
| 305 | subsys_initcall(msm_iommu_driver_init); |
| 306 | module_exit(msm_iommu_driver_exit); |
| 307 | |
| 308 | MODULE_LICENSE("GPL v2"); |