- 
                Notifications
    You must be signed in to change notification settings 
- Fork 8.2k
Adding Arm Ethos-U NPU support #42580
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
      
      
            carlescufi
  merged 5 commits into
  zephyrproject-rtos:main
from
kristofer-jonsson-arm:main
  
      
      
   
  Nov 15, 2022 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      422e9b6
              
                dts: add Arm Ethos-U bindings
              
              
                kristofer-jonsson-arm 117b194
              
                boards: mps3_an547: Add entry for Ethos-U
              
              
                kristofer-jonsson-arm d7d2c41
              
                modules: add an Arm Ethos-U HAL module
              
              
                kristofer-jonsson-arm 3fa3ead
              
                drivers: misc: Add an Ethos-U NPU driver
              
              
                kristofer-jonsson-arm d66442d
              
                samples: Add a TFLU Ethos-U sample program
              
              
                kristofer-jonsson-arm File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| /* | ||
| * Copyright (c) 2018-2021 Linaro Limited | ||
| * Copyright 2022 Arm Limited and/or its affiliates <[email protected]> | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|  | @@ -85,6 +86,21 @@ | |
| }; | ||
| }; | ||
|  | ||
| ethosu { | ||
| #address-cells = <1>; | ||
| #size-cells = <0>; | ||
| interrupt-parent = <&nvic>; | ||
|  | ||
| ethosu0: ethosu@48102000 { | ||
| compatible = "arm,ethos-u"; | ||
| reg = <0x48102000>; | ||
| interrupts = <56 3>; | ||
| secure-enable; | ||
| privilege-enable; | ||
| status = "okay"; | ||
| }; | ||
| }; | ||
|  | ||
| cpus { | ||
| #address-cells = <1>; | ||
| #size-cells = <0>; | ||
|  | @@ -132,6 +148,7 @@ | |
| /* DDR4 - 2G, alternates non-secure/secure every 256M */ | ||
| ddr4: memory@60000000 { | ||
| device_type = "memory"; | ||
|         
                  carlescufi marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| compatible = "zephyr,memory-region"; | ||
| reg = <0x60000000 DT_SIZE_M(256) | ||
| 0x70000000 DT_SIZE_M(256) | ||
| 0x80000000 DT_SIZE_M(256) | ||
|  | @@ -140,6 +157,7 @@ | |
| 0xb0000000 DT_SIZE_M(256) | ||
| 0xc0000000 DT_SIZE_M(256) | ||
| 0xd0000000 DT_SIZE_M(256)>; | ||
| zephyr,memory-region = "DDR4"; | ||
| }; | ||
|  | ||
| soc { | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|  | ||
| add_subdirectory_ifdef(CONFIG_ARM_ETHOS_U ethos_u) | ||
| add_subdirectory_ifdef(CONFIG_FT800 ft8xx) | ||
| add_subdirectory_ifdef(CONFIG_GROVE_LCD_RGB grove_lcd_rgb) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Copyright 2022 Arm Limited and/or its affiliates <[email protected]> | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|  | ||
| zephyr_library() | ||
|         
                  carlescufi marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| zephyr_library_sources(init.c) | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /* | ||
| * Copyright 2021-2022 Arm Limited and/or its affiliates <[email protected]> | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|  | ||
| #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> | ||
|         
                  carlescufi marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| #include <ethosu_driver.h> | ||
|  | ||
| #include <zephyr/logging/log.h> | ||
| LOG_MODULE_REGISTER(ethos_u, CONFIG_ARM_ETHOS_U_LOG_LEVEL); | ||
|  | ||
| #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) | ||
| { | ||
| int status; | ||
|  | ||
| status = k_sem_take((struct k_sem *)sem, K_FOREVER); | ||
| if (status != 0) { | ||
| 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; | ||
| }; | ||
|  | ||
| static int ethosu_zephyr_init(const struct device *dev) | ||
| { | ||
| const struct ethosu_dts_info *config = dev->config; | ||
| struct ethosu_data *data = dev->data; | ||
| struct ethosu_driver *drv = &data->drv; | ||
| struct ethosu_driver_version version; | ||
|  | ||
| LOG_DBG("Ethos-U DTS info. base_address=0x%p, secure_enable=%u, privilege_enable=%u", | ||
| config->base_addr, config->secure_enable, config->privilege_enable); | ||
|  | ||
| ethosu_get_driver_version(&version); | ||
|  | ||
| LOG_DBG("Version. major=%u, minor=%u, patch=%u", version.major, version.minor, | ||
| version.patch); | ||
|  | ||
| if (ethosu_init(drv, config->base_addr, NULL, 0, config->secure_enable, | ||
| config->privilege_enable)) { | ||
| LOG_ERR("Failed to initialize NPU with ethosu_init()."); | ||
| return -EINVAL; | ||
| } | ||
|  | ||
| config->irq_config(); | ||
|  | ||
| return 0; | ||
| } | ||
|  | ||
| #define ETHOSU_DEVICE_INIT(n) \ | ||
| static struct ethosu_data ethosu_data_##n; \ | ||
| \ | ||
| static void ethosu_zephyr_irq_handler_##n(void) \ | ||
| { \ | ||
| struct ethosu_driver *drv = ðosu_data_##n.drv; \ | ||
| ethosu_irq_handler(drv); \ | ||
| } \ | ||
| \ | ||
| static void ethosu_zephyr_irq_config_##n(void) \ | ||
| { \ | ||
| IRQ_DIRECT_CONNECT(DT_INST_IRQN(n), \ | ||
|         
                  carlescufi marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| DT_INST_IRQ(n, priority), \ | ||
| ethosu_zephyr_irq_handler_##n, 0); \ | ||
| irq_enable(DT_INST_IRQN(n)); \ | ||
| } \ | ||
| \ | ||
| static const struct ethosu_dts_info ethosu_dts_info_##n = { \ | ||
| .base_addr = (void *)DT_INST_REG_ADDR(n), \ | ||
| .secure_enable = DT_INST_PROP(n, secure_enable), \ | ||
| .privilege_enable = DT_INST_PROP(n, privilege_enable), \ | ||
| .irq_config = ðosu_zephyr_irq_config_##n, \ | ||
| }; \ | ||
| \ | ||
| DEVICE_DT_INST_DEFINE(n, ethosu_zephyr_init, NULL, ðosu_data_##n, ðosu_dts_info_##n, \ | ||
| POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL); | ||
|  | ||
| DT_INST_FOREACH_STATUS_OKAY(ETHOSU_DEVICE_INIT); | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Copyright 2022 Arm Limited and/or its affiliates <[email protected]> | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|  | ||
| description: | | ||
| The Arm Ethos-U is a micro NPU that enables neural networks to be hardware | ||
| accelerated on embedded devices. The Ethos-U NPU driver is provided as a | ||
| Zephyr module that is hooked up into the Tensorflow Lite Micro framework | ||
| as a 'TFLM custom operation'. | ||
|  | ||
| This device tree entry defines board specific properties like the register | ||
| map address and interrupt line, and is therefore expected to be included | ||
| into the device specific device tree file. | ||
|  | ||
| compatible: "arm,ethos-u" | ||
|         
                  mbolivar-nordic marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| include: base.yaml | ||
|  | ||
| properties: | ||
| reg: | ||
| required: true | ||
|  | ||
| interrupts: | ||
| required: true | ||
|  | ||
| secure-enable: | ||
| type: boolean | ||
| description: Configure Ethos-U NPU to operate in secure- or non-secure mode | ||
|  | ||
| privilege-enable: | ||
| type: boolean | ||
| description: Configure Ethos-U NPU to operate in privileged- or non-privileged mode | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Copyright 2022 Arm Limited and/or its affiliates <[email protected]> | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|  | ||
| if(CONFIG_ARM_ETHOS_U AND CONFIG_MULTITHREADING) | ||
| set(ETHOSU_TARGET_NPU_CONFIG ${CONFIG_ARM_ETHOS_U_NPU_NAME}) | ||
|  | ||
| # 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}" LESS_EQUAL "${ETHOSU_LOG_SEVERITY_MAP_LENGTH}") | ||
| list(GET ETHOSU_LOG_SEVERITY_MAP ${CONFIG_ARM_ETHOS_U_LOG_LEVEL} ETHOSU_LOG_SEVERITY) | ||
| else() | ||
| set(ETHOSU_LOG_SEVERITY debug) | ||
| endif() | ||
|  | ||
| add_subdirectory(${ZEPHYR_CURRENT_MODULE_DIR} ethos-u-core-driver) | ||
|  | ||
| target_link_libraries(ethosu_core_driver PUBLIC | ||
| zephyr_interface) | ||
|  | ||
| zephyr_link_libraries(ethosu_core_driver) | ||
| endif() | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Copyright 2021-2022 Arm Limited and/or its affiliates <[email protected]> | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|  | ||
| config ARM_ETHOS_U | ||
| bool "Ethos-U core driver" | ||
| default n | ||
| depends on MULTITHREADING | ||
| help | ||
| This option enables the Arm Ethos-U core driver. | ||
|  | ||
| menu "Arm Ethos-U NPU configuration" | ||
| choice ARM_ETHOS_U_NPU_CONFIG | ||
| prompt "Arm Ethos-U NPU configuration" | ||
| depends on ARM_ETHOS_U | ||
| default ARM_ETHOS_U55_128 | ||
| config ARM_ETHOS_U55_64 | ||
| bool "using Ethos-U55 with 64 macs" | ||
| config ARM_ETHOS_U55_128 | ||
| bool "using Ethos-U55 with 128 macs" | ||
| config ARM_ETHOS_U55_256 | ||
| bool "using Ethos-U55 with 256 macs" | ||
| config ARM_ETHOS_U65_128 | ||
| bool "using Ethos-U65 with 128 macs" | ||
| config ARM_ETHOS_U65_256 | ||
| bool "using Ethos-U65 with 256 macs" | ||
| config ARM_ETHOS_U65_512 | ||
| bool "using Ethos-U65 with 512 macs" | ||
| endchoice | ||
| endmenu | ||
|  | ||
| config ARM_ETHOS_U_NPU_NAME | ||
| string | ||
| default "ethos-u55-64" if ARM_ETHOS_U55_64 | ||
| default "ethos-u55-128" if ARM_ETHOS_U55_128 | ||
| default "ethos-u55-256" if ARM_ETHOS_U55_256 | ||
| default "ethos-u65-128" if ARM_ETHOS_U65_128 | ||
| default "ethos-u65-256" if ARM_ETHOS_U65_256 | ||
| default "ethos-u65-512" if ARM_ETHOS_U65_512 | ||
| help | ||
| Name of the used Arm NPU | ||
|  | ||
| module = ARM_ETHOS_U | ||
| module-str = arm_ethos_u | ||
| source "subsys/logging/Kconfig.template.log_config" | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Copyright 2021-2022 Arm Limited and/or its affiliates <[email protected]> | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|  | ||
| cmake_minimum_required(VERSION 3.20.0) | ||
|  | ||
| find_package(Zephyr HINTS $ENV{ZEPHYR_BASE}) | ||
|  | ||
| project(tflm_ethosu_app) | ||
|  | ||
| target_include_directories(app PRIVATE src/models/keyword_spotting_cnn_small_int8) | ||
|  | ||
| target_sources(app PRIVATE src/main.cpp src/inference_process.cpp) | ||
|  | ||
| zephyr_linker_sources(SECTIONS linker.ld) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # Copyright 2022 Arm Limited and/or its affiliates <[email protected]> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|  | ||
| config TFLM_ETHOSU_TAINT_BLOBS | ||
| bool | ||
| default y | ||
| select TAINT_BLOBS | ||
|  | ||
| source "Kconfig.zephyr" | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.