From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 051BF4233C;
	Mon,  9 Oct 2023 16:06:23 +0200 (CEST)
Received: from mails.dpdk.org (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 8316E40A81;
	Mon,  9 Oct 2023 16:06:10 +0200 (CEST)
Received: from NAM10-DM6-obe.outbound.protection.outlook.com
 (mail-dm6nam10on2075.outbound.protection.outlook.com [40.107.93.75])
 by mails.dpdk.org (Postfix) with ESMTP id B283C4026B
 for <dev@dpdk.org>; Mon,  9 Oct 2023 16:06:06 +0200 (CEST)
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
 b=NQ5ujkZpFs4SCTiKQsxYUPNtpqDoulCPZkw2oJw0SBbgy+yspvE2WfjAilG/q0x7TfEKD53/CeMiYPea31WbYiQAHjcPUn61gAQSazeyjgwufghGooepL7lCd3pvkFeDafHfSMQOzoKE08ggAJRVzC4Di4HIVKSbZyNDlD0efH0rp25d68HkLlKy/DQ7I/zUB/5ytByaGz9hHyFXML9k7muK09vWUCLyoML7h4ig2KbXHhPQuDXSygwk0vKpqJI4EnitMnv9Sq+3CQ34kKPiNAbn99Rga8gFEP6/OBdCP6LhfPIm3ptvWpUZ1l+sLthEn7UkGOOWQ5oSQYbzPfLkVg==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; 
 s=arcselector9901;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1;
 bh=ivKCXEKCcA0KwMuCfWevSRC54dcc85CrmVRahiwL99M=;
 b=jatG6CU+RLI2uvtyUgRiDEGen7R0MgMeeFFTxwxYNcQ7ZlH0x0h4O2FCP5gVL3lcQxjTSqQyokZuYmhY9IjK4tl4Fyq9O8jwVwtGuZvPrGlc8gY9t7yQwsrAKnoflkAD47ubNeppureuKqidlQuFK16iXgjhZ4+HN8AZN7VNlvfDHKr3d/WsiULXsh1tjmwjbTQXVMmeMwGIgmvBX4NhS+1iHPeNLAqzefY12GuoDQ10gdgZyzdwK0/68I8IJGT8cukYw534zNuJA5tDCXJ4ZG2AOTrJg7rDAEYNINwYJLDUeYgPTIEnaB76V7p0J+xBizRoTiUSxJ/KHkULNqVzOQ==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is
 165.204.84.17) smtp.rcpttodomain=redhat.com smtp.mailfrom=amd.com; dmarc=pass
 (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com;
 dkim=none (message not signed); arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; 
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=ivKCXEKCcA0KwMuCfWevSRC54dcc85CrmVRahiwL99M=;
 b=OYqMFIOVcsvUN1MxC6kkKycUW9YDipkOrhkU65ZTe7KI9Ml1QIjWWlVhBDcSERVQdYOLFtx5kSbVgpMTvOH9HiRXN5NneZo6p1UcwmlTRQt7sEVlVEzwnWw4HBpwB2x3RZXPEYq5yqr+IHahLHogpfFZ+fWjMCyBEQwSREr7hH0=
Received: from CH0PR13CA0056.namprd13.prod.outlook.com (2603:10b6:610:b2::31)
 by DS0PR12MB9424.namprd12.prod.outlook.com (2603:10b6:8:1b4::6) with
 Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6863.36; Mon, 9 Oct
 2023 14:06:05 +0000
Received: from DS2PEPF00003444.namprd04.prod.outlook.com
 (2603:10b6:610:b2:cafe::18) by CH0PR13CA0056.outlook.office365.com
 (2603:10b6:610:b2::31) with Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6886.20 via Frontend
 Transport; Mon, 9 Oct 2023 14:06:04 +0000
X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17)
 smtp.mailfrom=amd.com; dkim=none (message not signed)
 header.d=none;dmarc=pass action=none header.from=amd.com;
Received-SPF: Pass (protection.outlook.com: domain of amd.com designates
 165.204.84.17 as permitted sender) receiver=protection.outlook.com;
 client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C
Received: from SATLEXMB04.amd.com (165.204.84.17) by
 DS2PEPF00003444.mail.protection.outlook.com (10.167.17.71) with Microsoft
 SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id
 15.20.6838.14 via Frontend Transport; Mon, 9 Oct 2023 14:06:04 +0000
Received: from telco-siena.amd.com (10.180.168.240) by SATLEXMB04.amd.com
 (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Mon, 9 Oct
 2023 09:06:03 -0500
From: Sivaprasad Tummala <sivaprasad.tummala@amd.com>
To: <david.marchand@redhat.com>
CC: <david.hunt@intel.com>, <konstantin.v.ananyev@yandex.ru>,
 <roretzla@linux.microsoft.com>, <anatoly.burakov@intel.com>,
 <thomas@monjalon.net>, <ferruh.yigit@amd.com>, <dev@dpdk.org>
Subject: [PATCH v6 3/3] power: amd power monitor support
Date: Mon, 9 Oct 2023 07:05:46 -0700
Message-ID: <20231009140546.862553-3-sivaprasad.tummala@amd.com>
X-Mailer: git-send-email 2.34.1
In-Reply-To: <20231009140546.862553-1-sivaprasad.tummala@amd.com>
References: <20230816185959.1331336-3-sivaprasad.tummala@amd.com>
 <20231009140546.862553-1-sivaprasad.tummala@amd.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 8bit
X-Originating-IP: [10.180.168.240]
X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com
 (10.181.40.145)
X-EOPAttributedMessage: 0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: DS2PEPF00003444:EE_|DS0PR12MB9424:EE_
X-MS-Office365-Filtering-Correlation-Id: 6545c65d-2983-4675-20ed-08dbc8d0e089
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam: BCL:0;
X-Microsoft-Antispam-Message-Info: FTcoHvsyTVika3vcVEIG2HAvP4IRAJl353oy6BjLwNYvsujXMOL7/Nd73Wz1n+y084rFAh6NdhPKebCo/Ewbm/oyp2hFZvlzW3wW0rGuFYVsEnSIni7EfqijbyK15jwMWGfxPMdCpWeH6ViyxBFVtg/8Qw922SQvvqFC6PpCN6ea7hUN1SPasuOzxnZ35hAgtBM1v2wDhFxvTGyiP3V0YjpcsCZbQG9Mx+IlNDCFl50zGCMzS2RyYkgETznjZTnIHR8/zHydQ5Vu3ja82ISRZEFgEc+Icr7cMEnbCgojQ1FK3n3tDQndFQr76KVUkXLH2j5AODfCvTAWrEYauonO01VUPVCV7BGH1OjoOEbybgTRU9Ya4h/xwZkYSLXd5XEynQpq3+/Tb2SS0ksqqw+UxXbSV7ml60YhV6pH3X7iD+If93FjtOjYM8Emf9c/gmuRulZ616ByH4Hgz5BKSv7WmF2uX7Pd4QZG8MNB7dpX3FHijdg3oi3Iprr60uUjCz1cPpJO3zTAwea21zAyLXB431exLjjHog7k9Dl940gCzb/h1MGLoZ6WmBhZfTVx9trPe439mWh5G1veyu0E1MbnxvwwHlKeJqF3rgzzjKMIJ7+SKDiGFlSzcNOYUAUBJ/sqeT03BihNbM6yYY+VF14kraIF4wyUC65XHuWR7ae9oz31JtDn3HrVU25U8so21OCmXw0XEeETYFM18Pc1/vBxSZzxy9zSlpUT17pavgoi574z5A/iSWSMv51OrQP3RQIblGDmysTNgSlHXo4U0MRjvg==
X-Forefront-Antispam-Report: CIP:165.204.84.17; CTRY:US; LANG:en; SCL:1; SRV:;
 IPV:CAL; SFV:NSPM; H:SATLEXMB04.amd.com; PTR:InfoDomainNonexistent; CAT:NONE;
 SFS:(13230031)(4636009)(39860400002)(346002)(136003)(396003)(376002)(230922051799003)(82310400011)(451199024)(186009)(1800799009)(64100799003)(40470700004)(36840700001)(46966006)(7696005)(2616005)(1076003)(41300700001)(478600001)(47076005)(336012)(426003)(26005)(44832011)(83380400001)(2906002)(6916009)(70586007)(5660300002)(70206006)(54906003)(316002)(4326008)(8676002)(8936002)(16526019)(40460700003)(82740400003)(36860700001)(356005)(36756003)(81166007)(40480700001)(86362001)(6666004)(36900700001);
 DIR:OUT; SFP:1101; 
X-OriginatorOrg: amd.com
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Oct 2023 14:06:04.8111 (UTC)
X-MS-Exchange-CrossTenant-Network-Message-Id: 6545c65d-2983-4675-20ed-08dbc8d0e089
X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d
X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17];
 Helo=[SATLEXMB04.amd.com]
X-MS-Exchange-CrossTenant-AuthSource: DS2PEPF00003444.namprd04.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Anonymous
X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem
X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB9424
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

mwaitx allows EPYC processors to enter a implementation dependent
power/performance optimized state (C1 state) for a specific period
or until a store to the monitored address range.

Signed-off-by: Sivaprasad Tummala <sivaprasad.tummala@amd.com>
Acked-by: Anatoly Burakov <anatoly.burakov@intel.com>
---
 lib/eal/x86/rte_power_intrinsics.c | 108 ++++++++++++++++++++++-------
 1 file changed, 84 insertions(+), 24 deletions(-)

diff --git a/lib/eal/x86/rte_power_intrinsics.c b/lib/eal/x86/rte_power_intrinsics.c
index 664cde01e9..0d2953f570 100644
--- a/lib/eal/x86/rte_power_intrinsics.c
+++ b/lib/eal/x86/rte_power_intrinsics.c
@@ -17,6 +17,78 @@ static struct power_wait_status {
 	volatile void *monitor_addr; /**< NULL if not currently sleeping */
 } __rte_cache_aligned wait_status[RTE_MAX_LCORE];
 
+/**
+ * This functions uses UMONITOR/UMWAIT instructions and will enter C0.2 state.
+ * For more information about usage of these instructions, please refer to
+ * Intel(R) 64 and IA-32 Architectures Software Developer's Manual.
+ */
+static void intel_umonitor(volatile void *addr)
+{
+#if defined(RTE_TOOLCHAIN_MSVC) || defined(__WAITPKG__)
+	/* cast away "volatile" when using the intrinsic */
+	_umonitor((void *)(uintptr_t)addr);
+#else
+	/*
+	 * we're using raw byte codes for compiler versions which
+	 * don't support this instruction natively.
+	 */
+	asm volatile(".byte 0xf3, 0x0f, 0xae, 0xf7;"
+			:
+			: "D"(addr));
+#endif
+}
+
+static void intel_umwait(const uint64_t timeout)
+{
+	const uint32_t tsc_l = (uint32_t)timeout;
+	const uint32_t tsc_h = (uint32_t)(timeout >> 32);
+#if defined(RTE_TOOLCHAIN_MSVC) || defined(__WAITPKG__)
+	_umwait(tsc_l, tsc_h);
+#else
+	asm volatile(".byte 0xf2, 0x0f, 0xae, 0xf7;"
+			: /* ignore rflags */
+			: "D"(0), /* enter C0.2 */
+			  "a"(tsc_l), "d"(tsc_h));
+#endif
+}
+
+/**
+ * This functions uses MONITORX/MWAITX instructions and will enter C1 state.
+ * For more information about usage of these instructions, please refer to
+ * AMD64 Architecture Programmer’s Manual.
+ */
+static void amd_monitorx(volatile void *addr)
+{
+#if defined(__MWAITX__)
+	/* cast away "volatile" when using the intrinsic */
+	_mm_monitorx((void *)(uintptr_t)addr, 0, 0);
+#else
+	asm volatile(".byte 0x0f, 0x01, 0xfa;"
+			:
+			: "a"(addr),
+			"c"(0),  /* no extensions */
+			"d"(0)); /* no hints */
+#endif
+}
+
+static void amd_mwaitx(const uint64_t timeout)
+{
+	RTE_SET_USED(timeout);
+#if defined(__MWAITX__)
+	_mm_mwaitx(0, 0, 0);
+#else
+	asm volatile(".byte 0x0f, 0x01, 0xfb;"
+			: /* ignore rflags */
+			: "a"(0), /* enter C1 */
+			"c"(0)); /* no time-out */
+#endif
+}
+
+static struct {
+	void (*mmonitor)(volatile void *addr);
+	void (*mwait)(const uint64_t timeout);
+} __rte_cache_aligned power_monitor_ops;
+
 static inline void
 __umwait_wakeup(volatile void *addr)
 {
@@ -76,8 +148,6 @@ int
 rte_power_monitor(const struct rte_power_monitor_cond *pmc,
 		const uint64_t tsc_timestamp)
 {
-	const uint32_t tsc_l = (uint32_t)tsc_timestamp;
-	const uint32_t tsc_h = (uint32_t)(tsc_timestamp >> 32);
 	const unsigned int lcore_id = rte_lcore_id();
 	struct power_wait_status *s;
 	uint64_t cur_value;
@@ -105,19 +175,8 @@ rte_power_monitor(const struct rte_power_monitor_cond *pmc,
 	rte_spinlock_lock(&s->lock);
 	s->monitor_addr = pmc->addr;
 
-	/* set address for UMONITOR */
-#if defined(RTE_TOOLCHAIN_MSVC) || defined(__WAITPKG__)
-	/* cast away "volatile" when using the intrinsic */
-	_umonitor((void *)(uintptr_t)pmc->addr);
-#else
-	/*
-	 * we're using raw byte codes for compiler versions which
-	 * don't support this instruction natively.
-	 */
-	asm volatile(".byte 0xf3, 0x0f, 0xae, 0xf7;"
-			:
-			: "D"(pmc->addr));
-#endif
+	/* set address for memory monitor */
+	power_monitor_ops.mmonitor(pmc->addr);
 
 	/* now that we've put this address into monitor, we can unlock */
 	rte_spinlock_unlock(&s->lock);
@@ -128,15 +187,8 @@ rte_power_monitor(const struct rte_power_monitor_cond *pmc,
 	if (pmc->fn(cur_value, pmc->opaque) != 0)
 		goto end;
 
-	/* execute UMWAIT */
-#if defined(RTE_TOOLCHAIN_MSVC) || defined(__WAITPKG__)
-	_umwait(tsc_l, tsc_h);
-#else
-	asm volatile(".byte 0xf2, 0x0f, 0xae, 0xf7;"
-			: /* ignore rflags */
-			: "D"(0), /* enter C0.2 */
-			  "a"(tsc_l), "d"(tsc_h));
-#endif
+	/* execute mwait */
+	power_monitor_ops.mwait(tsc_timestamp);
 
 end:
 	/* erase sleep address */
@@ -186,6 +238,14 @@ RTE_INIT(rte_power_intrinsics_init) {
 		wait_multi_supported = 1;
 	if (i.power_monitor)
 		monitor_supported = 1;
+
+	if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_MONITORX)) { /* AMD */
+		power_monitor_ops.mmonitor = &amd_monitorx;
+		power_monitor_ops.mwait = &amd_mwaitx;
+	} else { /* Intel */
+		power_monitor_ops.mmonitor = &intel_umonitor;
+		power_monitor_ops.mwait = &intel_umwait;
+	}
 }
 
 int
-- 
2.34.1