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 /* 1384244065bSChristopher Covington * As a simple sanity check on the PMCR_EL0, ensure the implementer field isn't 1394244065bSChristopher Covington * null. Also print out a couple other interesting fields for diagnostic 1404244065bSChristopher Covington * purposes. For example, as of fall 2016, QEMU TCG mode doesn't implement 1414244065bSChristopher Covington * event counters and therefore reports zero event counters, but hopefully 1424244065bSChristopher Covington * support for at least the instructions event will be added in the future and 1434244065bSChristopher Covington * the reported number of event counters will become nonzero. 1444244065bSChristopher Covington */ 1454244065bSChristopher Covington static bool check_pmcr(void) 1464244065bSChristopher Covington { 1474244065bSChristopher Covington uint32_t pmcr; 1484244065bSChristopher Covington 1494244065bSChristopher Covington pmcr = get_pmcr(); 1504244065bSChristopher Covington 151fd6aada0SRadim Krčmář report_info("PMU implementer/ID code/counters: %#x(\"%c\")/%#x/%d", 1524244065bSChristopher Covington (pmcr >> PMU_PMCR_IMP_SHIFT) & PMU_PMCR_IMP_MASK, 1534244065bSChristopher Covington ((pmcr >> PMU_PMCR_IMP_SHIFT) & PMU_PMCR_IMP_MASK) ? : ' ', 1544244065bSChristopher Covington (pmcr >> PMU_PMCR_ID_SHIFT) & PMU_PMCR_ID_MASK, 1554244065bSChristopher Covington (pmcr >> PMU_PMCR_N_SHIFT) & PMU_PMCR_N_MASK); 1564244065bSChristopher Covington 1574244065bSChristopher Covington return ((pmcr >> PMU_PMCR_IMP_SHIFT) & PMU_PMCR_IMP_MASK) != 0; 1584244065bSChristopher Covington } 1594244065bSChristopher Covington 160d81bb7a3SChristopher 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__ 2684c357610SAndrew Jones if (pmu_version == 0x3) { 2694c357610SAndrew Jones if (ERRATA(9e3f7a296940)) { 2704c357610SAndrew Jones write_sysreg(0xdead, PMCCNTR64); 271*a299895bSThomas 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 { 281098add54SAndrew Jones pmu_version = get_pmu_version(); 2824244065bSChristopher Covington report_info("PMU version: %d", pmu_version); 283098add54SAndrew Jones return pmu_version != 0 && pmu_version != 0xf; 2844244065bSChristopher Covington } 2854244065bSChristopher Covington 2868f76a347SChristopher Covington int main(int argc, char *argv[]) 2874244065bSChristopher Covington { 2888f76a347SChristopher Covington int cpi = 0; 2898f76a347SChristopher Covington 2908f76a347SChristopher Covington if (argc > 1) 2918f76a347SChristopher Covington cpi = atol(argv[1]); 2928f76a347SChristopher Covington 2934244065bSChristopher Covington if (!pmu_probe()) { 2944244065bSChristopher Covington printf("No PMU found, test skipped...\n"); 2954244065bSChristopher Covington return report_summary(); 2964244065bSChristopher Covington } 2974244065bSChristopher Covington 2984244065bSChristopher Covington report_prefix_push("pmu"); 2994244065bSChristopher Covington 300*a299895bSThomas Huth report(check_pmcr(), "Control register"); 301*a299895bSThomas Huth report(check_cycles_increase(), 302*a299895bSThomas Huth "Monotonically increasing cycle count"); 303*a299895bSThomas Huth report(check_cpi(cpi), "Cycle/instruction ratio"); 3044244065bSChristopher Covington 3054c357610SAndrew Jones pmccntr64_test(); 3064c357610SAndrew Jones 3074244065bSChristopher Covington return report_summary(); 3084244065bSChristopher Covington } 309