aboutsummaryrefslogtreecommitdiffstats
path: root/arch
diff options
context:
space:
mode:
authorHendrik Brueckner <brueckner@linux.vnet.ibm.com>2012-03-23 06:13:06 -0400
committerMartin Schwidefsky <schwidefsky@de.ibm.com>2012-03-23 06:13:25 -0400
commit212188a596d17d519842ef2173150315735b54e1 (patch)
tree22f4f391fc9979a231028e45fd11b8a6a6c99232 /arch
parentb03d541aa45b52e1b723890121a9fe3920eb438b (diff)
[S390] perf: add support for s390x CPU counters
Add a perf PMU to access the CPU-measurement counter facility CPUM CF. CPUM CF provides multiple counter sets for measuring generic, problem-state, and crypto activaties. Also an extended counter set for the IBM System z10 and IBM z196 mainframes is available. Counters from the basic and problem-state counter set are mapped to generic perf hardware events. Other counters are accessible through raw events. For a list of available counter sets and counters, see: - The Load-Program-Parameter and the CPU-Measurement Facilities (SA23-2260) - The CPU-Measurement Facility Extended Counters Definition for z10 and z196 (SA23-2261) Reviewed-by: Jan Glauber <jang@linux.vnet.ibm.com> Signed-off-by: Hendrik Brueckner <brueckner@linux.vnet.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
Diffstat (limited to 'arch')
-rw-r--r--arch/s390/include/asm/cpu_mf.h89
-rw-r--r--arch/s390/include/asm/perf_event.h12
-rw-r--r--arch/s390/kernel/Makefile1
-rw-r--r--arch/s390/kernel/perf_cpum_cf.c690
-rw-r--r--arch/s390/kernel/perf_event.c125
5 files changed, 912 insertions, 5 deletions
diff --git a/arch/s390/include/asm/cpu_mf.h b/arch/s390/include/asm/cpu_mf.h
index d91dd38112de..e49db5d5d06f 100644
--- a/arch/s390/include/asm/cpu_mf.h
+++ b/arch/s390/include/asm/cpu_mf.h
@@ -1,12 +1,95 @@
1/*
2 * CPU-measurement facilities
3 *
4 * Copyright IBM Corp. 2012
5 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
6 * Jan Glauber <jang@linux.vnet.ibm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License (version 2 only)
10 * as published by the Free Software Foundation.
11 */
1#ifndef _ASM_S390_CPU_MF_H 12#ifndef _ASM_S390_CPU_MF_H
2#define _ASM_S390_CPU_MF_H 13#define _ASM_S390_CPU_MF_H
3 14
4#define CPU_MF_INT_SF_MASK 0xffc00000
5
6#define CPU_MF_INT_SF_IAE (1 << 31) /* invalid entry address */ 15#define CPU_MF_INT_SF_IAE (1 << 31) /* invalid entry address */
7#define CPU_MF_INT_SF_ISE (1 << 30) /* incorrect SDBT entry */ 16#define CPU_MF_INT_SF_ISE (1 << 30) /* incorrect SDBT entry */
8#define CPU_MF_INT_SF_PRA (1 << 29) /* program request alert */ 17#define CPU_MF_INT_SF_PRA (1 << 29) /* program request alert */
9#define CPU_MF_INT_SF_SACA (1 << 23) /* sampler auth. change alert */ 18#define CPU_MF_INT_SF_SACA (1 << 23) /* sampler auth. change alert */
10#define CPU_MF_INT_SF_LSDA (1 << 22) /* loss of sample data alert */ 19#define CPU_MF_INT_SF_LSDA (1 << 22) /* loss of sample data alert */
20#define CPU_MF_INT_CF_CACA (1 << 7) /* counter auth. change alert */
21#define CPU_MF_INT_CF_LCDA (1 << 6) /* loss of counter data alert */
22
23#define CPU_MF_INT_CF_MASK (CPU_MF_INT_CF_CACA|CPU_MF_INT_CF_LCDA)
24#define CPU_MF_INT_SF_MASK (CPU_MF_INT_SF_IAE|CPU_MF_INT_SF_ISE| \
25 CPU_MF_INT_SF_PRA|CPU_MF_INT_SF_SACA| \
26 CPU_MF_INT_SF_LSDA)
27
28/* CPU measurement facility support */
29static inline int cpum_cf_avail(void)
30{
31 return MACHINE_HAS_SPP && test_facility(67);
32}
33
34static inline int cpum_sf_avail(void)
35{
36 return MACHINE_HAS_SPP && test_facility(68);
37}
38
39
40struct cpumf_ctr_info {
41 u16 cfvn;
42 u16 auth_ctl;
43 u16 enable_ctl;
44 u16 act_ctl;
45 u16 max_cpu;
46 u16 csvn;
47 u16 max_cg;
48 u16 reserved1;
49 u32 reserved2[12];
50} __packed;
51
52/* Query counter information */
53static inline int qctri(struct cpumf_ctr_info *info)
54{
55 int rc = -EINVAL;
56
57 asm volatile (
58 "0: .insn s,0xb28e0000,%1\n"
59 "1: lhi %0,0\n"
60 "2:\n"
61 EX_TABLE(1b, 2b)
62 : "+d" (rc), "=Q" (*info));
63 return rc;
64}
65
66/* Load CPU-counter-set controls */
67static inline int lcctl(u64 ctl)
68{
69 int cc;
70
71 asm volatile (
72 " .insn s,0xb2840000,%1\n"
73 " ipm %0\n"
74 " srl %0,28\n"
75 : "=d" (cc) : "m" (ctl) : "cc");
76 return cc;
77}
78
79/* Extract CPU counter */
80static inline int ecctr(u64 ctr, u64 *val)
81{
82 register u64 content asm("4") = 0;
83 int cc;
84
85 asm volatile (
86 " .insn rre,0xb2e40000,%0,%2\n"
87 " ipm %1\n"
88 " srl %1,28\n"
89 : "=d" (content), "=d" (cc) : "d" (ctr) : "cc");
90 if (!cc)
91 *val = content;
92 return cc;
93}
11 94
12#endif 95#endif /* _ASM_S390_CPU_MF_H */
diff --git a/arch/s390/include/asm/perf_event.h b/arch/s390/include/asm/perf_event.h
index 4eb444edbe49..7941968e12b4 100644
--- a/arch/s390/include/asm/perf_event.h
+++ b/arch/s390/include/asm/perf_event.h
@@ -1,8 +1,16 @@
1/* 1/*
2 * Performance event support - s390 specific definitions. 2 * Performance event support - s390 specific definitions.
3 * 3 *
4 * Copyright 2009 Martin Schwidefsky, IBM Corporation. 4 * Copyright IBM Corp. 2009, 2012
5 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
5 */ 7 */
6 8
7/* Empty, just to avoid compiling error */ 9#include <asm/cpu_mf.h>
8 10
11/* CPU-measurement counter facility */
12#define PERF_CPUM_CF_MAX_CTR 160
13
14/* Per-CPU flags for PMU states */
15#define PMU_F_RESERVED 0x1000
16#define PMU_F_ENABLED 0x2000
diff --git a/arch/s390/kernel/Makefile b/arch/s390/kernel/Makefile
index 16b0b433f1f4..884b18afc864 100644
--- a/arch/s390/kernel/Makefile
+++ b/arch/s390/kernel/Makefile
@@ -48,6 +48,7 @@ obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o
48obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o 48obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
49obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o 49obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o
50obj-$(CONFIG_CRASH_DUMP) += crash_dump.o 50obj-$(CONFIG_CRASH_DUMP) += crash_dump.o
51obj-$(CONFIG_PERF_EVENTS) += perf_event.o perf_cpum_cf.o
51 52
52# Kexec part 53# Kexec part
53S390_KEXEC_OBJS := machine_kexec.o crash.o 54S390_KEXEC_OBJS := machine_kexec.o crash.o
diff --git a/arch/s390/kernel/perf_cpum_cf.c b/arch/s390/kernel/perf_cpum_cf.c
new file mode 100644
index 000000000000..8481ecf2ad71
--- /dev/null
+++ b/arch/s390/kernel/perf_cpum_cf.c
@@ -0,0 +1,690 @@
1/*
2 * Performance event support for s390x - CPU-measurement Counter Facility
3 *
4 * Copyright IBM Corp. 2012
5 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License (version 2 only)
9 * as published by the Free Software Foundation.
10 */
11#define KMSG_COMPONENT "cpum_cf"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
14#include <linux/kernel.h>
15#include <linux/kernel_stat.h>
16#include <linux/perf_event.h>
17#include <linux/percpu.h>
18#include <linux/notifier.h>
19#include <linux/init.h>
20#include <linux/export.h>
21#include <asm/system.h>
22#include <asm/irq.h>
23#include <asm/cpu_mf.h>
24
25/* CPU-measurement counter facility supports these CPU counter sets:
26 * For CPU counter sets:
27 * Basic counter set: 0-31
28 * Problem-state counter set: 32-63
29 * Crypto-activity counter set: 64-127
30 * Extented counter set: 128-159
31 */
32enum cpumf_ctr_set {
33 /* CPU counter sets */
34 CPUMF_CTR_SET_BASIC = 0,
35 CPUMF_CTR_SET_USER = 1,
36 CPUMF_CTR_SET_CRYPTO = 2,
37 CPUMF_CTR_SET_EXT = 3,
38
39 /* Maximum number of counter sets */
40 CPUMF_CTR_SET_MAX,
41};
42
43#define CPUMF_LCCTL_ENABLE_SHIFT 16
44#define CPUMF_LCCTL_ACTCTL_SHIFT 0
45static const u64 cpumf_state_ctl[CPUMF_CTR_SET_MAX] = {
46 [CPUMF_CTR_SET_BASIC] = 0x02,
47 [CPUMF_CTR_SET_USER] = 0x04,
48 [CPUMF_CTR_SET_CRYPTO] = 0x08,
49 [CPUMF_CTR_SET_EXT] = 0x01,
50};
51
52static void ctr_set_enable(u64 *state, int ctr_set)
53{
54 *state |= cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ENABLE_SHIFT;
55}
56static void ctr_set_disable(u64 *state, int ctr_set)
57{
58 *state &= ~(cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ENABLE_SHIFT);
59}
60static void ctr_set_start(u64 *state, int ctr_set)
61{
62 *state |= cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ACTCTL_SHIFT;
63}
64static void ctr_set_stop(u64 *state, int ctr_set)
65{
66 *state &= ~(cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ACTCTL_SHIFT);
67}
68
69/* Local CPUMF event structure */
70struct cpu_hw_events {
71 struct cpumf_ctr_info info;
72 atomic_t ctr_set[CPUMF_CTR_SET_MAX];
73 u64 state, tx_state;
74 unsigned int flags;
75};
76static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
77 .ctr_set = {
78 [CPUMF_CTR_SET_BASIC] = ATOMIC_INIT(0),
79 [CPUMF_CTR_SET_USER] = ATOMIC_INIT(0),
80 [CPUMF_CTR_SET_CRYPTO] = ATOMIC_INIT(0),
81 [CPUMF_CTR_SET_EXT] = ATOMIC_INIT(0),
82 },
83 .state = 0,
84 .flags = 0,
85};
86
87static int get_counter_set(u64 event)
88{
89 int set = -1;
90
91 if (event < 32)
92 set = CPUMF_CTR_SET_BASIC;
93 else if (event < 64)
94 set = CPUMF_CTR_SET_USER;
95 else if (event < 128)
96 set = CPUMF_CTR_SET_CRYPTO;
97 else if (event < 160)
98 set = CPUMF_CTR_SET_EXT;
99
100 return set;
101}
102
103static int validate_event(const struct hw_perf_event *hwc)
104{
105 switch (hwc->config_base) {
106 case CPUMF_CTR_SET_BASIC:
107 case CPUMF_CTR_SET_USER:
108 case CPUMF_CTR_SET_CRYPTO:
109 case CPUMF_CTR_SET_EXT:
110 /* check for reserved counters */
111 if ((hwc->config >= 6 && hwc->config <= 31) ||
112 (hwc->config >= 38 && hwc->config <= 63) ||
113 (hwc->config >= 80 && hwc->config <= 127))
114 return -EOPNOTSUPP;
115 break;
116 default:
117 return -EINVAL;
118 }
119
120 return 0;
121}
122
123static int validate_ctr_version(const struct hw_perf_event *hwc)
124{
125 struct cpu_hw_events *cpuhw;
126 int err = 0;
127
128 cpuhw = &get_cpu_var(cpu_hw_events);
129
130 /* check required version for counter sets */
131 switch (hwc->config_base) {
132 case CPUMF_CTR_SET_BASIC:
133 case CPUMF_CTR_SET_USER:
134 if (cpuhw->info.cfvn < 1)
135 err = -EOPNOTSUPP;
136 break;
137 case CPUMF_CTR_SET_CRYPTO:
138 case CPUMF_CTR_SET_EXT:
139 if (cpuhw->info.csvn < 1)
140 err = -EOPNOTSUPP;
141 break;
142 }
143
144 put_cpu_var(cpu_hw_events);
145 return err;
146}
147
148static int validate_ctr_auth(const struct hw_perf_event *hwc)
149{
150 struct cpu_hw_events *cpuhw;
151 u64 ctrs_state;
152 int err = 0;
153
154 cpuhw = &get_cpu_var(cpu_hw_events);
155
156 /* check authorization for cpu counter sets */
157 ctrs_state = cpumf_state_ctl[hwc->config_base];
158 if (!(ctrs_state & cpuhw->info.auth_ctl))
159 err = -EPERM;
160
161 put_cpu_var(cpu_hw_events);
162 return err;
163}
164
165/*
166 * Change the CPUMF state to active.
167 * Enable and activate the CPU-counter sets according
168 * to the per-cpu control state.
169 */
170static void cpumf_pmu_enable(struct pmu *pmu)
171{
172 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
173 int err;
174
175 if (cpuhw->flags & PMU_F_ENABLED)
176 return;
177
178 err = lcctl(cpuhw->state);
179 if (err) {
180 pr_err("Enabling the performance measuring unit "
181 "failed with rc=%lx\n", err);
182 return;
183 }
184
185 cpuhw->flags |= PMU_F_ENABLED;
186}
187
188/*
189 * Change the CPUMF state to inactive.
190 * Disable and enable (inactive) the CPU-counter sets according
191 * to the per-cpu control state.
192 */
193static void cpumf_pmu_disable(struct pmu *pmu)
194{
195 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
196 int err;
197 u64 inactive;
198
199 if (!(cpuhw->flags & PMU_F_ENABLED))
200 return;
201
202 inactive = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1);
203 err = lcctl(inactive);
204 if (err) {
205 pr_err("Disabling the performance measuring unit "
206 "failed with rc=%lx\n", err);
207 return;
208 }
209
210 cpuhw->flags &= ~PMU_F_ENABLED;
211}
212
213
214/* Number of perf events counting hardware events */
215static atomic_t num_events = ATOMIC_INIT(0);
216/* Used to avoid races in calling reserve/release_cpumf_hardware */
217static DEFINE_MUTEX(pmc_reserve_mutex);
218
219/* CPU-measurement alerts for the counter facility */
220static void cpumf_measurement_alert(struct ext_code ext_code,
221 unsigned int alert, unsigned long unused)
222{
223 struct cpu_hw_events *cpuhw;
224
225 if (!(alert & CPU_MF_INT_CF_MASK))
226 return;
227
228 kstat_cpu(smp_processor_id()).irqs[EXTINT_CPM]++;
229 cpuhw = &__get_cpu_var(cpu_hw_events);
230
231 /* Measurement alerts are shared and might happen when the PMU
232 * is not reserved. Ignore these alerts in this case. */
233 if (!(cpuhw->flags & PMU_F_RESERVED))
234 return;
235
236 /* counter authorization change alert */
237 if (alert & CPU_MF_INT_CF_CACA)
238 qctri(&cpuhw->info);
239
240 /* loss of counter data alert */
241 if (alert & CPU_MF_INT_CF_LCDA)
242 pr_err("CPU[%i] Counter data was lost\n", smp_processor_id());
243}
244
245#define PMC_INIT 0
246#define PMC_RELEASE 1
247static void setup_pmc_cpu(void *flags)
248{
249 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
250
251 switch (*((int *) flags)) {
252 case PMC_INIT:
253 memset(&cpuhw->info, 0, sizeof(cpuhw->info));
254 qctri(&cpuhw->info);
255 cpuhw->flags |= PMU_F_RESERVED;
256 break;
257
258 case PMC_RELEASE:
259 cpuhw->flags &= ~PMU_F_RESERVED;
260 break;
261 }
262
263 /* Disable CPU counter sets */
264 lcctl(0);
265}
266
267/* Initialize the CPU-measurement facility */
268static int reserve_pmc_hardware(void)
269{
270 int flags = PMC_INIT;
271
272 on_each_cpu(setup_pmc_cpu, &flags, 1);
273 measurement_alert_subclass_register();
274
275 return 0;
276}
277
278/* Release the CPU-measurement facility */
279static void release_pmc_hardware(void)
280{
281 int flags = PMC_RELEASE;
282
283 on_each_cpu(setup_pmc_cpu, &flags, 1);
284 measurement_alert_subclass_unregister();
285}
286
287/* Release the PMU if event is the last perf event */
288static void hw_perf_event_destroy(struct perf_event *event)
289{
290 if (!atomic_add_unless(&num_events, -1, 1)) {
291 mutex_lock(&pmc_reserve_mutex);
292 if (atomic_dec_return(&num_events) == 0)
293 release_pmc_hardware();
294 mutex_unlock(&pmc_reserve_mutex);
295 }
296}
297
298/* CPUMF <-> perf event mappings for kernel+userspace (basic set) */
299static const int cpumf_generic_events_basic[] = {
300 [PERF_COUNT_HW_CPU_CYCLES] = 0,
301 [PERF_COUNT_HW_INSTRUCTIONS] = 1,
302 [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
303 [PERF_COUNT_HW_CACHE_MISSES] = -1,
304 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
305 [PERF_COUNT_HW_BRANCH_MISSES] = -1,
306 [PERF_COUNT_HW_BUS_CYCLES] = -1,
307};
308/* CPUMF <-> perf event mappings for userspace (problem-state set) */
309static const int cpumf_generic_events_user[] = {
310 [PERF_COUNT_HW_CPU_CYCLES] = 32,
311 [PERF_COUNT_HW_INSTRUCTIONS] = 33,
312 [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
313 [PERF_COUNT_HW_CACHE_MISSES] = -1,
314 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
315 [PERF_COUNT_HW_BRANCH_MISSES] = -1,
316 [PERF_COUNT_HW_BUS_CYCLES] = -1,
317};
318
319static int __hw_perf_event_init(struct perf_event *event)
320{
321 struct perf_event_attr *attr = &event->attr;
322 struct hw_perf_event *hwc = &event->hw;
323 int err;
324 u64 ev;
325
326 switch (attr->type) {
327 case PERF_TYPE_RAW:
328 /* Raw events are used to access counters directly,
329 * hence do not permit excludes */
330 if (attr->exclude_kernel || attr->exclude_user ||
331 attr->exclude_hv)
332 return -EOPNOTSUPP;
333 ev = attr->config;
334 break;
335
336 case PERF_TYPE_HARDWARE:
337 ev = attr->config;
338 /* Count user space (problem-state) only */
339 if (!attr->exclude_user && attr->exclude_kernel) {
340 if (ev >= ARRAY_SIZE(cpumf_generic_events_user))
341 return -EOPNOTSUPP;
342 ev = cpumf_generic_events_user[ev];
343
344 /* No support for kernel space counters only */
345 } else if (!attr->exclude_kernel && attr->exclude_user) {
346 return -EOPNOTSUPP;
347
348 /* Count user and kernel space */
349 } else {
350 if (ev >= ARRAY_SIZE(cpumf_generic_events_basic))
351 return -EOPNOTSUPP;
352 ev = cpumf_generic_events_basic[ev];
353 }
354 break;
355
356 default:
357 return -ENOENT;
358 }
359
360 if (ev == -1)
361 return -ENOENT;
362
363 if (ev >= PERF_CPUM_CF_MAX_CTR)
364 return -EINVAL;
365
366 /* The CPU measurement counter facility does not have any interrupts
367 * to do sampling. Sampling must be provided by external means,
368 * for example, by timers.
369 */
370 if (hwc->sample_period)
371 return -EINVAL;
372
373 /* Use the hardware perf event structure to store the counter number
374 * in 'config' member and the counter set to which the counter belongs
375 * in the 'config_base'. The counter set (config_base) is then used
376 * to enable/disable the counters.
377 */
378 hwc->config = ev;
379 hwc->config_base = get_counter_set(ev);
380
381 /* Validate the counter that is assigned to this event.
382 * Because the counter facility can use numerous counters at the
383 * same time without constraints, it is not necessary to explicity
384 * validate event groups (event->group_leader != event).
385 */
386 err = validate_event(hwc);
387 if (err)
388 return err;
389
390 /* Initialize for using the CPU-measurement counter facility */
391 if (!atomic_inc_not_zero(&num_events)) {
392 mutex_lock(&pmc_reserve_mutex);
393 if (atomic_read(&num_events) == 0 && reserve_pmc_hardware())
394 err = -EBUSY;
395 else
396 atomic_inc(&num_events);
397 mutex_unlock(&pmc_reserve_mutex);
398 }
399 event->destroy = hw_perf_event_destroy;
400
401 /* Finally, validate version and authorization of the counter set */
402 err = validate_ctr_auth(hwc);
403 if (!err)
404 err = validate_ctr_version(hwc);
405
406 return err;
407}
408
409static int cpumf_pmu_event_init(struct perf_event *event)
410{
411 int err;
412
413 switch (event->attr.type) {
414 case PERF_TYPE_HARDWARE:
415 case PERF_TYPE_HW_CACHE:
416 case PERF_TYPE_RAW:
417 err = __hw_perf_event_init(event);
418 break;
419 default:
420 return -ENOENT;
421 }
422
423 if (unlikely(err) && event->destroy)
424 event->destroy(event);
425
426 return err;
427}
428
429static int hw_perf_event_reset(struct perf_event *event)
430{
431 u64 prev, new;
432 int err;
433
434 do {
435 prev = local64_read(&event->hw.prev_count);
436 err = ecctr(event->hw.config, &new);
437 if (err) {
438 if (err != 3)
439 break;
440 /* The counter is not (yet) available. This
441 * might happen if the counter set to which
442 * this counter belongs is in the disabled
443 * state.
444 */
445 new = 0;
446 }
447 } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
448
449 return err;
450}
451
452static int hw_perf_event_update(struct perf_event *event)
453{
454 u64 prev, new, delta;
455 int err;
456
457 do {
458 prev = local64_read(&event->hw.prev_count);
459 err = ecctr(event->hw.config, &new);
460 if (err)
461 goto out;
462 } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
463
464 delta = (prev <= new) ? new - prev
465 : (-1ULL - prev) + new + 1; /* overflow */
466 local64_add(delta, &event->count);
467out:
468 return err;
469}
470
471static void cpumf_pmu_read(struct perf_event *event)
472{
473 if (event->hw.state & PERF_HES_STOPPED)
474 return;
475
476 hw_perf_event_update(event);
477}
478
479static void cpumf_pmu_start(struct perf_event *event, int flags)
480{
481 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
482 struct hw_perf_event *hwc = &event->hw;
483
484 if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
485 return;
486
487 if (WARN_ON_ONCE(hwc->config == -1))
488 return;
489
490 if (flags & PERF_EF_RELOAD)
491 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
492
493 hwc->state = 0;
494
495 /* (Re-)enable and activate the counter set */
496 ctr_set_enable(&cpuhw->state, hwc->config_base);
497 ctr_set_start(&cpuhw->state, hwc->config_base);
498
499 /* The counter set to which this counter belongs can be already active.
500 * Because all counters in a set are active, the event->hw.prev_count
501 * needs to be synchronized. At this point, the counter set can be in
502 * the inactive or disabled state.
503 */
504 hw_perf_event_reset(event);
505
506 /* increment refcount for this counter set */
507 atomic_inc(&cpuhw->ctr_set[hwc->config_base]);
508}
509
510static void cpumf_pmu_stop(struct perf_event *event, int flags)
511{
512 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
513 struct hw_perf_event *hwc = &event->hw;
514
515 if (!(hwc->state & PERF_HES_STOPPED)) {
516 /* Decrement reference count for this counter set and if this
517 * is the last used counter in the set, clear activation
518 * control and set the counter set state to inactive.
519 */
520 if (!atomic_dec_return(&cpuhw->ctr_set[hwc->config_base]))
521 ctr_set_stop(&cpuhw->state, hwc->config_base);
522 event->hw.state |= PERF_HES_STOPPED;
523 }
524
525 if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
526 hw_perf_event_update(event);
527 event->hw.state |= PERF_HES_UPTODATE;
528 }
529}
530
531static int cpumf_pmu_add(struct perf_event *event, int flags)
532{
533 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
534
535 /* Check authorization for the counter set to which this
536 * counter belongs.
537 * For group events transaction, the authorization check is
538 * done in cpumf_pmu_commit_txn().
539 */
540 if (!(cpuhw->flags & PERF_EVENT_TXN))
541 if (validate_ctr_auth(&event->hw))
542 return -EPERM;
543
544 ctr_set_enable(&cpuhw->state, event->hw.config_base);
545 event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
546
547 if (flags & PERF_EF_START)
548 cpumf_pmu_start(event, PERF_EF_RELOAD);
549
550 perf_event_update_userpage(event);
551
552 return 0;
553}
554
555static void cpumf_pmu_del(struct perf_event *event, int flags)
556{
557 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
558
559 cpumf_pmu_stop(event, PERF_EF_UPDATE);
560
561 /* Check if any counter in the counter set is still used. If not used,
562 * change the counter set to the disabled state. This also clears the
563 * content of all counters in the set.
564 *
565 * When a new perf event has been added but not yet started, this can
566 * clear enable control and resets all counters in a set. Therefore,
567 * cpumf_pmu_start() always has to reenable a counter set.
568 */
569 if (!atomic_read(&cpuhw->ctr_set[event->hw.config_base]))
570 ctr_set_disable(&cpuhw->state, event->hw.config_base);
571
572 perf_event_update_userpage(event);
573}
574
575/*
576 * Start group events scheduling transaction.
577 * Set flags to perform a single test at commit time.
578 */
579static void cpumf_pmu_start_txn(struct pmu *pmu)
580{
581 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
582
583 perf_pmu_disable(pmu);
584 cpuhw->flags |= PERF_EVENT_TXN;
585 cpuhw->tx_state = cpuhw->state;
586}
587
588/*
589 * Stop and cancel a group events scheduling tranctions.
590 * Assumes cpumf_pmu_del() is called for each successful added
591 * cpumf_pmu_add() during the transaction.
592 */
593static void cpumf_pmu_cancel_txn(struct pmu *pmu)
594{
595 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
596
597 WARN_ON(cpuhw->tx_state != cpuhw->state);
598
599 cpuhw->flags &= ~PERF_EVENT_TXN;
600 perf_pmu_enable(pmu);
601}
602
603/*
604 * Commit the group events scheduling transaction. On success, the
605 * transaction is closed. On error, the transaction is kept open
606 * until cpumf_pmu_cancel_txn() is called.
607 */
608static int cpumf_pmu_commit_txn(struct pmu *pmu)
609{
610 struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
611 u64 state;
612
613 /* check if the updated state can be scheduled */
614 state = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1);
615 state >>= CPUMF_LCCTL_ENABLE_SHIFT;
616 if ((state & cpuhw->info.auth_ctl) != state)
617 return -EPERM;
618
619 cpuhw->flags &= ~PERF_EVENT_TXN;
620 perf_pmu_enable(pmu);
621 return 0;
622}
623
624/* Performance monitoring unit for s390x */
625static struct pmu cpumf_pmu = {
626 .pmu_enable = cpumf_pmu_enable,
627 .pmu_disable = cpumf_pmu_disable,
628 .event_init = cpumf_pmu_event_init,
629 .add = cpumf_pmu_add,
630 .del = cpumf_pmu_del,
631 .start = cpumf_pmu_start,
632 .stop = cpumf_pmu_stop,
633 .read = cpumf_pmu_read,
634 .start_txn = cpumf_pmu_start_txn,
635 .commit_txn = cpumf_pmu_commit_txn,
636 .cancel_txn = cpumf_pmu_cancel_txn,
637};
638
639static int __cpuinit cpumf_pmu_notifier(struct notifier_block *self,
640 unsigned long action, void *hcpu)
641{
642 unsigned int cpu = (long) hcpu;
643 int flags;
644
645 switch (action & ~CPU_TASKS_FROZEN) {
646 case CPU_ONLINE:
647 flags = PMC_INIT;
648 smp_call_function_single(cpu, setup_pmc_cpu, &flags, 1);
649 break;
650 case CPU_DOWN_PREPARE:
651 flags = PMC_RELEASE;
652 smp_call_function_single(cpu, setup_pmc_cpu, &flags, 1);
653 break;
654 default:
655 break;
656 }
657
658 return NOTIFY_OK;
659}
660
661static int __init cpumf_pmu_init(void)
662{
663 int rc;
664
665 if (!cpum_cf_avail())
666 return -ENODEV;
667
668 /* clear bit 15 of cr0 to unauthorize problem-state to
669 * extract measurement counters */
670 ctl_clear_bit(0, 48);
671
672 /* register handler for measurement-alert interruptions */
673 rc = register_external_interrupt(0x1407, cpumf_measurement_alert);
674 if (rc) {
675 pr_err("Registering for CPU-measurement alerts "
676 "failed with rc=%i\n", rc);
677 goto out;
678 }
679
680 rc = perf_pmu_register(&cpumf_pmu, "cpum_cf", PERF_TYPE_RAW);
681 if (rc) {
682 pr_err("Registering the cpum_cf PMU failed with rc=%i\n", rc);
683 unregister_external_interrupt(0x1407, cpumf_measurement_alert);
684 goto out;
685 }
686 perf_cpu_notifier(cpumf_pmu_notifier);
687out:
688 return rc;
689}
690early_initcall(cpumf_pmu_init);
diff --git a/arch/s390/kernel/perf_event.c b/arch/s390/kernel/perf_event.c
new file mode 100644
index 000000000000..609f985198cf
--- /dev/null
+++ b/arch/s390/kernel/perf_event.c
@@ -0,0 +1,125 @@
1/*
2 * Performance event support for s390x
3 *
4 * Copyright IBM Corp. 2012
5 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License (version 2 only)
9 * as published by the Free Software Foundation.
10 */
11#define KMSG_COMPONENT "perf"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
14#include <linux/kernel.h>
15#include <linux/perf_event.h>
16#include <linux/percpu.h>
17#include <linux/export.h>
18#include <asm/system.h>
19#include <asm/irq.h>
20#include <asm/cpu_mf.h>
21#include <asm/lowcore.h>
22#include <asm/processor.h>
23
24const char *perf_pmu_name(void)
25{
26 if (cpum_cf_avail() || cpum_sf_avail())
27 return "CPU-measurement facilities (CPUMF)";
28 return "pmu";
29}
30EXPORT_SYMBOL(perf_pmu_name);
31
32int perf_num_counters(void)
33{
34 int num = 0;
35
36 if (cpum_cf_avail())
37 num += PERF_CPUM_CF_MAX_CTR;
38
39 return num;
40}
41EXPORT_SYMBOL(perf_num_counters);
42
43void perf_event_print_debug(void)
44{
45 struct cpumf_ctr_info cf_info;
46 unsigned long flags;
47 int cpu;
48
49 if (!cpum_cf_avail())
50 return;
51
52 local_irq_save(flags);
53
54 cpu = smp_processor_id();
55 memset(&cf_info, 0, sizeof(cf_info));
56 if (!qctri(&cf_info)) {
57 pr_info("CPU[%i] CPUM_CF: ver=%u.%u A=%04x E=%04x C=%04x\n",
58 cpu, cf_info.cfvn, cf_info.csvn,
59 cf_info.auth_ctl, cf_info.enable_ctl, cf_info.act_ctl);
60 print_hex_dump_bytes("CPUMF Query: ", DUMP_PREFIX_OFFSET,
61 &cf_info, sizeof(cf_info));
62 }
63
64 local_irq_restore(flags);
65}
66
67/* See also arch/s390/kernel/traps.c */
68static unsigned long __store_trace(struct perf_callchain_entry *entry,
69 unsigned long sp,
70 unsigned long low, unsigned long high)
71{
72 struct stack_frame *sf;
73 struct pt_regs *regs;
74
75 while (1) {
76 sp = sp & PSW_ADDR_INSN;
77 if (sp < low || sp > high - sizeof(*sf))
78 return sp;
79 sf = (struct stack_frame *) sp;
80 perf_callchain_store(entry, sf->gprs[8] & PSW_ADDR_INSN);
81 /* Follow the backchain. */
82 while (1) {
83 low = sp;
84 sp = sf->back_chain & PSW_ADDR_INSN;
85 if (!sp)
86 break;
87 if (sp <= low || sp > high - sizeof(*sf))
88 return sp;
89 sf = (struct stack_frame *) sp;
90 perf_callchain_store(entry,
91 sf->gprs[8] & PSW_ADDR_INSN);
92 }
93 /* Zero backchain detected, check for interrupt frame. */
94 sp = (unsigned long) (sf + 1);
95 if (sp <= low || sp > high - sizeof(*regs))
96 return sp;
97 regs = (struct pt_regs *) sp;
98 perf_callchain_store(entry, sf->gprs[8] & PSW_ADDR_INSN);
99 low = sp;
100 sp = regs->gprs[15];
101 }
102}
103
104void perf_callchain_kernel(struct perf_callchain_entry *entry,
105 struct pt_regs *regs)
106{
107 unsigned long head;
108 struct stack_frame *head_sf;
109
110 if (user_mode(regs))
111 return;
112
113 head = regs->gprs[15];
114 head_sf = (struct stack_frame *) head;
115
116 if (!head_sf || !head_sf->back_chain)
117 return;
118
119 head = head_sf->back_chain;
120 head = __store_trace(entry, head, S390_lowcore.async_stack - ASYNC_SIZE,
121 S390_lowcore.async_stack);
122
123 __store_trace(entry, head, S390_lowcore.thread_info,
124 S390_lowcore.thread_info + THREAD_SIZE);
125}