···11+/*22+ * Copyright 2014, Michael Ellerman, IBM Corp.33+ * Licensed under GPLv2.44+ */55+66+#include <stdio.h>77+#include <stdlib.h>88+#include <stdbool.h>99+1010+#include "ebb.h"1111+1212+1313+/*1414+ * Test of counting cycles while manipulating the user accessible bits in MMCR2.1515+ */1616+1717+/* We use two values because the first freezes PMC1 and so we would get no EBBs */1818+#define MMCR2_EXPECTED_1 0x4020100804020000UL /* (FC1P|FC2P|FC3P|FC4P|FC5P|FC6P) */1919+#define MMCR2_EXPECTED_2 0x0020100804020000UL /* ( FC2P|FC3P|FC4P|FC5P|FC6P) */2020+2121+2222+int cycles_with_mmcr2(void)2323+{2424+ struct event event;2525+ uint64_t val, expected[2], actual;2626+ int i;2727+ bool bad_mmcr2;2828+2929+ event_init_named(&event, 0x1001e, "cycles");3030+ event_leader_ebb_init(&event);3131+3232+ event.attr.exclude_kernel = 1;3333+ event.attr.exclude_hv = 1;3434+ event.attr.exclude_idle = 1;3535+3636+ FAIL_IF(event_open(&event));3737+3838+ ebb_enable_pmc_counting(1);3939+ setup_ebb_handler(standard_ebb_callee);4040+ ebb_global_enable();4141+4242+ FAIL_IF(ebb_event_enable(&event));4343+4444+ mtspr(SPRN_PMC1, pmc_sample_period(sample_period));4545+4646+ /* XXX Set of MMCR2 must be after enable */4747+ expected[0] = MMCR2_EXPECTED_1;4848+ expected[1] = MMCR2_EXPECTED_2;4949+ i = 0;5050+ bad_mmcr2 = false;5151+5252+ /* Make sure we loop until we take at least one EBB */5353+ while ((ebb_state.stats.ebb_count < 20 && !bad_mmcr2) ||5454+ ebb_state.stats.ebb_count < 1)5555+ {5656+ mtspr(SPRN_MMCR2, expected[i % 2]);5757+5858+ FAIL_IF(core_busy_loop());5959+6060+ val = mfspr(SPRN_MMCR2);6161+ if (val != expected[i % 2]) {6262+ bad_mmcr2 = true;6363+ actual = val;6464+ }6565+6666+ i++;6767+ }6868+6969+ ebb_global_disable();7070+ ebb_freeze_pmcs();7171+7272+ count_pmc(1, sample_period);7373+7474+ dump_ebb_state();7575+7676+ event_close(&event);7777+7878+ FAIL_IF(ebb_state.stats.ebb_count == 0);7979+8080+ if (bad_mmcr2)8181+ printf("Bad MMCR2 value seen is 0x%lx\n", actual);8282+8383+ FAIL_IF(bad_mmcr2);8484+8585+ return 0;8686+}8787+8888+int main(void)8989+{9090+ return test_harness(cycles_with_mmcr2, "cycles_with_mmcr2");9191+}