Skip to content

Commit 41f5863

Browse files
willieyzmkannwischer
authored andcommitted
Port: Add PMU cycle counting for Armv8.1-M
Add PMU-based cycle counting support for Armv8.1-M Cortex-M processors. This uses the CMSIS PMU APIs for portable cycle counter access. Signed-off-by: willieyz <[email protected]>
1 parent 83f2970 commit 41f5863

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

test/baremetal/platform/m55-an547/platform.mk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
# Copyright (c) The mldsa-native project authors
2+
# Copyright (c) The mlkem-native project authors
23
# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
34

45
PLATFORM_PATH:=test/baremetal/platform/m55-an547
56

67
CROSS_PREFIX=arm-none-eabi-
78
CC=gcc
89

10+
# Use PMU cycle counting by default
11+
CYCLES ?= PMU
12+
13+
# Reduce iterations for benchmarking
14+
CFLAGS += -DMLD_BENCHMARK_NTESTS=3 -DMLD_BENCHMARK_NITERATIONS=2 -DMLD_BENCHMARK_NWARMUP=3
15+
916
CFLAGS += \
1017
-O3 \
1118
-Wall -Wextra -Wshadow \
1219
-Wno-pedantic \
1320
-Wno-redundant-decls \
1421
-Wno-missing-prototypes \
22+
-Wno-conversion \
23+
-Wno-sign-conversion \
1524
-fno-common \
1625
-ffunction-sections \
1726
-fdata-sections \

test/hal/hal.c

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* Copyright (c) The mldsa-native project authors
33
* Copyright (c) The mlkem-native project authors
4-
* Copyright (c) 2022 Arm Limited
54
* Copyright (c) 2020 Dougall Johnson
5+
* Copyright (c) 2022 Arm Limited
66
* SPDX-License-Identifier: MIT
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -95,9 +95,52 @@ uint64_t get_cyclecounter(void)
9595
return retval;
9696
}
9797

98-
#else /* (!__x86_64__ && __AARCH64EL__) || _M_ARM64 */
99-
#error PMU_CYCLES option only supported on x86_64 and AArch64
100-
#endif /* !__x86_64__ && !(__AARCH64EL__ || _M_ARM64) */
98+
#elif defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8_1M_MAIN__)
99+
#include <ARMCM55.h>
100+
#include <system_ARMCM55.h>
101+
#include "pmu_armv8.h"
102+
103+
void enable_cyclecounter(void)
104+
{
105+
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
106+
ARM_PMU_Enable();
107+
ARM_PMU_CYCCNT_Reset();
108+
ARM_PMU_CNTR_Enable(PMU_CNTENSET_CCNTR_ENABLE_Msk);
109+
}
110+
111+
void disable_cyclecounter(void)
112+
{
113+
ARM_PMU_CNTR_Disable(PMU_CNTENSET_CCNTR_ENABLE_Msk);
114+
ARM_PMU_Disable();
115+
}
116+
117+
uint64_t get_cyclecounter(void) { return ARM_PMU_Get_CCNTR(); }
118+
119+
#elif defined(__riscv)
120+
121+
void enable_cyclecounter(void) {}
122+
123+
void disable_cyclecounter(void) {}
124+
125+
uint64_t get_cyclecounter(void)
126+
{
127+
#if (__riscv_xlen == 32)
128+
uint32_t lo, hi;
129+
__asm__ volatile("rdcycle %0" : "=r"(lo));
130+
__asm__ volatile("rdcycleh %0" : "=r"(hi));
131+
return (((uint64_t)hi) << 32) | ((uint64_t)lo);
132+
#else /* __riscv_xlen == 32 */
133+
uint64_t retval;
134+
__asm__ volatile("rdcycle %0" : "=r"(retval));
135+
return retval;
136+
#endif /* __riscv_xlen != 32 */
137+
}
138+
139+
#else /* !__x86_64__ && !(__AARCH64EL__ || _M_ARM64) && !(__ARM_ARCH_8M_MAIN__ \
140+
|| __ARM_ARCH_8_1M_MAIN__) && __riscv */
141+
#error PMU_CYCLES option only supported on x86_64, AArch64, Armv8-M, and RISC-V
142+
#endif /* !__x86_64__ && !(__AARCH64EL__ || _M_ARM64) && \
143+
!(__ARM_ARCH_8M_MAIN__ || __ARM_ARCH_8_1M_MAIN__) && !__riscv */
101144

102145
#elif defined(PERF_CYCLES)
103146

@@ -154,10 +197,8 @@ uint64_t get_cyclecounter(void)
154197
return (uint64_t)cpu_cycles;
155198
}
156199
#elif defined(MAC_CYCLES)
157-
158200
/* Based on @[m1cycles] */
159201

160-
161202
#include <dlfcn.h>
162203
#include <pthread.h>
163204
#include <stdio.h>

0 commit comments

Comments
 (0)