1 /*
2 * SPDX-FileCopyrightText: <text>Copyright 2021-2022, 2024 Arm Limited and/or its
3 * affiliates <open-source-office@arm.com></text>
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "zephyr/sys_clock.h"
8 #include <zephyr/device.h>
9 #include <zephyr/devicetree.h>
10 #include <zephyr/init.h>
11 #include <zephyr/kernel.h>
12 #include <zephyr/irq.h>
13 #include <zephyr/sys/util.h>
14
15 #include <ethosu_driver.h>
16
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_REGISTER(ethos_u, CONFIG_ARM_ETHOS_U_LOG_LEVEL);
19
20 #define DT_DRV_COMPAT arm_ethos_u
21
22 /*******************************************************************************
23 * Re-implementation/Overrides __((weak)) symbol functions from ethosu_driver.c
24 * To handle mutex and semaphores
25 *******************************************************************************/
26
ethosu_mutex_create(void)27 void *ethosu_mutex_create(void)
28 {
29 struct k_mutex *mutex;
30
31 mutex = k_malloc(sizeof(*mutex));
32 if (mutex == NULL) {
33 LOG_ERR("Failed allocate mutex");
34 return NULL;
35 }
36
37 k_mutex_init(mutex);
38
39 return (void *)mutex;
40 }
41
ethosu_mutex_lock(void * mutex)42 int ethosu_mutex_lock(void *mutex)
43 {
44 int status;
45
46 status = k_mutex_lock((struct k_mutex *)mutex, K_FOREVER);
47 if (status != 0) {
48 LOG_ERR("Failed to lock mutex with error - %d", status);
49 return -1;
50 }
51
52 return 0;
53 }
54
ethosu_mutex_unlock(void * mutex)55 int ethosu_mutex_unlock(void *mutex)
56 {
57 k_mutex_unlock((struct k_mutex *)mutex);
58 return 0;
59 }
60
ethosu_semaphore_create(void)61 void *ethosu_semaphore_create(void)
62 {
63 struct k_sem *sem;
64
65 sem = k_malloc(sizeof(*sem));
66 if (sem == NULL) {
67 LOG_ERR("Failed to allocate semaphore");
68 return NULL;
69 }
70
71 k_sem_init(sem, 0, 100);
72
73 return (void *)sem;
74 }
75
ethosu_semaphore_take(void * sem,uint64_t timeout)76 int ethosu_semaphore_take(void *sem, uint64_t timeout)
77 {
78 int status;
79
80 status = k_sem_take((struct k_sem *)sem, (timeout == ETHOSU_SEMAPHORE_WAIT_FOREVER)
81 ? K_FOREVER
82 : Z_TIMEOUT_TICKS(timeout));
83
84 if (status != 0) {
85 /* The Ethos-U driver expects the semaphore implementation to never fail except for
86 * when a timeout occurs, and the current ethosu_semaphore_take implementation makes
87 * no distinction, in terms of return codes, between a timeout and other semaphore
88 * take failures. Also, note that a timeout is virtually indistinguishable from
89 * other failures if the driver logging is disabled. Handling errors other than a
90 * timeout is therefore not covered here and is deferred to the application
91 * developer if necessary.
92 */
93 if (status != -EAGAIN) {
94 LOG_ERR("Failed to take semaphore with error - %d", status);
95 }
96 return -1;
97 }
98
99 return 0;
100 }
101
ethosu_semaphore_give(void * sem)102 int ethosu_semaphore_give(void *sem)
103 {
104 k_sem_give((struct k_sem *)sem);
105 return 0;
106 }
107
108 struct ethosu_dts_info {
109 void *base_addr;
110 bool secure_enable;
111 bool privilege_enable;
112 void (*irq_config)(void);
113 };
114
115 struct ethosu_data {
116 struct ethosu_driver drv;
117 };
118
ethosu_zephyr_irq_handler(const struct device * dev)119 void ethosu_zephyr_irq_handler(const struct device *dev)
120 {
121 struct ethosu_data *data = dev->data;
122 struct ethosu_driver *drv = &data->drv;
123
124 ethosu_irq_handler(drv);
125 }
126
ethosu_zephyr_init(const struct device * dev)127 static int ethosu_zephyr_init(const struct device *dev)
128 {
129 const struct ethosu_dts_info *config = dev->config;
130 struct ethosu_data *data = dev->data;
131 struct ethosu_driver *drv = &data->drv;
132 struct ethosu_driver_version version;
133
134 LOG_DBG("Ethos-U DTS info. base_address=0x%p, secure_enable=%u, privilege_enable=%u",
135 config->base_addr, config->secure_enable, config->privilege_enable);
136
137 ethosu_get_driver_version(&version);
138
139 LOG_DBG("Version. major=%u, minor=%u, patch=%u", version.major, version.minor,
140 version.patch);
141
142 if (ethosu_init(drv, config->base_addr, NULL, 0, config->secure_enable,
143 config->privilege_enable)) {
144 LOG_ERR("Failed to initialize NPU with ethosu_init().");
145 return -EINVAL;
146 }
147
148 config->irq_config();
149
150 return 0;
151 }
152
153 #define ETHOSU_DEVICE_INIT(n) \
154 static struct ethosu_data ethosu_data_##n; \
155 \
156 static void ethosu_zephyr_irq_config_##n(void) \
157 { \
158 IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), ethosu_zephyr_irq_handler, \
159 DEVICE_DT_INST_GET(n), 0); \
160 irq_enable(DT_INST_IRQN(n)); \
161 } \
162 \
163 static const struct ethosu_dts_info ethosu_dts_info_##n = { \
164 .base_addr = (void *)DT_INST_REG_ADDR(n), \
165 .secure_enable = DT_INST_PROP(n, secure_enable), \
166 .privilege_enable = DT_INST_PROP(n, privilege_enable), \
167 .irq_config = ðosu_zephyr_irq_config_##n, \
168 }; \
169 \
170 DEVICE_DT_INST_DEFINE(n, ethosu_zephyr_init, NULL, ðosu_data_##n, ðosu_dts_info_##n, \
171 POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL);
172
173 DT_INST_FOREACH_STATUS_OKAY(ETHOSU_DEVICE_INIT);
174