]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/s390/kernel/perf_cpum_cf.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / arch / s390 / kernel / perf_cpum_cf.c
CommitLineData
a17ae4c3 1// SPDX-License-Identifier: GPL-2.0
212188a5
HB
2/*
3 * Performance event support for s390x - CPU-measurement Counter Facility
4 *
10ee7bff
TR
5 * Copyright IBM Corp. 2012, 2019
6 * Author(s): Hendrik Brueckner <brueckner@linux.ibm.com>
212188a5
HB
7 */
8#define KMSG_COMPONENT "cpum_cf"
9#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10
11#include <linux/kernel.h>
12#include <linux/kernel_stat.h>
13#include <linux/perf_event.h>
14#include <linux/percpu.h>
15#include <linux/notifier.h>
16#include <linux/init.h>
17#include <linux/export.h>
1e3cab2f 18#include <asm/ctl_reg.h>
212188a5
HB
19#include <asm/irq.h>
20#include <asm/cpu_mf.h>
21
212188a5 22enum cpumf_ctr_set {
ee699f32
HB
23 CPUMF_CTR_SET_BASIC = 0, /* Basic Counter Set */
24 CPUMF_CTR_SET_USER = 1, /* Problem-State Counter Set */
25 CPUMF_CTR_SET_CRYPTO = 2, /* Crypto-Activity Counter Set */
26 CPUMF_CTR_SET_EXT = 3, /* Extended Counter Set */
27 CPUMF_CTR_SET_MT_DIAG = 4, /* MT-diagnostic Counter Set */
212188a5
HB
28
29 /* Maximum number of counter sets */
30 CPUMF_CTR_SET_MAX,
31};
32
33#define CPUMF_LCCTL_ENABLE_SHIFT 16
34#define CPUMF_LCCTL_ACTCTL_SHIFT 0
35static const u64 cpumf_state_ctl[CPUMF_CTR_SET_MAX] = {
36 [CPUMF_CTR_SET_BASIC] = 0x02,
37 [CPUMF_CTR_SET_USER] = 0x04,
38 [CPUMF_CTR_SET_CRYPTO] = 0x08,
39 [CPUMF_CTR_SET_EXT] = 0x01,
ee699f32 40 [CPUMF_CTR_SET_MT_DIAG] = 0x20,
212188a5
HB
41};
42
43static void ctr_set_enable(u64 *state, int ctr_set)
44{
45 *state |= cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ENABLE_SHIFT;
46}
47static void ctr_set_disable(u64 *state, int ctr_set)
48{
49 *state &= ~(cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ENABLE_SHIFT);
50}
51static void ctr_set_start(u64 *state, int ctr_set)
52{
53 *state |= cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ACTCTL_SHIFT;
54}
55static void ctr_set_stop(u64 *state, int ctr_set)
56{
57 *state &= ~(cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ACTCTL_SHIFT);
58}
59
60/* Local CPUMF event structure */
61struct cpu_hw_events {
62 struct cpumf_ctr_info info;
63 atomic_t ctr_set[CPUMF_CTR_SET_MAX];
64 u64 state, tx_state;
65 unsigned int flags;
fbbe0701 66 unsigned int txn_flags;
212188a5
HB
67};
68static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
69 .ctr_set = {
ee699f32
HB
70 [CPUMF_CTR_SET_BASIC] = ATOMIC_INIT(0),
71 [CPUMF_CTR_SET_USER] = ATOMIC_INIT(0),
72 [CPUMF_CTR_SET_CRYPTO] = ATOMIC_INIT(0),
73 [CPUMF_CTR_SET_EXT] = ATOMIC_INIT(0),
74 [CPUMF_CTR_SET_MT_DIAG] = ATOMIC_INIT(0),
212188a5
HB
75 },
76 .state = 0,
77 .flags = 0,
fbbe0701 78 .txn_flags = 0,
212188a5
HB
79};
80
ee699f32 81static enum cpumf_ctr_set get_counter_set(u64 event)
212188a5 82{
ee699f32 83 int set = CPUMF_CTR_SET_MAX;
212188a5
HB
84
85 if (event < 32)
86 set = CPUMF_CTR_SET_BASIC;
87 else if (event < 64)
88 set = CPUMF_CTR_SET_USER;
89 else if (event < 128)
90 set = CPUMF_CTR_SET_CRYPTO;
10ee7bff 91 else if (event < 288)
212188a5 92 set = CPUMF_CTR_SET_EXT;
ee699f32
HB
93 else if (event >= 448 && event < 496)
94 set = CPUMF_CTR_SET_MT_DIAG;
212188a5
HB
95
96 return set;
97}
98
212188a5
HB
99static int validate_ctr_version(const struct hw_perf_event *hwc)
100{
101 struct cpu_hw_events *cpuhw;
102 int err = 0;
ee699f32 103 u16 mtdiag_ctl;
212188a5
HB
104
105 cpuhw = &get_cpu_var(cpu_hw_events);
106
107 /* check required version for counter sets */
108 switch (hwc->config_base) {
109 case CPUMF_CTR_SET_BASIC:
110 case CPUMF_CTR_SET_USER:
111 if (cpuhw->info.cfvn < 1)
112 err = -EOPNOTSUPP;
113 break;
114 case CPUMF_CTR_SET_CRYPTO:
10ee7bff
TR
115 if ((cpuhw->info.csvn >= 1 && cpuhw->info.csvn <= 5 &&
116 hwc->config > 79) ||
117 (cpuhw->info.csvn >= 6 && hwc->config > 83))
118 err = -EOPNOTSUPP;
119 break;
212188a5
HB
120 case CPUMF_CTR_SET_EXT:
121 if (cpuhw->info.csvn < 1)
122 err = -EOPNOTSUPP;
f47586b2
HB
123 if ((cpuhw->info.csvn == 1 && hwc->config > 159) ||
124 (cpuhw->info.csvn == 2 && hwc->config > 175) ||
10ee7bff
TR
125 (cpuhw->info.csvn >= 3 && cpuhw->info.csvn <= 5
126 && hwc->config > 255) ||
127 (cpuhw->info.csvn >= 6 && hwc->config > 287))
f47586b2 128 err = -EOPNOTSUPP;
212188a5 129 break;
ee699f32
HB
130 case CPUMF_CTR_SET_MT_DIAG:
131 if (cpuhw->info.csvn <= 3)
132 err = -EOPNOTSUPP;
133 /*
134 * MT-diagnostic counters are read-only. The counter set
135 * is automatically enabled and activated on all CPUs with
136 * multithreading (SMT). Deactivation of multithreading
137 * also disables the counter set. State changes are ignored
138 * by lcctl(). Because Linux controls SMT enablement through
139 * a kernel parameter only, the counter set is either disabled
140 * or enabled and active.
141 *
142 * Thus, the counters can only be used if SMT is on and the
143 * counter set is enabled and active.
144 */
145 mtdiag_ctl = cpumf_state_ctl[CPUMF_CTR_SET_MT_DIAG];
146 if (!((cpuhw->info.auth_ctl & mtdiag_ctl) &&
147 (cpuhw->info.enable_ctl & mtdiag_ctl) &&
148 (cpuhw->info.act_ctl & mtdiag_ctl)))
149 err = -EOPNOTSUPP;
150 break;
212188a5
HB
151 }
152
153 put_cpu_var(cpu_hw_events);
154 return err;
155}
156
157static int validate_ctr_auth(const struct hw_perf_event *hwc)
158{
159 struct cpu_hw_events *cpuhw;
160 u64 ctrs_state;
161 int err = 0;
162
163 cpuhw = &get_cpu_var(cpu_hw_events);
164
58f8e9da
HB
165 /* Check authorization for cpu counter sets.
166 * If the particular CPU counter set is not authorized,
167 * return with -ENOENT in order to fall back to other
168 * PMUs that might suffice the event request.
169 */
212188a5
HB
170 ctrs_state = cpumf_state_ctl[hwc->config_base];
171 if (!(ctrs_state & cpuhw->info.auth_ctl))
58f8e9da 172 err = -ENOENT;
212188a5
HB
173
174 put_cpu_var(cpu_hw_events);
175 return err;
176}
177
178/*
179 * Change the CPUMF state to active.
180 * Enable and activate the CPU-counter sets according
181 * to the per-cpu control state.
182 */
183static void cpumf_pmu_enable(struct pmu *pmu)
184{
eb7e7d76 185 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
212188a5
HB
186 int err;
187
188 if (cpuhw->flags & PMU_F_ENABLED)
189 return;
190
191 err = lcctl(cpuhw->state);
192 if (err) {
193 pr_err("Enabling the performance measuring unit "
af0ee94e 194 "failed with rc=%x\n", err);
212188a5
HB
195 return;
196 }
197
198 cpuhw->flags |= PMU_F_ENABLED;
199}
200
201/*
202 * Change the CPUMF state to inactive.
203 * Disable and enable (inactive) the CPU-counter sets according
204 * to the per-cpu control state.
205 */
206static void cpumf_pmu_disable(struct pmu *pmu)
207{
eb7e7d76 208 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
212188a5
HB
209 int err;
210 u64 inactive;
211
212 if (!(cpuhw->flags & PMU_F_ENABLED))
213 return;
214
215 inactive = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1);
216 err = lcctl(inactive);
217 if (err) {
218 pr_err("Disabling the performance measuring unit "
af0ee94e 219 "failed with rc=%x\n", err);
212188a5
HB
220 return;
221 }
222
223 cpuhw->flags &= ~PMU_F_ENABLED;
224}
225
226
227/* Number of perf events counting hardware events */
228static atomic_t num_events = ATOMIC_INIT(0);
229/* Used to avoid races in calling reserve/release_cpumf_hardware */
230static DEFINE_MUTEX(pmc_reserve_mutex);
231
232/* CPU-measurement alerts for the counter facility */
233static void cpumf_measurement_alert(struct ext_code ext_code,
234 unsigned int alert, unsigned long unused)
235{
236 struct cpu_hw_events *cpuhw;
237
238 if (!(alert & CPU_MF_INT_CF_MASK))
239 return;
240
420f42ec 241 inc_irq_stat(IRQEXT_CMC);
eb7e7d76 242 cpuhw = this_cpu_ptr(&cpu_hw_events);
212188a5
HB
243
244 /* Measurement alerts are shared and might happen when the PMU
245 * is not reserved. Ignore these alerts in this case. */
246 if (!(cpuhw->flags & PMU_F_RESERVED))
247 return;
248
249 /* counter authorization change alert */
250 if (alert & CPU_MF_INT_CF_CACA)
251 qctri(&cpuhw->info);
252
253 /* loss of counter data alert */
254 if (alert & CPU_MF_INT_CF_LCDA)
255 pr_err("CPU[%i] Counter data was lost\n", smp_processor_id());
ee699f32
HB
256
257 /* loss of MT counter data alert */
258 if (alert & CPU_MF_INT_CF_MTDA)
259 pr_warn("CPU[%i] MT counter data was lost\n",
260 smp_processor_id());
212188a5
HB
261}
262
263#define PMC_INIT 0
264#define PMC_RELEASE 1
265static void setup_pmc_cpu(void *flags)
266{
eb7e7d76 267 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
212188a5
HB
268
269 switch (*((int *) flags)) {
270 case PMC_INIT:
271 memset(&cpuhw->info, 0, sizeof(cpuhw->info));
272 qctri(&cpuhw->info);
273 cpuhw->flags |= PMU_F_RESERVED;
274 break;
275
276 case PMC_RELEASE:
277 cpuhw->flags &= ~PMU_F_RESERVED;
278 break;
279 }
280
281 /* Disable CPU counter sets */
282 lcctl(0);
283}
284
285/* Initialize the CPU-measurement facility */
286static int reserve_pmc_hardware(void)
287{
288 int flags = PMC_INIT;
289
290 on_each_cpu(setup_pmc_cpu, &flags, 1);
82003c3e 291 irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT);
212188a5
HB
292
293 return 0;
294}
295
296/* Release the CPU-measurement facility */
297static void release_pmc_hardware(void)
298{
299 int flags = PMC_RELEASE;
300
301 on_each_cpu(setup_pmc_cpu, &flags, 1);
82003c3e 302 irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT);
212188a5
HB
303}
304
305/* Release the PMU if event is the last perf event */
306static void hw_perf_event_destroy(struct perf_event *event)
307{
308 if (!atomic_add_unless(&num_events, -1, 1)) {
309 mutex_lock(&pmc_reserve_mutex);
310 if (atomic_dec_return(&num_events) == 0)
311 release_pmc_hardware();
312 mutex_unlock(&pmc_reserve_mutex);
313 }
314}
315
316/* CPUMF <-> perf event mappings for kernel+userspace (basic set) */
317static const int cpumf_generic_events_basic[] = {
318 [PERF_COUNT_HW_CPU_CYCLES] = 0,
319 [PERF_COUNT_HW_INSTRUCTIONS] = 1,
320 [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
321 [PERF_COUNT_HW_CACHE_MISSES] = -1,
322 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
323 [PERF_COUNT_HW_BRANCH_MISSES] = -1,
324 [PERF_COUNT_HW_BUS_CYCLES] = -1,
325};
326/* CPUMF <-> perf event mappings for userspace (problem-state set) */
327static const int cpumf_generic_events_user[] = {
328 [PERF_COUNT_HW_CPU_CYCLES] = 32,
329 [PERF_COUNT_HW_INSTRUCTIONS] = 33,
330 [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
331 [PERF_COUNT_HW_CACHE_MISSES] = -1,
332 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
333 [PERF_COUNT_HW_BRANCH_MISSES] = -1,
334 [PERF_COUNT_HW_BUS_CYCLES] = -1,
335};
336
337static int __hw_perf_event_init(struct perf_event *event)
338{
339 struct perf_event_attr *attr = &event->attr;
340 struct hw_perf_event *hwc = &event->hw;
ee699f32 341 enum cpumf_ctr_set set;
212188a5
HB
342 int err;
343 u64 ev;
344
345 switch (attr->type) {
346 case PERF_TYPE_RAW:
347 /* Raw events are used to access counters directly,
348 * hence do not permit excludes */
349 if (attr->exclude_kernel || attr->exclude_user ||
350 attr->exclude_hv)
351 return -EOPNOTSUPP;
352 ev = attr->config;
353 break;
354
355 case PERF_TYPE_HARDWARE:
b0ad0a95
TR
356 if (is_sampling_event(event)) /* No sampling support */
357 return -ENOENT;
212188a5
HB
358 ev = attr->config;
359 /* Count user space (problem-state) only */
360 if (!attr->exclude_user && attr->exclude_kernel) {
361 if (ev >= ARRAY_SIZE(cpumf_generic_events_user))
362 return -EOPNOTSUPP;
363 ev = cpumf_generic_events_user[ev];
364
365 /* No support for kernel space counters only */
366 } else if (!attr->exclude_kernel && attr->exclude_user) {
367 return -EOPNOTSUPP;
368
369 /* Count user and kernel space */
370 } else {
371 if (ev >= ARRAY_SIZE(cpumf_generic_events_basic))
372 return -EOPNOTSUPP;
373 ev = cpumf_generic_events_basic[ev];
374 }
375 break;
376
377 default:
378 return -ENOENT;
379 }
380
381 if (ev == -1)
382 return -ENOENT;
383
20ba46da 384 if (ev > PERF_CPUM_CF_MAX_CTR)
e0bbb55f 385 return -ENOENT;
212188a5 386
ee699f32
HB
387 /* Obtain the counter set to which the specified counter belongs */
388 set = get_counter_set(ev);
389 switch (set) {
390 case CPUMF_CTR_SET_BASIC:
391 case CPUMF_CTR_SET_USER:
392 case CPUMF_CTR_SET_CRYPTO:
393 case CPUMF_CTR_SET_EXT:
394 case CPUMF_CTR_SET_MT_DIAG:
395 /*
396 * Use the hardware perf event structure to store the
397 * counter number in the 'config' member and the counter
398 * set number in the 'config_base'. The counter set number
399 * is then later used to enable/disable the counter(s).
400 */
401 hwc->config = ev;
402 hwc->config_base = set;
403 break;
404 case CPUMF_CTR_SET_MAX:
405 /* The counter could not be associated to a counter set */
406 return -EINVAL;
407 };
212188a5 408
212188a5
HB
409 /* Initialize for using the CPU-measurement counter facility */
410 if (!atomic_inc_not_zero(&num_events)) {
411 mutex_lock(&pmc_reserve_mutex);
412 if (atomic_read(&num_events) == 0 && reserve_pmc_hardware())
413 err = -EBUSY;
414 else
415 atomic_inc(&num_events);
416 mutex_unlock(&pmc_reserve_mutex);
417 }
418 event->destroy = hw_perf_event_destroy;
419
420 /* Finally, validate version and authorization of the counter set */
421 err = validate_ctr_auth(hwc);
422 if (!err)
423 err = validate_ctr_version(hwc);
424
425 return err;
426}
427
428static int cpumf_pmu_event_init(struct perf_event *event)
429{
430 int err;
431
432 switch (event->attr.type) {
433 case PERF_TYPE_HARDWARE:
434 case PERF_TYPE_HW_CACHE:
435 case PERF_TYPE_RAW:
436 err = __hw_perf_event_init(event);
437 break;
438 default:
439 return -ENOENT;
440 }
441
442 if (unlikely(err) && event->destroy)
443 event->destroy(event);
444
445 return err;
446}
447
448static int hw_perf_event_reset(struct perf_event *event)
449{
450 u64 prev, new;
451 int err;
452
453 do {
454 prev = local64_read(&event->hw.prev_count);
455 err = ecctr(event->hw.config, &new);
456 if (err) {
457 if (err != 3)
458 break;
459 /* The counter is not (yet) available. This
460 * might happen if the counter set to which
461 * this counter belongs is in the disabled
462 * state.
463 */
464 new = 0;
465 }
466 } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
467
468 return err;
469}
470
485527ba 471static void hw_perf_event_update(struct perf_event *event)
212188a5
HB
472{
473 u64 prev, new, delta;
474 int err;
475
476 do {
477 prev = local64_read(&event->hw.prev_count);
478 err = ecctr(event->hw.config, &new);
479 if (err)
485527ba 480 return;
212188a5
HB
481 } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
482
483 delta = (prev <= new) ? new - prev
484 : (-1ULL - prev) + new + 1; /* overflow */
485 local64_add(delta, &event->count);
212188a5
HB
486}
487
488static void cpumf_pmu_read(struct perf_event *event)
489{
490 if (event->hw.state & PERF_HES_STOPPED)
491 return;
492
493 hw_perf_event_update(event);
494}
495
496static void cpumf_pmu_start(struct perf_event *event, int flags)
497{
eb7e7d76 498 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
212188a5
HB
499 struct hw_perf_event *hwc = &event->hw;
500
501 if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
502 return;
503
504 if (WARN_ON_ONCE(hwc->config == -1))
505 return;
506
507 if (flags & PERF_EF_RELOAD)
508 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
509
510 hwc->state = 0;
511
512 /* (Re-)enable and activate the counter set */
513 ctr_set_enable(&cpuhw->state, hwc->config_base);
514 ctr_set_start(&cpuhw->state, hwc->config_base);
515
516 /* The counter set to which this counter belongs can be already active.
517 * Because all counters in a set are active, the event->hw.prev_count
518 * needs to be synchronized. At this point, the counter set can be in
519 * the inactive or disabled state.
520 */
521 hw_perf_event_reset(event);
522
523 /* increment refcount for this counter set */
524 atomic_inc(&cpuhw->ctr_set[hwc->config_base]);
525}
526
527static void cpumf_pmu_stop(struct perf_event *event, int flags)
528{
eb7e7d76 529 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
212188a5
HB
530 struct hw_perf_event *hwc = &event->hw;
531
532 if (!(hwc->state & PERF_HES_STOPPED)) {
533 /* Decrement reference count for this counter set and if this
534 * is the last used counter in the set, clear activation
535 * control and set the counter set state to inactive.
536 */
537 if (!atomic_dec_return(&cpuhw->ctr_set[hwc->config_base]))
538 ctr_set_stop(&cpuhw->state, hwc->config_base);
539 event->hw.state |= PERF_HES_STOPPED;
540 }
541
542 if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
543 hw_perf_event_update(event);
544 event->hw.state |= PERF_HES_UPTODATE;
545 }
546}
547
548static int cpumf_pmu_add(struct perf_event *event, int flags)
549{
eb7e7d76 550 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
212188a5
HB
551
552 /* Check authorization for the counter set to which this
553 * counter belongs.
554 * For group events transaction, the authorization check is
555 * done in cpumf_pmu_commit_txn().
556 */
8f3e5684 557 if (!(cpuhw->txn_flags & PERF_PMU_TXN_ADD))
212188a5 558 if (validate_ctr_auth(&event->hw))
58f8e9da 559 return -ENOENT;
212188a5
HB
560
561 ctr_set_enable(&cpuhw->state, event->hw.config_base);
562 event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
563
564 if (flags & PERF_EF_START)
565 cpumf_pmu_start(event, PERF_EF_RELOAD);
566
567 perf_event_update_userpage(event);
568
569 return 0;
570}
571
572static void cpumf_pmu_del(struct perf_event *event, int flags)
573{
eb7e7d76 574 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
212188a5
HB
575
576 cpumf_pmu_stop(event, PERF_EF_UPDATE);
577
578 /* Check if any counter in the counter set is still used. If not used,
579 * change the counter set to the disabled state. This also clears the
580 * content of all counters in the set.
581 *
582 * When a new perf event has been added but not yet started, this can
583 * clear enable control and resets all counters in a set. Therefore,
584 * cpumf_pmu_start() always has to reenable a counter set.
585 */
586 if (!atomic_read(&cpuhw->ctr_set[event->hw.config_base]))
587 ctr_set_disable(&cpuhw->state, event->hw.config_base);
588
589 perf_event_update_userpage(event);
590}
591
592/*
593 * Start group events scheduling transaction.
594 * Set flags to perform a single test at commit time.
fbbe0701
SB
595 *
596 * We only support PERF_PMU_TXN_ADD transactions. Save the
597 * transaction flags but otherwise ignore non-PERF_PMU_TXN_ADD
598 * transactions.
212188a5 599 */
fbbe0701 600static void cpumf_pmu_start_txn(struct pmu *pmu, unsigned int txn_flags)
212188a5 601{
eb7e7d76 602 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
212188a5 603
fbbe0701
SB
604 WARN_ON_ONCE(cpuhw->txn_flags); /* txn already in flight */
605
606 cpuhw->txn_flags = txn_flags;
607 if (txn_flags & ~PERF_PMU_TXN_ADD)
608 return;
609
212188a5 610 perf_pmu_disable(pmu);
212188a5
HB
611 cpuhw->tx_state = cpuhw->state;
612}
613
614/*
615 * Stop and cancel a group events scheduling tranctions.
616 * Assumes cpumf_pmu_del() is called for each successful added
617 * cpumf_pmu_add() during the transaction.
618 */
619static void cpumf_pmu_cancel_txn(struct pmu *pmu)
620{
fbbe0701 621 unsigned int txn_flags;
eb7e7d76 622 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
212188a5 623
fbbe0701
SB
624 WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
625
626 txn_flags = cpuhw->txn_flags;
627 cpuhw->txn_flags = 0;
628 if (txn_flags & ~PERF_PMU_TXN_ADD)
629 return;
630
212188a5
HB
631 WARN_ON(cpuhw->tx_state != cpuhw->state);
632
212188a5
HB
633 perf_pmu_enable(pmu);
634}
635
636/*
637 * Commit the group events scheduling transaction. On success, the
638 * transaction is closed. On error, the transaction is kept open
639 * until cpumf_pmu_cancel_txn() is called.
640 */
641static int cpumf_pmu_commit_txn(struct pmu *pmu)
642{
eb7e7d76 643 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
212188a5
HB
644 u64 state;
645
fbbe0701
SB
646 WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
647
648 if (cpuhw->txn_flags & ~PERF_PMU_TXN_ADD) {
649 cpuhw->txn_flags = 0;
650 return 0;
651 }
652
212188a5
HB
653 /* check if the updated state can be scheduled */
654 state = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1);
655 state >>= CPUMF_LCCTL_ENABLE_SHIFT;
656 if ((state & cpuhw->info.auth_ctl) != state)
58f8e9da 657 return -ENOENT;
212188a5 658
fbbe0701 659 cpuhw->txn_flags = 0;
212188a5
HB
660 perf_pmu_enable(pmu);
661 return 0;
662}
663
664/* Performance monitoring unit for s390x */
665static struct pmu cpumf_pmu = {
9254e70c
HB
666 .task_ctx_nr = perf_sw_context,
667 .capabilities = PERF_PMU_CAP_NO_INTERRUPT,
212188a5
HB
668 .pmu_enable = cpumf_pmu_enable,
669 .pmu_disable = cpumf_pmu_disable,
670 .event_init = cpumf_pmu_event_init,
671 .add = cpumf_pmu_add,
672 .del = cpumf_pmu_del,
673 .start = cpumf_pmu_start,
674 .stop = cpumf_pmu_stop,
675 .read = cpumf_pmu_read,
676 .start_txn = cpumf_pmu_start_txn,
677 .commit_txn = cpumf_pmu_commit_txn,
678 .cancel_txn = cpumf_pmu_cancel_txn,
679};
680
4f0f8217 681static int cpumf_pmf_setup(unsigned int cpu, int flags)
212188a5 682{
4f0f8217
TG
683 local_irq_disable();
684 setup_pmc_cpu(&flags);
685 local_irq_enable();
686 return 0;
687}
688
689static int s390_pmu_online_cpu(unsigned int cpu)
690{
691 return cpumf_pmf_setup(cpu, PMC_INIT);
692}
212188a5 693
4f0f8217
TG
694static int s390_pmu_offline_cpu(unsigned int cpu)
695{
696 return cpumf_pmf_setup(cpu, PMC_RELEASE);
212188a5
HB
697}
698
699static int __init cpumf_pmu_init(void)
700{
701 int rc;
702
703 if (!cpum_cf_avail())
704 return -ENODEV;
705
706 /* clear bit 15 of cr0 to unauthorize problem-state to
707 * extract measurement counters */
708 ctl_clear_bit(0, 48);
709
710 /* register handler for measurement-alert interruptions */
1dad093b
TH
711 rc = register_external_irq(EXT_IRQ_MEASURE_ALERT,
712 cpumf_measurement_alert);
212188a5
HB
713 if (rc) {
714 pr_err("Registering for CPU-measurement alerts "
715 "failed with rc=%i\n", rc);
4f0f8217 716 return rc;
212188a5
HB
717 }
718
c7168325 719 cpumf_pmu.attr_groups = cpumf_cf_event_group();
212188a5
HB
720 rc = perf_pmu_register(&cpumf_pmu, "cpum_cf", PERF_TYPE_RAW);
721 if (rc) {
722 pr_err("Registering the cpum_cf PMU failed with rc=%i\n", rc);
1dad093b
TH
723 unregister_external_irq(EXT_IRQ_MEASURE_ALERT,
724 cpumf_measurement_alert);
4f0f8217 725 return rc;
212188a5 726 }
4f0f8217 727 return cpuhp_setup_state(CPUHP_AP_PERF_S390_CF_ONLINE,
73c1b41e 728 "perf/s390/cf:online",
4f0f8217 729 s390_pmu_online_cpu, s390_pmu_offline_cpu);
212188a5
HB
730}
731early_initcall(cpumf_pmu_init);