Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp144681ybn; Tue, 24 Sep 2019 20:03:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqysaKi2zAR5yjYIJO66fbmoGLlz66XyLHt79SyxzzIfOD1ab9l9rl9HUKE8UgwrPM19R8H2 X-Received: by 2002:a1c:454:: with SMTP id 81mr4301346wme.119.1569380633225; Tue, 24 Sep 2019 20:03:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569380633; cv=none; d=google.com; s=arc-20160816; b=ivgu/AMbJB8oThbzIt/TrBX8H6Sb6C9SSYLAzCBZ5wntIIyGm1ZwRT3MFil8TJiCoo I/RDdek/BxpcravkYyIAzj7B4kleWbNsd39cTPoXnx1IqnpKrdbgl7u4EEyetNr9Lq0t gUwYA3kHCRtTjf9vmIz2ZQi+wgr1uPlEk4Uug4BazqKc7DlPAu5lJZ4no1ytH3k+vG6q Fj1Jwex2QjEkJAuNScHPWWRu7csqWhyrrTu1S5om1S9/weykQapTiYRko5WnsOY612/W nJvr9ISs6RYMv0rvheD/s9D07Dpg4EtR4rikBH9SS1baeL+v+8Tr/5jhiRPnqkQiGL1Q n/uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=aW5JAQkQgpvqTjYjNxgV8q6UjWSZi6NlskA09iDonQI=; b=fQFfCt/jGkRdrwSXVGX9Sa+ODBG9biT4JnympqwR2ZbNU1wexKvuVSLl2jq5VSIWHa igLPEzZjUWyx7P8b/QI5HSsDr8HTcLWi6ijP3OClJ7CPjjfr80+sBeO/LEE2cNlP76lk /i8Xr+Kz+kj4d5mVD3lBVAFSMdgl9kCrr4CHs7NkCJJjBWIfLL85+6Jq+NYgme7x6yLV zydoezaP9/LQDs+ga6GkXbMUth1lpKW6U6j2HuT4IxVSBO2DNp+HwjXgJVnEFaQJgjMs SawkHwnOmyY0Oq2n7ZJ6Z7mW+VK6mmXGJscCJA3j7oJ3CIGbeAGe/+FTtwXN9gdO4M4A TCOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=ThCt24HN; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k16si2047558ejc.134.2019.09.24.20.03.28; Tue, 24 Sep 2019 20:03:53 -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; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=ThCt24HN; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406266AbfIWDjx (ORCPT + 99 others); Sun, 22 Sep 2019 23:39:53 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:35407 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404753AbfIWDjw (ORCPT ); Sun, 22 Sep 2019 23:39:52 -0400 Received: by mail-wr1-f68.google.com with SMTP id v8so12209020wrt.2 for ; Sun, 22 Sep 2019 20:39:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brainfault-org.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=aW5JAQkQgpvqTjYjNxgV8q6UjWSZi6NlskA09iDonQI=; b=ThCt24HNfG5UA4GZNfmLY2rU4yX1Uu8S5olqh71hBvnVMmYi4He3jQPWcvcHcgunvY ePdoJ1Jd3k6WpFIwnL+6w590IMFQcsO65bY2obQZ9U5W7FvdKVM2oFBlsmPKyjarFp9O BymdKJId+cdGvIlMabnANJjHMKPv3HE8t+xFFfztPQXJUMRTRoIu2aiayGgOHp8Mkkcj juZ54TxLZhUtxXUMAQVUwwJjkewLON9rTptWzP//4Pf8/d15/40jOkOQ3vzB783VFVQD 22Je84qniXInOMqQoyBzh5EYsy+EoKjBhYxOyHLT8sx71mykqdC7zF29NoHeCfu7dsnc D3Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=aW5JAQkQgpvqTjYjNxgV8q6UjWSZi6NlskA09iDonQI=; b=r4iT59ZY2GJqh3X4ODI1sAa/HENnt/3EdIZ4ercrzdK0QXrR/fQBfcxGWXF8KhMJ08 1jS9fnARUs10xQzbEn8O8E8A2IodzMqkm5C3G8Fpz+c8Leip9xuziHE9BKvrMBy086XS rSmqu5FKFHj70x5lgQW6YTXOVIOURt2wjDoH6A0FI6cz1ImDrB0TG1/qeL6DbJ6g7eQZ WP7/RhMMxvwhAZh0cGQOKu6JEkgUEowe3OpABLQPSLav+VLTNPMwp3m6x8Mjic03pMWe LAHnss0uWO39p6iXihlMrj+EDfs1eQjrO0eUDa3gykLeFW54GwQgCvZXTzDt5FbA8wNp /Hdg== X-Gm-Message-State: APjAAAWoCJg5r5HF+28iEWj+0R4eOeuT/XkpBXxXqVhCD5XxyziknvvX vjKzz6DvLqkyphlvmd8L6edWuUX29C5WsaoaKlhh9w== X-Received: by 2002:a05:6000:2:: with SMTP id h2mr19124674wrx.309.1569209987215; Sun, 22 Sep 2019 20:39:47 -0700 (PDT) MIME-Version: 1.0 References: <20190904161245.111924-1-anup.patel@wdc.com> <20190904161245.111924-4-anup.patel@wdc.com> In-Reply-To: From: Anup Patel Date: Mon, 23 Sep 2019 09:09:35 +0530 Message-ID: Subject: Re: [PATCH v7 02/21] RISC-V: Add bitmap reprensenting ISA features common across CPUs To: Paul Walmsley Cc: Anup Patel , Palmer Dabbelt , Paolo Bonzini , Radim K , Daniel Lezcano , Thomas Gleixner , Alexander Graf , Atish Patra , Alistair Francis , Damien Le Moal , Christoph Hellwig , "kvm@vger.kernel.org" , "linux-riscv@lists.infradead.org" , "linux-kernel@vger.kernel.org" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Sep 21, 2019 at 3:31 PM Paul Walmsley wrote: > > Hi Anup, > > Thanks for changing this to use a bitmap. A few comments below - > > On Wed, 4 Sep 2019, Anup Patel wrote: > > > This patch adds riscv_isa bitmap which represents Host ISA features > > common across all Host CPUs. The riscv_isa is not same as elf_hwcap > > because elf_hwcap will only have ISA features relevant for user-space > > apps whereas riscv_isa will have ISA features relevant to both kernel > > and user-space apps. > > > > One of the use-case for riscv_isa bitmap is in KVM hypervisor where > > we will use it to do following operations: > > > > 1. Check whether hypervisor extension is available > > 2. Find ISA features that need to be virtualized (e.g. floating > > point support, vector extension, etc.) > > > > Signed-off-by: Anup Patel > > Signed-off-by: Atish Patra > > Reviewed-by: Alexander Graf > > --- > > arch/riscv/include/asm/hwcap.h | 26 +++++++++++ > > arch/riscv/kernel/cpufeature.c | 79 ++++++++++++++++++++++++++++++++-- > > 2 files changed, 102 insertions(+), 3 deletions(-) > > > > diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h > > index 7ecb7c6a57b1..9b657375aa51 100644 > > --- a/arch/riscv/include/asm/hwcap.h > > +++ b/arch/riscv/include/asm/hwcap.h > > @@ -8,6 +8,7 @@ > > #ifndef __ASM_HWCAP_H > > #define __ASM_HWCAP_H > > > > +#include > > #include > > > > #ifndef __ASSEMBLY__ > > @@ -22,5 +23,30 @@ enum { > > }; > > > > extern unsigned long elf_hwcap; > > + > > +#define RISCV_ISA_EXT_a ('a' - 'a') > > +#define RISCV_ISA_EXT_c ('c' - 'a') > > +#define RISCV_ISA_EXT_d ('d' - 'a') > > +#define RISCV_ISA_EXT_f ('f' - 'a') > > +#define RISCV_ISA_EXT_h ('h' - 'a') > > +#define RISCV_ISA_EXT_i ('i' - 'a') > > +#define RISCV_ISA_EXT_m ('m' - 'a') > > +#define RISCV_ISA_EXT_s ('s' - 'a') > > +#define RISCV_ISA_EXT_u ('u' - 'a') > > +#define RISCV_ISA_EXT_zicsr (('z' - 'a') + 1) > > +#define RISCV_ISA_EXT_zifencei (('z' - 'a') + 2) > > +#define RISCV_ISA_EXT_zam (('z' - 'a') + 3) > > +#define RISCV_ISA_EXT_ztso (('z' - 'a') + 4) > > If we add the Z extensions here, it's probably best if we drop Zam from > this list. The rationale is, as maintainers, we're planning to hold off > on merging any support for extensions or modules that aren't in the > "frozen" or "ratified" states, and according to the RISC-V specs, Zicsr, > Zifencei, and Ztso are all either frozen or ratified. However, see > below - No problem, I will remove Zam define. Please add documentation under Documentation/riscv regarding the policy of not merging support for RISC-V extensions which aren't in "frozen" or "ratified" state. > > > + > > +#define RISCV_ISA_EXT_MAX 256 > > + > > +unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap); > > + > > +#define riscv_isa_extension_mask(ext) BIT_MASK(RISCV_ISA_EXT_##ext) > > + > > +bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, int bit); > > +#define riscv_isa_extension_available(isa_bitmap, ext) \ > > + __riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_##ext) > > + > > #endif > > #endif > > diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c > > index b1ade9a49347..4ce71ce5e290 100644 > > --- a/arch/riscv/kernel/cpufeature.c > > +++ b/arch/riscv/kernel/cpufeature.c > > @@ -6,21 +6,64 @@ > > * Copyright (C) 2017 SiFive > > */ > > > > +#include > > #include > > #include > > #include > > #include > > > > unsigned long elf_hwcap __read_mostly; > > + > > +/* Host ISA bitmap */ > > +static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly; > > + > > #ifdef CONFIG_FPU > > bool has_fpu __read_mostly; > > #endif > > > > +/** > > + * riscv_isa_extension_base - Get base extension word > > + * > > + * @isa_bitmap ISA bitmap to use > > + * @returns base extension word as unsigned long value > > + * > > + * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used. > > + */ > > Am happy to see comments that can be automatically parsed, but could you > reformat them into kernel-doc format? > > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/doc-guide/kernel-doc.rst Sure, I will update comments as-per kernel-doc.rst. > > > +unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap) > > +{ > > + if (!isa_bitmap) > > + return riscv_isa[0]; > > + return isa_bitmap[0]; > > +} > > +EXPORT_SYMBOL_GPL(riscv_isa_extension_base); > > + > > +/** > > + * __riscv_isa_extension_available - Check whether given extension > > + * is available or not > > + * > > + * @isa_bitmap ISA bitmap to use > > + * @bit bit position of the desired extension > > + * @returns true or false > > + * > > + * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used. > > + */ > > Same comment as above. Okay, I will update. > > > +bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, int bit) > > +{ > > + const unsigned long *bmap = (isa_bitmap) ? isa_bitmap : riscv_isa; > > + > > + if (bit >= RISCV_ISA_EXT_MAX) > > + return false; > > + > > + return test_bit(bit, bmap) ? true : false; > > +} > > +EXPORT_SYMBOL_GPL(__riscv_isa_extension_available); > > + > > void riscv_fill_hwcap(void) > > { > > struct device_node *node; > > const char *isa; > > - size_t i; > > + char print_str[BITS_PER_LONG+1]; > > + size_t i, j, isa_len; > > static unsigned long isa2hwcap[256] = {0}; > > > > isa2hwcap['i'] = isa2hwcap['I'] = COMPAT_HWCAP_ISA_I; > > @@ -32,8 +75,11 @@ void riscv_fill_hwcap(void) > > > > elf_hwcap = 0; > > > > + bitmap_zero(riscv_isa, RISCV_ISA_EXT_MAX); > > + > > for_each_of_cpu_node(node) { > > unsigned long this_hwcap = 0; > > + unsigned long this_isa = 0; > > > > if (riscv_of_processor_hartid(node) < 0) > > continue; > > @@ -43,8 +89,20 @@ void riscv_fill_hwcap(void) > > continue; > > } > > > > - for (i = 0; i < strlen(isa); ++i) > > + i = 0; > > + isa_len = strlen(isa); > > +#if defined(CONFIG_32BIT) > > + if (!strncmp(isa, "rv32", 4)) > > + i += 4; > > +#elif defined(CONFIG_64BIT) > > + if (!strncmp(isa, "rv64", 4)) > > + i += 4; > > +#endif > > + for (; i < isa_len; ++i) { > > this_hwcap |= isa2hwcap[(unsigned char)(isa[i])]; > > + if ('a' <= isa[i] && isa[i] <= 'z') > > + this_isa |= (1UL << (isa[i] - 'a')); > > Continuing from the earlier comment, this code won't properly handle the X > and Z prefix extensions. So maybe for the time being, we should just drop > the lines mentioned earlier that imply that we can parse Z-prefix > extensions, and change this line so it ignores X and Z letters? > > Then a subsequent patch can add support for more complicated extension > string parsing. Okay, we sill not parse 'x' and 'z' bits (which can be added later for X and Z prefix extensions). > > > > + } > > > > /* > > * All "okay" hart should have same isa. Set HWCAP based on > > @@ -55,6 +113,11 @@ void riscv_fill_hwcap(void) > > elf_hwcap &= this_hwcap; > > else > > elf_hwcap = this_hwcap; > > + > > + if (riscv_isa[0]) > > + riscv_isa[0] &= this_isa; > > + else > > + riscv_isa[0] = this_isa; > > } > > > > /* We don't support systems with F but without D, so mask those out > > @@ -64,7 +127,17 @@ void riscv_fill_hwcap(void) > > elf_hwcap &= ~COMPAT_HWCAP_ISA_F; > > } > > > > - pr_info("elf_hwcap is 0x%lx\n", elf_hwcap); > > + memset(print_str, 0, sizeof(print_str)); > > + for (i = 0, j = 0; i < BITS_PER_LONG; i++) > > + if (riscv_isa[0] & BIT_MASK(i)) > > + print_str[j++] = (char)('a' + i); > > + pr_info("riscv: ISA extensions %s\n", print_str); > > + > > + memset(print_str, 0, sizeof(print_str)); > > + for (i = 0, j = 0; i < BITS_PER_LONG; i++) > > + if (elf_hwcap & BIT_MASK(i)) > > + print_str[j++] = (char)('a' + i); > > + pr_info("riscv: ELF capabilities %s\n", print_str); > > > > #ifdef CONFIG_FPU > > if (elf_hwcap & (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D)) > > -- > > 2.17.1 > > > > > > > - Paul Regards, Anup