blob: b7ed9c7009e697cf6227a9e46422e19b0205a6c9 [file] [log] [blame]
Isabella Gottardiee4920b2022-02-25 14:29:32 +00001/*
Kshitij Sisodia5385a642024-01-17 13:29:43 +00002 * SPDX-FileCopyrightText: Copyright 2022-2024 Arm Limited and/or its affiliates
3 * <open-source-office@arm.com> SPDX-License-Identifier: Apache-2.0
Isabella Gottardiee4920b2022-02-25 14:29:32 +00004 *
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 */
17
18#include "ethosu_npu_init.h"
19
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000020#include "RTE_Components.h" /* For CPU related defintiions */
Isabella Gottardiee4920b2022-02-25 14:29:32 +000021#include "log_macros.h" /* Logging functions */
22
23#include "ethosu_mem_config.h" /* Arm Ethos-U memory config */
24#include "ethosu_driver.h" /* Arm Ethos-U driver header */
25
Kshitij Sisodia5385a642024-01-17 13:29:43 +000026/* Mandatory definition checks. */
27#if !defined(ETHOS_U_BASE_ADDR)
28#error "NPU base address is undefined."
29#endif /* defnied(ETHOS_U_BASE_ADDR) */
30
31#if !defined(ETHOS_U_IRQN)
32#error "Arm NPU interrupt number is undefined."
33#endif /* !defined(ETHOS_U_IRQN) */
34
35#if !defined(ETHOS_U_SEC_ENABLED)
36#error "Arm NPU security mode is undefined."
37#endif /* !defined(ETHOS_U_SEC_ENABLED) */
38
39#if !defined(ETHOS_U_PRIV_ENABLED)
40#error "Arm NPU privilege mode is undefined."
41#endif /* !defined(ETHOS_U_PRIV_ENABLED) */
Isabella Gottardiee4920b2022-02-25 14:29:32 +000042
43#if defined(ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0)
44static uint8_t cache_arena[ETHOS_U_CACHE_BUF_SZ] CACHE_BUF_ATTRIBUTE;
45#else /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */
46static uint8_t *cache_arena = NULL;
47#endif /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */
48
Kshitij Sisodia5385a642024-01-17 13:29:43 +000049struct ethosu_driver ethosu_drv; /* Default Ethos-U device driver */
50
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000051static uint8_t *get_cache_arena()
Isabella Gottardiee4920b2022-02-25 14:29:32 +000052{
53 return cache_arena;
54}
55
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000056static size_t get_cache_arena_size()
Isabella Gottardiee4920b2022-02-25 14:29:32 +000057{
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
65/**
Isabella Gottardiee4920b2022-02-25 14:29:32 +000066 * @brief Initialises the NPU IRQ
67 **/
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000068static void arm_ethosu_npu_irq_init(void)
Isabella Gottardiee4920b2022-02-25 14:29:32 +000069{
Kshitij Sisodia8bc863d2022-03-24 17:53:34 +000070 const IRQn_Type ethosu_irqnum = (IRQn_Type)ETHOS_U_IRQN;
Isabella Gottardiee4920b2022-02-25 14:29:32 +000071
72 /* Register the EthosU IRQ handler in our vector table.
73 * Note, this handler comes from the EthosU driver */
74 NVIC_SetVector(ethosu_irqnum, (uint32_t)arm_ethosu_npu_irq_handler);
75
76 /* Enable the IRQ */
77 NVIC_EnableIRQ(ethosu_irqnum);
78
79 debug("EthosU IRQ#: %u, Handler: 0x%p\n",
80 ethosu_irqnum, arm_ethosu_npu_irq_handler);
81}
82
Kshitij Sisodia1ec169b2022-06-01 09:06:21 +010083/**
84 * @brief Defines the Ethos-U interrupt handler: just a wrapper around the default
85 * implementation.
86 **/
87void arm_ethosu_npu_irq_handler(void)
88{
89 /* Call the default interrupt handler from the NPU driver */
90 ethosu_irq_handler(&ethosu_drv);
91}
92
Isabella Gottardiee4920b2022-02-25 14:29:32 +000093int arm_ethosu_npu_init(void)
94{
95 int err = 0;
96
97 /* Initialise the IRQ */
98 arm_ethosu_npu_irq_init();
99
100 /* Initialise Ethos-U device */
Kshitij Sisodia26bc9232023-03-10 16:33:23 +0000101 void* const ethosu_base_address = (void *)(ETHOS_U_BASE_ADDR);
Kshitij Sisodia5385a642024-01-17 13:29:43 +0000102 info("Initialising Ethos-U device@0x%" PRIx32 "\n", ETHOS_U_BASE_ADDR);
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000103
Kshitij Sisodia5385a642024-01-17 13:29:43 +0000104 if (0 != (err = ethosu_init(&ethosu_drv, /* Ethos-U driver device pointer */
105 ethosu_base_address, /* Ethos-U NPU's base address. */
106 get_cache_arena(), /* Pointer to fast mem area - NULL for U55. */
107 get_cache_arena_size(), /* Fast mem region size. */
108 ETHOS_U_SEC_ENABLED, /* Security enable. */
109 ETHOS_U_PRIV_ENABLED))) /* Privilege enable. */ {
110 printf_err("Failed to initialise Ethos-U device\n");
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000111 return err;
112 }
113
114 info("Ethos-U device initialised\n");
115
116 /* Get Ethos-U version */
117 struct ethosu_driver_version driver_version;
118 struct ethosu_hw_info hw_info;
119
120 ethosu_get_driver_version(&driver_version);
121 ethosu_get_hw_info(&ethosu_drv, &hw_info);
122
123 info("Ethos-U version info:\n");
124 info("\tArch: v%" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n",
125 hw_info.version.arch_major_rev,
126 hw_info.version.arch_minor_rev,
127 hw_info.version.arch_patch_rev);
128 info("\tDriver: v%" PRIu8 ".%" PRIu8 ".%" PRIu8 "\n",
129 driver_version.major,
130 driver_version.minor,
131 driver_version.patch);
132 info("\tMACs/cc: %" PRIu32 "\n", (uint32_t)(1 << hw_info.cfg.macs_per_cc));
133 info("\tCmd stream: v%" PRIu32 "\n", hw_info.cfg.cmd_stream_version);
134
135 return 0;
136}