ARM PMU tests
Subsystems (3)
PATCH v14 3/5 arm: Add PMU test
In reply to Wei HuangFrom: Christopher Covington <[email protected]>
Beginning with a simple sanity check of the control register, add a unit test for the ARM Performance Monitors Unit (PMU).
--- arm/Makefile.common | 3 +- arm/pmu.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ arm/unittests.cfg | 5 +++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 arm/pmu.c
arm/Makefile.common +2 -1
@@ -12,7 +12,8 @@ endif tests-common = \ $(TEST_DIR)/selftest.flat \ $(TEST_DIR)/spinlock-test.flat \ - $(TEST_DIR)/pci-test.flat + $(TEST_DIR)/pci-test.flat \ + $(TEST_DIR)/pmu.flat all: test_cases
arm/pmu.c new file +93 -0
93 lines not on this page · raw message
arm/unittests.cfg +5 -0
@@ -58,3 +58,8 @@ groups = selftest [pci-test] file = pci-test.flat groups = pci + +# Test PMU support +[pmu] +file = pmu.flat +groups = pmu
Signature · 1 line
1.8.3.1
PATCH v14 4/5 arm: pmu: Check cycle count increases
In reply to Wei HuangFrom: Christopher Covington <[email protected]>
Ensure that reads of the PMCCNTR_EL0 are monotonically increasing, even for the smallest delta of two subsequent reads.
--- arm/pmu.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+)
arm/pmu.c +66 -0
@@ -18,6 +18,9 @@#include "asm/sysreg.h" #include "asm/processor.h" +#define PMU_PMCR_E (1 << 0) +#define PMU_PMCR_C (1 << 2) +#define PMU_PMCR_LC (1 << 6)#define PMU_PMCR_N_SHIFT 11#define PMU_PMCR_N_MASK 0x1f#define PMU_PMCR_ID_SHIFT 16
@@ -28,16 +31,47 @@#define ID_DFR0_PERFMON_SHIFT 24#define ID_DFR0_PERFMON_MASK 0xf +#define PMU_CYCLE_IDX 31 + +#define NR_SAMPLES 10 +static unsigned int pmu_version;#if defined(__arm__)#define PMCR __ACCESS_CP15(c9, 0, c12, 0)#define ID_DFR0 __ACCESS_CP15(c0, 0, c1, 2) +#define PMSELR __ACCESS_CP15(c9, 0, c12, 5) +#define PMXEVTYPER __ACCESS_CP15(c9, 0, c13, 1) +#define PMCNTENSET __ACCESS_CP15(c9, 0, c12, 1) +#define PMCCNTR32 __ACCESS_CP15(c9, 0, c13, 0) +#define PMCCNTR64 __ACCESS_CP15_64(0, c9)static inline uint32_t get_id_dfr0 (void ) {return read_sysreg (ID_DFR0 ); }static inline uint32_t get_pmcr (void ) {return read_sysreg (PMCR ); } +static inline void set_pmcr (uint32_t v) {write_sysreg (v,PMCR ); } +static inline void set_pmcntenset (uint32_t v) {write_sysreg (v,PMCNTENSET ); } + +static inline uint64_t get_pmccntr (void ) +{ +if (pmu_version ==0x3 ) +return read_sysreg (PMCCNTR64 ); +else +return read_sysreg (PMCCNTR32 ); +} + +/* PMCCFILTR is an obsolete name for PMXEVTYPER31 in ARMv7 */ +static inline void set_pmccfiltr (uint32_t value) +{ +write_sysreg (PMU_CYCLE_IDX ,PMSELR ); +write_sysreg (value,PMXEVTYPER ); +isb (); +}#elif defined(__aarch64__)static inline uint32_t get_id_dfr0 (void ) {return read_sysreg (id_dfr0_el1); }static inline uint32_t get_pmcr (void ) {return read_sysreg (pmcr_el0); } +static inline void set_pmcr (uint32_t v) {write_sysreg (v, pmcr_el0); } +static inline uint64_t get_pmccntr (void ) {return read_sysreg (pmccntr_el0); } +static inline void set_pmcntenset (uint32_t v) {write_sysreg (v, pmcntenset_el0); } +static inline void set_pmccfiltr (uint32_t v) {write_sysreg (v, pmccfiltr_el0); }#endif /*
@@ -63,6 +97,37 @@ static bool check_pmcr(void)return ((pmcr >>PMU_PMCR_IMP_SHIFT ) &PMU_PMCR_IMP_MASK ) !=0 ; } +/* +* Ensure that the cycle counter progresses between back-to-back reads. +*/ +static bool check_cycles_increase (void ) +{ +bool success = true; + +/* init before event access, this test only cares about cycle count */ +set_pmcntenset (1 <<PMU_CYCLE_IDX ); +set_pmccfiltr (0 );/* count cycles in EL0, EL1, but not EL2 */ + +set_pmcr (get_pmcr () |PMU_PMCR_LC |PMU_PMCR_C |PMU_PMCR_E ); + +for (int i =0 ; i <NR_SAMPLES ; i++) { +uint64_t a, b; + + a =get_pmccntr (); + b =get_pmccntr (); + +if (a >= b) { +printf ("Read %" PRId64" then %" PRId64".\n" , a, b); + success = false; +break ; + } + } + +set_pmcr (get_pmcr () & ~PMU_PMCR_E ); + +return success; +} +/* Return FALSE if no PMU found, otherwise return TRUE */ bool pmu_probe (void ) {
@@ -88,6 +153,7 @@ int main(void)report_prefix_push ("pmu" );report ("Control register" ,check_pmcr ()); +report ("Monotonically increasing cycle count" ,check_cycles_increase ());return report_summary (); }
Signature · 1 line
1.8.3.1
PATCH v14 5/5 arm: pmu: Add CPI checking
In reply to Wei HuangFrom: Christopher Covington <[email protected]>
Calculate the numbers of cycles per instruction (CPI) implied by ARM PMU cycle counter values. The code includes a strict checking facility intended for the -icount option in TCG mode in the configuration file.
--- arm/pmu.c | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++- arm/unittests.cfg | 14 ++++++ 2 files changed, 146 insertions(+), 1 deletion(-)
arm/pmu.c +132 -1
@@ -58,6 +58,14 @@ static inline uint64_t get_pmccntr(void)return read_sysreg (PMCCNTR32 ); } +static inline void set_pmccntr (uint64_t value) +{ +if (pmu_version ==0x3 ) +write_sysreg (value,PMCCNTR64 ); +else +write_sysreg (value &0xffffffff ,PMCCNTR32 ); +} +/* PMCCFILTR is an obsolete name for PMXEVTYPER31 in ARMv7 */ static inline void set_pmccfiltr (uint32_t value) {
@@ -65,13 +73,56 @@ static inline void set_pmccfiltr(uint32_t value)write_sysreg (value,PMXEVTYPER ); isb(); } + +/* +* Extra instructions inserted by the compiler would be difficult to compensate +* for, so hand assemble everything between, and including, the PMCR accesses +* to start and stop counting. isb instructions were inserted to make sure +* pmccntr read after this function returns the exact instructions executed in +* the controlled block. Total instrs = isb + mcr + 2*loop = 2 + 2*loop. +*/ +static inline void precise_instrs_loop (int loop,uint32_t pmcr) +{ + asmvolatile ( +" mcr p15, 0, %[pmcr], c9, c12, 0\n" +" isb\n" +"1: subs %[loop], %[loop], #1\n" +" bgt 1b\n" +" mcr p15, 0, %[z], c9, c12, 0\n" +" isb\n" + : [loop]"+r" (loop) + : [pmcr]"r" (pmcr), [z]"r" (0 ) + :"cc" ); +}#elif defined(__aarch64__)static inline uint32_t get_id_dfr0 (void ) {return read_sysreg (id_dfr0_el1); }static inline uint32_t get_pmcr (void ) {return read_sysreg (pmcr_el0); }static inline void set_pmcr (uint32_t v) {write_sysreg (v, pmcr_el0); }static inline uint64_t get_pmccntr (void ) {return read_sysreg (pmccntr_el0); } +static inline void set_pmccntr (uint64_t v) {write_sysreg (v, pmccntr_el0); }static inline void set_pmcntenset (uint32_t v) {write_sysreg (v, pmcntenset_el0); }static inline void set_pmccfiltr (uint32_t v) {write_sysreg (v, pmccfiltr_el0); } + +/* +* Extra instructions inserted by the compiler would be difficult to compensate +* for, so hand assemble everything between, and including, the PMCR accesses +* to start and stop counting. isb instructions are inserted to make sure +* pmccntr read after this function returns the exact instructions executed +* in the controlled block. Total instrs = isb + msr + 2*loop = 2 + 2*loop. +*/ +static inline void precise_instrs_loop (int loop,uint32_t pmcr) +{ + asmvolatile ( +" msr pmcr_el0, %[pmcr]\n" +" isb\n" +"1: subs %[loop], %[loop], #1\n" +" b.gt 1b\n" +" msr pmcr_el0, xzr\n" +" isb\n" + : [loop]"+r" (loop) + : [pmcr]"r" (pmcr) + :"cc" ); +}#endif /*
@@ -128,6 +179,80 @@ static bool check_cycles_increase(void)return success; } +/* +* Execute a known number of guest instructions. Only even instruction counts +* greater than or equal to 4 are supported by the in-line assembly code. The +* control register (PMCR_EL0) is initialized with the provided value (allowing +* for example for the cycle counter or event counters to be reset). At the end +* of the exact instruction loop, zero is written to PMCR_EL0 to disable +* counting, allowing the cycle counter or event counters to be read at the +* leisure of the calling code. +*/ +static void measure_instrs (int num,uint32_t pmcr) +{ +int loop = (num -2 ) /2 ; + +assert (num >=4 && ((num -2 ) %2 ==0 )); +precise_instrs_loop (loop, pmcr); +} + +/* +* Measure cycle counts for various known instruction counts. Ensure that the +* cycle counter progresses (similar to check_cycles_increase() but with more +* instructions and using reset and stop controls). If supplied a positive, +* nonzero CPI parameter, it also strictly checks that every measurement matches +* it. Strict CPI checking is used to test -icount mode. +*/ +static bool check_cpi (int cpi) +{ +uint32_t pmcr =get_pmcr () |PMU_PMCR_LC |PMU_PMCR_C |PMU_PMCR_E ; + +/* init before event access, this test only cares about cycle count */ +set_pmcntenset (1 <<PMU_CYCLE_IDX ); +set_pmccfiltr (0 );/* count cycles in EL0, EL1, but not EL2 */ + +if (cpi >0 ) +printf ("Checking for CPI=%d.\n" , cpi); +printf ("instrs : cycles0 cycles1 ...\n" ); + +for (unsigned int i =4 ; i <300 ; i +=32 ) { +uint64_t avg, sum =0 ; + +printf ("%4d:" , i); +for (int j =0 ; j <NR_SAMPLES ; j++) { +uint64_t cycles; + +set_pmccntr (0 ); +measure_instrs (i, pmcr); + cycles =get_pmccntr (); +printf (" %4" PRId64"" , cycles); + +if (!cycles) { +printf ("\ncycles not incrementing!\n" ); +return false; + }else if (cpi >0 && cycles != i * cpi) { +printf ("\nunexpected cycle count received!\n" ); +return false; + }else if ((cycles >>32 ) !=0 ) { +/* The cycles taken by the loop above should +* fit in 32 bits easily. We check the upper +* 32 bits of the cycle counter to make sure +* there is no supprise. */ +printf ("\ncycle count bigger than 32bit!\n" ); +return false; + } + + sum += cycles; + } + avg = sum /NR_SAMPLES ; +printf (" avg=%-4" PRId64" %s=%-3" PRId64"\n" , avg, + (avg >= i) ?"cpi" :"ipc" , + (avg >= i) ? avg / i : i / avg); + } + +return true; +} +/* Return FALSE if no PMU found, otherwise return TRUE */ bool pmu_probe (void ) {
@@ -143,8 +268,13 @@ bool pmu_probe(void)return pmu_version; } -int main (void ) +int main (int argc,char *argv[]) { +int cpi =0 ; + +if (argc >1 ) + cpi =atol (argv[1 ]); +if (!pmu_probe ()) {printf ("No PMU found, test skipped...\n" );return report_summary ();
@@ -154,6 +284,7 @@ int main(void)report ("Control register" ,check_pmcr ());report ("Monotonically increasing cycle count" ,check_cycles_increase ()); +report ("Cycle/instruction ratio" ,check_cpi (cpi));return report_summary (); }
arm/unittests.cfg +14 -0
@@ -63,3 +63,17 @@ groups = pci [pmu] file = pmu.flat groups = pmu + +# Test PMU support (TCG) with -icount IPC=1 +[pmu-tcg-icount-1] +file = pmu.flat +extra_params = -icount 0 -append '1' +groups = pmu +accel = tcg + +# Test PMU support (TCG) with -icount IPC=256 +[pmu-tcg-icount-256] +file = pmu.flat +extra_params = -icount 8 -append '256' +groups = pmu +accel = tcg
Signature · 1 line
1.8.3.1
v14 cover ARM PMU tests
Changes from v13:
* Rename cp15.h to sysreg.h for ARMv7 and add a new file sysreg.h for ARMv8
* Add macros for read_sysreg() and write_sysreg(). CP15 registers can be defined with __ACCESS_CP15() or __ACCESS_CP15_64(). sysreg.h (ARMv8) was from Drew's GIC testing code, which can be leveraged when his GIC testing code is imported.
* Rewrite PMU testing code based on new macros. All get_xxx() and set_xxx() functions are defined in pmu.c based on read_sysreg() and write_sysreg(). So the code parsing tool, like cscope, can parse them easily.
* Minor fixes inside pmu.c, printf formatting, pmu_probe() func, based on Andre's comments.
Note:
Current KVM code has bugs in handling PMCCFILTR write. A fix (see below) is required for this unit testing code to work correctly under KVM mode.
Thanks,
-Wei
Christopher Covington (3):
arm: Add PMU test
arm: pmu: Check cycle count increases
arm: pmu: Add CPI checking
Wei Huang (2):
arm: rename cp15.h to sysreg.h
arm: Add support for read_sysreg() and write_sysreg()
arm/Makefile.common | 3 +-
arm/cstart.S | 2 +-
arm/pmu.c | 290 +++++++++++++++++++++++++++++++++++++++
arm/unittests.cfg | 19 +++
lib/arm/asm/processor.h | 6 +-
lib/arm/asm/{cp15.h => sysreg.h} | 25 +++-
lib/arm64/asm/processor.h | 11 +-
lib/arm64/asm/sysreg.h | 26 ++++
lib/libcflat.h | 4 +-
9 files changed, 369 insertions(+), 17 deletions(-)
create mode 100644 arm/pmu.c
rename lib/arm/asm/{cp15.h => sysreg.h} (68%)
create mode 100644 lib/arm64/asm/sysreg.hSignature · 1 line
1.8.3.1
PATCH v14 1/5 arm: rename cp15.h to sysreg.h
To prepare for future support of ARMv8 system register, rename cp15.h file to sysreg.h, with _ASMARM_CP15_H_ renamed to _ASMARM_SYSREG_H_ in header file.
---
arm/cstart.S | 2 +-
lib/arm/asm/{cp15.h => sysreg.h} | 6 +++---
2 files changed, 4 insertions(+), 4 deletions(-)
rename lib/arm/asm/{cp15.h => sysreg.h} (94%)arm/cstart.S +1 -1
@@ -9,7 +9,7 @@ #include <asm/thread_info.h> #include <asm/asm-offsets.h> #include <asm/ptrace.h> -#include <asm/cp15.h> +#include <asm/sysreg.h> #define THREAD_START_SP ((THREAD_SIZE - S_FRAME_SIZE * 8) & ~7)
lib/arm/asm/sysreg.h renamed from lib/arm/asm/cp15.h +3 -3
@@ -1,5 +1,5 @@ -#ifndef _ASMARM_CP15_H_ -#define _ASMARM_CP15_H_ +#ifndef _ASMARM_SYSREG_H_ +#define _ASMARM_SYSREG_H_/* * From the Linux kernel arch/arm/include/asm/cp15.h *
@@ -34,4 +34,4 @@#define CR_AFE (1 << 29)/* Access flag enable */ #define CR_TE (1 << 30)/* Thumb exception enable */ -#endif /* _ASMARM_CP15_H_ */ +#endif /* _ASMARM_SYSREG_H_ */
Signature · 1 line
1.8.3.1
PATCH v14 2/5 arm: Add support for read_sysreg() and write_sysreg()
In reply to Wei HuangThis patch adds two new macros to support read/write operations of ARMv7 and ARMv8 system registers. As part of the change, xstr() is revised to support variable arguments. With it, ARMv7 system register can be defined with __ACCESS_CP15() or __ACCESS_CP15_64() depending if it is 32-bit or 64-bit. get_mpidr() is re-written with new macros.
--- lib/arm/asm/processor.h | 6 +++--- lib/arm/asm/sysreg.h | 19 +++++++++++++++++++ lib/arm64/asm/processor.h | 11 ++++------- lib/arm64/asm/sysreg.h | 26 ++++++++++++++++++++++++++ lib/libcflat.h | 4 ++-- 5 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 lib/arm64/asm/sysreg.h
lib/arm/asm/processor.h +3 -3
@@ -6,6 +6,7 @@* This work is licensed under the terms of the GNU LGPL, version 2. */ #include <asm/ptrace.h> +#include <asm/sysreg.h> enum vector {EXCPTN_RST ,
@@ -33,11 +34,10 @@ static inline unsigned long current_cpsr(void)#define current_mode () (current_cpsr() & MODE_MASK) +#define MPIDR __ACCESS_CP15(c0, 0, c0, 5)static inline unsigned int get_mpidr (void ) { -unsigned int mpidr; - asmvolatile ("mrc p15, 0, %0, c0, c0, 5" :"=r" (mpidr)); -return mpidr; +return read_sysreg (MPIDR ); }/* Only support Aff0 for now, up to 4 cpus */
lib/arm/asm/sysreg.h +19 -0
@@ -34,4 +34,23 @@#define CR_AFE (1 << 29)/* Access flag enable */ #define CR_TE (1 << 30)/* Thumb exception enable */ +#ifndef __ASSEMBLY__ +#include <libcflat.h> + +#define __ACCESS_CP15 (CRn, Op1, CRm, Op2) \ + "mrc", "mcr", xstr(p15, Op1, %0, CRn, CRm, Op2), u32 +#define __ACCESS_CP15_64 (Op1, CRm) \ + "mrrc", "mcrr", xstr(p15, Op1, %Q0, %R0, CRm), u64 + +#define __read_sysreg (r, w, c, t) ({ \ + t __val; \ + asm volatile(r " " c : "=r" (__val)); \ + __val; \ + }) +#define read_sysreg (...) __read_sysreg(__VA_ARGS__) + +#define __write_sysreg (v, r, w, c, t) asm volatile(w " " c : : "r" ((t)(v))) +#define write_sysreg (v, ...) __write_sysreg(v, __VA_ARGS__) +#endif /* !__ASSEMBLY__ */ +#endif /* _ASMARM_SYSREG_H_ */
lib/arm64/asm/processor.h +4 -7
@@ -19,6 +19,7 @@#ifndef __ASSEMBLY__#include <asm/ptrace.h> #include <asm/esr.h> +#include <asm/sysreg.h> enum vector { EL1T_SYNC,
@@ -66,14 +67,10 @@ static inline unsigned long current_level(void)return el &0xc ; } -#define DEFINE_GET_SYSREG32 (reg) \ -static inline unsigned int get_##reg(void) \ -{ \ - unsigned int reg; \ - asm volatile("mrs %0, " #reg "_el1" : "=r" (reg)); \ - return reg; \ +static inline unsigned int get_mpidr (void ) +{ +return read_sysreg (mpidr_el1); } -DEFINE_GET_SYSREG32 (mpidr)/* Only support Aff0 for now, gicv2 only */ #define mpidr_to_cpu (mpidr) ((int)((mpidr) & 0xff))
lib/arm64/asm/sysreg.h new file +26 -0
@@ -0,0 +1,26 @@ +/* +* Ripped off from arch/arm64/include/asm/sysreg.h +* +* Copyright (C) 2016, Red Hat Inc, Andrew Jones <[email protected]> +* +* This work is licensed under the terms of the GNU LGPL, version 2. +*/ +#ifndef _ASMARM64_SYSREG_H_ +#define _ASMARM64_SYSREG_H_ + +#ifndef __ASSEMBLY__ +#include <libcflat.h> + +#define read_sysreg (r) ({ \ + u64 __val; \ + asm volatile("mrs %0, " xstr(r) : "=r" (__val)); \ + __val; \ +}) + +#define write_sysreg (v, r) do { \ + u64 __val = (u64)v; \ + asm volatile("msr " xstr(r) ", %x0" : : "rZ" (__val)); \ +} while (0) + +#endif /* !__ASSEMBLY__ */ +#endif /* _ASMARM64_SYSREG_H_ */
lib/libcflat.h +2 -2
@@ -27,8 +27,8 @@#define __unused __attribute__((__unused__)) -#define xstr (s) xxstr(s) -#define xxstr (s) #s +#define xstr(s...) xxstr(s) +#define xxstr(s...) #s#define __ALIGN_MASK (x, mask) (((x) + (mask)) & ~(mask))#define __ALIGN (x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [kvm-unit-tests PATCH v14 2/5] arm: Add support for read_sysreg() and write_sysreg()
Quoted 30 lines · Wei Huang
This patch adds two new macros to support read/write operations of ARMv7 and ARMv8 system registers. As part of the change,
xstr()is revised to support variable arguments. With it, ARMv7 system register can be defined with__ACCESS_CP15()or__ACCESS_CP15_64()depending if it is 32-bit or 64-bit.get_mpidr()is re-written with new macros.Suggested-by Andrew Jones <[email protected]>Signed-off-by Wei Huang <[email protected]>--- lib/arm/asm/processor.h | 6 +++--- lib/arm/asm/sysreg.h | 19 +++++++++++++++++++ lib/arm64/asm/processor.h | 11 ++++------- lib/arm64/asm/sysreg.h | 26 ++++++++++++++++++++++++++ lib/libcflat.h | 4 ++-- 5 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 lib/arm64/asm/sysreg.hlib/arm/asm/processor.h +1
-0@@ -6,6 +6,7 @@* This work is licensed under the terms of the GNU LGPL, version 2. */ #include <asm/ptrace.h> +#include <asm/sysreg.h> enum vector {EXCPTN_RST ,
lib/arm/asm/processor.h static inline unsigned long current_cpsr(void) · line 38
Quoted 5 lines
lib/arm/asm/processor.h +1
-0@@ -33,11 +34,10 @@ static inline unsigned long current_cpsr(void)#define current_mode () (current_cpsr() & MODE_MASK) +#define MPIDR __ACCESS_CP15(c0, 0, c0, 5)static inline unsigned int get_mpidr (void )
I'll change the get_mpidr functions to u64 after the gic series is merged, as we've discussed previously.
Quoted 85 lines
lib/arm/asm/processor.h +1
-3{ -unsigned int mpidr; - asmvolatile ("mrc p15, 0, %0, c0, c0, 5" :"=r" (mpidr)); -return mpidr; +return read_sysreg (MPIDR ); }/* Only support Aff0 for now, up to 4 cpus */ lib/arm/asm/sysreg.h +19
-0@@ -34,4 +34,23 @@#define CR_AFE (1 << 29)/* Access flag enable */ #define CR_TE (1 << 30)/* Thumb exception enable */ +#ifndef __ASSEMBLY__ +#include <libcflat.h> + +#define __ACCESS_CP15 (CRn, Op1, CRm, Op2) \ + "mrc", "mcr", xstr(p15, Op1, %0, CRn, CRm, Op2), u32 +#define __ACCESS_CP15_64 (Op1, CRm) \ + "mrrc", "mcrr", xstr(p15, Op1, %Q0, %R0, CRm), u64 + +#define __read_sysreg (r, w, c, t) ({ \ + t __val; \ + asm volatile(r " " c : "=r" (__val)); \ + __val; \ + }) +#define read_sysreg (...) __read_sysreg(__VA_ARGS__) + +#define __write_sysreg (v, r, w, c, t) asm volatile(w " " c : : "r" ((t)(v))) +#define write_sysreg (v, ...) __write_sysreg(v, __VA_ARGS__) +#endif /* !__ASSEMBLY__ */ +#endif /* _ASMARM_SYSREG_H_ */ lib/arm64/asm/processor.h +4
-7@@ -19,6 +19,7 @@#ifndef __ASSEMBLY__#include <asm/ptrace.h> #include <asm/esr.h> +#include <asm/sysreg.h> enum vector { EL1T_SYNC,@@ -66,14 +67,10 @@ static inline unsigned long current_level(void)return el &0xc ; } -#define DEFINE_GET_SYSREG32 (reg) \ -static inline unsigned int get_##reg(void) \ -{ \ - unsigned int reg; \ - asm volatile("mrs %0, " #reg "_el1" : "=r" (reg)); \ - return reg; \ +static inline unsigned int get_mpidr (void ) +{ +return read_sysreg (mpidr_el1); } -DEFINE_GET_SYSREG32 (mpidr)/* Only support Aff0 for now, gicv2 only */ #define mpidr_to_cpu (mpidr) ((int)((mpidr) & 0xff))lib/arm64/asm/sysreg.h new file +13
-0@@ -0,0 +1,26 @@ +/* +* Ripped off from arch/arm64/include/asm/sysreg.h +* +* Copyright (C) 2016, Red Hat Inc, Andrew Jones <[email protected]> +* +* This work is licensed under the terms of the GNU LGPL, version 2. +*/ +#ifndef _ASMARM64_SYSREG_H_ +#define _ASMARM64_SYSREG_H_ + +#ifndef __ASSEMBLY__ +#include <libcflat.h> +
lib/arm64/asm/sysreg.h line 23
Quoted 10 lines
lib/arm64/asm/sysreg.h new file +10
-0+#define read_sysreg (r) ({ \ + u64 __val; \ + asm volatile("mrs %0, " xstr(r) : "=r" (__val)); \ + __val; \ +}) + +#define write_sysreg (v, r) do { \ + u64 __val = (u64)v; \ + asm volatile("msr " xstr(r) ", %x0" : : "rZ" (__val)); \ +} while (0)
I'd prefer using the ({}) syntax here to the do-while, but it doesn't matter.
Quoted 22 lines
lib/arm64/asm/sysreg.h new file +3
-0+ +#endif /* !__ASSEMBLY__ */ +#endif /* _ASMARM64_SYSREG_H_ */ lib/libcflat.h +2
-2@@ -27,8 +27,8 @@#define __unused __attribute__((__unused__)) -#define xstr (s) xxstr(s) -#define xxstr (s) #s +#define xstr(s...) xxstr(s) +#define xxstr(s...) #s#define __ALIGN_MASK (x, mask) (((x) + (mask)) & ~(mask))#define __ALIGN (x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)Signature · 1 line
1.8.3.1
Thanks,
drew
Quoted 11 lines · Wei Huang
Changes from v13:
* Renamecp15.htosysreg.hfor ARMv7 and add a new filesysreg.hfor ARMv8
* Add macros forread_sysreg()andwrite_sysreg(). CP15 registers can be defined with__ACCESS_CP15()or__ACCESS_CP15_64().sysreg.h(ARMv8) was from Drew's GIC testing code, which can be leveraged when his GIC testing code is imported.
* Rewrite PMU testing code based on new macros. Allget_xxx()andset_xxx()functions are defined inpmu.cbased onread_sysreg()andwrite_sysreg(). So the code parsing tool, like cscope, can parse them easily.
* Minor fixes insidepmu.c, printf formatting,pmu_probe()func, based on Andre's comments.
Thanks for this Wei. I like the sysreg framework. It should address Andre's concerns with asm-maintainability, plus maintain code analysis tool (cscope) support.
The series looks good to me, so I'll apply it to arm/next and rebase the gic series on it, using the new sysreg framework everywhere needed.
drew
Quoted 36 lines
Note:
Current KVM code has bugs in handling PMCCFILTR write. A fix (see below) is required for this unit testing code to work correctly under KVM mode.Thanks,
-WeiChristopher Covington (3):
arm: Add PMU test
arm: pmu: Check cycle count increases
arm: pmu: Add CPI checkingWei Huang (2):
arm: renamecp15.htosysreg.h
arm: Add support forread_sysreg()andwrite_sysreg()arm/Makefile.common | 3 +- arm/cstart.S | 2 +- arm/pmu.c | 290 +++++++++++++++++++++++++++++++++++++++ arm/unittests.cfg | 19 +++ lib/arm/asm/processor.h | 6 +- lib/arm/asm/{cp15.h => sysreg.h} | 25 +++- lib/arm64/asm/processor.h | 11 +- lib/arm64/asm/sysreg.h | 26 ++++ lib/libcflat.h | 4 +- 9 files changed, 369 insertions(+), 17 deletions(-) create mode 100644 arm/pmu.c rename lib/arm/asm/{cp15.h => sysreg.h} (68%) create mode 100644 lib/arm64/asm/sysreg.hSignature · 1 line
1.8.3.1
Re: [Qemu-devel] [kvm-unit-tests PATCH v14 3/5] arm: Add PMU test
In reply to Wei HuangQuoted 96 lines · Wei Huang
From: Christopher Covington <[email protected]>
Beginning with a simple sanity check of the control register, add a unit test for the ARM Performance Monitors Unit (PMU).
Signed-off-by Christopher Covington <[email protected]>Signed-off-by Wei Huang <[email protected]>--- arm/Makefile.common | 3 +- arm/pmu.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ arm/unittests.cfg | 5 +++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 arm/pmu.carm/Makefile.common +2
-1@@ -12,7 +12,8 @@ endif tests-common = \ $(TEST_DIR)/selftest.flat \ $(TEST_DIR)/spinlock-test.flat \ - $(TEST_DIR)/pci-test.flat + $(TEST_DIR)/pci-test.flat \ + $(TEST_DIR)/pmu.flat all: test_casesarm/pmu.c new file +62
-0@@ -0,0 +1,93 @@ +/* +* Test the ARM Performance Monitors Unit (PMU). +* +* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved. +* Copyright (C) 2016, Red Hat Inc, Wei Huang <[email protected]> +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU Lesser General Public License version 2.1 and +* only version 2.1 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +* for more details. +*/ +#include "libcflat.h" +#include "asm/barrier.h" +#include "asm/sysreg.h" +#include "asm/processor.h" + +#define PMU_PMCR_N_SHIFT 11 +#define PMU_PMCR_N_MASK 0x1f +#define PMU_PMCR_ID_SHIFT 16 +#define PMU_PMCR_ID_MASK 0xff +#define PMU_PMCR_IMP_SHIFT 24 +#define PMU_PMCR_IMP_MASK 0xff + +#define ID_DFR0_PERFMON_SHIFT 24 +#define ID_DFR0_PERFMON_MASK 0xf + +static unsigned int pmu_version; +#if defined(__arm__) +#define PMCR __ACCESS_CP15(c9, 0, c12, 0) +#define ID_DFR0 __ACCESS_CP15(c0, 0, c1, 2) + +static inline uint32_t get_id_dfr0 (void ) {return read_sysreg (ID_DFR0 ); } +static inline uint32_t get_pmcr (void ) {return read_sysreg (PMCR ); } +#elif defined(__aarch64__) +static inline uint32_t get_id_dfr0 (void ) {return read_sysreg (id_dfr0_el1); } +static inline uint32_t get_pmcr (void ) {return read_sysreg (pmcr_el0); } +#endif + +/* +* As a simple sanity check on the PMCR_EL0, ensure the implementer field isn't +* null. Also print out a couple other interesting fields for diagnostic +* purposes. For example, as of fall 2016, QEMU TCG mode doesn't implement +* event counters and therefore reports zero event counters, but hopefully +* support for at least the instructions event will be added in the future and +* the reported number of event counters will become nonzero. +*/ +static bool check_pmcr (void ) +{ +uint32_t pmcr; + + pmcr =get_pmcr (); + +report_info ("PMU implementer/ID code/counters: 0x%x(\"%c\")/0x%x/%d" , + (pmcr >>PMU_PMCR_IMP_SHIFT ) &PMU_PMCR_IMP_MASK , + ((pmcr >>PMU_PMCR_IMP_SHIFT ) &PMU_PMCR_IMP_MASK ) ? :' ' , + (pmcr >>PMU_PMCR_ID_SHIFT ) &PMU_PMCR_ID_MASK , + (pmcr >>PMU_PMCR_N_SHIFT ) &PMU_PMCR_N_MASK ); +
arm/pmu.c line 72
Quoted 10 lines
arm/pmu.c new file +10
-0+return ((pmcr >>PMU_PMCR_IMP_SHIFT ) &PMU_PMCR_IMP_MASK ) !=0 ; +} + +/* Return FALSE if no PMU found, otherwise return TRUE */ +bool pmu_probe (void ) +{ +uint32_t dfr0; + +/* probe pmu version */ + dfr0 =get_id_dfr0 ();
Testing on ThunderX I found this register was zero. I looked at the spec and see "In an AArch64-only implementation, this register is UNKNOWN." Which means...
arm/pmu.c line 85
Quoted 13 lines
arm/pmu.c new file +13
-0+ pmu_version = (dfr0 >>ID_DFR0_PERFMON_SHIFT ) &ID_DFR0_PERFMON_MASK ; + +if (pmu_version) +report_info ("PMU version: %d" , pmu_version); + +return pmu_version; +} + +int main (void ) +{ +if (!pmu_probe ()) { +printf ("No PMU found, test skipped...\n" ); +return report_summary ();
...we don't want to do this.
We haven't heard from other reviewers yet (Andre, are you out there?), but if this is the only issue with this series, than rather than require another round, I'll just post a patch that fixes it later.
drew
Quoted 25 lines
arm/pmu.c new file +8
-0+ } + +report_prefix_push ("pmu" ); + +report ("Control register" ,check_pmcr ()); + +return report_summary (); +}arm/unittests.cfg +5
-0@@ -58,3 +58,8 @@ groups = selftest [pci-test] file = pci-test.flat groups = pci + +# Test PMU support +[pmu] +file = pmu.flat +groups = pmuSignature · 1 line
1.8.3.1