blob: 161d613a8df573508a802885b71f793c243b8e51 [file] [log] [blame]
Isabella Gottardiee4920b2022-02-25 14:29:32 +00001/*
2 * Copyright (c) 2022 Arm Limited. All rights reserved.
3 * 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 */
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 "peripheral_memmap.h" /* Peripheral memory map definitions. */
22#include "peripheral_irqs.h" /* IRQ numbers for this platform. */
23#include "log_macros.h" /* Logging functions */
24
25#include "ethosu_mem_config.h" /* Arm Ethos-U memory config */
26#include "ethosu_driver.h" /* Arm Ethos-U driver header */
27
28struct ethosu_driver ethosu_drv; /* Default Ethos-U device driver */
29
30#if defined(ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0)
31static uint8_t cache_arena[ETHOS_U_CACHE_BUF_SZ] CACHE_BUF_ATTRIBUTE;
32#else /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */
33static uint8_t *cache_arena = NULL;
34#endif /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */
35
36uint8_t *get_cache_arena()
37{
38 return cache_arena;
39}
40
41size_t get_cache_arena_size()
42{
43#if defined(ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0)
44 return sizeof(cache_arena);
45#else /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */
46 return 0;
47#endif /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */
48}
49
50/**
51 * @brief Defines the Ethos-U interrupt handler: just a wrapper around the default
52 * implementation.
53 **/
54void arm_ethosu_npu_irq_handler(void)
55{
56 /* Call the default interrupt handler from the NPU driver */
57 ethosu_irq_handler(&ethosu_drv);
58}
59
60/**
61 * @brief Initialises the NPU IRQ
62 **/
63void arm_ethosu_npu_irq_init(void)
64{
65 const IRQn_Type ethosu_irqnum = (IRQn_Type)EthosU_IRQn;
66
67 /* Register the EthosU IRQ handler in our vector table.
68 * Note, this handler comes from the EthosU driver */
69 NVIC_SetVector(ethosu_irqnum, (uint32_t)arm_ethosu_npu_irq_handler);
70
71 /* Enable the IRQ */
72 NVIC_EnableIRQ(ethosu_irqnum);
73
74 debug("EthosU IRQ#: %u, Handler: 0x%p\n",
75 ethosu_irqnum, arm_ethosu_npu_irq_handler);
76}
77
78int arm_ethosu_npu_init(void)
79{
80 int err = 0;
81
82 /* Initialise the IRQ */
83 arm_ethosu_npu_irq_init();
84
85 /* Initialise Ethos-U device */
86 const void *ethosu_base_address = (void *)(SEC_ETHOS_U_NPU_BASE);
87
88 if (0 != (err = ethosu_init(
89 &ethosu_drv, /* Ethos-U driver device pointer */
90 ethosu_base_address, /* Ethos-U NPU's base address. */
91 get_cache_arena(), /* Pointer to fast mem area - NULL for U55. */
92 get_cache_arena_size(), /* Fast mem region size. */
93 1, /* Security enable. */
94 1))) /* Privilege enable. */
95 {
96 printf_err("failed to initialise Ethos-U device\n");
97 return err;
98 }
99
100 info("Ethos-U device initialised\n");
101
102 /* Get Ethos-U version */
103 struct ethosu_driver_version driver_version;
104 struct ethosu_hw_info hw_info;
105
106 ethosu_get_driver_version(&driver_version);
107 ethosu_get_hw_info(&ethosu_drv, &hw_info);
108
109 info("Ethos-U version info:\n");
110 info("\tArch: v%" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n",
111 hw_info.version.arch_major_rev,
112 hw_info.version.arch_minor_rev,
113 hw_info.version.arch_patch_rev);
114 info("\tDriver: v%" PRIu8 ".%" PRIu8 ".%" PRIu8 "\n",
115 driver_version.major,
116 driver_version.minor,
117 driver_version.patch);
118 info("\tMACs/cc: %" PRIu32 "\n", (uint32_t)(1 << hw_info.cfg.macs_per_cc));
119 info("\tCmd stream: v%" PRIu32 "\n", hw_info.cfg.cmd_stream_version);
120
121 return 0;
122}