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