From patchwork Wed Feb 9 10:21:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Efraim Flashner X-Patchwork-Id: 197 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 527B227BBEA; Wed, 9 Feb 2022 10:52:44 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id E690C27BBE9 for ; Wed, 9 Feb 2022 10:52:43 +0000 (GMT) Received: from localhost ([::1]:38332 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nHkaY-0002Ni-0Z for patchwork@mira.cbaines.net; Wed, 09 Feb 2022 05:52:43 -0500 Received: from eggs.gnu.org ([209.51.188.92]:33002) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nHk93-0002aE-GR for guix-patches@gnu.org; Wed, 09 Feb 2022 05:24:17 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:56071) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nHk8o-0001ej-Dg for guix-patches@gnu.org; Wed, 09 Feb 2022 05:24:16 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nHk8o-00083b-Ae for guix-patches@gnu.org; Wed, 09 Feb 2022 05:24:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#53895] [PATCH 2/5] gnu: cpu: Add detection for AMD CPUs. Resent-From: Efraim Flashner Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 09 Feb 2022 10:24:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 53895 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 53895@debbugs.gnu.org Cc: Efraim Flashner Received: via spool by 53895-submit@debbugs.gnu.org id=B53895.164440218830865 (code B ref 53895); Wed, 09 Feb 2022 10:24:02 +0000 Received: (at 53895) by debbugs.gnu.org; 9 Feb 2022 10:23:08 +0000 Received: from localhost ([127.0.0.1]:49959 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nHk7w-00081l-5R for submit@debbugs.gnu.org; Wed, 09 Feb 2022 05:23:08 -0500 Received: from flashner.co.il ([178.62.234.194]:50040) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nHk7u-00080t-KW for 53895@debbugs.gnu.org; Wed, 09 Feb 2022 05:23:07 -0500 Received: from localhost (unknown [37.46.46.251]) by flashner.co.il (Postfix) with ESMTPSA id EA4C5403CD; Wed, 9 Feb 2022 10:23:00 +0000 (UTC) From: Efraim Flashner Date: Wed, 9 Feb 2022 12:21:48 +0200 Message-Id: X-Mailer: git-send-email 2.34.0 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/cpu.scm : Add vendor field. (current-cpu): Also fill in the 'vendor' field. (cpu->gcc-architecture): Add detection logic for AMD CPUs. --- guix/cpu.scm | 60 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/guix/cpu.scm b/guix/cpu.scm index 5bb3fa9d2f..6d44599822 100644 --- a/guix/cpu.scm +++ b/guix/cpu.scm @@ -27,6 +27,7 @@ (define-module (guix cpu) #:export (current-cpu cpu? cpu-architecture + cpu-vendor cpu-family cpu-model cpu-flags @@ -42,9 +43,10 @@ (define-module (guix cpu) ;; CPU description. (define-record-type - (cpu architecture family model flags) + (cpu architecture vendor family model flags) cpu? (architecture cpu-architecture) ;string, from 'uname' + (vendor cpu-vendor) ;string (family cpu-family) ;integer (model cpu-model) ;integer (flags cpu-flags)) ;set of strings @@ -58,28 +60,33 @@ (define (prefix? prefix) (call-with-input-file "/proc/cpuinfo" (lambda (port) - (let loop ((family #f) + (let loop ((vendor #f) + (family #f) (model #f)) (match (read-line port) ((? eof-object?) #f) + ((? (prefix? "vendor_id") str) + (match (string-tokenize str) + (("vendor_id" ":" vendor) + (loop vendor family model)))) ((? (prefix? "cpu family") str) (match (string-tokenize str) (("cpu" "family" ":" family) - (loop (string->number family) model)))) + (loop vendor (string->number family) model)))) ((? (prefix? "model") str) (match (string-tokenize str) (("model" ":" model) - (loop family (string->number model))) + (loop vendor family (string->number model))) (_ - (loop family model)))) + (loop vendor family model)))) ((? (prefix? "flags") str) (match (string-tokenize str) (("flags" ":" flags ...) (cpu (utsname:machine (uname)) - family model (list->set flags))))) + vendor family model (list->set flags))))) (_ - (loop family model)))))))) + (loop vendor family model)))))))) (define (cpu->gcc-architecture cpu) "Return the architecture name, suitable for GCC's '-march' flag, that @@ -87,7 +94,8 @@ (define (cpu->gcc-architecture cpu) (match (cpu-architecture cpu) ("x86_64" ;; Transcribed from GCC's 'host_detect_local_cpu' in driver-i386.c. - (or (and (= 6 (cpu-family cpu)) ;the "Pentium Pro" family + (or (and (equal? "GenuineIntel" (cpu-vendor cpu)) + (= 6 (cpu-family cpu)) ;the "Pentium Pro" family (letrec-syntax ((if-flags (syntax-rules (=>) ((_) #f) @@ -122,6 +130,40 @@ (define (cpu->gcc-architecture cpu) ("ssse3" => "core2") ("longmode" => "x86-64")))) + (and (equal? "AuthenticAMD" (cpu-vendor cpu)) + (letrec-syntax ((if-flags (syntax-rules (=>) + ((_) + #f) + ((_ (flags ... => name) rest ...) + (if (every (lambda (flag) + (set-contains? (cpu-flags cpu) + flag)) + '(flags ...)) + name + (if-flags rest ...)))))) + + (when (= 22 (cpu-family cpu)) + (if-flags ("movbe" => "btver2"))) + (when (= 6 (cpu-family cpu)) + (if-flags ("3dnowp" => "athalon"))) + + (if-flags ("vaes" => "znver3") + ("clwb" => "znver2") + ("clzero" => "znver1") + ("avx2" => "bdver4") + ("xsaveopt" => "bdver3") + ("bmi" => "bdver2") + ("xop" => "bdver1") + ("sse4a" "has_ssse3" => "btver1") + ("sse4a" => "amdfam10") + ("sse2" "sse3" => "k8-sse3") + ("longmode" "sse3" => "k8-sse3") + ("sse2" => "k8") + ("longmode" => "k8") + ("mmx" "3dnow" => "k6-3") + ("mmx" => "k6") + (_ => "pentium")))) + ;; Fallback case for non-Intel processors or for Intel processors not ;; recognized above. (letrec-syntax ((if-flags (syntax-rules (=>) @@ -147,7 +189,7 @@ (define (cpu->gcc-architecture cpu) ("ssse3" "movbe" => "bonnell") ("ssse3" => "core2"))) - ;; TODO: Recognize AMD models (bdver*, znver*, etc.)? + ;; TODO: Recognize CENTAUR/CYRIX/NSC? "x86_64")) (architecture