Skip to content

Commit 0519883

Browse files
Minor fixes
1 parent 1c03839 commit 0519883

File tree

7 files changed

+83
-61
lines changed

7 files changed

+83
-61
lines changed

src/riscv/api.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,29 @@ CPUINFO_INTERNAL void cpuinfo_riscv_decode_vendor_uarch(
4242
enum cpuinfo_vendor vendor[restrict static 1],
4343
enum cpuinfo_uarch uarch[restrict static 1]);
4444

45-
CPUINFO_INTERNAL void cpuinfo_riscv_decode_cache(
45+
/**
46+
* Decodes the cache hierarchy based on the provided inpu parameters,
47+
* regardless of underlying operating system.
48+
*
49+
* @param[uarch]: The processor micro-architecture code.
50+
* @param[l1i] - Reference to the l1i cpuinfo_cache to populate.
51+
* @param[l1d]: - Reference to the l1d cpuinfo_cache to populate.
52+
* @param[l2]: - Reference to the l2 cpuinfo_cache to populate.
53+
* @return false if any error occurred, true otherwise
54+
*/
55+
56+
CPUINFO_INTERNAL bool cpuinfo_riscv_decode_cache(
4657
enum cpuinfo_uarch uarch,
4758
struct cpuinfo_cache l1i[restrict static 1],
4859
struct cpuinfo_cache l1d[restrict static 1],
4960
struct cpuinfo_cache l2[restrict static 1]);
5061

62+
/**
63+
* Extracts the maximum cache size from a RISC-V processor, independently
64+
* of underlying operating system.
65+
*
66+
* @param[processor]: The RISC-V processor.
67+
* @preturn: The maximum cache size.
68+
*/
5169
CPUINFO_INTERNAL uint32_t cpuinfo_riscv_compute_max_cache_size(
5270
const struct cpuinfo_processor processor[restrict static 1]);

src/riscv/cache.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#include <cpuinfo/internal-api.h>
55
#include <cpuinfo/log.h>
66

7-
void cpuinfo_riscv_decode_cache(
7+
bool cpuinfo_riscv_decode_cache(
88
enum cpuinfo_uarch uarch,
99
struct cpuinfo_cache l1i[restrict static 1],
1010
struct cpuinfo_cache l1d[restrict static 1],
11-
struct cpuinfo_cache l2[restrict static 1])
12-
{
11+
struct cpuinfo_cache l2[restrict static 1]) {
1312
switch(uarch) {
13+
// According to https://starfivetech.com/uploads/u74mc_core_complex_manual_21G1.pdf
1414
case cpuinfo_uarch_u74_mc:
1515
*l1i = (struct cpuinfo_cache) {
1616
.size = 32 * 1024,
@@ -30,6 +30,7 @@ void cpuinfo_riscv_decode_cache(
3030
break;
3131
default:
3232
cpuinfo_log_warning("target uarch not recognized; cache data is not populated");
33+
return false;
3334
}
3435
l1i->sets = l1i->size / (l1i->associativity * l1i->line_size);
3536
l1i->partitions = 1;
@@ -39,14 +40,17 @@ void cpuinfo_riscv_decode_cache(
3940
l2->sets = l2->size / (l2->associativity * l2->line_size);
4041
l2->partitions = 1;
4142
}
43+
44+
return true;
4245
}
4346

4447
uint32_t cpuinfo_riscv_compute_max_cache_size(const struct cpuinfo_processor* processor) {
4548
switch(processor->core->uarch) {
49+
// According to https://starfivetech.com/uploads/u74mc_core_complex_manual_21G1.pdf
4650
case cpuinfo_uarch_u74_mc:
4751
return 2 * 1024 * 1024;
4852
default:
49-
cpuinfo_log_warning("target uarch not recognized; mas cache size is not populated");
53+
cpuinfo_log_warning("target uarch not recognized; max cache size is not populated");
5054
return 0;
5155
}
5256
}

src/riscv/linux/api.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,24 @@ CPUINFO_INTERNAL void cpuinfo_riscv_linux_decode_vendor_uarch_from_hwprobe(
8686
enum cpuinfo_vendor vendor[restrict static 1],
8787
enum cpuinfo_uarch uarch[restrict static 1]);
8888

89-
CPUINFO_INTERNAL void cpuinfo_riscv_linux_count_cluster_processors(
90-
uint32_t max_processors,
91-
struct cpuinfo_riscv_linux_processor processors[restrict static max_processors]);
89+
/**
90+
* Reads the value of hwcap from the `getauxval` function, or
91+
* mocks a fake value for testing purposes
92+
*
93+
* @param[hwcap] - The hwcap flags to be populated
94+
*/
9295

9396
CPUINFO_INTERNAL void cpuinfo_riscv_linux_hwcap_from_getauxval(
9497
uint32_t hwcap[restrict static 1]);
9598

99+
/**
100+
* Parses the output of the `/proc/cpuinfo` command to extract
101+
* info about the RISC-V processors.
102+
*
103+
* @param[max_processors_count] - The maximum number of processors.
104+
* @param processors - Reference to the processor list to populate.
105+
* @return false if any error occurred, true otherwise.
106+
*/
96107
CPUINFO_INTERNAL bool cpuinfo_riscv_linux_parse_proc_cpuinfo(
97108
uint32_t max_processors_count,
98109
struct cpuinfo_riscv_linux_processor processors[restrict static max_processors_count]);

src/riscv/linux/cpuinfo.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static uint32_t parse_processor_number(
2121

2222
if (processor_length == 0) {
2323
cpuinfo_log_warning("Processor number in /proc/cpuinfo is ignored: string is empty");
24-
return 0;
24+
return -1;
2525
}
2626

2727
uint32_t processor_number = 0;
@@ -45,7 +45,7 @@ static void parse_isa(
4545
struct cpuinfo_riscv_linux_processor processor[restrict static 1])
4646
{
4747
const size_t isa_length = (size_t) (isa_end - isa_start);
48-
if (!(memcmp(isa_start, "rv32", 4) == 0 || memcmp(isa_start, "rv64", 4) == 0)) {
48+
if (isa_length < 4 || !(memcmp(isa_start, "rv32", 4) == 0 || memcmp(isa_start, "rv64", 4) == 0)) {
4949
cpuinfo_log_error("Invalid isa format in /proc/cpuinfo: %.*s. It should start with either `rv32` or `rv64`",
5050
(int) (isa_length), isa_start);
5151
return;
@@ -178,8 +178,8 @@ static bool parse_line(
178178
}
179179
/* Skip line if no ':' separator was found. */
180180
if (separator == line_end) {
181-
cpuinfo_log_info("Line %.*s in /proc/cpuinfo is ignored: key/value separator ':' not found",
182-
(int) (line_end - line_start), line_start);
181+
cpuinfo_log_warning("Line %.*s in /proc/cpuinfo is ignored: key/value separator ':' not found",
182+
(int) (line_end - line_start), line_start);
183183
return true;
184184
}
185185

@@ -238,13 +238,6 @@ static bool parse_line(
238238
goto unknown;
239239
}
240240
break;
241-
case 4:
242-
if (memcmp(line_start, "hart", key_length) == 0) {
243-
// Do nothing
244-
} else {
245-
goto unknown;
246-
}
247-
break;
248241
case 5:
249242
if (memcmp(line_start, "uarch", key_length) == 0) {
250243
parse_uarch(value_start, value_end, processor);
@@ -255,7 +248,10 @@ static bool parse_line(
255248
case 9:
256249
if (memcmp(line_start, "processor", key_length) == 0) {
257250
const uint32_t new_processor_index = parse_processor_number(value_start, value_end);
258-
if (new_processor_index < processor_index) {
251+
if (new_processor_index < 0) {
252+
/* Strange: empty string */
253+
break;
254+
} else if (new_processor_index < processor_index) {
259255
/* Strange: decreasing processor number */
260256
cpuinfo_log_warning(
261257
"unexpectedly low processor number %"PRIu32" following processor %"PRIu32" in /proc/cpuinfo",
@@ -280,6 +276,8 @@ static bool parse_line(
280276
}
281277
break;
282278
default:
279+
// Do nothing
280+
break;
283281
unknown:
284282
cpuinfo_log_debug("unknown /proc/cpuinfo key: %.*s", (int) key_length, line_start);
285283
}

src/riscv/linux/hwcap.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,16 @@
22

33
#if CPUINFO_MOCK
44
#include <cpuinfo-mock.h>
5-
#endif
6-
7-
#if CPUINFO_ARCH_RISCV32 || CPUINFO_ARCH_RISCV64
8-
#include <sys/auxv.h>
9-
#else
10-
#define AT_HWCAP 16
11-
#define AT_HWCAP2 26
12-
#endif
135

14-
#if CPUINFO_MOCK
156
static uint32_t mock_hwcap = 0;
167
void cpuinfo_set_hwcap(uint32_t hwcap) {
178
mock_hwcap = hwcap;
189
}
19-
20-
static uint32_t mock_hwcap2 = 0;
21-
void cpuinfo_set_hwcap2(uint32_t hwcap2) {
22-
mock_hwcap2 = hwcap2;
23-
}
2410
#endif
2511

2612
#if CPUINFO_ARCH_RISCV32 || CPUINFO_ARCH_RISCV64
13+
#include <sys/auxv.h>
14+
2715
void cpuinfo_riscv_linux_hwcap_from_getauxval(
2816
uint32_t hwcap[restrict static 1])
2917
{

src/riscv/linux/init.c

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ void cpuinfo_riscv_linux_init(void) {
447447
enum cpuinfo_uarch last_uarch = cpuinfo_uarch_unknown;
448448
for (size_t processor = 0; processor < riscv_linux_processors_count; processor++) {
449449
if (!bitmask_all(riscv_linux_processors[processor].flags, CPUINFO_LINUX_FLAG_VALID)) {
450-
continue;
450+
break;
451451
}
452452

453453
/**
@@ -627,15 +627,16 @@ void cpuinfo_riscv_linux_init(void) {
627627

628628
/* Populate cache information structures in l1i, l1d */
629629
struct cpuinfo_cache temp_l2 = { 0 };
630-
cpuinfo_riscv_decode_cache(
630+
if (cpuinfo_riscv_decode_cache(
631631
riscv_linux_processors[processor].core.uarch,
632-
&l1i[processor], &l1d[processor], &temp_l2);
633-
l1i[processor].processor_start = l1d[processor].processor_start = processor;
634-
l1i[processor].processor_count = l1d[processor].processor_count = 1;
635-
if (temp_l2.size != 0) {
636-
/* Assume L2 is shared by cores in the same cluster */
637-
if (riscv_linux_processors[processor].package_leader_id == linux_id) {
638-
l2_count += 1;
632+
&l1i[processor], &l1d[processor], &temp_l2)) {
633+
l1i[processor].processor_start = l1d[processor].processor_start = processor;
634+
l1i[processor].processor_count = l1d[processor].processor_count = 1;
635+
if (temp_l2.size != 0) {
636+
/* Assume L2 is shared by cores in the same cluster */
637+
if (riscv_linux_processors[processor].cluster_leader_id == linux_id) {
638+
l2_count += 1;
639+
}
639640
}
640641
}
641642

@@ -669,25 +670,27 @@ void cpuinfo_riscv_linux_init(void) {
669670
uint32_t l2_index = UINT32_MAX;
670671
for (uint32_t processor = 0; processor < valid_processors_count; processor++) {
671672
struct cpuinfo_cache dummy_l1i, dummy_l1d, temp_l2 = { 0 };
672-
cpuinfo_riscv_decode_cache(
673+
if (cpuinfo_riscv_decode_cache(
673674
riscv_linux_processors[processor].core.uarch,
674-
&dummy_l1i, &dummy_l1d, &temp_l2);
675-
676-
if (temp_l2.size != 0) {
677-
if (riscv_linux_processors[processor].package_leader_id == riscv_linux_processors[processor].processor.linux_id) {
678-
l2_index += 1;
679-
l2[l2_index] = (struct cpuinfo_cache) {
680-
.size = temp_l2.size,
681-
.associativity = temp_l2.associativity,
682-
.sets = temp_l2.sets,
683-
.partitions = temp_l2.partitions,
684-
.line_size = temp_l2.line_size,
685-
.flags = temp_l2.flags,
686-
.processor_start = processor,
687-
.processor_count = riscv_linux_processors[processor].package.processor_count,
688-
};
675+
&dummy_l1i, &dummy_l1d, &temp_l2)) {
676+
677+
if (temp_l2.size != 0) {
678+
if (riscv_linux_processors[processor].cluster_leader_id ==
679+
riscv_linux_processors[processor].processor.linux_id) {
680+
l2_index += 1;
681+
l2[l2_index] = (struct cpuinfo_cache) {
682+
.size = temp_l2.size,
683+
.associativity = temp_l2.associativity,
684+
.sets = temp_l2.sets,
685+
.partitions = temp_l2.partitions,
686+
.line_size = temp_l2.line_size,
687+
.flags = temp_l2.flags,
688+
.processor_start = processor,
689+
.processor_count = riscv_linux_processors[processor].package.processor_count,
690+
};
691+
}
692+
processors[processor].cache.l2 = l2 + l2_index;
689693
}
690-
processors[processor].cache.l2 = l2 + l2_index;
691694
}
692695
}
693696

src/riscv/linux/riscv-isa.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
void cpuinfo_riscv_linux_decode_isa_from_hwcap(
88
struct cpuinfo_riscv_isa isa[restrict static 1]) {
9-
unsigned long hwcap = 0;
9+
uint32_t hwcap = 0;
1010
cpuinfo_riscv_linux_hwcap_from_getauxval(&hwcap);
1111

1212
if (hwcap & CPUINFO_RISCV_LINUX_FEATURE_I) {

0 commit comments

Comments
 (0)