2 * IOMMU API for SMMU in Tegra30
4 * Copyright (c) 2011-2012, 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/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/spinlock.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
28 #include <linux/pagemap.h>
29 #include <linux/device.h>
30 #include <linux/sched.h>
31 #include <linux/iommu.h>
34 #include <linux/of_iommu.h>
37 #include <asm/cacheflush.h>
39 #include <mach/iomap.h>
40 #include <mach/smmu.h>
41 #include <mach/tegra-ahb.h>
43 /* bitmap of the page sizes currently supported */
44 #define SMMU_IOMMU_PGSIZES (SZ_4K)
46 #define SMMU_CONFIG 0x10
47 #define SMMU_CONFIG_DISABLE 0
48 #define SMMU_CONFIG_ENABLE 1
50 #define SMMU_TLB_CONFIG 0x14
51 #define SMMU_TLB_CONFIG_STATS__MASK (1 << 31)
52 #define SMMU_TLB_CONFIG_STATS__ENABLE (1 << 31)
53 #define SMMU_TLB_CONFIG_HIT_UNDER_MISS__ENABLE (1 << 29)
54 #define SMMU_TLB_CONFIG_ACTIVE_LINES__VALUE 0x10
55 #define SMMU_TLB_CONFIG_RESET_VAL 0x20000010
57 #define SMMU_PTC_CONFIG 0x18
58 #define SMMU_PTC_CONFIG_STATS__MASK (1 << 31)
59 #define SMMU_PTC_CONFIG_STATS__ENABLE (1 << 31)
60 #define SMMU_PTC_CONFIG_CACHE__ENABLE (1 << 29)
61 #define SMMU_PTC_CONFIG_INDEX_MAP__PATTERN 0x3f
62 #define SMMU_PTC_CONFIG_RESET_VAL 0x2000003f
64 #define SMMU_PTB_ASID 0x1c
65 #define SMMU_PTB_ASID_CURRENT_SHIFT 0
67 #define SMMU_PTB_DATA 0x20
68 #define SMMU_PTB_DATA_RESET_VAL 0
69 #define SMMU_PTB_DATA_ASID_NONSECURE_SHIFT 29
70 #define SMMU_PTB_DATA_ASID_WRITABLE_SHIFT 30
71 #define SMMU_PTB_DATA_ASID_READABLE_SHIFT 31
73 #define SMMU_TLB_FLUSH 0x30
74 #define SMMU_TLB_FLUSH_VA_MATCH_ALL 0
75 #define SMMU_TLB_FLUSH_VA_MATCH_SECTION 2
76 #define SMMU_TLB_FLUSH_VA_MATCH_GROUP 3
77 #define SMMU_TLB_FLUSH_ASID_SHIFT 29
78 #define SMMU_TLB_FLUSH_ASID_MATCH_DISABLE 0
79 #define SMMU_TLB_FLUSH_ASID_MATCH_ENABLE 1
80 #define SMMU_TLB_FLUSH_ASID_MATCH_SHIFT 31
82 #define SMMU_PTC_FLUSH 0x34
83 #define SMMU_PTC_FLUSH_TYPE_ALL 0
84 #define SMMU_PTC_FLUSH_TYPE_ADR 1
85 #define SMMU_PTC_FLUSH_ADR_SHIFT 4
87 #define SMMU_ASID_SECURITY 0x38
89 #define SMMU_STATS_TLB_HIT_COUNT 0x1f0
90 #define SMMU_STATS_TLB_MISS_COUNT 0x1f4
91 #define SMMU_STATS_PTC_HIT_COUNT 0x1f8
92 #define SMMU_STATS_PTC_MISS_COUNT 0x1fc
94 #define SMMU_TRANSLATION_ENABLE_0 0x228
95 #define SMMU_TRANSLATION_ENABLE_1 0x22c
96 #define SMMU_TRANSLATION_ENABLE_2 0x230
98 #define SMMU_AFI_ASID 0x238 /* PCIE */
99 #define SMMU_AVPC_ASID 0x23c /* AVP */
100 #define SMMU_DC_ASID 0x240 /* Display controller */
101 #define SMMU_DCB_ASID 0x244 /* Display controller B */
102 #define SMMU_EPP_ASID 0x248 /* Encoder pre-processor */
103 #define SMMU_G2_ASID 0x24c /* 2D engine */
104 #define SMMU_HC_ASID 0x250 /* Host1x */
105 #define SMMU_HDA_ASID 0x254 /* High-def audio */
106 #define SMMU_ISP_ASID 0x258 /* Image signal processor */
107 #define SMMU_MPE_ASID 0x264 /* MPEG encoder */
108 #define SMMU_NV_ASID 0x268 /* (3D) */
109 #define SMMU_NV2_ASID 0x26c /* (3D) */
110 #define SMMU_PPCS_ASID 0x270 /* AHB */
111 #define SMMU_SATA_ASID 0x278 /* SATA */
112 #define SMMU_VDE_ASID 0x27c /* Video decoder */
113 #define SMMU_VI_ASID 0x280 /* Video input */
115 #define SMMU_PDE_NEXT_SHIFT 28
117 #define SMMU_TLB_FLUSH_VA_SECTION__MASK 0xffc00000
118 #define SMMU_TLB_FLUSH_VA_SECTION__SHIFT 12 /* right shift */
119 #define SMMU_TLB_FLUSH_VA_GROUP__MASK 0xffffc000
120 #define SMMU_TLB_FLUSH_VA_GROUP__SHIFT 12 /* right shift */
121 #define SMMU_TLB_FLUSH_VA(iova, which) \
122 ((((iova) & SMMU_TLB_FLUSH_VA_##which##__MASK) >> \
123 SMMU_TLB_FLUSH_VA_##which##__SHIFT) | \
124 SMMU_TLB_FLUSH_VA_MATCH_##which)
125 #define SMMU_PTB_ASID_CUR(n) \
126 ((n) << SMMU_PTB_ASID_CURRENT_SHIFT)
127 #define SMMU_TLB_FLUSH_ASID_MATCH_disable \
128 (SMMU_TLB_FLUSH_ASID_MATCH_DISABLE << \
129 SMMU_TLB_FLUSH_ASID_MATCH_SHIFT)
130 #define SMMU_TLB_FLUSH_ASID_MATCH__ENABLE \
131 (SMMU_TLB_FLUSH_ASID_MATCH_ENABLE << \
132 SMMU_TLB_FLUSH_ASID_MATCH_SHIFT)
134 #define SMMU_PAGE_SHIFT 12
135 #define SMMU_PAGE_SIZE (1 << SMMU_PAGE_SHIFT)
136 #define SMMU_PAGE_MASK ((1 << SMMU_PAGE_SHIFT) - 1)
138 #define SMMU_PDIR_COUNT 1024
139 #define SMMU_PDIR_SIZE (sizeof(unsigned long) * SMMU_PDIR_COUNT)
140 #define SMMU_PTBL_COUNT 1024
141 #define SMMU_PTBL_SIZE (sizeof(unsigned long) * SMMU_PTBL_COUNT)
142 #define SMMU_PDIR_SHIFT 12
143 #define SMMU_PDE_SHIFT 12
144 #define SMMU_PTE_SHIFT 12
145 #define SMMU_PFN_MASK 0x000fffff
147 #define SMMU_ADDR_TO_PFN(addr) ((addr) >> 12)
148 #define SMMU_ADDR_TO_PDN(addr) ((addr) >> 22)
149 #define SMMU_PDN_TO_ADDR(addr) ((pdn) << 22)
151 #define _READABLE (1 << SMMU_PTB_DATA_ASID_READABLE_SHIFT)
152 #define _WRITABLE (1 << SMMU_PTB_DATA_ASID_WRITABLE_SHIFT)
153 #define _NONSECURE (1 << SMMU_PTB_DATA_ASID_NONSECURE_SHIFT)
154 #define _PDE_NEXT (1 << SMMU_PDE_NEXT_SHIFT)
155 #define _MASK_ATTR (_READABLE | _WRITABLE | _NONSECURE)
157 #define _PDIR_ATTR (_READABLE | _WRITABLE | _NONSECURE)
159 #define _PDE_ATTR (_READABLE | _WRITABLE | _NONSECURE)
160 #define _PDE_ATTR_N (_PDE_ATTR | _PDE_NEXT)
161 #define _PDE_VACANT(pdn) (((pdn) << 10) | _PDE_ATTR)
163 #define _PTE_ATTR (_READABLE | _WRITABLE | _NONSECURE)
164 #define _PTE_VACANT(addr) (((addr) >> SMMU_PAGE_SHIFT) | _PTE_ATTR)
166 #define SMMU_MK_PDIR(page, attr) \
167 ((page_to_phys(page) >> SMMU_PDIR_SHIFT) | (attr))
168 #define SMMU_MK_PDE(page, attr) \
169 (unsigned long)((page_to_phys(page) >> SMMU_PDE_SHIFT) | (attr))
170 #define SMMU_EX_PTBL_PAGE(pde) \
171 pfn_to_page((unsigned long)(pde) & SMMU_PFN_MASK)
172 #define SMMU_PFN_TO_PTE(pfn, attr) (unsigned long)((pfn) | (attr))
174 #define SMMU_ASID_ENABLE(asid) ((asid) | (1 << 31))
175 #define SMMU_ASID_DISABLE 0
176 #define SMMU_ASID_ASID(n) ((n) & ~SMMU_ASID_ENABLE(0))
178 #define NUM_SMMU_REG_BANKS 3
180 #define smmu_client_enable_hwgrp(c, m) smmu_client_set_hwgrp(c, m, 1)
181 #define smmu_client_disable_hwgrp(c) smmu_client_set_hwgrp(c, 0, 0)
182 #define __smmu_client_enable_hwgrp(c, m) __smmu_client_set_hwgrp(c, m, 1)
183 #define __smmu_client_disable_hwgrp(c) __smmu_client_set_hwgrp(c, 0, 0)
185 #define HWGRP_INIT(client) [HWGRP_##client] = SMMU_##client##_ASID
187 static const u32 smmu_hwgrp_asid_reg[] = {
205 #define HWGRP_ASID_REG(x) (smmu_hwgrp_asid_reg[x])
208 * Per client for address space
212 struct list_head list;
221 struct smmu_device *smmu; /* back pointer to container */
223 spinlock_t lock; /* for pagetable */
224 struct page *pdir_page;
225 unsigned long pdir_attr;
226 unsigned long pde_attr;
227 unsigned long pte_attr;
228 unsigned int *pte_count;
230 struct list_head client;
231 spinlock_t client_lock; /* for client list */
235 * Per SMMU device - IOMMU device
238 void __iomem *regs[NUM_SMMU_REG_BANKS];
239 unsigned long iovmm_base; /* remappable base address */
240 unsigned long page_count; /* total remappable size */
244 struct page *avp_vector_page; /* dummy page shared by all AS's */
247 * Register image savers for suspend/resume
249 unsigned long translation_enable_0;
250 unsigned long translation_enable_1;
251 unsigned long translation_enable_2;
252 unsigned long asid_security;
254 struct device_node *ahb;
257 struct smmu_as as[0]; /* Run-time allocated array */
260 static struct smmu_device *smmu_handle; /* unique for a system */
263 * SMMU register accessors
265 static inline u32 smmu_read(struct smmu_device *smmu, size_t offs)
269 return readl(smmu->regs[0] + offs - 0x10);
270 BUG_ON(offs < 0x1f0);
272 return readl(smmu->regs[1] + offs - 0x1f0);
273 BUG_ON(offs < 0x228);
275 return readl(smmu->regs[2] + offs - 0x228);
279 static inline void smmu_write(struct smmu_device *smmu, u32 val, size_t offs)
283 writel(val, smmu->regs[0] + offs - 0x10);
286 BUG_ON(offs < 0x1f0);
288 writel(val, smmu->regs[1] + offs - 0x1f0);
291 BUG_ON(offs < 0x228);
293 writel(val, smmu->regs[2] + offs - 0x228);
299 #define VA_PAGE_TO_PA(va, page) \
300 (page_to_phys(page) + ((unsigned long)(va) & ~PAGE_MASK))
302 #define FLUSH_CPU_DCACHE(va, page, size) \
304 unsigned long _pa_ = VA_PAGE_TO_PA(va, page); \
305 __cpuc_flush_dcache_area((void *)(va), (size_t)(size)); \
306 outer_flush_range(_pa_, _pa_+(size_t)(size)); \
310 * Any interaction between any block on PPSB and a block on APB or AHB
311 * must have these read-back barriers to ensure the APB/AHB bus
312 * transaction is complete before initiating activity on the PPSB
315 #define FLUSH_SMMU_REGS(smmu) smmu_read(smmu, SMMU_CONFIG)
317 #define smmu_client_hwgrp(c) (u32)((c)->dev->platform_data)
319 static int __smmu_client_set_hwgrp(struct smmu_client *c,
320 unsigned long map, int on)
323 struct smmu_as *as = c->as;
324 u32 val, offs, mask = SMMU_ASID_ENABLE(as->asid);
325 struct smmu_device *smmu = as->smmu;
331 map = smmu_client_hwgrp(c);
333 for_each_set_bit(i, &map, HWGRP_COUNT) {
334 offs = HWGRP_ASID_REG(i);
335 val = smmu_read(smmu, offs);
337 if (WARN_ON(val & mask))
341 WARN_ON((val & mask) == mask);
344 smmu_write(smmu, val, offs);
346 FLUSH_SMMU_REGS(smmu);
351 for_each_set_bit(i, &map, HWGRP_COUNT) {
352 offs = HWGRP_ASID_REG(i);
353 val = smmu_read(smmu, offs);
355 smmu_write(smmu, val, offs);
360 static int smmu_client_set_hwgrp(struct smmu_client *c, u32 map, int on)
364 struct smmu_as *as = c->as;
365 struct smmu_device *smmu = as->smmu;
367 spin_lock_irqsave(&smmu->lock, flags);
368 val = __smmu_client_set_hwgrp(c, map, on);
369 spin_unlock_irqrestore(&smmu->lock, flags);
374 * Flush all TLB entries and all PTC entries
375 * Caller must lock smmu
377 static void smmu_flush_regs(struct smmu_device *smmu, int enable)
381 smmu_write(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
382 FLUSH_SMMU_REGS(smmu);
383 val = SMMU_TLB_FLUSH_VA_MATCH_ALL |
384 SMMU_TLB_FLUSH_ASID_MATCH_disable;
385 smmu_write(smmu, val, SMMU_TLB_FLUSH);
388 smmu_write(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
389 FLUSH_SMMU_REGS(smmu);
392 static int smmu_setup_regs(struct smmu_device *smmu)
397 for (i = 0; i < smmu->num_as; i++) {
398 struct smmu_as *as = &smmu->as[i];
399 struct smmu_client *c;
401 smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
402 val = as->pdir_page ?
403 SMMU_MK_PDIR(as->pdir_page, as->pdir_attr) :
404 SMMU_PTB_DATA_RESET_VAL;
405 smmu_write(smmu, val, SMMU_PTB_DATA);
407 list_for_each_entry(c, &as->client, list)
408 __smmu_client_set_hwgrp(c, c->hwgrp, 1);
411 smmu_write(smmu, smmu->translation_enable_0, SMMU_TRANSLATION_ENABLE_0);
412 smmu_write(smmu, smmu->translation_enable_1, SMMU_TRANSLATION_ENABLE_1);
413 smmu_write(smmu, smmu->translation_enable_2, SMMU_TRANSLATION_ENABLE_2);
414 smmu_write(smmu, smmu->asid_security, SMMU_ASID_SECURITY);
415 smmu_write(smmu, SMMU_TLB_CONFIG_RESET_VAL, SMMU_TLB_CONFIG);
416 smmu_write(smmu, SMMU_PTC_CONFIG_RESET_VAL, SMMU_PTC_CONFIG);
418 smmu_flush_regs(smmu, 1);
420 return tegra_ahb_enable_smmu(smmu->ahb);
423 static void flush_ptc_and_tlb(struct smmu_device *smmu,
424 struct smmu_as *as, dma_addr_t iova,
425 unsigned long *pte, struct page *page, int is_pde)
428 unsigned long tlb_flush_va = is_pde
429 ? SMMU_TLB_FLUSH_VA(iova, SECTION)
430 : SMMU_TLB_FLUSH_VA(iova, GROUP);
432 val = SMMU_PTC_FLUSH_TYPE_ADR | VA_PAGE_TO_PA(pte, page);
433 smmu_write(smmu, val, SMMU_PTC_FLUSH);
434 FLUSH_SMMU_REGS(smmu);
436 SMMU_TLB_FLUSH_ASID_MATCH__ENABLE |
437 (as->asid << SMMU_TLB_FLUSH_ASID_SHIFT);
438 smmu_write(smmu, val, SMMU_TLB_FLUSH);
439 FLUSH_SMMU_REGS(smmu);
442 static void free_ptbl(struct smmu_as *as, dma_addr_t iova)
444 unsigned long pdn = SMMU_ADDR_TO_PDN(iova);
445 unsigned long *pdir = (unsigned long *)page_address(as->pdir_page);
447 if (pdir[pdn] != _PDE_VACANT(pdn)) {
448 dev_dbg(as->smmu->dev, "pdn: %lx\n", pdn);
450 ClearPageReserved(SMMU_EX_PTBL_PAGE(pdir[pdn]));
451 __free_page(SMMU_EX_PTBL_PAGE(pdir[pdn]));
452 pdir[pdn] = _PDE_VACANT(pdn);
453 FLUSH_CPU_DCACHE(&pdir[pdn], as->pdir_page, sizeof pdir[pdn]);
454 flush_ptc_and_tlb(as->smmu, as, iova, &pdir[pdn],
459 static void free_pdir(struct smmu_as *as)
463 struct device *dev = as->smmu->dev;
468 addr = as->smmu->iovmm_base;
469 count = as->smmu->page_count;
470 while (count-- > 0) {
472 addr += SMMU_PAGE_SIZE * SMMU_PTBL_COUNT;
474 ClearPageReserved(as->pdir_page);
475 __free_page(as->pdir_page);
476 as->pdir_page = NULL;
477 devm_kfree(dev, as->pte_count);
478 as->pte_count = NULL;
482 * Maps PTBL for given iova and returns the PTE address
483 * Caller must unmap the mapped PTBL returned in *ptbl_page_p
485 static unsigned long *locate_pte(struct smmu_as *as,
486 dma_addr_t iova, bool allocate,
487 struct page **ptbl_page_p,
488 unsigned int **count)
490 unsigned long ptn = SMMU_ADDR_TO_PFN(iova);
491 unsigned long pdn = SMMU_ADDR_TO_PDN(iova);
492 unsigned long *pdir = page_address(as->pdir_page);
495 if (pdir[pdn] != _PDE_VACANT(pdn)) {
496 /* Mapped entry table already exists */
497 *ptbl_page_p = SMMU_EX_PTBL_PAGE(pdir[pdn]);
498 ptbl = page_address(*ptbl_page_p);
499 } else if (!allocate) {
503 unsigned long addr = SMMU_PDN_TO_ADDR(pdn);
505 /* Vacant - allocate a new page table */
506 dev_dbg(as->smmu->dev, "New PTBL pdn: %lx\n", pdn);
508 *ptbl_page_p = alloc_page(GFP_ATOMIC);
510 dev_err(as->smmu->dev,
511 "failed to allocate smmu_device page table\n");
514 SetPageReserved(*ptbl_page_p);
515 ptbl = (unsigned long *)page_address(*ptbl_page_p);
516 for (pn = 0; pn < SMMU_PTBL_COUNT;
517 pn++, addr += SMMU_PAGE_SIZE) {
518 ptbl[pn] = _PTE_VACANT(addr);
520 FLUSH_CPU_DCACHE(ptbl, *ptbl_page_p, SMMU_PTBL_SIZE);
521 pdir[pdn] = SMMU_MK_PDE(*ptbl_page_p,
522 as->pde_attr | _PDE_NEXT);
523 FLUSH_CPU_DCACHE(&pdir[pdn], as->pdir_page, sizeof pdir[pdn]);
524 flush_ptc_and_tlb(as->smmu, as, iova, &pdir[pdn],
527 *count = &as->pte_count[pdn];
529 return &ptbl[ptn % SMMU_PTBL_COUNT];
532 #ifdef CONFIG_SMMU_SIG_DEBUG
533 static void put_signature(struct smmu_as *as,
534 dma_addr_t iova, unsigned long pfn)
537 unsigned long *vaddr;
539 page = pfn_to_page(pfn);
540 vaddr = page_address(page);
545 vaddr[1] = pfn << PAGE_SHIFT;
546 FLUSH_CPU_DCACHE(vaddr, page, sizeof(vaddr[0]) * 2);
549 static inline void put_signature(struct smmu_as *as,
550 unsigned long addr, unsigned long pfn)
556 * Caller must not hold as->lock
558 static int alloc_pdir(struct smmu_as *as)
560 unsigned long *pdir, flags;
563 struct smmu_device *smmu = as->smmu;
568 * do the allocation, then grab as->lock
570 cnt = devm_kzalloc(smmu->dev,
571 sizeof(cnt[0]) * SMMU_PDIR_COUNT,
573 page = alloc_page(GFP_KERNEL | __GFP_DMA);
575 spin_lock_irqsave(&as->lock, flags);
578 /* We raced, free the redundant */
584 dev_err(smmu->dev, "failed to allocate at %s\n", __func__);
589 as->pdir_page = page;
592 SetPageReserved(as->pdir_page);
593 pdir = page_address(as->pdir_page);
595 for (pdn = 0; pdn < SMMU_PDIR_COUNT; pdn++)
596 pdir[pdn] = _PDE_VACANT(pdn);
597 FLUSH_CPU_DCACHE(pdir, as->pdir_page, SMMU_PDIR_SIZE);
598 val = SMMU_PTC_FLUSH_TYPE_ADR | VA_PAGE_TO_PA(pdir, as->pdir_page);
599 smmu_write(smmu, val, SMMU_PTC_FLUSH);
600 FLUSH_SMMU_REGS(as->smmu);
601 val = SMMU_TLB_FLUSH_VA_MATCH_ALL |
602 SMMU_TLB_FLUSH_ASID_MATCH__ENABLE |
603 (as->asid << SMMU_TLB_FLUSH_ASID_SHIFT);
604 smmu_write(smmu, val, SMMU_TLB_FLUSH);
605 FLUSH_SMMU_REGS(as->smmu);
607 spin_unlock_irqrestore(&as->lock, flags);
612 spin_unlock_irqrestore(&as->lock, flags);
614 devm_kfree(smmu->dev, cnt);
620 static void __smmu_iommu_unmap(struct smmu_as *as, dma_addr_t iova)
626 pte = locate_pte(as, iova, false, &page, &count);
630 if (WARN_ON(*pte == _PTE_VACANT(iova)))
633 *pte = _PTE_VACANT(iova);
634 FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
635 flush_ptc_and_tlb(as->smmu, as, iova, pte, page, 0);
638 smmu_flush_regs(as->smmu, 0);
642 static void __smmu_iommu_map_pfn(struct smmu_as *as, dma_addr_t iova,
645 struct smmu_device *smmu = as->smmu;
650 pte = locate_pte(as, iova, true, &page, &count);
654 if (*pte == _PTE_VACANT(iova))
656 *pte = SMMU_PFN_TO_PTE(pfn, as->pte_attr);
657 if (unlikely((*pte == _PTE_VACANT(iova))))
659 FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
660 flush_ptc_and_tlb(smmu, as, iova, pte, page, 0);
661 put_signature(as, iova, pfn);
664 static int smmu_iommu_map(struct iommu_domain *domain, unsigned long iova,
665 phys_addr_t pa, size_t bytes, int prot)
667 struct smmu_as *as = domain->priv;
668 unsigned long pfn = __phys_to_pfn(pa);
671 dev_dbg(as->smmu->dev, "[%d] %08lx:%08x\n", as->asid, iova, pa);
676 spin_lock_irqsave(&as->lock, flags);
677 __smmu_iommu_map_pfn(as, iova, pfn);
678 spin_unlock_irqrestore(&as->lock, flags);
682 static size_t smmu_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
685 struct smmu_as *as = domain->priv;
688 dev_dbg(as->smmu->dev, "[%d] %08lx\n", as->asid, iova);
690 spin_lock_irqsave(&as->lock, flags);
691 __smmu_iommu_unmap(as, iova);
692 spin_unlock_irqrestore(&as->lock, flags);
693 return SMMU_PAGE_SIZE;
696 static phys_addr_t smmu_iommu_iova_to_phys(struct iommu_domain *domain,
699 struct smmu_as *as = domain->priv;
706 spin_lock_irqsave(&as->lock, flags);
708 pte = locate_pte(as, iova, true, &page, &count);
709 pfn = *pte & SMMU_PFN_MASK;
710 WARN_ON(!pfn_valid(pfn));
711 dev_dbg(as->smmu->dev,
712 "iova:%08lx pfn:%08lx asid:%d\n", iova, pfn, as->asid);
714 spin_unlock_irqrestore(&as->lock, flags);
715 return PFN_PHYS(pfn);
718 static int smmu_iommu_domain_has_cap(struct iommu_domain *domain,
724 static int smmu_iommu_attach_dev(struct iommu_domain *domain,
727 struct smmu_as *as = domain->priv;
728 struct smmu_device *smmu = as->smmu;
729 struct smmu_client *client, *c;
733 client = devm_kzalloc(smmu->dev, sizeof(*c), GFP_KERNEL);
738 map = (unsigned long)dev->platform_data;
742 err = smmu_client_enable_hwgrp(client, map);
746 spin_lock(&as->client_lock);
747 list_for_each_entry(c, &as->client, list) {
750 "%s is already attached\n", dev_name(c->dev));
755 list_add(&client->list, &as->client);
756 spin_unlock(&as->client_lock);
759 * Reserve "page zero" for AVP vectors using a common dummy
762 if (map & HWG_AVPC) {
765 page = as->smmu->avp_vector_page;
766 __smmu_iommu_map_pfn(as, 0, page_to_pfn(page));
768 pr_info("Reserve \"page zero\" for AVP vectors using a common dummy\n");
771 dev_dbg(smmu->dev, "%s is attached\n", dev_name(dev));
775 smmu_client_disable_hwgrp(client);
776 spin_unlock(&as->client_lock);
778 devm_kfree(smmu->dev, client);
782 static void smmu_iommu_detach_dev(struct iommu_domain *domain,
785 struct smmu_as *as = domain->priv;
786 struct smmu_device *smmu = as->smmu;
787 struct smmu_client *c;
789 spin_lock(&as->client_lock);
791 list_for_each_entry(c, &as->client, list) {
793 smmu_client_disable_hwgrp(c);
795 devm_kfree(smmu->dev, c);
798 "%s is detached\n", dev_name(c->dev));
802 dev_err(smmu->dev, "Couldn't find %s\n", dev_name(c->dev));
804 spin_unlock(&as->client_lock);
807 static int smmu_iommu_domain_init(struct iommu_domain *domain)
809 int i, err = -ENODEV;
812 struct smmu_device *smmu = smmu_handle;
814 /* Look for a free AS with lock held */
815 for (i = 0; i < smmu->num_as; i++) {
817 if (!as->pdir_page) {
818 err = alloc_pdir(as);
825 if (i == smmu->num_as)
826 dev_err(smmu->dev, "no free AS\n");
830 spin_lock_irqsave(&smmu->lock, flags);
832 /* Update PDIR register */
833 smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
835 SMMU_MK_PDIR(as->pdir_page, as->pdir_attr), SMMU_PTB_DATA);
836 FLUSH_SMMU_REGS(smmu);
838 spin_unlock_irqrestore(&smmu->lock, flags);
842 domain->geometry.aperture_start = smmu->iovmm_base;
843 domain->geometry.aperture_end = smmu->iovmm_base +
844 smmu->page_count * SMMU_PAGE_SIZE - 1;
845 domain->geometry.force_aperture = true;
847 dev_dbg(smmu->dev, "smmu_as@%p\n", as);
852 static void smmu_iommu_domain_destroy(struct iommu_domain *domain)
854 struct smmu_as *as = domain->priv;
855 struct smmu_device *smmu = as->smmu;
858 spin_lock_irqsave(&as->lock, flags);
861 spin_lock(&smmu->lock);
862 smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
863 smmu_write(smmu, SMMU_PTB_DATA_RESET_VAL, SMMU_PTB_DATA);
864 FLUSH_SMMU_REGS(smmu);
865 spin_unlock(&smmu->lock);
870 if (!list_empty(&as->client)) {
871 struct smmu_client *c;
873 list_for_each_entry(c, &as->client, list)
874 smmu_iommu_detach_dev(domain, c->dev);
877 spin_unlock_irqrestore(&as->lock, flags);
880 dev_dbg(smmu->dev, "smmu_as@%p\n", as);
883 static struct iommu_ops smmu_iommu_ops = {
884 .domain_init = smmu_iommu_domain_init,
885 .domain_destroy = smmu_iommu_domain_destroy,
886 .attach_dev = smmu_iommu_attach_dev,
887 .detach_dev = smmu_iommu_detach_dev,
888 .map = smmu_iommu_map,
889 .unmap = smmu_iommu_unmap,
890 .iova_to_phys = smmu_iommu_iova_to_phys,
891 .domain_has_cap = smmu_iommu_domain_has_cap,
892 .pgsize_bitmap = SMMU_IOMMU_PGSIZES,
895 static int tegra_smmu_suspend(struct device *dev)
897 struct smmu_device *smmu = dev_get_drvdata(dev);
899 smmu->translation_enable_0 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_0);
900 smmu->translation_enable_1 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_1);
901 smmu->translation_enable_2 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_2);
902 smmu->asid_security = smmu_read(smmu, SMMU_ASID_SECURITY);
906 static int tegra_smmu_resume(struct device *dev)
908 struct smmu_device *smmu = dev_get_drvdata(dev);
912 spin_lock_irqsave(&smmu->lock, flags);
913 err = smmu_setup_regs(smmu);
914 spin_unlock_irqrestore(&smmu->lock, flags);
918 static int tegra_smmu_probe(struct platform_device *pdev)
920 struct smmu_device *smmu;
921 struct device *dev = &pdev->dev;
922 int i, asids, err = 0;
923 dma_addr_t uninitialized_var(base);
924 size_t bytes, uninitialized_var(size);
929 BUILD_BUG_ON(PAGE_SHIFT != SMMU_PAGE_SHIFT);
931 if (of_property_read_u32(dev->of_node, "nvidia,#asids", &asids))
934 bytes = sizeof(*smmu) + asids * sizeof(*smmu->as);
935 smmu = devm_kzalloc(dev, bytes, GFP_KERNEL);
937 dev_err(dev, "failed to allocate smmu_device\n");
941 for (i = 0; i < ARRAY_SIZE(smmu->regs); i++) {
942 struct resource *res;
944 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
947 smmu->regs[i] = devm_request_and_ioremap(&pdev->dev, res);
952 err = of_get_dma_window(dev->of_node, NULL, 0, NULL, &base, &size);
956 if (size & SMMU_PAGE_MASK)
959 size >>= SMMU_PAGE_SHIFT;
963 smmu->ahb = of_parse_phandle(dev->of_node, "nvidia,ahb", 0);
968 smmu->num_as = asids;
969 smmu->iovmm_base = base;
970 smmu->page_count = size;
972 smmu->translation_enable_0 = ~0;
973 smmu->translation_enable_1 = ~0;
974 smmu->translation_enable_2 = ~0;
975 smmu->asid_security = 0;
977 for (i = 0; i < smmu->num_as; i++) {
978 struct smmu_as *as = &smmu->as[i];
982 as->pdir_attr = _PDIR_ATTR;
983 as->pde_attr = _PDE_ATTR;
984 as->pte_attr = _PTE_ATTR;
986 spin_lock_init(&as->lock);
987 INIT_LIST_HEAD(&as->client);
989 spin_lock_init(&smmu->lock);
990 err = smmu_setup_regs(smmu);
993 platform_set_drvdata(pdev, smmu);
995 smmu->avp_vector_page = alloc_page(GFP_KERNEL);
996 if (!smmu->avp_vector_page)
1003 static int tegra_smmu_remove(struct platform_device *pdev)
1005 struct smmu_device *smmu = platform_get_drvdata(pdev);
1008 smmu_write(smmu, SMMU_CONFIG_DISABLE, SMMU_CONFIG);
1009 for (i = 0; i < smmu->num_as; i++)
1010 free_pdir(&smmu->as[i]);
1011 __free_page(smmu->avp_vector_page);
1016 const struct dev_pm_ops tegra_smmu_pm_ops = {
1017 .suspend = tegra_smmu_suspend,
1018 .resume = tegra_smmu_resume,
1022 static struct of_device_id tegra_smmu_of_match[] __devinitdata = {
1023 { .compatible = "nvidia,tegra30-smmu", },
1026 MODULE_DEVICE_TABLE(of, tegra_smmu_of_match);
1029 static struct platform_driver tegra_smmu_driver = {
1030 .probe = tegra_smmu_probe,
1031 .remove = tegra_smmu_remove,
1033 .owner = THIS_MODULE,
1034 .name = "tegra-smmu",
1035 .pm = &tegra_smmu_pm_ops,
1036 .of_match_table = of_match_ptr(tegra_smmu_of_match),
1040 static int __devinit tegra_smmu_init(void)
1042 bus_set_iommu(&platform_bus_type, &smmu_iommu_ops);
1043 return platform_driver_register(&tegra_smmu_driver);
1046 static void __exit tegra_smmu_exit(void)
1048 platform_driver_unregister(&tegra_smmu_driver);
1051 subsys_initcall(tegra_smmu_init);
1052 module_exit(tegra_smmu_exit);
1054 MODULE_DESCRIPTION("IOMMU API for SMMU in Tegra30");
1055 MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
1056 MODULE_ALIAS("platform:tegra-smmu");
1057 MODULE_LICENSE("GPL v2");