Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp252385ybb; Thu, 19 Mar 2020 21:14:13 -0700 (PDT) X-Google-Smtp-Source: ADFU+vucj6hlVJbkz5BZnhMIp04XSLmu1OLV2eGFf4bEU71uV6EM8yW0LbP0EhKTBIka2crxOArF X-Received: by 2002:aca:be08:: with SMTP id o8mr4741224oif.101.1584677652845; Thu, 19 Mar 2020 21:14:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584677652; cv=none; d=google.com; s=arc-20160816; b=Gghm2rJmZj1iqXMsmi1rlg5abPNPf1FnaYodVu2OlRJqtkCYkyWLURDmejWlGGV/PD IQibtFIxWLslfLdYGZ3fcG2lk04NX1t1nmhAegxV+iaPl0WL+pYCN30vnS5OOTaltBm+ TxO6shykbLWtoZBlIPwVqBifN943zBe4I/tVrhW52xYI2gCCeznXfE2Ni2W6zj6dMQDJ nZAO7JsBX1tBa4fEVcas7MFmNRwqo+6dG++Vaoyf+Lt8cxJvw/+WZ9Qlns+lmium+s7/ 4OMs3vrd1ol1LDd/9adh92ggtJEbRxGUE6i5aqY2rzzZT41tYRo0BoEQB+YRDPZ6EO/y aJMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:ironport-sdr:ironport-sdr; bh=T/GqLGEqwkDvU2XpZgKDxmNMukg8YgCKIS2Wn9m2uCA=; b=acf8XRemhStSa+r+tNu922R9JBi28R7/8F65jVnhPMXiZrQDf7WHmsBmpq6d61H4NA jIDDJ2jS6a8NNqQ+F0k+G8V7827Jazu0yzppsvHLS/HLBunGpMGsf9zrKdNubqJMImPw 9+OCK4kkurLLu1vhEu1oVDWJf/vQXrMImNiVftPkKBcH832FyTu3bMzXzkxrsxv+Y9aI ofPwOk7h6abZ+A6Ja2hDuJ0eEujeXQ5K4OFpYIsk4vOWZh/fPaM1LV7ywKPeHx0/A9FI KB2ROFhF+6cxyne9S9PIIz1+G+OCvilWIv50X9YJBQMzIz74kyHshdWS8PJEJfK8I1nE Pi2A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t11si2251099oig.108.2020.03.19.21.14.01; Thu, 19 Mar 2020 21:14:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727049AbgCTENk (ORCPT + 99 others); Fri, 20 Mar 2020 00:13:40 -0400 Received: from mga17.intel.com ([192.55.52.151]:24678 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726030AbgCTENk (ORCPT ); Fri, 20 Mar 2020 00:13:40 -0400 IronPort-SDR: OO6+9lypHaCruljyP9wzfkfo9utR+CweNy43OI1oBhkhwif1gpkYfNBhSUqqynrMtc7DsY8/L+ Y172kelJ1FmA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Mar 2020 21:13:40 -0700 IronPort-SDR: zfCQ6rsfjTisTMrOsRO6JR9sAeh+zg76FwUjQb11I/t3FkvxATNIJ5fuTPYL80iHEO8Epdi373 8pRofmeJ7puw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,283,1580803200"; d="scan'208";a="280306863" Received: from kmp-skylake-client-platform.sc.intel.com ([172.25.112.108]) by fmsmga002.fm.intel.com with ESMTP; 19 Mar 2020 21:13:38 -0700 From: Kyung Min Park To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, gregkh@linuxfoundation.org, ak@linux.intel.com, tony.luck@intel.com, ashok.raj@intel.com, ravi.v.shankar@intel.com, fenghua.yu@intel.com, kyung.min.park@intel.com Subject: [PATCH v2 2/2] x86/delay: Introduce TPAUSE delay Date: Thu, 19 Mar 2020 21:13:24 -0700 Message-Id: <1584677604-32707-3-git-send-email-kyung.min.park@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1584677604-32707-1-git-send-email-kyung.min.park@intel.com> References: <1584677604-32707-1-git-send-email-kyung.min.park@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org TPAUSE instructs the processor to enter an implementation-dependent optimized state. The instruction execution wakes up when the time-stamp counter reaches or exceeds the implicit EDX:EAX 64-bit input value. The instruction execution also wakes up due to the expiration of the operating system time-limit or by an external interrupt or exceptions such as a debug exception or a machine check exception. TPAUSE offers a choice of two lower power states: 1. Light-weight power/performance optimized state C0.1 2. Improved power/performance optimized state C0.2 This way, it can save power with low wake-up latency in comparison to spinloop based delay. The selection between the two is governed by the input register. TPAUSE is available on processors with X86_FEATURE_WAITPKG. Reviewed-by: Tony Luck Co-developed-by: Fenghua Yu Signed-off-by: Fenghua Yu Signed-off-by: Kyung Min Park --- arch/x86/include/asm/mwait.h | 17 +++++++++++++++++ arch/x86/lib/delay.c | 27 ++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/mwait.h b/arch/x86/include/asm/mwait.h index aaf6643..fd59db0 100644 --- a/arch/x86/include/asm/mwait.h +++ b/arch/x86/include/asm/mwait.h @@ -22,6 +22,8 @@ #define MWAITX_ECX_TIMER_ENABLE BIT(1) #define MWAITX_MAX_WAIT_CYCLES UINT_MAX #define MWAITX_DISABLE_CSTATES 0xf0 +#define TPAUSE_C01_STATE 1 +#define TPAUSE_C02_STATE 0 static inline void __monitor(const void *eax, unsigned long ecx, unsigned long edx) @@ -120,4 +122,19 @@ static inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx) current_clr_polling(); } +/* + * Caller can specify whether to enter C0.1 (low latency, less + * power saving) or C0.2 state (saves more power, but longer wakeup + * latency). This may be overridden by the IA32_UMWAIT_CONTROL MSR + * which can force requests for C0.2 to be downgraded to C0.1. + */ +static inline void __tpause(unsigned int ecx, unsigned int edx, + unsigned int eax) +{ + /* "tpause %ecx, %edx, %eax;" */ + asm volatile(".byte 0x66, 0x0f, 0xae, 0xf1\t\n" + : + : "c"(ecx), "d"(edx), "a"(eax)); +} + #endif /* _ASM_X86_MWAIT_H */ diff --git a/arch/x86/lib/delay.c b/arch/x86/lib/delay.c index e6db855..5f11f0a 100644 --- a/arch/x86/lib/delay.c +++ b/arch/x86/lib/delay.c @@ -97,6 +97,27 @@ static void delay_tsc(u64 cycles) } /* + * On Intel the TPAUSE instruction waits until any of: + * 1) the TSC counter exceeds the value provided in EAX:EDX + * 2) global timeout in IA32_UMWAIT_CONTROL is exceeded + * 3) an external interrupt occurs + */ +static void delay_halt_tpause(u64 start, u64 cycles) +{ + u64 until = start + cycles; + unsigned int eax, edx; + + eax = (unsigned int)(until & 0xffffffff); + edx = (unsigned int)(until >> 32); + + /* + * Hard code the deeper (C0.2) sleep state because exit latency is + * small compared to the "microseconds" that usleep() will delay. + */ + __tpause(TPAUSE_C02_STATE, edx, eax); +} + +/* * On some AMD platforms, MWAITX has a configurable 32-bit timer, that * counts with TSC frequency. The input value is the number of TSC cycles * to wait. MWAITX will also exit when the timer expires. @@ -152,8 +173,12 @@ static void delay_halt(u64 __cycles) void use_tsc_delay(void) { - if (delay_fn == delay_loop) + if (static_cpu_has(X86_FEATURE_WAITPKG)) { + delay_halt_fn = delay_halt_tpause; + delay_fn = delay_halt; + } else if (delay_fn == delay_loop) { delay_fn = delay_tsc; + } } void use_mwaitx_delay(void) -- 2.7.4