Merge branch 'fix/rt5645' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[deliverable/linux.git] / arch / s390 / kernel / perf_event.c
1 /*
2 * Performance event support for s390x
3 *
4 * Copyright IBM Corp. 2012, 2013
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/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>
22 #include <asm/irq.h>
23 #include <asm/cpu_mf.h>
24 #include <asm/lowcore.h>
25 #include <asm/processor.h>
26 #include <asm/sysinfo.h>
27
28 const char *perf_pmu_name(void)
29 {
30 if (cpum_cf_avail() || cpum_sf_avail())
31 return "CPU-Measurement Facilities (CPU-MF)";
32 return "pmu";
33 }
34 EXPORT_SYMBOL(perf_pmu_name);
35
36 int perf_num_counters(void)
37 {
38 int num = 0;
39
40 if (cpum_cf_avail())
41 num += PERF_CPUM_CF_MAX_CTR;
42 if (cpum_sf_avail())
43 num += PERF_CPUM_SF_MAX_CTR;
44
45 return num;
46 }
47 EXPORT_SYMBOL(perf_num_counters);
48
49 static struct kvm_s390_sie_block *sie_block(struct pt_regs *regs)
50 {
51 struct stack_frame *stack = (struct stack_frame *) regs->gprs[15];
52
53 if (!stack)
54 return NULL;
55
56 return (struct kvm_s390_sie_block *) stack->empty1[0];
57 }
58
59 static bool is_in_guest(struct pt_regs *regs)
60 {
61 if (user_mode(regs))
62 return false;
63 #if IS_ENABLED(CONFIG_KVM)
64 return instruction_pointer(regs) == (unsigned long) &sie_exit;
65 #else
66 return false;
67 #endif
68 }
69
70 static unsigned long guest_is_user_mode(struct pt_regs *regs)
71 {
72 return sie_block(regs)->gpsw.mask & PSW_MASK_PSTATE;
73 }
74
75 static unsigned long instruction_pointer_guest(struct pt_regs *regs)
76 {
77 return sie_block(regs)->gpsw.addr & PSW_ADDR_INSN;
78 }
79
80 unsigned long perf_instruction_pointer(struct pt_regs *regs)
81 {
82 return is_in_guest(regs) ? instruction_pointer_guest(regs)
83 : instruction_pointer(regs);
84 }
85
86 static unsigned long perf_misc_guest_flags(struct pt_regs *regs)
87 {
88 return guest_is_user_mode(regs) ? PERF_RECORD_MISC_GUEST_USER
89 : PERF_RECORD_MISC_GUEST_KERNEL;
90 }
91
92 static unsigned long perf_misc_flags_sf(struct pt_regs *regs)
93 {
94 struct perf_sf_sde_regs *sde_regs;
95 unsigned long flags;
96
97 sde_regs = (struct perf_sf_sde_regs *) &regs->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;
101 else
102 flags = user_mode(regs) ? PERF_RECORD_MISC_USER
103 : PERF_RECORD_MISC_KERNEL;
104 return flags;
105 }
106
107 unsigned long perf_misc_flags(struct pt_regs *regs)
108 {
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.
112 */
113 if (regs->int_code == 0x1407 && regs->int_parm == CPU_MF_INT_SF_PRA)
114 if (!regs->gprs[15])
115 return perf_misc_flags_sf(regs);
116
117 if (is_in_guest(regs))
118 return perf_misc_guest_flags(regs);
119
120 return user_mode(regs) ? PERF_RECORD_MISC_USER
121 : PERF_RECORD_MISC_KERNEL;
122 }
123
124 static void print_debug_cf(void)
125 {
126 struct cpumf_ctr_info cf_info;
127 int cpu = smp_processor_id();
128
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);
134 }
135
136 static void print_debug_sf(void)
137 {
138 struct hws_qsi_info_block si;
139 int cpu = smp_processor_id();
140
141 memset(&si, 0, sizeof(si));
142 if (qsi(&si))
143 return;
144
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,
147 si.cpu_speed);
148
149 if (si.as)
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);
153 if (si.ad)
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);
157 }
158
159 void perf_event_print_debug(void)
160 {
161 unsigned long flags;
162
163 local_irq_save(flags);
164 if (cpum_cf_avail())
165 print_debug_cf();
166 if (cpum_sf_avail())
167 print_debug_sf();
168 local_irq_restore(flags);
169 }
170
171 /* Service level infrastructure */
172 static void sl_print_counter(struct seq_file *m)
173 {
174 struct cpumf_ctr_info ci;
175
176 memset(&ci, 0, sizeof(ci));
177 if (qctri(&ci))
178 return;
179
180 seq_printf(m, "CPU-MF: Counter facility: version=%u.%u "
181 "authorization=%04x\n", ci.cfvn, ci.csvn, ci.auth_ctl);
182 }
183
184 static void sl_print_sampling(struct seq_file *m)
185 {
186 struct hws_qsi_info_block si;
187
188 memset(&si, 0, sizeof(si));
189 if (qsi(&si))
190 return;
191
192 if (!si.as && !si.ad)
193 return;
194
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,
197 si.cpu_speed);
198 if (si.as)
199 seq_printf(m, "CPU-MF: Sampling facility: mode=basic"
200 " sample_size=%u\n", si.bsdes);
201 if (si.ad)
202 seq_printf(m, "CPU-MF: Sampling facility: mode=diagnostic"
203 " sample_size=%u\n", si.dsdes);
204 }
205
206 static void service_level_perf_print(struct seq_file *m,
207 struct service_level *sl)
208 {
209 if (cpum_cf_avail())
210 sl_print_counter(m);
211 if (cpum_sf_avail())
212 sl_print_sampling(m);
213 }
214
215 static struct service_level service_level_perf = {
216 .seq_print = service_level_perf_print,
217 };
218
219 static int __init service_level_perf_register(void)
220 {
221 return register_service_level(&service_level_perf);
222 }
223 arch_initcall(service_level_perf_register);
224
225 /* See also arch/s390/kernel/traps.c */
226 static unsigned long __store_trace(struct perf_callchain_entry *entry,
227 unsigned long sp,
228 unsigned long low, unsigned long high)
229 {
230 struct stack_frame *sf;
231 struct pt_regs *regs;
232
233 while (1) {
234 sp = sp & PSW_ADDR_INSN;
235 if (sp < low || sp > high - sizeof(*sf))
236 return sp;
237 sf = (struct stack_frame *) sp;
238 perf_callchain_store(entry, sf->gprs[8] & PSW_ADDR_INSN);
239 /* Follow the backchain. */
240 while (1) {
241 low = sp;
242 sp = sf->back_chain & PSW_ADDR_INSN;
243 if (!sp)
244 break;
245 if (sp <= low || sp > high - sizeof(*sf))
246 return sp;
247 sf = (struct stack_frame *) sp;
248 perf_callchain_store(entry,
249 sf->gprs[8] & PSW_ADDR_INSN);
250 }
251 /* Zero backchain detected, check for interrupt frame. */
252 sp = (unsigned long) (sf + 1);
253 if (sp <= low || sp > high - sizeof(*regs))
254 return sp;
255 regs = (struct pt_regs *) sp;
256 perf_callchain_store(entry, sf->gprs[8] & PSW_ADDR_INSN);
257 low = sp;
258 sp = regs->gprs[15];
259 }
260 }
261
262 void perf_callchain_kernel(struct perf_callchain_entry *entry,
263 struct pt_regs *regs)
264 {
265 unsigned long head;
266 struct stack_frame *head_sf;
267
268 if (user_mode(regs))
269 return;
270
271 head = regs->gprs[15];
272 head_sf = (struct stack_frame *) head;
273
274 if (!head_sf || !head_sf->back_chain)
275 return;
276
277 head = head_sf->back_chain;
278 head = __store_trace(entry, head, S390_lowcore.async_stack - ASYNC_SIZE,
279 S390_lowcore.async_stack);
280
281 __store_trace(entry, head, S390_lowcore.thread_info,
282 S390_lowcore.thread_info + THREAD_SIZE);
283 }
284
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)
288 {
289 struct perf_pmu_events_attr *pmu_attr;
290
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);
294 }
295
296 /* Reserve/release functions for sharing perf hardware */
297 static DEFINE_SPINLOCK(perf_hw_owner_lock);
298 static void *perf_sampling_owner;
299
300 int perf_reserve_sampling(void)
301 {
302 int err;
303
304 err = 0;
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);
309 err = -EBUSY;
310 } else
311 perf_sampling_owner = __builtin_return_address(0);
312 spin_unlock(&perf_hw_owner_lock);
313 return err;
314 }
315 EXPORT_SYMBOL(perf_reserve_sampling);
316
317 void perf_release_sampling(void)
318 {
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);
323 }
324 EXPORT_SYMBOL(perf_release_sampling);
This page took 0.037072 seconds and 5 git commands to generate.