blob: 7db150ca163e2798a9105f12fe4c0292c9c3fd95 [file] [log] [blame]
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001/*
2 * IOMMU API for SMMU in Tegra30
3 *
4 * Copyright (c) 2011-2012, NVIDIA CORPORATION. All rights reserved.
5 *
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
22#include <linux/module.h>
23#include <linux/platform_device.h>
24#include <linux/spinlock.h>
25#include <linux/slab.h>
26#include <linux/vmalloc.h>
27#include <linux/mm.h>
28#include <linux/pagemap.h>
29#include <linux/device.h>
30#include <linux/sched.h>
31#include <linux/iommu.h>
32#include <linux/io.h>
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +030033#include <linux/of.h>
34#include <linux/of_iommu.h>
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +030035#include <linux/debugfs.h>
36#include <linux/seq_file.h>
Stephen Warrencc95e342012-09-19 15:51:01 -060037#include <linux/tegra-ahb.h>
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020038
39#include <asm/page.h>
40#include <asm/cacheflush.h>
41
Stephen Warrene6bc5932012-09-04 16:36:15 -060042enum smmu_hwgrp {
43 HWGRP_AFI,
44 HWGRP_AVPC,
45 HWGRP_DC,
46 HWGRP_DCB,
47 HWGRP_EPP,
48 HWGRP_G2,
49 HWGRP_HC,
50 HWGRP_HDA,
51 HWGRP_ISP,
52 HWGRP_MPE,
53 HWGRP_NV,
54 HWGRP_NV2,
55 HWGRP_PPCS,
56 HWGRP_SATA,
57 HWGRP_VDE,
58 HWGRP_VI,
59
60 HWGRP_COUNT,
61
62 HWGRP_END = ~0,
63};
64
65#define HWG_AFI (1 << HWGRP_AFI)
66#define HWG_AVPC (1 << HWGRP_AVPC)
67#define HWG_DC (1 << HWGRP_DC)
68#define HWG_DCB (1 << HWGRP_DCB)
69#define HWG_EPP (1 << HWGRP_EPP)
70#define HWG_G2 (1 << HWGRP_G2)
71#define HWG_HC (1 << HWGRP_HC)
72#define HWG_HDA (1 << HWGRP_HDA)
73#define HWG_ISP (1 << HWGRP_ISP)
74#define HWG_MPE (1 << HWGRP_MPE)
75#define HWG_NV (1 << HWGRP_NV)
76#define HWG_NV2 (1 << HWGRP_NV2)
77#define HWG_PPCS (1 << HWGRP_PPCS)
78#define HWG_SATA (1 << HWGRP_SATA)
79#define HWG_VDE (1 << HWGRP_VDE)
80#define HWG_VI (1 << HWGRP_VI)
81
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020082/* bitmap of the page sizes currently supported */
83#define SMMU_IOMMU_PGSIZES (SZ_4K)
84
85#define SMMU_CONFIG 0x10
86#define SMMU_CONFIG_DISABLE 0
87#define SMMU_CONFIG_ENABLE 1
88
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +030089/* REVISIT: To support multiple MCs */
90enum {
91 _MC = 0,
92};
93
94enum {
95 _TLB = 0,
96 _PTC,
97};
98
99#define SMMU_CACHE_CONFIG_BASE 0x14
100#define __SMMU_CACHE_CONFIG(mc, cache) (SMMU_CACHE_CONFIG_BASE + 4 * cache)
101#define SMMU_CACHE_CONFIG(cache) __SMMU_CACHE_CONFIG(_MC, cache)
102
103#define SMMU_CACHE_CONFIG_STATS_SHIFT 31
104#define SMMU_CACHE_CONFIG_STATS_ENABLE (1 << SMMU_CACHE_CONFIG_STATS_SHIFT)
105#define SMMU_CACHE_CONFIG_STATS_TEST_SHIFT 30
106#define SMMU_CACHE_CONFIG_STATS_TEST (1 << SMMU_CACHE_CONFIG_STATS_TEST_SHIFT)
107
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200108#define SMMU_TLB_CONFIG_HIT_UNDER_MISS__ENABLE (1 << 29)
109#define SMMU_TLB_CONFIG_ACTIVE_LINES__VALUE 0x10
110#define SMMU_TLB_CONFIG_RESET_VAL 0x20000010
111
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200112#define SMMU_PTC_CONFIG_CACHE__ENABLE (1 << 29)
113#define SMMU_PTC_CONFIG_INDEX_MAP__PATTERN 0x3f
114#define SMMU_PTC_CONFIG_RESET_VAL 0x2000003f
115
116#define SMMU_PTB_ASID 0x1c
117#define SMMU_PTB_ASID_CURRENT_SHIFT 0
118
119#define SMMU_PTB_DATA 0x20
120#define SMMU_PTB_DATA_RESET_VAL 0
121#define SMMU_PTB_DATA_ASID_NONSECURE_SHIFT 29
122#define SMMU_PTB_DATA_ASID_WRITABLE_SHIFT 30
123#define SMMU_PTB_DATA_ASID_READABLE_SHIFT 31
124
125#define SMMU_TLB_FLUSH 0x30
126#define SMMU_TLB_FLUSH_VA_MATCH_ALL 0
127#define SMMU_TLB_FLUSH_VA_MATCH_SECTION 2
128#define SMMU_TLB_FLUSH_VA_MATCH_GROUP 3
129#define SMMU_TLB_FLUSH_ASID_SHIFT 29
130#define SMMU_TLB_FLUSH_ASID_MATCH_DISABLE 0
131#define SMMU_TLB_FLUSH_ASID_MATCH_ENABLE 1
132#define SMMU_TLB_FLUSH_ASID_MATCH_SHIFT 31
133
134#define SMMU_PTC_FLUSH 0x34
135#define SMMU_PTC_FLUSH_TYPE_ALL 0
136#define SMMU_PTC_FLUSH_TYPE_ADR 1
137#define SMMU_PTC_FLUSH_ADR_SHIFT 4
138
139#define SMMU_ASID_SECURITY 0x38
140
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300141#define SMMU_STATS_CACHE_COUNT_BASE 0x1f0
142
143#define SMMU_STATS_CACHE_COUNT(mc, cache, hitmiss) \
144 (SMMU_STATS_CACHE_COUNT_BASE + 8 * cache + 4 * hitmiss)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200145
146#define SMMU_TRANSLATION_ENABLE_0 0x228
147#define SMMU_TRANSLATION_ENABLE_1 0x22c
148#define SMMU_TRANSLATION_ENABLE_2 0x230
149
150#define SMMU_AFI_ASID 0x238 /* PCIE */
151#define SMMU_AVPC_ASID 0x23c /* AVP */
152#define SMMU_DC_ASID 0x240 /* Display controller */
153#define SMMU_DCB_ASID 0x244 /* Display controller B */
154#define SMMU_EPP_ASID 0x248 /* Encoder pre-processor */
155#define SMMU_G2_ASID 0x24c /* 2D engine */
156#define SMMU_HC_ASID 0x250 /* Host1x */
157#define SMMU_HDA_ASID 0x254 /* High-def audio */
158#define SMMU_ISP_ASID 0x258 /* Image signal processor */
159#define SMMU_MPE_ASID 0x264 /* MPEG encoder */
160#define SMMU_NV_ASID 0x268 /* (3D) */
161#define SMMU_NV2_ASID 0x26c /* (3D) */
162#define SMMU_PPCS_ASID 0x270 /* AHB */
163#define SMMU_SATA_ASID 0x278 /* SATA */
164#define SMMU_VDE_ASID 0x27c /* Video decoder */
165#define SMMU_VI_ASID 0x280 /* Video input */
166
167#define SMMU_PDE_NEXT_SHIFT 28
168
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200169#define SMMU_TLB_FLUSH_VA_SECTION__MASK 0xffc00000
170#define SMMU_TLB_FLUSH_VA_SECTION__SHIFT 12 /* right shift */
171#define SMMU_TLB_FLUSH_VA_GROUP__MASK 0xffffc000
172#define SMMU_TLB_FLUSH_VA_GROUP__SHIFT 12 /* right shift */
173#define SMMU_TLB_FLUSH_VA(iova, which) \
174 ((((iova) & SMMU_TLB_FLUSH_VA_##which##__MASK) >> \
175 SMMU_TLB_FLUSH_VA_##which##__SHIFT) | \
176 SMMU_TLB_FLUSH_VA_MATCH_##which)
177#define SMMU_PTB_ASID_CUR(n) \
178 ((n) << SMMU_PTB_ASID_CURRENT_SHIFT)
179#define SMMU_TLB_FLUSH_ASID_MATCH_disable \
180 (SMMU_TLB_FLUSH_ASID_MATCH_DISABLE << \
181 SMMU_TLB_FLUSH_ASID_MATCH_SHIFT)
182#define SMMU_TLB_FLUSH_ASID_MATCH__ENABLE \
183 (SMMU_TLB_FLUSH_ASID_MATCH_ENABLE << \
184 SMMU_TLB_FLUSH_ASID_MATCH_SHIFT)
185
186#define SMMU_PAGE_SHIFT 12
187#define SMMU_PAGE_SIZE (1 << SMMU_PAGE_SHIFT)
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300188#define SMMU_PAGE_MASK ((1 << SMMU_PAGE_SHIFT) - 1)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200189
190#define SMMU_PDIR_COUNT 1024
191#define SMMU_PDIR_SIZE (sizeof(unsigned long) * SMMU_PDIR_COUNT)
192#define SMMU_PTBL_COUNT 1024
193#define SMMU_PTBL_SIZE (sizeof(unsigned long) * SMMU_PTBL_COUNT)
194#define SMMU_PDIR_SHIFT 12
195#define SMMU_PDE_SHIFT 12
196#define SMMU_PTE_SHIFT 12
197#define SMMU_PFN_MASK 0x000fffff
198
199#define SMMU_ADDR_TO_PFN(addr) ((addr) >> 12)
200#define SMMU_ADDR_TO_PDN(addr) ((addr) >> 22)
Hiro Sugawarad0078e72012-10-18 08:35:10 +0300201#define SMMU_PDN_TO_ADDR(pdn) ((pdn) << 22)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200202
203#define _READABLE (1 << SMMU_PTB_DATA_ASID_READABLE_SHIFT)
204#define _WRITABLE (1 << SMMU_PTB_DATA_ASID_WRITABLE_SHIFT)
205#define _NONSECURE (1 << SMMU_PTB_DATA_ASID_NONSECURE_SHIFT)
206#define _PDE_NEXT (1 << SMMU_PDE_NEXT_SHIFT)
207#define _MASK_ATTR (_READABLE | _WRITABLE | _NONSECURE)
208
209#define _PDIR_ATTR (_READABLE | _WRITABLE | _NONSECURE)
210
211#define _PDE_ATTR (_READABLE | _WRITABLE | _NONSECURE)
212#define _PDE_ATTR_N (_PDE_ATTR | _PDE_NEXT)
213#define _PDE_VACANT(pdn) (((pdn) << 10) | _PDE_ATTR)
214
215#define _PTE_ATTR (_READABLE | _WRITABLE | _NONSECURE)
216#define _PTE_VACANT(addr) (((addr) >> SMMU_PAGE_SHIFT) | _PTE_ATTR)
217
218#define SMMU_MK_PDIR(page, attr) \
219 ((page_to_phys(page) >> SMMU_PDIR_SHIFT) | (attr))
220#define SMMU_MK_PDE(page, attr) \
221 (unsigned long)((page_to_phys(page) >> SMMU_PDE_SHIFT) | (attr))
222#define SMMU_EX_PTBL_PAGE(pde) \
223 pfn_to_page((unsigned long)(pde) & SMMU_PFN_MASK)
224#define SMMU_PFN_TO_PTE(pfn, attr) (unsigned long)((pfn) | (attr))
225
226#define SMMU_ASID_ENABLE(asid) ((asid) | (1 << 31))
227#define SMMU_ASID_DISABLE 0
228#define SMMU_ASID_ASID(n) ((n) & ~SMMU_ASID_ENABLE(0))
229
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300230#define NUM_SMMU_REG_BANKS 3
231
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200232#define smmu_client_enable_hwgrp(c, m) smmu_client_set_hwgrp(c, m, 1)
233#define smmu_client_disable_hwgrp(c) smmu_client_set_hwgrp(c, 0, 0)
234#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
237#define HWGRP_INIT(client) [HWGRP_##client] = SMMU_##client##_ASID
238
239static const u32 smmu_hwgrp_asid_reg[] = {
240 HWGRP_INIT(AFI),
241 HWGRP_INIT(AVPC),
242 HWGRP_INIT(DC),
243 HWGRP_INIT(DCB),
244 HWGRP_INIT(EPP),
245 HWGRP_INIT(G2),
246 HWGRP_INIT(HC),
247 HWGRP_INIT(HDA),
248 HWGRP_INIT(ISP),
249 HWGRP_INIT(MPE),
250 HWGRP_INIT(NV),
251 HWGRP_INIT(NV2),
252 HWGRP_INIT(PPCS),
253 HWGRP_INIT(SATA),
254 HWGRP_INIT(VDE),
255 HWGRP_INIT(VI),
256};
257#define HWGRP_ASID_REG(x) (smmu_hwgrp_asid_reg[x])
258
259/*
260 * Per client for address space
261 */
262struct smmu_client {
263 struct device *dev;
264 struct list_head list;
265 struct smmu_as *as;
266 u32 hwgrp;
267};
268
269/*
270 * Per address space
271 */
272struct smmu_as {
273 struct smmu_device *smmu; /* back pointer to container */
274 unsigned int asid;
275 spinlock_t lock; /* for pagetable */
276 struct page *pdir_page;
277 unsigned long pdir_attr;
278 unsigned long pde_attr;
279 unsigned long pte_attr;
280 unsigned int *pte_count;
281
282 struct list_head client;
283 spinlock_t client_lock; /* for client list */
284};
285
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -0600286struct smmu_debugfs_info {
287 struct smmu_device *smmu;
288 int mc;
289 int cache;
290};
291
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200292/*
293 * Per SMMU device - IOMMU device
294 */
295struct smmu_device {
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300296 void __iomem *regs[NUM_SMMU_REG_BANKS];
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200297 unsigned long iovmm_base; /* remappable base address */
298 unsigned long page_count; /* total remappable size */
299 spinlock_t lock;
300 char *name;
301 struct device *dev;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200302 struct page *avp_vector_page; /* dummy page shared by all AS's */
303
304 /*
305 * Register image savers for suspend/resume
306 */
307 unsigned long translation_enable_0;
308 unsigned long translation_enable_1;
309 unsigned long translation_enable_2;
310 unsigned long asid_security;
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300311
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300312 struct dentry *debugfs_root;
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -0600313 struct smmu_debugfs_info *debugfs_info;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300314
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300315 struct device_node *ahb;
Hiroshi Doyua3b24912012-06-25 14:23:56 +0300316
317 int num_as;
318 struct smmu_as as[0]; /* Run-time allocated array */
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200319};
320
321static struct smmu_device *smmu_handle; /* unique for a system */
322
323/*
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300324 * SMMU register accessors
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200325 */
326static inline u32 smmu_read(struct smmu_device *smmu, size_t offs)
327{
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300328 BUG_ON(offs < 0x10);
329 if (offs < 0x3c)
330 return readl(smmu->regs[0] + offs - 0x10);
331 BUG_ON(offs < 0x1f0);
332 if (offs < 0x200)
333 return readl(smmu->regs[1] + offs - 0x1f0);
334 BUG_ON(offs < 0x228);
335 if (offs < 0x284)
336 return readl(smmu->regs[2] + offs - 0x228);
337 BUG();
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200338}
339
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300340static inline void smmu_write(struct smmu_device *smmu, u32 val, size_t offs)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200341{
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300342 BUG_ON(offs < 0x10);
343 if (offs < 0x3c) {
344 writel(val, smmu->regs[0] + offs - 0x10);
345 return;
346 }
347 BUG_ON(offs < 0x1f0);
348 if (offs < 0x200) {
349 writel(val, smmu->regs[1] + offs - 0x1f0);
350 return;
351 }
352 BUG_ON(offs < 0x228);
353 if (offs < 0x284) {
354 writel(val, smmu->regs[2] + offs - 0x228);
355 return;
356 }
357 BUG();
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200358}
359
360#define VA_PAGE_TO_PA(va, page) \
361 (page_to_phys(page) + ((unsigned long)(va) & ~PAGE_MASK))
362
363#define FLUSH_CPU_DCACHE(va, page, size) \
364 do { \
365 unsigned long _pa_ = VA_PAGE_TO_PA(va, page); \
366 __cpuc_flush_dcache_area((void *)(va), (size_t)(size)); \
367 outer_flush_range(_pa_, _pa_+(size_t)(size)); \
368 } while (0)
369
370/*
371 * Any interaction between any block on PPSB and a block on APB or AHB
372 * must have these read-back barriers to ensure the APB/AHB bus
373 * transaction is complete before initiating activity on the PPSB
374 * block.
375 */
376#define FLUSH_SMMU_REGS(smmu) smmu_read(smmu, SMMU_CONFIG)
377
378#define smmu_client_hwgrp(c) (u32)((c)->dev->platform_data)
379
380static int __smmu_client_set_hwgrp(struct smmu_client *c,
381 unsigned long map, int on)
382{
383 int i;
384 struct smmu_as *as = c->as;
385 u32 val, offs, mask = SMMU_ASID_ENABLE(as->asid);
386 struct smmu_device *smmu = as->smmu;
387
388 WARN_ON(!on && map);
389 if (on && !map)
390 return -EINVAL;
391 if (!on)
392 map = smmu_client_hwgrp(c);
393
394 for_each_set_bit(i, &map, HWGRP_COUNT) {
395 offs = HWGRP_ASID_REG(i);
396 val = smmu_read(smmu, offs);
397 if (on) {
398 if (WARN_ON(val & mask))
399 goto err_hw_busy;
400 val |= mask;
401 } else {
402 WARN_ON((val & mask) == mask);
403 val &= ~mask;
404 }
405 smmu_write(smmu, val, offs);
406 }
407 FLUSH_SMMU_REGS(smmu);
408 c->hwgrp = map;
409 return 0;
410
411err_hw_busy:
412 for_each_set_bit(i, &map, HWGRP_COUNT) {
413 offs = HWGRP_ASID_REG(i);
414 val = smmu_read(smmu, offs);
415 val &= ~mask;
416 smmu_write(smmu, val, offs);
417 }
418 return -EBUSY;
419}
420
421static int smmu_client_set_hwgrp(struct smmu_client *c, u32 map, int on)
422{
423 u32 val;
424 unsigned long flags;
425 struct smmu_as *as = c->as;
426 struct smmu_device *smmu = as->smmu;
427
428 spin_lock_irqsave(&smmu->lock, flags);
429 val = __smmu_client_set_hwgrp(c, map, on);
430 spin_unlock_irqrestore(&smmu->lock, flags);
431 return val;
432}
433
434/*
435 * Flush all TLB entries and all PTC entries
436 * Caller must lock smmu
437 */
438static void smmu_flush_regs(struct smmu_device *smmu, int enable)
439{
440 u32 val;
441
442 smmu_write(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
443 FLUSH_SMMU_REGS(smmu);
444 val = SMMU_TLB_FLUSH_VA_MATCH_ALL |
445 SMMU_TLB_FLUSH_ASID_MATCH_disable;
446 smmu_write(smmu, val, SMMU_TLB_FLUSH);
447
448 if (enable)
449 smmu_write(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
450 FLUSH_SMMU_REGS(smmu);
451}
452
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300453static int smmu_setup_regs(struct smmu_device *smmu)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200454{
455 int i;
456 u32 val;
457
458 for (i = 0; i < smmu->num_as; i++) {
459 struct smmu_as *as = &smmu->as[i];
460 struct smmu_client *c;
461
462 smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
463 val = as->pdir_page ?
464 SMMU_MK_PDIR(as->pdir_page, as->pdir_attr) :
465 SMMU_PTB_DATA_RESET_VAL;
466 smmu_write(smmu, val, SMMU_PTB_DATA);
467
468 list_for_each_entry(c, &as->client, list)
469 __smmu_client_set_hwgrp(c, c->hwgrp, 1);
470 }
471
472 smmu_write(smmu, smmu->translation_enable_0, SMMU_TRANSLATION_ENABLE_0);
473 smmu_write(smmu, smmu->translation_enable_1, SMMU_TRANSLATION_ENABLE_1);
474 smmu_write(smmu, smmu->translation_enable_2, SMMU_TRANSLATION_ENABLE_2);
475 smmu_write(smmu, smmu->asid_security, SMMU_ASID_SECURITY);
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300476 smmu_write(smmu, SMMU_TLB_CONFIG_RESET_VAL, SMMU_CACHE_CONFIG(_TLB));
477 smmu_write(smmu, SMMU_PTC_CONFIG_RESET_VAL, SMMU_CACHE_CONFIG(_PTC));
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200478
479 smmu_flush_regs(smmu, 1);
480
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300481 return tegra_ahb_enable_smmu(smmu->ahb);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200482}
483
484static void flush_ptc_and_tlb(struct smmu_device *smmu,
485 struct smmu_as *as, dma_addr_t iova,
486 unsigned long *pte, struct page *page, int is_pde)
487{
488 u32 val;
489 unsigned long tlb_flush_va = is_pde
490 ? SMMU_TLB_FLUSH_VA(iova, SECTION)
491 : SMMU_TLB_FLUSH_VA(iova, GROUP);
492
493 val = SMMU_PTC_FLUSH_TYPE_ADR | VA_PAGE_TO_PA(pte, page);
494 smmu_write(smmu, val, SMMU_PTC_FLUSH);
495 FLUSH_SMMU_REGS(smmu);
496 val = tlb_flush_va |
497 SMMU_TLB_FLUSH_ASID_MATCH__ENABLE |
498 (as->asid << SMMU_TLB_FLUSH_ASID_SHIFT);
499 smmu_write(smmu, val, SMMU_TLB_FLUSH);
500 FLUSH_SMMU_REGS(smmu);
501}
502
503static void free_ptbl(struct smmu_as *as, dma_addr_t iova)
504{
505 unsigned long pdn = SMMU_ADDR_TO_PDN(iova);
506 unsigned long *pdir = (unsigned long *)page_address(as->pdir_page);
507
508 if (pdir[pdn] != _PDE_VACANT(pdn)) {
509 dev_dbg(as->smmu->dev, "pdn: %lx\n", pdn);
510
511 ClearPageReserved(SMMU_EX_PTBL_PAGE(pdir[pdn]));
512 __free_page(SMMU_EX_PTBL_PAGE(pdir[pdn]));
513 pdir[pdn] = _PDE_VACANT(pdn);
514 FLUSH_CPU_DCACHE(&pdir[pdn], as->pdir_page, sizeof pdir[pdn]);
515 flush_ptc_and_tlb(as->smmu, as, iova, &pdir[pdn],
516 as->pdir_page, 1);
517 }
518}
519
520static void free_pdir(struct smmu_as *as)
521{
522 unsigned addr;
523 int count;
524 struct device *dev = as->smmu->dev;
525
526 if (!as->pdir_page)
527 return;
528
529 addr = as->smmu->iovmm_base;
530 count = as->smmu->page_count;
531 while (count-- > 0) {
532 free_ptbl(as, addr);
533 addr += SMMU_PAGE_SIZE * SMMU_PTBL_COUNT;
534 }
535 ClearPageReserved(as->pdir_page);
536 __free_page(as->pdir_page);
537 as->pdir_page = NULL;
538 devm_kfree(dev, as->pte_count);
539 as->pte_count = NULL;
540}
541
542/*
543 * Maps PTBL for given iova and returns the PTE address
544 * Caller must unmap the mapped PTBL returned in *ptbl_page_p
545 */
546static unsigned long *locate_pte(struct smmu_as *as,
547 dma_addr_t iova, bool allocate,
548 struct page **ptbl_page_p,
549 unsigned int **count)
550{
551 unsigned long ptn = SMMU_ADDR_TO_PFN(iova);
552 unsigned long pdn = SMMU_ADDR_TO_PDN(iova);
553 unsigned long *pdir = page_address(as->pdir_page);
554 unsigned long *ptbl;
555
556 if (pdir[pdn] != _PDE_VACANT(pdn)) {
557 /* Mapped entry table already exists */
558 *ptbl_page_p = SMMU_EX_PTBL_PAGE(pdir[pdn]);
559 ptbl = page_address(*ptbl_page_p);
560 } else if (!allocate) {
561 return NULL;
562 } else {
563 int pn;
564 unsigned long addr = SMMU_PDN_TO_ADDR(pdn);
565
566 /* Vacant - allocate a new page table */
567 dev_dbg(as->smmu->dev, "New PTBL pdn: %lx\n", pdn);
568
569 *ptbl_page_p = alloc_page(GFP_ATOMIC);
570 if (!*ptbl_page_p) {
571 dev_err(as->smmu->dev,
572 "failed to allocate smmu_device page table\n");
573 return NULL;
574 }
575 SetPageReserved(*ptbl_page_p);
576 ptbl = (unsigned long *)page_address(*ptbl_page_p);
577 for (pn = 0; pn < SMMU_PTBL_COUNT;
578 pn++, addr += SMMU_PAGE_SIZE) {
579 ptbl[pn] = _PTE_VACANT(addr);
580 }
581 FLUSH_CPU_DCACHE(ptbl, *ptbl_page_p, SMMU_PTBL_SIZE);
582 pdir[pdn] = SMMU_MK_PDE(*ptbl_page_p,
583 as->pde_attr | _PDE_NEXT);
584 FLUSH_CPU_DCACHE(&pdir[pdn], as->pdir_page, sizeof pdir[pdn]);
585 flush_ptc_and_tlb(as->smmu, as, iova, &pdir[pdn],
586 as->pdir_page, 1);
587 }
588 *count = &as->pte_count[pdn];
589
590 return &ptbl[ptn % SMMU_PTBL_COUNT];
591}
592
593#ifdef CONFIG_SMMU_SIG_DEBUG
594static void put_signature(struct smmu_as *as,
595 dma_addr_t iova, unsigned long pfn)
596{
597 struct page *page;
598 unsigned long *vaddr;
599
600 page = pfn_to_page(pfn);
601 vaddr = page_address(page);
602 if (!vaddr)
603 return;
604
605 vaddr[0] = iova;
606 vaddr[1] = pfn << PAGE_SHIFT;
607 FLUSH_CPU_DCACHE(vaddr, page, sizeof(vaddr[0]) * 2);
608}
609#else
610static inline void put_signature(struct smmu_as *as,
611 unsigned long addr, unsigned long pfn)
612{
613}
614#endif
615
616/*
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200617 * Caller must not hold as->lock
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200618 */
619static int alloc_pdir(struct smmu_as *as)
620{
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200621 unsigned long *pdir, flags;
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300622 int pdn, err = 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200623 u32 val;
624 struct smmu_device *smmu = as->smmu;
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300625 struct page *page;
626 unsigned int *cnt;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200627
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300628 /*
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200629 * do the allocation, then grab as->lock
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300630 */
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300631 cnt = devm_kzalloc(smmu->dev,
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200632 sizeof(cnt[0]) * SMMU_PDIR_COUNT,
633 GFP_KERNEL);
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300634 page = alloc_page(GFP_KERNEL | __GFP_DMA);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200635
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200636 spin_lock_irqsave(&as->lock, flags);
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300637
638 if (as->pdir_page) {
639 /* We raced, free the redundant */
640 err = -EAGAIN;
641 goto err_out;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200642 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300643
644 if (!page || !cnt) {
645 dev_err(smmu->dev, "failed to allocate at %s\n", __func__);
646 err = -ENOMEM;
647 goto err_out;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200648 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300649
650 as->pdir_page = page;
651 as->pte_count = cnt;
652
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200653 SetPageReserved(as->pdir_page);
654 pdir = page_address(as->pdir_page);
655
656 for (pdn = 0; pdn < SMMU_PDIR_COUNT; pdn++)
657 pdir[pdn] = _PDE_VACANT(pdn);
658 FLUSH_CPU_DCACHE(pdir, as->pdir_page, SMMU_PDIR_SIZE);
659 val = SMMU_PTC_FLUSH_TYPE_ADR | VA_PAGE_TO_PA(pdir, as->pdir_page);
660 smmu_write(smmu, val, SMMU_PTC_FLUSH);
661 FLUSH_SMMU_REGS(as->smmu);
662 val = SMMU_TLB_FLUSH_VA_MATCH_ALL |
663 SMMU_TLB_FLUSH_ASID_MATCH__ENABLE |
664 (as->asid << SMMU_TLB_FLUSH_ASID_SHIFT);
665 smmu_write(smmu, val, SMMU_TLB_FLUSH);
666 FLUSH_SMMU_REGS(as->smmu);
667
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200668 spin_unlock_irqrestore(&as->lock, flags);
669
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200670 return 0;
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300671
672err_out:
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200673 spin_unlock_irqrestore(&as->lock, flags);
674
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300675 devm_kfree(smmu->dev, cnt);
676 if (page)
677 __free_page(page);
678 return err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200679}
680
681static void __smmu_iommu_unmap(struct smmu_as *as, dma_addr_t iova)
682{
683 unsigned long *pte;
684 struct page *page;
685 unsigned int *count;
686
687 pte = locate_pte(as, iova, false, &page, &count);
688 if (WARN_ON(!pte))
689 return;
690
691 if (WARN_ON(*pte == _PTE_VACANT(iova)))
692 return;
693
694 *pte = _PTE_VACANT(iova);
695 FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
696 flush_ptc_and_tlb(as->smmu, as, iova, pte, page, 0);
Hiroshi Doyu37683e42012-11-28 15:52:53 +0200697 if (!--(*count))
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200698 free_ptbl(as, iova);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200699}
700
701static void __smmu_iommu_map_pfn(struct smmu_as *as, dma_addr_t iova,
702 unsigned long pfn)
703{
704 struct smmu_device *smmu = as->smmu;
705 unsigned long *pte;
706 unsigned int *count;
707 struct page *page;
708
709 pte = locate_pte(as, iova, true, &page, &count);
710 if (WARN_ON(!pte))
711 return;
712
713 if (*pte == _PTE_VACANT(iova))
714 (*count)++;
715 *pte = SMMU_PFN_TO_PTE(pfn, as->pte_attr);
716 if (unlikely((*pte == _PTE_VACANT(iova))))
717 (*count)--;
718 FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
719 flush_ptc_and_tlb(smmu, as, iova, pte, page, 0);
720 put_signature(as, iova, pfn);
721}
722
723static int smmu_iommu_map(struct iommu_domain *domain, unsigned long iova,
724 phys_addr_t pa, size_t bytes, int prot)
725{
726 struct smmu_as *as = domain->priv;
727 unsigned long pfn = __phys_to_pfn(pa);
728 unsigned long flags;
729
730 dev_dbg(as->smmu->dev, "[%d] %08lx:%08x\n", as->asid, iova, pa);
731
732 if (!pfn_valid(pfn))
733 return -ENOMEM;
734
735 spin_lock_irqsave(&as->lock, flags);
736 __smmu_iommu_map_pfn(as, iova, pfn);
737 spin_unlock_irqrestore(&as->lock, flags);
738 return 0;
739}
740
741static size_t smmu_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
742 size_t bytes)
743{
744 struct smmu_as *as = domain->priv;
745 unsigned long flags;
746
747 dev_dbg(as->smmu->dev, "[%d] %08lx\n", as->asid, iova);
748
749 spin_lock_irqsave(&as->lock, flags);
750 __smmu_iommu_unmap(as, iova);
751 spin_unlock_irqrestore(&as->lock, flags);
752 return SMMU_PAGE_SIZE;
753}
754
755static phys_addr_t smmu_iommu_iova_to_phys(struct iommu_domain *domain,
756 unsigned long iova)
757{
758 struct smmu_as *as = domain->priv;
759 unsigned long *pte;
760 unsigned int *count;
761 struct page *page;
762 unsigned long pfn;
763 unsigned long flags;
764
765 spin_lock_irqsave(&as->lock, flags);
766
767 pte = locate_pte(as, iova, true, &page, &count);
768 pfn = *pte & SMMU_PFN_MASK;
769 WARN_ON(!pfn_valid(pfn));
770 dev_dbg(as->smmu->dev,
771 "iova:%08lx pfn:%08lx asid:%d\n", iova, pfn, as->asid);
772
773 spin_unlock_irqrestore(&as->lock, flags);
774 return PFN_PHYS(pfn);
775}
776
777static int smmu_iommu_domain_has_cap(struct iommu_domain *domain,
778 unsigned long cap)
779{
780 return 0;
781}
782
783static int smmu_iommu_attach_dev(struct iommu_domain *domain,
784 struct device *dev)
785{
786 struct smmu_as *as = domain->priv;
787 struct smmu_device *smmu = as->smmu;
788 struct smmu_client *client, *c;
789 u32 map;
790 int err;
791
792 client = devm_kzalloc(smmu->dev, sizeof(*c), GFP_KERNEL);
793 if (!client)
794 return -ENOMEM;
795 client->dev = dev;
796 client->as = as;
797 map = (unsigned long)dev->platform_data;
798 if (!map)
799 return -EINVAL;
800
801 err = smmu_client_enable_hwgrp(client, map);
802 if (err)
803 goto err_hwgrp;
804
805 spin_lock(&as->client_lock);
806 list_for_each_entry(c, &as->client, list) {
807 if (c->dev == dev) {
808 dev_err(smmu->dev,
809 "%s is already attached\n", dev_name(c->dev));
810 err = -EINVAL;
811 goto err_client;
812 }
813 }
814 list_add(&client->list, &as->client);
815 spin_unlock(&as->client_lock);
816
817 /*
818 * Reserve "page zero" for AVP vectors using a common dummy
819 * page.
820 */
821 if (map & HWG_AVPC) {
822 struct page *page;
823
824 page = as->smmu->avp_vector_page;
825 __smmu_iommu_map_pfn(as, 0, page_to_pfn(page));
826
827 pr_info("Reserve \"page zero\" for AVP vectors using a common dummy\n");
828 }
829
Hiroshi DOYU90730912012-04-13 12:18:30 +0200830 dev_dbg(smmu->dev, "%s is attached\n", dev_name(dev));
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200831 return 0;
832
833err_client:
834 smmu_client_disable_hwgrp(client);
835 spin_unlock(&as->client_lock);
836err_hwgrp:
837 devm_kfree(smmu->dev, client);
838 return err;
839}
840
841static void smmu_iommu_detach_dev(struct iommu_domain *domain,
842 struct device *dev)
843{
844 struct smmu_as *as = domain->priv;
845 struct smmu_device *smmu = as->smmu;
846 struct smmu_client *c;
847
848 spin_lock(&as->client_lock);
849
850 list_for_each_entry(c, &as->client, list) {
851 if (c->dev == dev) {
852 smmu_client_disable_hwgrp(c);
853 list_del(&c->list);
854 devm_kfree(smmu->dev, c);
855 c->as = NULL;
856 dev_dbg(smmu->dev,
857 "%s is detached\n", dev_name(c->dev));
858 goto out;
859 }
860 }
Julia Lawall9579a972012-07-08 13:37:42 +0200861 dev_err(smmu->dev, "Couldn't find %s\n", dev_name(dev));
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200862out:
863 spin_unlock(&as->client_lock);
864}
865
866static int smmu_iommu_domain_init(struct iommu_domain *domain)
867{
Hiroshi Doyud1d076f2012-07-30 08:39:19 +0300868 int i, err = -EAGAIN;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200869 unsigned long flags;
870 struct smmu_as *as;
871 struct smmu_device *smmu = smmu_handle;
872
873 /* Look for a free AS with lock held */
874 for (i = 0; i < smmu->num_as; i++) {
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300875 as = &smmu->as[i];
Hiroshi Doyud2453b22012-07-30 08:39:18 +0300876
877 if (as->pdir_page)
878 continue;
879
880 err = alloc_pdir(as);
881 if (!err)
882 goto found;
883
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300884 if (err != -EAGAIN)
885 break;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200886 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300887 if (i == smmu->num_as)
888 dev_err(smmu->dev, "no free AS\n");
889 return err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200890
891found:
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200892 spin_lock_irqsave(&smmu->lock, flags);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200893
894 /* Update PDIR register */
895 smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
896 smmu_write(smmu,
897 SMMU_MK_PDIR(as->pdir_page, as->pdir_attr), SMMU_PTB_DATA);
898 FLUSH_SMMU_REGS(smmu);
899
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200900 spin_unlock_irqrestore(&smmu->lock, flags);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200901
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200902 domain->priv = as;
903
Hiroshi DOYU233499022012-01-26 19:40:57 +0100904 domain->geometry.aperture_start = smmu->iovmm_base;
905 domain->geometry.aperture_end = smmu->iovmm_base +
906 smmu->page_count * SMMU_PAGE_SIZE - 1;
907 domain->geometry.force_aperture = true;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200908
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200909 dev_dbg(smmu->dev, "smmu_as@%p\n", as);
Joerg Roedelf9a4f062012-07-17 11:47:14 +0200910
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200911 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200912}
913
914static void smmu_iommu_domain_destroy(struct iommu_domain *domain)
915{
916 struct smmu_as *as = domain->priv;
917 struct smmu_device *smmu = as->smmu;
918 unsigned long flags;
919
920 spin_lock_irqsave(&as->lock, flags);
921
922 if (as->pdir_page) {
923 spin_lock(&smmu->lock);
924 smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
925 smmu_write(smmu, SMMU_PTB_DATA_RESET_VAL, SMMU_PTB_DATA);
926 FLUSH_SMMU_REGS(smmu);
927 spin_unlock(&smmu->lock);
928
929 free_pdir(as);
930 }
931
932 if (!list_empty(&as->client)) {
933 struct smmu_client *c;
934
935 list_for_each_entry(c, &as->client, list)
936 smmu_iommu_detach_dev(domain, c->dev);
937 }
938
939 spin_unlock_irqrestore(&as->lock, flags);
940
941 domain->priv = NULL;
942 dev_dbg(smmu->dev, "smmu_as@%p\n", as);
943}
944
945static struct iommu_ops smmu_iommu_ops = {
946 .domain_init = smmu_iommu_domain_init,
947 .domain_destroy = smmu_iommu_domain_destroy,
948 .attach_dev = smmu_iommu_attach_dev,
949 .detach_dev = smmu_iommu_detach_dev,
950 .map = smmu_iommu_map,
951 .unmap = smmu_iommu_unmap,
952 .iova_to_phys = smmu_iommu_iova_to_phys,
953 .domain_has_cap = smmu_iommu_domain_has_cap,
954 .pgsize_bitmap = SMMU_IOMMU_PGSIZES,
955};
956
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300957/* Should be in the order of enum */
958static const char * const smmu_debugfs_mc[] = { "mc", };
959static const char * const smmu_debugfs_cache[] = { "tlb", "ptc", };
960
961static ssize_t smmu_debugfs_stats_write(struct file *file,
962 const char __user *buffer,
963 size_t count, loff_t *pos)
964{
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -0600965 struct smmu_debugfs_info *info;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300966 struct smmu_device *smmu;
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -0600967 int i;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300968 enum {
969 _OFF = 0,
970 _ON,
971 _RESET,
972 };
973 const char * const command[] = {
974 [_OFF] = "off",
975 [_ON] = "on",
976 [_RESET] = "reset",
977 };
978 char str[] = "reset";
979 u32 val;
980 size_t offs;
981
982 count = min_t(size_t, count, sizeof(str));
983 if (copy_from_user(str, buffer, count))
984 return -EINVAL;
985
986 for (i = 0; i < ARRAY_SIZE(command); i++)
987 if (strncmp(str, command[i],
988 strlen(command[i])) == 0)
989 break;
990
991 if (i == ARRAY_SIZE(command))
992 return -EINVAL;
993
Al Viro496ad9a2013-01-23 17:07:38 -0500994 info = file_inode(file)->i_private;
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -0600995 smmu = info->smmu;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300996
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -0600997 offs = SMMU_CACHE_CONFIG(info->cache);
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300998 val = smmu_read(smmu, offs);
999 switch (i) {
1000 case _OFF:
1001 val &= ~SMMU_CACHE_CONFIG_STATS_ENABLE;
1002 val &= ~SMMU_CACHE_CONFIG_STATS_TEST;
1003 smmu_write(smmu, val, offs);
1004 break;
1005 case _ON:
1006 val |= SMMU_CACHE_CONFIG_STATS_ENABLE;
1007 val &= ~SMMU_CACHE_CONFIG_STATS_TEST;
1008 smmu_write(smmu, val, offs);
1009 break;
1010 case _RESET:
1011 val |= SMMU_CACHE_CONFIG_STATS_TEST;
1012 smmu_write(smmu, val, offs);
1013 val &= ~SMMU_CACHE_CONFIG_STATS_TEST;
1014 smmu_write(smmu, val, offs);
1015 break;
1016 default:
1017 BUG();
1018 break;
1019 }
1020
1021 dev_dbg(smmu->dev, "%s() %08x, %08x @%08x\n", __func__,
1022 val, smmu_read(smmu, offs), offs);
1023
1024 return count;
1025}
1026
1027static int smmu_debugfs_stats_show(struct seq_file *s, void *v)
1028{
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001029 struct smmu_debugfs_info *info;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001030 struct smmu_device *smmu;
1031 struct dentry *dent;
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001032 int i;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001033 const char * const stats[] = { "hit", "miss", };
1034
1035 dent = d_find_alias(s->private);
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001036 info = dent->d_inode->i_private;
1037 smmu = info->smmu;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001038
1039 for (i = 0; i < ARRAY_SIZE(stats); i++) {
1040 u32 val;
1041 size_t offs;
1042
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001043 offs = SMMU_STATS_CACHE_COUNT(info->mc, info->cache, i);
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001044 val = smmu_read(smmu, offs);
1045 seq_printf(s, "%s:%08x ", stats[i], val);
1046
1047 dev_dbg(smmu->dev, "%s() %s %08x @%08x\n", __func__,
1048 stats[i], val, offs);
1049 }
1050 seq_printf(s, "\n");
Cyril Roelandtb334b642012-11-11 21:49:30 +01001051 dput(dent);
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001052
1053 return 0;
1054}
1055
1056static int smmu_debugfs_stats_open(struct inode *inode, struct file *file)
1057{
1058 return single_open(file, smmu_debugfs_stats_show, inode);
1059}
1060
1061static const struct file_operations smmu_debugfs_stats_fops = {
1062 .open = smmu_debugfs_stats_open,
1063 .read = seq_read,
1064 .llseek = seq_lseek,
1065 .release = single_release,
1066 .write = smmu_debugfs_stats_write,
1067};
1068
1069static void smmu_debugfs_delete(struct smmu_device *smmu)
1070{
1071 debugfs_remove_recursive(smmu->debugfs_root);
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001072 kfree(smmu->debugfs_info);
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001073}
1074
1075static void smmu_debugfs_create(struct smmu_device *smmu)
1076{
1077 int i;
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001078 size_t bytes;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001079 struct dentry *root;
1080
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001081 bytes = ARRAY_SIZE(smmu_debugfs_mc) * ARRAY_SIZE(smmu_debugfs_cache) *
1082 sizeof(*smmu->debugfs_info);
1083 smmu->debugfs_info = kmalloc(bytes, GFP_KERNEL);
1084 if (!smmu->debugfs_info)
1085 return;
1086
1087 root = debugfs_create_dir(dev_name(smmu->dev), NULL);
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001088 if (!root)
1089 goto err_out;
1090 smmu->debugfs_root = root;
1091
1092 for (i = 0; i < ARRAY_SIZE(smmu_debugfs_mc); i++) {
1093 int j;
1094 struct dentry *mc;
1095
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001096 mc = debugfs_create_dir(smmu_debugfs_mc[i], root);
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001097 if (!mc)
1098 goto err_out;
1099
1100 for (j = 0; j < ARRAY_SIZE(smmu_debugfs_cache); j++) {
1101 struct dentry *cache;
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001102 struct smmu_debugfs_info *info;
1103
1104 info = smmu->debugfs_info;
1105 info += i * ARRAY_SIZE(smmu_debugfs_mc) + j;
1106 info->smmu = smmu;
1107 info->mc = i;
1108 info->cache = j;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001109
1110 cache = debugfs_create_file(smmu_debugfs_cache[j],
1111 S_IWUGO | S_IRUGO, mc,
Hiroshi Doyu5a2c9372012-09-14 10:22:00 -06001112 (void *)info,
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001113 &smmu_debugfs_stats_fops);
1114 if (!cache)
1115 goto err_out;
1116 }
1117 }
1118
1119 return;
1120
1121err_out:
1122 smmu_debugfs_delete(smmu);
1123}
1124
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001125static int tegra_smmu_suspend(struct device *dev)
1126{
1127 struct smmu_device *smmu = dev_get_drvdata(dev);
1128
1129 smmu->translation_enable_0 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_0);
1130 smmu->translation_enable_1 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_1);
1131 smmu->translation_enable_2 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_2);
1132 smmu->asid_security = smmu_read(smmu, SMMU_ASID_SECURITY);
1133 return 0;
1134}
1135
1136static int tegra_smmu_resume(struct device *dev)
1137{
1138 struct smmu_device *smmu = dev_get_drvdata(dev);
1139 unsigned long flags;
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001140 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001141
1142 spin_lock_irqsave(&smmu->lock, flags);
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001143 err = smmu_setup_regs(smmu);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001144 spin_unlock_irqrestore(&smmu->lock, flags);
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001145 return err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001146}
1147
1148static int tegra_smmu_probe(struct platform_device *pdev)
1149{
1150 struct smmu_device *smmu;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001151 struct device *dev = &pdev->dev;
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001152 int i, asids, err = 0;
Hiroshi Doyuff763622012-06-25 14:23:58 +03001153 dma_addr_t uninitialized_var(base);
1154 size_t bytes, uninitialized_var(size);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001155
1156 if (smmu_handle)
1157 return -EIO;
1158
1159 BUILD_BUG_ON(PAGE_SHIFT != SMMU_PAGE_SHIFT);
1160
Hiroshi Doyua3b24912012-06-25 14:23:56 +03001161 if (of_property_read_u32(dev->of_node, "nvidia,#asids", &asids))
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001162 return -ENODEV;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001163
Hiroshi Doyua3b24912012-06-25 14:23:56 +03001164 bytes = sizeof(*smmu) + asids * sizeof(*smmu->as);
1165 smmu = devm_kzalloc(dev, bytes, GFP_KERNEL);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001166 if (!smmu) {
1167 dev_err(dev, "failed to allocate smmu_device\n");
1168 return -ENOMEM;
1169 }
1170
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001171 for (i = 0; i < ARRAY_SIZE(smmu->regs); i++) {
1172 struct resource *res;
1173
1174 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1175 if (!res)
1176 return -ENODEV;
1177 smmu->regs[i] = devm_request_and_ioremap(&pdev->dev, res);
1178 if (!smmu->regs[i])
1179 return -EBUSY;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001180 }
1181
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001182 err = of_get_dma_window(dev->of_node, NULL, 0, NULL, &base, &size);
1183 if (err)
1184 return -ENODEV;
1185
1186 if (size & SMMU_PAGE_MASK)
1187 return -EINVAL;
1188
1189 size >>= SMMU_PAGE_SHIFT;
1190 if (!size)
1191 return -EINVAL;
1192
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001193 smmu->ahb = of_parse_phandle(dev->of_node, "nvidia,ahb", 0);
1194 if (!smmu->ahb)
1195 return -ENODEV;
1196
1197 smmu->dev = dev;
1198 smmu->num_as = asids;
1199 smmu->iovmm_base = base;
1200 smmu->page_count = size;
1201
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001202 smmu->translation_enable_0 = ~0;
1203 smmu->translation_enable_1 = ~0;
1204 smmu->translation_enable_2 = ~0;
1205 smmu->asid_security = 0;
1206
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001207 for (i = 0; i < smmu->num_as; i++) {
1208 struct smmu_as *as = &smmu->as[i];
1209
1210 as->smmu = smmu;
1211 as->asid = i;
1212 as->pdir_attr = _PDIR_ATTR;
1213 as->pde_attr = _PDE_ATTR;
1214 as->pte_attr = _PTE_ATTR;
1215
1216 spin_lock_init(&as->lock);
1217 INIT_LIST_HEAD(&as->client);
1218 }
1219 spin_lock_init(&smmu->lock);
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001220 err = smmu_setup_regs(smmu);
1221 if (err)
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +03001222 return err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001223 platform_set_drvdata(pdev, smmu);
1224
1225 smmu->avp_vector_page = alloc_page(GFP_KERNEL);
1226 if (!smmu->avp_vector_page)
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +03001227 return -ENOMEM;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001228
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001229 smmu_debugfs_create(smmu);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001230 smmu_handle = smmu;
Hiroshi Doyuf1bda292012-11-28 15:52:55 +02001231 bus_set_iommu(&platform_bus_type, &smmu_iommu_ops);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001232 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001233}
1234
1235static int tegra_smmu_remove(struct platform_device *pdev)
1236{
1237 struct smmu_device *smmu = platform_get_drvdata(pdev);
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +03001238 int i;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001239
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +03001240 smmu_debugfs_delete(smmu);
1241
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001242 smmu_write(smmu, SMMU_CONFIG_DISABLE, SMMU_CONFIG);
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +03001243 for (i = 0; i < smmu->num_as; i++)
1244 free_pdir(&smmu->as[i]);
1245 __free_page(smmu->avp_vector_page);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001246 smmu_handle = NULL;
1247 return 0;
1248}
1249
1250const struct dev_pm_ops tegra_smmu_pm_ops = {
1251 .suspend = tegra_smmu_suspend,
1252 .resume = tegra_smmu_resume,
1253};
1254
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001255#ifdef CONFIG_OF
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001256static struct of_device_id tegra_smmu_of_match[] = {
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001257 { .compatible = "nvidia,tegra30-smmu", },
1258 { },
1259};
1260MODULE_DEVICE_TABLE(of, tegra_smmu_of_match);
1261#endif
1262
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001263static struct platform_driver tegra_smmu_driver = {
1264 .probe = tegra_smmu_probe,
1265 .remove = tegra_smmu_remove,
1266 .driver = {
1267 .owner = THIS_MODULE,
1268 .name = "tegra-smmu",
1269 .pm = &tegra_smmu_pm_ops,
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001270 .of_match_table = of_match_ptr(tegra_smmu_of_match),
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001271 },
1272};
1273
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08001274static int tegra_smmu_init(void)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001275{
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001276 return platform_driver_register(&tegra_smmu_driver);
1277}
1278
1279static void __exit tegra_smmu_exit(void)
1280{
1281 platform_driver_unregister(&tegra_smmu_driver);
1282}
1283
1284subsys_initcall(tegra_smmu_init);
1285module_exit(tegra_smmu_exit);
1286
1287MODULE_DESCRIPTION("IOMMU API for SMMU in Tegra30");
1288MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +03001289MODULE_ALIAS("platform:tegra-smmu");
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001290MODULE_LICENSE("GPL v2");