blob: 78fffb2570aa396c71b305dae8d0e3c2ec9c1f85 [file] [log] [blame]
Olav Hauganf3782732013-01-11 11:23:30 -08001/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Laura Abbott0d135652012-10-04 12:59:03 -07002 *
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 Bohan1834e7f2013-01-18 17:16:38 -080028#include <linux/kmemleak.h>
Laura Abbott0d135652012-10-04 12:59:03 -070029
30#include <asm/sizes.h>
31
Olav Haugan64ffdf32013-01-24 17:20:24 -080032#include <mach/iommu_perfmon.h>
Olav Haugane6d01ef2013-01-25 16:55:44 -080033#include <mach/iommu_hw-v1.h>
Olav Haugan090614f2013-03-22 12:14:18 -070034#include <mach/msm_iommu_priv.h>
Laura Abbott0d135652012-10-04 12:59:03 -070035#include <mach/iommu.h>
36#include <mach/scm.h>
Mitchel Humpherys2b6e5c52013-06-05 16:01:54 -070037#include <mach/memory.h>
Laura Abbott0d135652012-10-04 12:59:03 -070038
39/* bitmap of the page sizes currently supported */
40#define MSM_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
41
Mitchel Humpherysf3b50912013-05-21 17:46:04 -070042/* commands for SCM_SVC_MP */
Laura Abbott0d135652012-10-04 12:59:03 -070043#define IOMMU_SECURE_CFG 2
44#define IOMMU_SECURE_PTBL_SIZE 3
45#define IOMMU_SECURE_PTBL_INIT 4
46#define IOMMU_SECURE_MAP 6
47#define IOMMU_SECURE_UNMAP 7
Adrian Alexeibfe7c462013-04-01 14:36:24 -070048#define IOMMU_SECURE_MAP2 0x0B
49#define IOMMU_SECURE_UNMAP2 0x0C
50#define IOMMU_TLBINVAL_FLAG 0x00000001
Laura Abbott0d135652012-10-04 12:59:03 -070051
Mitchel Humpherysf3b50912013-05-21 17:46:04 -070052/* commands for SCM_SVC_UTIL */
53#define IOMMU_DUMP_SMMU_FAULT_REGS 0X0C
54
Olav Hauganeece7e52013-04-02 10:22:21 -070055static struct iommu_access_ops *iommu_access_ops;
Laura Abbott0d135652012-10-04 12:59:03 -070056
Laura Abbott0d135652012-10-04 12:59:03 -070057struct msm_scm_paddr_list {
58 unsigned int list;
59 unsigned int list_size;
60 unsigned int size;
61};
62
63struct msm_scm_mapping_info {
64 unsigned int id;
65 unsigned int ctx_id;
66 unsigned int va;
67 unsigned int size;
68};
69
Adrian Alexeibfe7c462013-04-01 14:36:24 -070070struct msm_scm_map2_req {
Laura Abbott0d135652012-10-04 12:59:03 -070071 struct msm_scm_paddr_list plist;
72 struct msm_scm_mapping_info info;
Adrian Alexeibfe7c462013-04-01 14:36:24 -070073 unsigned int flags;
74};
75
76struct msm_scm_unmap2_req {
77 struct msm_scm_mapping_info info;
78 unsigned int flags;
Laura Abbott0d135652012-10-04 12:59:03 -070079};
80
Mitchel Humpherys2b6e5c52013-06-05 16:01:54 -070081#define NUM_DUMP_REGS 14
82/*
83 * some space to allow the number of registers returned by the secure
84 * environment to grow
85 */
86#define WIGGLE_ROOM (NUM_DUMP_REGS * 2)
87/* Each entry is a (reg_addr, reg_val) pair, hence the * 2 */
88#define SEC_DUMP_SIZE ((NUM_DUMP_REGS * 2) + WIGGLE_ROOM)
89
Mitchel Humpherysf3b50912013-05-21 17:46:04 -070090struct msm_scm_fault_regs_dump {
91 uint32_t dump_size;
Mitchel Humpherys2b6e5c52013-06-05 16:01:54 -070092 uint32_t dump_data[SEC_DUMP_SIZE];
93} __packed;
Mitchel Humpherysf3b50912013-05-21 17:46:04 -070094
Olav Hauganeece7e52013-04-02 10:22:21 -070095void msm_iommu_sec_set_access_ops(struct iommu_access_ops *access_ops)
96{
97 iommu_access_ops = access_ops;
98}
99
Mitchel Humpherysf3b50912013-05-21 17:46:04 -0700100static int msm_iommu_dump_fault_regs(int smmu_id, int cb_num,
101 struct msm_scm_fault_regs_dump *regs)
102{
103 int ret;
104
105 struct msm_scm_fault_regs_dump_req {
106 uint32_t id;
107 uint32_t cb_num;
108 phys_addr_t buff;
109 uint32_t len;
110 } req_info;
111 int resp;
112
113 req_info.id = smmu_id;
114 req_info.cb_num = cb_num;
115 req_info.buff = virt_to_phys(regs);
116 req_info.len = sizeof(*regs);
117
118 ret = scm_call(SCM_SVC_UTIL, IOMMU_DUMP_SMMU_FAULT_REGS,
119 &req_info, sizeof(req_info), &resp, 1);
120
Mitchel Humpherys4ec93cb2013-06-12 12:25:43 -0700121 invalidate_caches((unsigned long) regs, sizeof(*regs),
122 (unsigned long)virt_to_phys(regs));
123
Mitchel Humpherysf3b50912013-05-21 17:46:04 -0700124 return ret;
125}
126
Mitchel Humpherys2b6e5c52013-06-05 16:01:54 -0700127#define EXTRACT_DUMP_REG_KEY(addr, ctx) (addr & ((1 << CTX_SHIFT) - 1))
128
Mitchel Humpherys2b6e5c52013-06-05 16:01:54 -0700129static int msm_iommu_reg_dump_to_regs(
130 struct msm_iommu_context_reg ctx_regs[],
131 struct msm_scm_fault_regs_dump *dump, int cb_num)
132{
133 int i, j, ret = 0;
134 const uint32_t nvals = (dump->dump_size / sizeof(uint32_t));
135 uint32_t *it = (uint32_t *) dump->dump_data;
136 const uint32_t * const end = ((uint32_t *) dump) + nvals;
137
138 for (i = 1; it < end; it += 2, i += 2) {
139 uint32_t addr = *it;
140 uint32_t val = *(it + 1);
141 struct msm_iommu_context_reg *reg = NULL;
142
143 for (j = 0; j < MAX_DUMP_REGS; ++j) {
144 if (dump_regs_tbl[j].key ==
145 EXTRACT_DUMP_REG_KEY(addr, cb_num)) {
146 reg = &ctx_regs[j];
147 break;
148 }
149 }
150
151 if (reg == NULL) {
152 pr_debug("Unknown register in secure CB dump: %x (%x)\n",
153 addr, EXTRACT_DUMP_REG_KEY(addr, cb_num));
154 continue;
155 }
156
157 if (reg->valid) {
158 WARN(1, "Invalid (repeated?) register in CB dump: %x\n",
159 addr);
160 continue;
161 }
162
163 reg->val = val;
164 reg->valid = true;
165 }
166
167 if (i != nvals) {
168 pr_err("Invalid dump! %d != %d\n", i, nvals);
169 ret = 1;
170 goto out;
171 }
172
173 for (i = 0; i < MAX_DUMP_REGS; ++i) {
174 if (!ctx_regs[i].valid) {
Olav Haugancdb13112013-06-21 17:45:34 -0700175 if (dump_regs_tbl[i].must_be_present) {
176 pr_err("Register missing from dump: %s, %lx\n",
177 dump_regs_tbl[i].name,
178 dump_regs_tbl[i].key);
179 ret = 1;
180 }
181 ctx_regs[i].val = 0;
Mitchel Humpherys2b6e5c52013-06-05 16:01:54 -0700182 }
183 }
184
185out:
186 return ret;
187}
188
Mitchel Humpherysf3b50912013-05-21 17:46:04 -0700189irqreturn_t msm_iommu_secure_fault_handler_v2(int irq, void *dev_id)
190{
191 struct platform_device *pdev = dev_id;
192 struct msm_iommu_drvdata *drvdata;
193 struct msm_iommu_ctx_drvdata *ctx_drvdata;
194 struct msm_scm_fault_regs_dump *regs;
195 int tmp, ret = IRQ_HANDLED;
196
197 iommu_access_ops->iommu_lock_acquire();
198
199 BUG_ON(!pdev);
200
201 drvdata = dev_get_drvdata(pdev->dev.parent);
202 BUG_ON(!drvdata);
203
204 ctx_drvdata = dev_get_drvdata(&pdev->dev);
205 BUG_ON(!ctx_drvdata);
206
207 regs = kmalloc(sizeof(*regs), GFP_KERNEL);
208 if (!regs) {
209 pr_err("%s: Couldn't allocate memory\n", __func__);
210 goto lock_release;
211 }
212
213 if (!drvdata->ctx_attach_count) {
214 pr_err("Unexpected IOMMU page fault from secure context bank!\n");
215 pr_err("name = %s\n", drvdata->name);
216 pr_err("Power is OFF. Unable to read page fault information\n");
217 /*
218 * We cannot determine which context bank caused the issue so
219 * we just return handled here to ensure IRQ handler code is
220 * happy
221 */
222 goto free_regs;
223 }
224
225 iommu_access_ops->iommu_clk_on(drvdata);
226 tmp = msm_iommu_dump_fault_regs(drvdata->sec_id,
227 ctx_drvdata->num, regs);
228 iommu_access_ops->iommu_clk_off(drvdata);
229
230 if (tmp) {
Mitchel Humpherys2b6e5c52013-06-05 16:01:54 -0700231 pr_err("%s: Couldn't dump fault registers (%d) %s, ctx: %d\n",
232 __func__, tmp, drvdata->name, ctx_drvdata->num);
Mitchel Humpherysf3b50912013-05-21 17:46:04 -0700233 goto free_regs;
Mitchel Humpherysf3b50912013-05-21 17:46:04 -0700234 } else {
Mitchel Humpherys2b6e5c52013-06-05 16:01:54 -0700235 struct msm_iommu_context_reg ctx_regs[MAX_DUMP_REGS];
236 memset(ctx_regs, 0, sizeof(ctx_regs));
237 tmp = msm_iommu_reg_dump_to_regs(ctx_regs, regs,
238 ctx_drvdata->num);
239 if (!tmp && ctx_regs[DUMP_REG_FSR].val) {
240 if (!ctx_drvdata->attached_domain) {
241 pr_err("Bad domain in interrupt handler\n");
242 tmp = -ENOSYS;
243 } else {
244 tmp = report_iommu_fault(
245 ctx_drvdata->attached_domain,
246 &ctx_drvdata->pdev->dev,
247 COMBINE_DUMP_REG(
248 ctx_regs[DUMP_REG_FAR1].val,
249 ctx_regs[DUMP_REG_FAR0].val),
250 0);
251 }
252
253 /* if the fault wasn't handled by someone else: */
254 if (tmp == -ENOSYS) {
255 pr_err("Unexpected IOMMU page fault from secure context bank!\n");
256 pr_err("name = %s\n", drvdata->name);
257 pr_err("context = %s (%d)\n", ctx_drvdata->name,
258 ctx_drvdata->num);
259 pr_err("Interesting registers:\n");
260 print_ctx_regs(ctx_regs);
261 }
262 } else {
263 ret = IRQ_NONE;
264 }
Mitchel Humpherysf3b50912013-05-21 17:46:04 -0700265 }
266free_regs:
267 kfree(regs);
268lock_release:
269 iommu_access_ops->iommu_lock_release();
270 return ret;
271}
272
Laura Abbott0d135652012-10-04 12:59:03 -0700273static int msm_iommu_sec_ptbl_init(void)
274{
275 struct device_node *np;
276 struct msm_scm_ptbl_init {
277 unsigned int paddr;
278 unsigned int size;
279 unsigned int spare;
280 } pinit;
281 unsigned int *buf;
Mitchel Humpherys637cc532012-12-12 16:50:58 -0800282 int psize[2] = {0, 0};
Laura Abbott0d135652012-10-04 12:59:03 -0700283 unsigned int spare;
Mitchel Humpherys637cc532012-12-12 16:50:58 -0800284 int ret, ptbl_ret = 0;
Laura Abbott0d135652012-10-04 12:59:03 -0700285
Olav Haugan0e22c482013-01-28 17:39:36 -0800286 for_each_compatible_node(np, NULL, "qcom,msm-smmu-v1")
Laura Abbott0d135652012-10-04 12:59:03 -0700287 if (of_find_property(np, "qcom,iommu-secure-id", NULL))
288 break;
289
290 if (!np)
291 return 0;
292
293 of_node_put(np);
Syed Rameez Mustafa6ab6af32013-03-18 12:53:11 -0700294 ret = scm_call(SCM_SVC_MP, IOMMU_SECURE_PTBL_SIZE, &spare,
Laura Abbott0d135652012-10-04 12:59:03 -0700295 sizeof(spare), psize, sizeof(psize));
296 if (ret) {
297 pr_err("scm call IOMMU_SECURE_PTBL_SIZE failed\n");
298 goto fail;
299 }
300
301 if (psize[1]) {
302 pr_err("scm call IOMMU_SECURE_PTBL_SIZE failed\n");
303 goto fail;
304 }
305
306 buf = kmalloc(psize[0], GFP_KERNEL);
307 if (!buf) {
308 pr_err("%s: Failed to allocate %d bytes for PTBL\n",
309 __func__, psize[0]);
310 ret = -ENOMEM;
311 goto fail;
312 }
313
314 pinit.paddr = virt_to_phys(buf);
315 pinit.size = psize[0];
316
Syed Rameez Mustafa6ab6af32013-03-18 12:53:11 -0700317 ret = scm_call(SCM_SVC_MP, IOMMU_SECURE_PTBL_INIT, &pinit,
Laura Abbott0d135652012-10-04 12:59:03 -0700318 sizeof(pinit), &ptbl_ret, sizeof(ptbl_ret));
319 if (ret) {
320 pr_err("scm call IOMMU_SECURE_PTBL_INIT failed\n");
321 goto fail_mem;
322 }
323 if (ptbl_ret) {
324 pr_err("scm call IOMMU_SECURE_PTBL_INIT extended ret fail\n");
325 goto fail_mem;
326 }
327
Michael Bohan1834e7f2013-01-18 17:16:38 -0800328 kmemleak_not_leak(buf);
329
Laura Abbott0d135652012-10-04 12:59:03 -0700330 return 0;
331
332fail_mem:
333 kfree(buf);
334fail:
335 return ret;
336}
337
Laura Abbottf4daa692012-10-10 19:31:53 -0700338int msm_iommu_sec_program_iommu(int sec_id)
Laura Abbott0d135652012-10-04 12:59:03 -0700339{
340 struct msm_scm_sec_cfg {
341 unsigned int id;
342 unsigned int spare;
343 } cfg;
Mitchel Humpherys637cc532012-12-12 16:50:58 -0800344 int ret, scm_ret = 0;
Laura Abbott0d135652012-10-04 12:59:03 -0700345
346 cfg.id = sec_id;
347
Syed Rameez Mustafa6ab6af32013-03-18 12:53:11 -0700348 ret = scm_call(SCM_SVC_MP, IOMMU_SECURE_CFG, &cfg, sizeof(cfg),
Laura Abbott0d135652012-10-04 12:59:03 -0700349 &scm_ret, sizeof(scm_ret));
350 if (ret || scm_ret) {
351 pr_err("scm call IOMMU_SECURE_CFG failed\n");
352 return ret ? ret : -EINVAL;
353 }
354
355 return ret;
356}
357
358static int msm_iommu_sec_ptbl_map(struct msm_iommu_drvdata *iommu_drvdata,
359 struct msm_iommu_ctx_drvdata *ctx_drvdata,
360 unsigned long va, phys_addr_t pa, size_t len)
361{
Adrian Alexeibfe7c462013-04-01 14:36:24 -0700362 struct msm_scm_map2_req map;
Mitchel Humpherys4ec93cb2013-06-12 12:25:43 -0700363 void *flush_va;
364 phys_addr_t flush_pa;
Laura Abbott0d135652012-10-04 12:59:03 -0700365 int ret = 0;
366
367 map.plist.list = virt_to_phys(&pa);
368 map.plist.list_size = 1;
369 map.plist.size = len;
370 map.info.id = iommu_drvdata->sec_id;
371 map.info.ctx_id = ctx_drvdata->num;
372 map.info.va = va;
373 map.info.size = len;
Chintan Pandya8da8a5a2013-07-11 18:17:50 +0530374 map.flags = 0;
Mitchel Humpherys4ec93cb2013-06-12 12:25:43 -0700375 flush_va = &pa;
376 flush_pa = virt_to_phys(&pa);
377
378 /*
379 * Ensure that the buffer is in RAM by the time it gets to TZ
380 */
381 clean_caches((unsigned long) flush_va, len, flush_pa);
Laura Abbott0d135652012-10-04 12:59:03 -0700382
Adrian Alexeibfe7c462013-04-01 14:36:24 -0700383 if (scm_call(SCM_SVC_MP, IOMMU_SECURE_MAP2, &map, sizeof(map), &ret,
Laura Abbott0d135652012-10-04 12:59:03 -0700384 sizeof(ret)))
385 return -EINVAL;
386 if (ret)
387 return -EINVAL;
388
Mitchel Humpherys4ec93cb2013-06-12 12:25:43 -0700389 /* Invalidate cache since TZ touched this address range */
390 invalidate_caches((unsigned long) flush_va, len, flush_pa);
391
Laura Abbott0d135652012-10-04 12:59:03 -0700392 return 0;
393}
394
395static unsigned int get_phys_addr(struct scatterlist *sg)
396{
397 /*
398 * Try sg_dma_address first so that we can
399 * map carveout regions that do not have a
400 * struct page associated with them.
401 */
402 unsigned int pa = sg_dma_address(sg);
403 if (pa == 0)
404 pa = sg_phys(sg);
405 return pa;
406}
407
408static int msm_iommu_sec_ptbl_map_range(struct msm_iommu_drvdata *iommu_drvdata,
409 struct msm_iommu_ctx_drvdata *ctx_drvdata,
410 unsigned long va, struct scatterlist *sg, size_t len)
411{
412 struct scatterlist *sgiter;
Adrian Alexeibfe7c462013-04-01 14:36:24 -0700413 struct msm_scm_map2_req map;
Laura Abbott0d135652012-10-04 12:59:03 -0700414 unsigned int *pa_list = 0;
415 unsigned int pa, cnt;
Mitchel Humpherys4ec93cb2013-06-12 12:25:43 -0700416 void *flush_va;
Laura Abbott0d135652012-10-04 12:59:03 -0700417 unsigned int offset = 0, chunk_offset = 0;
418 int ret, scm_ret;
419
420 map.info.id = iommu_drvdata->sec_id;
421 map.info.ctx_id = ctx_drvdata->num;
422 map.info.va = va;
423 map.info.size = len;
Chintan Pandya8da8a5a2013-07-11 18:17:50 +0530424 map.flags = 0;
Laura Abbott0d135652012-10-04 12:59:03 -0700425
426 if (sg->length == len) {
427 pa = get_phys_addr(sg);
428 map.plist.list = virt_to_phys(&pa);
429 map.plist.list_size = 1;
430 map.plist.size = len;
Mitchel Humpherys4ec93cb2013-06-12 12:25:43 -0700431 flush_va = &pa;
Laura Abbott0d135652012-10-04 12:59:03 -0700432 } else {
433 sgiter = sg;
434 cnt = sg->length / SZ_1M;
435 while ((sgiter = sg_next(sgiter)))
436 cnt += sgiter->length / SZ_1M;
437
438 pa_list = kmalloc(cnt * sizeof(*pa_list), GFP_KERNEL);
439 if (!pa_list)
440 return -ENOMEM;
441
442 sgiter = sg;
443 cnt = 0;
444 pa = get_phys_addr(sgiter);
445 while (offset < len) {
446 pa += chunk_offset;
447 pa_list[cnt] = pa;
448 chunk_offset += SZ_1M;
449 offset += SZ_1M;
450 cnt++;
451
452 if (chunk_offset >= sgiter->length && offset < len) {
453 chunk_offset = 0;
454 sgiter = sg_next(sgiter);
455 pa = get_phys_addr(sgiter);
456 }
457 }
458
459 map.plist.list = virt_to_phys(pa_list);
460 map.plist.list_size = cnt;
461 map.plist.size = SZ_1M;
Mitchel Humpherys4ec93cb2013-06-12 12:25:43 -0700462 flush_va = pa_list;
Laura Abbott0d135652012-10-04 12:59:03 -0700463 }
464
Mitchel Humpherys4ec93cb2013-06-12 12:25:43 -0700465 /*
466 * Ensure that the buffer is in RAM by the time it gets to TZ
467 */
468 clean_caches((unsigned long) flush_va,
Laura Abbott87473062013-06-20 16:50:20 -0700469 sizeof(unsigned long) * map.plist.list_size,
470 virt_to_phys(flush_va));
Mitchel Humpherys4ec93cb2013-06-12 12:25:43 -0700471
Adrian Alexeibfe7c462013-04-01 14:36:24 -0700472 ret = scm_call(SCM_SVC_MP, IOMMU_SECURE_MAP2, &map, sizeof(map),
Laura Abbott0d135652012-10-04 12:59:03 -0700473 &scm_ret, sizeof(scm_ret));
474 kfree(pa_list);
475 return ret;
476}
477
478static int msm_iommu_sec_ptbl_unmap(struct msm_iommu_drvdata *iommu_drvdata,
479 struct msm_iommu_ctx_drvdata *ctx_drvdata,
480 unsigned long va, size_t len)
481{
Adrian Alexeibfe7c462013-04-01 14:36:24 -0700482 struct msm_scm_unmap2_req unmap;
Laura Abbott0d135652012-10-04 12:59:03 -0700483 int ret, scm_ret;
484
Adrian Alexeibfe7c462013-04-01 14:36:24 -0700485 unmap.info.id = iommu_drvdata->sec_id;
486 unmap.info.ctx_id = ctx_drvdata->num;
487 unmap.info.va = va;
488 unmap.info.size = len;
489 unmap.flags = IOMMU_TLBINVAL_FLAG;
Laura Abbott0d135652012-10-04 12:59:03 -0700490
Adrian Alexeibfe7c462013-04-01 14:36:24 -0700491 ret = scm_call(SCM_SVC_MP, IOMMU_SECURE_UNMAP2, &unmap, sizeof(unmap),
Laura Abbott0d135652012-10-04 12:59:03 -0700492 &scm_ret, sizeof(scm_ret));
493 return ret;
494}
495
Laura Abbott0d135652012-10-04 12:59:03 -0700496static int msm_iommu_domain_init(struct iommu_domain *domain, int flags)
497{
Olav Haugan090614f2013-03-22 12:14:18 -0700498 struct msm_iommu_priv *priv;
Laura Abbott0d135652012-10-04 12:59:03 -0700499
500 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
501 if (!priv)
502 return -ENOMEM;
503
504 INIT_LIST_HEAD(&priv->list_attached);
505 domain->priv = priv;
506 return 0;
507}
508
509static void msm_iommu_domain_destroy(struct iommu_domain *domain)
510{
Olav Haugan090614f2013-03-22 12:14:18 -0700511 struct msm_iommu_priv *priv;
Laura Abbott0d135652012-10-04 12:59:03 -0700512
Olav Hauganeece7e52013-04-02 10:22:21 -0700513 iommu_access_ops->iommu_lock_acquire();
Laura Abbott0d135652012-10-04 12:59:03 -0700514 priv = domain->priv;
515 domain->priv = NULL;
516
517 kfree(priv);
Olav Hauganeece7e52013-04-02 10:22:21 -0700518 iommu_access_ops->iommu_lock_release();
Laura Abbott0d135652012-10-04 12:59:03 -0700519}
520
521static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
522{
Olav Haugan090614f2013-03-22 12:14:18 -0700523 struct msm_iommu_priv *priv;
Laura Abbott0d135652012-10-04 12:59:03 -0700524 struct msm_iommu_drvdata *iommu_drvdata;
525 struct msm_iommu_ctx_drvdata *ctx_drvdata;
526 struct msm_iommu_ctx_drvdata *tmp_drvdata;
527 int ret = 0;
528
Olav Hauganeece7e52013-04-02 10:22:21 -0700529 iommu_access_ops->iommu_lock_acquire();
Laura Abbott0d135652012-10-04 12:59:03 -0700530
531 priv = domain->priv;
532 if (!priv || !dev) {
533 ret = -EINVAL;
534 goto fail;
535 }
536
537 iommu_drvdata = dev_get_drvdata(dev->parent);
538 ctx_drvdata = dev_get_drvdata(dev);
539 if (!iommu_drvdata || !ctx_drvdata) {
540 ret = -EINVAL;
541 goto fail;
542 }
543
544 if (!list_empty(&ctx_drvdata->attached_elm)) {
545 ret = -EBUSY;
546 goto fail;
547 }
548
549 list_for_each_entry(tmp_drvdata, &priv->list_attached, attached_elm)
550 if (tmp_drvdata == ctx_drvdata) {
551 ret = -EBUSY;
552 goto fail;
553 }
554
Olav Hauganeece7e52013-04-02 10:22:21 -0700555 ret = iommu_access_ops->iommu_power_on(iommu_drvdata);
Laura Abbott0d135652012-10-04 12:59:03 -0700556 if (ret)
557 goto fail;
558
Olav Haugane3885392013-03-06 16:22:53 -0800559 /* We can only do this once */
560 if (!iommu_drvdata->ctx_attach_count) {
Olav Hauganeece7e52013-04-02 10:22:21 -0700561 ret = iommu_access_ops->iommu_clk_on(iommu_drvdata);
Olav Haugane3885392013-03-06 16:22:53 -0800562 if (ret) {
Olav Hauganeece7e52013-04-02 10:22:21 -0700563 iommu_access_ops->iommu_power_off(iommu_drvdata);
Olav Haugane3885392013-03-06 16:22:53 -0800564 goto fail;
565 }
Laura Abbott0d135652012-10-04 12:59:03 -0700566
Olav Haugane3885392013-03-06 16:22:53 -0800567 ret = msm_iommu_sec_program_iommu(iommu_drvdata->sec_id);
Olav Haugance2eab92013-02-07 12:59:18 -0800568
Olav Haugane3885392013-03-06 16:22:53 -0800569 /* bfb settings are always programmed by HLOS */
570 program_iommu_bfb_settings(iommu_drvdata->base,
571 iommu_drvdata->bfb_settings);
Olav Hauganf3782732013-01-11 11:23:30 -0800572
Olav Hauganeece7e52013-04-02 10:22:21 -0700573 iommu_access_ops->iommu_clk_off(iommu_drvdata);
Olav Haugane3885392013-03-06 16:22:53 -0800574 if (ret) {
Olav Hauganeece7e52013-04-02 10:22:21 -0700575 iommu_access_ops->iommu_power_off(iommu_drvdata);
Olav Haugane3885392013-03-06 16:22:53 -0800576 goto fail;
577 }
Laura Abbott0d135652012-10-04 12:59:03 -0700578 }
579
580 list_add(&(ctx_drvdata->attached_elm), &priv->list_attached);
581 ctx_drvdata->attached_domain = domain;
Olav Haugane3885392013-03-06 16:22:53 -0800582 ++iommu_drvdata->ctx_attach_count;
Laura Abbott0d135652012-10-04 12:59:03 -0700583
Olav Hauganeece7e52013-04-02 10:22:21 -0700584 iommu_access_ops->iommu_lock_release();
Olav Haugan64ffdf32013-01-24 17:20:24 -0800585
586 msm_iommu_attached(dev->parent);
587 return ret;
Laura Abbott0d135652012-10-04 12:59:03 -0700588fail:
Olav Hauganeece7e52013-04-02 10:22:21 -0700589 iommu_access_ops->iommu_lock_release();
Laura Abbott0d135652012-10-04 12:59:03 -0700590 return ret;
591}
592
593static void msm_iommu_detach_dev(struct iommu_domain *domain,
594 struct device *dev)
595{
596 struct msm_iommu_drvdata *iommu_drvdata;
597 struct msm_iommu_ctx_drvdata *ctx_drvdata;
598
Olav Haugan64ffdf32013-01-24 17:20:24 -0800599 msm_iommu_detached(dev->parent);
600
Olav Hauganeece7e52013-04-02 10:22:21 -0700601 iommu_access_ops->iommu_lock_acquire();
Laura Abbott0d135652012-10-04 12:59:03 -0700602 if (!dev)
603 goto fail;
604
605 iommu_drvdata = dev_get_drvdata(dev->parent);
606 ctx_drvdata = dev_get_drvdata(dev);
607 if (!iommu_drvdata || !ctx_drvdata || !ctx_drvdata->attached_domain)
608 goto fail;
609
610 list_del_init(&ctx_drvdata->attached_elm);
611 ctx_drvdata->attached_domain = NULL;
612
Olav Hauganeece7e52013-04-02 10:22:21 -0700613 iommu_access_ops->iommu_power_off(iommu_drvdata);
Olav Haugane3885392013-03-06 16:22:53 -0800614 BUG_ON(iommu_drvdata->ctx_attach_count == 0);
615 --iommu_drvdata->ctx_attach_count;
Laura Abbott0d135652012-10-04 12:59:03 -0700616fail:
Olav Hauganeece7e52013-04-02 10:22:21 -0700617 iommu_access_ops->iommu_lock_release();
Laura Abbott0d135652012-10-04 12:59:03 -0700618}
619
620static int get_drvdata(struct iommu_domain *domain,
621 struct msm_iommu_drvdata **iommu_drvdata,
622 struct msm_iommu_ctx_drvdata **ctx_drvdata)
623{
Olav Haugan090614f2013-03-22 12:14:18 -0700624 struct msm_iommu_priv *priv = domain->priv;
Laura Abbott0d135652012-10-04 12:59:03 -0700625 struct msm_iommu_ctx_drvdata *ctx;
626
627 list_for_each_entry(ctx, &priv->list_attached, attached_elm) {
628 if (ctx->attached_domain == domain)
629 break;
630 }
631
632 if (ctx->attached_domain != domain)
633 return -EINVAL;
634
635 *ctx_drvdata = ctx;
636 *iommu_drvdata = dev_get_drvdata(ctx->pdev->dev.parent);
637 return 0;
638}
639
640static int msm_iommu_map(struct iommu_domain *domain, unsigned long va,
641 phys_addr_t pa, size_t len, int prot)
642{
643 struct msm_iommu_drvdata *iommu_drvdata;
644 struct msm_iommu_ctx_drvdata *ctx_drvdata;
645 int ret = 0;
646
Olav Hauganeece7e52013-04-02 10:22:21 -0700647 iommu_access_ops->iommu_lock_acquire();
Laura Abbott0d135652012-10-04 12:59:03 -0700648
649 ret = get_drvdata(domain, &iommu_drvdata, &ctx_drvdata);
650 if (ret)
651 goto fail;
652
Olav Hauganeece7e52013-04-02 10:22:21 -0700653 iommu_access_ops->iommu_clk_on(iommu_drvdata);
Laura Abbott0d135652012-10-04 12:59:03 -0700654 ret = msm_iommu_sec_ptbl_map(iommu_drvdata, ctx_drvdata,
655 va, pa, len);
Olav Hauganeece7e52013-04-02 10:22:21 -0700656 iommu_access_ops->iommu_clk_off(iommu_drvdata);
Laura Abbott0d135652012-10-04 12:59:03 -0700657fail:
Olav Hauganeece7e52013-04-02 10:22:21 -0700658 iommu_access_ops->iommu_lock_release();
Laura Abbott0d135652012-10-04 12:59:03 -0700659 return ret;
660}
661
662static size_t msm_iommu_unmap(struct iommu_domain *domain, unsigned long va,
663 size_t len)
664{
665 struct msm_iommu_drvdata *iommu_drvdata;
666 struct msm_iommu_ctx_drvdata *ctx_drvdata;
667 int ret = -ENODEV;
668
Olav Hauganeece7e52013-04-02 10:22:21 -0700669 iommu_access_ops->iommu_lock_acquire();
Laura Abbott0d135652012-10-04 12:59:03 -0700670
671 ret = get_drvdata(domain, &iommu_drvdata, &ctx_drvdata);
672 if (ret)
673 goto fail;
674
Olav Hauganeece7e52013-04-02 10:22:21 -0700675 iommu_access_ops->iommu_clk_on(iommu_drvdata);
Laura Abbott0d135652012-10-04 12:59:03 -0700676 ret = msm_iommu_sec_ptbl_unmap(iommu_drvdata, ctx_drvdata,
677 va, len);
Olav Hauganeece7e52013-04-02 10:22:21 -0700678 iommu_access_ops->iommu_clk_off(iommu_drvdata);
Laura Abbott0d135652012-10-04 12:59:03 -0700679fail:
Olav Hauganeece7e52013-04-02 10:22:21 -0700680 iommu_access_ops->iommu_lock_release();
Laura Abbott0d135652012-10-04 12:59:03 -0700681
682 /* the IOMMU API requires us to return how many bytes were unmapped */
683 len = ret ? 0 : len;
684 return len;
685}
686
687static int msm_iommu_map_range(struct iommu_domain *domain, unsigned int va,
688 struct scatterlist *sg, unsigned int len,
689 int prot)
690{
691 int ret;
692 struct msm_iommu_drvdata *iommu_drvdata;
693 struct msm_iommu_ctx_drvdata *ctx_drvdata;
694
Olav Hauganeece7e52013-04-02 10:22:21 -0700695 iommu_access_ops->iommu_lock_acquire();
Laura Abbott0d135652012-10-04 12:59:03 -0700696
697 ret = get_drvdata(domain, &iommu_drvdata, &ctx_drvdata);
698 if (ret)
699 goto fail;
Olav Hauganeece7e52013-04-02 10:22:21 -0700700 iommu_access_ops->iommu_clk_on(iommu_drvdata);
Laura Abbott0d135652012-10-04 12:59:03 -0700701 ret = msm_iommu_sec_ptbl_map_range(iommu_drvdata, ctx_drvdata,
702 va, sg, len);
Olav Hauganeece7e52013-04-02 10:22:21 -0700703 iommu_access_ops->iommu_clk_off(iommu_drvdata);
Laura Abbott0d135652012-10-04 12:59:03 -0700704fail:
Olav Hauganeece7e52013-04-02 10:22:21 -0700705 iommu_access_ops->iommu_lock_release();
Laura Abbott0d135652012-10-04 12:59:03 -0700706 return ret;
707}
708
709
710static int msm_iommu_unmap_range(struct iommu_domain *domain, unsigned int va,
711 unsigned int len)
712{
713 struct msm_iommu_drvdata *iommu_drvdata;
714 struct msm_iommu_ctx_drvdata *ctx_drvdata;
715 int ret;
716
Olav Hauganeece7e52013-04-02 10:22:21 -0700717 iommu_access_ops->iommu_lock_acquire();
Laura Abbott0d135652012-10-04 12:59:03 -0700718
719 ret = get_drvdata(domain, &iommu_drvdata, &ctx_drvdata);
720 if (ret)
721 goto fail;
722
Olav Hauganeece7e52013-04-02 10:22:21 -0700723 iommu_access_ops->iommu_clk_on(iommu_drvdata);
Laura Abbott0d135652012-10-04 12:59:03 -0700724 ret = msm_iommu_sec_ptbl_unmap(iommu_drvdata, ctx_drvdata, va, len);
Olav Hauganeece7e52013-04-02 10:22:21 -0700725 iommu_access_ops->iommu_clk_off(iommu_drvdata);
Laura Abbott0d135652012-10-04 12:59:03 -0700726
727fail:
Olav Hauganeece7e52013-04-02 10:22:21 -0700728 iommu_access_ops->iommu_lock_release();
Laura Abbott0d135652012-10-04 12:59:03 -0700729 return 0;
730}
731
732static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain,
733 unsigned long va)
734{
735 return 0;
736}
737
738static int msm_iommu_domain_has_cap(struct iommu_domain *domain,
739 unsigned long cap)
740{
741 return 0;
742}
743
744static phys_addr_t msm_iommu_get_pt_base_addr(struct iommu_domain *domain)
745{
746 return 0;
747}
748
749static struct iommu_ops msm_iommu_ops = {
750 .domain_init = msm_iommu_domain_init,
751 .domain_destroy = msm_iommu_domain_destroy,
752 .attach_dev = msm_iommu_attach_dev,
753 .detach_dev = msm_iommu_detach_dev,
754 .map = msm_iommu_map,
755 .unmap = msm_iommu_unmap,
756 .map_range = msm_iommu_map_range,
757 .unmap_range = msm_iommu_unmap_range,
758 .iova_to_phys = msm_iommu_iova_to_phys,
759 .domain_has_cap = msm_iommu_domain_has_cap,
760 .get_pt_base_addr = msm_iommu_get_pt_base_addr,
761 .pgsize_bitmap = MSM_IOMMU_PGSIZES,
762};
763
764static int __init msm_iommu_sec_init(void)
765{
766 int ret;
767
768 ret = bus_register(&msm_iommu_sec_bus_type);
769 if (ret)
770 goto fail;
771
772 bus_set_iommu(&msm_iommu_sec_bus_type, &msm_iommu_ops);
773 ret = msm_iommu_sec_ptbl_init();
Laura Abbott0d135652012-10-04 12:59:03 -0700774fail:
775 return ret;
776}
777
778subsys_initcall(msm_iommu_sec_init);
779
780MODULE_LICENSE("GPL v2");
781MODULE_DESCRIPTION("MSM SMMU Secure Driver");