blob: c4a652b227f8d6a794fa54a85ac9edf867f3f05f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02002/*
Thierry Reding89184652014-04-16 09:24:44 +02003 * Copyright (C) 2011-2014 NVIDIA CORPORATION. All rights reserved.
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02004 */
5
Thierry Reding804cb542015-03-27 11:07:27 +01006#include <linux/bitops.h>
Thierry Redingd1313e72015-01-23 09:49:25 +01007#include <linux/debugfs.h>
Thierry Redingbc5e6de2013-01-21 11:09:06 +01008#include <linux/err.h>
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02009#include <linux/iommu.h>
Thierry Reding89184652014-04-16 09:24:44 +020010#include <linux/kernel.h>
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +030011#include <linux/of.h>
Thierry Reding89184652014-04-16 09:24:44 +020012#include <linux/of_device.h>
13#include <linux/platform_device.h>
14#include <linux/slab.h>
Joerg Roedel461a6942017-04-26 15:46:20 +020015#include <linux/dma-mapping.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020016
17#include <soc/tegra/ahb.h>
Thierry Reding89184652014-04-16 09:24:44 +020018#include <soc/tegra/mc.h>
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020019
Thierry Reding7f4c9172017-10-12 16:19:16 +020020struct tegra_smmu_group {
21 struct list_head list;
22 const struct tegra_smmu_group_soc *soc;
23 struct iommu_group *group;
24};
25
Thierry Reding89184652014-04-16 09:24:44 +020026struct tegra_smmu {
27 void __iomem *regs;
28 struct device *dev;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020029
Thierry Reding89184652014-04-16 09:24:44 +020030 struct tegra_mc *mc;
31 const struct tegra_smmu_soc *soc;
Stephen Warrene6bc5932012-09-04 16:36:15 -060032
Thierry Reding7f4c9172017-10-12 16:19:16 +020033 struct list_head groups;
34
Thierry Reding804cb542015-03-27 11:07:27 +010035 unsigned long pfn_mask;
Thierry Reding11cec152015-08-06 14:20:31 +020036 unsigned long tlb_mask;
Thierry Reding804cb542015-03-27 11:07:27 +010037
Thierry Reding89184652014-04-16 09:24:44 +020038 unsigned long *asids;
39 struct mutex lock;
Stephen Warrene6bc5932012-09-04 16:36:15 -060040
Thierry Reding89184652014-04-16 09:24:44 +020041 struct list_head list;
Thierry Redingd1313e72015-01-23 09:49:25 +010042
43 struct dentry *debugfs;
Joerg Roedel0b480e42017-08-09 17:41:52 +020044
45 struct iommu_device iommu; /* IOMMU Core code handle */
Stephen Warrene6bc5932012-09-04 16:36:15 -060046};
47
Thierry Reding89184652014-04-16 09:24:44 +020048struct tegra_smmu_as {
Joerg Roedeld5f1a812015-03-26 13:43:12 +010049 struct iommu_domain domain;
Thierry Reding89184652014-04-16 09:24:44 +020050 struct tegra_smmu *smmu;
51 unsigned int use_count;
Russell King32924c72015-07-27 13:29:31 +010052 u32 *count;
Russell King853520f2015-07-27 13:29:26 +010053 struct page **pts;
Thierry Reding89184652014-04-16 09:24:44 +020054 struct page *pd;
Russell Kinge3c97192015-07-27 13:29:52 +010055 dma_addr_t pd_dma;
Thierry Reding89184652014-04-16 09:24:44 +020056 unsigned id;
57 u32 attr;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +030058};
59
Joerg Roedeld5f1a812015-03-26 13:43:12 +010060static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom)
61{
62 return container_of(dom, struct tegra_smmu_as, domain);
63}
64
Thierry Reding89184652014-04-16 09:24:44 +020065static inline void smmu_writel(struct tegra_smmu *smmu, u32 value,
66 unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020067{
Thierry Reding89184652014-04-16 09:24:44 +020068 writel(value, smmu->regs + offset);
Joerg Roedelfe1229b2013-02-04 20:40:58 +010069}
70
Thierry Reding89184652014-04-16 09:24:44 +020071static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020072{
Thierry Reding89184652014-04-16 09:24:44 +020073 return readl(smmu->regs + offset);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020074}
75
Thierry Reding89184652014-04-16 09:24:44 +020076#define SMMU_CONFIG 0x010
77#define SMMU_CONFIG_ENABLE (1 << 0)
78
79#define SMMU_TLB_CONFIG 0x14
80#define SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29)
81#define SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28)
Thierry Reding11cec152015-08-06 14:20:31 +020082#define SMMU_TLB_CONFIG_ACTIVE_LINES(smmu) \
83 ((smmu)->soc->num_tlb_lines & (smmu)->tlb_mask)
Thierry Reding89184652014-04-16 09:24:44 +020084
85#define SMMU_PTC_CONFIG 0x18
86#define SMMU_PTC_CONFIG_ENABLE (1 << 29)
87#define SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24)
88#define SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f)
89
90#define SMMU_PTB_ASID 0x01c
91#define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
92
93#define SMMU_PTB_DATA 0x020
Russell Kinge3c97192015-07-27 13:29:52 +010094#define SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr))
Thierry Reding89184652014-04-16 09:24:44 +020095
Russell Kinge3c97192015-07-27 13:29:52 +010096#define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr))
Thierry Reding89184652014-04-16 09:24:44 +020097
98#define SMMU_TLB_FLUSH 0x030
99#define SMMU_TLB_FLUSH_VA_MATCH_ALL (0 << 0)
100#define SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0)
101#define SMMU_TLB_FLUSH_VA_MATCH_GROUP (3 << 0)
Thierry Reding89184652014-04-16 09:24:44 +0200102#define SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
103 SMMU_TLB_FLUSH_VA_MATCH_SECTION)
104#define SMMU_TLB_FLUSH_VA_GROUP(addr) ((((addr) & 0xffffc000) >> 12) | \
105 SMMU_TLB_FLUSH_VA_MATCH_GROUP)
106#define SMMU_TLB_FLUSH_ASID_MATCH (1 << 31)
107
108#define SMMU_PTC_FLUSH 0x034
109#define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
110#define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
111
112#define SMMU_PTC_FLUSH_HI 0x9b8
113#define SMMU_PTC_FLUSH_HI_MASK 0x3
114
115/* per-SWGROUP SMMU_*_ASID register */
116#define SMMU_ASID_ENABLE (1 << 31)
117#define SMMU_ASID_MASK 0x7f
118#define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
119
120/* page table definitions */
121#define SMMU_NUM_PDE 1024
122#define SMMU_NUM_PTE 1024
123
124#define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
125#define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
126
127#define SMMU_PDE_SHIFT 22
128#define SMMU_PTE_SHIFT 12
129
Thierry Reding89184652014-04-16 09:24:44 +0200130#define SMMU_PD_READABLE (1 << 31)
131#define SMMU_PD_WRITABLE (1 << 30)
132#define SMMU_PD_NONSECURE (1 << 29)
133
134#define SMMU_PDE_READABLE (1 << 31)
135#define SMMU_PDE_WRITABLE (1 << 30)
136#define SMMU_PDE_NONSECURE (1 << 29)
137#define SMMU_PDE_NEXT (1 << 28)
138
139#define SMMU_PTE_READABLE (1 << 31)
140#define SMMU_PTE_WRITABLE (1 << 30)
141#define SMMU_PTE_NONSECURE (1 << 29)
142
143#define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
144 SMMU_PDE_NONSECURE)
Thierry Reding89184652014-04-16 09:24:44 +0200145
Russell King34d35f82015-07-27 13:29:16 +0100146static unsigned int iova_pd_index(unsigned long iova)
147{
148 return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1);
149}
150
151static unsigned int iova_pt_index(unsigned long iova)
152{
153 return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1);
154}
155
Russell Kinge3c97192015-07-27 13:29:52 +0100156static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr)
Russell King4b3c7d12015-07-27 13:29:36 +0100157{
Russell Kinge3c97192015-07-27 13:29:52 +0100158 addr >>= 12;
159 return (addr & smmu->pfn_mask) == addr;
160}
Russell King4b3c7d12015-07-27 13:29:36 +0100161
Russell Kinge3c97192015-07-27 13:29:52 +0100162static dma_addr_t smmu_pde_to_dma(u32 pde)
163{
164 return pde << 12;
Russell King4b3c7d12015-07-27 13:29:36 +0100165}
166
Russell Kingb8fe0382015-07-27 13:29:41 +0100167static void smmu_flush_ptc_all(struct tegra_smmu *smmu)
168{
169 smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
170}
171
Russell Kinge3c97192015-07-27 13:29:52 +0100172static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma,
Thierry Reding89184652014-04-16 09:24:44 +0200173 unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200174{
Thierry Reding89184652014-04-16 09:24:44 +0200175 u32 value;
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200176
Russell Kingb8fe0382015-07-27 13:29:41 +0100177 offset &= ~(smmu->mc->soc->atom_size - 1);
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200178
Russell Kingb8fe0382015-07-27 13:29:41 +0100179 if (smmu->mc->soc->num_address_bits > 32) {
Russell Kinge3c97192015-07-27 13:29:52 +0100180#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
181 value = (dma >> 32) & SMMU_PTC_FLUSH_HI_MASK;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200182#else
Russell Kingb8fe0382015-07-27 13:29:41 +0100183 value = 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200184#endif
Russell Kingb8fe0382015-07-27 13:29:41 +0100185 smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200186 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300187
Russell Kinge3c97192015-07-27 13:29:52 +0100188 value = (dma + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
Thierry Reding89184652014-04-16 09:24:44 +0200189 smmu_writel(smmu, value, SMMU_PTC_FLUSH);
190}
191
192static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
193{
194 smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
195}
196
197static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
198 unsigned long asid)
199{
200 u32 value;
201
Dmitry Osipenko43a05412019-03-07 01:50:07 +0300202 if (smmu->soc->num_asids == 4)
203 value = (asid & 0x3) << 29;
204 else
205 value = (asid & 0x7f) << 24;
206
207 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_MATCH_ALL;
Thierry Reding89184652014-04-16 09:24:44 +0200208 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
209}
210
211static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
212 unsigned long asid,
213 unsigned long iova)
214{
215 u32 value;
216
Dmitry Osipenko43a05412019-03-07 01:50:07 +0300217 if (smmu->soc->num_asids == 4)
218 value = (asid & 0x3) << 29;
219 else
220 value = (asid & 0x7f) << 24;
221
222 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_SECTION(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200223 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
224}
225
226static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu,
227 unsigned long asid,
228 unsigned long iova)
229{
230 u32 value;
231
Dmitry Osipenko43a05412019-03-07 01:50:07 +0300232 if (smmu->soc->num_asids == 4)
233 value = (asid & 0x3) << 29;
234 else
235 value = (asid & 0x7f) << 24;
236
237 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_GROUP(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200238 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
239}
240
241static inline void smmu_flush(struct tegra_smmu *smmu)
242{
243 smmu_readl(smmu, SMMU_CONFIG);
244}
245
246static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
247{
248 unsigned long id;
249
250 mutex_lock(&smmu->lock);
251
252 id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
253 if (id >= smmu->soc->num_asids) {
254 mutex_unlock(&smmu->lock);
255 return -ENOSPC;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200256 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300257
Thierry Reding89184652014-04-16 09:24:44 +0200258 set_bit(id, smmu->asids);
259 *idp = id;
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300260
Thierry Reding89184652014-04-16 09:24:44 +0200261 mutex_unlock(&smmu->lock);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200262 return 0;
263}
264
Thierry Reding89184652014-04-16 09:24:44 +0200265static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200266{
Thierry Reding89184652014-04-16 09:24:44 +0200267 mutex_lock(&smmu->lock);
268 clear_bit(id, smmu->asids);
269 mutex_unlock(&smmu->lock);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200270}
271
Thierry Reding89184652014-04-16 09:24:44 +0200272static bool tegra_smmu_capable(enum iommu_cap cap)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200273{
Joerg Roedel7c2aa642014-09-05 10:51:37 +0200274 return false;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200275}
276
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100277static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200278{
Thierry Reding89184652014-04-16 09:24:44 +0200279 struct tegra_smmu_as *as;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200280
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100281 if (type != IOMMU_DOMAIN_UNMANAGED)
282 return NULL;
283
Thierry Reding89184652014-04-16 09:24:44 +0200284 as = kzalloc(sizeof(*as), GFP_KERNEL);
285 if (!as)
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100286 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200287
Thierry Reding89184652014-04-16 09:24:44 +0200288 as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200289
Russell King707917c2015-07-27 13:30:02 +0100290 as->pd = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
Thierry Reding89184652014-04-16 09:24:44 +0200291 if (!as->pd) {
292 kfree(as);
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100293 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200294 }
295
Russell King32924c72015-07-27 13:29:31 +0100296 as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL);
Thierry Reding89184652014-04-16 09:24:44 +0200297 if (!as->count) {
298 __free_page(as->pd);
299 kfree(as);
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100300 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200301 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200302
Russell King853520f2015-07-27 13:29:26 +0100303 as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL);
304 if (!as->pts) {
Russell King32924c72015-07-27 13:29:31 +0100305 kfree(as->count);
Russell King853520f2015-07-27 13:29:26 +0100306 __free_page(as->pd);
307 kfree(as);
308 return NULL;
309 }
310
Thierry Reding471d9142015-03-27 11:07:25 +0100311 /* setup aperture */
Joerg Roedel7f65ef02015-04-02 13:33:19 +0200312 as->domain.geometry.aperture_start = 0;
313 as->domain.geometry.aperture_end = 0xffffffff;
314 as->domain.geometry.force_aperture = true;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200315
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100316 return &as->domain;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200317}
318
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100319static void tegra_smmu_domain_free(struct iommu_domain *domain)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200320{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100321 struct tegra_smmu_as *as = to_smmu_as(domain);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200322
Thierry Reding89184652014-04-16 09:24:44 +0200323 /* TODO: free page directory and page tables */
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200324
Dmitry Osipenko4f970312019-03-07 01:50:08 +0300325 WARN_ON_ONCE(as->use_count);
326 kfree(as->count);
327 kfree(as->pts);
Thierry Reding89184652014-04-16 09:24:44 +0200328 kfree(as);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200329}
330
Thierry Reding89184652014-04-16 09:24:44 +0200331static const struct tegra_smmu_swgroup *
332tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300333{
Thierry Reding89184652014-04-16 09:24:44 +0200334 const struct tegra_smmu_swgroup *group = NULL;
335 unsigned int i;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300336
Thierry Reding89184652014-04-16 09:24:44 +0200337 for (i = 0; i < smmu->soc->num_swgroups; i++) {
338 if (smmu->soc->swgroups[i].swgroup == swgroup) {
339 group = &smmu->soc->swgroups[i];
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300340 break;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300341 }
342 }
343
Thierry Reding89184652014-04-16 09:24:44 +0200344 return group;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300345}
346
Thierry Reding89184652014-04-16 09:24:44 +0200347static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
348 unsigned int asid)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200349{
Thierry Reding89184652014-04-16 09:24:44 +0200350 const struct tegra_smmu_swgroup *group;
351 unsigned int i;
352 u32 value;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200353
Thierry Reding89184652014-04-16 09:24:44 +0200354 for (i = 0; i < smmu->soc->num_clients; i++) {
355 const struct tegra_mc_client *client = &smmu->soc->clients[i];
356
357 if (client->swgroup != swgroup)
358 continue;
359
360 value = smmu_readl(smmu, client->smmu.reg);
361 value |= BIT(client->smmu.bit);
362 smmu_writel(smmu, value, client->smmu.reg);
363 }
364
365 group = tegra_smmu_find_swgroup(smmu, swgroup);
366 if (group) {
367 value = smmu_readl(smmu, group->reg);
368 value &= ~SMMU_ASID_MASK;
369 value |= SMMU_ASID_VALUE(asid);
370 value |= SMMU_ASID_ENABLE;
371 smmu_writel(smmu, value, group->reg);
372 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200373}
374
Thierry Reding89184652014-04-16 09:24:44 +0200375static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
376 unsigned int asid)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200377{
Thierry Reding89184652014-04-16 09:24:44 +0200378 const struct tegra_smmu_swgroup *group;
379 unsigned int i;
380 u32 value;
381
382 group = tegra_smmu_find_swgroup(smmu, swgroup);
383 if (group) {
384 value = smmu_readl(smmu, group->reg);
385 value &= ~SMMU_ASID_MASK;
386 value |= SMMU_ASID_VALUE(asid);
387 value &= ~SMMU_ASID_ENABLE;
388 smmu_writel(smmu, value, group->reg);
389 }
390
391 for (i = 0; i < smmu->soc->num_clients; i++) {
392 const struct tegra_mc_client *client = &smmu->soc->clients[i];
393
394 if (client->swgroup != swgroup)
395 continue;
396
397 value = smmu_readl(smmu, client->smmu.reg);
398 value &= ~BIT(client->smmu.bit);
399 smmu_writel(smmu, value, client->smmu.reg);
400 }
401}
402
403static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
404 struct tegra_smmu_as *as)
405{
406 u32 value;
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300407 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200408
Thierry Reding89184652014-04-16 09:24:44 +0200409 if (as->use_count > 0) {
410 as->use_count++;
411 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200412 }
413
Russell Kinge3c97192015-07-27 13:29:52 +0100414 as->pd_dma = dma_map_page(smmu->dev, as->pd, 0, SMMU_SIZE_PD,
415 DMA_TO_DEVICE);
416 if (dma_mapping_error(smmu->dev, as->pd_dma))
417 return -ENOMEM;
418
419 /* We can't handle 64-bit DMA addresses */
420 if (!smmu_dma_addr_valid(smmu, as->pd_dma)) {
421 err = -ENOMEM;
422 goto err_unmap;
423 }
424
Thierry Reding89184652014-04-16 09:24:44 +0200425 err = tegra_smmu_alloc_asid(smmu, &as->id);
426 if (err < 0)
Russell Kinge3c97192015-07-27 13:29:52 +0100427 goto err_unmap;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200428
Russell Kinge3c97192015-07-27 13:29:52 +0100429 smmu_flush_ptc(smmu, as->pd_dma, 0);
Thierry Reding89184652014-04-16 09:24:44 +0200430 smmu_flush_tlb_asid(smmu, as->id);
431
432 smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
Russell Kinge3c97192015-07-27 13:29:52 +0100433 value = SMMU_PTB_DATA_VALUE(as->pd_dma, as->attr);
Thierry Reding89184652014-04-16 09:24:44 +0200434 smmu_writel(smmu, value, SMMU_PTB_DATA);
435 smmu_flush(smmu);
436
437 as->smmu = smmu;
438 as->use_count++;
439
440 return 0;
Russell Kinge3c97192015-07-27 13:29:52 +0100441
442err_unmap:
443 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
444 return err;
Thierry Reding89184652014-04-16 09:24:44 +0200445}
446
447static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
448 struct tegra_smmu_as *as)
449{
450 if (--as->use_count > 0)
451 return;
452
453 tegra_smmu_free_asid(smmu, as->id);
Russell Kinge3c97192015-07-27 13:29:52 +0100454
455 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
456
Thierry Reding89184652014-04-16 09:24:44 +0200457 as->smmu = NULL;
458}
459
460static int tegra_smmu_attach_dev(struct iommu_domain *domain,
461 struct device *dev)
462{
463 struct tegra_smmu *smmu = dev->archdata.iommu;
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100464 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200465 struct device_node *np = dev->of_node;
466 struct of_phandle_args args;
467 unsigned int index = 0;
468 int err = 0;
469
470 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
471 &args)) {
472 unsigned int swgroup = args.args[0];
473
474 if (args.np != smmu->dev->of_node) {
475 of_node_put(args.np);
476 continue;
477 }
478
479 of_node_put(args.np);
480
481 err = tegra_smmu_as_prepare(smmu, as);
482 if (err < 0)
483 return err;
484
485 tegra_smmu_enable(smmu, swgroup, as->id);
486 index++;
487 }
488
489 if (index == 0)
490 return -ENODEV;
491
492 return 0;
493}
494
495static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
496{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100497 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200498 struct device_node *np = dev->of_node;
499 struct tegra_smmu *smmu = as->smmu;
500 struct of_phandle_args args;
501 unsigned int index = 0;
502
503 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
504 &args)) {
505 unsigned int swgroup = args.args[0];
506
507 if (args.np != smmu->dev->of_node) {
508 of_node_put(args.np);
509 continue;
510 }
511
512 of_node_put(args.np);
513
514 tegra_smmu_disable(smmu, swgroup, as->id);
515 tegra_smmu_as_unprepare(smmu, as);
516 index++;
517 }
518}
519
Russell King4080e992015-07-27 13:30:12 +0100520static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova,
521 u32 value)
522{
523 unsigned int pd_index = iova_pd_index(iova);
524 struct tegra_smmu *smmu = as->smmu;
525 u32 *pd = page_address(as->pd);
526 unsigned long offset = pd_index * sizeof(*pd);
527
528 /* Set the page directory entry first */
529 pd[pd_index] = value;
530
531 /* The flush the page directory entry from caches */
532 dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset,
533 sizeof(*pd), DMA_TO_DEVICE);
534
535 /* And flush the iommu */
536 smmu_flush_ptc(smmu, as->pd_dma, offset);
537 smmu_flush_tlb_section(smmu, as->id, iova);
538 smmu_flush(smmu);
539}
540
Russell King0b42c7c2015-07-27 13:29:21 +0100541static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova)
542{
543 u32 *pt = page_address(pt_page);
544
545 return pt + iova_pt_index(iova);
546}
547
548static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100549 dma_addr_t *dmap)
Russell King0b42c7c2015-07-27 13:29:21 +0100550{
551 unsigned int pd_index = iova_pd_index(iova);
552 struct page *pt_page;
Russell Kinge3c97192015-07-27 13:29:52 +0100553 u32 *pd;
Russell King0b42c7c2015-07-27 13:29:21 +0100554
Russell King853520f2015-07-27 13:29:26 +0100555 pt_page = as->pts[pd_index];
556 if (!pt_page)
Russell King0b42c7c2015-07-27 13:29:21 +0100557 return NULL;
558
Russell Kinge3c97192015-07-27 13:29:52 +0100559 pd = page_address(as->pd);
560 *dmap = smmu_pde_to_dma(pd[pd_index]);
Russell King0b42c7c2015-07-27 13:29:21 +0100561
562 return tegra_smmu_pte_offset(pt_page, iova);
563}
564
Thierry Reding89184652014-04-16 09:24:44 +0200565static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100566 dma_addr_t *dmap)
Thierry Reding89184652014-04-16 09:24:44 +0200567{
Russell King34d35f82015-07-27 13:29:16 +0100568 unsigned int pde = iova_pd_index(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200569 struct tegra_smmu *smmu = as->smmu;
Thierry Reding89184652014-04-16 09:24:44 +0200570
Russell King853520f2015-07-27 13:29:26 +0100571 if (!as->pts[pde]) {
Russell Kinge3c97192015-07-27 13:29:52 +0100572 struct page *page;
573 dma_addr_t dma;
574
Russell King707917c2015-07-27 13:30:02 +0100575 page = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
Thierry Reding89184652014-04-16 09:24:44 +0200576 if (!page)
577 return NULL;
578
Russell Kinge3c97192015-07-27 13:29:52 +0100579 dma = dma_map_page(smmu->dev, page, 0, SMMU_SIZE_PT,
580 DMA_TO_DEVICE);
581 if (dma_mapping_error(smmu->dev, dma)) {
582 __free_page(page);
583 return NULL;
584 }
585
586 if (!smmu_dma_addr_valid(smmu, dma)) {
587 dma_unmap_page(smmu->dev, dma, SMMU_SIZE_PT,
588 DMA_TO_DEVICE);
589 __free_page(page);
590 return NULL;
591 }
592
Russell King853520f2015-07-27 13:29:26 +0100593 as->pts[pde] = page;
594
Russell King4080e992015-07-27 13:30:12 +0100595 tegra_smmu_set_pde(as, iova, SMMU_MK_PDE(dma, SMMU_PDE_ATTR |
596 SMMU_PDE_NEXT));
Russell Kinge3c97192015-07-27 13:29:52 +0100597
598 *dmap = dma;
Thierry Reding89184652014-04-16 09:24:44 +0200599 } else {
Russell King4080e992015-07-27 13:30:12 +0100600 u32 *pd = page_address(as->pd);
601
Russell Kinge3c97192015-07-27 13:29:52 +0100602 *dmap = smmu_pde_to_dma(pd[pde]);
Thierry Reding89184652014-04-16 09:24:44 +0200603 }
604
Russell King7ffc6f02015-08-06 14:56:39 +0200605 return tegra_smmu_pte_offset(as->pts[pde], iova);
606}
Russell King0b42c7c2015-07-27 13:29:21 +0100607
Russell King7ffc6f02015-08-06 14:56:39 +0200608static void tegra_smmu_pte_get_use(struct tegra_smmu_as *as, unsigned long iova)
609{
610 unsigned int pd_index = iova_pd_index(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200611
Russell King7ffc6f02015-08-06 14:56:39 +0200612 as->count[pd_index]++;
Thierry Reding89184652014-04-16 09:24:44 +0200613}
614
Russell Kingb98e34f2015-07-27 13:29:05 +0100615static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
Thierry Reding89184652014-04-16 09:24:44 +0200616{
Russell King34d35f82015-07-27 13:29:16 +0100617 unsigned int pde = iova_pd_index(iova);
Russell King853520f2015-07-27 13:29:26 +0100618 struct page *page = as->pts[pde];
Thierry Reding89184652014-04-16 09:24:44 +0200619
620 /*
621 * When no entries in this page table are used anymore, return the
622 * memory page to the system.
623 */
Russell King32924c72015-07-27 13:29:31 +0100624 if (--as->count[pde] == 0) {
Russell King4080e992015-07-27 13:30:12 +0100625 struct tegra_smmu *smmu = as->smmu;
626 u32 *pd = page_address(as->pd);
Russell Kinge3c97192015-07-27 13:29:52 +0100627 dma_addr_t pte_dma = smmu_pde_to_dma(pd[pde]);
Thierry Reding89184652014-04-16 09:24:44 +0200628
Russell King4080e992015-07-27 13:30:12 +0100629 tegra_smmu_set_pde(as, iova, 0);
Russell Kingb98e34f2015-07-27 13:29:05 +0100630
Russell Kinge3c97192015-07-27 13:29:52 +0100631 dma_unmap_page(smmu->dev, pte_dma, SMMU_SIZE_PT, DMA_TO_DEVICE);
Russell Kingb98e34f2015-07-27 13:29:05 +0100632 __free_page(page);
Russell King853520f2015-07-27 13:29:26 +0100633 as->pts[pde] = NULL;
Thierry Reding89184652014-04-16 09:24:44 +0200634 }
635}
636
Russell King8482ee52015-07-27 13:29:10 +0100637static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100638 u32 *pte, dma_addr_t pte_dma, u32 val)
Russell King8482ee52015-07-27 13:29:10 +0100639{
640 struct tegra_smmu *smmu = as->smmu;
641 unsigned long offset = offset_in_page(pte);
642
643 *pte = val;
644
Russell Kinge3c97192015-07-27 13:29:52 +0100645 dma_sync_single_range_for_device(smmu->dev, pte_dma, offset,
646 4, DMA_TO_DEVICE);
647 smmu_flush_ptc(smmu, pte_dma, offset);
Russell King8482ee52015-07-27 13:29:10 +0100648 smmu_flush_tlb_group(smmu, as->id, iova);
649 smmu_flush(smmu);
650}
651
Thierry Reding89184652014-04-16 09:24:44 +0200652static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
653 phys_addr_t paddr, size_t size, int prot)
654{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100655 struct tegra_smmu_as *as = to_smmu_as(domain);
Russell Kinge3c97192015-07-27 13:29:52 +0100656 dma_addr_t pte_dma;
Dmitry Osipenko43d957b2019-03-07 01:50:09 +0300657 u32 pte_attrs;
Thierry Reding89184652014-04-16 09:24:44 +0200658 u32 *pte;
659
Russell Kinge3c97192015-07-27 13:29:52 +0100660 pte = as_get_pte(as, iova, &pte_dma);
Thierry Reding89184652014-04-16 09:24:44 +0200661 if (!pte)
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +0300662 return -ENOMEM;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200663
Russell King7ffc6f02015-08-06 14:56:39 +0200664 /* If we aren't overwriting a pre-existing entry, increment use */
665 if (*pte == 0)
666 tegra_smmu_pte_get_use(as, iova);
667
Dmitry Osipenko43d957b2019-03-07 01:50:09 +0300668 pte_attrs = SMMU_PTE_NONSECURE;
669
670 if (prot & IOMMU_READ)
671 pte_attrs |= SMMU_PTE_READABLE;
672
673 if (prot & IOMMU_WRITE)
674 pte_attrs |= SMMU_PTE_WRITABLE;
675
Russell Kinge3c97192015-07-27 13:29:52 +0100676 tegra_smmu_set_pte(as, iova, pte, pte_dma,
Dmitry Osipenko43d957b2019-03-07 01:50:09 +0300677 __phys_to_pfn(paddr) | pte_attrs);
Thierry Reding89184652014-04-16 09:24:44 +0200678
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200679 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200680}
681
Thierry Reding89184652014-04-16 09:24:44 +0200682static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
683 size_t size)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200684{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100685 struct tegra_smmu_as *as = to_smmu_as(domain);
Russell Kinge3c97192015-07-27 13:29:52 +0100686 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200687 u32 *pte;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200688
Russell Kinge3c97192015-07-27 13:29:52 +0100689 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
Russell Kingb98e34f2015-07-27 13:29:05 +0100690 if (!pte || !*pte)
Thierry Reding89184652014-04-16 09:24:44 +0200691 return 0;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300692
Russell Kinge3c97192015-07-27 13:29:52 +0100693 tegra_smmu_set_pte(as, iova, pte, pte_dma, 0);
Russell Kingb98e34f2015-07-27 13:29:05 +0100694 tegra_smmu_pte_put_use(as, iova);
695
Thierry Reding89184652014-04-16 09:24:44 +0200696 return size;
697}
698
699static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
700 dma_addr_t iova)
701{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100702 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200703 unsigned long pfn;
Russell Kinge3c97192015-07-27 13:29:52 +0100704 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200705 u32 *pte;
706
Russell Kinge3c97192015-07-27 13:29:52 +0100707 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
Russell King91137852015-07-27 13:29:00 +0100708 if (!pte || !*pte)
709 return 0;
710
Thierry Reding804cb542015-03-27 11:07:27 +0100711 pfn = *pte & as->smmu->pfn_mask;
Thierry Reding89184652014-04-16 09:24:44 +0200712
713 return PFN_PHYS(pfn);
714}
715
716static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
717{
718 struct platform_device *pdev;
719 struct tegra_mc *mc;
720
721 pdev = of_find_device_by_node(np);
722 if (!pdev)
723 return NULL;
724
725 mc = platform_get_drvdata(pdev);
726 if (!mc)
727 return NULL;
728
729 return mc->smmu;
730}
731
Thierry Reding7f4c9172017-10-12 16:19:16 +0200732static int tegra_smmu_configure(struct tegra_smmu *smmu, struct device *dev,
733 struct of_phandle_args *args)
734{
735 const struct iommu_ops *ops = smmu->iommu.ops;
736 int err;
737
738 err = iommu_fwspec_init(dev, &dev->of_node->fwnode, ops);
739 if (err < 0) {
740 dev_err(dev, "failed to initialize fwspec: %d\n", err);
741 return err;
742 }
743
744 err = ops->of_xlate(dev, args);
745 if (err < 0) {
746 dev_err(dev, "failed to parse SW group ID: %d\n", err);
747 iommu_fwspec_free(dev);
748 return err;
749 }
750
751 return 0;
752}
753
Thierry Reding89184652014-04-16 09:24:44 +0200754static int tegra_smmu_add_device(struct device *dev)
755{
756 struct device_node *np = dev->of_node;
Thierry Reding7f4c9172017-10-12 16:19:16 +0200757 struct tegra_smmu *smmu = NULL;
Robin Murphyd92e1f82017-07-21 13:12:36 +0100758 struct iommu_group *group;
Thierry Reding89184652014-04-16 09:24:44 +0200759 struct of_phandle_args args;
760 unsigned int index = 0;
Thierry Reding7f4c9172017-10-12 16:19:16 +0200761 int err;
Thierry Reding89184652014-04-16 09:24:44 +0200762
763 while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
764 &args) == 0) {
Thierry Reding89184652014-04-16 09:24:44 +0200765 smmu = tegra_smmu_find(args.np);
766 if (smmu) {
Thierry Reding7f4c9172017-10-12 16:19:16 +0200767 err = tegra_smmu_configure(smmu, dev, &args);
768 of_node_put(args.np);
769
770 if (err < 0)
771 return err;
772
Thierry Reding89184652014-04-16 09:24:44 +0200773 /*
774 * Only a single IOMMU master interface is currently
775 * supported by the Linux kernel, so abort after the
776 * first match.
777 */
778 dev->archdata.iommu = smmu;
Joerg Roedel0b480e42017-08-09 17:41:52 +0200779
780 iommu_device_link(&smmu->iommu, dev);
781
Thierry Reding89184652014-04-16 09:24:44 +0200782 break;
783 }
784
Thierry Reding7f4c9172017-10-12 16:19:16 +0200785 of_node_put(args.np);
Thierry Reding89184652014-04-16 09:24:44 +0200786 index++;
787 }
788
Thierry Reding7f4c9172017-10-12 16:19:16 +0200789 if (!smmu)
790 return -ENODEV;
791
Robin Murphyd92e1f82017-07-21 13:12:36 +0100792 group = iommu_group_get_for_dev(dev);
793 if (IS_ERR(group))
794 return PTR_ERR(group);
795
796 iommu_group_put(group);
797
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200798 return 0;
799}
800
Thierry Reding89184652014-04-16 09:24:44 +0200801static void tegra_smmu_remove_device(struct device *dev)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200802{
Joerg Roedel0b480e42017-08-09 17:41:52 +0200803 struct tegra_smmu *smmu = dev->archdata.iommu;
804
805 if (smmu)
806 iommu_device_unlink(&smmu->iommu, dev);
807
Thierry Reding89184652014-04-16 09:24:44 +0200808 dev->archdata.iommu = NULL;
Robin Murphyd92e1f82017-07-21 13:12:36 +0100809 iommu_group_remove_device(dev);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200810}
811
Thierry Reding7f4c9172017-10-12 16:19:16 +0200812static const struct tegra_smmu_group_soc *
813tegra_smmu_find_group(struct tegra_smmu *smmu, unsigned int swgroup)
814{
815 unsigned int i, j;
816
817 for (i = 0; i < smmu->soc->num_groups; i++)
818 for (j = 0; j < smmu->soc->groups[i].num_swgroups; j++)
819 if (smmu->soc->groups[i].swgroups[j] == swgroup)
820 return &smmu->soc->groups[i];
821
822 return NULL;
823}
824
825static struct iommu_group *tegra_smmu_group_get(struct tegra_smmu *smmu,
826 unsigned int swgroup)
827{
828 const struct tegra_smmu_group_soc *soc;
829 struct tegra_smmu_group *group;
830
831 soc = tegra_smmu_find_group(smmu, swgroup);
832 if (!soc)
833 return NULL;
834
835 mutex_lock(&smmu->lock);
836
837 list_for_each_entry(group, &smmu->groups, list)
838 if (group->soc == soc) {
839 mutex_unlock(&smmu->lock);
840 return group->group;
841 }
842
843 group = devm_kzalloc(smmu->dev, sizeof(*group), GFP_KERNEL);
844 if (!group) {
845 mutex_unlock(&smmu->lock);
846 return NULL;
847 }
848
849 INIT_LIST_HEAD(&group->list);
850 group->soc = soc;
851
852 group->group = iommu_group_alloc();
Wei Yongjun83476bf2017-12-20 03:06:09 +0000853 if (IS_ERR(group->group)) {
Thierry Reding7f4c9172017-10-12 16:19:16 +0200854 devm_kfree(smmu->dev, group);
855 mutex_unlock(&smmu->lock);
856 return NULL;
857 }
858
859 list_add_tail(&group->list, &smmu->groups);
860 mutex_unlock(&smmu->lock);
861
862 return group->group;
863}
864
865static struct iommu_group *tegra_smmu_device_group(struct device *dev)
866{
Joerg Roedeldb5d6a72018-11-29 14:01:00 +0100867 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Thierry Reding7f4c9172017-10-12 16:19:16 +0200868 struct tegra_smmu *smmu = dev->archdata.iommu;
869 struct iommu_group *group;
870
871 group = tegra_smmu_group_get(smmu, fwspec->ids[0]);
872 if (!group)
873 group = generic_device_group(dev);
874
875 return group;
876}
877
878static int tegra_smmu_of_xlate(struct device *dev,
879 struct of_phandle_args *args)
880{
881 u32 id = args->args[0];
882
883 return iommu_fwspec_add_ids(dev, &id, 1);
884}
885
Thierry Reding89184652014-04-16 09:24:44 +0200886static const struct iommu_ops tegra_smmu_ops = {
887 .capable = tegra_smmu_capable,
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100888 .domain_alloc = tegra_smmu_domain_alloc,
889 .domain_free = tegra_smmu_domain_free,
Thierry Reding89184652014-04-16 09:24:44 +0200890 .attach_dev = tegra_smmu_attach_dev,
891 .detach_dev = tegra_smmu_detach_dev,
892 .add_device = tegra_smmu_add_device,
893 .remove_device = tegra_smmu_remove_device,
Thierry Reding7f4c9172017-10-12 16:19:16 +0200894 .device_group = tegra_smmu_device_group,
Thierry Reding89184652014-04-16 09:24:44 +0200895 .map = tegra_smmu_map,
896 .unmap = tegra_smmu_unmap,
Thierry Reding89184652014-04-16 09:24:44 +0200897 .iova_to_phys = tegra_smmu_iova_to_phys,
Thierry Reding7f4c9172017-10-12 16:19:16 +0200898 .of_xlate = tegra_smmu_of_xlate,
Thierry Reding89184652014-04-16 09:24:44 +0200899 .pgsize_bitmap = SZ_4K,
900};
901
902static void tegra_smmu_ahb_enable(void)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200903{
Thierry Reding89184652014-04-16 09:24:44 +0200904 static const struct of_device_id ahb_match[] = {
905 { .compatible = "nvidia,tegra30-ahb", },
906 { }
907 };
908 struct device_node *ahb;
909
910 ahb = of_find_matching_node(NULL, ahb_match);
911 if (ahb) {
912 tegra_ahb_enable_smmu(ahb);
913 of_node_put(ahb);
914 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200915}
916
Thierry Redingd1313e72015-01-23 09:49:25 +0100917static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
918{
919 struct tegra_smmu *smmu = s->private;
920 unsigned int i;
921 u32 value;
922
923 seq_printf(s, "swgroup enabled ASID\n");
924 seq_printf(s, "------------------------\n");
925
926 for (i = 0; i < smmu->soc->num_swgroups; i++) {
927 const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
928 const char *status;
929 unsigned int asid;
930
931 value = smmu_readl(smmu, group->reg);
932
933 if (value & SMMU_ASID_ENABLE)
934 status = "yes";
935 else
936 status = "no";
937
938 asid = value & SMMU_ASID_MASK;
939
940 seq_printf(s, "%-9s %-7s %#04x\n", group->name, status,
941 asid);
942 }
943
944 return 0;
945}
946
Yangtao Li062e52a2018-11-22 08:30:47 -0500947DEFINE_SHOW_ATTRIBUTE(tegra_smmu_swgroups);
Thierry Redingd1313e72015-01-23 09:49:25 +0100948
949static int tegra_smmu_clients_show(struct seq_file *s, void *data)
950{
951 struct tegra_smmu *smmu = s->private;
952 unsigned int i;
953 u32 value;
954
955 seq_printf(s, "client enabled\n");
956 seq_printf(s, "--------------------\n");
957
958 for (i = 0; i < smmu->soc->num_clients; i++) {
959 const struct tegra_mc_client *client = &smmu->soc->clients[i];
960 const char *status;
961
962 value = smmu_readl(smmu, client->smmu.reg);
963
964 if (value & BIT(client->smmu.bit))
965 status = "yes";
966 else
967 status = "no";
968
969 seq_printf(s, "%-12s %s\n", client->name, status);
970 }
971
972 return 0;
973}
974
Yangtao Li062e52a2018-11-22 08:30:47 -0500975DEFINE_SHOW_ATTRIBUTE(tegra_smmu_clients);
Thierry Redingd1313e72015-01-23 09:49:25 +0100976
977static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
978{
979 smmu->debugfs = debugfs_create_dir("smmu", NULL);
980 if (!smmu->debugfs)
981 return;
982
983 debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
984 &tegra_smmu_swgroups_fops);
985 debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
986 &tegra_smmu_clients_fops);
987}
988
989static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
990{
991 debugfs_remove_recursive(smmu->debugfs);
992}
993
Thierry Reding89184652014-04-16 09:24:44 +0200994struct tegra_smmu *tegra_smmu_probe(struct device *dev,
995 const struct tegra_smmu_soc *soc,
996 struct tegra_mc *mc)
997{
998 struct tegra_smmu *smmu;
999 size_t size;
1000 u32 value;
1001 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001002
Thierry Reding89184652014-04-16 09:24:44 +02001003 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1004 if (!smmu)
1005 return ERR_PTR(-ENOMEM);
1006
1007 /*
1008 * This is a bit of a hack. Ideally we'd want to simply return this
1009 * value. However the IOMMU registration process will attempt to add
1010 * all devices to the IOMMU when bus_set_iommu() is called. In order
1011 * not to rely on global variables to track the IOMMU instance, we
1012 * set it here so that it can be looked up from the .add_device()
1013 * callback via the IOMMU device's .drvdata field.
1014 */
1015 mc->smmu = smmu;
1016
1017 size = BITS_TO_LONGS(soc->num_asids) * sizeof(long);
1018
1019 smmu->asids = devm_kzalloc(dev, size, GFP_KERNEL);
1020 if (!smmu->asids)
1021 return ERR_PTR(-ENOMEM);
1022
Thierry Reding7f4c9172017-10-12 16:19:16 +02001023 INIT_LIST_HEAD(&smmu->groups);
Thierry Reding89184652014-04-16 09:24:44 +02001024 mutex_init(&smmu->lock);
1025
1026 smmu->regs = mc->regs;
1027 smmu->soc = soc;
1028 smmu->dev = dev;
1029 smmu->mc = mc;
1030
Thierry Reding804cb542015-03-27 11:07:27 +01001031 smmu->pfn_mask = BIT_MASK(mc->soc->num_address_bits - PAGE_SHIFT) - 1;
1032 dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
1033 mc->soc->num_address_bits, smmu->pfn_mask);
Thierry Reding11cec152015-08-06 14:20:31 +02001034 smmu->tlb_mask = (smmu->soc->num_tlb_lines << 1) - 1;
1035 dev_dbg(dev, "TLB lines: %u, mask: %#lx\n", smmu->soc->num_tlb_lines,
1036 smmu->tlb_mask);
Thierry Reding804cb542015-03-27 11:07:27 +01001037
Thierry Reding89184652014-04-16 09:24:44 +02001038 value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
1039
1040 if (soc->supports_request_limit)
1041 value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
1042
1043 smmu_writel(smmu, value, SMMU_PTC_CONFIG);
1044
1045 value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
Thierry Reding11cec152015-08-06 14:20:31 +02001046 SMMU_TLB_CONFIG_ACTIVE_LINES(smmu);
Thierry Reding89184652014-04-16 09:24:44 +02001047
1048 if (soc->supports_round_robin_arbitration)
1049 value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
1050
1051 smmu_writel(smmu, value, SMMU_TLB_CONFIG);
1052
Russell Kingb8fe0382015-07-27 13:29:41 +01001053 smmu_flush_ptc_all(smmu);
Thierry Reding89184652014-04-16 09:24:44 +02001054 smmu_flush_tlb(smmu);
1055 smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
1056 smmu_flush(smmu);
1057
1058 tegra_smmu_ahb_enable();
1059
Joerg Roedel0b480e42017-08-09 17:41:52 +02001060 err = iommu_device_sysfs_add(&smmu->iommu, dev, NULL, dev_name(dev));
1061 if (err)
1062 return ERR_PTR(err);
1063
1064 iommu_device_set_ops(&smmu->iommu, &tegra_smmu_ops);
Thierry Reding7f4c9172017-10-12 16:19:16 +02001065 iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
Joerg Roedel0b480e42017-08-09 17:41:52 +02001066
1067 err = iommu_device_register(&smmu->iommu);
1068 if (err) {
1069 iommu_device_sysfs_remove(&smmu->iommu);
1070 return ERR_PTR(err);
1071 }
1072
Joerg Roedel96302d82017-08-30 15:06:43 +02001073 err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
1074 if (err < 0) {
1075 iommu_device_unregister(&smmu->iommu);
1076 iommu_device_sysfs_remove(&smmu->iommu);
1077 return ERR_PTR(err);
1078 }
1079
Thierry Redingd1313e72015-01-23 09:49:25 +01001080 if (IS_ENABLED(CONFIG_DEBUG_FS))
1081 tegra_smmu_debugfs_init(smmu);
1082
Thierry Reding89184652014-04-16 09:24:44 +02001083 return smmu;
1084}
Thierry Redingd1313e72015-01-23 09:49:25 +01001085
1086void tegra_smmu_remove(struct tegra_smmu *smmu)
1087{
Joerg Roedel0b480e42017-08-09 17:41:52 +02001088 iommu_device_unregister(&smmu->iommu);
1089 iommu_device_sysfs_remove(&smmu->iommu);
1090
Thierry Redingd1313e72015-01-23 09:49:25 +01001091 if (IS_ENABLED(CONFIG_DEBUG_FS))
1092 tegra_smmu_debugfs_exit(smmu);
1093}