blob: eeb19f560a05ee54d87da40859e206626f1418a6 [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>
Joerg Roedel461a6942017-04-26 15:46:20 +020018#include <linux/dma-mapping.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020019
20#include <soc/tegra/ahb.h>
Thierry Reding89184652014-04-16 09:24:44 +020021#include <soc/tegra/mc.h>
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020022
Thierry Reding89184652014-04-16 09:24:44 +020023struct tegra_smmu {
24 void __iomem *regs;
25 struct device *dev;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020026
Thierry Reding89184652014-04-16 09:24:44 +020027 struct tegra_mc *mc;
28 const struct tegra_smmu_soc *soc;
Stephen Warrene6bc5932012-09-04 16:36:15 -060029
Thierry Reding804cb542015-03-27 11:07:27 +010030 unsigned long pfn_mask;
Thierry Reding11cec152015-08-06 14:20:31 +020031 unsigned long tlb_mask;
Thierry Reding804cb542015-03-27 11:07:27 +010032
Thierry Reding89184652014-04-16 09:24:44 +020033 unsigned long *asids;
34 struct mutex lock;
Stephen Warrene6bc5932012-09-04 16:36:15 -060035
Thierry Reding89184652014-04-16 09:24:44 +020036 struct list_head list;
Thierry Redingd1313e72015-01-23 09:49:25 +010037
38 struct dentry *debugfs;
Stephen Warrene6bc5932012-09-04 16:36:15 -060039};
40
Thierry Reding89184652014-04-16 09:24:44 +020041struct tegra_smmu_as {
Joerg Roedeld5f1a812015-03-26 13:43:12 +010042 struct iommu_domain domain;
Thierry Reding89184652014-04-16 09:24:44 +020043 struct tegra_smmu *smmu;
44 unsigned int use_count;
Russell King32924c72015-07-27 13:29:31 +010045 u32 *count;
Russell King853520f2015-07-27 13:29:26 +010046 struct page **pts;
Thierry Reding89184652014-04-16 09:24:44 +020047 struct page *pd;
Russell Kinge3c97192015-07-27 13:29:52 +010048 dma_addr_t pd_dma;
Thierry Reding89184652014-04-16 09:24:44 +020049 unsigned id;
50 u32 attr;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +030051};
52
Joerg Roedeld5f1a812015-03-26 13:43:12 +010053static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom)
54{
55 return container_of(dom, struct tegra_smmu_as, domain);
56}
57
Thierry Reding89184652014-04-16 09:24:44 +020058static inline void smmu_writel(struct tegra_smmu *smmu, u32 value,
59 unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020060{
Thierry Reding89184652014-04-16 09:24:44 +020061 writel(value, smmu->regs + offset);
Joerg Roedelfe1229b2013-02-04 20:40:58 +010062}
63
Thierry Reding89184652014-04-16 09:24:44 +020064static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020065{
Thierry Reding89184652014-04-16 09:24:44 +020066 return readl(smmu->regs + offset);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020067}
68
Thierry Reding89184652014-04-16 09:24:44 +020069#define SMMU_CONFIG 0x010
70#define SMMU_CONFIG_ENABLE (1 << 0)
71
72#define SMMU_TLB_CONFIG 0x14
73#define SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29)
74#define SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28)
Thierry Reding11cec152015-08-06 14:20:31 +020075#define SMMU_TLB_CONFIG_ACTIVE_LINES(smmu) \
76 ((smmu)->soc->num_tlb_lines & (smmu)->tlb_mask)
Thierry Reding89184652014-04-16 09:24:44 +020077
78#define SMMU_PTC_CONFIG 0x18
79#define SMMU_PTC_CONFIG_ENABLE (1 << 29)
80#define SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24)
81#define SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f)
82
83#define SMMU_PTB_ASID 0x01c
84#define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
85
86#define SMMU_PTB_DATA 0x020
Russell Kinge3c97192015-07-27 13:29:52 +010087#define SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr))
Thierry Reding89184652014-04-16 09:24:44 +020088
Russell Kinge3c97192015-07-27 13:29:52 +010089#define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr))
Thierry Reding89184652014-04-16 09:24:44 +020090
91#define SMMU_TLB_FLUSH 0x030
92#define SMMU_TLB_FLUSH_VA_MATCH_ALL (0 << 0)
93#define SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0)
94#define SMMU_TLB_FLUSH_VA_MATCH_GROUP (3 << 0)
95#define SMMU_TLB_FLUSH_ASID(x) (((x) & 0x7f) << 24)
96#define SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
97 SMMU_TLB_FLUSH_VA_MATCH_SECTION)
98#define SMMU_TLB_FLUSH_VA_GROUP(addr) ((((addr) & 0xffffc000) >> 12) | \
99 SMMU_TLB_FLUSH_VA_MATCH_GROUP)
100#define SMMU_TLB_FLUSH_ASID_MATCH (1 << 31)
101
102#define SMMU_PTC_FLUSH 0x034
103#define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
104#define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
105
106#define SMMU_PTC_FLUSH_HI 0x9b8
107#define SMMU_PTC_FLUSH_HI_MASK 0x3
108
109/* per-SWGROUP SMMU_*_ASID register */
110#define SMMU_ASID_ENABLE (1 << 31)
111#define SMMU_ASID_MASK 0x7f
112#define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
113
114/* page table definitions */
115#define SMMU_NUM_PDE 1024
116#define SMMU_NUM_PTE 1024
117
118#define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
119#define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
120
121#define SMMU_PDE_SHIFT 22
122#define SMMU_PTE_SHIFT 12
123
Thierry Reding89184652014-04-16 09:24:44 +0200124#define SMMU_PD_READABLE (1 << 31)
125#define SMMU_PD_WRITABLE (1 << 30)
126#define SMMU_PD_NONSECURE (1 << 29)
127
128#define SMMU_PDE_READABLE (1 << 31)
129#define SMMU_PDE_WRITABLE (1 << 30)
130#define SMMU_PDE_NONSECURE (1 << 29)
131#define SMMU_PDE_NEXT (1 << 28)
132
133#define SMMU_PTE_READABLE (1 << 31)
134#define SMMU_PTE_WRITABLE (1 << 30)
135#define SMMU_PTE_NONSECURE (1 << 29)
136
137#define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
138 SMMU_PDE_NONSECURE)
139#define SMMU_PTE_ATTR (SMMU_PTE_READABLE | SMMU_PTE_WRITABLE | \
140 SMMU_PTE_NONSECURE)
141
Russell King34d35f82015-07-27 13:29:16 +0100142static unsigned int iova_pd_index(unsigned long iova)
143{
144 return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1);
145}
146
147static unsigned int iova_pt_index(unsigned long iova)
148{
149 return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1);
150}
151
Russell Kinge3c97192015-07-27 13:29:52 +0100152static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr)
Russell King4b3c7d12015-07-27 13:29:36 +0100153{
Russell Kinge3c97192015-07-27 13:29:52 +0100154 addr >>= 12;
155 return (addr & smmu->pfn_mask) == addr;
156}
Russell King4b3c7d12015-07-27 13:29:36 +0100157
Russell Kinge3c97192015-07-27 13:29:52 +0100158static dma_addr_t smmu_pde_to_dma(u32 pde)
159{
160 return pde << 12;
Russell King4b3c7d12015-07-27 13:29:36 +0100161}
162
Russell Kingb8fe0382015-07-27 13:29:41 +0100163static void smmu_flush_ptc_all(struct tegra_smmu *smmu)
164{
165 smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
166}
167
Russell Kinge3c97192015-07-27 13:29:52 +0100168static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma,
Thierry Reding89184652014-04-16 09:24:44 +0200169 unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200170{
Thierry Reding89184652014-04-16 09:24:44 +0200171 u32 value;
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200172
Russell Kingb8fe0382015-07-27 13:29:41 +0100173 offset &= ~(smmu->mc->soc->atom_size - 1);
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200174
Russell Kingb8fe0382015-07-27 13:29:41 +0100175 if (smmu->mc->soc->num_address_bits > 32) {
Russell Kinge3c97192015-07-27 13:29:52 +0100176#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
177 value = (dma >> 32) & SMMU_PTC_FLUSH_HI_MASK;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200178#else
Russell Kingb8fe0382015-07-27 13:29:41 +0100179 value = 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200180#endif
Russell Kingb8fe0382015-07-27 13:29:41 +0100181 smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200182 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300183
Russell Kinge3c97192015-07-27 13:29:52 +0100184 value = (dma + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
Thierry Reding89184652014-04-16 09:24:44 +0200185 smmu_writel(smmu, value, SMMU_PTC_FLUSH);
186}
187
188static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
189{
190 smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
191}
192
193static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
194 unsigned long asid)
195{
196 u32 value;
197
198 value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
199 SMMU_TLB_FLUSH_VA_MATCH_ALL;
200 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
201}
202
203static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
204 unsigned long asid,
205 unsigned long iova)
206{
207 u32 value;
208
209 value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
210 SMMU_TLB_FLUSH_VA_SECTION(iova);
211 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
212}
213
214static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu,
215 unsigned long asid,
216 unsigned long iova)
217{
218 u32 value;
219
220 value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
221 SMMU_TLB_FLUSH_VA_GROUP(iova);
222 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
223}
224
225static inline void smmu_flush(struct tegra_smmu *smmu)
226{
227 smmu_readl(smmu, SMMU_CONFIG);
228}
229
230static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
231{
232 unsigned long id;
233
234 mutex_lock(&smmu->lock);
235
236 id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
237 if (id >= smmu->soc->num_asids) {
238 mutex_unlock(&smmu->lock);
239 return -ENOSPC;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200240 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300241
Thierry Reding89184652014-04-16 09:24:44 +0200242 set_bit(id, smmu->asids);
243 *idp = id;
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300244
Thierry Reding89184652014-04-16 09:24:44 +0200245 mutex_unlock(&smmu->lock);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200246 return 0;
247}
248
Thierry Reding89184652014-04-16 09:24:44 +0200249static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200250{
Thierry Reding89184652014-04-16 09:24:44 +0200251 mutex_lock(&smmu->lock);
252 clear_bit(id, smmu->asids);
253 mutex_unlock(&smmu->lock);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200254}
255
Thierry Reding89184652014-04-16 09:24:44 +0200256static bool tegra_smmu_capable(enum iommu_cap cap)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200257{
Joerg Roedel7c2aa642014-09-05 10:51:37 +0200258 return false;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200259}
260
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100261static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200262{
Thierry Reding89184652014-04-16 09:24:44 +0200263 struct tegra_smmu_as *as;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200264
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100265 if (type != IOMMU_DOMAIN_UNMANAGED)
266 return NULL;
267
Thierry Reding89184652014-04-16 09:24:44 +0200268 as = kzalloc(sizeof(*as), GFP_KERNEL);
269 if (!as)
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100270 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200271
Thierry Reding89184652014-04-16 09:24:44 +0200272 as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200273
Russell King707917c2015-07-27 13:30:02 +0100274 as->pd = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
Thierry Reding89184652014-04-16 09:24:44 +0200275 if (!as->pd) {
276 kfree(as);
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100277 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200278 }
279
Russell King32924c72015-07-27 13:29:31 +0100280 as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL);
Thierry Reding89184652014-04-16 09:24:44 +0200281 if (!as->count) {
282 __free_page(as->pd);
283 kfree(as);
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100284 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200285 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200286
Russell King853520f2015-07-27 13:29:26 +0100287 as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL);
288 if (!as->pts) {
Russell King32924c72015-07-27 13:29:31 +0100289 kfree(as->count);
Russell King853520f2015-07-27 13:29:26 +0100290 __free_page(as->pd);
291 kfree(as);
292 return NULL;
293 }
294
Thierry Reding471d9142015-03-27 11:07:25 +0100295 /* setup aperture */
Joerg Roedel7f65ef02015-04-02 13:33:19 +0200296 as->domain.geometry.aperture_start = 0;
297 as->domain.geometry.aperture_end = 0xffffffff;
298 as->domain.geometry.force_aperture = true;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200299
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100300 return &as->domain;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200301}
302
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100303static void tegra_smmu_domain_free(struct iommu_domain *domain)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200304{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100305 struct tegra_smmu_as *as = to_smmu_as(domain);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200306
Thierry Reding89184652014-04-16 09:24:44 +0200307 /* TODO: free page directory and page tables */
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200308
Thierry Reding89184652014-04-16 09:24:44 +0200309 kfree(as);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200310}
311
Thierry Reding89184652014-04-16 09:24:44 +0200312static const struct tegra_smmu_swgroup *
313tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300314{
Thierry Reding89184652014-04-16 09:24:44 +0200315 const struct tegra_smmu_swgroup *group = NULL;
316 unsigned int i;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300317
Thierry Reding89184652014-04-16 09:24:44 +0200318 for (i = 0; i < smmu->soc->num_swgroups; i++) {
319 if (smmu->soc->swgroups[i].swgroup == swgroup) {
320 group = &smmu->soc->swgroups[i];
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300321 break;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300322 }
323 }
324
Thierry Reding89184652014-04-16 09:24:44 +0200325 return group;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300326}
327
Thierry Reding89184652014-04-16 09:24:44 +0200328static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
329 unsigned int asid)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200330{
Thierry Reding89184652014-04-16 09:24:44 +0200331 const struct tegra_smmu_swgroup *group;
332 unsigned int i;
333 u32 value;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200334
Thierry Reding89184652014-04-16 09:24:44 +0200335 for (i = 0; i < smmu->soc->num_clients; i++) {
336 const struct tegra_mc_client *client = &smmu->soc->clients[i];
337
338 if (client->swgroup != swgroup)
339 continue;
340
341 value = smmu_readl(smmu, client->smmu.reg);
342 value |= BIT(client->smmu.bit);
343 smmu_writel(smmu, value, client->smmu.reg);
344 }
345
346 group = tegra_smmu_find_swgroup(smmu, swgroup);
347 if (group) {
348 value = smmu_readl(smmu, group->reg);
349 value &= ~SMMU_ASID_MASK;
350 value |= SMMU_ASID_VALUE(asid);
351 value |= SMMU_ASID_ENABLE;
352 smmu_writel(smmu, value, group->reg);
353 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200354}
355
Thierry Reding89184652014-04-16 09:24:44 +0200356static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
357 unsigned int asid)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200358{
Thierry Reding89184652014-04-16 09:24:44 +0200359 const struct tegra_smmu_swgroup *group;
360 unsigned int i;
361 u32 value;
362
363 group = tegra_smmu_find_swgroup(smmu, swgroup);
364 if (group) {
365 value = smmu_readl(smmu, group->reg);
366 value &= ~SMMU_ASID_MASK;
367 value |= SMMU_ASID_VALUE(asid);
368 value &= ~SMMU_ASID_ENABLE;
369 smmu_writel(smmu, value, group->reg);
370 }
371
372 for (i = 0; i < smmu->soc->num_clients; i++) {
373 const struct tegra_mc_client *client = &smmu->soc->clients[i];
374
375 if (client->swgroup != swgroup)
376 continue;
377
378 value = smmu_readl(smmu, client->smmu.reg);
379 value &= ~BIT(client->smmu.bit);
380 smmu_writel(smmu, value, client->smmu.reg);
381 }
382}
383
384static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
385 struct tegra_smmu_as *as)
386{
387 u32 value;
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300388 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200389
Thierry Reding89184652014-04-16 09:24:44 +0200390 if (as->use_count > 0) {
391 as->use_count++;
392 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200393 }
394
Russell Kinge3c97192015-07-27 13:29:52 +0100395 as->pd_dma = dma_map_page(smmu->dev, as->pd, 0, SMMU_SIZE_PD,
396 DMA_TO_DEVICE);
397 if (dma_mapping_error(smmu->dev, as->pd_dma))
398 return -ENOMEM;
399
400 /* We can't handle 64-bit DMA addresses */
401 if (!smmu_dma_addr_valid(smmu, as->pd_dma)) {
402 err = -ENOMEM;
403 goto err_unmap;
404 }
405
Thierry Reding89184652014-04-16 09:24:44 +0200406 err = tegra_smmu_alloc_asid(smmu, &as->id);
407 if (err < 0)
Russell Kinge3c97192015-07-27 13:29:52 +0100408 goto err_unmap;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200409
Russell Kinge3c97192015-07-27 13:29:52 +0100410 smmu_flush_ptc(smmu, as->pd_dma, 0);
Thierry Reding89184652014-04-16 09:24:44 +0200411 smmu_flush_tlb_asid(smmu, as->id);
412
413 smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
Russell Kinge3c97192015-07-27 13:29:52 +0100414 value = SMMU_PTB_DATA_VALUE(as->pd_dma, as->attr);
Thierry Reding89184652014-04-16 09:24:44 +0200415 smmu_writel(smmu, value, SMMU_PTB_DATA);
416 smmu_flush(smmu);
417
418 as->smmu = smmu;
419 as->use_count++;
420
421 return 0;
Russell Kinge3c97192015-07-27 13:29:52 +0100422
423err_unmap:
424 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
425 return err;
Thierry Reding89184652014-04-16 09:24:44 +0200426}
427
428static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
429 struct tegra_smmu_as *as)
430{
431 if (--as->use_count > 0)
432 return;
433
434 tegra_smmu_free_asid(smmu, as->id);
Russell Kinge3c97192015-07-27 13:29:52 +0100435
436 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
437
Thierry Reding89184652014-04-16 09:24:44 +0200438 as->smmu = NULL;
439}
440
441static int tegra_smmu_attach_dev(struct iommu_domain *domain,
442 struct device *dev)
443{
444 struct tegra_smmu *smmu = dev->archdata.iommu;
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100445 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200446 struct device_node *np = dev->of_node;
447 struct of_phandle_args args;
448 unsigned int index = 0;
449 int err = 0;
450
451 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
452 &args)) {
453 unsigned int swgroup = args.args[0];
454
455 if (args.np != smmu->dev->of_node) {
456 of_node_put(args.np);
457 continue;
458 }
459
460 of_node_put(args.np);
461
462 err = tegra_smmu_as_prepare(smmu, as);
463 if (err < 0)
464 return err;
465
466 tegra_smmu_enable(smmu, swgroup, as->id);
467 index++;
468 }
469
470 if (index == 0)
471 return -ENODEV;
472
473 return 0;
474}
475
476static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
477{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100478 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200479 struct device_node *np = dev->of_node;
480 struct tegra_smmu *smmu = as->smmu;
481 struct of_phandle_args args;
482 unsigned int index = 0;
483
484 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
485 &args)) {
486 unsigned int swgroup = args.args[0];
487
488 if (args.np != smmu->dev->of_node) {
489 of_node_put(args.np);
490 continue;
491 }
492
493 of_node_put(args.np);
494
495 tegra_smmu_disable(smmu, swgroup, as->id);
496 tegra_smmu_as_unprepare(smmu, as);
497 index++;
498 }
499}
500
Russell King4080e992015-07-27 13:30:12 +0100501static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova,
502 u32 value)
503{
504 unsigned int pd_index = iova_pd_index(iova);
505 struct tegra_smmu *smmu = as->smmu;
506 u32 *pd = page_address(as->pd);
507 unsigned long offset = pd_index * sizeof(*pd);
508
509 /* Set the page directory entry first */
510 pd[pd_index] = value;
511
512 /* The flush the page directory entry from caches */
513 dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset,
514 sizeof(*pd), DMA_TO_DEVICE);
515
516 /* And flush the iommu */
517 smmu_flush_ptc(smmu, as->pd_dma, offset);
518 smmu_flush_tlb_section(smmu, as->id, iova);
519 smmu_flush(smmu);
520}
521
Russell King0b42c7c2015-07-27 13:29:21 +0100522static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova)
523{
524 u32 *pt = page_address(pt_page);
525
526 return pt + iova_pt_index(iova);
527}
528
529static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100530 dma_addr_t *dmap)
Russell King0b42c7c2015-07-27 13:29:21 +0100531{
532 unsigned int pd_index = iova_pd_index(iova);
533 struct page *pt_page;
Russell Kinge3c97192015-07-27 13:29:52 +0100534 u32 *pd;
Russell King0b42c7c2015-07-27 13:29:21 +0100535
Russell King853520f2015-07-27 13:29:26 +0100536 pt_page = as->pts[pd_index];
537 if (!pt_page)
Russell King0b42c7c2015-07-27 13:29:21 +0100538 return NULL;
539
Russell Kinge3c97192015-07-27 13:29:52 +0100540 pd = page_address(as->pd);
541 *dmap = smmu_pde_to_dma(pd[pd_index]);
Russell King0b42c7c2015-07-27 13:29:21 +0100542
543 return tegra_smmu_pte_offset(pt_page, iova);
544}
545
Thierry Reding89184652014-04-16 09:24:44 +0200546static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100547 dma_addr_t *dmap)
Thierry Reding89184652014-04-16 09:24:44 +0200548{
Russell King34d35f82015-07-27 13:29:16 +0100549 unsigned int pde = iova_pd_index(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200550 struct tegra_smmu *smmu = as->smmu;
Thierry Reding89184652014-04-16 09:24:44 +0200551
Russell King853520f2015-07-27 13:29:26 +0100552 if (!as->pts[pde]) {
Russell Kinge3c97192015-07-27 13:29:52 +0100553 struct page *page;
554 dma_addr_t dma;
555
Russell King707917c2015-07-27 13:30:02 +0100556 page = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
Thierry Reding89184652014-04-16 09:24:44 +0200557 if (!page)
558 return NULL;
559
Russell Kinge3c97192015-07-27 13:29:52 +0100560 dma = dma_map_page(smmu->dev, page, 0, SMMU_SIZE_PT,
561 DMA_TO_DEVICE);
562 if (dma_mapping_error(smmu->dev, dma)) {
563 __free_page(page);
564 return NULL;
565 }
566
567 if (!smmu_dma_addr_valid(smmu, dma)) {
568 dma_unmap_page(smmu->dev, dma, SMMU_SIZE_PT,
569 DMA_TO_DEVICE);
570 __free_page(page);
571 return NULL;
572 }
573
Russell King853520f2015-07-27 13:29:26 +0100574 as->pts[pde] = page;
575
Russell King4080e992015-07-27 13:30:12 +0100576 tegra_smmu_set_pde(as, iova, SMMU_MK_PDE(dma, SMMU_PDE_ATTR |
577 SMMU_PDE_NEXT));
Russell Kinge3c97192015-07-27 13:29:52 +0100578
579 *dmap = dma;
Thierry Reding89184652014-04-16 09:24:44 +0200580 } else {
Russell King4080e992015-07-27 13:30:12 +0100581 u32 *pd = page_address(as->pd);
582
Russell Kinge3c97192015-07-27 13:29:52 +0100583 *dmap = smmu_pde_to_dma(pd[pde]);
Thierry Reding89184652014-04-16 09:24:44 +0200584 }
585
Russell King7ffc6f02015-08-06 14:56:39 +0200586 return tegra_smmu_pte_offset(as->pts[pde], iova);
587}
Russell King0b42c7c2015-07-27 13:29:21 +0100588
Russell King7ffc6f02015-08-06 14:56:39 +0200589static void tegra_smmu_pte_get_use(struct tegra_smmu_as *as, unsigned long iova)
590{
591 unsigned int pd_index = iova_pd_index(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200592
Russell King7ffc6f02015-08-06 14:56:39 +0200593 as->count[pd_index]++;
Thierry Reding89184652014-04-16 09:24:44 +0200594}
595
Russell Kingb98e34f2015-07-27 13:29:05 +0100596static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
Thierry Reding89184652014-04-16 09:24:44 +0200597{
Russell King34d35f82015-07-27 13:29:16 +0100598 unsigned int pde = iova_pd_index(iova);
Russell King853520f2015-07-27 13:29:26 +0100599 struct page *page = as->pts[pde];
Thierry Reding89184652014-04-16 09:24:44 +0200600
601 /*
602 * When no entries in this page table are used anymore, return the
603 * memory page to the system.
604 */
Russell King32924c72015-07-27 13:29:31 +0100605 if (--as->count[pde] == 0) {
Russell King4080e992015-07-27 13:30:12 +0100606 struct tegra_smmu *smmu = as->smmu;
607 u32 *pd = page_address(as->pd);
Russell Kinge3c97192015-07-27 13:29:52 +0100608 dma_addr_t pte_dma = smmu_pde_to_dma(pd[pde]);
Thierry Reding89184652014-04-16 09:24:44 +0200609
Russell King4080e992015-07-27 13:30:12 +0100610 tegra_smmu_set_pde(as, iova, 0);
Russell Kingb98e34f2015-07-27 13:29:05 +0100611
Russell Kinge3c97192015-07-27 13:29:52 +0100612 dma_unmap_page(smmu->dev, pte_dma, SMMU_SIZE_PT, DMA_TO_DEVICE);
Russell Kingb98e34f2015-07-27 13:29:05 +0100613 __free_page(page);
Russell King853520f2015-07-27 13:29:26 +0100614 as->pts[pde] = NULL;
Thierry Reding89184652014-04-16 09:24:44 +0200615 }
616}
617
Russell King8482ee52015-07-27 13:29:10 +0100618static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100619 u32 *pte, dma_addr_t pte_dma, u32 val)
Russell King8482ee52015-07-27 13:29:10 +0100620{
621 struct tegra_smmu *smmu = as->smmu;
622 unsigned long offset = offset_in_page(pte);
623
624 *pte = val;
625
Russell Kinge3c97192015-07-27 13:29:52 +0100626 dma_sync_single_range_for_device(smmu->dev, pte_dma, offset,
627 4, DMA_TO_DEVICE);
628 smmu_flush_ptc(smmu, pte_dma, offset);
Russell King8482ee52015-07-27 13:29:10 +0100629 smmu_flush_tlb_group(smmu, as->id, iova);
630 smmu_flush(smmu);
631}
632
Thierry Reding89184652014-04-16 09:24:44 +0200633static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
634 phys_addr_t paddr, size_t size, int prot)
635{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100636 struct tegra_smmu_as *as = to_smmu_as(domain);
Russell Kinge3c97192015-07-27 13:29:52 +0100637 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200638 u32 *pte;
639
Russell Kinge3c97192015-07-27 13:29:52 +0100640 pte = as_get_pte(as, iova, &pte_dma);
Thierry Reding89184652014-04-16 09:24:44 +0200641 if (!pte)
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +0300642 return -ENOMEM;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200643
Russell King7ffc6f02015-08-06 14:56:39 +0200644 /* If we aren't overwriting a pre-existing entry, increment use */
645 if (*pte == 0)
646 tegra_smmu_pte_get_use(as, iova);
647
Russell Kinge3c97192015-07-27 13:29:52 +0100648 tegra_smmu_set_pte(as, iova, pte, pte_dma,
Russell King8482ee52015-07-27 13:29:10 +0100649 __phys_to_pfn(paddr) | SMMU_PTE_ATTR);
Thierry Reding89184652014-04-16 09:24:44 +0200650
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200651 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200652}
653
Thierry Reding89184652014-04-16 09:24:44 +0200654static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
655 size_t size)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200656{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100657 struct tegra_smmu_as *as = to_smmu_as(domain);
Russell Kinge3c97192015-07-27 13:29:52 +0100658 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200659 u32 *pte;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200660
Russell Kinge3c97192015-07-27 13:29:52 +0100661 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
Russell Kingb98e34f2015-07-27 13:29:05 +0100662 if (!pte || !*pte)
Thierry Reding89184652014-04-16 09:24:44 +0200663 return 0;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300664
Russell Kinge3c97192015-07-27 13:29:52 +0100665 tegra_smmu_set_pte(as, iova, pte, pte_dma, 0);
Russell Kingb98e34f2015-07-27 13:29:05 +0100666 tegra_smmu_pte_put_use(as, iova);
667
Thierry Reding89184652014-04-16 09:24:44 +0200668 return size;
669}
670
671static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
672 dma_addr_t iova)
673{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100674 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200675 unsigned long pfn;
Russell Kinge3c97192015-07-27 13:29:52 +0100676 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200677 u32 *pte;
678
Russell Kinge3c97192015-07-27 13:29:52 +0100679 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
Russell King91137852015-07-27 13:29:00 +0100680 if (!pte || !*pte)
681 return 0;
682
Thierry Reding804cb542015-03-27 11:07:27 +0100683 pfn = *pte & as->smmu->pfn_mask;
Thierry Reding89184652014-04-16 09:24:44 +0200684
685 return PFN_PHYS(pfn);
686}
687
688static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
689{
690 struct platform_device *pdev;
691 struct tegra_mc *mc;
692
693 pdev = of_find_device_by_node(np);
694 if (!pdev)
695 return NULL;
696
697 mc = platform_get_drvdata(pdev);
698 if (!mc)
699 return NULL;
700
701 return mc->smmu;
702}
703
704static int tegra_smmu_add_device(struct device *dev)
705{
706 struct device_node *np = dev->of_node;
707 struct of_phandle_args args;
708 unsigned int index = 0;
709
710 while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
711 &args) == 0) {
712 struct tegra_smmu *smmu;
713
714 smmu = tegra_smmu_find(args.np);
715 if (smmu) {
716 /*
717 * Only a single IOMMU master interface is currently
718 * supported by the Linux kernel, so abort after the
719 * first match.
720 */
721 dev->archdata.iommu = smmu;
722 break;
723 }
724
725 index++;
726 }
727
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200728 return 0;
729}
730
Thierry Reding89184652014-04-16 09:24:44 +0200731static void tegra_smmu_remove_device(struct device *dev)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200732{
Thierry Reding89184652014-04-16 09:24:44 +0200733 dev->archdata.iommu = NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200734}
735
Thierry Reding89184652014-04-16 09:24:44 +0200736static const struct iommu_ops tegra_smmu_ops = {
737 .capable = tegra_smmu_capable,
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100738 .domain_alloc = tegra_smmu_domain_alloc,
739 .domain_free = tegra_smmu_domain_free,
Thierry Reding89184652014-04-16 09:24:44 +0200740 .attach_dev = tegra_smmu_attach_dev,
741 .detach_dev = tegra_smmu_detach_dev,
742 .add_device = tegra_smmu_add_device,
743 .remove_device = tegra_smmu_remove_device,
744 .map = tegra_smmu_map,
745 .unmap = tegra_smmu_unmap,
746 .map_sg = default_iommu_map_sg,
747 .iova_to_phys = tegra_smmu_iova_to_phys,
748
749 .pgsize_bitmap = SZ_4K,
750};
751
752static void tegra_smmu_ahb_enable(void)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200753{
Thierry Reding89184652014-04-16 09:24:44 +0200754 static const struct of_device_id ahb_match[] = {
755 { .compatible = "nvidia,tegra30-ahb", },
756 { }
757 };
758 struct device_node *ahb;
759
760 ahb = of_find_matching_node(NULL, ahb_match);
761 if (ahb) {
762 tegra_ahb_enable_smmu(ahb);
763 of_node_put(ahb);
764 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200765}
766
Thierry Redingd1313e72015-01-23 09:49:25 +0100767static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
768{
769 struct tegra_smmu *smmu = s->private;
770 unsigned int i;
771 u32 value;
772
773 seq_printf(s, "swgroup enabled ASID\n");
774 seq_printf(s, "------------------------\n");
775
776 for (i = 0; i < smmu->soc->num_swgroups; i++) {
777 const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
778 const char *status;
779 unsigned int asid;
780
781 value = smmu_readl(smmu, group->reg);
782
783 if (value & SMMU_ASID_ENABLE)
784 status = "yes";
785 else
786 status = "no";
787
788 asid = value & SMMU_ASID_MASK;
789
790 seq_printf(s, "%-9s %-7s %#04x\n", group->name, status,
791 asid);
792 }
793
794 return 0;
795}
796
797static int tegra_smmu_swgroups_open(struct inode *inode, struct file *file)
798{
799 return single_open(file, tegra_smmu_swgroups_show, inode->i_private);
800}
801
802static const struct file_operations tegra_smmu_swgroups_fops = {
803 .open = tegra_smmu_swgroups_open,
804 .read = seq_read,
805 .llseek = seq_lseek,
806 .release = single_release,
807};
808
809static int tegra_smmu_clients_show(struct seq_file *s, void *data)
810{
811 struct tegra_smmu *smmu = s->private;
812 unsigned int i;
813 u32 value;
814
815 seq_printf(s, "client enabled\n");
816 seq_printf(s, "--------------------\n");
817
818 for (i = 0; i < smmu->soc->num_clients; i++) {
819 const struct tegra_mc_client *client = &smmu->soc->clients[i];
820 const char *status;
821
822 value = smmu_readl(smmu, client->smmu.reg);
823
824 if (value & BIT(client->smmu.bit))
825 status = "yes";
826 else
827 status = "no";
828
829 seq_printf(s, "%-12s %s\n", client->name, status);
830 }
831
832 return 0;
833}
834
835static int tegra_smmu_clients_open(struct inode *inode, struct file *file)
836{
837 return single_open(file, tegra_smmu_clients_show, inode->i_private);
838}
839
840static const struct file_operations tegra_smmu_clients_fops = {
841 .open = tegra_smmu_clients_open,
842 .read = seq_read,
843 .llseek = seq_lseek,
844 .release = single_release,
845};
846
847static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
848{
849 smmu->debugfs = debugfs_create_dir("smmu", NULL);
850 if (!smmu->debugfs)
851 return;
852
853 debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
854 &tegra_smmu_swgroups_fops);
855 debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
856 &tegra_smmu_clients_fops);
857}
858
859static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
860{
861 debugfs_remove_recursive(smmu->debugfs);
862}
863
Thierry Reding89184652014-04-16 09:24:44 +0200864struct tegra_smmu *tegra_smmu_probe(struct device *dev,
865 const struct tegra_smmu_soc *soc,
866 struct tegra_mc *mc)
867{
868 struct tegra_smmu *smmu;
869 size_t size;
870 u32 value;
871 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200872
Thierry Reding89184652014-04-16 09:24:44 +0200873 /* This can happen on Tegra20 which doesn't have an SMMU */
874 if (!soc)
875 return NULL;
876
877 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
878 if (!smmu)
879 return ERR_PTR(-ENOMEM);
880
881 /*
882 * This is a bit of a hack. Ideally we'd want to simply return this
883 * value. However the IOMMU registration process will attempt to add
884 * all devices to the IOMMU when bus_set_iommu() is called. In order
885 * not to rely on global variables to track the IOMMU instance, we
886 * set it here so that it can be looked up from the .add_device()
887 * callback via the IOMMU device's .drvdata field.
888 */
889 mc->smmu = smmu;
890
891 size = BITS_TO_LONGS(soc->num_asids) * sizeof(long);
892
893 smmu->asids = devm_kzalloc(dev, size, GFP_KERNEL);
894 if (!smmu->asids)
895 return ERR_PTR(-ENOMEM);
896
897 mutex_init(&smmu->lock);
898
899 smmu->regs = mc->regs;
900 smmu->soc = soc;
901 smmu->dev = dev;
902 smmu->mc = mc;
903
Thierry Reding804cb542015-03-27 11:07:27 +0100904 smmu->pfn_mask = BIT_MASK(mc->soc->num_address_bits - PAGE_SHIFT) - 1;
905 dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
906 mc->soc->num_address_bits, smmu->pfn_mask);
Thierry Reding11cec152015-08-06 14:20:31 +0200907 smmu->tlb_mask = (smmu->soc->num_tlb_lines << 1) - 1;
908 dev_dbg(dev, "TLB lines: %u, mask: %#lx\n", smmu->soc->num_tlb_lines,
909 smmu->tlb_mask);
Thierry Reding804cb542015-03-27 11:07:27 +0100910
Thierry Reding89184652014-04-16 09:24:44 +0200911 value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
912
913 if (soc->supports_request_limit)
914 value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
915
916 smmu_writel(smmu, value, SMMU_PTC_CONFIG);
917
918 value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
Thierry Reding11cec152015-08-06 14:20:31 +0200919 SMMU_TLB_CONFIG_ACTIVE_LINES(smmu);
Thierry Reding89184652014-04-16 09:24:44 +0200920
921 if (soc->supports_round_robin_arbitration)
922 value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
923
924 smmu_writel(smmu, value, SMMU_TLB_CONFIG);
925
Russell Kingb8fe0382015-07-27 13:29:41 +0100926 smmu_flush_ptc_all(smmu);
Thierry Reding89184652014-04-16 09:24:44 +0200927 smmu_flush_tlb(smmu);
928 smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
929 smmu_flush(smmu);
930
931 tegra_smmu_ahb_enable();
932
933 err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
934 if (err < 0)
935 return ERR_PTR(err);
936
Thierry Redingd1313e72015-01-23 09:49:25 +0100937 if (IS_ENABLED(CONFIG_DEBUG_FS))
938 tegra_smmu_debugfs_init(smmu);
939
Thierry Reding89184652014-04-16 09:24:44 +0200940 return smmu;
941}
Thierry Redingd1313e72015-01-23 09:49:25 +0100942
943void tegra_smmu_remove(struct tegra_smmu *smmu)
944{
945 if (IS_ENABLED(CONFIG_DEBUG_FS))
946 tegra_smmu_debugfs_exit(smmu);
947}