2 * Performance event support for s390x - CPU-measurement Counter Facility
4 * Copyright IBM Corp. 2012
5 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License (version 2 only)
9 * as published by the Free Software Foundation.
11 #define KMSG_COMPONENT "cpum_cf"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/kernel.h>
15 #include <linux/kernel_stat.h>
16 #include <linux/perf_event.h>
17 #include <linux/percpu.h>
18 #include <linux/notifier.h>
19 #include <linux/init.h>
20 #include <linux/export.h>
21 #include <asm/ctl_reg.h>
23 #include <asm/cpu_mf.h>
25 /* CPU-measurement counter facility supports these CPU counter sets:
26 * For CPU counter sets:
27 * Basic counter set: 0-31
28 * Problem-state counter set: 32-63
29 * Crypto-activity counter set: 64-127
30 * Extented counter set: 128-159
33 /* CPU counter sets */
34 CPUMF_CTR_SET_BASIC
= 0,
35 CPUMF_CTR_SET_USER
= 1,
36 CPUMF_CTR_SET_CRYPTO
= 2,
37 CPUMF_CTR_SET_EXT
= 3,
39 /* Maximum number of counter sets */
43 #define CPUMF_LCCTL_ENABLE_SHIFT 16
44 #define CPUMF_LCCTL_ACTCTL_SHIFT 0
45 static const u64 cpumf_state_ctl
[CPUMF_CTR_SET_MAX
] = {
46 [CPUMF_CTR_SET_BASIC
] = 0x02,
47 [CPUMF_CTR_SET_USER
] = 0x04,
48 [CPUMF_CTR_SET_CRYPTO
] = 0x08,
49 [CPUMF_CTR_SET_EXT
] = 0x01,
52 static void ctr_set_enable(u64
*state
, int ctr_set
)
54 *state
|= cpumf_state_ctl
[ctr_set
] << CPUMF_LCCTL_ENABLE_SHIFT
;
56 static void ctr_set_disable(u64
*state
, int ctr_set
)
58 *state
&= ~(cpumf_state_ctl
[ctr_set
] << CPUMF_LCCTL_ENABLE_SHIFT
);
60 static void ctr_set_start(u64
*state
, int ctr_set
)
62 *state
|= cpumf_state_ctl
[ctr_set
] << CPUMF_LCCTL_ACTCTL_SHIFT
;
64 static void ctr_set_stop(u64
*state
, int ctr_set
)
66 *state
&= ~(cpumf_state_ctl
[ctr_set
] << CPUMF_LCCTL_ACTCTL_SHIFT
);
69 /* Local CPUMF event structure */
70 struct cpu_hw_events
{
71 struct cpumf_ctr_info info
;
72 atomic_t ctr_set
[CPUMF_CTR_SET_MAX
];
76 static DEFINE_PER_CPU(struct cpu_hw_events
, cpu_hw_events
) = {
78 [CPUMF_CTR_SET_BASIC
] = ATOMIC_INIT(0),
79 [CPUMF_CTR_SET_USER
] = ATOMIC_INIT(0),
80 [CPUMF_CTR_SET_CRYPTO
] = ATOMIC_INIT(0),
81 [CPUMF_CTR_SET_EXT
] = ATOMIC_INIT(0),
87 static int get_counter_set(u64 event
)
92 set
= CPUMF_CTR_SET_BASIC
;
94 set
= CPUMF_CTR_SET_USER
;
96 set
= CPUMF_CTR_SET_CRYPTO
;
98 set
= CPUMF_CTR_SET_EXT
;
103 static int validate_event(const struct hw_perf_event
*hwc
)
105 switch (hwc
->config_base
) {
106 case CPUMF_CTR_SET_BASIC
:
107 case CPUMF_CTR_SET_USER
:
108 case CPUMF_CTR_SET_CRYPTO
:
109 case CPUMF_CTR_SET_EXT
:
110 /* check for reserved counters */
111 if ((hwc
->config
>= 6 && hwc
->config
<= 31) ||
112 (hwc
->config
>= 38 && hwc
->config
<= 63) ||
113 (hwc
->config
>= 80 && hwc
->config
<= 127))
123 static int validate_ctr_version(const struct hw_perf_event
*hwc
)
125 struct cpu_hw_events
*cpuhw
;
128 cpuhw
= &get_cpu_var(cpu_hw_events
);
130 /* check required version for counter sets */
131 switch (hwc
->config_base
) {
132 case CPUMF_CTR_SET_BASIC
:
133 case CPUMF_CTR_SET_USER
:
134 if (cpuhw
->info
.cfvn
< 1)
137 case CPUMF_CTR_SET_CRYPTO
:
138 case CPUMF_CTR_SET_EXT
:
139 if (cpuhw
->info
.csvn
< 1)
141 if ((cpuhw
->info
.csvn
== 1 && hwc
->config
> 159) ||
142 (cpuhw
->info
.csvn
== 2 && hwc
->config
> 175) ||
143 (cpuhw
->info
.csvn
> 2 && hwc
->config
> 255))
148 put_cpu_var(cpu_hw_events
);
152 static int validate_ctr_auth(const struct hw_perf_event
*hwc
)
154 struct cpu_hw_events
*cpuhw
;
158 cpuhw
= &get_cpu_var(cpu_hw_events
);
160 /* check authorization for cpu counter sets */
161 ctrs_state
= cpumf_state_ctl
[hwc
->config_base
];
162 if (!(ctrs_state
& cpuhw
->info
.auth_ctl
))
165 put_cpu_var(cpu_hw_events
);
170 * Change the CPUMF state to active.
171 * Enable and activate the CPU-counter sets according
172 * to the per-cpu control state.
174 static void cpumf_pmu_enable(struct pmu
*pmu
)
176 struct cpu_hw_events
*cpuhw
= this_cpu_ptr(&cpu_hw_events
);
179 if (cpuhw
->flags
& PMU_F_ENABLED
)
182 err
= lcctl(cpuhw
->state
);
184 pr_err("Enabling the performance measuring unit "
185 "failed with rc=%x\n", err
);
189 cpuhw
->flags
|= PMU_F_ENABLED
;
193 * Change the CPUMF state to inactive.
194 * Disable and enable (inactive) the CPU-counter sets according
195 * to the per-cpu control state.
197 static void cpumf_pmu_disable(struct pmu
*pmu
)
199 struct cpu_hw_events
*cpuhw
= this_cpu_ptr(&cpu_hw_events
);
203 if (!(cpuhw
->flags
& PMU_F_ENABLED
))
206 inactive
= cpuhw
->state
& ~((1 << CPUMF_LCCTL_ENABLE_SHIFT
) - 1);
207 err
= lcctl(inactive
);
209 pr_err("Disabling the performance measuring unit "
210 "failed with rc=%x\n", err
);
214 cpuhw
->flags
&= ~PMU_F_ENABLED
;
218 /* Number of perf events counting hardware events */
219 static atomic_t num_events
= ATOMIC_INIT(0);
220 /* Used to avoid races in calling reserve/release_cpumf_hardware */
221 static DEFINE_MUTEX(pmc_reserve_mutex
);
223 /* CPU-measurement alerts for the counter facility */
224 static void cpumf_measurement_alert(struct ext_code ext_code
,
225 unsigned int alert
, unsigned long unused
)
227 struct cpu_hw_events
*cpuhw
;
229 if (!(alert
& CPU_MF_INT_CF_MASK
))
232 inc_irq_stat(IRQEXT_CMC
);
233 cpuhw
= this_cpu_ptr(&cpu_hw_events
);
235 /* Measurement alerts are shared and might happen when the PMU
236 * is not reserved. Ignore these alerts in this case. */
237 if (!(cpuhw
->flags
& PMU_F_RESERVED
))
240 /* counter authorization change alert */
241 if (alert
& CPU_MF_INT_CF_CACA
)
244 /* loss of counter data alert */
245 if (alert
& CPU_MF_INT_CF_LCDA
)
246 pr_err("CPU[%i] Counter data was lost\n", smp_processor_id());
250 #define PMC_RELEASE 1
251 static void setup_pmc_cpu(void *flags
)
253 struct cpu_hw_events
*cpuhw
= this_cpu_ptr(&cpu_hw_events
);
255 switch (*((int *) flags
)) {
257 memset(&cpuhw
->info
, 0, sizeof(cpuhw
->info
));
259 cpuhw
->flags
|= PMU_F_RESERVED
;
263 cpuhw
->flags
&= ~PMU_F_RESERVED
;
267 /* Disable CPU counter sets */
271 /* Initialize the CPU-measurement facility */
272 static int reserve_pmc_hardware(void)
274 int flags
= PMC_INIT
;
276 on_each_cpu(setup_pmc_cpu
, &flags
, 1);
277 irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT
);
282 /* Release the CPU-measurement facility */
283 static void release_pmc_hardware(void)
285 int flags
= PMC_RELEASE
;
287 on_each_cpu(setup_pmc_cpu
, &flags
, 1);
288 irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT
);
291 /* Release the PMU if event is the last perf event */
292 static void hw_perf_event_destroy(struct perf_event
*event
)
294 if (!atomic_add_unless(&num_events
, -1, 1)) {
295 mutex_lock(&pmc_reserve_mutex
);
296 if (atomic_dec_return(&num_events
) == 0)
297 release_pmc_hardware();
298 mutex_unlock(&pmc_reserve_mutex
);
302 /* CPUMF <-> perf event mappings for kernel+userspace (basic set) */
303 static const int cpumf_generic_events_basic
[] = {
304 [PERF_COUNT_HW_CPU_CYCLES
] = 0,
305 [PERF_COUNT_HW_INSTRUCTIONS
] = 1,
306 [PERF_COUNT_HW_CACHE_REFERENCES
] = -1,
307 [PERF_COUNT_HW_CACHE_MISSES
] = -1,
308 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = -1,
309 [PERF_COUNT_HW_BRANCH_MISSES
] = -1,
310 [PERF_COUNT_HW_BUS_CYCLES
] = -1,
312 /* CPUMF <-> perf event mappings for userspace (problem-state set) */
313 static const int cpumf_generic_events_user
[] = {
314 [PERF_COUNT_HW_CPU_CYCLES
] = 32,
315 [PERF_COUNT_HW_INSTRUCTIONS
] = 33,
316 [PERF_COUNT_HW_CACHE_REFERENCES
] = -1,
317 [PERF_COUNT_HW_CACHE_MISSES
] = -1,
318 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = -1,
319 [PERF_COUNT_HW_BRANCH_MISSES
] = -1,
320 [PERF_COUNT_HW_BUS_CYCLES
] = -1,
323 static int __hw_perf_event_init(struct perf_event
*event
)
325 struct perf_event_attr
*attr
= &event
->attr
;
326 struct hw_perf_event
*hwc
= &event
->hw
;
330 switch (attr
->type
) {
332 /* Raw events are used to access counters directly,
333 * hence do not permit excludes */
334 if (attr
->exclude_kernel
|| attr
->exclude_user
||
340 case PERF_TYPE_HARDWARE
:
342 /* Count user space (problem-state) only */
343 if (!attr
->exclude_user
&& attr
->exclude_kernel
) {
344 if (ev
>= ARRAY_SIZE(cpumf_generic_events_user
))
346 ev
= cpumf_generic_events_user
[ev
];
348 /* No support for kernel space counters only */
349 } else if (!attr
->exclude_kernel
&& attr
->exclude_user
) {
352 /* Count user and kernel space */
354 if (ev
>= ARRAY_SIZE(cpumf_generic_events_basic
))
356 ev
= cpumf_generic_events_basic
[ev
];
367 if (ev
>= PERF_CPUM_CF_MAX_CTR
)
370 /* Use the hardware perf event structure to store the counter number
371 * in 'config' member and the counter set to which the counter belongs
372 * in the 'config_base'. The counter set (config_base) is then used
373 * to enable/disable the counters.
376 hwc
->config_base
= get_counter_set(ev
);
378 /* Validate the counter that is assigned to this event.
379 * Because the counter facility can use numerous counters at the
380 * same time without constraints, it is not necessary to explicity
381 * validate event groups (event->group_leader != event).
383 err
= validate_event(hwc
);
387 /* Initialize for using the CPU-measurement counter facility */
388 if (!atomic_inc_not_zero(&num_events
)) {
389 mutex_lock(&pmc_reserve_mutex
);
390 if (atomic_read(&num_events
) == 0 && reserve_pmc_hardware())
393 atomic_inc(&num_events
);
394 mutex_unlock(&pmc_reserve_mutex
);
396 event
->destroy
= hw_perf_event_destroy
;
398 /* Finally, validate version and authorization of the counter set */
399 err
= validate_ctr_auth(hwc
);
401 err
= validate_ctr_version(hwc
);
406 static int cpumf_pmu_event_init(struct perf_event
*event
)
410 switch (event
->attr
.type
) {
411 case PERF_TYPE_HARDWARE
:
412 case PERF_TYPE_HW_CACHE
:
414 err
= __hw_perf_event_init(event
);
420 if (unlikely(err
) && event
->destroy
)
421 event
->destroy(event
);
426 static int hw_perf_event_reset(struct perf_event
*event
)
432 prev
= local64_read(&event
->hw
.prev_count
);
433 err
= ecctr(event
->hw
.config
, &new);
437 /* The counter is not (yet) available. This
438 * might happen if the counter set to which
439 * this counter belongs is in the disabled
444 } while (local64_cmpxchg(&event
->hw
.prev_count
, prev
, new) != prev
);
449 static int hw_perf_event_update(struct perf_event
*event
)
451 u64 prev
, new, delta
;
455 prev
= local64_read(&event
->hw
.prev_count
);
456 err
= ecctr(event
->hw
.config
, &new);
459 } while (local64_cmpxchg(&event
->hw
.prev_count
, prev
, new) != prev
);
461 delta
= (prev
<= new) ? new - prev
462 : (-1ULL - prev
) + new + 1; /* overflow */
463 local64_add(delta
, &event
->count
);
468 static void cpumf_pmu_read(struct perf_event
*event
)
470 if (event
->hw
.state
& PERF_HES_STOPPED
)
473 hw_perf_event_update(event
);
476 static void cpumf_pmu_start(struct perf_event
*event
, int flags
)
478 struct cpu_hw_events
*cpuhw
= this_cpu_ptr(&cpu_hw_events
);
479 struct hw_perf_event
*hwc
= &event
->hw
;
481 if (WARN_ON_ONCE(!(hwc
->state
& PERF_HES_STOPPED
)))
484 if (WARN_ON_ONCE(hwc
->config
== -1))
487 if (flags
& PERF_EF_RELOAD
)
488 WARN_ON_ONCE(!(hwc
->state
& PERF_HES_UPTODATE
));
492 /* (Re-)enable and activate the counter set */
493 ctr_set_enable(&cpuhw
->state
, hwc
->config_base
);
494 ctr_set_start(&cpuhw
->state
, hwc
->config_base
);
496 /* The counter set to which this counter belongs can be already active.
497 * Because all counters in a set are active, the event->hw.prev_count
498 * needs to be synchronized. At this point, the counter set can be in
499 * the inactive or disabled state.
501 hw_perf_event_reset(event
);
503 /* increment refcount for this counter set */
504 atomic_inc(&cpuhw
->ctr_set
[hwc
->config_base
]);
507 static void cpumf_pmu_stop(struct perf_event
*event
, int flags
)
509 struct cpu_hw_events
*cpuhw
= this_cpu_ptr(&cpu_hw_events
);
510 struct hw_perf_event
*hwc
= &event
->hw
;
512 if (!(hwc
->state
& PERF_HES_STOPPED
)) {
513 /* Decrement reference count for this counter set and if this
514 * is the last used counter in the set, clear activation
515 * control and set the counter set state to inactive.
517 if (!atomic_dec_return(&cpuhw
->ctr_set
[hwc
->config_base
]))
518 ctr_set_stop(&cpuhw
->state
, hwc
->config_base
);
519 event
->hw
.state
|= PERF_HES_STOPPED
;
522 if ((flags
& PERF_EF_UPDATE
) && !(hwc
->state
& PERF_HES_UPTODATE
)) {
523 hw_perf_event_update(event
);
524 event
->hw
.state
|= PERF_HES_UPTODATE
;
528 static int cpumf_pmu_add(struct perf_event
*event
, int flags
)
530 struct cpu_hw_events
*cpuhw
= this_cpu_ptr(&cpu_hw_events
);
532 /* Check authorization for the counter set to which this
534 * For group events transaction, the authorization check is
535 * done in cpumf_pmu_commit_txn().
537 if (!(cpuhw
->flags
& PERF_EVENT_TXN
))
538 if (validate_ctr_auth(&event
->hw
))
541 ctr_set_enable(&cpuhw
->state
, event
->hw
.config_base
);
542 event
->hw
.state
= PERF_HES_UPTODATE
| PERF_HES_STOPPED
;
544 if (flags
& PERF_EF_START
)
545 cpumf_pmu_start(event
, PERF_EF_RELOAD
);
547 perf_event_update_userpage(event
);
552 static void cpumf_pmu_del(struct perf_event
*event
, int flags
)
554 struct cpu_hw_events
*cpuhw
= this_cpu_ptr(&cpu_hw_events
);
556 cpumf_pmu_stop(event
, PERF_EF_UPDATE
);
558 /* Check if any counter in the counter set is still used. If not used,
559 * change the counter set to the disabled state. This also clears the
560 * content of all counters in the set.
562 * When a new perf event has been added but not yet started, this can
563 * clear enable control and resets all counters in a set. Therefore,
564 * cpumf_pmu_start() always has to reenable a counter set.
566 if (!atomic_read(&cpuhw
->ctr_set
[event
->hw
.config_base
]))
567 ctr_set_disable(&cpuhw
->state
, event
->hw
.config_base
);
569 perf_event_update_userpage(event
);
573 * Start group events scheduling transaction.
574 * Set flags to perform a single test at commit time.
576 static void cpumf_pmu_start_txn(struct pmu
*pmu
)
578 struct cpu_hw_events
*cpuhw
= this_cpu_ptr(&cpu_hw_events
);
580 perf_pmu_disable(pmu
);
581 cpuhw
->flags
|= PERF_EVENT_TXN
;
582 cpuhw
->tx_state
= cpuhw
->state
;
586 * Stop and cancel a group events scheduling tranctions.
587 * Assumes cpumf_pmu_del() is called for each successful added
588 * cpumf_pmu_add() during the transaction.
590 static void cpumf_pmu_cancel_txn(struct pmu
*pmu
)
592 struct cpu_hw_events
*cpuhw
= this_cpu_ptr(&cpu_hw_events
);
594 WARN_ON(cpuhw
->tx_state
!= cpuhw
->state
);
596 cpuhw
->flags
&= ~PERF_EVENT_TXN
;
597 perf_pmu_enable(pmu
);
601 * Commit the group events scheduling transaction. On success, the
602 * transaction is closed. On error, the transaction is kept open
603 * until cpumf_pmu_cancel_txn() is called.
605 static int cpumf_pmu_commit_txn(struct pmu
*pmu
)
607 struct cpu_hw_events
*cpuhw
= this_cpu_ptr(&cpu_hw_events
);
610 /* check if the updated state can be scheduled */
611 state
= cpuhw
->state
& ~((1 << CPUMF_LCCTL_ENABLE_SHIFT
) - 1);
612 state
>>= CPUMF_LCCTL_ENABLE_SHIFT
;
613 if ((state
& cpuhw
->info
.auth_ctl
) != state
)
616 cpuhw
->flags
&= ~PERF_EVENT_TXN
;
617 perf_pmu_enable(pmu
);
621 /* Performance monitoring unit for s390x */
622 static struct pmu cpumf_pmu
= {
623 .pmu_enable
= cpumf_pmu_enable
,
624 .pmu_disable
= cpumf_pmu_disable
,
625 .event_init
= cpumf_pmu_event_init
,
626 .add
= cpumf_pmu_add
,
627 .del
= cpumf_pmu_del
,
628 .start
= cpumf_pmu_start
,
629 .stop
= cpumf_pmu_stop
,
630 .read
= cpumf_pmu_read
,
631 .start_txn
= cpumf_pmu_start_txn
,
632 .commit_txn
= cpumf_pmu_commit_txn
,
633 .cancel_txn
= cpumf_pmu_cancel_txn
,
636 static int cpumf_pmu_notifier(struct notifier_block
*self
, unsigned long action
,
639 unsigned int cpu
= (long) hcpu
;
642 switch (action
& ~CPU_TASKS_FROZEN
) {
645 smp_call_function_single(cpu
, setup_pmc_cpu
, &flags
, 1);
647 case CPU_DOWN_PREPARE
:
649 smp_call_function_single(cpu
, setup_pmc_cpu
, &flags
, 1);
658 static int __init
cpumf_pmu_init(void)
662 if (!cpum_cf_avail())
665 /* clear bit 15 of cr0 to unauthorize problem-state to
666 * extract measurement counters */
667 ctl_clear_bit(0, 48);
669 /* register handler for measurement-alert interruptions */
670 rc
= register_external_irq(EXT_IRQ_MEASURE_ALERT
,
671 cpumf_measurement_alert
);
673 pr_err("Registering for CPU-measurement alerts "
674 "failed with rc=%i\n", rc
);
678 /* The CPU measurement counter facility does not have overflow
679 * interrupts to do sampling. Sampling must be provided by
680 * external means, for example, by timers.
682 cpumf_pmu
.capabilities
|= PERF_PMU_CAP_NO_INTERRUPT
;
684 cpumf_pmu
.attr_groups
= cpumf_cf_event_group();
685 rc
= perf_pmu_register(&cpumf_pmu
, "cpum_cf", PERF_TYPE_RAW
);
687 pr_err("Registering the cpum_cf PMU failed with rc=%i\n", rc
);
688 unregister_external_irq(EXT_IRQ_MEASURE_ALERT
,
689 cpumf_measurement_alert
);
692 perf_cpu_notifier(cpumf_pmu_notifier
);
696 early_initcall(cpumf_pmu_init
);