blob: 94af3081cf06f42f761824535fb62bda03af74df [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 Sisodiada2ec062022-04-01 14:43:53 +010066pmu_counters platform_get_counters(void)
alexander3c798932021-03-26 21:42:19 +000067{
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010068 pmu_counters platform_counters = {
69 .num_counters = 0,
70 .initialised = true
71 };
72 uint32_t i = 0;
73
74#if defined (ARM_NPU)
75 ethosu_pmu_counters npu_counters = ethosu_get_pmu_counters();
76 for (i = 0; i < ETHOSU_PMU_NCOUNTERS; ++i) {
77 add_pmu_counter(
78 npu_counters.npu_evt_counters[i].counter_value,
79 npu_counters.npu_evt_counters[i].name,
80 npu_counters.npu_evt_counters[i].unit,
81 &platform_counters);
alexander3c798932021-03-26 21:42:19 +000082 }
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010083 for (i = 0; i < ETHOSU_DERIVED_NCOUNTERS; ++i) {
84 add_pmu_counter(
85 npu_counters.npu_derived_counters[i].counter_value,
86 npu_counters.npu_derived_counters[i].name,
87 npu_counters.npu_derived_counters[i].unit,
88 &platform_counters);
89 }
90 add_pmu_counter(
91 npu_counters.npu_total_ccnt,
92 "NPU TOTAL",
93 "cycles",
94 &platform_counters);
95#endif /* defined (ARM_NPU) */
alexander3c798932021-03-26 21:42:19 +000096
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010097#if defined(CPU_PROFILE_ENABLED)
98 add_pmu_counter(
99 Get_SysTick_Cycle_Count(),
100 "CPU TOTAL",
101 "cycles",
102 &platform_counters);
103#endif /* defined(CPU_PROFILE_ENABLED) */
alexander3c798932021-03-26 21:42:19 +0000104
Kshitij Sisodiada2ec062022-04-01 14:43:53 +0100105#if !defined(CPU_PROFILE_ENABLED)
106 UNUSED(Get_SysTick_Cycle_Count);
107#if !defined(ARM_NPU)
108 UNUSED(add_pmu_counter);
109 UNUSED(i);
110#endif /* !defined(ARM_NPU) */
111#endif /* !defined(CPU_PROFILE_ENABLED) */
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +0000112
Kshitij Sisodiada2ec062022-04-01 14:43:53 +0100113 return platform_counters;
114}
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +0000115
116void SysTick_Handler(void)
117{
118 /* Increment the cycle counter based on load value. */
119 cpu_cycle_count += SysTick->LOAD + 1;
120}
121
122/**
123 * Gets the current SysTick derived counter value
124 */
125static uint64_t Get_SysTick_Cycle_Count(void)
126{
127 uint32_t systick_val;
128
129 NVIC_DisableIRQ(SysTick_IRQn);
130 systick_val = SysTick->VAL & SysTick_VAL_CURRENT_Msk;
131 NVIC_EnableIRQ(SysTick_IRQn);
132
133 return cpu_cycle_count + (SysTick->LOAD - systick_val);
134}
135
136/**
137 * SysTick initialisation
138 */
139static int Init_SysTick(void)
140{
141 const uint32_t ticks_10ms = SystemCoreClock/100 + 1;
142 int err = 0;
143
144 /* Reset CPU cycle count value. */
145 cpu_cycle_count = 0;
146
147 /* Changing configuration for sys tick => guard from being
148 * interrupted. */
149 NVIC_DisableIRQ(SysTick_IRQn);
150
151 /* SysTick init - this will enable interrupt too. */
152 err = SysTick_Config(ticks_10ms);
153
154 /* Enable interrupt again. */
155 NVIC_EnableIRQ(SysTick_IRQn);
156
157 /* Wait for SysTick to kick off */
158 while (!err && !SysTick->VAL) {
159 __NOP();
160 }
161
162 return err;
Kshitij Sisodiada2ec062022-04-01 14:43:53 +0100163}
164
165static bool add_pmu_counter(uint64_t value,
166 const char* name,
167 const char* unit,
168 pmu_counters* counters)
169{
170 const uint32_t idx = counters->num_counters;
171 if (idx < NUM_PMU_COUNTERS) {
172 counters->counters[idx].value = value;
173 counters->counters[idx].name = name;
174 counters->counters[idx].unit = unit;
175 ++counters->num_counters;
176 return true;
177 }
178 printf_err("Failed to add PMU counter!\n");
179 return false;
180}