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" 214870738cSEric Auger #include <bitops.h> 224ce2a804SEric Auger #include <asm/gic.h> 234244065bSChristopher Covington 24d81bb7a3SChristopher Covington #define PMU_PMCR_E (1 << 0) 254ce2a804SEric Auger #define PMU_PMCR_P (1 << 1) 26d81bb7a3SChristopher Covington #define PMU_PMCR_C (1 << 2) 274ce2a804SEric Auger #define PMU_PMCR_D (1 << 3) 284ce2a804SEric Auger #define PMU_PMCR_X (1 << 4) 294ce2a804SEric Auger #define PMU_PMCR_DP (1 << 5) 30d81bb7a3SChristopher Covington #define PMU_PMCR_LC (1 << 6) 314244065bSChristopher Covington #define PMU_PMCR_N_SHIFT 11 324244065bSChristopher Covington #define PMU_PMCR_N_MASK 0x1f 334244065bSChristopher Covington #define PMU_PMCR_ID_SHIFT 16 344244065bSChristopher Covington #define PMU_PMCR_ID_MASK 0xff 354244065bSChristopher Covington #define PMU_PMCR_IMP_SHIFT 24 364244065bSChristopher Covington #define PMU_PMCR_IMP_MASK 0xff 374244065bSChristopher Covington 38d81bb7a3SChristopher Covington #define PMU_CYCLE_IDX 31 39d81bb7a3SChristopher Covington 40d81bb7a3SChristopher Covington #define NR_SAMPLES 10 41d81bb7a3SChristopher Covington 424870738cSEric Auger /* Some PMU events */ 434870738cSEric Auger #define SW_INCR 0x0 444870738cSEric Auger #define INST_RETIRED 0x8 454870738cSEric Auger #define CPU_CYCLES 0x11 464ce2a804SEric Auger #define MEM_ACCESS 0x13 474870738cSEric Auger #define INST_PREC 0x1B 484870738cSEric Auger #define STALL_FRONTEND 0x23 494870738cSEric Auger #define STALL_BACKEND 0x24 50*66fee034SEric Auger #define CHAIN 0x1E 514870738cSEric Auger 524870738cSEric Auger #define COMMON_EVENTS_LOW 0x0 534870738cSEric Auger #define COMMON_EVENTS_HIGH 0x3F 544870738cSEric Auger #define EXT_COMMON_EVENTS_LOW 0x4000 554870738cSEric Auger #define EXT_COMMON_EVENTS_HIGH 0x403F 564870738cSEric Auger 574ce2a804SEric Auger #define ALL_SET 0xFFFFFFFF 584ce2a804SEric Auger #define ALL_CLEAR 0x0 594ce2a804SEric Auger #define PRE_OVERFLOW 0xFFFFFFF0 604ce2a804SEric Auger 618f747a85SEric Auger struct pmu { 628f747a85SEric Auger unsigned int version; 638f747a85SEric Auger unsigned int nb_implemented_counters; 648f747a85SEric Auger uint32_t pmcr_ro; 658f747a85SEric Auger }; 668f747a85SEric Auger 678f747a85SEric Auger static struct pmu pmu; 688f747a85SEric Auger 694244065bSChristopher Covington #if defined(__arm__) 70098add54SAndrew Jones #define ID_DFR0_PERFMON_SHIFT 24 71098add54SAndrew Jones #define ID_DFR0_PERFMON_MASK 0xf 72098add54SAndrew Jones 73784ee933SEric Auger #define ID_DFR0_PMU_NOTIMPL 0b0000 74784ee933SEric Auger #define ID_DFR0_PMU_V1 0b0001 75784ee933SEric Auger #define ID_DFR0_PMU_V2 0b0010 76784ee933SEric Auger #define ID_DFR0_PMU_V3 0b0011 77784ee933SEric Auger #define ID_DFR0_PMU_V3_8_1 0b0100 78784ee933SEric Auger #define ID_DFR0_PMU_V3_8_4 0b0101 79784ee933SEric Auger #define ID_DFR0_PMU_V3_8_5 0b0110 80784ee933SEric Auger #define ID_DFR0_PMU_IMPDEF 0b1111 81784ee933SEric Auger 824244065bSChristopher Covington #define PMCR __ACCESS_CP15(c9, 0, c12, 0) 834244065bSChristopher Covington #define ID_DFR0 __ACCESS_CP15(c0, 0, c1, 2) 84d81bb7a3SChristopher Covington #define PMSELR __ACCESS_CP15(c9, 0, c12, 5) 85d81bb7a3SChristopher Covington #define PMXEVTYPER __ACCESS_CP15(c9, 0, c13, 1) 86d81bb7a3SChristopher Covington #define PMCNTENSET __ACCESS_CP15(c9, 0, c12, 1) 87d81bb7a3SChristopher Covington #define PMCCNTR32 __ACCESS_CP15(c9, 0, c13, 0) 88d81bb7a3SChristopher Covington #define PMCCNTR64 __ACCESS_CP15_64(0, c9) 894244065bSChristopher Covington 904244065bSChristopher Covington static inline uint32_t get_id_dfr0(void) { return read_sysreg(ID_DFR0); } 914244065bSChristopher Covington static inline uint32_t get_pmcr(void) { return read_sysreg(PMCR); } 92d81bb7a3SChristopher Covington static inline void set_pmcr(uint32_t v) { write_sysreg(v, PMCR); } 93d81bb7a3SChristopher Covington static inline void set_pmcntenset(uint32_t v) { write_sysreg(v, PMCNTENSET); } 94d81bb7a3SChristopher Covington 95098add54SAndrew Jones static inline uint8_t get_pmu_version(void) 96098add54SAndrew Jones { 97098add54SAndrew Jones return (get_id_dfr0() >> ID_DFR0_PERFMON_SHIFT) & ID_DFR0_PERFMON_MASK; 98098add54SAndrew Jones } 99098add54SAndrew Jones 100d81bb7a3SChristopher Covington static inline uint64_t get_pmccntr(void) 101d81bb7a3SChristopher Covington { 102d81bb7a3SChristopher Covington return read_sysreg(PMCCNTR32); 103d81bb7a3SChristopher Covington } 104d81bb7a3SChristopher Covington 1058f76a347SChristopher Covington static inline void set_pmccntr(uint64_t value) 1068f76a347SChristopher Covington { 1078f76a347SChristopher Covington write_sysreg(value & 0xffffffff, PMCCNTR32); 1088f76a347SChristopher Covington } 1098f76a347SChristopher Covington 110d81bb7a3SChristopher Covington /* PMCCFILTR is an obsolete name for PMXEVTYPER31 in ARMv7 */ 111d81bb7a3SChristopher Covington static inline void set_pmccfiltr(uint32_t value) 112d81bb7a3SChristopher Covington { 113d81bb7a3SChristopher Covington write_sysreg(PMU_CYCLE_IDX, PMSELR); 114d81bb7a3SChristopher Covington write_sysreg(value, PMXEVTYPER); 115d81bb7a3SChristopher Covington isb(); 116d81bb7a3SChristopher Covington } 1178f76a347SChristopher Covington 1188f76a347SChristopher Covington /* 1198f76a347SChristopher Covington * Extra instructions inserted by the compiler would be difficult to compensate 1208f76a347SChristopher Covington * for, so hand assemble everything between, and including, the PMCR accesses 1218f76a347SChristopher Covington * to start and stop counting. isb instructions were inserted to make sure 1228f76a347SChristopher Covington * pmccntr read after this function returns the exact instructions executed in 1238f76a347SChristopher Covington * the controlled block. Total instrs = isb + mcr + 2*loop = 2 + 2*loop. 1248f76a347SChristopher Covington */ 1258f76a347SChristopher Covington static inline void precise_instrs_loop(int loop, uint32_t pmcr) 1268f76a347SChristopher Covington { 1278f76a347SChristopher Covington asm volatile( 1288f76a347SChristopher Covington " mcr p15, 0, %[pmcr], c9, c12, 0\n" 1298f76a347SChristopher Covington " isb\n" 1308f76a347SChristopher Covington "1: subs %[loop], %[loop], #1\n" 1318f76a347SChristopher Covington " bgt 1b\n" 1328f76a347SChristopher Covington " mcr p15, 0, %[z], c9, c12, 0\n" 1338f76a347SChristopher Covington " isb\n" 1348f76a347SChristopher Covington : [loop] "+r" (loop) 1358f76a347SChristopher Covington : [pmcr] "r" (pmcr), [z] "r" (0) 1368f76a347SChristopher Covington : "cc"); 1378f76a347SChristopher Covington } 1384870738cSEric Auger 1394870738cSEric Auger /* event counter tests only implemented for aarch64 */ 1404870738cSEric Auger static void test_event_introspection(void) {} 1414ce2a804SEric Auger static void test_event_counter_config(void) {} 1424ce2a804SEric Auger static void test_basic_event_count(void) {} 1434ce2a804SEric Auger static void test_mem_access(void) {} 144bb9a5adcSEric Auger static void test_sw_incr(void) {} 145*66fee034SEric Auger static void test_chained_counters(void) {} 146*66fee034SEric Auger static void test_chained_sw_incr(void) {} 1474870738cSEric Auger 1484244065bSChristopher Covington #elif defined(__aarch64__) 149098add54SAndrew Jones #define ID_AA64DFR0_PERFMON_SHIFT 8 150098add54SAndrew Jones #define ID_AA64DFR0_PERFMON_MASK 0xf 151098add54SAndrew Jones 152784ee933SEric Auger #define ID_DFR0_PMU_NOTIMPL 0b0000 153784ee933SEric Auger #define ID_DFR0_PMU_V3 0b0001 154784ee933SEric Auger #define ID_DFR0_PMU_V3_8_1 0b0100 155784ee933SEric Auger #define ID_DFR0_PMU_V3_8_4 0b0101 156784ee933SEric Auger #define ID_DFR0_PMU_V3_8_5 0b0110 157784ee933SEric Auger #define ID_DFR0_PMU_IMPDEF 0b1111 158784ee933SEric Auger 159098add54SAndrew Jones static inline uint32_t get_id_aa64dfr0(void) { return read_sysreg(id_aa64dfr0_el1); } 1604244065bSChristopher Covington static inline uint32_t get_pmcr(void) { return read_sysreg(pmcr_el0); } 161d81bb7a3SChristopher Covington static inline void set_pmcr(uint32_t v) { write_sysreg(v, pmcr_el0); } 162d81bb7a3SChristopher Covington static inline uint64_t get_pmccntr(void) { return read_sysreg(pmccntr_el0); } 1638f76a347SChristopher Covington static inline void set_pmccntr(uint64_t v) { write_sysreg(v, pmccntr_el0); } 164d81bb7a3SChristopher Covington static inline void set_pmcntenset(uint32_t v) { write_sysreg(v, pmcntenset_el0); } 165d81bb7a3SChristopher Covington static inline void set_pmccfiltr(uint32_t v) { write_sysreg(v, pmccfiltr_el0); } 1668f76a347SChristopher Covington 167098add54SAndrew Jones static inline uint8_t get_pmu_version(void) 168098add54SAndrew Jones { 169098add54SAndrew Jones uint8_t ver = (get_id_aa64dfr0() >> ID_AA64DFR0_PERFMON_SHIFT) & ID_AA64DFR0_PERFMON_MASK; 170784ee933SEric Auger return ver; 171098add54SAndrew Jones } 172098add54SAndrew Jones 1738f76a347SChristopher Covington /* 1748f76a347SChristopher Covington * Extra instructions inserted by the compiler would be difficult to compensate 1758f76a347SChristopher Covington * for, so hand assemble everything between, and including, the PMCR accesses 1768f76a347SChristopher Covington * to start and stop counting. isb instructions are inserted to make sure 1778f76a347SChristopher Covington * pmccntr read after this function returns the exact instructions executed 1788f76a347SChristopher Covington * in the controlled block. Total instrs = isb + msr + 2*loop = 2 + 2*loop. 1798f76a347SChristopher Covington */ 1808f76a347SChristopher Covington static inline void precise_instrs_loop(int loop, uint32_t pmcr) 1818f76a347SChristopher Covington { 1828f76a347SChristopher Covington asm volatile( 1838f76a347SChristopher Covington " msr pmcr_el0, %[pmcr]\n" 1848f76a347SChristopher Covington " isb\n" 1858f76a347SChristopher Covington "1: subs %[loop], %[loop], #1\n" 1868f76a347SChristopher Covington " b.gt 1b\n" 1878f76a347SChristopher Covington " msr pmcr_el0, xzr\n" 1888f76a347SChristopher Covington " isb\n" 1898f76a347SChristopher Covington : [loop] "+r" (loop) 1908f76a347SChristopher Covington : [pmcr] "r" (pmcr) 1918f76a347SChristopher Covington : "cc"); 1928f76a347SChristopher Covington } 1934870738cSEric Auger 1944870738cSEric Auger #define PMCEID1_EL0 sys_reg(3, 3, 9, 12, 7) 1954ce2a804SEric Auger #define PMCNTENSET_EL0 sys_reg(3, 3, 9, 12, 1) 1964ce2a804SEric Auger #define PMCNTENCLR_EL0 sys_reg(3, 3, 9, 12, 2) 1974ce2a804SEric Auger 1984ce2a804SEric Auger #define PMEVTYPER_EXCLUDE_EL1 BIT(31) 1994ce2a804SEric Auger #define PMEVTYPER_EXCLUDE_EL0 BIT(30) 2004870738cSEric Auger 2014870738cSEric Auger static bool is_event_supported(uint32_t n, bool warn) 2024870738cSEric Auger { 2034870738cSEric Auger uint64_t pmceid0 = read_sysreg(pmceid0_el0); 2044870738cSEric Auger uint64_t pmceid1 = read_sysreg_s(PMCEID1_EL0); 2054870738cSEric Auger bool supported; 2064870738cSEric Auger uint64_t reg; 2074870738cSEric Auger 2084870738cSEric Auger /* 2094870738cSEric Auger * The low 32-bits of PMCEID0/1 respectively describe 2104870738cSEric Auger * event support for events 0-31/32-63. Their High 2114870738cSEric Auger * 32-bits describe support for extended events 2124870738cSEric Auger * starting at 0x4000, using the same split. 2134870738cSEric Auger */ 2144870738cSEric Auger assert((n >= COMMON_EVENTS_LOW && n <= COMMON_EVENTS_HIGH) || 2154870738cSEric Auger (n >= EXT_COMMON_EVENTS_LOW && n <= EXT_COMMON_EVENTS_HIGH)); 2164870738cSEric Auger 2174870738cSEric Auger if (n <= COMMON_EVENTS_HIGH) 2184870738cSEric Auger reg = lower_32_bits(pmceid0) | ((u64)lower_32_bits(pmceid1) << 32); 2194870738cSEric Auger else 2204870738cSEric Auger reg = upper_32_bits(pmceid0) | ((u64)upper_32_bits(pmceid1) << 32); 2214870738cSEric Auger 2224870738cSEric Auger supported = reg & (1UL << (n & 0x3F)); 2234870738cSEric Auger 2244870738cSEric Auger if (!supported && warn) 2254870738cSEric Auger report_info("event 0x%x is not supported", n); 2264870738cSEric Auger return supported; 2274870738cSEric Auger } 2284870738cSEric Auger 2294870738cSEric Auger static void test_event_introspection(void) 2304870738cSEric Auger { 2314870738cSEric Auger bool required_events; 2324870738cSEric Auger 2334870738cSEric Auger if (!pmu.nb_implemented_counters) { 2344870738cSEric Auger report_skip("No event counter, skip ..."); 2354870738cSEric Auger return; 2364870738cSEric Auger } 2374870738cSEric Auger 2384870738cSEric Auger /* PMUv3 requires an implementation includes some common events */ 2394870738cSEric Auger required_events = is_event_supported(SW_INCR, true) && 2404870738cSEric Auger is_event_supported(CPU_CYCLES, true) && 2414870738cSEric Auger (is_event_supported(INST_RETIRED, true) || 2424870738cSEric Auger is_event_supported(INST_PREC, true)); 2434870738cSEric Auger 2444870738cSEric Auger if (pmu.version >= ID_DFR0_PMU_V3_8_1) { 2454870738cSEric Auger required_events = required_events && 2464870738cSEric Auger is_event_supported(STALL_FRONTEND, true) && 2474870738cSEric Auger is_event_supported(STALL_BACKEND, true); 2484870738cSEric Auger } 2494870738cSEric Auger 2504870738cSEric Auger report(required_events, "Check required events are implemented"); 2514870738cSEric Auger } 2524870738cSEric Auger 2534ce2a804SEric Auger /* 2544ce2a804SEric Auger * Extra instructions inserted by the compiler would be difficult to compensate 2554ce2a804SEric Auger * for, so hand assemble everything between, and including, the PMCR accesses 2564ce2a804SEric Auger * to start and stop counting. isb instructions are inserted to make sure 2574ce2a804SEric Auger * pmccntr read after this function returns the exact instructions executed 2584ce2a804SEric Auger * in the controlled block. Loads @loop times the data at @address into x9. 2594ce2a804SEric Auger */ 2604ce2a804SEric Auger static void mem_access_loop(void *addr, int loop, uint32_t pmcr) 2614ce2a804SEric Auger { 2624ce2a804SEric Auger asm volatile( 2634ce2a804SEric Auger " msr pmcr_el0, %[pmcr]\n" 2644ce2a804SEric Auger " isb\n" 2654ce2a804SEric Auger " mov x10, %[loop]\n" 2664ce2a804SEric Auger "1: sub x10, x10, #1\n" 2674ce2a804SEric Auger " ldr x9, [%[addr]]\n" 2684ce2a804SEric Auger " cmp x10, #0x0\n" 2694ce2a804SEric Auger " b.gt 1b\n" 2704ce2a804SEric Auger " msr pmcr_el0, xzr\n" 2714ce2a804SEric Auger " isb\n" 2724ce2a804SEric Auger : 2734ce2a804SEric Auger : [addr] "r" (addr), [pmcr] "r" (pmcr), [loop] "r" (loop) 2744ce2a804SEric Auger : "x9", "x10", "cc"); 2754ce2a804SEric Auger } 2764ce2a804SEric Auger 2774ce2a804SEric Auger static void pmu_reset(void) 2784ce2a804SEric Auger { 2794ce2a804SEric Auger /* reset all counters, counting disabled at PMCR level*/ 2804ce2a804SEric Auger set_pmcr(pmu.pmcr_ro | PMU_PMCR_LC | PMU_PMCR_C | PMU_PMCR_P); 2814ce2a804SEric Auger /* Disable all counters */ 2824ce2a804SEric Auger write_sysreg_s(ALL_SET, PMCNTENCLR_EL0); 2834ce2a804SEric Auger /* clear overflow reg */ 2844ce2a804SEric Auger write_sysreg(ALL_SET, pmovsclr_el0); 2854ce2a804SEric Auger /* disable overflow interrupts on all counters */ 2864ce2a804SEric Auger write_sysreg(ALL_SET, pmintenclr_el1); 2874ce2a804SEric Auger isb(); 2884ce2a804SEric Auger } 2894ce2a804SEric Auger 2904ce2a804SEric Auger static void test_event_counter_config(void) 2914ce2a804SEric Auger { 2924ce2a804SEric Auger int i; 2934ce2a804SEric Auger 2944ce2a804SEric Auger if (!pmu.nb_implemented_counters) { 2954ce2a804SEric Auger report_skip("No event counter, skip ..."); 2964ce2a804SEric Auger return; 2974ce2a804SEric Auger } 2984ce2a804SEric Auger 2994ce2a804SEric Auger pmu_reset(); 3004ce2a804SEric Auger 3014ce2a804SEric Auger /* 3024ce2a804SEric Auger * Test setting through PMESELR/PMXEVTYPER and PMEVTYPERn read, 3034ce2a804SEric Auger * select counter 0 3044ce2a804SEric Auger */ 3054ce2a804SEric Auger write_sysreg(1, PMSELR_EL0); 3064ce2a804SEric Auger /* program this counter to count unsupported event */ 3074ce2a804SEric Auger write_sysreg(0xEA, PMXEVTYPER_EL0); 3084ce2a804SEric Auger write_sysreg(0xdeadbeef, PMXEVCNTR_EL0); 3094ce2a804SEric Auger report((read_regn_el0(pmevtyper, 1) & 0xFFF) == 0xEA, 3104ce2a804SEric Auger "PMESELR/PMXEVTYPER/PMEVTYPERn"); 3114ce2a804SEric Auger report((read_regn_el0(pmevcntr, 1) == 0xdeadbeef), 3124ce2a804SEric Auger "PMESELR/PMXEVCNTR/PMEVCNTRn"); 3134ce2a804SEric Auger 3144ce2a804SEric Auger /* try to configure an unsupported event within the range [0x0, 0x3F] */ 3154ce2a804SEric Auger for (i = 0; i <= 0x3F; i++) { 3164ce2a804SEric Auger if (!is_event_supported(i, false)) 3174ce2a804SEric Auger break; 3184ce2a804SEric Auger } 3194ce2a804SEric Auger if (i > 0x3F) { 3204ce2a804SEric Auger report_skip("pmevtyper: all events within [0x0, 0x3F] are supported"); 3214ce2a804SEric Auger return; 3224ce2a804SEric Auger } 3234ce2a804SEric Auger 3244ce2a804SEric Auger /* select counter 0 */ 3254ce2a804SEric Auger write_sysreg(0, PMSELR_EL0); 3264ce2a804SEric Auger /* program this counter to count unsupported event */ 3274ce2a804SEric Auger write_sysreg(i, PMXEVCNTR_EL0); 3284ce2a804SEric Auger /* read the counter value */ 3294ce2a804SEric Auger read_sysreg(PMXEVCNTR_EL0); 3304ce2a804SEric Auger report(read_sysreg(PMXEVCNTR_EL0) == i, 3314ce2a804SEric Auger "read of a counter programmed with unsupported event"); 3324ce2a804SEric Auger } 3334ce2a804SEric Auger 3344ce2a804SEric Auger static bool satisfy_prerequisites(uint32_t *events, unsigned int nb_events) 3354ce2a804SEric Auger { 3364ce2a804SEric Auger int i; 3374ce2a804SEric Auger 3384ce2a804SEric Auger if (pmu.nb_implemented_counters < nb_events) { 3394ce2a804SEric Auger report_skip("Skip test as number of counters is too small (%d)", 3404ce2a804SEric Auger pmu.nb_implemented_counters); 3414ce2a804SEric Auger return false; 3424ce2a804SEric Auger } 3434ce2a804SEric Auger 3444ce2a804SEric Auger for (i = 0; i < nb_events; i++) { 3454ce2a804SEric Auger if (!is_event_supported(events[i], false)) { 3464ce2a804SEric Auger report_skip("Skip test as event 0x%x is not supported", 3474ce2a804SEric Auger events[i]); 3484ce2a804SEric Auger return false; 3494ce2a804SEric Auger } 3504ce2a804SEric Auger } 3514ce2a804SEric Auger return true; 3524ce2a804SEric Auger } 3534ce2a804SEric Auger 3544ce2a804SEric Auger static void test_basic_event_count(void) 3554ce2a804SEric Auger { 3564ce2a804SEric Auger uint32_t implemented_counter_mask, non_implemented_counter_mask; 3574ce2a804SEric Auger uint32_t counter_mask; 3584ce2a804SEric Auger uint32_t events[] = {CPU_CYCLES, INST_RETIRED}; 3594ce2a804SEric Auger 3604ce2a804SEric Auger if (!satisfy_prerequisites(events, ARRAY_SIZE(events))) 3614ce2a804SEric Auger return; 3624ce2a804SEric Auger 3634ce2a804SEric Auger implemented_counter_mask = BIT(pmu.nb_implemented_counters) - 1; 3644ce2a804SEric Auger non_implemented_counter_mask = ~(BIT(31) | implemented_counter_mask); 3654ce2a804SEric Auger counter_mask = implemented_counter_mask | non_implemented_counter_mask; 3664ce2a804SEric Auger 3674ce2a804SEric Auger write_regn_el0(pmevtyper, 0, CPU_CYCLES | PMEVTYPER_EXCLUDE_EL0); 3684ce2a804SEric Auger write_regn_el0(pmevtyper, 1, INST_RETIRED | PMEVTYPER_EXCLUDE_EL0); 3694ce2a804SEric Auger 3704ce2a804SEric Auger /* disable all counters */ 3714ce2a804SEric Auger write_sysreg_s(ALL_SET, PMCNTENCLR_EL0); 3724ce2a804SEric Auger report(!read_sysreg_s(PMCNTENCLR_EL0) && !read_sysreg_s(PMCNTENSET_EL0), 3734ce2a804SEric Auger "pmcntenclr: disable all counters"); 3744ce2a804SEric Auger 3754ce2a804SEric Auger /* 3764ce2a804SEric Auger * clear cycle and all event counters and allow counter enablement 3774ce2a804SEric Auger * through PMCNTENSET. LC is RES1. 3784ce2a804SEric Auger */ 3794ce2a804SEric Auger set_pmcr(pmu.pmcr_ro | PMU_PMCR_LC | PMU_PMCR_C | PMU_PMCR_P); 3804ce2a804SEric Auger isb(); 3814ce2a804SEric Auger report(get_pmcr() == (pmu.pmcr_ro | PMU_PMCR_LC), "pmcr: reset counters"); 3824ce2a804SEric Auger 3834ce2a804SEric Auger /* Preset counter #0 to pre overflow value to trigger an overflow */ 3844ce2a804SEric Auger write_regn_el0(pmevcntr, 0, PRE_OVERFLOW); 3854ce2a804SEric Auger report(read_regn_el0(pmevcntr, 0) == PRE_OVERFLOW, 3864ce2a804SEric Auger "counter #0 preset to pre-overflow value"); 3874ce2a804SEric Auger report(!read_regn_el0(pmevcntr, 1), "counter #1 is 0"); 3884ce2a804SEric Auger 3894ce2a804SEric Auger /* 3904ce2a804SEric Auger * Enable all implemented counters and also attempt to enable 3914ce2a804SEric Auger * not supported counters. Counting still is disabled by !PMCR.E 3924ce2a804SEric Auger */ 3934ce2a804SEric Auger write_sysreg_s(counter_mask, PMCNTENSET_EL0); 3944ce2a804SEric Auger 3954ce2a804SEric Auger /* check only those implemented are enabled */ 3964ce2a804SEric Auger report((read_sysreg_s(PMCNTENSET_EL0) == read_sysreg_s(PMCNTENCLR_EL0)) && 3974ce2a804SEric Auger (read_sysreg_s(PMCNTENSET_EL0) == implemented_counter_mask), 3984ce2a804SEric Auger "pmcntenset: enabled implemented_counters"); 3994ce2a804SEric Auger 4004ce2a804SEric Auger /* Disable all counters but counters #0 and #1 */ 4014ce2a804SEric Auger write_sysreg_s(~0x3, PMCNTENCLR_EL0); 4024ce2a804SEric Auger report((read_sysreg_s(PMCNTENSET_EL0) == read_sysreg_s(PMCNTENCLR_EL0)) && 4034ce2a804SEric Auger (read_sysreg_s(PMCNTENSET_EL0) == 0x3), 4044ce2a804SEric Auger "pmcntenset: just enabled #0 and #1"); 4054ce2a804SEric Auger 4064ce2a804SEric Auger /* clear overflow register */ 4074ce2a804SEric Auger write_sysreg(ALL_SET, pmovsclr_el0); 4084ce2a804SEric Auger report(!read_sysreg(pmovsclr_el0), "check overflow reg is 0"); 4094ce2a804SEric Auger 4104ce2a804SEric Auger /* disable overflow interrupts on all counters*/ 4114ce2a804SEric Auger write_sysreg(ALL_SET, pmintenclr_el1); 4124ce2a804SEric Auger report(!read_sysreg(pmintenclr_el1), 4134ce2a804SEric Auger "pmintenclr_el1=0, all interrupts disabled"); 4144ce2a804SEric Auger 4154ce2a804SEric Auger /* enable overflow interrupts on all event counters */ 4164ce2a804SEric Auger write_sysreg(implemented_counter_mask | non_implemented_counter_mask, 4174ce2a804SEric Auger pmintenset_el1); 4184ce2a804SEric Auger report(read_sysreg(pmintenset_el1) == implemented_counter_mask, 4194ce2a804SEric Auger "overflow interrupts enabled on all implemented counters"); 4204ce2a804SEric Auger 4214ce2a804SEric Auger /* Set PMCR.E, execute asm code and unset PMCR.E */ 4224ce2a804SEric Auger precise_instrs_loop(20, pmu.pmcr_ro | PMU_PMCR_E); 4234ce2a804SEric Auger 4244ce2a804SEric Auger report_info("counter #0 is 0x%lx (CPU_CYCLES)", 4254ce2a804SEric Auger read_regn_el0(pmevcntr, 0)); 4264ce2a804SEric Auger report_info("counter #1 is 0x%lx (INST_RETIRED)", 4274ce2a804SEric Auger read_regn_el0(pmevcntr, 1)); 4284ce2a804SEric Auger 4294ce2a804SEric Auger report_info("overflow reg = 0x%lx", read_sysreg(pmovsclr_el0)); 4304ce2a804SEric Auger report(read_sysreg(pmovsclr_el0) & 0x1, 4314ce2a804SEric Auger "check overflow happened on #0 only"); 4324ce2a804SEric Auger } 4334ce2a804SEric Auger 4344ce2a804SEric Auger static void test_mem_access(void) 4354ce2a804SEric Auger { 4364ce2a804SEric Auger void *addr = malloc(PAGE_SIZE); 4374ce2a804SEric Auger uint32_t events[] = {MEM_ACCESS, MEM_ACCESS}; 4384ce2a804SEric Auger 4394ce2a804SEric Auger if (!satisfy_prerequisites(events, ARRAY_SIZE(events))) 4404ce2a804SEric Auger return; 4414ce2a804SEric Auger 4424ce2a804SEric Auger pmu_reset(); 4434ce2a804SEric Auger 4444ce2a804SEric Auger write_regn_el0(pmevtyper, 0, MEM_ACCESS | PMEVTYPER_EXCLUDE_EL0); 4454ce2a804SEric Auger write_regn_el0(pmevtyper, 1, MEM_ACCESS | PMEVTYPER_EXCLUDE_EL0); 4464ce2a804SEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 4474ce2a804SEric Auger isb(); 4484ce2a804SEric Auger mem_access_loop(addr, 20, pmu.pmcr_ro | PMU_PMCR_E); 4494ce2a804SEric Auger report_info("counter #0 is %ld (MEM_ACCESS)", read_regn_el0(pmevcntr, 0)); 4504ce2a804SEric Auger report_info("counter #1 is %ld (MEM_ACCESS)", read_regn_el0(pmevcntr, 1)); 4514ce2a804SEric Auger /* We may measure more than 20 mem access depending on the core */ 4524ce2a804SEric Auger report((read_regn_el0(pmevcntr, 0) == read_regn_el0(pmevcntr, 1)) && 4534ce2a804SEric Auger (read_regn_el0(pmevcntr, 0) >= 20) && !read_sysreg(pmovsclr_el0), 4544ce2a804SEric Auger "Ran 20 mem accesses"); 4554ce2a804SEric Auger 4564ce2a804SEric Auger pmu_reset(); 4574ce2a804SEric Auger 4584ce2a804SEric Auger write_regn_el0(pmevcntr, 0, PRE_OVERFLOW); 4594ce2a804SEric Auger write_regn_el0(pmevcntr, 1, PRE_OVERFLOW); 4604ce2a804SEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 4614ce2a804SEric Auger isb(); 4624ce2a804SEric Auger mem_access_loop(addr, 20, pmu.pmcr_ro | PMU_PMCR_E); 4634ce2a804SEric Auger report(read_sysreg(pmovsclr_el0) == 0x3, 4644ce2a804SEric Auger "Ran 20 mem accesses with expected overflows on both counters"); 4654ce2a804SEric Auger report_info("cnt#0 = %ld cnt#1=%ld overflow=0x%lx", 4664ce2a804SEric Auger read_regn_el0(pmevcntr, 0), read_regn_el0(pmevcntr, 1), 4674ce2a804SEric Auger read_sysreg(pmovsclr_el0)); 4684ce2a804SEric Auger } 4694ce2a804SEric Auger 470bb9a5adcSEric Auger static void test_sw_incr(void) 471bb9a5adcSEric Auger { 472bb9a5adcSEric Auger uint32_t events[] = {SW_INCR, SW_INCR}; 473bb9a5adcSEric Auger int i; 474bb9a5adcSEric Auger 475bb9a5adcSEric Auger if (!satisfy_prerequisites(events, ARRAY_SIZE(events))) 476bb9a5adcSEric Auger return; 477bb9a5adcSEric Auger 478bb9a5adcSEric Auger pmu_reset(); 479bb9a5adcSEric Auger 480bb9a5adcSEric Auger write_regn_el0(pmevtyper, 0, SW_INCR | PMEVTYPER_EXCLUDE_EL0); 481bb9a5adcSEric Auger write_regn_el0(pmevtyper, 1, SW_INCR | PMEVTYPER_EXCLUDE_EL0); 482bb9a5adcSEric Auger /* enable counters #0 and #1 */ 483bb9a5adcSEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 484bb9a5adcSEric Auger 485bb9a5adcSEric Auger write_regn_el0(pmevcntr, 0, PRE_OVERFLOW); 486bb9a5adcSEric Auger 487bb9a5adcSEric Auger for (i = 0; i < 100; i++) 488bb9a5adcSEric Auger write_sysreg(0x1, pmswinc_el0); 489bb9a5adcSEric Auger 490bb9a5adcSEric Auger report_info("SW_INCR counter #0 has value %ld", read_regn_el0(pmevcntr, 0)); 491bb9a5adcSEric Auger report(read_regn_el0(pmevcntr, 0) == PRE_OVERFLOW, 492bb9a5adcSEric Auger "PWSYNC does not increment if PMCR.E is unset"); 493bb9a5adcSEric Auger 494bb9a5adcSEric Auger pmu_reset(); 495bb9a5adcSEric Auger 496bb9a5adcSEric Auger write_regn_el0(pmevcntr, 0, PRE_OVERFLOW); 497bb9a5adcSEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 498bb9a5adcSEric Auger set_pmcr(pmu.pmcr_ro | PMU_PMCR_E); 499bb9a5adcSEric Auger 500bb9a5adcSEric Auger for (i = 0; i < 100; i++) 501bb9a5adcSEric Auger write_sysreg(0x3, pmswinc_el0); 502bb9a5adcSEric Auger 503bb9a5adcSEric Auger report(read_regn_el0(pmevcntr, 0) == 84, "counter #1 after + 100 SW_INCR"); 504bb9a5adcSEric Auger report(read_regn_el0(pmevcntr, 1) == 100, 505bb9a5adcSEric Auger "counter #0 after + 100 SW_INCR"); 506bb9a5adcSEric Auger report_info("counter values after 100 SW_INCR #0=%ld #1=%ld", 507bb9a5adcSEric Auger read_regn_el0(pmevcntr, 0), read_regn_el0(pmevcntr, 1)); 508bb9a5adcSEric Auger report(read_sysreg(pmovsclr_el0) == 0x1, 509*66fee034SEric Auger "overflow on counter #0 after 100 SW_INCR"); 510*66fee034SEric Auger } 511*66fee034SEric Auger 512*66fee034SEric Auger static void test_chained_counters(void) 513*66fee034SEric Auger { 514*66fee034SEric Auger uint32_t events[] = {CPU_CYCLES, CHAIN}; 515*66fee034SEric Auger 516*66fee034SEric Auger if (!satisfy_prerequisites(events, ARRAY_SIZE(events))) 517*66fee034SEric Auger return; 518*66fee034SEric Auger 519*66fee034SEric Auger pmu_reset(); 520*66fee034SEric Auger 521*66fee034SEric Auger write_regn_el0(pmevtyper, 0, CPU_CYCLES | PMEVTYPER_EXCLUDE_EL0); 522*66fee034SEric Auger write_regn_el0(pmevtyper, 1, CHAIN | PMEVTYPER_EXCLUDE_EL0); 523*66fee034SEric Auger /* enable counters #0 and #1 */ 524*66fee034SEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 525*66fee034SEric Auger write_regn_el0(pmevcntr, 0, PRE_OVERFLOW); 526*66fee034SEric Auger 527*66fee034SEric Auger precise_instrs_loop(22, pmu.pmcr_ro | PMU_PMCR_E); 528*66fee034SEric Auger 529*66fee034SEric Auger report(read_regn_el0(pmevcntr, 1) == 1, "CHAIN counter #1 incremented"); 530*66fee034SEric Auger report(!read_sysreg(pmovsclr_el0), "no overflow recorded for chained incr #1"); 531*66fee034SEric Auger 532*66fee034SEric Auger /* test 64b overflow */ 533*66fee034SEric Auger 534*66fee034SEric Auger pmu_reset(); 535*66fee034SEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 536*66fee034SEric Auger 537*66fee034SEric Auger write_regn_el0(pmevcntr, 0, PRE_OVERFLOW); 538*66fee034SEric Auger write_regn_el0(pmevcntr, 1, 0x1); 539*66fee034SEric Auger precise_instrs_loop(22, pmu.pmcr_ro | PMU_PMCR_E); 540*66fee034SEric Auger report_info("overflow reg = 0x%lx", read_sysreg(pmovsclr_el0)); 541*66fee034SEric Auger report(read_regn_el0(pmevcntr, 1) == 2, "CHAIN counter #1 set to 2"); 542*66fee034SEric Auger report(!read_sysreg(pmovsclr_el0), "no overflow recorded for chained incr #2"); 543*66fee034SEric Auger 544*66fee034SEric Auger write_regn_el0(pmevcntr, 0, PRE_OVERFLOW); 545*66fee034SEric Auger write_regn_el0(pmevcntr, 1, ALL_SET); 546*66fee034SEric Auger 547*66fee034SEric Auger precise_instrs_loop(22, pmu.pmcr_ro | PMU_PMCR_E); 548*66fee034SEric Auger report_info("overflow reg = 0x%lx", read_sysreg(pmovsclr_el0)); 549*66fee034SEric Auger report(!read_regn_el0(pmevcntr, 1), "CHAIN counter #1 wrapped"); 550*66fee034SEric Auger report(read_sysreg(pmovsclr_el0) == 0x2, "overflow on chain counter"); 551*66fee034SEric Auger } 552*66fee034SEric Auger 553*66fee034SEric Auger static void test_chained_sw_incr(void) 554*66fee034SEric Auger { 555*66fee034SEric Auger uint32_t events[] = {SW_INCR, CHAIN}; 556*66fee034SEric Auger int i; 557*66fee034SEric Auger 558*66fee034SEric Auger if (!satisfy_prerequisites(events, ARRAY_SIZE(events))) 559*66fee034SEric Auger return; 560*66fee034SEric Auger 561*66fee034SEric Auger pmu_reset(); 562*66fee034SEric Auger 563*66fee034SEric Auger write_regn_el0(pmevtyper, 0, SW_INCR | PMEVTYPER_EXCLUDE_EL0); 564*66fee034SEric Auger write_regn_el0(pmevtyper, 1, CHAIN | PMEVTYPER_EXCLUDE_EL0); 565*66fee034SEric Auger /* enable counters #0 and #1 */ 566*66fee034SEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 567*66fee034SEric Auger 568*66fee034SEric Auger write_regn_el0(pmevcntr, 0, PRE_OVERFLOW); 569*66fee034SEric Auger set_pmcr(pmu.pmcr_ro | PMU_PMCR_E); 570*66fee034SEric Auger for (i = 0; i < 100; i++) 571*66fee034SEric Auger write_sysreg(0x1, pmswinc_el0); 572*66fee034SEric Auger 573*66fee034SEric Auger report(!read_sysreg(pmovsclr_el0) && (read_regn_el0(pmevcntr, 1) == 1), 574*66fee034SEric Auger "no overflow and chain counter incremented after 100 SW_INCR/CHAIN"); 575*66fee034SEric Auger report_info("overflow=0x%lx, #0=%ld #1=%ld", read_sysreg(pmovsclr_el0), 576*66fee034SEric Auger read_regn_el0(pmevcntr, 0), read_regn_el0(pmevcntr, 1)); 577*66fee034SEric Auger 578*66fee034SEric Auger /* 64b SW_INCR and overflow on CHAIN counter*/ 579*66fee034SEric Auger pmu_reset(); 580*66fee034SEric Auger 581*66fee034SEric Auger write_regn_el0(pmevtyper, 1, events[1] | PMEVTYPER_EXCLUDE_EL0); 582*66fee034SEric Auger write_regn_el0(pmevcntr, 0, PRE_OVERFLOW); 583*66fee034SEric Auger write_regn_el0(pmevcntr, 1, ALL_SET); 584*66fee034SEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 585*66fee034SEric Auger set_pmcr(pmu.pmcr_ro | PMU_PMCR_E); 586*66fee034SEric Auger for (i = 0; i < 100; i++) 587*66fee034SEric Auger write_sysreg(0x1, pmswinc_el0); 588*66fee034SEric Auger 589*66fee034SEric Auger report((read_sysreg(pmovsclr_el0) == 0x2) && 590*66fee034SEric Auger (read_regn_el0(pmevcntr, 1) == 0) && 591*66fee034SEric Auger (read_regn_el0(pmevcntr, 0) == 84), 592*66fee034SEric Auger "overflow on chain counter and expected values after 100 SW_INCR/CHAIN"); 593*66fee034SEric Auger report_info("overflow=0x%lx, #0=%ld #1=%ld", read_sysreg(pmovsclr_el0), 594*66fee034SEric Auger read_regn_el0(pmevcntr, 0), read_regn_el0(pmevcntr, 1)); 595bb9a5adcSEric Auger } 596bb9a5adcSEric Auger 5974244065bSChristopher Covington #endif 5984244065bSChristopher Covington 5994244065bSChristopher Covington /* 600d81bb7a3SChristopher Covington * Ensure that the cycle counter progresses between back-to-back reads. 601d81bb7a3SChristopher Covington */ 602d81bb7a3SChristopher Covington static bool check_cycles_increase(void) 603d81bb7a3SChristopher Covington { 604d81bb7a3SChristopher Covington bool success = true; 605d81bb7a3SChristopher Covington 606d81bb7a3SChristopher Covington /* init before event access, this test only cares about cycle count */ 607d81bb7a3SChristopher Covington set_pmcntenset(1 << PMU_CYCLE_IDX); 608d81bb7a3SChristopher Covington set_pmccfiltr(0); /* count cycles in EL0, EL1, but not EL2 */ 609d81bb7a3SChristopher Covington 610d81bb7a3SChristopher Covington set_pmcr(get_pmcr() | PMU_PMCR_LC | PMU_PMCR_C | PMU_PMCR_E); 611d81bb7a3SChristopher Covington 612d81bb7a3SChristopher Covington for (int i = 0; i < NR_SAMPLES; i++) { 613d81bb7a3SChristopher Covington uint64_t a, b; 614d81bb7a3SChristopher Covington 615d81bb7a3SChristopher Covington a = get_pmccntr(); 616d81bb7a3SChristopher Covington b = get_pmccntr(); 617d81bb7a3SChristopher Covington 618d81bb7a3SChristopher Covington if (a >= b) { 619d81bb7a3SChristopher Covington printf("Read %"PRId64" then %"PRId64".\n", a, b); 620d81bb7a3SChristopher Covington success = false; 621d81bb7a3SChristopher Covington break; 622d81bb7a3SChristopher Covington } 623d81bb7a3SChristopher Covington } 624d81bb7a3SChristopher Covington 625d81bb7a3SChristopher Covington set_pmcr(get_pmcr() & ~PMU_PMCR_E); 626d81bb7a3SChristopher Covington 627d81bb7a3SChristopher Covington return success; 628d81bb7a3SChristopher Covington } 629d81bb7a3SChristopher Covington 6308f76a347SChristopher Covington /* 6318f76a347SChristopher Covington * Execute a known number of guest instructions. Only even instruction counts 6328f76a347SChristopher Covington * greater than or equal to 4 are supported by the in-line assembly code. The 6338f76a347SChristopher Covington * control register (PMCR_EL0) is initialized with the provided value (allowing 6348f76a347SChristopher Covington * for example for the cycle counter or event counters to be reset). At the end 6358f76a347SChristopher Covington * of the exact instruction loop, zero is written to PMCR_EL0 to disable 6368f76a347SChristopher Covington * counting, allowing the cycle counter or event counters to be read at the 6378f76a347SChristopher Covington * leisure of the calling code. 6388f76a347SChristopher Covington */ 6398f76a347SChristopher Covington static void measure_instrs(int num, uint32_t pmcr) 6408f76a347SChristopher Covington { 6418f76a347SChristopher Covington int loop = (num - 2) / 2; 6428f76a347SChristopher Covington 6438f76a347SChristopher Covington assert(num >= 4 && ((num - 2) % 2 == 0)); 6448f76a347SChristopher Covington precise_instrs_loop(loop, pmcr); 6458f76a347SChristopher Covington } 6468f76a347SChristopher Covington 6478f76a347SChristopher Covington /* 6488f76a347SChristopher Covington * Measure cycle counts for various known instruction counts. Ensure that the 6498f76a347SChristopher Covington * cycle counter progresses (similar to check_cycles_increase() but with more 6508f76a347SChristopher Covington * instructions and using reset and stop controls). If supplied a positive, 6518f76a347SChristopher Covington * nonzero CPI parameter, it also strictly checks that every measurement matches 6528f76a347SChristopher Covington * it. Strict CPI checking is used to test -icount mode. 6538f76a347SChristopher Covington */ 6548f76a347SChristopher Covington static bool check_cpi(int cpi) 6558f76a347SChristopher Covington { 6568f76a347SChristopher Covington uint32_t pmcr = get_pmcr() | PMU_PMCR_LC | PMU_PMCR_C | PMU_PMCR_E; 6578f76a347SChristopher Covington 6588f76a347SChristopher Covington /* init before event access, this test only cares about cycle count */ 6598f76a347SChristopher Covington set_pmcntenset(1 << PMU_CYCLE_IDX); 6608f76a347SChristopher Covington set_pmccfiltr(0); /* count cycles in EL0, EL1, but not EL2 */ 6618f76a347SChristopher Covington 6628f76a347SChristopher Covington if (cpi > 0) 6638f76a347SChristopher Covington printf("Checking for CPI=%d.\n", cpi); 6648f76a347SChristopher Covington printf("instrs : cycles0 cycles1 ...\n"); 6658f76a347SChristopher Covington 6668f76a347SChristopher Covington for (unsigned int i = 4; i < 300; i += 32) { 6678f76a347SChristopher Covington uint64_t avg, sum = 0; 6688f76a347SChristopher Covington 6698f76a347SChristopher Covington printf("%4d:", i); 6708f76a347SChristopher Covington for (int j = 0; j < NR_SAMPLES; j++) { 6718f76a347SChristopher Covington uint64_t cycles; 6728f76a347SChristopher Covington 6738f76a347SChristopher Covington set_pmccntr(0); 6748f76a347SChristopher Covington measure_instrs(i, pmcr); 6758f76a347SChristopher Covington cycles = get_pmccntr(); 6768f76a347SChristopher Covington printf(" %4"PRId64"", cycles); 6778f76a347SChristopher Covington 6788f76a347SChristopher Covington if (!cycles) { 6798f76a347SChristopher Covington printf("\ncycles not incrementing!\n"); 6808f76a347SChristopher Covington return false; 6818f76a347SChristopher Covington } else if (cpi > 0 && cycles != i * cpi) { 6828f76a347SChristopher Covington printf("\nunexpected cycle count received!\n"); 6838f76a347SChristopher Covington return false; 6848f76a347SChristopher Covington } else if ((cycles >> 32) != 0) { 6858f76a347SChristopher Covington /* The cycles taken by the loop above should 6868f76a347SChristopher Covington * fit in 32 bits easily. We check the upper 6878f76a347SChristopher Covington * 32 bits of the cycle counter to make sure 6888f76a347SChristopher Covington * there is no supprise. */ 6898f76a347SChristopher Covington printf("\ncycle count bigger than 32bit!\n"); 6908f76a347SChristopher Covington return false; 6918f76a347SChristopher Covington } 6928f76a347SChristopher Covington 6938f76a347SChristopher Covington sum += cycles; 6948f76a347SChristopher Covington } 6958f76a347SChristopher Covington avg = sum / NR_SAMPLES; 6968f76a347SChristopher Covington printf(" avg=%-4"PRId64" %s=%-3"PRId64"\n", avg, 6978f76a347SChristopher Covington (avg >= i) ? "cpi" : "ipc", 6988f76a347SChristopher Covington (avg >= i) ? avg / i : i / avg); 6998f76a347SChristopher Covington } 7008f76a347SChristopher Covington 7018f76a347SChristopher Covington return true; 7028f76a347SChristopher Covington } 7038f76a347SChristopher Covington 7044c357610SAndrew Jones static void pmccntr64_test(void) 7054c357610SAndrew Jones { 7064c357610SAndrew Jones #ifdef __arm__ 707784ee933SEric Auger if (pmu.version == ID_DFR0_PMU_V3) { 7084c357610SAndrew Jones if (ERRATA(9e3f7a296940)) { 7094c357610SAndrew Jones write_sysreg(0xdead, PMCCNTR64); 710a299895bSThomas Huth report(read_sysreg(PMCCNTR64) == 0xdead, "pmccntr64"); 7114c357610SAndrew Jones } else 7124c357610SAndrew Jones report_skip("Skipping unsafe pmccntr64 test. Set ERRATA_9e3f7a296940=y to enable."); 7134c357610SAndrew Jones } 7144c357610SAndrew Jones #endif 7154c357610SAndrew Jones } 7164c357610SAndrew Jones 7174244065bSChristopher Covington /* Return FALSE if no PMU found, otherwise return TRUE */ 71823b8916bSThomas Huth static bool pmu_probe(void) 7194244065bSChristopher Covington { 720784ee933SEric Auger uint32_t pmcr = get_pmcr(); 721eff8f161SEric Auger 7228f747a85SEric Auger pmu.version = get_pmu_version(); 723784ee933SEric Auger if (pmu.version == ID_DFR0_PMU_NOTIMPL || pmu.version == ID_DFR0_PMU_IMPDEF) 724eff8f161SEric Auger return false; 725eff8f161SEric Auger 726784ee933SEric Auger report_info("PMU version: 0x%x", pmu.version); 727eff8f161SEric Auger 728eff8f161SEric Auger pmcr = get_pmcr(); 7298f747a85SEric Auger report_info("PMU implementer/ID code: %#x(\"%c\")/%#x", 730eff8f161SEric Auger (pmcr >> PMU_PMCR_IMP_SHIFT) & PMU_PMCR_IMP_MASK, 731eff8f161SEric Auger ((pmcr >> PMU_PMCR_IMP_SHIFT) & PMU_PMCR_IMP_MASK) ? : ' ', 7328f747a85SEric Auger (pmcr >> PMU_PMCR_ID_SHIFT) & PMU_PMCR_ID_MASK); 7338f747a85SEric Auger 7348f747a85SEric Auger /* store read-only and RES0 fields of the PMCR bottom-half*/ 7358f747a85SEric Auger pmu.pmcr_ro = pmcr & 0xFFFFFF00; 7368f747a85SEric Auger pmu.nb_implemented_counters = 7378f747a85SEric Auger (pmcr >> PMU_PMCR_N_SHIFT) & PMU_PMCR_N_MASK; 7388f747a85SEric Auger report_info("Implements %d event counters", 7398f747a85SEric Auger pmu.nb_implemented_counters); 740eff8f161SEric Auger 741eff8f161SEric Auger return true; 7424244065bSChristopher Covington } 7434244065bSChristopher Covington 7448f76a347SChristopher Covington int main(int argc, char *argv[]) 7454244065bSChristopher Covington { 7468f76a347SChristopher Covington int cpi = 0; 7478f76a347SChristopher Covington 7484244065bSChristopher Covington if (!pmu_probe()) { 7494244065bSChristopher Covington printf("No PMU found, test skipped...\n"); 7504244065bSChristopher Covington return report_summary(); 7514244065bSChristopher Covington } 7524244065bSChristopher Covington 75357ec1086SEric Auger if (argc < 2) 75457ec1086SEric Auger report_abort("no test specified"); 75557ec1086SEric Auger 7564244065bSChristopher Covington report_prefix_push("pmu"); 7574244065bSChristopher Covington 75857ec1086SEric Auger if (strcmp(argv[1], "cycle-counter") == 0) { 75957ec1086SEric Auger report_prefix_push(argv[1]); 76057ec1086SEric Auger if (argc > 2) 76157ec1086SEric Auger cpi = atol(argv[2]); 762a299895bSThomas Huth report(check_cycles_increase(), 763a299895bSThomas Huth "Monotonically increasing cycle count"); 764a299895bSThomas Huth report(check_cpi(cpi), "Cycle/instruction ratio"); 7654c357610SAndrew Jones pmccntr64_test(); 76657ec1086SEric Auger report_prefix_pop(); 7674870738cSEric Auger } else if (strcmp(argv[1], "pmu-event-introspection") == 0) { 7684870738cSEric Auger report_prefix_push(argv[1]); 7694870738cSEric Auger test_event_introspection(); 7704870738cSEric Auger report_prefix_pop(); 7714ce2a804SEric Auger } else if (strcmp(argv[1], "pmu-event-counter-config") == 0) { 7724ce2a804SEric Auger report_prefix_push(argv[1]); 7734ce2a804SEric Auger test_event_counter_config(); 7744ce2a804SEric Auger report_prefix_pop(); 7754ce2a804SEric Auger } else if (strcmp(argv[1], "pmu-basic-event-count") == 0) { 7764ce2a804SEric Auger report_prefix_push(argv[1]); 7774ce2a804SEric Auger test_basic_event_count(); 7784ce2a804SEric Auger report_prefix_pop(); 7794ce2a804SEric Auger } else if (strcmp(argv[1], "pmu-mem-access") == 0) { 7804ce2a804SEric Auger report_prefix_push(argv[1]); 7814ce2a804SEric Auger test_mem_access(); 7824ce2a804SEric Auger report_prefix_pop(); 783bb9a5adcSEric Auger } else if (strcmp(argv[1], "pmu-sw-incr") == 0) { 784bb9a5adcSEric Auger report_prefix_push(argv[1]); 785bb9a5adcSEric Auger test_sw_incr(); 786bb9a5adcSEric Auger report_prefix_pop(); 787*66fee034SEric Auger } else if (strcmp(argv[1], "pmu-chained-counters") == 0) { 788*66fee034SEric Auger report_prefix_push(argv[1]); 789*66fee034SEric Auger test_chained_counters(); 790*66fee034SEric Auger report_prefix_pop(); 791*66fee034SEric Auger } else if (strcmp(argv[1], "pmu-chained-sw-incr") == 0) { 792*66fee034SEric Auger report_prefix_push(argv[1]); 793*66fee034SEric Auger test_chained_sw_incr(); 794*66fee034SEric Auger report_prefix_pop(); 79557ec1086SEric Auger } else { 79657ec1086SEric Auger report_abort("Unknown sub-test '%s'", argv[1]); 79757ec1086SEric Auger } 7984c357610SAndrew Jones 7994244065bSChristopher Covington return report_summary(); 8004244065bSChristopher Covington } 801