Skip to content

Commit 38f5bd2

Browse files
zongboxpalmer-dabbelt
authored andcommitted
riscv: Add cache information in AUX vector
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 <zong.li@sifive.com> Reviewed-by: Palmer Dabbelt <palmerdabbelt@google.com> Reviewed-by: Pekka Enberg <penberg@kernel.org> Signed-off-by: Palmer Dabbelt <palmerdabbelt@google.com>
1 parent b5fca7c commit 38f5bd2

4 files changed

Lines changed: 71 additions & 2 deletions

File tree

‎arch/riscv/include/asm/cacheinfo.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2020 SiFive
4+
*/
25

36
#ifndef _ASM_RISCV_CACHEINFO_H
47
#define _ASM_RISCV_CACHEINFO_H
@@ -11,5 +14,7 @@ struct riscv_cacheinfo_ops {
1114
};
1215

1316
void riscv_set_cacheinfo_ops(struct riscv_cacheinfo_ops *ops);
17+
uintptr_t get_cache_size(u32 level, enum cache_type type);
18+
uintptr_t get_cache_geometry(u32 level, enum cache_type type);
1419

1520
#endif /* _ASM_RISCV_CACHEINFO_H */

‎arch/riscv/include/asm/elf.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <uapi/asm/elf.h>
1212
#include <asm/auxvec.h>
1313
#include <asm/byteorder.h>
14+
#include <asm/cacheinfo.h>
1415

1516
/*
1617
* These are used to set parameters in the core dumps.
@@ -61,6 +62,18 @@ extern unsigned long elf_hwcap;
6162
do { \
6263
NEW_AUX_ENT(AT_SYSINFO_EHDR, \
6364
(elf_addr_t)current->mm->context.vdso); \
65+
NEW_AUX_ENT(AT_L1I_CACHESIZE, \
66+
get_cache_size(1, CACHE_TYPE_INST)); \
67+
NEW_AUX_ENT(AT_L1I_CACHEGEOMETRY, \
68+
get_cache_geometry(1, CACHE_TYPE_INST)); \
69+
NEW_AUX_ENT(AT_L1D_CACHESIZE, \
70+
get_cache_size(1, CACHE_TYPE_DATA)); \
71+
NEW_AUX_ENT(AT_L1D_CACHEGEOMETRY, \
72+
get_cache_geometry(1, CACHE_TYPE_DATA)); \
73+
NEW_AUX_ENT(AT_L2_CACHESIZE, \
74+
get_cache_size(2, CACHE_TYPE_UNIFIED)); \
75+
NEW_AUX_ENT(AT_L2_CACHEGEOMETRY, \
76+
get_cache_geometry(2, CACHE_TYPE_UNIFIED)); \
6477
} while (0)
6578
#define ARCH_HAS_SETUP_ADDITIONAL_PAGES
6679
struct linux_binprm;

‎arch/riscv/include/uapi/asm/auxvec.h‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,28 @@
1010
/* vDSO location */
1111
#define AT_SYSINFO_EHDR 33
1212

13+
/*
14+
* The set of entries below represent more extensive information
15+
* about the caches, in the form of two entry per cache type,
16+
* one entry containing the cache size in bytes, and the other
17+
* containing the cache line size in bytes in the bottom 16 bits
18+
* and the cache associativity in the next 16 bits.
19+
*
20+
* The associativity is such that if N is the 16-bit value, the
21+
* cache is N way set associative. A value if 0xffff means fully
22+
* associative, a value of 1 means directly mapped.
23+
*
24+
* For all these fields, a value of 0 means that the information
25+
* is not known.
26+
*/
27+
#define AT_L1I_CACHESIZE 40
28+
#define AT_L1I_CACHEGEOMETRY 41
29+
#define AT_L1D_CACHESIZE 42
30+
#define AT_L1D_CACHEGEOMETRY 43
31+
#define AT_L2_CACHESIZE 44
32+
#define AT_L2_CACHEGEOMETRY 45
33+
1334
/* entries in ARCH_DLINFO */
14-
#define AT_VECTOR_SIZE_ARCH 1
35+
#define AT_VECTOR_SIZE_ARCH 7
1536

1637
#endif /* _UAPI_ASM_RISCV_AUXVEC_H */

‎arch/riscv/kernel/cacheinfo.c‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright (C) 2017 SiFive
44
*/
55

6-
#include <linux/cacheinfo.h>
76
#include <linux/cpu.h>
87
#include <linux/of.h>
98
#include <linux/of_device.h>
@@ -25,6 +24,37 @@ cache_get_priv_group(struct cacheinfo *this_leaf)
2524
return NULL;
2625
}
2726

27+
static struct cacheinfo *get_cacheinfo(u32 level, enum cache_type type)
28+
{
29+
struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(smp_processor_id());
30+
struct cacheinfo *this_leaf;
31+
int index;
32+
33+
for (index = 0; index < this_cpu_ci->num_leaves; index++) {
34+
this_leaf = this_cpu_ci->info_list + index;
35+
if (this_leaf->level == level && this_leaf->type == type)
36+
return this_leaf;
37+
}
38+
39+
return NULL;
40+
}
41+
42+
uintptr_t get_cache_size(u32 level, enum cache_type type)
43+
{
44+
struct cacheinfo *this_leaf = get_cacheinfo(level, type);
45+
46+
return this_leaf ? this_leaf->size : 0;
47+
}
48+
49+
uintptr_t get_cache_geometry(u32 level, enum cache_type type)
50+
{
51+
struct cacheinfo *this_leaf = get_cacheinfo(level, type);
52+
53+
return this_leaf ? (this_leaf->ways_of_associativity << 16 |
54+
this_leaf->coherency_line_size) :
55+
0;
56+
}
57+
2858
static void ci_leaf_init(struct cacheinfo *this_leaf, enum cache_type type,
2959
unsigned int level, unsigned int size,
3060
unsigned int sets, unsigned int line_size)

0 commit comments

Comments
 (0)