xref: /kvm-unit-tests/arm/pmu.c (revision eff8f161869348936702c1767a5a6df93e846205)
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 
364244065bSChristopher Covington static unsigned int pmu_version;
374244065bSChristopher Covington #if defined(__arm__)
38098add54SAndrew Jones #define ID_DFR0_PERFMON_SHIFT 24
39098add54SAndrew Jones #define ID_DFR0_PERFMON_MASK  0xf
40098add54SAndrew Jones 
414244065bSChristopher Covington #define PMCR         __ACCESS_CP15(c9, 0, c12, 0)
424244065bSChristopher Covington #define ID_DFR0      __ACCESS_CP15(c0, 0, c1, 2)
43d81bb7a3SChristopher Covington #define PMSELR       __ACCESS_CP15(c9, 0, c12, 5)
44d81bb7a3SChristopher Covington #define PMXEVTYPER   __ACCESS_CP15(c9, 0, c13, 1)
45d81bb7a3SChristopher Covington #define PMCNTENSET   __ACCESS_CP15(c9, 0, c12, 1)
46d81bb7a3SChristopher Covington #define PMCCNTR32    __ACCESS_CP15(c9, 0, c13, 0)
47d81bb7a3SChristopher Covington #define PMCCNTR64    __ACCESS_CP15_64(0, c9)
484244065bSChristopher Covington 
494244065bSChristopher Covington static inline uint32_t get_id_dfr0(void) { return read_sysreg(ID_DFR0); }
504244065bSChristopher Covington static inline uint32_t get_pmcr(void) { return read_sysreg(PMCR); }
51d81bb7a3SChristopher Covington static inline void set_pmcr(uint32_t v) { write_sysreg(v, PMCR); }
52d81bb7a3SChristopher Covington static inline void set_pmcntenset(uint32_t v) { write_sysreg(v, PMCNTENSET); }
53d81bb7a3SChristopher Covington 
54098add54SAndrew Jones static inline uint8_t get_pmu_version(void)
55098add54SAndrew Jones {
56098add54SAndrew Jones 	return (get_id_dfr0() >> ID_DFR0_PERFMON_SHIFT) & ID_DFR0_PERFMON_MASK;
57098add54SAndrew Jones }
58098add54SAndrew Jones 
59d81bb7a3SChristopher Covington static inline uint64_t get_pmccntr(void)
60d81bb7a3SChristopher Covington {
61d81bb7a3SChristopher Covington 	return read_sysreg(PMCCNTR32);
62d81bb7a3SChristopher Covington }
63d81bb7a3SChristopher Covington 
648f76a347SChristopher Covington static inline void set_pmccntr(uint64_t value)
658f76a347SChristopher Covington {
668f76a347SChristopher Covington 	write_sysreg(value & 0xffffffff, PMCCNTR32);
678f76a347SChristopher Covington }
688f76a347SChristopher Covington 
69d81bb7a3SChristopher Covington /* PMCCFILTR is an obsolete name for PMXEVTYPER31 in ARMv7 */
70d81bb7a3SChristopher Covington static inline void set_pmccfiltr(uint32_t value)
71d81bb7a3SChristopher Covington {
72d81bb7a3SChristopher Covington 	write_sysreg(PMU_CYCLE_IDX, PMSELR);
73d81bb7a3SChristopher Covington 	write_sysreg(value, PMXEVTYPER);
74d81bb7a3SChristopher Covington 	isb();
75d81bb7a3SChristopher Covington }
768f76a347SChristopher Covington 
778f76a347SChristopher Covington /*
788f76a347SChristopher Covington  * Extra instructions inserted by the compiler would be difficult to compensate
798f76a347SChristopher Covington  * for, so hand assemble everything between, and including, the PMCR accesses
808f76a347SChristopher Covington  * to start and stop counting. isb instructions were inserted to make sure
818f76a347SChristopher Covington  * pmccntr read after this function returns the exact instructions executed in
828f76a347SChristopher Covington  * the controlled block. Total instrs = isb + mcr + 2*loop = 2 + 2*loop.
838f76a347SChristopher Covington  */
848f76a347SChristopher Covington static inline void precise_instrs_loop(int loop, uint32_t pmcr)
858f76a347SChristopher Covington {
868f76a347SChristopher Covington 	asm volatile(
878f76a347SChristopher Covington 	"	mcr	p15, 0, %[pmcr], c9, c12, 0\n"
888f76a347SChristopher Covington 	"	isb\n"
898f76a347SChristopher Covington 	"1:	subs	%[loop], %[loop], #1\n"
908f76a347SChristopher Covington 	"	bgt	1b\n"
918f76a347SChristopher Covington 	"	mcr	p15, 0, %[z], c9, c12, 0\n"
928f76a347SChristopher Covington 	"	isb\n"
938f76a347SChristopher Covington 	: [loop] "+r" (loop)
948f76a347SChristopher Covington 	: [pmcr] "r" (pmcr), [z] "r" (0)
958f76a347SChristopher Covington 	: "cc");
968f76a347SChristopher Covington }
974244065bSChristopher Covington #elif defined(__aarch64__)
98098add54SAndrew Jones #define ID_AA64DFR0_PERFMON_SHIFT 8
99098add54SAndrew Jones #define ID_AA64DFR0_PERFMON_MASK  0xf
100098add54SAndrew Jones 
101098add54SAndrew Jones static inline uint32_t get_id_aa64dfr0(void) { return read_sysreg(id_aa64dfr0_el1); }
1024244065bSChristopher Covington static inline uint32_t get_pmcr(void) { return read_sysreg(pmcr_el0); }
103d81bb7a3SChristopher Covington static inline void set_pmcr(uint32_t v) { write_sysreg(v, pmcr_el0); }
104d81bb7a3SChristopher Covington static inline uint64_t get_pmccntr(void) { return read_sysreg(pmccntr_el0); }
1058f76a347SChristopher Covington static inline void set_pmccntr(uint64_t v) { write_sysreg(v, pmccntr_el0); }
106d81bb7a3SChristopher Covington static inline void set_pmcntenset(uint32_t v) { write_sysreg(v, pmcntenset_el0); }
107d81bb7a3SChristopher Covington static inline void set_pmccfiltr(uint32_t v) { write_sysreg(v, pmccfiltr_el0); }
1088f76a347SChristopher Covington 
109098add54SAndrew Jones static inline uint8_t get_pmu_version(void)
110098add54SAndrew Jones {
111098add54SAndrew Jones 	uint8_t ver = (get_id_aa64dfr0() >> ID_AA64DFR0_PERFMON_SHIFT) & ID_AA64DFR0_PERFMON_MASK;
112098add54SAndrew Jones 	return ver == 1 ? 3 : ver;
113098add54SAndrew Jones }
114098add54SAndrew Jones 
1158f76a347SChristopher Covington /*
1168f76a347SChristopher Covington  * Extra instructions inserted by the compiler would be difficult to compensate
1178f76a347SChristopher Covington  * for, so hand assemble everything between, and including, the PMCR accesses
1188f76a347SChristopher Covington  * to start and stop counting. isb instructions are inserted to make sure
1198f76a347SChristopher Covington  * pmccntr read after this function returns the exact instructions executed
1208f76a347SChristopher Covington  * in the controlled block. Total instrs = isb + msr + 2*loop = 2 + 2*loop.
1218f76a347SChristopher Covington  */
1228f76a347SChristopher Covington static inline void precise_instrs_loop(int loop, uint32_t pmcr)
1238f76a347SChristopher Covington {
1248f76a347SChristopher Covington 	asm volatile(
1258f76a347SChristopher Covington 	"	msr	pmcr_el0, %[pmcr]\n"
1268f76a347SChristopher Covington 	"	isb\n"
1278f76a347SChristopher Covington 	"1:	subs	%[loop], %[loop], #1\n"
1288f76a347SChristopher Covington 	"	b.gt	1b\n"
1298f76a347SChristopher Covington 	"	msr	pmcr_el0, xzr\n"
1308f76a347SChristopher Covington 	"	isb\n"
1318f76a347SChristopher Covington 	: [loop] "+r" (loop)
1328f76a347SChristopher Covington 	: [pmcr] "r" (pmcr)
1338f76a347SChristopher Covington 	: "cc");
1348f76a347SChristopher Covington }
1354244065bSChristopher Covington #endif
1364244065bSChristopher Covington 
1374244065bSChristopher Covington /*
138d81bb7a3SChristopher Covington  * Ensure that the cycle counter progresses between back-to-back reads.
139d81bb7a3SChristopher Covington  */
140d81bb7a3SChristopher Covington static bool check_cycles_increase(void)
141d81bb7a3SChristopher Covington {
142d81bb7a3SChristopher Covington 	bool success = true;
143d81bb7a3SChristopher Covington 
144d81bb7a3SChristopher Covington 	/* init before event access, this test only cares about cycle count */
145d81bb7a3SChristopher Covington 	set_pmcntenset(1 << PMU_CYCLE_IDX);
146d81bb7a3SChristopher Covington 	set_pmccfiltr(0); /* count cycles in EL0, EL1, but not EL2 */
147d81bb7a3SChristopher Covington 
148d81bb7a3SChristopher Covington 	set_pmcr(get_pmcr() | PMU_PMCR_LC | PMU_PMCR_C | PMU_PMCR_E);
149d81bb7a3SChristopher Covington 
150d81bb7a3SChristopher Covington 	for (int i = 0; i < NR_SAMPLES; i++) {
151d81bb7a3SChristopher Covington 		uint64_t a, b;
152d81bb7a3SChristopher Covington 
153d81bb7a3SChristopher Covington 		a = get_pmccntr();
154d81bb7a3SChristopher Covington 		b = get_pmccntr();
155d81bb7a3SChristopher Covington 
156d81bb7a3SChristopher Covington 		if (a >= b) {
157d81bb7a3SChristopher Covington 			printf("Read %"PRId64" then %"PRId64".\n", a, b);
158d81bb7a3SChristopher Covington 			success = false;
159d81bb7a3SChristopher Covington 			break;
160d81bb7a3SChristopher Covington 		}
161d81bb7a3SChristopher Covington 	}
162d81bb7a3SChristopher Covington 
163d81bb7a3SChristopher Covington 	set_pmcr(get_pmcr() & ~PMU_PMCR_E);
164d81bb7a3SChristopher Covington 
165d81bb7a3SChristopher Covington 	return success;
166d81bb7a3SChristopher Covington }
167d81bb7a3SChristopher Covington 
1688f76a347SChristopher Covington /*
1698f76a347SChristopher Covington  * Execute a known number of guest instructions. Only even instruction counts
1708f76a347SChristopher Covington  * greater than or equal to 4 are supported by the in-line assembly code. The
1718f76a347SChristopher Covington  * control register (PMCR_EL0) is initialized with the provided value (allowing
1728f76a347SChristopher Covington  * for example for the cycle counter or event counters to be reset). At the end
1738f76a347SChristopher Covington  * of the exact instruction loop, zero is written to PMCR_EL0 to disable
1748f76a347SChristopher Covington  * counting, allowing the cycle counter or event counters to be read at the
1758f76a347SChristopher Covington  * leisure of the calling code.
1768f76a347SChristopher Covington  */
1778f76a347SChristopher Covington static void measure_instrs(int num, uint32_t pmcr)
1788f76a347SChristopher Covington {
1798f76a347SChristopher Covington 	int loop = (num - 2) / 2;
1808f76a347SChristopher Covington 
1818f76a347SChristopher Covington 	assert(num >= 4 && ((num - 2) % 2 == 0));
1828f76a347SChristopher Covington 	precise_instrs_loop(loop, pmcr);
1838f76a347SChristopher Covington }
1848f76a347SChristopher Covington 
1858f76a347SChristopher Covington /*
1868f76a347SChristopher Covington  * Measure cycle counts for various known instruction counts. Ensure that the
1878f76a347SChristopher Covington  * cycle counter progresses (similar to check_cycles_increase() but with more
1888f76a347SChristopher Covington  * instructions and using reset and stop controls). If supplied a positive,
1898f76a347SChristopher Covington  * nonzero CPI parameter, it also strictly checks that every measurement matches
1908f76a347SChristopher Covington  * it. Strict CPI checking is used to test -icount mode.
1918f76a347SChristopher Covington  */
1928f76a347SChristopher Covington static bool check_cpi(int cpi)
1938f76a347SChristopher Covington {
1948f76a347SChristopher Covington 	uint32_t pmcr = get_pmcr() | PMU_PMCR_LC | PMU_PMCR_C | PMU_PMCR_E;
1958f76a347SChristopher Covington 
1968f76a347SChristopher Covington 	/* init before event access, this test only cares about cycle count */
1978f76a347SChristopher Covington 	set_pmcntenset(1 << PMU_CYCLE_IDX);
1988f76a347SChristopher Covington 	set_pmccfiltr(0); /* count cycles in EL0, EL1, but not EL2 */
1998f76a347SChristopher Covington 
2008f76a347SChristopher Covington 	if (cpi > 0)
2018f76a347SChristopher Covington 		printf("Checking for CPI=%d.\n", cpi);
2028f76a347SChristopher Covington 	printf("instrs : cycles0 cycles1 ...\n");
2038f76a347SChristopher Covington 
2048f76a347SChristopher Covington 	for (unsigned int i = 4; i < 300; i += 32) {
2058f76a347SChristopher Covington 		uint64_t avg, sum = 0;
2068f76a347SChristopher Covington 
2078f76a347SChristopher Covington 		printf("%4d:", i);
2088f76a347SChristopher Covington 		for (int j = 0; j < NR_SAMPLES; j++) {
2098f76a347SChristopher Covington 			uint64_t cycles;
2108f76a347SChristopher Covington 
2118f76a347SChristopher Covington 			set_pmccntr(0);
2128f76a347SChristopher Covington 			measure_instrs(i, pmcr);
2138f76a347SChristopher Covington 			cycles = get_pmccntr();
2148f76a347SChristopher Covington 			printf(" %4"PRId64"", cycles);
2158f76a347SChristopher Covington 
2168f76a347SChristopher Covington 			if (!cycles) {
2178f76a347SChristopher Covington 				printf("\ncycles not incrementing!\n");
2188f76a347SChristopher Covington 				return false;
2198f76a347SChristopher Covington 			} else if (cpi > 0 && cycles != i * cpi) {
2208f76a347SChristopher Covington 				printf("\nunexpected cycle count received!\n");
2218f76a347SChristopher Covington 				return false;
2228f76a347SChristopher Covington 			} else if ((cycles >> 32) != 0) {
2238f76a347SChristopher Covington 				/* The cycles taken by the loop above should
2248f76a347SChristopher Covington 				 * fit in 32 bits easily. We check the upper
2258f76a347SChristopher Covington 				 * 32 bits of the cycle counter to make sure
2268f76a347SChristopher Covington 				 * there is no supprise. */
2278f76a347SChristopher Covington 				printf("\ncycle count bigger than 32bit!\n");
2288f76a347SChristopher Covington 				return false;
2298f76a347SChristopher Covington 			}
2308f76a347SChristopher Covington 
2318f76a347SChristopher Covington 			sum += cycles;
2328f76a347SChristopher Covington 		}
2338f76a347SChristopher Covington 		avg = sum / NR_SAMPLES;
2348f76a347SChristopher Covington 		printf(" avg=%-4"PRId64" %s=%-3"PRId64"\n", avg,
2358f76a347SChristopher Covington 		       (avg >= i) ? "cpi" : "ipc",
2368f76a347SChristopher Covington 		       (avg >= i) ? avg / i : i / avg);
2378f76a347SChristopher Covington 	}
2388f76a347SChristopher Covington 
2398f76a347SChristopher Covington 	return true;
2408f76a347SChristopher Covington }
2418f76a347SChristopher Covington 
2424c357610SAndrew Jones static void pmccntr64_test(void)
2434c357610SAndrew Jones {
2444c357610SAndrew Jones #ifdef __arm__
2454c357610SAndrew Jones 	if (pmu_version == 0x3) {
2464c357610SAndrew Jones 		if (ERRATA(9e3f7a296940)) {
2474c357610SAndrew Jones 			write_sysreg(0xdead, PMCCNTR64);
248a299895bSThomas Huth 			report(read_sysreg(PMCCNTR64) == 0xdead, "pmccntr64");
2494c357610SAndrew Jones 		} else
2504c357610SAndrew Jones 			report_skip("Skipping unsafe pmccntr64 test. Set ERRATA_9e3f7a296940=y to enable.");
2514c357610SAndrew Jones 	}
2524c357610SAndrew Jones #endif
2534c357610SAndrew Jones }
2544c357610SAndrew Jones 
2554244065bSChristopher Covington /* Return FALSE if no PMU found, otherwise return TRUE */
25623b8916bSThomas Huth static bool pmu_probe(void)
2574244065bSChristopher Covington {
258*eff8f161SEric Auger 	uint32_t pmcr;
259*eff8f161SEric Auger 
260098add54SAndrew Jones 	pmu_version = get_pmu_version();
261*eff8f161SEric Auger 	if (pmu_version == 0 || pmu_version == 0xf)
262*eff8f161SEric Auger 		return false;
263*eff8f161SEric Auger 
2644244065bSChristopher Covington 	report_info("PMU version: %d", pmu_version);
265*eff8f161SEric Auger 
266*eff8f161SEric Auger 	pmcr = get_pmcr();
267*eff8f161SEric Auger 	report_info("PMU implementer/ID code/counters: %#x(\"%c\")/%#x/%d",
268*eff8f161SEric Auger 		    (pmcr >> PMU_PMCR_IMP_SHIFT) & PMU_PMCR_IMP_MASK,
269*eff8f161SEric Auger 		    ((pmcr >> PMU_PMCR_IMP_SHIFT) & PMU_PMCR_IMP_MASK) ? : ' ',
270*eff8f161SEric Auger 		    (pmcr >> PMU_PMCR_ID_SHIFT) & PMU_PMCR_ID_MASK,
271*eff8f161SEric Auger 		    (pmcr >> PMU_PMCR_N_SHIFT) & PMU_PMCR_N_MASK);
272*eff8f161SEric Auger 
273*eff8f161SEric Auger 	return true;
2744244065bSChristopher Covington }
2754244065bSChristopher Covington 
2768f76a347SChristopher Covington int main(int argc, char *argv[])
2774244065bSChristopher Covington {
2788f76a347SChristopher Covington 	int cpi = 0;
2798f76a347SChristopher Covington 
2804244065bSChristopher Covington 	if (!pmu_probe()) {
2814244065bSChristopher Covington 		printf("No PMU found, test skipped...\n");
2824244065bSChristopher Covington 		return report_summary();
2834244065bSChristopher Covington 	}
2844244065bSChristopher Covington 
28557ec1086SEric Auger 	if (argc < 2)
28657ec1086SEric Auger 		report_abort("no test specified");
28757ec1086SEric Auger 
2884244065bSChristopher Covington 	report_prefix_push("pmu");
2894244065bSChristopher Covington 
29057ec1086SEric Auger 	if (strcmp(argv[1], "cycle-counter") == 0) {
29157ec1086SEric Auger 		report_prefix_push(argv[1]);
29257ec1086SEric Auger 		if (argc > 2)
29357ec1086SEric Auger 			cpi = atol(argv[2]);
294a299895bSThomas Huth 		report(check_cycles_increase(),
295a299895bSThomas Huth 		       "Monotonically increasing cycle count");
296a299895bSThomas Huth 		report(check_cpi(cpi), "Cycle/instruction ratio");
2974c357610SAndrew Jones 		pmccntr64_test();
29857ec1086SEric Auger 		report_prefix_pop();
29957ec1086SEric Auger 	} else {
30057ec1086SEric Auger 		report_abort("Unknown sub-test '%s'", argv[1]);
30157ec1086SEric Auger 	}
3024c357610SAndrew Jones 
3034244065bSChristopher Covington 	return report_summary();
3044244065bSChristopher Covington }
305