Olav Haugan | e6d01ef | 2013-01-25 16:55:44 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved. |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -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 | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 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> |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 24 | #include <linux/list.h> |
| 25 | #include <linux/mutex.h> |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 26 | #include <linux/of.h> |
| 27 | #include <linux/of_address.h> |
| 28 | #include <linux/of_device.h> |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 29 | |
Olav Haugan | c599314 | 2013-02-04 13:59:39 -0800 | [diff] [blame] | 30 | #include <mach/iommu_perfmon.h> |
Olav Haugan | e6d01ef | 2013-01-25 16:55:44 -0800 | [diff] [blame] | 31 | #include <mach/iommu_hw-v0.h> |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 32 | #include <mach/iommu.h> |
| 33 | |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 34 | static DEFINE_MUTEX(iommu_list_lock); |
| 35 | static LIST_HEAD(iommu_list); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 36 | |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 37 | void msm_iommu_add_drv(struct msm_iommu_drvdata *drv) |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 38 | { |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 39 | mutex_lock(&iommu_list_lock); |
| 40 | list_add(&drv->list, &iommu_list); |
| 41 | mutex_unlock(&iommu_list_lock); |
| 42 | } |
| 43 | |
| 44 | void msm_iommu_remove_drv(struct msm_iommu_drvdata *drv) |
| 45 | { |
| 46 | mutex_lock(&iommu_list_lock); |
| 47 | list_del(&drv->list); |
| 48 | mutex_unlock(&iommu_list_lock); |
| 49 | } |
| 50 | |
| 51 | static int find_iommu_ctx(struct device *dev, void *data) |
| 52 | { |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 53 | struct msm_iommu_ctx_drvdata *c; |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 54 | |
Sathish Ambley | d1b89ed | 2012-02-07 21:47:47 -0800 | [diff] [blame] | 55 | c = dev_get_drvdata(dev); |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 56 | if (!c || !c->name) |
| 57 | return 0; |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 58 | |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 59 | return !strcmp(data, c->name); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 62 | static struct device *find_context(struct device *dev, const char *name) |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 63 | { |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 64 | return device_find_child(dev, (void *)name, find_iommu_ctx); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | struct device *msm_iommu_get_ctx(const char *ctx_name) |
| 68 | { |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 69 | struct msm_iommu_drvdata *drv; |
| 70 | struct device *dev = NULL; |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 71 | |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 72 | mutex_lock(&iommu_list_lock); |
| 73 | list_for_each_entry(drv, &iommu_list, list) { |
| 74 | dev = find_context(drv->dev, ctx_name); |
| 75 | if (dev) |
| 76 | break; |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 77 | } |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 78 | mutex_unlock(&iommu_list_lock); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 79 | |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 80 | if (!dev || !dev_get_drvdata(dev)) |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 81 | pr_err("Could not find context <%s>\n", ctx_name); |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 82 | put_device(dev); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 83 | |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 84 | return dev; |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 85 | } |
| 86 | EXPORT_SYMBOL(msm_iommu_get_ctx); |
| 87 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 88 | static void msm_iommu_reset(void __iomem *base, void __iomem *glb_base, int ncb) |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 89 | { |
Stepan Moskovchenko | a43d8c1 | 2011-02-24 18:00:42 -0800 | [diff] [blame] | 90 | int ctx; |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 91 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 92 | SET_RPUE(glb_base, 0); |
| 93 | SET_RPUEIE(glb_base, 0); |
| 94 | SET_ESRRESTORE(glb_base, 0); |
| 95 | SET_TBE(glb_base, 0); |
| 96 | SET_CR(glb_base, 0); |
| 97 | SET_SPDMBE(glb_base, 0); |
| 98 | SET_TESTBUSCR(glb_base, 0); |
| 99 | SET_TLBRSW(glb_base, 0); |
| 100 | SET_GLOBAL_TLBIALL(glb_base, 0); |
| 101 | SET_RPU_ACR(glb_base, 0); |
| 102 | SET_TLBLKCRWE(glb_base, 1); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 103 | |
| 104 | for (ctx = 0; ctx < ncb; ctx++) { |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 105 | SET_BPRCOSH(glb_base, ctx, 0); |
| 106 | SET_BPRCISH(glb_base, ctx, 0); |
| 107 | SET_BPRCNSH(glb_base, ctx, 0); |
| 108 | SET_BPSHCFG(glb_base, ctx, 0); |
| 109 | SET_BPMTCFG(glb_base, ctx, 0); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 110 | SET_ACTLR(base, ctx, 0); |
| 111 | SET_SCTLR(base, ctx, 0); |
| 112 | SET_FSRRESTORE(base, ctx, 0); |
| 113 | SET_TTBR0(base, ctx, 0); |
| 114 | SET_TTBR1(base, ctx, 0); |
| 115 | SET_TTBCR(base, ctx, 0); |
| 116 | SET_BFBCR(base, ctx, 0); |
| 117 | SET_PAR(base, ctx, 0); |
| 118 | SET_FAR(base, ctx, 0); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 119 | SET_TLBFLPTER(base, ctx, 0); |
| 120 | SET_TLBSLPTER(base, ctx, 0); |
| 121 | SET_TLBLKCR(base, ctx, 0); |
Stepan Moskovchenko | 0272fd6 | 2011-09-26 15:06:40 -0700 | [diff] [blame] | 122 | SET_CTX_TLBIALL(base, ctx, 0); |
| 123 | SET_TLBIVA(base, ctx, 0); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 124 | SET_PRRR(base, ctx, 0); |
| 125 | SET_NMRR(base, ctx, 0); |
| 126 | SET_CONTEXTIDR(base, ctx, 0); |
| 127 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 128 | mb(); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 131 | static int msm_iommu_parse_dt(struct platform_device *pdev, |
Olav Haugan | 97ce7aa | 2013-04-30 13:59:41 -0700 | [diff] [blame] | 132 | struct msm_iommu_drvdata *drvdata, |
| 133 | int *needs_alt_core_clk) |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 134 | { |
| 135 | #ifdef CONFIG_OF_DEVICE |
| 136 | struct device_node *child; |
| 137 | struct resource *r; |
| 138 | u32 glb_offset = 0; |
Olav Haugan | 7a2f99c | 2013-02-04 14:43:26 -0800 | [diff] [blame] | 139 | int ret; |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 140 | |
| 141 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 142 | if (!r) { |
| 143 | pr_err("%s: Missing property reg\n", __func__); |
| 144 | return -EINVAL; |
| 145 | } |
| 146 | drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r)); |
| 147 | if (!drvdata->base) { |
Stepan Moskovchenko | 61fa3db | 2012-08-10 17:51:18 +0100 | [diff] [blame] | 148 | pr_err("%s: Unable to ioremap %pr\n", __func__, r); |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 149 | return -ENOMEM; |
| 150 | } |
| 151 | drvdata->glb_base = drvdata->base; |
| 152 | |
| 153 | if (!of_property_read_u32(pdev->dev.of_node, "qcom,glb-offset", |
| 154 | &glb_offset)) { |
| 155 | drvdata->glb_base += glb_offset; |
| 156 | } else { |
| 157 | pr_err("%s: Missing property qcom,glb-offset\n", __func__); |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
| 161 | for_each_child_of_node(pdev->dev.of_node, child) { |
| 162 | drvdata->ncb++; |
| 163 | if (!of_platform_device_create(child, NULL, &pdev->dev)) |
| 164 | pr_err("Failed to create %s device\n", child->name); |
| 165 | } |
| 166 | |
Olav Haugan | 7a2f99c | 2013-02-04 14:43:26 -0800 | [diff] [blame] | 167 | ret = of_property_read_string(pdev->dev.of_node, "label", |
| 168 | &drvdata->name); |
| 169 | if (ret) { |
| 170 | pr_err("%s: Missing property label\n", __func__); |
| 171 | return -EINVAL; |
| 172 | } |
Olav Haugan | 97ce7aa | 2013-04-30 13:59:41 -0700 | [diff] [blame] | 173 | |
| 174 | *needs_alt_core_clk = of_property_read_bool(pdev->dev.of_node, |
| 175 | "qcom,needs-alt-core-clk"); |
| 176 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 177 | drvdata->sec_id = -1; |
| 178 | drvdata->ttbr_split = 0; |
| 179 | #endif |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static int __get_clocks(struct platform_device *pdev, |
Olav Haugan | 97ce7aa | 2013-04-30 13:59:41 -0700 | [diff] [blame] | 184 | struct msm_iommu_drvdata *drvdata, |
| 185 | int needs_alt_core_clk) |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 186 | { |
| 187 | int ret = 0; |
| 188 | |
| 189 | drvdata->pclk = clk_get(&pdev->dev, "iface_clk"); |
| 190 | if (IS_ERR(drvdata->pclk)) { |
| 191 | ret = PTR_ERR(drvdata->pclk); |
| 192 | drvdata->pclk = NULL; |
| 193 | pr_err("Unable to get %s clock for %s IOMMU device\n", |
| 194 | dev_name(&pdev->dev), drvdata->name); |
| 195 | goto fail; |
| 196 | } |
| 197 | |
| 198 | drvdata->clk = clk_get(&pdev->dev, "core_clk"); |
| 199 | |
| 200 | if (!IS_ERR(drvdata->clk)) { |
| 201 | if (clk_get_rate(drvdata->clk) == 0) { |
| 202 | ret = clk_round_rate(drvdata->clk, 1000); |
| 203 | clk_set_rate(drvdata->clk, ret); |
| 204 | } |
| 205 | } else { |
| 206 | drvdata->clk = NULL; |
| 207 | } |
Olav Haugan | 97ce7aa | 2013-04-30 13:59:41 -0700 | [diff] [blame] | 208 | |
| 209 | if (needs_alt_core_clk) { |
| 210 | drvdata->aclk = devm_clk_get(&pdev->dev, "alt_core_clk"); |
| 211 | if (IS_ERR(drvdata->aclk)) |
| 212 | return PTR_ERR(drvdata->aclk); |
| 213 | } |
| 214 | |
| 215 | if (drvdata->aclk && clk_get_rate(drvdata->aclk) == 0) { |
| 216 | ret = clk_round_rate(drvdata->aclk, 1000); |
| 217 | clk_set_rate(drvdata->aclk, ret); |
| 218 | } |
| 219 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 220 | return 0; |
| 221 | fail: |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | static void __put_clocks(struct msm_iommu_drvdata *drvdata) |
| 226 | { |
Olav Haugan | 97ce7aa | 2013-04-30 13:59:41 -0700 | [diff] [blame] | 227 | if (drvdata->aclk) |
| 228 | clk_put(drvdata->aclk); |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 229 | if (drvdata->clk) |
| 230 | clk_put(drvdata->clk); |
| 231 | clk_put(drvdata->pclk); |
| 232 | } |
| 233 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 234 | /* |
| 235 | * Do a basic check of the IOMMU by performing an ATS operation |
| 236 | * on context bank 0. |
| 237 | */ |
| 238 | static int iommu_sanity_check(struct msm_iommu_drvdata *drvdata) |
| 239 | { |
| 240 | int par; |
| 241 | int ret = 0; |
| 242 | |
| 243 | SET_M(drvdata->base, 0, 1); |
| 244 | SET_PAR(drvdata->base, 0, 0); |
| 245 | SET_V2PCFG(drvdata->base, 0, 1); |
| 246 | SET_V2PPR(drvdata->base, 0, 0); |
| 247 | mb(); |
| 248 | par = GET_PAR(drvdata->base, 0); |
| 249 | SET_V2PCFG(drvdata->base, 0, 0); |
| 250 | SET_M(drvdata->base, 0, 0); |
| 251 | mb(); |
| 252 | |
| 253 | if (!par) { |
| 254 | pr_err("%s: Invalid PAR value detected\n", drvdata->name); |
| 255 | ret = -ENODEV; |
| 256 | } |
| 257 | return ret; |
| 258 | } |
| 259 | |
Olav Haugan | c599314 | 2013-02-04 13:59:39 -0800 | [diff] [blame] | 260 | static int msm_iommu_pmon_parse_dt(struct platform_device *pdev, |
| 261 | struct iommu_pmon *pmon_info) |
| 262 | { |
| 263 | int ret = 0; |
| 264 | int irq = platform_get_irq(pdev, 0); |
| 265 | unsigned int cls_prop_size; |
| 266 | |
| 267 | if (irq > 0) { |
| 268 | pmon_info->iommu.evt_irq = platform_get_irq(pdev, 0); |
| 269 | |
| 270 | ret = of_property_read_u32(pdev->dev.of_node, |
| 271 | "qcom,iommu-pmu-ngroups", |
| 272 | &pmon_info->num_groups); |
| 273 | if (ret) { |
| 274 | pr_err("Error reading qcom,iommu-pmu-ngroups\n"); |
| 275 | goto fail; |
| 276 | } |
| 277 | ret = of_property_read_u32(pdev->dev.of_node, |
| 278 | "qcom,iommu-pmu-ncounters", |
| 279 | &pmon_info->num_counters); |
| 280 | if (ret) { |
| 281 | pr_err("Error reading qcom,iommu-pmu-ncounters\n"); |
| 282 | goto fail; |
| 283 | } |
| 284 | |
| 285 | if (!of_get_property(pdev->dev.of_node, |
| 286 | "qcom,iommu-pmu-event-classes", |
| 287 | &cls_prop_size)) { |
| 288 | pr_err("Error reading qcom,iommu-pmu-event-classes\n"); |
| 289 | return -EINVAL; |
| 290 | } |
| 291 | |
| 292 | pmon_info->event_cls_supported = |
| 293 | devm_kzalloc(&pdev->dev, cls_prop_size, GFP_KERNEL); |
| 294 | |
| 295 | if (!pmon_info->event_cls_supported) { |
| 296 | pr_err("Unable to get memory for event class array\n"); |
| 297 | return -ENOMEM; |
| 298 | } |
| 299 | |
| 300 | pmon_info->nevent_cls_supported = cls_prop_size / sizeof(u32); |
| 301 | |
| 302 | ret = of_property_read_u32_array(pdev->dev.of_node, |
| 303 | "qcom,iommu-pmu-event-classes", |
| 304 | pmon_info->event_cls_supported, |
| 305 | pmon_info->nevent_cls_supported); |
| 306 | if (ret) { |
| 307 | pr_err("Error reading qcom,iommu-pmu-event-classes\n"); |
| 308 | return ret; |
| 309 | } |
| 310 | } else { |
| 311 | pmon_info->iommu.evt_irq = -1; |
| 312 | ret = irq; |
| 313 | } |
| 314 | |
| 315 | fail: |
| 316 | return ret; |
| 317 | } |
| 318 | |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 319 | static int msm_iommu_probe(struct platform_device *pdev) |
| 320 | { |
Olav Haugan | c599314 | 2013-02-04 13:59:39 -0800 | [diff] [blame] | 321 | struct iommu_pmon *pmon_info; |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 322 | struct msm_iommu_drvdata *drvdata; |
| 323 | struct msm_iommu_dev *iommu_dev = pdev->dev.platform_data; |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 324 | int ret; |
Olav Haugan | 97ce7aa | 2013-04-30 13:59:41 -0700 | [diff] [blame] | 325 | int needs_alt_core_clk = 0; |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 326 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 327 | drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 328 | |
Stepan Moskovchenko | b61401a | 2011-02-28 16:03:02 -0800 | [diff] [blame] | 329 | if (!drvdata) { |
| 330 | ret = -ENOMEM; |
| 331 | goto fail; |
| 332 | } |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 333 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 334 | if (pdev->dev.of_node) { |
Olav Haugan | 97ce7aa | 2013-04-30 13:59:41 -0700 | [diff] [blame] | 335 | ret = msm_iommu_parse_dt(pdev, drvdata, &needs_alt_core_clk); |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 336 | if (ret) |
| 337 | goto fail; |
| 338 | } else if (pdev->dev.platform_data) { |
| 339 | struct resource *r, *r2; |
| 340 | resource_size_t len; |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 341 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 342 | r = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 343 | "physbase"); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 344 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 345 | if (!r) { |
| 346 | ret = -ENODEV; |
| 347 | goto fail; |
Matt Wagantall | 9985822 | 2011-11-08 14:31:27 -0800 | [diff] [blame] | 348 | } |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 349 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 350 | len = resource_size(r); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 351 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 352 | r2 = request_mem_region(r->start, len, r->name); |
| 353 | if (!r2) { |
Stepan Moskovchenko | 61fa3db | 2012-08-10 17:51:18 +0100 | [diff] [blame] | 354 | pr_err("Could not request memory region: %pr\n", r); |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 355 | ret = -EBUSY; |
| 356 | goto fail; |
| 357 | } |
Stepan Moskovchenko | b61401a | 2011-02-28 16:03:02 -0800 | [diff] [blame] | 358 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 359 | drvdata->base = devm_ioremap(&pdev->dev, r2->start, len); |
Stepan Moskovchenko | b61401a | 2011-02-28 16:03:02 -0800 | [diff] [blame] | 360 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 361 | if (!drvdata->base) { |
Stepan Moskovchenko | 61fa3db | 2012-08-10 17:51:18 +0100 | [diff] [blame] | 362 | pr_err("Could not ioremap: %pr\n", r); |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 363 | ret = -EBUSY; |
| 364 | goto fail; |
| 365 | } |
| 366 | /* |
| 367 | * Global register space offset for legacy IOMMUv1 hardware |
| 368 | * is always 0xFF000 |
| 369 | */ |
| 370 | drvdata->glb_base = drvdata->base + 0xFF000; |
| 371 | drvdata->name = iommu_dev->name; |
| 372 | drvdata->dev = &pdev->dev; |
| 373 | drvdata->ncb = iommu_dev->ncb; |
| 374 | drvdata->ttbr_split = iommu_dev->ttbr_split; |
| 375 | } else { |
Stepan Moskovchenko | b61401a | 2011-02-28 16:03:02 -0800 | [diff] [blame] | 376 | ret = -ENODEV; |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 377 | goto fail; |
Stepan Moskovchenko | b61401a | 2011-02-28 16:03:02 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 380 | drvdata->dev = &pdev->dev; |
| 381 | |
Olav Haugan | 97ce7aa | 2013-04-30 13:59:41 -0700 | [diff] [blame] | 382 | ret = __get_clocks(pdev, drvdata, needs_alt_core_clk); |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 383 | |
| 384 | if (ret) |
| 385 | goto fail; |
| 386 | |
Olav Haugan | 97ce7aa | 2013-04-30 13:59:41 -0700 | [diff] [blame] | 387 | iommu_access_ops_v0.iommu_clk_on(drvdata); |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 388 | |
| 389 | msm_iommu_reset(drvdata->base, drvdata->glb_base, drvdata->ncb); |
| 390 | |
| 391 | ret = iommu_sanity_check(drvdata); |
| 392 | if (ret) |
| 393 | goto fail_clk; |
Stepan Moskovchenko | b61401a | 2011-02-28 16:03:02 -0800 | [diff] [blame] | 394 | |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 395 | pr_info("device %s mapped at %p, with %d ctx banks\n", |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 396 | drvdata->name, drvdata->base, drvdata->ncb); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 397 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 398 | msm_iommu_add_drv(drvdata); |
Stepan Moskovchenko | b61401a | 2011-02-28 16:03:02 -0800 | [diff] [blame] | 399 | platform_set_drvdata(pdev, drvdata); |
| 400 | |
Olav Haugan | 97ce7aa | 2013-04-30 13:59:41 -0700 | [diff] [blame] | 401 | iommu_access_ops_v0.iommu_clk_off(drvdata); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 402 | |
Olav Haugan | c599314 | 2013-02-04 13:59:39 -0800 | [diff] [blame] | 403 | pmon_info = msm_iommu_pm_alloc(&pdev->dev); |
| 404 | if (pmon_info != NULL) { |
| 405 | ret = msm_iommu_pmon_parse_dt(pdev, pmon_info); |
| 406 | if (ret) { |
| 407 | msm_iommu_pm_free(&pdev->dev); |
| 408 | pr_info("%s: pmon not available.\n", drvdata->name); |
| 409 | } else { |
| 410 | pmon_info->iommu.base = drvdata->base; |
| 411 | pmon_info->iommu.ops = &iommu_access_ops_v0; |
| 412 | pmon_info->iommu.hw_ops = iommu_pm_get_hw_ops_v0(); |
| 413 | pmon_info->iommu.iommu_name = drvdata->name; |
Olav Haugan | 463e640 | 2013-04-15 10:53:32 -0700 | [diff] [blame] | 414 | pmon_info->iommu.always_on = 1; |
Olav Haugan | c599314 | 2013-02-04 13:59:39 -0800 | [diff] [blame] | 415 | ret = msm_iommu_pm_iommu_register(pmon_info); |
| 416 | if (ret) { |
| 417 | pr_err("%s iommu register fail\n", |
| 418 | drvdata->name); |
| 419 | msm_iommu_pm_free(&pdev->dev); |
| 420 | } else { |
| 421 | pr_debug("%s iommu registered for pmon\n", |
| 422 | pmon_info->iommu.iommu_name); |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 427 | return 0; |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 428 | |
Stepan Moskovchenko | b61401a | 2011-02-28 16:03:02 -0800 | [diff] [blame] | 429 | fail_clk: |
Olav Haugan | 97ce7aa | 2013-04-30 13:59:41 -0700 | [diff] [blame] | 430 | iommu_access_ops_v0.iommu_clk_off(drvdata); |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 431 | __put_clocks(drvdata); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 432 | fail: |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | static int msm_iommu_remove(struct platform_device *pdev) |
| 437 | { |
| 438 | struct msm_iommu_drvdata *drv = NULL; |
| 439 | |
| 440 | drv = platform_get_drvdata(pdev); |
| 441 | if (drv) { |
Stephen Boyd | 55742b7 | 2012-08-08 11:40:26 -0700 | [diff] [blame] | 442 | msm_iommu_remove_drv(drv); |
Stepan Moskovchenko | b61401a | 2011-02-28 16:03:02 -0800 | [diff] [blame] | 443 | if (drv->clk) |
| 444 | clk_put(drv->clk); |
| 445 | clk_put(drv->pclk); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 446 | platform_set_drvdata(pdev, NULL); |
| 447 | } |
| 448 | return 0; |
| 449 | } |
| 450 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 451 | static int msm_iommu_ctx_parse_dt(struct platform_device *pdev, |
| 452 | struct msm_iommu_ctx_drvdata *ctx_drvdata) |
| 453 | { |
| 454 | struct resource *r, rp; |
| 455 | int irq, ret; |
| 456 | u32 nmid_array_size; |
| 457 | u32 nmid; |
| 458 | |
| 459 | irq = platform_get_irq(pdev, 0); |
| 460 | if (irq > 0) { |
| 461 | ret = request_threaded_irq(irq, NULL, |
| 462 | msm_iommu_fault_handler, |
| 463 | IRQF_ONESHOT | IRQF_SHARED, |
Olav Haugan | 3b9d854 | 2013-02-28 17:49:00 -0800 | [diff] [blame] | 464 | "msm_iommu_nonsecure_irq", ctx_drvdata); |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 465 | if (ret) { |
| 466 | pr_err("Request IRQ %d failed with ret=%d\n", irq, ret); |
| 467 | return ret; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 472 | if (!r) { |
| 473 | pr_err("Could not find reg property for context bank\n"); |
| 474 | return -EINVAL; |
| 475 | } |
| 476 | |
| 477 | ret = of_address_to_resource(pdev->dev.parent->of_node, 0, &rp); |
| 478 | if (ret) { |
| 479 | pr_err("of_address_to_resource failed\n"); |
| 480 | return -EINVAL; |
| 481 | } |
| 482 | |
| 483 | /* Calculate the context bank number using the base addresses. CB0 |
| 484 | * starts at the base address. |
| 485 | */ |
| 486 | ctx_drvdata->num = ((r->start - rp.start) >> CTX_SHIFT); |
| 487 | |
| 488 | if (of_property_read_string(pdev->dev.of_node, "label", |
| 489 | &ctx_drvdata->name)) { |
| 490 | pr_err("Could not find label property\n"); |
| 491 | return -EINVAL; |
| 492 | } |
| 493 | |
| 494 | if (!of_get_property(pdev->dev.of_node, "qcom,iommu-ctx-mids", |
| 495 | &nmid_array_size)) { |
| 496 | pr_err("Could not find iommu-ctx-mids property\n"); |
| 497 | return -EINVAL; |
| 498 | } |
| 499 | if (nmid_array_size >= sizeof(ctx_drvdata->sids)) { |
| 500 | pr_err("Too many mids defined - array size: %u, mids size: %u\n", |
| 501 | nmid_array_size, sizeof(ctx_drvdata->sids)); |
| 502 | return -EINVAL; |
| 503 | } |
| 504 | nmid = nmid_array_size / sizeof(*ctx_drvdata->sids); |
| 505 | |
| 506 | if (of_property_read_u32_array(pdev->dev.of_node, "qcom,iommu-ctx-mids", |
| 507 | ctx_drvdata->sids, nmid)) { |
| 508 | pr_err("Could not find iommu-ctx-mids property\n"); |
| 509 | return -EINVAL; |
| 510 | } |
| 511 | ctx_drvdata->nsid = nmid; |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | static void __program_m2v_tables(struct msm_iommu_drvdata *drvdata, |
| 517 | struct msm_iommu_ctx_drvdata *ctx_drvdata) |
| 518 | { |
| 519 | int i; |
| 520 | |
| 521 | /* Program the M2V tables for this context */ |
| 522 | for (i = 0; i < ctx_drvdata->nsid; i++) { |
| 523 | int sid = ctx_drvdata->sids[i]; |
| 524 | int num = ctx_drvdata->num; |
| 525 | |
| 526 | SET_M2VCBR_N(drvdata->glb_base, sid, 0); |
| 527 | SET_CBACR_N(drvdata->glb_base, num, 0); |
| 528 | |
| 529 | /* Route page faults to the non-secure interrupt */ |
| 530 | SET_IRPTNDX(drvdata->glb_base, num, 1); |
| 531 | |
| 532 | /* Set VMID = 0 */ |
| 533 | SET_VMID(drvdata->glb_base, sid, 0); |
| 534 | |
| 535 | /* Set the context number for that SID to this context */ |
| 536 | SET_CBNDX(drvdata->glb_base, sid, num); |
| 537 | |
| 538 | /* Set SID associated with this context bank to 0 */ |
| 539 | SET_CBVMID(drvdata->glb_base, num, 0); |
| 540 | |
| 541 | /* Set the ASID for TLB tagging for this context to 0 */ |
| 542 | SET_CONTEXTIDR_ASID(drvdata->base, num, 0); |
| 543 | |
| 544 | /* Set security bit override to be Non-secure */ |
| 545 | SET_NSCFG(drvdata->glb_base, sid, 3); |
| 546 | } |
| 547 | mb(); |
| 548 | } |
| 549 | |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 550 | static int msm_iommu_ctx_probe(struct platform_device *pdev) |
| 551 | { |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 552 | struct msm_iommu_drvdata *drvdata; |
| 553 | struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL; |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 554 | int i, ret, irq; |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 555 | if (!pdev->dev.parent) { |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 556 | ret = -EINVAL; |
| 557 | goto fail; |
| 558 | } |
| 559 | |
| 560 | drvdata = dev_get_drvdata(pdev->dev.parent); |
| 561 | |
| 562 | if (!drvdata) { |
| 563 | ret = -ENODEV; |
| 564 | goto fail; |
| 565 | } |
| 566 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 567 | ctx_drvdata = devm_kzalloc(&pdev->dev, sizeof(*ctx_drvdata), |
| 568 | GFP_KERNEL); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 569 | if (!ctx_drvdata) { |
| 570 | ret = -ENOMEM; |
| 571 | goto fail; |
| 572 | } |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 573 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 574 | ctx_drvdata->pdev = pdev; |
| 575 | INIT_LIST_HEAD(&ctx_drvdata->attached_elm); |
| 576 | platform_set_drvdata(pdev, ctx_drvdata); |
Olav Haugan | e99ee7e | 2012-12-11 15:02:02 -0800 | [diff] [blame] | 577 | ctx_drvdata->attach_count = 0; |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 578 | |
| 579 | if (pdev->dev.of_node) { |
| 580 | ret = msm_iommu_ctx_parse_dt(pdev, ctx_drvdata); |
| 581 | if (ret) |
| 582 | goto fail; |
| 583 | } else if (pdev->dev.platform_data) { |
| 584 | struct msm_iommu_ctx_dev *c = pdev->dev.platform_data; |
| 585 | |
| 586 | ctx_drvdata->num = c->num; |
| 587 | ctx_drvdata->name = c->name; |
| 588 | |
| 589 | for (i = 0; i < MAX_NUM_MIDS; ++i) { |
| 590 | if (c->mids[i] == -1) { |
| 591 | ctx_drvdata->nsid = i; |
| 592 | break; |
| 593 | } |
| 594 | ctx_drvdata->sids[i] = c->mids[i]; |
| 595 | } |
| 596 | irq = platform_get_irq_byname( |
| 597 | to_platform_device(pdev->dev.parent), |
| 598 | "nonsecure_irq"); |
| 599 | if (irq < 0) { |
| 600 | ret = -ENODEV; |
| 601 | goto fail; |
| 602 | } |
| 603 | |
| 604 | ret = request_threaded_irq(irq, NULL, msm_iommu_fault_handler, |
| 605 | IRQF_ONESHOT | IRQF_SHARED, |
| 606 | "msm_iommu_nonsecure_irq", ctx_drvdata); |
| 607 | |
| 608 | if (ret) { |
| 609 | pr_err("request_threaded_irq %d failed: %d\n", irq, |
| 610 | ret); |
| 611 | goto fail; |
| 612 | } |
| 613 | } else { |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 614 | ret = -ENODEV; |
| 615 | goto fail; |
| 616 | } |
| 617 | |
Olav Haugan | 97ce7aa | 2013-04-30 13:59:41 -0700 | [diff] [blame] | 618 | iommu_access_ops_v0.iommu_clk_on(drvdata); |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 619 | __program_m2v_tables(drvdata, ctx_drvdata); |
Olav Haugan | 97ce7aa | 2013-04-30 13:59:41 -0700 | [diff] [blame] | 620 | iommu_access_ops_v0.iommu_clk_off(drvdata); |
Stepan Moskovchenko | 73a50f6 | 2012-05-03 17:29:12 -0700 | [diff] [blame] | 621 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 622 | dev_info(&pdev->dev, "context %s using bank %d\n", ctx_drvdata->name, |
| 623 | ctx_drvdata->num); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 624 | return 0; |
| 625 | fail: |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 626 | return ret; |
| 627 | } |
| 628 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 629 | static int __devexit msm_iommu_ctx_remove(struct platform_device *pdev) |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 630 | { |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 631 | platform_set_drvdata(pdev, NULL); |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 632 | return 0; |
| 633 | } |
| 634 | |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 635 | |
| 636 | static struct of_device_id msm_iommu_match_table[] = { |
Olav Haugan | 0e22c48 | 2013-01-28 17:39:36 -0800 | [diff] [blame] | 637 | { .compatible = "qcom,msm-smmu-v0", }, |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 638 | {} |
| 639 | }; |
| 640 | |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 641 | static struct platform_driver msm_iommu_driver = { |
| 642 | .driver = { |
Olav Haugan | 0e22c48 | 2013-01-28 17:39:36 -0800 | [diff] [blame] | 643 | .name = "msm_iommu-v0", |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 644 | .of_match_table = msm_iommu_match_table, |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 645 | }, |
| 646 | .probe = msm_iommu_probe, |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 647 | .remove = __devexit_p(msm_iommu_remove), |
| 648 | }; |
| 649 | |
| 650 | static struct of_device_id msm_iommu_ctx_match_table[] = { |
| 651 | { .name = "qcom,iommu-ctx", }, |
| 652 | {} |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 653 | }; |
| 654 | |
| 655 | static struct platform_driver msm_iommu_ctx_driver = { |
| 656 | .driver = { |
| 657 | .name = "msm_iommu_ctx", |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 658 | .of_match_table = msm_iommu_ctx_match_table, |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 659 | }, |
| 660 | .probe = msm_iommu_ctx_probe, |
Olav Haugan | 95d2416 | 2012-12-05 14:47:47 -0800 | [diff] [blame] | 661 | .remove = __devexit_p(msm_iommu_ctx_remove), |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 662 | }; |
| 663 | |
Stepan Moskovchenko | 516cbc7 | 2010-11-12 19:29:53 -0800 | [diff] [blame] | 664 | static int __init msm_iommu_driver_init(void) |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 665 | { |
| 666 | int ret; |
| 667 | ret = platform_driver_register(&msm_iommu_driver); |
| 668 | if (ret != 0) { |
| 669 | pr_err("Failed to register IOMMU driver\n"); |
| 670 | goto error; |
| 671 | } |
| 672 | |
| 673 | ret = platform_driver_register(&msm_iommu_ctx_driver); |
| 674 | if (ret != 0) { |
| 675 | pr_err("Failed to register IOMMU context driver\n"); |
| 676 | goto error; |
| 677 | } |
| 678 | |
| 679 | error: |
| 680 | return ret; |
| 681 | } |
| 682 | |
Stepan Moskovchenko | 516cbc7 | 2010-11-12 19:29:53 -0800 | [diff] [blame] | 683 | static void __exit msm_iommu_driver_exit(void) |
Stepan Moskovchenko | c6a5951 | 2010-08-24 18:32:38 -0700 | [diff] [blame] | 684 | { |
| 685 | platform_driver_unregister(&msm_iommu_ctx_driver); |
| 686 | platform_driver_unregister(&msm_iommu_driver); |
| 687 | } |
| 688 | |
| 689 | subsys_initcall(msm_iommu_driver_init); |
| 690 | module_exit(msm_iommu_driver_exit); |
| 691 | |
| 692 | MODULE_LICENSE("GPL v2"); |
| 693 | MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>"); |