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 5066fee034SEric 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 57*7d1f853aSRicardo Koller #define ALL_SET_32 0x00000000FFFFFFFFULL 5839d1347aSRicardo Koller #define ALL_CLEAR 0x0000000000000000ULL 59*7d1f853aSRicardo Koller #define PRE_OVERFLOW_32 0x00000000FFFFFFF0ULL 60*7d1f853aSRicardo Koller #define PRE_OVERFLOW2_32 0x00000000FFFFFFDCULL 614ce2a804SEric Auger 624f5ef94fSEric Auger #define PMU_PPI 23 634f5ef94fSEric Auger 648f747a85SEric Auger struct pmu { 658f747a85SEric Auger unsigned int version; 668f747a85SEric Auger unsigned int nb_implemented_counters; 678f747a85SEric Auger uint32_t pmcr_ro; 688f747a85SEric Auger }; 698f747a85SEric Auger 704f5ef94fSEric Auger struct pmu_stats { 714f5ef94fSEric Auger unsigned long bitmap; 724f5ef94fSEric Auger uint32_t interrupts[32]; 734f5ef94fSEric Auger bool unexpected; 744f5ef94fSEric Auger }; 754f5ef94fSEric Auger 768f747a85SEric Auger static struct pmu pmu; 778f747a85SEric Auger 784244065bSChristopher Covington #if defined(__arm__) 79098add54SAndrew Jones #define ID_DFR0_PERFMON_SHIFT 24 80098add54SAndrew Jones #define ID_DFR0_PERFMON_MASK 0xf 81098add54SAndrew Jones 82784ee933SEric Auger #define ID_DFR0_PMU_NOTIMPL 0b0000 83784ee933SEric Auger #define ID_DFR0_PMU_V1 0b0001 84784ee933SEric Auger #define ID_DFR0_PMU_V2 0b0010 85784ee933SEric Auger #define ID_DFR0_PMU_V3 0b0011 86784ee933SEric Auger #define ID_DFR0_PMU_V3_8_1 0b0100 87784ee933SEric Auger #define ID_DFR0_PMU_V3_8_4 0b0101 88784ee933SEric Auger #define ID_DFR0_PMU_V3_8_5 0b0110 89784ee933SEric Auger #define ID_DFR0_PMU_IMPDEF 0b1111 90784ee933SEric Auger 914244065bSChristopher Covington #define PMCR __ACCESS_CP15(c9, 0, c12, 0) 924244065bSChristopher Covington #define ID_DFR0 __ACCESS_CP15(c0, 0, c1, 2) 93d81bb7a3SChristopher Covington #define PMSELR __ACCESS_CP15(c9, 0, c12, 5) 94d81bb7a3SChristopher Covington #define PMXEVTYPER __ACCESS_CP15(c9, 0, c13, 1) 95d81bb7a3SChristopher Covington #define PMCNTENSET __ACCESS_CP15(c9, 0, c12, 1) 96a7326740SRicardo Koller #define PMCNTENCLR __ACCESS_CP15(c9, 0, c12, 2) 97a7326740SRicardo Koller #define PMOVSR __ACCESS_CP15(c9, 0, c12, 3) 98d81bb7a3SChristopher Covington #define PMCCNTR32 __ACCESS_CP15(c9, 0, c13, 0) 99a7326740SRicardo Koller #define PMINTENCLR __ACCESS_CP15(c9, 0, c14, 2) 100d81bb7a3SChristopher Covington #define PMCCNTR64 __ACCESS_CP15_64(0, c9) 1014244065bSChristopher Covington 1024244065bSChristopher Covington static inline uint32_t get_id_dfr0(void) { return read_sysreg(ID_DFR0); } 1034244065bSChristopher Covington static inline uint32_t get_pmcr(void) { return read_sysreg(PMCR); } 104d81bb7a3SChristopher Covington static inline void set_pmcr(uint32_t v) { write_sysreg(v, PMCR); } 105d81bb7a3SChristopher Covington static inline void set_pmcntenset(uint32_t v) { write_sysreg(v, PMCNTENSET); } 106d81bb7a3SChristopher Covington 107098add54SAndrew Jones static inline uint8_t get_pmu_version(void) 108098add54SAndrew Jones { 109098add54SAndrew Jones return (get_id_dfr0() >> ID_DFR0_PERFMON_SHIFT) & ID_DFR0_PERFMON_MASK; 110098add54SAndrew Jones } 111098add54SAndrew Jones 112d81bb7a3SChristopher Covington static inline uint64_t get_pmccntr(void) 113d81bb7a3SChristopher Covington { 114d81bb7a3SChristopher Covington return read_sysreg(PMCCNTR32); 115d81bb7a3SChristopher Covington } 116d81bb7a3SChristopher Covington 1178f76a347SChristopher Covington static inline void set_pmccntr(uint64_t value) 1188f76a347SChristopher Covington { 1198f76a347SChristopher Covington write_sysreg(value & 0xffffffff, PMCCNTR32); 1208f76a347SChristopher Covington } 1218f76a347SChristopher Covington 122d81bb7a3SChristopher Covington /* PMCCFILTR is an obsolete name for PMXEVTYPER31 in ARMv7 */ 123d81bb7a3SChristopher Covington static inline void set_pmccfiltr(uint32_t value) 124d81bb7a3SChristopher Covington { 125d81bb7a3SChristopher Covington write_sysreg(PMU_CYCLE_IDX, PMSELR); 126d81bb7a3SChristopher Covington write_sysreg(value, PMXEVTYPER); 127d81bb7a3SChristopher Covington isb(); 128d81bb7a3SChristopher Covington } 1298f76a347SChristopher Covington 1308f76a347SChristopher Covington /* 1318f76a347SChristopher Covington * Extra instructions inserted by the compiler would be difficult to compensate 1328f76a347SChristopher Covington * for, so hand assemble everything between, and including, the PMCR accesses 1338f76a347SChristopher Covington * to start and stop counting. isb instructions were inserted to make sure 1348f76a347SChristopher Covington * pmccntr read after this function returns the exact instructions executed in 1358f76a347SChristopher Covington * the controlled block. Total instrs = isb + mcr + 2*loop = 2 + 2*loop. 1368f76a347SChristopher Covington */ 1378f76a347SChristopher Covington static inline void precise_instrs_loop(int loop, uint32_t pmcr) 1388f76a347SChristopher Covington { 1398f76a347SChristopher Covington asm volatile( 1408f76a347SChristopher Covington " mcr p15, 0, %[pmcr], c9, c12, 0\n" 1418f76a347SChristopher Covington " isb\n" 1428f76a347SChristopher Covington "1: subs %[loop], %[loop], #1\n" 1438f76a347SChristopher Covington " bgt 1b\n" 1448f76a347SChristopher Covington " mcr p15, 0, %[z], c9, c12, 0\n" 1458f76a347SChristopher Covington " isb\n" 1468f76a347SChristopher Covington : [loop] "+r" (loop) 1478f76a347SChristopher Covington : [pmcr] "r" (pmcr), [z] "r" (0) 1488f76a347SChristopher Covington : "cc"); 1498f76a347SChristopher Covington } 1504870738cSEric Auger 151a7326740SRicardo Koller static void pmu_reset(void) 152a7326740SRicardo Koller { 153a7326740SRicardo Koller /* reset all counters, counting disabled at PMCR level*/ 154a7326740SRicardo Koller set_pmcr(pmu.pmcr_ro | PMU_PMCR_LC | PMU_PMCR_C | PMU_PMCR_P); 155a7326740SRicardo Koller /* Disable all counters */ 156*7d1f853aSRicardo Koller write_sysreg(ALL_SET_32, PMCNTENCLR); 157a7326740SRicardo Koller /* clear overflow reg */ 158*7d1f853aSRicardo Koller write_sysreg(ALL_SET_32, PMOVSR); 159a7326740SRicardo Koller /* disable overflow interrupts on all counters */ 160*7d1f853aSRicardo Koller write_sysreg(ALL_SET_32, PMINTENCLR); 161a7326740SRicardo Koller isb(); 162a7326740SRicardo Koller } 163a7326740SRicardo Koller 1644870738cSEric Auger /* event counter tests only implemented for aarch64 */ 1654870738cSEric Auger static void test_event_introspection(void) {} 1664ce2a804SEric Auger static void test_event_counter_config(void) {} 167041df25bSRicardo Koller static void test_basic_event_count(bool overflow_at_64bits) {} 168041df25bSRicardo Koller static void test_mem_access(bool overflow_at_64bits) {} 169041df25bSRicardo Koller static void test_sw_incr(bool overflow_at_64bits) {} 170041df25bSRicardo Koller static void test_chained_counters(bool unused) {} 171041df25bSRicardo Koller static void test_chained_sw_incr(bool unused) {} 172041df25bSRicardo Koller static void test_chain_promotion(bool unused) {} 173041df25bSRicardo Koller static void test_overflow_interrupt(bool overflow_at_64bits) {} 1744870738cSEric Auger 1754244065bSChristopher Covington #elif defined(__aarch64__) 176098add54SAndrew Jones #define ID_AA64DFR0_PERFMON_SHIFT 8 177098add54SAndrew Jones #define ID_AA64DFR0_PERFMON_MASK 0xf 178098add54SAndrew Jones 179784ee933SEric Auger #define ID_DFR0_PMU_NOTIMPL 0b0000 180784ee933SEric Auger #define ID_DFR0_PMU_V3 0b0001 181784ee933SEric Auger #define ID_DFR0_PMU_V3_8_1 0b0100 182784ee933SEric Auger #define ID_DFR0_PMU_V3_8_4 0b0101 183784ee933SEric Auger #define ID_DFR0_PMU_V3_8_5 0b0110 184784ee933SEric Auger #define ID_DFR0_PMU_IMPDEF 0b1111 185784ee933SEric Auger 186098add54SAndrew Jones static inline uint32_t get_id_aa64dfr0(void) { return read_sysreg(id_aa64dfr0_el1); } 1874244065bSChristopher Covington static inline uint32_t get_pmcr(void) { return read_sysreg(pmcr_el0); } 188d81bb7a3SChristopher Covington static inline void set_pmcr(uint32_t v) { write_sysreg(v, pmcr_el0); } 189d81bb7a3SChristopher Covington static inline uint64_t get_pmccntr(void) { return read_sysreg(pmccntr_el0); } 1908f76a347SChristopher Covington static inline void set_pmccntr(uint64_t v) { write_sysreg(v, pmccntr_el0); } 191d81bb7a3SChristopher Covington static inline void set_pmcntenset(uint32_t v) { write_sysreg(v, pmcntenset_el0); } 192d81bb7a3SChristopher Covington static inline void set_pmccfiltr(uint32_t v) { write_sysreg(v, pmccfiltr_el0); } 1938f76a347SChristopher Covington 194098add54SAndrew Jones static inline uint8_t get_pmu_version(void) 195098add54SAndrew Jones { 196098add54SAndrew Jones uint8_t ver = (get_id_aa64dfr0() >> ID_AA64DFR0_PERFMON_SHIFT) & ID_AA64DFR0_PERFMON_MASK; 197784ee933SEric Auger return ver; 198098add54SAndrew Jones } 199098add54SAndrew Jones 2008f76a347SChristopher Covington /* 2018f76a347SChristopher Covington * Extra instructions inserted by the compiler would be difficult to compensate 2028f76a347SChristopher Covington * for, so hand assemble everything between, and including, the PMCR accesses 2038f76a347SChristopher Covington * to start and stop counting. isb instructions are inserted to make sure 2048f76a347SChristopher Covington * pmccntr read after this function returns the exact instructions executed 2058f76a347SChristopher Covington * in the controlled block. Total instrs = isb + msr + 2*loop = 2 + 2*loop. 2068f76a347SChristopher Covington */ 2078f76a347SChristopher Covington static inline void precise_instrs_loop(int loop, uint32_t pmcr) 2088f76a347SChristopher Covington { 2099e186511SThomas Huth uint64_t pmcr64 = pmcr; 2108f76a347SChristopher Covington asm volatile( 2118f76a347SChristopher Covington " msr pmcr_el0, %[pmcr]\n" 2128f76a347SChristopher Covington " isb\n" 2139e186511SThomas Huth "1: subs %w[loop], %w[loop], #1\n" 2148f76a347SChristopher Covington " b.gt 1b\n" 2158f76a347SChristopher Covington " msr pmcr_el0, xzr\n" 2168f76a347SChristopher Covington " isb\n" 2178f76a347SChristopher Covington : [loop] "+r" (loop) 2189e186511SThomas Huth : [pmcr] "r" (pmcr64) 2198f76a347SChristopher Covington : "cc"); 2208f76a347SChristopher Covington } 2214870738cSEric Auger 2224870738cSEric Auger #define PMCEID1_EL0 sys_reg(3, 3, 9, 12, 7) 2234ce2a804SEric Auger #define PMCNTENSET_EL0 sys_reg(3, 3, 9, 12, 1) 2244ce2a804SEric Auger #define PMCNTENCLR_EL0 sys_reg(3, 3, 9, 12, 2) 2254ce2a804SEric Auger 2264ce2a804SEric Auger #define PMEVTYPER_EXCLUDE_EL1 BIT(31) 2274ce2a804SEric Auger #define PMEVTYPER_EXCLUDE_EL0 BIT(30) 2284870738cSEric Auger 2294870738cSEric Auger static bool is_event_supported(uint32_t n, bool warn) 2304870738cSEric Auger { 2314870738cSEric Auger uint64_t pmceid0 = read_sysreg(pmceid0_el0); 2324870738cSEric Auger uint64_t pmceid1 = read_sysreg_s(PMCEID1_EL0); 2334870738cSEric Auger bool supported; 2344870738cSEric Auger uint64_t reg; 2354870738cSEric Auger 2364870738cSEric Auger /* 2374870738cSEric Auger * The low 32-bits of PMCEID0/1 respectively describe 2384870738cSEric Auger * event support for events 0-31/32-63. Their High 2394870738cSEric Auger * 32-bits describe support for extended events 2404870738cSEric Auger * starting at 0x4000, using the same split. 2414870738cSEric Auger */ 2424870738cSEric Auger assert((n >= COMMON_EVENTS_LOW && n <= COMMON_EVENTS_HIGH) || 2434870738cSEric Auger (n >= EXT_COMMON_EVENTS_LOW && n <= EXT_COMMON_EVENTS_HIGH)); 2444870738cSEric Auger 2454870738cSEric Auger if (n <= COMMON_EVENTS_HIGH) 2464870738cSEric Auger reg = lower_32_bits(pmceid0) | ((u64)lower_32_bits(pmceid1) << 32); 2474870738cSEric Auger else 2484870738cSEric Auger reg = upper_32_bits(pmceid0) | ((u64)upper_32_bits(pmceid1) << 32); 2494870738cSEric Auger 2504870738cSEric Auger supported = reg & (1UL << (n & 0x3F)); 2514870738cSEric Auger 2524870738cSEric Auger if (!supported && warn) 2534870738cSEric Auger report_info("event 0x%x is not supported", n); 2544870738cSEric Auger return supported; 2554870738cSEric Auger } 2564870738cSEric Auger 2574870738cSEric Auger static void test_event_introspection(void) 2584870738cSEric Auger { 2594870738cSEric Auger bool required_events; 2604870738cSEric Auger 2614870738cSEric Auger if (!pmu.nb_implemented_counters) { 2624870738cSEric Auger report_skip("No event counter, skip ..."); 2634870738cSEric Auger return; 2644870738cSEric Auger } 2654870738cSEric Auger 2664870738cSEric Auger /* PMUv3 requires an implementation includes some common events */ 2674870738cSEric Auger required_events = is_event_supported(SW_INCR, true) && 2684870738cSEric Auger is_event_supported(CPU_CYCLES, true) && 2694870738cSEric Auger (is_event_supported(INST_RETIRED, true) || 2704870738cSEric Auger is_event_supported(INST_PREC, true)); 2714870738cSEric Auger 2724870738cSEric Auger if (pmu.version >= ID_DFR0_PMU_V3_8_1) { 2734870738cSEric Auger required_events = required_events && 2744870738cSEric Auger is_event_supported(STALL_FRONTEND, true) && 2754870738cSEric Auger is_event_supported(STALL_BACKEND, true); 2764870738cSEric Auger } 2774870738cSEric Auger 2784870738cSEric Auger report(required_events, "Check required events are implemented"); 2794870738cSEric Auger } 2804870738cSEric Auger 2814ce2a804SEric Auger /* 2824ce2a804SEric Auger * Extra instructions inserted by the compiler would be difficult to compensate 2834ce2a804SEric Auger * for, so hand assemble everything between, and including, the PMCR accesses 2844ce2a804SEric Auger * to start and stop counting. isb instructions are inserted to make sure 2854ce2a804SEric Auger * pmccntr read after this function returns the exact instructions executed 2864ce2a804SEric Auger * in the controlled block. Loads @loop times the data at @address into x9. 2874ce2a804SEric Auger */ 2889e186511SThomas Huth static void mem_access_loop(void *addr, long loop, uint32_t pmcr) 2894ce2a804SEric Auger { 2909e186511SThomas Huth uint64_t pmcr64 = pmcr; 2914ce2a804SEric Auger asm volatile( 2924ce2a804SEric Auger " msr pmcr_el0, %[pmcr]\n" 2934ce2a804SEric Auger " isb\n" 2944ce2a804SEric Auger " mov x10, %[loop]\n" 2954ce2a804SEric Auger "1: sub x10, x10, #1\n" 2964ce2a804SEric Auger " ldr x9, [%[addr]]\n" 2974ce2a804SEric Auger " cmp x10, #0x0\n" 2984ce2a804SEric Auger " b.gt 1b\n" 2994ce2a804SEric Auger " msr pmcr_el0, xzr\n" 3004ce2a804SEric Auger " isb\n" 3014ce2a804SEric Auger : 3029e186511SThomas Huth : [addr] "r" (addr), [pmcr] "r" (pmcr64), [loop] "r" (loop) 3034ce2a804SEric Auger : "x9", "x10", "cc"); 3044ce2a804SEric Auger } 3054ce2a804SEric Auger 3064f5ef94fSEric Auger static struct pmu_stats pmu_stats; 3074f5ef94fSEric Auger 3084f5ef94fSEric Auger static void irq_handler(struct pt_regs *regs) 3094f5ef94fSEric Auger { 3104f5ef94fSEric Auger uint32_t irqstat, irqnr; 3114f5ef94fSEric Auger 3124f5ef94fSEric Auger irqstat = gic_read_iar(); 3134f5ef94fSEric Auger irqnr = gic_iar_irqnr(irqstat); 3144f5ef94fSEric Auger 3154f5ef94fSEric Auger if (irqnr == PMU_PPI) { 3164f5ef94fSEric Auger unsigned long overflows = read_sysreg(pmovsclr_el0); 3174f5ef94fSEric Auger int i; 3184f5ef94fSEric Auger 3194f5ef94fSEric Auger for (i = 0; i < 32; i++) { 3204f5ef94fSEric Auger if (test_and_clear_bit(i, &overflows)) { 3214f5ef94fSEric Auger pmu_stats.interrupts[i]++; 3224f5ef94fSEric Auger pmu_stats.bitmap |= 1 << i; 3234f5ef94fSEric Auger } 3244f5ef94fSEric Auger } 325*7d1f853aSRicardo Koller write_sysreg(ALL_SET_32, pmovsclr_el0); 326e0a6e56bSRicardo Koller isb(); 3274f5ef94fSEric Auger } else { 3284f5ef94fSEric Auger pmu_stats.unexpected = true; 3294f5ef94fSEric Auger } 3304f5ef94fSEric Auger gic_write_eoir(irqstat); 3314f5ef94fSEric Auger } 3324f5ef94fSEric Auger 3334f5ef94fSEric Auger static void pmu_reset_stats(void) 3344f5ef94fSEric Auger { 3354f5ef94fSEric Auger int i; 3364f5ef94fSEric Auger 3374f5ef94fSEric Auger for (i = 0; i < 32; i++) 3384f5ef94fSEric Auger pmu_stats.interrupts[i] = 0; 3394f5ef94fSEric Auger 3404f5ef94fSEric Auger pmu_stats.bitmap = 0; 3414f5ef94fSEric Auger pmu_stats.unexpected = false; 3424f5ef94fSEric Auger } 3434f5ef94fSEric Auger 3444ce2a804SEric Auger static void pmu_reset(void) 3454ce2a804SEric Auger { 3464ce2a804SEric Auger /* reset all counters, counting disabled at PMCR level*/ 3474ce2a804SEric Auger set_pmcr(pmu.pmcr_ro | PMU_PMCR_LC | PMU_PMCR_C | PMU_PMCR_P); 3484ce2a804SEric Auger /* Disable all counters */ 349*7d1f853aSRicardo Koller write_sysreg_s(ALL_SET_32, PMCNTENCLR_EL0); 3504ce2a804SEric Auger /* clear overflow reg */ 351*7d1f853aSRicardo Koller write_sysreg(ALL_SET_32, pmovsclr_el0); 3524ce2a804SEric Auger /* disable overflow interrupts on all counters */ 353*7d1f853aSRicardo Koller write_sysreg(ALL_SET_32, pmintenclr_el1); 3544f5ef94fSEric Auger pmu_reset_stats(); 3554ce2a804SEric Auger isb(); 3564ce2a804SEric Auger } 3574ce2a804SEric Auger 3584ce2a804SEric Auger static void test_event_counter_config(void) 3594ce2a804SEric Auger { 3604ce2a804SEric Auger int i; 3614ce2a804SEric Auger 3624ce2a804SEric Auger if (!pmu.nb_implemented_counters) { 3634ce2a804SEric Auger report_skip("No event counter, skip ..."); 3644ce2a804SEric Auger return; 3654ce2a804SEric Auger } 3664ce2a804SEric Auger 3674ce2a804SEric Auger pmu_reset(); 3684ce2a804SEric Auger 3694ce2a804SEric Auger /* 3704ce2a804SEric Auger * Test setting through PMESELR/PMXEVTYPER and PMEVTYPERn read, 3714ce2a804SEric Auger * select counter 0 3724ce2a804SEric Auger */ 3734ce2a804SEric Auger write_sysreg(1, PMSELR_EL0); 3744ce2a804SEric Auger /* program this counter to count unsupported event */ 3754ce2a804SEric Auger write_sysreg(0xEA, PMXEVTYPER_EL0); 3764ce2a804SEric Auger write_sysreg(0xdeadbeef, PMXEVCNTR_EL0); 3774ce2a804SEric Auger report((read_regn_el0(pmevtyper, 1) & 0xFFF) == 0xEA, 3784ce2a804SEric Auger "PMESELR/PMXEVTYPER/PMEVTYPERn"); 3794ce2a804SEric Auger report((read_regn_el0(pmevcntr, 1) == 0xdeadbeef), 3804ce2a804SEric Auger "PMESELR/PMXEVCNTR/PMEVCNTRn"); 3814ce2a804SEric Auger 3824ce2a804SEric Auger /* try to configure an unsupported event within the range [0x0, 0x3F] */ 3834ce2a804SEric Auger for (i = 0; i <= 0x3F; i++) { 3844ce2a804SEric Auger if (!is_event_supported(i, false)) 3854ce2a804SEric Auger break; 3864ce2a804SEric Auger } 3874ce2a804SEric Auger if (i > 0x3F) { 3884ce2a804SEric Auger report_skip("pmevtyper: all events within [0x0, 0x3F] are supported"); 3894ce2a804SEric Auger return; 3904ce2a804SEric Auger } 3914ce2a804SEric Auger 3924ce2a804SEric Auger /* select counter 0 */ 3934ce2a804SEric Auger write_sysreg(0, PMSELR_EL0); 3944ce2a804SEric Auger /* program this counter to count unsupported event */ 3954ce2a804SEric Auger write_sysreg(i, PMXEVCNTR_EL0); 3964ce2a804SEric Auger /* read the counter value */ 3974ce2a804SEric Auger read_sysreg(PMXEVCNTR_EL0); 3984ce2a804SEric Auger report(read_sysreg(PMXEVCNTR_EL0) == i, 3994ce2a804SEric Auger "read of a counter programmed with unsupported event"); 4004ce2a804SEric Auger } 4014ce2a804SEric Auger 4024ce2a804SEric Auger static bool satisfy_prerequisites(uint32_t *events, unsigned int nb_events) 4034ce2a804SEric Auger { 4044ce2a804SEric Auger int i; 4054ce2a804SEric Auger 4064ce2a804SEric Auger if (pmu.nb_implemented_counters < nb_events) { 4074ce2a804SEric Auger report_skip("Skip test as number of counters is too small (%d)", 4084ce2a804SEric Auger pmu.nb_implemented_counters); 4094ce2a804SEric Auger return false; 4104ce2a804SEric Auger } 4114ce2a804SEric Auger 4124ce2a804SEric Auger for (i = 0; i < nb_events; i++) { 4134ce2a804SEric Auger if (!is_event_supported(events[i], false)) { 4144ce2a804SEric Auger report_skip("Skip test as event 0x%x is not supported", 4154ce2a804SEric Auger events[i]); 4164ce2a804SEric Auger return false; 4174ce2a804SEric Auger } 4184ce2a804SEric Auger } 4194ce2a804SEric Auger return true; 4204ce2a804SEric Auger } 4214ce2a804SEric Auger 42239d1347aSRicardo Koller static uint64_t pmevcntr_mask(void) 42339d1347aSRicardo Koller { 42439d1347aSRicardo Koller /* 42539d1347aSRicardo Koller * Bits [63:0] are always incremented for 64-bit counters, 42639d1347aSRicardo Koller * even if the PMU is configured to generate an overflow at 42739d1347aSRicardo Koller * bits [31:0] 42839d1347aSRicardo Koller * 42939d1347aSRicardo Koller * For more details see the AArch64.IncrementEventCounter() 43039d1347aSRicardo Koller * pseudo-code in the ARM ARM DDI 0487I.a, section J1.1.1. 43139d1347aSRicardo Koller */ 43239d1347aSRicardo Koller if (pmu.version >= ID_DFR0_PMU_V3_8_5) 43339d1347aSRicardo Koller return ~0; 43439d1347aSRicardo Koller 43539d1347aSRicardo Koller return (uint32_t)~0; 43639d1347aSRicardo Koller } 43739d1347aSRicardo Koller 438041df25bSRicardo Koller static bool check_overflow_prerequisites(bool overflow_at_64bits) 439041df25bSRicardo Koller { 440041df25bSRicardo Koller if (overflow_at_64bits && pmu.version < ID_DFR0_PMU_V3_8_5) { 441041df25bSRicardo Koller report_skip("Skip test as 64 overflows need FEAT_PMUv3p5"); 442041df25bSRicardo Koller return false; 443041df25bSRicardo Koller } 444041df25bSRicardo Koller 445041df25bSRicardo Koller return true; 446041df25bSRicardo Koller } 447041df25bSRicardo Koller 448041df25bSRicardo Koller static void test_basic_event_count(bool overflow_at_64bits) 4494ce2a804SEric Auger { 4504ce2a804SEric Auger uint32_t implemented_counter_mask, non_implemented_counter_mask; 4514ce2a804SEric Auger uint32_t counter_mask; 4524ce2a804SEric Auger uint32_t events[] = {CPU_CYCLES, INST_RETIRED}; 4534ce2a804SEric Auger 454041df25bSRicardo Koller if (!satisfy_prerequisites(events, ARRAY_SIZE(events)) || 455041df25bSRicardo Koller !check_overflow_prerequisites(overflow_at_64bits)) 4564ce2a804SEric Auger return; 4574ce2a804SEric Auger 4584ce2a804SEric Auger implemented_counter_mask = BIT(pmu.nb_implemented_counters) - 1; 4594ce2a804SEric Auger non_implemented_counter_mask = ~(BIT(31) | implemented_counter_mask); 4604ce2a804SEric Auger counter_mask = implemented_counter_mask | non_implemented_counter_mask; 4614ce2a804SEric Auger 4624ce2a804SEric Auger write_regn_el0(pmevtyper, 0, CPU_CYCLES | PMEVTYPER_EXCLUDE_EL0); 4634ce2a804SEric Auger write_regn_el0(pmevtyper, 1, INST_RETIRED | PMEVTYPER_EXCLUDE_EL0); 4644ce2a804SEric Auger 4654ce2a804SEric Auger /* disable all counters */ 466*7d1f853aSRicardo Koller write_sysreg_s(ALL_SET_32, PMCNTENCLR_EL0); 4674ce2a804SEric Auger report(!read_sysreg_s(PMCNTENCLR_EL0) && !read_sysreg_s(PMCNTENSET_EL0), 4684ce2a804SEric Auger "pmcntenclr: disable all counters"); 4694ce2a804SEric Auger 4704ce2a804SEric Auger /* 4714ce2a804SEric Auger * clear cycle and all event counters and allow counter enablement 4724ce2a804SEric Auger * through PMCNTENSET. LC is RES1. 4734ce2a804SEric Auger */ 4744ce2a804SEric Auger set_pmcr(pmu.pmcr_ro | PMU_PMCR_LC | PMU_PMCR_C | PMU_PMCR_P); 4754ce2a804SEric Auger isb(); 4764ce2a804SEric Auger report(get_pmcr() == (pmu.pmcr_ro | PMU_PMCR_LC), "pmcr: reset counters"); 4774ce2a804SEric Auger 4784ce2a804SEric Auger /* Preset counter #0 to pre overflow value to trigger an overflow */ 479*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW_32); 480*7d1f853aSRicardo Koller report(read_regn_el0(pmevcntr, 0) == PRE_OVERFLOW_32, 4814ce2a804SEric Auger "counter #0 preset to pre-overflow value"); 4824ce2a804SEric Auger report(!read_regn_el0(pmevcntr, 1), "counter #1 is 0"); 4834ce2a804SEric Auger 4844ce2a804SEric Auger /* 4854ce2a804SEric Auger * Enable all implemented counters and also attempt to enable 4864ce2a804SEric Auger * not supported counters. Counting still is disabled by !PMCR.E 4874ce2a804SEric Auger */ 4884ce2a804SEric Auger write_sysreg_s(counter_mask, PMCNTENSET_EL0); 4894ce2a804SEric Auger 4904ce2a804SEric Auger /* check only those implemented are enabled */ 4914ce2a804SEric Auger report((read_sysreg_s(PMCNTENSET_EL0) == read_sysreg_s(PMCNTENCLR_EL0)) && 4924ce2a804SEric Auger (read_sysreg_s(PMCNTENSET_EL0) == implemented_counter_mask), 4934ce2a804SEric Auger "pmcntenset: enabled implemented_counters"); 4944ce2a804SEric Auger 4954ce2a804SEric Auger /* Disable all counters but counters #0 and #1 */ 4964ce2a804SEric Auger write_sysreg_s(~0x3, PMCNTENCLR_EL0); 4974ce2a804SEric Auger report((read_sysreg_s(PMCNTENSET_EL0) == read_sysreg_s(PMCNTENCLR_EL0)) && 4984ce2a804SEric Auger (read_sysreg_s(PMCNTENSET_EL0) == 0x3), 4994ce2a804SEric Auger "pmcntenset: just enabled #0 and #1"); 5004ce2a804SEric Auger 5014ce2a804SEric Auger /* clear overflow register */ 502*7d1f853aSRicardo Koller write_sysreg(ALL_SET_32, pmovsclr_el0); 5034ce2a804SEric Auger report(!read_sysreg(pmovsclr_el0), "check overflow reg is 0"); 5044ce2a804SEric Auger 5054ce2a804SEric Auger /* disable overflow interrupts on all counters*/ 506*7d1f853aSRicardo Koller write_sysreg(ALL_SET_32, pmintenclr_el1); 5074ce2a804SEric Auger report(!read_sysreg(pmintenclr_el1), 5084ce2a804SEric Auger "pmintenclr_el1=0, all interrupts disabled"); 5094ce2a804SEric Auger 5104ce2a804SEric Auger /* enable overflow interrupts on all event counters */ 5114ce2a804SEric Auger write_sysreg(implemented_counter_mask | non_implemented_counter_mask, 5124ce2a804SEric Auger pmintenset_el1); 5134ce2a804SEric Auger report(read_sysreg(pmintenset_el1) == implemented_counter_mask, 5144ce2a804SEric Auger "overflow interrupts enabled on all implemented counters"); 5154ce2a804SEric Auger 5164ce2a804SEric Auger /* Set PMCR.E, execute asm code and unset PMCR.E */ 5174ce2a804SEric Auger precise_instrs_loop(20, pmu.pmcr_ro | PMU_PMCR_E); 5184ce2a804SEric Auger 5194ce2a804SEric Auger report_info("counter #0 is 0x%lx (CPU_CYCLES)", 5204ce2a804SEric Auger read_regn_el0(pmevcntr, 0)); 5214ce2a804SEric Auger report_info("counter #1 is 0x%lx (INST_RETIRED)", 5224ce2a804SEric Auger read_regn_el0(pmevcntr, 1)); 5234ce2a804SEric Auger 5244ce2a804SEric Auger report_info("overflow reg = 0x%lx", read_sysreg(pmovsclr_el0)); 5254ce2a804SEric Auger report(read_sysreg(pmovsclr_el0) & 0x1, 5264ce2a804SEric Auger "check overflow happened on #0 only"); 5274ce2a804SEric Auger } 5284ce2a804SEric Auger 529041df25bSRicardo Koller static void test_mem_access(bool overflow_at_64bits) 5304ce2a804SEric Auger { 5314ce2a804SEric Auger void *addr = malloc(PAGE_SIZE); 5324ce2a804SEric Auger uint32_t events[] = {MEM_ACCESS, MEM_ACCESS}; 5334ce2a804SEric Auger 534041df25bSRicardo Koller if (!satisfy_prerequisites(events, ARRAY_SIZE(events)) || 535041df25bSRicardo Koller !check_overflow_prerequisites(overflow_at_64bits)) 5364ce2a804SEric Auger return; 5374ce2a804SEric Auger 5384ce2a804SEric Auger pmu_reset(); 5394ce2a804SEric Auger 5404ce2a804SEric Auger write_regn_el0(pmevtyper, 0, MEM_ACCESS | PMEVTYPER_EXCLUDE_EL0); 5414ce2a804SEric Auger write_regn_el0(pmevtyper, 1, MEM_ACCESS | PMEVTYPER_EXCLUDE_EL0); 5424ce2a804SEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 5434ce2a804SEric Auger isb(); 5444ce2a804SEric Auger mem_access_loop(addr, 20, pmu.pmcr_ro | PMU_PMCR_E); 5454ce2a804SEric Auger report_info("counter #0 is %ld (MEM_ACCESS)", read_regn_el0(pmevcntr, 0)); 5464ce2a804SEric Auger report_info("counter #1 is %ld (MEM_ACCESS)", read_regn_el0(pmevcntr, 1)); 5474ce2a804SEric Auger /* We may measure more than 20 mem access depending on the core */ 5484ce2a804SEric Auger report((read_regn_el0(pmevcntr, 0) == read_regn_el0(pmevcntr, 1)) && 5494ce2a804SEric Auger (read_regn_el0(pmevcntr, 0) >= 20) && !read_sysreg(pmovsclr_el0), 5504ce2a804SEric Auger "Ran 20 mem accesses"); 5514ce2a804SEric Auger 5524ce2a804SEric Auger pmu_reset(); 5534ce2a804SEric Auger 554*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW_32); 555*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 1, PRE_OVERFLOW_32); 5564ce2a804SEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 5574ce2a804SEric Auger isb(); 5584ce2a804SEric Auger mem_access_loop(addr, 20, pmu.pmcr_ro | PMU_PMCR_E); 5594ce2a804SEric Auger report(read_sysreg(pmovsclr_el0) == 0x3, 5604ce2a804SEric Auger "Ran 20 mem accesses with expected overflows on both counters"); 5614ce2a804SEric Auger report_info("cnt#0 = %ld cnt#1=%ld overflow=0x%lx", 5624ce2a804SEric Auger read_regn_el0(pmevcntr, 0), read_regn_el0(pmevcntr, 1), 5634ce2a804SEric Auger read_sysreg(pmovsclr_el0)); 5644ce2a804SEric Auger } 5654ce2a804SEric Auger 566041df25bSRicardo Koller static void test_sw_incr(bool overflow_at_64bits) 567bb9a5adcSEric Auger { 568bb9a5adcSEric Auger uint32_t events[] = {SW_INCR, SW_INCR}; 569*7d1f853aSRicardo Koller uint64_t cntr0 = (PRE_OVERFLOW_32 + 100) & pmevcntr_mask(); 570bb9a5adcSEric Auger int i; 571bb9a5adcSEric Auger 572041df25bSRicardo Koller if (!satisfy_prerequisites(events, ARRAY_SIZE(events)) || 573041df25bSRicardo Koller !check_overflow_prerequisites(overflow_at_64bits)) 574bb9a5adcSEric Auger return; 575bb9a5adcSEric Auger 576bb9a5adcSEric Auger pmu_reset(); 577bb9a5adcSEric Auger 578bb9a5adcSEric Auger write_regn_el0(pmevtyper, 0, SW_INCR | PMEVTYPER_EXCLUDE_EL0); 579bb9a5adcSEric Auger write_regn_el0(pmevtyper, 1, SW_INCR | PMEVTYPER_EXCLUDE_EL0); 580bb9a5adcSEric Auger /* enable counters #0 and #1 */ 581bb9a5adcSEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 582bb9a5adcSEric Auger 583*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW_32); 584e0a6e56bSRicardo Koller isb(); 585bb9a5adcSEric Auger 586bb9a5adcSEric Auger for (i = 0; i < 100; i++) 587bb9a5adcSEric Auger write_sysreg(0x1, pmswinc_el0); 588bb9a5adcSEric Auger 589e0a6e56bSRicardo Koller isb(); 590bb9a5adcSEric Auger report_info("SW_INCR counter #0 has value %ld", read_regn_el0(pmevcntr, 0)); 591*7d1f853aSRicardo Koller report(read_regn_el0(pmevcntr, 0) == PRE_OVERFLOW_32, 592bb9a5adcSEric Auger "PWSYNC does not increment if PMCR.E is unset"); 593bb9a5adcSEric Auger 594bb9a5adcSEric Auger pmu_reset(); 595bb9a5adcSEric Auger 596*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW_32); 597bb9a5adcSEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 598bb9a5adcSEric Auger set_pmcr(pmu.pmcr_ro | PMU_PMCR_E); 599e0a6e56bSRicardo Koller isb(); 600bb9a5adcSEric Auger 601bb9a5adcSEric Auger for (i = 0; i < 100; i++) 602bb9a5adcSEric Auger write_sysreg(0x3, pmswinc_el0); 603bb9a5adcSEric Auger 604e0a6e56bSRicardo Koller isb(); 60539d1347aSRicardo Koller report(read_regn_el0(pmevcntr, 0) == cntr0, "counter #0 after + 100 SW_INCR"); 60639d1347aSRicardo Koller report(read_regn_el0(pmevcntr, 1) == 100, "counter #1 after + 100 SW_INCR"); 607bb9a5adcSEric Auger report_info("counter values after 100 SW_INCR #0=%ld #1=%ld", 608bb9a5adcSEric Auger read_regn_el0(pmevcntr, 0), read_regn_el0(pmevcntr, 1)); 609bb9a5adcSEric Auger report(read_sysreg(pmovsclr_el0) == 0x1, 61066fee034SEric Auger "overflow on counter #0 after 100 SW_INCR"); 61166fee034SEric Auger } 61266fee034SEric Auger 613041df25bSRicardo Koller static void test_chained_counters(bool unused) 61466fee034SEric Auger { 61566fee034SEric Auger uint32_t events[] = {CPU_CYCLES, CHAIN}; 61666fee034SEric Auger 61766fee034SEric Auger if (!satisfy_prerequisites(events, ARRAY_SIZE(events))) 61866fee034SEric Auger return; 61966fee034SEric Auger 62066fee034SEric Auger pmu_reset(); 62166fee034SEric Auger 62266fee034SEric Auger write_regn_el0(pmevtyper, 0, CPU_CYCLES | PMEVTYPER_EXCLUDE_EL0); 62366fee034SEric Auger write_regn_el0(pmevtyper, 1, CHAIN | PMEVTYPER_EXCLUDE_EL0); 62466fee034SEric Auger /* enable counters #0 and #1 */ 62566fee034SEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 626*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW_32); 62766fee034SEric Auger 62866fee034SEric Auger precise_instrs_loop(22, pmu.pmcr_ro | PMU_PMCR_E); 62966fee034SEric Auger 63066fee034SEric Auger report(read_regn_el0(pmevcntr, 1) == 1, "CHAIN counter #1 incremented"); 631b5489580SRicardo Koller report(read_sysreg(pmovsclr_el0) == 0x1, "overflow recorded for chained incr #1"); 63266fee034SEric Auger 63366fee034SEric Auger /* test 64b overflow */ 63466fee034SEric Auger 63566fee034SEric Auger pmu_reset(); 63666fee034SEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 63766fee034SEric Auger 638*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW_32); 63966fee034SEric Auger write_regn_el0(pmevcntr, 1, 0x1); 64066fee034SEric Auger precise_instrs_loop(22, pmu.pmcr_ro | PMU_PMCR_E); 64166fee034SEric Auger report_info("overflow reg = 0x%lx", read_sysreg(pmovsclr_el0)); 64266fee034SEric Auger report(read_regn_el0(pmevcntr, 1) == 2, "CHAIN counter #1 set to 2"); 643b5489580SRicardo Koller report(read_sysreg(pmovsclr_el0) == 0x1, "overflow recorded for chained incr #2"); 64466fee034SEric Auger 645*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW_32); 646*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 1, ALL_SET_32); 64766fee034SEric Auger 64866fee034SEric Auger precise_instrs_loop(22, pmu.pmcr_ro | PMU_PMCR_E); 64966fee034SEric Auger report_info("overflow reg = 0x%lx", read_sysreg(pmovsclr_el0)); 65066fee034SEric Auger report(!read_regn_el0(pmevcntr, 1), "CHAIN counter #1 wrapped"); 651b5489580SRicardo Koller report(read_sysreg(pmovsclr_el0) == 0x3, "overflow on even and odd counters"); 65266fee034SEric Auger } 65366fee034SEric Auger 654041df25bSRicardo Koller static void test_chained_sw_incr(bool unused) 65566fee034SEric Auger { 65666fee034SEric Auger uint32_t events[] = {SW_INCR, CHAIN}; 657*7d1f853aSRicardo Koller uint64_t cntr0 = (PRE_OVERFLOW_32 + 100) & pmevcntr_mask(); 658*7d1f853aSRicardo Koller uint64_t cntr1 = (ALL_SET_32 + 1) & pmevcntr_mask(); 65966fee034SEric Auger int i; 66066fee034SEric Auger 66166fee034SEric Auger if (!satisfy_prerequisites(events, ARRAY_SIZE(events))) 66266fee034SEric Auger return; 66366fee034SEric Auger 66466fee034SEric Auger pmu_reset(); 66566fee034SEric Auger 66666fee034SEric Auger write_regn_el0(pmevtyper, 0, SW_INCR | PMEVTYPER_EXCLUDE_EL0); 66766fee034SEric Auger write_regn_el0(pmevtyper, 1, CHAIN | PMEVTYPER_EXCLUDE_EL0); 66866fee034SEric Auger /* enable counters #0 and #1 */ 66966fee034SEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 67066fee034SEric Auger 671*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW_32); 67266fee034SEric Auger set_pmcr(pmu.pmcr_ro | PMU_PMCR_E); 673e0a6e56bSRicardo Koller isb(); 674e0a6e56bSRicardo Koller 67566fee034SEric Auger for (i = 0; i < 100; i++) 67666fee034SEric Auger write_sysreg(0x1, pmswinc_el0); 67766fee034SEric Auger 678e0a6e56bSRicardo Koller isb(); 679b5489580SRicardo Koller report((read_sysreg(pmovsclr_el0) == 0x1) && 680b5489580SRicardo Koller (read_regn_el0(pmevcntr, 1) == 1), 681b5489580SRicardo Koller "overflow and chain counter incremented after 100 SW_INCR/CHAIN"); 68266fee034SEric Auger report_info("overflow=0x%lx, #0=%ld #1=%ld", read_sysreg(pmovsclr_el0), 68366fee034SEric Auger read_regn_el0(pmevcntr, 0), read_regn_el0(pmevcntr, 1)); 68466fee034SEric Auger 68566fee034SEric Auger /* 64b SW_INCR and overflow on CHAIN counter*/ 68666fee034SEric Auger pmu_reset(); 68766fee034SEric Auger 68866fee034SEric Auger write_regn_el0(pmevtyper, 1, events[1] | PMEVTYPER_EXCLUDE_EL0); 689*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW_32); 690*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 1, ALL_SET_32); 69166fee034SEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 69266fee034SEric Auger set_pmcr(pmu.pmcr_ro | PMU_PMCR_E); 693e0a6e56bSRicardo Koller isb(); 694e0a6e56bSRicardo Koller 69566fee034SEric Auger for (i = 0; i < 100; i++) 69666fee034SEric Auger write_sysreg(0x1, pmswinc_el0); 69766fee034SEric Auger 698e0a6e56bSRicardo Koller isb(); 699b5489580SRicardo Koller report((read_sysreg(pmovsclr_el0) == 0x3) && 70039d1347aSRicardo Koller (read_regn_el0(pmevcntr, 0) == cntr0) && 70139d1347aSRicardo Koller (read_regn_el0(pmevcntr, 1) == cntr1), 702b5489580SRicardo Koller "expected overflows and values after 100 SW_INCR/CHAIN"); 70366fee034SEric Auger report_info("overflow=0x%lx, #0=%ld #1=%ld", read_sysreg(pmovsclr_el0), 70466fee034SEric Auger read_regn_el0(pmevcntr, 0), read_regn_el0(pmevcntr, 1)); 705bb9a5adcSEric Auger } 706bb9a5adcSEric Auger 707041df25bSRicardo Koller static void test_chain_promotion(bool unused) 708ca42f29aSEric Auger { 709ca42f29aSEric Auger uint32_t events[] = {MEM_ACCESS, CHAIN}; 710ca42f29aSEric Auger void *addr = malloc(PAGE_SIZE); 711ca42f29aSEric Auger 712ca42f29aSEric Auger if (!satisfy_prerequisites(events, ARRAY_SIZE(events))) 713ca42f29aSEric Auger return; 714ca42f29aSEric Auger 715ca42f29aSEric Auger /* Only enable CHAIN counter */ 716ca42f29aSEric Auger pmu_reset(); 717ca42f29aSEric Auger write_regn_el0(pmevtyper, 0, MEM_ACCESS | PMEVTYPER_EXCLUDE_EL0); 718ca42f29aSEric Auger write_regn_el0(pmevtyper, 1, CHAIN | PMEVTYPER_EXCLUDE_EL0); 719ca42f29aSEric Auger write_sysreg_s(0x2, PMCNTENSET_EL0); 720ca42f29aSEric Auger isb(); 721ca42f29aSEric Auger 722ca42f29aSEric Auger mem_access_loop(addr, 20, pmu.pmcr_ro | PMU_PMCR_E); 723ca42f29aSEric Auger report(!read_regn_el0(pmevcntr, 0), 724ca42f29aSEric Auger "chain counter not counting if even counter is disabled"); 725ca42f29aSEric Auger 726ca42f29aSEric Auger /* Only enable even counter */ 727ca42f29aSEric Auger pmu_reset(); 728*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW_32); 729ca42f29aSEric Auger write_sysreg_s(0x1, PMCNTENSET_EL0); 730ca42f29aSEric Auger isb(); 731ca42f29aSEric Auger 732ca42f29aSEric Auger mem_access_loop(addr, 20, pmu.pmcr_ro | PMU_PMCR_E); 733ca42f29aSEric Auger report(!read_regn_el0(pmevcntr, 1) && (read_sysreg(pmovsclr_el0) == 0x1), 734ca42f29aSEric Auger "odd counter did not increment on overflow if disabled"); 735ca42f29aSEric Auger report_info("MEM_ACCESS counter #0 has value %ld", 736ca42f29aSEric Auger read_regn_el0(pmevcntr, 0)); 737ca42f29aSEric Auger report_info("CHAIN counter #1 has value %ld", 738ca42f29aSEric Auger read_regn_el0(pmevcntr, 1)); 739ca42f29aSEric Auger report_info("overflow counter %ld", read_sysreg(pmovsclr_el0)); 740ca42f29aSEric Auger 741ca42f29aSEric Auger /* start at 0xFFFFFFDC, +20 with CHAIN enabled, +20 with CHAIN disabled */ 742ca42f29aSEric Auger pmu_reset(); 743ca42f29aSEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 744*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW2_32); 745ca42f29aSEric Auger isb(); 746ca42f29aSEric Auger 747ca42f29aSEric Auger mem_access_loop(addr, 20, pmu.pmcr_ro | PMU_PMCR_E); 748ca42f29aSEric Auger report_info("MEM_ACCESS counter #0 has value 0x%lx", 749ca42f29aSEric Auger read_regn_el0(pmevcntr, 0)); 750ca42f29aSEric Auger 751ca42f29aSEric Auger /* disable the CHAIN event */ 752ca42f29aSEric Auger write_sysreg_s(0x2, PMCNTENCLR_EL0); 753ca42f29aSEric Auger mem_access_loop(addr, 20, pmu.pmcr_ro | PMU_PMCR_E); 754ca42f29aSEric Auger report_info("MEM_ACCESS counter #0 has value 0x%lx", 755ca42f29aSEric Auger read_regn_el0(pmevcntr, 0)); 756ca42f29aSEric Auger report(read_sysreg(pmovsclr_el0) == 0x1, 757ca42f29aSEric Auger "should have triggered an overflow on #0"); 758ca42f29aSEric Auger report(!read_regn_el0(pmevcntr, 1), 759ca42f29aSEric Auger "CHAIN counter #1 shouldn't have incremented"); 760ca42f29aSEric Auger 761ca42f29aSEric Auger /* start at 0xFFFFFFDC, +20 with CHAIN disabled, +20 with CHAIN enabled */ 762ca42f29aSEric Auger 763ca42f29aSEric Auger pmu_reset(); 764ca42f29aSEric Auger write_sysreg_s(0x1, PMCNTENSET_EL0); 765*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW2_32); 766ca42f29aSEric Auger isb(); 767ca42f29aSEric Auger report_info("counter #0 = 0x%lx, counter #1 = 0x%lx overflow=0x%lx", 768ca42f29aSEric Auger read_regn_el0(pmevcntr, 0), read_regn_el0(pmevcntr, 1), 769ca42f29aSEric Auger read_sysreg(pmovsclr_el0)); 770ca42f29aSEric Auger 771ca42f29aSEric Auger mem_access_loop(addr, 20, pmu.pmcr_ro | PMU_PMCR_E); 772ca42f29aSEric Auger report_info("MEM_ACCESS counter #0 has value 0x%lx", 773ca42f29aSEric Auger read_regn_el0(pmevcntr, 0)); 774ca42f29aSEric Auger 775ca42f29aSEric Auger /* enable the CHAIN event */ 776ca42f29aSEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 777ca42f29aSEric Auger isb(); 778ca42f29aSEric Auger mem_access_loop(addr, 20, pmu.pmcr_ro | PMU_PMCR_E); 779ca42f29aSEric Auger report_info("MEM_ACCESS counter #0 has value 0x%lx", 780ca42f29aSEric Auger read_regn_el0(pmevcntr, 0)); 781ca42f29aSEric Auger 782b5489580SRicardo Koller report((read_regn_el0(pmevcntr, 1) == 1) && 783b5489580SRicardo Koller (read_sysreg(pmovsclr_el0) == 0x1), 784b5489580SRicardo Koller "CHAIN counter enabled: CHAIN counter was incremented and overflow"); 785ca42f29aSEric Auger 786ca42f29aSEric Auger report_info("CHAIN counter #1 = 0x%lx, overflow=0x%lx", 787ca42f29aSEric Auger read_regn_el0(pmevcntr, 1), read_sysreg(pmovsclr_el0)); 788ca42f29aSEric Auger 789ca42f29aSEric Auger /* start as MEM_ACCESS/CPU_CYCLES and move to CHAIN/MEM_ACCESS */ 790ca42f29aSEric Auger pmu_reset(); 791ca42f29aSEric Auger write_regn_el0(pmevtyper, 0, MEM_ACCESS | PMEVTYPER_EXCLUDE_EL0); 792ca42f29aSEric Auger write_regn_el0(pmevtyper, 1, CPU_CYCLES | PMEVTYPER_EXCLUDE_EL0); 793ca42f29aSEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 794*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW2_32); 795ca42f29aSEric Auger isb(); 796ca42f29aSEric Auger 797ca42f29aSEric Auger mem_access_loop(addr, 20, pmu.pmcr_ro | PMU_PMCR_E); 798ca42f29aSEric Auger report_info("MEM_ACCESS counter #0 has value 0x%lx", 799ca42f29aSEric Auger read_regn_el0(pmevcntr, 0)); 800ca42f29aSEric Auger 801ca42f29aSEric Auger /* 0 becomes CHAINED */ 802ca42f29aSEric Auger write_sysreg_s(0x0, PMCNTENSET_EL0); 803ca42f29aSEric Auger write_regn_el0(pmevtyper, 1, CHAIN | PMEVTYPER_EXCLUDE_EL0); 804ca42f29aSEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 805ca42f29aSEric Auger write_regn_el0(pmevcntr, 1, 0x0); 806ca42f29aSEric Auger 807ca42f29aSEric Auger mem_access_loop(addr, 20, pmu.pmcr_ro | PMU_PMCR_E); 808ca42f29aSEric Auger report_info("MEM_ACCESS counter #0 has value 0x%lx", 809ca42f29aSEric Auger read_regn_el0(pmevcntr, 0)); 810ca42f29aSEric Auger 811b5489580SRicardo Koller report((read_regn_el0(pmevcntr, 1) == 1) && 812b5489580SRicardo Koller (read_sysreg(pmovsclr_el0) == 0x1), 813b5489580SRicardo Koller "32b->64b: CHAIN counter incremented and overflow"); 814ca42f29aSEric Auger 815ca42f29aSEric Auger report_info("CHAIN counter #1 = 0x%lx, overflow=0x%lx", 816ca42f29aSEric Auger read_regn_el0(pmevcntr, 1), read_sysreg(pmovsclr_el0)); 817ca42f29aSEric Auger 818ca42f29aSEric Auger /* start as CHAIN/MEM_ACCESS and move to MEM_ACCESS/CPU_CYCLES */ 819ca42f29aSEric Auger pmu_reset(); 820ca42f29aSEric Auger write_regn_el0(pmevtyper, 0, MEM_ACCESS | PMEVTYPER_EXCLUDE_EL0); 821ca42f29aSEric Auger write_regn_el0(pmevtyper, 1, CHAIN | PMEVTYPER_EXCLUDE_EL0); 822*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW2_32); 823ca42f29aSEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 824ca42f29aSEric Auger 825ca42f29aSEric Auger mem_access_loop(addr, 20, pmu.pmcr_ro | PMU_PMCR_E); 826ca42f29aSEric Auger report_info("counter #0=0x%lx, counter #1=0x%lx", 827ca42f29aSEric Auger read_regn_el0(pmevcntr, 0), read_regn_el0(pmevcntr, 1)); 828ca42f29aSEric Auger 829ca42f29aSEric Auger write_sysreg_s(0x0, PMCNTENSET_EL0); 830ca42f29aSEric Auger write_regn_el0(pmevtyper, 1, CPU_CYCLES | PMEVTYPER_EXCLUDE_EL0); 831ca42f29aSEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 832ca42f29aSEric Auger 833ca42f29aSEric Auger mem_access_loop(addr, 20, pmu.pmcr_ro | PMU_PMCR_E); 834ca42f29aSEric Auger report(read_sysreg(pmovsclr_el0) == 1, 835ca42f29aSEric Auger "overflow is expected on counter 0"); 836ca42f29aSEric Auger report_info("counter #0=0x%lx, counter #1=0x%lx overflow=0x%lx", 837ca42f29aSEric Auger read_regn_el0(pmevcntr, 0), read_regn_el0(pmevcntr, 1), 838ca42f29aSEric Auger read_sysreg(pmovsclr_el0)); 839ca42f29aSEric Auger } 840ca42f29aSEric Auger 8414f5ef94fSEric Auger static bool expect_interrupts(uint32_t bitmap) 8424f5ef94fSEric Auger { 8434f5ef94fSEric Auger int i; 8444f5ef94fSEric Auger 8454f5ef94fSEric Auger if (pmu_stats.bitmap ^ bitmap || pmu_stats.unexpected) 8464f5ef94fSEric Auger return false; 8474f5ef94fSEric Auger 8484f5ef94fSEric Auger for (i = 0; i < 32; i++) { 8494f5ef94fSEric Auger if (test_and_clear_bit(i, &pmu_stats.bitmap)) 8504f5ef94fSEric Auger if (pmu_stats.interrupts[i] != 1) 8514f5ef94fSEric Auger return false; 8524f5ef94fSEric Auger } 8534f5ef94fSEric Auger return true; 8544f5ef94fSEric Auger } 8554f5ef94fSEric Auger 856041df25bSRicardo Koller static void test_overflow_interrupt(bool overflow_at_64bits) 8574f5ef94fSEric Auger { 8584f5ef94fSEric Auger uint32_t events[] = {MEM_ACCESS, SW_INCR}; 8594f5ef94fSEric Auger void *addr = malloc(PAGE_SIZE); 8604f5ef94fSEric Auger int i; 8614f5ef94fSEric Auger 862041df25bSRicardo Koller if (!satisfy_prerequisites(events, ARRAY_SIZE(events)) || 863041df25bSRicardo Koller !check_overflow_prerequisites(overflow_at_64bits)) 8644f5ef94fSEric Auger return; 8654f5ef94fSEric Auger 8664f5ef94fSEric Auger gic_enable_defaults(); 8674f5ef94fSEric Auger install_irq_handler(EL1H_IRQ, irq_handler); 8684f5ef94fSEric Auger local_irq_enable(); 8694f5ef94fSEric Auger gic_enable_irq(23); 8704f5ef94fSEric Auger 8714f5ef94fSEric Auger pmu_reset(); 8724f5ef94fSEric Auger 8734f5ef94fSEric Auger write_regn_el0(pmevtyper, 0, MEM_ACCESS | PMEVTYPER_EXCLUDE_EL0); 8744f5ef94fSEric Auger write_regn_el0(pmevtyper, 1, SW_INCR | PMEVTYPER_EXCLUDE_EL0); 8754f5ef94fSEric Auger write_sysreg_s(0x3, PMCNTENSET_EL0); 876*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW_32); 877*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 1, PRE_OVERFLOW_32); 8784f5ef94fSEric Auger isb(); 8794f5ef94fSEric Auger 8801a97dad8SRicardo Koller /* interrupts are disabled (PMINTENSET_EL1 == 0) */ 8814f5ef94fSEric Auger 8824f5ef94fSEric Auger mem_access_loop(addr, 200, pmu.pmcr_ro | PMU_PMCR_E); 8834f5ef94fSEric Auger report(expect_interrupts(0), "no overflow interrupt after preset"); 8844f5ef94fSEric Auger 8854f5ef94fSEric Auger set_pmcr(pmu.pmcr_ro | PMU_PMCR_E); 886e0a6e56bSRicardo Koller isb(); 887e0a6e56bSRicardo Koller 8884f5ef94fSEric Auger for (i = 0; i < 100; i++) 8894f5ef94fSEric Auger write_sysreg(0x2, pmswinc_el0); 8904f5ef94fSEric Auger 891e0a6e56bSRicardo Koller isb(); 8924f5ef94fSEric Auger set_pmcr(pmu.pmcr_ro); 893e0a6e56bSRicardo Koller isb(); 8944f5ef94fSEric Auger report(expect_interrupts(0), "no overflow interrupt after counting"); 8954f5ef94fSEric Auger 896*7d1f853aSRicardo Koller /* enable interrupts (PMINTENSET_EL1 <= ALL_SET_32) */ 8974f5ef94fSEric Auger 8984f5ef94fSEric Auger pmu_reset_stats(); 8994f5ef94fSEric Auger 900*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW_32); 901*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 1, PRE_OVERFLOW_32); 902*7d1f853aSRicardo Koller write_sysreg(ALL_SET_32, pmintenset_el1); 9034f5ef94fSEric Auger isb(); 9044f5ef94fSEric Auger 9054f5ef94fSEric Auger mem_access_loop(addr, 200, pmu.pmcr_ro | PMU_PMCR_E); 9064f5ef94fSEric Auger for (i = 0; i < 100; i++) 9074f5ef94fSEric Auger write_sysreg(0x3, pmswinc_el0); 9084f5ef94fSEric Auger 9094f5ef94fSEric Auger mem_access_loop(addr, 200, pmu.pmcr_ro); 9104f5ef94fSEric Auger report_info("overflow=0x%lx", read_sysreg(pmovsclr_el0)); 9114f5ef94fSEric Auger report(expect_interrupts(0x3), 9124f5ef94fSEric Auger "overflow interrupts expected on #0 and #1"); 9134f5ef94fSEric Auger 9144f5ef94fSEric Auger /* promote to 64-b */ 9154f5ef94fSEric Auger 9164f5ef94fSEric Auger pmu_reset_stats(); 9174f5ef94fSEric Auger 9184f5ef94fSEric Auger write_regn_el0(pmevtyper, 1, CHAIN | PMEVTYPER_EXCLUDE_EL0); 919*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW_32); 9204f5ef94fSEric Auger isb(); 9214f5ef94fSEric Auger mem_access_loop(addr, 200, pmu.pmcr_ro | PMU_PMCR_E); 922b5489580SRicardo Koller report(expect_interrupts(0x1), 923b5489580SRicardo Koller "expect overflow interrupt on 32b boundary"); 9244f5ef94fSEric Auger 9254f5ef94fSEric Auger /* overflow on odd counter */ 9264f5ef94fSEric Auger pmu_reset_stats(); 927*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 0, PRE_OVERFLOW_32); 928*7d1f853aSRicardo Koller write_regn_el0(pmevcntr, 1, ALL_SET_32); 9294f5ef94fSEric Auger isb(); 9304f5ef94fSEric Auger mem_access_loop(addr, 400, pmu.pmcr_ro | PMU_PMCR_E); 931b5489580SRicardo Koller report(expect_interrupts(0x3), 932b5489580SRicardo Koller "expect overflow interrupt on even and odd counter"); 9334f5ef94fSEric Auger } 9344244065bSChristopher Covington #endif 9354244065bSChristopher Covington 9364244065bSChristopher Covington /* 937d81bb7a3SChristopher Covington * Ensure that the cycle counter progresses between back-to-back reads. 938d81bb7a3SChristopher Covington */ 939d81bb7a3SChristopher Covington static bool check_cycles_increase(void) 940d81bb7a3SChristopher Covington { 941d81bb7a3SChristopher Covington bool success = true; 942d81bb7a3SChristopher Covington 943d81bb7a3SChristopher Covington /* init before event access, this test only cares about cycle count */ 9441a97dad8SRicardo Koller pmu_reset(); 945d81bb7a3SChristopher Covington set_pmcntenset(1 << PMU_CYCLE_IDX); 946d81bb7a3SChristopher Covington set_pmccfiltr(0); /* count cycles in EL0, EL1, but not EL2 */ 947d81bb7a3SChristopher Covington 948d81bb7a3SChristopher Covington set_pmcr(get_pmcr() | PMU_PMCR_LC | PMU_PMCR_C | PMU_PMCR_E); 949e0a6e56bSRicardo Koller isb(); 950d81bb7a3SChristopher Covington 951d81bb7a3SChristopher Covington for (int i = 0; i < NR_SAMPLES; i++) { 952d81bb7a3SChristopher Covington uint64_t a, b; 953d81bb7a3SChristopher Covington 954d81bb7a3SChristopher Covington a = get_pmccntr(); 955d81bb7a3SChristopher Covington b = get_pmccntr(); 956d81bb7a3SChristopher Covington 957d81bb7a3SChristopher Covington if (a >= b) { 958d81bb7a3SChristopher Covington printf("Read %"PRId64" then %"PRId64".\n", a, b); 959d81bb7a3SChristopher Covington success = false; 960d81bb7a3SChristopher Covington break; 961d81bb7a3SChristopher Covington } 962d81bb7a3SChristopher Covington } 963d81bb7a3SChristopher Covington 964d81bb7a3SChristopher Covington set_pmcr(get_pmcr() & ~PMU_PMCR_E); 965e0a6e56bSRicardo Koller isb(); 966d81bb7a3SChristopher Covington 967d81bb7a3SChristopher Covington return success; 968d81bb7a3SChristopher Covington } 969d81bb7a3SChristopher Covington 9708f76a347SChristopher Covington /* 9718f76a347SChristopher Covington * Execute a known number of guest instructions. Only even instruction counts 9728f76a347SChristopher Covington * greater than or equal to 4 are supported by the in-line assembly code. The 9738f76a347SChristopher Covington * control register (PMCR_EL0) is initialized with the provided value (allowing 9748f76a347SChristopher Covington * for example for the cycle counter or event counters to be reset). At the end 9758f76a347SChristopher Covington * of the exact instruction loop, zero is written to PMCR_EL0 to disable 9768f76a347SChristopher Covington * counting, allowing the cycle counter or event counters to be read at the 9778f76a347SChristopher Covington * leisure of the calling code. 9788f76a347SChristopher Covington */ 9798f76a347SChristopher Covington static void measure_instrs(int num, uint32_t pmcr) 9808f76a347SChristopher Covington { 9818f76a347SChristopher Covington int loop = (num - 2) / 2; 9828f76a347SChristopher Covington 9838f76a347SChristopher Covington assert(num >= 4 && ((num - 2) % 2 == 0)); 9848f76a347SChristopher Covington precise_instrs_loop(loop, pmcr); 9858f76a347SChristopher Covington } 9868f76a347SChristopher Covington 9878f76a347SChristopher Covington /* 9888f76a347SChristopher Covington * Measure cycle counts for various known instruction counts. Ensure that the 9898f76a347SChristopher Covington * cycle counter progresses (similar to check_cycles_increase() but with more 9908f76a347SChristopher Covington * instructions and using reset and stop controls). If supplied a positive, 9918f76a347SChristopher Covington * nonzero CPI parameter, it also strictly checks that every measurement matches 9928f76a347SChristopher Covington * it. Strict CPI checking is used to test -icount mode. 9938f76a347SChristopher Covington */ 9948f76a347SChristopher Covington static bool check_cpi(int cpi) 9958f76a347SChristopher Covington { 9968f76a347SChristopher Covington uint32_t pmcr = get_pmcr() | PMU_PMCR_LC | PMU_PMCR_C | PMU_PMCR_E; 9978f76a347SChristopher Covington 9988f76a347SChristopher Covington /* init before event access, this test only cares about cycle count */ 9991a97dad8SRicardo Koller pmu_reset(); 10008f76a347SChristopher Covington set_pmcntenset(1 << PMU_CYCLE_IDX); 10018f76a347SChristopher Covington set_pmccfiltr(0); /* count cycles in EL0, EL1, but not EL2 */ 10028f76a347SChristopher Covington 10038f76a347SChristopher Covington if (cpi > 0) 10048f76a347SChristopher Covington printf("Checking for CPI=%d.\n", cpi); 10058f76a347SChristopher Covington printf("instrs : cycles0 cycles1 ...\n"); 10068f76a347SChristopher Covington 10078f76a347SChristopher Covington for (unsigned int i = 4; i < 300; i += 32) { 10088f76a347SChristopher Covington uint64_t avg, sum = 0; 10098f76a347SChristopher Covington 10108f76a347SChristopher Covington printf("%4d:", i); 10118f76a347SChristopher Covington for (int j = 0; j < NR_SAMPLES; j++) { 10128f76a347SChristopher Covington uint64_t cycles; 10138f76a347SChristopher Covington 10148f76a347SChristopher Covington set_pmccntr(0); 10158f76a347SChristopher Covington measure_instrs(i, pmcr); 10168f76a347SChristopher Covington cycles = get_pmccntr(); 10178f76a347SChristopher Covington printf(" %4"PRId64"", cycles); 10188f76a347SChristopher Covington 10198f76a347SChristopher Covington if (!cycles) { 10208f76a347SChristopher Covington printf("\ncycles not incrementing!\n"); 10218f76a347SChristopher Covington return false; 10228f76a347SChristopher Covington } else if (cpi > 0 && cycles != i * cpi) { 10238f76a347SChristopher Covington printf("\nunexpected cycle count received!\n"); 10248f76a347SChristopher Covington return false; 10258f76a347SChristopher Covington } else if ((cycles >> 32) != 0) { 10268f76a347SChristopher Covington /* The cycles taken by the loop above should 10278f76a347SChristopher Covington * fit in 32 bits easily. We check the upper 10288f76a347SChristopher Covington * 32 bits of the cycle counter to make sure 10298f76a347SChristopher Covington * there is no supprise. */ 10308f76a347SChristopher Covington printf("\ncycle count bigger than 32bit!\n"); 10318f76a347SChristopher Covington return false; 10328f76a347SChristopher Covington } 10338f76a347SChristopher Covington 10348f76a347SChristopher Covington sum += cycles; 10358f76a347SChristopher Covington } 10368f76a347SChristopher Covington avg = sum / NR_SAMPLES; 10378f76a347SChristopher Covington printf(" avg=%-4"PRId64" %s=%-3"PRId64"\n", avg, 10388f76a347SChristopher Covington (avg >= i) ? "cpi" : "ipc", 10398f76a347SChristopher Covington (avg >= i) ? avg / i : i / avg); 10408f76a347SChristopher Covington } 10418f76a347SChristopher Covington 10428f76a347SChristopher Covington return true; 10438f76a347SChristopher Covington } 10448f76a347SChristopher Covington 10454c357610SAndrew Jones static void pmccntr64_test(void) 10464c357610SAndrew Jones { 10474c357610SAndrew Jones #ifdef __arm__ 1048784ee933SEric Auger if (pmu.version == ID_DFR0_PMU_V3) { 10494c357610SAndrew Jones if (ERRATA(9e3f7a296940)) { 10504c357610SAndrew Jones write_sysreg(0xdead, PMCCNTR64); 1051a299895bSThomas Huth report(read_sysreg(PMCCNTR64) == 0xdead, "pmccntr64"); 10524c357610SAndrew Jones } else 10534c357610SAndrew Jones report_skip("Skipping unsafe pmccntr64 test. Set ERRATA_9e3f7a296940=y to enable."); 10544c357610SAndrew Jones } 10554c357610SAndrew Jones #endif 10564c357610SAndrew Jones } 10574c357610SAndrew Jones 10584244065bSChristopher Covington /* Return FALSE if no PMU found, otherwise return TRUE */ 105923b8916bSThomas Huth static bool pmu_probe(void) 10604244065bSChristopher Covington { 10611e4f5392SAlexandru Elisei uint32_t pmcr; 106246ca10f4SAlexandru Elisei uint8_t implementer; 1063eff8f161SEric Auger 10648f747a85SEric Auger pmu.version = get_pmu_version(); 1065784ee933SEric Auger if (pmu.version == ID_DFR0_PMU_NOTIMPL || pmu.version == ID_DFR0_PMU_IMPDEF) 1066eff8f161SEric Auger return false; 1067eff8f161SEric Auger 1068784ee933SEric Auger report_info("PMU version: 0x%x", pmu.version); 1069eff8f161SEric Auger 10701e4f5392SAlexandru Elisei pmcr = get_pmcr(); 107146ca10f4SAlexandru Elisei implementer = (pmcr >> PMU_PMCR_IMP_SHIFT) & PMU_PMCR_IMP_MASK; 107246ca10f4SAlexandru Elisei report_info("PMU implementer/ID code: %#"PRIx32"(\"%c\")/%#"PRIx32, 1073eff8f161SEric Auger (pmcr >> PMU_PMCR_IMP_SHIFT) & PMU_PMCR_IMP_MASK, 107446ca10f4SAlexandru Elisei implementer ? implementer : ' ', 10758f747a85SEric Auger (pmcr >> PMU_PMCR_ID_SHIFT) & PMU_PMCR_ID_MASK); 10768f747a85SEric Auger 10778f747a85SEric Auger /* store read-only and RES0 fields of the PMCR bottom-half*/ 10788f747a85SEric Auger pmu.pmcr_ro = pmcr & 0xFFFFFF00; 10798f747a85SEric Auger pmu.nb_implemented_counters = 10808f747a85SEric Auger (pmcr >> PMU_PMCR_N_SHIFT) & PMU_PMCR_N_MASK; 10818f747a85SEric Auger report_info("Implements %d event counters", 10828f747a85SEric Auger pmu.nb_implemented_counters); 1083eff8f161SEric Auger 1084eff8f161SEric Auger return true; 10854244065bSChristopher Covington } 10864244065bSChristopher Covington 1087041df25bSRicardo Koller static void run_test(const char *name, const char *prefix, 1088041df25bSRicardo Koller void (*test)(bool), void *arg) 1089041df25bSRicardo Koller { 1090041df25bSRicardo Koller report_prefix_push(name); 1091041df25bSRicardo Koller report_prefix_push(prefix); 1092041df25bSRicardo Koller 1093041df25bSRicardo Koller test(arg); 1094041df25bSRicardo Koller 1095041df25bSRicardo Koller report_prefix_pop(); 1096041df25bSRicardo Koller report_prefix_pop(); 1097041df25bSRicardo Koller } 1098041df25bSRicardo Koller 1099041df25bSRicardo Koller static void run_event_test(char *name, void (*test)(bool), 1100041df25bSRicardo Koller bool overflow_at_64bits) 1101041df25bSRicardo Koller { 1102041df25bSRicardo Koller const char *prefix = overflow_at_64bits ? "64-bit overflows" 1103041df25bSRicardo Koller : "32-bit overflows"; 1104041df25bSRicardo Koller 1105041df25bSRicardo Koller run_test(name, prefix, test, (void *)overflow_at_64bits); 1106041df25bSRicardo Koller } 1107041df25bSRicardo Koller 11088f76a347SChristopher Covington int main(int argc, char *argv[]) 11094244065bSChristopher Covington { 11108f76a347SChristopher Covington int cpi = 0; 11118f76a347SChristopher Covington 11124244065bSChristopher Covington if (!pmu_probe()) { 11134244065bSChristopher Covington printf("No PMU found, test skipped...\n"); 11144244065bSChristopher Covington return report_summary(); 11154244065bSChristopher Covington } 11164244065bSChristopher Covington 111757ec1086SEric Auger if (argc < 2) 111857ec1086SEric Auger report_abort("no test specified"); 111957ec1086SEric Auger 11204244065bSChristopher Covington report_prefix_push("pmu"); 11214244065bSChristopher Covington 112257ec1086SEric Auger if (strcmp(argv[1], "cycle-counter") == 0) { 112357ec1086SEric Auger report_prefix_push(argv[1]); 112457ec1086SEric Auger if (argc > 2) 112557ec1086SEric Auger cpi = atol(argv[2]); 1126a299895bSThomas Huth report(check_cycles_increase(), 1127a299895bSThomas Huth "Monotonically increasing cycle count"); 1128a299895bSThomas Huth report(check_cpi(cpi), "Cycle/instruction ratio"); 11294c357610SAndrew Jones pmccntr64_test(); 113057ec1086SEric Auger report_prefix_pop(); 11314870738cSEric Auger } else if (strcmp(argv[1], "pmu-event-introspection") == 0) { 11324870738cSEric Auger report_prefix_push(argv[1]); 11334870738cSEric Auger test_event_introspection(); 11344870738cSEric Auger report_prefix_pop(); 11354ce2a804SEric Auger } else if (strcmp(argv[1], "pmu-event-counter-config") == 0) { 11364ce2a804SEric Auger report_prefix_push(argv[1]); 11374ce2a804SEric Auger test_event_counter_config(); 11384ce2a804SEric Auger report_prefix_pop(); 11394ce2a804SEric Auger } else if (strcmp(argv[1], "pmu-basic-event-count") == 0) { 1140041df25bSRicardo Koller run_event_test(argv[1], test_basic_event_count, false); 11414ce2a804SEric Auger } else if (strcmp(argv[1], "pmu-mem-access") == 0) { 1142041df25bSRicardo Koller run_event_test(argv[1], test_mem_access, false); 1143bb9a5adcSEric Auger } else if (strcmp(argv[1], "pmu-sw-incr") == 0) { 1144041df25bSRicardo Koller run_event_test(argv[1], test_sw_incr, false); 114566fee034SEric Auger } else if (strcmp(argv[1], "pmu-chained-counters") == 0) { 1146041df25bSRicardo Koller run_event_test(argv[1], test_chained_counters, false); 114766fee034SEric Auger } else if (strcmp(argv[1], "pmu-chained-sw-incr") == 0) { 1148041df25bSRicardo Koller run_event_test(argv[1], test_chained_sw_incr, false); 1149ca42f29aSEric Auger } else if (strcmp(argv[1], "pmu-chain-promotion") == 0) { 1150041df25bSRicardo Koller run_event_test(argv[1], test_chain_promotion, false); 11514f5ef94fSEric Auger } else if (strcmp(argv[1], "pmu-overflow-interrupt") == 0) { 1152041df25bSRicardo Koller run_event_test(argv[1], test_overflow_interrupt, false); 115357ec1086SEric Auger } else { 115457ec1086SEric Auger report_abort("Unknown sub-test '%s'", argv[1]); 115557ec1086SEric Auger } 11564c357610SAndrew Jones 11574244065bSChristopher Covington return report_summary(); 11584244065bSChristopher Covington } 1159