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