From 991af2bd8fb6c79dfb317837353857f34a727b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonny=20Sv=C3=A4rd?= Date: Thu, 15 Apr 2021 17:31:01 +0200 Subject: Enable MPU and CPU cache for Corstone-300 - Enable CPU instruction- and data cache by default. - Add a CMake option to turn CPU cache on/off. - Add basic MPU configuration for memory areas. Make the code segment RO (NS address' are reachable from secure state, hence MPU config entries for both S and NS address of ITCM). - Target latest NPU API version Change-Id: Ie9bf2f02e5ad534375d146804fdc66b9f2f6770f Change-Id: I9def430d1e61d18e521798db4f48ed0a8c58380e --- targets/corstone-300/target.cpp | 85 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 2 deletions(-) (limited to 'targets/corstone-300/target.cpp') diff --git a/targets/corstone-300/target.cpp b/targets/corstone-300/target.cpp index e5f4680..a44c12f 100644 --- a/targets/corstone-300/target.cpp +++ b/targets/corstone-300/target.cpp @@ -26,6 +26,7 @@ #include #endif +#include "mpu.hpp" #include #include "uart.h" @@ -33,6 +34,7 @@ #include #include #include +#include using namespace EthosU; @@ -57,6 +59,10 @@ __attribute__((aligned(16), section(".bss.ethosu_scratch"))) uint8_t ethosu_scra #define ETHOSU_FAST_MEMORY_SIZE 0 #endif +#ifdef ETHOSU +struct ethosu_driver *ethosu0_driver = ðosu_drv; +#endif + static uintptr_t ethosu_ta_base_addrs[ETHOSU_NPU_COUNT][ETHOSU_NPU_TA_COUNT] = { {ETHOSU0_TA0_BASE_ADDRESS, ETHOSU0_TA1_BASE_ADDRESS}}; struct timing_adapter ethosu_ta[ETHOSU_NPU_COUNT][ETHOSU_NPU_TA_COUNT]; @@ -152,15 +158,90 @@ void targetSetup() { #ifdef ETHOSU // Initialize Ethos-U NPU driver - if (ethosu_init_v3(reinterpret_cast(ETHOSU_BASE_ADDRESS), ethosu_scratch, ETHOSU_FAST_MEMORY_SIZE, 1, 1)) { + if (ethosu_init_v4(ethosu0_driver, + reinterpret_cast(ETHOSU_BASE_ADDRESS), + ethosu_scratch, + ETHOSU_FAST_MEMORY_SIZE, + 1, + 1)) { printf("Failed to initialize NPU.\n"); return; } - /* Assumes SCB->VTOR point to RW memory */ + // Assumes SCB->VTOR point to RW memory NVIC_SetVector(static_cast(ETHOSU_IRQ), (uint32_t)ðosuIrqHandler); NVIC_EnableIRQ(static_cast(ETHOSU_IRQ)); #endif + + // MPU setup + const std::vector mpuConfig = { + { + // ITCM + ARM_MPU_RBAR(0x00000000, // Base + ARM_MPU_SH_NON, // Non-shareable + 1, // Read-Only + 1, // Non-Privileged + 0), // eXecute Never disabled + ARM_MPU_RLAR(0x0007ffff, // Limit + Mpu::WTRA_index) // Attribute index - Write-Through, Read-allocate + }, + { + // ITCM + ARM_MPU_RBAR(0x10000000, // Base + ARM_MPU_SH_NON, // Non-shareable + 1, // Read-Only + 1, // Non-Privileged + 0), // eXecute Never disabled + ARM_MPU_RLAR(0x1007ffff, // Limit + Mpu::WTRA_index) // Attribute index - Write-Through, Read-allocate + }, + { + // FPGA DATA SRAM; BRAM + ARM_MPU_RBAR(0x11000000, // Base + ARM_MPU_SH_NON, // Non-shareable + 0, // Read-Write + 1, // Non-Privileged + 0), // eXecute Never disabled + ARM_MPU_RLAR(0x111fffff, // Limit + Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate + }, + { + // DTCM + ARM_MPU_RBAR(0x30000000, // Base + ARM_MPU_SH_NON, // Non-shareable + 0, // Read-Write + 1, // Non-Privileged + 1), // eXecute Never enabled + ARM_MPU_RLAR(0x3007ffff, // Limit + Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate + }, + { + // SSE-300 internal SRAM + ARM_MPU_RBAR(0x31000000, // Base + ARM_MPU_SH_NON, // Non-shareable + 0, // Read-Write + 1, // Non-Privileged + 1), // eXecute Never enabled + ARM_MPU_RLAR(0x313fffff, // Limit + Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate + }, + { + ARM_MPU_RBAR(0x70000000, // Base + ARM_MPU_SH_NON, // Non-shareable + 0, // Read-Write + 1, // Non-Privileged + 1), // eXecute Never enabled + ARM_MPU_RLAR(0x7fffffff, // Limit + Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate + }}; + + // Setup MPU configuration + Mpu::loadAndEnableConfig(&mpuConfig[0], mpuConfig.size()); + +#if defined(CPU_CACHE_ENABLE) && defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U) + SCB_EnableICache(); + SCB_EnableDCache(); +#endif } } // namespace EthosU -- cgit v1.2.1