2 * Performance event support for s390x
4 * Copyright IBM Corp. 2012, 2013
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/seq_file.h>
20 #include <linux/spinlock.h>
21 #include <linux/sysfs.h>
23 #include <asm/cpu_mf.h>
24 #include <asm/lowcore.h>
25 #include <asm/processor.h>
26 #include <asm/sysinfo.h>
28 const char *perf_pmu_name(void)
30 if (cpum_cf_avail() || cpum_sf_avail())
31 return "CPU-Measurement Facilities (CPU-MF)";
34 EXPORT_SYMBOL(perf_pmu_name);
36 int perf_num_counters(void)
41 num += PERF_CPUM_CF_MAX_CTR;
43 num += PERF_CPUM_SF_MAX_CTR;
47 EXPORT_SYMBOL(perf_num_counters);
49 static struct kvm_s390_sie_block *sie_block(struct pt_regs *regs)
51 struct stack_frame *stack = (struct stack_frame *) regs->gprs[15];
56 return (struct kvm_s390_sie_block *) stack->empty1[0];
59 static bool is_in_guest(struct pt_regs *regs)
63 #if IS_ENABLED(CONFIG_KVM)
64 return instruction_pointer(regs) == (unsigned long) &sie_exit;
70 static unsigned long guest_is_user_mode(struct pt_regs *regs)
72 return sie_block(regs)->gpsw.mask & PSW_MASK_PSTATE;
75 static unsigned long instruction_pointer_guest(struct pt_regs *regs)
77 return sie_block(regs)->gpsw.addr & PSW_ADDR_INSN;
80 unsigned long perf_instruction_pointer(struct pt_regs *regs)
82 return is_in_guest(regs) ? instruction_pointer_guest(regs)
83 : instruction_pointer(regs);
86 static unsigned long perf_misc_guest_flags(struct pt_regs *regs)
88 return guest_is_user_mode(regs) ? PERF_RECORD_MISC_GUEST_USER
89 : PERF_RECORD_MISC_GUEST_KERNEL;
92 static unsigned long perf_misc_flags_sf(struct pt_regs *regs)
94 struct perf_sf_sde_regs *sde_regs;
97 sde_regs = (struct perf_sf_sde_regs *) ®s->int_parm_long;
98 if (sde_regs->in_guest)
99 flags = user_mode(regs) ? PERF_RECORD_MISC_GUEST_USER
100 : PERF_RECORD_MISC_GUEST_KERNEL;
102 flags = user_mode(regs) ? PERF_RECORD_MISC_USER
103 : PERF_RECORD_MISC_KERNEL;
107 unsigned long perf_misc_flags(struct pt_regs *regs)
109 /* Check if the cpum_sf PMU has created the pt_regs structure.
110 * In this case, perf misc flags can be easily extracted. Otherwise,
111 * do regular checks on the pt_regs content.
113 if (regs->int_code == 0x1407 && regs->int_parm == CPU_MF_INT_SF_PRA)
115 return perf_misc_flags_sf(regs);
117 if (is_in_guest(regs))
118 return perf_misc_guest_flags(regs);
120 return user_mode(regs) ? PERF_RECORD_MISC_USER
121 : PERF_RECORD_MISC_KERNEL;
124 static void print_debug_cf(void)
126 struct cpumf_ctr_info cf_info;
127 int cpu = smp_processor_id();
129 memset(&cf_info, 0, sizeof(cf_info));
130 if (!qctri(&cf_info))
131 pr_info("CPU[%i] CPUM_CF: ver=%u.%u A=%04x E=%04x C=%04x\n",
132 cpu, cf_info.cfvn, cf_info.csvn,
133 cf_info.auth_ctl, cf_info.enable_ctl, cf_info.act_ctl);
136 static void print_debug_sf(void)
138 struct hws_qsi_info_block si;
139 int cpu = smp_processor_id();
141 memset(&si, 0, sizeof(si));
145 pr_info("CPU[%i] CPUM_SF: basic=%i diag=%i min=%lu max=%lu cpu_speed=%u\n",
146 cpu, si.as, si.ad, si.min_sampl_rate, si.max_sampl_rate,
150 pr_info("CPU[%i] CPUM_SF: Basic-sampling: a=%i e=%i c=%i"
151 " bsdes=%i tear=%016lx dear=%016lx\n", cpu,
152 si.as, si.es, si.cs, si.bsdes, si.tear, si.dear);
154 pr_info("CPU[%i] CPUM_SF: Diagnostic-sampling: a=%i e=%i c=%i"
155 " dsdes=%i tear=%016lx dear=%016lx\n", cpu,
156 si.ad, si.ed, si.cd, si.dsdes, si.tear, si.dear);
159 void perf_event_print_debug(void)
163 local_irq_save(flags);
168 local_irq_restore(flags);
171 /* Service level infrastructure */
172 static void sl_print_counter(struct seq_file *m)
174 struct cpumf_ctr_info ci;
176 memset(&ci, 0, sizeof(ci));
180 seq_printf(m, "CPU-MF: Counter facility: version=%u.%u "
181 "authorization=%04x\n", ci.cfvn, ci.csvn, ci.auth_ctl);
184 static void sl_print_sampling(struct seq_file *m)
186 struct hws_qsi_info_block si;
188 memset(&si, 0, sizeof(si));
192 if (!si.as && !si.ad)
195 seq_printf(m, "CPU-MF: Sampling facility: min_rate=%lu max_rate=%lu"
196 " cpu_speed=%u\n", si.min_sampl_rate, si.max_sampl_rate,
199 seq_printf(m, "CPU-MF: Sampling facility: mode=basic"
200 " sample_size=%u\n", si.bsdes);
202 seq_printf(m, "CPU-MF: Sampling facility: mode=diagnostic"
203 " sample_size=%u\n", si.dsdes);
206 static void service_level_perf_print(struct seq_file *m,
207 struct service_level *sl)
212 sl_print_sampling(m);
215 static struct service_level service_level_perf = {
216 .seq_print = service_level_perf_print,
219 static int __init service_level_perf_register(void)
221 return register_service_level(&service_level_perf);
223 arch_initcall(service_level_perf_register);
225 /* See also arch/s390/kernel/traps.c */
226 static unsigned long __store_trace(struct perf_callchain_entry *entry,
228 unsigned long low, unsigned long high)
230 struct stack_frame *sf;
231 struct pt_regs *regs;
234 sp = sp & PSW_ADDR_INSN;
235 if (sp < low || sp > high - sizeof(*sf))
237 sf = (struct stack_frame *) sp;
238 perf_callchain_store(entry, sf->gprs[8] & PSW_ADDR_INSN);
239 /* Follow the backchain. */
242 sp = sf->back_chain & PSW_ADDR_INSN;
245 if (sp <= low || sp > high - sizeof(*sf))
247 sf = (struct stack_frame *) sp;
248 perf_callchain_store(entry,
249 sf->gprs[8] & PSW_ADDR_INSN);
251 /* Zero backchain detected, check for interrupt frame. */
252 sp = (unsigned long) (sf + 1);
253 if (sp <= low || sp > high - sizeof(*regs))
255 regs = (struct pt_regs *) sp;
256 perf_callchain_store(entry, sf->gprs[8] & PSW_ADDR_INSN);
262 void perf_callchain_kernel(struct perf_callchain_entry *entry,
263 struct pt_regs *regs)
266 struct stack_frame *head_sf;
271 head = regs->gprs[15];
272 head_sf = (struct stack_frame *) head;
274 if (!head_sf || !head_sf->back_chain)
277 head = head_sf->back_chain;
278 head = __store_trace(entry, head, S390_lowcore.async_stack - ASYNC_SIZE,
279 S390_lowcore.async_stack);
281 __store_trace(entry, head, S390_lowcore.thread_info,
282 S390_lowcore.thread_info + THREAD_SIZE);
285 /* Perf defintions for PMU event attributes in sysfs */
286 ssize_t cpumf_events_sysfs_show(struct device *dev,
287 struct device_attribute *attr, char *page)
289 struct perf_pmu_events_attr *pmu_attr;
291 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
292 return sprintf(page, "event=0x%04llx,name=%s\n",
293 pmu_attr->id, attr->attr.name);
296 /* Reserve/release functions for sharing perf hardware */
297 static DEFINE_SPINLOCK(perf_hw_owner_lock);
298 static void *perf_sampling_owner;
300 int perf_reserve_sampling(void)
305 spin_lock(&perf_hw_owner_lock);
306 if (perf_sampling_owner) {
307 pr_warn("The sampling facility is already reserved by %p\n",
308 perf_sampling_owner);
311 perf_sampling_owner = __builtin_return_address(0);
312 spin_unlock(&perf_hw_owner_lock);
315 EXPORT_SYMBOL(perf_reserve_sampling);
317 void perf_release_sampling(void)
319 spin_lock(&perf_hw_owner_lock);
320 WARN_ON(!perf_sampling_owner);
321 perf_sampling_owner = NULL;
322 spin_unlock(&perf_hw_owner_lock);
324 EXPORT_SYMBOL(perf_release_sampling);