blob: 04cec050e42bf677b13b5d2d882b5815286b06a5 [file] [log] [blame]
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02001/*
Thierry Reding89184652014-04-16 09:24:44 +02002 * Copyright (C) 2011-2014 NVIDIA CORPORATION. All rights reserved.
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02003 *
Thierry Reding89184652014-04-16 09:24:44 +02004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +02007 */
8
Thierry Reding804cb542015-03-27 11:07:27 +01009#include <linux/bitops.h>
Thierry Redingd1313e72015-01-23 09:49:25 +010010#include <linux/debugfs.h>
Thierry Redingbc5e6de2013-01-21 11:09:06 +010011#include <linux/err.h>
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020012#include <linux/iommu.h>
Thierry Reding89184652014-04-16 09:24:44 +020013#include <linux/kernel.h>
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +030014#include <linux/of.h>
Thierry Reding89184652014-04-16 09:24:44 +020015#include <linux/of_device.h>
16#include <linux/platform_device.h>
17#include <linux/slab.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020018
19#include <soc/tegra/ahb.h>
Thierry Reding89184652014-04-16 09:24:44 +020020#include <soc/tegra/mc.h>
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020021
Thierry Reding89184652014-04-16 09:24:44 +020022struct tegra_smmu {
23 void __iomem *regs;
24 struct device *dev;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020025
Thierry Reding89184652014-04-16 09:24:44 +020026 struct tegra_mc *mc;
27 const struct tegra_smmu_soc *soc;
Stephen Warrene6bc5932012-09-04 16:36:15 -060028
Thierry Reding804cb542015-03-27 11:07:27 +010029 unsigned long pfn_mask;
Thierry Reding11cec152015-08-06 14:20:31 +020030 unsigned long tlb_mask;
Thierry Reding804cb542015-03-27 11:07:27 +010031
Thierry Reding89184652014-04-16 09:24:44 +020032 unsigned long *asids;
33 struct mutex lock;
Stephen Warrene6bc5932012-09-04 16:36:15 -060034
Thierry Reding89184652014-04-16 09:24:44 +020035 struct list_head list;
Thierry Redingd1313e72015-01-23 09:49:25 +010036
37 struct dentry *debugfs;
Stephen Warrene6bc5932012-09-04 16:36:15 -060038};
39
Thierry Reding89184652014-04-16 09:24:44 +020040struct tegra_smmu_as {
Joerg Roedeld5f1a812015-03-26 13:43:12 +010041 struct iommu_domain domain;
Thierry Reding89184652014-04-16 09:24:44 +020042 struct tegra_smmu *smmu;
43 unsigned int use_count;
Russell King32924c72015-07-27 13:29:31 +010044 u32 *count;
Russell King853520f2015-07-27 13:29:26 +010045 struct page **pts;
Thierry Reding89184652014-04-16 09:24:44 +020046 struct page *pd;
Russell Kinge3c97192015-07-27 13:29:52 +010047 dma_addr_t pd_dma;
Thierry Reding89184652014-04-16 09:24:44 +020048 unsigned id;
49 u32 attr;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +030050};
51
Joerg Roedeld5f1a812015-03-26 13:43:12 +010052static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom)
53{
54 return container_of(dom, struct tegra_smmu_as, domain);
55}
56
Thierry Reding89184652014-04-16 09:24:44 +020057static inline void smmu_writel(struct tegra_smmu *smmu, u32 value,
58 unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020059{
Thierry Reding89184652014-04-16 09:24:44 +020060 writel(value, smmu->regs + offset);
Joerg Roedelfe1229b2013-02-04 20:40:58 +010061}
62
Thierry Reding89184652014-04-16 09:24:44 +020063static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020064{
Thierry Reding89184652014-04-16 09:24:44 +020065 return readl(smmu->regs + offset);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +020066}
67
Thierry Reding89184652014-04-16 09:24:44 +020068#define SMMU_CONFIG 0x010
69#define SMMU_CONFIG_ENABLE (1 << 0)
70
71#define SMMU_TLB_CONFIG 0x14
72#define SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29)
73#define SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28)
Thierry Reding11cec152015-08-06 14:20:31 +020074#define SMMU_TLB_CONFIG_ACTIVE_LINES(smmu) \
75 ((smmu)->soc->num_tlb_lines & (smmu)->tlb_mask)
Thierry Reding89184652014-04-16 09:24:44 +020076
77#define SMMU_PTC_CONFIG 0x18
78#define SMMU_PTC_CONFIG_ENABLE (1 << 29)
79#define SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24)
80#define SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f)
81
82#define SMMU_PTB_ASID 0x01c
83#define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
84
85#define SMMU_PTB_DATA 0x020
Russell Kinge3c97192015-07-27 13:29:52 +010086#define SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr))
Thierry Reding89184652014-04-16 09:24:44 +020087
Russell Kinge3c97192015-07-27 13:29:52 +010088#define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr))
Thierry Reding89184652014-04-16 09:24:44 +020089
90#define SMMU_TLB_FLUSH 0x030
91#define SMMU_TLB_FLUSH_VA_MATCH_ALL (0 << 0)
92#define SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0)
93#define SMMU_TLB_FLUSH_VA_MATCH_GROUP (3 << 0)
Thierry Reding89184652014-04-16 09:24:44 +020094#define SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
95 SMMU_TLB_FLUSH_VA_MATCH_SECTION)
96#define SMMU_TLB_FLUSH_VA_GROUP(addr) ((((addr) & 0xffffc000) >> 12) | \
97 SMMU_TLB_FLUSH_VA_MATCH_GROUP)
98#define SMMU_TLB_FLUSH_ASID_MATCH (1 << 31)
99
100#define SMMU_PTC_FLUSH 0x034
101#define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
102#define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
103
104#define SMMU_PTC_FLUSH_HI 0x9b8
105#define SMMU_PTC_FLUSH_HI_MASK 0x3
106
107/* per-SWGROUP SMMU_*_ASID register */
108#define SMMU_ASID_ENABLE (1 << 31)
109#define SMMU_ASID_MASK 0x7f
110#define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
111
112/* page table definitions */
113#define SMMU_NUM_PDE 1024
114#define SMMU_NUM_PTE 1024
115
116#define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
117#define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
118
119#define SMMU_PDE_SHIFT 22
120#define SMMU_PTE_SHIFT 12
121
Thierry Reding89184652014-04-16 09:24:44 +0200122#define SMMU_PD_READABLE (1 << 31)
123#define SMMU_PD_WRITABLE (1 << 30)
124#define SMMU_PD_NONSECURE (1 << 29)
125
126#define SMMU_PDE_READABLE (1 << 31)
127#define SMMU_PDE_WRITABLE (1 << 30)
128#define SMMU_PDE_NONSECURE (1 << 29)
129#define SMMU_PDE_NEXT (1 << 28)
130
131#define SMMU_PTE_READABLE (1 << 31)
132#define SMMU_PTE_WRITABLE (1 << 30)
133#define SMMU_PTE_NONSECURE (1 << 29)
134
135#define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
136 SMMU_PDE_NONSECURE)
137#define SMMU_PTE_ATTR (SMMU_PTE_READABLE | SMMU_PTE_WRITABLE | \
138 SMMU_PTE_NONSECURE)
139
Russell King34d35f82015-07-27 13:29:16 +0100140static unsigned int iova_pd_index(unsigned long iova)
141{
142 return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1);
143}
144
145static unsigned int iova_pt_index(unsigned long iova)
146{
147 return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1);
148}
149
Russell Kinge3c97192015-07-27 13:29:52 +0100150static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr)
Russell King4b3c7d12015-07-27 13:29:36 +0100151{
Russell Kinge3c97192015-07-27 13:29:52 +0100152 addr >>= 12;
153 return (addr & smmu->pfn_mask) == addr;
154}
Russell King4b3c7d12015-07-27 13:29:36 +0100155
Thierry Redinge5fb4e72019-10-16 13:50:26 +0200156static dma_addr_t smmu_pde_to_dma(struct tegra_smmu *smmu, u32 pde)
Russell Kinge3c97192015-07-27 13:29:52 +0100157{
Thierry Redinge5fb4e72019-10-16 13:50:26 +0200158 return (dma_addr_t)(pde & smmu->pfn_mask) << 12;
Russell King4b3c7d12015-07-27 13:29:36 +0100159}
160
Russell Kingb8fe0382015-07-27 13:29:41 +0100161static void smmu_flush_ptc_all(struct tegra_smmu *smmu)
162{
163 smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
164}
165
Russell Kinge3c97192015-07-27 13:29:52 +0100166static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma,
Thierry Reding89184652014-04-16 09:24:44 +0200167 unsigned long offset)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200168{
Thierry Reding89184652014-04-16 09:24:44 +0200169 u32 value;
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200170
Russell Kingb8fe0382015-07-27 13:29:41 +0100171 offset &= ~(smmu->mc->soc->atom_size - 1);
Hiroshi Doyua6870e92013-01-31 10:14:10 +0200172
Russell Kingb8fe0382015-07-27 13:29:41 +0100173 if (smmu->mc->soc->num_address_bits > 32) {
Russell Kinge3c97192015-07-27 13:29:52 +0100174#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
175 value = (dma >> 32) & SMMU_PTC_FLUSH_HI_MASK;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200176#else
Russell Kingb8fe0382015-07-27 13:29:41 +0100177 value = 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200178#endif
Russell Kingb8fe0382015-07-27 13:29:41 +0100179 smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200180 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300181
Russell Kinge3c97192015-07-27 13:29:52 +0100182 value = (dma + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
Thierry Reding89184652014-04-16 09:24:44 +0200183 smmu_writel(smmu, value, SMMU_PTC_FLUSH);
184}
185
186static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
187{
188 smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
189}
190
191static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
192 unsigned long asid)
193{
194 u32 value;
195
Dmitry Osipenko202494a92019-03-07 01:50:07 +0300196 if (smmu->soc->num_asids == 4)
197 value = (asid & 0x3) << 29;
198 else
199 value = (asid & 0x7f) << 24;
200
201 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_MATCH_ALL;
Thierry Reding89184652014-04-16 09:24:44 +0200202 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
203}
204
205static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
206 unsigned long asid,
207 unsigned long iova)
208{
209 u32 value;
210
Dmitry Osipenko202494a92019-03-07 01:50:07 +0300211 if (smmu->soc->num_asids == 4)
212 value = (asid & 0x3) << 29;
213 else
214 value = (asid & 0x7f) << 24;
215
216 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_SECTION(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200217 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
218}
219
220static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu,
221 unsigned long asid,
222 unsigned long iova)
223{
224 u32 value;
225
Dmitry Osipenko202494a92019-03-07 01:50:07 +0300226 if (smmu->soc->num_asids == 4)
227 value = (asid & 0x3) << 29;
228 else
229 value = (asid & 0x7f) << 24;
230
231 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_GROUP(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200232 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
233}
234
235static inline void smmu_flush(struct tegra_smmu *smmu)
236{
237 smmu_readl(smmu, SMMU_CONFIG);
238}
239
240static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
241{
242 unsigned long id;
243
244 mutex_lock(&smmu->lock);
245
246 id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
247 if (id >= smmu->soc->num_asids) {
248 mutex_unlock(&smmu->lock);
249 return -ENOSPC;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200250 }
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300251
Thierry Reding89184652014-04-16 09:24:44 +0200252 set_bit(id, smmu->asids);
253 *idp = id;
Hiroshi DOYU9e971a02012-07-02 14:26:38 +0300254
Thierry Reding89184652014-04-16 09:24:44 +0200255 mutex_unlock(&smmu->lock);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200256 return 0;
257}
258
Thierry Reding89184652014-04-16 09:24:44 +0200259static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200260{
Thierry Reding89184652014-04-16 09:24:44 +0200261 mutex_lock(&smmu->lock);
262 clear_bit(id, smmu->asids);
263 mutex_unlock(&smmu->lock);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200264}
265
Thierry Reding89184652014-04-16 09:24:44 +0200266static bool tegra_smmu_capable(enum iommu_cap cap)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200267{
Joerg Roedel7c2aa642014-09-05 10:51:37 +0200268 return false;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200269}
270
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100271static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200272{
Thierry Reding89184652014-04-16 09:24:44 +0200273 struct tegra_smmu_as *as;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200274
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100275 if (type != IOMMU_DOMAIN_UNMANAGED)
276 return NULL;
277
Thierry Reding89184652014-04-16 09:24:44 +0200278 as = kzalloc(sizeof(*as), GFP_KERNEL);
279 if (!as)
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100280 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200281
Thierry Reding89184652014-04-16 09:24:44 +0200282 as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200283
Russell King707917c2015-07-27 13:30:02 +0100284 as->pd = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
Thierry Reding89184652014-04-16 09:24:44 +0200285 if (!as->pd) {
286 kfree(as);
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100287 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200288 }
289
Russell King32924c72015-07-27 13:29:31 +0100290 as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL);
Thierry Reding89184652014-04-16 09:24:44 +0200291 if (!as->count) {
292 __free_page(as->pd);
293 kfree(as);
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100294 return NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200295 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200296
Russell King853520f2015-07-27 13:29:26 +0100297 as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL);
298 if (!as->pts) {
Russell King32924c72015-07-27 13:29:31 +0100299 kfree(as->count);
Russell King853520f2015-07-27 13:29:26 +0100300 __free_page(as->pd);
301 kfree(as);
302 return NULL;
303 }
304
Thierry Reding471d9142015-03-27 11:07:25 +0100305 /* setup aperture */
Joerg Roedel7f65ef02015-04-02 13:33:19 +0200306 as->domain.geometry.aperture_start = 0;
307 as->domain.geometry.aperture_end = 0xffffffff;
308 as->domain.geometry.force_aperture = true;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200309
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100310 return &as->domain;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200311}
312
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100313static void tegra_smmu_domain_free(struct iommu_domain *domain)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200314{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100315 struct tegra_smmu_as *as = to_smmu_as(domain);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200316
Thierry Reding89184652014-04-16 09:24:44 +0200317 /* TODO: free page directory and page tables */
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200318
Thierry Reding89184652014-04-16 09:24:44 +0200319 kfree(as);
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200320}
321
Thierry Reding89184652014-04-16 09:24:44 +0200322static const struct tegra_smmu_swgroup *
323tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300324{
Thierry Reding89184652014-04-16 09:24:44 +0200325 const struct tegra_smmu_swgroup *group = NULL;
326 unsigned int i;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300327
Thierry Reding89184652014-04-16 09:24:44 +0200328 for (i = 0; i < smmu->soc->num_swgroups; i++) {
329 if (smmu->soc->swgroups[i].swgroup == swgroup) {
330 group = &smmu->soc->swgroups[i];
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300331 break;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300332 }
333 }
334
Thierry Reding89184652014-04-16 09:24:44 +0200335 return group;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300336}
337
Thierry Reding89184652014-04-16 09:24:44 +0200338static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
339 unsigned int asid)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200340{
Thierry Reding89184652014-04-16 09:24:44 +0200341 const struct tegra_smmu_swgroup *group;
342 unsigned int i;
343 u32 value;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200344
Thierry Reding89184652014-04-16 09:24:44 +0200345 for (i = 0; i < smmu->soc->num_clients; i++) {
346 const struct tegra_mc_client *client = &smmu->soc->clients[i];
347
348 if (client->swgroup != swgroup)
349 continue;
350
351 value = smmu_readl(smmu, client->smmu.reg);
352 value |= BIT(client->smmu.bit);
353 smmu_writel(smmu, value, client->smmu.reg);
354 }
355
356 group = tegra_smmu_find_swgroup(smmu, swgroup);
357 if (group) {
358 value = smmu_readl(smmu, group->reg);
359 value &= ~SMMU_ASID_MASK;
360 value |= SMMU_ASID_VALUE(asid);
361 value |= SMMU_ASID_ENABLE;
362 smmu_writel(smmu, value, group->reg);
363 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200364}
365
Thierry Reding89184652014-04-16 09:24:44 +0200366static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
367 unsigned int asid)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200368{
Thierry Reding89184652014-04-16 09:24:44 +0200369 const struct tegra_smmu_swgroup *group;
370 unsigned int i;
371 u32 value;
372
373 group = tegra_smmu_find_swgroup(smmu, swgroup);
374 if (group) {
375 value = smmu_readl(smmu, group->reg);
376 value &= ~SMMU_ASID_MASK;
377 value |= SMMU_ASID_VALUE(asid);
378 value &= ~SMMU_ASID_ENABLE;
379 smmu_writel(smmu, value, group->reg);
380 }
381
382 for (i = 0; i < smmu->soc->num_clients; i++) {
383 const struct tegra_mc_client *client = &smmu->soc->clients[i];
384
385 if (client->swgroup != swgroup)
386 continue;
387
388 value = smmu_readl(smmu, client->smmu.reg);
389 value &= ~BIT(client->smmu.bit);
390 smmu_writel(smmu, value, client->smmu.reg);
391 }
392}
393
394static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
395 struct tegra_smmu_as *as)
396{
397 u32 value;
Hiroshi Doyu0760e8f2012-06-25 14:23:55 +0300398 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200399
Thierry Reding89184652014-04-16 09:24:44 +0200400 if (as->use_count > 0) {
401 as->use_count++;
402 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200403 }
404
Russell Kinge3c97192015-07-27 13:29:52 +0100405 as->pd_dma = dma_map_page(smmu->dev, as->pd, 0, SMMU_SIZE_PD,
406 DMA_TO_DEVICE);
407 if (dma_mapping_error(smmu->dev, as->pd_dma))
408 return -ENOMEM;
409
410 /* We can't handle 64-bit DMA addresses */
411 if (!smmu_dma_addr_valid(smmu, as->pd_dma)) {
412 err = -ENOMEM;
413 goto err_unmap;
414 }
415
Thierry Reding89184652014-04-16 09:24:44 +0200416 err = tegra_smmu_alloc_asid(smmu, &as->id);
417 if (err < 0)
Russell Kinge3c97192015-07-27 13:29:52 +0100418 goto err_unmap;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200419
Russell Kinge3c97192015-07-27 13:29:52 +0100420 smmu_flush_ptc(smmu, as->pd_dma, 0);
Thierry Reding89184652014-04-16 09:24:44 +0200421 smmu_flush_tlb_asid(smmu, as->id);
422
423 smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
Russell Kinge3c97192015-07-27 13:29:52 +0100424 value = SMMU_PTB_DATA_VALUE(as->pd_dma, as->attr);
Thierry Reding89184652014-04-16 09:24:44 +0200425 smmu_writel(smmu, value, SMMU_PTB_DATA);
426 smmu_flush(smmu);
427
428 as->smmu = smmu;
429 as->use_count++;
430
431 return 0;
Russell Kinge3c97192015-07-27 13:29:52 +0100432
433err_unmap:
434 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
435 return err;
Thierry Reding89184652014-04-16 09:24:44 +0200436}
437
438static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
439 struct tegra_smmu_as *as)
440{
441 if (--as->use_count > 0)
442 return;
443
444 tegra_smmu_free_asid(smmu, as->id);
Russell Kinge3c97192015-07-27 13:29:52 +0100445
446 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
447
Thierry Reding89184652014-04-16 09:24:44 +0200448 as->smmu = NULL;
449}
450
451static int tegra_smmu_attach_dev(struct iommu_domain *domain,
452 struct device *dev)
453{
454 struct tegra_smmu *smmu = dev->archdata.iommu;
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100455 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200456 struct device_node *np = dev->of_node;
457 struct of_phandle_args args;
458 unsigned int index = 0;
459 int err = 0;
460
461 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
462 &args)) {
463 unsigned int swgroup = args.args[0];
464
465 if (args.np != smmu->dev->of_node) {
466 of_node_put(args.np);
467 continue;
468 }
469
470 of_node_put(args.np);
471
472 err = tegra_smmu_as_prepare(smmu, as);
473 if (err < 0)
474 return err;
475
476 tegra_smmu_enable(smmu, swgroup, as->id);
477 index++;
478 }
479
480 if (index == 0)
481 return -ENODEV;
482
483 return 0;
484}
485
486static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
487{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100488 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200489 struct device_node *np = dev->of_node;
490 struct tegra_smmu *smmu = as->smmu;
491 struct of_phandle_args args;
492 unsigned int index = 0;
493
494 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
495 &args)) {
496 unsigned int swgroup = args.args[0];
497
498 if (args.np != smmu->dev->of_node) {
499 of_node_put(args.np);
500 continue;
501 }
502
503 of_node_put(args.np);
504
505 tegra_smmu_disable(smmu, swgroup, as->id);
506 tegra_smmu_as_unprepare(smmu, as);
507 index++;
508 }
509}
510
Russell King4080e992015-07-27 13:30:12 +0100511static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova,
512 u32 value)
513{
514 unsigned int pd_index = iova_pd_index(iova);
515 struct tegra_smmu *smmu = as->smmu;
516 u32 *pd = page_address(as->pd);
517 unsigned long offset = pd_index * sizeof(*pd);
518
519 /* Set the page directory entry first */
520 pd[pd_index] = value;
521
522 /* The flush the page directory entry from caches */
523 dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset,
524 sizeof(*pd), DMA_TO_DEVICE);
525
526 /* And flush the iommu */
527 smmu_flush_ptc(smmu, as->pd_dma, offset);
528 smmu_flush_tlb_section(smmu, as->id, iova);
529 smmu_flush(smmu);
530}
531
Russell King0b42c7c2015-07-27 13:29:21 +0100532static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova)
533{
534 u32 *pt = page_address(pt_page);
535
536 return pt + iova_pt_index(iova);
537}
538
539static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100540 dma_addr_t *dmap)
Russell King0b42c7c2015-07-27 13:29:21 +0100541{
542 unsigned int pd_index = iova_pd_index(iova);
Thierry Redinge5fb4e72019-10-16 13:50:26 +0200543 struct tegra_smmu *smmu = as->smmu;
Russell King0b42c7c2015-07-27 13:29:21 +0100544 struct page *pt_page;
Russell Kinge3c97192015-07-27 13:29:52 +0100545 u32 *pd;
Russell King0b42c7c2015-07-27 13:29:21 +0100546
Russell King853520f2015-07-27 13:29:26 +0100547 pt_page = as->pts[pd_index];
548 if (!pt_page)
Russell King0b42c7c2015-07-27 13:29:21 +0100549 return NULL;
550
Russell Kinge3c97192015-07-27 13:29:52 +0100551 pd = page_address(as->pd);
Thierry Redinge5fb4e72019-10-16 13:50:26 +0200552 *dmap = smmu_pde_to_dma(smmu, pd[pd_index]);
Russell King0b42c7c2015-07-27 13:29:21 +0100553
554 return tegra_smmu_pte_offset(pt_page, iova);
555}
556
Thierry Reding89184652014-04-16 09:24:44 +0200557static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100558 dma_addr_t *dmap)
Thierry Reding89184652014-04-16 09:24:44 +0200559{
Russell King34d35f82015-07-27 13:29:16 +0100560 unsigned int pde = iova_pd_index(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200561 struct tegra_smmu *smmu = as->smmu;
Thierry Reding89184652014-04-16 09:24:44 +0200562
Russell King853520f2015-07-27 13:29:26 +0100563 if (!as->pts[pde]) {
Russell Kinge3c97192015-07-27 13:29:52 +0100564 struct page *page;
565 dma_addr_t dma;
566
Russell King707917c2015-07-27 13:30:02 +0100567 page = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
Thierry Reding89184652014-04-16 09:24:44 +0200568 if (!page)
569 return NULL;
570
Russell Kinge3c97192015-07-27 13:29:52 +0100571 dma = dma_map_page(smmu->dev, page, 0, SMMU_SIZE_PT,
572 DMA_TO_DEVICE);
573 if (dma_mapping_error(smmu->dev, dma)) {
574 __free_page(page);
575 return NULL;
576 }
577
578 if (!smmu_dma_addr_valid(smmu, dma)) {
579 dma_unmap_page(smmu->dev, dma, SMMU_SIZE_PT,
580 DMA_TO_DEVICE);
581 __free_page(page);
582 return NULL;
583 }
584
Russell King853520f2015-07-27 13:29:26 +0100585 as->pts[pde] = page;
586
Russell King4080e992015-07-27 13:30:12 +0100587 tegra_smmu_set_pde(as, iova, SMMU_MK_PDE(dma, SMMU_PDE_ATTR |
588 SMMU_PDE_NEXT));
Russell Kinge3c97192015-07-27 13:29:52 +0100589
590 *dmap = dma;
Thierry Reding89184652014-04-16 09:24:44 +0200591 } else {
Russell King4080e992015-07-27 13:30:12 +0100592 u32 *pd = page_address(as->pd);
593
Thierry Redinge5fb4e72019-10-16 13:50:26 +0200594 *dmap = smmu_pde_to_dma(smmu, pd[pde]);
Thierry Reding89184652014-04-16 09:24:44 +0200595 }
596
Russell King7ffc6f02015-08-06 14:56:39 +0200597 return tegra_smmu_pte_offset(as->pts[pde], iova);
598}
Russell King0b42c7c2015-07-27 13:29:21 +0100599
Russell King7ffc6f02015-08-06 14:56:39 +0200600static void tegra_smmu_pte_get_use(struct tegra_smmu_as *as, unsigned long iova)
601{
602 unsigned int pd_index = iova_pd_index(iova);
Thierry Reding89184652014-04-16 09:24:44 +0200603
Russell King7ffc6f02015-08-06 14:56:39 +0200604 as->count[pd_index]++;
Thierry Reding89184652014-04-16 09:24:44 +0200605}
606
Russell Kingb98e34f2015-07-27 13:29:05 +0100607static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
Thierry Reding89184652014-04-16 09:24:44 +0200608{
Russell King34d35f82015-07-27 13:29:16 +0100609 unsigned int pde = iova_pd_index(iova);
Russell King853520f2015-07-27 13:29:26 +0100610 struct page *page = as->pts[pde];
Thierry Reding89184652014-04-16 09:24:44 +0200611
612 /*
613 * When no entries in this page table are used anymore, return the
614 * memory page to the system.
615 */
Russell King32924c72015-07-27 13:29:31 +0100616 if (--as->count[pde] == 0) {
Russell King4080e992015-07-27 13:30:12 +0100617 struct tegra_smmu *smmu = as->smmu;
618 u32 *pd = page_address(as->pd);
Thierry Redinge5fb4e72019-10-16 13:50:26 +0200619 dma_addr_t pte_dma = smmu_pde_to_dma(smmu, pd[pde]);
Thierry Reding89184652014-04-16 09:24:44 +0200620
Russell King4080e992015-07-27 13:30:12 +0100621 tegra_smmu_set_pde(as, iova, 0);
Russell Kingb98e34f2015-07-27 13:29:05 +0100622
Russell Kinge3c97192015-07-27 13:29:52 +0100623 dma_unmap_page(smmu->dev, pte_dma, SMMU_SIZE_PT, DMA_TO_DEVICE);
Russell Kingb98e34f2015-07-27 13:29:05 +0100624 __free_page(page);
Russell King853520f2015-07-27 13:29:26 +0100625 as->pts[pde] = NULL;
Thierry Reding89184652014-04-16 09:24:44 +0200626 }
627}
628
Russell King8482ee52015-07-27 13:29:10 +0100629static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
Russell Kinge3c97192015-07-27 13:29:52 +0100630 u32 *pte, dma_addr_t pte_dma, u32 val)
Russell King8482ee52015-07-27 13:29:10 +0100631{
632 struct tegra_smmu *smmu = as->smmu;
633 unsigned long offset = offset_in_page(pte);
634
635 *pte = val;
636
Russell Kinge3c97192015-07-27 13:29:52 +0100637 dma_sync_single_range_for_device(smmu->dev, pte_dma, offset,
638 4, DMA_TO_DEVICE);
639 smmu_flush_ptc(smmu, pte_dma, offset);
Russell King8482ee52015-07-27 13:29:10 +0100640 smmu_flush_tlb_group(smmu, as->id, iova);
641 smmu_flush(smmu);
642}
643
Thierry Reding89184652014-04-16 09:24:44 +0200644static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
645 phys_addr_t paddr, size_t size, int prot)
646{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100647 struct tegra_smmu_as *as = to_smmu_as(domain);
Russell Kinge3c97192015-07-27 13:29:52 +0100648 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200649 u32 *pte;
650
Russell Kinge3c97192015-07-27 13:29:52 +0100651 pte = as_get_pte(as, iova, &pte_dma);
Thierry Reding89184652014-04-16 09:24:44 +0200652 if (!pte)
Hiroshi Doyu0547c2f2012-06-25 14:23:57 +0300653 return -ENOMEM;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200654
Russell King7ffc6f02015-08-06 14:56:39 +0200655 /* If we aren't overwriting a pre-existing entry, increment use */
656 if (*pte == 0)
657 tegra_smmu_pte_get_use(as, iova);
658
Russell Kinge3c97192015-07-27 13:29:52 +0100659 tegra_smmu_set_pte(as, iova, pte, pte_dma,
Russell King8482ee52015-07-27 13:29:10 +0100660 __phys_to_pfn(paddr) | SMMU_PTE_ATTR);
Thierry Reding89184652014-04-16 09:24:44 +0200661
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200662 return 0;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200663}
664
Thierry Reding89184652014-04-16 09:24:44 +0200665static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
666 size_t size)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200667{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100668 struct tegra_smmu_as *as = to_smmu_as(domain);
Russell Kinge3c97192015-07-27 13:29:52 +0100669 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200670 u32 *pte;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200671
Russell Kinge3c97192015-07-27 13:29:52 +0100672 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
Russell Kingb98e34f2015-07-27 13:29:05 +0100673 if (!pte || !*pte)
Thierry Reding89184652014-04-16 09:24:44 +0200674 return 0;
Hiroshi Doyu39abf8a2012-08-02 11:46:40 +0300675
Russell Kinge3c97192015-07-27 13:29:52 +0100676 tegra_smmu_set_pte(as, iova, pte, pte_dma, 0);
Russell Kingb98e34f2015-07-27 13:29:05 +0100677 tegra_smmu_pte_put_use(as, iova);
678
Thierry Reding89184652014-04-16 09:24:44 +0200679 return size;
680}
681
682static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
683 dma_addr_t iova)
684{
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100685 struct tegra_smmu_as *as = to_smmu_as(domain);
Thierry Reding89184652014-04-16 09:24:44 +0200686 unsigned long pfn;
Russell Kinge3c97192015-07-27 13:29:52 +0100687 dma_addr_t pte_dma;
Thierry Reding89184652014-04-16 09:24:44 +0200688 u32 *pte;
689
Russell Kinge3c97192015-07-27 13:29:52 +0100690 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
Russell King91137852015-07-27 13:29:00 +0100691 if (!pte || !*pte)
692 return 0;
693
Thierry Reding804cb542015-03-27 11:07:27 +0100694 pfn = *pte & as->smmu->pfn_mask;
Thierry Reding89184652014-04-16 09:24:44 +0200695
696 return PFN_PHYS(pfn);
697}
698
699static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
700{
701 struct platform_device *pdev;
702 struct tegra_mc *mc;
703
704 pdev = of_find_device_by_node(np);
705 if (!pdev)
706 return NULL;
707
708 mc = platform_get_drvdata(pdev);
709 if (!mc)
710 return NULL;
711
712 return mc->smmu;
713}
714
715static int tegra_smmu_add_device(struct device *dev)
716{
717 struct device_node *np = dev->of_node;
718 struct of_phandle_args args;
719 unsigned int index = 0;
720
721 while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
722 &args) == 0) {
723 struct tegra_smmu *smmu;
724
725 smmu = tegra_smmu_find(args.np);
726 if (smmu) {
727 /*
728 * Only a single IOMMU master interface is currently
729 * supported by the Linux kernel, so abort after the
730 * first match.
731 */
732 dev->archdata.iommu = smmu;
733 break;
734 }
735
736 index++;
737 }
738
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200739 return 0;
740}
741
Thierry Reding89184652014-04-16 09:24:44 +0200742static void tegra_smmu_remove_device(struct device *dev)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200743{
Thierry Reding89184652014-04-16 09:24:44 +0200744 dev->archdata.iommu = NULL;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200745}
746
Thierry Reding89184652014-04-16 09:24:44 +0200747static const struct iommu_ops tegra_smmu_ops = {
748 .capable = tegra_smmu_capable,
Joerg Roedeld5f1a812015-03-26 13:43:12 +0100749 .domain_alloc = tegra_smmu_domain_alloc,
750 .domain_free = tegra_smmu_domain_free,
Thierry Reding89184652014-04-16 09:24:44 +0200751 .attach_dev = tegra_smmu_attach_dev,
752 .detach_dev = tegra_smmu_detach_dev,
753 .add_device = tegra_smmu_add_device,
754 .remove_device = tegra_smmu_remove_device,
755 .map = tegra_smmu_map,
756 .unmap = tegra_smmu_unmap,
757 .map_sg = default_iommu_map_sg,
758 .iova_to_phys = tegra_smmu_iova_to_phys,
759
760 .pgsize_bitmap = SZ_4K,
761};
762
763static void tegra_smmu_ahb_enable(void)
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200764{
Thierry Reding89184652014-04-16 09:24:44 +0200765 static const struct of_device_id ahb_match[] = {
766 { .compatible = "nvidia,tegra30-ahb", },
767 { }
768 };
769 struct device_node *ahb;
770
771 ahb = of_find_matching_node(NULL, ahb_match);
772 if (ahb) {
773 tegra_ahb_enable_smmu(ahb);
774 of_node_put(ahb);
775 }
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200776}
777
Thierry Redingd1313e72015-01-23 09:49:25 +0100778static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
779{
780 struct tegra_smmu *smmu = s->private;
781 unsigned int i;
782 u32 value;
783
784 seq_printf(s, "swgroup enabled ASID\n");
785 seq_printf(s, "------------------------\n");
786
787 for (i = 0; i < smmu->soc->num_swgroups; i++) {
788 const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
789 const char *status;
790 unsigned int asid;
791
792 value = smmu_readl(smmu, group->reg);
793
794 if (value & SMMU_ASID_ENABLE)
795 status = "yes";
796 else
797 status = "no";
798
799 asid = value & SMMU_ASID_MASK;
800
801 seq_printf(s, "%-9s %-7s %#04x\n", group->name, status,
802 asid);
803 }
804
805 return 0;
806}
807
808static int tegra_smmu_swgroups_open(struct inode *inode, struct file *file)
809{
810 return single_open(file, tegra_smmu_swgroups_show, inode->i_private);
811}
812
813static const struct file_operations tegra_smmu_swgroups_fops = {
814 .open = tegra_smmu_swgroups_open,
815 .read = seq_read,
816 .llseek = seq_lseek,
817 .release = single_release,
818};
819
820static int tegra_smmu_clients_show(struct seq_file *s, void *data)
821{
822 struct tegra_smmu *smmu = s->private;
823 unsigned int i;
824 u32 value;
825
826 seq_printf(s, "client enabled\n");
827 seq_printf(s, "--------------------\n");
828
829 for (i = 0; i < smmu->soc->num_clients; i++) {
830 const struct tegra_mc_client *client = &smmu->soc->clients[i];
831 const char *status;
832
833 value = smmu_readl(smmu, client->smmu.reg);
834
835 if (value & BIT(client->smmu.bit))
836 status = "yes";
837 else
838 status = "no";
839
840 seq_printf(s, "%-12s %s\n", client->name, status);
841 }
842
843 return 0;
844}
845
846static int tegra_smmu_clients_open(struct inode *inode, struct file *file)
847{
848 return single_open(file, tegra_smmu_clients_show, inode->i_private);
849}
850
851static const struct file_operations tegra_smmu_clients_fops = {
852 .open = tegra_smmu_clients_open,
853 .read = seq_read,
854 .llseek = seq_lseek,
855 .release = single_release,
856};
857
858static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
859{
860 smmu->debugfs = debugfs_create_dir("smmu", NULL);
861 if (!smmu->debugfs)
862 return;
863
864 debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
865 &tegra_smmu_swgroups_fops);
866 debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
867 &tegra_smmu_clients_fops);
868}
869
870static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
871{
872 debugfs_remove_recursive(smmu->debugfs);
873}
874
Thierry Reding89184652014-04-16 09:24:44 +0200875struct tegra_smmu *tegra_smmu_probe(struct device *dev,
876 const struct tegra_smmu_soc *soc,
877 struct tegra_mc *mc)
878{
879 struct tegra_smmu *smmu;
880 size_t size;
881 u32 value;
882 int err;
Hiroshi DOYU7a31f6f2011-11-17 07:31:31 +0200883
Thierry Reding89184652014-04-16 09:24:44 +0200884 /* This can happen on Tegra20 which doesn't have an SMMU */
885 if (!soc)
886 return NULL;
887
888 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
889 if (!smmu)
890 return ERR_PTR(-ENOMEM);
891
892 /*
893 * This is a bit of a hack. Ideally we'd want to simply return this
894 * value. However the IOMMU registration process will attempt to add
895 * all devices to the IOMMU when bus_set_iommu() is called. In order
896 * not to rely on global variables to track the IOMMU instance, we
897 * set it here so that it can be looked up from the .add_device()
898 * callback via the IOMMU device's .drvdata field.
899 */
900 mc->smmu = smmu;
901
902 size = BITS_TO_LONGS(soc->num_asids) * sizeof(long);
903
904 smmu->asids = devm_kzalloc(dev, size, GFP_KERNEL);
905 if (!smmu->asids)
906 return ERR_PTR(-ENOMEM);
907
908 mutex_init(&smmu->lock);
909
910 smmu->regs = mc->regs;
911 smmu->soc = soc;
912 smmu->dev = dev;
913 smmu->mc = mc;
914
Thierry Reding804cb542015-03-27 11:07:27 +0100915 smmu->pfn_mask = BIT_MASK(mc->soc->num_address_bits - PAGE_SHIFT) - 1;
916 dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
917 mc->soc->num_address_bits, smmu->pfn_mask);
Thierry Reding11cec152015-08-06 14:20:31 +0200918 smmu->tlb_mask = (smmu->soc->num_tlb_lines << 1) - 1;
919 dev_dbg(dev, "TLB lines: %u, mask: %#lx\n", smmu->soc->num_tlb_lines,
920 smmu->tlb_mask);
Thierry Reding804cb542015-03-27 11:07:27 +0100921
Thierry Reding89184652014-04-16 09:24:44 +0200922 value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
923
924 if (soc->supports_request_limit)
925 value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
926
927 smmu_writel(smmu, value, SMMU_PTC_CONFIG);
928
929 value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
Thierry Reding11cec152015-08-06 14:20:31 +0200930 SMMU_TLB_CONFIG_ACTIVE_LINES(smmu);
Thierry Reding89184652014-04-16 09:24:44 +0200931
932 if (soc->supports_round_robin_arbitration)
933 value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
934
935 smmu_writel(smmu, value, SMMU_TLB_CONFIG);
936
Russell Kingb8fe0382015-07-27 13:29:41 +0100937 smmu_flush_ptc_all(smmu);
Thierry Reding89184652014-04-16 09:24:44 +0200938 smmu_flush_tlb(smmu);
939 smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
940 smmu_flush(smmu);
941
942 tegra_smmu_ahb_enable();
943
944 err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
945 if (err < 0)
946 return ERR_PTR(err);
947
Thierry Redingd1313e72015-01-23 09:49:25 +0100948 if (IS_ENABLED(CONFIG_DEBUG_FS))
949 tegra_smmu_debugfs_init(smmu);
950
Thierry Reding89184652014-04-16 09:24:44 +0200951 return smmu;
952}
Thierry Redingd1313e72015-01-23 09:49:25 +0100953
954void tegra_smmu_remove(struct tegra_smmu *smmu)
955{
956 if (IS_ENABLED(CONFIG_DEBUG_FS))
957 tegra_smmu_debugfs_exit(smmu);
958}