1 // SPDX-License-Identifier: GPL-2.0
3 * Performance event support for s390x
5 * Copyright IBM Corp. 2012, 2013
6 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
8 #define KMSG_COMPONENT "perf"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 #include <linux/kernel.h>
12 #include <linux/perf_event.h>
13 #include <linux/kvm_host.h>
14 #include <linux/percpu.h>
15 #include <linux/export.h>
16 #include <linux/seq_file.h>
17 #include <linux/spinlock.h>
18 #include <linux/sysfs.h>
20 #include <asm/cpu_mf.h>
21 #include <asm/lowcore.h>
22 #include <asm/processor.h>
23 #include <asm/sysinfo.h>
24 #include <asm/unwind.h>
26 const char *perf_pmu_name(void)
28 if (cpum_cf_avail() || cpum_sf_avail())
29 return "CPU-Measurement Facilities (CPU-MF)";
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 IS_ENABLED(CONFIG_KVM)
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
;
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 static unsigned long perf_misc_flags_sf(struct pt_regs
*regs
)
92 struct perf_sf_sde_regs
*sde_regs
;
95 sde_regs
= (struct perf_sf_sde_regs
*) ®s
->int_parm_long
;
96 if (sde_regs
->in_guest
)
97 flags
= user_mode(regs
) ? PERF_RECORD_MISC_GUEST_USER
98 : PERF_RECORD_MISC_GUEST_KERNEL
;
100 flags
= user_mode(regs
) ? PERF_RECORD_MISC_USER
101 : PERF_RECORD_MISC_KERNEL
;
105 unsigned long perf_misc_flags(struct pt_regs
*regs
)
107 /* Check if the cpum_sf PMU has created the pt_regs structure.
108 * In this case, perf misc flags can be easily extracted. Otherwise,
109 * do regular checks on the pt_regs content.
111 if (regs
->int_code
== 0x1407 && regs
->int_parm
== CPU_MF_INT_SF_PRA
)
113 return perf_misc_flags_sf(regs
);
115 if (is_in_guest(regs
))
116 return perf_misc_guest_flags(regs
);
118 return user_mode(regs
) ? PERF_RECORD_MISC_USER
119 : PERF_RECORD_MISC_KERNEL
;
122 static void print_debug_cf(void)
124 struct cpumf_ctr_info cf_info
;
125 int cpu
= smp_processor_id();
127 memset(&cf_info
, 0, sizeof(cf_info
));
128 if (!qctri(&cf_info
))
129 pr_info("CPU[%i] CPUM_CF: ver=%u.%u A=%04x E=%04x C=%04x\n",
130 cpu
, cf_info
.cfvn
, cf_info
.csvn
,
131 cf_info
.auth_ctl
, cf_info
.enable_ctl
, cf_info
.act_ctl
);
134 static void print_debug_sf(void)
136 struct hws_qsi_info_block si
;
137 int cpu
= smp_processor_id();
139 memset(&si
, 0, sizeof(si
));
143 pr_info("CPU[%i] CPUM_SF: basic=%i diag=%i min=%lu max=%lu cpu_speed=%u\n",
144 cpu
, si
.as
, si
.ad
, si
.min_sampl_rate
, si
.max_sampl_rate
,
148 pr_info("CPU[%i] CPUM_SF: Basic-sampling: a=%i e=%i c=%i"
149 " bsdes=%i tear=%016lx dear=%016lx\n", cpu
,
150 si
.as
, si
.es
, si
.cs
, si
.bsdes
, si
.tear
, si
.dear
);
152 pr_info("CPU[%i] CPUM_SF: Diagnostic-sampling: a=%i e=%i c=%i"
153 " dsdes=%i tear=%016lx dear=%016lx\n", cpu
,
154 si
.ad
, si
.ed
, si
.cd
, si
.dsdes
, si
.tear
, si
.dear
);
157 void perf_event_print_debug(void)
161 local_irq_save(flags
);
166 local_irq_restore(flags
);
169 /* Service level infrastructure */
170 static void sl_print_counter(struct seq_file
*m
)
172 struct cpumf_ctr_info ci
;
174 memset(&ci
, 0, sizeof(ci
));
178 seq_printf(m
, "CPU-MF: Counter facility: version=%u.%u "
179 "authorization=%04x\n", ci
.cfvn
, ci
.csvn
, ci
.auth_ctl
);
182 static void sl_print_sampling(struct seq_file
*m
)
184 struct hws_qsi_info_block si
;
186 memset(&si
, 0, sizeof(si
));
190 if (!si
.as
&& !si
.ad
)
193 seq_printf(m
, "CPU-MF: Sampling facility: min_rate=%lu max_rate=%lu"
194 " cpu_speed=%u\n", si
.min_sampl_rate
, si
.max_sampl_rate
,
197 seq_printf(m
, "CPU-MF: Sampling facility: mode=basic"
198 " sample_size=%u\n", si
.bsdes
);
200 seq_printf(m
, "CPU-MF: Sampling facility: mode=diagnostic"
201 " sample_size=%u\n", si
.dsdes
);
204 static void service_level_perf_print(struct seq_file
*m
,
205 struct service_level
*sl
)
210 sl_print_sampling(m
);
213 static struct service_level service_level_perf
= {
214 .seq_print
= service_level_perf_print
,
217 static int __init
service_level_perf_register(void)
219 return register_service_level(&service_level_perf
);
221 arch_initcall(service_level_perf_register
);
223 void perf_callchain_kernel(struct perf_callchain_entry_ctx
*entry
,
224 struct pt_regs
*regs
)
226 struct unwind_state state
;
228 unwind_for_each_frame(&state
, current
, regs
, 0)
229 perf_callchain_store(entry
, state
.ip
);
232 /* Perf definitions for PMU event attributes in sysfs */
233 ssize_t
cpumf_events_sysfs_show(struct device
*dev
,
234 struct device_attribute
*attr
, char *page
)
236 struct perf_pmu_events_attr
*pmu_attr
;
238 pmu_attr
= container_of(attr
, struct perf_pmu_events_attr
, attr
);
239 return sprintf(page
, "event=0x%04llx\n", pmu_attr
->id
);