1 // SPDX-License-Identifier: GPL-2.0
3 * Performance event support for s390x - CPU-measurement Counter Sets
5 * Copyright IBM Corp. 2019
6 * Author(s): Hendrik Brueckner <brueckner@linux.ibm.com>
7 * Thomas Richer <tmricht@linux.ibm.com>
9 #define KMSG_COMPONENT "cpum_cf_diag"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/kernel.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/percpu.h>
15 #include <linux/notifier.h>
16 #include <linux/init.h>
17 #include <linux/export.h>
18 #include <linux/slab.h>
19 #include <linux/processor.h>
21 #include <asm/ctl_reg.h>
23 #include <asm/cpu_mcf.h>
24 #include <asm/timex.h>
25 #include <asm/debug.h>
27 #define CF_DIAG_CTRSET_DEF 0xfeef /* Counter set header mark */
29 static unsigned int cf_diag_cpu_speed;
30 static debug_info_t *cf_diag_dbg;
32 struct cf_diag_csd { /* Counter set data per CPU */
33 size_t used; /* Bytes used in data/start */
34 unsigned char start[PAGE_SIZE]; /* Counter set at event start */
35 unsigned char data[PAGE_SIZE]; /* Counter set at event delete */
37 DEFINE_PER_CPU(struct cf_diag_csd, cf_diag_csd);
39 /* Counter sets are stored as data stream in a page sized memory buffer and
40 * exported to user space via raw data attached to the event sample data.
41 * Each counter set starts with an eight byte header consisting of:
42 * - a two byte eye catcher (0xfeef)
43 * - a one byte counter set number
44 * - a two byte counter set size (indicates the number of counters in this set)
45 * - a three byte reserved value (must be zero) to make the header the same
46 * size as a counter value.
47 * All counter values are eight byte in size.
49 * All counter sets are followed by a 64 byte trailer.
50 * The trailer consists of a:
51 * - flag field indicating valid fields when corresponding bit set
52 * - the counter facility first and second version number
53 * - the CPU speed if nonzero
54 * - the time stamp the counter sets have been collected
55 * - the time of day (TOD) base value
58 * The counter sets are saved when the process is prepared to be executed on a
59 * CPU and saved again when the process is going to be removed from a CPU.
60 * The difference of both counter sets are calculated and stored in the event
64 struct cf_ctrset_entry { /* CPU-M CF counter set entry (8 byte) */
65 unsigned int def:16; /* 0-15 Data Entry Format */
66 unsigned int set:16; /* 16-31 Counter set identifier */
67 unsigned int ctr:16; /* 32-47 Number of stored counters */
68 unsigned int res1:16; /* 48-63 Reserved */
71 struct cf_trailer_entry { /* CPU-M CF_DIAG trailer (64 byte) */
75 unsigned int clock_base:1; /* TOD clock base set */
76 unsigned int speed:1; /* CPU speed set */
77 /* Measurement alerts */
78 unsigned int mtda:1; /* Loss of MT ctr. data alert */
79 unsigned int caca:1; /* Counter auth. change alert */
80 unsigned int lcda:1; /* Loss of counter data alert */
82 unsigned long flags; /* 0-63 All indicators */
85 unsigned int cfvn:16; /* 64-79 Ctr First Version */
86 unsigned int csvn:16; /* 80-95 Ctr Second Version */
87 unsigned int cpu_speed:32; /* 96-127 CPU speed */
89 unsigned long timestamp; /* 128-191 Timestamp (TOD) */
93 unsigned long progusage1;
94 unsigned long progusage2;
95 unsigned long progusage3;
96 unsigned long tod_base;
98 unsigned long progusage[4];
101 unsigned int mach_type:16; /* Machine type */
102 unsigned int res1:16; /* Reserved */
103 unsigned int res2:32; /* Reserved */
106 /* Create the trailer data at the end of a page. */
107 static void cf_diag_trailer(struct cf_trailer_entry *te)
109 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
112 te->cfvn = cpuhw->info.cfvn; /* Counter version numbers */
113 te->csvn = cpuhw->info.csvn;
115 get_cpu_id(&cpuid); /* Machine type */
116 te->mach_type = cpuid.machine;
117 te->cpu_speed = cf_diag_cpu_speed;
120 te->clock_base = 1; /* Save clock base */
121 memcpy(&te->tod_base, &tod_clock_base[1], 8);
122 store_tod_clock((__u64 *)&te->timestamp);
126 * Change the CPUMF state to active.
127 * Enable and activate the CPU-counter sets according
128 * to the per-cpu control state.
130 static void cf_diag_enable(struct pmu *pmu)
132 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
135 debug_sprintf_event(cf_diag_dbg, 5,
136 "%s pmu %p cpu %d flags %#x state %#llx\n",
137 __func__, pmu, smp_processor_id(), cpuhw->flags,
139 if (cpuhw->flags & PMU_F_ENABLED)
142 err = lcctl(cpuhw->state);
144 pr_err("Enabling the performance measuring unit "
145 "failed with rc=%x\n", err);
148 cpuhw->flags |= PMU_F_ENABLED;
152 * Change the CPUMF state to inactive.
153 * Disable and enable (inactive) the CPU-counter sets according
154 * to the per-cpu control state.
156 static void cf_diag_disable(struct pmu *pmu)
158 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
162 debug_sprintf_event(cf_diag_dbg, 5,
163 "%s pmu %p cpu %d flags %#x state %#llx\n",
164 __func__, pmu, smp_processor_id(), cpuhw->flags,
166 if (!(cpuhw->flags & PMU_F_ENABLED))
169 inactive = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1);
170 err = lcctl(inactive);
172 pr_err("Disabling the performance measuring unit "
173 "failed with rc=%x\n", err);
176 cpuhw->flags &= ~PMU_F_ENABLED;
179 /* Number of perf events counting hardware events */
180 static atomic_t cf_diag_events = ATOMIC_INIT(0);
182 /* Release the PMU if event is the last perf event */
183 static void cf_diag_perf_event_destroy(struct perf_event *event)
185 debug_sprintf_event(cf_diag_dbg, 5,
186 "%s event %p cpu %d cf_diag_events %d\n",
187 __func__, event, event->cpu,
188 atomic_read(&cf_diag_events));
189 if (atomic_dec_return(&cf_diag_events) == 0)
190 __kernel_cpumcf_end();
193 /* Setup the event. Test for authorized counter sets and only include counter
194 * sets which are authorized at the time of the setup. Including unauthorized
195 * counter sets result in specification exception (and panic).
197 static int __hw_perf_event_init(struct perf_event *event)
199 struct perf_event_attr *attr = &event->attr;
200 struct cpu_cf_events *cpuhw;
201 enum cpumf_ctr_set i;
204 debug_sprintf_event(cf_diag_dbg, 5, "%s event %p cpu %d\n", __func__,
207 event->hw.config = attr->config;
208 event->hw.config_base = 0;
210 /* Add all authorized counter sets to config_base. The
211 * the hardware init function is either called per-cpu or just once
212 * for all CPUS (event->cpu == -1). This depends on the whether
213 * counting is started for all CPUs or on a per workload base where
214 * the perf event moves from one CPU to another CPU.
215 * Checking the authorization on any CPU is fine as the hardware
216 * applies the same authorization settings to all CPUs.
218 cpuhw = &get_cpu_var(cpu_cf_events);
219 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i)
220 if (cpuhw->info.auth_ctl & cpumf_ctr_ctl[i])
221 event->hw.config_base |= cpumf_ctr_ctl[i];
222 put_cpu_var(cpu_cf_events);
224 /* No authorized counter sets, nothing to count/sample */
225 if (!event->hw.config_base) {
230 /* Set sample_period to indicate sampling */
231 event->hw.sample_period = attr->sample_period;
232 local64_set(&event->hw.period_left, event->hw.sample_period);
233 event->hw.last_period = event->hw.sample_period;
235 debug_sprintf_event(cf_diag_dbg, 5, "%s err %d config_base %#lx\n",
236 __func__, err, event->hw.config_base);
240 static int cf_diag_event_init(struct perf_event *event)
242 struct perf_event_attr *attr = &event->attr;
245 debug_sprintf_event(cf_diag_dbg, 5,
246 "%s event %p cpu %d config %#llx "
247 "sample_type %#llx cf_diag_events %d\n", __func__,
248 event, event->cpu, attr->config, attr->sample_type,
249 atomic_read(&cf_diag_events));
251 if (event->attr.config != PERF_EVENT_CPUM_CF_DIAG ||
252 event->attr.type != PERF_TYPE_RAW)
255 /* Raw events are used to access counters directly,
256 * hence do not permit excludes.
257 * This event is usesless without PERF_SAMPLE_RAW to return counter set
258 * values as raw data.
260 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv ||
261 !(attr->sample_type & (PERF_SAMPLE_CPU | PERF_SAMPLE_RAW))) {
266 /* Initialize for using the CPU-measurement counter facility */
267 if (atomic_inc_return(&cf_diag_events) == 1) {
268 if (__kernel_cpumcf_begin()) {
269 atomic_dec(&cf_diag_events);
274 event->destroy = cf_diag_perf_event_destroy;
276 err = __hw_perf_event_init(event);
278 event->destroy(event);
280 debug_sprintf_event(cf_diag_dbg, 5, "%s err %d\n", __func__, err);
284 static void cf_diag_read(struct perf_event *event)
286 debug_sprintf_event(cf_diag_dbg, 5, "%s event %p\n", __func__, event);
289 /* Return the maximum possible counter set size (in number of 8 byte counters)
290 * depending on type and model number.
292 static size_t cf_diag_ctrset_size(enum cpumf_ctr_set ctrset,
293 struct cpumf_ctr_info *info)
295 size_t ctrset_size = 0;
298 case CPUMF_CTR_SET_BASIC:
302 case CPUMF_CTR_SET_USER:
305 else if (info->cfvn >= 3)
308 case CPUMF_CTR_SET_CRYPTO:
311 case CPUMF_CTR_SET_EXT:
314 else if (info->csvn == 2)
316 else if (info->csvn >= 3)
319 case CPUMF_CTR_SET_MT_DIAG:
323 case CPUMF_CTR_SET_MAX:
330 /* Calculate memory needed to store all counter sets together with header and
331 * trailer data. This is independend of the counter set authorization which
332 * can vary depending on the configuration.
334 static size_t cf_diag_ctrset_maxsize(struct cpumf_ctr_info *info)
336 size_t max_size = sizeof(struct cf_trailer_entry);
337 enum cpumf_ctr_set i;
339 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
340 size_t size = cf_diag_ctrset_size(i, info);
343 max_size += size * sizeof(u64) +
344 sizeof(struct cf_ctrset_entry);
346 debug_sprintf_event(cf_diag_dbg, 5, "%s max_size %zu\n", __func__,
352 /* Read a counter set. The counter set number determines which counter set and
353 * the CPUM-CF first and second version number determine the number of
354 * available counters in this counter set.
355 * Each counter set starts with header containing the counter set number and
356 * the number of 8 byte counters.
358 * The functions returns the number of bytes occupied by this counter set
359 * including the header.
360 * If there is no counter in the counter set, this counter set is useless and
361 * zero is returned on this case.
363 static size_t cf_diag_getctrset(struct cf_ctrset_entry *ctrdata, int ctrset,
366 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
367 size_t ctrset_size, need = 0;
368 int rc = 3; /* Assume write failure */
370 ctrdata->def = CF_DIAG_CTRSET_DEF;
371 ctrdata->set = ctrset;
373 ctrset_size = cf_diag_ctrset_size(ctrset, &cpuhw->info);
375 if (ctrset_size) { /* Save data */
376 need = ctrset_size * sizeof(u64) + sizeof(*ctrdata);
378 rc = ctr_stcctm(ctrset, ctrset_size,
379 (u64 *)(ctrdata + 1));
381 ctrdata->ctr = ctrset_size;
386 debug_sprintf_event(cf_diag_dbg, 6,
387 "%s ctrset %d ctrset_size %zu cfvn %d csvn %d"
389 __func__, ctrset, ctrset_size, cpuhw->info.cfvn,
390 cpuhw->info.csvn, need, rc);
394 /* Read out all counter sets and save them in the provided data buffer.
395 * The last 64 byte host an artificial trailer entry.
397 static size_t cf_diag_getctr(void *data, size_t sz, unsigned long auth)
399 struct cf_trailer_entry *trailer;
400 size_t offset = 0, done;
404 sz -= sizeof(*trailer); /* Always room for trailer */
405 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
406 struct cf_ctrset_entry *ctrdata = data + offset;
408 if (!(auth & cpumf_ctr_ctl[i]))
409 continue; /* Counter set not authorized */
411 done = cf_diag_getctrset(ctrdata, i, sz - offset);
413 debug_sprintf_event(cf_diag_dbg, 6,
414 "%s ctrset %d offset %zu done %zu\n",
415 __func__, i, offset, done);
417 trailer = data + offset;
418 cf_diag_trailer(trailer);
419 return offset + sizeof(*trailer);
422 /* Calculate the difference for each counter in a counter set. */
423 static void cf_diag_diffctrset(u64 *pstart, u64 *pstop, int counters)
425 for (; --counters >= 0; ++pstart, ++pstop)
426 if (*pstop >= *pstart)
429 *pstop = *pstart - *pstop;
432 /* Scan the counter sets and calculate the difference of each counter
433 * in each set. The result is the increment of each counter during the
434 * period the counter set has been activated.
436 * Return true on success.
438 static int cf_diag_diffctr(struct cf_diag_csd *csd, unsigned long auth)
440 struct cf_trailer_entry *trailer_start, *trailer_stop;
441 struct cf_ctrset_entry *ctrstart, *ctrstop;
444 auth &= (1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1;
446 ctrstart = (struct cf_ctrset_entry *)(csd->start + offset);
447 ctrstop = (struct cf_ctrset_entry *)(csd->data + offset);
449 if (memcmp(ctrstop, ctrstart, sizeof(*ctrstop))) {
450 pr_err("cpum_cf_diag counter set compare error "
451 "in set %i\n", ctrstart->set);
454 auth &= ~cpumf_ctr_ctl[ctrstart->set];
455 if (ctrstart->def == CF_DIAG_CTRSET_DEF) {
456 cf_diag_diffctrset((u64 *)(ctrstart + 1),
457 (u64 *)(ctrstop + 1), ctrstart->ctr);
458 offset += ctrstart->ctr * sizeof(u64) +
461 debug_sprintf_event(cf_diag_dbg, 6,
462 "%s set %d ctr %d offset %zu auth %lx\n",
463 __func__, ctrstart->set, ctrstart->ctr,
465 } while (ctrstart->def && auth);
467 /* Save time_stamp from start of event in stop's trailer */
468 trailer_start = (struct cf_trailer_entry *)(csd->start + offset);
469 trailer_stop = (struct cf_trailer_entry *)(csd->data + offset);
470 trailer_stop->progusage[0] = trailer_start->timestamp;
475 /* Create perf event sample with the counter sets as raw data. The sample
476 * is then pushed to the event subsystem and the function checks for
477 * possible event overflows. If an event overflow occurs, the PMU is
480 * Return non-zero if an event overflow occurred.
482 static int cf_diag_push_sample(struct perf_event *event,
483 struct cf_diag_csd *csd)
485 struct perf_sample_data data;
486 struct perf_raw_record raw;
490 /* Setup perf sample */
491 perf_sample_data_init(&data, 0, event->hw.last_period);
492 memset(®s, 0, sizeof(regs));
493 memset(&raw, 0, sizeof(raw));
495 if (event->attr.sample_type & PERF_SAMPLE_CPU)
496 data.cpu_entry.cpu = event->cpu;
497 if (event->attr.sample_type & PERF_SAMPLE_RAW) {
498 raw.frag.size = csd->used;
499 raw.frag.data = csd->data;
500 raw.size = csd->used;
504 overflow = perf_event_overflow(event, &data, ®s);
505 debug_sprintf_event(cf_diag_dbg, 6,
506 "%s event %p cpu %d sample_type %#llx raw %d "
507 "ov %d\n", __func__, event, event->cpu,
508 event->attr.sample_type, raw.size, overflow);
510 event->pmu->stop(event, 0);
512 perf_event_update_userpage(event);
516 static void cf_diag_start(struct perf_event *event, int flags)
518 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
519 struct cf_diag_csd *csd = this_cpu_ptr(&cf_diag_csd);
520 struct hw_perf_event *hwc = &event->hw;
522 debug_sprintf_event(cf_diag_dbg, 5,
523 "%s event %p cpu %d flags %#x hwc-state %#x\n",
524 __func__, event, event->cpu, flags, hwc->state);
525 if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
528 /* (Re-)enable and activate all counter sets */
529 lcctl(0); /* Reset counter sets */
531 ctr_set_multiple_enable(&cpuhw->state, hwc->config_base);
532 lcctl(cpuhw->state); /* Enable counter sets */
533 csd->used = cf_diag_getctr(csd->start, sizeof(csd->start),
534 event->hw.config_base);
535 ctr_set_multiple_start(&cpuhw->state, hwc->config_base);
536 /* Function cf_diag_enable() starts the counter sets. */
539 static void cf_diag_stop(struct perf_event *event, int flags)
541 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
542 struct cf_diag_csd *csd = this_cpu_ptr(&cf_diag_csd);
543 struct hw_perf_event *hwc = &event->hw;
545 debug_sprintf_event(cf_diag_dbg, 5,
546 "%s event %p cpu %d flags %#x hwc-state %#x\n",
547 __func__, event, event->cpu, flags, hwc->state);
549 /* Deactivate all counter sets */
550 ctr_set_multiple_stop(&cpuhw->state, hwc->config_base);
551 local64_inc(&event->count);
552 csd->used = cf_diag_getctr(csd->data, sizeof(csd->data),
553 event->hw.config_base);
554 if (cf_diag_diffctr(csd, event->hw.config_base))
555 cf_diag_push_sample(event, csd);
556 hwc->state |= PERF_HES_STOPPED;
559 static int cf_diag_add(struct perf_event *event, int flags)
561 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
564 debug_sprintf_event(cf_diag_dbg, 5,
565 "%s event %p cpu %d flags %#x cpuhw:%p\n",
566 __func__, event, event->cpu, flags, cpuhw);
568 if (cpuhw->flags & PMU_F_IN_USE) {
573 event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
575 cpuhw->flags |= PMU_F_IN_USE;
576 if (flags & PERF_EF_START)
577 cf_diag_start(event, PERF_EF_RELOAD);
579 debug_sprintf_event(cf_diag_dbg, 5, "%s err %d\n", __func__, err);
583 static void cf_diag_del(struct perf_event *event, int flags)
585 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
587 debug_sprintf_event(cf_diag_dbg, 5,
588 "%s event %p cpu %d flags %#x\n",
589 __func__, event, event->cpu, flags);
591 cf_diag_stop(event, PERF_EF_UPDATE);
592 ctr_set_multiple_stop(&cpuhw->state, event->hw.config_base);
593 ctr_set_multiple_disable(&cpuhw->state, event->hw.config_base);
594 cpuhw->flags &= ~PMU_F_IN_USE;
597 CPUMF_EVENT_ATTR(CF_DIAG, CF_DIAG, PERF_EVENT_CPUM_CF_DIAG);
599 static struct attribute *cf_diag_events_attr[] = {
600 CPUMF_EVENT_PTR(CF_DIAG, CF_DIAG),
604 PMU_FORMAT_ATTR(event, "config:0-63");
606 static struct attribute *cf_diag_format_attr[] = {
607 &format_attr_event.attr,
611 static struct attribute_group cf_diag_events_group = {
613 .attrs = cf_diag_events_attr,
615 static struct attribute_group cf_diag_format_group = {
617 .attrs = cf_diag_format_attr,
619 static const struct attribute_group *cf_diag_attr_groups[] = {
620 &cf_diag_events_group,
621 &cf_diag_format_group,
625 /* Performance monitoring unit for s390x */
626 static struct pmu cf_diag = {
627 .task_ctx_nr = perf_sw_context,
628 .pmu_enable = cf_diag_enable,
629 .pmu_disable = cf_diag_disable,
630 .event_init = cf_diag_event_init,
633 .start = cf_diag_start,
634 .stop = cf_diag_stop,
635 .read = cf_diag_read,
637 .attr_groups = cf_diag_attr_groups
640 /* Get the CPU speed, try sampling facility first and CPU attributes second. */
641 static void cf_diag_get_cpu_speed(void)
643 if (cpum_sf_avail()) { /* Sampling facility first */
644 struct hws_qsi_info_block si;
646 memset(&si, 0, sizeof(si));
648 cf_diag_cpu_speed = si.cpu_speed;
653 if (test_facility(34)) { /* CPU speed extract static part */
654 unsigned long mhz = __ecag(ECAG_CPU_ATTRIBUTE, 0);
657 cf_diag_cpu_speed = mhz & 0xffffffff;
661 /* Initialize the counter set PMU to generate complete counter set data as
662 * event raw data. This relies on the CPU Measurement Counter Facility device
663 * already being loaded and initialized.
665 static int __init cf_diag_init(void)
667 struct cpumf_ctr_info info;
671 if (!kernel_cpumcf_avail() || !stccm_avail() || qctri(&info))
673 cf_diag_get_cpu_speed();
675 /* Make sure the counter set data fits into predefined buffer. */
676 need = cf_diag_ctrset_maxsize(&info);
677 if (need > sizeof(((struct cf_diag_csd *)0)->start)) {
678 pr_err("Insufficient memory for PMU(cpum_cf_diag) need=%zu\n",
683 /* Setup s390dbf facility */
684 cf_diag_dbg = debug_register(KMSG_COMPONENT, 2, 1, 128);
686 pr_err("Registration of s390dbf(cpum_cf_diag) failed\n");
689 debug_register_view(cf_diag_dbg, &debug_sprintf_view);
691 rc = perf_pmu_register(&cf_diag, "cpum_cf_diag", PERF_TYPE_RAW);
693 debug_unregister_view(cf_diag_dbg, &debug_sprintf_view);
694 debug_unregister(cf_diag_dbg);
695 pr_err("Registration of PMU(cpum_cf_diag) failed with rc=%i\n",
700 arch_initcall(cf_diag_init);