Merge tag 'pm-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-block.git] / drivers / iommu / tegra-smmu.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
7a31f6f4 2/*
89184651 3 * Copyright (C) 2011-2014 NVIDIA CORPORATION. All rights reserved.
7a31f6f4
HD
4 */
5
804cb54c 6#include <linux/bitops.h>
d1313e78 7#include <linux/debugfs.h>
bc5e6dea 8#include <linux/err.h>
7a31f6f4 9#include <linux/iommu.h>
89184651 10#include <linux/kernel.h>
0760e8fa 11#include <linux/of.h>
d477f603 12#include <linux/of_platform.h>
541f29bb 13#include <linux/pci.h>
89184651
TR
14#include <linux/platform_device.h>
15#include <linux/slab.h>
404d0b30 16#include <linux/spinlock.h>
461a6946 17#include <linux/dma-mapping.h>
306a7f91
TR
18
19#include <soc/tegra/ahb.h>
89184651 20#include <soc/tegra/mc.h>
7a31f6f4 21
8e8b4ac5
PT
22#include "iommu-pages.h"
23
7f4c9176
TR
24struct tegra_smmu_group {
25 struct list_head list;
1ea5440e 26 struct tegra_smmu *smmu;
7f4c9176
TR
27 const struct tegra_smmu_group_soc *soc;
28 struct iommu_group *group;
21d3c040 29 unsigned int swgroup;
7f4c9176
TR
30};
31
89184651
TR
32struct tegra_smmu {
33 void __iomem *regs;
34 struct device *dev;
e6bc5933 35
89184651
TR
36 struct tegra_mc *mc;
37 const struct tegra_smmu_soc *soc;
39abf8aa 38
7f4c9176
TR
39 struct list_head groups;
40
804cb54c 41 unsigned long pfn_mask;
11cec15b 42 unsigned long tlb_mask;
804cb54c 43
89184651
TR
44 unsigned long *asids;
45 struct mutex lock;
39abf8aa 46
89184651 47 struct list_head list;
d1313e78
TR
48
49 struct dentry *debugfs;
0b480e44
JR
50
51 struct iommu_device iommu; /* IOMMU Core code handle */
7a31f6f4 52};
7a31f6f4 53
50568f87 54struct tegra_pd;
a96969a9 55struct tegra_pt;
50568f87 56
89184651 57struct tegra_smmu_as {
d5f1a81c 58 struct iommu_domain domain;
89184651
TR
59 struct tegra_smmu *smmu;
60 unsigned int use_count;
404d0b30 61 spinlock_t lock;
32924c76 62 u32 *count;
a96969a9 63 struct tegra_pt **pts;
50568f87 64 struct tegra_pd *pd;
e3c97196 65 dma_addr_t pd_dma;
89184651
TR
66 unsigned id;
67 u32 attr;
7a31f6f4
HD
68};
69
d5f1a81c
JR
70static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom)
71{
72 return container_of(dom, struct tegra_smmu_as, domain);
73}
74
89184651
TR
75static inline void smmu_writel(struct tegra_smmu *smmu, u32 value,
76 unsigned long offset)
77{
78 writel(value, smmu->regs + offset);
79}
7a31f6f4 80
89184651
TR
81static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset)
82{
83 return readl(smmu->regs + offset);
84}
5a2c937a 85
89184651
TR
86#define SMMU_CONFIG 0x010
87#define SMMU_CONFIG_ENABLE (1 << 0)
7a31f6f4 88
89184651
TR
89#define SMMU_TLB_CONFIG 0x14
90#define SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29)
91#define SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28)
11cec15b
TR
92#define SMMU_TLB_CONFIG_ACTIVE_LINES(smmu) \
93 ((smmu)->soc->num_tlb_lines & (smmu)->tlb_mask)
0760e8fa 94
89184651
TR
95#define SMMU_PTC_CONFIG 0x18
96#define SMMU_PTC_CONFIG_ENABLE (1 << 29)
97#define SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24)
98#define SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f)
39abf8aa 99
89184651
TR
100#define SMMU_PTB_ASID 0x01c
101#define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
a3b24915 102
89184651 103#define SMMU_PTB_DATA 0x020
e3c97196 104#define SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr))
7a31f6f4 105
e3c97196 106#define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr))
7a31f6f4 107
89184651
TR
108#define SMMU_TLB_FLUSH 0x030
109#define SMMU_TLB_FLUSH_VA_MATCH_ALL (0 << 0)
110#define SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0)
111#define SMMU_TLB_FLUSH_VA_MATCH_GROUP (3 << 0)
89184651
TR
112#define SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
113 SMMU_TLB_FLUSH_VA_MATCH_SECTION)
114#define SMMU_TLB_FLUSH_VA_GROUP(addr) ((((addr) & 0xffffc000) >> 12) | \
115 SMMU_TLB_FLUSH_VA_MATCH_GROUP)
116#define SMMU_TLB_FLUSH_ASID_MATCH (1 << 31)
a6870e92 117
89184651
TR
118#define SMMU_PTC_FLUSH 0x034
119#define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
120#define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
a6870e92 121
89184651
TR
122#define SMMU_PTC_FLUSH_HI 0x9b8
123#define SMMU_PTC_FLUSH_HI_MASK 0x3
7a31f6f4 124
89184651
TR
125/* per-SWGROUP SMMU_*_ASID register */
126#define SMMU_ASID_ENABLE (1 << 31)
127#define SMMU_ASID_MASK 0x7f
128#define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
a6870e92 129
89184651
TR
130/* page table definitions */
131#define SMMU_NUM_PDE 1024
132#define SMMU_NUM_PTE 1024
a6870e92 133
89184651
TR
134#define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
135#define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
7a31f6f4 136
89184651
TR
137#define SMMU_PDE_SHIFT 22
138#define SMMU_PTE_SHIFT 12
fe1229b9 139
82fa58e8
NC
140#define SMMU_PAGE_MASK (~(SMMU_SIZE_PT-1))
141#define SMMU_OFFSET_IN_PAGE(x) ((unsigned long)(x) & ~SMMU_PAGE_MASK)
142#define SMMU_PFN_PHYS(x) ((phys_addr_t)(x) << SMMU_PTE_SHIFT)
143#define SMMU_PHYS_PFN(x) ((unsigned long)((x) >> SMMU_PTE_SHIFT))
144
89184651
TR
145#define SMMU_PD_READABLE (1 << 31)
146#define SMMU_PD_WRITABLE (1 << 30)
147#define SMMU_PD_NONSECURE (1 << 29)
7a31f6f4 148
89184651
TR
149#define SMMU_PDE_READABLE (1 << 31)
150#define SMMU_PDE_WRITABLE (1 << 30)
151#define SMMU_PDE_NONSECURE (1 << 29)
152#define SMMU_PDE_NEXT (1 << 28)
7a31f6f4 153
89184651
TR
154#define SMMU_PTE_READABLE (1 << 31)
155#define SMMU_PTE_WRITABLE (1 << 30)
156#define SMMU_PTE_NONSECURE (1 << 29)
7a31f6f4 157
89184651
TR
158#define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
159 SMMU_PDE_NONSECURE)
7a31f6f4 160
50568f87
JG
161struct tegra_pd {
162 u32 val[SMMU_NUM_PDE];
163};
164
a96969a9
JG
165struct tegra_pt {
166 u32 val[SMMU_NUM_PTE];
167};
168
34d35f8c
RK
169static unsigned int iova_pd_index(unsigned long iova)
170{
171 return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1);
172}
173
174static unsigned int iova_pt_index(unsigned long iova)
175{
176 return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1);
177}
178
e3c97196 179static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr)
4b3c7d10 180{
e3c97196
RK
181 addr >>= 12;
182 return (addr & smmu->pfn_mask) == addr;
183}
4b3c7d10 184
96d3ab80 185static dma_addr_t smmu_pde_to_dma(struct tegra_smmu *smmu, u32 pde)
e3c97196 186{
96d3ab80 187 return (dma_addr_t)(pde & smmu->pfn_mask) << 12;
4b3c7d10
RK
188}
189
b8fe0382
RK
190static void smmu_flush_ptc_all(struct tegra_smmu *smmu)
191{
192 smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
193}
194
e3c97196 195static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma,
89184651 196 unsigned long offset)
7a31f6f4 197{
89184651
TR
198 u32 value;
199
b8fe0382 200 offset &= ~(smmu->mc->soc->atom_size - 1);
89184651 201
b8fe0382 202 if (smmu->mc->soc->num_address_bits > 32) {
e3c97196
RK
203#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
204 value = (dma >> 32) & SMMU_PTC_FLUSH_HI_MASK;
89184651 205#else
b8fe0382 206 value = 0;
89184651 207#endif
b8fe0382 208 smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
7a31f6f4 209 }
89184651 210
e3c97196 211 value = (dma + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
89184651 212 smmu_writel(smmu, value, SMMU_PTC_FLUSH);
7a31f6f4
HD
213}
214
89184651 215static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
7a31f6f4 216{
89184651 217 smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
7a31f6f4
HD
218}
219
89184651
TR
220static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
221 unsigned long asid)
7a31f6f4 222{
89184651 223 u32 value;
7a31f6f4 224
43a0541e
DO
225 if (smmu->soc->num_asids == 4)
226 value = (asid & 0x3) << 29;
227 else
228 value = (asid & 0x7f) << 24;
229
230 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_MATCH_ALL;
89184651 231 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
7a31f6f4
HD
232}
233
89184651
TR
234static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
235 unsigned long asid,
236 unsigned long iova)
7a31f6f4 237{
89184651 238 u32 value;
7a31f6f4 239
43a0541e
DO
240 if (smmu->soc->num_asids == 4)
241 value = (asid & 0x3) << 29;
242 else
243 value = (asid & 0x7f) << 24;
244
245 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_SECTION(iova);
89184651 246 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
7a31f6f4
HD
247}
248
89184651
TR
249static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu,
250 unsigned long asid,
251 unsigned long iova)
7a31f6f4 252{
89184651 253 u32 value;
7a31f6f4 254
43a0541e
DO
255 if (smmu->soc->num_asids == 4)
256 value = (asid & 0x3) << 29;
257 else
258 value = (asid & 0x7f) << 24;
259
260 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_GROUP(iova);
89184651 261 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
7a31f6f4
HD
262}
263
89184651 264static inline void smmu_flush(struct tegra_smmu *smmu)
7a31f6f4 265{
446152d5 266 smmu_readl(smmu, SMMU_PTB_ASID);
7a31f6f4
HD
267}
268
89184651 269static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
7a31f6f4 270{
89184651 271 unsigned long id;
7a31f6f4 272
89184651 273 id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
d5f583bf 274 if (id >= smmu->soc->num_asids)
89184651 275 return -ENOSPC;
7a31f6f4 276
89184651
TR
277 set_bit(id, smmu->asids);
278 *idp = id;
279
89184651 280 return 0;
7a31f6f4
HD
281}
282
89184651 283static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
7a31f6f4 284{
89184651 285 clear_bit(id, smmu->asids);
7a31f6f4 286}
89184651 287
3529375e 288static struct iommu_domain *tegra_smmu_domain_alloc_paging(struct device *dev)
7a31f6f4 289{
89184651 290 struct tegra_smmu_as *as;
7a31f6f4 291
89184651
TR
292 as = kzalloc(sizeof(*as), GFP_KERNEL);
293 if (!as)
d5f1a81c 294 return NULL;
7a31f6f4 295
89184651 296 as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
7a31f6f4 297
28024569 298 as->pd = iommu_alloc_pages_sz(GFP_KERNEL | __GFP_DMA, SMMU_SIZE_PD);
89184651
TR
299 if (!as->pd) {
300 kfree(as);
d5f1a81c 301 return NULL;
7a31f6f4 302 }
9e971a03 303
32924c76 304 as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL);
89184651 305 if (!as->count) {
3e8e986c 306 iommu_free_pages(as->pd);
89184651 307 kfree(as);
d5f1a81c 308 return NULL;
7a31f6f4 309 }
9e971a03 310
853520fa
RK
311 as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL);
312 if (!as->pts) {
32924c76 313 kfree(as->count);
3e8e986c 314 iommu_free_pages(as->pd);
853520fa
RK
315 kfree(as);
316 return NULL;
317 }
318
404d0b30
DO
319 spin_lock_init(&as->lock);
320
471d9144 321 /* setup aperture */
7f65ef01
JR
322 as->domain.geometry.aperture_start = 0;
323 as->domain.geometry.aperture_end = 0xffffffff;
324 as->domain.geometry.force_aperture = true;
f9a4f063 325
d5f1a81c 326 return &as->domain;
7a31f6f4
HD
327}
328
d5f1a81c 329static void tegra_smmu_domain_free(struct iommu_domain *domain)
7a31f6f4 330{
d5f1a81c 331 struct tegra_smmu_as *as = to_smmu_as(domain);
7a31f6f4 332
89184651 333 /* TODO: free page directory and page tables */
7a31f6f4 334
4f97031f
DO
335 WARN_ON_ONCE(as->use_count);
336 kfree(as->count);
337 kfree(as->pts);
89184651 338 kfree(as);
7a31f6f4
HD
339}
340
89184651
TR
341static const struct tegra_smmu_swgroup *
342tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
7a31f6f4 343{
89184651
TR
344 const struct tegra_smmu_swgroup *group = NULL;
345 unsigned int i;
7a31f6f4 346
89184651
TR
347 for (i = 0; i < smmu->soc->num_swgroups; i++) {
348 if (smmu->soc->swgroups[i].swgroup == swgroup) {
349 group = &smmu->soc->swgroups[i];
350 break;
351 }
352 }
7a31f6f4 353
89184651 354 return group;
7a31f6f4
HD
355}
356
89184651
TR
357static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
358 unsigned int asid)
7a31f6f4 359{
89184651
TR
360 const struct tegra_smmu_swgroup *group;
361 unsigned int i;
362 u32 value;
7a31f6f4 363
e31e5929
NK
364 group = tegra_smmu_find_swgroup(smmu, swgroup);
365 if (group) {
366 value = smmu_readl(smmu, group->reg);
367 value &= ~SMMU_ASID_MASK;
368 value |= SMMU_ASID_VALUE(asid);
369 value |= SMMU_ASID_ENABLE;
370 smmu_writel(smmu, value, group->reg);
371 } else {
372 pr_warn("%s group from swgroup %u not found\n", __func__,
373 swgroup);
374 /* No point moving ahead if group was not found */
375 return;
376 }
377
89184651
TR
378 for (i = 0; i < smmu->soc->num_clients; i++) {
379 const struct tegra_mc_client *client = &smmu->soc->clients[i];
7a31f6f4 380
89184651
TR
381 if (client->swgroup != swgroup)
382 continue;
7a31f6f4 383
4f1ac76e
TR
384 value = smmu_readl(smmu, client->regs.smmu.reg);
385 value |= BIT(client->regs.smmu.bit);
386 smmu_writel(smmu, value, client->regs.smmu.reg);
89184651 387 }
7a31f6f4
HD
388}
389
89184651
TR
390static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
391 unsigned int asid)
7a31f6f4 392{
89184651
TR
393 const struct tegra_smmu_swgroup *group;
394 unsigned int i;
395 u32 value;
7a31f6f4 396
89184651
TR
397 group = tegra_smmu_find_swgroup(smmu, swgroup);
398 if (group) {
399 value = smmu_readl(smmu, group->reg);
400 value &= ~SMMU_ASID_MASK;
401 value |= SMMU_ASID_VALUE(asid);
402 value &= ~SMMU_ASID_ENABLE;
403 smmu_writel(smmu, value, group->reg);
404 }
7a31f6f4 405
89184651
TR
406 for (i = 0; i < smmu->soc->num_clients; i++) {
407 const struct tegra_mc_client *client = &smmu->soc->clients[i];
7a31f6f4 408
89184651
TR
409 if (client->swgroup != swgroup)
410 continue;
7a31f6f4 411
4f1ac76e
TR
412 value = smmu_readl(smmu, client->regs.smmu.reg);
413 value &= ~BIT(client->regs.smmu.bit);
414 smmu_writel(smmu, value, client->regs.smmu.reg);
89184651 415 }
7a31f6f4
HD
416}
417
89184651
TR
418static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
419 struct tegra_smmu_as *as)
7a31f6f4 420{
89184651 421 u32 value;
d5f583bf
NC
422 int err = 0;
423
424 mutex_lock(&smmu->lock);
7a31f6f4 425
89184651
TR
426 if (as->use_count > 0) {
427 as->use_count++;
d5f583bf 428 goto unlock;
7a31f6f4 429 }
7a31f6f4 430
50568f87
JG
431 as->pd_dma =
432 dma_map_single(smmu->dev, as->pd, SMMU_SIZE_PD, DMA_TO_DEVICE);
d5f583bf
NC
433 if (dma_mapping_error(smmu->dev, as->pd_dma)) {
434 err = -ENOMEM;
435 goto unlock;
436 }
e3c97196
RK
437
438 /* We can't handle 64-bit DMA addresses */
439 if (!smmu_dma_addr_valid(smmu, as->pd_dma)) {
440 err = -ENOMEM;
441 goto err_unmap;
442 }
443
89184651
TR
444 err = tegra_smmu_alloc_asid(smmu, &as->id);
445 if (err < 0)
e3c97196 446 goto err_unmap;
7a31f6f4 447
e3c97196 448 smmu_flush_ptc(smmu, as->pd_dma, 0);
89184651 449 smmu_flush_tlb_asid(smmu, as->id);
7a31f6f4 450
89184651 451 smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
e3c97196 452 value = SMMU_PTB_DATA_VALUE(as->pd_dma, as->attr);
89184651
TR
453 smmu_writel(smmu, value, SMMU_PTB_DATA);
454 smmu_flush(smmu);
7a31f6f4 455
89184651
TR
456 as->smmu = smmu;
457 as->use_count++;
7a31f6f4 458
d5f583bf
NC
459 mutex_unlock(&smmu->lock);
460
89184651 461 return 0;
e3c97196
RK
462
463err_unmap:
50568f87 464 dma_unmap_single(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
d5f583bf
NC
465unlock:
466 mutex_unlock(&smmu->lock);
467
e3c97196 468 return err;
7a31f6f4
HD
469}
470
89184651
TR
471static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
472 struct tegra_smmu_as *as)
7a31f6f4 473{
d5f583bf
NC
474 mutex_lock(&smmu->lock);
475
476 if (--as->use_count > 0) {
477 mutex_unlock(&smmu->lock);
89184651 478 return;
d5f583bf 479 }
89184651
TR
480
481 tegra_smmu_free_asid(smmu, as->id);
e3c97196 482
50568f87 483 dma_unmap_single(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
e3c97196 484
89184651 485 as->smmu = NULL;
d5f583bf
NC
486
487 mutex_unlock(&smmu->lock);
7a31f6f4
HD
488}
489
89184651
TR
490static int tegra_smmu_attach_dev(struct iommu_domain *domain,
491 struct device *dev)
7a31f6f4 492{
8750d207 493 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
a5616e24 494 struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
d5f1a81c 495 struct tegra_smmu_as *as = to_smmu_as(domain);
8750d207
NC
496 unsigned int index;
497 int err;
d2453b2c 498
8750d207
NC
499 if (!fwspec)
500 return -ENOENT;
d2453b2c 501
8750d207 502 for (index = 0; index < fwspec->num_ids; index++) {
89184651 503 err = tegra_smmu_as_prepare(smmu, as);
8750d207
NC
504 if (err)
505 goto disable;
89184651 506
8750d207 507 tegra_smmu_enable(smmu, fwspec->ids[index], as->id);
7a31f6f4 508 }
7a31f6f4 509
89184651
TR
510 if (index == 0)
511 return -ENODEV;
7a31f6f4 512
89184651 513 return 0;
8750d207
NC
514
515disable:
516 while (index--) {
517 tegra_smmu_disable(smmu, fwspec->ids[index], as->id);
518 tegra_smmu_as_unprepare(smmu, as);
519 }
520
521 return err;
89184651 522}
7a31f6f4 523
c8cc2655
JG
524static int tegra_smmu_identity_attach(struct iommu_domain *identity_domain,
525 struct device *dev)
89184651 526{
c1fe9119 527 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
8750d207 528 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
c8cc2655
JG
529 struct tegra_smmu_as *as;
530 struct tegra_smmu *smmu;
8750d207 531 unsigned int index;
7a31f6f4 532
8750d207 533 if (!fwspec)
c8cc2655 534 return -ENODEV;
7a31f6f4 535
c8cc2655
JG
536 if (domain == identity_domain || !domain)
537 return 0;
7a31f6f4 538
c8cc2655
JG
539 as = to_smmu_as(domain);
540 smmu = as->smmu;
8750d207
NC
541 for (index = 0; index < fwspec->num_ids; index++) {
542 tegra_smmu_disable(smmu, fwspec->ids[index], as->id);
89184651 543 tegra_smmu_as_unprepare(smmu, as);
89184651 544 }
c8cc2655 545 return 0;
7a31f6f4
HD
546}
547
c8cc2655
JG
548static struct iommu_domain_ops tegra_smmu_identity_ops = {
549 .attach_dev = tegra_smmu_identity_attach,
550};
551
552static struct iommu_domain tegra_smmu_identity_domain = {
553 .type = IOMMU_DOMAIN_IDENTITY,
554 .ops = &tegra_smmu_identity_ops,
555};
556
4080e99b
RK
557static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova,
558 u32 value)
559{
560 unsigned int pd_index = iova_pd_index(iova);
561 struct tegra_smmu *smmu = as->smmu;
f9705d66 562 u32 *pd = &as->pd->val[pd_index];
4080e99b
RK
563 unsigned long offset = pd_index * sizeof(*pd);
564
565 /* Set the page directory entry first */
f9705d66 566 *pd = value;
4080e99b
RK
567
568 /* The flush the page directory entry from caches */
569 dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset,
570 sizeof(*pd), DMA_TO_DEVICE);
571
572 /* And flush the iommu */
573 smmu_flush_ptc(smmu, as->pd_dma, offset);
574 smmu_flush_tlb_section(smmu, as->id, iova);
575 smmu_flush(smmu);
576}
577
a96969a9 578static u32 *tegra_smmu_pte_offset(struct tegra_pt *pt, unsigned long iova)
0b42c7c1 579{
a96969a9 580 return &pt->val[iova_pt_index(iova)];
0b42c7c1
RK
581}
582
583static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
e3c97196 584 dma_addr_t *dmap)
0b42c7c1
RK
585{
586 unsigned int pd_index = iova_pd_index(iova);
96d3ab80 587 struct tegra_smmu *smmu = as->smmu;
a96969a9 588 struct tegra_pt *pt;
0b42c7c1 589
a96969a9
JG
590 pt = as->pts[pd_index];
591 if (!pt)
0b42c7c1
RK
592 return NULL;
593
50568f87 594 *dmap = smmu_pde_to_dma(smmu, as->pd->val[pd_index]);
0b42c7c1 595
a96969a9 596 return tegra_smmu_pte_offset(pt, iova);
0b42c7c1
RK
597}
598
89184651 599static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
a96969a9 600 dma_addr_t *dmap, struct tegra_pt *pt)
7a31f6f4 601{
34d35f8c 602 unsigned int pde = iova_pd_index(iova);
89184651 603 struct tegra_smmu *smmu = as->smmu;
89184651 604
853520fa 605 if (!as->pts[pde]) {
e3c97196
RK
606 dma_addr_t dma;
607
a96969a9
JG
608 dma = dma_map_single(smmu->dev, pt, SMMU_SIZE_PT,
609 DMA_TO_DEVICE);
e3c97196 610 if (dma_mapping_error(smmu->dev, dma)) {
3e8e986c 611 iommu_free_pages(pt);
e3c97196
RK
612 return NULL;
613 }
614
615 if (!smmu_dma_addr_valid(smmu, dma)) {
a96969a9
JG
616 dma_unmap_single(smmu->dev, dma, SMMU_SIZE_PT,
617 DMA_TO_DEVICE);
3e8e986c 618 iommu_free_pages(pt);
e3c97196
RK
619 return NULL;
620 }
621
a96969a9 622 as->pts[pde] = pt;
853520fa 623
4080e99b
RK
624 tegra_smmu_set_pde(as, iova, SMMU_MK_PDE(dma, SMMU_PDE_ATTR |
625 SMMU_PDE_NEXT));
e3c97196
RK
626
627 *dmap = dma;
89184651 628 } else {
50568f87 629 *dmap = smmu_pde_to_dma(smmu, as->pd->val[pde]);
7a31f6f4
HD
630 }
631
7ffc6f06
RK
632 return tegra_smmu_pte_offset(as->pts[pde], iova);
633}
0b42c7c1 634
7ffc6f06
RK
635static void tegra_smmu_pte_get_use(struct tegra_smmu_as *as, unsigned long iova)
636{
637 unsigned int pd_index = iova_pd_index(iova);
7a31f6f4 638
7ffc6f06 639 as->count[pd_index]++;
89184651 640}
39abf8aa 641
b98e34f0 642static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
39abf8aa 643{
34d35f8c 644 unsigned int pde = iova_pd_index(iova);
a96969a9 645 struct tegra_pt *pt = as->pts[pde];
39abf8aa 646
89184651
TR
647 /*
648 * When no entries in this page table are used anymore, return the
649 * memory page to the system.
650 */
32924c76 651 if (--as->count[pde] == 0) {
4080e99b 652 struct tegra_smmu *smmu = as->smmu;
50568f87 653 dma_addr_t pte_dma = smmu_pde_to_dma(smmu, as->pd->val[pde]);
39abf8aa 654
4080e99b 655 tegra_smmu_set_pde(as, iova, 0);
b98e34f0 656
a96969a9
JG
657 dma_unmap_single(smmu->dev, pte_dma, SMMU_SIZE_PT,
658 DMA_TO_DEVICE);
3e8e986c 659 iommu_free_pages(pt);
853520fa 660 as->pts[pde] = NULL;
39abf8aa 661 }
39abf8aa
HD
662}
663
8482ee5e 664static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
e3c97196 665 u32 *pte, dma_addr_t pte_dma, u32 val)
8482ee5e
RK
666{
667 struct tegra_smmu *smmu = as->smmu;
82fa58e8 668 unsigned long offset = SMMU_OFFSET_IN_PAGE(pte);
8482ee5e
RK
669
670 *pte = val;
671
e3c97196
RK
672 dma_sync_single_range_for_device(smmu->dev, pte_dma, offset,
673 4, DMA_TO_DEVICE);
674 smmu_flush_ptc(smmu, pte_dma, offset);
8482ee5e
RK
675 smmu_flush_tlb_group(smmu, as->id, iova);
676 smmu_flush(smmu);
677}
678
a96969a9
JG
679static struct tegra_pt *as_get_pde_page(struct tegra_smmu_as *as,
680 unsigned long iova, gfp_t gfp,
681 unsigned long *flags)
404d0b30
DO
682{
683 unsigned int pde = iova_pd_index(iova);
a96969a9 684 struct tegra_pt *pt = as->pts[pde];
404d0b30
DO
685
686 /* at first check whether allocation needs to be done at all */
a96969a9
JG
687 if (pt)
688 return pt;
404d0b30
DO
689
690 /*
691 * In order to prevent exhaustion of the atomic memory pool, we
692 * allocate page in a sleeping context if GFP flags permit. Hence
693 * spinlock needs to be unlocked and re-locked after allocation.
694 */
2973d822 695 if (gfpflags_allow_blocking(gfp))
404d0b30
DO
696 spin_unlock_irqrestore(&as->lock, *flags);
697
28024569 698 pt = iommu_alloc_pages_sz(gfp | __GFP_DMA, SMMU_SIZE_PT);
404d0b30 699
2973d822 700 if (gfpflags_allow_blocking(gfp))
404d0b30
DO
701 spin_lock_irqsave(&as->lock, *flags);
702
703 /*
704 * In a case of blocking allocation, a concurrent mapping may win
705 * the PDE allocation. In this case the allocated page isn't needed
706 * if allocation succeeded and the allocation failure isn't fatal.
707 */
708 if (as->pts[pde]) {
a96969a9 709 if (pt)
3e8e986c 710 iommu_free_pages(pt);
404d0b30 711
a96969a9 712 pt = as->pts[pde];
404d0b30
DO
713 }
714
a96969a9 715 return pt;
404d0b30
DO
716}
717
718static int
719__tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
720 phys_addr_t paddr, size_t size, int prot, gfp_t gfp,
721 unsigned long *flags)
39abf8aa 722{
d5f1a81c 723 struct tegra_smmu_as *as = to_smmu_as(domain);
e3c97196 724 dma_addr_t pte_dma;
a96969a9 725 struct tegra_pt *pt;
43d957b1 726 u32 pte_attrs;
89184651 727 u32 *pte;
39abf8aa 728
a96969a9
JG
729 pt = as_get_pde_page(as, iova, gfp, flags);
730 if (!pt)
404d0b30
DO
731 return -ENOMEM;
732
a96969a9 733 pte = as_get_pte(as, iova, &pte_dma, pt);
89184651
TR
734 if (!pte)
735 return -ENOMEM;
39abf8aa 736
7ffc6f06
RK
737 /* If we aren't overwriting a pre-existing entry, increment use */
738 if (*pte == 0)
739 tegra_smmu_pte_get_use(as, iova);
740
43d957b1
DO
741 pte_attrs = SMMU_PTE_NONSECURE;
742
743 if (prot & IOMMU_READ)
744 pte_attrs |= SMMU_PTE_READABLE;
745
746 if (prot & IOMMU_WRITE)
747 pte_attrs |= SMMU_PTE_WRITABLE;
748
e3c97196 749 tegra_smmu_set_pte(as, iova, pte, pte_dma,
82fa58e8 750 SMMU_PHYS_PFN(paddr) | pte_attrs);
39abf8aa 751
39abf8aa
HD
752 return 0;
753}
754
404d0b30
DO
755static size_t
756__tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
757 size_t size, struct iommu_iotlb_gather *gather)
39abf8aa 758{
d5f1a81c 759 struct tegra_smmu_as *as = to_smmu_as(domain);
e3c97196 760 dma_addr_t pte_dma;
89184651 761 u32 *pte;
39abf8aa 762
e3c97196 763 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
b98e34f0 764 if (!pte || !*pte)
89184651 765 return 0;
39abf8aa 766
e3c97196 767 tegra_smmu_set_pte(as, iova, pte, pte_dma, 0);
b98e34f0
RK
768 tegra_smmu_pte_put_use(as, iova);
769
89184651 770 return size;
39abf8aa
HD
771}
772
404d0b30 773static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
39f823df
RM
774 phys_addr_t paddr, size_t size, size_t count,
775 int prot, gfp_t gfp, size_t *mapped)
404d0b30
DO
776{
777 struct tegra_smmu_as *as = to_smmu_as(domain);
778 unsigned long flags;
779 int ret;
780
781 spin_lock_irqsave(&as->lock, flags);
782 ret = __tegra_smmu_map(domain, iova, paddr, size, prot, gfp, &flags);
783 spin_unlock_irqrestore(&as->lock, flags);
784
39f823df
RM
785 if (!ret)
786 *mapped = size;
787
404d0b30
DO
788 return ret;
789}
790
791static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
39f823df 792 size_t size, size_t count, struct iommu_iotlb_gather *gather)
404d0b30
DO
793{
794 struct tegra_smmu_as *as = to_smmu_as(domain);
795 unsigned long flags;
796
797 spin_lock_irqsave(&as->lock, flags);
798 size = __tegra_smmu_unmap(domain, iova, size, gather);
799 spin_unlock_irqrestore(&as->lock, flags);
800
801 return size;
802}
803
89184651
TR
804static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
805 dma_addr_t iova)
39abf8aa 806{
d5f1a81c 807 struct tegra_smmu_as *as = to_smmu_as(domain);
89184651 808 unsigned long pfn;
e3c97196 809 dma_addr_t pte_dma;
89184651 810 u32 *pte;
39abf8aa 811
e3c97196 812 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
9113785c
RK
813 if (!pte || !*pte)
814 return 0;
815
804cb54c 816 pfn = *pte & as->smmu->pfn_mask;
39abf8aa 817
4fba9885 818 return SMMU_PFN_PHYS(pfn) + SMMU_OFFSET_IN_PAGE(iova);
39abf8aa
HD
819}
820
765a9d1d
NC
821static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
822{
823 struct platform_device *pdev;
824 struct tegra_mc *mc;
825
826 pdev = of_find_device_by_node(np);
827 if (!pdev)
828 return NULL;
829
830 mc = platform_get_drvdata(pdev);
9826e393
ML
831 if (!mc) {
832 put_device(&pdev->dev);
765a9d1d 833 return NULL;
9826e393 834 }
765a9d1d
NC
835
836 return mc->smmu;
837}
838
839static int tegra_smmu_configure(struct tegra_smmu *smmu, struct device *dev,
b42a905b 840 const struct of_phandle_args *args)
765a9d1d
NC
841{
842 const struct iommu_ops *ops = smmu->iommu.ops;
843 int err;
844
342d3c1c 845 err = iommu_fwspec_init(dev, dev_fwnode(smmu->dev));
765a9d1d
NC
846 if (err < 0) {
847 dev_err(dev, "failed to initialize fwspec: %d\n", err);
848 return err;
849 }
850
851 err = ops->of_xlate(dev, args);
852 if (err < 0) {
853 dev_err(dev, "failed to parse SW group ID: %d\n", err);
765a9d1d
NC
854 return err;
855 }
856
857 return 0;
858}
859
b287ba73 860static struct iommu_device *tegra_smmu_probe_device(struct device *dev)
7a31f6f4 861{
765a9d1d
NC
862 struct device_node *np = dev->of_node;
863 struct tegra_smmu *smmu = NULL;
864 struct of_phandle_args args;
865 unsigned int index = 0;
866 int err;
867
868 while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
869 &args) == 0) {
870 smmu = tegra_smmu_find(args.np);
871 if (smmu) {
872 err = tegra_smmu_configure(smmu, dev, &args);
89184651 873
8dfd0fa6
DO
874 if (err < 0) {
875 of_node_put(args.np);
765a9d1d 876 return ERR_PTR(err);
8dfd0fa6 877 }
765a9d1d
NC
878 }
879
880 of_node_put(args.np);
881 index++;
882 }
883
884 smmu = dev_iommu_priv_get(dev);
7f4c9176 885 if (!smmu)
b287ba73 886 return ERR_PTR(-ENODEV);
d92e1f84 887
b287ba73 888 return &smmu->iommu;
7a31f6f4
HD
889}
890
7f4c9176
TR
891static const struct tegra_smmu_group_soc *
892tegra_smmu_find_group(struct tegra_smmu *smmu, unsigned int swgroup)
893{
894 unsigned int i, j;
895
896 for (i = 0; i < smmu->soc->num_groups; i++)
897 for (j = 0; j < smmu->soc->groups[i].num_swgroups; j++)
898 if (smmu->soc->groups[i].swgroups[j] == swgroup)
899 return &smmu->soc->groups[i];
900
901 return NULL;
902}
903
1ea5440e
TR
904static void tegra_smmu_group_release(void *iommu_data)
905{
906 struct tegra_smmu_group *group = iommu_data;
907 struct tegra_smmu *smmu = group->smmu;
908
909 mutex_lock(&smmu->lock);
910 list_del(&group->list);
911 mutex_unlock(&smmu->lock);
912}
913
cf910f61 914static struct iommu_group *tegra_smmu_device_group(struct device *dev)
7f4c9176 915{
cf910f61
NC
916 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
917 struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
7f4c9176 918 const struct tegra_smmu_group_soc *soc;
cf910f61 919 unsigned int swgroup = fwspec->ids[0];
7f4c9176 920 struct tegra_smmu_group *group;
5b30fbfa 921 struct iommu_group *grp;
7f4c9176 922
21d3c040 923 /* Find group_soc associating with swgroup */
7f4c9176 924 soc = tegra_smmu_find_group(smmu, swgroup);
7f4c9176
TR
925
926 mutex_lock(&smmu->lock);
927
21d3c040 928 /* Find existing iommu_group associating with swgroup or group_soc */
7f4c9176 929 list_for_each_entry(group, &smmu->groups, list)
21d3c040 930 if ((group->swgroup == swgroup) || (soc && group->soc == soc)) {
5b30fbfa 931 grp = iommu_group_ref_get(group->group);
7f4c9176 932 mutex_unlock(&smmu->lock);
5b30fbfa 933 return grp;
7f4c9176
TR
934 }
935
936 group = devm_kzalloc(smmu->dev, sizeof(*group), GFP_KERNEL);
937 if (!group) {
938 mutex_unlock(&smmu->lock);
939 return NULL;
940 }
941
942 INIT_LIST_HEAD(&group->list);
21d3c040 943 group->swgroup = swgroup;
1ea5440e 944 group->smmu = smmu;
7f4c9176
TR
945 group->soc = soc;
946
541f29bb
NC
947 if (dev_is_pci(dev))
948 group->group = pci_device_group(dev);
949 else
950 group->group = generic_device_group(dev);
951
83476bfa 952 if (IS_ERR(group->group)) {
7f4c9176
TR
953 devm_kfree(smmu->dev, group);
954 mutex_unlock(&smmu->lock);
955 return NULL;
956 }
957
1ea5440e 958 iommu_group_set_iommudata(group->group, group, tegra_smmu_group_release);
21d3c040
NC
959 if (soc)
960 iommu_group_set_name(group->group, soc->name);
7f4c9176
TR
961 list_add_tail(&group->list, &smmu->groups);
962 mutex_unlock(&smmu->lock);
963
964 return group->group;
965}
966
7f4c9176 967static int tegra_smmu_of_xlate(struct device *dev,
b42a905b 968 const struct of_phandle_args *args)
7f4c9176 969{
25938c73
NC
970 struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
971 struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
7f4c9176
TR
972 u32 id = args->args[0];
973
25938c73
NC
974 /*
975 * Note: we are here releasing the reference of &iommu_pdev->dev, which
976 * is mc->dev. Although some functions in tegra_smmu_ops may keep using
977 * its private data beyond this point, it's still safe to do so because
978 * the SMMU parent device is the same as the MC, so the reference count
979 * isn't strictly necessary.
980 */
981 put_device(&iommu_pdev->dev);
982
983 dev_iommu_priv_set(dev, mc->smmu);
984
7f4c9176
TR
985 return iommu_fwspec_add_ids(dev, &id, 1);
986}
987
c8cc2655
JG
988static int tegra_smmu_def_domain_type(struct device *dev)
989{
990 /*
991 * FIXME: For now we want to run all translation in IDENTITY mode, due
992 * to some device quirks. Better would be to just quirk the troubled
993 * devices.
994 */
995 return IOMMU_DOMAIN_IDENTITY;
996}
997
89184651 998static const struct iommu_ops tegra_smmu_ops = {
c8cc2655
JG
999 .identity_domain = &tegra_smmu_identity_domain,
1000 .def_domain_type = &tegra_smmu_def_domain_type,
3529375e 1001 .domain_alloc_paging = tegra_smmu_domain_alloc_paging,
b287ba73 1002 .probe_device = tegra_smmu_probe_device,
7f4c9176 1003 .device_group = tegra_smmu_device_group,
7f4c9176 1004 .of_xlate = tegra_smmu_of_xlate,
89184651 1005 .pgsize_bitmap = SZ_4K,
9a630a4b
LB
1006 .default_domain_ops = &(const struct iommu_domain_ops) {
1007 .attach_dev = tegra_smmu_attach_dev,
39f823df
RM
1008 .map_pages = tegra_smmu_map,
1009 .unmap_pages = tegra_smmu_unmap,
9a630a4b
LB
1010 .iova_to_phys = tegra_smmu_iova_to_phys,
1011 .free = tegra_smmu_domain_free,
1012 }
89184651 1013};
7a31f6f4 1014
89184651
TR
1015static void tegra_smmu_ahb_enable(void)
1016{
1017 static const struct of_device_id ahb_match[] = {
1018 { .compatible = "nvidia,tegra30-ahb", },
1019 { }
1020 };
1021 struct device_node *ahb;
7a31f6f4 1022
89184651
TR
1023 ahb = of_find_matching_node(NULL, ahb_match);
1024 if (ahb) {
1025 tegra_ahb_enable_smmu(ahb);
1026 of_node_put(ahb);
7a31f6f4 1027 }
89184651 1028}
7a31f6f4 1029
d1313e78
TR
1030static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
1031{
1032 struct tegra_smmu *smmu = s->private;
1033 unsigned int i;
1034 u32 value;
1035
1036 seq_printf(s, "swgroup enabled ASID\n");
1037 seq_printf(s, "------------------------\n");
1038
1039 for (i = 0; i < smmu->soc->num_swgroups; i++) {
1040 const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
1041 const char *status;
1042 unsigned int asid;
1043
1044 value = smmu_readl(smmu, group->reg);
1045
1046 if (value & SMMU_ASID_ENABLE)
1047 status = "yes";
1048 else
1049 status = "no";
1050
1051 asid = value & SMMU_ASID_MASK;
1052
1053 seq_printf(s, "%-9s %-7s %#04x\n", group->name, status,
1054 asid);
1055 }
1056
1057 return 0;
1058}
1059
062e52a5 1060DEFINE_SHOW_ATTRIBUTE(tegra_smmu_swgroups);
d1313e78
TR
1061
1062static int tegra_smmu_clients_show(struct seq_file *s, void *data)
1063{
1064 struct tegra_smmu *smmu = s->private;
1065 unsigned int i;
1066 u32 value;
1067
1068 seq_printf(s, "client enabled\n");
1069 seq_printf(s, "--------------------\n");
1070
1071 for (i = 0; i < smmu->soc->num_clients; i++) {
1072 const struct tegra_mc_client *client = &smmu->soc->clients[i];
1073 const char *status;
1074
4f1ac76e 1075 value = smmu_readl(smmu, client->regs.smmu.reg);
d1313e78 1076
4f1ac76e 1077 if (value & BIT(client->regs.smmu.bit))
d1313e78
TR
1078 status = "yes";
1079 else
1080 status = "no";
1081
1082 seq_printf(s, "%-12s %s\n", client->name, status);
1083 }
1084
1085 return 0;
1086}
1087
062e52a5 1088DEFINE_SHOW_ATTRIBUTE(tegra_smmu_clients);
d1313e78
TR
1089
1090static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
1091{
1092 smmu->debugfs = debugfs_create_dir("smmu", NULL);
d1313e78
TR
1093
1094 debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
1095 &tegra_smmu_swgroups_fops);
1096 debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
1097 &tegra_smmu_clients_fops);
1098}
1099
1100static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
1101{
1102 debugfs_remove_recursive(smmu->debugfs);
1103}
1104
89184651
TR
1105struct tegra_smmu *tegra_smmu_probe(struct device *dev,
1106 const struct tegra_smmu_soc *soc,
1107 struct tegra_mc *mc)
1108{
1109 struct tegra_smmu *smmu;
89184651
TR
1110 u32 value;
1111 int err;
7a31f6f4 1112
89184651
TR
1113 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1114 if (!smmu)
1115 return ERR_PTR(-ENOMEM);
0760e8fa 1116
765a9d1d
NC
1117 /*
1118 * This is a bit of a hack. Ideally we'd want to simply return this
48a7c508
RM
1119 * value. However iommu_device_register() will attempt to add
1120 * all devices to the IOMMU before we get that far. In order
765a9d1d
NC
1121 * not to rely on global variables to track the IOMMU instance, we
1122 * set it here so that it can be looked up from the .probe_device()
1123 * callback via the IOMMU device's .drvdata field.
1124 */
1125 mc->smmu = smmu;
1126
89374244 1127 smmu->asids = devm_bitmap_zalloc(dev, soc->num_asids, GFP_KERNEL);
89184651
TR
1128 if (!smmu->asids)
1129 return ERR_PTR(-ENOMEM);
7a31f6f4 1130
7f4c9176 1131 INIT_LIST_HEAD(&smmu->groups);
89184651 1132 mutex_init(&smmu->lock);
7a31f6f4 1133
89184651
TR
1134 smmu->regs = mc->regs;
1135 smmu->soc = soc;
1136 smmu->dev = dev;
1137 smmu->mc = mc;
7a31f6f4 1138
82fa58e8
NC
1139 smmu->pfn_mask =
1140 BIT_MASK(mc->soc->num_address_bits - SMMU_PTE_SHIFT) - 1;
804cb54c
TR
1141 dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
1142 mc->soc->num_address_bits, smmu->pfn_mask);
d5c152c3 1143 smmu->tlb_mask = (1 << fls(smmu->soc->num_tlb_lines)) - 1;
11cec15b
TR
1144 dev_dbg(dev, "TLB lines: %u, mask: %#lx\n", smmu->soc->num_tlb_lines,
1145 smmu->tlb_mask);
804cb54c 1146
89184651 1147 value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
7a31f6f4 1148
89184651
TR
1149 if (soc->supports_request_limit)
1150 value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
39abf8aa 1151
89184651 1152 smmu_writel(smmu, value, SMMU_PTC_CONFIG);
7a31f6f4 1153
89184651 1154 value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
11cec15b 1155 SMMU_TLB_CONFIG_ACTIVE_LINES(smmu);
7a31f6f4 1156
89184651
TR
1157 if (soc->supports_round_robin_arbitration)
1158 value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
7a31f6f4 1159
89184651 1160 smmu_writel(smmu, value, SMMU_TLB_CONFIG);
7a31f6f4 1161
b8fe0382 1162 smmu_flush_ptc_all(smmu);
89184651
TR
1163 smmu_flush_tlb(smmu);
1164 smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
1165 smmu_flush(smmu);
1166
1167 tegra_smmu_ahb_enable();
7a31f6f4 1168
0b480e44
JR
1169 err = iommu_device_sysfs_add(&smmu->iommu, dev, NULL, dev_name(dev));
1170 if (err)
1171 return ERR_PTR(err);
1172
2d471b20 1173 err = iommu_device_register(&smmu->iommu, &tegra_smmu_ops, dev);
48a7c508
RM
1174 if (err) {
1175 iommu_device_sysfs_remove(&smmu->iommu);
1176 return ERR_PTR(err);
1177 }
96302d89 1178
d1313e78
TR
1179 if (IS_ENABLED(CONFIG_DEBUG_FS))
1180 tegra_smmu_debugfs_init(smmu);
1181
89184651
TR
1182 return smmu;
1183}
d1313e78
TR
1184
1185void tegra_smmu_remove(struct tegra_smmu *smmu)
1186{
0b480e44
JR
1187 iommu_device_unregister(&smmu->iommu);
1188 iommu_device_sysfs_remove(&smmu->iommu);
1189
d1313e78
TR
1190 if (IS_ENABLED(CONFIG_DEBUG_FS))
1191 tegra_smmu_debugfs_exit(smmu);
1192}