Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp511000ybg; Fri, 18 Oct 2019 03:17:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqxPKCe5xYs9H/vebKWthPyIX4VXhYbS4845pXRpZodqOLOMUntGGzKOdNo+TmsaZstQ+N9N X-Received: by 2002:a17:906:181b:: with SMTP id v27mr8083270eje.117.1571393855056; Fri, 18 Oct 2019 03:17:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571393855; cv=none; d=google.com; s=arc-20160816; b=teqjrq93t7WGMvyaLoxsHMuA4Wibtz4JsgydxrukTYESb3DFwEw+J6xfLRdFTSDZPf DuUEIxSNG10kcBUIGtVMQgHAc2F6COFSkuo08o1tmuZTqZQF7ZFVIgX6hLayO5C4L/3V ZXLqWNquFez3jim/jQTfYB0IVsMTkJpCMTksvpD0Y0AmGQTUhvo8k5pWEAskkI7INLFg JZuweFk70pYot0EUos4lI4cYQUwmrxmYBfJrrs1bL5FuhODRQj2gso0lpgIMfR5baYOs Kaz41liVZEHgoljSm+2ATqWIcxR1T8dgW2r/0lQd1NAtx+pvQ7MZDF78wbqWeZQ6XBdT 19HA== 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=ABLJXVRMPFM4pBjF5MtvPjsU9wNvxgGgz98k9KABKpI=; b=bjvh59YsFsG96gF6S2QPtxh8Q395l6ricRUi1UJCt+NtA21LDAAzbbjHPI6OwNfOOz zsgmzyum/mSlj0R4Wg/QZ3XQ33WCf3ci560QxLyxG/MaBFHan6kjd2TQ6zHS5BXF/X/e l86ZXOSpd88g7buf/W7fNGw/ydiCpwMexyIQOyavAlhElcXN1Lk8SsqOc4ewMKEAT4cO OKSZyyHfPo9FIoftFnMgmZhNmZSToYbBkFpbiboxAr5BCwzs+LRTI/8kdKE5FXuVSsVf FLFPEuyhlE/CMHEpnpZc1hAhiVUnt/3RFdw8NoA7ztuW/QF8zhWfQ6e3qiiBYIo0vCnz ZRCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=PF17a64C; 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 f15si3269595edm.414.2019.10.18.03.17.11; Fri, 18 Oct 2019 03:17:35 -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=PF17a64C; 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 S2408379AbfJQIss (ORCPT + 99 others); Thu, 17 Oct 2019 04:48:48 -0400 Received: from mail-wm1-f68.google.com ([209.85.128.68]:52131 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2408364AbfJQIsr (ORCPT ); Thu, 17 Oct 2019 04:48:47 -0400 Received: by mail-wm1-f68.google.com with SMTP id 7so1654075wme.1 for ; Thu, 17 Oct 2019 01:48:45 -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=ABLJXVRMPFM4pBjF5MtvPjsU9wNvxgGgz98k9KABKpI=; b=PF17a64CORHUVSvDRifR6t0U+QaHY0SKqGdGnvTTcrru79sqhAAXGH6HSdjaq2cdjS CcpHuf6rNGpuxa+PlYMxn4isoV+C0Et/4/tzdrMj9uG0vH6zebbqfsSph3nVP0TQFysU H1Jy6snrhBE4ar/G+trpHXjpEurwRgcecMUgiTGghi0y/soPmKbpHz9hbQDdfzQ7XjyD hU2CZO7bChiJGKt7E2qHxoqBfSu4cIODG1cVG/ULE+s/5m+1rFnxqIF2spLdv3gGM4D5 X2nnwbiYmVsRCF1psZaQPuYGRZv3brqDQ4+fR6y5qPYW5DZxtvX60CTeDwCBoO653he/ EmcQ== 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=ABLJXVRMPFM4pBjF5MtvPjsU9wNvxgGgz98k9KABKpI=; b=ouVG1yQGFZaXPsk1LqyQ6DnLl/yUomSxEMZLWal/8EQo6zdcE6Q0PTToIXVbcgmCXx UJZ+y9ZncWcqWqCHerkQJyEk+XPoEunDZs3SvNZ5PeiRVrG808Xyi4/iM3OV0Ea9N3gq wYUu4NBrrAy91uogcGx/QZk3/Ror+Rf4wreqgffzcFVekU+nCUrLxpVG4KFc+VS9d2bf v+87N0w2iZjyCm0weoPs1G7V/Wi/w2QgZu3eWxxp8qnBoZxu6xa+/GHYPzyh3ClMS8tA X+E4IVIQ/glc9+JZsMYp+U1ef3NcVTtZ7vgGK1Ua6mQmZHgfL+WOTUYZtgQln7tye0eA zK7g== X-Gm-Message-State: APjAAAVOEyFMgoEFigmVJegEX+jTMkaUypeKa19SDsHcwFLnLgQ0nwdV S2405YlrThirGRb8fOqqV1ohuipFn37hLHJf6vJytw== X-Received: by 2002:a1c:a697:: with SMTP id p145mr1734740wme.24.1571302124460; Thu, 17 Oct 2019 01:48:44 -0700 (PDT) MIME-Version: 1.0 References: <20191016160649.24622-1-anup.patel@wdc.com> <20191016160649.24622-2-anup.patel@wdc.com> In-Reply-To: <20191016160649.24622-2-anup.patel@wdc.com> From: Anup Patel Date: Thu, 17 Oct 2019 14:18:33 +0530 Message-ID: Subject: Re: [PATCH v9 01/22] RISC-V: Add bitmap reprensenting ISA features common across CPUs To: Paul Walmsley Cc: Palmer Dabbelt , Anup Patel , 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 Hi Paul, On Wed, Oct 16, 2019 at 9:38 PM 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 Can you consider this patch for Linux-5.4-rcX ?? Regards, Anup > --- > arch/riscv/include/asm/hwcap.h | 22 +++++++++ > arch/riscv/kernel/cpufeature.c | 83 ++++++++++++++++++++++++++++++++-- > 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..5989dd4426d1 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,26 @@ 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_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 eaad5aa07403..64068d36658d 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 > + * Return: base extension word as unsigned long value > + * > + * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used. > + */ > +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 > + * Return: true or false > + * > + * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used. > + */ > +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; > @@ -41,8 +87,24 @@ void riscv_fill_hwcap(void) > if (riscv_read_check_isa(node, &isa) < 0) > continue; > > - for (i = 0; i < strlen(isa); ++i) > + i = 0; > + isa_len = strlen(isa); > +#if IS_ENABLED(CONFIG_32BIT) > + if (!strncmp(isa, "rv32", 4)) > + i += 4; > +#elif IS_ENABLED(CONFIG_64BIT) > + if (!strncmp(isa, "rv64", 4)) > + i += 4; > +#endif > + for (; i < isa_len; ++i) { > this_hwcap |= isa2hwcap[(unsigned char)(isa[i])]; > + /* > + * TODO: X, Y and Z extension parsing for Host ISA > + * bitmap will be added in-future. > + */ > + if ('a' <= isa[i] && isa[i] < 'x') > + this_isa |= (1UL << (isa[i] - 'a')); > + } > > /* > * All "okay" hart should have same isa. Set HWCAP based on > @@ -53,6 +115,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 > @@ -62,7 +129,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 >