stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>,
	Tj <ml.linux@elloe.vision>, Joerg Roedel <jroedel@suse.de>,
	Sasha Levin <sashal@kernel.org>,
	iommu@lists.linux-foundation.org
Subject: [PATCH AUTOSEL 5.10 21/47] iommu/amd: Fix performance counter initialization
Date: Tue,  2 Mar 2021 06:56:20 -0500	[thread overview]
Message-ID: <20210302115646.62291-21-sashal@kernel.org> (raw)
In-Reply-To: <20210302115646.62291-1-sashal@kernel.org>

From: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>

[ Upstream commit 6778ff5b21bd8e78c8bd547fd66437cf2657fd9b ]

Certain AMD platforms enable power gating feature for IOMMU PMC,
which prevents the IOMMU driver from updating the counter while
trying to validate the PMC functionality in the init_iommu_perf_ctr().
This results in disabling PMC support and the following error message:

    "AMD-Vi: Unable to read/write to IOMMU perf counter"

To workaround this issue, disable power gating temporarily by programming
the counter source to non-zero value while validating the counter,
and restore the prior state afterward.

Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Tested-by: Tj (Elloe Linux) <ml.linux@elloe.vision>
Link: https://lore.kernel.org/r/20210208122712.5048-1-suravee.suthikulpanit@amd.com
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=201753
Signed-off-by: Joerg Roedel <jroedel@suse.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/iommu/amd/init.c | 45 ++++++++++++++++++++++++++++++----------
 1 file changed, 34 insertions(+), 11 deletions(-)

diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c
index c842545368fd..3c215f0a6052 100644
--- a/drivers/iommu/amd/init.c
+++ b/drivers/iommu/amd/init.c
@@ -12,6 +12,7 @@
 #include <linux/acpi.h>
 #include <linux/list.h>
 #include <linux/bitmap.h>
+#include <linux/delay.h>
 #include <linux/slab.h>
 #include <linux/syscore_ops.h>
 #include <linux/interrupt.h>
@@ -254,6 +255,8 @@ static enum iommu_init_state init_state = IOMMU_START_STATE;
 static int amd_iommu_enable_interrupts(void);
 static int __init iommu_go_to_state(enum iommu_init_state state);
 static void init_device_table_dma(void);
+static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
+				u8 fxn, u64 *value, bool is_write);
 
 static bool amd_iommu_pre_enabled = true;
 
@@ -1717,13 +1720,11 @@ static int __init init_iommu_all(struct acpi_table_header *table)
 	return 0;
 }
 
-static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
-				u8 fxn, u64 *value, bool is_write);
-
-static void init_iommu_perf_ctr(struct amd_iommu *iommu)
+static void __init init_iommu_perf_ctr(struct amd_iommu *iommu)
 {
+	int retry;
 	struct pci_dev *pdev = iommu->dev;
-	u64 val = 0xabcd, val2 = 0, save_reg = 0;
+	u64 val = 0xabcd, val2 = 0, save_reg, save_src;
 
 	if (!iommu_feature(iommu, FEATURE_PC))
 		return;
@@ -1731,17 +1732,39 @@ static void init_iommu_perf_ctr(struct amd_iommu *iommu)
 	amd_iommu_pc_present = true;
 
 	/* save the value to restore, if writable */
-	if (iommu_pc_get_set_reg(iommu, 0, 0, 0, &save_reg, false))
+	if (iommu_pc_get_set_reg(iommu, 0, 0, 0, &save_reg, false) ||
+	    iommu_pc_get_set_reg(iommu, 0, 0, 8, &save_src, false))
 		goto pc_false;
 
-	/* Check if the performance counters can be written to */
-	if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
-	    (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
-	    (val != val2))
+	/*
+	 * Disable power gating by programing the performance counter
+	 * source to 20 (i.e. counts the reads and writes from/to IOMMU
+	 * Reserved Register [MMIO Offset 1FF8h] that are ignored.),
+	 * which never get incremented during this init phase.
+	 * (Note: The event is also deprecated.)
+	 */
+	val = 20;
+	if (iommu_pc_get_set_reg(iommu, 0, 0, 8, &val, true))
 		goto pc_false;
 
+	/* Check if the performance counters can be written to */
+	val = 0xabcd;
+	for (retry = 5; retry; retry--) {
+		if (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true) ||
+		    iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false) ||
+		    val2)
+			break;
+
+		/* Wait about 20 msec for power gating to disable and retry. */
+		msleep(20);
+	}
+
 	/* restore */
-	if (iommu_pc_get_set_reg(iommu, 0, 0, 0, &save_reg, true))
+	if (iommu_pc_get_set_reg(iommu, 0, 0, 0, &save_reg, true) ||
+	    iommu_pc_get_set_reg(iommu, 0, 0, 8, &save_src, true))
+		goto pc_false;
+
+	if (val != val2)
 		goto pc_false;
 
 	pci_info(pdev, "IOMMU performance counters supported\n");
-- 
2.30.1


  parent reply	other threads:[~2021-03-03  0:21 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-02 11:56 [PATCH AUTOSEL 5.10 01/47] i2c: rcar: faster irq code to minimize HW race condition Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 02/47] i2c: rcar: optimize cacheline " Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 03/47] scsi: ufs: Add a quirk to permit overriding UniPro defaults Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 04/47] scsi: ufs: WB is only available on LUN #0 to #7 Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 05/47] scsi: ufs: Introduce a quirk to allow only page-aligned sg entries Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 06/47] udf: fix silent AED tagLocation corruption Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 07/47] iommu/vt-d: Clear PRQ overflow only when PRQ is empty Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 08/47] mmc: mxs-mmc: Fix a resource leak in an error handling path in 'mxs_mmc_probe()' Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 09/47] mmc: sdhci-of-dwcmshc: set SDHCI_QUIRK2_PRESET_VALUE_BROKEN Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 10/47] mmc: mediatek: fix race condition between msdc_request_timeout and irq Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 11/47] mmc: sdhci-iproc: Add ACPI bindings for the RPi Sasha Levin
2021-03-02 16:16   ` Jeremy Linton
2021-03-12 22:12     ` Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 12/47] Platform: OLPC: Fix probe error handling Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 13/47] powerpc/pci: Add ppc_md.discover_phbs() Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 14/47] HID: i2c-hid: Add I2C_HID_QUIRK_NO_IRQ_AFTER_RESET for ITE8568 EC on Voyo Winpad A15 Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 15/47] spi: stm32: make spurious and overrun interrupts visible Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 16/47] powerpc: improve handling of unrecoverable system reset Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 17/47] powerpc/perf: Record counter overflow always if SAMPLE_IP is unset Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 18/47] kunit: tool: fix unit test cleanup handling Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 19/47] HID: logitech-dj: add support for the new lightspeed connection iteration Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 20/47] powerpc/64: Fix stack trace not displaying final frame Sasha Levin
2021-03-02 11:56 ` Sasha Levin [this message]
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 22/47] clk: qcom: gdsc: Implement NO_RET_PERIPH flag Sasha Levin
2021-03-02 23:04   ` Stephen Boyd
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 23/47] sparc32: Limit memblock allocation to low memory Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 24/47] sparc64: Use arch_validate_flags() to validate ADI flag Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 25/47] ACPICA: Fix race in generic_serial_bus (I2C) and GPIO op_region parameter handling Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 26/47] Input: applespi - don't wait for responses to commands indefinitely Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 27/47] x86, build: use objtool mcount Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 28/47] PCI: xgene-msi: Fix race in installing chained irq handler Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 29/47] misc: eeprom_93xx46: Add quirk to support Microchip 93LC46B eeprom Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 30/47] PCI: mediatek: Add missing of_node_put() to fix reference leak Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 31/47] drivers/base: build kunit tests without structleak plugin Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 32/47] drm/msm/a5xx: Remove overwriting A5XX_PC_DBG_ECO_CNTL register Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 33/47] PCI/LINK: Remove bandwidth notification Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 34/47] ext4: don't try to processed freed blocks until mballoc is initialized Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 35/47] PCI: cadence: Retrain Link to work around Gen2 training defect Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 36/47] kbuild: clamp SUBLEVEL to 255 Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 37/47] PCI: Fix pci_register_io_range() memory leak Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 38/47] i40e: Fix memory leak in i40e_probe Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 39/47] kasan: fix memory corruption in kasan_bitops_tags test Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 40/47] riscv: Get rid of MAX_EARLY_MAPPING_SIZE Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 41/47] s390/smp: __smp_rescan_cpus() - move cpumask away from stack Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 42/47] drivers/base/memory: don't store phys_device in memory blocks Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 43/47] sysctl.c: fix underflow value setting risk in vm_table Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 44/47] scsi: libiscsi: Fix iscsi_prep_scsi_cmd_pdu() error handling Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 45/47] nbd: handle device refs for DESTROY_ON_DISCONNECT properly Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 46/47] scsi: target: core: Add cmd length set before cmd complete Sasha Levin
2021-03-02 11:56 ` [PATCH AUTOSEL 5.10 47/47] scsi: target: core: Prevent underflow for service actions Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210302115646.62291-21-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=iommu@lists.linux-foundation.org \
    --cc=jroedel@suse.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ml.linux@elloe.vision \
    --cc=stable@vger.kernel.org \
    --cc=suravee.suthikulpanit@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).