Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2011-2014 NVIDIA CORPORATION.  All rights reserved.
0004  */
0005 
0006 #include <linux/bitops.h>
0007 #include <linux/debugfs.h>
0008 #include <linux/err.h>
0009 #include <linux/iommu.h>
0010 #include <linux/kernel.h>
0011 #include <linux/of.h>
0012 #include <linux/of_device.h>
0013 #include <linux/pci.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/slab.h>
0016 #include <linux/spinlock.h>
0017 #include <linux/dma-mapping.h>
0018 
0019 #include <soc/tegra/ahb.h>
0020 #include <soc/tegra/mc.h>
0021 
0022 struct tegra_smmu_group {
0023     struct list_head list;
0024     struct tegra_smmu *smmu;
0025     const struct tegra_smmu_group_soc *soc;
0026     struct iommu_group *group;
0027     unsigned int swgroup;
0028 };
0029 
0030 struct tegra_smmu {
0031     void __iomem *regs;
0032     struct device *dev;
0033 
0034     struct tegra_mc *mc;
0035     const struct tegra_smmu_soc *soc;
0036 
0037     struct list_head groups;
0038 
0039     unsigned long pfn_mask;
0040     unsigned long tlb_mask;
0041 
0042     unsigned long *asids;
0043     struct mutex lock;
0044 
0045     struct list_head list;
0046 
0047     struct dentry *debugfs;
0048 
0049     struct iommu_device iommu;  /* IOMMU Core code handle */
0050 };
0051 
0052 struct tegra_smmu_as {
0053     struct iommu_domain domain;
0054     struct tegra_smmu *smmu;
0055     unsigned int use_count;
0056     spinlock_t lock;
0057     u32 *count;
0058     struct page **pts;
0059     struct page *pd;
0060     dma_addr_t pd_dma;
0061     unsigned id;
0062     u32 attr;
0063 };
0064 
0065 static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom)
0066 {
0067     return container_of(dom, struct tegra_smmu_as, domain);
0068 }
0069 
0070 static inline void smmu_writel(struct tegra_smmu *smmu, u32 value,
0071                    unsigned long offset)
0072 {
0073     writel(value, smmu->regs + offset);
0074 }
0075 
0076 static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset)
0077 {
0078     return readl(smmu->regs + offset);
0079 }
0080 
0081 #define SMMU_CONFIG 0x010
0082 #define  SMMU_CONFIG_ENABLE (1 << 0)
0083 
0084 #define SMMU_TLB_CONFIG 0x14
0085 #define  SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29)
0086 #define  SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28)
0087 #define  SMMU_TLB_CONFIG_ACTIVE_LINES(smmu) \
0088     ((smmu)->soc->num_tlb_lines & (smmu)->tlb_mask)
0089 
0090 #define SMMU_PTC_CONFIG 0x18
0091 #define  SMMU_PTC_CONFIG_ENABLE (1 << 29)
0092 #define  SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24)
0093 #define  SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f)
0094 
0095 #define SMMU_PTB_ASID 0x01c
0096 #define  SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
0097 
0098 #define SMMU_PTB_DATA 0x020
0099 #define  SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr))
0100 
0101 #define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr))
0102 
0103 #define SMMU_TLB_FLUSH 0x030
0104 #define  SMMU_TLB_FLUSH_VA_MATCH_ALL     (0 << 0)
0105 #define  SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0)
0106 #define  SMMU_TLB_FLUSH_VA_MATCH_GROUP   (3 << 0)
0107 #define  SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
0108                       SMMU_TLB_FLUSH_VA_MATCH_SECTION)
0109 #define  SMMU_TLB_FLUSH_VA_GROUP(addr)   ((((addr) & 0xffffc000) >> 12) | \
0110                       SMMU_TLB_FLUSH_VA_MATCH_GROUP)
0111 #define  SMMU_TLB_FLUSH_ASID_MATCH       (1 << 31)
0112 
0113 #define SMMU_PTC_FLUSH 0x034
0114 #define  SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
0115 #define  SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
0116 
0117 #define SMMU_PTC_FLUSH_HI 0x9b8
0118 #define  SMMU_PTC_FLUSH_HI_MASK 0x3
0119 
0120 /* per-SWGROUP SMMU_*_ASID register */
0121 #define SMMU_ASID_ENABLE (1 << 31)
0122 #define SMMU_ASID_MASK 0x7f
0123 #define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
0124 
0125 /* page table definitions */
0126 #define SMMU_NUM_PDE 1024
0127 #define SMMU_NUM_PTE 1024
0128 
0129 #define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
0130 #define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
0131 
0132 #define SMMU_PDE_SHIFT 22
0133 #define SMMU_PTE_SHIFT 12
0134 
0135 #define SMMU_PAGE_MASK      (~(SMMU_SIZE_PT-1))
0136 #define SMMU_OFFSET_IN_PAGE(x)  ((unsigned long)(x) & ~SMMU_PAGE_MASK)
0137 #define SMMU_PFN_PHYS(x)    ((phys_addr_t)(x) << SMMU_PTE_SHIFT)
0138 #define SMMU_PHYS_PFN(x)    ((unsigned long)((x) >> SMMU_PTE_SHIFT))
0139 
0140 #define SMMU_PD_READABLE    (1 << 31)
0141 #define SMMU_PD_WRITABLE    (1 << 30)
0142 #define SMMU_PD_NONSECURE   (1 << 29)
0143 
0144 #define SMMU_PDE_READABLE   (1 << 31)
0145 #define SMMU_PDE_WRITABLE   (1 << 30)
0146 #define SMMU_PDE_NONSECURE  (1 << 29)
0147 #define SMMU_PDE_NEXT       (1 << 28)
0148 
0149 #define SMMU_PTE_READABLE   (1 << 31)
0150 #define SMMU_PTE_WRITABLE   (1 << 30)
0151 #define SMMU_PTE_NONSECURE  (1 << 29)
0152 
0153 #define SMMU_PDE_ATTR       (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
0154                  SMMU_PDE_NONSECURE)
0155 
0156 static unsigned int iova_pd_index(unsigned long iova)
0157 {
0158     return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1);
0159 }
0160 
0161 static unsigned int iova_pt_index(unsigned long iova)
0162 {
0163     return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1);
0164 }
0165 
0166 static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr)
0167 {
0168     addr >>= 12;
0169     return (addr & smmu->pfn_mask) == addr;
0170 }
0171 
0172 static dma_addr_t smmu_pde_to_dma(struct tegra_smmu *smmu, u32 pde)
0173 {
0174     return (dma_addr_t)(pde & smmu->pfn_mask) << 12;
0175 }
0176 
0177 static void smmu_flush_ptc_all(struct tegra_smmu *smmu)
0178 {
0179     smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
0180 }
0181 
0182 static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma,
0183                   unsigned long offset)
0184 {
0185     u32 value;
0186 
0187     offset &= ~(smmu->mc->soc->atom_size - 1);
0188 
0189     if (smmu->mc->soc->num_address_bits > 32) {
0190 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
0191         value = (dma >> 32) & SMMU_PTC_FLUSH_HI_MASK;
0192 #else
0193         value = 0;
0194 #endif
0195         smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
0196     }
0197 
0198     value = (dma + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
0199     smmu_writel(smmu, value, SMMU_PTC_FLUSH);
0200 }
0201 
0202 static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
0203 {
0204     smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
0205 }
0206 
0207 static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
0208                        unsigned long asid)
0209 {
0210     u32 value;
0211 
0212     if (smmu->soc->num_asids == 4)
0213         value = (asid & 0x3) << 29;
0214     else
0215         value = (asid & 0x7f) << 24;
0216 
0217     value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_MATCH_ALL;
0218     smmu_writel(smmu, value, SMMU_TLB_FLUSH);
0219 }
0220 
0221 static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
0222                       unsigned long asid,
0223                       unsigned long iova)
0224 {
0225     u32 value;
0226 
0227     if (smmu->soc->num_asids == 4)
0228         value = (asid & 0x3) << 29;
0229     else
0230         value = (asid & 0x7f) << 24;
0231 
0232     value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_SECTION(iova);
0233     smmu_writel(smmu, value, SMMU_TLB_FLUSH);
0234 }
0235 
0236 static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu,
0237                     unsigned long asid,
0238                     unsigned long iova)
0239 {
0240     u32 value;
0241 
0242     if (smmu->soc->num_asids == 4)
0243         value = (asid & 0x3) << 29;
0244     else
0245         value = (asid & 0x7f) << 24;
0246 
0247     value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_GROUP(iova);
0248     smmu_writel(smmu, value, SMMU_TLB_FLUSH);
0249 }
0250 
0251 static inline void smmu_flush(struct tegra_smmu *smmu)
0252 {
0253     smmu_readl(smmu, SMMU_PTB_ASID);
0254 }
0255 
0256 static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
0257 {
0258     unsigned long id;
0259 
0260     id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
0261     if (id >= smmu->soc->num_asids)
0262         return -ENOSPC;
0263 
0264     set_bit(id, smmu->asids);
0265     *idp = id;
0266 
0267     return 0;
0268 }
0269 
0270 static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
0271 {
0272     clear_bit(id, smmu->asids);
0273 }
0274 
0275 static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type)
0276 {
0277     struct tegra_smmu_as *as;
0278 
0279     if (type != IOMMU_DOMAIN_UNMANAGED)
0280         return NULL;
0281 
0282     as = kzalloc(sizeof(*as), GFP_KERNEL);
0283     if (!as)
0284         return NULL;
0285 
0286     as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
0287 
0288     as->pd = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
0289     if (!as->pd) {
0290         kfree(as);
0291         return NULL;
0292     }
0293 
0294     as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL);
0295     if (!as->count) {
0296         __free_page(as->pd);
0297         kfree(as);
0298         return NULL;
0299     }
0300 
0301     as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL);
0302     if (!as->pts) {
0303         kfree(as->count);
0304         __free_page(as->pd);
0305         kfree(as);
0306         return NULL;
0307     }
0308 
0309     spin_lock_init(&as->lock);
0310 
0311     /* setup aperture */
0312     as->domain.geometry.aperture_start = 0;
0313     as->domain.geometry.aperture_end = 0xffffffff;
0314     as->domain.geometry.force_aperture = true;
0315 
0316     return &as->domain;
0317 }
0318 
0319 static void tegra_smmu_domain_free(struct iommu_domain *domain)
0320 {
0321     struct tegra_smmu_as *as = to_smmu_as(domain);
0322 
0323     /* TODO: free page directory and page tables */
0324 
0325     WARN_ON_ONCE(as->use_count);
0326     kfree(as->count);
0327     kfree(as->pts);
0328     kfree(as);
0329 }
0330 
0331 static const struct tegra_smmu_swgroup *
0332 tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
0333 {
0334     const struct tegra_smmu_swgroup *group = NULL;
0335     unsigned int i;
0336 
0337     for (i = 0; i < smmu->soc->num_swgroups; i++) {
0338         if (smmu->soc->swgroups[i].swgroup == swgroup) {
0339             group = &smmu->soc->swgroups[i];
0340             break;
0341         }
0342     }
0343 
0344     return group;
0345 }
0346 
0347 static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
0348                   unsigned int asid)
0349 {
0350     const struct tegra_smmu_swgroup *group;
0351     unsigned int i;
0352     u32 value;
0353 
0354     group = tegra_smmu_find_swgroup(smmu, swgroup);
0355     if (group) {
0356         value = smmu_readl(smmu, group->reg);
0357         value &= ~SMMU_ASID_MASK;
0358         value |= SMMU_ASID_VALUE(asid);
0359         value |= SMMU_ASID_ENABLE;
0360         smmu_writel(smmu, value, group->reg);
0361     } else {
0362         pr_warn("%s group from swgroup %u not found\n", __func__,
0363                 swgroup);
0364         /* No point moving ahead if group was not found */
0365         return;
0366     }
0367 
0368     for (i = 0; i < smmu->soc->num_clients; i++) {
0369         const struct tegra_mc_client *client = &smmu->soc->clients[i];
0370 
0371         if (client->swgroup != swgroup)
0372             continue;
0373 
0374         value = smmu_readl(smmu, client->regs.smmu.reg);
0375         value |= BIT(client->regs.smmu.bit);
0376         smmu_writel(smmu, value, client->regs.smmu.reg);
0377     }
0378 }
0379 
0380 static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
0381                    unsigned int asid)
0382 {
0383     const struct tegra_smmu_swgroup *group;
0384     unsigned int i;
0385     u32 value;
0386 
0387     group = tegra_smmu_find_swgroup(smmu, swgroup);
0388     if (group) {
0389         value = smmu_readl(smmu, group->reg);
0390         value &= ~SMMU_ASID_MASK;
0391         value |= SMMU_ASID_VALUE(asid);
0392         value &= ~SMMU_ASID_ENABLE;
0393         smmu_writel(smmu, value, group->reg);
0394     }
0395 
0396     for (i = 0; i < smmu->soc->num_clients; i++) {
0397         const struct tegra_mc_client *client = &smmu->soc->clients[i];
0398 
0399         if (client->swgroup != swgroup)
0400             continue;
0401 
0402         value = smmu_readl(smmu, client->regs.smmu.reg);
0403         value &= ~BIT(client->regs.smmu.bit);
0404         smmu_writel(smmu, value, client->regs.smmu.reg);
0405     }
0406 }
0407 
0408 static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
0409                  struct tegra_smmu_as *as)
0410 {
0411     u32 value;
0412     int err = 0;
0413 
0414     mutex_lock(&smmu->lock);
0415 
0416     if (as->use_count > 0) {
0417         as->use_count++;
0418         goto unlock;
0419     }
0420 
0421     as->pd_dma = dma_map_page(smmu->dev, as->pd, 0, SMMU_SIZE_PD,
0422                   DMA_TO_DEVICE);
0423     if (dma_mapping_error(smmu->dev, as->pd_dma)) {
0424         err = -ENOMEM;
0425         goto unlock;
0426     }
0427 
0428     /* We can't handle 64-bit DMA addresses */
0429     if (!smmu_dma_addr_valid(smmu, as->pd_dma)) {
0430         err = -ENOMEM;
0431         goto err_unmap;
0432     }
0433 
0434     err = tegra_smmu_alloc_asid(smmu, &as->id);
0435     if (err < 0)
0436         goto err_unmap;
0437 
0438     smmu_flush_ptc(smmu, as->pd_dma, 0);
0439     smmu_flush_tlb_asid(smmu, as->id);
0440 
0441     smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
0442     value = SMMU_PTB_DATA_VALUE(as->pd_dma, as->attr);
0443     smmu_writel(smmu, value, SMMU_PTB_DATA);
0444     smmu_flush(smmu);
0445 
0446     as->smmu = smmu;
0447     as->use_count++;
0448 
0449     mutex_unlock(&smmu->lock);
0450 
0451     return 0;
0452 
0453 err_unmap:
0454     dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
0455 unlock:
0456     mutex_unlock(&smmu->lock);
0457 
0458     return err;
0459 }
0460 
0461 static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
0462                     struct tegra_smmu_as *as)
0463 {
0464     mutex_lock(&smmu->lock);
0465 
0466     if (--as->use_count > 0) {
0467         mutex_unlock(&smmu->lock);
0468         return;
0469     }
0470 
0471     tegra_smmu_free_asid(smmu, as->id);
0472 
0473     dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
0474 
0475     as->smmu = NULL;
0476 
0477     mutex_unlock(&smmu->lock);
0478 }
0479 
0480 static int tegra_smmu_attach_dev(struct iommu_domain *domain,
0481                  struct device *dev)
0482 {
0483     struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
0484     struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
0485     struct tegra_smmu_as *as = to_smmu_as(domain);
0486     unsigned int index;
0487     int err;
0488 
0489     if (!fwspec)
0490         return -ENOENT;
0491 
0492     for (index = 0; index < fwspec->num_ids; index++) {
0493         err = tegra_smmu_as_prepare(smmu, as);
0494         if (err)
0495             goto disable;
0496 
0497         tegra_smmu_enable(smmu, fwspec->ids[index], as->id);
0498     }
0499 
0500     if (index == 0)
0501         return -ENODEV;
0502 
0503     return 0;
0504 
0505 disable:
0506     while (index--) {
0507         tegra_smmu_disable(smmu, fwspec->ids[index], as->id);
0508         tegra_smmu_as_unprepare(smmu, as);
0509     }
0510 
0511     return err;
0512 }
0513 
0514 static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
0515 {
0516     struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
0517     struct tegra_smmu_as *as = to_smmu_as(domain);
0518     struct tegra_smmu *smmu = as->smmu;
0519     unsigned int index;
0520 
0521     if (!fwspec)
0522         return;
0523 
0524     for (index = 0; index < fwspec->num_ids; index++) {
0525         tegra_smmu_disable(smmu, fwspec->ids[index], as->id);
0526         tegra_smmu_as_unprepare(smmu, as);
0527     }
0528 }
0529 
0530 static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova,
0531                    u32 value)
0532 {
0533     unsigned int pd_index = iova_pd_index(iova);
0534     struct tegra_smmu *smmu = as->smmu;
0535     u32 *pd = page_address(as->pd);
0536     unsigned long offset = pd_index * sizeof(*pd);
0537 
0538     /* Set the page directory entry first */
0539     pd[pd_index] = value;
0540 
0541     /* The flush the page directory entry from caches */
0542     dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset,
0543                      sizeof(*pd), DMA_TO_DEVICE);
0544 
0545     /* And flush the iommu */
0546     smmu_flush_ptc(smmu, as->pd_dma, offset);
0547     smmu_flush_tlb_section(smmu, as->id, iova);
0548     smmu_flush(smmu);
0549 }
0550 
0551 static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova)
0552 {
0553     u32 *pt = page_address(pt_page);
0554 
0555     return pt + iova_pt_index(iova);
0556 }
0557 
0558 static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
0559                   dma_addr_t *dmap)
0560 {
0561     unsigned int pd_index = iova_pd_index(iova);
0562     struct tegra_smmu *smmu = as->smmu;
0563     struct page *pt_page;
0564     u32 *pd;
0565 
0566     pt_page = as->pts[pd_index];
0567     if (!pt_page)
0568         return NULL;
0569 
0570     pd = page_address(as->pd);
0571     *dmap = smmu_pde_to_dma(smmu, pd[pd_index]);
0572 
0573     return tegra_smmu_pte_offset(pt_page, iova);
0574 }
0575 
0576 static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
0577                dma_addr_t *dmap, struct page *page)
0578 {
0579     unsigned int pde = iova_pd_index(iova);
0580     struct tegra_smmu *smmu = as->smmu;
0581 
0582     if (!as->pts[pde]) {
0583         dma_addr_t dma;
0584 
0585         dma = dma_map_page(smmu->dev, page, 0, SMMU_SIZE_PT,
0586                    DMA_TO_DEVICE);
0587         if (dma_mapping_error(smmu->dev, dma)) {
0588             __free_page(page);
0589             return NULL;
0590         }
0591 
0592         if (!smmu_dma_addr_valid(smmu, dma)) {
0593             dma_unmap_page(smmu->dev, dma, SMMU_SIZE_PT,
0594                        DMA_TO_DEVICE);
0595             __free_page(page);
0596             return NULL;
0597         }
0598 
0599         as->pts[pde] = page;
0600 
0601         tegra_smmu_set_pde(as, iova, SMMU_MK_PDE(dma, SMMU_PDE_ATTR |
0602                                   SMMU_PDE_NEXT));
0603 
0604         *dmap = dma;
0605     } else {
0606         u32 *pd = page_address(as->pd);
0607 
0608         *dmap = smmu_pde_to_dma(smmu, pd[pde]);
0609     }
0610 
0611     return tegra_smmu_pte_offset(as->pts[pde], iova);
0612 }
0613 
0614 static void tegra_smmu_pte_get_use(struct tegra_smmu_as *as, unsigned long iova)
0615 {
0616     unsigned int pd_index = iova_pd_index(iova);
0617 
0618     as->count[pd_index]++;
0619 }
0620 
0621 static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
0622 {
0623     unsigned int pde = iova_pd_index(iova);
0624     struct page *page = as->pts[pde];
0625 
0626     /*
0627      * When no entries in this page table are used anymore, return the
0628      * memory page to the system.
0629      */
0630     if (--as->count[pde] == 0) {
0631         struct tegra_smmu *smmu = as->smmu;
0632         u32 *pd = page_address(as->pd);
0633         dma_addr_t pte_dma = smmu_pde_to_dma(smmu, pd[pde]);
0634 
0635         tegra_smmu_set_pde(as, iova, 0);
0636 
0637         dma_unmap_page(smmu->dev, pte_dma, SMMU_SIZE_PT, DMA_TO_DEVICE);
0638         __free_page(page);
0639         as->pts[pde] = NULL;
0640     }
0641 }
0642 
0643 static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
0644                    u32 *pte, dma_addr_t pte_dma, u32 val)
0645 {
0646     struct tegra_smmu *smmu = as->smmu;
0647     unsigned long offset = SMMU_OFFSET_IN_PAGE(pte);
0648 
0649     *pte = val;
0650 
0651     dma_sync_single_range_for_device(smmu->dev, pte_dma, offset,
0652                      4, DMA_TO_DEVICE);
0653     smmu_flush_ptc(smmu, pte_dma, offset);
0654     smmu_flush_tlb_group(smmu, as->id, iova);
0655     smmu_flush(smmu);
0656 }
0657 
0658 static struct page *as_get_pde_page(struct tegra_smmu_as *as,
0659                     unsigned long iova, gfp_t gfp,
0660                     unsigned long *flags)
0661 {
0662     unsigned int pde = iova_pd_index(iova);
0663     struct page *page = as->pts[pde];
0664 
0665     /* at first check whether allocation needs to be done at all */
0666     if (page)
0667         return page;
0668 
0669     /*
0670      * In order to prevent exhaustion of the atomic memory pool, we
0671      * allocate page in a sleeping context if GFP flags permit. Hence
0672      * spinlock needs to be unlocked and re-locked after allocation.
0673      */
0674     if (!(gfp & __GFP_ATOMIC))
0675         spin_unlock_irqrestore(&as->lock, *flags);
0676 
0677     page = alloc_page(gfp | __GFP_DMA | __GFP_ZERO);
0678 
0679     if (!(gfp & __GFP_ATOMIC))
0680         spin_lock_irqsave(&as->lock, *flags);
0681 
0682     /*
0683      * In a case of blocking allocation, a concurrent mapping may win
0684      * the PDE allocation. In this case the allocated page isn't needed
0685      * if allocation succeeded and the allocation failure isn't fatal.
0686      */
0687     if (as->pts[pde]) {
0688         if (page)
0689             __free_page(page);
0690 
0691         page = as->pts[pde];
0692     }
0693 
0694     return page;
0695 }
0696 
0697 static int
0698 __tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
0699          phys_addr_t paddr, size_t size, int prot, gfp_t gfp,
0700          unsigned long *flags)
0701 {
0702     struct tegra_smmu_as *as = to_smmu_as(domain);
0703     dma_addr_t pte_dma;
0704     struct page *page;
0705     u32 pte_attrs;
0706     u32 *pte;
0707 
0708     page = as_get_pde_page(as, iova, gfp, flags);
0709     if (!page)
0710         return -ENOMEM;
0711 
0712     pte = as_get_pte(as, iova, &pte_dma, page);
0713     if (!pte)
0714         return -ENOMEM;
0715 
0716     /* If we aren't overwriting a pre-existing entry, increment use */
0717     if (*pte == 0)
0718         tegra_smmu_pte_get_use(as, iova);
0719 
0720     pte_attrs = SMMU_PTE_NONSECURE;
0721 
0722     if (prot & IOMMU_READ)
0723         pte_attrs |= SMMU_PTE_READABLE;
0724 
0725     if (prot & IOMMU_WRITE)
0726         pte_attrs |= SMMU_PTE_WRITABLE;
0727 
0728     tegra_smmu_set_pte(as, iova, pte, pte_dma,
0729                SMMU_PHYS_PFN(paddr) | pte_attrs);
0730 
0731     return 0;
0732 }
0733 
0734 static size_t
0735 __tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
0736            size_t size, struct iommu_iotlb_gather *gather)
0737 {
0738     struct tegra_smmu_as *as = to_smmu_as(domain);
0739     dma_addr_t pte_dma;
0740     u32 *pte;
0741 
0742     pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
0743     if (!pte || !*pte)
0744         return 0;
0745 
0746     tegra_smmu_set_pte(as, iova, pte, pte_dma, 0);
0747     tegra_smmu_pte_put_use(as, iova);
0748 
0749     return size;
0750 }
0751 
0752 static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
0753               phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
0754 {
0755     struct tegra_smmu_as *as = to_smmu_as(domain);
0756     unsigned long flags;
0757     int ret;
0758 
0759     spin_lock_irqsave(&as->lock, flags);
0760     ret = __tegra_smmu_map(domain, iova, paddr, size, prot, gfp, &flags);
0761     spin_unlock_irqrestore(&as->lock, flags);
0762 
0763     return ret;
0764 }
0765 
0766 static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
0767                    size_t size, struct iommu_iotlb_gather *gather)
0768 {
0769     struct tegra_smmu_as *as = to_smmu_as(domain);
0770     unsigned long flags;
0771 
0772     spin_lock_irqsave(&as->lock, flags);
0773     size = __tegra_smmu_unmap(domain, iova, size, gather);
0774     spin_unlock_irqrestore(&as->lock, flags);
0775 
0776     return size;
0777 }
0778 
0779 static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
0780                        dma_addr_t iova)
0781 {
0782     struct tegra_smmu_as *as = to_smmu_as(domain);
0783     unsigned long pfn;
0784     dma_addr_t pte_dma;
0785     u32 *pte;
0786 
0787     pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
0788     if (!pte || !*pte)
0789         return 0;
0790 
0791     pfn = *pte & as->smmu->pfn_mask;
0792 
0793     return SMMU_PFN_PHYS(pfn) + SMMU_OFFSET_IN_PAGE(iova);
0794 }
0795 
0796 static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
0797 {
0798     struct platform_device *pdev;
0799     struct tegra_mc *mc;
0800 
0801     pdev = of_find_device_by_node(np);
0802     if (!pdev)
0803         return NULL;
0804 
0805     mc = platform_get_drvdata(pdev);
0806     if (!mc) {
0807         put_device(&pdev->dev);
0808         return NULL;
0809     }
0810 
0811     return mc->smmu;
0812 }
0813 
0814 static int tegra_smmu_configure(struct tegra_smmu *smmu, struct device *dev,
0815                 struct of_phandle_args *args)
0816 {
0817     const struct iommu_ops *ops = smmu->iommu.ops;
0818     int err;
0819 
0820     err = iommu_fwspec_init(dev, &dev->of_node->fwnode, ops);
0821     if (err < 0) {
0822         dev_err(dev, "failed to initialize fwspec: %d\n", err);
0823         return err;
0824     }
0825 
0826     err = ops->of_xlate(dev, args);
0827     if (err < 0) {
0828         dev_err(dev, "failed to parse SW group ID: %d\n", err);
0829         iommu_fwspec_free(dev);
0830         return err;
0831     }
0832 
0833     return 0;
0834 }
0835 
0836 static struct iommu_device *tegra_smmu_probe_device(struct device *dev)
0837 {
0838     struct device_node *np = dev->of_node;
0839     struct tegra_smmu *smmu = NULL;
0840     struct of_phandle_args args;
0841     unsigned int index = 0;
0842     int err;
0843 
0844     while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
0845                       &args) == 0) {
0846         smmu = tegra_smmu_find(args.np);
0847         if (smmu) {
0848             err = tegra_smmu_configure(smmu, dev, &args);
0849 
0850             if (err < 0) {
0851                 of_node_put(args.np);
0852                 return ERR_PTR(err);
0853             }
0854         }
0855 
0856         of_node_put(args.np);
0857         index++;
0858     }
0859 
0860     smmu = dev_iommu_priv_get(dev);
0861     if (!smmu)
0862         return ERR_PTR(-ENODEV);
0863 
0864     return &smmu->iommu;
0865 }
0866 
0867 static const struct tegra_smmu_group_soc *
0868 tegra_smmu_find_group(struct tegra_smmu *smmu, unsigned int swgroup)
0869 {
0870     unsigned int i, j;
0871 
0872     for (i = 0; i < smmu->soc->num_groups; i++)
0873         for (j = 0; j < smmu->soc->groups[i].num_swgroups; j++)
0874             if (smmu->soc->groups[i].swgroups[j] == swgroup)
0875                 return &smmu->soc->groups[i];
0876 
0877     return NULL;
0878 }
0879 
0880 static void tegra_smmu_group_release(void *iommu_data)
0881 {
0882     struct tegra_smmu_group *group = iommu_data;
0883     struct tegra_smmu *smmu = group->smmu;
0884 
0885     mutex_lock(&smmu->lock);
0886     list_del(&group->list);
0887     mutex_unlock(&smmu->lock);
0888 }
0889 
0890 static struct iommu_group *tegra_smmu_device_group(struct device *dev)
0891 {
0892     struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
0893     struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
0894     const struct tegra_smmu_group_soc *soc;
0895     unsigned int swgroup = fwspec->ids[0];
0896     struct tegra_smmu_group *group;
0897     struct iommu_group *grp;
0898 
0899     /* Find group_soc associating with swgroup */
0900     soc = tegra_smmu_find_group(smmu, swgroup);
0901 
0902     mutex_lock(&smmu->lock);
0903 
0904     /* Find existing iommu_group associating with swgroup or group_soc */
0905     list_for_each_entry(group, &smmu->groups, list)
0906         if ((group->swgroup == swgroup) || (soc && group->soc == soc)) {
0907             grp = iommu_group_ref_get(group->group);
0908             mutex_unlock(&smmu->lock);
0909             return grp;
0910         }
0911 
0912     group = devm_kzalloc(smmu->dev, sizeof(*group), GFP_KERNEL);
0913     if (!group) {
0914         mutex_unlock(&smmu->lock);
0915         return NULL;
0916     }
0917 
0918     INIT_LIST_HEAD(&group->list);
0919     group->swgroup = swgroup;
0920     group->smmu = smmu;
0921     group->soc = soc;
0922 
0923     if (dev_is_pci(dev))
0924         group->group = pci_device_group(dev);
0925     else
0926         group->group = generic_device_group(dev);
0927 
0928     if (IS_ERR(group->group)) {
0929         devm_kfree(smmu->dev, group);
0930         mutex_unlock(&smmu->lock);
0931         return NULL;
0932     }
0933 
0934     iommu_group_set_iommudata(group->group, group, tegra_smmu_group_release);
0935     if (soc)
0936         iommu_group_set_name(group->group, soc->name);
0937     list_add_tail(&group->list, &smmu->groups);
0938     mutex_unlock(&smmu->lock);
0939 
0940     return group->group;
0941 }
0942 
0943 static int tegra_smmu_of_xlate(struct device *dev,
0944                    struct of_phandle_args *args)
0945 {
0946     struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
0947     struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
0948     u32 id = args->args[0];
0949 
0950     /*
0951      * Note: we are here releasing the reference of &iommu_pdev->dev, which
0952      * is mc->dev. Although some functions in tegra_smmu_ops may keep using
0953      * its private data beyond this point, it's still safe to do so because
0954      * the SMMU parent device is the same as the MC, so the reference count
0955      * isn't strictly necessary.
0956      */
0957     put_device(&iommu_pdev->dev);
0958 
0959     dev_iommu_priv_set(dev, mc->smmu);
0960 
0961     return iommu_fwspec_add_ids(dev, &id, 1);
0962 }
0963 
0964 static const struct iommu_ops tegra_smmu_ops = {
0965     .domain_alloc = tegra_smmu_domain_alloc,
0966     .probe_device = tegra_smmu_probe_device,
0967     .device_group = tegra_smmu_device_group,
0968     .of_xlate = tegra_smmu_of_xlate,
0969     .pgsize_bitmap = SZ_4K,
0970     .default_domain_ops = &(const struct iommu_domain_ops) {
0971         .attach_dev = tegra_smmu_attach_dev,
0972         .detach_dev = tegra_smmu_detach_dev,
0973         .map        = tegra_smmu_map,
0974         .unmap      = tegra_smmu_unmap,
0975         .iova_to_phys   = tegra_smmu_iova_to_phys,
0976         .free       = tegra_smmu_domain_free,
0977     }
0978 };
0979 
0980 static void tegra_smmu_ahb_enable(void)
0981 {
0982     static const struct of_device_id ahb_match[] = {
0983         { .compatible = "nvidia,tegra30-ahb", },
0984         { }
0985     };
0986     struct device_node *ahb;
0987 
0988     ahb = of_find_matching_node(NULL, ahb_match);
0989     if (ahb) {
0990         tegra_ahb_enable_smmu(ahb);
0991         of_node_put(ahb);
0992     }
0993 }
0994 
0995 static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
0996 {
0997     struct tegra_smmu *smmu = s->private;
0998     unsigned int i;
0999     u32 value;
1000 
1001     seq_printf(s, "swgroup    enabled  ASID\n");
1002     seq_printf(s, "------------------------\n");
1003 
1004     for (i = 0; i < smmu->soc->num_swgroups; i++) {
1005         const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
1006         const char *status;
1007         unsigned int asid;
1008 
1009         value = smmu_readl(smmu, group->reg);
1010 
1011         if (value & SMMU_ASID_ENABLE)
1012             status = "yes";
1013         else
1014             status = "no";
1015 
1016         asid = value & SMMU_ASID_MASK;
1017 
1018         seq_printf(s, "%-9s  %-7s  %#04x\n", group->name, status,
1019                asid);
1020     }
1021 
1022     return 0;
1023 }
1024 
1025 DEFINE_SHOW_ATTRIBUTE(tegra_smmu_swgroups);
1026 
1027 static int tegra_smmu_clients_show(struct seq_file *s, void *data)
1028 {
1029     struct tegra_smmu *smmu = s->private;
1030     unsigned int i;
1031     u32 value;
1032 
1033     seq_printf(s, "client       enabled\n");
1034     seq_printf(s, "--------------------\n");
1035 
1036     for (i = 0; i < smmu->soc->num_clients; i++) {
1037         const struct tegra_mc_client *client = &smmu->soc->clients[i];
1038         const char *status;
1039 
1040         value = smmu_readl(smmu, client->regs.smmu.reg);
1041 
1042         if (value & BIT(client->regs.smmu.bit))
1043             status = "yes";
1044         else
1045             status = "no";
1046 
1047         seq_printf(s, "%-12s %s\n", client->name, status);
1048     }
1049 
1050     return 0;
1051 }
1052 
1053 DEFINE_SHOW_ATTRIBUTE(tegra_smmu_clients);
1054 
1055 static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
1056 {
1057     smmu->debugfs = debugfs_create_dir("smmu", NULL);
1058     if (!smmu->debugfs)
1059         return;
1060 
1061     debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
1062                 &tegra_smmu_swgroups_fops);
1063     debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
1064                 &tegra_smmu_clients_fops);
1065 }
1066 
1067 static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
1068 {
1069     debugfs_remove_recursive(smmu->debugfs);
1070 }
1071 
1072 struct tegra_smmu *tegra_smmu_probe(struct device *dev,
1073                     const struct tegra_smmu_soc *soc,
1074                     struct tegra_mc *mc)
1075 {
1076     struct tegra_smmu *smmu;
1077     u32 value;
1078     int err;
1079 
1080     smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1081     if (!smmu)
1082         return ERR_PTR(-ENOMEM);
1083 
1084     /*
1085      * This is a bit of a hack. Ideally we'd want to simply return this
1086      * value. However the IOMMU registration process will attempt to add
1087      * all devices to the IOMMU when bus_set_iommu() is called. In order
1088      * not to rely on global variables to track the IOMMU instance, we
1089      * set it here so that it can be looked up from the .probe_device()
1090      * callback via the IOMMU device's .drvdata field.
1091      */
1092     mc->smmu = smmu;
1093 
1094     smmu->asids = devm_bitmap_zalloc(dev, soc->num_asids, GFP_KERNEL);
1095     if (!smmu->asids)
1096         return ERR_PTR(-ENOMEM);
1097 
1098     INIT_LIST_HEAD(&smmu->groups);
1099     mutex_init(&smmu->lock);
1100 
1101     smmu->regs = mc->regs;
1102     smmu->soc = soc;
1103     smmu->dev = dev;
1104     smmu->mc = mc;
1105 
1106     smmu->pfn_mask =
1107         BIT_MASK(mc->soc->num_address_bits - SMMU_PTE_SHIFT) - 1;
1108     dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
1109         mc->soc->num_address_bits, smmu->pfn_mask);
1110     smmu->tlb_mask = (1 << fls(smmu->soc->num_tlb_lines)) - 1;
1111     dev_dbg(dev, "TLB lines: %u, mask: %#lx\n", smmu->soc->num_tlb_lines,
1112         smmu->tlb_mask);
1113 
1114     value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
1115 
1116     if (soc->supports_request_limit)
1117         value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
1118 
1119     smmu_writel(smmu, value, SMMU_PTC_CONFIG);
1120 
1121     value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
1122         SMMU_TLB_CONFIG_ACTIVE_LINES(smmu);
1123 
1124     if (soc->supports_round_robin_arbitration)
1125         value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
1126 
1127     smmu_writel(smmu, value, SMMU_TLB_CONFIG);
1128 
1129     smmu_flush_ptc_all(smmu);
1130     smmu_flush_tlb(smmu);
1131     smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
1132     smmu_flush(smmu);
1133 
1134     tegra_smmu_ahb_enable();
1135 
1136     err = iommu_device_sysfs_add(&smmu->iommu, dev, NULL, dev_name(dev));
1137     if (err)
1138         return ERR_PTR(err);
1139 
1140     err = iommu_device_register(&smmu->iommu, &tegra_smmu_ops, dev);
1141     if (err)
1142         goto remove_sysfs;
1143 
1144     err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
1145     if (err < 0)
1146         goto unregister;
1147 
1148 #ifdef CONFIG_PCI
1149     err = bus_set_iommu(&pci_bus_type, &tegra_smmu_ops);
1150     if (err < 0)
1151         goto unset_platform_bus;
1152 #endif
1153 
1154     if (IS_ENABLED(CONFIG_DEBUG_FS))
1155         tegra_smmu_debugfs_init(smmu);
1156 
1157     return smmu;
1158 
1159 unset_platform_bus: __maybe_unused;
1160     bus_set_iommu(&platform_bus_type, NULL);
1161 unregister:
1162     iommu_device_unregister(&smmu->iommu);
1163 remove_sysfs:
1164     iommu_device_sysfs_remove(&smmu->iommu);
1165 
1166     return ERR_PTR(err);
1167 }
1168 
1169 void tegra_smmu_remove(struct tegra_smmu *smmu)
1170 {
1171     iommu_device_unregister(&smmu->iommu);
1172     iommu_device_sysfs_remove(&smmu->iommu);
1173 
1174     if (IS_ENABLED(CONFIG_DEBUG_FS))
1175         tegra_smmu_debugfs_exit(smmu);
1176 }