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