xref: /linux/arch/s390/kernel/perf_pai_ext.c (revision bb118e86dfcc096b8a3889c1a5c88f214e1f65fa)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Performance event support - Processor Activity Instrumentation Extension
4  * Facility
5  *
6  *  Copyright IBM Corp. 2022
7  *  Author(s): Thomas Richter <tmricht@linux.ibm.com>
8  */
9 #define KMSG_COMPONENT	"pai_ext"
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/io.h>
19 #include <linux/perf_event.h>
20 #include <asm/ctlreg.h>
21 #include <asm/pai.h>
22 #include <asm/debug.h>
23 
24 #define	PAIE1_CB_SZ		0x200	/* Size of PAIE1 control block */
25 #define	PAIE1_CTRBLOCK_SZ	0x400	/* Size of PAIE1 counter blocks */
26 
27 static debug_info_t *paiext_dbg;
28 static unsigned int paiext_cnt;	/* Extracted with QPACI instruction */
29 
30 struct pai_userdata {
31 	u16 num;
32 	u64 value;
33 } __packed;
34 
35 /* Create the PAI extension 1 control block area.
36  * The PAI extension control block 1 is pointed to by lowcore
37  * address 0x1508 for each CPU. This control block is 512 bytes in size
38  * and requires a 512 byte boundary alignment.
39  */
40 struct paiext_cb {		/* PAI extension 1 control block */
41 	u64 header;		/* Not used */
42 	u64 reserved1;
43 	u64 acc;		/* Addr to analytics counter control block */
44 	u8 reserved2[488];
45 } __packed;
46 
47 struct paiext_map {
48 	unsigned long *area;		/* Area for CPU to store counters */
49 	struct pai_userdata *save;	/* Area to store non-zero counters */
50 	enum paievt_mode mode;		/* Type of event */
51 	unsigned int active_events;	/* # of PAI Extension users */
52 	refcount_t refcnt;
53 	struct perf_event *event;	/* Perf event for sampling */
54 	struct paiext_cb *paiext_cb;	/* PAI extension control block area */
55 };
56 
57 struct paiext_mapptr {
58 	struct paiext_map *mapptr;
59 };
60 
61 static struct paiext_root {		/* Anchor to per CPU data */
62 	refcount_t refcnt;		/* Overall active events */
63 	struct paiext_mapptr __percpu *mapptr;
64 } paiext_root;
65 
66 /* Free per CPU data when the last event is removed. */
67 static void paiext_root_free(void)
68 {
69 	if (refcount_dec_and_test(&paiext_root.refcnt)) {
70 		free_percpu(paiext_root.mapptr);
71 		paiext_root.mapptr = NULL;
72 	}
73 }
74 
75 /* On initialization of first event also allocate per CPU data dynamically.
76  * Start with an array of pointers, the array size is the maximum number of
77  * CPUs possible, which might be larger than the number of CPUs currently
78  * online.
79  */
80 static int paiext_root_alloc(void)
81 {
82 	if (!refcount_inc_not_zero(&paiext_root.refcnt)) {
83 		/* The memory is already zeroed. */
84 		paiext_root.mapptr = alloc_percpu(struct paiext_mapptr);
85 		if (!paiext_root.mapptr) {
86 			/* Returning without refcnt adjustment is ok. The
87 			 * error code is handled by paiext_alloc() which
88 			 * decrements refcnt when an event can not be
89 			 * created.
90 			 */
91 			return -ENOMEM;
92 		}
93 		refcount_set(&paiext_root.refcnt, 1);
94 	}
95 	return 0;
96 }
97 
98 /* Protects against concurrent increment of sampler and counter member
99  * increments at the same time and prohibits concurrent execution of
100  * counting and sampling events.
101  * Ensures that analytics counter block is deallocated only when the
102  * sampling and counting on that cpu is zero.
103  * For details see paiext_alloc().
104  */
105 static DEFINE_MUTEX(paiext_reserve_mutex);
106 
107 /* Free all memory allocated for event counting/sampling setup */
108 static void paiext_free(struct paiext_mapptr *mp)
109 {
110 	kfree(mp->mapptr->area);
111 	kfree(mp->mapptr->paiext_cb);
112 	kvfree(mp->mapptr->save);
113 	kfree(mp->mapptr);
114 	mp->mapptr = NULL;
115 }
116 
117 /* Release the PMU if event is the last perf event */
118 static void paiext_event_destroy(struct perf_event *event)
119 {
120 	struct paiext_mapptr *mp = per_cpu_ptr(paiext_root.mapptr, event->cpu);
121 	struct paiext_map *cpump = mp->mapptr;
122 
123 	mutex_lock(&paiext_reserve_mutex);
124 	cpump->event = NULL;
125 	if (refcount_dec_and_test(&cpump->refcnt))	/* Last reference gone */
126 		paiext_free(mp);
127 	paiext_root_free();
128 	mutex_unlock(&paiext_reserve_mutex);
129 	debug_sprintf_event(paiext_dbg, 4, "%s cpu %d mapptr %p\n", __func__,
130 			    event->cpu, mp->mapptr);
131 
132 }
133 
134 /* Used to avoid races in checking concurrent access of counting and
135  * sampling for pai_extension events.
136  *
137  * Only one instance of event pai_ext/NNPA_ALL/ for sampling is
138  * allowed and when this event is running, no counting event is allowed.
139  * Several counting events are allowed in parallel, but no sampling event
140  * is allowed while one (or more) counting events are running.
141  *
142  * This function is called in process context and it is safe to block.
143  * When the event initialization functions fails, no other call back will
144  * be invoked.
145  *
146  * Allocate the memory for the event.
147  */
148 static int paiext_alloc(struct perf_event_attr *a, struct perf_event *event)
149 {
150 	struct paiext_mapptr *mp;
151 	struct paiext_map *cpump;
152 	int rc;
153 
154 	mutex_lock(&paiext_reserve_mutex);
155 
156 	rc = paiext_root_alloc();
157 	if (rc)
158 		goto unlock;
159 
160 	mp = per_cpu_ptr(paiext_root.mapptr, event->cpu);
161 	cpump = mp->mapptr;
162 	if (!cpump) {			/* Paiext_map allocated? */
163 		rc = -ENOMEM;
164 		cpump = kzalloc(sizeof(*cpump), GFP_KERNEL);
165 		if (!cpump)
166 			goto undo;
167 
168 		/* Allocate memory for counter area and counter extraction.
169 		 * These are
170 		 * - a 512 byte block and requires 512 byte boundary alignment.
171 		 * - a 1KB byte block and requires 1KB boundary alignment.
172 		 * Only the first counting event has to allocate the area.
173 		 *
174 		 * Note: This works with commit 59bb47985c1d by default.
175 		 * Backporting this to kernels without this commit might
176 		 * need adjustment.
177 		 */
178 		mp->mapptr = cpump;
179 		cpump->area = kzalloc(PAIE1_CTRBLOCK_SZ, GFP_KERNEL);
180 		cpump->paiext_cb = kzalloc(PAIE1_CB_SZ, GFP_KERNEL);
181 		cpump->save = kvmalloc_array(paiext_cnt + 1,
182 					     sizeof(struct pai_userdata),
183 					     GFP_KERNEL);
184 		if (!cpump->save || !cpump->area || !cpump->paiext_cb) {
185 			paiext_free(mp);
186 			goto undo;
187 		}
188 		refcount_set(&cpump->refcnt, 1);
189 		cpump->mode = a->sample_period ? PAI_MODE_SAMPLING
190 					       : PAI_MODE_COUNTING;
191 	} else {
192 		/* Multiple invocation, check what is active.
193 		 * Supported are multiple counter events or only one sampling
194 		 * event concurrently at any one time.
195 		 */
196 		if (cpump->mode == PAI_MODE_SAMPLING ||
197 		    (cpump->mode == PAI_MODE_COUNTING && a->sample_period)) {
198 			rc = -EBUSY;
199 			goto undo;
200 		}
201 		refcount_inc(&cpump->refcnt);
202 	}
203 
204 	rc = 0;
205 	cpump->event = event;
206 
207 undo:
208 	if (rc) {
209 		/* Error in allocation of event, decrement anchor. Since
210 		 * the event in not created, its destroy() function is never
211 		 * invoked. Adjust the reference counter for the anchor.
212 		 */
213 		paiext_root_free();
214 	}
215 unlock:
216 	mutex_unlock(&paiext_reserve_mutex);
217 	/* If rc is non-zero, no increment of counter/sampler was done. */
218 	return rc;
219 }
220 
221 /* The PAI extension 1 control block supports up to 128 entries. Return
222  * the index within PAIE1_CB given the event number. Also validate event
223  * number.
224  */
225 static int paiext_event_valid(struct perf_event *event)
226 {
227 	u64 cfg = event->attr.config;
228 
229 	if (cfg >= PAI_NNPA_BASE && cfg <= PAI_NNPA_BASE + paiext_cnt) {
230 		/* Offset NNPA in paiext_cb */
231 		event->hw.config_base = offsetof(struct paiext_cb, acc);
232 		return 0;
233 	}
234 	return -EINVAL;
235 }
236 
237 /* Might be called on different CPU than the one the event is intended for. */
238 static int paiext_event_init(struct perf_event *event)
239 {
240 	struct perf_event_attr *a = &event->attr;
241 	int rc;
242 
243 	/* PMU pai_ext registered as PERF_TYPE_RAW, check event type */
244 	if (a->type != PERF_TYPE_RAW && event->pmu->type != a->type)
245 		return -ENOENT;
246 	/* PAI extension event must be valid and in supported range */
247 	rc = paiext_event_valid(event);
248 	if (rc)
249 		return rc;
250 	/* Allow only CPU wide operation, no process context for now. */
251 	if ((event->attach_state & PERF_ATTACH_TASK) || event->cpu == -1)
252 		return -ENOENT;
253 	/* Allow only event NNPA_ALL for sampling. */
254 	if (a->sample_period && a->config != PAI_NNPA_BASE)
255 		return -EINVAL;
256 	/* Prohibit exclude_user event selection */
257 	if (a->exclude_user)
258 		return -EINVAL;
259 
260 	rc = paiext_alloc(a, event);
261 	if (rc)
262 		return rc;
263 	event->hw.last_tag = 0;
264 	event->destroy = paiext_event_destroy;
265 
266 	if (a->sample_period) {
267 		a->sample_period = 1;
268 		a->freq = 0;
269 		/* Register for paicrypt_sched_task() to be called */
270 		event->attach_state |= PERF_ATTACH_SCHED_CB;
271 		/* Add raw data which are the memory mapped counters */
272 		a->sample_type |= PERF_SAMPLE_RAW;
273 		/* Turn off inheritance */
274 		a->inherit = 0;
275 	}
276 
277 	return 0;
278 }
279 
280 static u64 paiext_getctr(struct paiext_map *cpump, int nr)
281 {
282 	return cpump->area[nr];
283 }
284 
285 /* Read the counter values. Return value from location in buffer. For event
286  * NNPA_ALL sum up all events.
287  */
288 static u64 paiext_getdata(struct perf_event *event)
289 {
290 	struct paiext_mapptr *mp = this_cpu_ptr(paiext_root.mapptr);
291 	struct paiext_map *cpump = mp->mapptr;
292 	u64 sum = 0;
293 	int i;
294 
295 	if (event->attr.config != PAI_NNPA_BASE)
296 		return paiext_getctr(cpump, event->attr.config - PAI_NNPA_BASE);
297 
298 	for (i = 1; i <= paiext_cnt; i++)
299 		sum += paiext_getctr(cpump, i);
300 
301 	return sum;
302 }
303 
304 static u64 paiext_getall(struct perf_event *event)
305 {
306 	return paiext_getdata(event);
307 }
308 
309 static void paiext_read(struct perf_event *event)
310 {
311 	u64 prev, new, delta;
312 
313 	prev = local64_read(&event->hw.prev_count);
314 	new = paiext_getall(event);
315 	local64_set(&event->hw.prev_count, new);
316 	delta = new - prev;
317 	local64_add(delta, &event->count);
318 }
319 
320 static void paiext_start(struct perf_event *event, int flags)
321 {
322 	u64 sum;
323 
324 	if (event->hw.last_tag)
325 		return;
326 	event->hw.last_tag = 1;
327 	sum = paiext_getall(event);		/* Get current value */
328 	local64_set(&event->hw.prev_count, sum);
329 }
330 
331 static int paiext_add(struct perf_event *event, int flags)
332 {
333 	struct paiext_mapptr *mp = this_cpu_ptr(paiext_root.mapptr);
334 	struct paiext_map *cpump = mp->mapptr;
335 	struct paiext_cb *pcb = cpump->paiext_cb;
336 
337 	if (++cpump->active_events == 1) {
338 		S390_lowcore.aicd = virt_to_phys(cpump->paiext_cb);
339 		pcb->acc = virt_to_phys(cpump->area) | 0x1;
340 		/* Enable CPU instruction lookup for PAIE1 control block */
341 		local_ctl_set_bit(0, CR0_PAI_EXTENSION_BIT);
342 		debug_sprintf_event(paiext_dbg, 4, "%s 1508 %llx acc %llx\n",
343 				    __func__, S390_lowcore.aicd, pcb->acc);
344 	}
345 	if (flags & PERF_EF_START && !event->attr.sample_period) {
346 		/* Only counting needs initial counter value */
347 		paiext_start(event, PERF_EF_RELOAD);
348 	}
349 	event->hw.state = 0;
350 	if (event->attr.sample_period) {
351 		cpump->event = event;
352 		perf_sched_cb_inc(event->pmu);
353 	}
354 	return 0;
355 }
356 
357 static void paiext_stop(struct perf_event *event, int flags)
358 {
359 	paiext_read(event);
360 	event->hw.state = PERF_HES_STOPPED;
361 }
362 
363 static void paiext_del(struct perf_event *event, int flags)
364 {
365 	struct paiext_mapptr *mp = this_cpu_ptr(paiext_root.mapptr);
366 	struct paiext_map *cpump = mp->mapptr;
367 	struct paiext_cb *pcb = cpump->paiext_cb;
368 
369 	if (event->attr.sample_period)
370 		perf_sched_cb_dec(event->pmu);
371 	if (!event->attr.sample_period) {
372 		/* Only counting needs to read counter */
373 		paiext_stop(event, PERF_EF_UPDATE);
374 	}
375 	if (--cpump->active_events == 0) {
376 		/* Disable CPU instruction lookup for PAIE1 control block */
377 		local_ctl_clear_bit(0, CR0_PAI_EXTENSION_BIT);
378 		pcb->acc = 0;
379 		S390_lowcore.aicd = 0;
380 		debug_sprintf_event(paiext_dbg, 4, "%s 1508 %llx acc %llx\n",
381 				    __func__, S390_lowcore.aicd, pcb->acc);
382 	}
383 }
384 
385 /* Create raw data and save it in buffer. Returns number of bytes copied.
386  * Saves only positive counter entries of the form
387  * 2 bytes: Number of counter
388  * 8 bytes: Value of counter
389  */
390 static size_t paiext_copy(struct paiext_map *cpump)
391 {
392 	struct pai_userdata *userdata = cpump->save;
393 	int i, outidx = 0;
394 
395 	for (i = 1; i <= paiext_cnt; i++) {
396 		u64 val = paiext_getctr(cpump, i);
397 
398 		if (val) {
399 			userdata[outidx].num = i;
400 			userdata[outidx].value = val;
401 			outidx++;
402 		}
403 	}
404 	return outidx * sizeof(*userdata);
405 }
406 
407 /* Write sample when one or more counters values are nonzero.
408  *
409  * Note: The function paiext_sched_task() and paiext_push_sample() are not
410  * invoked after function paiext_del() has been called because of function
411  * perf_sched_cb_dec().
412  * The function paiext_sched_task() and paiext_push_sample() are only
413  * called when sampling is active. Function perf_sched_cb_inc()
414  * has been invoked to install function paiext_sched_task() as call back
415  * to run at context switch time (see paiext_add()).
416  *
417  * This causes function perf_event_context_sched_out() and
418  * perf_event_context_sched_in() to check whether the PMU has installed an
419  * sched_task() callback. That callback is not active after paiext_del()
420  * returns and has deleted the event on that CPU.
421  */
422 static int paiext_push_sample(void)
423 {
424 	struct paiext_mapptr *mp = this_cpu_ptr(paiext_root.mapptr);
425 	struct paiext_map *cpump = mp->mapptr;
426 	struct perf_event *event = cpump->event;
427 	struct perf_sample_data data;
428 	struct perf_raw_record raw;
429 	struct pt_regs regs;
430 	size_t rawsize;
431 	int overflow;
432 
433 	rawsize = paiext_copy(cpump);
434 	if (!rawsize)			/* No incremented counters */
435 		return 0;
436 
437 	/* Setup perf sample */
438 	memset(&regs, 0, sizeof(regs));
439 	memset(&raw, 0, sizeof(raw));
440 	memset(&data, 0, sizeof(data));
441 	perf_sample_data_init(&data, 0, event->hw.last_period);
442 	if (event->attr.sample_type & PERF_SAMPLE_TID) {
443 		data.tid_entry.pid = task_tgid_nr(current);
444 		data.tid_entry.tid = task_pid_nr(current);
445 	}
446 	if (event->attr.sample_type & PERF_SAMPLE_TIME)
447 		data.time = event->clock();
448 	if (event->attr.sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
449 		data.id = event->id;
450 	if (event->attr.sample_type & PERF_SAMPLE_CPU)
451 		data.cpu_entry.cpu = smp_processor_id();
452 	if (event->attr.sample_type & PERF_SAMPLE_RAW) {
453 		raw.frag.size = rawsize;
454 		raw.frag.data = cpump->save;
455 		perf_sample_save_raw_data(&data, &raw);
456 	}
457 
458 	overflow = perf_event_overflow(event, &data, &regs);
459 	perf_event_update_userpage(event);
460 	/* Clear lowcore area after read */
461 	memset(cpump->area, 0, PAIE1_CTRBLOCK_SZ);
462 	return overflow;
463 }
464 
465 /* Called on schedule-in and schedule-out. No access to event structure,
466  * but for sampling only event NNPA_ALL is allowed.
467  */
468 static void paiext_sched_task(struct perf_event_pmu_context *pmu_ctx, bool sched_in)
469 {
470 	/* We started with a clean page on event installation. So read out
471 	 * results on schedule_out and if page was dirty, clear values.
472 	 */
473 	if (!sched_in)
474 		paiext_push_sample();
475 }
476 
477 /* Attribute definitions for pai extension1 interface. As with other CPU
478  * Measurement Facilities, there is one attribute per mapped counter.
479  * The number of mapped counters may vary per machine generation. Use
480  * the QUERY PROCESSOR ACTIVITY COUNTER INFORMATION (QPACI) instruction
481  * to determine the number of mapped counters. The instructions returns
482  * a positive number, which is the highest number of supported counters.
483  * All counters less than this number are also supported, there are no
484  * holes. A returned number of zero means no support for mapped counters.
485  *
486  * The identification of the counter is a unique number. The chosen range
487  * is 0x1800 + offset in mapped kernel page.
488  * All CPU Measurement Facility counters identifiers must be unique and
489  * the numbers from 0 to 496 are already used for the CPU Measurement
490  * Counter facility. Number 0x1000 to 0x103e are used for PAI cryptography
491  * counters.
492  * Numbers 0xb0000, 0xbc000 and 0xbd000 are already
493  * used for the CPU Measurement Sampling facility.
494  */
495 PMU_FORMAT_ATTR(event, "config:0-63");
496 
497 static struct attribute *paiext_format_attr[] = {
498 	&format_attr_event.attr,
499 	NULL,
500 };
501 
502 static struct attribute_group paiext_events_group = {
503 	.name = "events",
504 	.attrs = NULL,			/* Filled in attr_event_init() */
505 };
506 
507 static struct attribute_group paiext_format_group = {
508 	.name = "format",
509 	.attrs = paiext_format_attr,
510 };
511 
512 static const struct attribute_group *paiext_attr_groups[] = {
513 	&paiext_events_group,
514 	&paiext_format_group,
515 	NULL,
516 };
517 
518 /* Performance monitoring unit for mapped counters */
519 static struct pmu paiext = {
520 	.task_ctx_nr  = perf_invalid_context,
521 	.event_init   = paiext_event_init,
522 	.add	      = paiext_add,
523 	.del	      = paiext_del,
524 	.start	      = paiext_start,
525 	.stop	      = paiext_stop,
526 	.read	      = paiext_read,
527 	.sched_task   = paiext_sched_task,
528 	.attr_groups  = paiext_attr_groups,
529 };
530 
531 /* List of symbolic PAI extension 1 NNPA counter names. */
532 static const char * const paiext_ctrnames[] = {
533 	[0] = "NNPA_ALL",
534 	[1] = "NNPA_ADD",
535 	[2] = "NNPA_SUB",
536 	[3] = "NNPA_MUL",
537 	[4] = "NNPA_DIV",
538 	[5] = "NNPA_MIN",
539 	[6] = "NNPA_MAX",
540 	[7] = "NNPA_LOG",
541 	[8] = "NNPA_EXP",
542 	[9] = "NNPA_IBM_RESERVED_9",
543 	[10] = "NNPA_RELU",
544 	[11] = "NNPA_TANH",
545 	[12] = "NNPA_SIGMOID",
546 	[13] = "NNPA_SOFTMAX",
547 	[14] = "NNPA_BATCHNORM",
548 	[15] = "NNPA_MAXPOOL2D",
549 	[16] = "NNPA_AVGPOOL2D",
550 	[17] = "NNPA_LSTMACT",
551 	[18] = "NNPA_GRUACT",
552 	[19] = "NNPA_CONVOLUTION",
553 	[20] = "NNPA_MATMUL_OP",
554 	[21] = "NNPA_MATMUL_OP_BCAST23",
555 	[22] = "NNPA_SMALLBATCH",
556 	[23] = "NNPA_LARGEDIM",
557 	[24] = "NNPA_SMALLTENSOR",
558 	[25] = "NNPA_1MFRAME",
559 	[26] = "NNPA_2GFRAME",
560 	[27] = "NNPA_ACCESSEXCEPT",
561 };
562 
563 static void __init attr_event_free(struct attribute **attrs, int num)
564 {
565 	struct perf_pmu_events_attr *pa;
566 	struct device_attribute *dap;
567 	int i;
568 
569 	for (i = 0; i < num; i++) {
570 		dap = container_of(attrs[i], struct device_attribute, attr);
571 		pa = container_of(dap, struct perf_pmu_events_attr, attr);
572 		kfree(pa);
573 	}
574 	kfree(attrs);
575 }
576 
577 static int __init attr_event_init_one(struct attribute **attrs, int num)
578 {
579 	struct perf_pmu_events_attr *pa;
580 
581 	pa = kzalloc(sizeof(*pa), GFP_KERNEL);
582 	if (!pa)
583 		return -ENOMEM;
584 
585 	sysfs_attr_init(&pa->attr.attr);
586 	pa->id = PAI_NNPA_BASE + num;
587 	pa->attr.attr.name = paiext_ctrnames[num];
588 	pa->attr.attr.mode = 0444;
589 	pa->attr.show = cpumf_events_sysfs_show;
590 	pa->attr.store = NULL;
591 	attrs[num] = &pa->attr.attr;
592 	return 0;
593 }
594 
595 /* Create PMU sysfs event attributes on the fly. */
596 static int __init attr_event_init(void)
597 {
598 	struct attribute **attrs;
599 	int ret, i;
600 
601 	attrs = kmalloc_array(ARRAY_SIZE(paiext_ctrnames) + 1, sizeof(*attrs),
602 			      GFP_KERNEL);
603 	if (!attrs)
604 		return -ENOMEM;
605 	for (i = 0; i < ARRAY_SIZE(paiext_ctrnames); i++) {
606 		ret = attr_event_init_one(attrs, i);
607 		if (ret) {
608 			attr_event_free(attrs, i - 1);
609 			return ret;
610 		}
611 	}
612 	attrs[i] = NULL;
613 	paiext_events_group.attrs = attrs;
614 	return 0;
615 }
616 
617 static int __init paiext_init(void)
618 {
619 	struct qpaci_info_block ib;
620 	int rc = -ENOMEM;
621 
622 	if (!test_facility(197))
623 		return 0;
624 
625 	qpaci(&ib);
626 	paiext_cnt = ib.num_nnpa;
627 	if (paiext_cnt >= PAI_NNPA_MAXCTR)
628 		paiext_cnt = PAI_NNPA_MAXCTR;
629 	if (!paiext_cnt)
630 		return 0;
631 
632 	rc = attr_event_init();
633 	if (rc) {
634 		pr_err("Creation of PMU " KMSG_COMPONENT " /sysfs failed\n");
635 		return rc;
636 	}
637 
638 	/* Setup s390dbf facility */
639 	paiext_dbg = debug_register(KMSG_COMPONENT, 2, 256, 128);
640 	if (!paiext_dbg) {
641 		pr_err("Registration of s390dbf " KMSG_COMPONENT " failed\n");
642 		rc = -ENOMEM;
643 		goto out_init;
644 	}
645 	debug_register_view(paiext_dbg, &debug_sprintf_view);
646 
647 	rc = perf_pmu_register(&paiext, KMSG_COMPONENT, -1);
648 	if (rc) {
649 		pr_err("Registration of " KMSG_COMPONENT " PMU failed with "
650 		       "rc=%i\n", rc);
651 		goto out_pmu;
652 	}
653 
654 	return 0;
655 
656 out_pmu:
657 	debug_unregister_view(paiext_dbg, &debug_sprintf_view);
658 	debug_unregister(paiext_dbg);
659 out_init:
660 	attr_event_free(paiext_events_group.attrs,
661 			ARRAY_SIZE(paiext_ctrnames) + 1);
662 	return rc;
663 }
664 
665 device_initcall(paiext_init);
666