blob: 24d28549425939490c7ad3aa580da8b0c3812869 [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
Olav Haugane6d01ef2013-01-25 16:55:44 -080031#include <mach/iommu_hw-v1.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070032#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);
Olav Hauganaf4eb0b2013-02-06 09:51:48 -080095 /* Ensure clock is on before continuing */
96 mb();
Olav Haugan3c7fb382013-01-02 17:32:25 -080097 }
Steve Mucklef132c6c2012-06-06 18:30:57 -070098fail:
99 return ret;
100}
101
102static void __disable_clocks(struct msm_iommu_drvdata *drvdata)
103{
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -0700104 if (drvdata->aclk)
105 clk_disable_unprepare(drvdata->aclk);
106 clk_disable_unprepare(drvdata->clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700107 clk_disable_unprepare(drvdata->pclk);
108}
109
Olav Haugan5ebfbc62013-01-07 17:49:10 -0800110static int _iommu_power_off(void *data)
111{
112 struct msm_iommu_drvdata *drvdata;
113
114 drvdata = (struct msm_iommu_drvdata *)data;
115 __disable_clocks(drvdata);
116 __disable_regulators(drvdata);
117 return 0;
118}
119
120static int _iommu_power_on(void *data)
121{
122 int ret;
123 struct msm_iommu_drvdata *drvdata;
124
125 drvdata = (struct msm_iommu_drvdata *)data;
126 ret = __enable_regulators(drvdata);
127 if (ret)
128 goto fail;
129
130 ret = __enable_clocks(drvdata);
131 if (ret) {
132 __disable_regulators(drvdata);
133 goto fail;
134 }
135 return 0;
136fail:
137 return -EIO;
138}
139
140static void _iommu_lock_acquire(void)
141{
142 mutex_lock(&msm_iommu_lock);
143}
144
145static void _iommu_lock_release(void)
146{
147 mutex_unlock(&msm_iommu_lock);
148}
149
Olav Hauganef69e892013-02-04 13:47:08 -0800150struct iommu_access_ops iommu_access_ops_v1 = {
Olav Haugan5ebfbc62013-01-07 17:49:10 -0800151 .iommu_power_on = _iommu_power_on,
152 .iommu_power_off = _iommu_power_off,
153 .iommu_lock_acquire = _iommu_lock_acquire,
154 .iommu_lock_release = _iommu_lock_release,
155};
Olav Hauganef69e892013-02-04 13:47:08 -0800156EXPORT_SYMBOL(iommu_access_ops_v1);
Olav Haugan5ebfbc62013-01-07 17:49:10 -0800157
Olav Haugancd932192013-01-31 18:30:15 -0800158void iommu_halt(const struct msm_iommu_drvdata *iommu_drvdata)
159{
160 if (iommu_drvdata->halt_enabled) {
161 SET_MICRO_MMU_CTRL_HALT_REQ(iommu_drvdata->base, 1);
162
163 while (GET_MICRO_MMU_CTRL_IDLE(iommu_drvdata->base) == 0)
164 cpu_relax();
165 /* Ensure device is idle before continuing */
166 mb();
167 }
168}
169
170void iommu_resume(const struct msm_iommu_drvdata *iommu_drvdata)
171{
172 if (iommu_drvdata->halt_enabled) {
173 /*
174 * Ensure transactions have completed before releasing
175 * the halt
176 */
177 mb();
178 SET_MICRO_MMU_CTRL_HALT_REQ(iommu_drvdata->base, 0);
Olav Haugana142c982013-02-07 12:33:05 -0800179 /*
180 * Ensure write is complete before continuing to ensure
181 * we don't turn off clocks while transaction is still
182 * pending.
183 */
184 mb();
Olav Haugancd932192013-01-31 18:30:15 -0800185 }
186}
187
Stepan Moskovchenko22d32c62012-07-11 18:00:06 -0700188static void __sync_tlb(void __iomem *base, int ctx)
189{
190 SET_TLBSYNC(base, ctx, 0);
191
192 /* No barrier needed due to register proximity */
193 while (GET_CB_TLBSTATUS_SACTIVE(base, ctx))
194 cpu_relax();
195
196 /* No barrier needed due to read dependency */
197}
198
Steve Mucklef132c6c2012-06-06 18:30:57 -0700199static int __flush_iotlb_va(struct iommu_domain *domain, unsigned int va)
200{
201 struct msm_priv *priv = domain->priv;
202 struct msm_iommu_drvdata *iommu_drvdata;
203 struct msm_iommu_ctx_drvdata *ctx_drvdata;
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700204 int ret = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700205
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
213 ret = __enable_clocks(iommu_drvdata);
214 if (ret)
215 goto fail;
216
Steve Mucklef132c6c2012-06-06 18:30:57 -0700217 SET_TLBIVA(iommu_drvdata->base, ctx_drvdata->num,
Olav Haugan4e315c42013-03-06 10:14:28 -0800218 ctx_drvdata->asid | (va & CB_TLBIVA_VA));
Steve Mucklef132c6c2012-06-06 18:30:57 -0700219 mb();
Stepan Moskovchenko22d32c62012-07-11 18:00:06 -0700220 __sync_tlb(iommu_drvdata->base, ctx_drvdata->num);
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700221 __disable_clocks(iommu_drvdata);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700222 }
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700223fail:
224 return ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700225}
226
227static int __flush_iotlb(struct iommu_domain *domain)
228{
229 struct msm_priv *priv = domain->priv;
230 struct msm_iommu_drvdata *iommu_drvdata;
231 struct msm_iommu_ctx_drvdata *ctx_drvdata;
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700232 int ret = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700233
234 list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) {
235 BUG_ON(!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent);
236
237 iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
238 BUG_ON(!iommu_drvdata);
239
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700240 ret = __enable_clocks(iommu_drvdata);
241 if (ret)
242 goto fail;
243
Olav Haugan4e315c42013-03-06 10:14:28 -0800244 SET_TLBIASID(iommu_drvdata->base, ctx_drvdata->num,
245 ctx_drvdata->asid);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700246 mb();
Stepan Moskovchenko22d32c62012-07-11 18:00:06 -0700247 __sync_tlb(iommu_drvdata->base, ctx_drvdata->num);
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700248 __disable_clocks(iommu_drvdata);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700249 }
250
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700251fail:
252 return ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700253}
254
Laura Abbottf4daa692012-10-10 19:31:53 -0700255/*
256 * May only be called for non-secure iommus
257 */
Stepan Moskovchenko00f0cac2012-10-05 23:56:05 -0700258static void __reset_iommu(void __iomem *base)
Sathish Ambleycf045e62012-06-07 12:56:50 -0700259{
Stepan Moskovchenko00f0cac2012-10-05 23:56:05 -0700260 int i, smt_size;
Sathish Ambleycf045e62012-06-07 12:56:50 -0700261
262 SET_ACR(base, 0);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700263 SET_CR2(base, 0);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700264 SET_GFAR(base, 0);
265 SET_GFSRRESTORE(base, 0);
266 SET_TLBIALLNSNH(base, 0);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700267 SET_SCR1(base, 0);
268 SET_SSDR_N(base, 0, 0);
Stepan Moskovchenko00f0cac2012-10-05 23:56:05 -0700269 smt_size = GET_IDR0_NUMSMRG(base);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700270
Stepan Moskovchenko518ca102012-06-27 15:15:26 -0700271 for (i = 0; i < smt_size; i++)
Sathish Ambleycf045e62012-06-07 12:56:50 -0700272 SET_SMR_VALID(base, i, 0);
273
274 mb();
275}
276
Laura Abbottf4daa692012-10-10 19:31:53 -0700277/*
278 * May only be called for non-secure iommus
279 */
Olav Hauganf3782732013-01-11 11:23:30 -0800280static void __program_iommu(void __iomem *base)
Sathish Ambleycf045e62012-06-07 12:56:50 -0700281{
Stepan Moskovchenko00f0cac2012-10-05 23:56:05 -0700282 __reset_iommu(base);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700283
284 SET_CR0_SMCFCFG(base, 1);
285 SET_CR0_USFCFG(base, 1);
286 SET_CR0_STALLD(base, 1);
287 SET_CR0_GCFGFIE(base, 1);
288 SET_CR0_GCFGFRE(base, 1);
289 SET_CR0_GFIE(base, 1);
290 SET_CR0_GFRE(base, 1);
291 SET_CR0_CLIENTPD(base, 0);
Stepan Moskovchenko880a3182012-10-01 12:35:24 -0700292
Olav Hauganf3782732013-01-11 11:23:30 -0800293 mb(); /* Make sure writes complete before returning */
294}
295
296void program_iommu_bfb_settings(void __iomem *base,
297 const struct msm_iommu_bfb_settings *bfb_settings)
298{
299 unsigned int i;
Stepan Moskovchenko880a3182012-10-01 12:35:24 -0700300 if (bfb_settings)
301 for (i = 0; i < bfb_settings->length; i++)
302 SET_GLOBAL_REG(base, bfb_settings->regs[i],
303 bfb_settings->data[i]);
304
Olav Hauganf3782732013-01-11 11:23:30 -0800305 mb(); /* Make sure writes complete before returning */
Sathish Ambleycf045e62012-06-07 12:56:50 -0700306}
307
Steve Mucklef132c6c2012-06-06 18:30:57 -0700308static void __reset_context(void __iomem *base, int ctx)
309{
310 SET_ACTLR(base, ctx, 0);
311 SET_FAR(base, ctx, 0);
312 SET_FSRRESTORE(base, ctx, 0);
313 SET_NMRR(base, ctx, 0);
314 SET_PAR(base, ctx, 0);
315 SET_PRRR(base, ctx, 0);
316 SET_SCTLR(base, ctx, 0);
317 SET_TLBIALL(base, ctx, 0);
318 SET_TTBCR(base, ctx, 0);
319 SET_TTBR0(base, ctx, 0);
320 SET_TTBR1(base, ctx, 0);
321 mb();
322}
323
Stepan Moskovchenko00f0cac2012-10-05 23:56:05 -0700324static void __release_smg(void __iomem *base, int ctx)
Stepan Moskovchenkoce749352012-10-04 19:02:03 -0700325{
Stepan Moskovchenko00f0cac2012-10-05 23:56:05 -0700326 int i, smt_size;
327 smt_size = GET_IDR0_NUMSMRG(base);
Stepan Moskovchenkoce749352012-10-04 19:02:03 -0700328
329 /* Invalidate any SMGs associated with this context */
330 for (i = 0; i < smt_size; i++)
331 if (GET_SMR_VALID(base, i) &&
332 GET_S2CR_CBNDX(base, i) == ctx)
333 SET_SMR_VALID(base, i, 0);
334}
335
Olav Haugan26ddd432012-12-07 11:39:21 -0800336static void msm_iommu_assign_ASID(const struct msm_iommu_drvdata *iommu_drvdata,
337 struct msm_iommu_ctx_drvdata *curr_ctx,
Olav Haugan4e315c42013-03-06 10:14:28 -0800338 struct msm_priv *priv)
Olav Haugan26ddd432012-12-07 11:39:21 -0800339{
Olav Haugan26ddd432012-12-07 11:39:21 -0800340 unsigned int found = 0;
341 void __iomem *base = iommu_drvdata->base;
Olav Haugan4e315c42013-03-06 10:14:28 -0800342 unsigned int i;
Olav Haugan26ddd432012-12-07 11:39:21 -0800343 unsigned int ncb = iommu_drvdata->ncb;
Olav Haugan4e315c42013-03-06 10:14:28 -0800344 struct msm_iommu_ctx_drvdata *tmp_drvdata;
Olav Haugan26ddd432012-12-07 11:39:21 -0800345
346 /* Find if this page table is used elsewhere, and re-use ASID */
Olav Haugan4e315c42013-03-06 10:14:28 -0800347 if (!list_empty(&priv->list_attached)) {
348 tmp_drvdata = list_first_entry(&priv->list_attached,
349 struct msm_iommu_ctx_drvdata, attached_elm);
Olav Haugan26ddd432012-12-07 11:39:21 -0800350
Olav Haugan4e315c42013-03-06 10:14:28 -0800351 ++iommu_drvdata->asid[tmp_drvdata->asid - 1];
352 curr_ctx->asid = tmp_drvdata->asid;
Olav Haugan26ddd432012-12-07 11:39:21 -0800353
Olav Haugan4e315c42013-03-06 10:14:28 -0800354 SET_CB_CONTEXTIDR_ASID(base, curr_ctx->num, curr_ctx->asid);
355 found = 1;
Olav Haugan26ddd432012-12-07 11:39:21 -0800356 }
357
358 /* If page table is new, find an unused ASID */
359 if (!found) {
Olav Haugan4e315c42013-03-06 10:14:28 -0800360 for (i = 0; i < ncb; ++i) {
361 if (iommu_drvdata->asid[i] == 0) {
362 ++iommu_drvdata->asid[i];
363 curr_ctx->asid = i + 1;
Olav Haugan26ddd432012-12-07 11:39:21 -0800364
Olav Haugan26ddd432012-12-07 11:39:21 -0800365 SET_CB_CONTEXTIDR_ASID(base, curr_ctx->num,
Olav Haugan4e315c42013-03-06 10:14:28 -0800366 curr_ctx->asid);
367 found = 1;
Olav Haugan26ddd432012-12-07 11:39:21 -0800368 break;
369 }
370 }
Olav Haugan4e315c42013-03-06 10:14:28 -0800371 BUG_ON(!found);
Olav Haugan26ddd432012-12-07 11:39:21 -0800372 }
373}
374
375static void __program_context(struct msm_iommu_drvdata *iommu_drvdata,
376 struct msm_iommu_ctx_drvdata *ctx_drvdata,
Olav Haugan4e315c42013-03-06 10:14:28 -0800377 struct msm_priv *priv, bool is_secure)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700378{
379 unsigned int prrr, nmrr;
380 unsigned int pn;
Olav Haugan26ddd432012-12-07 11:39:21 -0800381 int num = 0, i, smt_size;
382 void __iomem *base = iommu_drvdata->base;
383 unsigned int ctx = ctx_drvdata->num;
384 u32 *sids = ctx_drvdata->sids;
385 int len = ctx_drvdata->nsid;
Olav Haugan4e315c42013-03-06 10:14:28 -0800386 phys_addr_t pgtable = __pa(priv->pt.fl_table);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700387
388 __reset_context(base, ctx);
Laura Abbottf4daa692012-10-10 19:31:53 -0700389
Steve Mucklef132c6c2012-06-06 18:30:57 -0700390 pn = pgtable >> CB_TTBR0_ADDR_SHIFT;
391 SET_TTBCR(base, ctx, 0);
392 SET_CB_TTBR0_ADDR(base, ctx, pn);
393
394 /* Enable context fault interrupt */
395 SET_CB_SCTLR_CFIE(base, ctx, 1);
396
397 /* Redirect all cacheable requests to L2 slave port. */
398 SET_CB_ACTLR_BPRCISH(base, ctx, 1);
399 SET_CB_ACTLR_BPRCOSH(base, ctx, 1);
400 SET_CB_ACTLR_BPRCNSH(base, ctx, 1);
401
402 /* Turn on TEX Remap */
403 SET_CB_SCTLR_TRE(base, ctx, 1);
404
405 /* Enable private ASID namespace */
406 SET_CB_SCTLR_ASIDPNE(base, ctx, 1);
407
408 /* Set TEX remap attributes */
409 RCP15_PRRR(prrr);
410 RCP15_NMRR(nmrr);
411 SET_PRRR(base, ctx, prrr);
412 SET_NMRR(base, ctx, nmrr);
413
414 /* Configure page tables as inner-cacheable and shareable to reduce
415 * the TLB miss penalty.
416 */
Olav Haugan4e315c42013-03-06 10:14:28 -0800417 if (priv->pt.redirect) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700418 SET_CB_TTBR0_S(base, ctx, 1);
419 SET_CB_TTBR0_NOS(base, ctx, 1);
420 SET_CB_TTBR0_IRGN1(base, ctx, 0); /* WB, WA */
421 SET_CB_TTBR0_IRGN0(base, ctx, 1);
422 SET_CB_TTBR0_RGN(base, ctx, 1); /* WB, WA */
423 }
424
Laura Abbottf4daa692012-10-10 19:31:53 -0700425 if (!is_secure) {
426 smt_size = GET_IDR0_NUMSMRG(base);
427 /* Program the M2V tables for this context */
428 for (i = 0; i < len / sizeof(*sids); i++) {
429 for (; num < smt_size; num++)
430 if (GET_SMR_VALID(base, num) == 0)
431 break;
432 BUG_ON(num >= smt_size);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700433
Laura Abbottf4daa692012-10-10 19:31:53 -0700434 SET_SMR_VALID(base, num, 1);
435 SET_SMR_MASK(base, num, 0);
436 SET_SMR_ID(base, num, sids[i]);
Sathish Ambleycf045e62012-06-07 12:56:50 -0700437
Laura Abbottf4daa692012-10-10 19:31:53 -0700438 SET_S2CR_N(base, num, 0);
439 SET_S2CR_CBNDX(base, num, ctx);
440 SET_S2CR_MEMATTR(base, num, 0x0A);
441 /* Set security bit override to be Non-secure */
442 SET_S2CR_NSCFG(base, num, 3);
443 }
444 SET_CBAR_N(base, ctx, 0);
445
446 /* Stage 1 Context with Stage 2 bypass */
447 SET_CBAR_TYPE(base, ctx, 1);
448
449 /* Route page faults to the non-secure interrupt */
450 SET_CBAR_IRPTNDX(base, ctx, 1);
451
452 /* Set VMID to non-secure HLOS */
453 SET_CBAR_VMID(base, ctx, 3);
454
455 /* Bypass is treated as inner-shareable */
456 SET_CBAR_BPSHCFG(base, ctx, 2);
457
458 /* Do not downgrade memory attributes */
459 SET_CBAR_MEMATTR(base, ctx, 0x0A);
460
Sathish Ambleycf045e62012-06-07 12:56:50 -0700461 }
462
Olav Haugan4e315c42013-03-06 10:14:28 -0800463 msm_iommu_assign_ASID(iommu_drvdata, ctx_drvdata, priv);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700464
465 /* Enable the MMU */
466 SET_CB_SCTLR_M(base, ctx, 1);
467 mb();
468}
469
470static int msm_iommu_domain_init(struct iommu_domain *domain, int flags)
471{
472 struct msm_priv *priv;
473
474 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
475 if (!priv)
476 goto fail_nomem;
477
478#ifdef CONFIG_IOMMU_PGTABLES_L2
479 priv->pt.redirect = flags & MSM_IOMMU_DOMAIN_PT_CACHEABLE;
480#endif
481
482 INIT_LIST_HEAD(&priv->list_attached);
483 if (msm_iommu_pagetable_alloc(&priv->pt))
484 goto fail_nomem;
485
486 domain->priv = priv;
487 return 0;
488
489fail_nomem:
490 kfree(priv);
491 return -ENOMEM;
492}
493
494static void msm_iommu_domain_destroy(struct iommu_domain *domain)
495{
496 struct msm_priv *priv;
497
498 mutex_lock(&msm_iommu_lock);
499 priv = domain->priv;
500 domain->priv = NULL;
501
502 if (priv)
503 msm_iommu_pagetable_free(&priv->pt);
504
505 kfree(priv);
506 mutex_unlock(&msm_iommu_lock);
507}
508
509static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
510{
511 struct msm_priv *priv;
512 struct msm_iommu_drvdata *iommu_drvdata;
513 struct msm_iommu_ctx_drvdata *ctx_drvdata;
514 struct msm_iommu_ctx_drvdata *tmp_drvdata;
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -0700515 int ret;
Laura Abbottf4daa692012-10-10 19:31:53 -0700516 int is_secure;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700517
518 mutex_lock(&msm_iommu_lock);
519
520 priv = domain->priv;
521 if (!priv || !dev) {
522 ret = -EINVAL;
523 goto fail;
524 }
525
526 iommu_drvdata = dev_get_drvdata(dev->parent);
527 ctx_drvdata = dev_get_drvdata(dev);
528 if (!iommu_drvdata || !ctx_drvdata) {
529 ret = -EINVAL;
530 goto fail;
531 }
532
533 if (!list_empty(&ctx_drvdata->attached_elm)) {
534 ret = -EBUSY;
535 goto fail;
536 }
537
538 list_for_each_entry(tmp_drvdata, &priv->list_attached, attached_elm)
539 if (tmp_drvdata == ctx_drvdata) {
540 ret = -EBUSY;
541 goto fail;
542 }
543
Laura Abbottf4daa692012-10-10 19:31:53 -0700544 is_secure = iommu_drvdata->sec_id != -1;
545
Olav Haugan2648d972013-01-07 17:32:31 -0800546 ret = __enable_regulators(iommu_drvdata);
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700547 if (ret)
548 goto fail;
549
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -0700550 ret = __enable_clocks(iommu_drvdata);
551 if (ret) {
Olav Haugan2648d972013-01-07 17:32:31 -0800552 __disable_regulators(iommu_drvdata);
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -0700553 goto fail;
554 }
555
Olav Haugane3885392013-03-06 16:22:53 -0800556 /* We can only do this once */
557 if (!iommu_drvdata->ctx_attach_count) {
Laura Abbottf4daa692012-10-10 19:31:53 -0700558 if (!is_secure) {
Olav Haugance2eab92013-02-07 12:59:18 -0800559 iommu_halt(iommu_drvdata);
Olav Hauganf3782732013-01-11 11:23:30 -0800560 __program_iommu(iommu_drvdata->base);
Olav Haugance2eab92013-02-07 12:59:18 -0800561 iommu_resume(iommu_drvdata);
Laura Abbottf4daa692012-10-10 19:31:53 -0700562 } else {
563 ret = msm_iommu_sec_program_iommu(
564 iommu_drvdata->sec_id);
565 if (ret) {
Olav Haugan2648d972013-01-07 17:32:31 -0800566 __disable_regulators(iommu_drvdata);
Laura Abbottf4daa692012-10-10 19:31:53 -0700567 __disable_clocks(iommu_drvdata);
568 goto fail;
569 }
570 }
Olav Hauganf3782732013-01-11 11:23:30 -0800571 program_iommu_bfb_settings(iommu_drvdata->base,
572 iommu_drvdata->bfb_settings);
Laura Abbottf4daa692012-10-10 19:31:53 -0700573 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700574
Olav Haugance2eab92013-02-07 12:59:18 -0800575 iommu_halt(iommu_drvdata);
576
Olav Haugan4e315c42013-03-06 10:14:28 -0800577 __program_context(iommu_drvdata, ctx_drvdata, priv, is_secure);
Olav Haugan26ddd432012-12-07 11:39:21 -0800578
Olav Haugancd932192013-01-31 18:30:15 -0800579 iommu_resume(iommu_drvdata);
580
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700581 __disable_clocks(iommu_drvdata);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700582
Steve Mucklef132c6c2012-06-06 18:30:57 -0700583 list_add(&(ctx_drvdata->attached_elm), &priv->list_attached);
584 ctx_drvdata->attached_domain = domain;
Olav Haugane3885392013-03-06 16:22:53 -0800585 ++iommu_drvdata->ctx_attach_count;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700586
Olav Haugan5ebfbc62013-01-07 17:49:10 -0800587 mutex_unlock(&msm_iommu_lock);
588
589 msm_iommu_attached(dev->parent);
590 return ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700591fail:
592 mutex_unlock(&msm_iommu_lock);
593 return ret;
594}
595
596static void msm_iommu_detach_dev(struct iommu_domain *domain,
597 struct device *dev)
598{
599 struct msm_priv *priv;
600 struct msm_iommu_drvdata *iommu_drvdata;
601 struct msm_iommu_ctx_drvdata *ctx_drvdata;
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700602 int ret;
Laura Abbottf4daa692012-10-10 19:31:53 -0700603 int is_secure;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700604
Olav Haugan5ebfbc62013-01-07 17:49:10 -0800605 msm_iommu_detached(dev->parent);
606
Steve Mucklef132c6c2012-06-06 18:30:57 -0700607 mutex_lock(&msm_iommu_lock);
608 priv = domain->priv;
609 if (!priv || !dev)
610 goto fail;
611
612 iommu_drvdata = dev_get_drvdata(dev->parent);
613 ctx_drvdata = dev_get_drvdata(dev);
614 if (!iommu_drvdata || !ctx_drvdata || !ctx_drvdata->attached_domain)
615 goto fail;
616
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700617 ret = __enable_clocks(iommu_drvdata);
618 if (ret)
619 goto fail;
620
Laura Abbottf4daa692012-10-10 19:31:53 -0700621 is_secure = iommu_drvdata->sec_id != -1;
622
Olav Haugan26ddd432012-12-07 11:39:21 -0800623 SET_TLBIASID(iommu_drvdata->base, ctx_drvdata->num, ctx_drvdata->asid);
Olav Haugan4e315c42013-03-06 10:14:28 -0800624
625 BUG_ON(iommu_drvdata->asid[ctx_drvdata->asid - 1] == 0);
626 iommu_drvdata->asid[ctx_drvdata->asid - 1]--;
Olav Haugan26ddd432012-12-07 11:39:21 -0800627 ctx_drvdata->asid = -1;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700628
Olav Haugancd932192013-01-31 18:30:15 -0800629 iommu_halt(iommu_drvdata);
630
Steve Mucklef132c6c2012-06-06 18:30:57 -0700631 __reset_context(iommu_drvdata->base, ctx_drvdata->num);
Laura Abbottf4daa692012-10-10 19:31:53 -0700632 if (!is_secure)
633 __release_smg(iommu_drvdata->base, ctx_drvdata->num);
Stepan Moskovchenkoce749352012-10-04 19:02:03 -0700634
Olav Haugancd932192013-01-31 18:30:15 -0800635 iommu_resume(iommu_drvdata);
636
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700637 __disable_clocks(iommu_drvdata);
638
Olav Haugan2648d972013-01-07 17:32:31 -0800639 __disable_regulators(iommu_drvdata);
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -0700640
Steve Mucklef132c6c2012-06-06 18:30:57 -0700641 list_del_init(&ctx_drvdata->attached_elm);
642 ctx_drvdata->attached_domain = NULL;
Olav Haugane3885392013-03-06 16:22:53 -0800643 BUG_ON(iommu_drvdata->ctx_attach_count == 0);
644 --iommu_drvdata->ctx_attach_count;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700645fail:
646 mutex_unlock(&msm_iommu_lock);
647}
648
649static int msm_iommu_map(struct iommu_domain *domain, unsigned long va,
650 phys_addr_t pa, size_t len, int prot)
651{
652 struct msm_priv *priv;
653 int ret = 0;
654
655 mutex_lock(&msm_iommu_lock);
656
657 priv = domain->priv;
658 if (!priv) {
659 ret = -EINVAL;
660 goto fail;
661 }
662
663 ret = msm_iommu_pagetable_map(&priv->pt, va, pa, len, prot);
664 if (ret)
665 goto fail;
666
667 ret = __flush_iotlb_va(domain, va);
668fail:
669 mutex_unlock(&msm_iommu_lock);
670 return ret;
671}
672
673static size_t msm_iommu_unmap(struct iommu_domain *domain, unsigned long va,
674 size_t len)
675{
676 struct msm_priv *priv;
677 int ret = -ENODEV;
678
679 mutex_lock(&msm_iommu_lock);
680
681 priv = domain->priv;
682 if (!priv)
683 goto fail;
684
685 ret = msm_iommu_pagetable_unmap(&priv->pt, va, len);
686 if (ret < 0)
687 goto fail;
688
689 ret = __flush_iotlb_va(domain, va);
690fail:
691 mutex_unlock(&msm_iommu_lock);
692
693 /* the IOMMU API requires us to return how many bytes were unmapped */
694 len = ret ? 0 : len;
695 return len;
696}
697
698static int msm_iommu_map_range(struct iommu_domain *domain, unsigned int va,
699 struct scatterlist *sg, unsigned int len,
700 int prot)
701{
702 int ret;
703 struct msm_priv *priv;
704
705 mutex_lock(&msm_iommu_lock);
706
707 priv = domain->priv;
708 if (!priv) {
709 ret = -EINVAL;
710 goto fail;
711 }
712
713 ret = msm_iommu_pagetable_map_range(&priv->pt, va, sg, len, prot);
714 if (ret)
715 goto fail;
716
717 __flush_iotlb(domain);
718fail:
719 mutex_unlock(&msm_iommu_lock);
720 return ret;
721}
722
723
724static int msm_iommu_unmap_range(struct iommu_domain *domain, unsigned int va,
725 unsigned int len)
726{
727 struct msm_priv *priv;
728
729 mutex_lock(&msm_iommu_lock);
730
731 priv = domain->priv;
732 msm_iommu_pagetable_unmap_range(&priv->pt, va, len);
733
734 __flush_iotlb(domain);
735 mutex_unlock(&msm_iommu_lock);
736 return 0;
737}
738
739static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain,
740 unsigned long va)
741{
742 struct msm_priv *priv;
743 struct msm_iommu_drvdata *iommu_drvdata;
744 struct msm_iommu_ctx_drvdata *ctx_drvdata;
745 unsigned int par;
746 void __iomem *base;
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700747 phys_addr_t ret = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700748 int ctx;
749
750 mutex_lock(&msm_iommu_lock);
751
752 priv = domain->priv;
753 if (list_empty(&priv->list_attached))
754 goto fail;
755
756 ctx_drvdata = list_entry(priv->list_attached.next,
757 struct msm_iommu_ctx_drvdata, attached_elm);
758 iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
759
760 base = iommu_drvdata->base;
761 ctx = ctx_drvdata->num;
762
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700763 ret = __enable_clocks(iommu_drvdata);
764 if (ret) {
765 ret = 0; /* 0 indicates translation failed */
766 goto fail;
767 }
768
Steve Mucklef132c6c2012-06-06 18:30:57 -0700769 SET_ATS1PR(base, ctx, va & CB_ATS1PR_ADDR);
770 mb();
771 while (GET_CB_ATSR_ACTIVE(base, ctx))
772 cpu_relax();
773
774 par = GET_PAR(base, ctx);
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700775 __disable_clocks(iommu_drvdata);
776
Steve Mucklef132c6c2012-06-06 18:30:57 -0700777 if (par & CB_PAR_F) {
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700778 ret = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700779 } else {
780 /* We are dealing with a supersection */
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700781 if (ret & CB_PAR_SS)
782 ret = (par & 0xFF000000) | (va & 0x00FFFFFF);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700783 else /* Upper 20 bits from PAR, lower 12 from VA */
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700784 ret = (par & 0xFFFFF000) | (va & 0x00000FFF);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700785 }
786
787fail:
788 mutex_unlock(&msm_iommu_lock);
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700789 return ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700790}
791
792static int msm_iommu_domain_has_cap(struct iommu_domain *domain,
793 unsigned long cap)
794{
795 return 0;
796}
797
798static void print_ctx_regs(void __iomem *base, int ctx, unsigned int fsr)
799{
800 pr_err("FAR = %08x PAR = %08x\n",
801 GET_FAR(base, ctx), GET_PAR(base, ctx));
802 pr_err("FSR = %08x [%s%s%s%s%s%s%s%s%s]\n", fsr,
803 (fsr & 0x02) ? "TF " : "",
804 (fsr & 0x04) ? "AFF " : "",
805 (fsr & 0x08) ? "PF " : "",
806 (fsr & 0x10) ? "EF " : "",
807 (fsr & 0x20) ? "TLBMCF " : "",
808 (fsr & 0x40) ? "TLBLKF " : "",
809 (fsr & 0x80) ? "MHF " : "",
810 (fsr & 0x40000000) ? "SS " : "",
811 (fsr & 0x80000000) ? "MULTI " : "");
812
813 pr_err("FSYNR0 = %08x FSYNR1 = %08x\n",
814 GET_FSYNR0(base, ctx), GET_FSYNR1(base, ctx));
815 pr_err("TTBR0 = %08x TTBR1 = %08x\n",
816 GET_TTBR0(base, ctx), GET_TTBR1(base, ctx));
817 pr_err("SCTLR = %08x ACTLR = %08x\n",
818 GET_SCTLR(base, ctx), GET_ACTLR(base, ctx));
819 pr_err("PRRR = %08x NMRR = %08x\n",
820 GET_PRRR(base, ctx), GET_NMRR(base, ctx));
821}
822
823irqreturn_t msm_iommu_fault_handler_v2(int irq, void *dev_id)
824{
825 struct platform_device *pdev = dev_id;
826 struct msm_iommu_drvdata *drvdata;
827 struct msm_iommu_ctx_drvdata *ctx_drvdata;
828 unsigned int fsr;
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700829 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700830
831 mutex_lock(&msm_iommu_lock);
832
833 BUG_ON(!pdev);
834
835 drvdata = dev_get_drvdata(pdev->dev.parent);
836 BUG_ON(!drvdata);
837
838 ctx_drvdata = dev_get_drvdata(&pdev->dev);
839 BUG_ON(!ctx_drvdata);
840
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700841 ret = __enable_clocks(drvdata);
842 if (ret) {
843 ret = IRQ_NONE;
844 goto fail;
845 }
846
Steve Mucklef132c6c2012-06-06 18:30:57 -0700847 fsr = GET_FSR(drvdata->base, ctx_drvdata->num);
848 if (fsr) {
849 if (!ctx_drvdata->attached_domain) {
850 pr_err("Bad domain in interrupt handler\n");
851 ret = -ENOSYS;
852 } else
853 ret = report_iommu_fault(ctx_drvdata->attached_domain,
854 &ctx_drvdata->pdev->dev,
855 GET_FAR(drvdata->base, ctx_drvdata->num), 0);
856
857 if (ret == -ENOSYS) {
858 pr_err("Unexpected IOMMU page fault!\n");
859 pr_err("name = %s\n", drvdata->name);
860 pr_err("context = %s (%d)\n", ctx_drvdata->name,
861 ctx_drvdata->num);
862 pr_err("Interesting registers:\n");
863 print_ctx_regs(drvdata->base, ctx_drvdata->num, fsr);
864 }
865
866 SET_FSR(drvdata->base, ctx_drvdata->num, fsr);
867 ret = IRQ_HANDLED;
868 } else
869 ret = IRQ_NONE;
870
Stepan Moskovchenko0bab7482012-06-21 17:15:01 -0700871 __disable_clocks(drvdata);
872fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700873 mutex_unlock(&msm_iommu_lock);
874 return ret;
875}
876
877static phys_addr_t msm_iommu_get_pt_base_addr(struct iommu_domain *domain)
878{
879 struct msm_priv *priv = domain->priv;
880 return __pa(priv->pt.fl_table);
881}
882
883static struct iommu_ops msm_iommu_ops = {
884 .domain_init = msm_iommu_domain_init,
885 .domain_destroy = msm_iommu_domain_destroy,
886 .attach_dev = msm_iommu_attach_dev,
887 .detach_dev = msm_iommu_detach_dev,
888 .map = msm_iommu_map,
889 .unmap = msm_iommu_unmap,
890 .map_range = msm_iommu_map_range,
891 .unmap_range = msm_iommu_unmap_range,
892 .iova_to_phys = msm_iommu_iova_to_phys,
893 .domain_has_cap = msm_iommu_domain_has_cap,
894 .get_pt_base_addr = msm_iommu_get_pt_base_addr,
895 .pgsize_bitmap = MSM_IOMMU_PGSIZES,
896};
897
898static int __init msm_iommu_init(void)
899{
900 msm_iommu_pagetable_init();
901 bus_set_iommu(&platform_bus_type, &msm_iommu_ops);
902 return 0;
903}
904
905subsys_initcall(msm_iommu_init);
906
907MODULE_LICENSE("GPL v2");
908MODULE_DESCRIPTION("MSM SMMU v2 Driver");