blob: 687269eece31dee258ba5b190a791ac7af6f97a8 [file] [log] [blame]
Olav Haugan3c7fb382013-01-02 17:32:25 -08001/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Steve Mucklef132c6c2012-06-06 18:30:57 -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>
Sathish Ambleycf045e62012-06-07 12:56:50 -070026#include <linux/of.h>
27#include <linux/of_device.h>
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -070028#include <linux/regulator/consumer.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070029#include <asm/sizes.h>
30
31#include <mach/iommu_hw-v2.h>
32#include <mach/iommu.h>
Olav Haugan5ebfbc62013-01-07 17:49:10 -080033#include <mach/iommu_perfmon.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070034#include "msm_iommu_pagetable.h"
35
36/* bitmap of the page sizes currently supported */
37#define MSM_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
38
39static DEFINE_MUTEX(msm_iommu_lock);
40
41struct msm_priv {
42 struct iommu_pt pt;
43 struct list_head list_attached;
44};
45
Olav Haugan2648d972013-01-07 17:32:31 -080046static int __enable_regulators(struct msm_iommu_drvdata *drvdata)
47{
48 int ret = regulator_enable(drvdata->gdsc);
49 if (ret)
50 goto fail;
51
52 if (drvdata->alt_gdsc)
53 ret = regulator_enable(drvdata->alt_gdsc);
54
55 if (ret)
56 regulator_disable(drvdata->gdsc);
57fail:
58 return ret;
59}
60
61static void __disable_regulators(struct msm_iommu_drvdata *drvdata)
62{
63 if (drvdata->alt_gdsc)
64 regulator_disable(drvdata->alt_gdsc);
65
66 regulator_disable(drvdata->gdsc);
67}
68
Steve Mucklef132c6c2012-06-06 18:30:57 -070069static int __enable_clocks(struct msm_iommu_drvdata *drvdata)
70{
71 int ret;
72
73 ret = clk_prepare_enable(drvdata->pclk);
74 if (ret)
75 goto fail;
76
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -070077 ret = clk_prepare_enable(drvdata->clk);
78 if (ret)
79 clk_disable_unprepare(drvdata->pclk);
80
81 if (drvdata->aclk) {
82 ret = clk_prepare_enable(drvdata->aclk);
83 if (ret) {
84 clk_disable_unprepare(drvdata->clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -070085 clk_disable_unprepare(drvdata->pclk);
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -070086 }
Steve Mucklef132c6c2012-06-06 18:30:57 -070087 }
Olav Haugan3c7fb382013-01-02 17:32:25 -080088
89 if (drvdata->clk_reg_virt) {
90 unsigned int value;
91
92 value = readl_relaxed(drvdata->clk_reg_virt);
93 value &= ~0x1;
94 writel_relaxed(value, drvdata->clk_reg_virt);
95 }
Steve Mucklef132c6c2012-06-06 18:30:57 -070096fail:
97 return ret;
98}
99
100static void __disable_clocks(struct msm_iommu_drvdata *drvdata)
101{
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -0700102 if (drvdata->aclk)
103 clk_disable_unprepare(drvdata->aclk);
104 clk_disable_unprepare(drvdata->clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700105 clk_disable_unprepare(drvdata->pclk);
106}
107
Olav Haugan5ebfbc62013-01-07 17:49:10 -0800108static int _iommu_power_off(void *data)
109{
110 struct msm_iommu_drvdata *drvdata;
111
112 drvdata = (struct msm_iommu_drvdata *)data;
113 __disable_clocks(drvdata);
114 __disable_regulators(drvdata);
115 return 0;
116}
117
118static int _iommu_power_on(void *data)
119{
120 int ret;
121 struct msm_iommu_drvdata *drvdata;
122
123 drvdata = (struct msm_iommu_drvdata *)data;
124 ret = __enable_regulators(drvdata);
125 if (ret)
126 goto fail;
127
128 ret = __enable_clocks(drvdata);
129 if (ret) {
130 __disable_regulators(drvdata);
131 goto fail;
132 }
133 return 0;
134fail:
135 return -EIO;
136}
137
138static void _iommu_lock_acquire(void)
139{
140 mutex_lock(&msm_iommu_lock);
141}
142
143static void _iommu_lock_release(void)
144{
145 mutex_unlock(&msm_iommu_lock);
146}
147
148struct iommu_access_ops iommu_access_ops = {
149 .iommu_power_on = _iommu_power_on,
150 .iommu_power_off = _iommu_power_off,
151 .iommu_lock_acquire = _iommu_lock_acquire,
152 .iommu_lock_release = _iommu_lock_release,
153};
154
Stepan Moskovchenko22d32c62012-07-11 18:00:06 -0700155static void __sync_tlb(void __iomem *base, int ctx)
156{
157 SET_TLBSYNC(base, ctx, 0);
158
159 /* No barrier needed due to register proximity */
160 while (GET_CB_TLBSTATUS_SACTIVE(base, ctx))
161 cpu_relax();
162
163 /* No barrier needed due to read dependency */
164}
165
Steve Mucklef132c6c2012-06-06 18:30:57 -0700166static int __flush_iotlb_va(struct iommu_domain *domain, unsigned int va)
167{
168 struct msm_priv *priv = domain->priv;
169 struct msm_iommu_drvdata *iommu_drvdata;
170 struct msm_iommu_ctx_drvdata *ctx_drvdata;
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700171 int ret = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700172 int asid;
173
174 list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) {
175 BUG_ON(!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent);
176
177 iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
178 BUG_ON(!iommu_drvdata);
179
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700180
181 ret = __enable_clocks(iommu_drvdata);
182 if (ret)
183 goto fail;
184
Steve Mucklef132c6c2012-06-06 18:30:57 -0700185 asid = GET_CB_CONTEXTIDR_ASID(iommu_drvdata->base,
186 ctx_drvdata->num);
187
188 SET_TLBIVA(iommu_drvdata->base, ctx_drvdata->num,
189 asid | (va & CB_TLBIVA_VA));
190 mb();
Stepan Moskovchenko22d32c62012-07-11 18:00:06 -0700191 __sync_tlb(iommu_drvdata->base, ctx_drvdata->num);
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700192 __disable_clocks(iommu_drvdata);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700193 }
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700194fail:
195 return ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700196}
197
198static int __flush_iotlb(struct iommu_domain *domain)
199{
200 struct msm_priv *priv = domain->priv;
201 struct msm_iommu_drvdata *iommu_drvdata;
202 struct msm_iommu_ctx_drvdata *ctx_drvdata;
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700203 int ret = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700204 int asid;
205
206 list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) {
207 BUG_ON(!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent);
208
209 iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
210 BUG_ON(!iommu_drvdata);
211
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700212 ret = __enable_clocks(iommu_drvdata);
213 if (ret)
214 goto fail;
215
Steve Mucklef132c6c2012-06-06 18:30:57 -0700216 asid = GET_CB_CONTEXTIDR_ASID(iommu_drvdata->base,
217 ctx_drvdata->num);
218
219 SET_TLBIASID(iommu_drvdata->base, ctx_drvdata->num, asid);
220 mb();
Stepan Moskovchenko22d32c62012-07-11 18:00:06 -0700221 __sync_tlb(iommu_drvdata->base, ctx_drvdata->num);
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700222 __disable_clocks(iommu_drvdata);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700223 }
224
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700225fail:
226 return ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700227}
228
Laura Abbottf4daa692012-10-10 19:31:53 -0700229/*
230 * May only be called for non-secure iommus
231 */
Stepan Moskovchenko00f0cac2012-10-05 23:56:05 -0700232static void __reset_iommu(void __iomem *base)
Sathish Ambleycf045e62012-06-07 12:56:50 -0700233{
Stepan Moskovchenko00f0cac2012-10-05 23:56:05 -0700234 int i, smt_size;
Sathish Ambleycf045e62012-06-07 12:56:50 -0700235
236 SET_ACR(base, 0);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700237 SET_CR2(base, 0);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700238 SET_GFAR(base, 0);
239 SET_GFSRRESTORE(base, 0);
240 SET_TLBIALLNSNH(base, 0);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700241 SET_SCR1(base, 0);
242 SET_SSDR_N(base, 0, 0);
Stepan Moskovchenko00f0cac2012-10-05 23:56:05 -0700243 smt_size = GET_IDR0_NUMSMRG(base);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700244
Stepan Moskovchenko518ca102012-06-27 15:15:26 -0700245 for (i = 0; i < smt_size; i++)
Sathish Ambleycf045e62012-06-07 12:56:50 -0700246 SET_SMR_VALID(base, i, 0);
247
248 mb();
249}
250
Laura Abbottf4daa692012-10-10 19:31:53 -0700251/*
252 * May only be called for non-secure iommus
253 */
Olav Hauganf3782732013-01-11 11:23:30 -0800254static void __program_iommu(void __iomem *base)
Sathish Ambleycf045e62012-06-07 12:56:50 -0700255{
Stepan Moskovchenko00f0cac2012-10-05 23:56:05 -0700256 __reset_iommu(base);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700257
258 SET_CR0_SMCFCFG(base, 1);
259 SET_CR0_USFCFG(base, 1);
260 SET_CR0_STALLD(base, 1);
261 SET_CR0_GCFGFIE(base, 1);
262 SET_CR0_GCFGFRE(base, 1);
263 SET_CR0_GFIE(base, 1);
264 SET_CR0_GFRE(base, 1);
265 SET_CR0_CLIENTPD(base, 0);
Stepan Moskovchenko880a3182012-10-01 12:35:24 -0700266
Olav Hauganf3782732013-01-11 11:23:30 -0800267 mb(); /* Make sure writes complete before returning */
268}
269
270void program_iommu_bfb_settings(void __iomem *base,
271 const struct msm_iommu_bfb_settings *bfb_settings)
272{
273 unsigned int i;
Stepan Moskovchenko880a3182012-10-01 12:35:24 -0700274 if (bfb_settings)
275 for (i = 0; i < bfb_settings->length; i++)
276 SET_GLOBAL_REG(base, bfb_settings->regs[i],
277 bfb_settings->data[i]);
278
Olav Hauganf3782732013-01-11 11:23:30 -0800279 mb(); /* Make sure writes complete before returning */
Sathish Ambleycf045e62012-06-07 12:56:50 -0700280}
281
Steve Mucklef132c6c2012-06-06 18:30:57 -0700282static void __reset_context(void __iomem *base, int ctx)
283{
284 SET_ACTLR(base, ctx, 0);
285 SET_FAR(base, ctx, 0);
286 SET_FSRRESTORE(base, ctx, 0);
287 SET_NMRR(base, ctx, 0);
288 SET_PAR(base, ctx, 0);
289 SET_PRRR(base, ctx, 0);
290 SET_SCTLR(base, ctx, 0);
291 SET_TLBIALL(base, ctx, 0);
292 SET_TTBCR(base, ctx, 0);
293 SET_TTBR0(base, ctx, 0);
294 SET_TTBR1(base, ctx, 0);
295 mb();
296}
297
Stepan Moskovchenko00f0cac2012-10-05 23:56:05 -0700298static void __release_smg(void __iomem *base, int ctx)
Stepan Moskovchenkoce749352012-10-04 19:02:03 -0700299{
Stepan Moskovchenko00f0cac2012-10-05 23:56:05 -0700300 int i, smt_size;
301 smt_size = GET_IDR0_NUMSMRG(base);
Stepan Moskovchenkoce749352012-10-04 19:02:03 -0700302
303 /* Invalidate any SMGs associated with this context */
304 for (i = 0; i < smt_size; i++)
305 if (GET_SMR_VALID(base, i) &&
306 GET_S2CR_CBNDX(base, i) == ctx)
307 SET_SMR_VALID(base, i, 0);
308}
309
Olav Haugan26ddd432012-12-07 11:39:21 -0800310static void msm_iommu_assign_ASID(const struct msm_iommu_drvdata *iommu_drvdata,
311 struct msm_iommu_ctx_drvdata *curr_ctx,
312 phys_addr_t pgtable)
313{
314 struct platform_device *pdev;
315 struct device_node *child;
316 struct msm_iommu_ctx_drvdata *ctx;
317 unsigned int found = 0;
318 void __iomem *base = iommu_drvdata->base;
319 struct device_node *iommu_node = iommu_drvdata->dev->of_node;
320 unsigned int asid;
321 unsigned int ncb = iommu_drvdata->ncb;
322
323 /* Find if this page table is used elsewhere, and re-use ASID */
324 for_each_child_of_node(iommu_node, child) {
325 pdev = of_find_device_by_node(child);
326 ctx = dev_get_drvdata(&pdev->dev);
327
328 if (ctx->secure_context) {
329 of_dev_put(pdev);
330 continue;
331 }
332
333 if ((ctx != curr_ctx) &&
334 (GET_CB_TTBR0_ADDR(base, ctx->num) == pgtable)) {
335 SET_CB_CONTEXTIDR_ASID(base, curr_ctx->num, ctx->asid);
336 curr_ctx->asid = ctx->asid;
337 found = 1;
338 of_dev_put(pdev);
339 of_node_put(child);
340 break;
341 }
342 of_dev_put(pdev);
343 }
344
345 /* If page table is new, find an unused ASID */
346 if (!found) {
347 for (asid = 1; asid < ncb + 1; ++asid) {
348 found = 0;
349 for_each_child_of_node(iommu_node, child) {
350 pdev = of_find_device_by_node(child);
351 ctx = dev_get_drvdata(&pdev->dev);
352
353 if (ctx != curr_ctx && ctx->asid == asid) {
354 found = 1;
355 of_dev_put(pdev);
356 of_node_put(child);
357 break;
358 }
359 of_dev_put(pdev);
360 }
361 if (!found) {
362 SET_CB_CONTEXTIDR_ASID(base, curr_ctx->num,
363 asid);
364 curr_ctx->asid = asid;
365 break;
366 }
367 }
368 BUG_ON(found);
369 }
370}
371
372static void __program_context(struct msm_iommu_drvdata *iommu_drvdata,
373 struct msm_iommu_ctx_drvdata *ctx_drvdata,
374 phys_addr_t pgtable, int redirect, bool is_secure)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700375{
376 unsigned int prrr, nmrr;
377 unsigned int pn;
Olav Haugan26ddd432012-12-07 11:39:21 -0800378 int num = 0, i, smt_size;
379 void __iomem *base = iommu_drvdata->base;
380 unsigned int ctx = ctx_drvdata->num;
381 u32 *sids = ctx_drvdata->sids;
382 int len = ctx_drvdata->nsid;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700383
384 __reset_context(base, ctx);
Laura Abbottf4daa692012-10-10 19:31:53 -0700385
Steve Mucklef132c6c2012-06-06 18:30:57 -0700386 pn = pgtable >> CB_TTBR0_ADDR_SHIFT;
387 SET_TTBCR(base, ctx, 0);
388 SET_CB_TTBR0_ADDR(base, ctx, pn);
389
390 /* Enable context fault interrupt */
391 SET_CB_SCTLR_CFIE(base, ctx, 1);
392
393 /* Redirect all cacheable requests to L2 slave port. */
394 SET_CB_ACTLR_BPRCISH(base, ctx, 1);
395 SET_CB_ACTLR_BPRCOSH(base, ctx, 1);
396 SET_CB_ACTLR_BPRCNSH(base, ctx, 1);
397
398 /* Turn on TEX Remap */
399 SET_CB_SCTLR_TRE(base, ctx, 1);
400
401 /* Enable private ASID namespace */
402 SET_CB_SCTLR_ASIDPNE(base, ctx, 1);
403
404 /* Set TEX remap attributes */
405 RCP15_PRRR(prrr);
406 RCP15_NMRR(nmrr);
407 SET_PRRR(base, ctx, prrr);
408 SET_NMRR(base, ctx, nmrr);
409
410 /* Configure page tables as inner-cacheable and shareable to reduce
411 * the TLB miss penalty.
412 */
413 if (redirect) {
414 SET_CB_TTBR0_S(base, ctx, 1);
415 SET_CB_TTBR0_NOS(base, ctx, 1);
416 SET_CB_TTBR0_IRGN1(base, ctx, 0); /* WB, WA */
417 SET_CB_TTBR0_IRGN0(base, ctx, 1);
418 SET_CB_TTBR0_RGN(base, ctx, 1); /* WB, WA */
419 }
420
Laura Abbottf4daa692012-10-10 19:31:53 -0700421 if (!is_secure) {
422 smt_size = GET_IDR0_NUMSMRG(base);
423 /* Program the M2V tables for this context */
424 for (i = 0; i < len / sizeof(*sids); i++) {
425 for (; num < smt_size; num++)
426 if (GET_SMR_VALID(base, num) == 0)
427 break;
428 BUG_ON(num >= smt_size);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700429
Laura Abbottf4daa692012-10-10 19:31:53 -0700430 SET_SMR_VALID(base, num, 1);
431 SET_SMR_MASK(base, num, 0);
432 SET_SMR_ID(base, num, sids[i]);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700433
Laura Abbottf4daa692012-10-10 19:31:53 -0700434 SET_S2CR_N(base, num, 0);
435 SET_S2CR_CBNDX(base, num, ctx);
436 SET_S2CR_MEMATTR(base, num, 0x0A);
437 /* Set security bit override to be Non-secure */
438 SET_S2CR_NSCFG(base, num, 3);
439 }
440 SET_CBAR_N(base, ctx, 0);
441
442 /* Stage 1 Context with Stage 2 bypass */
443 SET_CBAR_TYPE(base, ctx, 1);
444
445 /* Route page faults to the non-secure interrupt */
446 SET_CBAR_IRPTNDX(base, ctx, 1);
447
448 /* Set VMID to non-secure HLOS */
449 SET_CBAR_VMID(base, ctx, 3);
450
451 /* Bypass is treated as inner-shareable */
452 SET_CBAR_BPSHCFG(base, ctx, 2);
453
454 /* Do not downgrade memory attributes */
455 SET_CBAR_MEMATTR(base, ctx, 0x0A);
456
Sathish Ambleycf045e62012-06-07 12:56:50 -0700457 }
458
Olav Haugan26ddd432012-12-07 11:39:21 -0800459 msm_iommu_assign_ASID(iommu_drvdata, ctx_drvdata, pn);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700460
461 /* Enable the MMU */
462 SET_CB_SCTLR_M(base, ctx, 1);
463 mb();
464}
465
466static int msm_iommu_domain_init(struct iommu_domain *domain, int flags)
467{
468 struct msm_priv *priv;
469
470 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
471 if (!priv)
472 goto fail_nomem;
473
474#ifdef CONFIG_IOMMU_PGTABLES_L2
475 priv->pt.redirect = flags & MSM_IOMMU_DOMAIN_PT_CACHEABLE;
476#endif
477
478 INIT_LIST_HEAD(&priv->list_attached);
479 if (msm_iommu_pagetable_alloc(&priv->pt))
480 goto fail_nomem;
481
482 domain->priv = priv;
483 return 0;
484
485fail_nomem:
486 kfree(priv);
487 return -ENOMEM;
488}
489
490static void msm_iommu_domain_destroy(struct iommu_domain *domain)
491{
492 struct msm_priv *priv;
493
494 mutex_lock(&msm_iommu_lock);
495 priv = domain->priv;
496 domain->priv = NULL;
497
498 if (priv)
499 msm_iommu_pagetable_free(&priv->pt);
500
501 kfree(priv);
502 mutex_unlock(&msm_iommu_lock);
503}
504
Sathish Ambleycf045e62012-06-07 12:56:50 -0700505static int msm_iommu_ctx_attached(struct device *dev)
506{
507 struct platform_device *pdev;
508 struct device_node *child;
509 struct msm_iommu_ctx_drvdata *ctx;
510
511 for_each_child_of_node(dev->of_node, child) {
512 pdev = of_find_device_by_node(child);
513
514 ctx = dev_get_drvdata(&pdev->dev);
515 if (ctx->attached_domain) {
Olav Hauganbd3e9332012-12-19 10:50:02 -0800516 of_dev_put(pdev);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700517 of_node_put(child);
518 return 1;
519 }
Olav Hauganbd3e9332012-12-19 10:50:02 -0800520 of_dev_put(pdev);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700521 }
522
523 return 0;
524}
525
Steve Mucklef132c6c2012-06-06 18:30:57 -0700526static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
527{
528 struct msm_priv *priv;
529 struct msm_iommu_drvdata *iommu_drvdata;
530 struct msm_iommu_ctx_drvdata *ctx_drvdata;
531 struct msm_iommu_ctx_drvdata *tmp_drvdata;
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -0700532 int ret;
Laura Abbottf4daa692012-10-10 19:31:53 -0700533 int is_secure;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700534
535 mutex_lock(&msm_iommu_lock);
536
537 priv = domain->priv;
538 if (!priv || !dev) {
539 ret = -EINVAL;
540 goto fail;
541 }
542
543 iommu_drvdata = dev_get_drvdata(dev->parent);
544 ctx_drvdata = dev_get_drvdata(dev);
545 if (!iommu_drvdata || !ctx_drvdata) {
546 ret = -EINVAL;
547 goto fail;
548 }
549
550 if (!list_empty(&ctx_drvdata->attached_elm)) {
551 ret = -EBUSY;
552 goto fail;
553 }
554
555 list_for_each_entry(tmp_drvdata, &priv->list_attached, attached_elm)
556 if (tmp_drvdata == ctx_drvdata) {
557 ret = -EBUSY;
558 goto fail;
559 }
560
Laura Abbottf4daa692012-10-10 19:31:53 -0700561 is_secure = iommu_drvdata->sec_id != -1;
562
Olav Haugan2648d972013-01-07 17:32:31 -0800563 ret = __enable_regulators(iommu_drvdata);
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700564 if (ret)
565 goto fail;
566
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -0700567 ret = __enable_clocks(iommu_drvdata);
568 if (ret) {
Olav Haugan2648d972013-01-07 17:32:31 -0800569 __disable_regulators(iommu_drvdata);
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -0700570 goto fail;
571 }
572
Laura Abbottf4daa692012-10-10 19:31:53 -0700573 if (!msm_iommu_ctx_attached(dev->parent)) {
574 if (!is_secure) {
Olav Hauganf3782732013-01-11 11:23:30 -0800575 __program_iommu(iommu_drvdata->base);
Laura Abbottf4daa692012-10-10 19:31:53 -0700576 } else {
577 ret = msm_iommu_sec_program_iommu(
578 iommu_drvdata->sec_id);
579 if (ret) {
Olav Haugan2648d972013-01-07 17:32:31 -0800580 __disable_regulators(iommu_drvdata);
Laura Abbottf4daa692012-10-10 19:31:53 -0700581 __disable_clocks(iommu_drvdata);
582 goto fail;
583 }
584 }
Olav Hauganf3782732013-01-11 11:23:30 -0800585 program_iommu_bfb_settings(iommu_drvdata->base,
586 iommu_drvdata->bfb_settings);
Laura Abbottf4daa692012-10-10 19:31:53 -0700587 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700588
Olav Haugan26ddd432012-12-07 11:39:21 -0800589 __program_context(iommu_drvdata, ctx_drvdata, __pa(priv->pt.fl_table),
590 priv->pt.redirect, is_secure);
591
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700592 __disable_clocks(iommu_drvdata);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700593
Steve Mucklef132c6c2012-06-06 18:30:57 -0700594 list_add(&(ctx_drvdata->attached_elm), &priv->list_attached);
595 ctx_drvdata->attached_domain = domain;
596
Olav Haugan5ebfbc62013-01-07 17:49:10 -0800597 mutex_unlock(&msm_iommu_lock);
598
599 msm_iommu_attached(dev->parent);
600 return ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700601fail:
602 mutex_unlock(&msm_iommu_lock);
603 return ret;
604}
605
606static void msm_iommu_detach_dev(struct iommu_domain *domain,
607 struct device *dev)
608{
609 struct msm_priv *priv;
610 struct msm_iommu_drvdata *iommu_drvdata;
611 struct msm_iommu_ctx_drvdata *ctx_drvdata;
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700612 int ret;
Laura Abbottf4daa692012-10-10 19:31:53 -0700613 int is_secure;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700614
Olav Haugan5ebfbc62013-01-07 17:49:10 -0800615 msm_iommu_detached(dev->parent);
616
Steve Mucklef132c6c2012-06-06 18:30:57 -0700617 mutex_lock(&msm_iommu_lock);
618 priv = domain->priv;
619 if (!priv || !dev)
620 goto fail;
621
622 iommu_drvdata = dev_get_drvdata(dev->parent);
623 ctx_drvdata = dev_get_drvdata(dev);
624 if (!iommu_drvdata || !ctx_drvdata || !ctx_drvdata->attached_domain)
625 goto fail;
626
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700627 ret = __enable_clocks(iommu_drvdata);
628 if (ret)
629 goto fail;
630
Laura Abbottf4daa692012-10-10 19:31:53 -0700631 is_secure = iommu_drvdata->sec_id != -1;
632
Olav Haugan26ddd432012-12-07 11:39:21 -0800633 SET_TLBIASID(iommu_drvdata->base, ctx_drvdata->num, ctx_drvdata->asid);
634 ctx_drvdata->asid = -1;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700635
636 __reset_context(iommu_drvdata->base, ctx_drvdata->num);
Laura Abbottf4daa692012-10-10 19:31:53 -0700637 if (!is_secure)
638 __release_smg(iommu_drvdata->base, ctx_drvdata->num);
Stepan Moskovchenkoce749352012-10-04 19:02:03 -0700639
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700640 __disable_clocks(iommu_drvdata);
641
Olav Haugan2648d972013-01-07 17:32:31 -0800642 __disable_regulators(iommu_drvdata);
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -0700643
Steve Mucklef132c6c2012-06-06 18:30:57 -0700644 list_del_init(&ctx_drvdata->attached_elm);
645 ctx_drvdata->attached_domain = NULL;
646
Steve Mucklef132c6c2012-06-06 18:30:57 -0700647fail:
648 mutex_unlock(&msm_iommu_lock);
649}
650
651static int msm_iommu_map(struct iommu_domain *domain, unsigned long va,
652 phys_addr_t pa, size_t len, int prot)
653{
654 struct msm_priv *priv;
655 int ret = 0;
656
657 mutex_lock(&msm_iommu_lock);
658
659 priv = domain->priv;
660 if (!priv) {
661 ret = -EINVAL;
662 goto fail;
663 }
664
665 ret = msm_iommu_pagetable_map(&priv->pt, va, pa, len, prot);
666 if (ret)
667 goto fail;
668
669 ret = __flush_iotlb_va(domain, va);
670fail:
671 mutex_unlock(&msm_iommu_lock);
672 return ret;
673}
674
675static size_t msm_iommu_unmap(struct iommu_domain *domain, unsigned long va,
676 size_t len)
677{
678 struct msm_priv *priv;
679 int ret = -ENODEV;
680
681 mutex_lock(&msm_iommu_lock);
682
683 priv = domain->priv;
684 if (!priv)
685 goto fail;
686
687 ret = msm_iommu_pagetable_unmap(&priv->pt, va, len);
688 if (ret < 0)
689 goto fail;
690
691 ret = __flush_iotlb_va(domain, va);
692fail:
693 mutex_unlock(&msm_iommu_lock);
694
695 /* the IOMMU API requires us to return how many bytes were unmapped */
696 len = ret ? 0 : len;
697 return len;
698}
699
700static int msm_iommu_map_range(struct iommu_domain *domain, unsigned int va,
701 struct scatterlist *sg, unsigned int len,
702 int prot)
703{
704 int ret;
705 struct msm_priv *priv;
706
707 mutex_lock(&msm_iommu_lock);
708
709 priv = domain->priv;
710 if (!priv) {
711 ret = -EINVAL;
712 goto fail;
713 }
714
715 ret = msm_iommu_pagetable_map_range(&priv->pt, va, sg, len, prot);
716 if (ret)
717 goto fail;
718
719 __flush_iotlb(domain);
720fail:
721 mutex_unlock(&msm_iommu_lock);
722 return ret;
723}
724
725
726static int msm_iommu_unmap_range(struct iommu_domain *domain, unsigned int va,
727 unsigned int len)
728{
729 struct msm_priv *priv;
730
731 mutex_lock(&msm_iommu_lock);
732
733 priv = domain->priv;
734 msm_iommu_pagetable_unmap_range(&priv->pt, va, len);
735
736 __flush_iotlb(domain);
737 mutex_unlock(&msm_iommu_lock);
738 return 0;
739}
740
741static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain,
742 unsigned long va)
743{
744 struct msm_priv *priv;
745 struct msm_iommu_drvdata *iommu_drvdata;
746 struct msm_iommu_ctx_drvdata *ctx_drvdata;
747 unsigned int par;
748 void __iomem *base;
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700749 phys_addr_t ret = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700750 int ctx;
751
752 mutex_lock(&msm_iommu_lock);
753
754 priv = domain->priv;
755 if (list_empty(&priv->list_attached))
756 goto fail;
757
758 ctx_drvdata = list_entry(priv->list_attached.next,
759 struct msm_iommu_ctx_drvdata, attached_elm);
760 iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
761
762 base = iommu_drvdata->base;
763 ctx = ctx_drvdata->num;
764
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700765 ret = __enable_clocks(iommu_drvdata);
766 if (ret) {
767 ret = 0; /* 0 indicates translation failed */
768 goto fail;
769 }
770
Steve Mucklef132c6c2012-06-06 18:30:57 -0700771 SET_ATS1PR(base, ctx, va & CB_ATS1PR_ADDR);
772 mb();
773 while (GET_CB_ATSR_ACTIVE(base, ctx))
774 cpu_relax();
775
776 par = GET_PAR(base, ctx);
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700777 __disable_clocks(iommu_drvdata);
778
Steve Mucklef132c6c2012-06-06 18:30:57 -0700779 if (par & CB_PAR_F) {
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700780 ret = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700781 } else {
782 /* We are dealing with a supersection */
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700783 if (ret & CB_PAR_SS)
784 ret = (par & 0xFF000000) | (va & 0x00FFFFFF);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700785 else /* Upper 20 bits from PAR, lower 12 from VA */
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700786 ret = (par & 0xFFFFF000) | (va & 0x00000FFF);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700787 }
788
789fail:
790 mutex_unlock(&msm_iommu_lock);
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700791 return ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700792}
793
794static int msm_iommu_domain_has_cap(struct iommu_domain *domain,
795 unsigned long cap)
796{
797 return 0;
798}
799
800static void print_ctx_regs(void __iomem *base, int ctx, unsigned int fsr)
801{
802 pr_err("FAR = %08x PAR = %08x\n",
803 GET_FAR(base, ctx), GET_PAR(base, ctx));
804 pr_err("FSR = %08x [%s%s%s%s%s%s%s%s%s]\n", fsr,
805 (fsr & 0x02) ? "TF " : "",
806 (fsr & 0x04) ? "AFF " : "",
807 (fsr & 0x08) ? "PF " : "",
808 (fsr & 0x10) ? "EF " : "",
809 (fsr & 0x20) ? "TLBMCF " : "",
810 (fsr & 0x40) ? "TLBLKF " : "",
811 (fsr & 0x80) ? "MHF " : "",
812 (fsr & 0x40000000) ? "SS " : "",
813 (fsr & 0x80000000) ? "MULTI " : "");
814
815 pr_err("FSYNR0 = %08x FSYNR1 = %08x\n",
816 GET_FSYNR0(base, ctx), GET_FSYNR1(base, ctx));
817 pr_err("TTBR0 = %08x TTBR1 = %08x\n",
818 GET_TTBR0(base, ctx), GET_TTBR1(base, ctx));
819 pr_err("SCTLR = %08x ACTLR = %08x\n",
820 GET_SCTLR(base, ctx), GET_ACTLR(base, ctx));
821 pr_err("PRRR = %08x NMRR = %08x\n",
822 GET_PRRR(base, ctx), GET_NMRR(base, ctx));
823}
824
825irqreturn_t msm_iommu_fault_handler_v2(int irq, void *dev_id)
826{
827 struct platform_device *pdev = dev_id;
828 struct msm_iommu_drvdata *drvdata;
829 struct msm_iommu_ctx_drvdata *ctx_drvdata;
830 unsigned int fsr;
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700831 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700832
833 mutex_lock(&msm_iommu_lock);
834
835 BUG_ON(!pdev);
836
837 drvdata = dev_get_drvdata(pdev->dev.parent);
838 BUG_ON(!drvdata);
839
840 ctx_drvdata = dev_get_drvdata(&pdev->dev);
841 BUG_ON(!ctx_drvdata);
842
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700843 ret = __enable_clocks(drvdata);
844 if (ret) {
845 ret = IRQ_NONE;
846 goto fail;
847 }
848
Steve Mucklef132c6c2012-06-06 18:30:57 -0700849 fsr = GET_FSR(drvdata->base, ctx_drvdata->num);
850 if (fsr) {
851 if (!ctx_drvdata->attached_domain) {
852 pr_err("Bad domain in interrupt handler\n");
853 ret = -ENOSYS;
854 } else
855 ret = report_iommu_fault(ctx_drvdata->attached_domain,
856 &ctx_drvdata->pdev->dev,
857 GET_FAR(drvdata->base, ctx_drvdata->num), 0);
858
859 if (ret == -ENOSYS) {
860 pr_err("Unexpected IOMMU page fault!\n");
861 pr_err("name = %s\n", drvdata->name);
862 pr_err("context = %s (%d)\n", ctx_drvdata->name,
863 ctx_drvdata->num);
864 pr_err("Interesting registers:\n");
865 print_ctx_regs(drvdata->base, ctx_drvdata->num, fsr);
866 }
867
868 SET_FSR(drvdata->base, ctx_drvdata->num, fsr);
869 ret = IRQ_HANDLED;
870 } else
871 ret = IRQ_NONE;
872
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700873 __disable_clocks(drvdata);
874fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700875 mutex_unlock(&msm_iommu_lock);
876 return ret;
877}
878
879static phys_addr_t msm_iommu_get_pt_base_addr(struct iommu_domain *domain)
880{
881 struct msm_priv *priv = domain->priv;
882 return __pa(priv->pt.fl_table);
883}
884
885static struct iommu_ops msm_iommu_ops = {
886 .domain_init = msm_iommu_domain_init,
887 .domain_destroy = msm_iommu_domain_destroy,
888 .attach_dev = msm_iommu_attach_dev,
889 .detach_dev = msm_iommu_detach_dev,
890 .map = msm_iommu_map,
891 .unmap = msm_iommu_unmap,
892 .map_range = msm_iommu_map_range,
893 .unmap_range = msm_iommu_unmap_range,
894 .iova_to_phys = msm_iommu_iova_to_phys,
895 .domain_has_cap = msm_iommu_domain_has_cap,
896 .get_pt_base_addr = msm_iommu_get_pt_base_addr,
897 .pgsize_bitmap = MSM_IOMMU_PGSIZES,
898};
899
900static int __init msm_iommu_init(void)
901{
902 msm_iommu_pagetable_init();
903 bus_set_iommu(&platform_bus_type, &msm_iommu_ops);
904 return 0;
905}
906
907subsys_initcall(msm_iommu_init);
908
909MODULE_LICENSE("GPL v2");
910MODULE_DESCRIPTION("MSM SMMU v2 Driver");