2 * Copyright 2014, Michael Ellerman, IBM Corp.
3 * Licensed under GPLv2.
14 * Test of counting cycles while using MMCR0_FC (freeze counters) to only count
15 * parts of the code. This is complicated by the fact that FC is set by the
16 * hardware when the event overflows. We may take the EBB after we have set FC,
17 * so we have to be careful about whether we clear FC at the end of the EBB
21 static bool counters_frozen = false;
22 static int ebbs_while_frozen = 0;
24 static void ebb_callee(void)
28 mask = MMCR0_PMAO | MMCR0_FC;
30 val = mfspr(SPRN_BESCR);
31 if (!(val & BESCR_PMEO)) {
32 ebb_state.stats.spurious++;
36 ebb_state.stats.ebb_count++;
37 trace_log_counter(ebb_state.trace, ebb_state.stats.ebb_count);
39 val = mfspr(SPRN_MMCR0);
40 trace_log_reg(ebb_state.trace, SPRN_MMCR0, val);
42 if (counters_frozen) {
43 trace_log_string(ebb_state.trace, "frozen");
48 count_pmc(1, sample_period);
50 reset_ebb_with_clear_mask(mask);
53 int cycles_with_freeze(void)
59 event_init_named(&event, 0x1001e, "cycles");
60 event_leader_ebb_init(&event);
62 event.attr.exclude_kernel = 1;
63 event.attr.exclude_hv = 1;
64 event.attr.exclude_idle = 1;
66 FAIL_IF(event_open(&event));
68 setup_ebb_handler(ebb_callee);
70 FAIL_IF(ebb_event_enable(&event));
72 mtspr(SPRN_PMC1, pmc_sample_period(sample_period));
76 /* Make sure we loop until we take at least one EBB */
77 while ((ebb_state.stats.ebb_count < 20 && !fc_cleared) ||
78 ebb_state.stats.ebb_count < 1)
80 counters_frozen = false;
82 mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) & ~MMCR0_FC);
84 FAIL_IF(core_busy_loop());
86 counters_frozen = true;
88 mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) | MMCR0_FC);
90 val = mfspr(SPRN_MMCR0);
91 if (! (val & MMCR0_FC)) {
92 printf("Outside of loop, FC NOT set MMCR0 0x%lx\n", val);
100 count_pmc(1, sample_period);
104 printf("EBBs while frozen %d\n", ebbs_while_frozen);
108 FAIL_IF(ebb_state.stats.ebb_count == 0);
116 return test_harness(cycles_with_freeze, "cycles_with_freeze");