20 #define pr_fmt(fmt) "%s(): " fmt, __func__
22 #include <linux/module.h>
25 #include <linux/slab.h>
29 #include <linux/device.h>
30 #include <linux/sched.h>
39 #include <asm/cacheflush.h>
67 #define HWG_AFI (1 << HWGRP_AFI)
68 #define HWG_AVPC (1 << HWGRP_AVPC)
69 #define HWG_DC (1 << HWGRP_DC)
70 #define HWG_DCB (1 << HWGRP_DCB)
71 #define HWG_EPP (1 << HWGRP_EPP)
72 #define HWG_G2 (1 << HWGRP_G2)
73 #define HWG_HC (1 << HWGRP_HC)
74 #define HWG_HDA (1 << HWGRP_HDA)
75 #define HWG_ISP (1 << HWGRP_ISP)
76 #define HWG_MPE (1 << HWGRP_MPE)
77 #define HWG_NV (1 << HWGRP_NV)
78 #define HWG_NV2 (1 << HWGRP_NV2)
79 #define HWG_PPCS (1 << HWGRP_PPCS)
80 #define HWG_SATA (1 << HWGRP_SATA)
81 #define HWG_VDE (1 << HWGRP_VDE)
82 #define HWG_VI (1 << HWGRP_VI)
85 #define SMMU_IOMMU_PGSIZES (SZ_4K)
87 #define SMMU_CONFIG 0x10
88 #define SMMU_CONFIG_DISABLE 0
89 #define SMMU_CONFIG_ENABLE 1
101 #define SMMU_CACHE_CONFIG_BASE 0x14
102 #define __SMMU_CACHE_CONFIG(mc, cache) (SMMU_CACHE_CONFIG_BASE + 4 * cache)
103 #define SMMU_CACHE_CONFIG(cache) __SMMU_CACHE_CONFIG(_MC, cache)
105 #define SMMU_CACHE_CONFIG_STATS_SHIFT 31
106 #define SMMU_CACHE_CONFIG_STATS_ENABLE (1 << SMMU_CACHE_CONFIG_STATS_SHIFT)
107 #define SMMU_CACHE_CONFIG_STATS_TEST_SHIFT 30
108 #define SMMU_CACHE_CONFIG_STATS_TEST (1 << SMMU_CACHE_CONFIG_STATS_TEST_SHIFT)
110 #define SMMU_TLB_CONFIG_HIT_UNDER_MISS__ENABLE (1 << 29)
111 #define SMMU_TLB_CONFIG_ACTIVE_LINES__VALUE 0x10
112 #define SMMU_TLB_CONFIG_RESET_VAL 0x20000010
114 #define SMMU_PTC_CONFIG_CACHE__ENABLE (1 << 29)
115 #define SMMU_PTC_CONFIG_INDEX_MAP__PATTERN 0x3f
116 #define SMMU_PTC_CONFIG_RESET_VAL 0x2000003f
118 #define SMMU_PTB_ASID 0x1c
119 #define SMMU_PTB_ASID_CURRENT_SHIFT 0
121 #define SMMU_PTB_DATA 0x20
122 #define SMMU_PTB_DATA_RESET_VAL 0
123 #define SMMU_PTB_DATA_ASID_NONSECURE_SHIFT 29
124 #define SMMU_PTB_DATA_ASID_WRITABLE_SHIFT 30
125 #define SMMU_PTB_DATA_ASID_READABLE_SHIFT 31
127 #define SMMU_TLB_FLUSH 0x30
128 #define SMMU_TLB_FLUSH_VA_MATCH_ALL 0
129 #define SMMU_TLB_FLUSH_VA_MATCH_SECTION 2
130 #define SMMU_TLB_FLUSH_VA_MATCH_GROUP 3
131 #define SMMU_TLB_FLUSH_ASID_SHIFT 29
132 #define SMMU_TLB_FLUSH_ASID_MATCH_DISABLE 0
133 #define SMMU_TLB_FLUSH_ASID_MATCH_ENABLE 1
134 #define SMMU_TLB_FLUSH_ASID_MATCH_SHIFT 31
136 #define SMMU_PTC_FLUSH 0x34
137 #define SMMU_PTC_FLUSH_TYPE_ALL 0
138 #define SMMU_PTC_FLUSH_TYPE_ADR 1
139 #define SMMU_PTC_FLUSH_ADR_SHIFT 4
141 #define SMMU_ASID_SECURITY 0x38
143 #define SMMU_STATS_CACHE_COUNT_BASE 0x1f0
145 #define SMMU_STATS_CACHE_COUNT(mc, cache, hitmiss) \
146 (SMMU_STATS_CACHE_COUNT_BASE + 8 * cache + 4 * hitmiss)
148 #define SMMU_TRANSLATION_ENABLE_0 0x228
149 #define SMMU_TRANSLATION_ENABLE_1 0x22c
150 #define SMMU_TRANSLATION_ENABLE_2 0x230
152 #define SMMU_AFI_ASID 0x238
153 #define SMMU_AVPC_ASID 0x23c
154 #define SMMU_DC_ASID 0x240
155 #define SMMU_DCB_ASID 0x244
156 #define SMMU_EPP_ASID 0x248
157 #define SMMU_G2_ASID 0x24c
158 #define SMMU_HC_ASID 0x250
159 #define SMMU_HDA_ASID 0x254
160 #define SMMU_ISP_ASID 0x258
161 #define SMMU_MPE_ASID 0x264
162 #define SMMU_NV_ASID 0x268
163 #define SMMU_NV2_ASID 0x26c
164 #define SMMU_PPCS_ASID 0x270
165 #define SMMU_SATA_ASID 0x278
166 #define SMMU_VDE_ASID 0x27c
167 #define SMMU_VI_ASID 0x280
169 #define SMMU_PDE_NEXT_SHIFT 28
171 #define SMMU_TLB_FLUSH_VA_SECTION__MASK 0xffc00000
172 #define SMMU_TLB_FLUSH_VA_SECTION__SHIFT 12
173 #define SMMU_TLB_FLUSH_VA_GROUP__MASK 0xffffc000
174 #define SMMU_TLB_FLUSH_VA_GROUP__SHIFT 12
175 #define SMMU_TLB_FLUSH_VA(iova, which) \
176 ((((iova) & SMMU_TLB_FLUSH_VA_##which##__MASK) >> \
177 SMMU_TLB_FLUSH_VA_##which##__SHIFT) | \
178 SMMU_TLB_FLUSH_VA_MATCH_##which)
179 #define SMMU_PTB_ASID_CUR(n) \
180 ((n) << SMMU_PTB_ASID_CURRENT_SHIFT)
181 #define SMMU_TLB_FLUSH_ASID_MATCH_disable \
182 (SMMU_TLB_FLUSH_ASID_MATCH_DISABLE << \
183 SMMU_TLB_FLUSH_ASID_MATCH_SHIFT)
184 #define SMMU_TLB_FLUSH_ASID_MATCH__ENABLE \
185 (SMMU_TLB_FLUSH_ASID_MATCH_ENABLE << \
186 SMMU_TLB_FLUSH_ASID_MATCH_SHIFT)
188 #define SMMU_PAGE_SHIFT 12
189 #define SMMU_PAGE_SIZE (1 << SMMU_PAGE_SHIFT)
190 #define SMMU_PAGE_MASK ((1 << SMMU_PAGE_SHIFT) - 1)
192 #define SMMU_PDIR_COUNT 1024
193 #define SMMU_PDIR_SIZE (sizeof(unsigned long) * SMMU_PDIR_COUNT)
194 #define SMMU_PTBL_COUNT 1024
195 #define SMMU_PTBL_SIZE (sizeof(unsigned long) * SMMU_PTBL_COUNT)
196 #define SMMU_PDIR_SHIFT 12
197 #define SMMU_PDE_SHIFT 12
198 #define SMMU_PTE_SHIFT 12
199 #define SMMU_PFN_MASK 0x000fffff
201 #define SMMU_ADDR_TO_PFN(addr) ((addr) >> 12)
202 #define SMMU_ADDR_TO_PDN(addr) ((addr) >> 22)
203 #define SMMU_PDN_TO_ADDR(pdn) ((pdn) << 22)
205 #define _READABLE (1 << SMMU_PTB_DATA_ASID_READABLE_SHIFT)
206 #define _WRITABLE (1 << SMMU_PTB_DATA_ASID_WRITABLE_SHIFT)
207 #define _NONSECURE (1 << SMMU_PTB_DATA_ASID_NONSECURE_SHIFT)
208 #define _PDE_NEXT (1 << SMMU_PDE_NEXT_SHIFT)
209 #define _MASK_ATTR (_READABLE | _WRITABLE | _NONSECURE)
211 #define _PDIR_ATTR (_READABLE | _WRITABLE | _NONSECURE)
213 #define _PDE_ATTR (_READABLE | _WRITABLE | _NONSECURE)
214 #define _PDE_ATTR_N (_PDE_ATTR | _PDE_NEXT)
215 #define _PDE_VACANT(pdn) (((pdn) << 10) | _PDE_ATTR)
217 #define _PTE_ATTR (_READABLE | _WRITABLE | _NONSECURE)
218 #define _PTE_VACANT(addr) (((addr) >> SMMU_PAGE_SHIFT) | _PTE_ATTR)
220 #define SMMU_MK_PDIR(page, attr) \
221 ((page_to_phys(page) >> SMMU_PDIR_SHIFT) | (attr))
222 #define SMMU_MK_PDE(page, attr) \
223 (unsigned long)((page_to_phys(page) >> SMMU_PDE_SHIFT) | (attr))
224 #define SMMU_EX_PTBL_PAGE(pde) \
225 pfn_to_page((unsigned long)(pde) & SMMU_PFN_MASK)
226 #define SMMU_PFN_TO_PTE(pfn, attr) (unsigned long)((pfn) | (attr))
228 #define SMMU_ASID_ENABLE(asid) ((asid) | (1 << 31))
229 #define SMMU_ASID_DISABLE 0
230 #define SMMU_ASID_ASID(n) ((n) & ~SMMU_ASID_ENABLE(0))
232 #define NUM_SMMU_REG_BANKS 3
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 #define __smmu_client_enable_hwgrp(c, m) __smmu_client_set_hwgrp(c, m, 1)
237 #define __smmu_client_disable_hwgrp(c) __smmu_client_set_hwgrp(c, 0, 0)
239 #define HWGRP_INIT(client) [HWGRP_##client] = SMMU_##client##_ASID
241 static const u32 smmu_hwgrp_asid_reg[] = {
259 #define HWGRP_ASID_REG(x) (smmu_hwgrp_asid_reg[x])
332 return readl(smmu->
regs[0] + offs - 0x10);
335 return readl(smmu->
regs[1] + offs - 0x1f0);
338 return readl(smmu->
regs[2] + offs - 0x228);
362 #define VA_PAGE_TO_PA(va, page) \
363 (page_to_phys(page) + ((unsigned long)(va) & ~PAGE_MASK))
365 #define FLUSH_CPU_DCACHE(va, page, size) \
367 unsigned long _pa_ = VA_PAGE_TO_PA(va, page); \
368 __cpuc_flush_dcache_area((void *)(va), (size_t)(size)); \
369 outer_flush_range(_pa_, _pa_+(size_t)(size)); \
378 #define FLUSH_SMMU_REGS(smmu) smmu_read(smmu, SMMU_CONFIG)
380 #define smmu_client_hwgrp(c) (u32)((c)->dev->platform_data)
382 static int __smmu_client_set_hwgrp(
struct smmu_client *
c,
383 unsigned long map,
int on)
398 val = smmu_read(smmu, offs);
407 smmu_write(smmu, val, offs);
416 val = smmu_read(smmu, offs);
418 smmu_write(smmu, val, offs);
431 val = __smmu_client_set_hwgrp(c, map, on);
432 spin_unlock_irqrestore(&smmu->
lock, flags);
455 static int smmu_setup_regs(
struct smmu_device *smmu)
460 for (i = 0; i < smmu->
num_as; i++) {
471 __smmu_client_set_hwgrp(c, c->
hwgrp, 1);
481 smmu_flush_regs(smmu, 1);
491 unsigned long tlb_flush_va = is_pde
493 : SMMU_TLB_FLUSH_VA(iova,
GROUP);
517 flush_ptc_and_tlb(as->
smmu, as, iova, &pdir[pdn],
522 static void free_pdir(
struct smmu_as *as)
531 addr = as->
smmu->iovmm_base;
532 count = as->
smmu->page_count;
533 while (count-- > 0) {
548 static unsigned long *locate_pte(
struct smmu_as *as,
550 struct page **ptbl_page_p,
551 unsigned int **count)
562 }
else if (!allocate) {
569 dev_dbg(as->
smmu->dev,
"New PTBL pdn: %lx\n", pdn);
574 "failed to allocate smmu_device page table\n");
577 SetPageReserved(*ptbl_page_p);
587 flush_ptc_and_tlb(as->
smmu, as, iova, &pdir[pdn],
595 #ifdef CONFIG_SMMU_SIG_DEBUG
596 static void put_signature(
struct smmu_as *as,
600 unsigned long *
vaddr;
612 static inline void put_signature(
struct smmu_as *as,
613 unsigned long addr,
unsigned long pfn)
621 static int alloc_pdir(
struct smmu_as *as)
623 unsigned long *pdir,
flags;
647 dev_err(smmu->
dev,
"failed to allocate at %s\n", __func__);
670 spin_unlock_irqrestore(&as->
lock, flags);
675 spin_unlock_irqrestore(&as->
lock, flags);
689 pte = locate_pte(as, iova,
false, &page, &count);
698 flush_ptc_and_tlb(as->
smmu, as, iova, pte, page, 0);
701 smmu_flush_regs(as->
smmu, 0);
713 pte = locate_pte(as, iova,
true, &page, &count);
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,
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,
754 __smmu_iommu_unmap(as, iova);
755 spin_unlock_irqrestore(&as->
lock, flags);
771 pte = locate_pte(as, iova,
true, &page, &count);
775 "iova:%08lx pfn:%08lx asid:%d\n", iova, pfn, as->
asid);
777 spin_unlock_irqrestore(&as->
lock, flags);
781 static int smmu_iommu_domain_has_cap(
struct iommu_domain *domain,
787 static int smmu_iommu_attach_dev(
struct iommu_domain *domain,
813 "%s is already attached\n", dev_name(c->
dev));
828 page = as->
smmu->avp_vector_page;
831 pr_info(
"Reserve \"page zero\" for AVP vectors using a common dummy\n");
834 dev_dbg(smmu->
dev,
"%s is attached\n", dev_name(dev));
845 static void smmu_iommu_detach_dev(
struct iommu_domain *domain,
861 "%s is detached\n", dev_name(c->
dev));
865 dev_err(smmu->
dev,
"Couldn't find %s\n", dev_name(dev));
870 static int smmu_iommu_domain_init(
struct iommu_domain *domain)
878 for (i = 0; i < smmu->
num_as; i++) {
884 err = alloc_pdir(as);
904 spin_unlock_irqrestore(&smmu->
lock, flags);
911 domain->
geometry.force_aperture =
true;
918 static void smmu_iommu_domain_destroy(
struct iommu_domain *domain)
927 spin_lock(&smmu->
lock);
931 spin_unlock(&smmu->
lock);
936 if (!list_empty(&as->
client)) {
940 smmu_iommu_detach_dev(domain, c->dev);
943 spin_unlock_irqrestore(&as->lock, flags);
950 .domain_init = smmu_iommu_domain_init,
951 .domain_destroy = smmu_iommu_domain_destroy,
952 .attach_dev = smmu_iommu_attach_dev,
953 .detach_dev = smmu_iommu_detach_dev,
954 .map = smmu_iommu_map,
955 .unmap = smmu_iommu_unmap,
956 .iova_to_phys = smmu_iommu_iova_to_phys,
957 .domain_has_cap = smmu_iommu_domain_has_cap,
962 static const char *
const smmu_debugfs_mc[] = {
"mc", };
963 static const char *
const smmu_debugfs_cache[] = {
"tlb",
"ptc", };
966 const char __user *
buffer,
967 size_t count, loff_t *
pos)
978 const char *
const command[] = {
983 char str[] =
"reset";
987 count =
min_t(
size_t, count,
sizeof(str));
999 dent = file->f_dentry;
1000 info = dent->
d_inode->i_private;
1004 val = smmu_read(smmu, offs);
1009 smmu_write(smmu, val, offs);
1014 smmu_write(smmu, val, offs);
1018 smmu_write(smmu, val, offs);
1020 smmu_write(smmu, val, offs);
1027 dev_dbg(smmu->
dev,
"%s() %08x, %08x @%08x\n", __func__,
1028 val, smmu_read(smmu, offs), offs);
1033 static int smmu_debugfs_stats_show(
struct seq_file *
s,
void *
v)
1039 const char *
const stats[] = {
"hit",
"miss", };
1042 info = dent->
d_inode->i_private;
1050 val = smmu_read(smmu, offs);
1053 dev_dbg(smmu->
dev,
"%s() %s %08x @%08x\n", __func__,
1054 stats[i], val, offs);
1062 static int smmu_debugfs_stats_open(
struct inode *
inode,
struct file *file)
1064 return single_open(file, smmu_debugfs_stats_show, inode);
1068 .open = smmu_debugfs_stats_open,
1072 .write = smmu_debugfs_stats_write,
1075 static void smmu_debugfs_delete(
struct smmu_device *smmu)
1081 static void smmu_debugfs_create(
struct smmu_device *smmu)
1098 for (i = 0; i <
ARRAY_SIZE(smmu_debugfs_mc); i++) {
1106 for (j = 0; j <
ARRAY_SIZE(smmu_debugfs_cache); j++) {
1119 &smmu_debugfs_stats_fops);
1128 smmu_debugfs_delete(smmu);
1131 static int tegra_smmu_suspend(
struct device *dev)
1142 static int tegra_smmu_resume(
struct device *dev)
1145 unsigned long flags;
1149 err = smmu_setup_regs(smmu);
1150 spin_unlock_irqrestore(&smmu->
lock, flags);
1158 int i, asids, err = 0;
1167 if (of_property_read_u32(dev->
of_node,
"nvidia,#asids", &asids))
1170 bytes =
sizeof(*smmu) + asids *
sizeof(*smmu->
as);
1173 dev_err(dev,
"failed to allocate smmu_device\n");
1177 for (i = 0; i <
ARRAY_SIZE(smmu->regs); i++) {
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++) {
1223 INIT_LIST_HEAD(&as->
client);
1226 err = smmu_setup_regs(smmu);
1229 platform_set_drvdata(pdev, smmu);
1232 if (!smmu->avp_vector_page)
1235 smmu_debugfs_create(smmu);
1242 struct smmu_device *smmu = platform_get_drvdata(pdev);
1245 smmu_debugfs_delete(smmu);
1248 for (i = 0; i < smmu->
num_as; i++)
1249 free_pdir(&smmu->
as[i]);
1256 .suspend = tegra_smmu_suspend,
1257 .resume = tegra_smmu_resume,
1262 { .compatible =
"nvidia,tegra30-smmu", },
1269 .probe = tegra_smmu_probe,
1270 .remove = tegra_smmu_remove,
1273 .name =
"tegra-smmu",
1279 static int __devinit tegra_smmu_init(
void)
1285 static void __exit tegra_smmu_exit(
void)