]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/s390/kernel/perf_event.c
Merge branch 'for-4.5-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[mirror_ubuntu-bionic-kernel.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;
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 if (sp < low || sp > high - sizeof(*sf))
235 return sp;
236 sf = (struct stack_frame *) sp;
237 perf_callchain_store(entry, sf->gprs[8]);
238 /* Follow the backchain. */
239 while (1) {
240 low = sp;
241 sp = sf->back_chain;
242 if (!sp)
243 break;
244 if (sp <= low || sp > high - sizeof(*sf))
245 return sp;
246 sf = (struct stack_frame *) sp;
247 perf_callchain_store(entry, sf->gprs[8]);
248 }
249 /* Zero backchain detected, check for interrupt frame. */
250 sp = (unsigned long) (sf + 1);
251 if (sp <= low || sp > high - sizeof(*regs))
252 return sp;
253 regs = (struct pt_regs *) sp;
254 perf_callchain_store(entry, sf->gprs[8]);
255 low = sp;
256 sp = regs->gprs[15];
257 }
258 }
259
260 void perf_callchain_kernel(struct perf_callchain_entry *entry,
261 struct pt_regs *regs)
262 {
263 unsigned long head;
264 struct stack_frame *head_sf;
265
266 if (user_mode(regs))
267 return;
268
269 head = regs->gprs[15];
270 head_sf = (struct stack_frame *) head;
271
272 if (!head_sf || !head_sf->back_chain)
273 return;
274
275 head = head_sf->back_chain;
276 head = __store_trace(entry, head, S390_lowcore.async_stack - ASYNC_SIZE,
277 S390_lowcore.async_stack);
278
279 __store_trace(entry, head, S390_lowcore.thread_info,
280 S390_lowcore.thread_info + THREAD_SIZE);
281 }
282
283 /* Perf defintions for PMU event attributes in sysfs */
284 ssize_t cpumf_events_sysfs_show(struct device *dev,
285 struct device_attribute *attr, char *page)
286 {
287 struct perf_pmu_events_attr *pmu_attr;
288
289 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
290 return sprintf(page, "event=0x%04llx,name=%s\n",
291 pmu_attr->id, attr->attr.name);
292 }
293
294 /* Reserve/release functions for sharing perf hardware */
295 static DEFINE_SPINLOCK(perf_hw_owner_lock);
296 static void *perf_sampling_owner;
297
298 int perf_reserve_sampling(void)
299 {
300 int err;
301
302 err = 0;
303 spin_lock(&perf_hw_owner_lock);
304 if (perf_sampling_owner) {
305 pr_warn("The sampling facility is already reserved by %p\n",
306 perf_sampling_owner);
307 err = -EBUSY;
308 } else
309 perf_sampling_owner = __builtin_return_address(0);
310 spin_unlock(&perf_hw_owner_lock);
311 return err;
312 }
313 EXPORT_SYMBOL(perf_reserve_sampling);
314
315 void perf_release_sampling(void)
316 {
317 spin_lock(&perf_hw_owner_lock);
318 WARN_ON(!perf_sampling_owner);
319 perf_sampling_owner = NULL;
320 spin_unlock(&perf_hw_owner_lock);
321 }
322 EXPORT_SYMBOL(perf_release_sampling);