blob: 10c110bdf59aee5309b9fef5f946391c79d3b0f1 [file] [log] [blame]
Olav Haugan5622d1c2012-11-07 15:02:56 -08001/* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -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.
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -070011 */
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/spinlock.h>
22#include <linux/slab.h>
23#include <linux/iommu.h>
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -080024#include <linux/clk.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070025#include <linux/scatterlist.h>
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -070026
27#include <asm/cacheflush.h>
28#include <asm/sizes.h>
29
30#include <mach/iommu_hw-8xxx.h>
31#include <mach/iommu.h>
Olav Haugan5622d1c2012-11-07 15:02:56 -080032#include <mach/msm_smsm.h>
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -070033
Stepan Moskovchenko100832c2010-11-15 18:20:08 -080034#define MRC(reg, processor, op1, crn, crm, op2) \
35__asm__ __volatile__ ( \
36" mrc " #processor "," #op1 ", %0," #crn "," #crm "," #op2 "\n" \
37: "=r" (reg))
38
39#define RCP15_PRRR(reg) MRC(reg, p15, 0, c10, c2, 0)
40#define RCP15_NMRR(reg) MRC(reg, p15, 0, c10, c2, 1)
41
Steve Mucklef132c6c2012-06-06 18:30:57 -070042/* Sharability attributes of MSM IOMMU mappings */
43#define MSM_IOMMU_ATTR_NON_SH 0x0
44#define MSM_IOMMU_ATTR_SH 0x4
45
46/* Cacheability attributes of MSM IOMMU mappings */
47#define MSM_IOMMU_ATTR_NONCACHED 0x0
48#define MSM_IOMMU_ATTR_CACHED_WB_WA 0x1
49#define MSM_IOMMU_ATTR_CACHED_WB_NWA 0x2
50#define MSM_IOMMU_ATTR_CACHED_WT 0x3
51
52
53static inline void clean_pte(unsigned long *start, unsigned long *end,
54 int redirect)
55{
56 if (!redirect)
57 dmac_flush_range(start, end);
58}
59
Ohad Ben-Cohen83427272011-11-10 11:32:28 +020060/* bitmap of the page sizes currently supported */
61#define MSM_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
62
Stepan Moskovchenko100832c2010-11-15 18:20:08 -080063static int msm_iommu_tex_class[4];
64
Steve Mucklef132c6c2012-06-06 18:30:57 -070065DEFINE_MUTEX(msm_iommu_lock);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -070066
Olav Haugan5622d1c2012-11-07 15:02:56 -080067/**
68 * Remote spinlock implementation based on Peterson's algorithm to be used
69 * to synchronize IOMMU config port access between CPU and GPU.
70 * This implements Process 0 of the spin lock algorithm. GPU implements
71 * Process 1. Flag and turn is stored in shared memory to allow GPU to
72 * access these.
73 */
74struct msm_iommu_remote_lock {
75 int initialized;
76 struct remote_iommu_petersons_spinlock *lock;
77};
78
79static struct msm_iommu_remote_lock msm_iommu_remote_lock;
80
81#ifdef CONFIG_MSM_IOMMU_GPU_SYNC
82static void _msm_iommu_remote_spin_lock_init(void)
83{
84 msm_iommu_remote_lock.lock = smem_alloc(SMEM_SPINLOCK_ARRAY, 32);
85 memset(msm_iommu_remote_lock.lock, 0,
86 sizeof(*msm_iommu_remote_lock.lock));
87}
88
89void msm_iommu_remote_p0_spin_lock(void)
90{
91 msm_iommu_remote_lock.lock->flag[PROC_APPS] = 1;
92 msm_iommu_remote_lock.lock->turn = 1;
93
94 smp_mb();
95
96 while (msm_iommu_remote_lock.lock->flag[PROC_GPU] == 1 &&
97 msm_iommu_remote_lock.lock->turn == 1)
98 cpu_relax();
99}
100
101void msm_iommu_remote_p0_spin_unlock(void)
102{
103 smp_mb();
104
105 msm_iommu_remote_lock.lock->flag[PROC_APPS] = 0;
106}
107#endif
108
109inline void msm_iommu_mutex_lock(void)
110{
111 mutex_lock(&msm_iommu_lock);
112}
113
114inline void msm_iommu_mutex_unlock(void)
115{
116 mutex_unlock(&msm_iommu_lock);
117}
118
119void *msm_iommu_lock_initialize(void)
120{
121 mutex_lock(&msm_iommu_lock);
122 if (!msm_iommu_remote_lock.initialized) {
123 msm_iommu_remote_lock_init();
124 msm_iommu_remote_lock.initialized = 1;
125 }
126 mutex_unlock(&msm_iommu_lock);
127 return msm_iommu_remote_lock.lock;
128}
129
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700130struct msm_priv {
131 unsigned long *pgtable;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700132 int redirect;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700133 struct list_head list_attached;
134};
135
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -0800136static int __enable_clocks(struct msm_iommu_drvdata *drvdata)
137{
138 int ret;
139
Steve Mucklef132c6c2012-06-06 18:30:57 -0700140 ret = clk_prepare_enable(drvdata->pclk);
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -0800141 if (ret)
142 goto fail;
143
144 if (drvdata->clk) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700145 ret = clk_prepare_enable(drvdata->clk);
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -0800146 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700147 clk_disable_unprepare(drvdata->pclk);
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -0800148 }
149fail:
150 return ret;
151}
152
153static void __disable_clocks(struct msm_iommu_drvdata *drvdata)
154{
155 if (drvdata->clk)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700156 clk_disable_unprepare(drvdata->clk);
157 clk_disable_unprepare(drvdata->pclk);
158}
159
160static int __flush_iotlb_va(struct iommu_domain *domain, unsigned int va)
161{
162 struct msm_priv *priv = domain->priv;
163 struct msm_iommu_drvdata *iommu_drvdata;
164 struct msm_iommu_ctx_drvdata *ctx_drvdata;
165 int ret = 0;
166 int asid;
167
168 list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) {
169 if (!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent)
170 BUG();
171
172 iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
173 if (!iommu_drvdata)
174 BUG();
175
176 ret = __enable_clocks(iommu_drvdata);
177 if (ret)
178 goto fail;
179
Olav Haugan5622d1c2012-11-07 15:02:56 -0800180 msm_iommu_remote_spin_lock();
181
Steve Mucklef132c6c2012-06-06 18:30:57 -0700182 asid = GET_CONTEXTIDR_ASID(iommu_drvdata->base,
183 ctx_drvdata->num);
184
185 SET_TLBIVA(iommu_drvdata->base, ctx_drvdata->num,
186 asid | (va & TLBIVA_VA));
187 mb();
Olav Haugan5622d1c2012-11-07 15:02:56 -0800188
189 msm_iommu_remote_spin_unlock();
190
Steve Mucklef132c6c2012-06-06 18:30:57 -0700191 __disable_clocks(iommu_drvdata);
192 }
193fail:
194 return ret;
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -0800195}
196
Stepan Moskovchenko33069732010-11-12 19:30:00 -0800197static int __flush_iotlb(struct iommu_domain *domain)
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700198{
199 struct msm_priv *priv = domain->priv;
200 struct msm_iommu_drvdata *iommu_drvdata;
201 struct msm_iommu_ctx_drvdata *ctx_drvdata;
Stepan Moskovchenko33069732010-11-12 19:30:00 -0800202 int ret = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700203 int asid;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700204
205 list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) {
206 if (!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent)
207 BUG();
208
209 iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700210 if (!iommu_drvdata)
211 BUG();
Stepan Moskovchenko33069732010-11-12 19:30:00 -0800212
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -0800213 ret = __enable_clocks(iommu_drvdata);
214 if (ret)
215 goto fail;
216
Olav Haugan5622d1c2012-11-07 15:02:56 -0800217 msm_iommu_remote_spin_lock();
218
Steve Mucklef132c6c2012-06-06 18:30:57 -0700219 asid = GET_CONTEXTIDR_ASID(iommu_drvdata->base,
220 ctx_drvdata->num);
221
222 SET_TLBIASID(iommu_drvdata->base, ctx_drvdata->num, asid);
223 mb();
Olav Haugan5622d1c2012-11-07 15:02:56 -0800224
225 msm_iommu_remote_spin_unlock();
226
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -0800227 __disable_clocks(iommu_drvdata);
228 }
229fail:
Stepan Moskovchenko33069732010-11-12 19:30:00 -0800230 return ret;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700231}
232
233static void __reset_context(void __iomem *base, int ctx)
234{
235 SET_BPRCOSH(base, ctx, 0);
236 SET_BPRCISH(base, ctx, 0);
237 SET_BPRCNSH(base, ctx, 0);
238 SET_BPSHCFG(base, ctx, 0);
239 SET_BPMTCFG(base, ctx, 0);
240 SET_ACTLR(base, ctx, 0);
241 SET_SCTLR(base, ctx, 0);
242 SET_FSRRESTORE(base, ctx, 0);
243 SET_TTBR0(base, ctx, 0);
244 SET_TTBR1(base, ctx, 0);
245 SET_TTBCR(base, ctx, 0);
246 SET_BFBCR(base, ctx, 0);
247 SET_PAR(base, ctx, 0);
248 SET_FAR(base, ctx, 0);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700249 SET_TLBFLPTER(base, ctx, 0);
250 SET_TLBSLPTER(base, ctx, 0);
251 SET_TLBLKCR(base, ctx, 0);
252 SET_PRRR(base, ctx, 0);
253 SET_NMRR(base, ctx, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700254 mb();
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700255}
256
Steve Mucklef132c6c2012-06-06 18:30:57 -0700257static void __program_context(void __iomem *base, int ctx, int ncb,
258 phys_addr_t pgtable, int redirect,
259 int ttbr_split)
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700260{
Stepan Moskovchenko100832c2010-11-15 18:20:08 -0800261 unsigned int prrr, nmrr;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700262 int i, j, found;
Olav Haugan5622d1c2012-11-07 15:02:56 -0800263
264 msm_iommu_remote_spin_lock();
265
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700266 __reset_context(base, ctx);
267
268 /* Set up HTW mode */
269 /* TLB miss configuration: perform HTW on miss */
270 SET_TLBMCFG(base, ctx, 0x3);
271
272 /* V2P configuration: HTW for access */
273 SET_V2PCFG(base, ctx, 0x3);
274
Steve Mucklef132c6c2012-06-06 18:30:57 -0700275 SET_TTBCR(base, ctx, ttbr_split);
276 SET_TTBR0_PA(base, ctx, (pgtable >> TTBR0_PA_SHIFT));
277 if (ttbr_split)
278 SET_TTBR1_PA(base, ctx, (pgtable >> TTBR1_PA_SHIFT));
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700279
280 /* Enable context fault interrupt */
281 SET_CFEIE(base, ctx, 1);
282
283 /* Stall access on a context fault and let the handler deal with it */
284 SET_CFCFG(base, ctx, 1);
285
286 /* Redirect all cacheable requests to L2 slave port. */
287 SET_RCISH(base, ctx, 1);
288 SET_RCOSH(base, ctx, 1);
289 SET_RCNSH(base, ctx, 1);
290
291 /* Turn on TEX Remap */
292 SET_TRE(base, ctx, 1);
293
Stepan Moskovchenko100832c2010-11-15 18:20:08 -0800294 /* Set TEX remap attributes */
295 RCP15_PRRR(prrr);
296 RCP15_NMRR(nmrr);
297 SET_PRRR(base, ctx, prrr);
298 SET_NMRR(base, ctx, nmrr);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700299
300 /* Turn on BFB prefetch */
301 SET_BFBDFE(base, ctx, 1);
302
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700303 /* Configure page tables as inner-cacheable and shareable to reduce
304 * the TLB miss penalty.
305 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700306 if (redirect) {
307 SET_TTBR0_SH(base, ctx, 1);
308 SET_TTBR1_SH(base, ctx, 1);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700309
Steve Mucklef132c6c2012-06-06 18:30:57 -0700310 SET_TTBR0_NOS(base, ctx, 1);
311 SET_TTBR1_NOS(base, ctx, 1);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700312
Steve Mucklef132c6c2012-06-06 18:30:57 -0700313 SET_TTBR0_IRGNH(base, ctx, 0); /* WB, WA */
314 SET_TTBR0_IRGNL(base, ctx, 1);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700315
Steve Mucklef132c6c2012-06-06 18:30:57 -0700316 SET_TTBR1_IRGNH(base, ctx, 0); /* WB, WA */
317 SET_TTBR1_IRGNL(base, ctx, 1);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700318
Steve Mucklef132c6c2012-06-06 18:30:57 -0700319 SET_TTBR0_ORGN(base, ctx, 1); /* WB, WA */
320 SET_TTBR1_ORGN(base, ctx, 1); /* WB, WA */
321 }
322
323 /* Find if this page table is used elsewhere, and re-use ASID */
324 found = 0;
325 for (i = 0; i < ncb; i++)
326 if (GET_TTBR0_PA(base, i) == (pgtable >> TTBR0_PA_SHIFT) &&
327 i != ctx) {
328 SET_CONTEXTIDR_ASID(base, ctx, \
329 GET_CONTEXTIDR_ASID(base, i));
330 found = 1;
331 break;
332 }
333
334 /* If page table is new, find an unused ASID */
335 if (!found) {
336 for (i = 0; i < ncb; i++) {
337 found = 0;
338 for (j = 0; j < ncb; j++) {
339 if (GET_CONTEXTIDR_ASID(base, j) == i &&
340 j != ctx)
341 found = 1;
342 }
343
344 if (!found) {
345 SET_CONTEXTIDR_ASID(base, ctx, i);
346 break;
347 }
348 }
349 BUG_ON(found);
350 }
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700351
352 /* Enable the MMU */
353 SET_M(base, ctx, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700354 mb();
Olav Haugan5622d1c2012-11-07 15:02:56 -0800355
356 msm_iommu_remote_spin_unlock();
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700357}
358
Steve Mucklef132c6c2012-06-06 18:30:57 -0700359static int msm_iommu_domain_init(struct iommu_domain *domain, int flags)
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700360{
361 struct msm_priv *priv = kzalloc(sizeof(*priv), GFP_KERNEL);
362
363 if (!priv)
364 goto fail_nomem;
365
366 INIT_LIST_HEAD(&priv->list_attached);
367 priv->pgtable = (unsigned long *)__get_free_pages(GFP_KERNEL,
368 get_order(SZ_16K));
369
370 if (!priv->pgtable)
371 goto fail_nomem;
372
Steve Mucklef132c6c2012-06-06 18:30:57 -0700373#ifdef CONFIG_IOMMU_PGTABLES_L2
374 priv->redirect = flags & MSM_IOMMU_DOMAIN_PT_CACHEABLE;
375#endif
376
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700377 memset(priv->pgtable, 0, SZ_16K);
378 domain->priv = priv;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700379
380 clean_pte(priv->pgtable, priv->pgtable + NUM_FL_PTE, priv->redirect);
381
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700382 return 0;
383
384fail_nomem:
385 kfree(priv);
386 return -ENOMEM;
387}
388
389static void msm_iommu_domain_destroy(struct iommu_domain *domain)
390{
391 struct msm_priv *priv;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700392 unsigned long *fl_table;
393 int i;
394
Steve Mucklef132c6c2012-06-06 18:30:57 -0700395 mutex_lock(&msm_iommu_lock);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700396 priv = domain->priv;
397 domain->priv = NULL;
398
399 if (priv) {
400 fl_table = priv->pgtable;
401
402 for (i = 0; i < NUM_FL_PTE; i++)
403 if ((fl_table[i] & 0x03) == FL_TYPE_TABLE)
404 free_page((unsigned long) __va(((fl_table[i]) &
405 FL_BASE_MASK)));
406
407 free_pages((unsigned long)priv->pgtable, get_order(SZ_16K));
408 priv->pgtable = NULL;
409 }
410
411 kfree(priv);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700412 mutex_unlock(&msm_iommu_lock);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700413}
414
415static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
416{
417 struct msm_priv *priv;
418 struct msm_iommu_ctx_dev *ctx_dev;
419 struct msm_iommu_drvdata *iommu_drvdata;
420 struct msm_iommu_ctx_drvdata *ctx_drvdata;
421 struct msm_iommu_ctx_drvdata *tmp_drvdata;
422 int ret = 0;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700423
Steve Mucklef132c6c2012-06-06 18:30:57 -0700424 mutex_lock(&msm_iommu_lock);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700425
426 priv = domain->priv;
427
428 if (!priv || !dev) {
429 ret = -EINVAL;
430 goto fail;
431 }
432
433 iommu_drvdata = dev_get_drvdata(dev->parent);
434 ctx_drvdata = dev_get_drvdata(dev);
435 ctx_dev = dev->platform_data;
436
437 if (!iommu_drvdata || !ctx_drvdata || !ctx_dev) {
438 ret = -EINVAL;
439 goto fail;
440 }
441
Stepan Moskovchenko00d4b2b2010-11-12 19:29:56 -0800442 if (!list_empty(&ctx_drvdata->attached_elm)) {
443 ret = -EBUSY;
444 goto fail;
445 }
446
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700447 list_for_each_entry(tmp_drvdata, &priv->list_attached, attached_elm)
448 if (tmp_drvdata == ctx_drvdata) {
449 ret = -EBUSY;
450 goto fail;
451 }
452
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -0800453 ret = __enable_clocks(iommu_drvdata);
454 if (ret)
455 goto fail;
456
Steve Mucklef132c6c2012-06-06 18:30:57 -0700457 __program_context(iommu_drvdata->base, ctx_dev->num, iommu_drvdata->ncb,
458 __pa(priv->pgtable), priv->redirect,
459 iommu_drvdata->ttbr_split);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700460
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -0800461 __disable_clocks(iommu_drvdata);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700462 list_add(&(ctx_drvdata->attached_elm), &priv->list_attached);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700463
Steve Mucklef132c6c2012-06-06 18:30:57 -0700464 ctx_drvdata->attached_domain = domain;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700465fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700466 mutex_unlock(&msm_iommu_lock);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700467 return ret;
468}
469
470static void msm_iommu_detach_dev(struct iommu_domain *domain,
471 struct device *dev)
472{
473 struct msm_priv *priv;
474 struct msm_iommu_ctx_dev *ctx_dev;
475 struct msm_iommu_drvdata *iommu_drvdata;
476 struct msm_iommu_ctx_drvdata *ctx_drvdata;
Stepan Moskovchenko33069732010-11-12 19:30:00 -0800477 int ret;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700478
Steve Mucklef132c6c2012-06-06 18:30:57 -0700479 mutex_lock(&msm_iommu_lock);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700480 priv = domain->priv;
481
482 if (!priv || !dev)
483 goto fail;
484
485 iommu_drvdata = dev_get_drvdata(dev->parent);
486 ctx_drvdata = dev_get_drvdata(dev);
487 ctx_dev = dev->platform_data;
488
489 if (!iommu_drvdata || !ctx_drvdata || !ctx_dev)
490 goto fail;
491
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -0800492 ret = __enable_clocks(iommu_drvdata);
493 if (ret)
494 goto fail;
495
Olav Haugan5622d1c2012-11-07 15:02:56 -0800496 msm_iommu_remote_spin_lock();
497
Steve Mucklef132c6c2012-06-06 18:30:57 -0700498 SET_TLBIASID(iommu_drvdata->base, ctx_dev->num,
499 GET_CONTEXTIDR_ASID(iommu_drvdata->base, ctx_dev->num));
500
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700501 __reset_context(iommu_drvdata->base, ctx_dev->num);
Olav Haugan5622d1c2012-11-07 15:02:56 -0800502
503 msm_iommu_remote_spin_unlock();
504
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -0800505 __disable_clocks(iommu_drvdata);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700506 list_del_init(&ctx_drvdata->attached_elm);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700507 ctx_drvdata->attached_domain = NULL;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700508fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700509 mutex_unlock(&msm_iommu_lock);
510}
511
512static int __get_pgprot(int prot, int len)
513{
514 unsigned int pgprot;
515 int tex;
516
517 if (!(prot & (IOMMU_READ | IOMMU_WRITE))) {
518 prot |= IOMMU_READ | IOMMU_WRITE;
519 WARN_ONCE(1, "No attributes in iommu mapping; assuming RW\n");
520 }
521
522 if ((prot & IOMMU_WRITE) && !(prot & IOMMU_READ)) {
523 prot |= IOMMU_READ;
524 WARN_ONCE(1, "Write-only iommu mappings unsupported; falling back to RW\n");
525 }
526
527 if (prot & IOMMU_CACHE)
528 tex = (pgprot_kernel >> 2) & 0x07;
529 else
530 tex = msm_iommu_tex_class[MSM_IOMMU_ATTR_NONCACHED];
531
532 if (tex < 0 || tex > NUM_TEX_CLASS - 1)
533 return 0;
534
535 if (len == SZ_16M || len == SZ_1M) {
536 pgprot = FL_SHARED;
537 pgprot |= tex & 0x01 ? FL_BUFFERABLE : 0;
538 pgprot |= tex & 0x02 ? FL_CACHEABLE : 0;
539 pgprot |= tex & 0x04 ? FL_TEX0 : 0;
540 pgprot |= FL_AP0 | FL_AP1;
541 pgprot |= prot & IOMMU_WRITE ? 0 : FL_AP2;
542 } else {
543 pgprot = SL_SHARED;
544 pgprot |= tex & 0x01 ? SL_BUFFERABLE : 0;
545 pgprot |= tex & 0x02 ? SL_CACHEABLE : 0;
546 pgprot |= tex & 0x04 ? SL_TEX0 : 0;
547 pgprot |= SL_AP0 | SL_AP1;
548 pgprot |= prot & IOMMU_WRITE ? 0 : SL_AP2;
549 }
550
551 return pgprot;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700552}
553
554static int msm_iommu_map(struct iommu_domain *domain, unsigned long va,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +0200555 phys_addr_t pa, size_t len, int prot)
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700556{
557 struct msm_priv *priv;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700558 unsigned long *fl_table;
559 unsigned long *fl_pte;
560 unsigned long fl_offset;
561 unsigned long *sl_table;
562 unsigned long *sl_pte;
563 unsigned long sl_offset;
Stepan Moskovchenko100832c2010-11-15 18:20:08 -0800564 unsigned int pgprot;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700565 int ret = 0;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700566
Steve Mucklef132c6c2012-06-06 18:30:57 -0700567 mutex_lock(&msm_iommu_lock);
Stepan Moskovchenko100832c2010-11-15 18:20:08 -0800568
569 priv = domain->priv;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700570 if (!priv) {
571 ret = -EINVAL;
572 goto fail;
573 }
574
575 fl_table = priv->pgtable;
576
577 if (len != SZ_16M && len != SZ_1M &&
578 len != SZ_64K && len != SZ_4K) {
579 pr_debug("Bad size: %d\n", len);
580 ret = -EINVAL;
581 goto fail;
582 }
583
584 if (!fl_table) {
585 pr_debug("Null page table\n");
586 ret = -EINVAL;
587 goto fail;
588 }
589
Steve Mucklef132c6c2012-06-06 18:30:57 -0700590 pgprot = __get_pgprot(prot, len);
591
592 if (!pgprot) {
593 ret = -EINVAL;
594 goto fail;
Stepan Moskovchenko100832c2010-11-15 18:20:08 -0800595 }
596
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700597 fl_offset = FL_OFFSET(va); /* Upper 12 bits */
598 fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */
599
600 if (len == SZ_16M) {
601 int i = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700602
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700603 for (i = 0; i < 16; i++)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700604 if (*(fl_pte+i)) {
605 ret = -EBUSY;
606 goto fail;
607 }
608
609 for (i = 0; i < 16; i++)
610 *(fl_pte+i) = (pa & 0xFF000000) | FL_SUPERSECTION
611 | FL_TYPE_SECT | FL_SHARED | FL_NG | pgprot;
612 clean_pte(fl_pte, fl_pte + 16, priv->redirect);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700613 }
614
Steve Mucklef132c6c2012-06-06 18:30:57 -0700615 if (len == SZ_1M) {
616 if (*fl_pte) {
617 ret = -EBUSY;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700618 goto fail;
619 }
620
Steve Mucklef132c6c2012-06-06 18:30:57 -0700621 *fl_pte = (pa & 0xFFF00000) | FL_NG | FL_TYPE_SECT | FL_SHARED
622 | pgprot;
623 clean_pte(fl_pte, fl_pte + 1, priv->redirect);
624 }
625
626 /* Need a 2nd level table */
627 if (len == SZ_4K || len == SZ_64K) {
628
629 if (*fl_pte == 0) {
630 unsigned long *sl;
631 sl = (unsigned long *) __get_free_pages(GFP_KERNEL,
632 get_order(SZ_4K));
633
634 if (!sl) {
635 pr_debug("Could not allocate second level table\n");
636 ret = -ENOMEM;
637 goto fail;
638 }
639 memset(sl, 0, SZ_4K);
640 clean_pte(sl, sl + NUM_SL_PTE, priv->redirect);
641
642 *fl_pte = ((((int)__pa(sl)) & FL_BASE_MASK) | \
643 FL_TYPE_TABLE);
644
645 clean_pte(fl_pte, fl_pte + 1, priv->redirect);
646 }
647
648 if (!(*fl_pte & FL_TYPE_TABLE)) {
649 ret = -EBUSY;
650 goto fail;
651 }
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700652 }
653
654 sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK));
655 sl_offset = SL_OFFSET(va);
656 sl_pte = sl_table + sl_offset;
657
Steve Mucklef132c6c2012-06-06 18:30:57 -0700658 if (len == SZ_4K) {
659 if (*sl_pte) {
660 ret = -EBUSY;
661 goto fail;
662 }
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700663
Steve Mucklef132c6c2012-06-06 18:30:57 -0700664 *sl_pte = (pa & SL_BASE_MASK_SMALL) | SL_NG | SL_SHARED
665 | SL_TYPE_SMALL | pgprot;
666 clean_pte(sl_pte, sl_pte + 1, priv->redirect);
667 }
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700668
669 if (len == SZ_64K) {
670 int i;
671
672 for (i = 0; i < 16; i++)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700673 if (*(sl_pte+i)) {
674 ret = -EBUSY;
675 goto fail;
676 }
677
678 for (i = 0; i < 16; i++)
679 *(sl_pte+i) = (pa & SL_BASE_MASK_LARGE) | SL_NG
680 | SL_SHARED | SL_TYPE_LARGE | pgprot;
681
682 clean_pte(sl_pte, sl_pte + 16, priv->redirect);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700683 }
684
Steve Mucklef132c6c2012-06-06 18:30:57 -0700685 ret = __flush_iotlb_va(domain, va);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700686fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700687 mutex_unlock(&msm_iommu_lock);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700688 return ret;
689}
690
Ohad Ben-Cohen50090652011-11-10 11:32:25 +0200691static size_t msm_iommu_unmap(struct iommu_domain *domain, unsigned long va,
692 size_t len)
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700693{
694 struct msm_priv *priv;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700695 unsigned long *fl_table;
696 unsigned long *fl_pte;
697 unsigned long fl_offset;
698 unsigned long *sl_table;
699 unsigned long *sl_pte;
700 unsigned long sl_offset;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700701 int i, ret = 0;
702
Steve Mucklef132c6c2012-06-06 18:30:57 -0700703 mutex_lock(&msm_iommu_lock);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700704
705 priv = domain->priv;
706
Joerg Roedel05df1f32012-01-26 18:25:37 +0100707 if (!priv)
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700708 goto fail;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700709
710 fl_table = priv->pgtable;
711
712 if (len != SZ_16M && len != SZ_1M &&
713 len != SZ_64K && len != SZ_4K) {
714 pr_debug("Bad length: %d\n", len);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700715 goto fail;
716 }
717
718 if (!fl_table) {
719 pr_debug("Null page table\n");
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700720 goto fail;
721 }
722
723 fl_offset = FL_OFFSET(va); /* Upper 12 bits */
724 fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */
725
726 if (*fl_pte == 0) {
727 pr_debug("First level PTE is 0\n");
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700728 goto fail;
729 }
730
731 /* Unmap supersection */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700732 if (len == SZ_16M) {
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700733 for (i = 0; i < 16; i++)
734 *(fl_pte+i) = 0;
735
Steve Mucklef132c6c2012-06-06 18:30:57 -0700736 clean_pte(fl_pte, fl_pte + 16, priv->redirect);
737 }
738
739 if (len == SZ_1M) {
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700740 *fl_pte = 0;
741
Steve Mucklef132c6c2012-06-06 18:30:57 -0700742 clean_pte(fl_pte, fl_pte + 1, priv->redirect);
743 }
744
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700745 sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK));
746 sl_offset = SL_OFFSET(va);
747 sl_pte = sl_table + sl_offset;
748
749 if (len == SZ_64K) {
750 for (i = 0; i < 16; i++)
751 *(sl_pte+i) = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700752
753 clean_pte(sl_pte, sl_pte + 16, priv->redirect);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700754 }
755
Steve Mucklef132c6c2012-06-06 18:30:57 -0700756 if (len == SZ_4K) {
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700757 *sl_pte = 0;
758
Steve Mucklef132c6c2012-06-06 18:30:57 -0700759 clean_pte(sl_pte, sl_pte + 1, priv->redirect);
760 }
761
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700762 if (len == SZ_4K || len == SZ_64K) {
763 int used = 0;
764
765 for (i = 0; i < NUM_SL_PTE; i++)
766 if (sl_table[i])
767 used = 1;
768 if (!used) {
769 free_page((unsigned long)sl_table);
770 *fl_pte = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700771
772 clean_pte(fl_pte, fl_pte + 1, priv->redirect);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700773 }
774 }
775
Steve Mucklef132c6c2012-06-06 18:30:57 -0700776 ret = __flush_iotlb_va(domain, va);
Ohad Ben-Cohen9e285472011-09-02 13:32:34 -0400777
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700778fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700779 mutex_unlock(&msm_iommu_lock);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +0200780
781 /* the IOMMU API requires us to return how many bytes were unmapped */
782 len = ret ? 0 : len;
783 return len;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700784}
785
Steve Mucklef132c6c2012-06-06 18:30:57 -0700786static unsigned int get_phys_addr(struct scatterlist *sg)
787{
788 /*
789 * Try sg_dma_address first so that we can
790 * map carveout regions that do not have a
791 * struct page associated with them.
792 */
793 unsigned int pa = sg_dma_address(sg);
794 if (pa == 0)
795 pa = sg_phys(sg);
796 return pa;
797}
798
799static int msm_iommu_map_range(struct iommu_domain *domain, unsigned int va,
800 struct scatterlist *sg, unsigned int len,
801 int prot)
802{
803 unsigned int pa;
804 unsigned int offset = 0;
805 unsigned int pgprot;
806 unsigned long *fl_table;
807 unsigned long *fl_pte;
808 unsigned long fl_offset;
809 unsigned long *sl_table;
810 unsigned long sl_offset, sl_start;
811 unsigned int chunk_offset = 0;
812 unsigned int chunk_pa;
813 int ret = 0;
814 struct msm_priv *priv;
815
816 mutex_lock(&msm_iommu_lock);
817
818 BUG_ON(len & (SZ_4K - 1));
819
820 priv = domain->priv;
821 fl_table = priv->pgtable;
822
823 pgprot = __get_pgprot(prot, SZ_4K);
824
825 if (!pgprot) {
826 ret = -EINVAL;
827 goto fail;
828 }
829
830 fl_offset = FL_OFFSET(va); /* Upper 12 bits */
831 fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */
832
833 sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK));
834 sl_offset = SL_OFFSET(va);
835
836 chunk_pa = get_phys_addr(sg);
837 if (chunk_pa == 0) {
838 pr_debug("No dma address for sg %p\n", sg);
839 ret = -EINVAL;
840 goto fail;
841 }
842
843 while (offset < len) {
844 /* Set up a 2nd level page table if one doesn't exist */
845 if (*fl_pte == 0) {
846 sl_table = (unsigned long *)
847 __get_free_pages(GFP_KERNEL, get_order(SZ_4K));
848
849 if (!sl_table) {
850 pr_debug("Could not allocate second level table\n");
851 ret = -ENOMEM;
852 goto fail;
853 }
854
855 memset(sl_table, 0, SZ_4K);
856 clean_pte(sl_table, sl_table + NUM_SL_PTE,
857 priv->redirect);
858
859 *fl_pte = ((((int)__pa(sl_table)) & FL_BASE_MASK) |
860 FL_TYPE_TABLE);
861 clean_pte(fl_pte, fl_pte + 1, priv->redirect);
862 } else
863 sl_table = (unsigned long *)
864 __va(((*fl_pte) & FL_BASE_MASK));
865
866 /* Keep track of initial position so we
867 * don't clean more than we have to
868 */
869 sl_start = sl_offset;
870
871 /* Build the 2nd level page table */
872 while (offset < len && sl_offset < NUM_SL_PTE) {
873 pa = chunk_pa + chunk_offset;
874 sl_table[sl_offset] = (pa & SL_BASE_MASK_SMALL) |
875 pgprot | SL_NG | SL_SHARED | SL_TYPE_SMALL;
876 sl_offset++;
877 offset += SZ_4K;
878
879 chunk_offset += SZ_4K;
880
881 if (chunk_offset >= sg->length && offset < len) {
882 chunk_offset = 0;
883 sg = sg_next(sg);
884 chunk_pa = get_phys_addr(sg);
885 if (chunk_pa == 0) {
886 pr_debug("No dma address for sg %p\n",
887 sg);
888 ret = -EINVAL;
889 goto fail;
890 }
891 }
892 }
893
894 clean_pte(sl_table + sl_start, sl_table + sl_offset,
895 priv->redirect);
896
897 fl_pte++;
898 sl_offset = 0;
899 }
900 __flush_iotlb(domain);
901fail:
902 mutex_unlock(&msm_iommu_lock);
903 return ret;
904}
905
906
907static int msm_iommu_unmap_range(struct iommu_domain *domain, unsigned int va,
908 unsigned int len)
909{
910 unsigned int offset = 0;
911 unsigned long *fl_table;
912 unsigned long *fl_pte;
913 unsigned long fl_offset;
914 unsigned long *sl_table;
915 unsigned long sl_start, sl_end;
916 int used, i;
917 struct msm_priv *priv;
918
919 mutex_lock(&msm_iommu_lock);
920
921 BUG_ON(len & (SZ_4K - 1));
922
923 priv = domain->priv;
924 fl_table = priv->pgtable;
925
926 fl_offset = FL_OFFSET(va); /* Upper 12 bits */
927 fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */
928
929 sl_start = SL_OFFSET(va);
930
931 while (offset < len) {
932 sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK));
933 sl_end = ((len - offset) / SZ_4K) + sl_start;
934
935 if (sl_end > NUM_SL_PTE)
936 sl_end = NUM_SL_PTE;
937
938 memset(sl_table + sl_start, 0, (sl_end - sl_start) * 4);
939 clean_pte(sl_table + sl_start, sl_table + sl_end,
940 priv->redirect);
941
942 offset += (sl_end - sl_start) * SZ_4K;
943
944 /* Unmap and free the 2nd level table if all mappings in it
945 * were removed. This saves memory, but the table will need
946 * to be re-allocated the next time someone tries to map these
947 * VAs.
948 */
949 used = 0;
950
951 /* If we just unmapped the whole table, don't bother
952 * seeing if there are still used entries left.
953 */
954 if (sl_end - sl_start != NUM_SL_PTE)
955 for (i = 0; i < NUM_SL_PTE; i++)
956 if (sl_table[i]) {
957 used = 1;
958 break;
959 }
960 if (!used) {
961 free_page((unsigned long)sl_table);
962 *fl_pte = 0;
963
964 clean_pte(fl_pte, fl_pte + 1, priv->redirect);
965 }
966
967 sl_start = 0;
968 fl_pte++;
969 }
970
971 __flush_iotlb(domain);
972 mutex_unlock(&msm_iommu_lock);
973 return 0;
974}
975
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700976static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain,
977 unsigned long va)
978{
979 struct msm_priv *priv;
980 struct msm_iommu_drvdata *iommu_drvdata;
981 struct msm_iommu_ctx_drvdata *ctx_drvdata;
982 unsigned int par;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700983 void __iomem *base;
984 phys_addr_t ret = 0;
985 int ctx;
986
Steve Mucklef132c6c2012-06-06 18:30:57 -0700987 mutex_lock(&msm_iommu_lock);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700988
989 priv = domain->priv;
990 if (list_empty(&priv->list_attached))
991 goto fail;
992
993 ctx_drvdata = list_entry(priv->list_attached.next,
994 struct msm_iommu_ctx_drvdata, attached_elm);
995 iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
996
997 base = iommu_drvdata->base;
998 ctx = ctx_drvdata->num;
999
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -08001000 ret = __enable_clocks(iommu_drvdata);
1001 if (ret)
1002 goto fail;
1003
Olav Haugan5622d1c2012-11-07 15:02:56 -08001004 msm_iommu_remote_spin_lock();
1005
Stepan Moskovchenkob0e78082011-02-28 16:04:55 -08001006 SET_V2PPR(base, ctx, va & V2Pxx_VA);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001007
Steve Mucklef132c6c2012-06-06 18:30:57 -07001008 mb();
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001009 par = GET_PAR(base, ctx);
1010
1011 /* We are dealing with a supersection */
1012 if (GET_NOFAULT_SS(base, ctx))
1013 ret = (par & 0xFF000000) | (va & 0x00FFFFFF);
1014 else /* Upper 20 bits from PAR, lower 12 from VA */
1015 ret = (par & 0xFFFFF000) | (va & 0x00000FFF);
1016
Stepan Moskovchenko33069732010-11-12 19:30:00 -08001017 if (GET_FAULT(base, ctx))
1018 ret = 0;
1019
Olav Haugan5622d1c2012-11-07 15:02:56 -08001020 msm_iommu_remote_spin_unlock();
1021
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -08001022 __disable_clocks(iommu_drvdata);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001023fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001024 mutex_unlock(&msm_iommu_lock);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001025 return ret;
1026}
1027
1028static int msm_iommu_domain_has_cap(struct iommu_domain *domain,
1029 unsigned long cap)
1030{
1031 return 0;
1032}
1033
1034static void print_ctx_regs(void __iomem *base, int ctx)
1035{
1036 unsigned int fsr = GET_FSR(base, ctx);
1037 pr_err("FAR = %08x PAR = %08x\n",
1038 GET_FAR(base, ctx), GET_PAR(base, ctx));
1039 pr_err("FSR = %08x [%s%s%s%s%s%s%s%s%s%s]\n", fsr,
1040 (fsr & 0x02) ? "TF " : "",
1041 (fsr & 0x04) ? "AFF " : "",
1042 (fsr & 0x08) ? "APF " : "",
1043 (fsr & 0x10) ? "TLBMF " : "",
1044 (fsr & 0x20) ? "HTWDEEF " : "",
1045 (fsr & 0x40) ? "HTWSEEF " : "",
1046 (fsr & 0x80) ? "MHF " : "",
1047 (fsr & 0x10000) ? "SL " : "",
1048 (fsr & 0x40000000) ? "SS " : "",
1049 (fsr & 0x80000000) ? "MULTI " : "");
1050
1051 pr_err("FSYNR0 = %08x FSYNR1 = %08x\n",
1052 GET_FSYNR0(base, ctx), GET_FSYNR1(base, ctx));
1053 pr_err("TTBR0 = %08x TTBR1 = %08x\n",
1054 GET_TTBR0(base, ctx), GET_TTBR1(base, ctx));
1055 pr_err("SCTLR = %08x ACTLR = %08x\n",
1056 GET_SCTLR(base, ctx), GET_ACTLR(base, ctx));
1057 pr_err("PRRR = %08x NMRR = %08x\n",
1058 GET_PRRR(base, ctx), GET_NMRR(base, ctx));
1059}
1060
1061irqreturn_t msm_iommu_fault_handler(int irq, void *dev_id)
1062{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001063 struct msm_iommu_ctx_drvdata *ctx_drvdata = dev_id;
1064 struct msm_iommu_drvdata *drvdata;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001065 void __iomem *base;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001066 unsigned int fsr, num;
1067 int ret;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001068
Steve Mucklef132c6c2012-06-06 18:30:57 -07001069 mutex_lock(&msm_iommu_lock);
1070 BUG_ON(!ctx_drvdata);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001071
Steve Mucklef132c6c2012-06-06 18:30:57 -07001072 drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
1073 BUG_ON(!drvdata);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001074
1075 base = drvdata->base;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001076 num = ctx_drvdata->num;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001077
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -08001078 ret = __enable_clocks(drvdata);
1079 if (ret)
1080 goto fail;
1081
Olav Haugan5622d1c2012-11-07 15:02:56 -08001082 msm_iommu_remote_spin_lock();
1083
Steve Mucklef132c6c2012-06-06 18:30:57 -07001084 fsr = GET_FSR(base, num);
1085
1086 if (fsr) {
1087 if (!ctx_drvdata->attached_domain) {
1088 pr_err("Bad domain in interrupt handler\n");
1089 ret = -ENOSYS;
1090 } else
1091 ret = report_iommu_fault(ctx_drvdata->attached_domain,
1092 &ctx_drvdata->pdev->dev,
1093 GET_FAR(base, num), 0);
1094
1095 if (ret == -ENOSYS) {
1096 pr_err("Unexpected IOMMU page fault!\n");
1097 pr_err("name = %s\n", drvdata->name);
1098 pr_err("context = %s (%d)\n", ctx_drvdata->name, num);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001099 pr_err("Interesting registers:\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07001100 print_ctx_regs(base, num);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001101 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07001102
1103 SET_FSR(base, num, fsr);
1104 SET_RESUME(base, num, 1);
1105
1106 ret = IRQ_HANDLED;
1107 } else
1108 ret = IRQ_NONE;
1109
Olav Haugan5622d1c2012-11-07 15:02:56 -08001110 msm_iommu_remote_spin_unlock();
1111
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -08001112 __disable_clocks(drvdata);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001113fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001114 mutex_unlock(&msm_iommu_lock);
1115 return ret;
1116}
1117
1118static phys_addr_t msm_iommu_get_pt_base_addr(struct iommu_domain *domain)
1119{
1120 struct msm_priv *priv = domain->priv;
1121 return __pa(priv->pgtable);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001122}
1123
1124static struct iommu_ops msm_iommu_ops = {
1125 .domain_init = msm_iommu_domain_init,
1126 .domain_destroy = msm_iommu_domain_destroy,
1127 .attach_dev = msm_iommu_attach_dev,
1128 .detach_dev = msm_iommu_detach_dev,
1129 .map = msm_iommu_map,
1130 .unmap = msm_iommu_unmap,
Steve Mucklef132c6c2012-06-06 18:30:57 -07001131 .map_range = msm_iommu_map_range,
1132 .unmap_range = msm_iommu_unmap_range,
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001133 .iova_to_phys = msm_iommu_iova_to_phys,
Ohad Ben-Cohen83427272011-11-10 11:32:28 +02001134 .domain_has_cap = msm_iommu_domain_has_cap,
Steve Mucklef132c6c2012-06-06 18:30:57 -07001135 .get_pt_base_addr = msm_iommu_get_pt_base_addr,
Ohad Ben-Cohen83427272011-11-10 11:32:28 +02001136 .pgsize_bitmap = MSM_IOMMU_PGSIZES,
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001137};
1138
Stepan Moskovchenko100832c2010-11-15 18:20:08 -08001139static int __init get_tex_class(int icp, int ocp, int mt, int nos)
1140{
1141 int i = 0;
1142 unsigned int prrr = 0;
1143 unsigned int nmrr = 0;
1144 int c_icp, c_ocp, c_mt, c_nos;
1145
1146 RCP15_PRRR(prrr);
1147 RCP15_NMRR(nmrr);
1148
1149 for (i = 0; i < NUM_TEX_CLASS; i++) {
1150 c_nos = PRRR_NOS(prrr, i);
1151 c_mt = PRRR_MT(prrr, i);
1152 c_icp = NMRR_ICP(nmrr, i);
1153 c_ocp = NMRR_OCP(nmrr, i);
1154
1155 if (icp == c_icp && ocp == c_ocp && c_mt == mt && c_nos == nos)
1156 return i;
1157 }
1158
1159 return -ENODEV;
1160}
1161
1162static void __init setup_iommu_tex_classes(void)
1163{
1164 msm_iommu_tex_class[MSM_IOMMU_ATTR_NONCACHED] =
1165 get_tex_class(CP_NONCACHED, CP_NONCACHED, MT_NORMAL, 1);
1166
1167 msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WB_WA] =
1168 get_tex_class(CP_WB_WA, CP_WB_WA, MT_NORMAL, 1);
1169
1170 msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WB_NWA] =
1171 get_tex_class(CP_WB_NWA, CP_WB_NWA, MT_NORMAL, 1);
1172
1173 msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WT] =
1174 get_tex_class(CP_WT, CP_WT, MT_NORMAL, 1);
1175}
1176
Stepan Moskovchenko516cbc72010-11-12 19:29:53 -08001177static int __init msm_iommu_init(void)
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001178{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001179 if (!msm_soc_version_supports_iommu_v1())
1180 return -ENODEV;
1181
Olav Haugan5622d1c2012-11-07 15:02:56 -08001182 msm_iommu_lock_initialize();
1183
Stepan Moskovchenko100832c2010-11-15 18:20:08 -08001184 setup_iommu_tex_classes();
Joerg Roedel85eebbc2011-09-06 17:56:07 +02001185 bus_set_iommu(&platform_bus_type, &msm_iommu_ops);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -07001186 return 0;
1187}
1188
1189subsys_initcall(msm_iommu_init);
1190
1191MODULE_LICENSE("GPL v2");
1192MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");