Skip to content

Commit

Permalink
[SW] move l1cache software into snRuntime
Browse files Browse the repository at this point in the history
  • Loading branch information
Navaneeth-KunhiPurayil committed Nov 29, 2024
1 parent e04e18b commit 15cf9d6
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 66 deletions.
1 change: 1 addition & 0 deletions sw/snRuntime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ set(sources
src/alloc.c
src/interrupt.c
src/perf_cnt.c
src/l1cache.c
)

# platform specific sources
Expand Down
18 changes: 18 additions & 0 deletions sw/snRuntime/include/l1cache.h
Original file line number Diff line number Diff line change
@@ -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();
73 changes: 73 additions & 0 deletions sw/snRuntime/src/l1cache.c
Original file line number Diff line number Diff line change
@@ -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 <l1cache.h>

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;
}
60 changes: 0 additions & 60 deletions sw/spatzBenchmarks/benchmark/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 1 addition & 6 deletions sw/spatzBenchmarks/include/benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
#pragma once
#include <snrt.h>
#include <stddef.h>

#include <l1cache.h>
#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);

0 comments on commit 15cf9d6

Please sign in to comment.