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>
25 const char *perf_pmu_name(void)
27 if (cpum_cf_avail() || cpum_sf_avail())
28 return "CPU-Measurement Facilities (CPU-MF)";
31 EXPORT_SYMBOL(perf_pmu_name
);
33 int perf_num_counters(void)
38 num
+= PERF_CPUM_CF_MAX_CTR
;
40 num
+= PERF_CPUM_SF_MAX_CTR
;
44 EXPORT_SYMBOL(perf_num_counters
);
46 static struct kvm_s390_sie_block
*sie_block(struct pt_regs
*regs
)
48 struct stack_frame
*stack
= (struct stack_frame
*) regs
->gprs
[15];
53 return (struct kvm_s390_sie_block
*) stack
->empty1
[0];
56 static bool is_in_guest(struct pt_regs
*regs
)
60 #if IS_ENABLED(CONFIG_KVM)
61 return instruction_pointer(regs
) == (unsigned long) &sie_exit
;
67 static unsigned long guest_is_user_mode(struct pt_regs
*regs
)
69 return sie_block(regs
)->gpsw
.mask
& PSW_MASK_PSTATE
;
72 static unsigned long instruction_pointer_guest(struct pt_regs
*regs
)
74 return sie_block(regs
)->gpsw
.addr
;
77 unsigned long perf_instruction_pointer(struct pt_regs
*regs
)
79 return is_in_guest(regs
) ? instruction_pointer_guest(regs
)
80 : instruction_pointer(regs
);
83 static unsigned long perf_misc_guest_flags(struct pt_regs
*regs
)
85 return guest_is_user_mode(regs
) ? PERF_RECORD_MISC_GUEST_USER
86 : PERF_RECORD_MISC_GUEST_KERNEL
;
89 static unsigned long perf_misc_flags_sf(struct pt_regs
*regs
)
91 struct perf_sf_sde_regs
*sde_regs
;
94 sde_regs
= (struct perf_sf_sde_regs
*) ®s
->int_parm_long
;
95 if (sde_regs
->in_guest
)
96 flags
= user_mode(regs
) ? PERF_RECORD_MISC_GUEST_USER
97 : PERF_RECORD_MISC_GUEST_KERNEL
;
99 flags
= user_mode(regs
) ? PERF_RECORD_MISC_USER
100 : PERF_RECORD_MISC_KERNEL
;
104 unsigned long perf_misc_flags(struct pt_regs
*regs
)
106 /* Check if the cpum_sf PMU has created the pt_regs structure.
107 * In this case, perf misc flags can be easily extracted. Otherwise,
108 * do regular checks on the pt_regs content.
110 if (regs
->int_code
== 0x1407 && regs
->int_parm
== CPU_MF_INT_SF_PRA
)
112 return perf_misc_flags_sf(regs
);
114 if (is_in_guest(regs
))
115 return perf_misc_guest_flags(regs
);
117 return user_mode(regs
) ? PERF_RECORD_MISC_USER
118 : PERF_RECORD_MISC_KERNEL
;
121 static void print_debug_cf(void)
123 struct cpumf_ctr_info cf_info
;
124 int cpu
= smp_processor_id();
126 memset(&cf_info
, 0, sizeof(cf_info
));
127 if (!qctri(&cf_info
))
128 pr_info("CPU[%i] CPUM_CF: ver=%u.%u A=%04x E=%04x C=%04x\n",
129 cpu
, cf_info
.cfvn
, cf_info
.csvn
,
130 cf_info
.auth_ctl
, cf_info
.enable_ctl
, cf_info
.act_ctl
);
133 static void print_debug_sf(void)
135 struct hws_qsi_info_block si
;
136 int cpu
= smp_processor_id();
138 memset(&si
, 0, sizeof(si
));
142 pr_info("CPU[%i] CPUM_SF: basic=%i diag=%i min=%lu max=%lu cpu_speed=%u\n",
143 cpu
, si
.as
, si
.ad
, si
.min_sampl_rate
, si
.max_sampl_rate
,
147 pr_info("CPU[%i] CPUM_SF: Basic-sampling: a=%i e=%i c=%i"
148 " bsdes=%i tear=%016lx dear=%016lx\n", cpu
,
149 si
.as
, si
.es
, si
.cs
, si
.bsdes
, si
.tear
, si
.dear
);
151 pr_info("CPU[%i] CPUM_SF: Diagnostic-sampling: a=%i e=%i c=%i"
152 " dsdes=%i tear=%016lx dear=%016lx\n", cpu
,
153 si
.ad
, si
.ed
, si
.cd
, si
.dsdes
, si
.tear
, si
.dear
);
156 void perf_event_print_debug(void)
160 local_irq_save(flags
);
165 local_irq_restore(flags
);
168 /* Service level infrastructure */
169 static void sl_print_counter(struct seq_file
*m
)
171 struct cpumf_ctr_info ci
;
173 memset(&ci
, 0, sizeof(ci
));
177 seq_printf(m
, "CPU-MF: Counter facility: version=%u.%u "
178 "authorization=%04x\n", ci
.cfvn
, ci
.csvn
, ci
.auth_ctl
);
181 static void sl_print_sampling(struct seq_file
*m
)
183 struct hws_qsi_info_block si
;
185 memset(&si
, 0, sizeof(si
));
189 if (!si
.as
&& !si
.ad
)
192 seq_printf(m
, "CPU-MF: Sampling facility: min_rate=%lu max_rate=%lu"
193 " cpu_speed=%u\n", si
.min_sampl_rate
, si
.max_sampl_rate
,
196 seq_printf(m
, "CPU-MF: Sampling facility: mode=basic"
197 " sample_size=%u\n", si
.bsdes
);
199 seq_printf(m
, "CPU-MF: Sampling facility: mode=diagnostic"
200 " sample_size=%u\n", si
.dsdes
);
203 static void service_level_perf_print(struct seq_file
*m
,
204 struct service_level
*sl
)
209 sl_print_sampling(m
);
212 static struct service_level service_level_perf
= {
213 .seq_print
= service_level_perf_print
,
216 static int __init
service_level_perf_register(void)
218 return register_service_level(&service_level_perf
);
220 arch_initcall(service_level_perf_register
);
222 static int __perf_callchain_kernel(void *data
, unsigned long address
, int reliable
)
224 struct perf_callchain_entry_ctx
*entry
= data
;
226 perf_callchain_store(entry
, address
);
230 void perf_callchain_kernel(struct perf_callchain_entry_ctx
*entry
,
231 struct pt_regs
*regs
)
235 dump_trace(__perf_callchain_kernel
, entry
, NULL
, regs
->gprs
[15]);
238 /* Perf definitions for PMU event attributes in sysfs */
239 ssize_t
cpumf_events_sysfs_show(struct device
*dev
,
240 struct device_attribute
*attr
, char *page
)
242 struct perf_pmu_events_attr
*pmu_attr
;
244 pmu_attr
= container_of(attr
, struct perf_pmu_events_attr
, attr
);
245 return sprintf(page
, "event=0x%04llx\n", pmu_attr
->id
);