blob: d78ac95dd911b342b3c8097c380f22e0393ea1ff [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +00002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000017#include "hal.h" /* API */
alexander3c798932021-03-26 21:42:19 +000018
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000019#include "hal_config.h" /* HAL configuration */
20#include "platform_drivers.h" /* Platform drivers */
21#include "log_macros.h" /* Logging macros */
alexander3c798932021-03-26 21:42:19 +000022
23#include <stdio.h>
24#include <assert.h>
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000025#include <string.h>
alexander3c798932021-03-26 21:42:19 +000026
27#if defined(ARM_NPU)
28
Isabella Gottardi118f73e2021-09-16 17:54:35 +010029#include "ethosu_mem_config.h" /* Arm Ethos-U memory config */
Cisco Cervelleraf085fa52021-08-02 09:32:07 +010030#include "ethosu_driver.h" /* Arm Ethos-U driver header */
31#include "timing_adapter.h" /* Arm Ethos-U timing adapter driver header */
alexander31ae9f02022-02-10 16:15:54 +000032
33#if defined(TIMING_ADAPTER_AVAILABLE)
Cisco Cervelleraf085fa52021-08-02 09:32:07 +010034#include "timing_adapter_settings.h" /* Arm Ethos-U timing adapter settings */
alexander31ae9f02022-02-10 16:15:54 +000035#endif /* defined(TIMING_ADAPTER_AVAILABLE) */
alexander3c798932021-03-26 21:42:19 +000036
Cisco Cervellera414b1b92021-09-28 18:15:10 +010037struct ethosu_driver ethosu_drv; /* Default Ethos-U device driver */
Kshitij Sisodia659fcd92021-05-19 10:30:06 +010038
Isabella Gottardi118f73e2021-09-16 17:54:35 +010039#if defined(ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0)
40 static uint8_t cache_arena[ETHOS_U_CACHE_BUF_SZ] CACHE_BUF_ATTRIBUTE;
41#else /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */
42 static uint8_t* cache_arena = NULL;
43#endif /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */
44
alexander3c798932021-03-26 21:42:19 +000045/**
Cisco Cervelleraf085fa52021-08-02 09:32:07 +010046 * @brief Initialises the Arm Ethos-U NPU
alexander3c798932021-03-26 21:42:19 +000047 * @return 0 if successful, error code otherwise
48 **/
alexanderc350cdc2021-04-29 20:36:09 +010049static int arm_npu_init(void);
alexander3c798932021-03-26 21:42:19 +000050
Isabella Gottardi118f73e2021-09-16 17:54:35 +010051static uint8_t * get_cache_arena()
52{
53 return cache_arena;
54}
55
56static size_t get_cache_arena_size()
57{
58#if defined(ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0)
59 return sizeof(cache_arena);
60#else /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */
61 return 0;
62#endif /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */
63}
64
alexander3c798932021-03-26 21:42:19 +000065#endif /* ARM_NPU */
66
67int hal_init(hal_platform* platform, data_acq_module* data_acq,
68 data_psn_module* data_psn, platform_timer* timer)
69{
70 assert(platform && data_acq && data_psn);
71
72 platform->data_acq = data_acq;
73 platform->data_psn = data_psn;
74 platform->timer = timer;
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000075 platform->platform_init = platform_init;
76 platform->platform_release = platform_release;
77 platform_name(platform->plat_name, sizeof(platform->plat_name));
alexander3c798932021-03-26 21:42:19 +000078
79 return 0;
80}
81
82/**
83 * @brief Local helper function to clean the slate for current platform.
84 **/
alexanderc350cdc2021-04-29 20:36:09 +010085static void hal_platform_clear(hal_platform* platform)
alexander3c798932021-03-26 21:42:19 +000086{
87 assert(platform);
88 platform->inited = 0;
89}
90
91int hal_platform_init(hal_platform* platform)
92{
93 int state;
94 assert(platform && platform->platform_init);
alexanderc350cdc2021-04-29 20:36:09 +010095 hal_platform_clear(platform);
alexander3c798932021-03-26 21:42:19 +000096
97 /* Initialise platform */
98 if (0 != (state = platform->platform_init())) {
99 printf_err("failed to initialise platform %s\n", platform->plat_name);
100 return state;
101 }
102
103 /* Initialise the data acquisition module */
104 if (0 != (state = data_acq_channel_init(platform->data_acq))) {
105 if (!platform->data_acq->inited) {
106 printf_err("failed to initialise data acq module: %s\n",
107 platform->data_acq->system_name);
108 }
109 hal_platform_release(platform);
110 return state;
111 }
112
113 /* Initialise the presentation module */
114 if (0 != (state = data_psn_system_init(platform->data_psn))) {
115 printf_err("failed to initialise data psn module: %s\n",
116 platform->data_psn->system_name);
117 data_acq_channel_release(platform->data_acq);
118 hal_platform_release(platform);
119 return state;
120 }
121
122#if defined(ARM_NPU)
123
Cisco Cervelleraf085fa52021-08-02 09:32:07 +0100124 /* If Arm Ethos-U NPU is to be used, we initialise it here */
alexanderc350cdc2021-04-29 20:36:09 +0100125 if (0 != (state = arm_npu_init())) {
alexander3c798932021-03-26 21:42:19 +0000126 return state;
127 }
128
129#endif /* ARM_NPU */
130
131 /* followed by the timer module */
132 init_timer(platform->timer);
133
134 info("%s platform initialised\n", platform->plat_name);
135 debug("using %s module for data acquisition\n",
136 platform->data_acq->system_name);
137 debug("using %s module for data presentation\n",
138 platform->data_psn->system_name);
139
140 platform->inited = !state;
141
142 return state;
143}
144
145void hal_platform_release(hal_platform *platform)
146{
147 assert(platform && platform->platform_release);
148 data_acq_channel_release(platform->data_acq);
149 data_psn_system_release(platform->data_psn);
150
alexanderc350cdc2021-04-29 20:36:09 +0100151 hal_platform_clear(platform);
alexander3c798932021-03-26 21:42:19 +0000152 info("releasing platform %s\n", platform->plat_name);
153 platform->platform_release();
154}
155
156#if defined(ARM_NPU)
157/**
158 * @brief Defines the Ethos-U interrupt handler: just a wrapper around the default
159 * implementation.
160 **/
alexanderc350cdc2021-04-29 20:36:09 +0100161static void arm_npu_irq_handler(void)
alexander3c798932021-03-26 21:42:19 +0000162{
163 /* Call the default interrupt handler from the NPU driver */
Kshitij Sisodia659fcd92021-05-19 10:30:06 +0100164 ethosu_irq_handler(&ethosu_drv);
alexander3c798932021-03-26 21:42:19 +0000165}
166
167/**
168 * @brief Initialises the NPU IRQ
169 **/
alexanderc350cdc2021-04-29 20:36:09 +0100170static void arm_npu_irq_init(void)
alexander3c798932021-03-26 21:42:19 +0000171{
172 const IRQn_Type ethosu_irqnum = (IRQn_Type)EthosU_IRQn;
173
174 /* Register the EthosU IRQ handler in our vector table.
175 * Note, this handler comes from the EthosU driver */
alexanderc350cdc2021-04-29 20:36:09 +0100176 NVIC_SetVector(ethosu_irqnum, (uint32_t)arm_npu_irq_handler);
alexander3c798932021-03-26 21:42:19 +0000177
178 /* Enable the IRQ */
179 NVIC_EnableIRQ(ethosu_irqnum);
180
181 debug("EthosU IRQ#: %u, Handler: 0x%p\n",
alexanderc350cdc2021-04-29 20:36:09 +0100182 ethosu_irqnum, arm_npu_irq_handler);
alexander3c798932021-03-26 21:42:19 +0000183}
184
alexander31ae9f02022-02-10 16:15:54 +0000185#if defined(TIMING_ADAPTER_AVAILABLE)
186 static int _arm_npu_timing_adapter_init(void)
187 {
188 #if defined (TA0_BASE)
189 struct timing_adapter ta_0;
190 struct timing_adapter_settings ta_0_settings = {
191 .maxr = TA0_MAXR,
192 .maxw = TA0_MAXW,
193 .maxrw = TA0_MAXRW,
194 .rlatency = TA0_RLATENCY,
195 .wlatency = TA0_WLATENCY,
196 .pulse_on = TA0_PULSE_ON,
197 .pulse_off = TA0_PULSE_OFF,
198 .bwcap = TA0_BWCAP,
199 .perfctrl = TA0_PERFCTRL,
200 .perfcnt = TA0_PERFCNT,
201 .mode = TA0_MODE,
202 .maxpending = 0, /* This is a read-only parameter */
203 .histbin = TA0_HISTBIN,
204 .histcnt = TA0_HISTCNT
205 };
alexander3c798932021-03-26 21:42:19 +0000206
alexander31ae9f02022-02-10 16:15:54 +0000207 if (0 != ta_init(&ta_0, TA0_BASE)) {
208 printf_err("TA0 initialisation failed\n");
209 return 1;
210 }
211
212 ta_set_all(&ta_0, &ta_0_settings);
213 #endif /* defined (TA0_BASE) */
214
215 #if defined (TA1_BASE)
216 struct timing_adapter ta_1;
217 struct timing_adapter_settings ta_1_settings = {
218 .maxr = TA1_MAXR,
219 .maxw = TA1_MAXW,
220 .maxrw = TA1_MAXRW,
221 .rlatency = TA1_RLATENCY,
222 .wlatency = TA1_WLATENCY,
223 .pulse_on = TA1_PULSE_ON,
224 .pulse_off = TA1_PULSE_OFF,
225 .bwcap = TA1_BWCAP,
226 .perfctrl = TA1_PERFCTRL,
227 .perfcnt = TA1_PERFCNT,
228 .mode = TA1_MODE,
229 .maxpending = 0, /* This is a read-only parameter */
230 .histbin = TA1_HISTBIN,
231 .histcnt = TA1_HISTCNT
232 };
233
234 if (0 != ta_init(&ta_1, TA1_BASE)) {
235 printf_err("TA1 initialisation failed\n");
236 return 1;
237 }
238
239 ta_set_all(&ta_1, &ta_1_settings);
240 #endif /* defined (TA1_BASE) */
241
242 return 0;
alexander3c798932021-03-26 21:42:19 +0000243 }
alexander31ae9f02022-02-10 16:15:54 +0000244#endif /* defined(TIMING_ADAPTER_AVAILABLE) */
alexander3c798932021-03-26 21:42:19 +0000245
alexanderc350cdc2021-04-29 20:36:09 +0100246static int arm_npu_init(void)
alexander3c798932021-03-26 21:42:19 +0000247{
248 int err = 0;
249
Cisco Cervelleraf085fa52021-08-02 09:32:07 +0100250 /* If the platform has timing adapter blocks along with Ethos-U core
alexander3c798932021-03-26 21:42:19 +0000251 * block, initialise them here. */
alexander31ae9f02022-02-10 16:15:54 +0000252#if defined(TIMING_ADAPTER_AVAILABLE)
alexander3c798932021-03-26 21:42:19 +0000253 if (0 != (err = _arm_npu_timing_adapter_init())) {
254 return err;
255 }
alexander31ae9f02022-02-10 16:15:54 +0000256#endif /* defined(TIMING_ADAPTER_AVAILABLE) */
alexander3c798932021-03-26 21:42:19 +0000257
258 /* Initialise the IRQ */
alexanderc350cdc2021-04-29 20:36:09 +0100259 arm_npu_irq_init();
alexander3c798932021-03-26 21:42:19 +0000260
Cisco Cervelleraf085fa52021-08-02 09:32:07 +0100261 /* Initialise Ethos-U device */
262 const void * ethosu_base_address = (void *)(SEC_ETHOS_U_NPU_BASE);
alexander3c798932021-03-26 21:42:19 +0000263
Kshitij Sisodia659fcd92021-05-19 10:30:06 +0100264 if (0 != (err = ethosu_init(
Isabella Gottardi118f73e2021-09-16 17:54:35 +0100265 &ethosu_drv, /* Ethos-U driver device pointer */
266 ethosu_base_address, /* Ethos-U NPU's base address. */
267 get_cache_arena(), /* Pointer to fast mem area - NULL for U55. */
268 get_cache_arena_size(), /* Fast mem region size. */
269 1, /* Security enable. */
270 1))) { /* Privilege enable. */
Cisco Cervelleraf085fa52021-08-02 09:32:07 +0100271 printf_err("failed to initalise Ethos-U device\n");
alexander3c798932021-03-26 21:42:19 +0000272 return err;
273 }
274
Cisco Cervelleraf085fa52021-08-02 09:32:07 +0100275 info("Ethos-U device initialised\n");
alexander3c798932021-03-26 21:42:19 +0000276
Cisco Cervelleraf085fa52021-08-02 09:32:07 +0100277 /* Get Ethos-U version */
Cisco Cervellera414b1b92021-09-28 18:15:10 +0100278 struct ethosu_driver_version driver_version;
279 struct ethosu_hw_info hw_info;
alexander31ae9f02022-02-10 16:15:54 +0000280
Cisco Cervellera414b1b92021-09-28 18:15:10 +0100281 ethosu_get_driver_version(&driver_version);
282 ethosu_get_hw_info(&ethosu_drv, &hw_info);
alexander3c798932021-03-26 21:42:19 +0000283
Cisco Cervelleraf085fa52021-08-02 09:32:07 +0100284 info("Ethos-U version info:\n");
alexander31ae9f02022-02-10 16:15:54 +0000285 info("\tArch: v%"PRIu32".%"PRIu32".%"PRIu32"\n",
Cisco Cervellera414b1b92021-09-28 18:15:10 +0100286 hw_info.version.arch_major_rev,
287 hw_info.version.arch_minor_rev,
288 hw_info.version.arch_patch_rev);
alexander31ae9f02022-02-10 16:15:54 +0000289 info("\tDriver: v%"PRIu8".%"PRIu8".%"PRIu8"\n",
Cisco Cervellera414b1b92021-09-28 18:15:10 +0100290 driver_version.major,
291 driver_version.minor,
292 driver_version.patch);
293 info("\tMACs/cc: %"PRIu32"\n", (uint32_t)(1 << hw_info.cfg.macs_per_cc));
294 info("\tCmd stream: v%"PRIu32"\n", hw_info.cfg.cmd_stream_version);
alexander3c798932021-03-26 21:42:19 +0000295
296 return 0;
297}
Isabella Gottardi118f73e2021-09-16 17:54:35 +0100298
alexander3c798932021-03-26 21:42:19 +0000299#endif /* ARM_NPU */