Skip to content

drivers: misc: ethos_u: Create the ethos_u_common for every vendor #90787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/releases/migration-guide-4.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ Misc
* All memc_flexram_* namespaced things including kconfigs and C API
have been changed to just flexram_*.

* Select ``CONFIG_ETHOS_U`` instead ``CONFIG_ARM_ETHOS_U`` to enable Ethos-U NPU driver.
* Rename all configs that have prefix ``CONFIG_ARM_ETHOS_U_`` to ``CONFIG_ETHOS_U_``.

Bluetooth
*********

Expand Down
2 changes: 1 addition & 1 deletion drivers/misc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: Apache-2.0

add_subdirectory_ifdef(CONFIG_ARM_ETHOS_U ethos_u)
add_subdirectory_ifdef(CONFIG_ETHOS_U ethos_u)
add_subdirectory_ifdef(CONFIG_FT800 ft8xx)
add_subdirectory_ifdef(CONFIG_GROVE_LCD_RGB grove_lcd_rgb)
add_subdirectory_ifdef(CONFIG_PIO_RPI_PICO pio_rpi_pico)
Expand Down
1 change: 1 addition & 0 deletions drivers/misc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

menu "Miscellaneous Drivers"

source "drivers/misc/ethos_u/Kconfig"
source "drivers/misc/ft8xx/Kconfig"
source "drivers/misc/grove_lcd_rgb/Kconfig"
source "drivers/misc/pio_rpi_pico/Kconfig"
Expand Down
3 changes: 2 additions & 1 deletion drivers/misc/ethos_u/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
# SPDX-License-Identifier: Apache-2.0

zephyr_library()
zephyr_library_sources(ethos_u.c)
zephyr_library_sources(ethos_u_common.c)
zephyr_library_sources_ifdef(CONFIG_ETHOS_U_ARM ethos_u_arm.c)
14 changes: 14 additions & 0 deletions drivers/misc/ethos_u/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) 2025 Renesas Electronics Corporation
# SPDX-License-Identifier: Apache-2.0

choice
prompt "Select vendor Ethos-U NPU driver"
depends on ETHOS_U
default ETHOS_U_ARM if DT_HAS_ARM_ETHOS_U_ENABLED

config ETHOS_U_ARM
bool "Arm Ethos-U NPU driver"
help
Enables Arm Ethos-U NPU driver.

endchoice
110 changes: 5 additions & 105 deletions drivers/misc/ethos_u/ethos_u.c → drivers/misc/ethos_u/ethos_u_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,117 +4,17 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include "zephyr/sys_clock.h"
#include <zephyr/irq.h>
#include <zephyr/init.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/irq.h>
#include <zephyr/sys/util.h>

#include <zephyr/logging/log.h>
#include <ethosu_driver.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(ethos_u, CONFIG_ARM_ETHOS_U_LOG_LEVEL);
#include "ethos_u_common.h"

#define DT_DRV_COMPAT arm_ethos_u

/*******************************************************************************
* Re-implementation/Overrides __((weak)) symbol functions from ethosu_driver.c
* To handle mutex and semaphores
*******************************************************************************/

void *ethosu_mutex_create(void)
{
struct k_mutex *mutex;

mutex = k_malloc(sizeof(*mutex));
if (mutex == NULL) {
LOG_ERR("Failed allocate mutex");
return NULL;
}

k_mutex_init(mutex);

return (void *)mutex;
}

int ethosu_mutex_lock(void *mutex)
{
int status;

status = k_mutex_lock((struct k_mutex *)mutex, K_FOREVER);
if (status != 0) {
LOG_ERR("Failed to lock mutex with error - %d", status);
return -1;
}

return 0;
}

int ethosu_mutex_unlock(void *mutex)
{
k_mutex_unlock((struct k_mutex *)mutex);
return 0;
}

void *ethosu_semaphore_create(void)
{
struct k_sem *sem;

sem = k_malloc(sizeof(*sem));
if (sem == NULL) {
LOG_ERR("Failed to allocate semaphore");
return NULL;
}

k_sem_init(sem, 0, 100);

return (void *)sem;
}

int ethosu_semaphore_take(void *sem, uint64_t timeout)
{
int status;

status = k_sem_take((struct k_sem *)sem, (timeout == ETHOSU_SEMAPHORE_WAIT_FOREVER)
? K_FOREVER
: Z_TIMEOUT_TICKS(timeout));

if (status != 0) {
/* The Ethos-U driver expects the semaphore implementation to never fail except for
* when a timeout occurs, and the current ethosu_semaphore_take implementation makes
* no distinction, in terms of return codes, between a timeout and other semaphore
* take failures. Also, note that a timeout is virtually indistinguishable from
* other failures if the driver logging is disabled. Handling errors other than a
* timeout is therefore not covered here and is deferred to the application
* developer if necessary.
*/
if (status != -EAGAIN) {
LOG_ERR("Failed to take semaphore with error - %d", status);
}
return -1;
}

return 0;
}

int ethosu_semaphore_give(void *sem)
{
k_sem_give((struct k_sem *)sem);
return 0;
}

struct ethosu_dts_info {
void *base_addr;
bool secure_enable;
bool privilege_enable;
void (*irq_config)(void);
};

struct ethosu_data {
struct ethosu_driver drv;
};
LOG_MODULE_REGISTER(arm_ethos_u, CONFIG_ETHOS_U_LOG_LEVEL);

void ethosu_zephyr_irq_handler(const struct device *dev)
{
Expand Down
102 changes: 102 additions & 0 deletions drivers/misc/ethos_u/ethos_u_common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* SPDX-FileCopyrightText: <text>Copyright 2021-2022, 2024 Arm Limited and/or its
* affiliates <[email protected]></text>
* SPDX-License-Identifier: Apache-2.0
*/

#include "zephyr/sys_clock.h"
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/util.h>

#include <ethosu_driver.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(ethos_u, CONFIG_ETHOS_U_LOG_LEVEL);

/*******************************************************************************
* Re-implementation/Overrides __((weak)) symbol functions from ethosu_driver.c
* To handle mutex and semaphores
*******************************************************************************/

void *ethosu_mutex_create(void)
{
struct k_mutex *mutex;

mutex = k_malloc(sizeof(*mutex));
if (mutex == NULL) {
LOG_ERR("Failed allocate mutex");
return NULL;
}

k_mutex_init(mutex);

return (void *)mutex;
}

int ethosu_mutex_lock(void *mutex)
{
int status;

status = k_mutex_lock((struct k_mutex *)mutex, K_FOREVER);
if (status != 0) {
LOG_ERR("Failed to lock mutex with error - %d", status);
return -1;
}

return 0;
}

int ethosu_mutex_unlock(void *mutex)
{
k_mutex_unlock((struct k_mutex *)mutex);
return 0;
}

void *ethosu_semaphore_create(void)
{
struct k_sem *sem;

sem = k_malloc(sizeof(*sem));
if (sem == NULL) {
LOG_ERR("Failed to allocate semaphore");
return NULL;
}

k_sem_init(sem, 0, 100);

return (void *)sem;
}

int ethosu_semaphore_take(void *sem, uint64_t timeout)
{
int status;

status = k_sem_take((struct k_sem *)sem, (timeout == ETHOSU_SEMAPHORE_WAIT_FOREVER)
? K_FOREVER
: Z_TIMEOUT_TICKS(timeout));

if (status != 0) {
/* The Ethos-U driver expects the semaphore implementation to never fail except for
* when a timeout occurs, and the current ethosu_semaphore_take implementation makes
* no distinction, in terms of return codes, between a timeout and other semaphore
* take failures. Also, note that a timeout is virtually indistinguishable from
* other failures if the driver logging is disabled. Handling errors other than a
* timeout is therefore not covered here and is deferred to the application
* developer if necessary.
*/
if (status != -EAGAIN) {
LOG_ERR("Failed to take semaphore with error - %d", status);
}
return -1;
}

return 0;
}

int ethosu_semaphore_give(void *sem)
{
k_sem_give((struct k_sem *)sem);
return 0;
}
31 changes: 31 additions & 0 deletions drivers/misc/ethos_u/ethos_u_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* SPDX-FileCopyrightText: <text>Copyright 2021-2022, 2024 Arm Limited and/or its
* affiliates <[email protected]></text>
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_DRIVERS_MISC_ETHOS_U_ETHOS_U_COMMON_H_
#define ZEPHYR_DRIVERS_MISC_ETHOS_U_ETHOS_U_COMMON_H_

#include <ethosu_driver.h>

#ifdef __cplusplus
extern "C" {
#endif

struct ethosu_dts_info {
void *base_addr;
bool secure_enable;
bool privilege_enable;
void (*irq_config)(void);
};

struct ethosu_data {
struct ethosu_driver drv;
};

#ifdef __cplusplus
}
#endif

#endif /* ZEPHYR_DRIVERS_MISC_ETHOS_U_ETHOS_U_COMMON_H_ */
10 changes: 5 additions & 5 deletions modules/hal_ethos_u/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
# affiliates <[email protected]></text>
# SPDX-License-Identifier: Apache-2.0

if(CONFIG_ARM_ETHOS_U AND CONFIG_MULTITHREADING)
if(CONFIG_ETHOS_U AND CONFIG_MULTITHREADING)
# Due to CMP0126 not being NEW, ETHOSU_TARGET_NPU_CONFIG originally
# as directory variable will fail to override that in ethos-u-core-driver
# as cache variable. Fix by passing as cache variable. See:
# https://cmake.org/cmake/help/latest/policy/CMP0126.html#policy:CMP0126
set(ETHOSU_TARGET_NPU_CONFIG ${CONFIG_ARM_ETHOS_U_NPU_NAME} CACHE STRING "NPU configuration")
set(ETHOSU_TARGET_NPU_CONFIG ${CONFIG_ETHOS_U_NPU_NAME} CACHE STRING "NPU configuration")

# Mapping log level from Zephyr (none=0, err=1, wrn=2, inf=3, dbg=4) to
# Ethos-U driver (err=0, warn=1, info=2, debug=3)
set(ETHOSU_LOG_SEVERITY_MAP err err warning info debug)
list(LENGTH ETHOSU_LOG_SEVERITY_MAP ETHOSU_LOG_SEVERITY_MAP_LENGTH)

if (${CONFIG_ARM_ETHOS_U_LOG_LEVEL} EQUAL 0)
if (${CONFIG_ETHOS_U_LOG_LEVEL} EQUAL 0)
# The Ethos-U driver does not have a corresponding "none" log level. Disable logging instead.
set(ETHOSU_LOG_ENABLE OFF CACHE BOOL "")
elseif (${CONFIG_ARM_ETHOS_U_LOG_LEVEL} LESS "${ETHOSU_LOG_SEVERITY_MAP_LENGTH}")
list(GET ETHOSU_LOG_SEVERITY_MAP ${CONFIG_ARM_ETHOS_U_LOG_LEVEL} ETHOSU_LOG_SEVERITY)
elseif (${CONFIG_ETHOS_U_LOG_LEVEL} LESS "${ETHOSU_LOG_SEVERITY_MAP_LENGTH}")
list(GET ETHOSU_LOG_SEVERITY_MAP ${CONFIG_ETHOS_U_LOG_LEVEL} ETHOSU_LOG_SEVERITY)
set(ETHOSU_LOG_SEVERITY ${ETHOSU_LOG_SEVERITY} CACHE STRING "")
else()
set(ETHOSU_LOG_SEVERITY debug CACHE STRING "")
Expand Down
Loading