Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 1 | /* |
| 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 Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 20 | #include "RTE_Components.h" /* For CPU related defintiions */ |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 21 | #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 | |
| 26 | struct ethosu_driver ethosu_drv; /* Default Ethos-U device driver */ |
| 27 | |
| 28 | #if defined(ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) |
| 29 | static uint8_t cache_arena[ETHOS_U_CACHE_BUF_SZ] CACHE_BUF_ATTRIBUTE; |
| 30 | #else /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */ |
| 31 | static uint8_t *cache_arena = NULL; |
| 32 | #endif /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */ |
| 33 | |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame] | 34 | static uint8_t *get_cache_arena() |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 35 | { |
| 36 | return cache_arena; |
| 37 | } |
| 38 | |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame] | 39 | static size_t get_cache_arena_size() |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 40 | { |
| 41 | #if defined(ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) |
| 42 | return sizeof(cache_arena); |
| 43 | #else /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */ |
| 44 | return 0; |
| 45 | #endif /* defined (ETHOS_U_CACHE_BUF_SZ) && (ETHOS_U_CACHE_BUF_SZ > 0) */ |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @brief Defines the Ethos-U interrupt handler: just a wrapper around the default |
| 50 | * implementation. |
| 51 | **/ |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame] | 52 | static void arm_ethosu_npu_irq_handler(void) |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 53 | { |
| 54 | /* Call the default interrupt handler from the NPU driver */ |
| 55 | ethosu_irq_handler(ðosu_drv); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @brief Initialises the NPU IRQ |
| 60 | **/ |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame] | 61 | static void arm_ethosu_npu_irq_init(void) |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 62 | { |
Kshitij Sisodia | 8bc863d | 2022-03-24 17:53:34 +0000 | [diff] [blame] | 63 | const IRQn_Type ethosu_irqnum = (IRQn_Type)ETHOS_U_IRQN; |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 64 | |
| 65 | /* Register the EthosU IRQ handler in our vector table. |
| 66 | * Note, this handler comes from the EthosU driver */ |
| 67 | NVIC_SetVector(ethosu_irqnum, (uint32_t)arm_ethosu_npu_irq_handler); |
| 68 | |
| 69 | /* Enable the IRQ */ |
| 70 | NVIC_EnableIRQ(ethosu_irqnum); |
| 71 | |
| 72 | debug("EthosU IRQ#: %u, Handler: 0x%p\n", |
| 73 | ethosu_irqnum, arm_ethosu_npu_irq_handler); |
| 74 | } |
| 75 | |
| 76 | int arm_ethosu_npu_init(void) |
| 77 | { |
| 78 | int err = 0; |
| 79 | |
| 80 | /* Initialise the IRQ */ |
| 81 | arm_ethosu_npu_irq_init(); |
| 82 | |
| 83 | /* Initialise Ethos-U device */ |
Kshitij Sisodia | 8bc863d | 2022-03-24 17:53:34 +0000 | [diff] [blame] | 84 | const void *ethosu_base_address = (void *)(ETHOS_U_BASE_ADDR); |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 85 | |
| 86 | if (0 != (err = ethosu_init( |
| 87 | ðosu_drv, /* Ethos-U driver device pointer */ |
| 88 | ethosu_base_address, /* Ethos-U NPU's base address. */ |
| 89 | get_cache_arena(), /* Pointer to fast mem area - NULL for U55. */ |
| 90 | get_cache_arena_size(), /* Fast mem region size. */ |
Kshitij Sisodia | 8bc863d | 2022-03-24 17:53:34 +0000 | [diff] [blame] | 91 | ETHOS_U_SEC_ENABLED, /* Security enable. */ |
| 92 | ETHOS_U_PRIV_ENABLED))) /* Privilege enable. */ |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 93 | { |
| 94 | printf_err("failed to initialise Ethos-U device\n"); |
| 95 | return err; |
| 96 | } |
| 97 | |
| 98 | info("Ethos-U device initialised\n"); |
| 99 | |
| 100 | /* Get Ethos-U version */ |
| 101 | struct ethosu_driver_version driver_version; |
| 102 | struct ethosu_hw_info hw_info; |
| 103 | |
| 104 | ethosu_get_driver_version(&driver_version); |
| 105 | ethosu_get_hw_info(ðosu_drv, &hw_info); |
| 106 | |
| 107 | info("Ethos-U version info:\n"); |
| 108 | info("\tArch: v%" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n", |
| 109 | hw_info.version.arch_major_rev, |
| 110 | hw_info.version.arch_minor_rev, |
| 111 | hw_info.version.arch_patch_rev); |
| 112 | info("\tDriver: v%" PRIu8 ".%" PRIu8 ".%" PRIu8 "\n", |
| 113 | driver_version.major, |
| 114 | driver_version.minor, |
| 115 | driver_version.patch); |
| 116 | info("\tMACs/cc: %" PRIu32 "\n", (uint32_t)(1 << hw_info.cfg.macs_per_cc)); |
| 117 | info("\tCmd stream: v%" PRIu32 "\n", hw_info.cfg.cmd_stream_version); |
| 118 | |
| 119 | return 0; |
| 120 | } |