Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2148198ybt; Fri, 3 Jul 2020 02:01:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzdpGH2UdT1d95dKHqxk7c12OYSrjiCcgr67l3bvoRBUeCt5oeBcQtTYnffqZflY14ET+Tq X-Received: by 2002:a17:906:915:: with SMTP id i21mr32030957ejd.313.1593766875769; Fri, 03 Jul 2020 02:01:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593766875; cv=none; d=google.com; s=arc-20160816; b=R51Tho4DqLbPrHVM3dMZuA/UgQ7TZYu+zl4qbabR12UovVNS7xy2z7KoqVSuUDYiW+ BBDkaYP2WtYvAc4RgEAvtty5K+a+u/xXwUymKiL+8Wn2KSNnd5U4mux7gArfOcN+eYcz Ki+E93Yb6tcS4jSVNbxscwrkeo9oyTOno4j9HjfFZnxRDLJR//dhOA73/bddupClYwGV HOWcYhJDcHOPJ90Yh9iTxztVopaB5wekXzlh5mdXEgXfCgHwACJKIShFv/cBDZwc6IAO P3akYy/b5EwWPdVXmjYBhm4uafz5zgokdEDRQqzQIULhalBs15/xEx5HZYDy/g2+wYRT RnLA== 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 :dkim-signature; bh=k5L8NObQEzyS+/JLPwFcEH2ZHcXncJHg+hMC0oanQIM=; b=fiXikMDPJAKveus+eG8cS2CRJfy9Xs5SBRLOAAFS9NpWUug5zWv3m5sNueQGJ0xZlC hg+jKwqxmEiKx4RiWlIbG8OO8tUR2czGsUl/Vlkg2gIkVNFRdHEA9GQV64PFqKEU57Se qO7dBbEr8EiBuRK4cDGMyIhON34YLgYn/fVHeax6xsJ4p4UxnCbNjPBRKY5zE8CObqVD C9QujvGBAkmeodjpW34uTFliZNibM6AF/73dbRB9HmTEIQL18MJGO/2OC7/IX5kvDeSL XUAaNm7zNfkLN8kqKR0h2ftXXgfjNSw0vUPr5DtJI50+7WWmOn/NSlJRwFuVOt73vV8E BgAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=lrQzB7Ek; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g19si8351198edv.174.2020.07.03.02.00.52; Fri, 03 Jul 2020 02:01:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=lrQzB7Ek; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726406AbgGCI6J (ORCPT + 99 others); Fri, 3 Jul 2020 04:58:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726347AbgGCI6F (ORCPT ); Fri, 3 Jul 2020 04:58:05 -0400 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF847C08C5C1 for ; Fri, 3 Jul 2020 01:58:04 -0700 (PDT) Received: by mail-pj1-x1041.google.com with SMTP id h22so13737440pjf.1 for ; Fri, 03 Jul 2020 01:58:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=k5L8NObQEzyS+/JLPwFcEH2ZHcXncJHg+hMC0oanQIM=; b=lrQzB7Ekjzc0cdr3gHo1HfR/yMcchGFdyAFKx9DU60Rc5yHUbn0Lx3CAUA8k4VwGGs uTHooG0TVm1cP47El3ohXhymEdIf3zhq29247azDfApabPx5kf7GxT44LhiQfiKMc4w4 58qqA9yCZq19OkKXtzdkOAjsL2ONOLlfj4ICN+qC2ac+0ZZlmtPrwNZJxYrgs/2tTzJy oV0lT53Nlfpzy1CBOpS9dh/MjyEQfaM4evClnqDtQoz0lxy2lDO74Af9bDJ1B+h8rDvU 32v7+PN8/vGHmoOk7uLDkT/OFK5+EGuUmZ6e3YX5dI+nSnh86GoXylI/DVhLK+ViFyEI SnHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=k5L8NObQEzyS+/JLPwFcEH2ZHcXncJHg+hMC0oanQIM=; b=YM4HWO1ia8fUCrDCVetmh3UmkTVJ+II0LI7QSkvYnRNShC1Bfam1/OtDW/ET2OUQmz 1eOmrlSBcGvCVtU62VMFV+UCETqzLZRoVSY956hKrNcFXeY4VNwuQa+K9yiHx2na24Qy wfk6eAOmnDy1rR1DzQuC3/UL4Q7vXM+iS/Wtq53j1v2GQPMlgdB8PgrCW/Z0t6iZGCFf 3M1br1M99L9j0opWFkMz2v/Hivqno0WMWgvZjDs9DVQBqbhHnrvJWST8ZpnCA6Od+4d7 KRoUKzBB8U+nL3RhVjQQQR+cPCC4aXgC8juX+ihh4KV7n20/b8pCdmVGovrsjocbPAd3 dQeA== X-Gm-Message-State: AOAM531UOX5i9HNO4j/5Z4M9oew5p9Ln9ELUHq71yWajtlQSEZWHrLMa FF5oqN1+ANNjUYm1FHLcNl9LqQ== X-Received: by 2002:a17:902:9042:: with SMTP id w2mr20339410plz.8.1593766684385; Fri, 03 Jul 2020 01:58:04 -0700 (PDT) Received: from hsinchu02.internal.sifive.com (114-34-229-221.HINET-IP.hinet.net. [114.34.229.221]) by smtp.gmail.com with ESMTPSA id l23sm2126287pjy.45.2020.07.03.01.58.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Jul 2020 01:58:04 -0700 (PDT) From: Zong Li To: palmer@dabbelt.com, paul.walmsley@sifive.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Zong Li Subject: [PATCH 3/3] riscv: Add cache information in AUX vector Date: Fri, 3 Jul 2020 16:57:55 +0800 Message-Id: X-Mailer: git-send-email 2.27.0 In-Reply-To: References: 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 There are no standard CSR registers to provide cache information, the way for RISC-V is to get this information from DT. Currently, AT_L1I_X, AT_L1D_X and AT_L2_X are present in glibc header, and sysconf syscall could use them to get information of cache through AUX vector. The result of 'getconf -a' as follows: LEVEL1_ICACHE_SIZE 32768 LEVEL1_ICACHE_ASSOC 8 LEVEL1_ICACHE_LINESIZE 64 LEVEL1_DCACHE_SIZE 32768 LEVEL1_DCACHE_ASSOC 8 LEVEL1_DCACHE_LINESIZE 64 LEVEL2_CACHE_SIZE 2097152 LEVEL2_CACHE_ASSOC 32 LEVEL2_CACHE_LINESIZE 64 Signed-off-by: Zong Li --- arch/riscv/include/asm/cacheinfo.h | 14 ++++++++++++ arch/riscv/include/asm/elf.h | 13 +++++++++++ arch/riscv/include/uapi/asm/auxvec.h | 23 ++++++++++++++++++- arch/riscv/kernel/cacheinfo.c | 33 +++++++++++++++++++++++++++- 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 arch/riscv/include/asm/cacheinfo.h diff --git a/arch/riscv/include/asm/cacheinfo.h b/arch/riscv/include/asm/cacheinfo.h new file mode 100644 index 000000000000..ba179cd198b9 --- /dev/null +++ b/arch/riscv/include/asm/cacheinfo.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2020 SiFive + */ + +#ifndef _ASM_RISCV_CACHEINFO_H +#define _ASM_RISCV_CACHEINFO_H + +#include + +uintptr_t get_cache_size(u32 level, enum cache_type type); +uintptr_t get_cache_geometry(u32 level, enum cache_type type); + +#endif /* _ASM_RISCV_CACHEINFO_H */ diff --git a/arch/riscv/include/asm/elf.h b/arch/riscv/include/asm/elf.h index d83a4efd052b..5c725e1df58b 100644 --- a/arch/riscv/include/asm/elf.h +++ b/arch/riscv/include/asm/elf.h @@ -11,6 +11,7 @@ #include #include #include +#include /* * These are used to set parameters in the core dumps. @@ -61,6 +62,18 @@ extern unsigned long elf_hwcap; do { \ NEW_AUX_ENT(AT_SYSINFO_EHDR, \ (elf_addr_t)current->mm->context.vdso); \ + NEW_AUX_ENT(AT_L1I_CACHESIZE, \ + get_cache_size(1, CACHE_TYPE_INST)); \ + NEW_AUX_ENT(AT_L1I_CACHEGEOMETRY, \ + get_cache_geometry(1, CACHE_TYPE_INST)); \ + NEW_AUX_ENT(AT_L1D_CACHESIZE, \ + get_cache_size(1, CACHE_TYPE_DATA)); \ + NEW_AUX_ENT(AT_L1D_CACHEGEOMETRY, \ + get_cache_geometry(1, CACHE_TYPE_DATA)); \ + NEW_AUX_ENT(AT_L2_CACHESIZE, \ + get_cache_size(2, CACHE_TYPE_UNIFIED)); \ + NEW_AUX_ENT(AT_L2_CACHEGEOMETRY, \ + get_cache_geometry(2, CACHE_TYPE_UNIFIED)); \ } while (0) #define ARCH_HAS_SETUP_ADDITIONAL_PAGES struct linux_binprm; diff --git a/arch/riscv/include/uapi/asm/auxvec.h b/arch/riscv/include/uapi/asm/auxvec.h index 22e0ae888406..32c73ba1d531 100644 --- a/arch/riscv/include/uapi/asm/auxvec.h +++ b/arch/riscv/include/uapi/asm/auxvec.h @@ -10,7 +10,28 @@ /* vDSO location */ #define AT_SYSINFO_EHDR 33 +/* + * The set of entries below represent more extensive information + * about the caches, in the form of two entry per cache type, + * one entry containing the cache size in bytes, and the other + * containing the cache line size in bytes in the bottom 16 bits + * and the cache associativity in the next 16 bits. + * + * The associativity is such that if N is the 16-bit value, the + * cache is N way set associative. A value if 0xffff means fully + * associative, a value of 1 means directly mapped. + * + * For all these fields, a value of 0 means that the information + * is not known. + */ +#define AT_L1I_CACHESIZE 40 +#define AT_L1I_CACHEGEOMETRY 41 +#define AT_L1D_CACHESIZE 42 +#define AT_L1D_CACHEGEOMETRY 43 +#define AT_L2_CACHESIZE 44 +#define AT_L2_CACHEGEOMETRY 45 + /* entries in ARCH_DLINFO */ -#define AT_VECTOR_SIZE_ARCH 1 +#define AT_VECTOR_SIZE_ARCH 7 #endif /* _UAPI_ASM_RISCV_AUXVEC_H */ diff --git a/arch/riscv/kernel/cacheinfo.c b/arch/riscv/kernel/cacheinfo.c index cdd35e53fd98..263335def44b 100644 --- a/arch/riscv/kernel/cacheinfo.c +++ b/arch/riscv/kernel/cacheinfo.c @@ -3,10 +3,41 @@ * Copyright (C) 2017 SiFive */ -#include #include #include #include +#include + +static struct cacheinfo *get_cacheinfo(u32 level, enum cache_type type) +{ + struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(smp_processor_id()); + struct cacheinfo *this_leaf; + int index; + + for (index = 0; index < this_cpu_ci->num_leaves; index++) { + this_leaf = this_cpu_ci->info_list + index; + if (this_leaf->level == level && this_leaf->type == type) + return this_leaf; + } + + return 0; +} + +uintptr_t get_cache_size(u32 level, enum cache_type type) +{ + struct cacheinfo *this_leaf = get_cacheinfo(level, type); + + return this_leaf ? this_leaf->size : 0; +} + +uintptr_t get_cache_geometry(u32 level, enum cache_type type) +{ + struct cacheinfo *this_leaf = get_cacheinfo(level, type); + uintptr_t ret = (this_leaf->ways_of_associativity << 16 | + this_leaf->coherency_line_size); + + return this_leaf ? ret : 0; +} static void ci_leaf_init(struct cacheinfo *this_leaf, enum cache_type type, unsigned int level, unsigned int size, -- 2.27.0