1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Performance event support for s390x - CPU-measurement Counter Facility
4 *
5 * Copyright IBM Corp. 2012, 2023
6 * Author(s): Hendrik Brueckner <[email protected]>
7 * Thomas Richter <[email protected]>
8 */
9 #define KMSG_COMPONENT "cpum_cf"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
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>
19 #include <linux/perf_event.h>
20
21 #include <asm/cpu_mf.h>
22 #include <asm/hwctrset.h>
23 #include <asm/debug.h>
24
25 /* Perf PMU definitions for the counter facility */
26 #define PERF_CPUM_CF_MAX_CTR 0xffffUL /* Max ctr for ECCTR */
27 #define PERF_EVENT_CPUM_CF_DIAG 0xBC000UL /* Event: Counter sets */
28
29 enum cpumf_ctr_set {
30 CPUMF_CTR_SET_BASIC = 0, /* Basic Counter Set */
31 CPUMF_CTR_SET_USER = 1, /* Problem-State Counter Set */
32 CPUMF_CTR_SET_CRYPTO = 2, /* Crypto-Activity Counter Set */
33 CPUMF_CTR_SET_EXT = 3, /* Extended Counter Set */
34 CPUMF_CTR_SET_MT_DIAG = 4, /* MT-diagnostic Counter Set */
35
36 /* Maximum number of counter sets */
37 CPUMF_CTR_SET_MAX,
38 };
39
40 #define CPUMF_LCCTL_ENABLE_SHIFT 16
41 #define CPUMF_LCCTL_ACTCTL_SHIFT 0
42
ctr_set_enable(u64 * state,u64 ctrsets)43 static inline void ctr_set_enable(u64 *state, u64 ctrsets)
44 {
45 *state |= ctrsets << CPUMF_LCCTL_ENABLE_SHIFT;
46 }
47
ctr_set_disable(u64 * state,u64 ctrsets)48 static inline void ctr_set_disable(u64 *state, u64 ctrsets)
49 {
50 *state &= ~(ctrsets << CPUMF_LCCTL_ENABLE_SHIFT);
51 }
52
ctr_set_start(u64 * state,u64 ctrsets)53 static inline void ctr_set_start(u64 *state, u64 ctrsets)
54 {
55 *state |= ctrsets << CPUMF_LCCTL_ACTCTL_SHIFT;
56 }
57
ctr_set_stop(u64 * state,u64 ctrsets)58 static inline void ctr_set_stop(u64 *state, u64 ctrsets)
59 {
60 *state &= ~(ctrsets << CPUMF_LCCTL_ACTCTL_SHIFT);
61 }
62
ctr_stcctm(enum cpumf_ctr_set set,u64 range,u64 * dest)63 static inline int ctr_stcctm(enum cpumf_ctr_set set, u64 range, u64 *dest)
64 {
65 switch (set) {
66 case CPUMF_CTR_SET_BASIC:
67 return stcctm(BASIC, range, dest);
68 case CPUMF_CTR_SET_USER:
69 return stcctm(PROBLEM_STATE, range, dest);
70 case CPUMF_CTR_SET_CRYPTO:
71 return stcctm(CRYPTO_ACTIVITY, range, dest);
72 case CPUMF_CTR_SET_EXT:
73 return stcctm(EXTENDED, range, dest);
74 case CPUMF_CTR_SET_MT_DIAG:
75 return stcctm(MT_DIAG_CLEARING, range, dest);
76 case CPUMF_CTR_SET_MAX:
77 return 3;
78 }
79 return 3;
80 }
81
82 struct cpu_cf_events {
83 refcount_t refcnt; /* Reference count */
84 atomic_t ctr_set[CPUMF_CTR_SET_MAX];
85 u64 state; /* For perf_event_open SVC */
86 u64 dev_state; /* For /dev/hwctr */
87 unsigned int flags;
88 size_t used; /* Bytes used in data */
89 size_t usedss; /* Bytes used in start/stop */
90 unsigned char start[PAGE_SIZE]; /* Counter set at event add */
91 unsigned char stop[PAGE_SIZE]; /* Counter set at event delete */
92 unsigned char data[PAGE_SIZE]; /* Counter set at /dev/hwctr */
93 unsigned int sets; /* # Counter set saved in memory */
94 };
95
96 static unsigned int cfdiag_cpu_speed; /* CPU speed for CF_DIAG trailer */
97 static debug_info_t *cf_dbg;
98
99 /*
100 * The CPU Measurement query counter information instruction contains
101 * information which varies per machine generation, but is constant and
102 * does not change when running on a particular machine, such as counter
103 * first and second version number. This is needed to determine the size
104 * of counter sets. Extract this information at device driver initialization.
105 */
106 static struct cpumf_ctr_info cpumf_ctr_info;
107
108 struct cpu_cf_ptr {
109 struct cpu_cf_events *cpucf;
110 };
111
112 static struct cpu_cf_root { /* Anchor to per CPU data */
113 refcount_t refcnt; /* Overall active events */
114 struct cpu_cf_ptr __percpu *cfptr;
115 } cpu_cf_root;
116
117 /*
118 * Serialize event initialization and event removal. Both are called from
119 * user space in task context with perf_event_open() and close()
120 * system calls.
121 *
122 * This mutex serializes functions cpum_cf_alloc_cpu() called at event
123 * initialization via cpumf_pmu_event_init() and function cpum_cf_free_cpu()
124 * called at event removal via call back function hw_perf_event_destroy()
125 * when the event is deleted. They are serialized to enforce correct
126 * bookkeeping of pointer and reference counts anchored by
127 * struct cpu_cf_root and the access to cpu_cf_root::refcnt and the
128 * per CPU pointers stored in cpu_cf_root::cfptr.
129 */
130 static DEFINE_MUTEX(pmc_reserve_mutex);
131
132 /*
133 * Get pointer to per-cpu structure.
134 *
135 * Function get_cpu_cfhw() is called from
136 * - cfset_copy_all(): This function is protected by cpus_read_lock(), so
137 * CPU hot plug remove can not happen. Event removal requires a close()
138 * first.
139 *
140 * Function this_cpu_cfhw() is called from perf common code functions:
141 * - pmu_{en|dis}able(), pmu_{add|del}()and pmu_{start|stop}():
142 * All functions execute with interrupts disabled on that particular CPU.
143 * - cfset_ioctl_{on|off}, cfset_cpu_read(): see comment cfset_copy_all().
144 *
145 * Therefore it is safe to access the CPU specific pointer to the event.
146 */
get_cpu_cfhw(int cpu)147 static struct cpu_cf_events *get_cpu_cfhw(int cpu)
148 {
149 struct cpu_cf_ptr __percpu *p = cpu_cf_root.cfptr;
150
151 if (p) {
152 struct cpu_cf_ptr *q = per_cpu_ptr(p, cpu);
153
154 return q->cpucf;
155 }
156 return NULL;
157 }
158
this_cpu_cfhw(void)159 static struct cpu_cf_events *this_cpu_cfhw(void)
160 {
161 return get_cpu_cfhw(smp_processor_id());
162 }
163
164 /* Disable counter sets on dedicated CPU */
cpum_cf_reset_cpu(void * flags)165 static void cpum_cf_reset_cpu(void *flags)
166 {
167 lcctl(0);
168 }
169
170 /* Free per CPU data when the last event is removed. */
cpum_cf_free_root(void)171 static void cpum_cf_free_root(void)
172 {
173 if (!refcount_dec_and_test(&cpu_cf_root.refcnt))
174 return;
175 free_percpu(cpu_cf_root.cfptr);
176 cpu_cf_root.cfptr = NULL;
177 irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT);
178 on_each_cpu(cpum_cf_reset_cpu, NULL, 1);
179 debug_sprintf_event(cf_dbg, 4, "%s root.refcnt %u cfptr %d\n",
180 __func__, refcount_read(&cpu_cf_root.refcnt),
181 !cpu_cf_root.cfptr);
182 }
183
184 /*
185 * On initialization of first event also allocate per CPU data dynamically.
186 * Start with an array of pointers, the array size is the maximum number of
187 * CPUs possible, which might be larger than the number of CPUs currently
188 * online.
189 */
cpum_cf_alloc_root(void)190 static int cpum_cf_alloc_root(void)
191 {
192 int rc = 0;
193
194 if (refcount_inc_not_zero(&cpu_cf_root.refcnt))
195 return rc;
196
197 /* The memory is already zeroed. */
198 cpu_cf_root.cfptr = alloc_percpu(struct cpu_cf_ptr);
199 if (cpu_cf_root.cfptr) {
200 refcount_set(&cpu_cf_root.refcnt, 1);
201 on_each_cpu(cpum_cf_reset_cpu, NULL, 1);
202 irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT);
203 } else {
204 rc = -ENOMEM;
205 }
206
207 return rc;
208 }
209
210 /* Free CPU counter data structure for a PMU */
cpum_cf_free_cpu(int cpu)211 static void cpum_cf_free_cpu(int cpu)
212 {
213 struct cpu_cf_events *cpuhw;
214 struct cpu_cf_ptr *p;
215
216 mutex_lock(&pmc_reserve_mutex);
217 /*
218 * When invoked via CPU hotplug handler, there might be no events
219 * installed or that particular CPU might not have an
220 * event installed. This anchor pointer can be NULL!
221 */
222 if (!cpu_cf_root.cfptr)
223 goto out;
224 p = per_cpu_ptr(cpu_cf_root.cfptr, cpu);
225 cpuhw = p->cpucf;
226 /*
227 * Might be zero when called from CPU hotplug handler and no event
228 * installed on that CPU, but on different CPUs.
229 */
230 if (!cpuhw)
231 goto out;
232
233 if (refcount_dec_and_test(&cpuhw->refcnt)) {
234 kfree(cpuhw);
235 p->cpucf = NULL;
236 }
237 cpum_cf_free_root();
238 out:
239 mutex_unlock(&pmc_reserve_mutex);
240 }
241
242 /* Allocate CPU counter data structure for a PMU. Called under mutex lock. */
cpum_cf_alloc_cpu(int cpu)243 static int cpum_cf_alloc_cpu(int cpu)
244 {
245 struct cpu_cf_events *cpuhw;
246 struct cpu_cf_ptr *p;
247 int rc;
248
249 mutex_lock(&pmc_reserve_mutex);
250 rc = cpum_cf_alloc_root();
251 if (rc)
252 goto unlock;
253 p = per_cpu_ptr(cpu_cf_root.cfptr, cpu);
254 cpuhw = p->cpucf;
255
256 if (!cpuhw) {
257 cpuhw = kzalloc(sizeof(*cpuhw), GFP_KERNEL);
258 if (cpuhw) {
259 p->cpucf = cpuhw;
260 refcount_set(&cpuhw->refcnt, 1);
261 } else {
262 rc = -ENOMEM;
263 }
264 } else {
265 refcount_inc(&cpuhw->refcnt);
266 }
267 if (rc) {
268 /*
269 * Error in allocation of event, decrement anchor. Since
270 * cpu_cf_event in not created, its destroy() function is not
271 * invoked. Adjust the reference counter for the anchor.
272 */
273 cpum_cf_free_root();
274 }
275 unlock:
276 mutex_unlock(&pmc_reserve_mutex);
277 return rc;
278 }
279
280 /*
281 * Create/delete per CPU data structures for /dev/hwctr interface and events
282 * created by perf_event_open().
283 * If cpu is -1, track task on all available CPUs. This requires
284 * allocation of hardware data structures for all CPUs. This setup handles
285 * perf_event_open() with task context and /dev/hwctr interface.
286 * If cpu is non-zero install event on this CPU only. This setup handles
287 * perf_event_open() with CPU context.
288 */
cpum_cf_alloc(int cpu)289 static int cpum_cf_alloc(int cpu)
290 {
291 cpumask_var_t mask;
292 int rc;
293
294 if (cpu == -1) {
295 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
296 return -ENOMEM;
297 for_each_online_cpu(cpu) {
298 rc = cpum_cf_alloc_cpu(cpu);
299 if (rc) {
300 for_each_cpu(cpu, mask)
301 cpum_cf_free_cpu(cpu);
302 break;
303 }
304 cpumask_set_cpu(cpu, mask);
305 }
306 free_cpumask_var(mask);
307 } else {
308 rc = cpum_cf_alloc_cpu(cpu);
309 }
310 return rc;
311 }
312
cpum_cf_free(int cpu)313 static void cpum_cf_free(int cpu)
314 {
315 if (cpu == -1) {
316 for_each_online_cpu(cpu)
317 cpum_cf_free_cpu(cpu);
318 } else {
319 cpum_cf_free_cpu(cpu);
320 }
321 }
322
323 #define CF_DIAG_CTRSET_DEF 0xfeef /* Counter set header mark */
324 /* interval in seconds */
325
326 /* Counter sets are stored as data stream in a page sized memory buffer and
327 * exported to user space via raw data attached to the event sample data.
328 * Each counter set starts with an eight byte header consisting of:
329 * - a two byte eye catcher (0xfeef)
330 * - a one byte counter set number
331 * - a two byte counter set size (indicates the number of counters in this set)
332 * - a three byte reserved value (must be zero) to make the header the same
333 * size as a counter value.
334 * All counter values are eight byte in size.
335 *
336 * All counter sets are followed by a 64 byte trailer.
337 * The trailer consists of a:
338 * - flag field indicating valid fields when corresponding bit set
339 * - the counter facility first and second version number
340 * - the CPU speed if nonzero
341 * - the time stamp the counter sets have been collected
342 * - the time of day (TOD) base value
343 * - the machine type.
344 *
345 * The counter sets are saved when the process is prepared to be executed on a
346 * CPU and saved again when the process is going to be removed from a CPU.
347 * The difference of both counter sets are calculated and stored in the event
348 * sample data area.
349 */
350 struct cf_ctrset_entry { /* CPU-M CF counter set entry (8 byte) */
351 unsigned int def:16; /* 0-15 Data Entry Format */
352 unsigned int set:16; /* 16-31 Counter set identifier */
353 unsigned int ctr:16; /* 32-47 Number of stored counters */
354 unsigned int res1:16; /* 48-63 Reserved */
355 };
356
357 struct cf_trailer_entry { /* CPU-M CF_DIAG trailer (64 byte) */
358 /* 0 - 7 */
359 union {
360 struct {
361 unsigned int clock_base:1; /* TOD clock base set */
362 unsigned int speed:1; /* CPU speed set */
363 /* Measurement alerts */
364 unsigned int mtda:1; /* Loss of MT ctr. data alert */
365 unsigned int caca:1; /* Counter auth. change alert */
366 unsigned int lcda:1; /* Loss of counter data alert */
367 };
368 unsigned long flags; /* 0-63 All indicators */
369 };
370 /* 8 - 15 */
371 unsigned int cfvn:16; /* 64-79 Ctr First Version */
372 unsigned int csvn:16; /* 80-95 Ctr Second Version */
373 unsigned int cpu_speed:32; /* 96-127 CPU speed */
374 /* 16 - 23 */
375 unsigned long timestamp; /* 128-191 Timestamp (TOD) */
376 /* 24 - 55 */
377 union {
378 struct {
379 unsigned long progusage1;
380 unsigned long progusage2;
381 unsigned long progusage3;
382 unsigned long tod_base;
383 };
384 unsigned long progusage[4];
385 };
386 /* 56 - 63 */
387 unsigned int mach_type:16; /* Machine type */
388 unsigned int res1:16; /* Reserved */
389 unsigned int res2:32; /* Reserved */
390 };
391
392 /* Create the trailer data at the end of a page. */
cfdiag_trailer(struct cf_trailer_entry * te)393 static void cfdiag_trailer(struct cf_trailer_entry *te)
394 {
395 struct cpuid cpuid;
396
397 te->cfvn = cpumf_ctr_info.cfvn; /* Counter version numbers */
398 te->csvn = cpumf_ctr_info.csvn;
399
400 get_cpu_id(&cpuid); /* Machine type */
401 te->mach_type = cpuid.machine;
402 te->cpu_speed = cfdiag_cpu_speed;
403 if (te->cpu_speed)
404 te->speed = 1;
405 te->clock_base = 1; /* Save clock base */
406 te->tod_base = tod_clock_base.tod;
407 te->timestamp = get_tod_clock_fast();
408 }
409
410 /*
411 * The number of counters per counter set varies between machine generations,
412 * but is constant when running on a particular machine generation.
413 * Determine each counter set size at device driver initialization and
414 * retrieve it later.
415 */
416 static size_t cpumf_ctr_setsizes[CPUMF_CTR_SET_MAX];
cpum_cf_make_setsize(enum cpumf_ctr_set ctrset)417 static void cpum_cf_make_setsize(enum cpumf_ctr_set ctrset)
418 {
419 size_t ctrset_size = 0;
420
421 switch (ctrset) {
422 case CPUMF_CTR_SET_BASIC:
423 if (cpumf_ctr_info.cfvn >= 1)
424 ctrset_size = 6;
425 break;
426 case CPUMF_CTR_SET_USER:
427 if (cpumf_ctr_info.cfvn == 1)
428 ctrset_size = 6;
429 else if (cpumf_ctr_info.cfvn >= 3)
430 ctrset_size = 2;
431 break;
432 case CPUMF_CTR_SET_CRYPTO:
433 if (cpumf_ctr_info.csvn >= 1 && cpumf_ctr_info.csvn <= 5)
434 ctrset_size = 16;
435 else if (cpumf_ctr_info.csvn >= 6)
436 ctrset_size = 20;
437 break;
438 case CPUMF_CTR_SET_EXT:
439 if (cpumf_ctr_info.csvn == 1)
440 ctrset_size = 32;
441 else if (cpumf_ctr_info.csvn == 2)
442 ctrset_size = 48;
443 else if (cpumf_ctr_info.csvn >= 3 && cpumf_ctr_info.csvn <= 5)
444 ctrset_size = 128;
445 else if (cpumf_ctr_info.csvn == 6 || cpumf_ctr_info.csvn == 7)
446 ctrset_size = 160;
447 break;
448 case CPUMF_CTR_SET_MT_DIAG:
449 if (cpumf_ctr_info.csvn > 3)
450 ctrset_size = 48;
451 break;
452 case CPUMF_CTR_SET_MAX:
453 break;
454 }
455 cpumf_ctr_setsizes[ctrset] = ctrset_size;
456 }
457
458 /*
459 * Return the maximum possible counter set size (in number of 8 byte counters)
460 * depending on type and model number.
461 */
cpum_cf_read_setsize(enum cpumf_ctr_set ctrset)462 static size_t cpum_cf_read_setsize(enum cpumf_ctr_set ctrset)
463 {
464 return cpumf_ctr_setsizes[ctrset];
465 }
466
467 /* Read a counter set. The counter set number determines the counter set and
468 * the CPUM-CF first and second version number determine the number of
469 * available counters in each counter set.
470 * Each counter set starts with header containing the counter set number and
471 * the number of eight byte counters.
472 *
473 * The functions returns the number of bytes occupied by this counter set
474 * including the header.
475 * If there is no counter in the counter set, this counter set is useless and
476 * zero is returned on this case.
477 *
478 * Note that the counter sets may not be enabled or active and the stcctm
479 * instruction might return error 3. Depending on error_ok value this is ok,
480 * for example when called from cpumf_pmu_start() call back function.
481 */
cfdiag_getctrset(struct cf_ctrset_entry * ctrdata,int ctrset,size_t room,bool error_ok)482 static size_t cfdiag_getctrset(struct cf_ctrset_entry *ctrdata, int ctrset,
483 size_t room, bool error_ok)
484 {
485 size_t ctrset_size, need = 0;
486 int rc = 3; /* Assume write failure */
487
488 ctrdata->def = CF_DIAG_CTRSET_DEF;
489 ctrdata->set = ctrset;
490 ctrdata->res1 = 0;
491 ctrset_size = cpum_cf_read_setsize(ctrset);
492
493 if (ctrset_size) { /* Save data */
494 need = ctrset_size * sizeof(u64) + sizeof(*ctrdata);
495 if (need <= room) {
496 rc = ctr_stcctm(ctrset, ctrset_size,
497 (u64 *)(ctrdata + 1));
498 }
499 if (rc != 3 || error_ok)
500 ctrdata->ctr = ctrset_size;
501 else
502 need = 0;
503 }
504
505 return need;
506 }
507
508 static const u64 cpumf_ctr_ctl[CPUMF_CTR_SET_MAX] = {
509 [CPUMF_CTR_SET_BASIC] = 0x02,
510 [CPUMF_CTR_SET_USER] = 0x04,
511 [CPUMF_CTR_SET_CRYPTO] = 0x08,
512 [CPUMF_CTR_SET_EXT] = 0x01,
513 [CPUMF_CTR_SET_MT_DIAG] = 0x20,
514 };
515
516 /* Read out all counter sets and save them in the provided data buffer.
517 * The last 64 byte host an artificial trailer entry.
518 */
cfdiag_getctr(void * data,size_t sz,unsigned long auth,bool error_ok)519 static size_t cfdiag_getctr(void *data, size_t sz, unsigned long auth,
520 bool error_ok)
521 {
522 struct cf_trailer_entry *trailer;
523 size_t offset = 0, done;
524 int i;
525
526 memset(data, 0, sz);
527 sz -= sizeof(*trailer); /* Always room for trailer */
528 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
529 struct cf_ctrset_entry *ctrdata = data + offset;
530
531 if (!(auth & cpumf_ctr_ctl[i]))
532 continue; /* Counter set not authorized */
533
534 done = cfdiag_getctrset(ctrdata, i, sz - offset, error_ok);
535 offset += done;
536 }
537 trailer = data + offset;
538 cfdiag_trailer(trailer);
539 return offset + sizeof(*trailer);
540 }
541
542 /* Calculate the difference for each counter in a counter set. */
cfdiag_diffctrset(u64 * pstart,u64 * pstop,int counters)543 static void cfdiag_diffctrset(u64 *pstart, u64 *pstop, int counters)
544 {
545 for (; --counters >= 0; ++pstart, ++pstop)
546 if (*pstop >= *pstart)
547 *pstop -= *pstart;
548 else
549 *pstop = *pstart - *pstop + 1;
550 }
551
552 /* Scan the counter sets and calculate the difference of each counter
553 * in each set. The result is the increment of each counter during the
554 * period the counter set has been activated.
555 *
556 * Return true on success.
557 */
cfdiag_diffctr(struct cpu_cf_events * cpuhw,unsigned long auth)558 static int cfdiag_diffctr(struct cpu_cf_events *cpuhw, unsigned long auth)
559 {
560 struct cf_trailer_entry *trailer_start, *trailer_stop;
561 struct cf_ctrset_entry *ctrstart, *ctrstop;
562 size_t offset = 0;
563 int i;
564
565 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
566 ctrstart = (struct cf_ctrset_entry *)(cpuhw->start + offset);
567 ctrstop = (struct cf_ctrset_entry *)(cpuhw->stop + offset);
568
569 /* Counter set not authorized */
570 if (!(auth & cpumf_ctr_ctl[i]))
571 continue;
572 /* Counter set size zero was not saved */
573 if (!cpum_cf_read_setsize(i))
574 continue;
575
576 if (memcmp(ctrstop, ctrstart, sizeof(*ctrstop))) {
577 pr_err_once("cpum_cf_diag counter set compare error "
578 "in set %i\n", ctrstart->set);
579 return 0;
580 }
581 if (ctrstart->def == CF_DIAG_CTRSET_DEF) {
582 cfdiag_diffctrset((u64 *)(ctrstart + 1),
583 (u64 *)(ctrstop + 1), ctrstart->ctr);
584 offset += ctrstart->ctr * sizeof(u64) +
585 sizeof(*ctrstart);
586 }
587 }
588
589 /* Save time_stamp from start of event in stop's trailer */
590 trailer_start = (struct cf_trailer_entry *)(cpuhw->start + offset);
591 trailer_stop = (struct cf_trailer_entry *)(cpuhw->stop + offset);
592 trailer_stop->progusage[0] = trailer_start->timestamp;
593
594 return 1;
595 }
596
get_counter_set(u64 event)597 static enum cpumf_ctr_set get_counter_set(u64 event)
598 {
599 int set = CPUMF_CTR_SET_MAX;
600
601 if (event < 32)
602 set = CPUMF_CTR_SET_BASIC;
603 else if (event < 64)
604 set = CPUMF_CTR_SET_USER;
605 else if (event < 128)
606 set = CPUMF_CTR_SET_CRYPTO;
607 else if (event < 288)
608 set = CPUMF_CTR_SET_EXT;
609 else if (event >= 448 && event < 496)
610 set = CPUMF_CTR_SET_MT_DIAG;
611
612 return set;
613 }
614
validate_ctr_version(const u64 config,enum cpumf_ctr_set set)615 static int validate_ctr_version(const u64 config, enum cpumf_ctr_set set)
616 {
617 u16 mtdiag_ctl;
618 int err = 0;
619
620 /* check required version for counter sets */
621 switch (set) {
622 case CPUMF_CTR_SET_BASIC:
623 case CPUMF_CTR_SET_USER:
624 if (cpumf_ctr_info.cfvn < 1)
625 err = -EOPNOTSUPP;
626 break;
627 case CPUMF_CTR_SET_CRYPTO:
628 if ((cpumf_ctr_info.csvn >= 1 && cpumf_ctr_info.csvn <= 5 &&
629 config > 79) || (cpumf_ctr_info.csvn >= 6 && config > 83))
630 err = -EOPNOTSUPP;
631 break;
632 case CPUMF_CTR_SET_EXT:
633 if (cpumf_ctr_info.csvn < 1)
634 err = -EOPNOTSUPP;
635 if ((cpumf_ctr_info.csvn == 1 && config > 159) ||
636 (cpumf_ctr_info.csvn == 2 && config > 175) ||
637 (cpumf_ctr_info.csvn >= 3 && cpumf_ctr_info.csvn <= 5 &&
638 config > 255) ||
639 (cpumf_ctr_info.csvn >= 6 && config > 287))
640 err = -EOPNOTSUPP;
641 break;
642 case CPUMF_CTR_SET_MT_DIAG:
643 if (cpumf_ctr_info.csvn <= 3)
644 err = -EOPNOTSUPP;
645 /*
646 * MT-diagnostic counters are read-only. The counter set
647 * is automatically enabled and activated on all CPUs with
648 * multithreading (SMT). Deactivation of multithreading
649 * also disables the counter set. State changes are ignored
650 * by lcctl(). Because Linux controls SMT enablement through
651 * a kernel parameter only, the counter set is either disabled
652 * or enabled and active.
653 *
654 * Thus, the counters can only be used if SMT is on and the
655 * counter set is enabled and active.
656 */
657 mtdiag_ctl = cpumf_ctr_ctl[CPUMF_CTR_SET_MT_DIAG];
658 if (!((cpumf_ctr_info.auth_ctl & mtdiag_ctl) &&
659 (cpumf_ctr_info.enable_ctl & mtdiag_ctl) &&
660 (cpumf_ctr_info.act_ctl & mtdiag_ctl)))
661 err = -EOPNOTSUPP;
662 break;
663 case CPUMF_CTR_SET_MAX:
664 err = -EOPNOTSUPP;
665 }
666
667 return err;
668 }
669
670 /*
671 * Change the CPUMF state to active.
672 * Enable and activate the CPU-counter sets according
673 * to the per-cpu control state.
674 */
cpumf_pmu_enable(struct pmu * pmu)675 static void cpumf_pmu_enable(struct pmu *pmu)
676 {
677 struct cpu_cf_events *cpuhw = this_cpu_cfhw();
678 int err;
679
680 if (!cpuhw || (cpuhw->flags & PMU_F_ENABLED))
681 return;
682
683 err = lcctl(cpuhw->state | cpuhw->dev_state);
684 if (err)
685 pr_err("Enabling the performance measuring unit failed with rc=%x\n", err);
686 else
687 cpuhw->flags |= PMU_F_ENABLED;
688 }
689
690 /*
691 * Change the CPUMF state to inactive.
692 * Disable and enable (inactive) the CPU-counter sets according
693 * to the per-cpu control state.
694 */
cpumf_pmu_disable(struct pmu * pmu)695 static void cpumf_pmu_disable(struct pmu *pmu)
696 {
697 struct cpu_cf_events *cpuhw = this_cpu_cfhw();
698 u64 inactive;
699 int err;
700
701 if (!cpuhw || !(cpuhw->flags & PMU_F_ENABLED))
702 return;
703
704 inactive = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1);
705 inactive |= cpuhw->dev_state;
706 err = lcctl(inactive);
707 if (err)
708 pr_err("Disabling the performance measuring unit failed with rc=%x\n", err);
709 else
710 cpuhw->flags &= ~PMU_F_ENABLED;
711 }
712
713 /* Release the PMU if event is the last perf event */
hw_perf_event_destroy(struct perf_event * event)714 static void hw_perf_event_destroy(struct perf_event *event)
715 {
716 cpum_cf_free(event->cpu);
717 }
718
719 /* CPUMF <-> perf event mappings for kernel+userspace (basic set) */
720 static const int cpumf_generic_events_basic[] = {
721 [PERF_COUNT_HW_CPU_CYCLES] = 0,
722 [PERF_COUNT_HW_INSTRUCTIONS] = 1,
723 [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
724 [PERF_COUNT_HW_CACHE_MISSES] = -1,
725 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
726 [PERF_COUNT_HW_BRANCH_MISSES] = -1,
727 [PERF_COUNT_HW_BUS_CYCLES] = -1,
728 };
729 /* CPUMF <-> perf event mappings for userspace (problem-state set) */
730 static const int cpumf_generic_events_user[] = {
731 [PERF_COUNT_HW_CPU_CYCLES] = 32,
732 [PERF_COUNT_HW_INSTRUCTIONS] = 33,
733 [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
734 [PERF_COUNT_HW_CACHE_MISSES] = -1,
735 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
736 [PERF_COUNT_HW_BRANCH_MISSES] = -1,
737 [PERF_COUNT_HW_BUS_CYCLES] = -1,
738 };
739
is_userspace_event(u64 ev)740 static int is_userspace_event(u64 ev)
741 {
742 return cpumf_generic_events_user[PERF_COUNT_HW_CPU_CYCLES] == ev ||
743 cpumf_generic_events_user[PERF_COUNT_HW_INSTRUCTIONS] == ev;
744 }
745
__hw_perf_event_init(struct perf_event * event,unsigned int type)746 static int __hw_perf_event_init(struct perf_event *event, unsigned int type)
747 {
748 struct perf_event_attr *attr = &event->attr;
749 struct hw_perf_event *hwc = &event->hw;
750 enum cpumf_ctr_set set;
751 u64 ev;
752
753 switch (type) {
754 case PERF_TYPE_RAW:
755 /* Raw events are used to access counters directly,
756 * hence do not permit excludes */
757 if (attr->exclude_kernel || attr->exclude_user ||
758 attr->exclude_hv)
759 return -EOPNOTSUPP;
760 ev = attr->config;
761 break;
762
763 case PERF_TYPE_HARDWARE:
764 if (is_sampling_event(event)) /* No sampling support */
765 return -ENOENT;
766 ev = attr->config;
767 if (!attr->exclude_user && attr->exclude_kernel) {
768 /*
769 * Count user space (problem-state) only
770 * Handle events 32 and 33 as 0:u and 1:u
771 */
772 if (!is_userspace_event(ev)) {
773 if (ev >= ARRAY_SIZE(cpumf_generic_events_user))
774 return -EOPNOTSUPP;
775 ev = cpumf_generic_events_user[ev];
776 }
777 } else if (!attr->exclude_kernel && attr->exclude_user) {
778 /* No support for kernel space counters only */
779 return -EOPNOTSUPP;
780 } else {
781 /* Count user and kernel space, incl. events 32 + 33 */
782 if (!is_userspace_event(ev)) {
783 if (ev >= ARRAY_SIZE(cpumf_generic_events_basic))
784 return -EOPNOTSUPP;
785 ev = cpumf_generic_events_basic[ev];
786 }
787 }
788 break;
789
790 default:
791 return -ENOENT;
792 }
793
794 if (ev == -1)
795 return -ENOENT;
796
797 if (ev > PERF_CPUM_CF_MAX_CTR)
798 return -ENOENT;
799
800 /* Obtain the counter set to which the specified counter belongs */
801 set = get_counter_set(ev);
802 switch (set) {
803 case CPUMF_CTR_SET_BASIC:
804 case CPUMF_CTR_SET_USER:
805 case CPUMF_CTR_SET_CRYPTO:
806 case CPUMF_CTR_SET_EXT:
807 case CPUMF_CTR_SET_MT_DIAG:
808 /*
809 * Use the hardware perf event structure to store the
810 * counter number in the 'config' member and the counter
811 * set number in the 'config_base' as bit mask.
812 * It is later used to enable/disable the counter(s).
813 */
814 hwc->config = ev;
815 hwc->config_base = cpumf_ctr_ctl[set];
816 break;
817 case CPUMF_CTR_SET_MAX:
818 /* The counter could not be associated to a counter set */
819 return -EINVAL;
820 }
821
822 /* Initialize for using the CPU-measurement counter facility */
823 if (cpum_cf_alloc(event->cpu))
824 return -ENOMEM;
825 event->destroy = hw_perf_event_destroy;
826
827 /*
828 * Finally, validate version and authorization of the counter set.
829 * If the particular CPU counter set is not authorized,
830 * return with -ENOENT in order to fall back to other
831 * PMUs that might suffice the event request.
832 */
833 if (!(hwc->config_base & cpumf_ctr_info.auth_ctl))
834 return -ENOENT;
835 return validate_ctr_version(hwc->config, set);
836 }
837
838 /* Events CPU_CYCLES and INSTRUCTIONS can be submitted with two different
839 * attribute::type values:
840 * - PERF_TYPE_HARDWARE:
841 * - pmu->type:
842 * Handle both type of invocations identical. They address the same hardware.
843 * The result is different when event modifiers exclude_kernel and/or
844 * exclude_user are also set.
845 */
cpumf_pmu_event_type(struct perf_event * event)846 static int cpumf_pmu_event_type(struct perf_event *event)
847 {
848 u64 ev = event->attr.config;
849
850 if (cpumf_generic_events_basic[PERF_COUNT_HW_CPU_CYCLES] == ev ||
851 cpumf_generic_events_basic[PERF_COUNT_HW_INSTRUCTIONS] == ev ||
852 cpumf_generic_events_user[PERF_COUNT_HW_CPU_CYCLES] == ev ||
853 cpumf_generic_events_user[PERF_COUNT_HW_INSTRUCTIONS] == ev)
854 return PERF_TYPE_HARDWARE;
855 return PERF_TYPE_RAW;
856 }
857
cpumf_pmu_event_init(struct perf_event * event)858 static int cpumf_pmu_event_init(struct perf_event *event)
859 {
860 unsigned int type = event->attr.type;
861 int err = -ENOENT;
862
863 if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_RAW)
864 err = __hw_perf_event_init(event, type);
865 else if (event->pmu->type == type)
866 /* Registered as unknown PMU */
867 err = __hw_perf_event_init(event, cpumf_pmu_event_type(event));
868
869 return err;
870 }
871
hw_perf_event_reset(struct perf_event * event)872 static int hw_perf_event_reset(struct perf_event *event)
873 {
874 u64 prev, new;
875 int err;
876
877 prev = local64_read(&event->hw.prev_count);
878 do {
879 err = ecctr(event->hw.config, &new);
880 if (err) {
881 if (err != 3)
882 break;
883 /* The counter is not (yet) available. This
884 * might happen if the counter set to which
885 * this counter belongs is in the disabled
886 * state.
887 */
888 new = 0;
889 }
890 } while (!local64_try_cmpxchg(&event->hw.prev_count, &prev, new));
891
892 return err;
893 }
894
hw_perf_event_update(struct perf_event * event)895 static void hw_perf_event_update(struct perf_event *event)
896 {
897 u64 prev, new, delta;
898 int err;
899
900 prev = local64_read(&event->hw.prev_count);
901 do {
902 err = ecctr(event->hw.config, &new);
903 if (err)
904 return;
905 } while (!local64_try_cmpxchg(&event->hw.prev_count, &prev, new));
906
907 delta = (prev <= new) ? new - prev
908 : (-1ULL - prev) + new + 1; /* overflow */
909 local64_add(delta, &event->count);
910 }
911
cpumf_pmu_read(struct perf_event * event)912 static void cpumf_pmu_read(struct perf_event *event)
913 {
914 if (event->hw.state & PERF_HES_STOPPED)
915 return;
916
917 hw_perf_event_update(event);
918 }
919
cpumf_pmu_start(struct perf_event * event,int flags)920 static void cpumf_pmu_start(struct perf_event *event, int flags)
921 {
922 struct cpu_cf_events *cpuhw = this_cpu_cfhw();
923 struct hw_perf_event *hwc = &event->hw;
924 int i;
925
926 if (!(hwc->state & PERF_HES_STOPPED))
927 return;
928
929 hwc->state = 0;
930
931 /* (Re-)enable and activate the counter set */
932 ctr_set_enable(&cpuhw->state, hwc->config_base);
933 ctr_set_start(&cpuhw->state, hwc->config_base);
934
935 /* The counter set to which this counter belongs can be already active.
936 * Because all counters in a set are active, the event->hw.prev_count
937 * needs to be synchronized. At this point, the counter set can be in
938 * the inactive or disabled state.
939 */
940 if (hwc->config == PERF_EVENT_CPUM_CF_DIAG) {
941 cpuhw->usedss = cfdiag_getctr(cpuhw->start,
942 sizeof(cpuhw->start),
943 hwc->config_base, true);
944 } else {
945 hw_perf_event_reset(event);
946 }
947
948 /* Increment refcount for counter sets */
949 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i)
950 if ((hwc->config_base & cpumf_ctr_ctl[i]))
951 atomic_inc(&cpuhw->ctr_set[i]);
952 }
953
954 /* Create perf event sample with the counter sets as raw data. The sample
955 * is then pushed to the event subsystem and the function checks for
956 * possible event overflows. If an event overflow occurs, the PMU is
957 * stopped.
958 *
959 * Return non-zero if an event overflow occurred.
960 */
cfdiag_push_sample(struct perf_event * event,struct cpu_cf_events * cpuhw)961 static int cfdiag_push_sample(struct perf_event *event,
962 struct cpu_cf_events *cpuhw)
963 {
964 struct perf_sample_data data;
965 struct perf_raw_record raw;
966 struct pt_regs regs;
967 int overflow;
968
969 /* Setup perf sample */
970 perf_sample_data_init(&data, 0, event->hw.last_period);
971 memset(®s, 0, sizeof(regs));
972 memset(&raw, 0, sizeof(raw));
973
974 if (event->attr.sample_type & PERF_SAMPLE_CPU)
975 data.cpu_entry.cpu = event->cpu;
976 if (event->attr.sample_type & PERF_SAMPLE_RAW) {
977 raw.frag.size = cpuhw->usedss;
978 raw.frag.data = cpuhw->stop;
979 perf_sample_save_raw_data(&data, event, &raw);
980 }
981
982 overflow = perf_event_overflow(event, &data, ®s);
983 if (overflow)
984 event->pmu->stop(event, 0);
985
986 perf_event_update_userpage(event);
987 return overflow;
988 }
989
cpumf_pmu_stop(struct perf_event * event,int flags)990 static void cpumf_pmu_stop(struct perf_event *event, int flags)
991 {
992 struct cpu_cf_events *cpuhw = this_cpu_cfhw();
993 struct hw_perf_event *hwc = &event->hw;
994 int i;
995
996 if (!(hwc->state & PERF_HES_STOPPED)) {
997 /* Decrement reference count for this counter set and if this
998 * is the last used counter in the set, clear activation
999 * control and set the counter set state to inactive.
1000 */
1001 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
1002 if (!(hwc->config_base & cpumf_ctr_ctl[i]))
1003 continue;
1004 if (!atomic_dec_return(&cpuhw->ctr_set[i]))
1005 ctr_set_stop(&cpuhw->state, cpumf_ctr_ctl[i]);
1006 }
1007 hwc->state |= PERF_HES_STOPPED;
1008 }
1009
1010 if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
1011 if (hwc->config == PERF_EVENT_CPUM_CF_DIAG) {
1012 local64_inc(&event->count);
1013 cpuhw->usedss = cfdiag_getctr(cpuhw->stop,
1014 sizeof(cpuhw->stop),
1015 event->hw.config_base,
1016 false);
1017 if (cfdiag_diffctr(cpuhw, event->hw.config_base))
1018 cfdiag_push_sample(event, cpuhw);
1019 } else {
1020 hw_perf_event_update(event);
1021 }
1022 hwc->state |= PERF_HES_UPTODATE;
1023 }
1024 }
1025
cpumf_pmu_add(struct perf_event * event,int flags)1026 static int cpumf_pmu_add(struct perf_event *event, int flags)
1027 {
1028 struct cpu_cf_events *cpuhw = this_cpu_cfhw();
1029
1030 ctr_set_enable(&cpuhw->state, event->hw.config_base);
1031 event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
1032
1033 if (flags & PERF_EF_START)
1034 cpumf_pmu_start(event, PERF_EF_RELOAD);
1035
1036 return 0;
1037 }
1038
cpumf_pmu_del(struct perf_event * event,int flags)1039 static void cpumf_pmu_del(struct perf_event *event, int flags)
1040 {
1041 struct cpu_cf_events *cpuhw = this_cpu_cfhw();
1042 int i;
1043
1044 cpumf_pmu_stop(event, PERF_EF_UPDATE);
1045
1046 /* Check if any counter in the counter set is still used. If not used,
1047 * change the counter set to the disabled state. This also clears the
1048 * content of all counters in the set.
1049 *
1050 * When a new perf event has been added but not yet started, this can
1051 * clear enable control and resets all counters in a set. Therefore,
1052 * cpumf_pmu_start() always has to re-enable a counter set.
1053 */
1054 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i)
1055 if (!atomic_read(&cpuhw->ctr_set[i]))
1056 ctr_set_disable(&cpuhw->state, cpumf_ctr_ctl[i]);
1057 }
1058
1059 /* Performance monitoring unit for s390x */
1060 static struct pmu cpumf_pmu = {
1061 .task_ctx_nr = perf_sw_context,
1062 .capabilities = PERF_PMU_CAP_NO_INTERRUPT,
1063 .pmu_enable = cpumf_pmu_enable,
1064 .pmu_disable = cpumf_pmu_disable,
1065 .event_init = cpumf_pmu_event_init,
1066 .add = cpumf_pmu_add,
1067 .del = cpumf_pmu_del,
1068 .start = cpumf_pmu_start,
1069 .stop = cpumf_pmu_stop,
1070 .read = cpumf_pmu_read,
1071 };
1072
1073 static struct cfset_session { /* CPUs and counter set bit mask */
1074 struct list_head head; /* Head of list of active processes */
1075 } cfset_session = {
1076 .head = LIST_HEAD_INIT(cfset_session.head)
1077 };
1078
1079 static refcount_t cfset_opencnt = REFCOUNT_INIT(0); /* Access count */
1080 /*
1081 * Synchronize access to device /dev/hwc. This mutex protects against
1082 * concurrent access to functions cfset_open() and cfset_release().
1083 * Same for CPU hotplug add and remove events triggering
1084 * cpum_cf_online_cpu() and cpum_cf_offline_cpu().
1085 * It also serializes concurrent device ioctl access from multiple
1086 * processes accessing /dev/hwc.
1087 *
1088 * The mutex protects concurrent access to the /dev/hwctr session management
1089 * struct cfset_session and reference counting variable cfset_opencnt.
1090 */
1091 static DEFINE_MUTEX(cfset_ctrset_mutex);
1092
1093 /*
1094 * CPU hotplug handles only /dev/hwctr device.
1095 * For perf_event_open() the CPU hotplug handling is done on kernel common
1096 * code:
1097 * - CPU add: Nothing is done since a file descriptor can not be created
1098 * and returned to the user.
1099 * - CPU delete: Handled by common code via pmu_disable(), pmu_stop() and
1100 * pmu_delete(). The event itself is removed when the file descriptor is
1101 * closed.
1102 */
1103 static int cfset_online_cpu(unsigned int cpu);
1104
cpum_cf_online_cpu(unsigned int cpu)1105 static int cpum_cf_online_cpu(unsigned int cpu)
1106 {
1107 int rc = 0;
1108
1109 /*
1110 * Ignore notification for perf_event_open().
1111 * Handle only /dev/hwctr device sessions.
1112 */
1113 mutex_lock(&cfset_ctrset_mutex);
1114 if (refcount_read(&cfset_opencnt)) {
1115 rc = cpum_cf_alloc_cpu(cpu);
1116 if (!rc)
1117 cfset_online_cpu(cpu);
1118 }
1119 mutex_unlock(&cfset_ctrset_mutex);
1120 return rc;
1121 }
1122
1123 static int cfset_offline_cpu(unsigned int cpu);
1124
cpum_cf_offline_cpu(unsigned int cpu)1125 static int cpum_cf_offline_cpu(unsigned int cpu)
1126 {
1127 /*
1128 * During task exit processing of grouped perf events triggered by CPU
1129 * hotplug processing, pmu_disable() is called as part of perf context
1130 * removal process. Therefore do not trigger event removal now for
1131 * perf_event_open() created events. Perf common code triggers event
1132 * destruction when the event file descriptor is closed.
1133 *
1134 * Handle only /dev/hwctr device sessions.
1135 */
1136 mutex_lock(&cfset_ctrset_mutex);
1137 if (refcount_read(&cfset_opencnt)) {
1138 cfset_offline_cpu(cpu);
1139 cpum_cf_free_cpu(cpu);
1140 }
1141 mutex_unlock(&cfset_ctrset_mutex);
1142 return 0;
1143 }
1144
1145 /* Return true if store counter set multiple instruction is available */
stccm_avail(void)1146 static inline int stccm_avail(void)
1147 {
1148 return test_facility(142);
1149 }
1150
1151 /* CPU-measurement alerts for the counter facility */
cpumf_measurement_alert(struct ext_code ext_code,unsigned int alert,unsigned long unused)1152 static void cpumf_measurement_alert(struct ext_code ext_code,
1153 unsigned int alert, unsigned long unused)
1154 {
1155 struct cpu_cf_events *cpuhw;
1156
1157 if (!(alert & CPU_MF_INT_CF_MASK))
1158 return;
1159
1160 inc_irq_stat(IRQEXT_CMC);
1161
1162 /*
1163 * Measurement alerts are shared and might happen when the PMU
1164 * is not reserved. Ignore these alerts in this case.
1165 */
1166 cpuhw = this_cpu_cfhw();
1167 if (!cpuhw)
1168 return;
1169
1170 /* counter authorization change alert */
1171 if (alert & CPU_MF_INT_CF_CACA)
1172 qctri(&cpumf_ctr_info);
1173
1174 /* loss of counter data alert */
1175 if (alert & CPU_MF_INT_CF_LCDA)
1176 pr_err("CPU[%i] Counter data was lost\n", smp_processor_id());
1177
1178 /* loss of MT counter data alert */
1179 if (alert & CPU_MF_INT_CF_MTDA)
1180 pr_warn("CPU[%i] MT counter data was lost\n",
1181 smp_processor_id());
1182 }
1183
1184 static int cfset_init(void);
cpumf_pmu_init(void)1185 static int __init cpumf_pmu_init(void)
1186 {
1187 int rc;
1188
1189 /* Extract counter measurement facility information */
1190 if (!cpum_cf_avail() || qctri(&cpumf_ctr_info))
1191 return -ENODEV;
1192
1193 /* Determine and store counter set sizes for later reference */
1194 for (rc = CPUMF_CTR_SET_BASIC; rc < CPUMF_CTR_SET_MAX; ++rc)
1195 cpum_cf_make_setsize(rc);
1196
1197 /*
1198 * Clear bit 15 of cr0 to unauthorize problem-state to
1199 * extract measurement counters
1200 */
1201 system_ctl_clear_bit(0, CR0_CPUMF_EXTRACTION_AUTH_BIT);
1202
1203 /* register handler for measurement-alert interruptions */
1204 rc = register_external_irq(EXT_IRQ_MEASURE_ALERT,
1205 cpumf_measurement_alert);
1206 if (rc) {
1207 pr_err("Registering for CPU-measurement alerts failed with rc=%i\n", rc);
1208 return rc;
1209 }
1210
1211 /* Setup s390dbf facility */
1212 cf_dbg = debug_register(KMSG_COMPONENT, 2, 1, 128);
1213 if (!cf_dbg) {
1214 pr_err("Registration of s390dbf(cpum_cf) failed\n");
1215 rc = -ENOMEM;
1216 goto out1;
1217 }
1218 debug_register_view(cf_dbg, &debug_sprintf_view);
1219
1220 cpumf_pmu.attr_groups = cpumf_cf_event_group();
1221 rc = perf_pmu_register(&cpumf_pmu, "cpum_cf", -1);
1222 if (rc) {
1223 pr_err("Registering the cpum_cf PMU failed with rc=%i\n", rc);
1224 goto out2;
1225 } else if (stccm_avail()) { /* Setup counter set device */
1226 cfset_init();
1227 }
1228
1229 rc = cpuhp_setup_state(CPUHP_AP_PERF_S390_CF_ONLINE,
1230 "perf/s390/cf:online",
1231 cpum_cf_online_cpu, cpum_cf_offline_cpu);
1232 return rc;
1233
1234 out2:
1235 debug_unregister_view(cf_dbg, &debug_sprintf_view);
1236 debug_unregister(cf_dbg);
1237 out1:
1238 unregister_external_irq(EXT_IRQ_MEASURE_ALERT, cpumf_measurement_alert);
1239 return rc;
1240 }
1241
1242 /* Support for the CPU Measurement Facility counter set extraction using
1243 * device /dev/hwctr. This allows user space programs to extract complete
1244 * counter set via normal file operations.
1245 */
1246
1247 struct cfset_call_on_cpu_parm { /* Parm struct for smp_call_on_cpu */
1248 unsigned int sets; /* Counter set bit mask */
1249 atomic_t cpus_ack; /* # CPUs successfully executed func */
1250 };
1251
1252 struct cfset_request { /* CPUs and counter set bit mask */
1253 unsigned long ctrset; /* Bit mask of counter set to read */
1254 cpumask_t mask; /* CPU mask to read from */
1255 struct list_head node; /* Chain to cfset_session.head */
1256 };
1257
cfset_session_init(void)1258 static void cfset_session_init(void)
1259 {
1260 INIT_LIST_HEAD(&cfset_session.head);
1261 }
1262
1263 /* Remove current request from global bookkeeping. Maintain a counter set bit
1264 * mask on a per CPU basis.
1265 * Done in process context under mutex protection.
1266 */
cfset_session_del(struct cfset_request * p)1267 static void cfset_session_del(struct cfset_request *p)
1268 {
1269 list_del(&p->node);
1270 }
1271
1272 /* Add current request to global bookkeeping. Maintain a counter set bit mask
1273 * on a per CPU basis.
1274 * Done in process context under mutex protection.
1275 */
cfset_session_add(struct cfset_request * p)1276 static void cfset_session_add(struct cfset_request *p)
1277 {
1278 list_add(&p->node, &cfset_session.head);
1279 }
1280
1281 /* The /dev/hwctr device access uses PMU_F_IN_USE to mark the device access
1282 * path is currently used.
1283 * The cpu_cf_events::dev_state is used to denote counter sets in use by this
1284 * interface. It is always or'ed in. If this interface is not active, its
1285 * value is zero and no additional counter sets will be included.
1286 *
1287 * The cpu_cf_events::state is used by the perf_event_open SVC and remains
1288 * unchanged.
1289 *
1290 * perf_pmu_enable() and perf_pmu_enable() and its call backs
1291 * cpumf_pmu_enable() and cpumf_pmu_disable() are called by the
1292 * performance measurement subsystem to enable per process
1293 * CPU Measurement counter facility.
1294 * The XXX_enable() and XXX_disable functions are used to turn off
1295 * x86 performance monitoring interrupt (PMI) during scheduling.
1296 * s390 uses these calls to temporarily stop and resume the active CPU
1297 * counters sets during scheduling.
1298 *
1299 * We do allow concurrent access of perf_event_open() SVC and /dev/hwctr
1300 * device access. The perf_event_open() SVC interface makes a lot of effort
1301 * to only run the counters while the calling process is actively scheduled
1302 * to run.
1303 * When /dev/hwctr interface is also used at the same time, the counter sets
1304 * will keep running, even when the process is scheduled off a CPU.
1305 * However this is not a problem and does not lead to wrong counter values
1306 * for the perf_event_open() SVC. The current counter value will be recorded
1307 * during schedule-in. At schedule-out time the current counter value is
1308 * extracted again and the delta is calculated and added to the event.
1309 */
1310 /* Stop all counter sets via ioctl interface */
cfset_ioctl_off(void * parm)1311 static void cfset_ioctl_off(void *parm)
1312 {
1313 struct cpu_cf_events *cpuhw = this_cpu_cfhw();
1314 struct cfset_call_on_cpu_parm *p = parm;
1315 int rc;
1316
1317 /* Check if any counter set used by /dev/hwctr */
1318 for (rc = CPUMF_CTR_SET_BASIC; rc < CPUMF_CTR_SET_MAX; ++rc)
1319 if ((p->sets & cpumf_ctr_ctl[rc])) {
1320 if (!atomic_dec_return(&cpuhw->ctr_set[rc])) {
1321 ctr_set_disable(&cpuhw->dev_state,
1322 cpumf_ctr_ctl[rc]);
1323 ctr_set_stop(&cpuhw->dev_state,
1324 cpumf_ctr_ctl[rc]);
1325 }
1326 }
1327 /* Keep perf_event_open counter sets */
1328 rc = lcctl(cpuhw->dev_state | cpuhw->state);
1329 if (rc)
1330 pr_err("Counter set stop %#llx of /dev/%s failed rc=%i\n",
1331 cpuhw->state, S390_HWCTR_DEVICE, rc);
1332 if (!cpuhw->dev_state)
1333 cpuhw->flags &= ~PMU_F_IN_USE;
1334 }
1335
1336 /* Start counter sets on particular CPU */
cfset_ioctl_on(void * parm)1337 static void cfset_ioctl_on(void *parm)
1338 {
1339 struct cpu_cf_events *cpuhw = this_cpu_cfhw();
1340 struct cfset_call_on_cpu_parm *p = parm;
1341 int rc;
1342
1343 cpuhw->flags |= PMU_F_IN_USE;
1344 ctr_set_enable(&cpuhw->dev_state, p->sets);
1345 ctr_set_start(&cpuhw->dev_state, p->sets);
1346 for (rc = CPUMF_CTR_SET_BASIC; rc < CPUMF_CTR_SET_MAX; ++rc)
1347 if ((p->sets & cpumf_ctr_ctl[rc]))
1348 atomic_inc(&cpuhw->ctr_set[rc]);
1349 rc = lcctl(cpuhw->dev_state | cpuhw->state); /* Start counter sets */
1350 if (!rc)
1351 atomic_inc(&p->cpus_ack);
1352 else
1353 pr_err("Counter set start %#llx of /dev/%s failed rc=%i\n",
1354 cpuhw->dev_state | cpuhw->state, S390_HWCTR_DEVICE, rc);
1355 }
1356
cfset_release_cpu(void * p)1357 static void cfset_release_cpu(void *p)
1358 {
1359 struct cpu_cf_events *cpuhw = this_cpu_cfhw();
1360 int rc;
1361
1362 cpuhw->dev_state = 0;
1363 rc = lcctl(cpuhw->state); /* Keep perf_event_open counter sets */
1364 if (rc)
1365 pr_err("Counter set release %#llx of /dev/%s failed rc=%i\n",
1366 cpuhw->state, S390_HWCTR_DEVICE, rc);
1367 }
1368
1369 /* This modifies the process CPU mask to adopt it to the currently online
1370 * CPUs. Offline CPUs can not be addresses. This call terminates the access
1371 * and is usually followed by close() or a new iotcl(..., START, ...) which
1372 * creates a new request structure.
1373 */
cfset_all_stop(struct cfset_request * req)1374 static void cfset_all_stop(struct cfset_request *req)
1375 {
1376 struct cfset_call_on_cpu_parm p = {
1377 .sets = req->ctrset,
1378 };
1379
1380 cpumask_and(&req->mask, &req->mask, cpu_online_mask);
1381 on_each_cpu_mask(&req->mask, cfset_ioctl_off, &p, 1);
1382 }
1383
1384 /* Release function is also called when application gets terminated without
1385 * doing a proper ioctl(..., S390_HWCTR_STOP, ...) command.
1386 */
cfset_release(struct inode * inode,struct file * file)1387 static int cfset_release(struct inode *inode, struct file *file)
1388 {
1389 mutex_lock(&cfset_ctrset_mutex);
1390 /* Open followed by close/exit has no private_data */
1391 if (file->private_data) {
1392 cfset_all_stop(file->private_data);
1393 cfset_session_del(file->private_data);
1394 kfree(file->private_data);
1395 file->private_data = NULL;
1396 }
1397 if (refcount_dec_and_test(&cfset_opencnt)) { /* Last close */
1398 on_each_cpu(cfset_release_cpu, NULL, 1);
1399 cpum_cf_free(-1);
1400 }
1401 mutex_unlock(&cfset_ctrset_mutex);
1402 return 0;
1403 }
1404
1405 /*
1406 * Open via /dev/hwctr device. Allocate all per CPU resources on the first
1407 * open of the device. The last close releases all per CPU resources.
1408 * Parallel perf_event_open system calls also use per CPU resources.
1409 * These invocations are handled via reference counting on the per CPU data
1410 * structures.
1411 */
cfset_open(struct inode * inode,struct file * file)1412 static int cfset_open(struct inode *inode, struct file *file)
1413 {
1414 int rc = 0;
1415
1416 if (!perfmon_capable())
1417 return -EPERM;
1418 file->private_data = NULL;
1419
1420 mutex_lock(&cfset_ctrset_mutex);
1421 if (!refcount_inc_not_zero(&cfset_opencnt)) { /* First open */
1422 rc = cpum_cf_alloc(-1);
1423 if (!rc) {
1424 cfset_session_init();
1425 refcount_set(&cfset_opencnt, 1);
1426 }
1427 }
1428 mutex_unlock(&cfset_ctrset_mutex);
1429
1430 /* nonseekable_open() never fails */
1431 return rc ?: nonseekable_open(inode, file);
1432 }
1433
cfset_all_start(struct cfset_request * req)1434 static int cfset_all_start(struct cfset_request *req)
1435 {
1436 struct cfset_call_on_cpu_parm p = {
1437 .sets = req->ctrset,
1438 .cpus_ack = ATOMIC_INIT(0),
1439 };
1440 cpumask_var_t mask;
1441 int rc = 0;
1442
1443 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1444 return -ENOMEM;
1445 cpumask_and(mask, &req->mask, cpu_online_mask);
1446 on_each_cpu_mask(mask, cfset_ioctl_on, &p, 1);
1447 if (atomic_read(&p.cpus_ack) != cpumask_weight(mask)) {
1448 on_each_cpu_mask(mask, cfset_ioctl_off, &p, 1);
1449 rc = -EIO;
1450 }
1451 free_cpumask_var(mask);
1452 return rc;
1453 }
1454
1455 /* Return the maximum required space for all possible CPUs in case one
1456 * CPU will be onlined during the START, READ, STOP cycles.
1457 * To find out the size of the counter sets, any one CPU will do. They
1458 * all have the same counter sets.
1459 */
cfset_needspace(unsigned int sets)1460 static size_t cfset_needspace(unsigned int sets)
1461 {
1462 size_t bytes = 0;
1463 int i;
1464
1465 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
1466 if (!(sets & cpumf_ctr_ctl[i]))
1467 continue;
1468 bytes += cpum_cf_read_setsize(i) * sizeof(u64) +
1469 sizeof(((struct s390_ctrset_setdata *)0)->set) +
1470 sizeof(((struct s390_ctrset_setdata *)0)->no_cnts);
1471 }
1472 bytes = sizeof(((struct s390_ctrset_read *)0)->no_cpus) + nr_cpu_ids *
1473 (bytes + sizeof(((struct s390_ctrset_cpudata *)0)->cpu_nr) +
1474 sizeof(((struct s390_ctrset_cpudata *)0)->no_sets));
1475 return bytes;
1476 }
1477
cfset_all_copy(unsigned long arg,cpumask_t * mask)1478 static int cfset_all_copy(unsigned long arg, cpumask_t *mask)
1479 {
1480 struct s390_ctrset_read __user *ctrset_read;
1481 unsigned int cpu, cpus, rc = 0;
1482 void __user *uptr;
1483
1484 ctrset_read = (struct s390_ctrset_read __user *)arg;
1485 uptr = ctrset_read->data;
1486 for_each_cpu(cpu, mask) {
1487 struct cpu_cf_events *cpuhw = get_cpu_cfhw(cpu);
1488 struct s390_ctrset_cpudata __user *ctrset_cpudata;
1489
1490 ctrset_cpudata = uptr;
1491 rc = put_user(cpu, &ctrset_cpudata->cpu_nr);
1492 rc |= put_user(cpuhw->sets, &ctrset_cpudata->no_sets);
1493 rc |= copy_to_user(ctrset_cpudata->data, cpuhw->data,
1494 cpuhw->used);
1495 if (rc) {
1496 rc = -EFAULT;
1497 goto out;
1498 }
1499 uptr += sizeof(struct s390_ctrset_cpudata) + cpuhw->used;
1500 cond_resched();
1501 }
1502 cpus = cpumask_weight(mask);
1503 if (put_user(cpus, &ctrset_read->no_cpus))
1504 rc = -EFAULT;
1505 out:
1506 return rc;
1507 }
1508
cfset_cpuset_read(struct s390_ctrset_setdata * p,int ctrset,int ctrset_size,size_t room)1509 static size_t cfset_cpuset_read(struct s390_ctrset_setdata *p, int ctrset,
1510 int ctrset_size, size_t room)
1511 {
1512 size_t need = 0;
1513 int rc = -1;
1514
1515 need = sizeof(*p) + sizeof(u64) * ctrset_size;
1516 if (need <= room) {
1517 p->set = cpumf_ctr_ctl[ctrset];
1518 p->no_cnts = ctrset_size;
1519 rc = ctr_stcctm(ctrset, ctrset_size, (u64 *)p->cv);
1520 if (rc == 3) /* Nothing stored */
1521 need = 0;
1522 }
1523 return need;
1524 }
1525
1526 /* Read all counter sets. */
cfset_cpu_read(void * parm)1527 static void cfset_cpu_read(void *parm)
1528 {
1529 struct cpu_cf_events *cpuhw = this_cpu_cfhw();
1530 struct cfset_call_on_cpu_parm *p = parm;
1531 int set, set_size;
1532 size_t space;
1533
1534 /* No data saved yet */
1535 cpuhw->used = 0;
1536 cpuhw->sets = 0;
1537 memset(cpuhw->data, 0, sizeof(cpuhw->data));
1538
1539 /* Scan the counter sets */
1540 for (set = CPUMF_CTR_SET_BASIC; set < CPUMF_CTR_SET_MAX; ++set) {
1541 struct s390_ctrset_setdata *sp = (void *)cpuhw->data +
1542 cpuhw->used;
1543
1544 if (!(p->sets & cpumf_ctr_ctl[set]))
1545 continue; /* Counter set not in list */
1546 set_size = cpum_cf_read_setsize(set);
1547 space = sizeof(cpuhw->data) - cpuhw->used;
1548 space = cfset_cpuset_read(sp, set, set_size, space);
1549 if (space) {
1550 cpuhw->used += space;
1551 cpuhw->sets += 1;
1552 }
1553 }
1554 }
1555
cfset_all_read(unsigned long arg,struct cfset_request * req)1556 static int cfset_all_read(unsigned long arg, struct cfset_request *req)
1557 {
1558 struct cfset_call_on_cpu_parm p;
1559 cpumask_var_t mask;
1560 int rc;
1561
1562 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
1563 return -ENOMEM;
1564
1565 p.sets = req->ctrset;
1566 cpumask_and(mask, &req->mask, cpu_online_mask);
1567 on_each_cpu_mask(mask, cfset_cpu_read, &p, 1);
1568 rc = cfset_all_copy(arg, mask);
1569 free_cpumask_var(mask);
1570 return rc;
1571 }
1572
cfset_ioctl_read(unsigned long arg,struct cfset_request * req)1573 static long cfset_ioctl_read(unsigned long arg, struct cfset_request *req)
1574 {
1575 int ret = -ENODATA;
1576
1577 if (req && req->ctrset)
1578 ret = cfset_all_read(arg, req);
1579 return ret;
1580 }
1581
cfset_ioctl_stop(struct file * file)1582 static long cfset_ioctl_stop(struct file *file)
1583 {
1584 struct cfset_request *req = file->private_data;
1585 int ret = -ENXIO;
1586
1587 if (req) {
1588 cfset_all_stop(req);
1589 cfset_session_del(req);
1590 kfree(req);
1591 file->private_data = NULL;
1592 ret = 0;
1593 }
1594 return ret;
1595 }
1596
cfset_ioctl_start(unsigned long arg,struct file * file)1597 static long cfset_ioctl_start(unsigned long arg, struct file *file)
1598 {
1599 struct s390_ctrset_start __user *ustart;
1600 struct s390_ctrset_start start;
1601 struct cfset_request *preq;
1602 void __user *umask;
1603 unsigned int len;
1604 int ret = 0;
1605 size_t need;
1606
1607 if (file->private_data)
1608 return -EBUSY;
1609 ustart = (struct s390_ctrset_start __user *)arg;
1610 if (copy_from_user(&start, ustart, sizeof(start)))
1611 return -EFAULT;
1612 if (start.version != S390_HWCTR_START_VERSION)
1613 return -EINVAL;
1614 if (start.counter_sets & ~(cpumf_ctr_ctl[CPUMF_CTR_SET_BASIC] |
1615 cpumf_ctr_ctl[CPUMF_CTR_SET_USER] |
1616 cpumf_ctr_ctl[CPUMF_CTR_SET_CRYPTO] |
1617 cpumf_ctr_ctl[CPUMF_CTR_SET_EXT] |
1618 cpumf_ctr_ctl[CPUMF_CTR_SET_MT_DIAG]))
1619 return -EINVAL; /* Invalid counter set */
1620 if (!start.counter_sets)
1621 return -EINVAL; /* No counter set at all? */
1622
1623 preq = kzalloc(sizeof(*preq), GFP_KERNEL);
1624 if (!preq)
1625 return -ENOMEM;
1626 cpumask_clear(&preq->mask);
1627 len = min_t(u64, start.cpumask_len, cpumask_size());
1628 umask = (void __user *)start.cpumask;
1629 if (copy_from_user(&preq->mask, umask, len)) {
1630 kfree(preq);
1631 return -EFAULT;
1632 }
1633 if (cpumask_empty(&preq->mask)) {
1634 kfree(preq);
1635 return -EINVAL;
1636 }
1637 need = cfset_needspace(start.counter_sets);
1638 if (put_user(need, &ustart->data_bytes)) {
1639 kfree(preq);
1640 return -EFAULT;
1641 }
1642 preq->ctrset = start.counter_sets;
1643 ret = cfset_all_start(preq);
1644 if (!ret) {
1645 cfset_session_add(preq);
1646 file->private_data = preq;
1647 } else {
1648 kfree(preq);
1649 }
1650 return ret;
1651 }
1652
1653 /* Entry point to the /dev/hwctr device interface.
1654 * The ioctl system call supports three subcommands:
1655 * S390_HWCTR_START: Start the specified counter sets on a CPU list. The
1656 * counter set keeps running until explicitly stopped. Returns the number
1657 * of bytes needed to store the counter values. If another S390_HWCTR_START
1658 * ioctl subcommand is called without a previous S390_HWCTR_STOP stop
1659 * command on the same file descriptor, -EBUSY is returned.
1660 * S390_HWCTR_READ: Read the counter set values from specified CPU list given
1661 * with the S390_HWCTR_START command.
1662 * S390_HWCTR_STOP: Stops the counter sets on the CPU list given with the
1663 * previous S390_HWCTR_START subcommand.
1664 */
cfset_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1665 static long cfset_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1666 {
1667 int ret;
1668
1669 cpus_read_lock();
1670 mutex_lock(&cfset_ctrset_mutex);
1671 switch (cmd) {
1672 case S390_HWCTR_START:
1673 ret = cfset_ioctl_start(arg, file);
1674 break;
1675 case S390_HWCTR_STOP:
1676 ret = cfset_ioctl_stop(file);
1677 break;
1678 case S390_HWCTR_READ:
1679 ret = cfset_ioctl_read(arg, file->private_data);
1680 break;
1681 default:
1682 ret = -ENOTTY;
1683 break;
1684 }
1685 mutex_unlock(&cfset_ctrset_mutex);
1686 cpus_read_unlock();
1687 return ret;
1688 }
1689
1690 static const struct file_operations cfset_fops = {
1691 .owner = THIS_MODULE,
1692 .open = cfset_open,
1693 .release = cfset_release,
1694 .unlocked_ioctl = cfset_ioctl,
1695 .compat_ioctl = cfset_ioctl,
1696 };
1697
1698 static struct miscdevice cfset_dev = {
1699 .name = S390_HWCTR_DEVICE,
1700 .minor = MISC_DYNAMIC_MINOR,
1701 .fops = &cfset_fops,
1702 .mode = 0666,
1703 };
1704
1705 /* Hotplug add of a CPU. Scan through all active processes and add
1706 * that CPU to the list of CPUs supplied with ioctl(..., START, ...).
1707 */
cfset_online_cpu(unsigned int cpu)1708 static int cfset_online_cpu(unsigned int cpu)
1709 {
1710 struct cfset_call_on_cpu_parm p;
1711 struct cfset_request *rp;
1712
1713 if (!list_empty(&cfset_session.head)) {
1714 list_for_each_entry(rp, &cfset_session.head, node) {
1715 p.sets = rp->ctrset;
1716 cfset_ioctl_on(&p);
1717 cpumask_set_cpu(cpu, &rp->mask);
1718 }
1719 }
1720 return 0;
1721 }
1722
1723 /* Hotplug remove of a CPU. Scan through all active processes and clear
1724 * that CPU from the list of CPUs supplied with ioctl(..., START, ...).
1725 * Adjust reference counts.
1726 */
cfset_offline_cpu(unsigned int cpu)1727 static int cfset_offline_cpu(unsigned int cpu)
1728 {
1729 struct cfset_call_on_cpu_parm p;
1730 struct cfset_request *rp;
1731
1732 if (!list_empty(&cfset_session.head)) {
1733 list_for_each_entry(rp, &cfset_session.head, node) {
1734 p.sets = rp->ctrset;
1735 cfset_ioctl_off(&p);
1736 cpumask_clear_cpu(cpu, &rp->mask);
1737 }
1738 }
1739 return 0;
1740 }
1741
cfdiag_read(struct perf_event * event)1742 static void cfdiag_read(struct perf_event *event)
1743 {
1744 }
1745
get_authctrsets(void)1746 static int get_authctrsets(void)
1747 {
1748 unsigned long auth = 0;
1749 enum cpumf_ctr_set i;
1750
1751 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
1752 if (cpumf_ctr_info.auth_ctl & cpumf_ctr_ctl[i])
1753 auth |= cpumf_ctr_ctl[i];
1754 }
1755 return auth;
1756 }
1757
1758 /* Setup the event. Test for authorized counter sets and only include counter
1759 * sets which are authorized at the time of the setup. Including unauthorized
1760 * counter sets result in specification exception (and panic).
1761 */
cfdiag_event_init2(struct perf_event * event)1762 static int cfdiag_event_init2(struct perf_event *event)
1763 {
1764 struct perf_event_attr *attr = &event->attr;
1765 int err = 0;
1766
1767 /* Set sample_period to indicate sampling */
1768 event->hw.config = attr->config;
1769 event->hw.sample_period = attr->sample_period;
1770 local64_set(&event->hw.period_left, event->hw.sample_period);
1771 local64_set(&event->count, 0);
1772 event->hw.last_period = event->hw.sample_period;
1773
1774 /* Add all authorized counter sets to config_base. The
1775 * the hardware init function is either called per-cpu or just once
1776 * for all CPUS (event->cpu == -1). This depends on the whether
1777 * counting is started for all CPUs or on a per workload base where
1778 * the perf event moves from one CPU to another CPU.
1779 * Checking the authorization on any CPU is fine as the hardware
1780 * applies the same authorization settings to all CPUs.
1781 */
1782 event->hw.config_base = get_authctrsets();
1783
1784 /* No authorized counter sets, nothing to count/sample */
1785 if (!event->hw.config_base)
1786 err = -EINVAL;
1787
1788 return err;
1789 }
1790
cfdiag_event_init(struct perf_event * event)1791 static int cfdiag_event_init(struct perf_event *event)
1792 {
1793 struct perf_event_attr *attr = &event->attr;
1794 int err = -ENOENT;
1795
1796 if (event->attr.config != PERF_EVENT_CPUM_CF_DIAG ||
1797 event->attr.type != event->pmu->type)
1798 goto out;
1799
1800 /* Raw events are used to access counters directly,
1801 * hence do not permit excludes.
1802 * This event is useless without PERF_SAMPLE_RAW to return counter set
1803 * values as raw data.
1804 */
1805 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv ||
1806 !(attr->sample_type & (PERF_SAMPLE_CPU | PERF_SAMPLE_RAW))) {
1807 err = -EOPNOTSUPP;
1808 goto out;
1809 }
1810
1811 /* Initialize for using the CPU-measurement counter facility */
1812 if (cpum_cf_alloc(event->cpu))
1813 return -ENOMEM;
1814 event->destroy = hw_perf_event_destroy;
1815
1816 err = cfdiag_event_init2(event);
1817 out:
1818 return err;
1819 }
1820
1821 /* Create cf_diag/events/CF_DIAG event sysfs file. This counter is used
1822 * to collect the complete counter sets for a scheduled process. Target
1823 * are complete counter sets attached as raw data to the artificial event.
1824 * This results in complete counter sets available when a process is
1825 * scheduled. Contains the delta of every counter while the process was
1826 * running.
1827 */
1828 CPUMF_EVENT_ATTR(CF_DIAG, CF_DIAG, PERF_EVENT_CPUM_CF_DIAG);
1829
1830 static struct attribute *cfdiag_events_attr[] = {
1831 CPUMF_EVENT_PTR(CF_DIAG, CF_DIAG),
1832 NULL,
1833 };
1834
1835 PMU_FORMAT_ATTR(event, "config:0-63");
1836
1837 static struct attribute *cfdiag_format_attr[] = {
1838 &format_attr_event.attr,
1839 NULL,
1840 };
1841
1842 static struct attribute_group cfdiag_events_group = {
1843 .name = "events",
1844 .attrs = cfdiag_events_attr,
1845 };
1846 static struct attribute_group cfdiag_format_group = {
1847 .name = "format",
1848 .attrs = cfdiag_format_attr,
1849 };
1850 static const struct attribute_group *cfdiag_attr_groups[] = {
1851 &cfdiag_events_group,
1852 &cfdiag_format_group,
1853 NULL,
1854 };
1855
1856 /* Performance monitoring unit for event CF_DIAG. Since this event
1857 * is also started and stopped via the perf_event_open() system call, use
1858 * the same event enable/disable call back functions. They do not
1859 * have a pointer to the perf_event structure as first parameter.
1860 *
1861 * The functions XXX_add, XXX_del, XXX_start and XXX_stop are also common.
1862 * Reuse them and distinguish the event (always first parameter) via
1863 * 'config' member.
1864 */
1865 static struct pmu cf_diag = {
1866 .task_ctx_nr = perf_sw_context,
1867 .event_init = cfdiag_event_init,
1868 .pmu_enable = cpumf_pmu_enable,
1869 .pmu_disable = cpumf_pmu_disable,
1870 .add = cpumf_pmu_add,
1871 .del = cpumf_pmu_del,
1872 .start = cpumf_pmu_start,
1873 .stop = cpumf_pmu_stop,
1874 .read = cfdiag_read,
1875
1876 .attr_groups = cfdiag_attr_groups
1877 };
1878
1879 /* Calculate memory needed to store all counter sets together with header and
1880 * trailer data. This is independent of the counter set authorization which
1881 * can vary depending on the configuration.
1882 */
cfdiag_maxsize(struct cpumf_ctr_info * info)1883 static size_t cfdiag_maxsize(struct cpumf_ctr_info *info)
1884 {
1885 size_t max_size = sizeof(struct cf_trailer_entry);
1886 enum cpumf_ctr_set i;
1887
1888 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) {
1889 size_t size = cpum_cf_read_setsize(i);
1890
1891 if (size)
1892 max_size += size * sizeof(u64) +
1893 sizeof(struct cf_ctrset_entry);
1894 }
1895 return max_size;
1896 }
1897
1898 /* Get the CPU speed, try sampling facility first and CPU attributes second. */
cfdiag_get_cpu_speed(void)1899 static void cfdiag_get_cpu_speed(void)
1900 {
1901 unsigned long mhz;
1902
1903 if (cpum_sf_avail()) { /* Sampling facility first */
1904 struct hws_qsi_info_block si;
1905
1906 memset(&si, 0, sizeof(si));
1907 if (!qsi(&si)) {
1908 cfdiag_cpu_speed = si.cpu_speed;
1909 return;
1910 }
1911 }
1912
1913 /* Fallback: CPU speed extract static part. Used in case
1914 * CPU Measurement Sampling Facility is turned off.
1915 */
1916 mhz = __ecag(ECAG_CPU_ATTRIBUTE, 0);
1917 if (mhz != -1UL)
1918 cfdiag_cpu_speed = mhz & 0xffffffff;
1919 }
1920
cfset_init(void)1921 static int cfset_init(void)
1922 {
1923 size_t need;
1924 int rc;
1925
1926 cfdiag_get_cpu_speed();
1927 /* Make sure the counter set data fits into predefined buffer. */
1928 need = cfdiag_maxsize(&cpumf_ctr_info);
1929 if (need > sizeof(((struct cpu_cf_events *)0)->start)) {
1930 pr_err("Insufficient memory for PMU(cpum_cf_diag) need=%zu\n",
1931 need);
1932 return -ENOMEM;
1933 }
1934
1935 rc = misc_register(&cfset_dev);
1936 if (rc) {
1937 pr_err("Registration of /dev/%s failed rc=%i\n",
1938 cfset_dev.name, rc);
1939 goto out;
1940 }
1941
1942 rc = perf_pmu_register(&cf_diag, "cpum_cf_diag", -1);
1943 if (rc) {
1944 misc_deregister(&cfset_dev);
1945 pr_err("Registration of PMU(cpum_cf_diag) failed with rc=%i\n",
1946 rc);
1947 }
1948 out:
1949 return rc;
1950 }
1951
1952 device_initcall(cpumf_pmu_init);
1953