Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp2025716rwl; Fri, 31 Mar 2023 23:38:10 -0700 (PDT) X-Google-Smtp-Source: AKy350aMpkGYneajAyrcOJ1/zijaPyxAYNH7isfxM8UZbsbqJ2kYfU1XhrknaZGd6qQFHWRhgzXU X-Received: by 2002:a05:6a00:1c91:b0:5e4:f141:568b with SMTP id y17-20020a056a001c9100b005e4f141568bmr8564551pfw.3.1680331090618; Fri, 31 Mar 2023 23:38:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680331090; cv=none; d=google.com; s=arc-20160816; b=oqzVqKr+NCCcTXoUJurU0r/QMw+ui9bJ5bVya2hsp3mvqTmY3ppXF0h/pyZeM4MYib aDJHOcoGOUAnABjZWDyMrgr2zXIbO6vjW6uRutPMhzinShU2GIGYzHr5ZwzXkhVuoC4a nYITF3nQSntW9SBSea5VUjIVz8RROh+Oce+0p2gSB2MNgBKW3qklqIWlPOXIDHhow1Oi XaN6tEl44aDTUfhnBR9mjyWd+20V33b7cEZw8vKhAB4LGOaoRVO9d0yPf24sE5xEB7Oo KT1QU+hcdCTf9Z9ES7tQ41XinAlXlcIcBz9/G92CPn7YUNzqlzaK3qIZ46Fc7w3gSF3V ywHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7+r4owlpRl68SnHTZez3F/1Q0HJdHfyMYOiLaES+G08=; b=RXvR2IUsiLt2XQuc+jjUV8IQadqSg0EQR7DSvo7bpN5kG5WJPrYlRFNGnXaIglJihm DmU6BBwSEAX2vfGr26yizEfWp/rT0jO3nLL3OYi6IJswfEFgqcUzG5tNtJn+XurKE9Tc h1MtXL2Z256gDgo9v3iKJ16Rd95Eh/qEtx5wtqY0sfRJn9Kr1EOzHeWoI0EUD2zY5JbC ARJtHs4k3bIAhPRmH46ugjmDJ3/1tD2+Gd778gmzY3mlWXngE0Xgp0NGF6gABqDPd8Po zthIOytIPqMQATQWCLgjONS3le5/051ZIfSc8EgwBNG+7Z1O6cLqEWJXfvcLmx5ysJfA r3ZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=IeBqVbmI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i4-20020a63d444000000b0051398cda533si4177274pgj.388.2023.03.31.23.37.59; Fri, 31 Mar 2023 23:38:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=IeBqVbmI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233296AbjDAGhO (ORCPT + 99 others); Sat, 1 Apr 2023 02:37:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233283AbjDAGhK (ORCPT ); Sat, 1 Apr 2023 02:37:10 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DBA11D86D for ; Fri, 31 Mar 2023 23:37:08 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id D459421A1E; Sat, 1 Apr 2023 06:37:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1680331026; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7+r4owlpRl68SnHTZez3F/1Q0HJdHfyMYOiLaES+G08=; b=IeBqVbmIMhtC5DMJs1CgV3RogvScaXZSq6C0oCoJUDt5DzNA78qzAcxx+++u6Jic+OWjMf ccxznvwrrJc41E+ot9pBd551crYP+rPHKnjKc6DoRVG55r8D7+H7NEFWXB+AY571usGbuN tnGZH9Qvi4hzt1Y0V63R1R+2jZinsto= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 92D9B134FB; Sat, 1 Apr 2023 06:37:06 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id KxKEIhLRJ2QcdwAAMHmgww (envelope-from ); Sat, 01 Apr 2023 06:37:06 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Juergen Gross , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Michael Kelley Subject: [PATCH v5 02/15] x86/mtrr: optimize mtrr_calc_physbits() Date: Sat, 1 Apr 2023 08:36:39 +0200 Message-Id: <20230401063652.23522-3-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230401063652.23522-1-jgross@suse.com> References: <20230401063652.23522-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Optimize mtrr_calc_physbits() for better readability. Drop a stale comment, as reality has made it obsolete. [ bp: - s/mtrr/MTRR/ - s/boot_cpu_has/cpu_feature_enabled/ - use GENMASK_ULL - simplify. ] Signed-off-by: Juergen Gross Signed-off-by: Borislav Petkov (AMD) Tested-by: Michael Kelley --- V3: - new patch, split off from previous patch (Boris Petkov) V5: - add some modifications by Boris Petkov --- arch/x86/kernel/cpu/mtrr/mtrr.c | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/arch/x86/kernel/cpu/mtrr/mtrr.c b/arch/x86/kernel/cpu/mtrr/mtrr.c index 8310bdb111d0..ce0b82209ad3 100644 --- a/arch/x86/kernel/cpu/mtrr/mtrr.c +++ b/arch/x86/kernel/cpu/mtrr/mtrr.c @@ -619,8 +619,6 @@ static struct syscore_ops mtrr_syscore_ops = { int __initdata changed_by_mtrr_cleanup; -#define SIZE_OR_MASK_BITS(n) (~((1ULL << ((n) - PAGE_SHIFT)) - 1)) - static unsigned int __init mtrr_calc_physbits(bool generic) { unsigned int phys_addr; @@ -628,15 +626,8 @@ static unsigned int __init mtrr_calc_physbits(bool generic) phys_addr = 32; if (generic) { - size_or_mask = SIZE_OR_MASK_BITS(36); - size_and_mask = 0x00f00000; phys_addr = 36; - /* - * This is an AMD specific MSR, but we assume(hope?) that - * Intel will implement it too when they extend the address - * bus of the Xeon. - */ if (cpuid_eax(0x80000000) >= 0x80000008) { phys_addr = cpuid_eax(0x80000008) & 0xff; /* CPUID workaround for Intel 0F33/0F34 CPU */ @@ -647,41 +638,37 @@ static unsigned int __init mtrr_calc_physbits(bool generic) boot_cpu_data.x86_stepping == 0x4)) phys_addr = 36; - size_or_mask = SIZE_OR_MASK_BITS(phys_addr); - size_and_mask = ~size_or_mask & 0xfffff00000ULL; } else if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR && boot_cpu_data.x86 == 6) { /* * VIA C* family have Intel style MTRRs, * but don't support PAE */ - size_or_mask = SIZE_OR_MASK_BITS(32); - size_and_mask = 0; phys_addr = 32; } - } else { - size_or_mask = SIZE_OR_MASK_BITS(32); - size_and_mask = 0; } + size_or_mask = ~GENMASK_ULL(phys_addr - PAGE_SHIFT - 1, 0); + size_and_mask = ~size_or_mask & GENMASK_ULL(39, 20); + return phys_addr; } /** - * mtrr_bp_init - initialize mtrrs on the boot CPU + * mtrr_bp_init - initialize MTRRs on the boot CPU * * This needs to be called early; before any of the other CPUs are * initialized (i.e. before smp_init()). - * */ void __init mtrr_bp_init(void) { + bool generic_mtrrs = cpu_feature_enabled(X86_FEATURE_MTRR); const char *why = "(not available)"; unsigned int phys_addr; - phys_addr = mtrr_calc_physbits(boot_cpu_has(X86_FEATURE_MTRR)); + phys_addr = mtrr_calc_physbits(generic_mtrrs); - if (boot_cpu_has(X86_FEATURE_MTRR)) { + if (generic_mtrrs) { mtrr_if = &generic_mtrr_ops; } else { switch (boot_cpu_data.x86_vendor) { -- 2.35.3