blob: 8fed8bdf8089e3cfec5e5102ee5929240ebdc5d3 [file] [log] [blame]
Kristofer Jonsson49bdee82020-04-06 13:21:21 +02001/*
2 * Copyright (c) 2019-2020 Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020019#ifndef PMU_ETHOSU_H
20#define PMU_ETHOSU_H
21
Kristofer Jonsson537c71c2020-05-05 14:17:22 +020022#include <stdint.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020028#define ETHOSU_PMU_NCOUNTERS 4
29
Kristofer Jonsson537c71c2020-05-05 14:17:22 +020030typedef volatile struct
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020031{
Kristofer Jonsson537c71c2020-05-05 14:17:22 +020032 uint32_t PMCR;
33 uint32_t PMCNTENSET;
34 uint32_t PMCNTENCLR;
35 uint32_t PMOVSSET;
36 uint32_t PMOVSCLR;
37 uint32_t PMINTSET;
38 uint32_t PMINTCLR;
39 uint64_t PMCCNTR;
40 uint32_t PMCCNTR_CFG;
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020041} PMU_Ethosu_ctrl_Type;
42
43typedef uint32_t PMU_Ethosu_cntr_Type[ETHOSU_PMU_NCOUNTERS];
44typedef uint32_t PMU_Ethosu_evnt_Type[ETHOSU_PMU_NCOUNTERS];
45
46/** \brief HW Supported ETHOSU PMU Events
47 *
48 * Note: These values are symbolic. Actual HW-values may change. I.e. always use API
49 * to set/get actual event-type value.
50 * */
51enum ethosu_pmu_event_type
52{
Diqing Zhong25e2c812020-04-27 13:47:25 +020053 ETHOSU_PMU_NO_EVENT = 0,
54 ETHOSU_PMU_CYCLE,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020055 ETHOSU_PMU_NPU_IDLE,
56 ETHOSU_PMU_MAC_ACTIVE,
57 ETHOSU_PMU_MAC_ACTIVE_8BIT,
58 ETHOSU_PMU_MAC_ACTIVE_16BIT,
59 ETHOSU_PMU_MAC_DPU_ACTIVE,
60 ETHOSU_PMU_MAC_STALLED_BY_WD_ACC,
61 ETHOSU_PMU_MAC_STALLED_BY_WD,
62 ETHOSU_PMU_MAC_STALLED_BY_ACC,
63 ETHOSU_PMU_MAC_STALLED_BY_IB,
Diqing Zhong25e2c812020-04-27 13:47:25 +020064 ETHOSU_PMU_MAC_ACTIVE_32BIT,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020065 ETHOSU_PMU_AO_ACTIVE,
66 ETHOSU_PMU_AO_ACTIVE_8BIT,
67 ETHOSU_PMU_AO_ACTIVE_16BIT,
68 ETHOSU_PMU_AO_STALLED_BY_OFMP_OB,
69 ETHOSU_PMU_AO_STALLED_BY_OFMP,
70 ETHOSU_PMU_AO_STALLED_BY_OB,
71 ETHOSU_PMU_AO_STALLED_BY_ACC_IB,
72 ETHOSU_PMU_AO_STALLED_BY_ACC,
73 ETHOSU_PMU_AO_STALLED_BY_IB,
74 ETHOSU_PMU_WD_ACTIVE,
75 ETHOSU_PMU_WD_STALLED,
76 ETHOSU_PMU_WD_STALLED_BY_WS,
77 ETHOSU_PMU_WD_STALLED_BY_WD_BUF,
78 ETHOSU_PMU_WD_PARSE_ACTIVE,
79 ETHOSU_PMU_WD_PARSE_STALLED,
80 ETHOSU_PMU_WD_PARSE_STALLED_IN,
81 ETHOSU_PMU_WD_PARSE_STALLED_OUT,
Diqing Zhong25e2c812020-04-27 13:47:25 +020082 ETHOSU_PMU_WD_TRANS_WS,
83 ETHOSU_PMU_WD_TRANS_WB,
84 ETHOSU_PMU_WD_TRANS_DW0,
85 ETHOSU_PMU_WD_TRANS_DW1,
Kristofer Jonsson49bdee82020-04-06 13:21:21 +020086 ETHOSU_PMU_AXI0_RD_TRANS_ACCEPTED,
87 ETHOSU_PMU_AXI0_RD_TRANS_COMPLETED,
88 ETHOSU_PMU_AXI0_RD_DATA_BEAT_RECEIVED,
89 ETHOSU_PMU_AXI0_RD_TRAN_REQ_STALLED,
90 ETHOSU_PMU_AXI0_WR_TRANS_ACCEPTED,
91 ETHOSU_PMU_AXI0_WR_TRANS_COMPLETED_M,
92 ETHOSU_PMU_AXI0_WR_TRANS_COMPLETED_S,
93 ETHOSU_PMU_AXI0_WR_DATA_BEAT_WRITTEN,
94 ETHOSU_PMU_AXI0_WR_TRAN_REQ_STALLED,
95 ETHOSU_PMU_AXI0_WR_DATA_BEAT_STALLED,
96 ETHOSU_PMU_AXI0_ENABLED_CYCLES,
97 ETHOSU_PMU_AXI0_RD_STALL_LIMIT,
98 ETHOSU_PMU_AXI0_WR_STALL_LIMIT,
99 ETHOSU_PMU_AXI1_RD_TRANS_ACCEPTED,
100 ETHOSU_PMU_AXI1_RD_TRANS_COMPLETED,
101 ETHOSU_PMU_AXI1_RD_DATA_BEAT_RECEIVED,
102 ETHOSU_PMU_AXI1_RD_TRAN_REQ_STALLED,
103 ETHOSU_PMU_AXI1_WR_TRANS_ACCEPTED,
104 ETHOSU_PMU_AXI1_WR_TRANS_COMPLETED_M,
105 ETHOSU_PMU_AXI1_WR_TRANS_COMPLETED_S,
106 ETHOSU_PMU_AXI1_WR_DATA_BEAT_WRITTEN,
107 ETHOSU_PMU_AXI1_WR_TRAN_REQ_STALLED,
108 ETHOSU_PMU_AXI1_WR_DATA_BEAT_STALLED,
109 ETHOSU_PMU_AXI1_ENABLED_CYCLES,
110 ETHOSU_PMU_AXI1_RD_STALL_LIMIT,
111 ETHOSU_PMU_AXI1_WR_STALL_LIMIT,
112 ETHOSU_PMU_AXI_LATENCY_ANY,
113 ETHOSU_PMU_AXI_LATENCY_32,
114 ETHOSU_PMU_AXI_LATENCY_64,
115 ETHOSU_PMU_AXI_LATENCY_128,
116 ETHOSU_PMU_AXI_LATENCY_256,
117 ETHOSU_PMU_AXI_LATENCY_512,
118 ETHOSU_PMU_AXI_LATENCY_1024,
119
120 ETHOSU_PMU_SENTINEL // End-marker (not event)
121};
122
123extern PMU_Ethosu_ctrl_Type *ethosu_pmu_ctrl;
124extern PMU_Ethosu_cntr_Type *ethosu_pmu_cntr;
125extern PMU_Ethosu_evnt_Type *ethosu_pmu_evnt;
126
127#define ETHOSU_PMU_CTRL_ENABLE_Msk (0x0001)
128#define ETHOSU_PMU_CTRL_EVENTCNT_RESET_Msk (0x0002)
129#define ETHOSU_PMU_CTRL_CYCCNT_RESET_Msk (0x0004)
130#define ETHOSU_PMU_CNT1_Msk (1UL << 0)
131#define ETHOSU_PMU_CNT2_Msk (1UL << 1)
132#define ETHOSU_PMU_CNT3_Msk (1UL << 2)
133#define ETHOSU_PMU_CNT4_Msk (1UL << 3)
134#define ETHOSU_PMU_CCNT_Msk (1UL << 31)
135
136/* Transpose functions between HW-event-type and event-id*/
137enum ethosu_pmu_event_type pmu_event_type(uint32_t);
138uint32_t pmu_event_value(enum ethosu_pmu_event_type);
139
140// CMSIS ref API
141/** \brief PMU Functions */
142
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200143static inline void ETHOSU_PMU_Enable(void);
144static inline void ETHOSU_PMU_Disable(void);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200145
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200146static inline void ETHOSU_PMU_Set_EVTYPER(uint32_t num, enum ethosu_pmu_event_type type);
147static inline enum ethosu_pmu_event_type ETHOSU_PMU_Get_EVTYPER(uint32_t num);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200148
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200149static inline void ETHOSU_PMU_CYCCNT_Reset(void);
150static inline void ETHOSU_PMU_EVCNTR_ALL_Reset(void);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200151
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200152static inline void ETHOSU_PMU_CNTR_Enable(uint32_t mask);
153static inline void ETHOSU_PMU_CNTR_Disable(uint32_t mask);
154static inline uint32_t ETHOSU_PMU_CNTR_Status();
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200155
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200156static inline uint64_t ETHOSU_PMU_Get_CCNTR(void);
157static inline void ETHOSU_PMU_Set_CCNTR(uint64_t val);
158static inline uint32_t ETHOSU_PMU_Get_EVCNTR(uint32_t num);
159static inline void ETHOSU_PMU_Set_EVCNTR(uint32_t num, uint32_t val);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200160
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200161static inline uint32_t ETHOSU_PMU_Get_CNTR_OVS(void);
162static inline void ETHOSU_PMU_Set_CNTR_OVS(uint32_t mask);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200163
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200164static inline void ETHOSU_PMU_Set_CNTR_IRQ_Enable(uint32_t mask);
165static inline void ETHOSU_PMU_Set_CNTR_IRQ_Disable(uint32_t mask);
166static inline uint32_t ETHOSU_PMU_Get_IRQ_Enable();
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200167
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200168static inline void ETHOSU_PMU_CNTR_Increment(uint32_t mask);
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200169
170/**
171 \brief Enable the PMU
172*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200173static inline void ETHOSU_PMU_Enable(void)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200174{
175 ethosu_pmu_ctrl->PMCR |= ETHOSU_PMU_CTRL_ENABLE_Msk;
176}
177
178/**
179 \brief Disable the PMU
180*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200181static inline void ETHOSU_PMU_Disable(void)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200182{
183 ethosu_pmu_ctrl->PMCR &= ~ETHOSU_PMU_CTRL_ENABLE_Msk;
184}
185
186/**
187 \brief Set event to count for PMU eventer counter
188 \param [in] num Event counter (0-ETHOSU_PMU_NCOUNTERS) to configure
189 \param [in] type Event to count
190*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200191static inline void ETHOSU_PMU_Set_EVTYPER(uint32_t num, enum ethosu_pmu_event_type type)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200192{
193 (*ethosu_pmu_evnt)[num] = pmu_event_value(type);
194}
195
196/**
197 \brief Get event to count for PMU eventer counter
198 \param [in] num Event counter (0-ETHOSU_PMU_NCOUNTERS) to configure
199 \return type Event to count
200*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200201static inline enum ethosu_pmu_event_type ETHOSU_PMU_Get_EVTYPER(uint32_t num)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200202{
203 return pmu_event_type((*ethosu_pmu_evnt)[num]);
204}
205
206/**
207 \brief Reset cycle counter
208*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200209static inline void ETHOSU_PMU_CYCCNT_Reset(void)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200210{
211 ethosu_pmu_ctrl->PMCR |= ETHOSU_PMU_CTRL_CYCCNT_RESET_Msk;
212}
213
214/**
215 \brief Reset all event counters
216*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200217static inline void ETHOSU_PMU_EVCNTR_ALL_Reset(void)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200218{
219 ethosu_pmu_ctrl->PMCR |= ETHOSU_PMU_CTRL_EVENTCNT_RESET_Msk;
220}
221
222/**
223 \brief Enable counters
224 \param [in] mask Counters to enable
225 \note Enables one or more of the following:
226 - event counters (bit 0-ETHOSU_PMU_NCOUNTERS)
227 - cycle counter (bit 31)
228*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200229static inline void ETHOSU_PMU_CNTR_Enable(uint32_t mask)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200230{
231 ethosu_pmu_ctrl->PMCNTENSET = mask;
232}
233
234/**
235 \brief Disable counters
236 \param [in] mask Counters to disable
237 \note Disables one or more of the following:
238 - event counters (bit 0-ETHOSU_PMU_NCOUNTERS)
239 - cycle counter (bit 31)
240*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200241static inline void ETHOSU_PMU_CNTR_Disable(uint32_t mask)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200242{
243 ethosu_pmu_ctrl->PMCNTENCLR = mask;
244}
245
246/**
247 \brief Determine counters activation
248
249 \return Event count
250 \param [in] mask Counters to enable
251 \return a bitmask where bit-set means:
252 - event counters activated (bit 0-ETHOSU_PMU_NCOUNTERS)
253 - cycle counter activate (bit 31)
254 \note ETHOSU specific. Usage breaks CMSIS complience
255*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200256static inline uint32_t ETHOSU_PMU_CNTR_Status()
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200257{
258 return ethosu_pmu_ctrl->PMCNTENSET;
259}
260
261/**
262 \brief Read cycle counter (64 bit)
263 \return Cycle count
264 \note Two HW 32-bit registers that can increment independently in-between reads.
265 To work-around raciness yet still avoid turning
266 off the event both are read as one value twice. If the latter read
267 is not greater than the former, it means overflow of LSW without
268 incrementing MSW has occurred, in which case the former value is used.
269*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200270static inline uint64_t ETHOSU_PMU_Get_CCNTR(void)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200271{
272 uint64_t val1 = ethosu_pmu_ctrl->PMCCNTR;
273 uint64_t val2 = ethosu_pmu_ctrl->PMCCNTR;
274
275 if (val2 > val1)
276 {
277 return val2;
278 }
279 return val1;
280}
281
282/**
283 \brief Set cycle counter (64 bit)
284 \param [in] val Conter value
285 \note Two HW 32-bit registers that can increment independently in-between reads.
286 To work-around raciness, counter is temporary disabled if enabled.
287 \note ETHOSU specific. Usage breaks CMSIS complience
288*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200289static inline void ETHOSU_PMU_Set_CCNTR(uint64_t val)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200290{
291 uint32_t mask = ETHOSU_PMU_CNTR_Status();
292
293 if (mask & ETHOSU_PMU_CCNT_Msk)
294 {
295 ETHOSU_PMU_CNTR_Disable(ETHOSU_PMU_CCNT_Msk);
296 }
297
298 ethosu_pmu_ctrl->PMCCNTR = val;
299
300 if (mask & ETHOSU_PMU_CCNT_Msk)
301 {
302 ETHOSU_PMU_CNTR_Enable(ETHOSU_PMU_CCNT_Msk);
303 }
304}
305
306/**
307 \brief Read event counter
308 \param [in] num Event counter (0-ETHOSU_PMU_NCOUNTERS)
309 \return Event count
310*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200311static inline uint32_t ETHOSU_PMU_Get_EVCNTR(uint32_t num)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200312{
313 return (*ethosu_pmu_cntr)[num];
314}
315
316/**
317 \brief Set event counter value
318 \param [in] num Event counter (0-ETHOSU_PMU_NCOUNTERS)
319 \param [in] val Conter value
320 \note ETHOSU specific. Usage breaks CMSIS complience
321*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200322static inline void ETHOSU_PMU_Set_EVCNTR(uint32_t num, uint32_t val)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200323{
324 (*ethosu_pmu_cntr)[num] = val;
325}
326/**
327 \brief Read counter overflow status
328 \return Counter overflow status bits for the following:
329 - event counters (bit 0-ETHOSU_PMU_NCOUNTERS))
330 - cycle counter (bit 31)
331*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200332static inline uint32_t ETHOSU_PMU_Get_CNTR_OVS(void)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200333{
334 return ethosu_pmu_ctrl->PMOVSSET;
335}
336
337/**
338 \brief Clear counter overflow status
339 \param [in] mask Counter overflow status bits to clear
340 \note Clears overflow status bits for one or more of the following:
341 - event counters (bit 0-ETHOSU_PMU_NCOUNTERS)
342 - cycle counter (bit 31)
343*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200344static inline void ETHOSU_PMU_Set_CNTR_OVS(uint32_t mask)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200345{
346 ethosu_pmu_ctrl->PMOVSCLR = mask;
347}
348
349/**
350 \brief Enable counter overflow interrupt request
351 \param [in] mask Counter overflow interrupt request bits to set
352 \note Sets overflow interrupt request bits for one or more of the following:
353 - event counters (bit 0-ETHOSU_PMU_NCOUNTERS)
354 - cycle counter (bit 31)
355*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200356static inline void ETHOSU_PMU_Set_CNTR_IRQ_Enable(uint32_t mask)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200357{
358 ethosu_pmu_ctrl->PMINTSET = mask;
359}
360
361/**
362 \brief Disable counter overflow interrupt request
363 \param [in] mask Counter overflow interrupt request bits to clear
364 \note Clears overflow interrupt request bits for one or more of the following:
365 - event counters (bit 0-ETHOSU_PMU_NCOUNTERS)
366 - cycle counter (bit 31)
367*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200368static inline void ETHOSU_PMU_Set_CNTR_IRQ_Disable(uint32_t mask)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200369{
370 ethosu_pmu_ctrl->PMINTCLR = mask;
371}
372
373/**
374 \brief Get counters overflow interrupt request stiinings
375 \return mask Counter overflow interrupt request bits
376 \note Sets overflow interrupt request bits for one or more of the following:
377 - event counters (bit 0-ETHOSU_PMU_NCOUNTERS)
378 - cycle counter (bit 31)
379 \note ETHOSU specific. Usage breaks CMSIS complience
380*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200381static inline uint32_t ETHOSU_PMU_Get_IRQ_Enable()
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200382{
383 return ethosu_pmu_ctrl->PMINTSET;
384}
385
386/**
387 \brief Software increment event counter
388 \param [in] mask Counters to increment
389 - event counters (bit 0-ETHOSU_PMU_NCOUNTERS)
390 - cycle counter (bit 31)
391 \note Software increment bits for one or more event counters.
392*/
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200393static inline void ETHOSU_PMU_CNTR_Increment(uint32_t mask)
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200394{
395 uint32_t cntrs_active = ETHOSU_PMU_CNTR_Status();
396
397 if (mask & ETHOSU_PMU_CCNT_Msk)
398 {
399 if (mask & ETHOSU_PMU_CCNT_Msk)
400 {
401 ETHOSU_PMU_CNTR_Disable(ETHOSU_PMU_CCNT_Msk);
402 ethosu_pmu_ctrl->PMCCNTR = ETHOSU_PMU_Get_CCNTR() + 1;
403 if (cntrs_active & ETHOSU_PMU_CCNT_Msk)
404 {
405 ETHOSU_PMU_CNTR_Enable(ETHOSU_PMU_CCNT_Msk);
406 }
407 }
408 }
409 for (int i = 0; i < ETHOSU_PMU_NCOUNTERS; i++)
410 {
411 uint32_t cntr = (0x0001 << i);
412
413 if (mask & cntr)
414 {
415 ETHOSU_PMU_CNTR_Disable(cntr);
416 (*ethosu_pmu_cntr)[i]++;
417 if (cntrs_active & cntr)
418 {
419 ETHOSU_PMU_CNTR_Enable(cntr);
420 }
421 }
422 }
423}
424
Kristofer Jonsson537c71c2020-05-05 14:17:22 +0200425#ifdef __cplusplus
426}
427#endif
428
Kristofer Jonsson49bdee82020-04-06 13:21:21 +0200429#endif /* PMU_ETHOSU_H */