Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp2490774pxf; Sun, 14 Mar 2021 00:12:47 -0800 (PST) X-Google-Smtp-Source: ABdhPJw0+K0ddMoQuT8pVMAvCed4Rq3ileLEd1i3iFHgG8CBFKCPjjh1Xvn15isptcrv2In/ya34 X-Received: by 2002:a05:6402:549:: with SMTP id i9mr24170215edx.379.1615709567650; Sun, 14 Mar 2021 00:12:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615709567; cv=none; d=google.com; s=arc-20160816; b=i23uos//wkRbb7jpfqKVpO1oHe0d3JgFnPxwWbqnEHR9Je7zUWVlmStQhp3nekZtDH jvINh1NT4I5isSfIPUIzF2paT/BQgNVPfgSqVgsEmPA3JHD1lH52NZSU3vIjPtCidWDH C+ZOpQO/EI9DE1/pT48vOoGa8T+mxkPxqSCqU2o0N4VvrF6wU6mlfN+/CQQLInBg9N05 UXeeVg/ktthWayDWWmKo59MMaBs0J0mpbs26TQ71FNwgh9notwBWopwoXPZESHaYFNoJ qzVEtpo7VKzLE+jTcpHxRpEU9DxQ01bW0Aff5R8xKdjFJJipXctf1BbRrX3SBYWxe2uq snXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=W7dU+Q7qoaFaxTyGRI+L/PrRz/7v4KYJxFg8oE5hPEU=; b=SuA3/46ANsOd4Rzkd2hRthiVnL7/Qd70TRQUyEOJgJUVdbNfE3hoZVBwo1hJU0bTXF k51rnP/e56KOAFqLEWuT/VbnVNo05S7jhhEHYAzVzWiZC3AT0gDjDwdJtir90LvaYYi8 /12ykCm+IZ4CBZGIEs6Kap/xxXoU/X6QAY6be7mQ4XmLPawOF1sExTl6sPu4+G1rf9mf kbV5EiLFTelVlidPxagn0d52NY4lJNr0bGXPlLxwoPOXO4Pi4Q+uCmqFebf7U19Z0W/p /xOjnjakbbA8OKQQw4sHrPFMY3Lqhc3qGa2UoCbAEwGXQ29zapVoiqCfjC9lNBt8OLnV Cntg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=vfIaUoeP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a3si7822293ejd.122.2021.03.14.00.12.25; Sun, 14 Mar 2021 00:12:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=vfIaUoeP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234489AbhCNIJg (ORCPT + 99 others); Sun, 14 Mar 2021 04:09:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229539AbhCNIJJ (ORCPT ); Sun, 14 Mar 2021 04:09:09 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6AF6AC061574; Sun, 14 Mar 2021 00:09:09 -0800 (PST) Received: by mail-pj1-x1034.google.com with SMTP id s21so7214358pjq.1; Sun, 14 Mar 2021 00:09:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=W7dU+Q7qoaFaxTyGRI+L/PrRz/7v4KYJxFg8oE5hPEU=; b=vfIaUoeP/ybextRTFvKL2FE1tEeVOBZxhG1aRhfkONh5WeD6sYlniqc+RiX+akr4Xa vIE5mQe+6hz41dNz0Xo/38rJhohSkfK35m67R3SCPuAI6KWgvk5VohrljSbytnSILaQT IyAWHk8Ijjg2IMX4yezqWLY4m8jvIy61JQGVsC0mPO3xXcn/i+lfkOwusbKxuXAGB7/l C5S3zw3z+TiO3k/MAdTqYVZ+/ksnlUCgTucVFJ3vQYvPENQLIOB+miJZpxFiC4KbMXbK LtrtLBzUcW/oxzQcrqV5z0iOYTSOQ547vIyqSUTH+uzxXgEcl1v2abs4G9IH8VeLr4DD QWxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=W7dU+Q7qoaFaxTyGRI+L/PrRz/7v4KYJxFg8oE5hPEU=; b=U9wUNj7vshUN6u/xpSY56IzYE0vbSbt1G9M2k2j63mzg6zBDh21rbYLa28bogm+z1K lYp3jfpukCbxj+BcWd2Wn3bcdy6gggT9PSrsqrp3JUzEeujiY5Sz1Kl2NdpgcGgZLdDm J/zoksel7v27x3hETCPlaBBdTakjGI4UWNu4Nas5QiDiYW0bxb6BglVutYZmw8YI+KL5 OVCiUHkVdNDvOWMg2VpQXv2LkRjViYedZFxwWv4hgagu/d488t5GyMZSCIHKAAH8wwut HZsHR/qFwm6Dwi7E4oH/BxWCxLjbOYZiVD4x3Og2aF/Qyxe8FhGhrsoAfxqro/kGqqxJ DlDQ== X-Gm-Message-State: AOAM530ERvUiXjq4WqLPM+TMk/KG6MicdsD4rsHj+OyL+WEEKEwRHieG nMItju/KdQDp3PM1fppM6/U= X-Received: by 2002:a17:90b:809:: with SMTP id bk9mr6963506pjb.83.1615709348699; Sun, 14 Mar 2021 00:09:08 -0800 (PST) Received: from Asurada-Nvidia.nvidia.com (thunderhill.nvidia.com. [216.228.112.22]) by smtp.gmail.com with ESMTPSA id c128sm9209422pfc.76.2021.03.14.00.09.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Mar 2021 00:09:08 -0800 (PST) From: Nicolin Chen To: joro@8bytes.org, thierry.reding@gmail.com, will@kernel.org, digetx@gmail.com Cc: vdumpa@nvidia.com, jonathanh@nvidia.com, linux-tegra@vger.kernel.org, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] iommu/tegra-smmu: Add pagetable mappings to debugfs Date: Sun, 14 Mar 2021 00:06:53 -0800 Message-Id: <20210314080653.29374-1-nicoleotsuka@gmail.com> X-Mailer: git-send-email 2.17.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch dumps all active mapping entries from pagetable to a debugfs directory named "mappings". Ataching an example: SWGROUP: hc ASID: 0 reg: 0x250 PTB_ASID: 0xe0080004 as->pd_dma: 0x80004000 { [1023] 0xf008000b (1) { PTE RANGE | ATTR | PHYS | IOVA | SIZE [#1023, #1023] | 0x5 | 0x0000000111a8d000 | 0x00000000fffff000 | 0x1000 } } Total PDE count: 1 Total PTE count: 1 Signed-off-by: Nicolin Chen --- Changelog v3: * Fixed PHYS and IOVA print formats * Changed variables to unsigned int type * Changed the table outputs to be compact v2: https://lkml.org/lkml/2021/3/9/1382 * Expanded mutex range to the entire function * Added as->lock to protect pagetable walkthrough * Replaced devm_kzalloc with devm_kcalloc for group_debug * Added "PTE RANGE" and "SIZE" columns to group contiguous mappings * Dropped as->count check; added WARN_ON when as->count mismatches pd[pd_index] v1: https://lkml.org/lkml/2020/9/26/70 drivers/iommu/tegra-smmu.c | 175 +++++++++++++++++++++++++++++++++++-- 1 file changed, 170 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/tegra-smmu.c b/drivers/iommu/tegra-smmu.c index 97eb62f667d2..269737d51ad4 100644 --- a/drivers/iommu/tegra-smmu.c +++ b/drivers/iommu/tegra-smmu.c @@ -19,6 +19,11 @@ #include #include +struct tegra_smmu_group_debug { + const struct tegra_smmu_swgroup *group; + void *priv; +}; + struct tegra_smmu_group { struct list_head list; struct tegra_smmu *smmu; @@ -47,6 +52,8 @@ struct tegra_smmu { struct dentry *debugfs; struct iommu_device iommu; /* IOMMU Core code handle */ + + struct tegra_smmu_group_debug *group_debug; }; struct tegra_smmu_as { @@ -152,6 +159,9 @@ static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset) #define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \ SMMU_PDE_NONSECURE) +#define SMMU_PTE_ATTR (SMMU_PTE_READABLE | SMMU_PTE_WRITABLE | \ + SMMU_PTE_NONSECURE) +#define SMMU_PTE_ATTR_SHIFT (29) static unsigned int iova_pd_index(unsigned long iova) { @@ -163,6 +173,12 @@ static unsigned int iova_pt_index(unsigned long iova) return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1); } +static unsigned long pd_pt_index_iova(unsigned int pd_index, unsigned int pt_index) +{ + return ((dma_addr_t)pd_index & (SMMU_NUM_PDE - 1)) << SMMU_PDE_SHIFT | + ((dma_addr_t)pt_index & (SMMU_NUM_PTE - 1)) << SMMU_PTE_SHIFT; +} + static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr) { addr >>= 12; @@ -334,7 +350,7 @@ static void tegra_smmu_domain_free(struct iommu_domain *domain) } static const struct tegra_smmu_swgroup * -tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup) +tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup, int *index) { const struct tegra_smmu_swgroup *group = NULL; unsigned int i; @@ -342,6 +358,8 @@ tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup) for (i = 0; i < smmu->soc->num_swgroups; i++) { if (smmu->soc->swgroups[i].swgroup == swgroup) { group = &smmu->soc->swgroups[i]; + if (index) + *index = i; break; } } @@ -350,19 +368,22 @@ tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup) } static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup, - unsigned int asid) + struct tegra_smmu_as *as) { const struct tegra_smmu_swgroup *group; + unsigned int asid = as->id; unsigned int i; u32 value; - group = tegra_smmu_find_swgroup(smmu, swgroup); + group = tegra_smmu_find_swgroup(smmu, swgroup, &i); if (group) { value = smmu_readl(smmu, group->reg); value &= ~SMMU_ASID_MASK; value |= SMMU_ASID_VALUE(asid); value |= SMMU_ASID_ENABLE; smmu_writel(smmu, value, group->reg); + if (smmu->group_debug) + smmu->group_debug[i].priv = as; } else { pr_warn("%s group from swgroup %u not found\n", __func__, swgroup); @@ -389,13 +410,15 @@ static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup, unsigned int i; u32 value; - group = tegra_smmu_find_swgroup(smmu, swgroup); + group = tegra_smmu_find_swgroup(smmu, swgroup, &i); if (group) { value = smmu_readl(smmu, group->reg); value &= ~SMMU_ASID_MASK; value |= SMMU_ASID_VALUE(asid); value &= ~SMMU_ASID_ENABLE; smmu_writel(smmu, value, group->reg); + if (smmu->group_debug) + smmu->group_debug[i].priv = NULL; } for (i = 0; i < smmu->soc->num_clients; i++) { @@ -499,7 +522,7 @@ static int tegra_smmu_attach_dev(struct iommu_domain *domain, if (err) goto disable; - tegra_smmu_enable(smmu, fwspec->ids[index], as->id); + tegra_smmu_enable(smmu, fwspec->ids[index], as); } if (index == 0) @@ -1058,8 +1081,135 @@ static int tegra_smmu_clients_show(struct seq_file *s, void *data) DEFINE_SHOW_ATTRIBUTE(tegra_smmu_clients); +static int tegra_smmu_mappings_show(struct seq_file *s, void *data) +{ + struct tegra_smmu_group_debug *group_debug = s->private; + const struct tegra_smmu_swgroup *group; + struct tegra_smmu_as *as; + struct tegra_smmu *smmu; + unsigned int pd_index; + unsigned int pt_index; + unsigned long flags; + u64 pte_count = 0; + u32 pde_count = 0; + u32 val, ptb_reg; + u32 *pd; + + if (!group_debug || !group_debug->priv || !group_debug->group) + return 0; + + group = group_debug->group; + as = group_debug->priv; + smmu = as->smmu; + + mutex_lock(&smmu->lock); + + val = smmu_readl(smmu, group->reg) & SMMU_ASID_ENABLE; + if (!val) + goto unlock; + + pd = page_address(as->pd); + if (!pd) + goto unlock; + + seq_printf(s, "\nSWGROUP: %s\nASID: %d\nreg: 0x%x\n", group->name, as->id, group->reg); + + smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID); + ptb_reg = smmu_readl(smmu, SMMU_PTB_DATA); + + seq_printf(s, "PTB_ASID: 0x%x\nas->pd_dma: 0x%llx\n", ptb_reg, as->pd_dma); + seq_puts(s, "{\n"); + + spin_lock_irqsave(&as->lock, flags); + + for (pd_index = 0; pd_index < SMMU_NUM_PDE; pd_index++) { + struct page *pt_page; + u32 *addr; + int i; + + /* An empty PDE should not have a pte use count */ + WARN_ON_ONCE(!pd[pd_index] ^ !as->count[pd_index]); + + /* Skip this empty PDE */ + if (!pd[pd_index]) + continue; + + pde_count++; + pte_count += as->count[pd_index]; + seq_printf(s, "\t[%d] 0x%x (%d)\n", pd_index, pd[pd_index], as->count[pd_index]); + pt_page = as->pts[pd_index]; + addr = page_address(pt_page); + + seq_puts(s, "\t{\n"); + seq_printf(s, "\t\t%-14s | %-4s | %-10s%8s | %-10s%8s | %-11s\n", + "PTE RANGE", "ATTR", "PHYS", sizeof(phys_addr_t) > 4 ? " " : "", + "IOVA", sizeof(unsigned long) > 4 ? " " : "", "SIZE"); + for (pt_index = 0; pt_index < SMMU_NUM_PTE; pt_index += i) { + size_t size = SMMU_SIZE_PT; + unsigned long iova; + phys_addr_t pa; + + i = 1; + + if (!addr[pt_index]) + continue; + + iova = pd_pt_index_iova(pd_index, pt_index); + pa = SMMU_PFN_PHYS(addr[pt_index] & ~SMMU_PTE_ATTR); + + /* Check contiguous mappings and increase size */ + while (pt_index + i < SMMU_NUM_PTE) { + phys_addr_t next_pa; + u64 next_iova; + + if (!addr[pt_index + i]) + break; + + next_iova = pd_pt_index_iova(pd_index, pt_index + i); + next_pa = SMMU_PFN_PHYS(addr[pt_index + i] & ~SMMU_PTE_ATTR); + + /* Break at the end of a linear mapping */ + if ((next_iova - iova != SMMU_SIZE_PT * i) || + (next_pa - pa != SMMU_SIZE_PT * i)) + break; + + i++; + } + + seq_printf(s, "\t\t[#%-4d, #%-4d] | 0x%-2x | %pa | %pad | 0x%-9lx\n", + pt_index, pt_index + i - 1, + addr[pt_index] >> SMMU_PTE_ATTR_SHIFT, + &pa, &iova, size * i); + } + seq_puts(s, "\t}\n"); + } + + spin_unlock_irqrestore(&as->lock, flags); + + seq_puts(s, "}\n"); + seq_printf(s, "Total PDE count: %d\n", pde_count); + seq_printf(s, "Total PTE count: %lld\n", pte_count); + +unlock: + mutex_unlock(&smmu->lock); + + return 0; +} + +DEFINE_SHOW_ATTRIBUTE(tegra_smmu_mappings); + static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu) { + const struct tegra_smmu_soc *soc = smmu->soc; + struct tegra_smmu_group_debug *group_debug; + struct device *dev = smmu->dev; + struct dentry *d; + unsigned int i; + + group_debug = devm_kcalloc(dev, soc->num_swgroups, sizeof(*group_debug), GFP_KERNEL); + if (!group_debug) + return; + smmu->debugfs = debugfs_create_dir("smmu", NULL); if (!smmu->debugfs) return; @@ -1068,6 +1218,21 @@ static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu) &tegra_smmu_swgroups_fops); debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu, &tegra_smmu_clients_fops); + d = debugfs_create_dir("mappings", smmu->debugfs); + + for (i = 0; i < soc->num_swgroups; i++) { + const struct tegra_smmu_swgroup *group = &soc->swgroups[i]; + + if (!group->name) + continue; + + group_debug[i].group = group; + + debugfs_create_file(group->name, 0444, d, &group_debug[i], + &tegra_smmu_mappings_fops); + } + + smmu->group_debug = group_debug; } static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu) -- 2.17.1