blob: 3bb91d01d7365bdb67b5a8e9885c81dfe5364198 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +00002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * 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 Sisodia659fcd92021-05-19 10:30:06 +010017#include "timer_simple_platform.h"
alexander3c798932021-03-26 21:42:19 +000018
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010019#include "log_macros.h" /* Logging macros. */
20#include "RTE_Components.h" /* CPU definitions and functions. */
alexander3c798932021-03-26 21:42:19 +000021
Kshitij Sisodia659fcd92021-05-19 10:30:06 +010022#include <inttypes.h>
23
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000024static uint64_t cpu_cycle_count = 0; /* 64-bit cpu cycle counter */
25extern 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 **/
31static uint64_t Get_SysTick_Cycle_Count(void);
32
33/**
34 * SysTick initialisation
35 */
36static int Init_SysTick(void);
37
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010038/**
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 */
46static bool add_pmu_counter(
47 uint64_t value,
48 const char* name,
49 const char* unit,
50 pmu_counters* counters);
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000051
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010052void platform_reset_counters(void)
alexander3c798932021-03-26 21:42:19 +000053{
54 if (0 != Init_SysTick()) {
55 printf_err("Failed to initialise system tick config\n");
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010056 return;
alexander3c798932021-03-26 21:42:19 +000057 }
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010058
59#if defined(ARM_NPU)
60 ethosu_pmu_init();
61#endif /* defined (ARM_NPU) */
62
alexander3c798932021-03-26 21:42:19 +000063 debug("system tick config ready\n");
64}
65
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010066void platform_get_counters(pmu_counters* counters)
alexander3c798932021-03-26 21:42:19 +000067{
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010068 counters->num_counters = 0;
69 counters->initialised = true;
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010070 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 Sisodia4cc40212022-04-08 09:54:53 +010079 counters);
alexander3c798932021-03-26 21:42:19 +000080 }
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010081 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 Sisodia4cc40212022-04-08 09:54:53 +010086 counters);
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010087 }
88 add_pmu_counter(
89 npu_counters.npu_total_ccnt,
90 "NPU TOTAL",
91 "cycles",
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010092 counters);
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010093#endif /* defined (ARM_NPU) */
alexander3c798932021-03-26 21:42:19 +000094
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010095#if defined(CPU_PROFILE_ENABLED)
96 add_pmu_counter(
97 Get_SysTick_Cycle_Count(),
98 "CPU TOTAL",
99 "cycles",
Kshitij Sisodia4cc40212022-04-08 09:54:53 +0100100 counters);
Kshitij Sisodiada2ec062022-04-01 14:43:53 +0100101#endif /* defined(CPU_PROFILE_ENABLED) */
alexander3c798932021-03-26 21:42:19 +0000102
Kshitij Sisodiada2ec062022-04-01 14:43:53 +0100103#if !defined(CPU_PROFILE_ENABLED)
104 UNUSED(Get_SysTick_Cycle_Count);
105#if !defined(ARM_NPU)
106 UNUSED(add_pmu_counter);
107 UNUSED(i);
108#endif /* !defined(ARM_NPU) */
109#endif /* !defined(CPU_PROFILE_ENABLED) */
Kshitij Sisodiada2ec062022-04-01 14:43:53 +0100110}
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +0000111
112void SysTick_Handler(void)
113{
114 /* Increment the cycle counter based on load value. */
115 cpu_cycle_count += SysTick->LOAD + 1;
116}
117
118/**
119 * Gets the current SysTick derived counter value
120 */
121static uint64_t Get_SysTick_Cycle_Count(void)
122{
123 uint32_t systick_val;
124
125 NVIC_DisableIRQ(SysTick_IRQn);
126 systick_val = SysTick->VAL & SysTick_VAL_CURRENT_Msk;
127 NVIC_EnableIRQ(SysTick_IRQn);
128
129 return cpu_cycle_count + (SysTick->LOAD - systick_val);
130}
131
132/**
133 * SysTick initialisation
134 */
135static int Init_SysTick(void)
136{
137 const uint32_t ticks_10ms = SystemCoreClock/100 + 1;
138 int err = 0;
139
140 /* Reset CPU cycle count value. */
141 cpu_cycle_count = 0;
142
143 /* Changing configuration for sys tick => guard from being
144 * interrupted. */
145 NVIC_DisableIRQ(SysTick_IRQn);
146
147 /* SysTick init - this will enable interrupt too. */
148 err = SysTick_Config(ticks_10ms);
149
150 /* Enable interrupt again. */
151 NVIC_EnableIRQ(SysTick_IRQn);
152
153 /* Wait for SysTick to kick off */
154 while (!err && !SysTick->VAL) {
155 __NOP();
156 }
157
158 return err;
Kshitij Sisodiada2ec062022-04-01 14:43:53 +0100159}
160
161static bool add_pmu_counter(uint64_t value,
162 const char* name,
163 const char* unit,
164 pmu_counters* counters)
165{
166 const uint32_t idx = counters->num_counters;
167 if (idx < NUM_PMU_COUNTERS) {
168 counters->counters[idx].value = value;
169 counters->counters[idx].name = name;
170 counters->counters[idx].unit = unit;
171 ++counters->num_counters;
172 return true;
173 }
174 printf_err("Failed to add PMU counter!\n");
175 return false;
176}