alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 | */ |
Kshitij Sisodia | 659fcd9 | 2021-05-19 10:30:06 +0100 | [diff] [blame] | 17 | #include "timer_simple_platform.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 18 | |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 19 | #include "log_macros.h" /* Logging macros. */ |
| 20 | #include "RTE_Components.h" /* CPU definitions and functions. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 21 | |
Kshitij Sisodia | 659fcd9 | 2021-05-19 10:30:06 +0100 | [diff] [blame] | 22 | #include <inttypes.h> |
| 23 | |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame] | 24 | static uint64_t cpu_cycle_count = 0; /* 64-bit cpu cycle counter */ |
| 25 | extern uint32_t SystemCoreClock; /* Expected to come from the cmsis-device lib */ |
| 26 | |
| 27 | /** |
| 28 | * @brief Gets the system tick triggered cycle counter for the CPU. |
| 29 | * @return 64-bit counter value. |
| 30 | **/ |
| 31 | static uint64_t Get_SysTick_Cycle_Count(void); |
| 32 | |
| 33 | /** |
| 34 | * SysTick initialisation |
| 35 | */ |
| 36 | static int Init_SysTick(void); |
| 37 | |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 38 | /** |
| 39 | * @brief Adds one PMU counter to the counters' array |
| 40 | * @param value Value of the counter |
| 41 | * @param name Name for the given counter |
| 42 | * @param unit Unit for the "value" |
| 43 | * @param counters Pointer to the counter struct - the one to be populated. |
| 44 | * @return true if successfully added, false otherwise |
| 45 | */ |
| 46 | static bool add_pmu_counter( |
| 47 | uint64_t value, |
| 48 | const char* name, |
| 49 | const char* unit, |
| 50 | pmu_counters* counters); |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame] | 51 | |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 52 | void platform_reset_counters(void) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 53 | { |
| 54 | if (0 != Init_SysTick()) { |
| 55 | printf_err("Failed to initialise system tick config\n"); |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 56 | return; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 57 | } |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 58 | |
| 59 | #if defined(ARM_NPU) |
| 60 | ethosu_pmu_init(); |
| 61 | #endif /* defined (ARM_NPU) */ |
| 62 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 63 | debug("system tick config ready\n"); |
| 64 | } |
| 65 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 66 | void platform_get_counters(pmu_counters* counters) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 67 | { |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 68 | counters->num_counters = 0; |
| 69 | counters->initialised = true; |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 70 | uint32_t i = 0; |
| 71 | |
| 72 | #if defined (ARM_NPU) |
| 73 | ethosu_pmu_counters npu_counters = ethosu_get_pmu_counters(); |
| 74 | for (i = 0; i < ETHOSU_PMU_NCOUNTERS; ++i) { |
| 75 | add_pmu_counter( |
| 76 | npu_counters.npu_evt_counters[i].counter_value, |
| 77 | npu_counters.npu_evt_counters[i].name, |
| 78 | npu_counters.npu_evt_counters[i].unit, |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 79 | counters); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 80 | } |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 81 | for (i = 0; i < ETHOSU_DERIVED_NCOUNTERS; ++i) { |
| 82 | add_pmu_counter( |
| 83 | npu_counters.npu_derived_counters[i].counter_value, |
| 84 | npu_counters.npu_derived_counters[i].name, |
| 85 | npu_counters.npu_derived_counters[i].unit, |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 86 | counters); |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 87 | } |
| 88 | add_pmu_counter( |
| 89 | npu_counters.npu_total_ccnt, |
| 90 | "NPU TOTAL", |
| 91 | "cycles", |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 92 | counters); |
Kshitij Sisodia | ea8ce56 | 2022-04-12 11:10:11 +0100 | [diff] [blame^] | 93 | #else /* defined (ARM_NPU) */ |
| 94 | UNUSED(i); |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 95 | #endif /* defined (ARM_NPU) */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 96 | |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 97 | #if defined(CPU_PROFILE_ENABLED) |
| 98 | add_pmu_counter( |
| 99 | Get_SysTick_Cycle_Count(), |
| 100 | "CPU TOTAL", |
| 101 | "cycles", |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 102 | counters); |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 103 | #endif /* defined(CPU_PROFILE_ENABLED) */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 104 | |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 105 | #if !defined(CPU_PROFILE_ENABLED) |
| 106 | UNUSED(Get_SysTick_Cycle_Count); |
| 107 | #if !defined(ARM_NPU) |
| 108 | UNUSED(add_pmu_counter); |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 109 | #endif /* !defined(ARM_NPU) */ |
| 110 | #endif /* !defined(CPU_PROFILE_ENABLED) */ |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 111 | } |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame] | 112 | |
| 113 | void SysTick_Handler(void) |
| 114 | { |
| 115 | /* Increment the cycle counter based on load value. */ |
| 116 | cpu_cycle_count += SysTick->LOAD + 1; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Gets the current SysTick derived counter value |
| 121 | */ |
| 122 | static uint64_t Get_SysTick_Cycle_Count(void) |
| 123 | { |
| 124 | uint32_t systick_val; |
| 125 | |
| 126 | NVIC_DisableIRQ(SysTick_IRQn); |
| 127 | systick_val = SysTick->VAL & SysTick_VAL_CURRENT_Msk; |
| 128 | NVIC_EnableIRQ(SysTick_IRQn); |
| 129 | |
| 130 | return cpu_cycle_count + (SysTick->LOAD - systick_val); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * SysTick initialisation |
| 135 | */ |
| 136 | static int Init_SysTick(void) |
| 137 | { |
| 138 | const uint32_t ticks_10ms = SystemCoreClock/100 + 1; |
| 139 | int err = 0; |
| 140 | |
| 141 | /* Reset CPU cycle count value. */ |
| 142 | cpu_cycle_count = 0; |
| 143 | |
| 144 | /* Changing configuration for sys tick => guard from being |
| 145 | * interrupted. */ |
| 146 | NVIC_DisableIRQ(SysTick_IRQn); |
| 147 | |
| 148 | /* SysTick init - this will enable interrupt too. */ |
| 149 | err = SysTick_Config(ticks_10ms); |
| 150 | |
| 151 | /* Enable interrupt again. */ |
| 152 | NVIC_EnableIRQ(SysTick_IRQn); |
| 153 | |
| 154 | /* Wait for SysTick to kick off */ |
| 155 | while (!err && !SysTick->VAL) { |
| 156 | __NOP(); |
| 157 | } |
| 158 | |
| 159 | return err; |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static bool add_pmu_counter(uint64_t value, |
| 163 | const char* name, |
| 164 | const char* unit, |
| 165 | pmu_counters* counters) |
| 166 | { |
| 167 | const uint32_t idx = counters->num_counters; |
| 168 | if (idx < NUM_PMU_COUNTERS) { |
| 169 | counters->counters[idx].value = value; |
| 170 | counters->counters[idx].name = name; |
| 171 | counters->counters[idx].unit = unit; |
| 172 | ++counters->num_counters; |
| 173 | return true; |
| 174 | } |
| 175 | printf_err("Failed to add PMU counter!\n"); |
| 176 | return false; |
| 177 | } |