blob: d649b06cc4caf59b8607a40d9ebf0e656d29fecb [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;
46 unsigned id;
47 u32 attr;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +030048};
49
Joerg Roedeld5f1a812015-03-26 13:43:12 +010050static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom)
51{
52 return container_of(dom, struct tegra_smmu_as, domain);
53}
54
Thierry Reding89184652014-04-16 09:24:44 +020055static inline void smmu_writel(struct tegra_smmu *smmu, u32 value,
56 unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020057{
Thierry Reding89184652014-04-16 09:24:44 +020058 writel(value, smmu->regs + offset);
Joerg Roedelfe1229b2013-02-04 20:40:58 +010059}
60
Thierry Reding89184652014-04-16 09:24:44 +020061static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020062{
Thierry Reding89184652014-04-16 09:24:44 +020063 return readl(smmu->regs + offset);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020064}
65
Thierry Reding89184652014-04-16 09:24:44 +020066#define SMMU_CONFIG 0x010
67#define SMMU_CONFIG_ENABLE (1 << 0)
68
69#define SMMU_TLB_CONFIG 0x14
70#define SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29)
71#define SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28)
72#define SMMU_TLB_CONFIG_ACTIVE_LINES(x) ((x) & 0x3f)
73
74#define SMMU_PTC_CONFIG 0x18
75#define SMMU_PTC_CONFIG_ENABLE (1 << 29)
76#define SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24)
77#define SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f)
78
79#define SMMU_PTB_ASID 0x01c
80#define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
81
82#define SMMU_PTB_DATA 0x020
83#define SMMU_PTB_DATA_VALUE(page, attr) (page_to_phys(page) >> 12 | (attr))
84
85#define SMMU_MK_PDE(page, attr) (page_to_phys(page) >> SMMU_PTE_SHIFT | (attr))
86
87#define SMMU_TLB_FLUSH 0x030
88#define SMMU_TLB_FLUSH_VA_MATCH_ALL (0 << 0)
89#define SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0)
90#define SMMU_TLB_FLUSH_VA_MATCH_GROUP (3 << 0)
91#define SMMU_TLB_FLUSH_ASID(x) (((x) & 0x7f) << 24)
92#define SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
93 SMMU_TLB_FLUSH_VA_MATCH_SECTION)
94#define SMMU_TLB_FLUSH_VA_GROUP(addr) ((((addr) & 0xffffc000) >> 12) | \
95 SMMU_TLB_FLUSH_VA_MATCH_GROUP)
96#define SMMU_TLB_FLUSH_ASID_MATCH (1 << 31)
97
98#define SMMU_PTC_FLUSH 0x034
99#define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
100#define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
101
102#define SMMU_PTC_FLUSH_HI 0x9b8
103#define SMMU_PTC_FLUSH_HI_MASK 0x3
104
105/* per-SWGROUP SMMU_*_ASID register */
106#define SMMU_ASID_ENABLE (1 << 31)
107#define SMMU_ASID_MASK 0x7f
108#define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
109
110/* page table definitions */
111#define SMMU_NUM_PDE 1024
112#define SMMU_NUM_PTE 1024
113
114#define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
115#define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
116
117#define SMMU_PDE_SHIFT 22
118#define SMMU_PTE_SHIFT 12
119
Thierry Reding89184652014-04-16 09:24:44 +0200120#define SMMU_PD_READABLE (1 << 31)
121#define SMMU_PD_WRITABLE (1 << 30)
122#define SMMU_PD_NONSECURE (1 << 29)
123
124#define SMMU_PDE_READABLE (1 << 31)
125#define SMMU_PDE_WRITABLE (1 << 30)
126#define SMMU_PDE_NONSECURE (1 << 29)
127#define SMMU_PDE_NEXT (1 << 28)
128
129#define SMMU_PTE_READABLE (1 << 31)
130#define SMMU_PTE_WRITABLE (1 << 30)
131#define SMMU_PTE_NONSECURE (1 << 29)
132
133#define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
134 SMMU_PDE_NONSECURE)
135#define SMMU_PTE_ATTR (SMMU_PTE_READABLE | SMMU_PTE_WRITABLE | \
136 SMMU_PTE_NONSECURE)
137
Russell King34d35f82015-07-27 13:29:16 +0100138static unsigned int iova_pd_index(unsigned long iova)
139{
140 return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1);
141}
142
143static unsigned int iova_pt_index(unsigned long iova)
144{
145 return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1);
146}
147
Thierry Reding89184652014-04-16 09:24:44 +0200148static inline void smmu_flush_ptc(struct tegra_smmu *smmu, struct page *page,
149 unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200150{
Thierry Reding89184652014-04-16 09:24:44 +0200151 phys_addr_t phys = page ? page_to_phys(page) : 0;
152 u32 value;
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200153
Thierry Reding89184652014-04-16 09:24:44 +0200154 if (page) {
155 offset &= ~(smmu->mc->soc->atom_size - 1);
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200156
Thierry Reding89184652014-04-16 09:24:44 +0200157 if (smmu->mc->soc->num_address_bits > 32) {
158#ifdef CONFIG_PHYS_ADDR_T_64BIT
159 value = (phys >> 32) & SMMU_PTC_FLUSH_HI_MASK;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200160#else
Thierry Reding89184652014-04-16 09:24:44 +0200161 value = 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200162#endif
Thierry Reding89184652014-04-16 09:24:44 +0200163 smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
164 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200165
Thierry Reding89184652014-04-16 09:24:44 +0200166 value = (phys + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
167 } else {
168 value = SMMU_PTC_FLUSH_TYPE_ALL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200169 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300170
Thierry Reding89184652014-04-16 09:24:44 +0200171 smmu_writel(smmu, value, SMMU_PTC_FLUSH);
172}
173
174static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
175{
176 smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
177}
178
179static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
180 unsigned long asid)
181{
182 u32 value;
183
184 value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
185 SMMU_TLB_FLUSH_VA_MATCH_ALL;
186 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
187}
188
189static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
190 unsigned long asid,
191 unsigned long iova)
192{
193 u32 value;
194
195 value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
196 SMMU_TLB_FLUSH_VA_SECTION(iova);
197 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
198}
199
200static inline void smmu_flush_tlb_group(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_GROUP(iova);
208 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
209}
210
211static inline void smmu_flush(struct tegra_smmu *smmu)
212{
213 smmu_readl(smmu, SMMU_CONFIG);
214}
215
216static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
217{
218 unsigned long id;
219
220 mutex_lock(&smmu->lock);
221
222 id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
223 if (id >= smmu->soc->num_asids) {
224 mutex_unlock(&smmu->lock);
225 return -ENOSPC;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200226 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300227
Thierry Reding89184652014-04-16 09:24:44 +0200228 set_bit(id, smmu->asids);
229 *idp = id;
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300230
Thierry Reding89184652014-04-16 09:24:44 +0200231 mutex_unlock(&smmu->lock);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200232 return 0;
233}
234
Thierry Reding89184652014-04-16 09:24:44 +0200235static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200236{
Thierry Reding89184652014-04-16 09:24:44 +0200237 mutex_lock(&smmu->lock);
238 clear_bit(id, smmu->asids);
239 mutex_unlock(&smmu->lock);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200240}
241
Thierry Reding89184652014-04-16 09:24:44 +0200242static bool tegra_smmu_capable(enum iommu_cap cap)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200243{
Joerg Roedel7c2aa642014-09-05 10:51:37 +0200244 return false;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200245}
246
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100247static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200248{
Thierry Reding89184652014-04-16 09:24:44 +0200249 struct tegra_smmu_as *as;
250 unsigned int i;
251 uint32_t *pd;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200252
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100253 if (type != IOMMU_DOMAIN_UNMANAGED)
254 return NULL;
255
Thierry Reding89184652014-04-16 09:24:44 +0200256 as = kzalloc(sizeof(*as), GFP_KERNEL);
257 if (!as)
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100258 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200259
Thierry Reding89184652014-04-16 09:24:44 +0200260 as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200261
Thierry Reding89184652014-04-16 09:24:44 +0200262 as->pd = alloc_page(GFP_KERNEL | __GFP_DMA);
263 if (!as->pd) {
264 kfree(as);
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100265 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200266 }
267
Russell King32924c72015-07-27 13:29:31 +0100268 as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL);
Thierry Reding89184652014-04-16 09:24:44 +0200269 if (!as->count) {
270 __free_page(as->pd);
271 kfree(as);
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100272 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200273 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200274
Russell King853520f2015-07-27 13:29:26 +0100275 as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL);
276 if (!as->pts) {
Russell King32924c72015-07-27 13:29:31 +0100277 kfree(as->count);
Russell King853520f2015-07-27 13:29:26 +0100278 __free_page(as->pd);
279 kfree(as);
280 return NULL;
281 }
282
Thierry Reding89184652014-04-16 09:24:44 +0200283 /* clear PDEs */
284 pd = page_address(as->pd);
285 SetPageReserved(as->pd);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200286
Thierry Reding89184652014-04-16 09:24:44 +0200287 for (i = 0; i < SMMU_NUM_PDE; i++)
288 pd[i] = 0;
Hiroshi Doyud2453b22012-07-30 08:39:18 +0300289
Thierry Reding471d9142015-03-27 11:07:25 +0100290 /* setup aperture */
Joerg Roedel7f65ef02015-04-02 13:33:19 +0200291 as->domain.geometry.aperture_start = 0;
292 as->domain.geometry.aperture_end = 0xffffffff;
293 as->domain.geometry.force_aperture = true;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200294
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100295 return &as->domain;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200296}
297
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100298static void tegra_smmu_domain_free(struct iommu_domain *domain)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200299{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100300 struct tegra_smmu_as *as = to_smmu_as(domain);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200301
Thierry Reding89184652014-04-16 09:24:44 +0200302 /* TODO: free page directory and page tables */
303 ClearPageReserved(as->pd);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200304
Thierry Reding89184652014-04-16 09:24:44 +0200305 kfree(as);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200306}
307
Thierry Reding89184652014-04-16 09:24:44 +0200308static const struct tegra_smmu_swgroup *
309tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300310{
Thierry Reding89184652014-04-16 09:24:44 +0200311 const struct tegra_smmu_swgroup *group = NULL;
312 unsigned int i;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300313
Thierry Reding89184652014-04-16 09:24:44 +0200314 for (i = 0; i < smmu->soc->num_swgroups; i++) {
315 if (smmu->soc->swgroups[i].swgroup == swgroup) {
316 group = &smmu->soc->swgroups[i];
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300317 break;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300318 }
319 }
320
Thierry Reding89184652014-04-16 09:24:44 +0200321 return group;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300322}
323
Thierry Reding89184652014-04-16 09:24:44 +0200324static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
325 unsigned int asid)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200326{
Thierry Reding89184652014-04-16 09:24:44 +0200327 const struct tegra_smmu_swgroup *group;
328 unsigned int i;
329 u32 value;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200330
Thierry Reding89184652014-04-16 09:24:44 +0200331 for (i = 0; i < smmu->soc->num_clients; i++) {
332 const struct tegra_mc_client *client = &smmu->soc->clients[i];
333
334 if (client->swgroup != swgroup)
335 continue;
336
337 value = smmu_readl(smmu, client->smmu.reg);
338 value |= BIT(client->smmu.bit);
339 smmu_writel(smmu, value, client->smmu.reg);
340 }
341
342 group = tegra_smmu_find_swgroup(smmu, swgroup);
343 if (group) {
344 value = smmu_readl(smmu, group->reg);
345 value &= ~SMMU_ASID_MASK;
346 value |= SMMU_ASID_VALUE(asid);
347 value |= SMMU_ASID_ENABLE;
348 smmu_writel(smmu, value, group->reg);
349 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200350}
351
Thierry Reding89184652014-04-16 09:24:44 +0200352static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
353 unsigned int asid)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200354{
Thierry Reding89184652014-04-16 09:24:44 +0200355 const struct tegra_smmu_swgroup *group;
356 unsigned int i;
357 u32 value;
358
359 group = tegra_smmu_find_swgroup(smmu, swgroup);
360 if (group) {
361 value = smmu_readl(smmu, group->reg);
362 value &= ~SMMU_ASID_MASK;
363 value |= SMMU_ASID_VALUE(asid);
364 value &= ~SMMU_ASID_ENABLE;
365 smmu_writel(smmu, value, group->reg);
366 }
367
368 for (i = 0; i < smmu->soc->num_clients; i++) {
369 const struct tegra_mc_client *client = &smmu->soc->clients[i];
370
371 if (client->swgroup != swgroup)
372 continue;
373
374 value = smmu_readl(smmu, client->smmu.reg);
375 value &= ~BIT(client->smmu.bit);
376 smmu_writel(smmu, value, client->smmu.reg);
377 }
378}
379
380static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
381 struct tegra_smmu_as *as)
382{
383 u32 value;
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300384 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200385
Thierry Reding89184652014-04-16 09:24:44 +0200386 if (as->use_count > 0) {
387 as->use_count++;
388 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200389 }
390
Thierry Reding89184652014-04-16 09:24:44 +0200391 err = tegra_smmu_alloc_asid(smmu, &as->id);
392 if (err < 0)
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +0300393 return err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200394
Thierry Reding89184652014-04-16 09:24:44 +0200395 smmu->soc->ops->flush_dcache(as->pd, 0, SMMU_SIZE_PD);
396 smmu_flush_ptc(smmu, as->pd, 0);
397 smmu_flush_tlb_asid(smmu, as->id);
398
399 smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
400 value = SMMU_PTB_DATA_VALUE(as->pd, as->attr);
401 smmu_writel(smmu, value, SMMU_PTB_DATA);
402 smmu_flush(smmu);
403
404 as->smmu = smmu;
405 as->use_count++;
406
407 return 0;
408}
409
410static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
411 struct tegra_smmu_as *as)
412{
413 if (--as->use_count > 0)
414 return;
415
416 tegra_smmu_free_asid(smmu, as->id);
417 as->smmu = NULL;
418}
419
420static int tegra_smmu_attach_dev(struct iommu_domain *domain,
421 struct device *dev)
422{
423 struct tegra_smmu *smmu = dev->archdata.iommu;
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100424 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200425 struct device_node *np = dev->of_node;
426 struct of_phandle_args args;
427 unsigned int index = 0;
428 int err = 0;
429
430 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
431 &args)) {
432 unsigned int swgroup = args.args[0];
433
434 if (args.np != smmu->dev->of_node) {
435 of_node_put(args.np);
436 continue;
437 }
438
439 of_node_put(args.np);
440
441 err = tegra_smmu_as_prepare(smmu, as);
442 if (err < 0)
443 return err;
444
445 tegra_smmu_enable(smmu, swgroup, as->id);
446 index++;
447 }
448
449 if (index == 0)
450 return -ENODEV;
451
452 return 0;
453}
454
455static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
456{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100457 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200458 struct device_node *np = dev->of_node;
459 struct tegra_smmu *smmu = as->smmu;
460 struct of_phandle_args args;
461 unsigned int index = 0;
462
463 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
464 &args)) {
465 unsigned int swgroup = args.args[0];
466
467 if (args.np != smmu->dev->of_node) {
468 of_node_put(args.np);
469 continue;
470 }
471
472 of_node_put(args.np);
473
474 tegra_smmu_disable(smmu, swgroup, as->id);
475 tegra_smmu_as_unprepare(smmu, as);
476 index++;
477 }
478}
479
Russell King0b42c7c2015-07-27 13:29:21 +0100480static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova)
481{
482 u32 *pt = page_address(pt_page);
483
484 return pt + iova_pt_index(iova);
485}
486
487static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
488 struct page **pagep)
489{
490 unsigned int pd_index = iova_pd_index(iova);
491 struct page *pt_page;
Russell King0b42c7c2015-07-27 13:29:21 +0100492
Russell King853520f2015-07-27 13:29:26 +0100493 pt_page = as->pts[pd_index];
494 if (!pt_page)
Russell King0b42c7c2015-07-27 13:29:21 +0100495 return NULL;
496
Russell King0b42c7c2015-07-27 13:29:21 +0100497 *pagep = pt_page;
498
499 return tegra_smmu_pte_offset(pt_page, iova);
500}
501
Thierry Reding89184652014-04-16 09:24:44 +0200502static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
503 struct page **pagep)
504{
Russell King32924c72015-07-27 13:29:31 +0100505 u32 *pd = page_address(as->pd), *pt;
Russell King34d35f82015-07-27 13:29:16 +0100506 unsigned int pde = iova_pd_index(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200507 struct tegra_smmu *smmu = as->smmu;
508 struct page *page;
509 unsigned int i;
510
Russell King853520f2015-07-27 13:29:26 +0100511 if (!as->pts[pde]) {
Thierry Reding89184652014-04-16 09:24:44 +0200512 page = alloc_page(GFP_KERNEL | __GFP_DMA);
513 if (!page)
514 return NULL;
515
516 pt = page_address(page);
517 SetPageReserved(page);
518
519 for (i = 0; i < SMMU_NUM_PTE; i++)
520 pt[i] = 0;
521
Russell King853520f2015-07-27 13:29:26 +0100522 as->pts[pde] = page;
523
Thierry Reding89184652014-04-16 09:24:44 +0200524 smmu->soc->ops->flush_dcache(page, 0, SMMU_SIZE_PT);
525
526 pd[pde] = SMMU_MK_PDE(page, SMMU_PDE_ATTR | SMMU_PDE_NEXT);
527
528 smmu->soc->ops->flush_dcache(as->pd, pde << 2, 4);
529 smmu_flush_ptc(smmu, as->pd, pde << 2);
530 smmu_flush_tlb_section(smmu, as->id, iova);
531 smmu_flush(smmu);
532 } else {
Russell King853520f2015-07-27 13:29:26 +0100533 page = as->pts[pde];
Thierry Reding89184652014-04-16 09:24:44 +0200534 }
535
536 *pagep = page;
537
Russell King0b42c7c2015-07-27 13:29:21 +0100538 pt = page_address(page);
539
Thierry Reding89184652014-04-16 09:24:44 +0200540 /* Keep track of entries in this page table. */
Russell King0b42c7c2015-07-27 13:29:21 +0100541 if (pt[iova_pt_index(iova)] == 0)
Russell King32924c72015-07-27 13:29:31 +0100542 as->count[pde]++;
Thierry Reding89184652014-04-16 09:24:44 +0200543
Russell King0b42c7c2015-07-27 13:29:21 +0100544 return tegra_smmu_pte_offset(page, iova);
Thierry Reding89184652014-04-16 09:24:44 +0200545}
546
Russell Kingb98e34f2015-07-27 13:29:05 +0100547static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
Thierry Reding89184652014-04-16 09:24:44 +0200548{
Russell Kingb98e34f2015-07-27 13:29:05 +0100549 struct tegra_smmu *smmu = as->smmu;
Russell King34d35f82015-07-27 13:29:16 +0100550 unsigned int pde = iova_pd_index(iova);
Russell Kingb98e34f2015-07-27 13:29:05 +0100551 u32 *pd = page_address(as->pd);
Russell King853520f2015-07-27 13:29:26 +0100552 struct page *page = as->pts[pde];
Thierry Reding89184652014-04-16 09:24:44 +0200553
554 /*
555 * When no entries in this page table are used anymore, return the
556 * memory page to the system.
557 */
Russell King32924c72015-07-27 13:29:31 +0100558 if (--as->count[pde] == 0) {
Russell Kingb98e34f2015-07-27 13:29:05 +0100559 unsigned int offset = pde * sizeof(*pd);
Thierry Reding89184652014-04-16 09:24:44 +0200560
Russell Kingb98e34f2015-07-27 13:29:05 +0100561 /* Clear the page directory entry first */
562 pd[pde] = 0;
563
564 /* Flush the page directory entry */
565 smmu->soc->ops->flush_dcache(as->pd, offset, sizeof(*pd));
566 smmu_flush_ptc(smmu, as->pd, offset);
567 smmu_flush_tlb_section(smmu, as->id, iova);
568 smmu_flush(smmu);
569
570 /* Finally, free the page */
571 ClearPageReserved(page);
572 __free_page(page);
Russell King853520f2015-07-27 13:29:26 +0100573 as->pts[pde] = NULL;
Thierry Reding89184652014-04-16 09:24:44 +0200574 }
575}
576
Russell King8482ee5e2015-07-27 13:29:10 +0100577static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
578 u32 *pte, struct page *pte_page, u32 val)
579{
580 struct tegra_smmu *smmu = as->smmu;
581 unsigned long offset = offset_in_page(pte);
582
583 *pte = val;
584
585 smmu->soc->ops->flush_dcache(pte_page, offset, 4);
586 smmu_flush_ptc(smmu, pte_page, offset);
587 smmu_flush_tlb_group(smmu, as->id, iova);
588 smmu_flush(smmu);
589}
590
Thierry Reding89184652014-04-16 09:24:44 +0200591static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
592 phys_addr_t paddr, size_t size, int prot)
593{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100594 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200595 struct page *page;
596 u32 *pte;
597
598 pte = as_get_pte(as, iova, &page);
599 if (!pte)
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +0300600 return -ENOMEM;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200601
Russell King8482ee5e2015-07-27 13:29:10 +0100602 tegra_smmu_set_pte(as, iova, pte, page,
603 __phys_to_pfn(paddr) | SMMU_PTE_ATTR);
Thierry Reding89184652014-04-16 09:24:44 +0200604
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200605 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200606}
607
Thierry Reding89184652014-04-16 09:24:44 +0200608static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
609 size_t size)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200610{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100611 struct tegra_smmu_as *as = to_smmu_as(domain);
Russell King0b42c7c2015-07-27 13:29:21 +0100612 struct page *pte_page;
Thierry Reding89184652014-04-16 09:24:44 +0200613 u32 *pte;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200614
Russell King0b42c7c2015-07-27 13:29:21 +0100615 pte = tegra_smmu_pte_lookup(as, iova, &pte_page);
Russell Kingb98e34f2015-07-27 13:29:05 +0100616 if (!pte || !*pte)
Thierry Reding89184652014-04-16 09:24:44 +0200617 return 0;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300618
Russell King0b42c7c2015-07-27 13:29:21 +0100619 tegra_smmu_set_pte(as, iova, pte, pte_page, 0);
Russell Kingb98e34f2015-07-27 13:29:05 +0100620 tegra_smmu_pte_put_use(as, iova);
621
Thierry Reding89184652014-04-16 09:24:44 +0200622 return size;
623}
624
625static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
626 dma_addr_t iova)
627{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100628 struct tegra_smmu_as *as = to_smmu_as(domain);
Russell King0b42c7c2015-07-27 13:29:21 +0100629 struct page *pte_page;
Thierry Reding89184652014-04-16 09:24:44 +0200630 unsigned long pfn;
631 u32 *pte;
632
Russell King0b42c7c2015-07-27 13:29:21 +0100633 pte = tegra_smmu_pte_lookup(as, iova, &pte_page);
Russell King91137852015-07-27 13:29:00 +0100634 if (!pte || !*pte)
635 return 0;
636
Thierry Reding804cb542015-03-27 11:07:27 +0100637 pfn = *pte & as->smmu->pfn_mask;
Thierry Reding89184652014-04-16 09:24:44 +0200638
639 return PFN_PHYS(pfn);
640}
641
642static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
643{
644 struct platform_device *pdev;
645 struct tegra_mc *mc;
646
647 pdev = of_find_device_by_node(np);
648 if (!pdev)
649 return NULL;
650
651 mc = platform_get_drvdata(pdev);
652 if (!mc)
653 return NULL;
654
655 return mc->smmu;
656}
657
658static int tegra_smmu_add_device(struct device *dev)
659{
660 struct device_node *np = dev->of_node;
661 struct of_phandle_args args;
662 unsigned int index = 0;
663
664 while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
665 &args) == 0) {
666 struct tegra_smmu *smmu;
667
668 smmu = tegra_smmu_find(args.np);
669 if (smmu) {
670 /*
671 * Only a single IOMMU master interface is currently
672 * supported by the Linux kernel, so abort after the
673 * first match.
674 */
675 dev->archdata.iommu = smmu;
676 break;
677 }
678
679 index++;
680 }
681
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200682 return 0;
683}
684
Thierry Reding89184652014-04-16 09:24:44 +0200685static void tegra_smmu_remove_device(struct device *dev)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200686{
Thierry Reding89184652014-04-16 09:24:44 +0200687 dev->archdata.iommu = NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200688}
689
Thierry Reding89184652014-04-16 09:24:44 +0200690static const struct iommu_ops tegra_smmu_ops = {
691 .capable = tegra_smmu_capable,
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100692 .domain_alloc = tegra_smmu_domain_alloc,
693 .domain_free = tegra_smmu_domain_free,
Thierry Reding89184652014-04-16 09:24:44 +0200694 .attach_dev = tegra_smmu_attach_dev,
695 .detach_dev = tegra_smmu_detach_dev,
696 .add_device = tegra_smmu_add_device,
697 .remove_device = tegra_smmu_remove_device,
698 .map = tegra_smmu_map,
699 .unmap = tegra_smmu_unmap,
700 .map_sg = default_iommu_map_sg,
701 .iova_to_phys = tegra_smmu_iova_to_phys,
702
703 .pgsize_bitmap = SZ_4K,
704};
705
706static void tegra_smmu_ahb_enable(void)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200707{
Thierry Reding89184652014-04-16 09:24:44 +0200708 static const struct of_device_id ahb_match[] = {
709 { .compatible = "nvidia,tegra30-ahb", },
710 { }
711 };
712 struct device_node *ahb;
713
714 ahb = of_find_matching_node(NULL, ahb_match);
715 if (ahb) {
716 tegra_ahb_enable_smmu(ahb);
717 of_node_put(ahb);
718 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200719}
720
Thierry Redingd1313e72015-01-23 09:49:25 +0100721static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
722{
723 struct tegra_smmu *smmu = s->private;
724 unsigned int i;
725 u32 value;
726
727 seq_printf(s, "swgroup enabled ASID\n");
728 seq_printf(s, "------------------------\n");
729
730 for (i = 0; i < smmu->soc->num_swgroups; i++) {
731 const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
732 const char *status;
733 unsigned int asid;
734
735 value = smmu_readl(smmu, group->reg);
736
737 if (value & SMMU_ASID_ENABLE)
738 status = "yes";
739 else
740 status = "no";
741
742 asid = value & SMMU_ASID_MASK;
743
744 seq_printf(s, "%-9s %-7s %#04x\n", group->name, status,
745 asid);
746 }
747
748 return 0;
749}
750
751static int tegra_smmu_swgroups_open(struct inode *inode, struct file *file)
752{
753 return single_open(file, tegra_smmu_swgroups_show, inode->i_private);
754}
755
756static const struct file_operations tegra_smmu_swgroups_fops = {
757 .open = tegra_smmu_swgroups_open,
758 .read = seq_read,
759 .llseek = seq_lseek,
760 .release = single_release,
761};
762
763static int tegra_smmu_clients_show(struct seq_file *s, void *data)
764{
765 struct tegra_smmu *smmu = s->private;
766 unsigned int i;
767 u32 value;
768
769 seq_printf(s, "client enabled\n");
770 seq_printf(s, "--------------------\n");
771
772 for (i = 0; i < smmu->soc->num_clients; i++) {
773 const struct tegra_mc_client *client = &smmu->soc->clients[i];
774 const char *status;
775
776 value = smmu_readl(smmu, client->smmu.reg);
777
778 if (value & BIT(client->smmu.bit))
779 status = "yes";
780 else
781 status = "no";
782
783 seq_printf(s, "%-12s %s\n", client->name, status);
784 }
785
786 return 0;
787}
788
789static int tegra_smmu_clients_open(struct inode *inode, struct file *file)
790{
791 return single_open(file, tegra_smmu_clients_show, inode->i_private);
792}
793
794static const struct file_operations tegra_smmu_clients_fops = {
795 .open = tegra_smmu_clients_open,
796 .read = seq_read,
797 .llseek = seq_lseek,
798 .release = single_release,
799};
800
801static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
802{
803 smmu->debugfs = debugfs_create_dir("smmu", NULL);
804 if (!smmu->debugfs)
805 return;
806
807 debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
808 &tegra_smmu_swgroups_fops);
809 debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
810 &tegra_smmu_clients_fops);
811}
812
813static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
814{
815 debugfs_remove_recursive(smmu->debugfs);
816}
817
Thierry Reding89184652014-04-16 09:24:44 +0200818struct tegra_smmu *tegra_smmu_probe(struct device *dev,
819 const struct tegra_smmu_soc *soc,
820 struct tegra_mc *mc)
821{
822 struct tegra_smmu *smmu;
823 size_t size;
824 u32 value;
825 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200826
Thierry Reding89184652014-04-16 09:24:44 +0200827 /* This can happen on Tegra20 which doesn't have an SMMU */
828 if (!soc)
829 return NULL;
830
831 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
832 if (!smmu)
833 return ERR_PTR(-ENOMEM);
834
835 /*
836 * This is a bit of a hack. Ideally we'd want to simply return this
837 * value. However the IOMMU registration process will attempt to add
838 * all devices to the IOMMU when bus_set_iommu() is called. In order
839 * not to rely on global variables to track the IOMMU instance, we
840 * set it here so that it can be looked up from the .add_device()
841 * callback via the IOMMU device's .drvdata field.
842 */
843 mc->smmu = smmu;
844
845 size = BITS_TO_LONGS(soc->num_asids) * sizeof(long);
846
847 smmu->asids = devm_kzalloc(dev, size, GFP_KERNEL);
848 if (!smmu->asids)
849 return ERR_PTR(-ENOMEM);
850
851 mutex_init(&smmu->lock);
852
853 smmu->regs = mc->regs;
854 smmu->soc = soc;
855 smmu->dev = dev;
856 smmu->mc = mc;
857
Thierry Reding804cb542015-03-27 11:07:27 +0100858 smmu->pfn_mask = BIT_MASK(mc->soc->num_address_bits - PAGE_SHIFT) - 1;
859 dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
860 mc->soc->num_address_bits, smmu->pfn_mask);
861
Thierry Reding89184652014-04-16 09:24:44 +0200862 value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
863
864 if (soc->supports_request_limit)
865 value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
866
867 smmu_writel(smmu, value, SMMU_PTC_CONFIG);
868
869 value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
870 SMMU_TLB_CONFIG_ACTIVE_LINES(0x20);
871
872 if (soc->supports_round_robin_arbitration)
873 value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
874
875 smmu_writel(smmu, value, SMMU_TLB_CONFIG);
876
877 smmu_flush_ptc(smmu, NULL, 0);
878 smmu_flush_tlb(smmu);
879 smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
880 smmu_flush(smmu);
881
882 tegra_smmu_ahb_enable();
883
884 err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
885 if (err < 0)
886 return ERR_PTR(err);
887
Thierry Redingd1313e72015-01-23 09:49:25 +0100888 if (IS_ENABLED(CONFIG_DEBUG_FS))
889 tegra_smmu_debugfs_init(smmu);
890
Thierry Reding89184652014-04-16 09:24:44 +0200891 return smmu;
892}
Thierry Redingd1313e72015-01-23 09:49:25 +0100893
894void tegra_smmu_remove(struct tegra_smmu *smmu)
895{
896 if (IS_ENABLED(CONFIG_DEBUG_FS))
897 tegra_smmu_debugfs_exit(smmu);
898}