From 15cf9d6f379b04178d00e4664d129502faeb0481 Mon Sep 17 00:00:00 2001 From: Navaneeth-Kunhi Purayil Date: Fri, 29 Nov 2024 17:12:54 +0100 Subject: [PATCH] [SW] move l1cache software into snRuntime --- sw/snRuntime/CMakeLists.txt | 1 + sw/snRuntime/include/l1cache.h | 18 ++++++ sw/snRuntime/src/l1cache.c | 73 ++++++++++++++++++++++++ sw/spatzBenchmarks/benchmark/benchmark.c | 60 ------------------- sw/spatzBenchmarks/include/benchmark.h | 7 +-- 5 files changed, 93 insertions(+), 66 deletions(-) create mode 100644 sw/snRuntime/include/l1cache.h create mode 100644 sw/snRuntime/src/l1cache.c diff --git a/sw/snRuntime/CMakeLists.txt b/sw/snRuntime/CMakeLists.txt index 1603f5f5..cd6b8d46 100644 --- a/sw/snRuntime/CMakeLists.txt +++ b/sw/snRuntime/CMakeLists.txt @@ -63,6 +63,7 @@ set(sources src/alloc.c src/interrupt.c src/perf_cnt.c + src/l1cache.c ) # platform specific sources diff --git a/sw/snRuntime/include/l1cache.h b/sw/snRuntime/include/l1cache.h new file mode 100644 index 00000000..e83a231d --- /dev/null +++ b/sw/snRuntime/include/l1cache.h @@ -0,0 +1,18 @@ +// Copyright 2020 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. + +// SPDX-License-Identifier: Apache-2.0 + +#include "encoding.h" +#include "spatz_cluster_peripheral.h" +#include "team.h" + +extern __thread struct snrt_team *_snrt_team_current; + +void l1d_commit(); +void l1d_init(uint32_t size); +void l1d_flush(); +void l1d_wait(); +void l1d_spm_config (uint32_t size); + +void set_eoc(); diff --git a/sw/snRuntime/src/l1cache.c b/sw/snRuntime/src/l1cache.c new file mode 100644 index 00000000..2b3122be --- /dev/null +++ b/sw/snRuntime/src/l1cache.c @@ -0,0 +1,73 @@ +// Copyright 2020 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. + +// SPDX-License-Identifier: Apache-2.0 + +#include + +void l1d_commit() { + uint32_t *commit = + (uint32_t *)(_snrt_team_current->root->cluster_mem.end + + SPATZ_CLUSTER_PERIPHERAL_L1D_INSN_COMMIT_REG_OFFSET); + *commit = 1; +} + +void l1d_init(uint32_t size) { + uint32_t *insn = + (uint32_t *)(_snrt_team_current->root->cluster_mem.end + + SPATZ_CLUSTER_PERIPHERAL_CFG_L1D_INSN_REG_OFFSET); + *insn = 3; + l1d_commit(); + l1d_wait(); + // Write in the default config immediately after initialization + // No need to call outside unless need a different config + l1d_spm_config(size); +} + +void l1d_flush() { + uint32_t *insn = + (uint32_t *)(_snrt_team_current->root->cluster_mem.end + + SPATZ_CLUSTER_PERIPHERAL_CFG_L1D_INSN_REG_OFFSET); + *insn = 0; + l1d_commit(); +} + +void l1d_wait() { + volatile uint32_t *busy = + (uint32_t *)(_snrt_team_current->root->cluster_mem.end + + SPATZ_CLUSTER_PERIPHERAL_L1D_FLUSH_STATUS_REG_OFFSET); + // wait until flush finished + while (*busy) { + + } +} + +void l1d_spm_config (uint32_t size) { + // flush the cache before reconfiguration + l1d_flush(); + l1d_wait(); + // free all allocated region + snrt_l1alloc_reset(); + // set the pointers + volatile uint32_t *cfg_size = + (uint32_t *)(_snrt_team_current->root->cluster_mem.end + + SPATZ_CLUSTER_PERIPHERAL_CFG_L1D_SPM_REG_OFFSET); + volatile uint32_t *commit = + (uint32_t *)(_snrt_team_current->root->cluster_mem.end + + SPATZ_CLUSTER_PERIPHERAL_L1D_SPM_COMMIT_REG_OFFSET); + // Make sure dummy region will not be optimized away + volatile double *dummy; + // Should be (L1_size - size) * 128 + int cache_region = (128 - size) * 128; + dummy = (volatile double *)snrt_l1alloc(cache_region * sizeof(double)); + // change size and commit the change + *cfg_size = size; + *commit = 1; +} + +void set_eoc () { + volatile uint32_t *eoc_reg = + (uint32_t *)(_snrt_team_current->root->cluster_mem.end + + SPATZ_CLUSTER_PERIPHERAL_CLUSTER_EOC_EXIT_REG_OFFSET); + *eoc_reg = 1; +} \ No newline at end of file diff --git a/sw/spatzBenchmarks/benchmark/benchmark.c b/sw/spatzBenchmarks/benchmark/benchmark.c index 2f26aee4..712771cb 100644 --- a/sw/spatzBenchmarks/benchmark/benchmark.c +++ b/sw/spatzBenchmarks/benchmark/benchmark.c @@ -24,63 +24,3 @@ void stop_kernel() { SPATZ_CLUSTER_PERIPHERAL_SPATZ_STATUS_REG_OFFSET); *bench = 0; } - -void l1d_commit() { - uint32_t *commit = - (uint32_t *)(_snrt_team_current->root->cluster_mem.end + - SPATZ_CLUSTER_PERIPHERAL_L1D_INSN_COMMIT_REG_OFFSET); - *commit = 1; -} - -void l1d_init(uint32_t size) { - uint32_t *insn = - (uint32_t *)(_snrt_team_current->root->cluster_mem.end + - SPATZ_CLUSTER_PERIPHERAL_CFG_L1D_INSN_REG_OFFSET); - *insn = 3; - l1d_commit(); - l1d_wait(); - // Write in the default config immediately after initialization - // No need to call outside unless need a different config - l1d_spm_config(size); -} - -void l1d_flush() { - uint32_t *insn = - (uint32_t *)(_snrt_team_current->root->cluster_mem.end + - SPATZ_CLUSTER_PERIPHERAL_CFG_L1D_INSN_REG_OFFSET); - *insn = 0; - l1d_commit(); -} - -void l1d_wait() { - volatile uint32_t *busy = - (uint32_t *)(_snrt_team_current->root->cluster_mem.end + - SPATZ_CLUSTER_PERIPHERAL_L1D_FLUSH_STATUS_REG_OFFSET); - // wait until flush finished - while (*busy) { - - } -} - -void l1d_spm_config (uint32_t size) { - // flush the cache before reconfiguration - l1d_flush(); - l1d_wait(); - // free all allocated region - snrt_l1alloc_reset(); - // set the pointers - volatile uint32_t *cfg_size = - (uint32_t *)(_snrt_team_current->root->cluster_mem.end + - SPATZ_CLUSTER_PERIPHERAL_CFG_L1D_SPM_REG_OFFSET); - volatile uint32_t *commit = - (uint32_t *)(_snrt_team_current->root->cluster_mem.end + - SPATZ_CLUSTER_PERIPHERAL_L1D_SPM_COMMIT_REG_OFFSET); - // Make sure dummy region will not be optimized away - volatile double *dummy; - // Should be (L1_size - size) * 128 - int cache_region = (128 - size) * 128; - dummy = (volatile double *)snrt_l1alloc(cache_region * sizeof(double)); - // change size and commit the change - *cfg_size = size; - *commit = 1; -} diff --git a/sw/spatzBenchmarks/include/benchmark.h b/sw/spatzBenchmarks/include/benchmark.h index 639cfd17..b6286710 100644 --- a/sw/spatzBenchmarks/include/benchmark.h +++ b/sw/spatzBenchmarks/include/benchmark.h @@ -4,15 +4,10 @@ #pragma once #include #include - +#include #include "printf.h" size_t benchmark_get_cycle(); void start_kernel(); void stop_kernel(); -void l1d_commit(); -void l1d_init(uint32_t size); -void l1d_flush(); -void l1d_wait(); -void l1d_spm_config (uint32_t size);