blob: 8d30653cd13a77056c05337a1e47dc9634e6b779 [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)
148#define SMMU_PTE_ATTR (SMMU_PTE_READABLE | SMMU_PTE_WRITABLE | \
149 SMMU_PTE_NONSECURE)
150
Russell King34d35f82015-07-27 13:29:16 +0100151static unsigned int iova_pd_index(unsigned long iova)
152{
153 return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1);
154}
155
156static unsigned int iova_pt_index(unsigned long iova)
157{
158 return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1);
159}
160
Russell Kinge3c97192015-07-27 13:29:52 +0100161static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr)
Russell King4b3c7d12015-07-27 13:29:36 +0100162{
Russell Kinge3c97192015-07-27 13:29:52 +0100163 addr >>= 12;
164 return (addr & smmu->pfn_mask) == addr;
165}
Russell King4b3c7d12015-07-27 13:29:36 +0100166
Russell Kinge3c97192015-07-27 13:29:52 +0100167static dma_addr_t smmu_pde_to_dma(u32 pde)
168{
169 return pde << 12;
Russell King4b3c7d12015-07-27 13:29:36 +0100170}
171
Russell Kingb8fe0382015-07-27 13:29:41 +0100172static void smmu_flush_ptc_all(struct tegra_smmu *smmu)
173{
174 smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
175}
176
Russell Kinge3c97192015-07-27 13:29:52 +0100177static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma,
Thierry Reding89184652014-04-16 09:24:44 +0200178 unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200179{
Thierry Reding89184652014-04-16 09:24:44 +0200180 u32 value;
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200181
Russell Kingb8fe0382015-07-27 13:29:41 +0100182 offset &= ~(smmu->mc->soc->atom_size - 1);
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200183
Russell Kingb8fe0382015-07-27 13:29:41 +0100184 if (smmu->mc->soc->num_address_bits > 32) {
Russell Kinge3c97192015-07-27 13:29:52 +0100185#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
186 value = (dma >> 32) & SMMU_PTC_FLUSH_HI_MASK;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200187#else
Russell Kingb8fe0382015-07-27 13:29:41 +0100188 value = 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200189#endif
Russell Kingb8fe0382015-07-27 13:29:41 +0100190 smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200191 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300192
Russell Kinge3c97192015-07-27 13:29:52 +0100193 value = (dma + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
Thierry Reding89184652014-04-16 09:24:44 +0200194 smmu_writel(smmu, value, SMMU_PTC_FLUSH);
195}
196
197static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
198{
199 smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
200}
201
202static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
203 unsigned long asid)
204{
205 u32 value;
206
Dmitry Osipenko43a05412019-03-07 01:50:07 +0300207 if (smmu->soc->num_asids == 4)
208 value = (asid & 0x3) << 29;
209 else
210 value = (asid & 0x7f) << 24;
211
212 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_MATCH_ALL;
Thierry Reding89184652014-04-16 09:24:44 +0200213 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
214}
215
216static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
217 unsigned long asid,
218 unsigned long iova)
219{
220 u32 value;
221
Dmitry Osipenko43a05412019-03-07 01:50:07 +0300222 if (smmu->soc->num_asids == 4)
223 value = (asid & 0x3) << 29;
224 else
225 value = (asid & 0x7f) << 24;
226
227 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_SECTION(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200228 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
229}
230
231static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu,
232 unsigned long asid,
233 unsigned long iova)
234{
235 u32 value;
236
Dmitry Osipenko43a05412019-03-07 01:50:07 +0300237 if (smmu->soc->num_asids == 4)
238 value = (asid & 0x3) << 29;
239 else
240 value = (asid & 0x7f) << 24;
241
242 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_GROUP(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200243 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
244}
245
246static inline void smmu_flush(struct tegra_smmu *smmu)
247{
248 smmu_readl(smmu, SMMU_CONFIG);
249}
250
251static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
252{
253 unsigned long id;
254
255 mutex_lock(&smmu->lock);
256
257 id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
258 if (id >= smmu->soc->num_asids) {
259 mutex_unlock(&smmu->lock);
260 return -ENOSPC;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200261 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300262
Thierry Reding89184652014-04-16 09:24:44 +0200263 set_bit(id, smmu->asids);
264 *idp = id;
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300265
Thierry Reding89184652014-04-16 09:24:44 +0200266 mutex_unlock(&smmu->lock);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200267 return 0;
268}
269
Thierry Reding89184652014-04-16 09:24:44 +0200270static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200271{
Thierry Reding89184652014-04-16 09:24:44 +0200272 mutex_lock(&smmu->lock);
273 clear_bit(id, smmu->asids);
274 mutex_unlock(&smmu->lock);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200275}
276
Thierry Reding89184652014-04-16 09:24:44 +0200277static bool tegra_smmu_capable(enum iommu_cap cap)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200278{
Joerg Roedel7c2aa642014-09-05 10:51:37 +0200279 return false;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200280}
281
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100282static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200283{
Thierry Reding89184652014-04-16 09:24:44 +0200284 struct tegra_smmu_as *as;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200285
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100286 if (type != IOMMU_DOMAIN_UNMANAGED)
287 return NULL;
288
Thierry Reding89184652014-04-16 09:24:44 +0200289 as = kzalloc(sizeof(*as), GFP_KERNEL);
290 if (!as)
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100291 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200292
Thierry Reding89184652014-04-16 09:24:44 +0200293 as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200294
Russell King707917c2015-07-27 13:30:02 +0100295 as->pd = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
Thierry Reding89184652014-04-16 09:24:44 +0200296 if (!as->pd) {
297 kfree(as);
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100298 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200299 }
300
Russell King32924c72015-07-27 13:29:31 +0100301 as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL);
Thierry Reding89184652014-04-16 09:24:44 +0200302 if (!as->count) {
303 __free_page(as->pd);
304 kfree(as);
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100305 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200306 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200307
Russell King853520f2015-07-27 13:29:26 +0100308 as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL);
309 if (!as->pts) {
Russell King32924c72015-07-27 13:29:31 +0100310 kfree(as->count);
Russell King853520f2015-07-27 13:29:26 +0100311 __free_page(as->pd);
312 kfree(as);
313 return NULL;
314 }
315
Thierry Reding471d9142015-03-27 11:07:25 +0100316 /* setup aperture */
Joerg Roedel7f65ef02015-04-02 13:33:19 +0200317 as->domain.geometry.aperture_start = 0;
318 as->domain.geometry.aperture_end = 0xffffffff;
319 as->domain.geometry.force_aperture = true;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200320
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100321 return &as->domain;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200322}
323
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100324static void tegra_smmu_domain_free(struct iommu_domain *domain)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200325{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100326 struct tegra_smmu_as *as = to_smmu_as(domain);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200327
Thierry Reding89184652014-04-16 09:24:44 +0200328 /* TODO: free page directory and page tables */
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200329
Thierry Reding89184652014-04-16 09:24:44 +0200330 kfree(as);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200331}
332
Thierry Reding89184652014-04-16 09:24:44 +0200333static const struct tegra_smmu_swgroup *
334tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300335{
Thierry Reding89184652014-04-16 09:24:44 +0200336 const struct tegra_smmu_swgroup *group = NULL;
337 unsigned int i;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300338
Thierry Reding89184652014-04-16 09:24:44 +0200339 for (i = 0; i < smmu->soc->num_swgroups; i++) {
340 if (smmu->soc->swgroups[i].swgroup == swgroup) {
341 group = &smmu->soc->swgroups[i];
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300342 break;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300343 }
344 }
345
Thierry Reding89184652014-04-16 09:24:44 +0200346 return group;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300347}
348
Thierry Reding89184652014-04-16 09:24:44 +0200349static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
350 unsigned int asid)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200351{
Thierry Reding89184652014-04-16 09:24:44 +0200352 const struct tegra_smmu_swgroup *group;
353 unsigned int i;
354 u32 value;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200355
Thierry Reding89184652014-04-16 09:24:44 +0200356 for (i = 0; i < smmu->soc->num_clients; i++) {
357 const struct tegra_mc_client *client = &smmu->soc->clients[i];
358
359 if (client->swgroup != swgroup)
360 continue;
361
362 value = smmu_readl(smmu, client->smmu.reg);
363 value |= BIT(client->smmu.bit);
364 smmu_writel(smmu, value, client->smmu.reg);
365 }
366
367 group = tegra_smmu_find_swgroup(smmu, swgroup);
368 if (group) {
369 value = smmu_readl(smmu, group->reg);
370 value &= ~SMMU_ASID_MASK;
371 value |= SMMU_ASID_VALUE(asid);
372 value |= SMMU_ASID_ENABLE;
373 smmu_writel(smmu, value, group->reg);
374 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200375}
376
Thierry Reding89184652014-04-16 09:24:44 +0200377static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
378 unsigned int asid)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200379{
Thierry Reding89184652014-04-16 09:24:44 +0200380 const struct tegra_smmu_swgroup *group;
381 unsigned int i;
382 u32 value;
383
384 group = tegra_smmu_find_swgroup(smmu, swgroup);
385 if (group) {
386 value = smmu_readl(smmu, group->reg);
387 value &= ~SMMU_ASID_MASK;
388 value |= SMMU_ASID_VALUE(asid);
389 value &= ~SMMU_ASID_ENABLE;
390 smmu_writel(smmu, value, group->reg);
391 }
392
393 for (i = 0; i < smmu->soc->num_clients; i++) {
394 const struct tegra_mc_client *client = &smmu->soc->clients[i];
395
396 if (client->swgroup != swgroup)
397 continue;
398
399 value = smmu_readl(smmu, client->smmu.reg);
400 value &= ~BIT(client->smmu.bit);
401 smmu_writel(smmu, value, client->smmu.reg);
402 }
403}
404
405static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
406 struct tegra_smmu_as *as)
407{
408 u32 value;
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300409 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200410
Thierry Reding89184652014-04-16 09:24:44 +0200411 if (as->use_count > 0) {
412 as->use_count++;
413 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200414 }
415
Russell Kinge3c97192015-07-27 13:29:52 +0100416 as->pd_dma = dma_map_page(smmu->dev, as->pd, 0, SMMU_SIZE_PD,
417 DMA_TO_DEVICE);
418 if (dma_mapping_error(smmu->dev, as->pd_dma))
419 return -ENOMEM;
420
421 /* We can't handle 64-bit DMA addresses */
422 if (!smmu_dma_addr_valid(smmu, as->pd_dma)) {
423 err = -ENOMEM;
424 goto err_unmap;
425 }
426
Thierry Reding89184652014-04-16 09:24:44 +0200427 err = tegra_smmu_alloc_asid(smmu, &as->id);
428 if (err < 0)
Russell Kinge3c97192015-07-27 13:29:52 +0100429 goto err_unmap;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200430
Russell Kinge3c97192015-07-27 13:29:52 +0100431 smmu_flush_ptc(smmu, as->pd_dma, 0);
Thierry Reding89184652014-04-16 09:24:44 +0200432 smmu_flush_tlb_asid(smmu, as->id);
433
434 smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
Russell Kinge3c97192015-07-27 13:29:52 +0100435 value = SMMU_PTB_DATA_VALUE(as->pd_dma, as->attr);
Thierry Reding89184652014-04-16 09:24:44 +0200436 smmu_writel(smmu, value, SMMU_PTB_DATA);
437 smmu_flush(smmu);
438
439 as->smmu = smmu;
440 as->use_count++;
441
442 return 0;
Russell Kinge3c97192015-07-27 13:29:52 +0100443
444err_unmap:
445 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
446 return err;
Thierry Reding89184652014-04-16 09:24:44 +0200447}
448
449static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
450 struct tegra_smmu_as *as)
451{
452 if (--as->use_count > 0)
453 return;
454
455 tegra_smmu_free_asid(smmu, as->id);
Russell Kinge3c97192015-07-27 13:29:52 +0100456
457 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
458
Thierry Reding89184652014-04-16 09:24:44 +0200459 as->smmu = NULL;
460}
461
462static int tegra_smmu_attach_dev(struct iommu_domain *domain,
463 struct device *dev)
464{
465 struct tegra_smmu *smmu = dev->archdata.iommu;
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100466 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200467 struct device_node *np = dev->of_node;
468 struct of_phandle_args args;
469 unsigned int index = 0;
470 int err = 0;
471
472 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
473 &args)) {
474 unsigned int swgroup = args.args[0];
475
476 if (args.np != smmu->dev->of_node) {
477 of_node_put(args.np);
478 continue;
479 }
480
481 of_node_put(args.np);
482
483 err = tegra_smmu_as_prepare(smmu, as);
484 if (err < 0)
485 return err;
486
487 tegra_smmu_enable(smmu, swgroup, as->id);
488 index++;
489 }
490
491 if (index == 0)
492 return -ENODEV;
493
494 return 0;
495}
496
497static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
498{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100499 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200500 struct device_node *np = dev->of_node;
501 struct tegra_smmu *smmu = as->smmu;
502 struct of_phandle_args args;
503 unsigned int index = 0;
504
505 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
506 &args)) {
507 unsigned int swgroup = args.args[0];
508
509 if (args.np != smmu->dev->of_node) {
510 of_node_put(args.np);
511 continue;
512 }
513
514 of_node_put(args.np);
515
516 tegra_smmu_disable(smmu, swgroup, as->id);
517 tegra_smmu_as_unprepare(smmu, as);
518 index++;
519 }
520}
521
Russell King4080e992015-07-27 13:30:12 +0100522static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova,
523 u32 value)
524{
525 unsigned int pd_index = iova_pd_index(iova);
526 struct tegra_smmu *smmu = as->smmu;
527 u32 *pd = page_address(as->pd);
528 unsigned long offset = pd_index * sizeof(*pd);
529
530 /* Set the page directory entry first */
531 pd[pd_index] = value;
532
533 /* The flush the page directory entry from caches */
534 dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset,
535 sizeof(*pd), DMA_TO_DEVICE);
536
537 /* And flush the iommu */
538 smmu_flush_ptc(smmu, as->pd_dma, offset);
539 smmu_flush_tlb_section(smmu, as->id, iova);
540 smmu_flush(smmu);
541}
542
Russell King0b42c7c2015-07-27 13:29:21 +0100543static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova)
544{
545 u32 *pt = page_address(pt_page);
546
547 return pt + iova_pt_index(iova);
548}
549
550static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100551 dma_addr_t *dmap)
Russell King0b42c7c2015-07-27 13:29:21 +0100552{
553 unsigned int pd_index = iova_pd_index(iova);
554 struct page *pt_page;
Russell Kinge3c97192015-07-27 13:29:52 +0100555 u32 *pd;
Russell King0b42c7c2015-07-27 13:29:21 +0100556
Russell King853520f2015-07-27 13:29:26 +0100557 pt_page = as->pts[pd_index];
558 if (!pt_page)
Russell King0b42c7c2015-07-27 13:29:21 +0100559 return NULL;
560
Russell Kinge3c97192015-07-27 13:29:52 +0100561 pd = page_address(as->pd);
562 *dmap = smmu_pde_to_dma(pd[pd_index]);
Russell King0b42c7c2015-07-27 13:29:21 +0100563
564 return tegra_smmu_pte_offset(pt_page, iova);
565}
566
Thierry Reding89184652014-04-16 09:24:44 +0200567static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100568 dma_addr_t *dmap)
Thierry Reding89184652014-04-16 09:24:44 +0200569{
Russell King34d35f82015-07-27 13:29:16 +0100570 unsigned int pde = iova_pd_index(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200571 struct tegra_smmu *smmu = as->smmu;
Thierry Reding89184652014-04-16 09:24:44 +0200572
Russell King853520f2015-07-27 13:29:26 +0100573 if (!as->pts[pde]) {
Russell Kinge3c97192015-07-27 13:29:52 +0100574 struct page *page;
575 dma_addr_t dma;
576
Russell King707917c2015-07-27 13:30:02 +0100577 page = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
Thierry Reding89184652014-04-16 09:24:44 +0200578 if (!page)
579 return NULL;
580
Russell Kinge3c97192015-07-27 13:29:52 +0100581 dma = dma_map_page(smmu->dev, page, 0, SMMU_SIZE_PT,
582 DMA_TO_DEVICE);
583 if (dma_mapping_error(smmu->dev, dma)) {
584 __free_page(page);
585 return NULL;
586 }
587
588 if (!smmu_dma_addr_valid(smmu, dma)) {
589 dma_unmap_page(smmu->dev, dma, SMMU_SIZE_PT,
590 DMA_TO_DEVICE);
591 __free_page(page);
592 return NULL;
593 }
594
Russell King853520f2015-07-27 13:29:26 +0100595 as->pts[pde] = page;
596
Russell King4080e992015-07-27 13:30:12 +0100597 tegra_smmu_set_pde(as, iova, SMMU_MK_PDE(dma, SMMU_PDE_ATTR |
598 SMMU_PDE_NEXT));
Russell Kinge3c97192015-07-27 13:29:52 +0100599
600 *dmap = dma;
Thierry Reding89184652014-04-16 09:24:44 +0200601 } else {
Russell King4080e992015-07-27 13:30:12 +0100602 u32 *pd = page_address(as->pd);
603
Russell Kinge3c97192015-07-27 13:29:52 +0100604 *dmap = smmu_pde_to_dma(pd[pde]);
Thierry Reding89184652014-04-16 09:24:44 +0200605 }
606
Russell King7ffc6f02015-08-06 14:56:39 +0200607 return tegra_smmu_pte_offset(as->pts[pde], iova);
608}
Russell King0b42c7c2015-07-27 13:29:21 +0100609
Russell King7ffc6f02015-08-06 14:56:39 +0200610static void tegra_smmu_pte_get_use(struct tegra_smmu_as *as, unsigned long iova)
611{
612 unsigned int pd_index = iova_pd_index(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200613
Russell King7ffc6f02015-08-06 14:56:39 +0200614 as->count[pd_index]++;
Thierry Reding89184652014-04-16 09:24:44 +0200615}
616
Russell Kingb98e34f2015-07-27 13:29:05 +0100617static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
Thierry Reding89184652014-04-16 09:24:44 +0200618{
Russell King34d35f82015-07-27 13:29:16 +0100619 unsigned int pde = iova_pd_index(iova);
Russell King853520f2015-07-27 13:29:26 +0100620 struct page *page = as->pts[pde];
Thierry Reding89184652014-04-16 09:24:44 +0200621
622 /*
623 * When no entries in this page table are used anymore, return the
624 * memory page to the system.
625 */
Russell King32924c72015-07-27 13:29:31 +0100626 if (--as->count[pde] == 0) {
Russell King4080e992015-07-27 13:30:12 +0100627 struct tegra_smmu *smmu = as->smmu;
628 u32 *pd = page_address(as->pd);
Russell Kinge3c97192015-07-27 13:29:52 +0100629 dma_addr_t pte_dma = smmu_pde_to_dma(pd[pde]);
Thierry Reding89184652014-04-16 09:24:44 +0200630
Russell King4080e992015-07-27 13:30:12 +0100631 tegra_smmu_set_pde(as, iova, 0);
Russell Kingb98e34f2015-07-27 13:29:05 +0100632
Russell Kinge3c97192015-07-27 13:29:52 +0100633 dma_unmap_page(smmu->dev, pte_dma, SMMU_SIZE_PT, DMA_TO_DEVICE);
Russell Kingb98e34f2015-07-27 13:29:05 +0100634 __free_page(page);
Russell King853520f2015-07-27 13:29:26 +0100635 as->pts[pde] = NULL;
Thierry Reding89184652014-04-16 09:24:44 +0200636 }
637}
638
Russell King8482ee52015-07-27 13:29:10 +0100639static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100640 u32 *pte, dma_addr_t pte_dma, u32 val)
Russell King8482ee52015-07-27 13:29:10 +0100641{
642 struct tegra_smmu *smmu = as->smmu;
643 unsigned long offset = offset_in_page(pte);
644
645 *pte = val;
646
Russell Kinge3c97192015-07-27 13:29:52 +0100647 dma_sync_single_range_for_device(smmu->dev, pte_dma, offset,
648 4, DMA_TO_DEVICE);
649 smmu_flush_ptc(smmu, pte_dma, offset);
Russell King8482ee52015-07-27 13:29:10 +0100650 smmu_flush_tlb_group(smmu, as->id, iova);
651 smmu_flush(smmu);
652}
653
Thierry Reding89184652014-04-16 09:24:44 +0200654static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
655 phys_addr_t paddr, size_t size, int prot)
656{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100657 struct tegra_smmu_as *as = to_smmu_as(domain);
Russell Kinge3c97192015-07-27 13:29:52 +0100658 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200659 u32 *pte;
660
Russell Kinge3c97192015-07-27 13:29:52 +0100661 pte = as_get_pte(as, iova, &pte_dma);
Thierry Reding89184652014-04-16 09:24:44 +0200662 if (!pte)
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +0300663 return -ENOMEM;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200664
Russell King7ffc6f02015-08-06 14:56:39 +0200665 /* If we aren't overwriting a pre-existing entry, increment use */
666 if (*pte == 0)
667 tegra_smmu_pte_get_use(as, iova);
668
Russell Kinge3c97192015-07-27 13:29:52 +0100669 tegra_smmu_set_pte(as, iova, pte, pte_dma,
Russell King8482ee52015-07-27 13:29:10 +0100670 __phys_to_pfn(paddr) | SMMU_PTE_ATTR);
Thierry Reding89184652014-04-16 09:24:44 +0200671
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200672 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200673}
674
Thierry Reding89184652014-04-16 09:24:44 +0200675static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
676 size_t size)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200677{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100678 struct tegra_smmu_as *as = to_smmu_as(domain);
Russell Kinge3c97192015-07-27 13:29:52 +0100679 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200680 u32 *pte;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200681
Russell Kinge3c97192015-07-27 13:29:52 +0100682 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
Russell Kingb98e34f2015-07-27 13:29:05 +0100683 if (!pte || !*pte)
Thierry Reding89184652014-04-16 09:24:44 +0200684 return 0;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300685
Russell Kinge3c97192015-07-27 13:29:52 +0100686 tegra_smmu_set_pte(as, iova, pte, pte_dma, 0);
Russell Kingb98e34f2015-07-27 13:29:05 +0100687 tegra_smmu_pte_put_use(as, iova);
688
Thierry Reding89184652014-04-16 09:24:44 +0200689 return size;
690}
691
692static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
693 dma_addr_t iova)
694{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100695 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200696 unsigned long pfn;
Russell Kinge3c97192015-07-27 13:29:52 +0100697 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200698 u32 *pte;
699
Russell Kinge3c97192015-07-27 13:29:52 +0100700 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
Russell King91137852015-07-27 13:29:00 +0100701 if (!pte || !*pte)
702 return 0;
703
Thierry Reding804cb542015-03-27 11:07:27 +0100704 pfn = *pte & as->smmu->pfn_mask;
Thierry Reding89184652014-04-16 09:24:44 +0200705
706 return PFN_PHYS(pfn);
707}
708
709static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
710{
711 struct platform_device *pdev;
712 struct tegra_mc *mc;
713
714 pdev = of_find_device_by_node(np);
715 if (!pdev)
716 return NULL;
717
718 mc = platform_get_drvdata(pdev);
719 if (!mc)
720 return NULL;
721
722 return mc->smmu;
723}
724
Thierry Reding7f4c9172017-10-12 16:19:16 +0200725static int tegra_smmu_configure(struct tegra_smmu *smmu, struct device *dev,
726 struct of_phandle_args *args)
727{
728 const struct iommu_ops *ops = smmu->iommu.ops;
729 int err;
730
731 err = iommu_fwspec_init(dev, &dev->of_node->fwnode, ops);
732 if (err < 0) {
733 dev_err(dev, "failed to initialize fwspec: %d\n", err);
734 return err;
735 }
736
737 err = ops->of_xlate(dev, args);
738 if (err < 0) {
739 dev_err(dev, "failed to parse SW group ID: %d\n", err);
740 iommu_fwspec_free(dev);
741 return err;
742 }
743
744 return 0;
745}
746
Thierry Reding89184652014-04-16 09:24:44 +0200747static int tegra_smmu_add_device(struct device *dev)
748{
749 struct device_node *np = dev->of_node;
Thierry Reding7f4c9172017-10-12 16:19:16 +0200750 struct tegra_smmu *smmu = NULL;
Robin Murphyd92e1f82017-07-21 13:12:36 +0100751 struct iommu_group *group;
Thierry Reding89184652014-04-16 09:24:44 +0200752 struct of_phandle_args args;
753 unsigned int index = 0;
Thierry Reding7f4c9172017-10-12 16:19:16 +0200754 int err;
Thierry Reding89184652014-04-16 09:24:44 +0200755
756 while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
757 &args) == 0) {
Thierry Reding89184652014-04-16 09:24:44 +0200758 smmu = tegra_smmu_find(args.np);
759 if (smmu) {
Thierry Reding7f4c9172017-10-12 16:19:16 +0200760 err = tegra_smmu_configure(smmu, dev, &args);
761 of_node_put(args.np);
762
763 if (err < 0)
764 return err;
765
Thierry Reding89184652014-04-16 09:24:44 +0200766 /*
767 * Only a single IOMMU master interface is currently
768 * supported by the Linux kernel, so abort after the
769 * first match.
770 */
771 dev->archdata.iommu = smmu;
Joerg Roedel0b480e42017-08-09 17:41:52 +0200772
773 iommu_device_link(&smmu->iommu, dev);
774
Thierry Reding89184652014-04-16 09:24:44 +0200775 break;
776 }
777
Thierry Reding7f4c9172017-10-12 16:19:16 +0200778 of_node_put(args.np);
Thierry Reding89184652014-04-16 09:24:44 +0200779 index++;
780 }
781
Thierry Reding7f4c9172017-10-12 16:19:16 +0200782 if (!smmu)
783 return -ENODEV;
784
Robin Murphyd92e1f82017-07-21 13:12:36 +0100785 group = iommu_group_get_for_dev(dev);
786 if (IS_ERR(group))
787 return PTR_ERR(group);
788
789 iommu_group_put(group);
790
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200791 return 0;
792}
793
Thierry Reding89184652014-04-16 09:24:44 +0200794static void tegra_smmu_remove_device(struct device *dev)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200795{
Joerg Roedel0b480e42017-08-09 17:41:52 +0200796 struct tegra_smmu *smmu = dev->archdata.iommu;
797
798 if (smmu)
799 iommu_device_unlink(&smmu->iommu, dev);
800
Thierry Reding89184652014-04-16 09:24:44 +0200801 dev->archdata.iommu = NULL;
Robin Murphyd92e1f82017-07-21 13:12:36 +0100802 iommu_group_remove_device(dev);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200803}
804
Thierry Reding7f4c9172017-10-12 16:19:16 +0200805static const struct tegra_smmu_group_soc *
806tegra_smmu_find_group(struct tegra_smmu *smmu, unsigned int swgroup)
807{
808 unsigned int i, j;
809
810 for (i = 0; i < smmu->soc->num_groups; i++)
811 for (j = 0; j < smmu->soc->groups[i].num_swgroups; j++)
812 if (smmu->soc->groups[i].swgroups[j] == swgroup)
813 return &smmu->soc->groups[i];
814
815 return NULL;
816}
817
818static struct iommu_group *tegra_smmu_group_get(struct tegra_smmu *smmu,
819 unsigned int swgroup)
820{
821 const struct tegra_smmu_group_soc *soc;
822 struct tegra_smmu_group *group;
823
824 soc = tegra_smmu_find_group(smmu, swgroup);
825 if (!soc)
826 return NULL;
827
828 mutex_lock(&smmu->lock);
829
830 list_for_each_entry(group, &smmu->groups, list)
831 if (group->soc == soc) {
832 mutex_unlock(&smmu->lock);
833 return group->group;
834 }
835
836 group = devm_kzalloc(smmu->dev, sizeof(*group), GFP_KERNEL);
837 if (!group) {
838 mutex_unlock(&smmu->lock);
839 return NULL;
840 }
841
842 INIT_LIST_HEAD(&group->list);
843 group->soc = soc;
844
845 group->group = iommu_group_alloc();
Wei Yongjun83476bf2017-12-20 03:06:09 +0000846 if (IS_ERR(group->group)) {
Thierry Reding7f4c9172017-10-12 16:19:16 +0200847 devm_kfree(smmu->dev, group);
848 mutex_unlock(&smmu->lock);
849 return NULL;
850 }
851
852 list_add_tail(&group->list, &smmu->groups);
853 mutex_unlock(&smmu->lock);
854
855 return group->group;
856}
857
858static struct iommu_group *tegra_smmu_device_group(struct device *dev)
859{
Joerg Roedeldb5d6a72018-11-29 14:01:00 +0100860 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Thierry Reding7f4c9172017-10-12 16:19:16 +0200861 struct tegra_smmu *smmu = dev->archdata.iommu;
862 struct iommu_group *group;
863
864 group = tegra_smmu_group_get(smmu, fwspec->ids[0]);
865 if (!group)
866 group = generic_device_group(dev);
867
868 return group;
869}
870
871static int tegra_smmu_of_xlate(struct device *dev,
872 struct of_phandle_args *args)
873{
874 u32 id = args->args[0];
875
876 return iommu_fwspec_add_ids(dev, &id, 1);
877}
878
Thierry Reding89184652014-04-16 09:24:44 +0200879static const struct iommu_ops tegra_smmu_ops = {
880 .capable = tegra_smmu_capable,
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100881 .domain_alloc = tegra_smmu_domain_alloc,
882 .domain_free = tegra_smmu_domain_free,
Thierry Reding89184652014-04-16 09:24:44 +0200883 .attach_dev = tegra_smmu_attach_dev,
884 .detach_dev = tegra_smmu_detach_dev,
885 .add_device = tegra_smmu_add_device,
886 .remove_device = tegra_smmu_remove_device,
Thierry Reding7f4c9172017-10-12 16:19:16 +0200887 .device_group = tegra_smmu_device_group,
Thierry Reding89184652014-04-16 09:24:44 +0200888 .map = tegra_smmu_map,
889 .unmap = tegra_smmu_unmap,
Thierry Reding89184652014-04-16 09:24:44 +0200890 .iova_to_phys = tegra_smmu_iova_to_phys,
Thierry Reding7f4c9172017-10-12 16:19:16 +0200891 .of_xlate = tegra_smmu_of_xlate,
Thierry Reding89184652014-04-16 09:24:44 +0200892 .pgsize_bitmap = SZ_4K,
893};
894
895static void tegra_smmu_ahb_enable(void)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200896{
Thierry Reding89184652014-04-16 09:24:44 +0200897 static const struct of_device_id ahb_match[] = {
898 { .compatible = "nvidia,tegra30-ahb", },
899 { }
900 };
901 struct device_node *ahb;
902
903 ahb = of_find_matching_node(NULL, ahb_match);
904 if (ahb) {
905 tegra_ahb_enable_smmu(ahb);
906 of_node_put(ahb);
907 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200908}
909
Thierry Redingd1313e72015-01-23 09:49:25 +0100910static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
911{
912 struct tegra_smmu *smmu = s->private;
913 unsigned int i;
914 u32 value;
915
916 seq_printf(s, "swgroup enabled ASID\n");
917 seq_printf(s, "------------------------\n");
918
919 for (i = 0; i < smmu->soc->num_swgroups; i++) {
920 const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
921 const char *status;
922 unsigned int asid;
923
924 value = smmu_readl(smmu, group->reg);
925
926 if (value & SMMU_ASID_ENABLE)
927 status = "yes";
928 else
929 status = "no";
930
931 asid = value & SMMU_ASID_MASK;
932
933 seq_printf(s, "%-9s %-7s %#04x\n", group->name, status,
934 asid);
935 }
936
937 return 0;
938}
939
Yangtao Li062e52a2018-11-22 08:30:47 -0500940DEFINE_SHOW_ATTRIBUTE(tegra_smmu_swgroups);
Thierry Redingd1313e72015-01-23 09:49:25 +0100941
942static int tegra_smmu_clients_show(struct seq_file *s, void *data)
943{
944 struct tegra_smmu *smmu = s->private;
945 unsigned int i;
946 u32 value;
947
948 seq_printf(s, "client enabled\n");
949 seq_printf(s, "--------------------\n");
950
951 for (i = 0; i < smmu->soc->num_clients; i++) {
952 const struct tegra_mc_client *client = &smmu->soc->clients[i];
953 const char *status;
954
955 value = smmu_readl(smmu, client->smmu.reg);
956
957 if (value & BIT(client->smmu.bit))
958 status = "yes";
959 else
960 status = "no";
961
962 seq_printf(s, "%-12s %s\n", client->name, status);
963 }
964
965 return 0;
966}
967
Yangtao Li062e52a2018-11-22 08:30:47 -0500968DEFINE_SHOW_ATTRIBUTE(tegra_smmu_clients);
Thierry Redingd1313e72015-01-23 09:49:25 +0100969
970static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
971{
972 smmu->debugfs = debugfs_create_dir("smmu", NULL);
973 if (!smmu->debugfs)
974 return;
975
976 debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
977 &tegra_smmu_swgroups_fops);
978 debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
979 &tegra_smmu_clients_fops);
980}
981
982static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
983{
984 debugfs_remove_recursive(smmu->debugfs);
985}
986
Thierry Reding89184652014-04-16 09:24:44 +0200987struct tegra_smmu *tegra_smmu_probe(struct device *dev,
988 const struct tegra_smmu_soc *soc,
989 struct tegra_mc *mc)
990{
991 struct tegra_smmu *smmu;
992 size_t size;
993 u32 value;
994 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200995
Thierry Reding89184652014-04-16 09:24:44 +0200996 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
997 if (!smmu)
998 return ERR_PTR(-ENOMEM);
999
1000 /*
1001 * This is a bit of a hack. Ideally we'd want to simply return this
1002 * value. However the IOMMU registration process will attempt to add
1003 * all devices to the IOMMU when bus_set_iommu() is called. In order
1004 * not to rely on global variables to track the IOMMU instance, we
1005 * set it here so that it can be looked up from the .add_device()
1006 * callback via the IOMMU device's .drvdata field.
1007 */
1008 mc->smmu = smmu;
1009
1010 size = BITS_TO_LONGS(soc->num_asids) * sizeof(long);
1011
1012 smmu->asids = devm_kzalloc(dev, size, GFP_KERNEL);
1013 if (!smmu->asids)
1014 return ERR_PTR(-ENOMEM);
1015
Thierry Reding7f4c9172017-10-12 16:19:16 +02001016 INIT_LIST_HEAD(&smmu->groups);
Thierry Reding89184652014-04-16 09:24:44 +02001017 mutex_init(&smmu->lock);
1018
1019 smmu->regs = mc->regs;
1020 smmu->soc = soc;
1021 smmu->dev = dev;
1022 smmu->mc = mc;
1023
Thierry Reding804cb542015-03-27 11:07:27 +01001024 smmu->pfn_mask = BIT_MASK(mc->soc->num_address_bits - PAGE_SHIFT) - 1;
1025 dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
1026 mc->soc->num_address_bits, smmu->pfn_mask);
Thierry Reding11cec152015-08-06 14:20:31 +02001027 smmu->tlb_mask = (smmu->soc->num_tlb_lines << 1) - 1;
1028 dev_dbg(dev, "TLB lines: %u, mask: %#lx\n", smmu->soc->num_tlb_lines,
1029 smmu->tlb_mask);
Thierry Reding804cb542015-03-27 11:07:27 +01001030
Thierry Reding89184652014-04-16 09:24:44 +02001031 value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
1032
1033 if (soc->supports_request_limit)
1034 value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
1035
1036 smmu_writel(smmu, value, SMMU_PTC_CONFIG);
1037
1038 value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
Thierry Reding11cec152015-08-06 14:20:31 +02001039 SMMU_TLB_CONFIG_ACTIVE_LINES(smmu);
Thierry Reding89184652014-04-16 09:24:44 +02001040
1041 if (soc->supports_round_robin_arbitration)
1042 value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
1043
1044 smmu_writel(smmu, value, SMMU_TLB_CONFIG);
1045
Russell Kingb8fe0382015-07-27 13:29:41 +01001046 smmu_flush_ptc_all(smmu);
Thierry Reding89184652014-04-16 09:24:44 +02001047 smmu_flush_tlb(smmu);
1048 smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
1049 smmu_flush(smmu);
1050
1051 tegra_smmu_ahb_enable();
1052
Joerg Roedel0b480e42017-08-09 17:41:52 +02001053 err = iommu_device_sysfs_add(&smmu->iommu, dev, NULL, dev_name(dev));
1054 if (err)
1055 return ERR_PTR(err);
1056
1057 iommu_device_set_ops(&smmu->iommu, &tegra_smmu_ops);
Thierry Reding7f4c9172017-10-12 16:19:16 +02001058 iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
Joerg Roedel0b480e42017-08-09 17:41:52 +02001059
1060 err = iommu_device_register(&smmu->iommu);
1061 if (err) {
1062 iommu_device_sysfs_remove(&smmu->iommu);
1063 return ERR_PTR(err);
1064 }
1065
Joerg Roedel96302d82017-08-30 15:06:43 +02001066 err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
1067 if (err < 0) {
1068 iommu_device_unregister(&smmu->iommu);
1069 iommu_device_sysfs_remove(&smmu->iommu);
1070 return ERR_PTR(err);
1071 }
1072
Thierry Redingd1313e72015-01-23 09:49:25 +01001073 if (IS_ENABLED(CONFIG_DEBUG_FS))
1074 tegra_smmu_debugfs_init(smmu);
1075
Thierry Reding89184652014-04-16 09:24:44 +02001076 return smmu;
1077}
Thierry Redingd1313e72015-01-23 09:49:25 +01001078
1079void tegra_smmu_remove(struct tegra_smmu *smmu)
1080{
Joerg Roedel0b480e42017-08-09 17:41:52 +02001081 iommu_device_unregister(&smmu->iommu);
1082 iommu_device_sysfs_remove(&smmu->iommu);
1083
Thierry Redingd1313e72015-01-23 09:49:25 +01001084 if (IS_ENABLED(CONFIG_DEBUG_FS))
1085 tegra_smmu_debugfs_exit(smmu);
1086}