2 * IOMMU API for SMMU in Tegra30
4 * Copyright (c) 2011-2013, NVIDIA CORPORATION. All rights reserved.
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.
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
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.
20 #define pr_fmt(fmt) "%s(): " fmt, __func__
22 #include <linux/err.h>
23 #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>
29 #include <linux/pagemap.h>
30 #include <linux/device.h>
31 #include <linux/sched.h>
32 #include <linux/iommu.h>
35 #include <linux/of_iommu.h>
36 #include <linux/debugfs.h>
37 #include <linux/seq_file.h>
38 #include <linux/tegra-ahb.h>
41 #include <asm/cacheflush.h>
66 #define HWG_AFI (1 << HWGRP_AFI)
67 #define HWG_AVPC (1 << HWGRP_AVPC)
68 #define HWG_DC (1 << HWGRP_DC)
69 #define HWG_DCB (1 << HWGRP_DCB)
70 #define HWG_EPP (1 << HWGRP_EPP)
71 #define HWG_G2 (1 << HWGRP_G2)
72 #define HWG_HC (1 << HWGRP_HC)
73 #define HWG_HDA (1 << HWGRP_HDA)
74 #define HWG_ISP (1 << HWGRP_ISP)
75 #define HWG_MPE (1 << HWGRP_MPE)
76 #define HWG_NV (1 << HWGRP_NV)
77 #define HWG_NV2 (1 << HWGRP_NV2)
78 #define HWG_PPCS (1 << HWGRP_PPCS)
79 #define HWG_SATA (1 << HWGRP_SATA)
80 #define HWG_VDE (1 << HWGRP_VDE)
81 #define HWG_VI (1 << HWGRP_VI)
83 /* bitmap of the page sizes currently supported */
84 #define SMMU_IOMMU_PGSIZES (SZ_4K)
86 #define SMMU_CONFIG 0x10
87 #define SMMU_CONFIG_DISABLE 0
88 #define SMMU_CONFIG_ENABLE 1
90 /* REVISIT: To support multiple MCs */
100 #define SMMU_CACHE_CONFIG_BASE 0x14
101 #define __SMMU_CACHE_CONFIG(mc, cache) (SMMU_CACHE_CONFIG_BASE + 4 * cache)
102 #define SMMU_CACHE_CONFIG(cache) __SMMU_CACHE_CONFIG(_MC, cache)
104 #define SMMU_CACHE_CONFIG_STATS_SHIFT 31
105 #define SMMU_CACHE_CONFIG_STATS_ENABLE (1 << SMMU_CACHE_CONFIG_STATS_SHIFT)
106 #define SMMU_CACHE_CONFIG_STATS_TEST_SHIFT 30
107 #define SMMU_CACHE_CONFIG_STATS_TEST (1 << SMMU_CACHE_CONFIG_STATS_TEST_SHIFT)
109 #define SMMU_TLB_CONFIG_HIT_UNDER_MISS__ENABLE (1 << 29)
110 #define SMMU_TLB_CONFIG_ACTIVE_LINES__VALUE 0x10
111 #define SMMU_TLB_CONFIG_RESET_VAL 0x20000010
113 #define SMMU_PTC_CONFIG_CACHE__ENABLE (1 << 29)
114 #define SMMU_PTC_CONFIG_INDEX_MAP__PATTERN 0x3f
115 #define SMMU_PTC_CONFIG_RESET_VAL 0x2000003f
117 #define SMMU_PTB_ASID 0x1c
118 #define SMMU_PTB_ASID_CURRENT_SHIFT 0
120 #define SMMU_PTB_DATA 0x20
121 #define SMMU_PTB_DATA_RESET_VAL 0
122 #define SMMU_PTB_DATA_ASID_NONSECURE_SHIFT 29
123 #define SMMU_PTB_DATA_ASID_WRITABLE_SHIFT 30
124 #define SMMU_PTB_DATA_ASID_READABLE_SHIFT 31
126 #define SMMU_TLB_FLUSH 0x30
127 #define SMMU_TLB_FLUSH_VA_MATCH_ALL 0
128 #define SMMU_TLB_FLUSH_VA_MATCH_SECTION 2
129 #define SMMU_TLB_FLUSH_VA_MATCH_GROUP 3
130 #define SMMU_TLB_FLUSH_ASID_SHIFT 29
131 #define SMMU_TLB_FLUSH_ASID_MATCH_DISABLE 0
132 #define SMMU_TLB_FLUSH_ASID_MATCH_ENABLE 1
133 #define SMMU_TLB_FLUSH_ASID_MATCH_SHIFT 31
135 #define SMMU_PTC_FLUSH 0x34
136 #define SMMU_PTC_FLUSH_TYPE_ALL 0
137 #define SMMU_PTC_FLUSH_TYPE_ADR 1
138 #define SMMU_PTC_FLUSH_ADR_SHIFT 4
140 #define SMMU_ASID_SECURITY 0x38
142 #define SMMU_STATS_CACHE_COUNT_BASE 0x1f0
144 #define SMMU_STATS_CACHE_COUNT(mc, cache, hitmiss) \
145 (SMMU_STATS_CACHE_COUNT_BASE + 8 * cache + 4 * hitmiss)
147 #define SMMU_TRANSLATION_ENABLE_0 0x228
148 #define SMMU_TRANSLATION_ENABLE_1 0x22c
149 #define SMMU_TRANSLATION_ENABLE_2 0x230
151 #define SMMU_AFI_ASID 0x238 /* PCIE */
152 #define SMMU_AVPC_ASID 0x23c /* AVP */
153 #define SMMU_DC_ASID 0x240 /* Display controller */
154 #define SMMU_DCB_ASID 0x244 /* Display controller B */
155 #define SMMU_EPP_ASID 0x248 /* Encoder pre-processor */
156 #define SMMU_G2_ASID 0x24c /* 2D engine */
157 #define SMMU_HC_ASID 0x250 /* Host1x */
158 #define SMMU_HDA_ASID 0x254 /* High-def audio */
159 #define SMMU_ISP_ASID 0x258 /* Image signal processor */
160 #define SMMU_MPE_ASID 0x264 /* MPEG encoder */
161 #define SMMU_NV_ASID 0x268 /* (3D) */
162 #define SMMU_NV2_ASID 0x26c /* (3D) */
163 #define SMMU_PPCS_ASID 0x270 /* AHB */
164 #define SMMU_SATA_ASID 0x278 /* SATA */
165 #define SMMU_VDE_ASID 0x27c /* Video decoder */
166 #define SMMU_VI_ASID 0x280 /* Video input */
168 #define SMMU_PDE_NEXT_SHIFT 28
170 #define SMMU_TLB_FLUSH_VA_SECTION__MASK 0xffc00000
171 #define SMMU_TLB_FLUSH_VA_SECTION__SHIFT 12 /* right shift */
172 #define SMMU_TLB_FLUSH_VA_GROUP__MASK 0xffffc000
173 #define SMMU_TLB_FLUSH_VA_GROUP__SHIFT 12 /* right shift */
174 #define SMMU_TLB_FLUSH_VA(iova, which) \
175 ((((iova) & SMMU_TLB_FLUSH_VA_##which##__MASK) >> \
176 SMMU_TLB_FLUSH_VA_##which##__SHIFT) | \
177 SMMU_TLB_FLUSH_VA_MATCH_##which)
178 #define SMMU_PTB_ASID_CUR(n) \
179 ((n) << SMMU_PTB_ASID_CURRENT_SHIFT)
180 #define SMMU_TLB_FLUSH_ASID_MATCH_disable \
181 (SMMU_TLB_FLUSH_ASID_MATCH_DISABLE << \
182 SMMU_TLB_FLUSH_ASID_MATCH_SHIFT)
183 #define SMMU_TLB_FLUSH_ASID_MATCH__ENABLE \
184 (SMMU_TLB_FLUSH_ASID_MATCH_ENABLE << \
185 SMMU_TLB_FLUSH_ASID_MATCH_SHIFT)
187 #define SMMU_PAGE_SHIFT 12
188 #define SMMU_PAGE_SIZE (1 << SMMU_PAGE_SHIFT)
189 #define SMMU_PAGE_MASK ((1 << SMMU_PAGE_SHIFT) - 1)
191 #define SMMU_PDIR_COUNT 1024
192 #define SMMU_PDIR_SIZE (sizeof(unsigned long) * SMMU_PDIR_COUNT)
193 #define SMMU_PTBL_COUNT 1024
194 #define SMMU_PTBL_SIZE (sizeof(unsigned long) * SMMU_PTBL_COUNT)
195 #define SMMU_PDIR_SHIFT 12
196 #define SMMU_PDE_SHIFT 12
197 #define SMMU_PTE_SHIFT 12
198 #define SMMU_PFN_MASK 0x000fffff
200 #define SMMU_ADDR_TO_PFN(addr) ((addr) >> 12)
201 #define SMMU_ADDR_TO_PDN(addr) ((addr) >> 22)
202 #define SMMU_PDN_TO_ADDR(pdn) ((pdn) << 22)
204 #define _READABLE (1 << SMMU_PTB_DATA_ASID_READABLE_SHIFT)
205 #define _WRITABLE (1 << SMMU_PTB_DATA_ASID_WRITABLE_SHIFT)
206 #define _NONSECURE (1 << SMMU_PTB_DATA_ASID_NONSECURE_SHIFT)
207 #define _PDE_NEXT (1 << SMMU_PDE_NEXT_SHIFT)
208 #define _MASK_ATTR (_READABLE | _WRITABLE | _NONSECURE)
210 #define _PDIR_ATTR (_READABLE | _WRITABLE | _NONSECURE)
212 #define _PDE_ATTR (_READABLE | _WRITABLE | _NONSECURE)
213 #define _PDE_ATTR_N (_PDE_ATTR | _PDE_NEXT)
214 #define _PDE_VACANT(pdn) (((pdn) << 10) | _PDE_ATTR)
216 #define _PTE_ATTR (_READABLE | _WRITABLE | _NONSECURE)
217 #define _PTE_VACANT(addr) (((addr) >> SMMU_PAGE_SHIFT) | _PTE_ATTR)
219 #define SMMU_MK_PDIR(page, attr) \
220 ((page_to_phys(page) >> SMMU_PDIR_SHIFT) | (attr))
221 #define SMMU_MK_PDE(page, attr) \
222 (unsigned long)((page_to_phys(page) >> SMMU_PDE_SHIFT) | (attr))
223 #define SMMU_EX_PTBL_PAGE(pde) \
224 pfn_to_page((unsigned long)(pde) & SMMU_PFN_MASK)
225 #define SMMU_PFN_TO_PTE(pfn, attr) (unsigned long)((pfn) | (attr))
227 #define SMMU_ASID_ENABLE(asid) ((asid) | (1 << 31))
228 #define SMMU_ASID_DISABLE 0
229 #define SMMU_ASID_ASID(n) ((n) & ~SMMU_ASID_ENABLE(0))
231 #define NUM_SMMU_REG_BANKS 3
233 #define smmu_client_enable_hwgrp(c, m) smmu_client_set_hwgrp(c, m, 1)
234 #define smmu_client_disable_hwgrp(c) smmu_client_set_hwgrp(c, 0, 0)
235 #define __smmu_client_enable_hwgrp(c, m) __smmu_client_set_hwgrp(c, m, 1)
236 #define __smmu_client_disable_hwgrp(c) __smmu_client_set_hwgrp(c, 0, 0)
238 #define HWGRP_INIT(client) [HWGRP_##client] = SMMU_##client##_ASID
240 static const u32 smmu_hwgrp_asid_reg[] = {
258 #define HWGRP_ASID_REG(x) (smmu_hwgrp_asid_reg[x])
261 * Per client for address space
265 struct list_head list;
274 struct smmu_device *smmu; /* back pointer to container */
276 spinlock_t lock; /* for pagetable */
277 struct page *pdir_page;
278 unsigned long pdir_attr;
279 unsigned long pde_attr;
280 unsigned long pte_attr;
281 unsigned int *pte_count;
283 struct list_head client;
284 spinlock_t client_lock; /* for client list */
287 struct smmu_debugfs_info {
288 struct smmu_device *smmu;
294 * Per SMMU device - IOMMU device
297 void __iomem *regbase; /* register offset base */
298 void __iomem **regs; /* register block start address array */
299 void __iomem **rege; /* register block end address array */
300 int nregs; /* number of register blocks */
302 unsigned long iovmm_base; /* remappable base address */
303 unsigned long page_count; /* total remappable size */
307 struct page *avp_vector_page; /* dummy page shared by all AS's */
310 * Register image savers for suspend/resume
312 unsigned long translation_enable_0;
313 unsigned long translation_enable_1;
314 unsigned long translation_enable_2;
315 unsigned long asid_security;
317 struct dentry *debugfs_root;
318 struct smmu_debugfs_info *debugfs_info;
320 struct device_node *ahb;
323 struct smmu_as as[0]; /* Run-time allocated array */
326 static struct smmu_device *smmu_handle; /* unique for a system */
329 * SMMU register accessors
331 static bool inline smmu_valid_reg(struct smmu_device *smmu,
336 for (i = 0; i < smmu->nregs; i++) {
337 if (addr < smmu->regs[i])
339 if (addr <= smmu->rege[i])
346 static inline u32 smmu_read(struct smmu_device *smmu, size_t offs)
348 void __iomem *addr = smmu->regbase + offs;
350 BUG_ON(!smmu_valid_reg(smmu, addr));
355 static inline void smmu_write(struct smmu_device *smmu, u32 val, size_t offs)
357 void __iomem *addr = smmu->regbase + offs;
359 BUG_ON(!smmu_valid_reg(smmu, addr));
364 #define VA_PAGE_TO_PA(va, page) \
365 (page_to_phys(page) + ((unsigned long)(va) & ~PAGE_MASK))
367 #define FLUSH_CPU_DCACHE(va, page, size) \
369 unsigned long _pa_ = VA_PAGE_TO_PA(va, page); \
370 __cpuc_flush_dcache_area((void *)(va), (size_t)(size)); \
371 outer_flush_range(_pa_, _pa_+(size_t)(size)); \
375 * Any interaction between any block on PPSB and a block on APB or AHB
376 * must have these read-back barriers to ensure the APB/AHB bus
377 * transaction is complete before initiating activity on the PPSB
380 #define FLUSH_SMMU_REGS(smmu) smmu_read(smmu, SMMU_CONFIG)
382 #define smmu_client_hwgrp(c) (u32)((c)->dev->platform_data)
384 static int __smmu_client_set_hwgrp(struct smmu_client *c,
385 unsigned long map, int on)
388 struct smmu_as *as = c->as;
389 u32 val, offs, mask = SMMU_ASID_ENABLE(as->asid);
390 struct smmu_device *smmu = as->smmu;
396 map = smmu_client_hwgrp(c);
398 for_each_set_bit(i, &map, HWGRP_COUNT) {
399 offs = HWGRP_ASID_REG(i);
400 val = smmu_read(smmu, offs);
402 if (WARN_ON(val & mask))
406 WARN_ON((val & mask) == mask);
409 smmu_write(smmu, val, offs);
411 FLUSH_SMMU_REGS(smmu);
416 for_each_set_bit(i, &map, HWGRP_COUNT) {
417 offs = HWGRP_ASID_REG(i);
418 val = smmu_read(smmu, offs);
420 smmu_write(smmu, val, offs);
425 static int smmu_client_set_hwgrp(struct smmu_client *c, u32 map, int on)
429 struct smmu_as *as = c->as;
430 struct smmu_device *smmu = as->smmu;
432 spin_lock_irqsave(&smmu->lock, flags);
433 val = __smmu_client_set_hwgrp(c, map, on);
434 spin_unlock_irqrestore(&smmu->lock, flags);
439 * Flush all TLB entries and all PTC entries
440 * Caller must lock smmu
442 static void smmu_flush_regs(struct smmu_device *smmu, int enable)
446 smmu_write(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
447 FLUSH_SMMU_REGS(smmu);
448 val = SMMU_TLB_FLUSH_VA_MATCH_ALL |
449 SMMU_TLB_FLUSH_ASID_MATCH_disable;
450 smmu_write(smmu, val, SMMU_TLB_FLUSH);
453 smmu_write(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
454 FLUSH_SMMU_REGS(smmu);
457 static int smmu_setup_regs(struct smmu_device *smmu)
462 for (i = 0; i < smmu->num_as; i++) {
463 struct smmu_as *as = &smmu->as[i];
464 struct smmu_client *c;
466 smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
467 val = as->pdir_page ?
468 SMMU_MK_PDIR(as->pdir_page, as->pdir_attr) :
469 SMMU_PTB_DATA_RESET_VAL;
470 smmu_write(smmu, val, SMMU_PTB_DATA);
472 list_for_each_entry(c, &as->client, list)
473 __smmu_client_set_hwgrp(c, c->hwgrp, 1);
476 smmu_write(smmu, smmu->translation_enable_0, SMMU_TRANSLATION_ENABLE_0);
477 smmu_write(smmu, smmu->translation_enable_1, SMMU_TRANSLATION_ENABLE_1);
478 smmu_write(smmu, smmu->translation_enable_2, SMMU_TRANSLATION_ENABLE_2);
479 smmu_write(smmu, smmu->asid_security, SMMU_ASID_SECURITY);
480 smmu_write(smmu, SMMU_TLB_CONFIG_RESET_VAL, SMMU_CACHE_CONFIG(_TLB));
481 smmu_write(smmu, SMMU_PTC_CONFIG_RESET_VAL, SMMU_CACHE_CONFIG(_PTC));
483 smmu_flush_regs(smmu, 1);
485 return tegra_ahb_enable_smmu(smmu->ahb);
488 static void flush_ptc_and_tlb(struct smmu_device *smmu,
489 struct smmu_as *as, dma_addr_t iova,
490 unsigned long *pte, struct page *page, int is_pde)
493 unsigned long tlb_flush_va = is_pde
494 ? SMMU_TLB_FLUSH_VA(iova, SECTION)
495 : SMMU_TLB_FLUSH_VA(iova, GROUP);
497 val = SMMU_PTC_FLUSH_TYPE_ADR | VA_PAGE_TO_PA(pte, page);
498 smmu_write(smmu, val, SMMU_PTC_FLUSH);
499 FLUSH_SMMU_REGS(smmu);
501 SMMU_TLB_FLUSH_ASID_MATCH__ENABLE |
502 (as->asid << SMMU_TLB_FLUSH_ASID_SHIFT);
503 smmu_write(smmu, val, SMMU_TLB_FLUSH);
504 FLUSH_SMMU_REGS(smmu);
507 static void free_ptbl(struct smmu_as *as, dma_addr_t iova)
509 unsigned long pdn = SMMU_ADDR_TO_PDN(iova);
510 unsigned long *pdir = (unsigned long *)page_address(as->pdir_page);
512 if (pdir[pdn] != _PDE_VACANT(pdn)) {
513 dev_dbg(as->smmu->dev, "pdn: %lx\n", pdn);
515 ClearPageReserved(SMMU_EX_PTBL_PAGE(pdir[pdn]));
516 __free_page(SMMU_EX_PTBL_PAGE(pdir[pdn]));
517 pdir[pdn] = _PDE_VACANT(pdn);
518 FLUSH_CPU_DCACHE(&pdir[pdn], as->pdir_page, sizeof pdir[pdn]);
519 flush_ptc_and_tlb(as->smmu, as, iova, &pdir[pdn],
524 static void free_pdir(struct smmu_as *as)
528 struct device *dev = as->smmu->dev;
533 addr = as->smmu->iovmm_base;
534 count = as->smmu->page_count;
535 while (count-- > 0) {
537 addr += SMMU_PAGE_SIZE * SMMU_PTBL_COUNT;
539 ClearPageReserved(as->pdir_page);
540 __free_page(as->pdir_page);
541 as->pdir_page = NULL;
542 devm_kfree(dev, as->pte_count);
543 as->pte_count = NULL;
547 * Maps PTBL for given iova and returns the PTE address
548 * Caller must unmap the mapped PTBL returned in *ptbl_page_p
550 static unsigned long *locate_pte(struct smmu_as *as,
551 dma_addr_t iova, bool allocate,
552 struct page **ptbl_page_p,
553 unsigned int **count)
555 unsigned long ptn = SMMU_ADDR_TO_PFN(iova);
556 unsigned long pdn = SMMU_ADDR_TO_PDN(iova);
557 unsigned long *pdir = page_address(as->pdir_page);
560 if (pdir[pdn] != _PDE_VACANT(pdn)) {
561 /* Mapped entry table already exists */
562 *ptbl_page_p = SMMU_EX_PTBL_PAGE(pdir[pdn]);
563 ptbl = page_address(*ptbl_page_p);
564 } else if (!allocate) {
568 unsigned long addr = SMMU_PDN_TO_ADDR(pdn);
570 /* Vacant - allocate a new page table */
571 dev_dbg(as->smmu->dev, "New PTBL pdn: %lx\n", pdn);
573 *ptbl_page_p = alloc_page(GFP_ATOMIC);
575 dev_err(as->smmu->dev,
576 "failed to allocate smmu_device page table\n");
579 SetPageReserved(*ptbl_page_p);
580 ptbl = (unsigned long *)page_address(*ptbl_page_p);
581 for (pn = 0; pn < SMMU_PTBL_COUNT;
582 pn++, addr += SMMU_PAGE_SIZE) {
583 ptbl[pn] = _PTE_VACANT(addr);
585 FLUSH_CPU_DCACHE(ptbl, *ptbl_page_p, SMMU_PTBL_SIZE);
586 pdir[pdn] = SMMU_MK_PDE(*ptbl_page_p,
587 as->pde_attr | _PDE_NEXT);
588 FLUSH_CPU_DCACHE(&pdir[pdn], as->pdir_page, sizeof pdir[pdn]);
589 flush_ptc_and_tlb(as->smmu, as, iova, &pdir[pdn],
592 *count = &as->pte_count[pdn];
594 return &ptbl[ptn % SMMU_PTBL_COUNT];
597 #ifdef CONFIG_SMMU_SIG_DEBUG
598 static void put_signature(struct smmu_as *as,
599 dma_addr_t iova, unsigned long pfn)
602 unsigned long *vaddr;
604 page = pfn_to_page(pfn);
605 vaddr = page_address(page);
610 vaddr[1] = pfn << PAGE_SHIFT;
611 FLUSH_CPU_DCACHE(vaddr, page, sizeof(vaddr[0]) * 2);
614 static inline void put_signature(struct smmu_as *as,
615 unsigned long addr, unsigned long pfn)
621 * Caller must not hold as->lock
623 static int alloc_pdir(struct smmu_as *as)
625 unsigned long *pdir, flags;
628 struct smmu_device *smmu = as->smmu;
633 * do the allocation, then grab as->lock
635 cnt = devm_kzalloc(smmu->dev,
636 sizeof(cnt[0]) * SMMU_PDIR_COUNT,
638 page = alloc_page(GFP_KERNEL | __GFP_DMA);
640 spin_lock_irqsave(&as->lock, flags);
643 /* We raced, free the redundant */
649 dev_err(smmu->dev, "failed to allocate at %s\n", __func__);
654 as->pdir_page = page;
657 SetPageReserved(as->pdir_page);
658 pdir = page_address(as->pdir_page);
660 for (pdn = 0; pdn < SMMU_PDIR_COUNT; pdn++)
661 pdir[pdn] = _PDE_VACANT(pdn);
662 FLUSH_CPU_DCACHE(pdir, as->pdir_page, SMMU_PDIR_SIZE);
663 val = SMMU_PTC_FLUSH_TYPE_ADR | VA_PAGE_TO_PA(pdir, as->pdir_page);
664 smmu_write(smmu, val, SMMU_PTC_FLUSH);
665 FLUSH_SMMU_REGS(as->smmu);
666 val = SMMU_TLB_FLUSH_VA_MATCH_ALL |
667 SMMU_TLB_FLUSH_ASID_MATCH__ENABLE |
668 (as->asid << SMMU_TLB_FLUSH_ASID_SHIFT);
669 smmu_write(smmu, val, SMMU_TLB_FLUSH);
670 FLUSH_SMMU_REGS(as->smmu);
672 spin_unlock_irqrestore(&as->lock, flags);
677 spin_unlock_irqrestore(&as->lock, flags);
679 devm_kfree(smmu->dev, cnt);
685 static void __smmu_iommu_unmap(struct smmu_as *as, dma_addr_t iova)
691 pte = locate_pte(as, iova, false, &page, &count);
695 if (WARN_ON(*pte == _PTE_VACANT(iova)))
698 *pte = _PTE_VACANT(iova);
699 FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
700 flush_ptc_and_tlb(as->smmu, as, iova, pte, page, 0);
705 static void __smmu_iommu_map_pfn(struct smmu_as *as, dma_addr_t iova,
708 struct smmu_device *smmu = as->smmu;
713 pte = locate_pte(as, iova, true, &page, &count);
717 if (*pte == _PTE_VACANT(iova))
719 *pte = SMMU_PFN_TO_PTE(pfn, as->pte_attr);
720 if (unlikely((*pte == _PTE_VACANT(iova))))
722 FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
723 flush_ptc_and_tlb(smmu, as, iova, pte, page, 0);
724 put_signature(as, iova, pfn);
727 static int smmu_iommu_map(struct iommu_domain *domain, unsigned long iova,
728 phys_addr_t pa, size_t bytes, int prot)
730 struct smmu_as *as = domain->priv;
731 unsigned long pfn = __phys_to_pfn(pa);
734 dev_dbg(as->smmu->dev, "[%d] %08lx:%pa\n", as->asid, iova, &pa);
739 spin_lock_irqsave(&as->lock, flags);
740 __smmu_iommu_map_pfn(as, iova, pfn);
741 spin_unlock_irqrestore(&as->lock, flags);
745 static size_t smmu_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
748 struct smmu_as *as = domain->priv;
751 dev_dbg(as->smmu->dev, "[%d] %08lx\n", as->asid, iova);
753 spin_lock_irqsave(&as->lock, flags);
754 __smmu_iommu_unmap(as, iova);
755 spin_unlock_irqrestore(&as->lock, flags);
756 return SMMU_PAGE_SIZE;
759 static phys_addr_t smmu_iommu_iova_to_phys(struct iommu_domain *domain,
762 struct smmu_as *as = domain->priv;
769 spin_lock_irqsave(&as->lock, flags);
771 pte = locate_pte(as, iova, true, &page, &count);
772 pfn = *pte & SMMU_PFN_MASK;
773 WARN_ON(!pfn_valid(pfn));
774 dev_dbg(as->smmu->dev,
775 "iova:%08llx pfn:%08lx asid:%d\n", (unsigned long long)iova,
778 spin_unlock_irqrestore(&as->lock, flags);
779 return PFN_PHYS(pfn);
782 static int smmu_iommu_domain_has_cap(struct iommu_domain *domain,
788 static int smmu_iommu_attach_dev(struct iommu_domain *domain,
791 struct smmu_as *as = domain->priv;
792 struct smmu_device *smmu = as->smmu;
793 struct smmu_client *client, *c;
797 client = devm_kzalloc(smmu->dev, sizeof(*c), GFP_KERNEL);
802 map = (unsigned long)dev->platform_data;
806 err = smmu_client_enable_hwgrp(client, map);
810 spin_lock(&as->client_lock);
811 list_for_each_entry(c, &as->client, list) {
814 "%s is already attached\n", dev_name(c->dev));
819 list_add(&client->list, &as->client);
820 spin_unlock(&as->client_lock);
823 * Reserve "page zero" for AVP vectors using a common dummy
826 if (map & HWG_AVPC) {
829 page = as->smmu->avp_vector_page;
830 __smmu_iommu_map_pfn(as, 0, page_to_pfn(page));
832 pr_info("Reserve \"page zero\" for AVP vectors using a common dummy\n");
835 dev_dbg(smmu->dev, "%s is attached\n", dev_name(dev));
839 smmu_client_disable_hwgrp(client);
840 spin_unlock(&as->client_lock);
842 devm_kfree(smmu->dev, client);
846 static void smmu_iommu_detach_dev(struct iommu_domain *domain,
849 struct smmu_as *as = domain->priv;
850 struct smmu_device *smmu = as->smmu;
851 struct smmu_client *c;
853 spin_lock(&as->client_lock);
855 list_for_each_entry(c, &as->client, list) {
857 smmu_client_disable_hwgrp(c);
859 devm_kfree(smmu->dev, c);
862 "%s is detached\n", dev_name(c->dev));
866 dev_err(smmu->dev, "Couldn't find %s\n", dev_name(dev));
868 spin_unlock(&as->client_lock);
871 static int smmu_iommu_domain_init(struct iommu_domain *domain)
873 int i, err = -EAGAIN;
876 struct smmu_device *smmu = smmu_handle;
878 /* Look for a free AS with lock held */
879 for (i = 0; i < smmu->num_as; i++) {
885 err = alloc_pdir(as);
892 if (i == smmu->num_as)
893 dev_err(smmu->dev, "no free AS\n");
897 spin_lock_irqsave(&smmu->lock, flags);
899 /* Update PDIR register */
900 smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
902 SMMU_MK_PDIR(as->pdir_page, as->pdir_attr), SMMU_PTB_DATA);
903 FLUSH_SMMU_REGS(smmu);
905 spin_unlock_irqrestore(&smmu->lock, flags);
909 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;
914 dev_dbg(smmu->dev, "smmu_as@%p\n", as);
919 static void smmu_iommu_domain_destroy(struct iommu_domain *domain)
921 struct smmu_as *as = domain->priv;
922 struct smmu_device *smmu = as->smmu;
925 spin_lock_irqsave(&as->lock, flags);
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);
937 if (!list_empty(&as->client)) {
938 struct smmu_client *c;
940 list_for_each_entry(c, &as->client, list)
941 smmu_iommu_detach_dev(domain, c->dev);
944 spin_unlock_irqrestore(&as->lock, flags);
947 dev_dbg(smmu->dev, "smmu_as@%p\n", as);
950 static struct iommu_ops smmu_iommu_ops = {
951 .domain_init = smmu_iommu_domain_init,
952 .domain_destroy = smmu_iommu_domain_destroy,
953 .attach_dev = smmu_iommu_attach_dev,
954 .detach_dev = smmu_iommu_detach_dev,
955 .map = smmu_iommu_map,
956 .unmap = smmu_iommu_unmap,
957 .iova_to_phys = smmu_iommu_iova_to_phys,
958 .domain_has_cap = smmu_iommu_domain_has_cap,
959 .pgsize_bitmap = SMMU_IOMMU_PGSIZES,
962 /* Should be in the order of enum */
963 static const char * const smmu_debugfs_mc[] = { "mc", };
964 static const char * const smmu_debugfs_cache[] = { "tlb", "ptc", };
966 static ssize_t smmu_debugfs_stats_write(struct file *file,
967 const char __user *buffer,
968 size_t count, loff_t *pos)
970 struct smmu_debugfs_info *info;
971 struct smmu_device *smmu;
978 const char * const command[] = {
983 char str[] = "reset";
987 count = min_t(size_t, count, sizeof(str));
988 if (copy_from_user(str, buffer, count))
991 for (i = 0; i < ARRAY_SIZE(command); i++)
992 if (strncmp(str, command[i],
993 strlen(command[i])) == 0)
996 if (i == ARRAY_SIZE(command))
999 info = file_inode(file)->i_private;
1002 offs = SMMU_CACHE_CONFIG(info->cache);
1003 val = smmu_read(smmu, offs);
1006 val &= ~SMMU_CACHE_CONFIG_STATS_ENABLE;
1007 val &= ~SMMU_CACHE_CONFIG_STATS_TEST;
1008 smmu_write(smmu, val, offs);
1011 val |= SMMU_CACHE_CONFIG_STATS_ENABLE;
1012 val &= ~SMMU_CACHE_CONFIG_STATS_TEST;
1013 smmu_write(smmu, val, offs);
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);
1026 dev_dbg(smmu->dev, "%s() %08x, %08x @%08x\n", __func__,
1027 val, smmu_read(smmu, offs), offs);
1032 static int smmu_debugfs_stats_show(struct seq_file *s, void *v)
1034 struct smmu_debugfs_info *info = s->private;
1035 struct smmu_device *smmu = info->smmu;
1037 const char * const stats[] = { "hit", "miss", };
1040 for (i = 0; i < ARRAY_SIZE(stats); i++) {
1044 offs = SMMU_STATS_CACHE_COUNT(info->mc, info->cache, i);
1045 val = smmu_read(smmu, offs);
1046 seq_printf(s, "%s:%08x ", stats[i], val);
1048 dev_dbg(smmu->dev, "%s() %s %08x @%08x\n", __func__,
1049 stats[i], val, offs);
1051 seq_printf(s, "\n");
1055 static int smmu_debugfs_stats_open(struct inode *inode, struct file *file)
1057 return single_open(file, smmu_debugfs_stats_show, inode->i_private);
1060 static const struct file_operations smmu_debugfs_stats_fops = {
1061 .open = smmu_debugfs_stats_open,
1063 .llseek = seq_lseek,
1064 .release = single_release,
1065 .write = smmu_debugfs_stats_write,
1068 static void smmu_debugfs_delete(struct smmu_device *smmu)
1070 debugfs_remove_recursive(smmu->debugfs_root);
1071 kfree(smmu->debugfs_info);
1074 static void smmu_debugfs_create(struct smmu_device *smmu)
1078 struct dentry *root;
1080 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)
1086 root = debugfs_create_dir(dev_name(smmu->dev), NULL);
1089 smmu->debugfs_root = root;
1091 for (i = 0; i < ARRAY_SIZE(smmu_debugfs_mc); i++) {
1095 mc = debugfs_create_dir(smmu_debugfs_mc[i], root);
1099 for (j = 0; j < ARRAY_SIZE(smmu_debugfs_cache); j++) {
1100 struct dentry *cache;
1101 struct smmu_debugfs_info *info;
1103 info = smmu->debugfs_info;
1104 info += i * ARRAY_SIZE(smmu_debugfs_mc) + j;
1109 cache = debugfs_create_file(smmu_debugfs_cache[j],
1110 S_IWUGO | S_IRUGO, mc,
1112 &smmu_debugfs_stats_fops);
1121 smmu_debugfs_delete(smmu);
1124 static int tegra_smmu_suspend(struct device *dev)
1126 struct smmu_device *smmu = dev_get_drvdata(dev);
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);
1135 static int tegra_smmu_resume(struct device *dev)
1137 struct smmu_device *smmu = dev_get_drvdata(dev);
1138 unsigned long flags;
1141 spin_lock_irqsave(&smmu->lock, flags);
1142 err = smmu_setup_regs(smmu);
1143 spin_unlock_irqrestore(&smmu->lock, flags);
1147 static int tegra_smmu_probe(struct platform_device *pdev)
1149 struct smmu_device *smmu;
1150 struct device *dev = &pdev->dev;
1151 int i, asids, err = 0;
1152 dma_addr_t uninitialized_var(base);
1153 size_t bytes, uninitialized_var(size);
1158 BUILD_BUG_ON(PAGE_SHIFT != SMMU_PAGE_SHIFT);
1160 if (of_property_read_u32(dev->of_node, "nvidia,#asids", &asids))
1163 bytes = sizeof(*smmu) + asids * sizeof(*smmu->as);
1164 smmu = devm_kzalloc(dev, bytes, GFP_KERNEL);
1166 dev_err(dev, "failed to allocate smmu_device\n");
1170 smmu->nregs = pdev->num_resources;
1171 smmu->regs = devm_kzalloc(dev, 2 * smmu->nregs * sizeof(*smmu->regs),
1173 smmu->rege = smmu->regs + smmu->nregs;
1176 for (i = 0; i < smmu->nregs; i++) {
1177 struct resource *res;
1179 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1180 smmu->regs[i] = devm_ioremap_resource(&pdev->dev, res);
1181 if (IS_ERR(smmu->regs[i]))
1182 return PTR_ERR(smmu->regs[i]);
1183 smmu->rege[i] = smmu->regs[i] + resource_size(res) - 1;
1185 /* Same as "mc" 1st regiter block start address */
1186 smmu->regbase = (void __iomem *)((u32)smmu->regs[0] & PAGE_MASK);
1188 err = of_get_dma_window(dev->of_node, NULL, 0, NULL, &base, &size);
1192 if (size & SMMU_PAGE_MASK)
1195 size >>= SMMU_PAGE_SHIFT;
1199 smmu->ahb = of_parse_phandle(dev->of_node, "nvidia,ahb", 0);
1204 smmu->num_as = asids;
1205 smmu->iovmm_base = base;
1206 smmu->page_count = size;
1208 smmu->translation_enable_0 = ~0;
1209 smmu->translation_enable_1 = ~0;
1210 smmu->translation_enable_2 = ~0;
1211 smmu->asid_security = 0;
1213 for (i = 0; i < smmu->num_as; i++) {
1214 struct smmu_as *as = &smmu->as[i];
1218 as->pdir_attr = _PDIR_ATTR;
1219 as->pde_attr = _PDE_ATTR;
1220 as->pte_attr = _PTE_ATTR;
1222 spin_lock_init(&as->lock);
1223 spin_lock_init(&as->client_lock);
1224 INIT_LIST_HEAD(&as->client);
1226 spin_lock_init(&smmu->lock);
1227 err = smmu_setup_regs(smmu);
1230 platform_set_drvdata(pdev, smmu);
1232 smmu->avp_vector_page = alloc_page(GFP_KERNEL);
1233 if (!smmu->avp_vector_page)
1236 smmu_debugfs_create(smmu);
1238 bus_set_iommu(&platform_bus_type, &smmu_iommu_ops);
1242 static int tegra_smmu_remove(struct platform_device *pdev)
1244 struct smmu_device *smmu = platform_get_drvdata(pdev);
1247 smmu_debugfs_delete(smmu);
1249 smmu_write(smmu, SMMU_CONFIG_DISABLE, SMMU_CONFIG);
1250 for (i = 0; i < smmu->num_as; i++)
1251 free_pdir(&smmu->as[i]);
1252 __free_page(smmu->avp_vector_page);
1257 static const struct dev_pm_ops tegra_smmu_pm_ops = {
1258 .suspend = tegra_smmu_suspend,
1259 .resume = tegra_smmu_resume,
1262 static struct of_device_id tegra_smmu_of_match[] = {
1263 { .compatible = "nvidia,tegra30-smmu", },
1266 MODULE_DEVICE_TABLE(of, tegra_smmu_of_match);
1268 static struct platform_driver tegra_smmu_driver = {
1269 .probe = tegra_smmu_probe,
1270 .remove = tegra_smmu_remove,
1272 .owner = THIS_MODULE,
1273 .name = "tegra-smmu",
1274 .pm = &tegra_smmu_pm_ops,
1275 .of_match_table = tegra_smmu_of_match,
1279 static int tegra_smmu_init(void)
1281 return platform_driver_register(&tegra_smmu_driver);
1284 static void __exit tegra_smmu_exit(void)
1286 platform_driver_unregister(&tegra_smmu_driver);
1289 subsys_initcall(tegra_smmu_init);
1290 module_exit(tegra_smmu_exit);
1292 MODULE_DESCRIPTION("IOMMU API for SMMU in Tegra30");
1293 MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
1294 MODULE_ALIAS("platform:tegra-smmu");
1295 MODULE_LICENSE("GPL v2");