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