Skip to content

Commit

Permalink
Merge pull request #342 from sifive/add-l2lim
Browse files Browse the repository at this point in the history
Add support for L2 LIM.
  • Loading branch information
nategraff-sifive authored Sep 23, 2020
2 parents 58f40ee + bb293e1 commit 54b0cbc
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 17 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ nobase_include_HEADERS += \
metal/io.h \
metal/itim.h \
metal/led.h \
metal/lim.h \
metal/lock.h \
metal/memory.h \
metal/pmp.h \
Expand Down
8 changes: 4 additions & 4 deletions Makefile.in

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions gloss/crt0.S
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,36 @@ _start:
complete */
fence.i

2:

/* Copy the LIM section */
la t0, metal_segment_lim_source_start
la t1, metal_segment_lim_target_start
la t2, metal_segment_lim_target_end

beq t0, t1, 2f
bge t1, t2, 2f

1:
#if __riscv_xlen == 32
lw a0, 0(t0)
addi t0, t0, 4
sw a0, 0(t1)
addi t1, t1, 4
blt t1, t2, 1b
#else
ld a0, 0(t0)
addi t0, t0, 8
sd a0, 0(t1)
addi t1, t1, 8
blt t1, t2, 1b
#endif
2:

/* Fence all subsequent instruction fetches until after the LIM writes
complete */
fence.i

/* Zero the BSS segment. */
la t1, metal_segment_bss_target_start
la t2, metal_segment_bss_target_end
Expand Down
1 change: 1 addition & 0 deletions metal/drivers/sifive_ccache0.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef enum {

/*! @brief Initialize cache controller, enables all available
* cache-ways.
* Note: If LIM is in use, corresponding cache ways are not enabled.
* @param None.
* @return 0 If no error.*/
int sifive_ccache0_init(void);
Expand Down
20 changes: 20 additions & 0 deletions metal/lim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Copyright 2020 SiFive, Inc */
/* SPDX-License-Identifier: Apache-2.0 */

#ifndef METAL__LIM_H
#define METAL__LIM_H

/*! @file lim.h
*
* API for manipulating LIM allocation
*/

/*! @def METAL_PLACE_IN_LIM
* @brief Link a function into the LIM
*
* Link a function into the LIM (Loosely Integrated
* Memory) if the LIM is present on the target device.
*/
#define METAL_PLACE_IN_LIM __attribute__((section(".lim")))

#endif
33 changes: 20 additions & 13 deletions src/drivers/sifive_ccache0.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@
#define REG_SHIFT_16 16
#define REG_SHIFT_24 24

/* Masks to get cache configuration data */
#define SIFIVE_CCACHE0_CONFIG_BANKS_MASK 0x000000FFUL
#define SIFIVE_CCACHE0_CONFIG_WAYS_MASK 0x0000FF00UL
#define SIFIVE_CCACHE0_CONFIG_SETS_MASK 0x00FF0000UL
#define SIFIVE_CCACHE0_CONFIG_BLOCKS_MASK 0xFF000000UL

#define SIFIVE_CCACHE0_WAY_ENABLE_MASK 0x000000FFUL
#define SIFIVE_CCACHE0_BYTE_MASK 0xFFUL

static int sifive_ccache0_interrupts[] = METAL_SIFIVE_CCACHE0_INTERRUPTS;

/* Initialize cache at start-up via metal constructors */
METAL_CONSTRUCTOR(_sifive_ccache0_init) { sifive_ccache0_init(); }

/* Linker symbols to calculate LIM allocated size */
extern char metal_segment_lim_target_start, metal_segment_lim_target_end;

int sifive_ccache0_init(void) {
int ret;

Expand All @@ -42,6 +39,17 @@ int sifive_ccache0_init(void) {
/* Get cache configuration data */
sifive_ccache0_get_config(&config);

int lim_size =
&metal_segment_lim_target_end - &metal_segment_lim_target_start;

if (lim_size) { /* Do not enable cache ways, corresponding to LIM area in
use. */
while (lim_size > 0) {
lim_size -= (config.block_size * config.num_sets * config.num_bank);
config.num_ways--;
}
}

/* Enable ways */
ret = sifive_ccache0_set_enabled_ways(config.num_ways);

Expand All @@ -56,21 +64,20 @@ void sifive_ccache0_get_config(sifive_ccache0_config *config) {
val = REGW(METAL_SIFIVE_CCACHE0_CONFIG);

/* Populate cache configuration data */
config->num_bank = (val & SIFIVE_CCACHE0_CONFIG_BANKS_MASK);
config->num_ways =
(val & SIFIVE_CCACHE0_CONFIG_WAYS_MASK) >> REG_SHIFT_8;
config->num_bank = (val & SIFIVE_CCACHE0_BYTE_MASK);
config->num_ways = ((val >> REG_SHIFT_8) & SIFIVE_CCACHE0_BYTE_MASK);
config->num_sets =
2 ^ ((val & SIFIVE_CCACHE0_CONFIG_SETS_MASK) >> REG_SHIFT_16);
2 << (((val >> REG_SHIFT_16) & SIFIVE_CCACHE0_BYTE_MASK) - 1);
config->block_size =
2 ^ ((val & SIFIVE_CCACHE0_CONFIG_BLOCKS_MASK) >> REG_SHIFT_24);
2 << (((val >> REG_SHIFT_24) & SIFIVE_CCACHE0_BYTE_MASK) - 1);
}
}

uint32_t sifive_ccache0_get_enabled_ways(void) {

uint32_t val = 0;

val = SIFIVE_CCACHE0_WAY_ENABLE_MASK & REGW(METAL_SIFIVE_CCACHE0_WAYENABLE);
val = SIFIVE_CCACHE0_BYTE_MASK & REGW(METAL_SIFIVE_CCACHE0_WAYENABLE);

/* The stored number is the way index, so increment by 1 */
val++;
Expand Down

0 comments on commit 54b0cbc

Please sign in to comment.