Olav Haugan | f378273 | 2013-01-11 11:23:30 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -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. |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/list.h> |
| 21 | #include <linux/mutex.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/iommu.h> |
| 24 | #include <linux/clk.h> |
| 25 | #include <linux/scatterlist.h> |
| 26 | #include <linux/of.h> |
| 27 | #include <linux/of_device.h> |
Michael Bohan | 1834e7f | 2013-01-18 17:16:38 -0800 | [diff] [blame] | 28 | #include <linux/kmemleak.h> |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 29 | |
| 30 | #include <asm/sizes.h> |
| 31 | |
Olav Haugan | 64ffdf3 | 2013-01-24 17:20:24 -0800 | [diff] [blame] | 32 | #include <mach/iommu_perfmon.h> |
Olav Haugan | e6d01ef | 2013-01-25 16:55:44 -0800 | [diff] [blame] | 33 | #include <mach/iommu_hw-v1.h> |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 34 | #include <mach/msm_iommu_priv.h> |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 35 | #include <mach/iommu.h> |
| 36 | #include <mach/scm.h> |
| 37 | |
| 38 | /* bitmap of the page sizes currently supported */ |
| 39 | #define MSM_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M) |
| 40 | |
| 41 | #define IOMMU_SECURE_CFG 2 |
| 42 | #define IOMMU_SECURE_PTBL_SIZE 3 |
| 43 | #define IOMMU_SECURE_PTBL_INIT 4 |
| 44 | #define IOMMU_SECURE_MAP 6 |
| 45 | #define IOMMU_SECURE_UNMAP 7 |
Adrian Alexei | bfe7c46 | 2013-04-01 14:36:24 -0700 | [diff] [blame] | 46 | #define IOMMU_SECURE_MAP2 0x0B |
| 47 | #define IOMMU_SECURE_UNMAP2 0x0C |
| 48 | #define IOMMU_TLBINVAL_FLAG 0x00000001 |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 49 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 50 | static struct iommu_access_ops *iommu_access_ops; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 51 | |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 52 | struct msm_scm_paddr_list { |
| 53 | unsigned int list; |
| 54 | unsigned int list_size; |
| 55 | unsigned int size; |
| 56 | }; |
| 57 | |
| 58 | struct msm_scm_mapping_info { |
| 59 | unsigned int id; |
| 60 | unsigned int ctx_id; |
| 61 | unsigned int va; |
| 62 | unsigned int size; |
| 63 | }; |
| 64 | |
Adrian Alexei | bfe7c46 | 2013-04-01 14:36:24 -0700 | [diff] [blame] | 65 | struct msm_scm_map2_req { |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 66 | struct msm_scm_paddr_list plist; |
| 67 | struct msm_scm_mapping_info info; |
Adrian Alexei | bfe7c46 | 2013-04-01 14:36:24 -0700 | [diff] [blame] | 68 | unsigned int flags; |
| 69 | }; |
| 70 | |
| 71 | struct msm_scm_unmap2_req { |
| 72 | struct msm_scm_mapping_info info; |
| 73 | unsigned int flags; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 76 | void msm_iommu_sec_set_access_ops(struct iommu_access_ops *access_ops) |
| 77 | { |
| 78 | iommu_access_ops = access_ops; |
| 79 | } |
| 80 | |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 81 | static int msm_iommu_sec_ptbl_init(void) |
| 82 | { |
| 83 | struct device_node *np; |
| 84 | struct msm_scm_ptbl_init { |
| 85 | unsigned int paddr; |
| 86 | unsigned int size; |
| 87 | unsigned int spare; |
| 88 | } pinit; |
| 89 | unsigned int *buf; |
Mitchel Humpherys | 637cc53 | 2012-12-12 16:50:58 -0800 | [diff] [blame] | 90 | int psize[2] = {0, 0}; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 91 | unsigned int spare; |
Mitchel Humpherys | 637cc53 | 2012-12-12 16:50:58 -0800 | [diff] [blame] | 92 | int ret, ptbl_ret = 0; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 93 | |
Olav Haugan | 0e22c48 | 2013-01-28 17:39:36 -0800 | [diff] [blame] | 94 | for_each_compatible_node(np, NULL, "qcom,msm-smmu-v1") |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 95 | if (of_find_property(np, "qcom,iommu-secure-id", NULL)) |
| 96 | break; |
| 97 | |
| 98 | if (!np) |
| 99 | return 0; |
| 100 | |
| 101 | of_node_put(np); |
Syed Rameez Mustafa | 6ab6af3 | 2013-03-18 12:53:11 -0700 | [diff] [blame] | 102 | ret = scm_call(SCM_SVC_MP, IOMMU_SECURE_PTBL_SIZE, &spare, |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 103 | sizeof(spare), psize, sizeof(psize)); |
| 104 | if (ret) { |
| 105 | pr_err("scm call IOMMU_SECURE_PTBL_SIZE failed\n"); |
| 106 | goto fail; |
| 107 | } |
| 108 | |
| 109 | if (psize[1]) { |
| 110 | pr_err("scm call IOMMU_SECURE_PTBL_SIZE failed\n"); |
| 111 | goto fail; |
| 112 | } |
| 113 | |
| 114 | buf = kmalloc(psize[0], GFP_KERNEL); |
| 115 | if (!buf) { |
| 116 | pr_err("%s: Failed to allocate %d bytes for PTBL\n", |
| 117 | __func__, psize[0]); |
| 118 | ret = -ENOMEM; |
| 119 | goto fail; |
| 120 | } |
| 121 | |
| 122 | pinit.paddr = virt_to_phys(buf); |
| 123 | pinit.size = psize[0]; |
| 124 | |
Syed Rameez Mustafa | 6ab6af3 | 2013-03-18 12:53:11 -0700 | [diff] [blame] | 125 | ret = scm_call(SCM_SVC_MP, IOMMU_SECURE_PTBL_INIT, &pinit, |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 126 | sizeof(pinit), &ptbl_ret, sizeof(ptbl_ret)); |
| 127 | if (ret) { |
| 128 | pr_err("scm call IOMMU_SECURE_PTBL_INIT failed\n"); |
| 129 | goto fail_mem; |
| 130 | } |
| 131 | if (ptbl_ret) { |
| 132 | pr_err("scm call IOMMU_SECURE_PTBL_INIT extended ret fail\n"); |
| 133 | goto fail_mem; |
| 134 | } |
| 135 | |
Michael Bohan | 1834e7f | 2013-01-18 17:16:38 -0800 | [diff] [blame] | 136 | kmemleak_not_leak(buf); |
| 137 | |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 138 | return 0; |
| 139 | |
| 140 | fail_mem: |
| 141 | kfree(buf); |
| 142 | fail: |
| 143 | return ret; |
| 144 | } |
| 145 | |
Laura Abbott | f4daa69 | 2012-10-10 19:31:53 -0700 | [diff] [blame] | 146 | int msm_iommu_sec_program_iommu(int sec_id) |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 147 | { |
| 148 | struct msm_scm_sec_cfg { |
| 149 | unsigned int id; |
| 150 | unsigned int spare; |
| 151 | } cfg; |
Mitchel Humpherys | 637cc53 | 2012-12-12 16:50:58 -0800 | [diff] [blame] | 152 | int ret, scm_ret = 0; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 153 | |
| 154 | cfg.id = sec_id; |
| 155 | |
Syed Rameez Mustafa | 6ab6af3 | 2013-03-18 12:53:11 -0700 | [diff] [blame] | 156 | ret = scm_call(SCM_SVC_MP, IOMMU_SECURE_CFG, &cfg, sizeof(cfg), |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 157 | &scm_ret, sizeof(scm_ret)); |
| 158 | if (ret || scm_ret) { |
| 159 | pr_err("scm call IOMMU_SECURE_CFG failed\n"); |
| 160 | return ret ? ret : -EINVAL; |
| 161 | } |
| 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | static int msm_iommu_sec_ptbl_map(struct msm_iommu_drvdata *iommu_drvdata, |
| 167 | struct msm_iommu_ctx_drvdata *ctx_drvdata, |
| 168 | unsigned long va, phys_addr_t pa, size_t len) |
| 169 | { |
Adrian Alexei | bfe7c46 | 2013-04-01 14:36:24 -0700 | [diff] [blame] | 170 | struct msm_scm_map2_req map; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 171 | int ret = 0; |
| 172 | |
| 173 | map.plist.list = virt_to_phys(&pa); |
| 174 | map.plist.list_size = 1; |
| 175 | map.plist.size = len; |
| 176 | map.info.id = iommu_drvdata->sec_id; |
| 177 | map.info.ctx_id = ctx_drvdata->num; |
| 178 | map.info.va = va; |
| 179 | map.info.size = len; |
Adrian Alexei | bfe7c46 | 2013-04-01 14:36:24 -0700 | [diff] [blame] | 180 | map.flags = IOMMU_TLBINVAL_FLAG; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 181 | |
Adrian Alexei | bfe7c46 | 2013-04-01 14:36:24 -0700 | [diff] [blame] | 182 | if (scm_call(SCM_SVC_MP, IOMMU_SECURE_MAP2, &map, sizeof(map), &ret, |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 183 | sizeof(ret))) |
| 184 | return -EINVAL; |
| 185 | if (ret) |
| 186 | return -EINVAL; |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static unsigned int get_phys_addr(struct scatterlist *sg) |
| 192 | { |
| 193 | /* |
| 194 | * Try sg_dma_address first so that we can |
| 195 | * map carveout regions that do not have a |
| 196 | * struct page associated with them. |
| 197 | */ |
| 198 | unsigned int pa = sg_dma_address(sg); |
| 199 | if (pa == 0) |
| 200 | pa = sg_phys(sg); |
| 201 | return pa; |
| 202 | } |
| 203 | |
| 204 | static int msm_iommu_sec_ptbl_map_range(struct msm_iommu_drvdata *iommu_drvdata, |
| 205 | struct msm_iommu_ctx_drvdata *ctx_drvdata, |
| 206 | unsigned long va, struct scatterlist *sg, size_t len) |
| 207 | { |
| 208 | struct scatterlist *sgiter; |
Adrian Alexei | bfe7c46 | 2013-04-01 14:36:24 -0700 | [diff] [blame] | 209 | struct msm_scm_map2_req map; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 210 | unsigned int *pa_list = 0; |
| 211 | unsigned int pa, cnt; |
| 212 | unsigned int offset = 0, chunk_offset = 0; |
| 213 | int ret, scm_ret; |
| 214 | |
| 215 | map.info.id = iommu_drvdata->sec_id; |
| 216 | map.info.ctx_id = ctx_drvdata->num; |
| 217 | map.info.va = va; |
| 218 | map.info.size = len; |
Adrian Alexei | bfe7c46 | 2013-04-01 14:36:24 -0700 | [diff] [blame] | 219 | map.flags = IOMMU_TLBINVAL_FLAG; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 220 | |
| 221 | if (sg->length == len) { |
| 222 | pa = get_phys_addr(sg); |
| 223 | map.plist.list = virt_to_phys(&pa); |
| 224 | map.plist.list_size = 1; |
| 225 | map.plist.size = len; |
| 226 | } else { |
| 227 | sgiter = sg; |
| 228 | cnt = sg->length / SZ_1M; |
| 229 | while ((sgiter = sg_next(sgiter))) |
| 230 | cnt += sgiter->length / SZ_1M; |
| 231 | |
| 232 | pa_list = kmalloc(cnt * sizeof(*pa_list), GFP_KERNEL); |
| 233 | if (!pa_list) |
| 234 | return -ENOMEM; |
| 235 | |
| 236 | sgiter = sg; |
| 237 | cnt = 0; |
| 238 | pa = get_phys_addr(sgiter); |
| 239 | while (offset < len) { |
| 240 | pa += chunk_offset; |
| 241 | pa_list[cnt] = pa; |
| 242 | chunk_offset += SZ_1M; |
| 243 | offset += SZ_1M; |
| 244 | cnt++; |
| 245 | |
| 246 | if (chunk_offset >= sgiter->length && offset < len) { |
| 247 | chunk_offset = 0; |
| 248 | sgiter = sg_next(sgiter); |
| 249 | pa = get_phys_addr(sgiter); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | map.plist.list = virt_to_phys(pa_list); |
| 254 | map.plist.list_size = cnt; |
| 255 | map.plist.size = SZ_1M; |
| 256 | } |
| 257 | |
Adrian Alexei | bfe7c46 | 2013-04-01 14:36:24 -0700 | [diff] [blame] | 258 | ret = scm_call(SCM_SVC_MP, IOMMU_SECURE_MAP2, &map, sizeof(map), |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 259 | &scm_ret, sizeof(scm_ret)); |
| 260 | kfree(pa_list); |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | static int msm_iommu_sec_ptbl_unmap(struct msm_iommu_drvdata *iommu_drvdata, |
| 265 | struct msm_iommu_ctx_drvdata *ctx_drvdata, |
| 266 | unsigned long va, size_t len) |
| 267 | { |
Adrian Alexei | bfe7c46 | 2013-04-01 14:36:24 -0700 | [diff] [blame] | 268 | struct msm_scm_unmap2_req unmap; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 269 | int ret, scm_ret; |
| 270 | |
Adrian Alexei | bfe7c46 | 2013-04-01 14:36:24 -0700 | [diff] [blame] | 271 | unmap.info.id = iommu_drvdata->sec_id; |
| 272 | unmap.info.ctx_id = ctx_drvdata->num; |
| 273 | unmap.info.va = va; |
| 274 | unmap.info.size = len; |
| 275 | unmap.flags = IOMMU_TLBINVAL_FLAG; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 276 | |
Adrian Alexei | bfe7c46 | 2013-04-01 14:36:24 -0700 | [diff] [blame] | 277 | ret = scm_call(SCM_SVC_MP, IOMMU_SECURE_UNMAP2, &unmap, sizeof(unmap), |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 278 | &scm_ret, sizeof(scm_ret)); |
| 279 | return ret; |
| 280 | } |
| 281 | |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 282 | static int msm_iommu_domain_init(struct iommu_domain *domain, int flags) |
| 283 | { |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 284 | struct msm_iommu_priv *priv; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 285 | |
| 286 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 287 | if (!priv) |
| 288 | return -ENOMEM; |
| 289 | |
| 290 | INIT_LIST_HEAD(&priv->list_attached); |
| 291 | domain->priv = priv; |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static void msm_iommu_domain_destroy(struct iommu_domain *domain) |
| 296 | { |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 297 | struct msm_iommu_priv *priv; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 298 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 299 | iommu_access_ops->iommu_lock_acquire(); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 300 | priv = domain->priv; |
| 301 | domain->priv = NULL; |
| 302 | |
| 303 | kfree(priv); |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 304 | iommu_access_ops->iommu_lock_release(); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev) |
| 308 | { |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 309 | struct msm_iommu_priv *priv; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 310 | struct msm_iommu_drvdata *iommu_drvdata; |
| 311 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 312 | struct msm_iommu_ctx_drvdata *tmp_drvdata; |
| 313 | int ret = 0; |
| 314 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 315 | iommu_access_ops->iommu_lock_acquire(); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 316 | |
| 317 | priv = domain->priv; |
| 318 | if (!priv || !dev) { |
| 319 | ret = -EINVAL; |
| 320 | goto fail; |
| 321 | } |
| 322 | |
| 323 | iommu_drvdata = dev_get_drvdata(dev->parent); |
| 324 | ctx_drvdata = dev_get_drvdata(dev); |
| 325 | if (!iommu_drvdata || !ctx_drvdata) { |
| 326 | ret = -EINVAL; |
| 327 | goto fail; |
| 328 | } |
| 329 | |
| 330 | if (!list_empty(&ctx_drvdata->attached_elm)) { |
| 331 | ret = -EBUSY; |
| 332 | goto fail; |
| 333 | } |
| 334 | |
| 335 | list_for_each_entry(tmp_drvdata, &priv->list_attached, attached_elm) |
| 336 | if (tmp_drvdata == ctx_drvdata) { |
| 337 | ret = -EBUSY; |
| 338 | goto fail; |
| 339 | } |
| 340 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 341 | ret = iommu_access_ops->iommu_power_on(iommu_drvdata); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 342 | if (ret) |
| 343 | goto fail; |
| 344 | |
Olav Haugan | e388539 | 2013-03-06 16:22:53 -0800 | [diff] [blame] | 345 | /* We can only do this once */ |
| 346 | if (!iommu_drvdata->ctx_attach_count) { |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 347 | ret = iommu_access_ops->iommu_clk_on(iommu_drvdata); |
Olav Haugan | e388539 | 2013-03-06 16:22:53 -0800 | [diff] [blame] | 348 | if (ret) { |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 349 | iommu_access_ops->iommu_power_off(iommu_drvdata); |
Olav Haugan | e388539 | 2013-03-06 16:22:53 -0800 | [diff] [blame] | 350 | goto fail; |
| 351 | } |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 352 | |
Olav Haugan | e388539 | 2013-03-06 16:22:53 -0800 | [diff] [blame] | 353 | ret = msm_iommu_sec_program_iommu(iommu_drvdata->sec_id); |
Olav Haugan | ce2eab9 | 2013-02-07 12:59:18 -0800 | [diff] [blame] | 354 | |
Olav Haugan | e388539 | 2013-03-06 16:22:53 -0800 | [diff] [blame] | 355 | /* bfb settings are always programmed by HLOS */ |
| 356 | program_iommu_bfb_settings(iommu_drvdata->base, |
| 357 | iommu_drvdata->bfb_settings); |
Olav Haugan | f378273 | 2013-01-11 11:23:30 -0800 | [diff] [blame] | 358 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 359 | iommu_access_ops->iommu_clk_off(iommu_drvdata); |
Olav Haugan | e388539 | 2013-03-06 16:22:53 -0800 | [diff] [blame] | 360 | if (ret) { |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 361 | iommu_access_ops->iommu_power_off(iommu_drvdata); |
Olav Haugan | e388539 | 2013-03-06 16:22:53 -0800 | [diff] [blame] | 362 | goto fail; |
| 363 | } |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | list_add(&(ctx_drvdata->attached_elm), &priv->list_attached); |
| 367 | ctx_drvdata->attached_domain = domain; |
Olav Haugan | e388539 | 2013-03-06 16:22:53 -0800 | [diff] [blame] | 368 | ++iommu_drvdata->ctx_attach_count; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 369 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 370 | iommu_access_ops->iommu_lock_release(); |
Olav Haugan | 64ffdf3 | 2013-01-24 17:20:24 -0800 | [diff] [blame] | 371 | |
| 372 | msm_iommu_attached(dev->parent); |
| 373 | return ret; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 374 | fail: |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 375 | iommu_access_ops->iommu_lock_release(); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 376 | return ret; |
| 377 | } |
| 378 | |
| 379 | static void msm_iommu_detach_dev(struct iommu_domain *domain, |
| 380 | struct device *dev) |
| 381 | { |
| 382 | struct msm_iommu_drvdata *iommu_drvdata; |
| 383 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 384 | |
Olav Haugan | 64ffdf3 | 2013-01-24 17:20:24 -0800 | [diff] [blame] | 385 | msm_iommu_detached(dev->parent); |
| 386 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 387 | iommu_access_ops->iommu_lock_acquire(); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 388 | if (!dev) |
| 389 | goto fail; |
| 390 | |
| 391 | iommu_drvdata = dev_get_drvdata(dev->parent); |
| 392 | ctx_drvdata = dev_get_drvdata(dev); |
| 393 | if (!iommu_drvdata || !ctx_drvdata || !ctx_drvdata->attached_domain) |
| 394 | goto fail; |
| 395 | |
| 396 | list_del_init(&ctx_drvdata->attached_elm); |
| 397 | ctx_drvdata->attached_domain = NULL; |
| 398 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 399 | iommu_access_ops->iommu_power_off(iommu_drvdata); |
Olav Haugan | e388539 | 2013-03-06 16:22:53 -0800 | [diff] [blame] | 400 | BUG_ON(iommu_drvdata->ctx_attach_count == 0); |
| 401 | --iommu_drvdata->ctx_attach_count; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 402 | fail: |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 403 | iommu_access_ops->iommu_lock_release(); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | static int get_drvdata(struct iommu_domain *domain, |
| 407 | struct msm_iommu_drvdata **iommu_drvdata, |
| 408 | struct msm_iommu_ctx_drvdata **ctx_drvdata) |
| 409 | { |
Olav Haugan | 090614f | 2013-03-22 12:14:18 -0700 | [diff] [blame] | 410 | struct msm_iommu_priv *priv = domain->priv; |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 411 | struct msm_iommu_ctx_drvdata *ctx; |
| 412 | |
| 413 | list_for_each_entry(ctx, &priv->list_attached, attached_elm) { |
| 414 | if (ctx->attached_domain == domain) |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | if (ctx->attached_domain != domain) |
| 419 | return -EINVAL; |
| 420 | |
| 421 | *ctx_drvdata = ctx; |
| 422 | *iommu_drvdata = dev_get_drvdata(ctx->pdev->dev.parent); |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | static int msm_iommu_map(struct iommu_domain *domain, unsigned long va, |
| 427 | phys_addr_t pa, size_t len, int prot) |
| 428 | { |
| 429 | struct msm_iommu_drvdata *iommu_drvdata; |
| 430 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 431 | int ret = 0; |
| 432 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 433 | iommu_access_ops->iommu_lock_acquire(); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 434 | |
| 435 | ret = get_drvdata(domain, &iommu_drvdata, &ctx_drvdata); |
| 436 | if (ret) |
| 437 | goto fail; |
| 438 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 439 | iommu_access_ops->iommu_clk_on(iommu_drvdata); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 440 | ret = msm_iommu_sec_ptbl_map(iommu_drvdata, ctx_drvdata, |
| 441 | va, pa, len); |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 442 | iommu_access_ops->iommu_clk_off(iommu_drvdata); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 443 | fail: |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 444 | iommu_access_ops->iommu_lock_release(); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | static size_t msm_iommu_unmap(struct iommu_domain *domain, unsigned long va, |
| 449 | size_t len) |
| 450 | { |
| 451 | struct msm_iommu_drvdata *iommu_drvdata; |
| 452 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 453 | int ret = -ENODEV; |
| 454 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 455 | iommu_access_ops->iommu_lock_acquire(); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 456 | |
| 457 | ret = get_drvdata(domain, &iommu_drvdata, &ctx_drvdata); |
| 458 | if (ret) |
| 459 | goto fail; |
| 460 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 461 | iommu_access_ops->iommu_clk_on(iommu_drvdata); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 462 | ret = msm_iommu_sec_ptbl_unmap(iommu_drvdata, ctx_drvdata, |
| 463 | va, len); |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 464 | iommu_access_ops->iommu_clk_off(iommu_drvdata); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 465 | fail: |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 466 | iommu_access_ops->iommu_lock_release(); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 467 | |
| 468 | /* the IOMMU API requires us to return how many bytes were unmapped */ |
| 469 | len = ret ? 0 : len; |
| 470 | return len; |
| 471 | } |
| 472 | |
| 473 | static int msm_iommu_map_range(struct iommu_domain *domain, unsigned int va, |
| 474 | struct scatterlist *sg, unsigned int len, |
| 475 | int prot) |
| 476 | { |
| 477 | int ret; |
| 478 | struct msm_iommu_drvdata *iommu_drvdata; |
| 479 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 480 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 481 | iommu_access_ops->iommu_lock_acquire(); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 482 | |
| 483 | ret = get_drvdata(domain, &iommu_drvdata, &ctx_drvdata); |
| 484 | if (ret) |
| 485 | goto fail; |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 486 | iommu_access_ops->iommu_clk_on(iommu_drvdata); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 487 | ret = msm_iommu_sec_ptbl_map_range(iommu_drvdata, ctx_drvdata, |
| 488 | va, sg, len); |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 489 | iommu_access_ops->iommu_clk_off(iommu_drvdata); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 490 | fail: |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 491 | iommu_access_ops->iommu_lock_release(); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | |
| 496 | static int msm_iommu_unmap_range(struct iommu_domain *domain, unsigned int va, |
| 497 | unsigned int len) |
| 498 | { |
| 499 | struct msm_iommu_drvdata *iommu_drvdata; |
| 500 | struct msm_iommu_ctx_drvdata *ctx_drvdata; |
| 501 | int ret; |
| 502 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 503 | iommu_access_ops->iommu_lock_acquire(); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 504 | |
| 505 | ret = get_drvdata(domain, &iommu_drvdata, &ctx_drvdata); |
| 506 | if (ret) |
| 507 | goto fail; |
| 508 | |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 509 | iommu_access_ops->iommu_clk_on(iommu_drvdata); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 510 | ret = msm_iommu_sec_ptbl_unmap(iommu_drvdata, ctx_drvdata, va, len); |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 511 | iommu_access_ops->iommu_clk_off(iommu_drvdata); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 512 | |
| 513 | fail: |
Olav Haugan | eece7e5 | 2013-04-02 10:22:21 -0700 | [diff] [blame] | 514 | iommu_access_ops->iommu_lock_release(); |
Laura Abbott | 0d13565 | 2012-10-04 12:59:03 -0700 | [diff] [blame] | 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain, |
| 519 | unsigned long va) |
| 520 | { |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | static int msm_iommu_domain_has_cap(struct iommu_domain *domain, |
| 525 | unsigned long cap) |
| 526 | { |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | static phys_addr_t msm_iommu_get_pt_base_addr(struct iommu_domain *domain) |
| 531 | { |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | static struct iommu_ops msm_iommu_ops = { |
| 536 | .domain_init = msm_iommu_domain_init, |
| 537 | .domain_destroy = msm_iommu_domain_destroy, |
| 538 | .attach_dev = msm_iommu_attach_dev, |
| 539 | .detach_dev = msm_iommu_detach_dev, |
| 540 | .map = msm_iommu_map, |
| 541 | .unmap = msm_iommu_unmap, |
| 542 | .map_range = msm_iommu_map_range, |
| 543 | .unmap_range = msm_iommu_unmap_range, |
| 544 | .iova_to_phys = msm_iommu_iova_to_phys, |
| 545 | .domain_has_cap = msm_iommu_domain_has_cap, |
| 546 | .get_pt_base_addr = msm_iommu_get_pt_base_addr, |
| 547 | .pgsize_bitmap = MSM_IOMMU_PGSIZES, |
| 548 | }; |
| 549 | |
| 550 | static int __init msm_iommu_sec_init(void) |
| 551 | { |
| 552 | int ret; |
| 553 | |
| 554 | ret = bus_register(&msm_iommu_sec_bus_type); |
| 555 | if (ret) |
| 556 | goto fail; |
| 557 | |
| 558 | bus_set_iommu(&msm_iommu_sec_bus_type, &msm_iommu_ops); |
| 559 | ret = msm_iommu_sec_ptbl_init(); |
| 560 | fail: |
| 561 | return ret; |
| 562 | } |
| 563 | |
| 564 | subsys_initcall(msm_iommu_sec_init); |
| 565 | |
| 566 | MODULE_LICENSE("GPL v2"); |
| 567 | MODULE_DESCRIPTION("MSM SMMU Secure Driver"); |