Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp449364rdb; Tue, 23 Jan 2024 04:53:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IEjPJyKjzQ2uaHHvbtBLFqY8AHRYaJYChISpk6XjQ49UVrh8rtM2KFgM+qdYC7OpZ3Rq629 X-Received: by 2002:a17:906:578d:b0:a2a:212:cfe5 with SMTP id k13-20020a170906578d00b00a2a0212cfe5mr179502ejq.12.1706014432097; Tue, 23 Jan 2024 04:53:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706014432; cv=pass; d=google.com; s=arc-20160816; b=F20I7fnEqhPR3hM/SEQaE0RKmUfqv2YUcCMsF34z7zbjC+ofHqEHbz9myBkRhV8mOa /Of38fmtCsClub4OzKWLorfXdJhjy/K/dWAy8tVVTWMDEXRqCCTmuidVBI1bYM1Af0wN 3z7Lx4rAFNN786NiNto4X3SlndgDy9OiP1R4cEqUKQXmmN9HbEmXmDFK7BIGAi9EYcIy Jd7MGgqGMvuZsx3v55IPUiJWiZCg3hBLWYSZ+Ry+2YWttaHmHasf7HAvplQnbLn9ra5d GBqqiyNYZmhUf83awrHR+1ZuVI8Qjkr5AGO0k0pEiuBr3HzBooMnfB/zbxNly8NScqhb XRfw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=date:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:subject:cc:to:from:dkim-signature :dkim-signature:message-id; bh=CHwg68P1vAZsPhjQ3yDPqQ0OD7LrrKUWUxcHvl79YxM=; fh=Te0fDPJ/sd+7p881M6fa/oF213lBbJvGdEFWr/TLqNA=; b=ToI9HdjhXS+xSujSqV3VcsuWZ+fHMgdAyhZf4DXrpIm8DzhVVClYZ32uDzDCSerC1/ eraDTJO8a+tPV58OMXHGQYUWEOf07lLm3M45fvMfFie/bwzUsw6aU/5xVa2lBX/XgS5H wE2EtCUImry/+pYTU1NteQifW1uJQIl4g0szFUZ3qUksX7AbJXu/rzfklfCpoxSz803S FWDg3TxY3CuZaYNhl8liRxP8bA3zkmK99KSrg7mcxDd1ZJXUAp/xnoV4rbHavtGuRtx6 X56a/5ZOAkAOJ1gBMiMSpDz2AdAzH9pbvJ50MdEwlrjyk3hzp0jY8LfLmK2ilRBcfwSC aQtA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=FAfpbnaM; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-35265-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35265-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id lt3-20020a170906fa8300b00a23539f10e9si11984235ejb.356.2024.01.23.04.53.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 04:53:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-35265-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=FAfpbnaM; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-35265-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35265-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id AE3E61F233D8 for ; Tue, 23 Jan 2024 12:53:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4652D5F561; Tue, 23 Jan 2024 12:53:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="FAfpbnaM"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="OwA8CbOf" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 06B375F541 for ; Tue, 23 Jan 2024 12:53:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706014415; cv=none; b=K4y+6SPdAT+T1GYZB8u/dJxzGK3e208Y9A3QVUCw+TWfJ0J3cO3xylPV/Xt9XP3rKTWB5ozTIT579MmMGjyjd8W7vLwfAHIfk8QGpvYtmLF6mJHnZpdrohWxjT1nREHqaVspwjbFtZ/HDEapuZ30umyeUuwkuPw2Bj5Y4gxXWyY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706014415; c=relaxed/simple; bh=NE38U5sSq0KEn81KCUffHn/ZAXWYI8/Ra1pEbVF+fpk=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=G3fL9G0iU6u/qogcrcMWZLV0CZDQPzbw7SLj7AJDG3dhzt8QSuSeVHP0mIieiX7qe2/ovmWk1j5QVVZ2vwhmtedRrdR4IZclkDR7Hfsujf2MlJT1DhAg2tQwKpmB38TPKP/JdqwbiNGIw5o9AzTVo4E59Tez5DQX0Ougc7OA9A0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=FAfpbnaM; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=OwA8CbOf; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Message-ID: <20240117115908.344295552@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1706014411; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=CHwg68P1vAZsPhjQ3yDPqQ0OD7LrrKUWUxcHvl79YxM=; b=FAfpbnaM66ByOQaVXjI9rCfUS1eMQPfzpTR08osAdyKX1ioK/p+OAFj97+Wd2dcHFCrLEh J6HQrCw2hTCTXQ8LeUfg+hwtgEoc2c/kED1xSbqayzUbfC+VA+EeiVTmsAPJCJqzDNkgUD uVrNlyRH1hbFZ5PGcnoGExUbzpHFk8RzNIedBTtvk2+I6IBF8GmRtlSTNmaxn/V2fZwa20 axOs5esqRYywB+mIFE+qo8y3CRX3twbxwk8LPzUtT9Z0/d+kcQqo+xmU67auZDGxbhgcBF 24maN2ACq+RzqPKrpOsFVhxmtD9n0BvyE48g/oWDfL4iqO9GCgc+nOa+7Ldetw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1706014411; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=CHwg68P1vAZsPhjQ3yDPqQ0OD7LrrKUWUxcHvl79YxM=; b=OwA8CbOfTHWs1se1A3pSZMnJP84bPpNl3gY+gkKnrNG+TmOfV1qf0s23rNHZUQnQJN8Z34 6M/IP8M9c2Qc0ZBg== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Tom Lendacky , Andrew Cooper , Arjan van de Ven , Huang Rui , Juergen Gross , Dimitri Sivanich , Sohil Mehta , K Prateek Nayak , Kan Liang , Zhang Rui , "Paul E. McKenney" , Feng Tang , Andy Shevchenko , Michael Kelley , "Peter Zijlstra (Intel)" Subject: [patch v5 01/19] x86/cpu: Provide cpuid_read() et al. References: <20240117115752.863482697@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Tue, 23 Jan 2024 13:53:30 +0100 (CET) From: Thomas Gleixner Provide a few helper functions to read CPUID leafs or individual registers into a data structure without requiring unions. Signed-off-by: Thomas Gleixner Tested-by: Juergen Gross Tested-by: Sohil Mehta Tested-by: Michael Kelley Tested-by: Peter Zijlstra (Intel) Tested-by: Zhang Rui Acked-by: Peter Zijlstra (Intel) --- arch/x86/include/asm/cpuid.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) --- --- a/arch/x86/include/asm/cpuid.h +++ b/arch/x86/include/asm/cpuid.h @@ -127,6 +127,42 @@ static inline unsigned int cpuid_edx(uns return edx; } +static inline void __cpuid_read(unsigned int leaf, unsigned int subleaf, u32 *regs) +{ + regs[CPUID_EAX] = leaf; + regs[CPUID_ECX] = subleaf; + __cpuid(regs, regs + 1, regs + 2, regs + 3); +} + +#define cpuid_subleaf(leaf, subleaf, regs) { \ + BUILD_BUG_ON(sizeof(*(regs)) != 16); \ + __cpuid_read(leaf, subleaf, (u32 *)(regs)); \ +} + +#define cpuid_leaf(leaf, regs) { \ + BUILD_BUG_ON(sizeof(*(regs)) != 16); \ + __cpuid_read(leaf, 0, (u32 *)(regs)); \ +} + +static inline void __cpuid_read_reg(unsigned int leaf, unsigned int subleaf, + enum cpuid_regs_idx regidx, u32 *reg) +{ + u32 regs[4]; + + __cpuid_read(leaf, subleaf, regs); + *reg = regs[regidx]; +} + +#define cpuid_subleaf_reg(leaf, subleaf, regidx, reg) { \ + BUILD_BUG_ON(sizeof(*(reg)) != 4); \ + __cpuid_read_reg(leaf, subleaf, regidx, (u32 *)(reg)); \ +} + +#define cpuid_leaf_reg(leaf, regidx, reg) { \ + BUILD_BUG_ON(sizeof(*(reg)) != 4); \ + __cpuid_read_reg(leaf, 0, regidx, (u32 *)(reg)); \ +} + static __always_inline bool cpuid_function_is_indexed(u32 function) { switch (function) {