From patchwork Mon Apr 24 19:49:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Heiko_St=C3=BCbner?= X-Patchwork-Id: 87152 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2974234vqo; Mon, 24 Apr 2023 12:58:23 -0700 (PDT) X-Google-Smtp-Source: AKy350bHDcgw/k5xZ1h/V5UkWXW9eY38IowDSThiFl5RqrWHtWSPyxIb7gLnzgmZhscRxma+Ex2/ X-Received: by 2002:a17:902:f153:b0:1a1:b172:5428 with SMTP id d19-20020a170902f15300b001a1b1725428mr11744851plb.18.1682366302883; Mon, 24 Apr 2023 12:58:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682366302; cv=none; d=google.com; s=arc-20160816; b=msqgsPDwRpiWr34PCcWgCEUvTY2gX82Qj7/EV6LcHXLevcnNkrCf7mWvO2HLjfv0kW iowTvZkTaPjSlfbmN3wTX8IXwzDgVJFzz1qpRSPcVhXZotEsbix1Sc7psCe/+X7CR7OQ eEppO4spB1t27RTj5nrZREVFo0azOeeq86lzrsO9dWVTw5MVE8dGHt9l24jN8OieKDyO VQOJPt6RvIwLQbEdnzIE1LWW0DKcr0N0d56gla9ZyCGUDc94f5LoxcwseQE+yaH9zkXU 1N6pUSG3tc3Z+n1zCEW1CbtayHZIctnWHGh/JGqKWmbkS1KzQ9ARHRZMeqcwj1S+yWJ+ 3nXA== 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; bh=WpVef7wKAZOD2zMRtPqWRoYyEBvCWl80BGBUi3JUXB8=; b=eimz+8kuFHNHx3Mwv126mea8tw/Nb8XzNCCQFOAOkGd6v8BFbUV2pAcNAJuNy+Gua7 J9rVLYfPAu24COCxM7MUoce9QA7qisYFzAjLQ6xairFHn/qIqqo31NDLz2QwAPY4iDwx 431FcXq8VQTi/BqP0tFMQ1aL169FttoVZsWpRZUpb2hFaPYqQtUlAUyj7Njv86UzHOrm 4U7sd38mketYbFKZsVuqVz6O91ZkLFPIWl9FXKKfFPbmUxsrAz/RHXEVafaV7+OBRVUu 55AZ1KWfx4pMD4Aci4q2WbLJkWuoVUtTJbs5gFYj3hAY6uQvQfG/RaIWURFWoJfl+y1a RbTA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=sntech.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lk4-20020a17090308c400b0019ccffb3fd3si11166040plb.509.2023.04.24.12.58.10; Mon, 24 Apr 2023 12:58:22 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=sntech.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232982AbjDXTuU (ORCPT + 99 others); Mon, 24 Apr 2023 15:50:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231945AbjDXTtl (ORCPT ); Mon, 24 Apr 2023 15:49:41 -0400 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E80B472A0 for ; Mon, 24 Apr 2023 12:49:39 -0700 (PDT) Received: from ip4d1634d3.dynamic.kabel-deutschland.de ([77.22.52.211] helo=phil.lan) by gloria.sntech.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1pr2Bi-0006Mz-8I; Mon, 24 Apr 2023 21:49:26 +0200 From: Heiko Stuebner To: palmer@dabbelt.com, linux-riscv@lists.infradead.org, paul.walmsley@sifive.com Cc: heiko@sntech.de, kito.cheng@sifive.com, jrtc27@jrtc27.com, conor.dooley@microchip.com, matthias.bgg@gmail.com, heinrich.schuchardt@canonical.com, greentime.hu@sifive.com, nick.knight@sifive.com, christoph.muellner@vrull.eu, philipp.tomsich@vrull.eu, richard.henderson@linaro.org, arnd@arndb.de, linux-kernel@vger.kernel.org, Heiko Stuebner Subject: [PATCH 1/4] RISC-V: create ISA string separately - not as part of cpuinfo Date: Mon, 24 Apr 2023 21:49:08 +0200 Message-Id: <20230424194911.264850-2-heiko.stuebner@vrull.eu> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230424194911.264850-1-heiko.stuebner@vrull.eu> References: <20230424194911.264850-1-heiko.stuebner@vrull.eu> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_PASS, T_SCC_BODY_TEXT_LINE,T_SPF_HELO_TEMPERROR autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764088928339327054?= X-GMAIL-MSGID: =?utf-8?q?1764088928339327054?= From: Heiko Stuebner The isa string is a very central part of the RISC-V architecture, so will be needed in other places as well. So in a first step decouple the generation of the runtime isa-string from /proc/cpuinfo - its current only user. The resulting string should not differ from the previously generated one. Signed-off-by: Heiko Stuebner Reviewed-by: Andrew Jones --- arch/riscv/kernel/cpu.c | 70 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 8 deletions(-) diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c index 3df38052dcbd..ebc478f0a16c 100644 --- a/arch/riscv/kernel/cpu.c +++ b/arch/riscv/kernel/cpu.c @@ -193,10 +193,33 @@ static struct riscv_isa_ext_data isa_ext_arr[] = { __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX), }; -static void print_isa_ext(struct seq_file *f) +static int strlen_isa_ext(void) { struct riscv_isa_ext_data *edata; - int i = 0, arr_sz; + int ext_len = 0, i, arr_sz; + + arr_sz = ARRAY_SIZE(isa_ext_arr) - 1; + + /* No extension support available */ + if (arr_sz <= 0) + return 0; + + for (i = 0; i <= arr_sz; i++) { + edata = &isa_ext_arr[i]; + if (!__riscv_isa_extension_available(NULL, edata->isa_ext_id)) + continue; + + /* string length + underscore */ + ext_len += strlen(edata->uprop) + 1; + } + + return ext_len; +} + +static void strcat_isa_ext(char *isa_str) +{ + struct riscv_isa_ext_data *edata; + int i, arr_sz; arr_sz = ARRAY_SIZE(isa_ext_arr) - 1; @@ -208,7 +231,8 @@ static void print_isa_ext(struct seq_file *f) edata = &isa_ext_arr[i]; if (!__riscv_isa_extension_available(NULL, edata->isa_ext_id)) continue; - seq_printf(f, "_%s", edata->uprop); + strcat(isa_str, "_"); + strcat(isa_str, edata->uprop); } } @@ -220,19 +244,49 @@ static void print_isa_ext(struct seq_file *f) */ static const char base_riscv_exts[13] = "imafdqcbkjpvh"; -static void print_isa(struct seq_file *f, const char *isa) +static char *riscv_create_isa_string(const char *isa) { + int maxlen = 4; + char *isa_str; int i; - seq_puts(f, "isa\t\t: "); + /* calculate the needed string length */ + for (i = 0; i < sizeof(base_riscv_exts); i++) + if (__riscv_isa_extension_available(NULL, base_riscv_exts[i] - 'a')) + maxlen++; + maxlen += strlen_isa_ext(); + + isa_str = kzalloc(maxlen, GFP_KERNEL); + if (!isa_str) + return ERR_PTR(-ENOMEM); + /* Print the rv[64/32] part */ - seq_write(f, isa, 4); + strncat(isa_str, isa, 4); + for (i = 0; i < sizeof(base_riscv_exts); i++) { if (__riscv_isa_extension_available(NULL, base_riscv_exts[i] - 'a')) /* Print only enabled the base ISA extensions */ - seq_write(f, &base_riscv_exts[i], 1); + strncat(isa_str, &base_riscv_exts[i], 1); + } + + strcat_isa_ext(isa_str); + + return isa_str; +} + +static void print_isa(struct seq_file *f, const char *isa) +{ + char *isa_str; + + seq_puts(f, "isa\t\t: "); + + isa_str = riscv_create_isa_string(isa); + if (!IS_ERR(isa_str)) { + seq_write(f, isa_str, strlen(isa_str)); + kfree(isa_str); + } else { + seq_puts(f, "unknown"); } - print_isa_ext(f); seq_puts(f, "\n"); } From patchwork Mon Apr 24 19:49:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Heiko_St=C3=BCbner?= X-Patchwork-Id: 87148 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2972570vqo; Mon, 24 Apr 2023 12:54:13 -0700 (PDT) X-Google-Smtp-Source: AKy350bFbwo3LqtnMyDwf3y4aSSo9CiVEGHRIh7SlzAADFKadF65nUcnpHu9Ujbr6UuBSjdV40k4 X-Received: by 2002:a05:6a21:338a:b0:f2:791f:7ce1 with SMTP id yy10-20020a056a21338a00b000f2791f7ce1mr15439170pzb.44.1682366053411; Mon, 24 Apr 2023 12:54:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682366053; cv=none; d=google.com; s=arc-20160816; b=S61/ooO6h3e3Y42hlRwcTFAO0By1Wl2EkuoxnLRRYciv8itqjm2c0YXipk6gbz2OJL cIxT3KmK2t41d+YSwmaaTxO0Odh6LtHkbmrLdTTqA9tr9rAcqpLT/X192M2CKGHDEF/m 2GG+ICdGVM5grbvSXXPU96w2NuEVvcMhqlsjX1tLg7SZmeOgP5VVBiyU+cAlExzDS5ZI Q70POXwiR+h5OAErFhyPnEFeCpFuqJKBgxQE3NtO3D/BFMVzes3wbnvJvUM0VpZAe9Sq MW0UppZdEDWffvVOj+h/xQKW0JumoBQVbYrgAUuyEYlc9YAd8zB0SvY0nCMWIM3mG0QO A+uw== 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; bh=trTmhS5lId6OYTwGH2NgD1aG797fWIRCzQnC/hW83ho=; b=iU4K38BaBfg4Ok5y5iCDhuXyljUDJ+CKOZvnB5XlnF11ThhLPwN3MkzENkPGkgtdrM 5/tv7qm4ITGXFZh2+EyQwzrxP0kXQ20bKGHIgM3QB/TLeSnkMyHE4JeR8gAf3F2rRlid E/dxw7gcokDMAQ0yIKIfPd+e5sqN+hAnwMGnOq8rXY9UmKH/dQ+4dU4+J4XB/3ItxGMl 7GaW2hS0E8qn8OmxyADkRpT8eUuD2yDQYpbRyoe5ZHA+x+mvaqWRWLjvhooIxJREgCPq pp4UMHTXSFw466YoL94p6RwkFJXJnd8CPRTAhFNINq15HLW3N1t0ijRYIC3PPYbWPNyC Hltw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=sntech.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 4-20020a630004000000b0050beec91e30si11623061pga.768.2023.04.24.12.53.59; Mon, 24 Apr 2023 12:54:13 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=sntech.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232963AbjDXTuM (ORCPT + 99 others); Mon, 24 Apr 2023 15:50:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232593AbjDXTtl (ORCPT ); Mon, 24 Apr 2023 15:49:41 -0400 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C2A65FDC for ; Mon, 24 Apr 2023 12:49:35 -0700 (PDT) Received: from ip4d1634d3.dynamic.kabel-deutschland.de ([77.22.52.211] helo=phil.lan) by gloria.sntech.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1pr2Bi-0006Mz-OX; Mon, 24 Apr 2023 21:49:26 +0200 From: Heiko Stuebner To: palmer@dabbelt.com, linux-riscv@lists.infradead.org, paul.walmsley@sifive.com Cc: heiko@sntech.de, kito.cheng@sifive.com, jrtc27@jrtc27.com, conor.dooley@microchip.com, matthias.bgg@gmail.com, heinrich.schuchardt@canonical.com, greentime.hu@sifive.com, nick.knight@sifive.com, christoph.muellner@vrull.eu, philipp.tomsich@vrull.eu, richard.henderson@linaro.org, arnd@arndb.de, linux-kernel@vger.kernel.org, Heiko Stuebner Subject: [PATCH 2/4] RISC-V: don't parse dt isa string to get rv32/rv64 Date: Mon, 24 Apr 2023 21:49:09 +0200 Message-Id: <20230424194911.264850-3-heiko.stuebner@vrull.eu> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230424194911.264850-1-heiko.stuebner@vrull.eu> References: <20230424194911.264850-1-heiko.stuebner@vrull.eu> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_PASS, T_SCC_BODY_TEXT_LINE,T_SPF_HELO_TEMPERROR autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764088667235853844?= X-GMAIL-MSGID: =?utf-8?q?1764088667235853844?= From: Heiko Stuebner When filling hwcap the kernel already expects the isa string to start with rv32 if CONFIG_32BIT and rv64 if CONFIG_64BIT. So when recreating the runtime isa-string we can also just go the other way to get the correct starting point for it. Signed-off-by: Heiko Stuebner Reviewed-by: Palmer Dabbelt --- arch/riscv/kernel/cpu.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c index ebc478f0a16c..06c2f587a176 100644 --- a/arch/riscv/kernel/cpu.c +++ b/arch/riscv/kernel/cpu.c @@ -244,7 +244,7 @@ static void strcat_isa_ext(char *isa_str) */ static const char base_riscv_exts[13] = "imafdqcbkjpvh"; -static char *riscv_create_isa_string(const char *isa) +static char *riscv_create_isa_string(void) { int maxlen = 4; char *isa_str; @@ -261,7 +261,11 @@ static char *riscv_create_isa_string(const char *isa) return ERR_PTR(-ENOMEM); /* Print the rv[64/32] part */ - strncat(isa_str, isa, 4); +#if IS_ENABLED(CONFIG_32BIT) + strncat(isa_str, "rv32", 4); +#elif IS_ENABLED(CONFIG_64BIT) + strncat(isa_str, "rv64", 4); +#endif for (i = 0; i < sizeof(base_riscv_exts); i++) { if (__riscv_isa_extension_available(NULL, base_riscv_exts[i] - 'a')) @@ -280,7 +284,7 @@ static void print_isa(struct seq_file *f, const char *isa) seq_puts(f, "isa\t\t: "); - isa_str = riscv_create_isa_string(isa); + isa_str = riscv_create_isa_string(); if (!IS_ERR(isa_str)) { seq_write(f, isa_str, strlen(isa_str)); kfree(isa_str); From patchwork Mon Apr 24 19:49:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Heiko_St=C3=BCbner?= X-Patchwork-Id: 87150 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2972988vqo; Mon, 24 Apr 2023 12:55:19 -0700 (PDT) X-Google-Smtp-Source: AKy350Y1ug2qDA5795wJKA/4ajmA/l3JfKWtpUqwjPAL2hxCSlnjSWhvuerWlWiy2/kSlh9Snlgl X-Received: by 2002:a05:6a00:a09:b0:63f:21e:5b0e with SMTP id p9-20020a056a000a0900b0063f021e5b0emr19191311pfh.4.1682366119273; Mon, 24 Apr 2023 12:55:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682366119; cv=none; d=google.com; s=arc-20160816; b=iL2J5o+rGTAqKkbZr9e+yDRRK2kefNJvGplcD9cEUDXonpo9Jd5zomPftZF4vxRIbs Q5QytNug0eJyM43uVaJhnMC5MvhkCHTvej3j34Z6uuyVv0tf+ZM4ZwNh/FgDdo/nwYW0 kV/Y3darcqRqjAa37VSpZOxsgshu8Or0lZ/eAxLUzOIqhb1cYSoFT0t9Work9K9rsfBm 6bn67oIYhDbX3nlxQglMp6hfM/U6TasTJUZMNqr1gneGSNbqMQzF53oCen4zeryjSEQw yYAt1VQDmEzGU9VULLLVkdwHEoH2/Jg2QhavZ0OIMPLlYpv5qquo4aC67g7YlBzvE77r +8Cw== 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; bh=N0wHGTykuORbsoqepQrCOR8tyUKNxJhl23oukg3hmXc=; b=VQRQ7UQSo+ZNPV2sgDMKISU9LKkoXXxTenpZcIG/yGOmFk1iNtEg0/byihJeS5KSsj cH+HD6HSNlEKk80VWTPVXfJVXmhX0toxflShtr9Ws9NCp8jSonTGBgA/5d4/L5SlHz5I wEBkGGQdThek/IBjUKg99jjzO+PrHGf+P2RXkxBXlnymbeVQXylIGY4dOd0YZXOoPUDm th3V9NuNVl7LPmwMGAWef8P0o0ES8+SqNID2bHJzxNbuCsM/7xfXiRhDapOTyia9505p LPWPCGv+C3eCV9RzUdX5EV+p4T2OLMfrU7ajxPMMOccE0Dxrblw/TCU0lYU7bofGb51e TlWg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=sntech.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w8-20020a63f508000000b0051b7ba485d9si12506193pgh.868.2023.04.24.12.55.06; Mon, 24 Apr 2023 12:55:19 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=sntech.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232678AbjDXTuR (ORCPT + 99 others); Mon, 24 Apr 2023 15:50:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232809AbjDXTtl (ORCPT ); Mon, 24 Apr 2023 15:49:41 -0400 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAA0D6EA1 for ; Mon, 24 Apr 2023 12:49:35 -0700 (PDT) Received: from ip4d1634d3.dynamic.kabel-deutschland.de ([77.22.52.211] helo=phil.lan) by gloria.sntech.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1pr2Bj-0006Mz-8z; Mon, 24 Apr 2023 21:49:27 +0200 From: Heiko Stuebner To: palmer@dabbelt.com, linux-riscv@lists.infradead.org, paul.walmsley@sifive.com Cc: heiko@sntech.de, kito.cheng@sifive.com, jrtc27@jrtc27.com, conor.dooley@microchip.com, matthias.bgg@gmail.com, heinrich.schuchardt@canonical.com, greentime.hu@sifive.com, nick.knight@sifive.com, christoph.muellner@vrull.eu, philipp.tomsich@vrull.eu, richard.henderson@linaro.org, arnd@arndb.de, linux-kernel@vger.kernel.org, Heiko Stuebner Subject: [PATCH 3/4] RISC-V: export the ISA string of the running machine in the aux vector Date: Mon, 24 Apr 2023 21:49:10 +0200 Message-Id: <20230424194911.264850-4-heiko.stuebner@vrull.eu> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230424194911.264850-1-heiko.stuebner@vrull.eu> References: <20230424194911.264850-1-heiko.stuebner@vrull.eu> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_PASS, T_SCC_BODY_TEXT_LINE,T_SPF_HELO_TEMPERROR autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764088736036856477?= X-GMAIL-MSGID: =?utf-8?q?1764088736036856477?= From: Heiko Stuebner Already defined aux-vectors regarding the machine type are AT_PLATFORM and AT_BASE_PLATFORM. PPC already uses AT_BASE_PLATFORM to identify the real platform the system is running on, so do a similar thing on RISC-V and export the ISA string of the running machine via this aux-vector element. This way userspace can possibly adapt to extensions that allow it to run certain loads more performantly. Signed-off-by: Heiko Stuebner Reviewed-by: Andrew Jones --- arch/riscv/include/asm/elf.h | 10 ++++++++++ arch/riscv/kernel/cpu.c | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/arch/riscv/include/asm/elf.h b/arch/riscv/include/asm/elf.h index 30e7d2455960..820ef627e83d 100644 --- a/arch/riscv/include/asm/elf.h +++ b/arch/riscv/include/asm/elf.h @@ -78,6 +78,16 @@ extern unsigned long elf_hwcap; #define COMPAT_ELF_PLATFORM (NULL) +/* + * ELF_PLATFORM indicates the ISA supported by the platform, but has + * special meaning to ld.so . + * Expose the ISA string including all usable extensions via + * ELF_BASE_PLATFORM instead and allow userspace to adapt to them + * if needed. + */ +#define ELF_BASE_PLATFORM (riscv_base_platform) +extern const char *riscv_base_platform; + #ifdef CONFIG_MMU #define ARCH_DLINFO \ do { \ diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c index 06c2f587a176..71770563199f 100644 --- a/arch/riscv/kernel/cpu.c +++ b/arch/riscv/kernel/cpu.c @@ -118,8 +118,12 @@ static int riscv_cpuinfo_starting(unsigned int cpu) return 0; } +const char *riscv_base_platform = NULL; +static char *riscv_create_isa_string(void); + static int __init riscv_cpuinfo_init(void) { + char *isa_str; int ret; ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "riscv/cpuinfo:starting", @@ -129,6 +133,14 @@ static int __init riscv_cpuinfo_init(void) return ret; } + /* + * Create the isa-string with the common set of extensions over + * all harts, to expose as AT_BASE_PLATFORM in the aux vector. + */ + isa_str = riscv_create_isa_string(); + if (!IS_ERR(isa_str)) + riscv_base_platform = isa_str; + return 0; } arch_initcall(riscv_cpuinfo_init); From patchwork Mon Apr 24 19:49:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Heiko_St=C3=BCbner?= X-Patchwork-Id: 87149 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2972781vqo; Mon, 24 Apr 2023 12:54:50 -0700 (PDT) X-Google-Smtp-Source: AKy350b0TI7nEG/e2PVPp0fbqLAUzAvzx9wX0AXbXv+blnmmDUL2iC9PF/S7EZeTY79arVMonoc6 X-Received: by 2002:a17:90b:4d88:b0:24b:66fe:6b4b with SMTP id oj8-20020a17090b4d8800b0024b66fe6b4bmr12001975pjb.47.1682366090142; Mon, 24 Apr 2023 12:54:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682366090; cv=none; d=google.com; s=arc-20160816; b=W1yQtJNRCIHQu8huf5DY9tuOS/N7aQAOYqLnr5tHb2n0e5HYYrs2HqbTVWyTQp8QR4 +DPZmSoXi8Qu+1xjD/dJ7tQC2j8wJtuCXkwlSOmSPjyevNGn50qi9WfybR0dyp7b819V eWUGKzj4VuPmRpmdnRmYOIm8MpNKTTJVd91s14p+Fw5TgoRRzTV1KuNUOxr0swyOtaQG dRYtC3F3hTwsMG9l339Mich4xLzioEnYbuNAm1kzHCc/9LO3j1j4ZUAE5VFhNJ8VPHPh aEv1Lw/o7MeMRNyzLxMFu3x3/br7vtzrHXPejkP9sFS6oL5Z+be+LexfglrLeSdhfbqR MMIw== 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; bh=6ZeYP/5YpgDIVyPA0QFehgEbTxE48qYXd+OorlNFdy8=; b=mfM7BmAtIXa8+vSMTbFUdQa6XBiavx4/zl3Zdt+IN9WFZ9x5zHLgZoiCK3iy+B++Pg KU7IIB4X6K/cmjhUtRC8IQ4STg0p74liRcd4CL7mrF3/Y92chfQHMLXwsXujth0K3Cwb IKNHX7sbjn9UV8x7oWcuweQITAtdUAtcA/G5VOg+isIF+aLLS1WMQJyMSE2xtpckGzAk mlUoTKwtf8DKF1kjyFBQ9VwbRTRrDrf3U/kPkAJOrmG+r23tIRQ2ct994s0oLGBjaoR6 3Zv1v/DZ/3pX50Llfu/sDkqmkAuWnTE24oz20MzJ0cxzsRXuCkcUFX0Zn5DTvVjHNNZO B8/Q== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=sntech.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w11-20020a17090abc0b00b0024696759991si14430508pjr.47.2023.04.24.12.54.38; Mon, 24 Apr 2023 12:54:50 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=sntech.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232970AbjDXTu1 (ORCPT + 99 others); Mon, 24 Apr 2023 15:50:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232887AbjDXTtm (ORCPT ); Mon, 24 Apr 2023 15:49:42 -0400 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 261EE44A4 for ; Mon, 24 Apr 2023 12:49:40 -0700 (PDT) Received: from ip4d1634d3.dynamic.kabel-deutschland.de ([77.22.52.211] helo=phil.lan) by gloria.sntech.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1pr2Bj-0006Mz-Qx; Mon, 24 Apr 2023 21:49:27 +0200 From: Heiko Stuebner To: palmer@dabbelt.com, linux-riscv@lists.infradead.org, paul.walmsley@sifive.com Cc: heiko@sntech.de, kito.cheng@sifive.com, jrtc27@jrtc27.com, conor.dooley@microchip.com, matthias.bgg@gmail.com, heinrich.schuchardt@canonical.com, greentime.hu@sifive.com, nick.knight@sifive.com, christoph.muellner@vrull.eu, philipp.tomsich@vrull.eu, richard.henderson@linaro.org, arnd@arndb.de, linux-kernel@vger.kernel.org, Heiko Stuebner Subject: [PATCH 4/4] RISC-V: add support for vendor-extensions via AT_BASE_PLATFORM and xthead Date: Mon, 24 Apr 2023 21:49:11 +0200 Message-Id: <20230424194911.264850-5-heiko.stuebner@vrull.eu> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230424194911.264850-1-heiko.stuebner@vrull.eu> References: <20230424194911.264850-1-heiko.stuebner@vrull.eu> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_PASS, T_SCC_BODY_TEXT_LINE,T_SPF_HELO_TEMPERROR autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764088704985860197?= X-GMAIL-MSGID: =?utf-8?q?1764088704985860197?= From: Heiko Stuebner T-Head cores support a number of own ISA extensions that also include optimized instructions which could benefit userspace to improve performance. Extensions supported by current T-Head cores are: * XTheadBa - bitmanipulation instructions for address calculation * XTheadBb - conditional basic bit-manipulation instructions * XTheadBs - instructions to access a single bit in a register * XTheadCmo - cache management operations * XTheadCondMov - conditional move instructions * XTheadFMemIdx - indexed memory operations for floating-point registers * XTheadFmv - double-precision floating-point high-bit data transmission intructions for RV32 * XTheadInt - instructions to reduce the code size of ISRs and/or the interrupt latencies that are caused by ISR entry/exit code * XTheadMac - multiply-accumulate instructions * XTheadMemIdx - indexed memory operations for GP registers * XTheadMemPair - two-GPR memory operations * XTheadSync - multi-core synchronization instructions In-depth descriptions of these extensions can be found on https://github.com/T-head-Semi/thead-extension-spec Support for those extensions was merged into the relevant toolchains so userspace programs can select necessary optimizations when needed. So a mechanism to the isa-string generation to export vendor-extension lists via the errata mechanism and implement it for T-Head C9xx cores. This exposes these vendor extensions then both in AT_BASE_PLATFORM and /proc/cpuinfo. Signed-off-by: Heiko Stuebner Reviewed-by: Andrew Jones --- arch/riscv/errata/thead/errata.c | 43 ++++++++++++++++++++++++++++ arch/riscv/include/asm/alternative.h | 4 +++ arch/riscv/kernel/alternative.c | 21 ++++++++++++++ arch/riscv/kernel/cpu.c | 12 ++++++++ 4 files changed, 80 insertions(+) diff --git a/arch/riscv/errata/thead/errata.c b/arch/riscv/errata/thead/errata.c index 1036b8f933ec..eb635bf80737 100644 --- a/arch/riscv/errata/thead/errata.c +++ b/arch/riscv/errata/thead/errata.c @@ -15,6 +15,7 @@ #include #include #include +#include #include static bool errata_probe_pbmt(unsigned int stage, @@ -125,3 +126,45 @@ void __init_or_module thead_feature_probe_func(unsigned int cpu, if ((archid == 0) && (impid == 0)) per_cpu(misaligned_access_speed, cpu) = RISCV_HWPROBE_MISALIGNED_FAST; } + + +char *thead_extension_list_func(unsigned long archid, + unsigned long impid) +{ + if ((archid == 0) && (impid == 0)) { + const char *xbase1 = "xtheadba_xtheadbb_xtheadbs_xtheadcmo_xtheadcondmov"; + const char *xbase2 = "_xtheadint_xtheadmac_xtheadmemidx_xtheadmempair_xtheadsync"; + const char *xfpu = "_xtheadfmemIdx"; +#ifdef CONFIG_32BIT + const char *xfpu32 = "_xtheadfmv"; +#endif + int len = strlen(xbase1) + strlen(xbase2); + char *str; + + if (has_fpu()) { + len += strlen(xfpu); +#ifdef CONFIG_32BIT + len+= strlen(xfpu32); +#endif + } + + str = kzalloc(len, GFP_KERNEL); + if (!str) + return str; + + strcpy(str, xbase1); + + if (has_fpu()) { + strcat(str, xfpu); +#ifdef CONFIG_32BIT + strcat(str, xfpu32); +#endif + } + + strcat(str, xbase2); + + return str; + } + + return NULL; +} diff --git a/arch/riscv/include/asm/alternative.h b/arch/riscv/include/asm/alternative.h index a8f5cf6694a1..8c9aec196649 100644 --- a/arch/riscv/include/asm/alternative.h +++ b/arch/riscv/include/asm/alternative.h @@ -31,6 +31,7 @@ #define ALT_ALT_PTR(a) __ALT_PTR(a, alt_offset) void __init probe_vendor_features(unsigned int cpu); +char *list_vendor_extensions(void); void __init apply_boot_alternatives(void); void __init apply_early_boot_alternatives(void); void apply_module_alternatives(void *start, size_t length); @@ -55,6 +56,8 @@ void thead_errata_patch_func(struct alt_entry *begin, struct alt_entry *end, void thead_feature_probe_func(unsigned int cpu, unsigned long archid, unsigned long impid); +char *thead_extension_list_func(unsigned long archid, + unsigned long impid); void riscv_cpufeature_patch_func(struct alt_entry *begin, struct alt_entry *end, unsigned int stage); @@ -62,6 +65,7 @@ void riscv_cpufeature_patch_func(struct alt_entry *begin, struct alt_entry *end, #else /* CONFIG_RISCV_ALTERNATIVE */ static inline void probe_vendor_features(unsigned int cpu) { } +static inline char *list_vendor_extensions(void) { return NULL; } static inline void apply_boot_alternatives(void) { } static inline void apply_early_boot_alternatives(void) { } static inline void apply_module_alternatives(void *start, size_t length) { } diff --git a/arch/riscv/kernel/alternative.c b/arch/riscv/kernel/alternative.c index fc65c9293ac5..18913fd1809f 100644 --- a/arch/riscv/kernel/alternative.c +++ b/arch/riscv/kernel/alternative.c @@ -29,6 +29,8 @@ struct cpu_manufacturer_info_t { unsigned int stage); void (*feature_probe_func)(unsigned int cpu, unsigned long archid, unsigned long impid); + char *(*extension_list_func)(unsigned long archid, + unsigned long impid); }; static void __init_or_module riscv_fill_cpu_mfr_info(struct cpu_manufacturer_info_t *cpu_mfr_info) @@ -54,6 +56,7 @@ static void __init_or_module riscv_fill_cpu_mfr_info(struct cpu_manufacturer_inf case THEAD_VENDOR_ID: cpu_mfr_info->patch_func = thead_errata_patch_func; cpu_mfr_info->feature_probe_func = thead_feature_probe_func; + cpu_mfr_info->extension_list_func = thead_extension_list_func; break; #endif default: @@ -157,6 +160,24 @@ void __init_or_module probe_vendor_features(unsigned int cpu) cpu_mfr_info.imp_id); } +/* + * Lists the vendor-specific extensions common to all cores. + * Returns a new underscore "_" concatenated string that the + * caller is supposed to free after use. + */ +char *list_vendor_extensions(void) +{ + struct cpu_manufacturer_info_t cpu_mfr_info; + + riscv_fill_cpu_mfr_info(&cpu_mfr_info); + if (!cpu_mfr_info.extension_list_func) + return NULL; + + return cpu_mfr_info.extension_list_func(cpu_mfr_info.arch_id, + cpu_mfr_info.imp_id); + +} + /* * This is called very early in the boot process (directly after we run * a feature detect on the boot CPU). No need to worry about other CPUs diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c index 71770563199f..6a0a45b2eb20 100644 --- a/arch/riscv/kernel/cpu.c +++ b/arch/riscv/kernel/cpu.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -260,6 +261,7 @@ static char *riscv_create_isa_string(void) { int maxlen = 4; char *isa_str; + char *vendor_isa; int i; /* calculate the needed string length */ @@ -268,6 +270,10 @@ static char *riscv_create_isa_string(void) maxlen++; maxlen += strlen_isa_ext(); + vendor_isa = list_vendor_extensions(); + if (vendor_isa) + maxlen += strlen(vendor_isa) + 1; + isa_str = kzalloc(maxlen, GFP_KERNEL); if (!isa_str) return ERR_PTR(-ENOMEM); @@ -287,6 +293,12 @@ static char *riscv_create_isa_string(void) strcat_isa_ext(isa_str); + if(vendor_isa) { + strcat(isa_str, "_"); + strcat(isa_str, vendor_isa); + kfree(vendor_isa); + } + return isa_str; }