2 * Performance event support for s390x
4 * Copyright IBM Corp. 2012
5 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
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.
11 #define KMSG_COMPONENT "perf"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/kernel.h>
15 #include <linux/perf_event.h>
16 #include <linux/kvm_host.h>
17 #include <linux/percpu.h>
18 #include <linux/export.h>
19 #include <linux/spinlock.h>
20 #include <linux/sysfs.h>
22 #include <asm/cpu_mf.h>
23 #include <asm/lowcore.h>
24 #include <asm/processor.h>
26 const char *perf_pmu_name(void)
28 if (cpum_cf_avail() || cpum_sf_avail())
29 return "CPU-measurement facilities (CPUMF)";
32 EXPORT_SYMBOL(perf_pmu_name
);
34 int perf_num_counters(void)
39 num
+= PERF_CPUM_CF_MAX_CTR
;
41 num
+= PERF_CPUM_SF_MAX_CTR
;
45 EXPORT_SYMBOL(perf_num_counters
);
47 static struct kvm_s390_sie_block
*sie_block(struct pt_regs
*regs
)
49 struct stack_frame
*stack
= (struct stack_frame
*) regs
->gprs
[15];
54 return (struct kvm_s390_sie_block
*) stack
->empty1
[0];
57 static bool is_in_guest(struct pt_regs
*regs
)
61 #if defined(CONFIG_KVM) || defined(CONFIG_KVM_MODULE)
62 return instruction_pointer(regs
) == (unsigned long) &sie_exit
;
68 static unsigned long guest_is_user_mode(struct pt_regs
*regs
)
70 return sie_block(regs
)->gpsw
.mask
& PSW_MASK_PSTATE
;
73 static unsigned long instruction_pointer_guest(struct pt_regs
*regs
)
75 return sie_block(regs
)->gpsw
.addr
& PSW_ADDR_INSN
;
78 unsigned long perf_instruction_pointer(struct pt_regs
*regs
)
80 return is_in_guest(regs
) ? instruction_pointer_guest(regs
)
81 : instruction_pointer(regs
);
84 static unsigned long perf_misc_guest_flags(struct pt_regs
*regs
)
86 return guest_is_user_mode(regs
) ? PERF_RECORD_MISC_GUEST_USER
87 : PERF_RECORD_MISC_GUEST_KERNEL
;
90 unsigned long perf_misc_flags(struct pt_regs
*regs
)
92 if (is_in_guest(regs
))
93 return perf_misc_guest_flags(regs
);
95 return user_mode(regs
) ? PERF_RECORD_MISC_USER
96 : PERF_RECORD_MISC_KERNEL
;
99 void print_debug_cf(void)
101 struct cpumf_ctr_info cf_info
;
102 int cpu
= smp_processor_id();
104 memset(&cf_info
, 0, sizeof(cf_info
));
105 if (!qctri(&cf_info
))
106 pr_info("CPU[%i] CPUM_CF: ver=%u.%u A=%04x E=%04x C=%04x\n",
107 cpu
, cf_info
.cfvn
, cf_info
.csvn
,
108 cf_info
.auth_ctl
, cf_info
.enable_ctl
, cf_info
.act_ctl
);
111 static void print_debug_sf(void)
113 struct hws_qsi_info_block si
;
114 int cpu
= smp_processor_id();
116 memset(&si
, 0, sizeof(si
));
118 pr_err("CPU[%i]: CPM_SF: qsi failed\n");
122 pr_info("CPU[%i]: CPM_SF: as=%i es=%i cs=%i bsdes=%i dsdes=%i"
123 " min=%i max=%i cpu_speed=%i tear=%p dear=%p\n",
124 cpu
, si
.as
, si
.es
, si
.cs
, si
.bsdes
, si
.dsdes
,
125 si
.min_sampl_rate
, si
.max_sampl_rate
, si
.cpu_speed
,
129 void perf_event_print_debug(void)
133 local_irq_save(flags
);
138 local_irq_restore(flags
);
141 /* See also arch/s390/kernel/traps.c */
142 static unsigned long __store_trace(struct perf_callchain_entry
*entry
,
144 unsigned long low
, unsigned long high
)
146 struct stack_frame
*sf
;
147 struct pt_regs
*regs
;
150 sp
= sp
& PSW_ADDR_INSN
;
151 if (sp
< low
|| sp
> high
- sizeof(*sf
))
153 sf
= (struct stack_frame
*) sp
;
154 perf_callchain_store(entry
, sf
->gprs
[8] & PSW_ADDR_INSN
);
155 /* Follow the backchain. */
158 sp
= sf
->back_chain
& PSW_ADDR_INSN
;
161 if (sp
<= low
|| sp
> high
- sizeof(*sf
))
163 sf
= (struct stack_frame
*) sp
;
164 perf_callchain_store(entry
,
165 sf
->gprs
[8] & PSW_ADDR_INSN
);
167 /* Zero backchain detected, check for interrupt frame. */
168 sp
= (unsigned long) (sf
+ 1);
169 if (sp
<= low
|| sp
> high
- sizeof(*regs
))
171 regs
= (struct pt_regs
*) sp
;
172 perf_callchain_store(entry
, sf
->gprs
[8] & PSW_ADDR_INSN
);
178 void perf_callchain_kernel(struct perf_callchain_entry
*entry
,
179 struct pt_regs
*regs
)
182 struct stack_frame
*head_sf
;
187 head
= regs
->gprs
[15];
188 head_sf
= (struct stack_frame
*) head
;
190 if (!head_sf
|| !head_sf
->back_chain
)
193 head
= head_sf
->back_chain
;
194 head
= __store_trace(entry
, head
, S390_lowcore
.async_stack
- ASYNC_SIZE
,
195 S390_lowcore
.async_stack
);
197 __store_trace(entry
, head
, S390_lowcore
.thread_info
,
198 S390_lowcore
.thread_info
+ THREAD_SIZE
);
201 /* Perf defintions for PMU event attributes in sysfs */
202 ssize_t
cpumf_events_sysfs_show(struct device
*dev
,
203 struct device_attribute
*attr
, char *page
)
205 struct perf_pmu_events_attr
*pmu_attr
;
207 pmu_attr
= container_of(attr
, struct perf_pmu_events_attr
, attr
);
208 return sprintf(page
, "event=0x%04llx,name=%s\n",
209 pmu_attr
->id
, attr
->attr
.name
);