blob: 3afdf43f732ac0df57e0893d37a71f1c2ca6d201 [file] [log] [blame]
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001/*
2 * IOMMU API for SMMU in Tegra30
3 *
Sami Liedes0fde6712013-02-04 14:31:48 +02004 * Copyright (c) 2011-2013, NVIDIA CORPORATION. All rights reserved.
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#define pr_fmt(fmt) "%s(): " fmt, __func__
21
Thierry Redingbc5e6de2013-01-21 11:09:06 +010022#include <linux/err.h>
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020023#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/spinlock.h>
26#include <linux/slab.h>
27#include <linux/vmalloc.h>
28#include <linux/mm.h>
29#include <linux/pagemap.h>
30#include <linux/device.h>
31#include <linux/sched.h>
32#include <linux/iommu.h>
33#include <linux/io.h>
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +030034#include <linux/of.h>
35#include <linux/of_iommu.h>
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +030036#include <linux/debugfs.h>
37#include <linux/seq_file.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020038
39#include <soc/tegra/ahb.h>
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020040
41#include <asm/page.h>
42#include <asm/cacheflush.h>
43
Stephen Warrene6bc5932012-09-04 16:36:15 -060044enum smmu_hwgrp {
45 HWGRP_AFI,
46 HWGRP_AVPC,
47 HWGRP_DC,
48 HWGRP_DCB,
49 HWGRP_EPP,
50 HWGRP_G2,
51 HWGRP_HC,
52 HWGRP_HDA,
53 HWGRP_ISP,
54 HWGRP_MPE,
55 HWGRP_NV,
56 HWGRP_NV2,
57 HWGRP_PPCS,
58 HWGRP_SATA,
59 HWGRP_VDE,
60 HWGRP_VI,
61
62 HWGRP_COUNT,
63
64 HWGRP_END = ~0,
65};
66
67#define HWG_AFI (1 << HWGRP_AFI)
68#define HWG_AVPC (1 << HWGRP_AVPC)
69#define HWG_DC (1 << HWGRP_DC)
70#define HWG_DCB (1 << HWGRP_DCB)
71#define HWG_EPP (1 << HWGRP_EPP)
72#define HWG_G2 (1 << HWGRP_G2)
73#define HWG_HC (1 << HWGRP_HC)
74#define HWG_HDA (1 << HWGRP_HDA)
75#define HWG_ISP (1 << HWGRP_ISP)
76#define HWG_MPE (1 << HWGRP_MPE)
77#define HWG_NV (1 << HWGRP_NV)
78#define HWG_NV2 (1 << HWGRP_NV2)
79#define HWG_PPCS (1 << HWGRP_PPCS)
80#define HWG_SATA (1 << HWGRP_SATA)
81#define HWG_VDE (1 << HWGRP_VDE)
82#define HWG_VI (1 << HWGRP_VI)
83
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020084/* bitmap of the page sizes currently supported */
85#define SMMU_IOMMU_PGSIZES (SZ_4K)
86
87#define SMMU_CONFIG 0x10
88#define SMMU_CONFIG_DISABLE 0
89#define SMMU_CONFIG_ENABLE 1
90
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +030091/* REVISIT: To support multiple MCs */
92enum {
93 _MC = 0,
94};
95
96enum {
97 _TLB = 0,
98 _PTC,
99};
100
101#define SMMU_CACHE_CONFIG_BASE 0x14
102#define __SMMU_CACHE_CONFIG(mc, cache) (SMMU_CACHE_CONFIG_BASE + 4 * cache)
103#define SMMU_CACHE_CONFIG(cache) __SMMU_CACHE_CONFIG(_MC, cache)
104
105#define SMMU_CACHE_CONFIG_STATS_SHIFT 31
106#define SMMU_CACHE_CONFIG_STATS_ENABLE (1 << SMMU_CACHE_CONFIG_STATS_SHIFT)
107#define SMMU_CACHE_CONFIG_STATS_TEST_SHIFT 30
108#define SMMU_CACHE_CONFIG_STATS_TEST (1 << SMMU_CACHE_CONFIG_STATS_TEST_SHIFT)
109
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200110#define SMMU_TLB_CONFIG_HIT_UNDER_MISS__ENABLE (1 << 29)
111#define SMMU_TLB_CONFIG_ACTIVE_LINES__VALUE 0x10
112#define SMMU_TLB_CONFIG_RESET_VAL 0x20000010
113
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200114#define SMMU_PTC_CONFIG_CACHE__ENABLE (1 << 29)
115#define SMMU_PTC_CONFIG_INDEX_MAP__PATTERN 0x3f
116#define SMMU_PTC_CONFIG_RESET_VAL 0x2000003f
117
118#define SMMU_PTB_ASID 0x1c
119#define SMMU_PTB_ASID_CURRENT_SHIFT 0
120
121#define SMMU_PTB_DATA 0x20
122#define SMMU_PTB_DATA_RESET_VAL 0
123#define SMMU_PTB_DATA_ASID_NONSECURE_SHIFT 29
124#define SMMU_PTB_DATA_ASID_WRITABLE_SHIFT 30
125#define SMMU_PTB_DATA_ASID_READABLE_SHIFT 31
126
127#define SMMU_TLB_FLUSH 0x30
128#define SMMU_TLB_FLUSH_VA_MATCH_ALL 0
129#define SMMU_TLB_FLUSH_VA_MATCH_SECTION 2
130#define SMMU_TLB_FLUSH_VA_MATCH_GROUP 3
131#define SMMU_TLB_FLUSH_ASID_SHIFT 29
132#define SMMU_TLB_FLUSH_ASID_MATCH_DISABLE 0
133#define SMMU_TLB_FLUSH_ASID_MATCH_ENABLE 1
134#define SMMU_TLB_FLUSH_ASID_MATCH_SHIFT 31
135
136#define SMMU_PTC_FLUSH 0x34
137#define SMMU_PTC_FLUSH_TYPE_ALL 0
138#define SMMU_PTC_FLUSH_TYPE_ADR 1
139#define SMMU_PTC_FLUSH_ADR_SHIFT 4
140
141#define SMMU_ASID_SECURITY 0x38
142
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300143#define SMMU_STATS_CACHE_COUNT_BASE 0x1f0
144
145#define SMMU_STATS_CACHE_COUNT(mc, cache, hitmiss) \
146 (SMMU_STATS_CACHE_COUNT_BASE + 8 * cache + 4 * hitmiss)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200147
148#define SMMU_TRANSLATION_ENABLE_0 0x228
149#define SMMU_TRANSLATION_ENABLE_1 0x22c
150#define SMMU_TRANSLATION_ENABLE_2 0x230
151
152#define SMMU_AFI_ASID 0x238 /* PCIE */
153#define SMMU_AVPC_ASID 0x23c /* AVP */
154#define SMMU_DC_ASID 0x240 /* Display controller */
155#define SMMU_DCB_ASID 0x244 /* Display controller B */
156#define SMMU_EPP_ASID 0x248 /* Encoder pre-processor */
157#define SMMU_G2_ASID 0x24c /* 2D engine */
158#define SMMU_HC_ASID 0x250 /* Host1x */
159#define SMMU_HDA_ASID 0x254 /* High-def audio */
160#define SMMU_ISP_ASID 0x258 /* Image signal processor */
161#define SMMU_MPE_ASID 0x264 /* MPEG encoder */
162#define SMMU_NV_ASID 0x268 /* (3D) */
163#define SMMU_NV2_ASID 0x26c /* (3D) */
164#define SMMU_PPCS_ASID 0x270 /* AHB */
165#define SMMU_SATA_ASID 0x278 /* SATA */
166#define SMMU_VDE_ASID 0x27c /* Video decoder */
167#define SMMU_VI_ASID 0x280 /* Video input */
168
169#define SMMU_PDE_NEXT_SHIFT 28
170
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200171#define SMMU_TLB_FLUSH_VA_SECTION__MASK 0xffc00000
172#define SMMU_TLB_FLUSH_VA_SECTION__SHIFT 12 /* right shift */
173#define SMMU_TLB_FLUSH_VA_GROUP__MASK 0xffffc000
174#define SMMU_TLB_FLUSH_VA_GROUP__SHIFT 12 /* right shift */
175#define SMMU_TLB_FLUSH_VA(iova, which) \
176 ((((iova) & SMMU_TLB_FLUSH_VA_##which##__MASK) >> \
177 SMMU_TLB_FLUSH_VA_##which##__SHIFT) | \
178 SMMU_TLB_FLUSH_VA_MATCH_##which)
179#define SMMU_PTB_ASID_CUR(n) \
180 ((n) << SMMU_PTB_ASID_CURRENT_SHIFT)
181#define SMMU_TLB_FLUSH_ASID_MATCH_disable \
182 (SMMU_TLB_FLUSH_ASID_MATCH_DISABLE << \
183 SMMU_TLB_FLUSH_ASID_MATCH_SHIFT)
184#define SMMU_TLB_FLUSH_ASID_MATCH__ENABLE \
185 (SMMU_TLB_FLUSH_ASID_MATCH_ENABLE << \
186 SMMU_TLB_FLUSH_ASID_MATCH_SHIFT)
187
188#define SMMU_PAGE_SHIFT 12
189#define SMMU_PAGE_SIZE (1 << SMMU_PAGE_SHIFT)
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300190#define SMMU_PAGE_MASK ((1 << SMMU_PAGE_SHIFT) - 1)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200191
192#define SMMU_PDIR_COUNT 1024
193#define SMMU_PDIR_SIZE (sizeof(unsigned long) * SMMU_PDIR_COUNT)
194#define SMMU_PTBL_COUNT 1024
195#define SMMU_PTBL_SIZE (sizeof(unsigned long) * SMMU_PTBL_COUNT)
196#define SMMU_PDIR_SHIFT 12
197#define SMMU_PDE_SHIFT 12
198#define SMMU_PTE_SHIFT 12
199#define SMMU_PFN_MASK 0x000fffff
200
201#define SMMU_ADDR_TO_PFN(addr) ((addr) >> 12)
202#define SMMU_ADDR_TO_PDN(addr) ((addr) >> 22)
Hiro Sugawarad0078e72012-10-18 08:35:10 +0300203#define SMMU_PDN_TO_ADDR(pdn) ((pdn) << 22)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200204
205#define _READABLE (1 << SMMU_PTB_DATA_ASID_READABLE_SHIFT)
206#define _WRITABLE (1 << SMMU_PTB_DATA_ASID_WRITABLE_SHIFT)
207#define _NONSECURE (1 << SMMU_PTB_DATA_ASID_NONSECURE_SHIFT)
208#define _PDE_NEXT (1 << SMMU_PDE_NEXT_SHIFT)
209#define _MASK_ATTR (_READABLE | _WRITABLE | _NONSECURE)
210
211#define _PDIR_ATTR (_READABLE | _WRITABLE | _NONSECURE)
212
213#define _PDE_ATTR (_READABLE | _WRITABLE | _NONSECURE)
214#define _PDE_ATTR_N (_PDE_ATTR | _PDE_NEXT)
215#define _PDE_VACANT(pdn) (((pdn) << 10) | _PDE_ATTR)
216
217#define _PTE_ATTR (_READABLE | _WRITABLE | _NONSECURE)
218#define _PTE_VACANT(addr) (((addr) >> SMMU_PAGE_SHIFT) | _PTE_ATTR)
219
220#define SMMU_MK_PDIR(page, attr) \
221 ((page_to_phys(page) >> SMMU_PDIR_SHIFT) | (attr))
222#define SMMU_MK_PDE(page, attr) \
223 (unsigned long)((page_to_phys(page) >> SMMU_PDE_SHIFT) | (attr))
224#define SMMU_EX_PTBL_PAGE(pde) \
225 pfn_to_page((unsigned long)(pde) & SMMU_PFN_MASK)
226#define SMMU_PFN_TO_PTE(pfn, attr) (unsigned long)((pfn) | (attr))
227
228#define SMMU_ASID_ENABLE(asid) ((asid) | (1 << 31))
229#define SMMU_ASID_DISABLE 0
230#define SMMU_ASID_ASID(n) ((n) & ~SMMU_ASID_ENABLE(0))
231
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300232#define NUM_SMMU_REG_BANKS 3
233
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200234#define smmu_client_enable_hwgrp(c, m) smmu_client_set_hwgrp(c, m, 1)
235#define smmu_client_disable_hwgrp(c) smmu_client_set_hwgrp(c, 0, 0)
236#define __smmu_client_enable_hwgrp(c, m) __smmu_client_set_hwgrp(c, m, 1)
237#define __smmu_client_disable_hwgrp(c) __smmu_client_set_hwgrp(c, 0, 0)
238
239#define HWGRP_INIT(client) [HWGRP_##client] = SMMU_##client##_ASID
240
241static const u32 smmu_hwgrp_asid_reg[] = {
242 HWGRP_INIT(AFI),
243 HWGRP_INIT(AVPC),
244 HWGRP_INIT(DC),
245 HWGRP_INIT(DCB),
246 HWGRP_INIT(EPP),
247 HWGRP_INIT(G2),
248 HWGRP_INIT(HC),
249 HWGRP_INIT(HDA),
250 HWGRP_INIT(ISP),
251 HWGRP_INIT(MPE),
252 HWGRP_INIT(NV),
253 HWGRP_INIT(NV2),
254 HWGRP_INIT(PPCS),
255 HWGRP_INIT(SATA),
256 HWGRP_INIT(VDE),
257 HWGRP_INIT(VI),
258};
259#define HWGRP_ASID_REG(x) (smmu_hwgrp_asid_reg[x])
260
261/*
262 * Per client for address space
263 */
264struct smmu_client {
265 struct device *dev;
266 struct list_head list;
267 struct smmu_as *as;
268 u32 hwgrp;
269};
270
271/*
272 * Per address space
273 */
274struct smmu_as {
275 struct smmu_device *smmu; /* back pointer to container */
276 unsigned int asid;
277 spinlock_t lock; /* for pagetable */
278 struct page *pdir_page;
279 unsigned long pdir_attr;
280 unsigned long pde_attr;
281 unsigned long pte_attr;
282 unsigned int *pte_count;
283
284 struct list_head client;
285 spinlock_t client_lock; /* for client list */
286};
287
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -0600288struct smmu_debugfs_info {
289 struct smmu_device *smmu;
290 int mc;
291 int cache;
292};
293
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200294/*
295 * Per SMMU device - IOMMU device
296 */
297struct smmu_device {
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200298 void __iomem *regbase; /* register offset base */
299 void __iomem **regs; /* register block start address array */
300 void __iomem **rege; /* register block end address array */
301 int nregs; /* number of register blocks */
302
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200303 unsigned long iovmm_base; /* remappable base address */
304 unsigned long page_count; /* total remappable size */
305 spinlock_t lock;
306 char *name;
307 struct device *dev;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200308 struct page *avp_vector_page; /* dummy page shared by all AS's */
309
310 /*
311 * Register image savers for suspend/resume
312 */
313 unsigned long translation_enable_0;
314 unsigned long translation_enable_1;
315 unsigned long translation_enable_2;
316 unsigned long asid_security;
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300317
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300318 struct dentry *debugfs_root;
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -0600319 struct smmu_debugfs_info *debugfs_info;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300320
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300321 struct device_node *ahb;
Hiroshi Doyua3b24912012-06-25 14:23:56 +0300322
323 int num_as;
324 struct smmu_as as[0]; /* Run-time allocated array */
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200325};
326
327static struct smmu_device *smmu_handle; /* unique for a system */
328
329/*
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300330 * SMMU register accessors
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200331 */
Joerg Roedelfe1229b2013-02-04 20:40:58 +0100332static bool inline smmu_valid_reg(struct smmu_device *smmu,
333 void __iomem *addr)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200334{
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200335 int i;
336
337 for (i = 0; i < smmu->nregs; i++) {
Joerg Roedelfe1229b2013-02-04 20:40:58 +0100338 if (addr < smmu->regs[i])
339 break;
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200340 if (addr <= smmu->rege[i])
Joerg Roedelfe1229b2013-02-04 20:40:58 +0100341 return true;
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200342 }
343
Joerg Roedelfe1229b2013-02-04 20:40:58 +0100344 return false;
345}
346
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200347static inline u32 smmu_read(struct smmu_device *smmu, size_t offs)
348{
Joerg Roedelfe1229b2013-02-04 20:40:58 +0100349 void __iomem *addr = smmu->regbase + offs;
350
351 BUG_ON(!smmu_valid_reg(smmu, addr));
352
353 return readl(addr);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200354}
355
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300356static inline void smmu_write(struct smmu_device *smmu, u32 val, size_t offs)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200357{
Joerg Roedelfe1229b2013-02-04 20:40:58 +0100358 void __iomem *addr = smmu->regbase + offs;
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200359
Joerg Roedelfe1229b2013-02-04 20:40:58 +0100360 BUG_ON(!smmu_valid_reg(smmu, addr));
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200361
Joerg Roedelfe1229b2013-02-04 20:40:58 +0100362 writel(val, addr);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200363}
364
365#define VA_PAGE_TO_PA(va, page) \
366 (page_to_phys(page) + ((unsigned long)(va) & ~PAGE_MASK))
367
368#define FLUSH_CPU_DCACHE(va, page, size) \
369 do { \
370 unsigned long _pa_ = VA_PAGE_TO_PA(va, page); \
371 __cpuc_flush_dcache_area((void *)(va), (size_t)(size)); \
372 outer_flush_range(_pa_, _pa_+(size_t)(size)); \
373 } while (0)
374
375/*
376 * Any interaction between any block on PPSB and a block on APB or AHB
377 * must have these read-back barriers to ensure the APB/AHB bus
378 * transaction is complete before initiating activity on the PPSB
379 * block.
380 */
381#define FLUSH_SMMU_REGS(smmu) smmu_read(smmu, SMMU_CONFIG)
382
383#define smmu_client_hwgrp(c) (u32)((c)->dev->platform_data)
384
385static int __smmu_client_set_hwgrp(struct smmu_client *c,
386 unsigned long map, int on)
387{
388 int i;
389 struct smmu_as *as = c->as;
390 u32 val, offs, mask = SMMU_ASID_ENABLE(as->asid);
391 struct smmu_device *smmu = as->smmu;
392
393 WARN_ON(!on && map);
394 if (on && !map)
395 return -EINVAL;
396 if (!on)
397 map = smmu_client_hwgrp(c);
398
399 for_each_set_bit(i, &map, HWGRP_COUNT) {
400 offs = HWGRP_ASID_REG(i);
401 val = smmu_read(smmu, offs);
402 if (on) {
403 if (WARN_ON(val & mask))
404 goto err_hw_busy;
405 val |= mask;
406 } else {
407 WARN_ON((val & mask) == mask);
408 val &= ~mask;
409 }
410 smmu_write(smmu, val, offs);
411 }
412 FLUSH_SMMU_REGS(smmu);
413 c->hwgrp = map;
414 return 0;
415
416err_hw_busy:
417 for_each_set_bit(i, &map, HWGRP_COUNT) {
418 offs = HWGRP_ASID_REG(i);
419 val = smmu_read(smmu, offs);
420 val &= ~mask;
421 smmu_write(smmu, val, offs);
422 }
423 return -EBUSY;
424}
425
426static int smmu_client_set_hwgrp(struct smmu_client *c, u32 map, int on)
427{
428 u32 val;
429 unsigned long flags;
430 struct smmu_as *as = c->as;
431 struct smmu_device *smmu = as->smmu;
432
433 spin_lock_irqsave(&smmu->lock, flags);
434 val = __smmu_client_set_hwgrp(c, map, on);
435 spin_unlock_irqrestore(&smmu->lock, flags);
436 return val;
437}
438
439/*
440 * Flush all TLB entries and all PTC entries
441 * Caller must lock smmu
442 */
443static void smmu_flush_regs(struct smmu_device *smmu, int enable)
444{
445 u32 val;
446
447 smmu_write(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
448 FLUSH_SMMU_REGS(smmu);
449 val = SMMU_TLB_FLUSH_VA_MATCH_ALL |
450 SMMU_TLB_FLUSH_ASID_MATCH_disable;
451 smmu_write(smmu, val, SMMU_TLB_FLUSH);
452
453 if (enable)
454 smmu_write(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
455 FLUSH_SMMU_REGS(smmu);
456}
457
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300458static int smmu_setup_regs(struct smmu_device *smmu)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200459{
460 int i;
461 u32 val;
462
463 for (i = 0; i < smmu->num_as; i++) {
464 struct smmu_as *as = &smmu->as[i];
465 struct smmu_client *c;
466
467 smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
468 val = as->pdir_page ?
469 SMMU_MK_PDIR(as->pdir_page, as->pdir_attr) :
470 SMMU_PTB_DATA_RESET_VAL;
471 smmu_write(smmu, val, SMMU_PTB_DATA);
472
473 list_for_each_entry(c, &as->client, list)
474 __smmu_client_set_hwgrp(c, c->hwgrp, 1);
475 }
476
477 smmu_write(smmu, smmu->translation_enable_0, SMMU_TRANSLATION_ENABLE_0);
478 smmu_write(smmu, smmu->translation_enable_1, SMMU_TRANSLATION_ENABLE_1);
479 smmu_write(smmu, smmu->translation_enable_2, SMMU_TRANSLATION_ENABLE_2);
480 smmu_write(smmu, smmu->asid_security, SMMU_ASID_SECURITY);
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300481 smmu_write(smmu, SMMU_TLB_CONFIG_RESET_VAL, SMMU_CACHE_CONFIG(_TLB));
482 smmu_write(smmu, SMMU_PTC_CONFIG_RESET_VAL, SMMU_CACHE_CONFIG(_PTC));
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200483
484 smmu_flush_regs(smmu, 1);
485
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300486 return tegra_ahb_enable_smmu(smmu->ahb);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200487}
488
489static void flush_ptc_and_tlb(struct smmu_device *smmu,
490 struct smmu_as *as, dma_addr_t iova,
491 unsigned long *pte, struct page *page, int is_pde)
492{
493 u32 val;
494 unsigned long tlb_flush_va = is_pde
495 ? SMMU_TLB_FLUSH_VA(iova, SECTION)
496 : SMMU_TLB_FLUSH_VA(iova, GROUP);
497
498 val = SMMU_PTC_FLUSH_TYPE_ADR | VA_PAGE_TO_PA(pte, page);
499 smmu_write(smmu, val, SMMU_PTC_FLUSH);
500 FLUSH_SMMU_REGS(smmu);
501 val = tlb_flush_va |
502 SMMU_TLB_FLUSH_ASID_MATCH__ENABLE |
503 (as->asid << SMMU_TLB_FLUSH_ASID_SHIFT);
504 smmu_write(smmu, val, SMMU_TLB_FLUSH);
505 FLUSH_SMMU_REGS(smmu);
506}
507
508static void free_ptbl(struct smmu_as *as, dma_addr_t iova)
509{
510 unsigned long pdn = SMMU_ADDR_TO_PDN(iova);
511 unsigned long *pdir = (unsigned long *)page_address(as->pdir_page);
512
513 if (pdir[pdn] != _PDE_VACANT(pdn)) {
514 dev_dbg(as->smmu->dev, "pdn: %lx\n", pdn);
515
516 ClearPageReserved(SMMU_EX_PTBL_PAGE(pdir[pdn]));
517 __free_page(SMMU_EX_PTBL_PAGE(pdir[pdn]));
518 pdir[pdn] = _PDE_VACANT(pdn);
519 FLUSH_CPU_DCACHE(&pdir[pdn], as->pdir_page, sizeof pdir[pdn]);
520 flush_ptc_and_tlb(as->smmu, as, iova, &pdir[pdn],
521 as->pdir_page, 1);
522 }
523}
524
525static void free_pdir(struct smmu_as *as)
526{
527 unsigned addr;
528 int count;
529 struct device *dev = as->smmu->dev;
530
531 if (!as->pdir_page)
532 return;
533
534 addr = as->smmu->iovmm_base;
535 count = as->smmu->page_count;
536 while (count-- > 0) {
537 free_ptbl(as, addr);
538 addr += SMMU_PAGE_SIZE * SMMU_PTBL_COUNT;
539 }
540 ClearPageReserved(as->pdir_page);
541 __free_page(as->pdir_page);
542 as->pdir_page = NULL;
543 devm_kfree(dev, as->pte_count);
544 as->pte_count = NULL;
545}
546
547/*
548 * Maps PTBL for given iova and returns the PTE address
549 * Caller must unmap the mapped PTBL returned in *ptbl_page_p
550 */
551static unsigned long *locate_pte(struct smmu_as *as,
552 dma_addr_t iova, bool allocate,
553 struct page **ptbl_page_p,
554 unsigned int **count)
555{
556 unsigned long ptn = SMMU_ADDR_TO_PFN(iova);
557 unsigned long pdn = SMMU_ADDR_TO_PDN(iova);
558 unsigned long *pdir = page_address(as->pdir_page);
559 unsigned long *ptbl;
560
561 if (pdir[pdn] != _PDE_VACANT(pdn)) {
562 /* Mapped entry table already exists */
563 *ptbl_page_p = SMMU_EX_PTBL_PAGE(pdir[pdn]);
564 ptbl = page_address(*ptbl_page_p);
565 } else if (!allocate) {
566 return NULL;
567 } else {
568 int pn;
569 unsigned long addr = SMMU_PDN_TO_ADDR(pdn);
570
571 /* Vacant - allocate a new page table */
572 dev_dbg(as->smmu->dev, "New PTBL pdn: %lx\n", pdn);
573
574 *ptbl_page_p = alloc_page(GFP_ATOMIC);
575 if (!*ptbl_page_p) {
576 dev_err(as->smmu->dev,
577 "failed to allocate smmu_device page table\n");
578 return NULL;
579 }
580 SetPageReserved(*ptbl_page_p);
581 ptbl = (unsigned long *)page_address(*ptbl_page_p);
582 for (pn = 0; pn < SMMU_PTBL_COUNT;
583 pn++, addr += SMMU_PAGE_SIZE) {
584 ptbl[pn] = _PTE_VACANT(addr);
585 }
586 FLUSH_CPU_DCACHE(ptbl, *ptbl_page_p, SMMU_PTBL_SIZE);
587 pdir[pdn] = SMMU_MK_PDE(*ptbl_page_p,
588 as->pde_attr | _PDE_NEXT);
589 FLUSH_CPU_DCACHE(&pdir[pdn], as->pdir_page, sizeof pdir[pdn]);
590 flush_ptc_and_tlb(as->smmu, as, iova, &pdir[pdn],
591 as->pdir_page, 1);
592 }
593 *count = &as->pte_count[pdn];
594
595 return &ptbl[ptn % SMMU_PTBL_COUNT];
596}
597
598#ifdef CONFIG_SMMU_SIG_DEBUG
599static void put_signature(struct smmu_as *as,
600 dma_addr_t iova, unsigned long pfn)
601{
602 struct page *page;
603 unsigned long *vaddr;
604
605 page = pfn_to_page(pfn);
606 vaddr = page_address(page);
607 if (!vaddr)
608 return;
609
610 vaddr[0] = iova;
611 vaddr[1] = pfn << PAGE_SHIFT;
612 FLUSH_CPU_DCACHE(vaddr, page, sizeof(vaddr[0]) * 2);
613}
614#else
615static inline void put_signature(struct smmu_as *as,
616 unsigned long addr, unsigned long pfn)
617{
618}
619#endif
620
621/*
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200622 * Caller must not hold as->lock
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200623 */
624static int alloc_pdir(struct smmu_as *as)
625{
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200626 unsigned long *pdir, flags;
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300627 int pdn, err = 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200628 u32 val;
629 struct smmu_device *smmu = as->smmu;
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300630 struct page *page;
631 unsigned int *cnt;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200632
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300633 /*
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200634 * do the allocation, then grab as->lock
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300635 */
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300636 cnt = devm_kzalloc(smmu->dev,
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200637 sizeof(cnt[0]) * SMMU_PDIR_COUNT,
638 GFP_KERNEL);
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300639 page = alloc_page(GFP_KERNEL | __GFP_DMA);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200640
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200641 spin_lock_irqsave(&as->lock, flags);
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300642
643 if (as->pdir_page) {
644 /* We raced, free the redundant */
645 err = -EAGAIN;
646 goto err_out;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200647 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300648
649 if (!page || !cnt) {
650 dev_err(smmu->dev, "failed to allocate at %s\n", __func__);
651 err = -ENOMEM;
652 goto err_out;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200653 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300654
655 as->pdir_page = page;
656 as->pte_count = cnt;
657
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200658 SetPageReserved(as->pdir_page);
659 pdir = page_address(as->pdir_page);
660
661 for (pdn = 0; pdn < SMMU_PDIR_COUNT; pdn++)
662 pdir[pdn] = _PDE_VACANT(pdn);
663 FLUSH_CPU_DCACHE(pdir, as->pdir_page, SMMU_PDIR_SIZE);
664 val = SMMU_PTC_FLUSH_TYPE_ADR | VA_PAGE_TO_PA(pdir, as->pdir_page);
665 smmu_write(smmu, val, SMMU_PTC_FLUSH);
666 FLUSH_SMMU_REGS(as->smmu);
667 val = SMMU_TLB_FLUSH_VA_MATCH_ALL |
668 SMMU_TLB_FLUSH_ASID_MATCH__ENABLE |
669 (as->asid << SMMU_TLB_FLUSH_ASID_SHIFT);
670 smmu_write(smmu, val, SMMU_TLB_FLUSH);
671 FLUSH_SMMU_REGS(as->smmu);
672
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200673 spin_unlock_irqrestore(&as->lock, flags);
674
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200675 return 0;
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300676
677err_out:
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200678 spin_unlock_irqrestore(&as->lock, flags);
679
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300680 devm_kfree(smmu->dev, cnt);
681 if (page)
682 __free_page(page);
683 return err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200684}
685
686static void __smmu_iommu_unmap(struct smmu_as *as, dma_addr_t iova)
687{
688 unsigned long *pte;
689 struct page *page;
690 unsigned int *count;
691
692 pte = locate_pte(as, iova, false, &page, &count);
693 if (WARN_ON(!pte))
694 return;
695
696 if (WARN_ON(*pte == _PTE_VACANT(iova)))
697 return;
698
699 *pte = _PTE_VACANT(iova);
700 FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
701 flush_ptc_and_tlb(as->smmu, as, iova, pte, page, 0);
Hiroshi Doyu37683e42012-11-28 15:52:53 +0200702 if (!--(*count))
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200703 free_ptbl(as, iova);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200704}
705
706static void __smmu_iommu_map_pfn(struct smmu_as *as, dma_addr_t iova,
707 unsigned long pfn)
708{
709 struct smmu_device *smmu = as->smmu;
710 unsigned long *pte;
711 unsigned int *count;
712 struct page *page;
713
714 pte = locate_pte(as, iova, true, &page, &count);
715 if (WARN_ON(!pte))
716 return;
717
718 if (*pte == _PTE_VACANT(iova))
719 (*count)++;
720 *pte = SMMU_PFN_TO_PTE(pfn, as->pte_attr);
721 if (unlikely((*pte == _PTE_VACANT(iova))))
722 (*count)--;
723 FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
724 flush_ptc_and_tlb(smmu, as, iova, pte, page, 0);
725 put_signature(as, iova, pfn);
726}
727
728static int smmu_iommu_map(struct iommu_domain *domain, unsigned long iova,
729 phys_addr_t pa, size_t bytes, int prot)
730{
731 struct smmu_as *as = domain->priv;
732 unsigned long pfn = __phys_to_pfn(pa);
733 unsigned long flags;
734
Thierry Redinge56b3da2013-09-17 10:19:31 +0200735 dev_dbg(as->smmu->dev, "[%d] %08lx:%pa\n", as->asid, iova, &pa);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200736
737 if (!pfn_valid(pfn))
738 return -ENOMEM;
739
740 spin_lock_irqsave(&as->lock, flags);
741 __smmu_iommu_map_pfn(as, iova, pfn);
742 spin_unlock_irqrestore(&as->lock, flags);
743 return 0;
744}
745
746static size_t smmu_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
747 size_t bytes)
748{
749 struct smmu_as *as = domain->priv;
750 unsigned long flags;
751
752 dev_dbg(as->smmu->dev, "[%d] %08lx\n", as->asid, iova);
753
754 spin_lock_irqsave(&as->lock, flags);
755 __smmu_iommu_unmap(as, iova);
756 spin_unlock_irqrestore(&as->lock, flags);
757 return SMMU_PAGE_SIZE;
758}
759
760static phys_addr_t smmu_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547ac2013-03-29 01:23:58 +0530761 dma_addr_t iova)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200762{
763 struct smmu_as *as = domain->priv;
764 unsigned long *pte;
765 unsigned int *count;
766 struct page *page;
767 unsigned long pfn;
768 unsigned long flags;
769
770 spin_lock_irqsave(&as->lock, flags);
771
772 pte = locate_pte(as, iova, true, &page, &count);
773 pfn = *pte & SMMU_PFN_MASK;
774 WARN_ON(!pfn_valid(pfn));
775 dev_dbg(as->smmu->dev,
Varun Sethi72ca55d2013-04-11 09:19:44 +0530776 "iova:%08llx pfn:%08lx asid:%d\n", (unsigned long long)iova,
777 pfn, as->asid);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200778
779 spin_unlock_irqrestore(&as->lock, flags);
780 return PFN_PHYS(pfn);
781}
782
Joerg Roedel7c2aa642014-09-05 10:51:37 +0200783static bool smmu_iommu_capable(enum iommu_cap cap)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200784{
Joerg Roedel7c2aa642014-09-05 10:51:37 +0200785 return false;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200786}
787
788static int smmu_iommu_attach_dev(struct iommu_domain *domain,
789 struct device *dev)
790{
791 struct smmu_as *as = domain->priv;
792 struct smmu_device *smmu = as->smmu;
793 struct smmu_client *client, *c;
794 u32 map;
795 int err;
796
797 client = devm_kzalloc(smmu->dev, sizeof(*c), GFP_KERNEL);
798 if (!client)
799 return -ENOMEM;
800 client->dev = dev;
801 client->as = as;
802 map = (unsigned long)dev->platform_data;
803 if (!map)
804 return -EINVAL;
805
806 err = smmu_client_enable_hwgrp(client, map);
807 if (err)
808 goto err_hwgrp;
809
810 spin_lock(&as->client_lock);
811 list_for_each_entry(c, &as->client, list) {
812 if (c->dev == dev) {
813 dev_err(smmu->dev,
814 "%s is already attached\n", dev_name(c->dev));
815 err = -EINVAL;
816 goto err_client;
817 }
818 }
819 list_add(&client->list, &as->client);
820 spin_unlock(&as->client_lock);
821
822 /*
823 * Reserve "page zero" for AVP vectors using a common dummy
824 * page.
825 */
826 if (map & HWG_AVPC) {
827 struct page *page;
828
829 page = as->smmu->avp_vector_page;
830 __smmu_iommu_map_pfn(as, 0, page_to_pfn(page));
831
832 pr_info("Reserve \"page zero\" for AVP vectors using a common dummy\n");
833 }
834
Hiroshi DOYU90730912012-04-13 12:18:30 +0200835 dev_dbg(smmu->dev, "%s is attached\n", dev_name(dev));
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200836 return 0;
837
838err_client:
839 smmu_client_disable_hwgrp(client);
840 spin_unlock(&as->client_lock);
841err_hwgrp:
842 devm_kfree(smmu->dev, client);
843 return err;
844}
845
846static void smmu_iommu_detach_dev(struct iommu_domain *domain,
847 struct device *dev)
848{
849 struct smmu_as *as = domain->priv;
850 struct smmu_device *smmu = as->smmu;
851 struct smmu_client *c;
852
853 spin_lock(&as->client_lock);
854
855 list_for_each_entry(c, &as->client, list) {
856 if (c->dev == dev) {
857 smmu_client_disable_hwgrp(c);
858 list_del(&c->list);
859 devm_kfree(smmu->dev, c);
860 c->as = NULL;
861 dev_dbg(smmu->dev,
862 "%s is detached\n", dev_name(c->dev));
863 goto out;
864 }
865 }
Julia Lawall9579a972012-07-08 13:37:42 +0200866 dev_err(smmu->dev, "Couldn't find %s\n", dev_name(dev));
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200867out:
868 spin_unlock(&as->client_lock);
869}
870
871static int smmu_iommu_domain_init(struct iommu_domain *domain)
872{
Hiroshi Doyud1d076f2012-07-30 08:39:19 +0300873 int i, err = -EAGAIN;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200874 unsigned long flags;
875 struct smmu_as *as;
876 struct smmu_device *smmu = smmu_handle;
877
878 /* Look for a free AS with lock held */
879 for (i = 0; i < smmu->num_as; i++) {
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300880 as = &smmu->as[i];
Hiroshi Doyud2453b22012-07-30 08:39:18 +0300881
882 if (as->pdir_page)
883 continue;
884
885 err = alloc_pdir(as);
886 if (!err)
887 goto found;
888
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300889 if (err != -EAGAIN)
890 break;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200891 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300892 if (i == smmu->num_as)
893 dev_err(smmu->dev, "no free AS\n");
894 return err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200895
896found:
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200897 spin_lock_irqsave(&smmu->lock, flags);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200898
899 /* Update PDIR register */
900 smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
901 smmu_write(smmu,
902 SMMU_MK_PDIR(as->pdir_page, as->pdir_attr), SMMU_PTB_DATA);
903 FLUSH_SMMU_REGS(smmu);
904
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200905 spin_unlock_irqrestore(&smmu->lock, flags);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200906
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200907 domain->priv = as;
908
Hiroshi DOYU233499022012-01-26 19:40:57 +0100909 domain->geometry.aperture_start = smmu->iovmm_base;
910 domain->geometry.aperture_end = smmu->iovmm_base +
911 smmu->page_count * SMMU_PAGE_SIZE - 1;
912 domain->geometry.force_aperture = true;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200913
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200914 dev_dbg(smmu->dev, "smmu_as@%p\n", as);
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200915
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200916 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200917}
918
919static void smmu_iommu_domain_destroy(struct iommu_domain *domain)
920{
921 struct smmu_as *as = domain->priv;
922 struct smmu_device *smmu = as->smmu;
923 unsigned long flags;
924
925 spin_lock_irqsave(&as->lock, flags);
926
927 if (as->pdir_page) {
928 spin_lock(&smmu->lock);
929 smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
930 smmu_write(smmu, SMMU_PTB_DATA_RESET_VAL, SMMU_PTB_DATA);
931 FLUSH_SMMU_REGS(smmu);
932 spin_unlock(&smmu->lock);
933
934 free_pdir(as);
935 }
936
937 if (!list_empty(&as->client)) {
938 struct smmu_client *c;
939
940 list_for_each_entry(c, &as->client, list)
941 smmu_iommu_detach_dev(domain, c->dev);
942 }
943
944 spin_unlock_irqrestore(&as->lock, flags);
945
946 domain->priv = NULL;
947 dev_dbg(smmu->dev, "smmu_as@%p\n", as);
948}
949
Thierry Redingb22f6432014-06-27 09:03:12 +0200950static const struct iommu_ops smmu_iommu_ops = {
Joerg Roedel7c2aa642014-09-05 10:51:37 +0200951 .capable = smmu_iommu_capable,
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200952 .domain_init = smmu_iommu_domain_init,
953 .domain_destroy = smmu_iommu_domain_destroy,
954 .attach_dev = smmu_iommu_attach_dev,
955 .detach_dev = smmu_iommu_detach_dev,
956 .map = smmu_iommu_map,
957 .unmap = smmu_iommu_unmap,
958 .iova_to_phys = smmu_iommu_iova_to_phys,
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200959 .pgsize_bitmap = SMMU_IOMMU_PGSIZES,
960};
961
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300962/* Should be in the order of enum */
963static const char * const smmu_debugfs_mc[] = { "mc", };
964static const char * const smmu_debugfs_cache[] = { "tlb", "ptc", };
965
966static ssize_t smmu_debugfs_stats_write(struct file *file,
967 const char __user *buffer,
968 size_t count, loff_t *pos)
969{
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -0600970 struct smmu_debugfs_info *info;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300971 struct smmu_device *smmu;
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -0600972 int i;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300973 enum {
974 _OFF = 0,
975 _ON,
976 _RESET,
977 };
978 const char * const command[] = {
979 [_OFF] = "off",
980 [_ON] = "on",
981 [_RESET] = "reset",
982 };
983 char str[] = "reset";
984 u32 val;
985 size_t offs;
986
987 count = min_t(size_t, count, sizeof(str));
988 if (copy_from_user(str, buffer, count))
989 return -EINVAL;
990
991 for (i = 0; i < ARRAY_SIZE(command); i++)
992 if (strncmp(str, command[i],
993 strlen(command[i])) == 0)
994 break;
995
996 if (i == ARRAY_SIZE(command))
997 return -EINVAL;
998
Al Viro496ad9a2013-01-23 17:07:38 -0500999 info = file_inode(file)->i_private;
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001000 smmu = info->smmu;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001001
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001002 offs = SMMU_CACHE_CONFIG(info->cache);
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001003 val = smmu_read(smmu, offs);
1004 switch (i) {
1005 case _OFF:
1006 val &= ~SMMU_CACHE_CONFIG_STATS_ENABLE;
1007 val &= ~SMMU_CACHE_CONFIG_STATS_TEST;
1008 smmu_write(smmu, val, offs);
1009 break;
1010 case _ON:
1011 val |= SMMU_CACHE_CONFIG_STATS_ENABLE;
1012 val &= ~SMMU_CACHE_CONFIG_STATS_TEST;
1013 smmu_write(smmu, val, offs);
1014 break;
1015 case _RESET:
1016 val |= SMMU_CACHE_CONFIG_STATS_TEST;
1017 smmu_write(smmu, val, offs);
1018 val &= ~SMMU_CACHE_CONFIG_STATS_TEST;
1019 smmu_write(smmu, val, offs);
1020 break;
1021 default:
1022 BUG();
1023 break;
1024 }
1025
1026 dev_dbg(smmu->dev, "%s() %08x, %08x @%08x\n", __func__,
1027 val, smmu_read(smmu, offs), offs);
1028
1029 return count;
1030}
1031
1032static int smmu_debugfs_stats_show(struct seq_file *s, void *v)
1033{
Al Viro8add8622013-01-30 21:17:57 -05001034 struct smmu_debugfs_info *info = s->private;
1035 struct smmu_device *smmu = info->smmu;
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001036 int i;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001037 const char * const stats[] = { "hit", "miss", };
1038
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001039
1040 for (i = 0; i < ARRAY_SIZE(stats); i++) {
1041 u32 val;
1042 size_t offs;
1043
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001044 offs = SMMU_STATS_CACHE_COUNT(info->mc, info->cache, i);
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001045 val = smmu_read(smmu, offs);
1046 seq_printf(s, "%s:%08x ", stats[i], val);
1047
1048 dev_dbg(smmu->dev, "%s() %s %08x @%08x\n", __func__,
1049 stats[i], val, offs);
1050 }
1051 seq_printf(s, "\n");
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001052 return 0;
1053}
1054
1055static int smmu_debugfs_stats_open(struct inode *inode, struct file *file)
1056{
Al Viro8add8622013-01-30 21:17:57 -05001057 return single_open(file, smmu_debugfs_stats_show, inode->i_private);
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001058}
1059
1060static const struct file_operations smmu_debugfs_stats_fops = {
1061 .open = smmu_debugfs_stats_open,
1062 .read = seq_read,
1063 .llseek = seq_lseek,
1064 .release = single_release,
1065 .write = smmu_debugfs_stats_write,
1066};
1067
1068static void smmu_debugfs_delete(struct smmu_device *smmu)
1069{
1070 debugfs_remove_recursive(smmu->debugfs_root);
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001071 kfree(smmu->debugfs_info);
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001072}
1073
1074static void smmu_debugfs_create(struct smmu_device *smmu)
1075{
1076 int i;
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001077 size_t bytes;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001078 struct dentry *root;
1079
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001080 bytes = ARRAY_SIZE(smmu_debugfs_mc) * ARRAY_SIZE(smmu_debugfs_cache) *
1081 sizeof(*smmu->debugfs_info);
1082 smmu->debugfs_info = kmalloc(bytes, GFP_KERNEL);
1083 if (!smmu->debugfs_info)
1084 return;
1085
1086 root = debugfs_create_dir(dev_name(smmu->dev), NULL);
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001087 if (!root)
1088 goto err_out;
1089 smmu->debugfs_root = root;
1090
1091 for (i = 0; i < ARRAY_SIZE(smmu_debugfs_mc); i++) {
1092 int j;
1093 struct dentry *mc;
1094
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001095 mc = debugfs_create_dir(smmu_debugfs_mc[i], root);
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001096 if (!mc)
1097 goto err_out;
1098
1099 for (j = 0; j < ARRAY_SIZE(smmu_debugfs_cache); j++) {
1100 struct dentry *cache;
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001101 struct smmu_debugfs_info *info;
1102
1103 info = smmu->debugfs_info;
1104 info += i * ARRAY_SIZE(smmu_debugfs_mc) + j;
1105 info->smmu = smmu;
1106 info->mc = i;
1107 info->cache = j;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001108
1109 cache = debugfs_create_file(smmu_debugfs_cache[j],
1110 S_IWUGO | S_IRUGO, mc,
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001111 (void *)info,
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001112 &smmu_debugfs_stats_fops);
1113 if (!cache)
1114 goto err_out;
1115 }
1116 }
1117
1118 return;
1119
1120err_out:
1121 smmu_debugfs_delete(smmu);
1122}
1123
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001124static int tegra_smmu_suspend(struct device *dev)
1125{
1126 struct smmu_device *smmu = dev_get_drvdata(dev);
1127
1128 smmu->translation_enable_0 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_0);
1129 smmu->translation_enable_1 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_1);
1130 smmu->translation_enable_2 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_2);
1131 smmu->asid_security = smmu_read(smmu, SMMU_ASID_SECURITY);
1132 return 0;
1133}
1134
1135static int tegra_smmu_resume(struct device *dev)
1136{
1137 struct smmu_device *smmu = dev_get_drvdata(dev);
1138 unsigned long flags;
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001139 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001140
1141 spin_lock_irqsave(&smmu->lock, flags);
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001142 err = smmu_setup_regs(smmu);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001143 spin_unlock_irqrestore(&smmu->lock, flags);
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001144 return err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001145}
1146
1147static int tegra_smmu_probe(struct platform_device *pdev)
1148{
1149 struct smmu_device *smmu;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001150 struct device *dev = &pdev->dev;
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001151 int i, asids, err = 0;
Hiroshi Doyuff763622012-06-25 14:23:58 +03001152 dma_addr_t uninitialized_var(base);
1153 size_t bytes, uninitialized_var(size);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001154
1155 if (smmu_handle)
1156 return -EIO;
1157
1158 BUILD_BUG_ON(PAGE_SHIFT != SMMU_PAGE_SHIFT);
1159
Hiroshi Doyua3b24912012-06-25 14:23:56 +03001160 if (of_property_read_u32(dev->of_node, "nvidia,#asids", &asids))
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001161 return -ENODEV;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001162
Hiroshi Doyua3b24912012-06-25 14:23:56 +03001163 bytes = sizeof(*smmu) + asids * sizeof(*smmu->as);
1164 smmu = devm_kzalloc(dev, bytes, GFP_KERNEL);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001165 if (!smmu) {
1166 dev_err(dev, "failed to allocate smmu_device\n");
1167 return -ENOMEM;
1168 }
1169
Hiroshi Doyua6870e92013-01-31 10:14:10 +02001170 smmu->nregs = pdev->num_resources;
1171 smmu->regs = devm_kzalloc(dev, 2 * smmu->nregs * sizeof(*smmu->regs),
1172 GFP_KERNEL);
1173 smmu->rege = smmu->regs + smmu->nregs;
1174 if (!smmu->regs)
1175 return -ENOMEM;
1176 for (i = 0; i < smmu->nregs; i++) {
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001177 struct resource *res;
1178
1179 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
Thierry Redingbc5e6de2013-01-21 11:09:06 +01001180 smmu->regs[i] = devm_ioremap_resource(&pdev->dev, res);
1181 if (IS_ERR(smmu->regs[i]))
1182 return PTR_ERR(smmu->regs[i]);
Hiroshi Doyua6870e92013-01-31 10:14:10 +02001183 smmu->rege[i] = smmu->regs[i] + resource_size(res) - 1;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001184 }
Hiroshi Doyua6870e92013-01-31 10:14:10 +02001185 /* Same as "mc" 1st regiter block start address */
Hiroshi Doyua3b72562013-02-06 19:38:15 +02001186 smmu->regbase = (void __iomem *)((u32)smmu->regs[0] & PAGE_MASK);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001187
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001188 err = of_get_dma_window(dev->of_node, NULL, 0, NULL, &base, &size);
1189 if (err)
1190 return -ENODEV;
1191
1192 if (size & SMMU_PAGE_MASK)
1193 return -EINVAL;
1194
1195 size >>= SMMU_PAGE_SHIFT;
1196 if (!size)
1197 return -EINVAL;
1198
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001199 smmu->ahb = of_parse_phandle(dev->of_node, "nvidia,ahb", 0);
1200 if (!smmu->ahb)
1201 return -ENODEV;
1202
1203 smmu->dev = dev;
1204 smmu->num_as = asids;
1205 smmu->iovmm_base = base;
1206 smmu->page_count = size;
1207
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001208 smmu->translation_enable_0 = ~0;
1209 smmu->translation_enable_1 = ~0;
1210 smmu->translation_enable_2 = ~0;
1211 smmu->asid_security = 0;
1212
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001213 for (i = 0; i < smmu->num_as; i++) {
1214 struct smmu_as *as = &smmu->as[i];
1215
1216 as->smmu = smmu;
1217 as->asid = i;
1218 as->pdir_attr = _PDIR_ATTR;
1219 as->pde_attr = _PDE_ATTR;
1220 as->pte_attr = _PTE_ATTR;
1221
1222 spin_lock_init(&as->lock);
Sami Liedes0fde6712013-02-04 14:31:48 +02001223 spin_lock_init(&as->client_lock);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001224 INIT_LIST_HEAD(&as->client);
1225 }
1226 spin_lock_init(&smmu->lock);
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001227 err = smmu_setup_regs(smmu);
1228 if (err)
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +03001229 return err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001230 platform_set_drvdata(pdev, smmu);
1231
1232 smmu->avp_vector_page = alloc_page(GFP_KERNEL);
1233 if (!smmu->avp_vector_page)
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +03001234 return -ENOMEM;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001235
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001236 smmu_debugfs_create(smmu);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001237 smmu_handle = smmu;
Hiroshi Doyuf1bda292012-11-28 15:52:55 +02001238 bus_set_iommu(&platform_bus_type, &smmu_iommu_ops);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001239 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001240}
1241
1242static int tegra_smmu_remove(struct platform_device *pdev)
1243{
1244 struct smmu_device *smmu = platform_get_drvdata(pdev);
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +03001245 int i;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001246
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001247 smmu_debugfs_delete(smmu);
1248
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001249 smmu_write(smmu, SMMU_CONFIG_DISABLE, SMMU_CONFIG);
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +03001250 for (i = 0; i < smmu->num_as; i++)
1251 free_pdir(&smmu->as[i]);
1252 __free_page(smmu->avp_vector_page);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001253 smmu_handle = NULL;
1254 return 0;
1255}
1256
Sachin Kamata33a97c2013-10-08 16:21:04 +05301257static const struct dev_pm_ops tegra_smmu_pm_ops = {
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001258 .suspend = tegra_smmu_suspend,
1259 .resume = tegra_smmu_resume,
1260};
1261
Kiran Padwald943b0f2014-09-11 19:07:36 +05301262static const struct of_device_id tegra_smmu_of_match[] = {
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001263 { .compatible = "nvidia,tegra30-smmu", },
1264 { },
1265};
1266MODULE_DEVICE_TABLE(of, tegra_smmu_of_match);
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001267
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001268static struct platform_driver tegra_smmu_driver = {
1269 .probe = tegra_smmu_probe,
1270 .remove = tegra_smmu_remove,
1271 .driver = {
1272 .owner = THIS_MODULE,
1273 .name = "tegra-smmu",
1274 .pm = &tegra_smmu_pm_ops,
Stephen Warren573f4142013-02-15 15:01:07 -07001275 .of_match_table = tegra_smmu_of_match,
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001276 },
1277};
1278
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001279static int tegra_smmu_init(void)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001280{
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001281 return platform_driver_register(&tegra_smmu_driver);
1282}
1283
1284static void __exit tegra_smmu_exit(void)
1285{
1286 platform_driver_unregister(&tegra_smmu_driver);
1287}
1288
1289subsys_initcall(tegra_smmu_init);
1290module_exit(tegra_smmu_exit);
1291
1292MODULE_DESCRIPTION("IOMMU API for SMMU in Tegra30");
1293MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001294MODULE_ALIAS("platform:tegra-smmu");
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001295MODULE_LICENSE("GPL v2");