Linux 4.19.133
[linux/fpc-iii.git] / drivers / iommu / tegra-smmu.c
blobfa0ecb5e6380998691ad8d3f06f2c2cd898043b8
1 /*
2 * Copyright (C) 2011-2014 NVIDIA CORPORATION. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <linux/bitops.h>
10 #include <linux/debugfs.h>
11 #include <linux/err.h>
12 #include <linux/iommu.h>
13 #include <linux/kernel.h>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/dma-mapping.h>
20 #include <soc/tegra/ahb.h>
21 #include <soc/tegra/mc.h>
23 struct tegra_smmu_group {
24 struct list_head list;
25 const struct tegra_smmu_group_soc *soc;
26 struct iommu_group *group;
29 struct tegra_smmu {
30 void __iomem *regs;
31 struct device *dev;
33 struct tegra_mc *mc;
34 const struct tegra_smmu_soc *soc;
36 struct list_head groups;
38 unsigned long pfn_mask;
39 unsigned long tlb_mask;
41 unsigned long *asids;
42 struct mutex lock;
44 struct list_head list;
46 struct dentry *debugfs;
48 struct iommu_device iommu; /* IOMMU Core code handle */
51 struct tegra_smmu_as {
52 struct iommu_domain domain;
53 struct tegra_smmu *smmu;
54 unsigned int use_count;
55 u32 *count;
56 struct page **pts;
57 struct page *pd;
58 dma_addr_t pd_dma;
59 unsigned id;
60 u32 attr;
63 static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom)
65 return container_of(dom, struct tegra_smmu_as, domain);
68 static inline void smmu_writel(struct tegra_smmu *smmu, u32 value,
69 unsigned long offset)
71 writel(value, smmu->regs + offset);
74 static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset)
76 return readl(smmu->regs + offset);
79 #define SMMU_CONFIG 0x010
80 #define SMMU_CONFIG_ENABLE (1 << 0)
82 #define SMMU_TLB_CONFIG 0x14
83 #define SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29)
84 #define SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28)
85 #define SMMU_TLB_CONFIG_ACTIVE_LINES(smmu) \
86 ((smmu)->soc->num_tlb_lines & (smmu)->tlb_mask)
88 #define SMMU_PTC_CONFIG 0x18
89 #define SMMU_PTC_CONFIG_ENABLE (1 << 29)
90 #define SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24)
91 #define SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f)
93 #define SMMU_PTB_ASID 0x01c
94 #define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
96 #define SMMU_PTB_DATA 0x020
97 #define SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr))
99 #define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr))
101 #define SMMU_TLB_FLUSH 0x030
102 #define SMMU_TLB_FLUSH_VA_MATCH_ALL (0 << 0)
103 #define SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0)
104 #define SMMU_TLB_FLUSH_VA_MATCH_GROUP (3 << 0)
105 #define SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
106 SMMU_TLB_FLUSH_VA_MATCH_SECTION)
107 #define SMMU_TLB_FLUSH_VA_GROUP(addr) ((((addr) & 0xffffc000) >> 12) | \
108 SMMU_TLB_FLUSH_VA_MATCH_GROUP)
109 #define SMMU_TLB_FLUSH_ASID_MATCH (1 << 31)
111 #define SMMU_PTC_FLUSH 0x034
112 #define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
113 #define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
115 #define SMMU_PTC_FLUSH_HI 0x9b8
116 #define SMMU_PTC_FLUSH_HI_MASK 0x3
118 /* per-SWGROUP SMMU_*_ASID register */
119 #define SMMU_ASID_ENABLE (1 << 31)
120 #define SMMU_ASID_MASK 0x7f
121 #define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
123 /* page table definitions */
124 #define SMMU_NUM_PDE 1024
125 #define SMMU_NUM_PTE 1024
127 #define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
128 #define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
130 #define SMMU_PDE_SHIFT 22
131 #define SMMU_PTE_SHIFT 12
133 #define SMMU_PD_READABLE (1 << 31)
134 #define SMMU_PD_WRITABLE (1 << 30)
135 #define SMMU_PD_NONSECURE (1 << 29)
137 #define SMMU_PDE_READABLE (1 << 31)
138 #define SMMU_PDE_WRITABLE (1 << 30)
139 #define SMMU_PDE_NONSECURE (1 << 29)
140 #define SMMU_PDE_NEXT (1 << 28)
142 #define SMMU_PTE_READABLE (1 << 31)
143 #define SMMU_PTE_WRITABLE (1 << 30)
144 #define SMMU_PTE_NONSECURE (1 << 29)
146 #define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
147 SMMU_PDE_NONSECURE)
148 #define SMMU_PTE_ATTR (SMMU_PTE_READABLE | SMMU_PTE_WRITABLE | \
149 SMMU_PTE_NONSECURE)
151 static unsigned int iova_pd_index(unsigned long iova)
153 return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1);
156 static unsigned int iova_pt_index(unsigned long iova)
158 return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1);
161 static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr)
163 addr >>= 12;
164 return (addr & smmu->pfn_mask) == addr;
167 static dma_addr_t smmu_pde_to_dma(struct tegra_smmu *smmu, u32 pde)
169 return (dma_addr_t)(pde & smmu->pfn_mask) << 12;
172 static void smmu_flush_ptc_all(struct tegra_smmu *smmu)
174 smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
177 static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma,
178 unsigned long offset)
180 u32 value;
182 offset &= ~(smmu->mc->soc->atom_size - 1);
184 if (smmu->mc->soc->num_address_bits > 32) {
185 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
186 value = (dma >> 32) & SMMU_PTC_FLUSH_HI_MASK;
187 #else
188 value = 0;
189 #endif
190 smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
193 value = (dma + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
194 smmu_writel(smmu, value, SMMU_PTC_FLUSH);
197 static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
199 smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
202 static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
203 unsigned long asid)
205 u32 value;
207 if (smmu->soc->num_asids == 4)
208 value = (asid & 0x3) << 29;
209 else
210 value = (asid & 0x7f) << 24;
212 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_MATCH_ALL;
213 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
216 static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
217 unsigned long asid,
218 unsigned long iova)
220 u32 value;
222 if (smmu->soc->num_asids == 4)
223 value = (asid & 0x3) << 29;
224 else
225 value = (asid & 0x7f) << 24;
227 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_SECTION(iova);
228 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
231 static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu,
232 unsigned long asid,
233 unsigned long iova)
235 u32 value;
237 if (smmu->soc->num_asids == 4)
238 value = (asid & 0x3) << 29;
239 else
240 value = (asid & 0x7f) << 24;
242 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_GROUP(iova);
243 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
246 static inline void smmu_flush(struct tegra_smmu *smmu)
248 smmu_readl(smmu, SMMU_CONFIG);
251 static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
253 unsigned long id;
255 mutex_lock(&smmu->lock);
257 id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
258 if (id >= smmu->soc->num_asids) {
259 mutex_unlock(&smmu->lock);
260 return -ENOSPC;
263 set_bit(id, smmu->asids);
264 *idp = id;
266 mutex_unlock(&smmu->lock);
267 return 0;
270 static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
272 mutex_lock(&smmu->lock);
273 clear_bit(id, smmu->asids);
274 mutex_unlock(&smmu->lock);
277 static bool tegra_smmu_capable(enum iommu_cap cap)
279 return false;
282 static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type)
284 struct tegra_smmu_as *as;
286 if (type != IOMMU_DOMAIN_UNMANAGED)
287 return NULL;
289 as = kzalloc(sizeof(*as), GFP_KERNEL);
290 if (!as)
291 return NULL;
293 as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
295 as->pd = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
296 if (!as->pd) {
297 kfree(as);
298 return NULL;
301 as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL);
302 if (!as->count) {
303 __free_page(as->pd);
304 kfree(as);
305 return NULL;
308 as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL);
309 if (!as->pts) {
310 kfree(as->count);
311 __free_page(as->pd);
312 kfree(as);
313 return NULL;
316 /* setup aperture */
317 as->domain.geometry.aperture_start = 0;
318 as->domain.geometry.aperture_end = 0xffffffff;
319 as->domain.geometry.force_aperture = true;
321 return &as->domain;
324 static void tegra_smmu_domain_free(struct iommu_domain *domain)
326 struct tegra_smmu_as *as = to_smmu_as(domain);
328 /* TODO: free page directory and page tables */
330 kfree(as);
333 static const struct tegra_smmu_swgroup *
334 tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
336 const struct tegra_smmu_swgroup *group = NULL;
337 unsigned int i;
339 for (i = 0; i < smmu->soc->num_swgroups; i++) {
340 if (smmu->soc->swgroups[i].swgroup == swgroup) {
341 group = &smmu->soc->swgroups[i];
342 break;
346 return group;
349 static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
350 unsigned int asid)
352 const struct tegra_smmu_swgroup *group;
353 unsigned int i;
354 u32 value;
356 for (i = 0; i < smmu->soc->num_clients; i++) {
357 const struct tegra_mc_client *client = &smmu->soc->clients[i];
359 if (client->swgroup != swgroup)
360 continue;
362 value = smmu_readl(smmu, client->smmu.reg);
363 value |= BIT(client->smmu.bit);
364 smmu_writel(smmu, value, client->smmu.reg);
367 group = tegra_smmu_find_swgroup(smmu, swgroup);
368 if (group) {
369 value = smmu_readl(smmu, group->reg);
370 value &= ~SMMU_ASID_MASK;
371 value |= SMMU_ASID_VALUE(asid);
372 value |= SMMU_ASID_ENABLE;
373 smmu_writel(smmu, value, group->reg);
377 static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
378 unsigned int asid)
380 const struct tegra_smmu_swgroup *group;
381 unsigned int i;
382 u32 value;
384 group = tegra_smmu_find_swgroup(smmu, swgroup);
385 if (group) {
386 value = smmu_readl(smmu, group->reg);
387 value &= ~SMMU_ASID_MASK;
388 value |= SMMU_ASID_VALUE(asid);
389 value &= ~SMMU_ASID_ENABLE;
390 smmu_writel(smmu, value, group->reg);
393 for (i = 0; i < smmu->soc->num_clients; i++) {
394 const struct tegra_mc_client *client = &smmu->soc->clients[i];
396 if (client->swgroup != swgroup)
397 continue;
399 value = smmu_readl(smmu, client->smmu.reg);
400 value &= ~BIT(client->smmu.bit);
401 smmu_writel(smmu, value, client->smmu.reg);
405 static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
406 struct tegra_smmu_as *as)
408 u32 value;
409 int err;
411 if (as->use_count > 0) {
412 as->use_count++;
413 return 0;
416 as->pd_dma = dma_map_page(smmu->dev, as->pd, 0, SMMU_SIZE_PD,
417 DMA_TO_DEVICE);
418 if (dma_mapping_error(smmu->dev, as->pd_dma))
419 return -ENOMEM;
421 /* We can't handle 64-bit DMA addresses */
422 if (!smmu_dma_addr_valid(smmu, as->pd_dma)) {
423 err = -ENOMEM;
424 goto err_unmap;
427 err = tegra_smmu_alloc_asid(smmu, &as->id);
428 if (err < 0)
429 goto err_unmap;
431 smmu_flush_ptc(smmu, as->pd_dma, 0);
432 smmu_flush_tlb_asid(smmu, as->id);
434 smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
435 value = SMMU_PTB_DATA_VALUE(as->pd_dma, as->attr);
436 smmu_writel(smmu, value, SMMU_PTB_DATA);
437 smmu_flush(smmu);
439 as->smmu = smmu;
440 as->use_count++;
442 return 0;
444 err_unmap:
445 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
446 return err;
449 static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
450 struct tegra_smmu_as *as)
452 if (--as->use_count > 0)
453 return;
455 tegra_smmu_free_asid(smmu, as->id);
457 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
459 as->smmu = NULL;
462 static int tegra_smmu_attach_dev(struct iommu_domain *domain,
463 struct device *dev)
465 struct tegra_smmu *smmu = dev->archdata.iommu;
466 struct tegra_smmu_as *as = to_smmu_as(domain);
467 struct device_node *np = dev->of_node;
468 struct of_phandle_args args;
469 unsigned int index = 0;
470 int err = 0;
472 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
473 &args)) {
474 unsigned int swgroup = args.args[0];
476 if (args.np != smmu->dev->of_node) {
477 of_node_put(args.np);
478 continue;
481 of_node_put(args.np);
483 err = tegra_smmu_as_prepare(smmu, as);
484 if (err < 0)
485 return err;
487 tegra_smmu_enable(smmu, swgroup, as->id);
488 index++;
491 if (index == 0)
492 return -ENODEV;
494 return 0;
497 static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
499 struct tegra_smmu_as *as = to_smmu_as(domain);
500 struct device_node *np = dev->of_node;
501 struct tegra_smmu *smmu = as->smmu;
502 struct of_phandle_args args;
503 unsigned int index = 0;
505 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
506 &args)) {
507 unsigned int swgroup = args.args[0];
509 if (args.np != smmu->dev->of_node) {
510 of_node_put(args.np);
511 continue;
514 of_node_put(args.np);
516 tegra_smmu_disable(smmu, swgroup, as->id);
517 tegra_smmu_as_unprepare(smmu, as);
518 index++;
522 static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova,
523 u32 value)
525 unsigned int pd_index = iova_pd_index(iova);
526 struct tegra_smmu *smmu = as->smmu;
527 u32 *pd = page_address(as->pd);
528 unsigned long offset = pd_index * sizeof(*pd);
530 /* Set the page directory entry first */
531 pd[pd_index] = value;
533 /* The flush the page directory entry from caches */
534 dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset,
535 sizeof(*pd), DMA_TO_DEVICE);
537 /* And flush the iommu */
538 smmu_flush_ptc(smmu, as->pd_dma, offset);
539 smmu_flush_tlb_section(smmu, as->id, iova);
540 smmu_flush(smmu);
543 static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova)
545 u32 *pt = page_address(pt_page);
547 return pt + iova_pt_index(iova);
550 static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
551 dma_addr_t *dmap)
553 unsigned int pd_index = iova_pd_index(iova);
554 struct tegra_smmu *smmu = as->smmu;
555 struct page *pt_page;
556 u32 *pd;
558 pt_page = as->pts[pd_index];
559 if (!pt_page)
560 return NULL;
562 pd = page_address(as->pd);
563 *dmap = smmu_pde_to_dma(smmu, pd[pd_index]);
565 return tegra_smmu_pte_offset(pt_page, iova);
568 static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
569 dma_addr_t *dmap)
571 unsigned int pde = iova_pd_index(iova);
572 struct tegra_smmu *smmu = as->smmu;
574 if (!as->pts[pde]) {
575 struct page *page;
576 dma_addr_t dma;
578 page = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
579 if (!page)
580 return NULL;
582 dma = dma_map_page(smmu->dev, page, 0, SMMU_SIZE_PT,
583 DMA_TO_DEVICE);
584 if (dma_mapping_error(smmu->dev, dma)) {
585 __free_page(page);
586 return NULL;
589 if (!smmu_dma_addr_valid(smmu, dma)) {
590 dma_unmap_page(smmu->dev, dma, SMMU_SIZE_PT,
591 DMA_TO_DEVICE);
592 __free_page(page);
593 return NULL;
596 as->pts[pde] = page;
598 tegra_smmu_set_pde(as, iova, SMMU_MK_PDE(dma, SMMU_PDE_ATTR |
599 SMMU_PDE_NEXT));
601 *dmap = dma;
602 } else {
603 u32 *pd = page_address(as->pd);
605 *dmap = smmu_pde_to_dma(smmu, pd[pde]);
608 return tegra_smmu_pte_offset(as->pts[pde], iova);
611 static void tegra_smmu_pte_get_use(struct tegra_smmu_as *as, unsigned long iova)
613 unsigned int pd_index = iova_pd_index(iova);
615 as->count[pd_index]++;
618 static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
620 unsigned int pde = iova_pd_index(iova);
621 struct page *page = as->pts[pde];
624 * When no entries in this page table are used anymore, return the
625 * memory page to the system.
627 if (--as->count[pde] == 0) {
628 struct tegra_smmu *smmu = as->smmu;
629 u32 *pd = page_address(as->pd);
630 dma_addr_t pte_dma = smmu_pde_to_dma(smmu, pd[pde]);
632 tegra_smmu_set_pde(as, iova, 0);
634 dma_unmap_page(smmu->dev, pte_dma, SMMU_SIZE_PT, DMA_TO_DEVICE);
635 __free_page(page);
636 as->pts[pde] = NULL;
640 static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
641 u32 *pte, dma_addr_t pte_dma, u32 val)
643 struct tegra_smmu *smmu = as->smmu;
644 unsigned long offset = offset_in_page(pte);
646 *pte = val;
648 dma_sync_single_range_for_device(smmu->dev, pte_dma, offset,
649 4, DMA_TO_DEVICE);
650 smmu_flush_ptc(smmu, pte_dma, offset);
651 smmu_flush_tlb_group(smmu, as->id, iova);
652 smmu_flush(smmu);
655 static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
656 phys_addr_t paddr, size_t size, int prot)
658 struct tegra_smmu_as *as = to_smmu_as(domain);
659 dma_addr_t pte_dma;
660 u32 *pte;
662 pte = as_get_pte(as, iova, &pte_dma);
663 if (!pte)
664 return -ENOMEM;
666 /* If we aren't overwriting a pre-existing entry, increment use */
667 if (*pte == 0)
668 tegra_smmu_pte_get_use(as, iova);
670 tegra_smmu_set_pte(as, iova, pte, pte_dma,
671 __phys_to_pfn(paddr) | SMMU_PTE_ATTR);
673 return 0;
676 static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
677 size_t size)
679 struct tegra_smmu_as *as = to_smmu_as(domain);
680 dma_addr_t pte_dma;
681 u32 *pte;
683 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
684 if (!pte || !*pte)
685 return 0;
687 tegra_smmu_set_pte(as, iova, pte, pte_dma, 0);
688 tegra_smmu_pte_put_use(as, iova);
690 return size;
693 static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
694 dma_addr_t iova)
696 struct tegra_smmu_as *as = to_smmu_as(domain);
697 unsigned long pfn;
698 dma_addr_t pte_dma;
699 u32 *pte;
701 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
702 if (!pte || !*pte)
703 return 0;
705 pfn = *pte & as->smmu->pfn_mask;
707 return PFN_PHYS(pfn);
710 static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
712 struct platform_device *pdev;
713 struct tegra_mc *mc;
715 pdev = of_find_device_by_node(np);
716 if (!pdev)
717 return NULL;
719 mc = platform_get_drvdata(pdev);
720 if (!mc)
721 return NULL;
723 return mc->smmu;
726 static int tegra_smmu_configure(struct tegra_smmu *smmu, struct device *dev,
727 struct of_phandle_args *args)
729 const struct iommu_ops *ops = smmu->iommu.ops;
730 int err;
732 err = iommu_fwspec_init(dev, &dev->of_node->fwnode, ops);
733 if (err < 0) {
734 dev_err(dev, "failed to initialize fwspec: %d\n", err);
735 return err;
738 err = ops->of_xlate(dev, args);
739 if (err < 0) {
740 dev_err(dev, "failed to parse SW group ID: %d\n", err);
741 iommu_fwspec_free(dev);
742 return err;
745 return 0;
748 static int tegra_smmu_add_device(struct device *dev)
750 struct device_node *np = dev->of_node;
751 struct tegra_smmu *smmu = NULL;
752 struct iommu_group *group;
753 struct of_phandle_args args;
754 unsigned int index = 0;
755 int err;
757 while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
758 &args) == 0) {
759 smmu = tegra_smmu_find(args.np);
760 if (smmu) {
761 err = tegra_smmu_configure(smmu, dev, &args);
762 of_node_put(args.np);
764 if (err < 0)
765 return err;
768 * Only a single IOMMU master interface is currently
769 * supported by the Linux kernel, so abort after the
770 * first match.
772 dev->archdata.iommu = smmu;
774 iommu_device_link(&smmu->iommu, dev);
776 break;
779 of_node_put(args.np);
780 index++;
783 if (!smmu)
784 return -ENODEV;
786 group = iommu_group_get_for_dev(dev);
787 if (IS_ERR(group))
788 return PTR_ERR(group);
790 iommu_group_put(group);
792 return 0;
795 static void tegra_smmu_remove_device(struct device *dev)
797 struct tegra_smmu *smmu = dev->archdata.iommu;
799 if (smmu)
800 iommu_device_unlink(&smmu->iommu, dev);
802 dev->archdata.iommu = NULL;
803 iommu_group_remove_device(dev);
806 static const struct tegra_smmu_group_soc *
807 tegra_smmu_find_group(struct tegra_smmu *smmu, unsigned int swgroup)
809 unsigned int i, j;
811 for (i = 0; i < smmu->soc->num_groups; i++)
812 for (j = 0; j < smmu->soc->groups[i].num_swgroups; j++)
813 if (smmu->soc->groups[i].swgroups[j] == swgroup)
814 return &smmu->soc->groups[i];
816 return NULL;
819 static struct iommu_group *tegra_smmu_group_get(struct tegra_smmu *smmu,
820 unsigned int swgroup)
822 const struct tegra_smmu_group_soc *soc;
823 struct tegra_smmu_group *group;
825 soc = tegra_smmu_find_group(smmu, swgroup);
826 if (!soc)
827 return NULL;
829 mutex_lock(&smmu->lock);
831 list_for_each_entry(group, &smmu->groups, list)
832 if (group->soc == soc) {
833 mutex_unlock(&smmu->lock);
834 return group->group;
837 group = devm_kzalloc(smmu->dev, sizeof(*group), GFP_KERNEL);
838 if (!group) {
839 mutex_unlock(&smmu->lock);
840 return NULL;
843 INIT_LIST_HEAD(&group->list);
844 group->soc = soc;
846 group->group = iommu_group_alloc();
847 if (IS_ERR(group->group)) {
848 devm_kfree(smmu->dev, group);
849 mutex_unlock(&smmu->lock);
850 return NULL;
853 list_add_tail(&group->list, &smmu->groups);
854 mutex_unlock(&smmu->lock);
856 return group->group;
859 static struct iommu_group *tegra_smmu_device_group(struct device *dev)
861 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
862 struct tegra_smmu *smmu = dev->archdata.iommu;
863 struct iommu_group *group;
865 group = tegra_smmu_group_get(smmu, fwspec->ids[0]);
866 if (!group)
867 group = generic_device_group(dev);
869 return group;
872 static int tegra_smmu_of_xlate(struct device *dev,
873 struct of_phandle_args *args)
875 u32 id = args->args[0];
877 return iommu_fwspec_add_ids(dev, &id, 1);
880 static const struct iommu_ops tegra_smmu_ops = {
881 .capable = tegra_smmu_capable,
882 .domain_alloc = tegra_smmu_domain_alloc,
883 .domain_free = tegra_smmu_domain_free,
884 .attach_dev = tegra_smmu_attach_dev,
885 .detach_dev = tegra_smmu_detach_dev,
886 .add_device = tegra_smmu_add_device,
887 .remove_device = tegra_smmu_remove_device,
888 .device_group = tegra_smmu_device_group,
889 .map = tegra_smmu_map,
890 .unmap = tegra_smmu_unmap,
891 .iova_to_phys = tegra_smmu_iova_to_phys,
892 .of_xlate = tegra_smmu_of_xlate,
893 .pgsize_bitmap = SZ_4K,
896 static void tegra_smmu_ahb_enable(void)
898 static const struct of_device_id ahb_match[] = {
899 { .compatible = "nvidia,tegra30-ahb", },
902 struct device_node *ahb;
904 ahb = of_find_matching_node(NULL, ahb_match);
905 if (ahb) {
906 tegra_ahb_enable_smmu(ahb);
907 of_node_put(ahb);
911 static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
913 struct tegra_smmu *smmu = s->private;
914 unsigned int i;
915 u32 value;
917 seq_printf(s, "swgroup enabled ASID\n");
918 seq_printf(s, "------------------------\n");
920 for (i = 0; i < smmu->soc->num_swgroups; i++) {
921 const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
922 const char *status;
923 unsigned int asid;
925 value = smmu_readl(smmu, group->reg);
927 if (value & SMMU_ASID_ENABLE)
928 status = "yes";
929 else
930 status = "no";
932 asid = value & SMMU_ASID_MASK;
934 seq_printf(s, "%-9s %-7s %#04x\n", group->name, status,
935 asid);
938 return 0;
941 static int tegra_smmu_swgroups_open(struct inode *inode, struct file *file)
943 return single_open(file, tegra_smmu_swgroups_show, inode->i_private);
946 static const struct file_operations tegra_smmu_swgroups_fops = {
947 .open = tegra_smmu_swgroups_open,
948 .read = seq_read,
949 .llseek = seq_lseek,
950 .release = single_release,
953 static int tegra_smmu_clients_show(struct seq_file *s, void *data)
955 struct tegra_smmu *smmu = s->private;
956 unsigned int i;
957 u32 value;
959 seq_printf(s, "client enabled\n");
960 seq_printf(s, "--------------------\n");
962 for (i = 0; i < smmu->soc->num_clients; i++) {
963 const struct tegra_mc_client *client = &smmu->soc->clients[i];
964 const char *status;
966 value = smmu_readl(smmu, client->smmu.reg);
968 if (value & BIT(client->smmu.bit))
969 status = "yes";
970 else
971 status = "no";
973 seq_printf(s, "%-12s %s\n", client->name, status);
976 return 0;
979 static int tegra_smmu_clients_open(struct inode *inode, struct file *file)
981 return single_open(file, tegra_smmu_clients_show, inode->i_private);
984 static const struct file_operations tegra_smmu_clients_fops = {
985 .open = tegra_smmu_clients_open,
986 .read = seq_read,
987 .llseek = seq_lseek,
988 .release = single_release,
991 static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
993 smmu->debugfs = debugfs_create_dir("smmu", NULL);
994 if (!smmu->debugfs)
995 return;
997 debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
998 &tegra_smmu_swgroups_fops);
999 debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
1000 &tegra_smmu_clients_fops);
1003 static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
1005 debugfs_remove_recursive(smmu->debugfs);
1008 struct tegra_smmu *tegra_smmu_probe(struct device *dev,
1009 const struct tegra_smmu_soc *soc,
1010 struct tegra_mc *mc)
1012 struct tegra_smmu *smmu;
1013 size_t size;
1014 u32 value;
1015 int err;
1017 /* This can happen on Tegra20 which doesn't have an SMMU */
1018 if (!soc)
1019 return NULL;
1021 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1022 if (!smmu)
1023 return ERR_PTR(-ENOMEM);
1026 * This is a bit of a hack. Ideally we'd want to simply return this
1027 * value. However the IOMMU registration process will attempt to add
1028 * all devices to the IOMMU when bus_set_iommu() is called. In order
1029 * not to rely on global variables to track the IOMMU instance, we
1030 * set it here so that it can be looked up from the .add_device()
1031 * callback via the IOMMU device's .drvdata field.
1033 mc->smmu = smmu;
1035 size = BITS_TO_LONGS(soc->num_asids) * sizeof(long);
1037 smmu->asids = devm_kzalloc(dev, size, GFP_KERNEL);
1038 if (!smmu->asids)
1039 return ERR_PTR(-ENOMEM);
1041 INIT_LIST_HEAD(&smmu->groups);
1042 mutex_init(&smmu->lock);
1044 smmu->regs = mc->regs;
1045 smmu->soc = soc;
1046 smmu->dev = dev;
1047 smmu->mc = mc;
1049 smmu->pfn_mask = BIT_MASK(mc->soc->num_address_bits - PAGE_SHIFT) - 1;
1050 dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
1051 mc->soc->num_address_bits, smmu->pfn_mask);
1052 smmu->tlb_mask = (smmu->soc->num_tlb_lines << 1) - 1;
1053 dev_dbg(dev, "TLB lines: %u, mask: %#lx\n", smmu->soc->num_tlb_lines,
1054 smmu->tlb_mask);
1056 value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
1058 if (soc->supports_request_limit)
1059 value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
1061 smmu_writel(smmu, value, SMMU_PTC_CONFIG);
1063 value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
1064 SMMU_TLB_CONFIG_ACTIVE_LINES(smmu);
1066 if (soc->supports_round_robin_arbitration)
1067 value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
1069 smmu_writel(smmu, value, SMMU_TLB_CONFIG);
1071 smmu_flush_ptc_all(smmu);
1072 smmu_flush_tlb(smmu);
1073 smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
1074 smmu_flush(smmu);
1076 tegra_smmu_ahb_enable();
1078 err = iommu_device_sysfs_add(&smmu->iommu, dev, NULL, dev_name(dev));
1079 if (err)
1080 return ERR_PTR(err);
1082 iommu_device_set_ops(&smmu->iommu, &tegra_smmu_ops);
1083 iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
1085 err = iommu_device_register(&smmu->iommu);
1086 if (err) {
1087 iommu_device_sysfs_remove(&smmu->iommu);
1088 return ERR_PTR(err);
1091 err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
1092 if (err < 0) {
1093 iommu_device_unregister(&smmu->iommu);
1094 iommu_device_sysfs_remove(&smmu->iommu);
1095 return ERR_PTR(err);
1098 if (IS_ENABLED(CONFIG_DEBUG_FS))
1099 tegra_smmu_debugfs_init(smmu);
1101 return smmu;
1104 void tegra_smmu_remove(struct tegra_smmu *smmu)
1106 iommu_device_unregister(&smmu->iommu);
1107 iommu_device_sysfs_remove(&smmu->iommu);
1109 if (IS_ENABLED(CONFIG_DEBUG_FS))
1110 tegra_smmu_debugfs_exit(smmu);