blob: ca44c32eb93dca369d71eb1785cdc0198adce5cc [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
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 Sisodiaea8ce562022-04-12 11:10:11 +010093#else /* defined (ARM_NPU) */
94 UNUSED(i);
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010095#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",
Kshitij Sisodia4cc40212022-04-08 09:54:53 +0100102 counters);
Kshitij Sisodiada2ec062022-04-01 14:43:53 +0100103#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);
Kshitij Sisodiada2ec062022-04-01 14:43:53 +0100109#endif /* !defined(ARM_NPU) */
110#endif /* !defined(CPU_PROFILE_ENABLED) */
Kshitij Sisodiada2ec062022-04-01 14:43:53 +0100111}
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +0000112
113void 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 */
122static 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 */
136static 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 Sisodiada2ec062022-04-01 14:43:53 +0100160}
161
162static 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}