blob: 74ad1f43265aca614af8078c934f3f24a579359e [file] [log] [blame]
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001/*
Thierry Reding89184652014-04-16 09:24:44 +02002 * Copyright (C) 2011-2014 NVIDIA CORPORATION. All rights reserved.
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02003 *
Thierry Reding89184652014-04-16 09:24:44 +02004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02007 */
8
Thierry Reding804cb542015-03-27 11:07:27 +01009#include <linux/bitops.h>
Thierry Redingd1313e72015-01-23 09:49:25 +010010#include <linux/debugfs.h>
Thierry Redingbc5e6de2013-01-21 11:09:06 +010011#include <linux/err.h>
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020012#include <linux/iommu.h>
Thierry Reding89184652014-04-16 09:24:44 +020013#include <linux/kernel.h>
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +030014#include <linux/of.h>
Thierry Reding89184652014-04-16 09:24:44 +020015#include <linux/of_device.h>
16#include <linux/platform_device.h>
17#include <linux/slab.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020018
19#include <soc/tegra/ahb.h>
Thierry Reding89184652014-04-16 09:24:44 +020020#include <soc/tegra/mc.h>
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020021
Thierry Reding89184652014-04-16 09:24:44 +020022struct tegra_smmu {
23 void __iomem *regs;
24 struct device *dev;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020025
Thierry Reding89184652014-04-16 09:24:44 +020026 struct tegra_mc *mc;
27 const struct tegra_smmu_soc *soc;
Stephen Warrene6bc5932012-09-04 16:36:15 -060028
Thierry Reding804cb542015-03-27 11:07:27 +010029 unsigned long pfn_mask;
30
Thierry Reding89184652014-04-16 09:24:44 +020031 unsigned long *asids;
32 struct mutex lock;
Stephen Warrene6bc5932012-09-04 16:36:15 -060033
Thierry Reding89184652014-04-16 09:24:44 +020034 struct list_head list;
Thierry Redingd1313e72015-01-23 09:49:25 +010035
36 struct dentry *debugfs;
Stephen Warrene6bc5932012-09-04 16:36:15 -060037};
38
Thierry Reding89184652014-04-16 09:24:44 +020039struct tegra_smmu_as {
Joerg Roedeld5f1a812015-03-26 13:43:12 +010040 struct iommu_domain domain;
Thierry Reding89184652014-04-16 09:24:44 +020041 struct tegra_smmu *smmu;
42 unsigned int use_count;
Russell King32924c72015-07-27 13:29:31 +010043 u32 *count;
Russell King853520f2015-07-27 13:29:26 +010044 struct page **pts;
Thierry Reding89184652014-04-16 09:24:44 +020045 struct page *pd;
Russell Kinge3c97192015-07-27 13:29:52 +010046 dma_addr_t pd_dma;
Thierry Reding89184652014-04-16 09:24:44 +020047 unsigned id;
48 u32 attr;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +030049};
50
Joerg Roedeld5f1a812015-03-26 13:43:12 +010051static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom)
52{
53 return container_of(dom, struct tegra_smmu_as, domain);
54}
55
Thierry Reding89184652014-04-16 09:24:44 +020056static inline void smmu_writel(struct tegra_smmu *smmu, u32 value,
57 unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020058{
Thierry Reding89184652014-04-16 09:24:44 +020059 writel(value, smmu->regs + offset);
Joerg Roedelfe1229b2013-02-04 20:40:58 +010060}
61
Thierry Reding89184652014-04-16 09:24:44 +020062static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020063{
Thierry Reding89184652014-04-16 09:24:44 +020064 return readl(smmu->regs + offset);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020065}
66
Thierry Reding89184652014-04-16 09:24:44 +020067#define SMMU_CONFIG 0x010
68#define SMMU_CONFIG_ENABLE (1 << 0)
69
70#define SMMU_TLB_CONFIG 0x14
71#define SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29)
72#define SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28)
73#define SMMU_TLB_CONFIG_ACTIVE_LINES(x) ((x) & 0x3f)
74
75#define SMMU_PTC_CONFIG 0x18
76#define SMMU_PTC_CONFIG_ENABLE (1 << 29)
77#define SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24)
78#define SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f)
79
80#define SMMU_PTB_ASID 0x01c
81#define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
82
83#define SMMU_PTB_DATA 0x020
Russell Kinge3c97192015-07-27 13:29:52 +010084#define SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr))
Thierry Reding89184652014-04-16 09:24:44 +020085
Russell Kinge3c97192015-07-27 13:29:52 +010086#define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr))
Thierry Reding89184652014-04-16 09:24:44 +020087
88#define SMMU_TLB_FLUSH 0x030
89#define SMMU_TLB_FLUSH_VA_MATCH_ALL (0 << 0)
90#define SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0)
91#define SMMU_TLB_FLUSH_VA_MATCH_GROUP (3 << 0)
92#define SMMU_TLB_FLUSH_ASID(x) (((x) & 0x7f) << 24)
93#define SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
94 SMMU_TLB_FLUSH_VA_MATCH_SECTION)
95#define SMMU_TLB_FLUSH_VA_GROUP(addr) ((((addr) & 0xffffc000) >> 12) | \
96 SMMU_TLB_FLUSH_VA_MATCH_GROUP)
97#define SMMU_TLB_FLUSH_ASID_MATCH (1 << 31)
98
99#define SMMU_PTC_FLUSH 0x034
100#define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
101#define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
102
103#define SMMU_PTC_FLUSH_HI 0x9b8
104#define SMMU_PTC_FLUSH_HI_MASK 0x3
105
106/* per-SWGROUP SMMU_*_ASID register */
107#define SMMU_ASID_ENABLE (1 << 31)
108#define SMMU_ASID_MASK 0x7f
109#define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
110
111/* page table definitions */
112#define SMMU_NUM_PDE 1024
113#define SMMU_NUM_PTE 1024
114
115#define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
116#define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
117
118#define SMMU_PDE_SHIFT 22
119#define SMMU_PTE_SHIFT 12
120
Thierry Reding89184652014-04-16 09:24:44 +0200121#define SMMU_PD_READABLE (1 << 31)
122#define SMMU_PD_WRITABLE (1 << 30)
123#define SMMU_PD_NONSECURE (1 << 29)
124
125#define SMMU_PDE_READABLE (1 << 31)
126#define SMMU_PDE_WRITABLE (1 << 30)
127#define SMMU_PDE_NONSECURE (1 << 29)
128#define SMMU_PDE_NEXT (1 << 28)
129
130#define SMMU_PTE_READABLE (1 << 31)
131#define SMMU_PTE_WRITABLE (1 << 30)
132#define SMMU_PTE_NONSECURE (1 << 29)
133
134#define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
135 SMMU_PDE_NONSECURE)
136#define SMMU_PTE_ATTR (SMMU_PTE_READABLE | SMMU_PTE_WRITABLE | \
137 SMMU_PTE_NONSECURE)
138
Russell King34d35f82015-07-27 13:29:16 +0100139static unsigned int iova_pd_index(unsigned long iova)
140{
141 return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1);
142}
143
144static unsigned int iova_pt_index(unsigned long iova)
145{
146 return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1);
147}
148
Russell Kinge3c97192015-07-27 13:29:52 +0100149static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr)
Russell King4b3c7d12015-07-27 13:29:36 +0100150{
Russell Kinge3c97192015-07-27 13:29:52 +0100151 addr >>= 12;
152 return (addr & smmu->pfn_mask) == addr;
153}
Russell King4b3c7d12015-07-27 13:29:36 +0100154
Russell Kinge3c97192015-07-27 13:29:52 +0100155static dma_addr_t smmu_pde_to_dma(u32 pde)
156{
157 return pde << 12;
Russell King4b3c7d12015-07-27 13:29:36 +0100158}
159
Russell Kingb8fe0382015-07-27 13:29:41 +0100160static void smmu_flush_ptc_all(struct tegra_smmu *smmu)
161{
162 smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
163}
164
Russell Kinge3c97192015-07-27 13:29:52 +0100165static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma,
Thierry Reding89184652014-04-16 09:24:44 +0200166 unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200167{
Thierry Reding89184652014-04-16 09:24:44 +0200168 u32 value;
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200169
Russell Kingb8fe0382015-07-27 13:29:41 +0100170 offset &= ~(smmu->mc->soc->atom_size - 1);
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200171
Russell Kingb8fe0382015-07-27 13:29:41 +0100172 if (smmu->mc->soc->num_address_bits > 32) {
Russell Kinge3c97192015-07-27 13:29:52 +0100173#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
174 value = (dma >> 32) & SMMU_PTC_FLUSH_HI_MASK;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200175#else
Russell Kingb8fe0382015-07-27 13:29:41 +0100176 value = 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200177#endif
Russell Kingb8fe0382015-07-27 13:29:41 +0100178 smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200179 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300180
Russell Kinge3c97192015-07-27 13:29:52 +0100181 value = (dma + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
Thierry Reding89184652014-04-16 09:24:44 +0200182 smmu_writel(smmu, value, SMMU_PTC_FLUSH);
183}
184
185static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
186{
187 smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
188}
189
190static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
191 unsigned long asid)
192{
193 u32 value;
194
195 value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
196 SMMU_TLB_FLUSH_VA_MATCH_ALL;
197 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
198}
199
200static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
201 unsigned long asid,
202 unsigned long iova)
203{
204 u32 value;
205
206 value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
207 SMMU_TLB_FLUSH_VA_SECTION(iova);
208 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
209}
210
211static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu,
212 unsigned long asid,
213 unsigned long iova)
214{
215 u32 value;
216
217 value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
218 SMMU_TLB_FLUSH_VA_GROUP(iova);
219 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
220}
221
222static inline void smmu_flush(struct tegra_smmu *smmu)
223{
224 smmu_readl(smmu, SMMU_CONFIG);
225}
226
227static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
228{
229 unsigned long id;
230
231 mutex_lock(&smmu->lock);
232
233 id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
234 if (id >= smmu->soc->num_asids) {
235 mutex_unlock(&smmu->lock);
236 return -ENOSPC;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200237 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300238
Thierry Reding89184652014-04-16 09:24:44 +0200239 set_bit(id, smmu->asids);
240 *idp = id;
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300241
Thierry Reding89184652014-04-16 09:24:44 +0200242 mutex_unlock(&smmu->lock);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200243 return 0;
244}
245
Thierry Reding89184652014-04-16 09:24:44 +0200246static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200247{
Thierry Reding89184652014-04-16 09:24:44 +0200248 mutex_lock(&smmu->lock);
249 clear_bit(id, smmu->asids);
250 mutex_unlock(&smmu->lock);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200251}
252
Thierry Reding89184652014-04-16 09:24:44 +0200253static bool tegra_smmu_capable(enum iommu_cap cap)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200254{
Joerg Roedel7c2aa642014-09-05 10:51:37 +0200255 return false;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200256}
257
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100258static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200259{
Thierry Reding89184652014-04-16 09:24:44 +0200260 struct tegra_smmu_as *as;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200261
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100262 if (type != IOMMU_DOMAIN_UNMANAGED)
263 return NULL;
264
Thierry Reding89184652014-04-16 09:24:44 +0200265 as = kzalloc(sizeof(*as), GFP_KERNEL);
266 if (!as)
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100267 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200268
Thierry Reding89184652014-04-16 09:24:44 +0200269 as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200270
Russell King707917c2015-07-27 13:30:02 +0100271 as->pd = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
Thierry Reding89184652014-04-16 09:24:44 +0200272 if (!as->pd) {
273 kfree(as);
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100274 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200275 }
276
Russell King32924c72015-07-27 13:29:31 +0100277 as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL);
Thierry Reding89184652014-04-16 09:24:44 +0200278 if (!as->count) {
279 __free_page(as->pd);
280 kfree(as);
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100281 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200282 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200283
Russell King853520f2015-07-27 13:29:26 +0100284 as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL);
285 if (!as->pts) {
Russell King32924c72015-07-27 13:29:31 +0100286 kfree(as->count);
Russell King853520f2015-07-27 13:29:26 +0100287 __free_page(as->pd);
288 kfree(as);
289 return NULL;
290 }
291
Thierry Reding471d9142015-03-27 11:07:25 +0100292 /* setup aperture */
Joerg Roedel7f65ef02015-04-02 13:33:19 +0200293 as->domain.geometry.aperture_start = 0;
294 as->domain.geometry.aperture_end = 0xffffffff;
295 as->domain.geometry.force_aperture = true;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200296
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100297 return &as->domain;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200298}
299
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100300static void tegra_smmu_domain_free(struct iommu_domain *domain)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200301{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100302 struct tegra_smmu_as *as = to_smmu_as(domain);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200303
Thierry Reding89184652014-04-16 09:24:44 +0200304 /* TODO: free page directory and page tables */
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200305
Thierry Reding89184652014-04-16 09:24:44 +0200306 kfree(as);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200307}
308
Thierry Reding89184652014-04-16 09:24:44 +0200309static const struct tegra_smmu_swgroup *
310tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300311{
Thierry Reding89184652014-04-16 09:24:44 +0200312 const struct tegra_smmu_swgroup *group = NULL;
313 unsigned int i;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300314
Thierry Reding89184652014-04-16 09:24:44 +0200315 for (i = 0; i < smmu->soc->num_swgroups; i++) {
316 if (smmu->soc->swgroups[i].swgroup == swgroup) {
317 group = &smmu->soc->swgroups[i];
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300318 break;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300319 }
320 }
321
Thierry Reding89184652014-04-16 09:24:44 +0200322 return group;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300323}
324
Thierry Reding89184652014-04-16 09:24:44 +0200325static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
326 unsigned int asid)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200327{
Thierry Reding89184652014-04-16 09:24:44 +0200328 const struct tegra_smmu_swgroup *group;
329 unsigned int i;
330 u32 value;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200331
Thierry Reding89184652014-04-16 09:24:44 +0200332 for (i = 0; i < smmu->soc->num_clients; i++) {
333 const struct tegra_mc_client *client = &smmu->soc->clients[i];
334
335 if (client->swgroup != swgroup)
336 continue;
337
338 value = smmu_readl(smmu, client->smmu.reg);
339 value |= BIT(client->smmu.bit);
340 smmu_writel(smmu, value, client->smmu.reg);
341 }
342
343 group = tegra_smmu_find_swgroup(smmu, swgroup);
344 if (group) {
345 value = smmu_readl(smmu, group->reg);
346 value &= ~SMMU_ASID_MASK;
347 value |= SMMU_ASID_VALUE(asid);
348 value |= SMMU_ASID_ENABLE;
349 smmu_writel(smmu, value, group->reg);
350 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200351}
352
Thierry Reding89184652014-04-16 09:24:44 +0200353static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
354 unsigned int asid)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200355{
Thierry Reding89184652014-04-16 09:24:44 +0200356 const struct tegra_smmu_swgroup *group;
357 unsigned int i;
358 u32 value;
359
360 group = tegra_smmu_find_swgroup(smmu, swgroup);
361 if (group) {
362 value = smmu_readl(smmu, group->reg);
363 value &= ~SMMU_ASID_MASK;
364 value |= SMMU_ASID_VALUE(asid);
365 value &= ~SMMU_ASID_ENABLE;
366 smmu_writel(smmu, value, group->reg);
367 }
368
369 for (i = 0; i < smmu->soc->num_clients; i++) {
370 const struct tegra_mc_client *client = &smmu->soc->clients[i];
371
372 if (client->swgroup != swgroup)
373 continue;
374
375 value = smmu_readl(smmu, client->smmu.reg);
376 value &= ~BIT(client->smmu.bit);
377 smmu_writel(smmu, value, client->smmu.reg);
378 }
379}
380
381static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
382 struct tegra_smmu_as *as)
383{
384 u32 value;
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300385 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200386
Thierry Reding89184652014-04-16 09:24:44 +0200387 if (as->use_count > 0) {
388 as->use_count++;
389 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200390 }
391
Russell Kinge3c97192015-07-27 13:29:52 +0100392 as->pd_dma = dma_map_page(smmu->dev, as->pd, 0, SMMU_SIZE_PD,
393 DMA_TO_DEVICE);
394 if (dma_mapping_error(smmu->dev, as->pd_dma))
395 return -ENOMEM;
396
397 /* We can't handle 64-bit DMA addresses */
398 if (!smmu_dma_addr_valid(smmu, as->pd_dma)) {
399 err = -ENOMEM;
400 goto err_unmap;
401 }
402
Thierry Reding89184652014-04-16 09:24:44 +0200403 err = tegra_smmu_alloc_asid(smmu, &as->id);
404 if (err < 0)
Russell Kinge3c97192015-07-27 13:29:52 +0100405 goto err_unmap;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200406
Russell Kinge3c97192015-07-27 13:29:52 +0100407 smmu_flush_ptc(smmu, as->pd_dma, 0);
Thierry Reding89184652014-04-16 09:24:44 +0200408 smmu_flush_tlb_asid(smmu, as->id);
409
410 smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
Russell Kinge3c97192015-07-27 13:29:52 +0100411 value = SMMU_PTB_DATA_VALUE(as->pd_dma, as->attr);
Thierry Reding89184652014-04-16 09:24:44 +0200412 smmu_writel(smmu, value, SMMU_PTB_DATA);
413 smmu_flush(smmu);
414
415 as->smmu = smmu;
416 as->use_count++;
417
418 return 0;
Russell Kinge3c97192015-07-27 13:29:52 +0100419
420err_unmap:
421 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
422 return err;
Thierry Reding89184652014-04-16 09:24:44 +0200423}
424
425static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
426 struct tegra_smmu_as *as)
427{
428 if (--as->use_count > 0)
429 return;
430
431 tegra_smmu_free_asid(smmu, as->id);
Russell Kinge3c97192015-07-27 13:29:52 +0100432
433 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
434
Thierry Reding89184652014-04-16 09:24:44 +0200435 as->smmu = NULL;
436}
437
438static int tegra_smmu_attach_dev(struct iommu_domain *domain,
439 struct device *dev)
440{
441 struct tegra_smmu *smmu = dev->archdata.iommu;
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100442 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200443 struct device_node *np = dev->of_node;
444 struct of_phandle_args args;
445 unsigned int index = 0;
446 int err = 0;
447
448 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
449 &args)) {
450 unsigned int swgroup = args.args[0];
451
452 if (args.np != smmu->dev->of_node) {
453 of_node_put(args.np);
454 continue;
455 }
456
457 of_node_put(args.np);
458
459 err = tegra_smmu_as_prepare(smmu, as);
460 if (err < 0)
461 return err;
462
463 tegra_smmu_enable(smmu, swgroup, as->id);
464 index++;
465 }
466
467 if (index == 0)
468 return -ENODEV;
469
470 return 0;
471}
472
473static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
474{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100475 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200476 struct device_node *np = dev->of_node;
477 struct tegra_smmu *smmu = as->smmu;
478 struct of_phandle_args args;
479 unsigned int index = 0;
480
481 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
482 &args)) {
483 unsigned int swgroup = args.args[0];
484
485 if (args.np != smmu->dev->of_node) {
486 of_node_put(args.np);
487 continue;
488 }
489
490 of_node_put(args.np);
491
492 tegra_smmu_disable(smmu, swgroup, as->id);
493 tegra_smmu_as_unprepare(smmu, as);
494 index++;
495 }
496}
497
Russell King0b42c7c2015-07-27 13:29:21 +0100498static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova)
499{
500 u32 *pt = page_address(pt_page);
501
502 return pt + iova_pt_index(iova);
503}
504
505static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100506 dma_addr_t *dmap)
Russell King0b42c7c2015-07-27 13:29:21 +0100507{
508 unsigned int pd_index = iova_pd_index(iova);
509 struct page *pt_page;
Russell Kinge3c97192015-07-27 13:29:52 +0100510 u32 *pd;
Russell King0b42c7c2015-07-27 13:29:21 +0100511
Russell King853520f2015-07-27 13:29:26 +0100512 pt_page = as->pts[pd_index];
513 if (!pt_page)
Russell King0b42c7c2015-07-27 13:29:21 +0100514 return NULL;
515
Russell Kinge3c97192015-07-27 13:29:52 +0100516 pd = page_address(as->pd);
517 *dmap = smmu_pde_to_dma(pd[pd_index]);
Russell King0b42c7c2015-07-27 13:29:21 +0100518
519 return tegra_smmu_pte_offset(pt_page, iova);
520}
521
Thierry Reding89184652014-04-16 09:24:44 +0200522static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100523 dma_addr_t *dmap)
Thierry Reding89184652014-04-16 09:24:44 +0200524{
Russell King7ffc6f02015-08-06 14:56:39 +0200525 u32 *pd = page_address(as->pd);
Russell King34d35f82015-07-27 13:29:16 +0100526 unsigned int pde = iova_pd_index(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200527 struct tegra_smmu *smmu = as->smmu;
Thierry Reding89184652014-04-16 09:24:44 +0200528
Russell King853520f2015-07-27 13:29:26 +0100529 if (!as->pts[pde]) {
Russell Kinge3c97192015-07-27 13:29:52 +0100530 struct page *page;
531 dma_addr_t dma;
532
Russell King707917c2015-07-27 13:30:02 +0100533 page = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
Thierry Reding89184652014-04-16 09:24:44 +0200534 if (!page)
535 return NULL;
536
Russell Kinge3c97192015-07-27 13:29:52 +0100537 dma = dma_map_page(smmu->dev, page, 0, SMMU_SIZE_PT,
538 DMA_TO_DEVICE);
539 if (dma_mapping_error(smmu->dev, dma)) {
540 __free_page(page);
541 return NULL;
542 }
543
544 if (!smmu_dma_addr_valid(smmu, dma)) {
545 dma_unmap_page(smmu->dev, dma, SMMU_SIZE_PT,
546 DMA_TO_DEVICE);
547 __free_page(page);
548 return NULL;
549 }
550
Russell King853520f2015-07-27 13:29:26 +0100551 as->pts[pde] = page;
552
Russell Kinge3c97192015-07-27 13:29:52 +0100553 pd[pde] = SMMU_MK_PDE(dma, SMMU_PDE_ATTR | SMMU_PDE_NEXT);
Thierry Reding89184652014-04-16 09:24:44 +0200554
Russell Kinge3c97192015-07-27 13:29:52 +0100555 dma_sync_single_range_for_device(smmu->dev, as->pd_dma,
556 pde << 2, 4, DMA_TO_DEVICE);
557 smmu_flush_ptc(smmu, as->pd_dma, pde << 2);
Thierry Reding89184652014-04-16 09:24:44 +0200558 smmu_flush_tlb_section(smmu, as->id, iova);
559 smmu_flush(smmu);
Russell Kinge3c97192015-07-27 13:29:52 +0100560
561 *dmap = dma;
Thierry Reding89184652014-04-16 09:24:44 +0200562 } else {
Russell Kinge3c97192015-07-27 13:29:52 +0100563 *dmap = smmu_pde_to_dma(pd[pde]);
Thierry Reding89184652014-04-16 09:24:44 +0200564 }
565
Russell King7ffc6f02015-08-06 14:56:39 +0200566 return tegra_smmu_pte_offset(as->pts[pde], iova);
567}
Russell King0b42c7c2015-07-27 13:29:21 +0100568
Russell King7ffc6f02015-08-06 14:56:39 +0200569static void tegra_smmu_pte_get_use(struct tegra_smmu_as *as, unsigned long iova)
570{
571 unsigned int pd_index = iova_pd_index(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200572
Russell King7ffc6f02015-08-06 14:56:39 +0200573 as->count[pd_index]++;
Thierry Reding89184652014-04-16 09:24:44 +0200574}
575
Russell Kingb98e34f2015-07-27 13:29:05 +0100576static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
Thierry Reding89184652014-04-16 09:24:44 +0200577{
Russell Kingb98e34f2015-07-27 13:29:05 +0100578 struct tegra_smmu *smmu = as->smmu;
Russell King34d35f82015-07-27 13:29:16 +0100579 unsigned int pde = iova_pd_index(iova);
Russell Kingb98e34f2015-07-27 13:29:05 +0100580 u32 *pd = page_address(as->pd);
Russell King853520f2015-07-27 13:29:26 +0100581 struct page *page = as->pts[pde];
Thierry Reding89184652014-04-16 09:24:44 +0200582
583 /*
584 * When no entries in this page table are used anymore, return the
585 * memory page to the system.
586 */
Russell King32924c72015-07-27 13:29:31 +0100587 if (--as->count[pde] == 0) {
Russell Kingb98e34f2015-07-27 13:29:05 +0100588 unsigned int offset = pde * sizeof(*pd);
Russell Kinge3c97192015-07-27 13:29:52 +0100589 dma_addr_t pte_dma = smmu_pde_to_dma(pd[pde]);
Thierry Reding89184652014-04-16 09:24:44 +0200590
Russell Kingb98e34f2015-07-27 13:29:05 +0100591 /* Clear the page directory entry first */
592 pd[pde] = 0;
593
594 /* Flush the page directory entry */
Russell Kinge3c97192015-07-27 13:29:52 +0100595 dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset,
596 sizeof(*pd), DMA_TO_DEVICE);
597 smmu_flush_ptc(smmu, as->pd_dma, offset);
Russell Kingb98e34f2015-07-27 13:29:05 +0100598 smmu_flush_tlb_section(smmu, as->id, iova);
599 smmu_flush(smmu);
600
601 /* Finally, free the page */
Russell Kinge3c97192015-07-27 13:29:52 +0100602 dma_unmap_page(smmu->dev, pte_dma, SMMU_SIZE_PT, DMA_TO_DEVICE);
Russell Kingb98e34f2015-07-27 13:29:05 +0100603 __free_page(page);
Russell King853520f2015-07-27 13:29:26 +0100604 as->pts[pde] = NULL;
Thierry Reding89184652014-04-16 09:24:44 +0200605 }
606}
607
Russell King8482ee5e2015-07-27 13:29:10 +0100608static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100609 u32 *pte, dma_addr_t pte_dma, u32 val)
Russell King8482ee5e2015-07-27 13:29:10 +0100610{
611 struct tegra_smmu *smmu = as->smmu;
612 unsigned long offset = offset_in_page(pte);
613
614 *pte = val;
615
Russell Kinge3c97192015-07-27 13:29:52 +0100616 dma_sync_single_range_for_device(smmu->dev, pte_dma, offset,
617 4, DMA_TO_DEVICE);
618 smmu_flush_ptc(smmu, pte_dma, offset);
Russell King8482ee5e2015-07-27 13:29:10 +0100619 smmu_flush_tlb_group(smmu, as->id, iova);
620 smmu_flush(smmu);
621}
622
Thierry Reding89184652014-04-16 09:24:44 +0200623static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
624 phys_addr_t paddr, size_t size, int prot)
625{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100626 struct tegra_smmu_as *as = to_smmu_as(domain);
Russell Kinge3c97192015-07-27 13:29:52 +0100627 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200628 u32 *pte;
629
Russell Kinge3c97192015-07-27 13:29:52 +0100630 pte = as_get_pte(as, iova, &pte_dma);
Thierry Reding89184652014-04-16 09:24:44 +0200631 if (!pte)
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +0300632 return -ENOMEM;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200633
Russell King7ffc6f02015-08-06 14:56:39 +0200634 /* If we aren't overwriting a pre-existing entry, increment use */
635 if (*pte == 0)
636 tegra_smmu_pte_get_use(as, iova);
637
Russell Kinge3c97192015-07-27 13:29:52 +0100638 tegra_smmu_set_pte(as, iova, pte, pte_dma,
Russell King8482ee5e2015-07-27 13:29:10 +0100639 __phys_to_pfn(paddr) | SMMU_PTE_ATTR);
Thierry Reding89184652014-04-16 09:24:44 +0200640
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200641 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200642}
643
Thierry Reding89184652014-04-16 09:24:44 +0200644static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
645 size_t size)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200646{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100647 struct tegra_smmu_as *as = to_smmu_as(domain);
Russell Kinge3c97192015-07-27 13:29:52 +0100648 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200649 u32 *pte;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200650
Russell Kinge3c97192015-07-27 13:29:52 +0100651 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
Russell Kingb98e34f2015-07-27 13:29:05 +0100652 if (!pte || !*pte)
Thierry Reding89184652014-04-16 09:24:44 +0200653 return 0;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300654
Russell Kinge3c97192015-07-27 13:29:52 +0100655 tegra_smmu_set_pte(as, iova, pte, pte_dma, 0);
Russell Kingb98e34f2015-07-27 13:29:05 +0100656 tegra_smmu_pte_put_use(as, iova);
657
Thierry Reding89184652014-04-16 09:24:44 +0200658 return size;
659}
660
661static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
662 dma_addr_t iova)
663{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100664 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200665 unsigned long pfn;
Russell Kinge3c97192015-07-27 13:29:52 +0100666 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200667 u32 *pte;
668
Russell Kinge3c97192015-07-27 13:29:52 +0100669 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
Russell King91137852015-07-27 13:29:00 +0100670 if (!pte || !*pte)
671 return 0;
672
Thierry Reding804cb542015-03-27 11:07:27 +0100673 pfn = *pte & as->smmu->pfn_mask;
Thierry Reding89184652014-04-16 09:24:44 +0200674
675 return PFN_PHYS(pfn);
676}
677
678static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
679{
680 struct platform_device *pdev;
681 struct tegra_mc *mc;
682
683 pdev = of_find_device_by_node(np);
684 if (!pdev)
685 return NULL;
686
687 mc = platform_get_drvdata(pdev);
688 if (!mc)
689 return NULL;
690
691 return mc->smmu;
692}
693
694static int tegra_smmu_add_device(struct device *dev)
695{
696 struct device_node *np = dev->of_node;
697 struct of_phandle_args args;
698 unsigned int index = 0;
699
700 while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
701 &args) == 0) {
702 struct tegra_smmu *smmu;
703
704 smmu = tegra_smmu_find(args.np);
705 if (smmu) {
706 /*
707 * Only a single IOMMU master interface is currently
708 * supported by the Linux kernel, so abort after the
709 * first match.
710 */
711 dev->archdata.iommu = smmu;
712 break;
713 }
714
715 index++;
716 }
717
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200718 return 0;
719}
720
Thierry Reding89184652014-04-16 09:24:44 +0200721static void tegra_smmu_remove_device(struct device *dev)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200722{
Thierry Reding89184652014-04-16 09:24:44 +0200723 dev->archdata.iommu = NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200724}
725
Thierry Reding89184652014-04-16 09:24:44 +0200726static const struct iommu_ops tegra_smmu_ops = {
727 .capable = tegra_smmu_capable,
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100728 .domain_alloc = tegra_smmu_domain_alloc,
729 .domain_free = tegra_smmu_domain_free,
Thierry Reding89184652014-04-16 09:24:44 +0200730 .attach_dev = tegra_smmu_attach_dev,
731 .detach_dev = tegra_smmu_detach_dev,
732 .add_device = tegra_smmu_add_device,
733 .remove_device = tegra_smmu_remove_device,
734 .map = tegra_smmu_map,
735 .unmap = tegra_smmu_unmap,
736 .map_sg = default_iommu_map_sg,
737 .iova_to_phys = tegra_smmu_iova_to_phys,
738
739 .pgsize_bitmap = SZ_4K,
740};
741
742static void tegra_smmu_ahb_enable(void)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200743{
Thierry Reding89184652014-04-16 09:24:44 +0200744 static const struct of_device_id ahb_match[] = {
745 { .compatible = "nvidia,tegra30-ahb", },
746 { }
747 };
748 struct device_node *ahb;
749
750 ahb = of_find_matching_node(NULL, ahb_match);
751 if (ahb) {
752 tegra_ahb_enable_smmu(ahb);
753 of_node_put(ahb);
754 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200755}
756
Thierry Redingd1313e72015-01-23 09:49:25 +0100757static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
758{
759 struct tegra_smmu *smmu = s->private;
760 unsigned int i;
761 u32 value;
762
763 seq_printf(s, "swgroup enabled ASID\n");
764 seq_printf(s, "------------------------\n");
765
766 for (i = 0; i < smmu->soc->num_swgroups; i++) {
767 const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
768 const char *status;
769 unsigned int asid;
770
771 value = smmu_readl(smmu, group->reg);
772
773 if (value & SMMU_ASID_ENABLE)
774 status = "yes";
775 else
776 status = "no";
777
778 asid = value & SMMU_ASID_MASK;
779
780 seq_printf(s, "%-9s %-7s %#04x\n", group->name, status,
781 asid);
782 }
783
784 return 0;
785}
786
787static int tegra_smmu_swgroups_open(struct inode *inode, struct file *file)
788{
789 return single_open(file, tegra_smmu_swgroups_show, inode->i_private);
790}
791
792static const struct file_operations tegra_smmu_swgroups_fops = {
793 .open = tegra_smmu_swgroups_open,
794 .read = seq_read,
795 .llseek = seq_lseek,
796 .release = single_release,
797};
798
799static int tegra_smmu_clients_show(struct seq_file *s, void *data)
800{
801 struct tegra_smmu *smmu = s->private;
802 unsigned int i;
803 u32 value;
804
805 seq_printf(s, "client enabled\n");
806 seq_printf(s, "--------------------\n");
807
808 for (i = 0; i < smmu->soc->num_clients; i++) {
809 const struct tegra_mc_client *client = &smmu->soc->clients[i];
810 const char *status;
811
812 value = smmu_readl(smmu, client->smmu.reg);
813
814 if (value & BIT(client->smmu.bit))
815 status = "yes";
816 else
817 status = "no";
818
819 seq_printf(s, "%-12s %s\n", client->name, status);
820 }
821
822 return 0;
823}
824
825static int tegra_smmu_clients_open(struct inode *inode, struct file *file)
826{
827 return single_open(file, tegra_smmu_clients_show, inode->i_private);
828}
829
830static const struct file_operations tegra_smmu_clients_fops = {
831 .open = tegra_smmu_clients_open,
832 .read = seq_read,
833 .llseek = seq_lseek,
834 .release = single_release,
835};
836
837static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
838{
839 smmu->debugfs = debugfs_create_dir("smmu", NULL);
840 if (!smmu->debugfs)
841 return;
842
843 debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
844 &tegra_smmu_swgroups_fops);
845 debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
846 &tegra_smmu_clients_fops);
847}
848
849static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
850{
851 debugfs_remove_recursive(smmu->debugfs);
852}
853
Thierry Reding89184652014-04-16 09:24:44 +0200854struct tegra_smmu *tegra_smmu_probe(struct device *dev,
855 const struct tegra_smmu_soc *soc,
856 struct tegra_mc *mc)
857{
858 struct tegra_smmu *smmu;
859 size_t size;
860 u32 value;
861 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200862
Thierry Reding89184652014-04-16 09:24:44 +0200863 /* This can happen on Tegra20 which doesn't have an SMMU */
864 if (!soc)
865 return NULL;
866
867 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
868 if (!smmu)
869 return ERR_PTR(-ENOMEM);
870
871 /*
872 * This is a bit of a hack. Ideally we'd want to simply return this
873 * value. However the IOMMU registration process will attempt to add
874 * all devices to the IOMMU when bus_set_iommu() is called. In order
875 * not to rely on global variables to track the IOMMU instance, we
876 * set it here so that it can be looked up from the .add_device()
877 * callback via the IOMMU device's .drvdata field.
878 */
879 mc->smmu = smmu;
880
881 size = BITS_TO_LONGS(soc->num_asids) * sizeof(long);
882
883 smmu->asids = devm_kzalloc(dev, size, GFP_KERNEL);
884 if (!smmu->asids)
885 return ERR_PTR(-ENOMEM);
886
887 mutex_init(&smmu->lock);
888
889 smmu->regs = mc->regs;
890 smmu->soc = soc;
891 smmu->dev = dev;
892 smmu->mc = mc;
893
Thierry Reding804cb542015-03-27 11:07:27 +0100894 smmu->pfn_mask = BIT_MASK(mc->soc->num_address_bits - PAGE_SHIFT) - 1;
895 dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
896 mc->soc->num_address_bits, smmu->pfn_mask);
897
Thierry Reding89184652014-04-16 09:24:44 +0200898 value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
899
900 if (soc->supports_request_limit)
901 value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
902
903 smmu_writel(smmu, value, SMMU_PTC_CONFIG);
904
905 value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
906 SMMU_TLB_CONFIG_ACTIVE_LINES(0x20);
907
908 if (soc->supports_round_robin_arbitration)
909 value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
910
911 smmu_writel(smmu, value, SMMU_TLB_CONFIG);
912
Russell Kingb8fe0382015-07-27 13:29:41 +0100913 smmu_flush_ptc_all(smmu);
Thierry Reding89184652014-04-16 09:24:44 +0200914 smmu_flush_tlb(smmu);
915 smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
916 smmu_flush(smmu);
917
918 tegra_smmu_ahb_enable();
919
920 err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
921 if (err < 0)
922 return ERR_PTR(err);
923
Thierry Redingd1313e72015-01-23 09:49:25 +0100924 if (IS_ENABLED(CONFIG_DEBUG_FS))
925 tegra_smmu_debugfs_init(smmu);
926
Thierry Reding89184652014-04-16 09:24:44 +0200927 return smmu;
928}
Thierry Redingd1313e72015-01-23 09:49:25 +0100929
930void tegra_smmu_remove(struct tegra_smmu *smmu)
931{
932 if (IS_ENABLED(CONFIG_DEBUG_FS))
933 tegra_smmu_debugfs_exit(smmu);
934}