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 #include "iommu-pages.h"
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;
37 const struct tegra_smmu_soc *soc;
39 struct list_head groups;
41 unsigned long pfn_mask;
42 unsigned long tlb_mask;
47 struct list_head list;
49 struct dentry *debugfs;
51 struct iommu_device iommu; /* IOMMU Core code handle */
57 struct tegra_smmu_as {
58 struct iommu_domain domain;
59 struct tegra_smmu *smmu;
60 unsigned int use_count;
63 struct tegra_pt **pts;
70 static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom)
72 return container_of(dom, struct tegra_smmu_as, domain);
75 static inline void smmu_writel(struct tegra_smmu *smmu, u32 value,
78 writel(value, smmu->regs + offset);
81 static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset)
83 return readl(smmu->regs + offset);
86 #define SMMU_CONFIG 0x010
87 #define SMMU_CONFIG_ENABLE (1 << 0)
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)
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)
100 #define SMMU_PTB_ASID 0x01c
101 #define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
103 #define SMMU_PTB_DATA 0x020
104 #define SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr))
106 #define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr))
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)
118 #define SMMU_PTC_FLUSH 0x034
119 #define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
120 #define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
122 #define SMMU_PTC_FLUSH_HI 0x9b8
123 #define SMMU_PTC_FLUSH_HI_MASK 0x3
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)
130 /* page table definitions */
131 #define SMMU_NUM_PDE 1024
132 #define SMMU_NUM_PTE 1024
134 #define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
135 #define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
137 #define SMMU_PDE_SHIFT 22
138 #define SMMU_PTE_SHIFT 12
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))
145 #define SMMU_PD_READABLE (1 << 31)
146 #define SMMU_PD_WRITABLE (1 << 30)
147 #define SMMU_PD_NONSECURE (1 << 29)
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)
154 #define SMMU_PTE_READABLE (1 << 31)
155 #define SMMU_PTE_WRITABLE (1 << 30)
156 #define SMMU_PTE_NONSECURE (1 << 29)
158 #define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
162 u32 val[SMMU_NUM_PDE];
166 u32 val[SMMU_NUM_PTE];
169 static unsigned int iova_pd_index(unsigned long iova)
171 return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1);
174 static unsigned int iova_pt_index(unsigned long iova)
176 return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1);
179 static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr)
182 return (addr & smmu->pfn_mask) == addr;
185 static dma_addr_t smmu_pde_to_dma(struct tegra_smmu *smmu, u32 pde)
187 return (dma_addr_t)(pde & smmu->pfn_mask) << 12;
190 static void smmu_flush_ptc_all(struct tegra_smmu *smmu)
192 smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
195 static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma,
196 unsigned long offset)
200 offset &= ~(smmu->mc->soc->atom_size - 1);
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;
208 smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
211 value = (dma + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
212 smmu_writel(smmu, value, SMMU_PTC_FLUSH);
215 static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
217 smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
220 static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
225 if (smmu->soc->num_asids == 4)
226 value = (asid & 0x3) << 29;
228 value = (asid & 0x7f) << 24;
230 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_MATCH_ALL;
231 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
234 static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
240 if (smmu->soc->num_asids == 4)
241 value = (asid & 0x3) << 29;
243 value = (asid & 0x7f) << 24;
245 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_SECTION(iova);
246 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
249 static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu,
255 if (smmu->soc->num_asids == 4)
256 value = (asid & 0x3) << 29;
258 value = (asid & 0x7f) << 24;
260 value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_GROUP(iova);
261 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
264 static inline void smmu_flush(struct tegra_smmu *smmu)
266 smmu_readl(smmu, SMMU_PTB_ASID);
269 static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
273 id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
274 if (id >= smmu->soc->num_asids)
277 set_bit(id, smmu->asids);
283 static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
285 clear_bit(id, smmu->asids);
288 static struct iommu_domain *tegra_smmu_domain_alloc_paging(struct device *dev)
290 struct tegra_smmu_as *as;
292 as = kzalloc(sizeof(*as), GFP_KERNEL);
296 as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
298 as->pd = iommu_alloc_pages_sz(GFP_KERNEL | __GFP_DMA, SMMU_SIZE_PD);
304 as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL);
306 iommu_free_pages(as->pd);
311 as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL);
314 iommu_free_pages(as->pd);
319 spin_lock_init(&as->lock);
322 as->domain.geometry.aperture_start = 0;
323 as->domain.geometry.aperture_end = 0xffffffff;
324 as->domain.geometry.force_aperture = true;
329 static void tegra_smmu_domain_free(struct iommu_domain *domain)
331 struct tegra_smmu_as *as = to_smmu_as(domain);
333 /* TODO: free page directory and page tables */
335 WARN_ON_ONCE(as->use_count);
341 static const struct tegra_smmu_swgroup *
342 tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
344 const struct tegra_smmu_swgroup *group = NULL;
347 for (i = 0; i < smmu->soc->num_swgroups; i++) {
348 if (smmu->soc->swgroups[i].swgroup == swgroup) {
349 group = &smmu->soc->swgroups[i];
357 static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
360 const struct tegra_smmu_swgroup *group;
364 group = tegra_smmu_find_swgroup(smmu, swgroup);
366 value = smmu_readl(smmu, group->reg);
367 value &= ~SMMU_ASID_MASK;
368 value |= SMMU_ASID_VALUE(asid);
369 value |= SMMU_ASID_ENABLE;
370 smmu_writel(smmu, value, group->reg);
372 pr_warn("%s group from swgroup %u not found\n", __func__,
374 /* No point moving ahead if group was not found */
378 for (i = 0; i < smmu->soc->num_clients; i++) {
379 const struct tegra_mc_client *client = &smmu->soc->clients[i];
381 if (client->swgroup != swgroup)
384 value = smmu_readl(smmu, client->regs.smmu.reg);
385 value |= BIT(client->regs.smmu.bit);
386 smmu_writel(smmu, value, client->regs.smmu.reg);
390 static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
393 const struct tegra_smmu_swgroup *group;
397 group = tegra_smmu_find_swgroup(smmu, swgroup);
399 value = smmu_readl(smmu, group->reg);
400 value &= ~SMMU_ASID_MASK;
401 value |= SMMU_ASID_VALUE(asid);
402 value &= ~SMMU_ASID_ENABLE;
403 smmu_writel(smmu, value, group->reg);
406 for (i = 0; i < smmu->soc->num_clients; i++) {
407 const struct tegra_mc_client *client = &smmu->soc->clients[i];
409 if (client->swgroup != swgroup)
412 value = smmu_readl(smmu, client->regs.smmu.reg);
413 value &= ~BIT(client->regs.smmu.bit);
414 smmu_writel(smmu, value, client->regs.smmu.reg);
418 static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
419 struct tegra_smmu_as *as)
424 mutex_lock(&smmu->lock);
426 if (as->use_count > 0) {
432 dma_map_single(smmu->dev, as->pd, SMMU_SIZE_PD, DMA_TO_DEVICE);
433 if (dma_mapping_error(smmu->dev, as->pd_dma)) {
438 /* We can't handle 64-bit DMA addresses */
439 if (!smmu_dma_addr_valid(smmu, as->pd_dma)) {
444 err = tegra_smmu_alloc_asid(smmu, &as->id);
448 smmu_flush_ptc(smmu, as->pd_dma, 0);
449 smmu_flush_tlb_asid(smmu, as->id);
451 smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
452 value = SMMU_PTB_DATA_VALUE(as->pd_dma, as->attr);
453 smmu_writel(smmu, value, SMMU_PTB_DATA);
459 mutex_unlock(&smmu->lock);
464 dma_unmap_single(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
466 mutex_unlock(&smmu->lock);
471 static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
472 struct tegra_smmu_as *as)
474 mutex_lock(&smmu->lock);
476 if (--as->use_count > 0) {
477 mutex_unlock(&smmu->lock);
481 tegra_smmu_free_asid(smmu, as->id);
483 dma_unmap_single(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
487 mutex_unlock(&smmu->lock);
490 static int tegra_smmu_attach_dev(struct iommu_domain *domain,
493 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
494 struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
495 struct tegra_smmu_as *as = to_smmu_as(domain);
502 for (index = 0; index < fwspec->num_ids; index++) {
503 err = tegra_smmu_as_prepare(smmu, as);
507 tegra_smmu_enable(smmu, fwspec->ids[index], as->id);
517 tegra_smmu_disable(smmu, fwspec->ids[index], as->id);
518 tegra_smmu_as_unprepare(smmu, as);
524 static int tegra_smmu_identity_attach(struct iommu_domain *identity_domain,
527 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
528 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
529 struct tegra_smmu_as *as;
530 struct tegra_smmu *smmu;
536 if (domain == identity_domain || !domain)
539 as = to_smmu_as(domain);
541 for (index = 0; index < fwspec->num_ids; index++) {
542 tegra_smmu_disable(smmu, fwspec->ids[index], as->id);
543 tegra_smmu_as_unprepare(smmu, as);
548 static struct iommu_domain_ops tegra_smmu_identity_ops = {
549 .attach_dev = tegra_smmu_identity_attach,
552 static struct iommu_domain tegra_smmu_identity_domain = {
553 .type = IOMMU_DOMAIN_IDENTITY,
554 .ops = &tegra_smmu_identity_ops,
557 static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova,
560 unsigned int pd_index = iova_pd_index(iova);
561 struct tegra_smmu *smmu = as->smmu;
562 u32 *pd = &as->pd->val[pd_index];
563 unsigned long offset = pd_index * sizeof(*pd);
565 /* Set the page directory entry first */
568 /* The flush the page directory entry from caches */
569 dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset,
570 sizeof(*pd), DMA_TO_DEVICE);
572 /* And flush the iommu */
573 smmu_flush_ptc(smmu, as->pd_dma, offset);
574 smmu_flush_tlb_section(smmu, as->id, iova);
578 static u32 *tegra_smmu_pte_offset(struct tegra_pt *pt, unsigned long iova)
580 return &pt->val[iova_pt_index(iova)];
583 static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
586 unsigned int pd_index = iova_pd_index(iova);
587 struct tegra_smmu *smmu = as->smmu;
590 pt = as->pts[pd_index];
594 *dmap = smmu_pde_to_dma(smmu, as->pd->val[pd_index]);
596 return tegra_smmu_pte_offset(pt, iova);
599 static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
600 dma_addr_t *dmap, struct tegra_pt *pt)
602 unsigned int pde = iova_pd_index(iova);
603 struct tegra_smmu *smmu = as->smmu;
608 dma = dma_map_single(smmu->dev, pt, SMMU_SIZE_PT,
610 if (dma_mapping_error(smmu->dev, dma)) {
611 iommu_free_pages(pt);
615 if (!smmu_dma_addr_valid(smmu, dma)) {
616 dma_unmap_single(smmu->dev, dma, SMMU_SIZE_PT,
618 iommu_free_pages(pt);
624 tegra_smmu_set_pde(as, iova, SMMU_MK_PDE(dma, SMMU_PDE_ATTR |
629 *dmap = smmu_pde_to_dma(smmu, as->pd->val[pde]);
632 return tegra_smmu_pte_offset(as->pts[pde], iova);
635 static void tegra_smmu_pte_get_use(struct tegra_smmu_as *as, unsigned long iova)
637 unsigned int pd_index = iova_pd_index(iova);
639 as->count[pd_index]++;
642 static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
644 unsigned int pde = iova_pd_index(iova);
645 struct tegra_pt *pt = as->pts[pde];
648 * When no entries in this page table are used anymore, return the
649 * memory page to the system.
651 if (--as->count[pde] == 0) {
652 struct tegra_smmu *smmu = as->smmu;
653 dma_addr_t pte_dma = smmu_pde_to_dma(smmu, as->pd->val[pde]);
655 tegra_smmu_set_pde(as, iova, 0);
657 dma_unmap_single(smmu->dev, pte_dma, SMMU_SIZE_PT,
659 iommu_free_pages(pt);
664 static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
665 u32 *pte, dma_addr_t pte_dma, u32 val)
667 struct tegra_smmu *smmu = as->smmu;
668 unsigned long offset = SMMU_OFFSET_IN_PAGE(pte);
672 dma_sync_single_range_for_device(smmu->dev, pte_dma, offset,
674 smmu_flush_ptc(smmu, pte_dma, offset);
675 smmu_flush_tlb_group(smmu, as->id, iova);
679 static struct tegra_pt *as_get_pde_page(struct tegra_smmu_as *as,
680 unsigned long iova, gfp_t gfp,
681 unsigned long *flags)
683 unsigned int pde = iova_pd_index(iova);
684 struct tegra_pt *pt = as->pts[pde];
686 /* at first check whether allocation needs to be done at all */
691 * In order to prevent exhaustion of the atomic memory pool, we
692 * allocate page in a sleeping context if GFP flags permit. Hence
693 * spinlock needs to be unlocked and re-locked after allocation.
695 if (gfpflags_allow_blocking(gfp))
696 spin_unlock_irqrestore(&as->lock, *flags);
698 pt = iommu_alloc_pages_sz(gfp | __GFP_DMA, SMMU_SIZE_PT);
700 if (gfpflags_allow_blocking(gfp))
701 spin_lock_irqsave(&as->lock, *flags);
704 * In a case of blocking allocation, a concurrent mapping may win
705 * the PDE allocation. In this case the allocated page isn't needed
706 * if allocation succeeded and the allocation failure isn't fatal.
710 iommu_free_pages(pt);
719 __tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
720 phys_addr_t paddr, size_t size, int prot, gfp_t gfp,
721 unsigned long *flags)
723 struct tegra_smmu_as *as = to_smmu_as(domain);
729 pt = as_get_pde_page(as, iova, gfp, flags);
733 pte = as_get_pte(as, iova, &pte_dma, pt);
737 /* If we aren't overwriting a pre-existing entry, increment use */
739 tegra_smmu_pte_get_use(as, iova);
741 pte_attrs = SMMU_PTE_NONSECURE;
743 if (prot & IOMMU_READ)
744 pte_attrs |= SMMU_PTE_READABLE;
746 if (prot & IOMMU_WRITE)
747 pte_attrs |= SMMU_PTE_WRITABLE;
749 tegra_smmu_set_pte(as, iova, pte, pte_dma,
750 SMMU_PHYS_PFN(paddr) | pte_attrs);
756 __tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
757 size_t size, struct iommu_iotlb_gather *gather)
759 struct tegra_smmu_as *as = to_smmu_as(domain);
763 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
767 tegra_smmu_set_pte(as, iova, pte, pte_dma, 0);
768 tegra_smmu_pte_put_use(as, iova);
773 static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
774 phys_addr_t paddr, size_t size, size_t count,
775 int prot, gfp_t gfp, size_t *mapped)
777 struct tegra_smmu_as *as = to_smmu_as(domain);
781 spin_lock_irqsave(&as->lock, flags);
782 ret = __tegra_smmu_map(domain, iova, paddr, size, prot, gfp, &flags);
783 spin_unlock_irqrestore(&as->lock, flags);
791 static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
792 size_t size, size_t count, struct iommu_iotlb_gather *gather)
794 struct tegra_smmu_as *as = to_smmu_as(domain);
797 spin_lock_irqsave(&as->lock, flags);
798 size = __tegra_smmu_unmap(domain, iova, size, gather);
799 spin_unlock_irqrestore(&as->lock, flags);
804 static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
807 struct tegra_smmu_as *as = to_smmu_as(domain);
812 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
816 pfn = *pte & as->smmu->pfn_mask;
818 return SMMU_PFN_PHYS(pfn) + SMMU_OFFSET_IN_PAGE(iova);
821 static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
823 struct platform_device *pdev;
826 pdev = of_find_device_by_node(np);
830 mc = platform_get_drvdata(pdev);
832 put_device(&pdev->dev);
839 static int tegra_smmu_configure(struct tegra_smmu *smmu, struct device *dev,
840 const struct of_phandle_args *args)
842 const struct iommu_ops *ops = smmu->iommu.ops;
845 err = iommu_fwspec_init(dev, dev_fwnode(smmu->dev));
847 dev_err(dev, "failed to initialize fwspec: %d\n", err);
851 err = ops->of_xlate(dev, args);
853 dev_err(dev, "failed to parse SW group ID: %d\n", err);
860 static struct iommu_device *tegra_smmu_probe_device(struct device *dev)
862 struct device_node *np = dev->of_node;
863 struct tegra_smmu *smmu = NULL;
864 struct of_phandle_args args;
865 unsigned int index = 0;
868 while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
870 smmu = tegra_smmu_find(args.np);
872 err = tegra_smmu_configure(smmu, dev, &args);
875 of_node_put(args.np);
880 of_node_put(args.np);
884 smmu = dev_iommu_priv_get(dev);
886 return ERR_PTR(-ENODEV);
891 static const struct tegra_smmu_group_soc *
892 tegra_smmu_find_group(struct tegra_smmu *smmu, unsigned int swgroup)
896 for (i = 0; i < smmu->soc->num_groups; i++)
897 for (j = 0; j < smmu->soc->groups[i].num_swgroups; j++)
898 if (smmu->soc->groups[i].swgroups[j] == swgroup)
899 return &smmu->soc->groups[i];
904 static void tegra_smmu_group_release(void *iommu_data)
906 struct tegra_smmu_group *group = iommu_data;
907 struct tegra_smmu *smmu = group->smmu;
909 mutex_lock(&smmu->lock);
910 list_del(&group->list);
911 mutex_unlock(&smmu->lock);
914 static struct iommu_group *tegra_smmu_device_group(struct device *dev)
916 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
917 struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
918 const struct tegra_smmu_group_soc *soc;
919 unsigned int swgroup = fwspec->ids[0];
920 struct tegra_smmu_group *group;
921 struct iommu_group *grp;
923 /* Find group_soc associating with swgroup */
924 soc = tegra_smmu_find_group(smmu, swgroup);
926 mutex_lock(&smmu->lock);
928 /* Find existing iommu_group associating with swgroup or group_soc */
929 list_for_each_entry(group, &smmu->groups, list)
930 if ((group->swgroup == swgroup) || (soc && group->soc == soc)) {
931 grp = iommu_group_ref_get(group->group);
932 mutex_unlock(&smmu->lock);
936 group = devm_kzalloc(smmu->dev, sizeof(*group), GFP_KERNEL);
938 mutex_unlock(&smmu->lock);
942 INIT_LIST_HEAD(&group->list);
943 group->swgroup = swgroup;
948 group->group = pci_device_group(dev);
950 group->group = generic_device_group(dev);
952 if (IS_ERR(group->group)) {
953 devm_kfree(smmu->dev, group);
954 mutex_unlock(&smmu->lock);
958 iommu_group_set_iommudata(group->group, group, tegra_smmu_group_release);
960 iommu_group_set_name(group->group, soc->name);
961 list_add_tail(&group->list, &smmu->groups);
962 mutex_unlock(&smmu->lock);
967 static int tegra_smmu_of_xlate(struct device *dev,
968 const struct of_phandle_args *args)
970 struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
971 struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
972 u32 id = args->args[0];
975 * Note: we are here releasing the reference of &iommu_pdev->dev, which
976 * is mc->dev. Although some functions in tegra_smmu_ops may keep using
977 * its private data beyond this point, it's still safe to do so because
978 * the SMMU parent device is the same as the MC, so the reference count
979 * isn't strictly necessary.
981 put_device(&iommu_pdev->dev);
983 dev_iommu_priv_set(dev, mc->smmu);
985 return iommu_fwspec_add_ids(dev, &id, 1);
988 static int tegra_smmu_def_domain_type(struct device *dev)
991 * FIXME: For now we want to run all translation in IDENTITY mode, due
992 * to some device quirks. Better would be to just quirk the troubled
995 return IOMMU_DOMAIN_IDENTITY;
998 static const struct iommu_ops tegra_smmu_ops = {
999 .identity_domain = &tegra_smmu_identity_domain,
1000 .def_domain_type = &tegra_smmu_def_domain_type,
1001 .domain_alloc_paging = tegra_smmu_domain_alloc_paging,
1002 .probe_device = tegra_smmu_probe_device,
1003 .device_group = tegra_smmu_device_group,
1004 .of_xlate = tegra_smmu_of_xlate,
1005 .pgsize_bitmap = SZ_4K,
1006 .default_domain_ops = &(const struct iommu_domain_ops) {
1007 .attach_dev = tegra_smmu_attach_dev,
1008 .map_pages = tegra_smmu_map,
1009 .unmap_pages = tegra_smmu_unmap,
1010 .iova_to_phys = tegra_smmu_iova_to_phys,
1011 .free = tegra_smmu_domain_free,
1015 static void tegra_smmu_ahb_enable(void)
1017 static const struct of_device_id ahb_match[] = {
1018 { .compatible = "nvidia,tegra30-ahb", },
1021 struct device_node *ahb;
1023 ahb = of_find_matching_node(NULL, ahb_match);
1025 tegra_ahb_enable_smmu(ahb);
1030 static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
1032 struct tegra_smmu *smmu = s->private;
1036 seq_printf(s, "swgroup enabled ASID\n");
1037 seq_printf(s, "------------------------\n");
1039 for (i = 0; i < smmu->soc->num_swgroups; i++) {
1040 const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
1044 value = smmu_readl(smmu, group->reg);
1046 if (value & SMMU_ASID_ENABLE)
1051 asid = value & SMMU_ASID_MASK;
1053 seq_printf(s, "%-9s %-7s %#04x\n", group->name, status,
1060 DEFINE_SHOW_ATTRIBUTE(tegra_smmu_swgroups);
1062 static int tegra_smmu_clients_show(struct seq_file *s, void *data)
1064 struct tegra_smmu *smmu = s->private;
1068 seq_printf(s, "client enabled\n");
1069 seq_printf(s, "--------------------\n");
1071 for (i = 0; i < smmu->soc->num_clients; i++) {
1072 const struct tegra_mc_client *client = &smmu->soc->clients[i];
1075 value = smmu_readl(smmu, client->regs.smmu.reg);
1077 if (value & BIT(client->regs.smmu.bit))
1082 seq_printf(s, "%-12s %s\n", client->name, status);
1088 DEFINE_SHOW_ATTRIBUTE(tegra_smmu_clients);
1090 static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
1092 smmu->debugfs = debugfs_create_dir("smmu", NULL);
1094 debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
1095 &tegra_smmu_swgroups_fops);
1096 debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
1097 &tegra_smmu_clients_fops);
1100 static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
1102 debugfs_remove_recursive(smmu->debugfs);
1105 struct tegra_smmu *tegra_smmu_probe(struct device *dev,
1106 const struct tegra_smmu_soc *soc,
1107 struct tegra_mc *mc)
1109 struct tegra_smmu *smmu;
1113 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1115 return ERR_PTR(-ENOMEM);
1118 * This is a bit of a hack. Ideally we'd want to simply return this
1119 * value. However iommu_device_register() will attempt to add
1120 * all devices to the IOMMU before we get that far. In order
1121 * not to rely on global variables to track the IOMMU instance, we
1122 * set it here so that it can be looked up from the .probe_device()
1123 * callback via the IOMMU device's .drvdata field.
1127 smmu->asids = devm_bitmap_zalloc(dev, soc->num_asids, GFP_KERNEL);
1129 return ERR_PTR(-ENOMEM);
1131 INIT_LIST_HEAD(&smmu->groups);
1132 mutex_init(&smmu->lock);
1134 smmu->regs = mc->regs;
1140 BIT_MASK(mc->soc->num_address_bits - SMMU_PTE_SHIFT) - 1;
1141 dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
1142 mc->soc->num_address_bits, smmu->pfn_mask);
1143 smmu->tlb_mask = (1 << fls(smmu->soc->num_tlb_lines)) - 1;
1144 dev_dbg(dev, "TLB lines: %u, mask: %#lx\n", smmu->soc->num_tlb_lines,
1147 value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
1149 if (soc->supports_request_limit)
1150 value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
1152 smmu_writel(smmu, value, SMMU_PTC_CONFIG);
1154 value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
1155 SMMU_TLB_CONFIG_ACTIVE_LINES(smmu);
1157 if (soc->supports_round_robin_arbitration)
1158 value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
1160 smmu_writel(smmu, value, SMMU_TLB_CONFIG);
1162 smmu_flush_ptc_all(smmu);
1163 smmu_flush_tlb(smmu);
1164 smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
1167 tegra_smmu_ahb_enable();
1169 err = iommu_device_sysfs_add(&smmu->iommu, dev, NULL, dev_name(dev));
1171 return ERR_PTR(err);
1173 err = iommu_device_register(&smmu->iommu, &tegra_smmu_ops, dev);
1175 iommu_device_sysfs_remove(&smmu->iommu);
1176 return ERR_PTR(err);
1179 if (IS_ENABLED(CONFIG_DEBUG_FS))
1180 tegra_smmu_debugfs_init(smmu);
1185 void tegra_smmu_remove(struct tegra_smmu *smmu)
1187 iommu_device_unregister(&smmu->iommu);
1188 iommu_device_sysfs_remove(&smmu->iommu);
1190 if (IS_ENABLED(CONFIG_DEBUG_FS))
1191 tegra_smmu_debugfs_exit(smmu);