blob: f186ddfb20717bea42f1b0434e044b676611fce8 [file] [log] [blame]
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +01001/*
Jonny Svärd4db21c92023-05-15 11:44:05 +02002 * SPDX-FileCopyrightText: Copyright 2020-2021, 2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +01003 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19/****************************************************************************
20 * Includes
21 ****************************************************************************/
22
23#include "target.hpp"
24
25#ifdef ETHOSU
26#include <ethosu_driver.h>
Kristofer Jonsson7b8fa5e2021-12-16 14:02:56 +010027#include <timing_adapter.h>
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010028#endif
29
Jonny Svärd991af2b2021-04-15 17:31:01 +020030#include "mpu.hpp"
Yulia Garboviche9cdc632021-11-23 20:00:04 +020031#include "uart_stdout.h"
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010032
Jonny Svärdd6670902021-03-18 15:49:27 +010033#include <inttypes.h>
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010034#include <stdio.h>
Per Åstrand174e98d2021-02-09 17:48:53 +010035#include <stdlib.h>
Jonny Svärd991af2b2021-04-15 17:31:01 +020036#include <vector>
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010037
38using namespace EthosU;
39
40/****************************************************************************
41 * Defines
42 ****************************************************************************/
43
44#define ETHOSU_BASE_ADDRESS 0x48102000
Jonny Svärdd6670902021-03-18 15:49:27 +010045#define ETHOSU_IRQ 56
Jonny Svärd4db21c92023-05-15 11:44:05 +020046#define ETHOSU_IRQ_PRIORITY 5
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010047
Jonny Svärdd6670902021-03-18 15:49:27 +010048#define ETHOSU0_TA0_BASE_ADDRESS 0x48103000
49#define ETHOSU0_TA1_BASE_ADDRESS 0x48103200
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010050
51/****************************************************************************
52 * Variables
53 ****************************************************************************/
54
55#if defined(ETHOSU_FAST_MEMORY_SIZE) && ETHOSU_FAST_MEMORY_SIZE > 0
56__attribute__((aligned(16), section(".bss.ethosu_scratch"))) uint8_t ethosu_scratch[ETHOSU_FAST_MEMORY_SIZE];
57#else
58#define ethosu_scratch 0
59#define ETHOSU_FAST_MEMORY_SIZE 0
60#endif
61
Jonny Svärd991af2b2021-04-15 17:31:01 +020062#ifdef ETHOSU
Jonny Svärdc5941c42021-06-01 18:40:45 +020063struct ethosu_driver ethosu0_driver;
Jonny Svärd991af2b2021-04-15 17:31:01 +020064#endif
65
Nir Ekhauza58edd82021-10-04 12:21:17 +030066/****************************************************************************
67 * Timing Adapters
68 ****************************************************************************/
69
Kristofer Jonsson7b8fa5e2021-12-16 14:02:56 +010070#ifdef ETHOSU
71
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020072#ifndef ETHOSU_TA_MAXR_0
Nir Ekhauza58edd82021-10-04 12:21:17 +030073#define ETHOSU_TA_MAXR_0 0
74#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020075
76#ifndef ETHOSU_TA_MAXW_0
Nir Ekhauza58edd82021-10-04 12:21:17 +030077#define ETHOSU_TA_MAXW_0 0
78#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020079
80#ifndef ETHOSU_TA_MAXRW_0
Nir Ekhauza58edd82021-10-04 12:21:17 +030081#define ETHOSU_TA_MAXRW_0 0
82#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020083
84#ifndef ETHOSU_TA_RLATENCY_0
Nir Ekhauza58edd82021-10-04 12:21:17 +030085#define ETHOSU_TA_RLATENCY_0 0
86#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020087
88#ifndef ETHOSU_TA_WLATENCY_0
Nir Ekhauza58edd82021-10-04 12:21:17 +030089#define ETHOSU_TA_WLATENCY_0 0
90#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020091
92#ifndef ETHOSU_TA_PULSE_ON_0
Nir Ekhauza58edd82021-10-04 12:21:17 +030093#define ETHOSU_TA_PULSE_ON_0 0
94#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020095
96#ifndef ETHOSU_TA_PULSE_OFF_0
Nir Ekhauza58edd82021-10-04 12:21:17 +030097#define ETHOSU_TA_PULSE_OFF_0 0
98#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020099
100#ifndef ETHOSU_TA_BWCAP_0
Nir Ekhauza58edd82021-10-04 12:21:17 +0300101#define ETHOSU_TA_BWCAP_0 0
102#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200103
104#ifndef ETHOSU_TA_PERFCTRL_0
Nir Ekhauza58edd82021-10-04 12:21:17 +0300105#define ETHOSU_TA_PERFCTRL_0 0
106#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200107
108#ifndef ETHOSU_TA_PERFCNT_0
Nir Ekhauza58edd82021-10-04 12:21:17 +0300109#define ETHOSU_TA_PERFCNT_0 0
110#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200111
112#ifndef ETHOSU_TA_MODE_0
113#define ETHOSU_TA_MODE_0 1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300114#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200115
116#ifndef ETHOSU_TA_HISTBIN_0
Nir Ekhauza58edd82021-10-04 12:21:17 +0300117#define ETHOSU_TA_HISTBIN_0 0
118#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200119
120#ifndef ETHOSU_TA_HISTCNT_0
Nir Ekhauza58edd82021-10-04 12:21:17 +0300121#define ETHOSU_TA_HISTCNT_0 0
122#endif
123
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200124#ifndef ETHOSU_TA_MAXR_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300125#define ETHOSU_TA_MAXR_1 0
126#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200127
128#ifndef ETHOSU_TA_MAXW_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300129#define ETHOSU_TA_MAXW_1 0
130#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200131
132#ifndef ETHOSU_TA_MAXRW_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300133#define ETHOSU_TA_MAXRW_1 0
134#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200135
136#ifndef ETHOSU_TA_RLATENCY_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300137#define ETHOSU_TA_RLATENCY_1 0
138#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200139
140#ifndef ETHOSU_TA_WLATENCY_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300141#define ETHOSU_TA_WLATENCY_1 0
142#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200143
144#ifndef ETHOSU_TA_PULSE_ON_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300145#define ETHOSU_TA_PULSE_ON_1 0
146#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200147
148#ifndef ETHOSU_TA_PULSE_OFF_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300149#define ETHOSU_TA_PULSE_OFF_1 0
150#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200151
152#ifndef ETHOSU_TA_BWCAP_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300153#define ETHOSU_TA_BWCAP_1 0
154#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200155
156#ifndef ETHOSU_TA_PERFCTRL_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300157#define ETHOSU_TA_PERFCTRL_1 0
158#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200159
160#ifndef ETHOSU_TA_PERFCNT_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300161#define ETHOSU_TA_PERFCNT_1 0
162#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200163
164#ifndef ETHOSU_TA_MODE_1
165#define ETHOSU_TA_MODE_1 1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300166#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200167
168#ifndef ETHOSU_TA_HISTBIN_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300169#define ETHOSU_TA_HISTBIN_1 0
170#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200171
172#ifndef ETHOSU_TA_HISTCNT_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300173#define ETHOSU_TA_HISTCNT_1 0
174#endif
175
Jonny Svärdd6670902021-03-18 15:49:27 +0100176static uintptr_t ethosu_ta_base_addrs[ETHOSU_NPU_COUNT][ETHOSU_NPU_TA_COUNT] = {
177 {ETHOSU0_TA0_BASE_ADDRESS, ETHOSU0_TA1_BASE_ADDRESS}};
178struct timing_adapter ethosu_ta[ETHOSU_NPU_COUNT][ETHOSU_NPU_TA_COUNT];
Nir Ekhauza58edd82021-10-04 12:21:17 +0300179struct timing_adapter_settings ethosu_ta_settings[ETHOSU_NPU_TA_COUNT] = {{ETHOSU_TA_MAXR_0,
180 ETHOSU_TA_MAXW_0,
181 ETHOSU_TA_MAXRW_0,
182 ETHOSU_TA_RLATENCY_0,
183 ETHOSU_TA_WLATENCY_0,
184 ETHOSU_TA_PULSE_ON_0,
185 ETHOSU_TA_PULSE_OFF_0,
186 ETHOSU_TA_BWCAP_0,
187 ETHOSU_TA_PERFCTRL_0,
188 ETHOSU_TA_PERFCNT_0,
189 ETHOSU_TA_MODE_0,
Kristofer Jonsson29467e02021-11-26 16:10:43 +0100190 0, // Read only register
Nir Ekhauza58edd82021-10-04 12:21:17 +0300191 ETHOSU_TA_HISTBIN_0,
192 ETHOSU_TA_HISTCNT_0},
193 {ETHOSU_TA_MAXR_1,
194 ETHOSU_TA_MAXW_1,
195 ETHOSU_TA_MAXRW_1,
196 ETHOSU_TA_RLATENCY_1,
197 ETHOSU_TA_WLATENCY_1,
198 ETHOSU_TA_PULSE_ON_1,
199 ETHOSU_TA_PULSE_OFF_1,
200 ETHOSU_TA_BWCAP_1,
201 ETHOSU_TA_PERFCTRL_1,
202 ETHOSU_TA_PERFCNT_1,
203 ETHOSU_TA_MODE_1,
Kristofer Jonsson29467e02021-11-26 16:10:43 +0100204 0, // Read only register
Nir Ekhauza58edd82021-10-04 12:21:17 +0300205 ETHOSU_TA_HISTBIN_1,
206 ETHOSU_TA_HISTCNT_1}};
Kristofer Jonsson29467e02021-11-26 16:10:43 +0100207
Kristofer Jonsson7b8fa5e2021-12-16 14:02:56 +0100208#endif
209
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100210/****************************************************************************
211 * Cache maintenance
212 ****************************************************************************/
213
214#if defined(CPU_CACHE_ENABLE) && defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
215extern "C" {
216void ethosu_flush_dcache(uint32_t *p, size_t bytes) {
217 if (p)
218 SCB_CleanDCache_by_Addr(p, bytes);
219 else
220 SCB_CleanDCache();
221}
222
223void ethosu_invalidate_dcache(uint32_t *p, size_t bytes) {
224 if (p)
225 SCB_InvalidateDCache_by_Addr(p, bytes);
226 else
227 SCB_InvalidateDCache();
228}
229}
230#endif
231
232/****************************************************************************
233 * Init
234 ****************************************************************************/
235
236namespace {
237
Per Åstrand174e98d2021-02-09 17:48:53 +0100238extern "C" {
239struct ExcContext {
240 uint32_t r0;
241 uint32_t r1;
242 uint32_t r2;
243 uint32_t r3;
244 uint32_t r12;
245 uint32_t lr;
246 uint32_t pc;
247 uint32_t xPsr;
248};
249
250void HardFault_Handler() {
251 int irq;
252 struct ExcContext *e;
253 uint32_t sp;
254
255 asm volatile("mrs %0, ipsr \n" // Read IPSR (Exceptio number)
256 "sub %0, #16 \n" // Get it into IRQn_Type range
257 "tst lr, #4 \n" // Select the stack which was in use
258 "ite eq \n"
259 "mrseq %1, msp \n"
260 "mrsne %1, psp \n"
261 "mov %2, sp \n"
262 : "=r"(irq), "=r"(e), "=r"(sp));
263
264 printf("Hard fault. irq=%d, pc=0x%08" PRIx32 ", lr=0x%08" PRIx32 ", xpsr=0x%08" PRIx32 ", sp=0x%08" PRIx32 "\n",
265 irq,
266 e->pc,
267 e->lr,
268 e->xPsr,
269 sp);
270 printf(
271 "%11s cfsr=0x%08" PRIx32 " bfar=0x%08" PRIx32 " mmfar=0x%08" PRIx32 "\n", "", SCB->CFSR, SCB->BFAR, SCB->MMFAR);
272 exit(1);
273}
274}
275
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100276#ifdef ETHOSU
277void ethosuIrqHandler() {
Jonny Svärdc5941c42021-06-01 18:40:45 +0200278 ethosu_irq_handler(&ethosu0_driver);
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100279}
280#endif
281
282} // namespace
283
284namespace EthosU {
285
286void targetSetup() {
287 // Initialize UART driver
Yulia Garboviche9cdc632021-11-23 20:00:04 +0200288 UartStdOutInit();
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100289
Kristofer Jonsson7b8fa5e2021-12-16 14:02:56 +0100290#ifdef ETHOSU
Jonny Svärdd6670902021-03-18 15:49:27 +0100291 // Initialize timing adapter(s)
292 for (int i = 0; i < ETHOSU_NPU_COUNT; i++) {
293 for (int j = 0; j < ETHOSU_NPU_TA_COUNT; j++) {
294 if (ta_init(&ethosu_ta[i][j], ethosu_ta_base_addrs[i][j])) {
295 printf("Failed to initialize timing-adapter %d for NPU %d\n", j, i);
Nir Ekhauza58edd82021-10-04 12:21:17 +0300296 } else {
297 // Set the updated configuration
298 ta_set_all(&ethosu_ta[i][j], &ethosu_ta_settings[j]);
Jonny Svärdd6670902021-03-18 15:49:27 +0100299 }
300 }
301 }
302
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100303 // Initialize Ethos-U NPU driver
Jonny Svärdc5941c42021-06-01 18:40:45 +0200304 if (ethosu_init(&ethosu0_driver,
Anton Mobergebe9a152021-05-03 09:28:52 +0200305 reinterpret_cast<void *>(ETHOSU_BASE_ADDRESS),
306 ethosu_scratch,
307 ETHOSU_FAST_MEMORY_SIZE,
308 1,
309 1)) {
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100310 printf("Failed to initialize NPU.\n");
311 return;
312 }
313
Jonny Svärd991af2b2021-04-15 17:31:01 +0200314 // Assumes SCB->VTOR point to RW memory
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100315 NVIC_SetVector(static_cast<IRQn_Type>(ETHOSU_IRQ), (uint32_t)&ethosuIrqHandler);
Jonny Svärd4db21c92023-05-15 11:44:05 +0200316 NVIC_SetPriority(static_cast<IRQn_Type>(ETHOSU_IRQ), ETHOSU_IRQ_PRIORITY);
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100317 NVIC_EnableIRQ(static_cast<IRQn_Type>(ETHOSU_IRQ));
318#endif
Jonny Svärd991af2b2021-04-15 17:31:01 +0200319
320 // MPU setup
321 const std::vector<ARM_MPU_Region_t> mpuConfig = {
322 {
Jonny Svärde3b6b962021-04-27 11:32:31 +0200323 // ITCM (NS)
Jonny Svärd991af2b2021-04-15 17:31:01 +0200324 ARM_MPU_RBAR(0x00000000, // Base
325 ARM_MPU_SH_NON, // Non-shareable
326 1, // Read-Only
327 1, // Non-Privileged
328 0), // eXecute Never disabled
329 ARM_MPU_RLAR(0x0007ffff, // Limit
330 Mpu::WTRA_index) // Attribute index - Write-Through, Read-allocate
331 },
332 {
Jonny Svärde3b6b962021-04-27 11:32:31 +0200333 // ITCM (S)
Jonny Svärd991af2b2021-04-15 17:31:01 +0200334 ARM_MPU_RBAR(0x10000000, // Base
335 ARM_MPU_SH_NON, // Non-shareable
336 1, // Read-Only
337 1, // Non-Privileged
338 0), // eXecute Never disabled
339 ARM_MPU_RLAR(0x1007ffff, // Limit
340 Mpu::WTRA_index) // Attribute index - Write-Through, Read-allocate
341 },
342 {
Jonny Svärde3b6b962021-04-27 11:32:31 +0200343 // FPGA DATA SRAM; BRAM (NS)
344 ARM_MPU_RBAR(0x01000000, // Base
345 ARM_MPU_SH_NON, // Non-shareable
346 0, // Read-Write
347 1, // Non-Privileged
348 0), // eXecute Never disabled
349 ARM_MPU_RLAR(0x011fffff, // Limit
350 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
351 },
352 {
353 // FPGA DATA SRAM; BRAM (S)
Jonny Svärd991af2b2021-04-15 17:31:01 +0200354 ARM_MPU_RBAR(0x11000000, // Base
355 ARM_MPU_SH_NON, // Non-shareable
356 0, // Read-Write
357 1, // Non-Privileged
358 0), // eXecute Never disabled
359 ARM_MPU_RLAR(0x111fffff, // Limit
360 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
361 },
362 {
Jonny Svärde3b6b962021-04-27 11:32:31 +0200363 // DTCM (NS)
364 ARM_MPU_RBAR(0x20000000, // Base
365 ARM_MPU_SH_NON, // Non-shareable
366 0, // Read-Write
367 1, // Non-Privileged
368 1), // eXecute Never enabled
369 ARM_MPU_RLAR(0x2007ffff, // Limit
370 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
371 },
372 {
373 // DTCM (S)
Jonny Svärd991af2b2021-04-15 17:31:01 +0200374 ARM_MPU_RBAR(0x30000000, // Base
375 ARM_MPU_SH_NON, // Non-shareable
376 0, // Read-Write
377 1, // Non-Privileged
378 1), // eXecute Never enabled
379 ARM_MPU_RLAR(0x3007ffff, // Limit
380 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
381 },
382 {
Jonny Svärde3b6b962021-04-27 11:32:31 +0200383 // SSE-300 internal SRAM (NS)
384 ARM_MPU_RBAR(0x21000000, // Base
385 ARM_MPU_SH_NON, // Non-shareable
386 0, // Read-Write
387 1, // Non-Privileged
388 1), // eXecute Never enabled
389 ARM_MPU_RLAR(0x213fffff, // Limit
390 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
391 },
392 {
393 // SSE-300 internal SRAM (S)
Jonny Svärd991af2b2021-04-15 17:31:01 +0200394 ARM_MPU_RBAR(0x31000000, // Base
395 ARM_MPU_SH_NON, // Non-shareable
396 0, // Read-Write
397 1, // Non-Privileged
398 1), // eXecute Never enabled
399 ARM_MPU_RLAR(0x313fffff, // Limit
400 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
401 },
402 {
Jonny Svärde3b6b962021-04-27 11:32:31 +0200403 // DDR (NS)
404 ARM_MPU_RBAR(0x60000000, // Base
405 ARM_MPU_SH_NON, // Non-shareable
406 0, // Read-Write
407 1, // Non-Privileged
408 1), // eXecute Never enabled
409 ARM_MPU_RLAR(0x6fffffff, // Limit
410 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
411 },
412 {
413 // DDR (S)
Jonny Svärd991af2b2021-04-15 17:31:01 +0200414 ARM_MPU_RBAR(0x70000000, // Base
415 ARM_MPU_SH_NON, // Non-shareable
416 0, // Read-Write
417 1, // Non-Privileged
418 1), // eXecute Never enabled
419 ARM_MPU_RLAR(0x7fffffff, // Limit
420 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
421 }};
422
423 // Setup MPU configuration
424 Mpu::loadAndEnableConfig(&mpuConfig[0], mpuConfig.size());
425
426#if defined(CPU_CACHE_ENABLE) && defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
427 SCB_EnableICache();
428 SCB_EnableDCache();
429#endif
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100430}
431
432} // namespace EthosU