xref: /kvm-unit-tests/arm/pmu.c (revision 784ee933fa5f1c5d4a2818d2bf24dcc194e0f33d)
14244065bSChristopher Covington /*
24244065bSChristopher Covington  * Test the ARM Performance Monitors Unit (PMU).
34244065bSChristopher Covington  *
44244065bSChristopher Covington  * Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
54244065bSChristopher Covington  * Copyright (C) 2016, Red Hat Inc, Wei Huang <wei@redhat.com>
64244065bSChristopher Covington  *
74244065bSChristopher Covington  * This program is free software; you can redistribute it and/or modify it
84244065bSChristopher Covington  * under the terms of the GNU Lesser General Public License version 2.1 and
94244065bSChristopher Covington  * only version 2.1 as published by the Free Software Foundation.
104244065bSChristopher Covington  *
114244065bSChristopher Covington  * This program is distributed in the hope that it will be useful, but WITHOUT
124244065bSChristopher Covington  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
134244065bSChristopher Covington  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
144244065bSChristopher Covington  * for more details.
154244065bSChristopher Covington  */
164244065bSChristopher Covington #include "libcflat.h"
174c357610SAndrew Jones #include "errata.h"
184244065bSChristopher Covington #include "asm/barrier.h"
194244065bSChristopher Covington #include "asm/sysreg.h"
204244065bSChristopher Covington #include "asm/processor.h"
214244065bSChristopher Covington 
22d81bb7a3SChristopher Covington #define PMU_PMCR_E         (1 << 0)
23d81bb7a3SChristopher Covington #define PMU_PMCR_C         (1 << 2)
24d81bb7a3SChristopher Covington #define PMU_PMCR_LC        (1 << 6)
254244065bSChristopher Covington #define PMU_PMCR_N_SHIFT   11
264244065bSChristopher Covington #define PMU_PMCR_N_MASK    0x1f
274244065bSChristopher Covington #define PMU_PMCR_ID_SHIFT  16
284244065bSChristopher Covington #define PMU_PMCR_ID_MASK   0xff
294244065bSChristopher Covington #define PMU_PMCR_IMP_SHIFT 24
304244065bSChristopher Covington #define PMU_PMCR_IMP_MASK  0xff
314244065bSChristopher Covington 
32d81bb7a3SChristopher Covington #define PMU_CYCLE_IDX      31
33d81bb7a3SChristopher Covington 
34d81bb7a3SChristopher Covington #define NR_SAMPLES 10
35d81bb7a3SChristopher Covington 
368f747a85SEric Auger struct pmu {
378f747a85SEric Auger 	unsigned int version;
388f747a85SEric Auger 	unsigned int nb_implemented_counters;
398f747a85SEric Auger 	uint32_t pmcr_ro;
408f747a85SEric Auger };
418f747a85SEric Auger 
428f747a85SEric Auger static struct pmu pmu;
438f747a85SEric Auger 
444244065bSChristopher Covington #if defined(__arm__)
45098add54SAndrew Jones #define ID_DFR0_PERFMON_SHIFT 24
46098add54SAndrew Jones #define ID_DFR0_PERFMON_MASK  0xf
47098add54SAndrew Jones 
48*784ee933SEric Auger #define ID_DFR0_PMU_NOTIMPL	0b0000
49*784ee933SEric Auger #define ID_DFR0_PMU_V1		0b0001
50*784ee933SEric Auger #define ID_DFR0_PMU_V2		0b0010
51*784ee933SEric Auger #define ID_DFR0_PMU_V3		0b0011
52*784ee933SEric Auger #define ID_DFR0_PMU_V3_8_1	0b0100
53*784ee933SEric Auger #define ID_DFR0_PMU_V3_8_4	0b0101
54*784ee933SEric Auger #define ID_DFR0_PMU_V3_8_5	0b0110
55*784ee933SEric Auger #define ID_DFR0_PMU_IMPDEF	0b1111
56*784ee933SEric Auger 
574244065bSChristopher Covington #define PMCR         __ACCESS_CP15(c9, 0, c12, 0)
584244065bSChristopher Covington #define ID_DFR0      __ACCESS_CP15(c0, 0, c1, 2)
59d81bb7a3SChristopher Covington #define PMSELR       __ACCESS_CP15(c9, 0, c12, 5)
60d81bb7a3SChristopher Covington #define PMXEVTYPER   __ACCESS_CP15(c9, 0, c13, 1)
61d81bb7a3SChristopher Covington #define PMCNTENSET   __ACCESS_CP15(c9, 0, c12, 1)
62d81bb7a3SChristopher Covington #define PMCCNTR32    __ACCESS_CP15(c9, 0, c13, 0)
63d81bb7a3SChristopher Covington #define PMCCNTR64    __ACCESS_CP15_64(0, c9)
644244065bSChristopher Covington 
654244065bSChristopher Covington static inline uint32_t get_id_dfr0(void) { return read_sysreg(ID_DFR0); }
664244065bSChristopher Covington static inline uint32_t get_pmcr(void) { return read_sysreg(PMCR); }
67d81bb7a3SChristopher Covington static inline void set_pmcr(uint32_t v) { write_sysreg(v, PMCR); }
68d81bb7a3SChristopher Covington static inline void set_pmcntenset(uint32_t v) { write_sysreg(v, PMCNTENSET); }
69d81bb7a3SChristopher Covington 
70098add54SAndrew Jones static inline uint8_t get_pmu_version(void)
71098add54SAndrew Jones {
72098add54SAndrew Jones 	return (get_id_dfr0() >> ID_DFR0_PERFMON_SHIFT) & ID_DFR0_PERFMON_MASK;
73098add54SAndrew Jones }
74098add54SAndrew Jones 
75d81bb7a3SChristopher Covington static inline uint64_t get_pmccntr(void)
76d81bb7a3SChristopher Covington {
77d81bb7a3SChristopher Covington 	return read_sysreg(PMCCNTR32);
78d81bb7a3SChristopher Covington }
79d81bb7a3SChristopher Covington 
808f76a347SChristopher Covington static inline void set_pmccntr(uint64_t value)
818f76a347SChristopher Covington {
828f76a347SChristopher Covington 	write_sysreg(value & 0xffffffff, PMCCNTR32);
838f76a347SChristopher Covington }
848f76a347SChristopher Covington 
85d81bb7a3SChristopher Covington /* PMCCFILTR is an obsolete name for PMXEVTYPER31 in ARMv7 */
86d81bb7a3SChristopher Covington static inline void set_pmccfiltr(uint32_t value)
87d81bb7a3SChristopher Covington {
88d81bb7a3SChristopher Covington 	write_sysreg(PMU_CYCLE_IDX, PMSELR);
89d81bb7a3SChristopher Covington 	write_sysreg(value, PMXEVTYPER);
90d81bb7a3SChristopher Covington 	isb();
91d81bb7a3SChristopher Covington }
928f76a347SChristopher Covington 
938f76a347SChristopher Covington /*
948f76a347SChristopher Covington  * Extra instructions inserted by the compiler would be difficult to compensate
958f76a347SChristopher Covington  * for, so hand assemble everything between, and including, the PMCR accesses
968f76a347SChristopher Covington  * to start and stop counting. isb instructions were inserted to make sure
978f76a347SChristopher Covington  * pmccntr read after this function returns the exact instructions executed in
988f76a347SChristopher Covington  * the controlled block. Total instrs = isb + mcr + 2*loop = 2 + 2*loop.
998f76a347SChristopher Covington  */
1008f76a347SChristopher Covington static inline void precise_instrs_loop(int loop, uint32_t pmcr)
1018f76a347SChristopher Covington {
1028f76a347SChristopher Covington 	asm volatile(
1038f76a347SChristopher Covington 	"	mcr	p15, 0, %[pmcr], c9, c12, 0\n"
1048f76a347SChristopher Covington 	"	isb\n"
1058f76a347SChristopher Covington 	"1:	subs	%[loop], %[loop], #1\n"
1068f76a347SChristopher Covington 	"	bgt	1b\n"
1078f76a347SChristopher Covington 	"	mcr	p15, 0, %[z], c9, c12, 0\n"
1088f76a347SChristopher Covington 	"	isb\n"
1098f76a347SChristopher Covington 	: [loop] "+r" (loop)
1108f76a347SChristopher Covington 	: [pmcr] "r" (pmcr), [z] "r" (0)
1118f76a347SChristopher Covington 	: "cc");
1128f76a347SChristopher Covington }
1134244065bSChristopher Covington #elif defined(__aarch64__)
114098add54SAndrew Jones #define ID_AA64DFR0_PERFMON_SHIFT 8
115098add54SAndrew Jones #define ID_AA64DFR0_PERFMON_MASK  0xf
116098add54SAndrew Jones 
117*784ee933SEric Auger #define ID_DFR0_PMU_NOTIMPL	0b0000
118*784ee933SEric Auger #define ID_DFR0_PMU_V3		0b0001
119*784ee933SEric Auger #define ID_DFR0_PMU_V3_8_1	0b0100
120*784ee933SEric Auger #define ID_DFR0_PMU_V3_8_4	0b0101
121*784ee933SEric Auger #define ID_DFR0_PMU_V3_8_5	0b0110
122*784ee933SEric Auger #define ID_DFR0_PMU_IMPDEF	0b1111
123*784ee933SEric Auger 
124098add54SAndrew Jones static inline uint32_t get_id_aa64dfr0(void) { return read_sysreg(id_aa64dfr0_el1); }
1254244065bSChristopher Covington static inline uint32_t get_pmcr(void) { return read_sysreg(pmcr_el0); }
126d81bb7a3SChristopher Covington static inline void set_pmcr(uint32_t v) { write_sysreg(v, pmcr_el0); }
127d81bb7a3SChristopher Covington static inline uint64_t get_pmccntr(void) { return read_sysreg(pmccntr_el0); }
1288f76a347SChristopher Covington static inline void set_pmccntr(uint64_t v) { write_sysreg(v, pmccntr_el0); }
129d81bb7a3SChristopher Covington static inline void set_pmcntenset(uint32_t v) { write_sysreg(v, pmcntenset_el0); }
130d81bb7a3SChristopher Covington static inline void set_pmccfiltr(uint32_t v) { write_sysreg(v, pmccfiltr_el0); }
1318f76a347SChristopher Covington 
132098add54SAndrew Jones static inline uint8_t get_pmu_version(void)
133098add54SAndrew Jones {
134098add54SAndrew Jones 	uint8_t ver = (get_id_aa64dfr0() >> ID_AA64DFR0_PERFMON_SHIFT) & ID_AA64DFR0_PERFMON_MASK;
135*784ee933SEric Auger 	return ver;
136098add54SAndrew Jones }
137098add54SAndrew Jones 
1388f76a347SChristopher Covington /*
1398f76a347SChristopher Covington  * Extra instructions inserted by the compiler would be difficult to compensate
1408f76a347SChristopher Covington  * for, so hand assemble everything between, and including, the PMCR accesses
1418f76a347SChristopher Covington  * to start and stop counting. isb instructions are inserted to make sure
1428f76a347SChristopher Covington  * pmccntr read after this function returns the exact instructions executed
1438f76a347SChristopher Covington  * in the controlled block. Total instrs = isb + msr + 2*loop = 2 + 2*loop.
1448f76a347SChristopher Covington  */
1458f76a347SChristopher Covington static inline void precise_instrs_loop(int loop, uint32_t pmcr)
1468f76a347SChristopher Covington {
1478f76a347SChristopher Covington 	asm volatile(
1488f76a347SChristopher Covington 	"	msr	pmcr_el0, %[pmcr]\n"
1498f76a347SChristopher Covington 	"	isb\n"
1508f76a347SChristopher Covington 	"1:	subs	%[loop], %[loop], #1\n"
1518f76a347SChristopher Covington 	"	b.gt	1b\n"
1528f76a347SChristopher Covington 	"	msr	pmcr_el0, xzr\n"
1538f76a347SChristopher Covington 	"	isb\n"
1548f76a347SChristopher Covington 	: [loop] "+r" (loop)
1558f76a347SChristopher Covington 	: [pmcr] "r" (pmcr)
1568f76a347SChristopher Covington 	: "cc");
1578f76a347SChristopher Covington }
1584244065bSChristopher Covington #endif
1594244065bSChristopher Covington 
1604244065bSChristopher Covington /*
161d81bb7a3SChristopher Covington  * Ensure that the cycle counter progresses between back-to-back reads.
162d81bb7a3SChristopher Covington  */
163d81bb7a3SChristopher Covington static bool check_cycles_increase(void)
164d81bb7a3SChristopher Covington {
165d81bb7a3SChristopher Covington 	bool success = true;
166d81bb7a3SChristopher Covington 
167d81bb7a3SChristopher Covington 	/* init before event access, this test only cares about cycle count */
168d81bb7a3SChristopher Covington 	set_pmcntenset(1 << PMU_CYCLE_IDX);
169d81bb7a3SChristopher Covington 	set_pmccfiltr(0); /* count cycles in EL0, EL1, but not EL2 */
170d81bb7a3SChristopher Covington 
171d81bb7a3SChristopher Covington 	set_pmcr(get_pmcr() | PMU_PMCR_LC | PMU_PMCR_C | PMU_PMCR_E);
172d81bb7a3SChristopher Covington 
173d81bb7a3SChristopher Covington 	for (int i = 0; i < NR_SAMPLES; i++) {
174d81bb7a3SChristopher Covington 		uint64_t a, b;
175d81bb7a3SChristopher Covington 
176d81bb7a3SChristopher Covington 		a = get_pmccntr();
177d81bb7a3SChristopher Covington 		b = get_pmccntr();
178d81bb7a3SChristopher Covington 
179d81bb7a3SChristopher Covington 		if (a >= b) {
180d81bb7a3SChristopher Covington 			printf("Read %"PRId64" then %"PRId64".\n", a, b);
181d81bb7a3SChristopher Covington 			success = false;
182d81bb7a3SChristopher Covington 			break;
183d81bb7a3SChristopher Covington 		}
184d81bb7a3SChristopher Covington 	}
185d81bb7a3SChristopher Covington 
186d81bb7a3SChristopher Covington 	set_pmcr(get_pmcr() & ~PMU_PMCR_E);
187d81bb7a3SChristopher Covington 
188d81bb7a3SChristopher Covington 	return success;
189d81bb7a3SChristopher Covington }
190d81bb7a3SChristopher Covington 
1918f76a347SChristopher Covington /*
1928f76a347SChristopher Covington  * Execute a known number of guest instructions. Only even instruction counts
1938f76a347SChristopher Covington  * greater than or equal to 4 are supported by the in-line assembly code. The
1948f76a347SChristopher Covington  * control register (PMCR_EL0) is initialized with the provided value (allowing
1958f76a347SChristopher Covington  * for example for the cycle counter or event counters to be reset). At the end
1968f76a347SChristopher Covington  * of the exact instruction loop, zero is written to PMCR_EL0 to disable
1978f76a347SChristopher Covington  * counting, allowing the cycle counter or event counters to be read at the
1988f76a347SChristopher Covington  * leisure of the calling code.
1998f76a347SChristopher Covington  */
2008f76a347SChristopher Covington static void measure_instrs(int num, uint32_t pmcr)
2018f76a347SChristopher Covington {
2028f76a347SChristopher Covington 	int loop = (num - 2) / 2;
2038f76a347SChristopher Covington 
2048f76a347SChristopher Covington 	assert(num >= 4 && ((num - 2) % 2 == 0));
2058f76a347SChristopher Covington 	precise_instrs_loop(loop, pmcr);
2068f76a347SChristopher Covington }
2078f76a347SChristopher Covington 
2088f76a347SChristopher Covington /*
2098f76a347SChristopher Covington  * Measure cycle counts for various known instruction counts. Ensure that the
2108f76a347SChristopher Covington  * cycle counter progresses (similar to check_cycles_increase() but with more
2118f76a347SChristopher Covington  * instructions and using reset and stop controls). If supplied a positive,
2128f76a347SChristopher Covington  * nonzero CPI parameter, it also strictly checks that every measurement matches
2138f76a347SChristopher Covington  * it. Strict CPI checking is used to test -icount mode.
2148f76a347SChristopher Covington  */
2158f76a347SChristopher Covington static bool check_cpi(int cpi)
2168f76a347SChristopher Covington {
2178f76a347SChristopher Covington 	uint32_t pmcr = get_pmcr() | PMU_PMCR_LC | PMU_PMCR_C | PMU_PMCR_E;
2188f76a347SChristopher Covington 
2198f76a347SChristopher Covington 	/* init before event access, this test only cares about cycle count */
2208f76a347SChristopher Covington 	set_pmcntenset(1 << PMU_CYCLE_IDX);
2218f76a347SChristopher Covington 	set_pmccfiltr(0); /* count cycles in EL0, EL1, but not EL2 */
2228f76a347SChristopher Covington 
2238f76a347SChristopher Covington 	if (cpi > 0)
2248f76a347SChristopher Covington 		printf("Checking for CPI=%d.\n", cpi);
2258f76a347SChristopher Covington 	printf("instrs : cycles0 cycles1 ...\n");
2268f76a347SChristopher Covington 
2278f76a347SChristopher Covington 	for (unsigned int i = 4; i < 300; i += 32) {
2288f76a347SChristopher Covington 		uint64_t avg, sum = 0;
2298f76a347SChristopher Covington 
2308f76a347SChristopher Covington 		printf("%4d:", i);
2318f76a347SChristopher Covington 		for (int j = 0; j < NR_SAMPLES; j++) {
2328f76a347SChristopher Covington 			uint64_t cycles;
2338f76a347SChristopher Covington 
2348f76a347SChristopher Covington 			set_pmccntr(0);
2358f76a347SChristopher Covington 			measure_instrs(i, pmcr);
2368f76a347SChristopher Covington 			cycles = get_pmccntr();
2378f76a347SChristopher Covington 			printf(" %4"PRId64"", cycles);
2388f76a347SChristopher Covington 
2398f76a347SChristopher Covington 			if (!cycles) {
2408f76a347SChristopher Covington 				printf("\ncycles not incrementing!\n");
2418f76a347SChristopher Covington 				return false;
2428f76a347SChristopher Covington 			} else if (cpi > 0 && cycles != i * cpi) {
2438f76a347SChristopher Covington 				printf("\nunexpected cycle count received!\n");
2448f76a347SChristopher Covington 				return false;
2458f76a347SChristopher Covington 			} else if ((cycles >> 32) != 0) {
2468f76a347SChristopher Covington 				/* The cycles taken by the loop above should
2478f76a347SChristopher Covington 				 * fit in 32 bits easily. We check the upper
2488f76a347SChristopher Covington 				 * 32 bits of the cycle counter to make sure
2498f76a347SChristopher Covington 				 * there is no supprise. */
2508f76a347SChristopher Covington 				printf("\ncycle count bigger than 32bit!\n");
2518f76a347SChristopher Covington 				return false;
2528f76a347SChristopher Covington 			}
2538f76a347SChristopher Covington 
2548f76a347SChristopher Covington 			sum += cycles;
2558f76a347SChristopher Covington 		}
2568f76a347SChristopher Covington 		avg = sum / NR_SAMPLES;
2578f76a347SChristopher Covington 		printf(" avg=%-4"PRId64" %s=%-3"PRId64"\n", avg,
2588f76a347SChristopher Covington 		       (avg >= i) ? "cpi" : "ipc",
2598f76a347SChristopher Covington 		       (avg >= i) ? avg / i : i / avg);
2608f76a347SChristopher Covington 	}
2618f76a347SChristopher Covington 
2628f76a347SChristopher Covington 	return true;
2638f76a347SChristopher Covington }
2648f76a347SChristopher Covington 
2654c357610SAndrew Jones static void pmccntr64_test(void)
2664c357610SAndrew Jones {
2674c357610SAndrew Jones #ifdef __arm__
268*784ee933SEric Auger 	if (pmu.version == ID_DFR0_PMU_V3) {
2694c357610SAndrew Jones 		if (ERRATA(9e3f7a296940)) {
2704c357610SAndrew Jones 			write_sysreg(0xdead, PMCCNTR64);
271a299895bSThomas Huth 			report(read_sysreg(PMCCNTR64) == 0xdead, "pmccntr64");
2724c357610SAndrew Jones 		} else
2734c357610SAndrew Jones 			report_skip("Skipping unsafe pmccntr64 test. Set ERRATA_9e3f7a296940=y to enable.");
2744c357610SAndrew Jones 	}
2754c357610SAndrew Jones #endif
2764c357610SAndrew Jones }
2774c357610SAndrew Jones 
2784244065bSChristopher Covington /* Return FALSE if no PMU found, otherwise return TRUE */
27923b8916bSThomas Huth static bool pmu_probe(void)
2804244065bSChristopher Covington {
281*784ee933SEric Auger 	uint32_t pmcr = get_pmcr();
282eff8f161SEric Auger 
2838f747a85SEric Auger 	pmu.version = get_pmu_version();
284*784ee933SEric Auger 	if (pmu.version == ID_DFR0_PMU_NOTIMPL || pmu.version == ID_DFR0_PMU_IMPDEF)
285eff8f161SEric Auger 		return false;
286eff8f161SEric Auger 
287*784ee933SEric Auger 	report_info("PMU version: 0x%x", pmu.version);
288eff8f161SEric Auger 
289eff8f161SEric Auger 	pmcr = get_pmcr();
2908f747a85SEric Auger 	report_info("PMU implementer/ID code: %#x(\"%c\")/%#x",
291eff8f161SEric Auger 		    (pmcr >> PMU_PMCR_IMP_SHIFT) & PMU_PMCR_IMP_MASK,
292eff8f161SEric Auger 		    ((pmcr >> PMU_PMCR_IMP_SHIFT) & PMU_PMCR_IMP_MASK) ? : ' ',
2938f747a85SEric Auger 		    (pmcr >> PMU_PMCR_ID_SHIFT) & PMU_PMCR_ID_MASK);
2948f747a85SEric Auger 
2958f747a85SEric Auger 	/* store read-only and RES0 fields of the PMCR bottom-half*/
2968f747a85SEric Auger 	pmu.pmcr_ro = pmcr & 0xFFFFFF00;
2978f747a85SEric Auger 	pmu.nb_implemented_counters =
2988f747a85SEric Auger 		(pmcr >> PMU_PMCR_N_SHIFT) & PMU_PMCR_N_MASK;
2998f747a85SEric Auger 	report_info("Implements %d event counters",
3008f747a85SEric Auger 		    pmu.nb_implemented_counters);
301eff8f161SEric Auger 
302eff8f161SEric Auger 	return true;
3034244065bSChristopher Covington }
3044244065bSChristopher Covington 
3058f76a347SChristopher Covington int main(int argc, char *argv[])
3064244065bSChristopher Covington {
3078f76a347SChristopher Covington 	int cpi = 0;
3088f76a347SChristopher Covington 
3094244065bSChristopher Covington 	if (!pmu_probe()) {
3104244065bSChristopher Covington 		printf("No PMU found, test skipped...\n");
3114244065bSChristopher Covington 		return report_summary();
3124244065bSChristopher Covington 	}
3134244065bSChristopher Covington 
31457ec1086SEric Auger 	if (argc < 2)
31557ec1086SEric Auger 		report_abort("no test specified");
31657ec1086SEric Auger 
3174244065bSChristopher Covington 	report_prefix_push("pmu");
3184244065bSChristopher Covington 
31957ec1086SEric Auger 	if (strcmp(argv[1], "cycle-counter") == 0) {
32057ec1086SEric Auger 		report_prefix_push(argv[1]);
32157ec1086SEric Auger 		if (argc > 2)
32257ec1086SEric Auger 			cpi = atol(argv[2]);
323a299895bSThomas Huth 		report(check_cycles_increase(),
324a299895bSThomas Huth 		       "Monotonically increasing cycle count");
325a299895bSThomas Huth 		report(check_cpi(cpi), "Cycle/instruction ratio");
3264c357610SAndrew Jones 		pmccntr64_test();
32757ec1086SEric Auger 		report_prefix_pop();
32857ec1086SEric Auger 	} else {
32957ec1086SEric Auger 		report_abort("Unknown sub-test '%s'", argv[1]);
33057ec1086SEric Auger 	}
3314c357610SAndrew Jones 
3324244065bSChristopher Covington 	return report_summary();
3334244065bSChristopher Covington }
334