1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011-2014 NVIDIA CORPORATION. All rights reserved.
6 #include <linux/bitops.h>
7 #include <linux/debugfs.h>
9 #include <linux/iommu.h>
10 #include <linux/kernel.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>
19 #include <soc/tegra/ahb.h>
20 #include <soc/tegra/mc.h>
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;
35 const struct tegra_smmu_soc *soc;
37 struct list_head groups;
39 unsigned long pfn_mask;
40 unsigned long tlb_mask;
45 struct list_head list;
47 struct dentry *debugfs;
49 struct iommu_device iommu; /* IOMMU Core code handle */
52 struct tegra_smmu_as {
53 struct iommu_domain domain;
54 struct tegra_smmu *smmu;
55 unsigned int use_count;
65 static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom)
67 return container_of(dom, struct tegra_smmu_as, domain);
70 static inline void smmu_writel(struct tegra_smmu *smmu, u32 value,
73 writel(value, smmu->regs + offset);
76 static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset)
78 return readl(smmu->regs + offset);
81 #define SMMU_CONFIG 0x010
82 #define SMMU_CONFIG_ENABLE (1 << 0)
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)
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)
95 #define SMMU_PTB_ASID 0x01c
96 #define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
98 #define SMMU_PTB_DATA 0x020
99 #define SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr))
101 #define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr))
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)
113 #define SMMU_PTC_FLUSH 0x034
114 #define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
115 #define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
117 #define SMMU_PTC_FLUSH_HI 0x9b8
118 #define SMMU_PTC_FLUSH_HI_MASK 0x3
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)
125 /* page table definitions */
126 #define SMMU_NUM_PDE 1024
127 #define SMMU_NUM_PTE 1024
129 #define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
130 #define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
132 #define SMMU_PDE_SHIFT 22
133 #define SMMU_PTE_SHIFT 12
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))
140 #define SMMU_PD_READABLE (1 << 31)
141 #define SMMU_PD_WRITABLE (1 << 30)
142 #define SMMU_PD_NONSECURE (1 << 29)
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)
149 #define SMMU_PTE_READABLE (1 << 31)
150 #define SMMU_PTE_WRITABLE (1 << 30)
151 #define SMMU_PTE_NONSECURE (1 << 29)
153 #define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
156 static unsigned int iova_pd_index(unsigned long iova)
158 return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1);
161 static unsigned int iova_pt_index(unsigned long iova)
163 return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1);
166 static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr)
169 return (addr & smmu->pfn_mask) == addr;
172 static dma_addr_t smmu_pde_to_dma(struct tegra_smmu *smmu, u32 pde)
174 return (dma_addr_t)(pde & smmu->pfn_mask) << 12;
177 static void smmu_flush_ptc_all(struct tegra_smmu *smmu)
179 smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
182 static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma,
183 unsigned long offset)
187 offset &= ~(smmu->mc->soc->atom_size - 1);
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;
195 smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
198 value = (dma + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
199 smmu_writel(smmu, value, SMMU_PTC_FLUSH);
202 static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
204 smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
207 static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
212 if (smmu->soc->num_asids == 4)
213 value = (asid & 0x3) << 29;
215 value = (asid & 0x7f) << 24;
217 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_MATCH_ALL;
218 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
221 static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
227 if (smmu->soc->num_asids == 4)
228 value = (asid & 0x3) << 29;
230 value = (asid & 0x7f) << 24;
232 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_SECTION(iova);
233 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
236 static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu,
242 if (smmu->soc->num_asids == 4)
243 value = (asid & 0x3) << 29;
245 value = (asid & 0x7f) << 24;
247 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_GROUP(iova);
248 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
251 static inline void smmu_flush(struct tegra_smmu *smmu)
253 smmu_readl(smmu, SMMU_PTB_ASID);
256 static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
260 id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
261 if (id >= smmu->soc->num_asids)
264 set_bit(id, smmu->asids);
270 static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
272 clear_bit(id, smmu->asids);
275 static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type)
277 struct tegra_smmu_as *as;
279 if (type != IOMMU_DOMAIN_UNMANAGED)
282 as = kzalloc(sizeof(*as), GFP_KERNEL);
286 as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
288 as->pd = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
294 as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL);
301 as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL);
309 spin_lock_init(&as->lock);
312 as->domain.geometry.aperture_start = 0;
313 as->domain.geometry.aperture_end = 0xffffffff;
314 as->domain.geometry.force_aperture = true;
319 static void tegra_smmu_domain_free(struct iommu_domain *domain)
321 struct tegra_smmu_as *as = to_smmu_as(domain);
323 /* TODO: free page directory and page tables */
325 WARN_ON_ONCE(as->use_count);
331 static const struct tegra_smmu_swgroup *
332 tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
334 const struct tegra_smmu_swgroup *group = NULL;
337 for (i = 0; i < smmu->soc->num_swgroups; i++) {
338 if (smmu->soc->swgroups[i].swgroup == swgroup) {
339 group = &smmu->soc->swgroups[i];
347 static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
350 const struct tegra_smmu_swgroup *group;
354 group = tegra_smmu_find_swgroup(smmu, swgroup);
356 value = smmu_readl(smmu, group->reg);
357 value &= ~SMMU_ASID_MASK;
358 value |= SMMU_ASID_VALUE(asid);
359 value |= SMMU_ASID_ENABLE;
360 smmu_writel(smmu, value, group->reg);
362 pr_warn("%s group from swgroup %u not found\n", __func__,
364 /* No point moving ahead if group was not found */
368 for (i = 0; i < smmu->soc->num_clients; i++) {
369 const struct tegra_mc_client *client = &smmu->soc->clients[i];
371 if (client->swgroup != swgroup)
374 value = smmu_readl(smmu, client->regs.smmu.reg);
375 value |= BIT(client->regs.smmu.bit);
376 smmu_writel(smmu, value, client->regs.smmu.reg);
380 static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
383 const struct tegra_smmu_swgroup *group;
387 group = tegra_smmu_find_swgroup(smmu, swgroup);
389 value = smmu_readl(smmu, group->reg);
390 value &= ~SMMU_ASID_MASK;
391 value |= SMMU_ASID_VALUE(asid);
392 value &= ~SMMU_ASID_ENABLE;
393 smmu_writel(smmu, value, group->reg);
396 for (i = 0; i < smmu->soc->num_clients; i++) {
397 const struct tegra_mc_client *client = &smmu->soc->clients[i];
399 if (client->swgroup != swgroup)
402 value = smmu_readl(smmu, client->regs.smmu.reg);
403 value &= ~BIT(client->regs.smmu.bit);
404 smmu_writel(smmu, value, client->regs.smmu.reg);
408 static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
409 struct tegra_smmu_as *as)
414 mutex_lock(&smmu->lock);
416 if (as->use_count > 0) {
421 as->pd_dma = dma_map_page(smmu->dev, as->pd, 0, SMMU_SIZE_PD,
423 if (dma_mapping_error(smmu->dev, as->pd_dma)) {
428 /* We can't handle 64-bit DMA addresses */
429 if (!smmu_dma_addr_valid(smmu, as->pd_dma)) {
434 err = tegra_smmu_alloc_asid(smmu, &as->id);
438 smmu_flush_ptc(smmu, as->pd_dma, 0);
439 smmu_flush_tlb_asid(smmu, as->id);
441 smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
442 value = SMMU_PTB_DATA_VALUE(as->pd_dma, as->attr);
443 smmu_writel(smmu, value, SMMU_PTB_DATA);
449 mutex_unlock(&smmu->lock);
454 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
456 mutex_unlock(&smmu->lock);
461 static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
462 struct tegra_smmu_as *as)
464 mutex_lock(&smmu->lock);
466 if (--as->use_count > 0) {
467 mutex_unlock(&smmu->lock);
471 tegra_smmu_free_asid(smmu, as->id);
473 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
477 mutex_unlock(&smmu->lock);
480 static int tegra_smmu_attach_dev(struct iommu_domain *domain,
483 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
484 struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
485 struct tegra_smmu_as *as = to_smmu_as(domain);
492 for (index = 0; index < fwspec->num_ids; index++) {
493 err = tegra_smmu_as_prepare(smmu, as);
497 tegra_smmu_enable(smmu, fwspec->ids[index], as->id);
507 tegra_smmu_disable(smmu, fwspec->ids[index], as->id);
508 tegra_smmu_as_unprepare(smmu, as);
514 static void tegra_smmu_set_platform_dma(struct device *dev)
516 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
517 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
518 struct tegra_smmu_as *as = to_smmu_as(domain);
519 struct tegra_smmu *smmu = as->smmu;
525 for (index = 0; index < fwspec->num_ids; index++) {
526 tegra_smmu_disable(smmu, fwspec->ids[index], as->id);
527 tegra_smmu_as_unprepare(smmu, as);
531 static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova,
534 unsigned int pd_index = iova_pd_index(iova);
535 struct tegra_smmu *smmu = as->smmu;
536 u32 *pd = page_address(as->pd);
537 unsigned long offset = pd_index * sizeof(*pd);
539 /* Set the page directory entry first */
540 pd[pd_index] = value;
542 /* The flush the page directory entry from caches */
543 dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset,
544 sizeof(*pd), DMA_TO_DEVICE);
546 /* And flush the iommu */
547 smmu_flush_ptc(smmu, as->pd_dma, offset);
548 smmu_flush_tlb_section(smmu, as->id, iova);
552 static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova)
554 u32 *pt = page_address(pt_page);
556 return pt + iova_pt_index(iova);
559 static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
562 unsigned int pd_index = iova_pd_index(iova);
563 struct tegra_smmu *smmu = as->smmu;
564 struct page *pt_page;
567 pt_page = as->pts[pd_index];
571 pd = page_address(as->pd);
572 *dmap = smmu_pde_to_dma(smmu, pd[pd_index]);
574 return tegra_smmu_pte_offset(pt_page, iova);
577 static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
578 dma_addr_t *dmap, struct page *page)
580 unsigned int pde = iova_pd_index(iova);
581 struct tegra_smmu *smmu = as->smmu;
586 dma = dma_map_page(smmu->dev, page, 0, SMMU_SIZE_PT,
588 if (dma_mapping_error(smmu->dev, dma)) {
593 if (!smmu_dma_addr_valid(smmu, dma)) {
594 dma_unmap_page(smmu->dev, dma, SMMU_SIZE_PT,
602 tegra_smmu_set_pde(as, iova, SMMU_MK_PDE(dma, SMMU_PDE_ATTR |
607 u32 *pd = page_address(as->pd);
609 *dmap = smmu_pde_to_dma(smmu, pd[pde]);
612 return tegra_smmu_pte_offset(as->pts[pde], iova);
615 static void tegra_smmu_pte_get_use(struct tegra_smmu_as *as, unsigned long iova)
617 unsigned int pd_index = iova_pd_index(iova);
619 as->count[pd_index]++;
622 static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
624 unsigned int pde = iova_pd_index(iova);
625 struct page *page = as->pts[pde];
628 * When no entries in this page table are used anymore, return the
629 * memory page to the system.
631 if (--as->count[pde] == 0) {
632 struct tegra_smmu *smmu = as->smmu;
633 u32 *pd = page_address(as->pd);
634 dma_addr_t pte_dma = smmu_pde_to_dma(smmu, pd[pde]);
636 tegra_smmu_set_pde(as, iova, 0);
638 dma_unmap_page(smmu->dev, pte_dma, SMMU_SIZE_PT, DMA_TO_DEVICE);
644 static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
645 u32 *pte, dma_addr_t pte_dma, u32 val)
647 struct tegra_smmu *smmu = as->smmu;
648 unsigned long offset = SMMU_OFFSET_IN_PAGE(pte);
652 dma_sync_single_range_for_device(smmu->dev, pte_dma, offset,
654 smmu_flush_ptc(smmu, pte_dma, offset);
655 smmu_flush_tlb_group(smmu, as->id, iova);
659 static struct page *as_get_pde_page(struct tegra_smmu_as *as,
660 unsigned long iova, gfp_t gfp,
661 unsigned long *flags)
663 unsigned int pde = iova_pd_index(iova);
664 struct page *page = as->pts[pde];
666 /* at first check whether allocation needs to be done at all */
671 * In order to prevent exhaustion of the atomic memory pool, we
672 * allocate page in a sleeping context if GFP flags permit. Hence
673 * spinlock needs to be unlocked and re-locked after allocation.
675 if (gfpflags_allow_blocking(gfp))
676 spin_unlock_irqrestore(&as->lock, *flags);
678 page = alloc_page(gfp | __GFP_DMA | __GFP_ZERO);
680 if (gfpflags_allow_blocking(gfp))
681 spin_lock_irqsave(&as->lock, *flags);
684 * In a case of blocking allocation, a concurrent mapping may win
685 * the PDE allocation. In this case the allocated page isn't needed
686 * if allocation succeeded and the allocation failure isn't fatal.
699 __tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
700 phys_addr_t paddr, size_t size, int prot, gfp_t gfp,
701 unsigned long *flags)
703 struct tegra_smmu_as *as = to_smmu_as(domain);
709 page = as_get_pde_page(as, iova, gfp, flags);
713 pte = as_get_pte(as, iova, &pte_dma, page);
717 /* If we aren't overwriting a pre-existing entry, increment use */
719 tegra_smmu_pte_get_use(as, iova);
721 pte_attrs = SMMU_PTE_NONSECURE;
723 if (prot & IOMMU_READ)
724 pte_attrs |= SMMU_PTE_READABLE;
726 if (prot & IOMMU_WRITE)
727 pte_attrs |= SMMU_PTE_WRITABLE;
729 tegra_smmu_set_pte(as, iova, pte, pte_dma,
730 SMMU_PHYS_PFN(paddr) | pte_attrs);
736 __tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
737 size_t size, struct iommu_iotlb_gather *gather)
739 struct tegra_smmu_as *as = to_smmu_as(domain);
743 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
747 tegra_smmu_set_pte(as, iova, pte, pte_dma, 0);
748 tegra_smmu_pte_put_use(as, iova);
753 static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
754 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
756 struct tegra_smmu_as *as = to_smmu_as(domain);
760 spin_lock_irqsave(&as->lock, flags);
761 ret = __tegra_smmu_map(domain, iova, paddr, size, prot, gfp, &flags);
762 spin_unlock_irqrestore(&as->lock, flags);
767 static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
768 size_t size, struct iommu_iotlb_gather *gather)
770 struct tegra_smmu_as *as = to_smmu_as(domain);
773 spin_lock_irqsave(&as->lock, flags);
774 size = __tegra_smmu_unmap(domain, iova, size, gather);
775 spin_unlock_irqrestore(&as->lock, flags);
780 static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
783 struct tegra_smmu_as *as = to_smmu_as(domain);
788 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
792 pfn = *pte & as->smmu->pfn_mask;
794 return SMMU_PFN_PHYS(pfn) + SMMU_OFFSET_IN_PAGE(iova);
797 static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
799 struct platform_device *pdev;
802 pdev = of_find_device_by_node(np);
806 mc = platform_get_drvdata(pdev);
808 put_device(&pdev->dev);
815 static int tegra_smmu_configure(struct tegra_smmu *smmu, struct device *dev,
816 struct of_phandle_args *args)
818 const struct iommu_ops *ops = smmu->iommu.ops;
821 err = iommu_fwspec_init(dev, &dev->of_node->fwnode, ops);
823 dev_err(dev, "failed to initialize fwspec: %d\n", err);
827 err = ops->of_xlate(dev, args);
829 dev_err(dev, "failed to parse SW group ID: %d\n", err);
830 iommu_fwspec_free(dev);
837 static struct iommu_device *tegra_smmu_probe_device(struct device *dev)
839 struct device_node *np = dev->of_node;
840 struct tegra_smmu *smmu = NULL;
841 struct of_phandle_args args;
842 unsigned int index = 0;
845 while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
847 smmu = tegra_smmu_find(args.np);
849 err = tegra_smmu_configure(smmu, dev, &args);
852 of_node_put(args.np);
857 of_node_put(args.np);
861 smmu = dev_iommu_priv_get(dev);
863 return ERR_PTR(-ENODEV);
868 static const struct tegra_smmu_group_soc *
869 tegra_smmu_find_group(struct tegra_smmu *smmu, unsigned int swgroup)
873 for (i = 0; i < smmu->soc->num_groups; i++)
874 for (j = 0; j < smmu->soc->groups[i].num_swgroups; j++)
875 if (smmu->soc->groups[i].swgroups[j] == swgroup)
876 return &smmu->soc->groups[i];
881 static void tegra_smmu_group_release(void *iommu_data)
883 struct tegra_smmu_group *group = iommu_data;
884 struct tegra_smmu *smmu = group->smmu;
886 mutex_lock(&smmu->lock);
887 list_del(&group->list);
888 mutex_unlock(&smmu->lock);
891 static struct iommu_group *tegra_smmu_device_group(struct device *dev)
893 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
894 struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
895 const struct tegra_smmu_group_soc *soc;
896 unsigned int swgroup = fwspec->ids[0];
897 struct tegra_smmu_group *group;
898 struct iommu_group *grp;
900 /* Find group_soc associating with swgroup */
901 soc = tegra_smmu_find_group(smmu, swgroup);
903 mutex_lock(&smmu->lock);
905 /* Find existing iommu_group associating with swgroup or group_soc */
906 list_for_each_entry(group, &smmu->groups, list)
907 if ((group->swgroup == swgroup) || (soc && group->soc == soc)) {
908 grp = iommu_group_ref_get(group->group);
909 mutex_unlock(&smmu->lock);
913 group = devm_kzalloc(smmu->dev, sizeof(*group), GFP_KERNEL);
915 mutex_unlock(&smmu->lock);
919 INIT_LIST_HEAD(&group->list);
920 group->swgroup = swgroup;
925 group->group = pci_device_group(dev);
927 group->group = generic_device_group(dev);
929 if (IS_ERR(group->group)) {
930 devm_kfree(smmu->dev, group);
931 mutex_unlock(&smmu->lock);
935 iommu_group_set_iommudata(group->group, group, tegra_smmu_group_release);
937 iommu_group_set_name(group->group, soc->name);
938 list_add_tail(&group->list, &smmu->groups);
939 mutex_unlock(&smmu->lock);
944 static int tegra_smmu_of_xlate(struct device *dev,
945 struct of_phandle_args *args)
947 struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
948 struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
949 u32 id = args->args[0];
952 * Note: we are here releasing the reference of &iommu_pdev->dev, which
953 * is mc->dev. Although some functions in tegra_smmu_ops may keep using
954 * its private data beyond this point, it's still safe to do so because
955 * the SMMU parent device is the same as the MC, so the reference count
956 * isn't strictly necessary.
958 put_device(&iommu_pdev->dev);
960 dev_iommu_priv_set(dev, mc->smmu);
962 return iommu_fwspec_add_ids(dev, &id, 1);
965 static const struct iommu_ops tegra_smmu_ops = {
966 .domain_alloc = tegra_smmu_domain_alloc,
967 .probe_device = tegra_smmu_probe_device,
968 .device_group = tegra_smmu_device_group,
969 .set_platform_dma_ops = tegra_smmu_set_platform_dma,
970 .of_xlate = tegra_smmu_of_xlate,
971 .pgsize_bitmap = SZ_4K,
972 .default_domain_ops = &(const struct iommu_domain_ops) {
973 .attach_dev = tegra_smmu_attach_dev,
974 .map = tegra_smmu_map,
975 .unmap = tegra_smmu_unmap,
976 .iova_to_phys = tegra_smmu_iova_to_phys,
977 .free = tegra_smmu_domain_free,
981 static void tegra_smmu_ahb_enable(void)
983 static const struct of_device_id ahb_match[] = {
984 { .compatible = "nvidia,tegra30-ahb", },
987 struct device_node *ahb;
989 ahb = of_find_matching_node(NULL, ahb_match);
991 tegra_ahb_enable_smmu(ahb);
996 static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
998 struct tegra_smmu *smmu = s->private;
1002 seq_printf(s, "swgroup enabled ASID\n");
1003 seq_printf(s, "------------------------\n");
1005 for (i = 0; i < smmu->soc->num_swgroups; i++) {
1006 const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
1010 value = smmu_readl(smmu, group->reg);
1012 if (value & SMMU_ASID_ENABLE)
1017 asid = value & SMMU_ASID_MASK;
1019 seq_printf(s, "%-9s %-7s %#04x\n", group->name, status,
1026 DEFINE_SHOW_ATTRIBUTE(tegra_smmu_swgroups);
1028 static int tegra_smmu_clients_show(struct seq_file *s, void *data)
1030 struct tegra_smmu *smmu = s->private;
1034 seq_printf(s, "client enabled\n");
1035 seq_printf(s, "--------------------\n");
1037 for (i = 0; i < smmu->soc->num_clients; i++) {
1038 const struct tegra_mc_client *client = &smmu->soc->clients[i];
1041 value = smmu_readl(smmu, client->regs.smmu.reg);
1043 if (value & BIT(client->regs.smmu.bit))
1048 seq_printf(s, "%-12s %s\n", client->name, status);
1054 DEFINE_SHOW_ATTRIBUTE(tegra_smmu_clients);
1056 static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
1058 smmu->debugfs = debugfs_create_dir("smmu", NULL);
1062 debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
1063 &tegra_smmu_swgroups_fops);
1064 debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
1065 &tegra_smmu_clients_fops);
1068 static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
1070 debugfs_remove_recursive(smmu->debugfs);
1073 struct tegra_smmu *tegra_smmu_probe(struct device *dev,
1074 const struct tegra_smmu_soc *soc,
1075 struct tegra_mc *mc)
1077 struct tegra_smmu *smmu;
1081 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1083 return ERR_PTR(-ENOMEM);
1086 * This is a bit of a hack. Ideally we'd want to simply return this
1087 * value. However iommu_device_register() will attempt to add
1088 * all devices to the IOMMU before we get that far. In order
1089 * not to rely on global variables to track the IOMMU instance, we
1090 * set it here so that it can be looked up from the .probe_device()
1091 * callback via the IOMMU device's .drvdata field.
1095 smmu->asids = devm_bitmap_zalloc(dev, soc->num_asids, GFP_KERNEL);
1097 return ERR_PTR(-ENOMEM);
1099 INIT_LIST_HEAD(&smmu->groups);
1100 mutex_init(&smmu->lock);
1102 smmu->regs = mc->regs;
1108 BIT_MASK(mc->soc->num_address_bits - SMMU_PTE_SHIFT) - 1;
1109 dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
1110 mc->soc->num_address_bits, smmu->pfn_mask);
1111 smmu->tlb_mask = (1 << fls(smmu->soc->num_tlb_lines)) - 1;
1112 dev_dbg(dev, "TLB lines: %u, mask: %#lx\n", smmu->soc->num_tlb_lines,
1115 value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
1117 if (soc->supports_request_limit)
1118 value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
1120 smmu_writel(smmu, value, SMMU_PTC_CONFIG);
1122 value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
1123 SMMU_TLB_CONFIG_ACTIVE_LINES(smmu);
1125 if (soc->supports_round_robin_arbitration)
1126 value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
1128 smmu_writel(smmu, value, SMMU_TLB_CONFIG);
1130 smmu_flush_ptc_all(smmu);
1131 smmu_flush_tlb(smmu);
1132 smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
1135 tegra_smmu_ahb_enable();
1137 err = iommu_device_sysfs_add(&smmu->iommu, dev, NULL, dev_name(dev));
1139 return ERR_PTR(err);
1141 err = iommu_device_register(&smmu->iommu, &tegra_smmu_ops, dev);
1143 iommu_device_sysfs_remove(&smmu->iommu);
1144 return ERR_PTR(err);
1147 if (IS_ENABLED(CONFIG_DEBUG_FS))
1148 tegra_smmu_debugfs_init(smmu);
1153 void tegra_smmu_remove(struct tegra_smmu *smmu)
1155 iommu_device_unregister(&smmu->iommu);
1156 iommu_device_sysfs_remove(&smmu->iommu);
1158 if (IS_ENABLED(CONFIG_DEBUG_FS))
1159 tegra_smmu_debugfs_exit(smmu);