1 // SPDX-License-Identifier: GPL-2.0
3 * Performance event support for s390x - CPU-measurement Counter Sets
5 * Copyright IBM Corp. 2019, 2021
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>
20 #include <linux/miscdevice.h>
21 #include <linux/mutex.h>
23 #include <asm/ctl_reg.h>
25 #include <asm/cpu_mcf.h>
26 #include <asm/timex.h>
27 #include <asm/debug.h>
29 #include <asm/hwctrset.h>
31 #define CF_DIAG_CTRSET_DEF 0xfeef /* Counter set header mark */
32 /* interval in seconds */
33 static unsigned int cf_diag_cpu_speed;
34 static debug_info_t *cf_diag_dbg;
36 struct cf_diag_csd { /* Counter set data per CPU */
37 size_t used; /* Bytes used in data/start */
38 unsigned char start[PAGE_SIZE]; /* Counter set at event start */
39 unsigned char data[PAGE_SIZE]; /* Counter set at event delete */
40 unsigned int sets; /* # Counter set saved in data */
42 static DEFINE_PER_CPU(struct cf_diag_csd, cf_diag_csd);
44 /* Counter sets are stored as data stream in a page sized memory buffer and
45 * exported to user space via raw data attached to the event sample data.
46 * Each counter set starts with an eight byte header consisting of:
47 * - a two byte eye catcher (0xfeef)
48 * - a one byte counter set number
49 * - a two byte counter set size (indicates the number of counters in this set)
50 * - a three byte reserved value (must be zero) to make the header the same
51 * size as a counter value.
52 * All counter values are eight byte in size.
54 * All counter sets are followed by a 64 byte trailer.
55 * The trailer consists of a:
56 * - flag field indicating valid fields when corresponding bit set
57 * - the counter facility first and second version number
58 * - the CPU speed if nonzero
59 * - the time stamp the counter sets have been collected
60 * - the time of day (TOD) base value
63 * The counter sets are saved when the process is prepared to be executed on a
64 * CPU and saved again when the process is going to be removed from a CPU.
65 * The difference of both counter sets are calculated and stored in the event
69 struct cf_ctrset_entry { /* CPU-M CF counter set entry (8 byte) */
70 unsigned int def:16; /* 0-15 Data Entry Format */
71 unsigned int set:16; /* 16-31 Counter set identifier */
72 unsigned int ctr:16; /* 32-47 Number of stored counters */
73 unsigned int res1:16; /* 48-63 Reserved */
76 struct cf_trailer_entry { /* CPU-M CF_DIAG trailer (64 byte) */
80 unsigned int clock_base:1; /* TOD clock base set */
81 unsigned int speed:1; /* CPU speed set */
82 /* Measurement alerts */
83 unsigned int mtda:1; /* Loss of MT ctr. data alert */
84 unsigned int caca:1; /* Counter auth. change alert */
85 unsigned int lcda:1; /* Loss of counter data alert */
87 unsigned long flags; /* 0-63 All indicators */
90 unsigned int cfvn:16; /* 64-79 Ctr First Version */
91 unsigned int csvn:16; /* 80-95 Ctr Second Version */
92 unsigned int cpu_speed:32; /* 96-127 CPU speed */
94 unsigned long timestamp; /* 128-191 Timestamp (TOD) */
98 unsigned long progusage1;
99 unsigned long progusage2;
100 unsigned long progusage3;
101 unsigned long tod_base;
103 unsigned long progusage[4];
106 unsigned int mach_type:16; /* Machine type */
107 unsigned int res1:16; /* Reserved */
108 unsigned int res2:32; /* Reserved */
111 /* Create the trailer data at the end of a page. */
112 static void cf_diag_trailer(struct cf_trailer_entry *te)
114 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
117 te->cfvn = cpuhw->info.cfvn; /* Counter version numbers */
118 te->csvn = cpuhw->info.csvn;
120 get_cpu_id(&cpuid); /* Machine type */
121 te->mach_type = cpuid.machine;
122 te->cpu_speed = cf_diag_cpu_speed;
125 te->clock_base = 1; /* Save clock base */
126 te->tod_base = tod_clock_base.tod;
127 te->timestamp = get_tod_clock_fast();
131 * Change the CPUMF state to active.
132 * Enable and activate the CPU-counter sets according
133 * to the per-cpu control state.
135 static void cf_diag_enable(struct pmu *pmu)
137 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
140 debug_sprintf_event(cf_diag_dbg, 5,
141 "%s pmu %p cpu %d flags %#x state %#llx\n",
142 __func__, pmu, smp_processor_id(), cpuhw->flags,
144 if (cpuhw->flags & PMU_F_ENABLED)
147 err = lcctl(cpuhw->state);
149 pr_err("Enabling the performance measuring unit "
150 "failed with rc=%x\n", err);
153 cpuhw->flags |= PMU_F_ENABLED;
157 * Change the CPUMF state to inactive.
158 * Disable and enable (inactive) the CPU-counter sets according
159 * to the per-cpu control state.
161 static void cf_diag_disable(struct pmu *pmu)
163 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
167 debug_sprintf_event(cf_diag_dbg, 5,
168 "%s pmu %p cpu %d flags %#x state %#llx\n",
169 __func__, pmu, smp_processor_id(), cpuhw->flags,
171 if (!(cpuhw->flags & PMU_F_ENABLED))
174 inactive = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1);
175 err = lcctl(inactive);
177 pr_err("Disabling the performance measuring unit "
178 "failed with rc=%x\n", err);
181 cpuhw->flags &= ~PMU_F_ENABLED;
184 /* Number of perf events counting hardware events */
185 static atomic_t cf_diag_events = ATOMIC_INIT(0);
186 /* Used to avoid races in calling reserve/release_cpumf_hardware */
187 static DEFINE_MUTEX(cf_diag_reserve_mutex);
189 /* Release the PMU if event is the last perf event */
190 static void cf_diag_perf_event_destroy(struct perf_event *event)
192 debug_sprintf_event(cf_diag_dbg, 5,
193 "%s event %p cpu %d cf_diag_events %d\n",
194 __func__, event, smp_processor_id(),
195 atomic_read(&cf_diag_events));
196 if (atomic_dec_return(&cf_diag_events) == 0)
197 __kernel_cpumcf_end();
200 static int get_authctrsets(void)
202 struct cpu_cf_events *cpuhw;
203 unsigned long auth = 0;
204 enum cpumf_ctr_set i;
206 cpuhw = &get_cpu_var(cpu_cf_events);
207 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
208 if (cpuhw->info.auth_ctl & cpumf_ctr_ctl[i])
209 auth |= cpumf_ctr_ctl[i];
211 put_cpu_var(cpu_cf_events);
215 /* Setup the event. Test for authorized counter sets and only include counter
216 * sets which are authorized at the time of the setup. Including unauthorized
217 * counter sets result in specification exception (and panic).
219 static int __hw_perf_event_init(struct perf_event *event)
221 struct perf_event_attr *attr = &event->attr;
224 debug_sprintf_event(cf_diag_dbg, 5, "%s event %p cpu %d\n", __func__,
227 event->hw.config = attr->config;
229 /* Add all authorized counter sets to config_base. The
230 * the hardware init function is either called per-cpu or just once
231 * for all CPUS (event->cpu == -1). This depends on the whether
232 * counting is started for all CPUs or on a per workload base where
233 * the perf event moves from one CPU to another CPU.
234 * Checking the authorization on any CPU is fine as the hardware
235 * applies the same authorization settings to all CPUs.
237 event->hw.config_base = get_authctrsets();
239 /* No authorized counter sets, nothing to count/sample */
240 if (!event->hw.config_base) {
245 /* Set sample_period to indicate sampling */
246 event->hw.sample_period = attr->sample_period;
247 local64_set(&event->hw.period_left, event->hw.sample_period);
248 event->hw.last_period = event->hw.sample_period;
250 debug_sprintf_event(cf_diag_dbg, 5, "%s err %d config_base %#lx\n",
251 __func__, err, event->hw.config_base);
255 /* Return 0 if the CPU-measurement counter facility is currently free
256 * and an error otherwise.
258 static int cf_diag_perf_event_inuse(void)
262 if (!atomic_inc_not_zero(&cf_diag_events)) {
263 mutex_lock(&cf_diag_reserve_mutex);
264 if (atomic_read(&cf_diag_events) == 0 &&
265 __kernel_cpumcf_begin())
268 err = atomic_inc_return(&cf_diag_events);
269 mutex_unlock(&cf_diag_reserve_mutex);
274 static int cf_diag_event_init(struct perf_event *event)
276 struct perf_event_attr *attr = &event->attr;
279 debug_sprintf_event(cf_diag_dbg, 5,
280 "%s event %p cpu %d config %#llx type:%u "
281 "sample_type %#llx cf_diag_events %d\n", __func__,
282 event, event->cpu, attr->config, event->pmu->type,
283 attr->sample_type, atomic_read(&cf_diag_events));
285 if (event->attr.config != PERF_EVENT_CPUM_CF_DIAG ||
286 event->attr.type != event->pmu->type)
289 /* Raw events are used to access counters directly,
290 * hence do not permit excludes.
291 * This event is usesless without PERF_SAMPLE_RAW to return counter set
292 * values as raw data.
294 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv ||
295 !(attr->sample_type & (PERF_SAMPLE_CPU | PERF_SAMPLE_RAW))) {
300 /* Initialize for using the CPU-measurement counter facility */
301 err = cf_diag_perf_event_inuse();
304 event->destroy = cf_diag_perf_event_destroy;
306 err = __hw_perf_event_init(event);
308 event->destroy(event);
310 debug_sprintf_event(cf_diag_dbg, 5, "%s err %d\n", __func__, err);
314 static void cf_diag_read(struct perf_event *event)
316 debug_sprintf_event(cf_diag_dbg, 5, "%s event %p\n", __func__, event);
319 /* Calculate memory needed to store all counter sets together with header and
320 * trailer data. This is independend of the counter set authorization which
321 * can vary depending on the configuration.
323 static size_t cf_diag_ctrset_maxsize(struct cpumf_ctr_info *info)
325 size_t max_size = sizeof(struct cf_trailer_entry);
326 enum cpumf_ctr_set i;
328 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
329 size_t size = cpum_cf_ctrset_size(i, info);
332 max_size += size * sizeof(u64) +
333 sizeof(struct cf_ctrset_entry);
335 debug_sprintf_event(cf_diag_dbg, 5, "%s max_size %zu\n", __func__,
341 /* Read a counter set. The counter set number determines which counter set and
342 * the CPUM-CF first and second version number determine the number of
343 * available counters in this counter set.
344 * Each counter set starts with header containing the counter set number and
345 * the number of 8 byte counters.
347 * The functions returns the number of bytes occupied by this counter set
348 * including the header.
349 * If there is no counter in the counter set, this counter set is useless and
350 * zero is returned on this case.
352 static size_t cf_diag_getctrset(struct cf_ctrset_entry *ctrdata, int ctrset,
355 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
356 size_t ctrset_size, need = 0;
357 int rc = 3; /* Assume write failure */
359 ctrdata->def = CF_DIAG_CTRSET_DEF;
360 ctrdata->set = ctrset;
362 ctrset_size = cpum_cf_ctrset_size(ctrset, &cpuhw->info);
364 if (ctrset_size) { /* Save data */
365 need = ctrset_size * sizeof(u64) + sizeof(*ctrdata);
367 rc = ctr_stcctm(ctrset, ctrset_size,
368 (u64 *)(ctrdata + 1));
370 ctrdata->ctr = ctrset_size;
375 debug_sprintf_event(cf_diag_dbg, 6,
376 "%s ctrset %d ctrset_size %zu cfvn %d csvn %d"
378 __func__, ctrset, ctrset_size, cpuhw->info.cfvn,
379 cpuhw->info.csvn, need, rc);
383 /* Read out all counter sets and save them in the provided data buffer.
384 * The last 64 byte host an artificial trailer entry.
386 static size_t cf_diag_getctr(void *data, size_t sz, unsigned long auth)
388 struct cf_trailer_entry *trailer;
389 size_t offset = 0, done;
393 sz -= sizeof(*trailer); /* Always room for trailer */
394 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
395 struct cf_ctrset_entry *ctrdata = data + offset;
397 if (!(auth & cpumf_ctr_ctl[i]))
398 continue; /* Counter set not authorized */
400 done = cf_diag_getctrset(ctrdata, i, sz - offset);
402 debug_sprintf_event(cf_diag_dbg, 6,
403 "%s ctrset %d offset %zu done %zu\n",
404 __func__, i, offset, done);
406 trailer = data + offset;
407 cf_diag_trailer(trailer);
408 return offset + sizeof(*trailer);
411 /* Calculate the difference for each counter in a counter set. */
412 static void cf_diag_diffctrset(u64 *pstart, u64 *pstop, int counters)
414 for (; --counters >= 0; ++pstart, ++pstop)
415 if (*pstop >= *pstart)
418 *pstop = *pstart - *pstop;
421 /* Scan the counter sets and calculate the difference of each counter
422 * in each set. The result is the increment of each counter during the
423 * period the counter set has been activated.
425 * Return true on success.
427 static int cf_diag_diffctr(struct cf_diag_csd *csd, unsigned long auth)
429 struct cf_trailer_entry *trailer_start, *trailer_stop;
430 struct cf_ctrset_entry *ctrstart, *ctrstop;
433 auth &= (1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1;
435 ctrstart = (struct cf_ctrset_entry *)(csd->start + offset);
436 ctrstop = (struct cf_ctrset_entry *)(csd->data + offset);
438 if (memcmp(ctrstop, ctrstart, sizeof(*ctrstop))) {
439 pr_err("cpum_cf_diag counter set compare error "
440 "in set %i\n", ctrstart->set);
443 auth &= ~cpumf_ctr_ctl[ctrstart->set];
444 if (ctrstart->def == CF_DIAG_CTRSET_DEF) {
445 cf_diag_diffctrset((u64 *)(ctrstart + 1),
446 (u64 *)(ctrstop + 1), ctrstart->ctr);
447 offset += ctrstart->ctr * sizeof(u64) +
450 debug_sprintf_event(cf_diag_dbg, 6,
451 "%s set %d ctr %d offset %zu auth %lx\n",
452 __func__, ctrstart->set, ctrstart->ctr,
454 } while (ctrstart->def && auth);
456 /* Save time_stamp from start of event in stop's trailer */
457 trailer_start = (struct cf_trailer_entry *)(csd->start + offset);
458 trailer_stop = (struct cf_trailer_entry *)(csd->data + offset);
459 trailer_stop->progusage[0] = trailer_start->timestamp;
464 /* Create perf event sample with the counter sets as raw data. The sample
465 * is then pushed to the event subsystem and the function checks for
466 * possible event overflows. If an event overflow occurs, the PMU is
469 * Return non-zero if an event overflow occurred.
471 static int cf_diag_push_sample(struct perf_event *event,
472 struct cf_diag_csd *csd)
474 struct perf_sample_data data;
475 struct perf_raw_record raw;
479 /* Setup perf sample */
480 perf_sample_data_init(&data, 0, event->hw.last_period);
481 memset(®s, 0, sizeof(regs));
482 memset(&raw, 0, sizeof(raw));
484 if (event->attr.sample_type & PERF_SAMPLE_CPU)
485 data.cpu_entry.cpu = event->cpu;
486 if (event->attr.sample_type & PERF_SAMPLE_RAW) {
487 raw.frag.size = csd->used;
488 raw.frag.data = csd->data;
489 raw.size = csd->used;
493 overflow = perf_event_overflow(event, &data, ®s);
494 debug_sprintf_event(cf_diag_dbg, 6,
495 "%s event %p cpu %d sample_type %#llx raw %d "
496 "ov %d\n", __func__, event, event->cpu,
497 event->attr.sample_type, raw.size, overflow);
499 event->pmu->stop(event, 0);
501 perf_event_update_userpage(event);
505 static void cf_diag_start(struct perf_event *event, int flags)
507 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
508 struct cf_diag_csd *csd = this_cpu_ptr(&cf_diag_csd);
509 struct hw_perf_event *hwc = &event->hw;
511 debug_sprintf_event(cf_diag_dbg, 5,
512 "%s event %p cpu %d flags %#x hwc-state %#x\n",
513 __func__, event, event->cpu, flags, hwc->state);
514 if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
517 /* (Re-)enable and activate all counter sets */
518 lcctl(0); /* Reset counter sets */
520 ctr_set_multiple_enable(&cpuhw->state, hwc->config_base);
521 lcctl(cpuhw->state); /* Enable counter sets */
522 csd->used = cf_diag_getctr(csd->start, sizeof(csd->start),
523 event->hw.config_base);
524 ctr_set_multiple_start(&cpuhw->state, hwc->config_base);
525 /* Function cf_diag_enable() starts the counter sets. */
528 static void cf_diag_stop(struct perf_event *event, int flags)
530 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
531 struct cf_diag_csd *csd = this_cpu_ptr(&cf_diag_csd);
532 struct hw_perf_event *hwc = &event->hw;
534 debug_sprintf_event(cf_diag_dbg, 5,
535 "%s event %p cpu %d flags %#x hwc-state %#x\n",
536 __func__, event, event->cpu, flags, hwc->state);
538 /* Deactivate all counter sets */
539 ctr_set_multiple_stop(&cpuhw->state, hwc->config_base);
540 local64_inc(&event->count);
541 csd->used = cf_diag_getctr(csd->data, sizeof(csd->data),
542 event->hw.config_base);
543 if (cf_diag_diffctr(csd, event->hw.config_base))
544 cf_diag_push_sample(event, csd);
545 hwc->state |= PERF_HES_STOPPED;
548 static int cf_diag_add(struct perf_event *event, int flags)
550 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
553 debug_sprintf_event(cf_diag_dbg, 5,
554 "%s event %p cpu %d flags %#x cpuhw %p\n",
555 __func__, event, event->cpu, flags, cpuhw);
557 if (cpuhw->flags & PMU_F_IN_USE) {
562 event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
564 cpuhw->flags |= PMU_F_IN_USE;
565 if (flags & PERF_EF_START)
566 cf_diag_start(event, PERF_EF_RELOAD);
568 debug_sprintf_event(cf_diag_dbg, 5, "%s err %d\n", __func__, err);
572 static void cf_diag_del(struct perf_event *event, int flags)
574 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
576 debug_sprintf_event(cf_diag_dbg, 5,
577 "%s event %p cpu %d flags %#x\n",
578 __func__, event, event->cpu, flags);
580 cf_diag_stop(event, PERF_EF_UPDATE);
581 ctr_set_multiple_stop(&cpuhw->state, event->hw.config_base);
582 ctr_set_multiple_disable(&cpuhw->state, event->hw.config_base);
583 cpuhw->flags &= ~PMU_F_IN_USE;
586 /* Default counter set events and format attribute groups */
588 CPUMF_EVENT_ATTR(CF_DIAG, CF_DIAG, PERF_EVENT_CPUM_CF_DIAG);
590 static struct attribute *cf_diag_events_attr[] = {
591 CPUMF_EVENT_PTR(CF_DIAG, CF_DIAG),
595 PMU_FORMAT_ATTR(event, "config:0-63");
597 static struct attribute *cf_diag_format_attr[] = {
598 &format_attr_event.attr,
602 static struct attribute_group cf_diag_events_group = {
604 .attrs = cf_diag_events_attr,
606 static struct attribute_group cf_diag_format_group = {
608 .attrs = cf_diag_format_attr,
610 static const struct attribute_group *cf_diag_attr_groups[] = {
611 &cf_diag_events_group,
612 &cf_diag_format_group,
616 /* Performance monitoring unit for s390x */
617 static struct pmu cf_diag = {
618 .task_ctx_nr = perf_sw_context,
619 .pmu_enable = cf_diag_enable,
620 .pmu_disable = cf_diag_disable,
621 .event_init = cf_diag_event_init,
624 .start = cf_diag_start,
625 .stop = cf_diag_stop,
626 .read = cf_diag_read,
628 .attr_groups = cf_diag_attr_groups
631 /* Get the CPU speed, try sampling facility first and CPU attributes second. */
632 static void cf_diag_get_cpu_speed(void)
634 if (cpum_sf_avail()) { /* Sampling facility first */
635 struct hws_qsi_info_block si;
637 memset(&si, 0, sizeof(si));
639 cf_diag_cpu_speed = si.cpu_speed;
644 if (test_facility(34)) { /* CPU speed extract static part */
645 unsigned long mhz = __ecag(ECAG_CPU_ATTRIBUTE, 0);
648 cf_diag_cpu_speed = mhz & 0xffffffff;
652 /* Code to create device and file I/O operations */
653 static atomic_t ctrset_opencnt = ATOMIC_INIT(0); /* Excl. access */
655 static int cf_diag_open(struct inode *inode, struct file *file)
659 if (!capable(CAP_SYS_ADMIN))
661 if (atomic_xchg(&ctrset_opencnt, 1))
664 /* Avoid concurrent access with perf_event_open() system call */
665 mutex_lock(&cf_diag_reserve_mutex);
666 if (atomic_read(&cf_diag_events) || __kernel_cpumcf_begin())
668 mutex_unlock(&cf_diag_reserve_mutex);
670 atomic_set(&ctrset_opencnt, 0);
673 file->private_data = NULL;
674 debug_sprintf_event(cf_diag_dbg, 2, "%s\n", __func__);
675 /* nonseekable_open() never fails */
676 return nonseekable_open(inode, file);
679 /* Variables for ioctl() interface support */
680 static DEFINE_MUTEX(cf_diag_ctrset_mutex);
681 static struct cf_diag_ctrset {
682 unsigned long ctrset; /* Bit mask of counter set to read */
683 cpumask_t mask; /* CPU mask to read from */
686 static void cf_diag_ctrset_clear(void)
688 cpumask_clear(&cf_diag_ctrset.mask);
689 cf_diag_ctrset.ctrset = 0;
692 static void cf_diag_release_cpu(void *p)
694 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
696 debug_sprintf_event(cf_diag_dbg, 3, "%s cpu %d\n", __func__,
698 lcctl(0); /* Reset counter sets */
699 cpuhw->state = 0; /* Save state in CPU hardware state */
702 /* Release function is also called when application gets terminated without
703 * doing a proper ioctl(..., S390_HWCTR_STOP, ...) command.
704 * Since only one application is allowed to open the device, simple stop all
707 static int cf_diag_release(struct inode *inode, struct file *file)
709 on_each_cpu(cf_diag_release_cpu, NULL, 1);
710 cf_diag_ctrset_clear();
711 atomic_set(&ctrset_opencnt, 0);
712 __kernel_cpumcf_end();
713 debug_sprintf_event(cf_diag_dbg, 2, "%s\n", __func__);
717 struct cf_diag_call_on_cpu_parm { /* Parm struct for smp_call_on_cpu */
718 unsigned int sets; /* Counter set bit mask */
719 atomic_t cpus_ack; /* # CPUs successfully executed func */
722 static int cf_diag_all_copy(unsigned long arg, cpumask_t *mask)
724 struct s390_ctrset_read __user *ctrset_read;
725 unsigned int cpu, cpus, rc;
728 ctrset_read = (struct s390_ctrset_read __user *)arg;
729 uptr = ctrset_read->data;
730 for_each_cpu(cpu, mask) {
731 struct cf_diag_csd *csd = per_cpu_ptr(&cf_diag_csd, cpu);
732 struct s390_ctrset_cpudata __user *ctrset_cpudata;
734 ctrset_cpudata = uptr;
735 debug_sprintf_event(cf_diag_dbg, 5, "%s cpu %d used %zd\n",
736 __func__, cpu, csd->used);
737 rc = put_user(cpu, &ctrset_cpudata->cpu_nr);
738 rc |= put_user(csd->sets, &ctrset_cpudata->no_sets);
739 rc |= copy_to_user(ctrset_cpudata->data, csd->data, csd->used);
742 uptr += sizeof(struct s390_ctrset_cpudata) + csd->used;
745 cpus = cpumask_weight(mask);
746 if (put_user(cpus, &ctrset_read->no_cpus))
748 debug_sprintf_event(cf_diag_dbg, 5, "%s copied %ld\n",
749 __func__, uptr - (void __user *)ctrset_read->data);
753 static size_t cf_diag_cpuset_read(struct s390_ctrset_setdata *p, int ctrset,
754 int ctrset_size, size_t room)
759 need = sizeof(*p) + sizeof(u64) * ctrset_size;
760 debug_sprintf_event(cf_diag_dbg, 5,
761 "%s room %zd need %zd set %#x set_size %d\n",
762 __func__, room, need, ctrset, ctrset_size);
764 p->set = cpumf_ctr_ctl[ctrset];
765 p->no_cnts = ctrset_size;
766 rc = ctr_stcctm(ctrset, ctrset_size, (u64 *)p->cv);
767 if (rc == 3) /* Nothing stored */
770 debug_sprintf_event(cf_diag_dbg, 5, "%s need %zd rc %d\n", __func__,
775 /* Read all counter sets. Since the perf_event_open() system call with
776 * event cpum_cf_diag/.../ is blocked when this interface is active, reuse
777 * the perf_event_open() data buffer to store the counter sets.
779 static void cf_diag_cpu_read(void *parm)
781 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
782 struct cf_diag_csd *csd = this_cpu_ptr(&cf_diag_csd);
783 struct cf_diag_call_on_cpu_parm *p = parm;
787 debug_sprintf_event(cf_diag_dbg, 5,
788 "%s new %#x flags %#x state %#llx\n",
789 __func__, p->sets, cpuhw->flags,
791 /* No data saved yet */
794 memset(csd->data, 0, sizeof(csd->data));
796 /* Scan the counter sets */
797 for (set = CPUMF_CTR_SET_BASIC; set < CPUMF_CTR_SET_MAX; ++set) {
798 struct s390_ctrset_setdata *sp = (void *)csd->data + csd->used;
800 if (!(p->sets & cpumf_ctr_ctl[set]))
801 continue; /* Counter set not in list */
802 set_size = cpum_cf_ctrset_size(set, &cpuhw->info);
803 space = sizeof(csd->data) - csd->used;
804 space = cf_diag_cpuset_read(sp, set, set_size, space);
809 debug_sprintf_event(cf_diag_dbg, 5, "%s sp %px space %zd\n",
810 __func__, sp, space);
812 debug_sprintf_event(cf_diag_dbg, 5, "%s sets %d used %zd\n", __func__,
813 csd->sets, csd->used);
816 static int cf_diag_all_read(unsigned long arg)
818 struct cf_diag_call_on_cpu_parm p;
822 debug_sprintf_event(cf_diag_dbg, 5, "%s\n", __func__);
823 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
826 p.sets = cf_diag_ctrset.ctrset;
827 cpumask_and(mask, &cf_diag_ctrset.mask, cpu_online_mask);
828 on_each_cpu_mask(mask, cf_diag_cpu_read, &p, 1);
829 rc = cf_diag_all_copy(arg, mask);
830 free_cpumask_var(mask);
831 debug_sprintf_event(cf_diag_dbg, 5, "%s rc %d\n", __func__, rc);
835 /* Stop all counter sets via ioctl interface */
836 static void cf_diag_ioctl_off(void *parm)
838 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
839 struct cf_diag_call_on_cpu_parm *p = parm;
842 debug_sprintf_event(cf_diag_dbg, 5,
843 "%s new %#x flags %#x state %#llx\n",
844 __func__, p->sets, cpuhw->flags,
847 ctr_set_multiple_disable(&cpuhw->state, p->sets);
848 ctr_set_multiple_stop(&cpuhw->state, p->sets);
849 rc = lcctl(cpuhw->state); /* Stop counter sets */
851 cpuhw->flags &= ~PMU_F_IN_USE;
852 debug_sprintf_event(cf_diag_dbg, 5,
853 "%s rc %d flags %#x state %#llx\n", __func__,
854 rc, cpuhw->flags, cpuhw->state);
857 /* Start counter sets on particular CPU */
858 static void cf_diag_ioctl_on(void *parm)
860 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
861 struct cf_diag_call_on_cpu_parm *p = parm;
864 debug_sprintf_event(cf_diag_dbg, 5,
865 "%s new %#x flags %#x state %#llx\n",
866 __func__, p->sets, cpuhw->flags,
869 if (!(cpuhw->flags & PMU_F_IN_USE))
871 cpuhw->flags |= PMU_F_IN_USE;
872 rc = lcctl(cpuhw->state); /* Reset unused counter sets */
873 ctr_set_multiple_enable(&cpuhw->state, p->sets);
874 ctr_set_multiple_start(&cpuhw->state, p->sets);
875 rc |= lcctl(cpuhw->state); /* Start counter sets */
877 atomic_inc(&p->cpus_ack);
878 debug_sprintf_event(cf_diag_dbg, 5, "%s rc %d state %#llx\n",
879 __func__, rc, cpuhw->state);
882 static int cf_diag_all_stop(void)
884 struct cf_diag_call_on_cpu_parm p = {
885 .sets = cf_diag_ctrset.ctrset,
889 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
891 cpumask_and(mask, &cf_diag_ctrset.mask, cpu_online_mask);
892 on_each_cpu_mask(mask, cf_diag_ioctl_off, &p, 1);
893 free_cpumask_var(mask);
897 static int cf_diag_all_start(void)
899 struct cf_diag_call_on_cpu_parm p = {
900 .sets = cf_diag_ctrset.ctrset,
901 .cpus_ack = ATOMIC_INIT(0),
906 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
908 cpumask_and(mask, &cf_diag_ctrset.mask, cpu_online_mask);
909 on_each_cpu_mask(mask, cf_diag_ioctl_on, &p, 1);
910 if (atomic_read(&p.cpus_ack) != cpumask_weight(mask)) {
911 on_each_cpu_mask(mask, cf_diag_ioctl_off, &p, 1);
914 free_cpumask_var(mask);
918 /* Return the maximum required space for all possible CPUs in case one
919 * CPU will be onlined during the START, READ, STOP cycles.
920 * To find out the size of the counter sets, any one CPU will do. They
921 * all have the same counter sets.
923 static size_t cf_diag_needspace(unsigned int sets)
925 struct cpu_cf_events *cpuhw = get_cpu_ptr(&cpu_cf_events);
929 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
930 if (!(sets & cpumf_ctr_ctl[i]))
932 bytes += cpum_cf_ctrset_size(i, &cpuhw->info) * sizeof(u64) +
933 sizeof(((struct s390_ctrset_setdata *)0)->set) +
934 sizeof(((struct s390_ctrset_setdata *)0)->no_cnts);
936 bytes = sizeof(((struct s390_ctrset_read *)0)->no_cpus) + nr_cpu_ids *
937 (bytes + sizeof(((struct s390_ctrset_cpudata *)0)->cpu_nr) +
938 sizeof(((struct s390_ctrset_cpudata *)0)->no_sets));
939 debug_sprintf_event(cf_diag_dbg, 5, "%s bytes %ld\n", __func__,
941 put_cpu_ptr(&cpu_cf_events);
945 static long cf_diag_ioctl_read(unsigned long arg)
947 struct s390_ctrset_read read;
950 debug_sprintf_event(cf_diag_dbg, 5, "%s\n", __func__);
951 if (copy_from_user(&read, (char __user *)arg, sizeof(read)))
953 ret = cf_diag_all_read(arg);
954 debug_sprintf_event(cf_diag_dbg, 5, "%s ret %d\n", __func__, ret);
958 static long cf_diag_ioctl_stop(void)
962 debug_sprintf_event(cf_diag_dbg, 5, "%s\n", __func__);
963 ret = cf_diag_all_stop();
964 cf_diag_ctrset_clear();
965 debug_sprintf_event(cf_diag_dbg, 5, "%s ret %d\n", __func__, ret);
969 static long cf_diag_ioctl_start(unsigned long arg)
971 struct s390_ctrset_start __user *ustart;
972 struct s390_ctrset_start start;
978 if (cf_diag_ctrset.ctrset)
980 ustart = (struct s390_ctrset_start __user *)arg;
981 if (copy_from_user(&start, ustart, sizeof(start)))
983 if (start.version != S390_HWCTR_START_VERSION)
985 if (start.counter_sets & ~(cpumf_ctr_ctl[CPUMF_CTR_SET_BASIC] |
986 cpumf_ctr_ctl[CPUMF_CTR_SET_USER] |
987 cpumf_ctr_ctl[CPUMF_CTR_SET_CRYPTO] |
988 cpumf_ctr_ctl[CPUMF_CTR_SET_EXT] |
989 cpumf_ctr_ctl[CPUMF_CTR_SET_MT_DIAG]))
990 return -EINVAL; /* Invalid counter set */
991 if (!start.counter_sets)
992 return -EINVAL; /* No counter set at all? */
993 cpumask_clear(&cf_diag_ctrset.mask);
994 len = min_t(u64, start.cpumask_len, cpumask_size());
995 umask = (void __user *)start.cpumask;
996 if (copy_from_user(&cf_diag_ctrset.mask, umask, len))
998 if (cpumask_empty(&cf_diag_ctrset.mask))
1000 need = cf_diag_needspace(start.counter_sets);
1001 if (put_user(need, &ustart->data_bytes))
1005 cf_diag_ctrset.ctrset = start.counter_sets;
1006 ret = cf_diag_all_start();
1009 cf_diag_ctrset_clear();
1010 debug_sprintf_event(cf_diag_dbg, 2, "%s sets %#lx need %ld ret %d\n",
1011 __func__, cf_diag_ctrset.ctrset, need, ret);
1015 static long cf_diag_ioctl(struct file *file, unsigned int cmd,
1020 debug_sprintf_event(cf_diag_dbg, 2, "%s cmd %#x arg %lx\n", __func__,
1023 mutex_lock(&cf_diag_ctrset_mutex);
1025 case S390_HWCTR_START:
1026 ret = cf_diag_ioctl_start(arg);
1028 case S390_HWCTR_STOP:
1029 ret = cf_diag_ioctl_stop();
1031 case S390_HWCTR_READ:
1032 ret = cf_diag_ioctl_read(arg);
1038 mutex_unlock(&cf_diag_ctrset_mutex);
1040 debug_sprintf_event(cf_diag_dbg, 2, "%s ret %d\n", __func__, ret);
1044 static const struct file_operations cf_diag_fops = {
1045 .owner = THIS_MODULE,
1046 .open = cf_diag_open,
1047 .release = cf_diag_release,
1048 .unlocked_ioctl = cf_diag_ioctl,
1049 .compat_ioctl = cf_diag_ioctl,
1053 static struct miscdevice cf_diag_dev = {
1054 .name = S390_HWCTR_DEVICE,
1055 .minor = MISC_DYNAMIC_MINOR,
1056 .fops = &cf_diag_fops,
1059 static int cf_diag_online_cpu(unsigned int cpu)
1061 struct cf_diag_call_on_cpu_parm p;
1063 mutex_lock(&cf_diag_ctrset_mutex);
1064 if (!cf_diag_ctrset.ctrset)
1066 p.sets = cf_diag_ctrset.ctrset;
1067 cf_diag_ioctl_on(&p);
1069 mutex_unlock(&cf_diag_ctrset_mutex);
1073 static int cf_diag_offline_cpu(unsigned int cpu)
1075 struct cf_diag_call_on_cpu_parm p;
1077 mutex_lock(&cf_diag_ctrset_mutex);
1078 if (!cf_diag_ctrset.ctrset)
1080 p.sets = cf_diag_ctrset.ctrset;
1081 cf_diag_ioctl_off(&p);
1083 mutex_unlock(&cf_diag_ctrset_mutex);
1087 /* Initialize the counter set PMU to generate complete counter set data as
1088 * event raw data. This relies on the CPU Measurement Counter Facility device
1089 * already being loaded and initialized.
1091 static int __init cf_diag_init(void)
1093 struct cpumf_ctr_info info;
1097 if (!kernel_cpumcf_avail() || !stccm_avail() || qctri(&info))
1099 cf_diag_get_cpu_speed();
1101 /* Make sure the counter set data fits into predefined buffer. */
1102 need = cf_diag_ctrset_maxsize(&info);
1103 if (need > sizeof(((struct cf_diag_csd *)0)->start)) {
1104 pr_err("Insufficient memory for PMU(cpum_cf_diag) need=%zu\n",
1109 rc = misc_register(&cf_diag_dev);
1111 pr_err("Registration of /dev/" S390_HWCTR_DEVICE
1112 "failed rc=%d\n", rc);
1116 /* Setup s390dbf facility */
1117 cf_diag_dbg = debug_register(KMSG_COMPONENT, 2, 1, 128);
1119 pr_err("Registration of s390dbf(cpum_cf_diag) failed\n");
1123 debug_register_view(cf_diag_dbg, &debug_sprintf_view);
1125 rc = perf_pmu_register(&cf_diag, "cpum_cf_diag", -1);
1127 pr_err("Registration of PMU(cpum_cf_diag) failed with rc=%i\n",
1131 rc = cpuhp_setup_state_nocalls(CPUHP_AP_PERF_S390_CFD_ONLINE,
1132 "perf/s390/cfd:online",
1133 cf_diag_online_cpu, cf_diag_offline_cpu);
1137 pr_err("Registration of CPUHP_AP_PERF_S390_CFD_ONLINE failed rc=%i\n",
1139 perf_pmu_unregister(&cf_diag);
1141 debug_unregister_view(cf_diag_dbg, &debug_sprintf_view);
1142 debug_unregister(cf_diag_dbg);
1144 misc_deregister(&cf_diag_dev);
1148 device_initcall(cf_diag_init);