Skip to content

Commit 71ae4bb

Browse files
committed
Skip CSR version check when cra_ring_root doesn't exist
1. Parse the new field from the auto-discovery string 2. Changed csr_version to be optional. 3.Skip csr version check if cra_ring_root doesn't exit. 4.Update the forward-compatibility auto-discovery string test. Note:previous test had an issue: The number of fields per device global is fixed (here to 1), not variable. I had to update that as well. 5.Add new unit tests.
1 parent aa2bbfe commit 71ae4bb

File tree

5 files changed

+133
-10
lines changed

5 files changed

+133
-10
lines changed

include/acl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ typedef struct acl_device_def_autodiscovery_t {
526526
// Device global definition.
527527
std::unordered_map<std::string, acl_device_global_mem_def_t>
528528
device_global_mem_defs;
529+
bool cra_ring_root_exist =
530+
true; // Set the default value to true for backwards compatibility flows.
529531
} acl_device_def_autodiscovery_t;
530532

531533
typedef struct acl_device_def_t {

include/acl_kernel_if.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,28 @@ typedef struct {
5050

5151
acl_bsp_io io;
5252

53-
unsigned int csr_version;
53+
// csr_version is absent if there is no accelerators or cra_ring_root doesn't
54+
// exist
55+
std::optional<unsigned int> csr_version;
56+
57+
// The real kern->cra_ring_root_exist value is initialized in the
58+
// acl_kernel_if_update() function (reading the actual auto-discovery string
59+
// at that time).
60+
// However, the kern->cra_ring_root_exist is referenced in the
61+
// acl_kernel_if_post_pll_config_init() to check whether it's OK to read from
62+
// the CSR. And acl_kernel_if_post_pll_config_init() can be called prior to
63+
// the acl_kernel_if_update(), using a dummy auto-discovery string.
64+
// Therefore we must set a default value. (Otherwise, it's reading an
65+
// uninitialized value)
66+
67+
// The reason of setting it to false:
68+
// Currently the cra read function will not be called in the early
69+
// acl_kernel_if_post_pll_config_init() call, because the kern->num_accel is 0
70+
// with dummy auto-discovery string, there is an if statement guard that. With
71+
// the default value to false, it will hit the assertions in the cra
72+
// read/write functions in case those are accidentally invoked too early,
73+
// e.g., in a future code refactoring.
74+
bool cra_ring_root_exist = false;
5475

5576
// Depth of hardware kernel invocation queue
5677
unsigned int *accel_invoc_queue_depth;

src/acl_auto_configure.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ static bool read_uint_counters(const std::string &str,
9999
return true;
100100
}
101101

102+
// Reads the next word in str and converts it into a boolean.
103+
// Returns true if a valid integer was read or false if an error occurred.
104+
// pos is updated to the position immediately following the parsed word
105+
// even if an error occurs.
106+
static bool read_bool_counters(const std::string &str,
107+
std::string::size_type &pos, bool &val,
108+
std::vector<int> &counters) noexcept {
109+
std::string result;
110+
pos = read_word(str, pos, result);
111+
decrement_section_counters(counters);
112+
try {
113+
val = static_cast<bool>(std::stoi(result));
114+
} catch (const std::exception &e) {
115+
UNREFERENCED_PARAMETER(e);
116+
return false;
117+
}
118+
return true;
119+
}
120+
102121
// Reads the next word in str and converts it into an unsigned.
103122
// Returns true if a valid integer was read or false if an error occurred.
104123
// pos is updated to the position immediately following the parsed word
@@ -1052,6 +1071,12 @@ bool acl_load_device_def_from_str(const std::string &config_str,
10521071
config_str, curr_pos, devdef.device_global_mem_defs, counters, err_str);
10531072
}
10541073

1074+
// Check whether csr_ring_root exist in the IP.
1075+
if (result && counters.back() > 0) {
1076+
result = read_bool_counters(config_str, curr_pos,
1077+
devdef.cra_ring_root_exist, counters);
1078+
}
1079+
10551080
// forward compatibility: bypassing remaining fields at the end of device
10561081
// description section
10571082
while (result && counters.size() > 0 &&

src/acl_kernel_if.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ static uintptr_t acl_kernel_cra_set_segment_rom(acl_kernel_if *kern,
477477

478478
static int acl_kernel_cra_read(acl_kernel_if *kern, unsigned int accel_id,
479479
unsigned int addr, unsigned int *val) {
480+
assert(kern->cra_ring_root_exist);
480481
uintptr_t segment_offset = acl_kernel_cra_set_segment(kern, accel_id, addr);
481482
acl_assert_locked_or_sig();
482483
return acl_kernel_if_read_32b(
@@ -485,6 +486,7 @@ static int acl_kernel_cra_read(acl_kernel_if *kern, unsigned int accel_id,
485486

486487
int acl_kernel_cra_read_64b(acl_kernel_if *kern, unsigned int accel_id,
487488
unsigned int addr, uint64_t *val) {
489+
assert(kern->cra_ring_root_exist);
488490
uintptr_t segment_offset = acl_kernel_cra_set_segment(kern, accel_id, addr);
489491
acl_assert_locked_or_sig();
490492
return acl_kernel_if_read_64b(
@@ -536,6 +538,7 @@ static int acl_kernel_rom_cra_read_block(acl_kernel_if *kern, unsigned int addr,
536538

537539
static int acl_kernel_cra_write(acl_kernel_if *kern, unsigned int accel_id,
538540
unsigned int addr, unsigned int val) {
541+
assert(kern->cra_ring_root_exist);
539542
uintptr_t segment_offset = acl_kernel_cra_set_segment(kern, accel_id, addr);
540543
acl_assert_locked_or_sig();
541544
return acl_kernel_if_write_32b(
@@ -544,6 +547,7 @@ static int acl_kernel_cra_write(acl_kernel_if *kern, unsigned int accel_id,
544547

545548
static int acl_kernel_cra_write_64b(acl_kernel_if *kern, unsigned int accel_id,
546549
unsigned int addr, uint64_t val) {
550+
assert(kern->cra_ring_root_exist);
547551
uintptr_t segment_offset = acl_kernel_cra_set_segment(kern, accel_id, addr);
548552
acl_assert_locked();
549553
return acl_kernel_if_write_64b(
@@ -553,6 +557,7 @@ static int acl_kernel_cra_write_64b(acl_kernel_if *kern, unsigned int accel_id,
553557
static int acl_kernel_cra_write_block(acl_kernel_if *kern,
554558
unsigned int accel_id, unsigned int addr,
555559
unsigned int *val, size_t size) {
560+
assert(kern->cra_ring_root_exist);
556561
uintptr_t segment_offset = acl_kernel_cra_set_segment(kern, accel_id, addr);
557562
uintptr_t logical_addr =
558563
kern->accel_csr[accel_id].address + addr - OFFSET_KERNEL_CRA;
@@ -712,8 +717,6 @@ int acl_kernel_if_init(acl_kernel_if *kern, acl_bsp_io bsp_io,
712717
kern->accel_perf_mon = NULL;
713718
kern->accel_num_printfs = NULL;
714719

715-
kern->csr_version = 0;
716-
717720
kern->autorun_profiling_kernel_id = -1;
718721

719722
// The simulator doesn't have any kernel interface information until the aocx
@@ -897,6 +900,11 @@ int acl_kernel_if_update(const acl_device_def_autodiscovery_t &devdef,
897900

898901
acl_kernel_if_close(kern);
899902

903+
// Initialize whether the cra_ring_root exist in the design
904+
kern->cra_ring_root_exist = devdef.cra_ring_root_exist;
905+
ACL_KERNEL_IF_DEBUG_MSG(kern, "Number of cra_ring_root : %d\n",
906+
kern->cra_ring_root_exist);
907+
900908
// Setup the PCIe HAL Structures
901909
kern->num_accel = static_cast<unsigned>(devdef.accel.size());
902910
ACL_KERNEL_IF_DEBUG_MSG(kern, "Number of Accelerators : %d\n",
@@ -1040,7 +1048,8 @@ int acl_kernel_if_post_pll_config_init(acl_kernel_if *kern) {
10401048
kern->io.printf("HAL Kern Error: Post PLL init: Invalid kernel handle");
10411049
assert(0 && "Invalid kernel handle");
10421050
}
1043-
if (kern->num_accel > 0) {
1051+
1052+
if (kern->num_accel > 0 && kern->cra_ring_root_exist) {
10441053
acl_kernel_cra_read(kern, 0, KERNEL_OFFSET_CSR, &csr);
10451054
version = ACL_KERNEL_READ_BIT_RANGE(csr, KERNEL_CSR_LAST_VERSION_BIT,
10461055
KERNEL_CSR_FIRST_VERSION_BIT);
@@ -1054,7 +1063,9 @@ int acl_kernel_if_post_pll_config_init(acl_kernel_if *kern) {
10541063
kern->cra_address_offset = 0;
10551064
}
10561065
} else {
1057-
kern->csr_version = 0;
1066+
ACL_KERNEL_IF_DEBUG_MSG(kern,
1067+
"No accelerator found or CRA ring root doesn't "
1068+
"exist, not setting kern->csr_version\n");
10581069
}
10591070

10601071
// If environment variables set, configure the profile hardware
@@ -1161,7 +1172,8 @@ void acl_kernel_if_launch_kernel_on_custom_sof(
11611172
// Version is defined in upper 16-bits of the status register and cached
11621173
// in the kern->csr_version field. The value is cached after PLL init to
11631174
// avoid reading back on every kernel launch, which would add overhead.
1164-
if (!(kern->csr_version >= CSR_VERSION_ID_18_1 &&
1175+
if (kern->csr_version.has_value() &&
1176+
!(kern->csr_version >= CSR_VERSION_ID_18_1 &&
11651177
kern->csr_version <= CSR_VERSION_ID)) {
11661178
kern->io.printf("Hardware CSR version ID differs from version expected by "
11671179
"software. Either:\n");
@@ -1253,7 +1265,8 @@ void acl_kernel_if_launch_kernel_on_custom_sof(
12531265
}
12541266
}
12551267

1256-
if (kern->csr_version != CSR_VERSION_ID_18_1) {
1268+
if (kern->csr_version.has_value() &&
1269+
(kern->csr_version != CSR_VERSION_ID_18_1)) {
12571270
for (uintptr_t p = 0; p < image->arg_value_size; p += sizeof(int)) {
12581271
unsigned int pword = *(unsigned int *)(image->arg_value + p);
12591272
ACL_KERNEL_IF_DEBUG_MSG_VERBOSE(
@@ -1270,7 +1283,9 @@ void acl_kernel_if_launch_kernel_on_custom_sof(
12701283
acl_kernel_cra_write_block(kern, accel_id, offset, (unsigned int *)image_p,
12711284
image_size_static);
12721285
}
1273-
if (kern->csr_version != CSR_VERSION_ID_18_1 && image->arg_value_size > 0) {
1286+
1287+
if (kern->csr_version.has_value() &&
1288+
(kern->csr_version != CSR_VERSION_ID_18_1 && image->arg_value_size > 0)) {
12741289
acl_kernel_cra_write_block(
12751290
kern, accel_id, offset + (unsigned int)image_size_static,
12761291
(unsigned int *)image->arg_value, image->arg_value_size);

test/acl_auto_configure_test.cpp

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,11 @@ TEST(auto_configure, many_ok_forward_compatibility) {
482482
// sections and subsections to check forward compatibility
483483

484484
std::string str(VERSIONIDTOSTR(
485-
ACL_AUTO_CONFIGURE_VERSIONID) " 28 "
485+
ACL_AUTO_CONFIGURE_VERSIONID) " 29 "
486486
"sample40byterandomhash000000000000000000 "
487487
"a10gx 0 1 15 DDR 2 1 6 0 2147483648 100 "
488488
"100 100 100 200 200 200 200 0 0 0 0 2 "
489-
"1 name1 1 name2 47 "
489+
"1 name1 name2 0 0 47 "
490490
"40 external_sort_stage_0 0 128 1 0 0 1 0 "
491491
"1 0 1 10 0 0 4 1 0 0 0 500 500 500 0 0 "
492492
"0 0 1 1 1 3 1 1 1 3 1 0 0 800 800 800 "
@@ -1410,3 +1410,63 @@ TEST(auto_configure, two_streaming_args_and_non_streaming_kernel) {
14101410
CHECK(!args[i].streaming_arg_info_available);
14111411
}
14121412
}
1413+
1414+
TEST(auto_configure, cra_ring_root_not_exist) {
1415+
const std::string config_str{
1416+
"23 50 2ccb683dee8e34a004c1a27e6d722090a8cc684d custom_ipa 0 1 9 0 2 1 2 "
1417+
"0 2199023255552 3 "
1418+
"- 0 6 5 ZTSZ4mainE4MyIP_arg_input_a 1 0 8 32768 "
1419+
"ZTSZ4mainE4MyIP_arg_input_b 1 0 8 32768 ZTSZ4mainE4MyIP_arg_input_c"
1420+
" 1 0 8 32768 ZTSZ4mainE4MyIP_arg_n 1 0 4 32768 "
1421+
"ZTSZ4mainE4MyIP_streaming_start 1 0 0 32768 "
1422+
"ZTSZ4mainE4MyIP_streaming_done"
1423+
" 0 1 0 32768 0 0 0 0 1 64 _ZTSZ4mainE4MyIP 0 128 1 0 0 1 0 1 0 4 8 2 1 "
1424+
"8 4 0 0 1 ZTSZ4mainE4MyIP_arg_input_a 8"
1425+
" 2 1 8 4 0 0 1 ZTSZ4mainE4MyIP_arg_input_b 8 2 1 8 4 0 0 1 "
1426+
"ZTSZ4mainE4MyIP_arg_input_c"
1427+
" 8 0 0 4 1 0 0 1 ZTSZ4mainE4MyIP_arg_n 0 0 0 0 1 1 1 3 1 1 1 3 1 1 1 "
1428+
"ZTSZ4mainE4MyIP_streaming_start"
1429+
" ZTSZ4mainE4MyIP_streaming_done"};
1430+
1431+
acl_device_def_autodiscovery_t devdef;
1432+
{
1433+
bool result;
1434+
std::string err_str;
1435+
ACL_LOCKED(result =
1436+
acl_load_device_def_from_str(config_str, devdef, err_str));
1437+
std::cerr << err_str;
1438+
CHECK(result);
1439+
}
1440+
1441+
CHECK_EQUAL(0, devdef.cra_ring_root_exist);
1442+
}
1443+
1444+
TEST(auto_configure, cra_ring_root_exist) {
1445+
const std::string config_str{
1446+
"23 50 2ccb683dee8e34a004c1a27e6d722090a8cc684d custom_ipa 0 1 9 0 2 1 2 "
1447+
"0 2199023255552 3 "
1448+
"- 0 6 5 ZTSZ4mainE4MyIP_arg_input_a 1 0 8 32768 "
1449+
"ZTSZ4mainE4MyIP_arg_input_b 1 0 8 32768 ZTSZ4mainE4MyIP_arg_input_c"
1450+
" 1 0 8 32768 ZTSZ4mainE4MyIP_arg_n 1 0 4 32768 "
1451+
"ZTSZ4mainE4MyIP_streaming_start 1 0 0 32768 "
1452+
"ZTSZ4mainE4MyIP_streaming_done"
1453+
" 0 1 0 32768 0 0 0 1 1 64 _ZTSZ4mainE4MyIP 0 128 1 0 0 1 0 1 0 4 8 2 1 "
1454+
"8 4 0 0 1 ZTSZ4mainE4MyIP_arg_input_a 8"
1455+
" 2 1 8 4 0 0 1 ZTSZ4mainE4MyIP_arg_input_b 8 2 1 8 4 0 0 1 "
1456+
"ZTSZ4mainE4MyIP_arg_input_c"
1457+
" 8 0 0 4 1 0 0 1 ZTSZ4mainE4MyIP_arg_n 0 0 0 0 1 1 1 3 1 1 1 3 1 1 1 "
1458+
"ZTSZ4mainE4MyIP_streaming_start"
1459+
" ZTSZ4mainE4MyIP_streaming_done"};
1460+
1461+
acl_device_def_autodiscovery_t devdef;
1462+
{
1463+
bool result;
1464+
std::string err_str;
1465+
ACL_LOCKED(result =
1466+
acl_load_device_def_from_str(config_str, devdef, err_str));
1467+
std::cerr << err_str;
1468+
CHECK(result);
1469+
}
1470+
1471+
CHECK_EQUAL(1, devdef.cra_ring_root_exist);
1472+
}

0 commit comments

Comments
 (0)