blob: 463ee08f7d3a5956f91d07aed7cf82a01b3ee7fd [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 Reding7f4c9172017-10-12 16:19:16 +020023struct tegra_smmu_group {
24 struct list_head list;
25 const struct tegra_smmu_group_soc *soc;
26 struct iommu_group *group;
27};
28
Thierry Reding89184652014-04-16 09:24:44 +020029struct tegra_smmu {
30 void __iomem *regs;
31 struct device *dev;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020032
Thierry Reding89184652014-04-16 09:24:44 +020033 struct tegra_mc *mc;
34 const struct tegra_smmu_soc *soc;
Stephen Warrene6bc5932012-09-04 16:36:15 -060035
Thierry Reding7f4c9172017-10-12 16:19:16 +020036 struct list_head groups;
37
Thierry Reding804cb542015-03-27 11:07:27 +010038 unsigned long pfn_mask;
Thierry Reding11cec152015-08-06 14:20:31 +020039 unsigned long tlb_mask;
Thierry Reding804cb542015-03-27 11:07:27 +010040
Thierry Reding89184652014-04-16 09:24:44 +020041 unsigned long *asids;
42 struct mutex lock;
Stephen Warrene6bc5932012-09-04 16:36:15 -060043
Thierry Reding89184652014-04-16 09:24:44 +020044 struct list_head list;
Thierry Redingd1313e72015-01-23 09:49:25 +010045
46 struct dentry *debugfs;
Joerg Roedel0b480e42017-08-09 17:41:52 +020047
48 struct iommu_device iommu; /* IOMMU Core code handle */
Stephen Warrene6bc5932012-09-04 16:36:15 -060049};
50
Thierry Reding89184652014-04-16 09:24:44 +020051struct tegra_smmu_as {
Joerg Roedeld5f1a812015-03-26 13:43:12 +010052 struct iommu_domain domain;
Thierry Reding89184652014-04-16 09:24:44 +020053 struct tegra_smmu *smmu;
54 unsigned int use_count;
Russell King32924c72015-07-27 13:29:31 +010055 u32 *count;
Russell King853520f2015-07-27 13:29:26 +010056 struct page **pts;
Thierry Reding89184652014-04-16 09:24:44 +020057 struct page *pd;
Russell Kinge3c97192015-07-27 13:29:52 +010058 dma_addr_t pd_dma;
Thierry Reding89184652014-04-16 09:24:44 +020059 unsigned id;
60 u32 attr;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +030061};
62
Joerg Roedeld5f1a812015-03-26 13:43:12 +010063static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom)
64{
65 return container_of(dom, struct tegra_smmu_as, domain);
66}
67
Thierry Reding89184652014-04-16 09:24:44 +020068static inline void smmu_writel(struct tegra_smmu *smmu, u32 value,
69 unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020070{
Thierry Reding89184652014-04-16 09:24:44 +020071 writel(value, smmu->regs + offset);
Joerg Roedelfe1229b2013-02-04 20:40:58 +010072}
73
Thierry Reding89184652014-04-16 09:24:44 +020074static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020075{
Thierry Reding89184652014-04-16 09:24:44 +020076 return readl(smmu->regs + offset);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020077}
78
Thierry Reding89184652014-04-16 09:24:44 +020079#define SMMU_CONFIG 0x010
80#define SMMU_CONFIG_ENABLE (1 << 0)
81
82#define SMMU_TLB_CONFIG 0x14
83#define SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29)
84#define SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28)
Thierry Reding11cec152015-08-06 14:20:31 +020085#define SMMU_TLB_CONFIG_ACTIVE_LINES(smmu) \
86 ((smmu)->soc->num_tlb_lines & (smmu)->tlb_mask)
Thierry Reding89184652014-04-16 09:24:44 +020087
88#define SMMU_PTC_CONFIG 0x18
89#define SMMU_PTC_CONFIG_ENABLE (1 << 29)
90#define SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24)
91#define SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f)
92
93#define SMMU_PTB_ASID 0x01c
94#define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
95
96#define SMMU_PTB_DATA 0x020
Russell Kinge3c97192015-07-27 13:29:52 +010097#define SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr))
Thierry Reding89184652014-04-16 09:24:44 +020098
Russell Kinge3c97192015-07-27 13:29:52 +010099#define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr))
Thierry Reding89184652014-04-16 09:24:44 +0200100
101#define SMMU_TLB_FLUSH 0x030
102#define SMMU_TLB_FLUSH_VA_MATCH_ALL (0 << 0)
103#define SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0)
104#define SMMU_TLB_FLUSH_VA_MATCH_GROUP (3 << 0)
Thierry Reding89184652014-04-16 09:24:44 +0200105#define SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
106 SMMU_TLB_FLUSH_VA_MATCH_SECTION)
107#define SMMU_TLB_FLUSH_VA_GROUP(addr) ((((addr) & 0xffffc000) >> 12) | \
108 SMMU_TLB_FLUSH_VA_MATCH_GROUP)
109#define SMMU_TLB_FLUSH_ASID_MATCH (1 << 31)
110
111#define SMMU_PTC_FLUSH 0x034
112#define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
113#define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
114
115#define SMMU_PTC_FLUSH_HI 0x9b8
116#define SMMU_PTC_FLUSH_HI_MASK 0x3
117
118/* per-SWGROUP SMMU_*_ASID register */
119#define SMMU_ASID_ENABLE (1 << 31)
120#define SMMU_ASID_MASK 0x7f
121#define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
122
123/* page table definitions */
124#define SMMU_NUM_PDE 1024
125#define SMMU_NUM_PTE 1024
126
127#define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
128#define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
129
130#define SMMU_PDE_SHIFT 22
131#define SMMU_PTE_SHIFT 12
132
Thierry Reding89184652014-04-16 09:24:44 +0200133#define SMMU_PD_READABLE (1 << 31)
134#define SMMU_PD_WRITABLE (1 << 30)
135#define SMMU_PD_NONSECURE (1 << 29)
136
137#define SMMU_PDE_READABLE (1 << 31)
138#define SMMU_PDE_WRITABLE (1 << 30)
139#define SMMU_PDE_NONSECURE (1 << 29)
140#define SMMU_PDE_NEXT (1 << 28)
141
142#define SMMU_PTE_READABLE (1 << 31)
143#define SMMU_PTE_WRITABLE (1 << 30)
144#define SMMU_PTE_NONSECURE (1 << 29)
145
146#define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
147 SMMU_PDE_NONSECURE)
Thierry Reding89184652014-04-16 09:24:44 +0200148
Russell King34d35f82015-07-27 13:29:16 +0100149static unsigned int iova_pd_index(unsigned long iova)
150{
151 return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1);
152}
153
154static unsigned int iova_pt_index(unsigned long iova)
155{
156 return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1);
157}
158
Russell Kinge3c97192015-07-27 13:29:52 +0100159static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr)
Russell King4b3c7d12015-07-27 13:29:36 +0100160{
Russell Kinge3c97192015-07-27 13:29:52 +0100161 addr >>= 12;
162 return (addr & smmu->pfn_mask) == addr;
163}
Russell King4b3c7d12015-07-27 13:29:36 +0100164
Russell Kinge3c97192015-07-27 13:29:52 +0100165static dma_addr_t smmu_pde_to_dma(u32 pde)
166{
167 return pde << 12;
Russell King4b3c7d12015-07-27 13:29:36 +0100168}
169
Russell Kingb8fe0382015-07-27 13:29:41 +0100170static void smmu_flush_ptc_all(struct tegra_smmu *smmu)
171{
172 smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
173}
174
Russell Kinge3c97192015-07-27 13:29:52 +0100175static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma,
Thierry Reding89184652014-04-16 09:24:44 +0200176 unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200177{
Thierry Reding89184652014-04-16 09:24:44 +0200178 u32 value;
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200179
Russell Kingb8fe0382015-07-27 13:29:41 +0100180 offset &= ~(smmu->mc->soc->atom_size - 1);
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200181
Russell Kingb8fe0382015-07-27 13:29:41 +0100182 if (smmu->mc->soc->num_address_bits > 32) {
Russell Kinge3c97192015-07-27 13:29:52 +0100183#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
184 value = (dma >> 32) & SMMU_PTC_FLUSH_HI_MASK;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200185#else
Russell Kingb8fe0382015-07-27 13:29:41 +0100186 value = 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200187#endif
Russell Kingb8fe0382015-07-27 13:29:41 +0100188 smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200189 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300190
Russell Kinge3c97192015-07-27 13:29:52 +0100191 value = (dma + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
Thierry Reding89184652014-04-16 09:24:44 +0200192 smmu_writel(smmu, value, SMMU_PTC_FLUSH);
193}
194
195static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
196{
197 smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
198}
199
200static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
201 unsigned long asid)
202{
203 u32 value;
204
Dmitry Osipenko43a05412019-03-07 01:50:07 +0300205 if (smmu->soc->num_asids == 4)
206 value = (asid & 0x3) << 29;
207 else
208 value = (asid & 0x7f) << 24;
209
210 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_MATCH_ALL;
Thierry Reding89184652014-04-16 09:24:44 +0200211 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
212}
213
214static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
215 unsigned long asid,
216 unsigned long iova)
217{
218 u32 value;
219
Dmitry Osipenko43a05412019-03-07 01:50:07 +0300220 if (smmu->soc->num_asids == 4)
221 value = (asid & 0x3) << 29;
222 else
223 value = (asid & 0x7f) << 24;
224
225 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_SECTION(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200226 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
227}
228
229static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu,
230 unsigned long asid,
231 unsigned long iova)
232{
233 u32 value;
234
Dmitry Osipenko43a05412019-03-07 01:50:07 +0300235 if (smmu->soc->num_asids == 4)
236 value = (asid & 0x3) << 29;
237 else
238 value = (asid & 0x7f) << 24;
239
240 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_GROUP(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200241 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
242}
243
244static inline void smmu_flush(struct tegra_smmu *smmu)
245{
246 smmu_readl(smmu, SMMU_CONFIG);
247}
248
249static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
250{
251 unsigned long id;
252
253 mutex_lock(&smmu->lock);
254
255 id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
256 if (id >= smmu->soc->num_asids) {
257 mutex_unlock(&smmu->lock);
258 return -ENOSPC;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200259 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300260
Thierry Reding89184652014-04-16 09:24:44 +0200261 set_bit(id, smmu->asids);
262 *idp = id;
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300263
Thierry Reding89184652014-04-16 09:24:44 +0200264 mutex_unlock(&smmu->lock);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200265 return 0;
266}
267
Thierry Reding89184652014-04-16 09:24:44 +0200268static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200269{
Thierry Reding89184652014-04-16 09:24:44 +0200270 mutex_lock(&smmu->lock);
271 clear_bit(id, smmu->asids);
272 mutex_unlock(&smmu->lock);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200273}
274
Thierry Reding89184652014-04-16 09:24:44 +0200275static bool tegra_smmu_capable(enum iommu_cap cap)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200276{
Joerg Roedel7c2aa642014-09-05 10:51:37 +0200277 return false;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200278}
279
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100280static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200281{
Thierry Reding89184652014-04-16 09:24:44 +0200282 struct tegra_smmu_as *as;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200283
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100284 if (type != IOMMU_DOMAIN_UNMANAGED)
285 return NULL;
286
Thierry Reding89184652014-04-16 09:24:44 +0200287 as = kzalloc(sizeof(*as), GFP_KERNEL);
288 if (!as)
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100289 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200290
Thierry Reding89184652014-04-16 09:24:44 +0200291 as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200292
Russell King707917c2015-07-27 13:30:02 +0100293 as->pd = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
Thierry Reding89184652014-04-16 09:24:44 +0200294 if (!as->pd) {
295 kfree(as);
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100296 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200297 }
298
Russell King32924c72015-07-27 13:29:31 +0100299 as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL);
Thierry Reding89184652014-04-16 09:24:44 +0200300 if (!as->count) {
301 __free_page(as->pd);
302 kfree(as);
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100303 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200304 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200305
Russell King853520f2015-07-27 13:29:26 +0100306 as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL);
307 if (!as->pts) {
Russell King32924c72015-07-27 13:29:31 +0100308 kfree(as->count);
Russell King853520f2015-07-27 13:29:26 +0100309 __free_page(as->pd);
310 kfree(as);
311 return NULL;
312 }
313
Thierry Reding471d9142015-03-27 11:07:25 +0100314 /* setup aperture */
Joerg Roedel7f65ef02015-04-02 13:33:19 +0200315 as->domain.geometry.aperture_start = 0;
316 as->domain.geometry.aperture_end = 0xffffffff;
317 as->domain.geometry.force_aperture = true;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200318
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100319 return &as->domain;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200320}
321
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100322static void tegra_smmu_domain_free(struct iommu_domain *domain)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200323{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100324 struct tegra_smmu_as *as = to_smmu_as(domain);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200325
Thierry Reding89184652014-04-16 09:24:44 +0200326 /* TODO: free page directory and page tables */
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200327
Dmitry Osipenko4f970312019-03-07 01:50:08 +0300328 WARN_ON_ONCE(as->use_count);
329 kfree(as->count);
330 kfree(as->pts);
Thierry Reding89184652014-04-16 09:24:44 +0200331 kfree(as);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200332}
333
Thierry Reding89184652014-04-16 09:24:44 +0200334static const struct tegra_smmu_swgroup *
335tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300336{
Thierry Reding89184652014-04-16 09:24:44 +0200337 const struct tegra_smmu_swgroup *group = NULL;
338 unsigned int i;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300339
Thierry Reding89184652014-04-16 09:24:44 +0200340 for (i = 0; i < smmu->soc->num_swgroups; i++) {
341 if (smmu->soc->swgroups[i].swgroup == swgroup) {
342 group = &smmu->soc->swgroups[i];
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300343 break;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300344 }
345 }
346
Thierry Reding89184652014-04-16 09:24:44 +0200347 return group;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300348}
349
Thierry Reding89184652014-04-16 09:24:44 +0200350static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
351 unsigned int asid)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200352{
Thierry Reding89184652014-04-16 09:24:44 +0200353 const struct tegra_smmu_swgroup *group;
354 unsigned int i;
355 u32 value;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200356
Thierry Reding89184652014-04-16 09:24:44 +0200357 for (i = 0; i < smmu->soc->num_clients; i++) {
358 const struct tegra_mc_client *client = &smmu->soc->clients[i];
359
360 if (client->swgroup != swgroup)
361 continue;
362
363 value = smmu_readl(smmu, client->smmu.reg);
364 value |= BIT(client->smmu.bit);
365 smmu_writel(smmu, value, client->smmu.reg);
366 }
367
368 group = tegra_smmu_find_swgroup(smmu, swgroup);
369 if (group) {
370 value = smmu_readl(smmu, group->reg);
371 value &= ~SMMU_ASID_MASK;
372 value |= SMMU_ASID_VALUE(asid);
373 value |= SMMU_ASID_ENABLE;
374 smmu_writel(smmu, value, group->reg);
375 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200376}
377
Thierry Reding89184652014-04-16 09:24:44 +0200378static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
379 unsigned int asid)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200380{
Thierry Reding89184652014-04-16 09:24:44 +0200381 const struct tegra_smmu_swgroup *group;
382 unsigned int i;
383 u32 value;
384
385 group = tegra_smmu_find_swgroup(smmu, swgroup);
386 if (group) {
387 value = smmu_readl(smmu, group->reg);
388 value &= ~SMMU_ASID_MASK;
389 value |= SMMU_ASID_VALUE(asid);
390 value &= ~SMMU_ASID_ENABLE;
391 smmu_writel(smmu, value, group->reg);
392 }
393
394 for (i = 0; i < smmu->soc->num_clients; i++) {
395 const struct tegra_mc_client *client = &smmu->soc->clients[i];
396
397 if (client->swgroup != swgroup)
398 continue;
399
400 value = smmu_readl(smmu, client->smmu.reg);
401 value &= ~BIT(client->smmu.bit);
402 smmu_writel(smmu, value, client->smmu.reg);
403 }
404}
405
406static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
407 struct tegra_smmu_as *as)
408{
409 u32 value;
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300410 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200411
Thierry Reding89184652014-04-16 09:24:44 +0200412 if (as->use_count > 0) {
413 as->use_count++;
414 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200415 }
416
Russell Kinge3c97192015-07-27 13:29:52 +0100417 as->pd_dma = dma_map_page(smmu->dev, as->pd, 0, SMMU_SIZE_PD,
418 DMA_TO_DEVICE);
419 if (dma_mapping_error(smmu->dev, as->pd_dma))
420 return -ENOMEM;
421
422 /* We can't handle 64-bit DMA addresses */
423 if (!smmu_dma_addr_valid(smmu, as->pd_dma)) {
424 err = -ENOMEM;
425 goto err_unmap;
426 }
427
Thierry Reding89184652014-04-16 09:24:44 +0200428 err = tegra_smmu_alloc_asid(smmu, &as->id);
429 if (err < 0)
Russell Kinge3c97192015-07-27 13:29:52 +0100430 goto err_unmap;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200431
Russell Kinge3c97192015-07-27 13:29:52 +0100432 smmu_flush_ptc(smmu, as->pd_dma, 0);
Thierry Reding89184652014-04-16 09:24:44 +0200433 smmu_flush_tlb_asid(smmu, as->id);
434
435 smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
Russell Kinge3c97192015-07-27 13:29:52 +0100436 value = SMMU_PTB_DATA_VALUE(as->pd_dma, as->attr);
Thierry Reding89184652014-04-16 09:24:44 +0200437 smmu_writel(smmu, value, SMMU_PTB_DATA);
438 smmu_flush(smmu);
439
440 as->smmu = smmu;
441 as->use_count++;
442
443 return 0;
Russell Kinge3c97192015-07-27 13:29:52 +0100444
445err_unmap:
446 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
447 return err;
Thierry Reding89184652014-04-16 09:24:44 +0200448}
449
450static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
451 struct tegra_smmu_as *as)
452{
453 if (--as->use_count > 0)
454 return;
455
456 tegra_smmu_free_asid(smmu, as->id);
Russell Kinge3c97192015-07-27 13:29:52 +0100457
458 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
459
Thierry Reding89184652014-04-16 09:24:44 +0200460 as->smmu = NULL;
461}
462
463static int tegra_smmu_attach_dev(struct iommu_domain *domain,
464 struct device *dev)
465{
466 struct tegra_smmu *smmu = dev->archdata.iommu;
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100467 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200468 struct device_node *np = dev->of_node;
469 struct of_phandle_args args;
470 unsigned int index = 0;
471 int err = 0;
472
473 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
474 &args)) {
475 unsigned int swgroup = args.args[0];
476
477 if (args.np != smmu->dev->of_node) {
478 of_node_put(args.np);
479 continue;
480 }
481
482 of_node_put(args.np);
483
484 err = tegra_smmu_as_prepare(smmu, as);
485 if (err < 0)
486 return err;
487
488 tegra_smmu_enable(smmu, swgroup, as->id);
489 index++;
490 }
491
492 if (index == 0)
493 return -ENODEV;
494
495 return 0;
496}
497
498static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
499{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100500 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200501 struct device_node *np = dev->of_node;
502 struct tegra_smmu *smmu = as->smmu;
503 struct of_phandle_args args;
504 unsigned int index = 0;
505
506 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
507 &args)) {
508 unsigned int swgroup = args.args[0];
509
510 if (args.np != smmu->dev->of_node) {
511 of_node_put(args.np);
512 continue;
513 }
514
515 of_node_put(args.np);
516
517 tegra_smmu_disable(smmu, swgroup, as->id);
518 tegra_smmu_as_unprepare(smmu, as);
519 index++;
520 }
521}
522
Russell King4080e992015-07-27 13:30:12 +0100523static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova,
524 u32 value)
525{
526 unsigned int pd_index = iova_pd_index(iova);
527 struct tegra_smmu *smmu = as->smmu;
528 u32 *pd = page_address(as->pd);
529 unsigned long offset = pd_index * sizeof(*pd);
530
531 /* Set the page directory entry first */
532 pd[pd_index] = value;
533
534 /* The flush the page directory entry from caches */
535 dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset,
536 sizeof(*pd), DMA_TO_DEVICE);
537
538 /* And flush the iommu */
539 smmu_flush_ptc(smmu, as->pd_dma, offset);
540 smmu_flush_tlb_section(smmu, as->id, iova);
541 smmu_flush(smmu);
542}
543
Russell King0b42c7c2015-07-27 13:29:21 +0100544static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova)
545{
546 u32 *pt = page_address(pt_page);
547
548 return pt + iova_pt_index(iova);
549}
550
551static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100552 dma_addr_t *dmap)
Russell King0b42c7c2015-07-27 13:29:21 +0100553{
554 unsigned int pd_index = iova_pd_index(iova);
555 struct page *pt_page;
Russell Kinge3c97192015-07-27 13:29:52 +0100556 u32 *pd;
Russell King0b42c7c2015-07-27 13:29:21 +0100557
Russell King853520f2015-07-27 13:29:26 +0100558 pt_page = as->pts[pd_index];
559 if (!pt_page)
Russell King0b42c7c2015-07-27 13:29:21 +0100560 return NULL;
561
Russell Kinge3c97192015-07-27 13:29:52 +0100562 pd = page_address(as->pd);
563 *dmap = smmu_pde_to_dma(pd[pd_index]);
Russell King0b42c7c2015-07-27 13:29:21 +0100564
565 return tegra_smmu_pte_offset(pt_page, iova);
566}
567
Thierry Reding89184652014-04-16 09:24:44 +0200568static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100569 dma_addr_t *dmap)
Thierry Reding89184652014-04-16 09:24:44 +0200570{
Russell King34d35f82015-07-27 13:29:16 +0100571 unsigned int pde = iova_pd_index(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200572 struct tegra_smmu *smmu = as->smmu;
Thierry Reding89184652014-04-16 09:24:44 +0200573
Russell King853520f2015-07-27 13:29:26 +0100574 if (!as->pts[pde]) {
Russell Kinge3c97192015-07-27 13:29:52 +0100575 struct page *page;
576 dma_addr_t dma;
577
Russell King707917c2015-07-27 13:30:02 +0100578 page = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
Thierry Reding89184652014-04-16 09:24:44 +0200579 if (!page)
580 return NULL;
581
Russell Kinge3c97192015-07-27 13:29:52 +0100582 dma = dma_map_page(smmu->dev, page, 0, SMMU_SIZE_PT,
583 DMA_TO_DEVICE);
584 if (dma_mapping_error(smmu->dev, dma)) {
585 __free_page(page);
586 return NULL;
587 }
588
589 if (!smmu_dma_addr_valid(smmu, dma)) {
590 dma_unmap_page(smmu->dev, dma, SMMU_SIZE_PT,
591 DMA_TO_DEVICE);
592 __free_page(page);
593 return NULL;
594 }
595
Russell King853520f2015-07-27 13:29:26 +0100596 as->pts[pde] = page;
597
Russell King4080e992015-07-27 13:30:12 +0100598 tegra_smmu_set_pde(as, iova, SMMU_MK_PDE(dma, SMMU_PDE_ATTR |
599 SMMU_PDE_NEXT));
Russell Kinge3c97192015-07-27 13:29:52 +0100600
601 *dmap = dma;
Thierry Reding89184652014-04-16 09:24:44 +0200602 } else {
Russell King4080e992015-07-27 13:30:12 +0100603 u32 *pd = page_address(as->pd);
604
Russell Kinge3c97192015-07-27 13:29:52 +0100605 *dmap = smmu_pde_to_dma(pd[pde]);
Thierry Reding89184652014-04-16 09:24:44 +0200606 }
607
Russell King7ffc6f02015-08-06 14:56:39 +0200608 return tegra_smmu_pte_offset(as->pts[pde], iova);
609}
Russell King0b42c7c2015-07-27 13:29:21 +0100610
Russell King7ffc6f02015-08-06 14:56:39 +0200611static void tegra_smmu_pte_get_use(struct tegra_smmu_as *as, unsigned long iova)
612{
613 unsigned int pd_index = iova_pd_index(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200614
Russell King7ffc6f02015-08-06 14:56:39 +0200615 as->count[pd_index]++;
Thierry Reding89184652014-04-16 09:24:44 +0200616}
617
Russell Kingb98e34f2015-07-27 13:29:05 +0100618static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
Thierry Reding89184652014-04-16 09:24:44 +0200619{
Russell King34d35f82015-07-27 13:29:16 +0100620 unsigned int pde = iova_pd_index(iova);
Russell King853520f2015-07-27 13:29:26 +0100621 struct page *page = as->pts[pde];
Thierry Reding89184652014-04-16 09:24:44 +0200622
623 /*
624 * When no entries in this page table are used anymore, return the
625 * memory page to the system.
626 */
Russell King32924c72015-07-27 13:29:31 +0100627 if (--as->count[pde] == 0) {
Russell King4080e992015-07-27 13:30:12 +0100628 struct tegra_smmu *smmu = as->smmu;
629 u32 *pd = page_address(as->pd);
Russell Kinge3c97192015-07-27 13:29:52 +0100630 dma_addr_t pte_dma = smmu_pde_to_dma(pd[pde]);
Thierry Reding89184652014-04-16 09:24:44 +0200631
Russell King4080e992015-07-27 13:30:12 +0100632 tegra_smmu_set_pde(as, iova, 0);
Russell Kingb98e34f2015-07-27 13:29:05 +0100633
Russell Kinge3c97192015-07-27 13:29:52 +0100634 dma_unmap_page(smmu->dev, pte_dma, SMMU_SIZE_PT, DMA_TO_DEVICE);
Russell Kingb98e34f2015-07-27 13:29:05 +0100635 __free_page(page);
Russell King853520f2015-07-27 13:29:26 +0100636 as->pts[pde] = NULL;
Thierry Reding89184652014-04-16 09:24:44 +0200637 }
638}
639
Russell King8482ee52015-07-27 13:29:10 +0100640static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100641 u32 *pte, dma_addr_t pte_dma, u32 val)
Russell King8482ee52015-07-27 13:29:10 +0100642{
643 struct tegra_smmu *smmu = as->smmu;
644 unsigned long offset = offset_in_page(pte);
645
646 *pte = val;
647
Russell Kinge3c97192015-07-27 13:29:52 +0100648 dma_sync_single_range_for_device(smmu->dev, pte_dma, offset,
649 4, DMA_TO_DEVICE);
650 smmu_flush_ptc(smmu, pte_dma, offset);
Russell King8482ee52015-07-27 13:29:10 +0100651 smmu_flush_tlb_group(smmu, as->id, iova);
652 smmu_flush(smmu);
653}
654
Thierry Reding89184652014-04-16 09:24:44 +0200655static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
656 phys_addr_t paddr, size_t size, int prot)
657{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100658 struct tegra_smmu_as *as = to_smmu_as(domain);
Russell Kinge3c97192015-07-27 13:29:52 +0100659 dma_addr_t pte_dma;
Dmitry Osipenko43d957b2019-03-07 01:50:09 +0300660 u32 pte_attrs;
Thierry Reding89184652014-04-16 09:24:44 +0200661 u32 *pte;
662
Russell Kinge3c97192015-07-27 13:29:52 +0100663 pte = as_get_pte(as, iova, &pte_dma);
Thierry Reding89184652014-04-16 09:24:44 +0200664 if (!pte)
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +0300665 return -ENOMEM;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200666
Russell King7ffc6f02015-08-06 14:56:39 +0200667 /* If we aren't overwriting a pre-existing entry, increment use */
668 if (*pte == 0)
669 tegra_smmu_pte_get_use(as, iova);
670
Dmitry Osipenko43d957b2019-03-07 01:50:09 +0300671 pte_attrs = SMMU_PTE_NONSECURE;
672
673 if (prot & IOMMU_READ)
674 pte_attrs |= SMMU_PTE_READABLE;
675
676 if (prot & IOMMU_WRITE)
677 pte_attrs |= SMMU_PTE_WRITABLE;
678
Russell Kinge3c97192015-07-27 13:29:52 +0100679 tegra_smmu_set_pte(as, iova, pte, pte_dma,
Dmitry Osipenko43d957b2019-03-07 01:50:09 +0300680 __phys_to_pfn(paddr) | pte_attrs);
Thierry Reding89184652014-04-16 09:24:44 +0200681
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200682 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200683}
684
Thierry Reding89184652014-04-16 09:24:44 +0200685static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
686 size_t size)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200687{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100688 struct tegra_smmu_as *as = to_smmu_as(domain);
Russell Kinge3c97192015-07-27 13:29:52 +0100689 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200690 u32 *pte;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200691
Russell Kinge3c97192015-07-27 13:29:52 +0100692 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
Russell Kingb98e34f2015-07-27 13:29:05 +0100693 if (!pte || !*pte)
Thierry Reding89184652014-04-16 09:24:44 +0200694 return 0;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300695
Russell Kinge3c97192015-07-27 13:29:52 +0100696 tegra_smmu_set_pte(as, iova, pte, pte_dma, 0);
Russell Kingb98e34f2015-07-27 13:29:05 +0100697 tegra_smmu_pte_put_use(as, iova);
698
Thierry Reding89184652014-04-16 09:24:44 +0200699 return size;
700}
701
702static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
703 dma_addr_t iova)
704{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100705 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200706 unsigned long pfn;
Russell Kinge3c97192015-07-27 13:29:52 +0100707 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200708 u32 *pte;
709
Russell Kinge3c97192015-07-27 13:29:52 +0100710 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
Russell King91137852015-07-27 13:29:00 +0100711 if (!pte || !*pte)
712 return 0;
713
Thierry Reding804cb542015-03-27 11:07:27 +0100714 pfn = *pte & as->smmu->pfn_mask;
Thierry Reding89184652014-04-16 09:24:44 +0200715
716 return PFN_PHYS(pfn);
717}
718
719static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
720{
721 struct platform_device *pdev;
722 struct tegra_mc *mc;
723
724 pdev = of_find_device_by_node(np);
725 if (!pdev)
726 return NULL;
727
728 mc = platform_get_drvdata(pdev);
729 if (!mc)
730 return NULL;
731
732 return mc->smmu;
733}
734
Thierry Reding7f4c9172017-10-12 16:19:16 +0200735static int tegra_smmu_configure(struct tegra_smmu *smmu, struct device *dev,
736 struct of_phandle_args *args)
737{
738 const struct iommu_ops *ops = smmu->iommu.ops;
739 int err;
740
741 err = iommu_fwspec_init(dev, &dev->of_node->fwnode, ops);
742 if (err < 0) {
743 dev_err(dev, "failed to initialize fwspec: %d\n", err);
744 return err;
745 }
746
747 err = ops->of_xlate(dev, args);
748 if (err < 0) {
749 dev_err(dev, "failed to parse SW group ID: %d\n", err);
750 iommu_fwspec_free(dev);
751 return err;
752 }
753
754 return 0;
755}
756
Thierry Reding89184652014-04-16 09:24:44 +0200757static int tegra_smmu_add_device(struct device *dev)
758{
759 struct device_node *np = dev->of_node;
Thierry Reding7f4c9172017-10-12 16:19:16 +0200760 struct tegra_smmu *smmu = NULL;
Robin Murphyd92e1f82017-07-21 13:12:36 +0100761 struct iommu_group *group;
Thierry Reding89184652014-04-16 09:24:44 +0200762 struct of_phandle_args args;
763 unsigned int index = 0;
Thierry Reding7f4c9172017-10-12 16:19:16 +0200764 int err;
Thierry Reding89184652014-04-16 09:24:44 +0200765
766 while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
767 &args) == 0) {
Thierry Reding89184652014-04-16 09:24:44 +0200768 smmu = tegra_smmu_find(args.np);
769 if (smmu) {
Thierry Reding7f4c9172017-10-12 16:19:16 +0200770 err = tegra_smmu_configure(smmu, dev, &args);
771 of_node_put(args.np);
772
773 if (err < 0)
774 return err;
775
Thierry Reding89184652014-04-16 09:24:44 +0200776 /*
777 * Only a single IOMMU master interface is currently
778 * supported by the Linux kernel, so abort after the
779 * first match.
780 */
781 dev->archdata.iommu = smmu;
Joerg Roedel0b480e42017-08-09 17:41:52 +0200782
783 iommu_device_link(&smmu->iommu, dev);
784
Thierry Reding89184652014-04-16 09:24:44 +0200785 break;
786 }
787
Thierry Reding7f4c9172017-10-12 16:19:16 +0200788 of_node_put(args.np);
Thierry Reding89184652014-04-16 09:24:44 +0200789 index++;
790 }
791
Thierry Reding7f4c9172017-10-12 16:19:16 +0200792 if (!smmu)
793 return -ENODEV;
794
Robin Murphyd92e1f82017-07-21 13:12:36 +0100795 group = iommu_group_get_for_dev(dev);
796 if (IS_ERR(group))
797 return PTR_ERR(group);
798
799 iommu_group_put(group);
800
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200801 return 0;
802}
803
Thierry Reding89184652014-04-16 09:24:44 +0200804static void tegra_smmu_remove_device(struct device *dev)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200805{
Joerg Roedel0b480e42017-08-09 17:41:52 +0200806 struct tegra_smmu *smmu = dev->archdata.iommu;
807
808 if (smmu)
809 iommu_device_unlink(&smmu->iommu, dev);
810
Thierry Reding89184652014-04-16 09:24:44 +0200811 dev->archdata.iommu = NULL;
Robin Murphyd92e1f82017-07-21 13:12:36 +0100812 iommu_group_remove_device(dev);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200813}
814
Thierry Reding7f4c9172017-10-12 16:19:16 +0200815static const struct tegra_smmu_group_soc *
816tegra_smmu_find_group(struct tegra_smmu *smmu, unsigned int swgroup)
817{
818 unsigned int i, j;
819
820 for (i = 0; i < smmu->soc->num_groups; i++)
821 for (j = 0; j < smmu->soc->groups[i].num_swgroups; j++)
822 if (smmu->soc->groups[i].swgroups[j] == swgroup)
823 return &smmu->soc->groups[i];
824
825 return NULL;
826}
827
828static struct iommu_group *tegra_smmu_group_get(struct tegra_smmu *smmu,
829 unsigned int swgroup)
830{
831 const struct tegra_smmu_group_soc *soc;
832 struct tegra_smmu_group *group;
833
834 soc = tegra_smmu_find_group(smmu, swgroup);
835 if (!soc)
836 return NULL;
837
838 mutex_lock(&smmu->lock);
839
840 list_for_each_entry(group, &smmu->groups, list)
841 if (group->soc == soc) {
842 mutex_unlock(&smmu->lock);
843 return group->group;
844 }
845
846 group = devm_kzalloc(smmu->dev, sizeof(*group), GFP_KERNEL);
847 if (!group) {
848 mutex_unlock(&smmu->lock);
849 return NULL;
850 }
851
852 INIT_LIST_HEAD(&group->list);
853 group->soc = soc;
854
855 group->group = iommu_group_alloc();
Wei Yongjun83476bf2017-12-20 03:06:09 +0000856 if (IS_ERR(group->group)) {
Thierry Reding7f4c9172017-10-12 16:19:16 +0200857 devm_kfree(smmu->dev, group);
858 mutex_unlock(&smmu->lock);
859 return NULL;
860 }
861
862 list_add_tail(&group->list, &smmu->groups);
863 mutex_unlock(&smmu->lock);
864
865 return group->group;
866}
867
868static struct iommu_group *tegra_smmu_device_group(struct device *dev)
869{
Joerg Roedeldb5d6a72018-11-29 14:01:00 +0100870 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Thierry Reding7f4c9172017-10-12 16:19:16 +0200871 struct tegra_smmu *smmu = dev->archdata.iommu;
872 struct iommu_group *group;
873
874 group = tegra_smmu_group_get(smmu, fwspec->ids[0]);
875 if (!group)
876 group = generic_device_group(dev);
877
878 return group;
879}
880
881static int tegra_smmu_of_xlate(struct device *dev,
882 struct of_phandle_args *args)
883{
884 u32 id = args->args[0];
885
886 return iommu_fwspec_add_ids(dev, &id, 1);
887}
888
Thierry Reding89184652014-04-16 09:24:44 +0200889static const struct iommu_ops tegra_smmu_ops = {
890 .capable = tegra_smmu_capable,
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100891 .domain_alloc = tegra_smmu_domain_alloc,
892 .domain_free = tegra_smmu_domain_free,
Thierry Reding89184652014-04-16 09:24:44 +0200893 .attach_dev = tegra_smmu_attach_dev,
894 .detach_dev = tegra_smmu_detach_dev,
895 .add_device = tegra_smmu_add_device,
896 .remove_device = tegra_smmu_remove_device,
Thierry Reding7f4c9172017-10-12 16:19:16 +0200897 .device_group = tegra_smmu_device_group,
Thierry Reding89184652014-04-16 09:24:44 +0200898 .map = tegra_smmu_map,
899 .unmap = tegra_smmu_unmap,
Thierry Reding89184652014-04-16 09:24:44 +0200900 .iova_to_phys = tegra_smmu_iova_to_phys,
Thierry Reding7f4c9172017-10-12 16:19:16 +0200901 .of_xlate = tegra_smmu_of_xlate,
Thierry Reding89184652014-04-16 09:24:44 +0200902 .pgsize_bitmap = SZ_4K,
903};
904
905static void tegra_smmu_ahb_enable(void)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200906{
Thierry Reding89184652014-04-16 09:24:44 +0200907 static const struct of_device_id ahb_match[] = {
908 { .compatible = "nvidia,tegra30-ahb", },
909 { }
910 };
911 struct device_node *ahb;
912
913 ahb = of_find_matching_node(NULL, ahb_match);
914 if (ahb) {
915 tegra_ahb_enable_smmu(ahb);
916 of_node_put(ahb);
917 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200918}
919
Thierry Redingd1313e72015-01-23 09:49:25 +0100920static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
921{
922 struct tegra_smmu *smmu = s->private;
923 unsigned int i;
924 u32 value;
925
926 seq_printf(s, "swgroup enabled ASID\n");
927 seq_printf(s, "------------------------\n");
928
929 for (i = 0; i < smmu->soc->num_swgroups; i++) {
930 const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
931 const char *status;
932 unsigned int asid;
933
934 value = smmu_readl(smmu, group->reg);
935
936 if (value & SMMU_ASID_ENABLE)
937 status = "yes";
938 else
939 status = "no";
940
941 asid = value & SMMU_ASID_MASK;
942
943 seq_printf(s, "%-9s %-7s %#04x\n", group->name, status,
944 asid);
945 }
946
947 return 0;
948}
949
Yangtao Li062e52a2018-11-22 08:30:47 -0500950DEFINE_SHOW_ATTRIBUTE(tegra_smmu_swgroups);
Thierry Redingd1313e72015-01-23 09:49:25 +0100951
952static int tegra_smmu_clients_show(struct seq_file *s, void *data)
953{
954 struct tegra_smmu *smmu = s->private;
955 unsigned int i;
956 u32 value;
957
958 seq_printf(s, "client enabled\n");
959 seq_printf(s, "--------------------\n");
960
961 for (i = 0; i < smmu->soc->num_clients; i++) {
962 const struct tegra_mc_client *client = &smmu->soc->clients[i];
963 const char *status;
964
965 value = smmu_readl(smmu, client->smmu.reg);
966
967 if (value & BIT(client->smmu.bit))
968 status = "yes";
969 else
970 status = "no";
971
972 seq_printf(s, "%-12s %s\n", client->name, status);
973 }
974
975 return 0;
976}
977
Yangtao Li062e52a2018-11-22 08:30:47 -0500978DEFINE_SHOW_ATTRIBUTE(tegra_smmu_clients);
Thierry Redingd1313e72015-01-23 09:49:25 +0100979
980static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
981{
982 smmu->debugfs = debugfs_create_dir("smmu", NULL);
983 if (!smmu->debugfs)
984 return;
985
986 debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
987 &tegra_smmu_swgroups_fops);
988 debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
989 &tegra_smmu_clients_fops);
990}
991
992static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
993{
994 debugfs_remove_recursive(smmu->debugfs);
995}
996
Thierry Reding89184652014-04-16 09:24:44 +0200997struct tegra_smmu *tegra_smmu_probe(struct device *dev,
998 const struct tegra_smmu_soc *soc,
999 struct tegra_mc *mc)
1000{
1001 struct tegra_smmu *smmu;
1002 size_t size;
1003 u32 value;
1004 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001005
Thierry Reding89184652014-04-16 09:24:44 +02001006 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1007 if (!smmu)
1008 return ERR_PTR(-ENOMEM);
1009
1010 /*
1011 * This is a bit of a hack. Ideally we'd want to simply return this
1012 * value. However the IOMMU registration process will attempt to add
1013 * all devices to the IOMMU when bus_set_iommu() is called. In order
1014 * not to rely on global variables to track the IOMMU instance, we
1015 * set it here so that it can be looked up from the .add_device()
1016 * callback via the IOMMU device's .drvdata field.
1017 */
1018 mc->smmu = smmu;
1019
1020 size = BITS_TO_LONGS(soc->num_asids) * sizeof(long);
1021
1022 smmu->asids = devm_kzalloc(dev, size, GFP_KERNEL);
1023 if (!smmu->asids)
1024 return ERR_PTR(-ENOMEM);
1025
Thierry Reding7f4c9172017-10-12 16:19:16 +02001026 INIT_LIST_HEAD(&smmu->groups);
Thierry Reding89184652014-04-16 09:24:44 +02001027 mutex_init(&smmu->lock);
1028
1029 smmu->regs = mc->regs;
1030 smmu->soc = soc;
1031 smmu->dev = dev;
1032 smmu->mc = mc;
1033
Thierry Reding804cb542015-03-27 11:07:27 +01001034 smmu->pfn_mask = BIT_MASK(mc->soc->num_address_bits - PAGE_SHIFT) - 1;
1035 dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
1036 mc->soc->num_address_bits, smmu->pfn_mask);
Thierry Reding11cec152015-08-06 14:20:31 +02001037 smmu->tlb_mask = (smmu->soc->num_tlb_lines << 1) - 1;
1038 dev_dbg(dev, "TLB lines: %u, mask: %#lx\n", smmu->soc->num_tlb_lines,
1039 smmu->tlb_mask);
Thierry Reding804cb542015-03-27 11:07:27 +01001040
Thierry Reding89184652014-04-16 09:24:44 +02001041 value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
1042
1043 if (soc->supports_request_limit)
1044 value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
1045
1046 smmu_writel(smmu, value, SMMU_PTC_CONFIG);
1047
1048 value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
Thierry Reding11cec152015-08-06 14:20:31 +02001049 SMMU_TLB_CONFIG_ACTIVE_LINES(smmu);
Thierry Reding89184652014-04-16 09:24:44 +02001050
1051 if (soc->supports_round_robin_arbitration)
1052 value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
1053
1054 smmu_writel(smmu, value, SMMU_TLB_CONFIG);
1055
Russell Kingb8fe0382015-07-27 13:29:41 +01001056 smmu_flush_ptc_all(smmu);
Thierry Reding89184652014-04-16 09:24:44 +02001057 smmu_flush_tlb(smmu);
1058 smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
1059 smmu_flush(smmu);
1060
1061 tegra_smmu_ahb_enable();
1062
Joerg Roedel0b480e42017-08-09 17:41:52 +02001063 err = iommu_device_sysfs_add(&smmu->iommu, dev, NULL, dev_name(dev));
1064 if (err)
1065 return ERR_PTR(err);
1066
1067 iommu_device_set_ops(&smmu->iommu, &tegra_smmu_ops);
Thierry Reding7f4c9172017-10-12 16:19:16 +02001068 iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
Joerg Roedel0b480e42017-08-09 17:41:52 +02001069
1070 err = iommu_device_register(&smmu->iommu);
1071 if (err) {
1072 iommu_device_sysfs_remove(&smmu->iommu);
1073 return ERR_PTR(err);
1074 }
1075
Joerg Roedel96302d82017-08-30 15:06:43 +02001076 err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
1077 if (err < 0) {
1078 iommu_device_unregister(&smmu->iommu);
1079 iommu_device_sysfs_remove(&smmu->iommu);
1080 return ERR_PTR(err);
1081 }
1082
Thierry Redingd1313e72015-01-23 09:49:25 +01001083 if (IS_ENABLED(CONFIG_DEBUG_FS))
1084 tegra_smmu_debugfs_init(smmu);
1085
Thierry Reding89184652014-04-16 09:24:44 +02001086 return smmu;
1087}
Thierry Redingd1313e72015-01-23 09:49:25 +01001088
1089void tegra_smmu_remove(struct tegra_smmu *smmu)
1090{
Joerg Roedel0b480e42017-08-09 17:41:52 +02001091 iommu_device_unregister(&smmu->iommu);
1092 iommu_device_sysfs_remove(&smmu->iommu);
1093
Thierry Redingd1313e72015-01-23 09:49:25 +01001094 if (IS_ENABLED(CONFIG_DEBUG_FS))
1095 tegra_smmu_debugfs_exit(smmu);
1096}