1 // SPDX-License-Identifier: GPL-2.0
3 * Performance event support for s390x - CPU-measurement Counter Facility
5 * Copyright IBM Corp. 2012, 2022
6 * Author(s): Hendrik Brueckner <brueckner@linux.ibm.com>
7 * Thomas Richter <tmricht@linux.ibm.com>
9 #define KMSG_COMPONENT "cpum_cf"
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/miscdevice.h>
20 #include <asm/cpu_mcf.h>
21 #include <asm/hwctrset.h>
22 #include <asm/debug.h>
24 static unsigned int cfdiag_cpu_speed; /* CPU speed for CF_DIAG trailer */
25 static debug_info_t *cf_dbg;
27 #define CF_DIAG_CTRSET_DEF 0xfeef /* Counter set header mark */
28 /* interval in seconds */
30 /* Counter sets are stored as data stream in a page sized memory buffer and
31 * exported to user space via raw data attached to the event sample data.
32 * Each counter set starts with an eight byte header consisting of:
33 * - a two byte eye catcher (0xfeef)
34 * - a one byte counter set number
35 * - a two byte counter set size (indicates the number of counters in this set)
36 * - a three byte reserved value (must be zero) to make the header the same
37 * size as a counter value.
38 * All counter values are eight byte in size.
40 * All counter sets are followed by a 64 byte trailer.
41 * The trailer consists of a:
42 * - flag field indicating valid fields when corresponding bit set
43 * - the counter facility first and second version number
44 * - the CPU speed if nonzero
45 * - the time stamp the counter sets have been collected
46 * - the time of day (TOD) base value
49 * The counter sets are saved when the process is prepared to be executed on a
50 * CPU and saved again when the process is going to be removed from a CPU.
51 * The difference of both counter sets are calculated and stored in the event
54 struct cf_ctrset_entry { /* CPU-M CF counter set entry (8 byte) */
55 unsigned int def:16; /* 0-15 Data Entry Format */
56 unsigned int set:16; /* 16-31 Counter set identifier */
57 unsigned int ctr:16; /* 32-47 Number of stored counters */
58 unsigned int res1:16; /* 48-63 Reserved */
61 struct cf_trailer_entry { /* CPU-M CF_DIAG trailer (64 byte) */
65 unsigned int clock_base:1; /* TOD clock base set */
66 unsigned int speed:1; /* CPU speed set */
67 /* Measurement alerts */
68 unsigned int mtda:1; /* Loss of MT ctr. data alert */
69 unsigned int caca:1; /* Counter auth. change alert */
70 unsigned int lcda:1; /* Loss of counter data alert */
72 unsigned long flags; /* 0-63 All indicators */
75 unsigned int cfvn:16; /* 64-79 Ctr First Version */
76 unsigned int csvn:16; /* 80-95 Ctr Second Version */
77 unsigned int cpu_speed:32; /* 96-127 CPU speed */
79 unsigned long timestamp; /* 128-191 Timestamp (TOD) */
83 unsigned long progusage1;
84 unsigned long progusage2;
85 unsigned long progusage3;
86 unsigned long tod_base;
88 unsigned long progusage[4];
91 unsigned int mach_type:16; /* Machine type */
92 unsigned int res1:16; /* Reserved */
93 unsigned int res2:32; /* Reserved */
96 /* Create the trailer data at the end of a page. */
97 static void cfdiag_trailer(struct cf_trailer_entry *te)
99 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
102 te->cfvn = cpuhw->info.cfvn; /* Counter version numbers */
103 te->csvn = cpuhw->info.csvn;
105 get_cpu_id(&cpuid); /* Machine type */
106 te->mach_type = cpuid.machine;
107 te->cpu_speed = cfdiag_cpu_speed;
110 te->clock_base = 1; /* Save clock base */
111 te->tod_base = tod_clock_base.tod;
112 te->timestamp = get_tod_clock_fast();
115 /* Read a counter set. The counter set number determines the counter set and
116 * the CPUM-CF first and second version number determine the number of
117 * available counters in each counter set.
118 * Each counter set starts with header containing the counter set number and
119 * the number of eight byte counters.
121 * The functions returns the number of bytes occupied by this counter set
122 * including the header.
123 * If there is no counter in the counter set, this counter set is useless and
124 * zero is returned on this case.
126 * Note that the counter sets may not be enabled or active and the stcctm
127 * instruction might return error 3. Depending on error_ok value this is ok,
128 * for example when called from cpumf_pmu_start() call back function.
130 static size_t cfdiag_getctrset(struct cf_ctrset_entry *ctrdata, int ctrset,
131 size_t room, bool error_ok)
133 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
134 size_t ctrset_size, need = 0;
135 int rc = 3; /* Assume write failure */
137 ctrdata->def = CF_DIAG_CTRSET_DEF;
138 ctrdata->set = ctrset;
140 ctrset_size = cpum_cf_ctrset_size(ctrset, &cpuhw->info);
142 if (ctrset_size) { /* Save data */
143 need = ctrset_size * sizeof(u64) + sizeof(*ctrdata);
145 rc = ctr_stcctm(ctrset, ctrset_size,
146 (u64 *)(ctrdata + 1));
148 if (rc != 3 || error_ok)
149 ctrdata->ctr = ctrset_size;
154 debug_sprintf_event(cf_dbg, 3,
155 "%s ctrset %d ctrset_size %zu cfvn %d csvn %d"
156 " need %zd rc %d\n", __func__, ctrset, ctrset_size,
157 cpuhw->info.cfvn, cpuhw->info.csvn, need, rc);
161 static const u64 cpumf_ctr_ctl[CPUMF_CTR_SET_MAX] = {
162 [CPUMF_CTR_SET_BASIC] = 0x02,
163 [CPUMF_CTR_SET_USER] = 0x04,
164 [CPUMF_CTR_SET_CRYPTO] = 0x08,
165 [CPUMF_CTR_SET_EXT] = 0x01,
166 [CPUMF_CTR_SET_MT_DIAG] = 0x20,
169 /* Read out all counter sets and save them in the provided data buffer.
170 * The last 64 byte host an artificial trailer entry.
172 static size_t cfdiag_getctr(void *data, size_t sz, unsigned long auth,
175 struct cf_trailer_entry *trailer;
176 size_t offset = 0, done;
180 sz -= sizeof(*trailer); /* Always room for trailer */
181 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
182 struct cf_ctrset_entry *ctrdata = data + offset;
184 if (!(auth & cpumf_ctr_ctl[i]))
185 continue; /* Counter set not authorized */
187 done = cfdiag_getctrset(ctrdata, i, sz - offset, error_ok);
190 trailer = data + offset;
191 cfdiag_trailer(trailer);
192 return offset + sizeof(*trailer);
195 /* Calculate the difference for each counter in a counter set. */
196 static void cfdiag_diffctrset(u64 *pstart, u64 *pstop, int counters)
198 for (; --counters >= 0; ++pstart, ++pstop)
199 if (*pstop >= *pstart)
202 *pstop = *pstart - *pstop + 1;
205 /* Scan the counter sets and calculate the difference of each counter
206 * in each set. The result is the increment of each counter during the
207 * period the counter set has been activated.
209 * Return true on success.
211 static int cfdiag_diffctr(struct cpu_cf_events *cpuhw, unsigned long auth)
213 struct cf_trailer_entry *trailer_start, *trailer_stop;
214 struct cf_ctrset_entry *ctrstart, *ctrstop;
217 auth &= (1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1;
219 ctrstart = (struct cf_ctrset_entry *)(cpuhw->start + offset);
220 ctrstop = (struct cf_ctrset_entry *)(cpuhw->stop + offset);
222 if (memcmp(ctrstop, ctrstart, sizeof(*ctrstop))) {
223 pr_err_once("cpum_cf_diag counter set compare error "
224 "in set %i\n", ctrstart->set);
227 auth &= ~cpumf_ctr_ctl[ctrstart->set];
228 if (ctrstart->def == CF_DIAG_CTRSET_DEF) {
229 cfdiag_diffctrset((u64 *)(ctrstart + 1),
230 (u64 *)(ctrstop + 1), ctrstart->ctr);
231 offset += ctrstart->ctr * sizeof(u64) +
234 } while (ctrstart->def && auth);
236 /* Save time_stamp from start of event in stop's trailer */
237 trailer_start = (struct cf_trailer_entry *)(cpuhw->start + offset);
238 trailer_stop = (struct cf_trailer_entry *)(cpuhw->stop + offset);
239 trailer_stop->progusage[0] = trailer_start->timestamp;
244 static enum cpumf_ctr_set get_counter_set(u64 event)
246 int set = CPUMF_CTR_SET_MAX;
249 set = CPUMF_CTR_SET_BASIC;
251 set = CPUMF_CTR_SET_USER;
252 else if (event < 128)
253 set = CPUMF_CTR_SET_CRYPTO;
254 else if (event < 288)
255 set = CPUMF_CTR_SET_EXT;
256 else if (event >= 448 && event < 496)
257 set = CPUMF_CTR_SET_MT_DIAG;
262 static int validate_ctr_version(const struct hw_perf_event *hwc,
263 enum cpumf_ctr_set set)
265 struct cpu_cf_events *cpuhw;
269 cpuhw = &get_cpu_var(cpu_cf_events);
271 /* check required version for counter sets */
273 case CPUMF_CTR_SET_BASIC:
274 case CPUMF_CTR_SET_USER:
275 if (cpuhw->info.cfvn < 1)
278 case CPUMF_CTR_SET_CRYPTO:
279 if ((cpuhw->info.csvn >= 1 && cpuhw->info.csvn <= 5 &&
281 (cpuhw->info.csvn >= 6 && hwc->config > 83))
284 case CPUMF_CTR_SET_EXT:
285 if (cpuhw->info.csvn < 1)
287 if ((cpuhw->info.csvn == 1 && hwc->config > 159) ||
288 (cpuhw->info.csvn == 2 && hwc->config > 175) ||
289 (cpuhw->info.csvn >= 3 && cpuhw->info.csvn <= 5
290 && hwc->config > 255) ||
291 (cpuhw->info.csvn >= 6 && hwc->config > 287))
294 case CPUMF_CTR_SET_MT_DIAG:
295 if (cpuhw->info.csvn <= 3)
298 * MT-diagnostic counters are read-only. The counter set
299 * is automatically enabled and activated on all CPUs with
300 * multithreading (SMT). Deactivation of multithreading
301 * also disables the counter set. State changes are ignored
302 * by lcctl(). Because Linux controls SMT enablement through
303 * a kernel parameter only, the counter set is either disabled
304 * or enabled and active.
306 * Thus, the counters can only be used if SMT is on and the
307 * counter set is enabled and active.
309 mtdiag_ctl = cpumf_ctr_ctl[CPUMF_CTR_SET_MT_DIAG];
310 if (!((cpuhw->info.auth_ctl & mtdiag_ctl) &&
311 (cpuhw->info.enable_ctl & mtdiag_ctl) &&
312 (cpuhw->info.act_ctl & mtdiag_ctl)))
315 case CPUMF_CTR_SET_MAX:
319 put_cpu_var(cpu_cf_events);
323 static int validate_ctr_auth(const struct hw_perf_event *hwc)
325 struct cpu_cf_events *cpuhw;
328 cpuhw = &get_cpu_var(cpu_cf_events);
330 /* Check authorization for cpu counter sets.
331 * If the particular CPU counter set is not authorized,
332 * return with -ENOENT in order to fall back to other
333 * PMUs that might suffice the event request.
335 if (!(hwc->config_base & cpuhw->info.auth_ctl))
338 put_cpu_var(cpu_cf_events);
343 * Change the CPUMF state to active.
344 * Enable and activate the CPU-counter sets according
345 * to the per-cpu control state.
347 static void cpumf_pmu_enable(struct pmu *pmu)
349 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
352 if (cpuhw->flags & PMU_F_ENABLED)
355 err = lcctl(cpuhw->state | cpuhw->dev_state);
357 pr_err("Enabling the performance measuring unit "
358 "failed with rc=%x\n", err);
362 cpuhw->flags |= PMU_F_ENABLED;
366 * Change the CPUMF state to inactive.
367 * Disable and enable (inactive) the CPU-counter sets according
368 * to the per-cpu control state.
370 static void cpumf_pmu_disable(struct pmu *pmu)
372 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
376 if (!(cpuhw->flags & PMU_F_ENABLED))
379 inactive = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1);
380 inactive |= cpuhw->dev_state;
381 err = lcctl(inactive);
383 pr_err("Disabling the performance measuring unit "
384 "failed with rc=%x\n", err);
388 cpuhw->flags &= ~PMU_F_ENABLED;
392 /* Number of perf events counting hardware events */
393 static atomic_t num_events = ATOMIC_INIT(0);
394 /* Used to avoid races in calling reserve/release_cpumf_hardware */
395 static DEFINE_MUTEX(pmc_reserve_mutex);
397 /* Release the PMU if event is the last perf event */
398 static void hw_perf_event_destroy(struct perf_event *event)
400 if (!atomic_add_unless(&num_events, -1, 1)) {
401 mutex_lock(&pmc_reserve_mutex);
402 if (atomic_dec_return(&num_events) == 0)
403 __kernel_cpumcf_end();
404 mutex_unlock(&pmc_reserve_mutex);
408 /* CPUMF <-> perf event mappings for kernel+userspace (basic set) */
409 static const int cpumf_generic_events_basic[] = {
410 [PERF_COUNT_HW_CPU_CYCLES] = 0,
411 [PERF_COUNT_HW_INSTRUCTIONS] = 1,
412 [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
413 [PERF_COUNT_HW_CACHE_MISSES] = -1,
414 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
415 [PERF_COUNT_HW_BRANCH_MISSES] = -1,
416 [PERF_COUNT_HW_BUS_CYCLES] = -1,
418 /* CPUMF <-> perf event mappings for userspace (problem-state set) */
419 static const int cpumf_generic_events_user[] = {
420 [PERF_COUNT_HW_CPU_CYCLES] = 32,
421 [PERF_COUNT_HW_INSTRUCTIONS] = 33,
422 [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
423 [PERF_COUNT_HW_CACHE_MISSES] = -1,
424 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
425 [PERF_COUNT_HW_BRANCH_MISSES] = -1,
426 [PERF_COUNT_HW_BUS_CYCLES] = -1,
429 static void cpumf_hw_inuse(void)
431 mutex_lock(&pmc_reserve_mutex);
432 if (atomic_inc_return(&num_events) == 1)
433 __kernel_cpumcf_begin();
434 mutex_unlock(&pmc_reserve_mutex);
437 static int is_userspace_event(u64 ev)
439 return cpumf_generic_events_user[PERF_COUNT_HW_CPU_CYCLES] == ev ||
440 cpumf_generic_events_user[PERF_COUNT_HW_INSTRUCTIONS] == ev;
443 static int __hw_perf_event_init(struct perf_event *event, unsigned int type)
445 struct perf_event_attr *attr = &event->attr;
446 struct hw_perf_event *hwc = &event->hw;
447 enum cpumf_ctr_set set;
453 /* Raw events are used to access counters directly,
454 * hence do not permit excludes */
455 if (attr->exclude_kernel || attr->exclude_user ||
461 case PERF_TYPE_HARDWARE:
462 if (is_sampling_event(event)) /* No sampling support */
465 if (!attr->exclude_user && attr->exclude_kernel) {
467 * Count user space (problem-state) only
468 * Handle events 32 and 33 as 0:u and 1:u
470 if (!is_userspace_event(ev)) {
471 if (ev >= ARRAY_SIZE(cpumf_generic_events_user))
473 ev = cpumf_generic_events_user[ev];
475 } else if (!attr->exclude_kernel && attr->exclude_user) {
476 /* No support for kernel space counters only */
479 /* Count user and kernel space, incl. events 32 + 33 */
480 if (!is_userspace_event(ev)) {
481 if (ev >= ARRAY_SIZE(cpumf_generic_events_basic))
483 ev = cpumf_generic_events_basic[ev];
495 if (ev > PERF_CPUM_CF_MAX_CTR)
498 /* Obtain the counter set to which the specified counter belongs */
499 set = get_counter_set(ev);
501 case CPUMF_CTR_SET_BASIC:
502 case CPUMF_CTR_SET_USER:
503 case CPUMF_CTR_SET_CRYPTO:
504 case CPUMF_CTR_SET_EXT:
505 case CPUMF_CTR_SET_MT_DIAG:
507 * Use the hardware perf event structure to store the
508 * counter number in the 'config' member and the counter
509 * set number in the 'config_base' as bit mask.
510 * It is later used to enable/disable the counter(s).
513 hwc->config_base = cpumf_ctr_ctl[set];
515 case CPUMF_CTR_SET_MAX:
516 /* The counter could not be associated to a counter set */
520 /* Initialize for using the CPU-measurement counter facility */
522 event->destroy = hw_perf_event_destroy;
524 /* Finally, validate version and authorization of the counter set */
525 err = validate_ctr_auth(hwc);
527 err = validate_ctr_version(hwc, set);
532 /* Events CPU_CYLCES and INSTRUCTIONS can be submitted with two different
533 * attribute::type values:
534 * - PERF_TYPE_HARDWARE:
536 * Handle both type of invocations identical. They address the same hardware.
537 * The result is different when event modifiers exclude_kernel and/or
538 * exclude_user are also set.
540 static int cpumf_pmu_event_type(struct perf_event *event)
542 u64 ev = event->attr.config;
544 if (cpumf_generic_events_basic[PERF_COUNT_HW_CPU_CYCLES] == ev ||
545 cpumf_generic_events_basic[PERF_COUNT_HW_INSTRUCTIONS] == ev ||
546 cpumf_generic_events_user[PERF_COUNT_HW_CPU_CYCLES] == ev ||
547 cpumf_generic_events_user[PERF_COUNT_HW_INSTRUCTIONS] == ev)
548 return PERF_TYPE_HARDWARE;
549 return PERF_TYPE_RAW;
552 static int cpumf_pmu_event_init(struct perf_event *event)
554 unsigned int type = event->attr.type;
557 if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_RAW)
558 err = __hw_perf_event_init(event, type);
559 else if (event->pmu->type == type)
560 /* Registered as unknown PMU */
561 err = __hw_perf_event_init(event, cpumf_pmu_event_type(event));
565 if (unlikely(err) && event->destroy)
566 event->destroy(event);
571 static int hw_perf_event_reset(struct perf_event *event)
577 prev = local64_read(&event->hw.prev_count);
578 err = ecctr(event->hw.config, &new);
582 /* The counter is not (yet) available. This
583 * might happen if the counter set to which
584 * this counter belongs is in the disabled
589 } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
594 static void hw_perf_event_update(struct perf_event *event)
596 u64 prev, new, delta;
600 prev = local64_read(&event->hw.prev_count);
601 err = ecctr(event->hw.config, &new);
604 } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
606 delta = (prev <= new) ? new - prev
607 : (-1ULL - prev) + new + 1; /* overflow */
608 local64_add(delta, &event->count);
611 static void cpumf_pmu_read(struct perf_event *event)
613 if (event->hw.state & PERF_HES_STOPPED)
616 hw_perf_event_update(event);
619 static void cpumf_pmu_start(struct perf_event *event, int flags)
621 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
622 struct hw_perf_event *hwc = &event->hw;
625 if (!(hwc->state & PERF_HES_STOPPED))
630 /* (Re-)enable and activate the counter set */
631 ctr_set_enable(&cpuhw->state, hwc->config_base);
632 ctr_set_start(&cpuhw->state, hwc->config_base);
634 /* The counter set to which this counter belongs can be already active.
635 * Because all counters in a set are active, the event->hw.prev_count
636 * needs to be synchronized. At this point, the counter set can be in
637 * the inactive or disabled state.
639 if (hwc->config == PERF_EVENT_CPUM_CF_DIAG) {
640 cpuhw->usedss = cfdiag_getctr(cpuhw->start,
641 sizeof(cpuhw->start),
642 hwc->config_base, true);
644 hw_perf_event_reset(event);
647 /* Increment refcount for counter sets */
648 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i)
649 if ((hwc->config_base & cpumf_ctr_ctl[i]))
650 atomic_inc(&cpuhw->ctr_set[i]);
653 /* Create perf event sample with the counter sets as raw data. The sample
654 * is then pushed to the event subsystem and the function checks for
655 * possible event overflows. If an event overflow occurs, the PMU is
658 * Return non-zero if an event overflow occurred.
660 static int cfdiag_push_sample(struct perf_event *event,
661 struct cpu_cf_events *cpuhw)
663 struct perf_sample_data data;
664 struct perf_raw_record raw;
668 /* Setup perf sample */
669 perf_sample_data_init(&data, 0, event->hw.last_period);
670 memset(®s, 0, sizeof(regs));
671 memset(&raw, 0, sizeof(raw));
673 if (event->attr.sample_type & PERF_SAMPLE_CPU)
674 data.cpu_entry.cpu = event->cpu;
675 if (event->attr.sample_type & PERF_SAMPLE_RAW) {
676 raw.frag.size = cpuhw->usedss;
677 raw.frag.data = cpuhw->stop;
678 raw.size = raw.frag.size;
680 data.sample_flags |= PERF_SAMPLE_RAW;
683 overflow = perf_event_overflow(event, &data, ®s);
684 debug_sprintf_event(cf_dbg, 3,
685 "%s event %#llx sample_type %#llx raw %d ov %d\n",
686 __func__, event->hw.config,
687 event->attr.sample_type, raw.size, overflow);
689 event->pmu->stop(event, 0);
691 perf_event_update_userpage(event);
695 static void cpumf_pmu_stop(struct perf_event *event, int flags)
697 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
698 struct hw_perf_event *hwc = &event->hw;
701 if (!(hwc->state & PERF_HES_STOPPED)) {
702 /* Decrement reference count for this counter set and if this
703 * is the last used counter in the set, clear activation
704 * control and set the counter set state to inactive.
706 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
707 if (!(hwc->config_base & cpumf_ctr_ctl[i]))
709 if (!atomic_dec_return(&cpuhw->ctr_set[i]))
710 ctr_set_stop(&cpuhw->state, cpumf_ctr_ctl[i]);
712 hwc->state |= PERF_HES_STOPPED;
715 if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
716 if (hwc->config == PERF_EVENT_CPUM_CF_DIAG) {
717 local64_inc(&event->count);
718 cpuhw->usedss = cfdiag_getctr(cpuhw->stop,
720 event->hw.config_base,
722 if (cfdiag_diffctr(cpuhw, event->hw.config_base))
723 cfdiag_push_sample(event, cpuhw);
724 } else if (cpuhw->flags & PMU_F_RESERVED) {
725 /* Only update when PMU not hotplugged off */
726 hw_perf_event_update(event);
728 hwc->state |= PERF_HES_UPTODATE;
732 static int cpumf_pmu_add(struct perf_event *event, int flags)
734 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
736 ctr_set_enable(&cpuhw->state, event->hw.config_base);
737 event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
739 if (flags & PERF_EF_START)
740 cpumf_pmu_start(event, PERF_EF_RELOAD);
745 static void cpumf_pmu_del(struct perf_event *event, int flags)
747 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
750 cpumf_pmu_stop(event, PERF_EF_UPDATE);
752 /* Check if any counter in the counter set is still used. If not used,
753 * change the counter set to the disabled state. This also clears the
754 * content of all counters in the set.
756 * When a new perf event has been added but not yet started, this can
757 * clear enable control and resets all counters in a set. Therefore,
758 * cpumf_pmu_start() always has to reenable a counter set.
760 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i)
761 if (!atomic_read(&cpuhw->ctr_set[i]))
762 ctr_set_disable(&cpuhw->state, cpumf_ctr_ctl[i]);
765 /* Performance monitoring unit for s390x */
766 static struct pmu cpumf_pmu = {
767 .task_ctx_nr = perf_sw_context,
768 .capabilities = PERF_PMU_CAP_NO_INTERRUPT,
769 .pmu_enable = cpumf_pmu_enable,
770 .pmu_disable = cpumf_pmu_disable,
771 .event_init = cpumf_pmu_event_init,
772 .add = cpumf_pmu_add,
773 .del = cpumf_pmu_del,
774 .start = cpumf_pmu_start,
775 .stop = cpumf_pmu_stop,
776 .read = cpumf_pmu_read,
779 static int cfset_init(void);
780 static int __init cpumf_pmu_init(void)
784 if (!kernel_cpumcf_avail())
787 /* Setup s390dbf facility */
788 cf_dbg = debug_register(KMSG_COMPONENT, 2, 1, 128);
790 pr_err("Registration of s390dbf(cpum_cf) failed\n");
793 debug_register_view(cf_dbg, &debug_sprintf_view);
795 cpumf_pmu.attr_groups = cpumf_cf_event_group();
796 rc = perf_pmu_register(&cpumf_pmu, "cpum_cf", -1);
798 debug_unregister_view(cf_dbg, &debug_sprintf_view);
799 debug_unregister(cf_dbg);
800 pr_err("Registering the cpum_cf PMU failed with rc=%i\n", rc);
801 } else if (stccm_avail()) { /* Setup counter set device */
807 /* Support for the CPU Measurement Facility counter set extraction using
808 * device /dev/hwctr. This allows user space programs to extract complete
809 * counter set via normal file operations.
812 static atomic_t cfset_opencnt = ATOMIC_INIT(0); /* Access count */
813 static DEFINE_MUTEX(cfset_ctrset_mutex);/* Synchronize access to hardware */
814 struct cfset_call_on_cpu_parm { /* Parm struct for smp_call_on_cpu */
815 unsigned int sets; /* Counter set bit mask */
816 atomic_t cpus_ack; /* # CPUs successfully executed func */
819 static struct cfset_session { /* CPUs and counter set bit mask */
820 struct list_head head; /* Head of list of active processes */
822 .head = LIST_HEAD_INIT(cfset_session.head)
825 struct cfset_request { /* CPUs and counter set bit mask */
826 unsigned long ctrset; /* Bit mask of counter set to read */
827 cpumask_t mask; /* CPU mask to read from */
828 struct list_head node; /* Chain to cfset_session.head */
831 static void cfset_session_init(void)
833 INIT_LIST_HEAD(&cfset_session.head);
836 /* Remove current request from global bookkeeping. Maintain a counter set bit
837 * mask on a per CPU basis.
838 * Done in process context under mutex protection.
840 static void cfset_session_del(struct cfset_request *p)
845 /* Add current request to global bookkeeping. Maintain a counter set bit mask
846 * on a per CPU basis.
847 * Done in process context under mutex protection.
849 static void cfset_session_add(struct cfset_request *p)
851 list_add(&p->node, &cfset_session.head);
854 /* The /dev/hwctr device access uses PMU_F_IN_USE to mark the device access
855 * path is currently used.
856 * The cpu_cf_events::dev_state is used to denote counter sets in use by this
857 * interface. It is always or'ed in. If this interface is not active, its
858 * value is zero and no additional counter sets will be included.
860 * The cpu_cf_events::state is used by the perf_event_open SVC and remains
863 * perf_pmu_enable() and perf_pmu_enable() and its call backs
864 * cpumf_pmu_enable() and cpumf_pmu_disable() are called by the
865 * performance measurement subsystem to enable per process
866 * CPU Measurement counter facility.
867 * The XXX_enable() and XXX_disable functions are used to turn off
868 * x86 performance monitoring interrupt (PMI) during scheduling.
869 * s390 uses these calls to temporarily stop and resume the active CPU
870 * counters sets during scheduling.
872 * We do allow concurrent access of perf_event_open() SVC and /dev/hwctr
873 * device access. The perf_event_open() SVC interface makes a lot of effort
874 * to only run the counters while the calling process is actively scheduled
876 * When /dev/hwctr interface is also used at the same time, the counter sets
877 * will keep running, even when the process is scheduled off a CPU.
878 * However this is not a problem and does not lead to wrong counter values
879 * for the perf_event_open() SVC. The current counter value will be recorded
880 * during schedule-in. At schedule-out time the current counter value is
881 * extracted again and the delta is calculated and added to the event.
883 /* Stop all counter sets via ioctl interface */
884 static void cfset_ioctl_off(void *parm)
886 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
887 struct cfset_call_on_cpu_parm *p = parm;
890 /* Check if any counter set used by /dev/hwc */
891 for (rc = CPUMF_CTR_SET_BASIC; rc < CPUMF_CTR_SET_MAX; ++rc)
892 if ((p->sets & cpumf_ctr_ctl[rc])) {
893 if (!atomic_dec_return(&cpuhw->ctr_set[rc])) {
894 ctr_set_disable(&cpuhw->dev_state,
896 ctr_set_stop(&cpuhw->dev_state,
900 /* Keep perf_event_open counter sets */
901 rc = lcctl(cpuhw->dev_state | cpuhw->state);
903 pr_err("Counter set stop %#llx of /dev/%s failed rc=%i\n",
904 cpuhw->state, S390_HWCTR_DEVICE, rc);
905 if (!cpuhw->dev_state)
906 cpuhw->flags &= ~PMU_F_IN_USE;
907 debug_sprintf_event(cf_dbg, 4, "%s rc %d state %#llx dev_state %#llx\n",
908 __func__, rc, cpuhw->state, cpuhw->dev_state);
911 /* Start counter sets on particular CPU */
912 static void cfset_ioctl_on(void *parm)
914 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
915 struct cfset_call_on_cpu_parm *p = parm;
918 cpuhw->flags |= PMU_F_IN_USE;
919 ctr_set_enable(&cpuhw->dev_state, p->sets);
920 ctr_set_start(&cpuhw->dev_state, p->sets);
921 for (rc = CPUMF_CTR_SET_BASIC; rc < CPUMF_CTR_SET_MAX; ++rc)
922 if ((p->sets & cpumf_ctr_ctl[rc]))
923 atomic_inc(&cpuhw->ctr_set[rc]);
924 rc = lcctl(cpuhw->dev_state | cpuhw->state); /* Start counter sets */
926 atomic_inc(&p->cpus_ack);
928 pr_err("Counter set start %#llx of /dev/%s failed rc=%i\n",
929 cpuhw->dev_state | cpuhw->state, S390_HWCTR_DEVICE, rc);
930 debug_sprintf_event(cf_dbg, 4, "%s rc %d state %#llx dev_state %#llx\n",
931 __func__, rc, cpuhw->state, cpuhw->dev_state);
934 static void cfset_release_cpu(void *p)
936 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
939 debug_sprintf_event(cf_dbg, 4, "%s state %#llx dev_state %#llx\n",
940 __func__, cpuhw->state, cpuhw->dev_state);
941 cpuhw->dev_state = 0;
942 rc = lcctl(cpuhw->state); /* Keep perf_event_open counter sets */
944 pr_err("Counter set release %#llx of /dev/%s failed rc=%i\n",
945 cpuhw->state, S390_HWCTR_DEVICE, rc);
948 /* This modifies the process CPU mask to adopt it to the currently online
949 * CPUs. Offline CPUs can not be addresses. This call terminates the access
950 * and is usually followed by close() or a new iotcl(..., START, ...) which
951 * creates a new request structure.
953 static void cfset_all_stop(struct cfset_request *req)
955 struct cfset_call_on_cpu_parm p = {
959 cpumask_and(&req->mask, &req->mask, cpu_online_mask);
960 on_each_cpu_mask(&req->mask, cfset_ioctl_off, &p, 1);
963 /* Release function is also called when application gets terminated without
964 * doing a proper ioctl(..., S390_HWCTR_STOP, ...) command.
966 static int cfset_release(struct inode *inode, struct file *file)
968 mutex_lock(&cfset_ctrset_mutex);
969 /* Open followed by close/exit has no private_data */
970 if (file->private_data) {
971 cfset_all_stop(file->private_data);
972 cfset_session_del(file->private_data);
973 kfree(file->private_data);
974 file->private_data = NULL;
976 if (!atomic_dec_return(&cfset_opencnt))
977 on_each_cpu(cfset_release_cpu, NULL, 1);
978 mutex_unlock(&cfset_ctrset_mutex);
980 hw_perf_event_destroy(NULL);
984 static int cfset_open(struct inode *inode, struct file *file)
986 if (!capable(CAP_SYS_ADMIN))
988 mutex_lock(&cfset_ctrset_mutex);
989 if (atomic_inc_return(&cfset_opencnt) == 1)
990 cfset_session_init();
991 mutex_unlock(&cfset_ctrset_mutex);
994 file->private_data = NULL;
995 /* nonseekable_open() never fails */
996 return nonseekable_open(inode, file);
999 static int cfset_all_start(struct cfset_request *req)
1001 struct cfset_call_on_cpu_parm p = {
1002 .sets = req->ctrset,
1003 .cpus_ack = ATOMIC_INIT(0),
1008 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1010 cpumask_and(mask, &req->mask, cpu_online_mask);
1011 on_each_cpu_mask(mask, cfset_ioctl_on, &p, 1);
1012 if (atomic_read(&p.cpus_ack) != cpumask_weight(mask)) {
1013 on_each_cpu_mask(mask, cfset_ioctl_off, &p, 1);
1015 debug_sprintf_event(cf_dbg, 4, "%s CPUs missing", __func__);
1017 free_cpumask_var(mask);
1022 /* Return the maximum required space for all possible CPUs in case one
1023 * CPU will be onlined during the START, READ, STOP cycles.
1024 * To find out the size of the counter sets, any one CPU will do. They
1025 * all have the same counter sets.
1027 static size_t cfset_needspace(unsigned int sets)
1029 struct cpu_cf_events *cpuhw = get_cpu_ptr(&cpu_cf_events);
1033 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
1034 if (!(sets & cpumf_ctr_ctl[i]))
1036 bytes += cpum_cf_ctrset_size(i, &cpuhw->info) * sizeof(u64) +
1037 sizeof(((struct s390_ctrset_setdata *)0)->set) +
1038 sizeof(((struct s390_ctrset_setdata *)0)->no_cnts);
1040 bytes = sizeof(((struct s390_ctrset_read *)0)->no_cpus) + nr_cpu_ids *
1041 (bytes + sizeof(((struct s390_ctrset_cpudata *)0)->cpu_nr) +
1042 sizeof(((struct s390_ctrset_cpudata *)0)->no_sets));
1043 put_cpu_ptr(&cpu_cf_events);
1047 static int cfset_all_copy(unsigned long arg, cpumask_t *mask)
1049 struct s390_ctrset_read __user *ctrset_read;
1050 unsigned int cpu, cpus, rc;
1053 ctrset_read = (struct s390_ctrset_read __user *)arg;
1054 uptr = ctrset_read->data;
1055 for_each_cpu(cpu, mask) {
1056 struct cpu_cf_events *cpuhw = per_cpu_ptr(&cpu_cf_events, cpu);
1057 struct s390_ctrset_cpudata __user *ctrset_cpudata;
1059 ctrset_cpudata = uptr;
1060 rc = put_user(cpu, &ctrset_cpudata->cpu_nr);
1061 rc |= put_user(cpuhw->sets, &ctrset_cpudata->no_sets);
1062 rc |= copy_to_user(ctrset_cpudata->data, cpuhw->data,
1066 uptr += sizeof(struct s390_ctrset_cpudata) + cpuhw->used;
1069 cpus = cpumask_weight(mask);
1070 if (put_user(cpus, &ctrset_read->no_cpus))
1072 debug_sprintf_event(cf_dbg, 4, "%s copied %ld\n", __func__,
1073 uptr - (void __user *)ctrset_read->data);
1077 static size_t cfset_cpuset_read(struct s390_ctrset_setdata *p, int ctrset,
1078 int ctrset_size, size_t room)
1083 need = sizeof(*p) + sizeof(u64) * ctrset_size;
1085 p->set = cpumf_ctr_ctl[ctrset];
1086 p->no_cnts = ctrset_size;
1087 rc = ctr_stcctm(ctrset, ctrset_size, (u64 *)p->cv);
1088 if (rc == 3) /* Nothing stored */
1094 /* Read all counter sets. */
1095 static void cfset_cpu_read(void *parm)
1097 struct cpu_cf_events *cpuhw = this_cpu_ptr(&cpu_cf_events);
1098 struct cfset_call_on_cpu_parm *p = parm;
1102 /* No data saved yet */
1105 memset(cpuhw->data, 0, sizeof(cpuhw->data));
1107 /* Scan the counter sets */
1108 for (set = CPUMF_CTR_SET_BASIC; set < CPUMF_CTR_SET_MAX; ++set) {
1109 struct s390_ctrset_setdata *sp = (void *)cpuhw->data +
1112 if (!(p->sets & cpumf_ctr_ctl[set]))
1113 continue; /* Counter set not in list */
1114 set_size = cpum_cf_ctrset_size(set, &cpuhw->info);
1115 space = sizeof(cpuhw->data) - cpuhw->used;
1116 space = cfset_cpuset_read(sp, set, set_size, space);
1118 cpuhw->used += space;
1122 debug_sprintf_event(cf_dbg, 4, "%s sets %d used %zd\n", __func__,
1123 cpuhw->sets, cpuhw->used);
1126 static int cfset_all_read(unsigned long arg, struct cfset_request *req)
1128 struct cfset_call_on_cpu_parm p;
1132 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1135 p.sets = req->ctrset;
1136 cpumask_and(mask, &req->mask, cpu_online_mask);
1137 on_each_cpu_mask(mask, cfset_cpu_read, &p, 1);
1138 rc = cfset_all_copy(arg, mask);
1139 free_cpumask_var(mask);
1143 static long cfset_ioctl_read(unsigned long arg, struct cfset_request *req)
1145 struct s390_ctrset_read read;
1148 if (req && req->ctrset) {
1149 if (copy_from_user(&read, (char __user *)arg, sizeof(read)))
1151 ret = cfset_all_read(arg, req);
1156 static long cfset_ioctl_stop(struct file *file)
1158 struct cfset_request *req = file->private_data;
1162 cfset_all_stop(req);
1163 cfset_session_del(req);
1165 file->private_data = NULL;
1171 static long cfset_ioctl_start(unsigned long arg, struct file *file)
1173 struct s390_ctrset_start __user *ustart;
1174 struct s390_ctrset_start start;
1175 struct cfset_request *preq;
1181 if (file->private_data)
1183 ustart = (struct s390_ctrset_start __user *)arg;
1184 if (copy_from_user(&start, ustart, sizeof(start)))
1186 if (start.version != S390_HWCTR_START_VERSION)
1188 if (start.counter_sets & ~(cpumf_ctr_ctl[CPUMF_CTR_SET_BASIC] |
1189 cpumf_ctr_ctl[CPUMF_CTR_SET_USER] |
1190 cpumf_ctr_ctl[CPUMF_CTR_SET_CRYPTO] |
1191 cpumf_ctr_ctl[CPUMF_CTR_SET_EXT] |
1192 cpumf_ctr_ctl[CPUMF_CTR_SET_MT_DIAG]))
1193 return -EINVAL; /* Invalid counter set */
1194 if (!start.counter_sets)
1195 return -EINVAL; /* No counter set at all? */
1197 preq = kzalloc(sizeof(*preq), GFP_KERNEL);
1200 cpumask_clear(&preq->mask);
1201 len = min_t(u64, start.cpumask_len, cpumask_size());
1202 umask = (void __user *)start.cpumask;
1203 if (copy_from_user(&preq->mask, umask, len)) {
1207 if (cpumask_empty(&preq->mask)) {
1211 need = cfset_needspace(start.counter_sets);
1212 if (put_user(need, &ustart->data_bytes)) {
1216 preq->ctrset = start.counter_sets;
1217 ret = cfset_all_start(preq);
1219 cfset_session_add(preq);
1220 file->private_data = preq;
1221 debug_sprintf_event(cf_dbg, 4, "%s set %#lx need %ld ret %d\n",
1222 __func__, preq->ctrset, need, ret);
1229 /* Entry point to the /dev/hwctr device interface.
1230 * The ioctl system call supports three subcommands:
1231 * S390_HWCTR_START: Start the specified counter sets on a CPU list. The
1232 * counter set keeps running until explicitly stopped. Returns the number
1233 * of bytes needed to store the counter values. If another S390_HWCTR_START
1234 * ioctl subcommand is called without a previous S390_HWCTR_STOP stop
1235 * command on the same file descriptor, -EBUSY is returned.
1236 * S390_HWCTR_READ: Read the counter set values from specified CPU list given
1237 * with the S390_HWCTR_START command.
1238 * S390_HWCTR_STOP: Stops the counter sets on the CPU list given with the
1239 * previous S390_HWCTR_START subcommand.
1241 static long cfset_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1246 mutex_lock(&cfset_ctrset_mutex);
1248 case S390_HWCTR_START:
1249 ret = cfset_ioctl_start(arg, file);
1251 case S390_HWCTR_STOP:
1252 ret = cfset_ioctl_stop(file);
1254 case S390_HWCTR_READ:
1255 ret = cfset_ioctl_read(arg, file->private_data);
1261 mutex_unlock(&cfset_ctrset_mutex);
1266 static const struct file_operations cfset_fops = {
1267 .owner = THIS_MODULE,
1269 .release = cfset_release,
1270 .unlocked_ioctl = cfset_ioctl,
1271 .compat_ioctl = cfset_ioctl,
1275 static struct miscdevice cfset_dev = {
1276 .name = S390_HWCTR_DEVICE,
1277 .minor = MISC_DYNAMIC_MINOR,
1278 .fops = &cfset_fops,
1281 /* Hotplug add of a CPU. Scan through all active processes and add
1282 * that CPU to the list of CPUs supplied with ioctl(..., START, ...).
1284 int cfset_online_cpu(unsigned int cpu)
1286 struct cfset_call_on_cpu_parm p;
1287 struct cfset_request *rp;
1289 mutex_lock(&cfset_ctrset_mutex);
1290 if (!list_empty(&cfset_session.head)) {
1291 list_for_each_entry(rp, &cfset_session.head, node) {
1292 p.sets = rp->ctrset;
1294 cpumask_set_cpu(cpu, &rp->mask);
1297 mutex_unlock(&cfset_ctrset_mutex);
1301 /* Hotplug remove of a CPU. Scan through all active processes and clear
1302 * that CPU from the list of CPUs supplied with ioctl(..., START, ...).
1304 int cfset_offline_cpu(unsigned int cpu)
1306 struct cfset_call_on_cpu_parm p;
1307 struct cfset_request *rp;
1309 mutex_lock(&cfset_ctrset_mutex);
1310 if (!list_empty(&cfset_session.head)) {
1311 list_for_each_entry(rp, &cfset_session.head, node) {
1312 p.sets = rp->ctrset;
1313 cfset_ioctl_off(&p);
1314 cpumask_clear_cpu(cpu, &rp->mask);
1317 mutex_unlock(&cfset_ctrset_mutex);
1321 static void cfdiag_read(struct perf_event *event)
1323 debug_sprintf_event(cf_dbg, 3, "%s event %#llx count %ld\n", __func__,
1324 event->attr.config, local64_read(&event->count));
1327 static int get_authctrsets(void)
1329 struct cpu_cf_events *cpuhw;
1330 unsigned long auth = 0;
1331 enum cpumf_ctr_set i;
1333 cpuhw = &get_cpu_var(cpu_cf_events);
1334 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
1335 if (cpuhw->info.auth_ctl & cpumf_ctr_ctl[i])
1336 auth |= cpumf_ctr_ctl[i];
1338 put_cpu_var(cpu_cf_events);
1342 /* Setup the event. Test for authorized counter sets and only include counter
1343 * sets which are authorized at the time of the setup. Including unauthorized
1344 * counter sets result in specification exception (and panic).
1346 static int cfdiag_event_init2(struct perf_event *event)
1348 struct perf_event_attr *attr = &event->attr;
1351 /* Set sample_period to indicate sampling */
1352 event->hw.config = attr->config;
1353 event->hw.sample_period = attr->sample_period;
1354 local64_set(&event->hw.period_left, event->hw.sample_period);
1355 local64_set(&event->count, 0);
1356 event->hw.last_period = event->hw.sample_period;
1358 /* Add all authorized counter sets to config_base. The
1359 * the hardware init function is either called per-cpu or just once
1360 * for all CPUS (event->cpu == -1). This depends on the whether
1361 * counting is started for all CPUs or on a per workload base where
1362 * the perf event moves from one CPU to another CPU.
1363 * Checking the authorization on any CPU is fine as the hardware
1364 * applies the same authorization settings to all CPUs.
1366 event->hw.config_base = get_authctrsets();
1368 /* No authorized counter sets, nothing to count/sample */
1369 if (!event->hw.config_base)
1372 debug_sprintf_event(cf_dbg, 5, "%s err %d config_base %#lx\n",
1373 __func__, err, event->hw.config_base);
1377 static int cfdiag_event_init(struct perf_event *event)
1379 struct perf_event_attr *attr = &event->attr;
1382 if (event->attr.config != PERF_EVENT_CPUM_CF_DIAG ||
1383 event->attr.type != event->pmu->type)
1386 /* Raw events are used to access counters directly,
1387 * hence do not permit excludes.
1388 * This event is useless without PERF_SAMPLE_RAW to return counter set
1389 * values as raw data.
1391 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv ||
1392 !(attr->sample_type & (PERF_SAMPLE_CPU | PERF_SAMPLE_RAW))) {
1397 /* Initialize for using the CPU-measurement counter facility */
1399 event->destroy = hw_perf_event_destroy;
1401 err = cfdiag_event_init2(event);
1403 event->destroy(event);
1408 /* Create cf_diag/events/CF_DIAG event sysfs file. This counter is used
1409 * to collect the complete counter sets for a scheduled process. Target
1410 * are complete counter sets attached as raw data to the artificial event.
1411 * This results in complete counter sets available when a process is
1412 * scheduled. Contains the delta of every counter while the process was
1415 CPUMF_EVENT_ATTR(CF_DIAG, CF_DIAG, PERF_EVENT_CPUM_CF_DIAG);
1417 static struct attribute *cfdiag_events_attr[] = {
1418 CPUMF_EVENT_PTR(CF_DIAG, CF_DIAG),
1422 PMU_FORMAT_ATTR(event, "config:0-63");
1424 static struct attribute *cfdiag_format_attr[] = {
1425 &format_attr_event.attr,
1429 static struct attribute_group cfdiag_events_group = {
1431 .attrs = cfdiag_events_attr,
1433 static struct attribute_group cfdiag_format_group = {
1435 .attrs = cfdiag_format_attr,
1437 static const struct attribute_group *cfdiag_attr_groups[] = {
1438 &cfdiag_events_group,
1439 &cfdiag_format_group,
1443 /* Performance monitoring unit for event CF_DIAG. Since this event
1444 * is also started and stopped via the perf_event_open() system call, use
1445 * the same event enable/disable call back functions. They do not
1446 * have a pointer to the perf_event strcture as first parameter.
1448 * The functions XXX_add, XXX_del, XXX_start and XXX_stop are also common.
1449 * Reuse them and distinguish the event (always first parameter) via
1452 static struct pmu cf_diag = {
1453 .task_ctx_nr = perf_sw_context,
1454 .event_init = cfdiag_event_init,
1455 .pmu_enable = cpumf_pmu_enable,
1456 .pmu_disable = cpumf_pmu_disable,
1457 .add = cpumf_pmu_add,
1458 .del = cpumf_pmu_del,
1459 .start = cpumf_pmu_start,
1460 .stop = cpumf_pmu_stop,
1461 .read = cfdiag_read,
1463 .attr_groups = cfdiag_attr_groups
1466 /* Calculate memory needed to store all counter sets together with header and
1467 * trailer data. This is independent of the counter set authorization which
1468 * can vary depending on the configuration.
1470 static size_t cfdiag_maxsize(struct cpumf_ctr_info *info)
1472 size_t max_size = sizeof(struct cf_trailer_entry);
1473 enum cpumf_ctr_set i;
1475 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
1476 size_t size = cpum_cf_ctrset_size(i, info);
1479 max_size += size * sizeof(u64) +
1480 sizeof(struct cf_ctrset_entry);
1485 /* Get the CPU speed, try sampling facility first and CPU attributes second. */
1486 static void cfdiag_get_cpu_speed(void)
1490 if (cpum_sf_avail()) { /* Sampling facility first */
1491 struct hws_qsi_info_block si;
1493 memset(&si, 0, sizeof(si));
1495 cfdiag_cpu_speed = si.cpu_speed;
1500 /* Fallback: CPU speed extract static part. Used in case
1501 * CPU Measurement Sampling Facility is turned off.
1503 mhz = __ecag(ECAG_CPU_ATTRIBUTE, 0);
1505 cfdiag_cpu_speed = mhz & 0xffffffff;
1508 static int cfset_init(void)
1510 struct cpumf_ctr_info info;
1517 cfdiag_get_cpu_speed();
1518 /* Make sure the counter set data fits into predefined buffer. */
1519 need = cfdiag_maxsize(&info);
1520 if (need > sizeof(((struct cpu_cf_events *)0)->start)) {
1521 pr_err("Insufficient memory for PMU(cpum_cf_diag) need=%zu\n",
1526 rc = misc_register(&cfset_dev);
1528 pr_err("Registration of /dev/%s failed rc=%i\n",
1529 cfset_dev.name, rc);
1533 rc = perf_pmu_register(&cf_diag, "cpum_cf_diag", -1);
1535 misc_deregister(&cfset_dev);
1536 pr_err("Registration of PMU(cpum_cf_diag) failed with rc=%i\n",
1543 device_initcall(cpumf_pmu_init);