Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp1200625ybh; Sat, 3 Aug 2019 20:28:21 -0700 (PDT) X-Google-Smtp-Source: APXvYqwgkZ5kO6dUpxS+yKVllmqeUOlxe+k0QdvijrPWPBzBtqPMZsTgp539wCGR8heYcFaQ1eTS X-Received: by 2002:a17:90a:1c1:: with SMTP id 1mr11898231pjd.72.1564889301110; Sat, 03 Aug 2019 20:28:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564889301; cv=none; d=google.com; s=arc-20160816; b=O5IWBApZRRBC+seaQm7jEH6BZRXPm69Iu+lPjP4RHaxdWkY3KyYWi16pgkNnctW3SW WWgbEDgC+hHBy7sFnZgRJVS7kWpC2DjUEj1AIpD2ii7fOsESzHsLwDeYXec3Vs63r56S Sx7yL7/MFWRJa4LqTJncnTTGm7ryZGNbm4huBmv92ByOucaWBYjSwdwceAENdXmHaIMC PgCo4pSsgmk96/RWCzKL08mI2vy6t7rXD6MZxePIkWhINgy5NnegNzTVWlrCoEUfvxzb OuL18GHKmYSnSkPtqdhfv76RXq4iH8dyf0+zeWW3kMxFSGI/Es/cAt4LRET7d2K4GXPm vhSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr:ironport-sdr:dkim-signature; bh=F/fiGBIKJFk+f/MrAcHXo2r4s0RKJQ/ZfUW8/iRbM4c=; b=OFxmbYTgwCuE4+EEs1p/A4vA/yG4LLoq8gDeM2HOjp9Y4TRtE9RcY3urirptcLFlxo 8iGn94u3fkV3nCoCpJuXkh2AeOYkvQZJ+cKwiIhOZQKBKjG9TRlIfDb8ObS5ApBVkTMZ 2ro+3kUVsUDBJlmucqZs1Y66hEohkukdRxhXBcm+FzyJuA+UuvwmNA32FMLPUdE+9uau UOsXXG1F+AhTHdN00ib1U5gArU7PImJW/u39dRY8sfDPnLLcDmqzwZmXeoicObpiNYb7 4R+CgiEUW2+FIsz39f/cNp7BbyWIQ3pHBktpKGXZRDF6Lck5nIlspzY8+9p9Z2z8ZDKS QDKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=CVuySkEZ; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wdc.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a15si45480160pfg.87.2019.08.03.20.28.06; Sat, 03 Aug 2019 20:28:21 -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=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=CVuySkEZ; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wdc.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726195AbfHCE1h (ORCPT + 99 others); Sat, 3 Aug 2019 00:27:37 -0400 Received: from esa4.hgst.iphmx.com ([216.71.154.42]:20709 "EHLO esa4.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725283AbfHCE1Z (ORCPT ); Sat, 3 Aug 2019 00:27:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1564806445; x=1596342445; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=YwZBgjDKqJC0ZZ1pMUEYPgwPKNXhIZ+3mPkTf+XP88Q=; b=CVuySkEZPupWWfefVu+RrYDl1xxFNNmpmCV01M82GlilztiphP/w75Py DWWFK+aNG+FNqTr3K8jlrp5XuotQJ3ke4hdNTDmxNSV7qkhdG5SZ2q81i 46fBtVX5cutiOGWn1EJeLE9V01+7iHoVY0d5DyaEwjv4ML9sGDmoUPXvw LrlaLkVuh7sVeMuq3phJKvYWn+oMLAdltYtDGNNb6sJF1n+Qvl7u3ZP/v pH89VjeNUd2lfUUde0v51raEDegliE6/HsyFyPiBe0eiPU75hBfFGyrDA CeiIb1aJeYH5Ntk0At04mq63EnG8j+0edxM8Umn4YzTIMzDuG1IZNKsOc A==; IronPort-SDR: wv1epBSRyuqFSrms1XWs1yMX2XN5Ii3/VozExOYCbMWWw+rSwW6yn5/wCKGvjy7uljO8odBLuM kIPM0QIjstx84w1TPg1JP48gWGBmEc8puGkVIzPxSwmxba8NK1cnVym9wQV4NdDTX3vAZA/N2w /77006vk7xz9bEI/GyrHDBtyTjgdtNov9hnJfzE5QNX7qwtlHY6upIS1FRM9LnaeCvBFd1e6kL mHkil3SNfVccMoD4huclmG+DK2QKXI9e89d48Pe/5bT0zm4h4Wjrq/Vpyvd0AChIgozbnRBTuN kMQ= X-IronPort-AV: E=Sophos;i="5.64,340,1559491200"; d="scan'208";a="114857035" Received: from uls-op-cesaip02.wdc.com (HELO uls-op-cesaep02.wdc.com) ([199.255.45.15]) by ob1.hgst.iphmx.com with ESMTP; 03 Aug 2019 12:27:25 +0800 IronPort-SDR: GPxeLPkYoV7hr+7kpqo7QyUvORpHzMwoTx+HZ8EGMc4vVwIPOjzJCyb/pOPHyq/5dcs6DEwcmi gW8B6yiGH9ycSswKIDQyWlQGaF0SeXyQa3hcJMBIQiWTW/E/Sa0b0pdrjmWmF45Sa0+xMHtRP9 C85GUcDd0XMc9ah9j/b2NI3aTg1teomGHz1Lup1BGCiWDslepeeFzH4+HM7AoiC2qetgJT7AqJ Rd5wl5dXTV0NwaHMY2/1e5nWYP1T9zuZIx910HPBK3cLUGXU3HovyJ9GmbNky+txyBsavne7zk Hwqf36P8HdAyjWWkdAvArP4v Received: from uls-op-cesaip01.wdc.com ([10.248.3.36]) by uls-op-cesaep02.wdc.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Aug 2019 21:25:19 -0700 IronPort-SDR: TGgqUa3HMqMxaoqCTrW3QLhYzOybrFKDPM08KZJsMMokyJlt2xboQ3altV76Sv9C9uUZZkLWnI zVJqpFyVRR9/RWCbiE0HxBLFX06w3P5AuyXEgJHhpvCKMonHCKEAn1hASuNd7mITpiXXqX8VmP zeqUjphUfgNIlztN4eKpU9bua6UbA0LDRfNj+c0bqpFNv6I9Aa+dF4raJPHjJB6nYDIysT0r7y 1CGPA5FIQqHpxMc9dSnJyFXl+y+J4pSxSrdqTQgJZMRn1IGAbKRvK1xjggyIUTJb17h5l3wN/P nh8= Received: from jedi-01.sdcorp.global.sandisk.com (HELO jedi-01.int.fusionio.com) ([10.11.143.218]) by uls-op-cesaip01.wdc.com with ESMTP; 02 Aug 2019 21:27:25 -0700 From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Anup Patel , Atish Patra , Alan Kao , Albert Ou , Daniel Lezcano , devicetree@vger.kernel.org, Enrico Weigelt , Greg Kroah-Hartman , Johan Hovold , linux-riscv@lists.infradead.org, Mark Rutland , Palmer Dabbelt , Paul Walmsley , Rob Herring , Thomas Gleixner Subject: [PATCH v4 2/4] RISC-V: Add riscv_isa reprensenting ISA features common across CPUs Date: Fri, 2 Aug 2019 21:27:21 -0700 Message-Id: <20190803042723.7163-3-atish.patra@wdc.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190803042723.7163-1-atish.patra@wdc.com> References: <20190803042723.7163-1-atish.patra@wdc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Anup Patel This patch adds riscv_isa integer to represent ISA features common across all 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 is KVM hypervisor where riscv_isa will be used 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 --- arch/riscv/include/asm/hwcap.h | 16 ++++++++++++++ arch/riscv/kernel/cpufeature.c | 39 +++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h index 7ecb7c6a57b1..717306780add 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -22,5 +22,21 @@ enum { }; extern unsigned long elf_hwcap; + +#define RISCV_ISA_EXT_a (1UL << ('a' - 'a')) +#define RISCV_ISA_EXT_c (1UL << ('c' - 'a')) +#define RISCV_ISA_EXT_d (1UL << ('d' - 'a')) +#define RISCV_ISA_EXT_f (1UL << ('f' - 'a')) +#define RISCV_ISA_EXT_h (1UL << ('h' - 'a')) +#define RISCV_ISA_EXT_i (1UL << ('i' - 'a')) +#define RISCV_ISA_EXT_m (1UL << ('m' - 'a')) +#define RISCV_ISA_EXT_s (1UL << ('s' - 'a')) +#define RISCV_ISA_EXT_u (1UL << ('u' - 'a')) + +extern unsigned long riscv_isa; + +#define riscv_isa_extension_available(ext_char) \ + (riscv_isa & RISCV_ISA_EXT_##ext_char) + #endif #endif diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index b1ade9a49347..becc99272341 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -12,6 +12,9 @@ #include unsigned long elf_hwcap __read_mostly; +unsigned long riscv_isa __read_mostly; +EXPORT_SYMBOL_GPL(riscv_isa); + #ifdef CONFIG_FPU bool has_fpu __read_mostly; #endif @@ -20,7 +23,8 @@ 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; @@ -31,9 +35,11 @@ void riscv_fill_hwcap(void) isa2hwcap['c'] = isa2hwcap['C'] = COMPAT_HWCAP_ISA_C; elf_hwcap = 0; + riscv_isa = 0; 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 +49,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')); + } /* * All "okay" hart should have same isa. Set HWCAP based on @@ -55,6 +73,11 @@ void riscv_fill_hwcap(void) elf_hwcap &= this_hwcap; else elf_hwcap = this_hwcap; + + if (riscv_isa) + riscv_isa &= this_isa; + else + riscv_isa = this_isa; } /* We don't support systems with F but without D, so mask those out @@ -64,7 +87,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 & (1UL << 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 & (1UL << 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.21.0