blob: 04e1b6a8536297cc61f2bacc0187c0b6a7089573 [file] [log] [blame]
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * S390 Version
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02003 * Copyright IBM Corp. 2002, 2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Author(s): Thomas Spatzier (tspat@de.ibm.com)
Heinz Graalfsc814d162011-02-15 13:02:14 -05005 * Author(s): Mahesh Salgaonkar (mahesh@linux.vnet.ibm.com)
6 * Author(s): Heinz Graalfs (graalfs@linux.vnet.ibm.com)
Andreas Krebbeldd3c4672011-11-25 20:03:05 +01007 * Author(s): Andreas Krebbel (krebbel@linux.vnet.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Heinz Graalfsc814d162011-02-15 13:02:14 -05009 * @remark Copyright 2002-2011 OProfile authors
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/oprofile.h>
13#include <linux/init.h>
14#include <linux/errno.h>
Heinz Graalfsc814d162011-02-15 13:02:14 -050015#include <linux/fs.h>
Andreas Krebbeldd3c4672011-11-25 20:03:05 +010016#include <linux/module.h>
17#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Heinz Graalfsc814d162011-02-15 13:02:14 -050019#include "../../../drivers/oprofile/oprof.h"
Heiko Carstens984e8482011-03-23 10:15:00 +010020
21extern void s390_backtrace(struct pt_regs * const regs, unsigned int depth);
22
23#ifdef CONFIG_64BIT
24
Heinz Graalfsc814d162011-02-15 13:02:14 -050025#include "hwsampler.h"
Andreas Krebbeldd3c4672011-11-25 20:03:05 +010026#include "op_counter.h"
Heinz Graalfsc814d162011-02-15 13:02:14 -050027
Christian Borntraeger68158232011-06-22 16:24:09 +020028#define DEFAULT_INTERVAL 4127518
Heinz Graalfsc814d162011-02-15 13:02:14 -050029
30#define DEFAULT_SDBT_BLOCKS 1
31#define DEFAULT_SDB_BLOCKS 511
32
33static unsigned long oprofile_hw_interval = DEFAULT_INTERVAL;
34static unsigned long oprofile_min_interval;
35static unsigned long oprofile_max_interval;
36
37static unsigned long oprofile_sdbt_blocks = DEFAULT_SDBT_BLOCKS;
38static unsigned long oprofile_sdb_blocks = DEFAULT_SDB_BLOCKS;
39
Andreas Krebbeldd3c4672011-11-25 20:03:05 +010040static int hwsampler_enabled;
Heinz Graalfsc814d162011-02-15 13:02:14 -050041static int hwsampler_running; /* start_mutex must be held to change */
Andreas Krebbeldd3c4672011-11-25 20:03:05 +010042static int hwsampler_available;
Heinz Graalfsc814d162011-02-15 13:02:14 -050043
44static struct oprofile_operations timer_ops;
Andreas Krebbeld0f4c162006-01-06 00:19:16 -080045
Andreas Krebbeldd3c4672011-11-25 20:03:05 +010046struct op_counter_config counter_config;
47
48enum __force_cpu_type {
49 reserved = 0, /* do not force */
50 timer,
51};
52static int force_cpu_type;
53
54static int set_cpu_type(const char *str, struct kernel_param *kp)
55{
56 if (!strcmp(str, "timer")) {
57 force_cpu_type = timer;
58 printk(KERN_INFO "oprofile: forcing timer to be returned "
59 "as cpu type\n");
60 } else {
61 force_cpu_type = 0;
62 }
63
64 return 0;
65}
66module_param_call(cpu_type, set_cpu_type, NULL, NULL, 0);
67MODULE_PARM_DESC(cpu_type, "Force legacy basic mode sampling"
68 "(report cpu_type \"timer\"");
69
Heinz Graalfsc814d162011-02-15 13:02:14 -050070static int oprofile_hwsampler_start(void)
71{
72 int retval;
73
Andreas Krebbeldd3c4672011-11-25 20:03:05 +010074 hwsampler_running = hwsampler_enabled;
Heinz Graalfsc814d162011-02-15 13:02:14 -050075
76 if (!hwsampler_running)
77 return timer_ops.start();
78
79 retval = hwsampler_allocate(oprofile_sdbt_blocks, oprofile_sdb_blocks);
80 if (retval)
81 return retval;
82
83 retval = hwsampler_start_all(oprofile_hw_interval);
84 if (retval)
85 hwsampler_deallocate();
86
87 return retval;
88}
89
90static void oprofile_hwsampler_stop(void)
91{
92 if (!hwsampler_running) {
93 timer_ops.stop();
94 return;
95 }
96
97 hwsampler_stop_all();
98 hwsampler_deallocate();
99 return;
100}
101
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100102/*
103 * File ops used for:
104 * /dev/oprofile/0/enabled
105 * /dev/oprofile/hwsampling/hwsampler (cpu_type = timer)
106 */
107
Heinz Graalfsc814d162011-02-15 13:02:14 -0500108static ssize_t hwsampler_read(struct file *file, char __user *buf,
109 size_t count, loff_t *offset)
110{
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100111 return oprofilefs_ulong_to_user(hwsampler_enabled, buf, count, offset);
Heinz Graalfsc814d162011-02-15 13:02:14 -0500112}
113
114static ssize_t hwsampler_write(struct file *file, char const __user *buf,
115 size_t count, loff_t *offset)
116{
117 unsigned long val;
118 int retval;
119
120 if (*offset)
121 return -EINVAL;
122
123 retval = oprofilefs_ulong_from_user(&val, buf, count);
Robert Richter913050b2011-12-19 16:38:30 +0100124 if (retval <= 0)
Heinz Graalfsc814d162011-02-15 13:02:14 -0500125 return retval;
126
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100127 if (val != 0 && val != 1)
128 return -EINVAL;
129
Heinz Graalfsc814d162011-02-15 13:02:14 -0500130 if (oprofile_started)
131 /*
132 * save to do without locking as we set
133 * hwsampler_running in start() when start_mutex is
134 * held
135 */
136 return -EBUSY;
137
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100138 hwsampler_enabled = val;
Heinz Graalfsc814d162011-02-15 13:02:14 -0500139
140 return count;
141}
142
143static const struct file_operations hwsampler_fops = {
144 .read = hwsampler_read,
145 .write = hwsampler_write,
146};
147
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100148/*
149 * File ops used for:
150 * /dev/oprofile/0/count
151 * /dev/oprofile/hwsampling/hw_interval (cpu_type = timer)
152 *
153 * Make sure that the value is within the hardware range.
154 */
155
156static ssize_t hw_interval_read(struct file *file, char __user *buf,
157 size_t count, loff_t *offset)
Heinz Graalfsc814d162011-02-15 13:02:14 -0500158{
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100159 return oprofilefs_ulong_to_user(oprofile_hw_interval, buf,
160 count, offset);
161}
Heinz Graalfsc814d162011-02-15 13:02:14 -0500162
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100163static ssize_t hw_interval_write(struct file *file, char const __user *buf,
164 size_t count, loff_t *offset)
165{
166 unsigned long val;
167 int retval;
Heinz Graalfsc814d162011-02-15 13:02:14 -0500168
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100169 if (*offset)
170 return -EINVAL;
171 retval = oprofilefs_ulong_from_user(&val, buf, count);
Robert Richter81ff3472012-07-19 18:28:26 +0200172 if (retval <= 0)
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100173 return retval;
174 if (val < oprofile_min_interval)
175 oprofile_hw_interval = oprofile_min_interval;
176 else if (val > oprofile_max_interval)
177 oprofile_hw_interval = oprofile_max_interval;
178 else
179 oprofile_hw_interval = val;
180
181 return count;
182}
183
184static const struct file_operations hw_interval_fops = {
185 .read = hw_interval_read,
186 .write = hw_interval_write,
187};
188
189/*
190 * File ops used for:
191 * /dev/oprofile/0/event
192 * Only a single event with number 0 is supported with this counter.
193 *
194 * /dev/oprofile/0/unit_mask
195 * This is a dummy file needed by the user space tools.
196 * No value other than 0 is accepted or returned.
197 */
198
199static ssize_t hwsampler_zero_read(struct file *file, char __user *buf,
200 size_t count, loff_t *offset)
201{
202 return oprofilefs_ulong_to_user(0, buf, count, offset);
203}
204
205static ssize_t hwsampler_zero_write(struct file *file, char const __user *buf,
206 size_t count, loff_t *offset)
207{
208 unsigned long val;
209 int retval;
210
211 if (*offset)
Heinz Graalfsc814d162011-02-15 13:02:14 -0500212 return -EINVAL;
213
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100214 retval = oprofilefs_ulong_from_user(&val, buf, count);
Robert Richter81ff3472012-07-19 18:28:26 +0200215 if (retval <= 0)
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100216 return retval;
217 if (val != 0)
218 return -EINVAL;
219 return count;
220}
Heinz Graalfsc814d162011-02-15 13:02:14 -0500221
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100222static const struct file_operations zero_fops = {
223 .read = hwsampler_zero_read,
224 .write = hwsampler_zero_write,
225};
226
227/* /dev/oprofile/0/kernel file ops. */
228
229static ssize_t hwsampler_kernel_read(struct file *file, char __user *buf,
230 size_t count, loff_t *offset)
231{
232 return oprofilefs_ulong_to_user(counter_config.kernel,
233 buf, count, offset);
234}
235
236static ssize_t hwsampler_kernel_write(struct file *file, char const __user *buf,
237 size_t count, loff_t *offset)
238{
239 unsigned long val;
240 int retval;
241
242 if (*offset)
243 return -EINVAL;
244
245 retval = oprofilefs_ulong_from_user(&val, buf, count);
Robert Richter81ff3472012-07-19 18:28:26 +0200246 if (retval <= 0)
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100247 return retval;
248
249 if (val != 0 && val != 1)
250 return -EINVAL;
251
252 counter_config.kernel = val;
253
254 return count;
255}
256
257static const struct file_operations kernel_fops = {
258 .read = hwsampler_kernel_read,
259 .write = hwsampler_kernel_write,
260};
261
262/* /dev/oprofile/0/user file ops. */
263
264static ssize_t hwsampler_user_read(struct file *file, char __user *buf,
265 size_t count, loff_t *offset)
266{
267 return oprofilefs_ulong_to_user(counter_config.user,
268 buf, count, offset);
269}
270
271static ssize_t hwsampler_user_write(struct file *file, char const __user *buf,
272 size_t count, loff_t *offset)
273{
274 unsigned long val;
275 int retval;
276
277 if (*offset)
278 return -EINVAL;
279
280 retval = oprofilefs_ulong_from_user(&val, buf, count);
Robert Richter81ff3472012-07-19 18:28:26 +0200281 if (retval <= 0)
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100282 return retval;
283
284 if (val != 0 && val != 1)
285 return -EINVAL;
286
287 counter_config.user = val;
288
289 return count;
290}
291
292static const struct file_operations user_fops = {
293 .read = hwsampler_user_read,
294 .write = hwsampler_user_write,
295};
296
297
298/*
299 * File ops used for: /dev/oprofile/timer/enabled
300 * The value always has to be the inverted value of hwsampler_enabled. So
301 * no separate variable is created. That way we do not need locking.
302 */
303
304static ssize_t timer_enabled_read(struct file *file, char __user *buf,
305 size_t count, loff_t *offset)
306{
307 return oprofilefs_ulong_to_user(!hwsampler_enabled, buf, count, offset);
308}
309
310static ssize_t timer_enabled_write(struct file *file, char const __user *buf,
311 size_t count, loff_t *offset)
312{
313 unsigned long val;
314 int retval;
315
316 if (*offset)
317 return -EINVAL;
318
319 retval = oprofilefs_ulong_from_user(&val, buf, count);
Robert Richter81ff3472012-07-19 18:28:26 +0200320 if (retval <= 0)
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100321 return retval;
322
323 if (val != 0 && val != 1)
324 return -EINVAL;
325
326 /* Timer cannot be disabled without having hardware sampling. */
327 if (val == 0 && !hwsampler_available)
328 return -EINVAL;
329
330 if (oprofile_started)
331 /*
332 * save to do without locking as we set
333 * hwsampler_running in start() when start_mutex is
334 * held
335 */
336 return -EBUSY;
337
338 hwsampler_enabled = !val;
339
340 return count;
341}
342
343static const struct file_operations timer_enabled_fops = {
344 .read = timer_enabled_read,
345 .write = timer_enabled_write,
346};
347
348
Al Viroef7bca12013-07-19 15:52:42 +0400349static int oprofile_create_hwsampling_files(struct dentry *root)
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100350{
351 struct dentry *dir;
352
Al Viroecde2822013-07-19 15:58:27 +0400353 dir = oprofilefs_mkdir(root, "timer");
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100354 if (!dir)
355 return -EINVAL;
356
Al Viro6af4ea02013-07-19 16:10:36 +0400357 oprofilefs_create_file(dir, "enabled", &timer_enabled_fops);
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100358
359 if (!hwsampler_available)
360 return 0;
361
362 /* reinitialize default values */
363 hwsampler_enabled = 1;
364 counter_config.kernel = 1;
365 counter_config.user = 1;
366
367 if (!force_cpu_type) {
368 /*
369 * Create the counter file system. A single virtual
370 * counter is created which can be used to
371 * enable/disable hardware sampling dynamically from
372 * user space. The user space will configure a single
373 * counter with a single event. The value of 'event'
374 * and 'unit_mask' are not evaluated by the kernel code
375 * and can only be set to 0.
376 */
377
Al Viroecde2822013-07-19 15:58:27 +0400378 dir = oprofilefs_mkdir(root, "0");
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100379 if (!dir)
380 return -EINVAL;
381
Al Viro6af4ea02013-07-19 16:10:36 +0400382 oprofilefs_create_file(dir, "enabled", &hwsampler_fops);
383 oprofilefs_create_file(dir, "event", &zero_fops);
384 oprofilefs_create_file(dir, "count", &hw_interval_fops);
385 oprofilefs_create_file(dir, "unit_mask", &zero_fops);
386 oprofilefs_create_file(dir, "kernel", &kernel_fops);
387 oprofilefs_create_file(dir, "user", &user_fops);
388 oprofilefs_create_ulong(dir, "hw_sdbt_blocks",
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100389 &oprofile_sdbt_blocks);
390
391 } else {
392 /*
393 * Hardware sampling can be used but the cpu_type is
394 * forced to timer in order to deal with legacy user
395 * space tools. The /dev/oprofile/hwsampling fs is
396 * provided in that case.
397 */
Al Viroecde2822013-07-19 15:58:27 +0400398 dir = oprofilefs_mkdir(root, "hwsampling");
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100399 if (!dir)
400 return -EINVAL;
401
Al Viro6af4ea02013-07-19 16:10:36 +0400402 oprofilefs_create_file(dir, "hwsampler",
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100403 &hwsampler_fops);
Al Viro6af4ea02013-07-19 16:10:36 +0400404 oprofilefs_create_file(dir, "hw_interval",
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100405 &hw_interval_fops);
Al Viro6af4ea02013-07-19 16:10:36 +0400406 oprofilefs_create_ro_ulong(dir, "hw_min_interval",
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100407 &oprofile_min_interval);
Al Viro6af4ea02013-07-19 16:10:36 +0400408 oprofilefs_create_ro_ulong(dir, "hw_max_interval",
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100409 &oprofile_max_interval);
Al Viro6af4ea02013-07-19 16:10:36 +0400410 oprofilefs_create_ulong(dir, "hw_sdbt_blocks",
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100411 &oprofile_sdbt_blocks);
412 }
Heinz Graalfsc814d162011-02-15 13:02:14 -0500413 return 0;
414}
415
Robert Richterec6b4262011-03-16 12:10:12 +0100416static int oprofile_hwsampler_init(struct oprofile_operations *ops)
Heinz Graalfsc814d162011-02-15 13:02:14 -0500417{
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100418 /*
419 * Initialize the timer mode infrastructure as well in order
420 * to be able to switch back dynamically. oprofile_timer_init
421 * is not supposed to fail.
422 */
423 if (oprofile_timer_init(ops))
424 BUG();
425
426 memcpy(&timer_ops, ops, sizeof(timer_ops));
427 ops->create_files = oprofile_create_hwsampling_files;
428
429 /*
430 * If the user space tools do not support newer cpu types,
431 * the force_cpu_type module parameter
432 * can be used to always return \"timer\" as cpu type.
433 */
434 if (force_cpu_type != timer) {
435 struct cpuid id;
436
437 get_cpu_id (&id);
438
439 switch (id.machine) {
440 case 0x2097: case 0x2098: ops->cpu_type = "s390/z10"; break;
441 case 0x2817: case 0x2818: ops->cpu_type = "s390/z196"; break;
Heiko Carstens59471222013-07-24 10:35:33 +0200442 case 0x2827: case 0x2828: ops->cpu_type = "s390/zEC12"; break;
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100443 default: return -ENODEV;
444 }
445 }
446
Heinz Graalfsc814d162011-02-15 13:02:14 -0500447 if (hwsampler_setup())
448 return -ENODEV;
449
450 /*
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100451 * Query the range for the sampling interval from the
452 * hardware.
Heinz Graalfsc814d162011-02-15 13:02:14 -0500453 */
454 oprofile_min_interval = hwsampler_query_min_interval();
Martin Schwidefsky3d8dcb32011-05-10 17:13:42 +0200455 if (oprofile_min_interval == 0)
Heinz Graalfsc814d162011-02-15 13:02:14 -0500456 return -ENODEV;
Heinz Graalfsc814d162011-02-15 13:02:14 -0500457 oprofile_max_interval = hwsampler_query_max_interval();
Martin Schwidefsky3d8dcb32011-05-10 17:13:42 +0200458 if (oprofile_max_interval == 0)
Heinz Graalfsc814d162011-02-15 13:02:14 -0500459 return -ENODEV;
Heinz Graalfsc814d162011-02-15 13:02:14 -0500460
Christian Borntraegerb530ce72011-06-22 16:24:08 +0200461 /* The initial value should be sane */
462 if (oprofile_hw_interval < oprofile_min_interval)
463 oprofile_hw_interval = oprofile_min_interval;
464 if (oprofile_hw_interval > oprofile_max_interval)
465 oprofile_hw_interval = oprofile_max_interval;
466
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100467 printk(KERN_INFO "oprofile: System z hardware sampling "
468 "facility found.\n");
Heinz Graalfsc814d162011-02-15 13:02:14 -0500469
470 ops->start = oprofile_hwsampler_start;
471 ops->stop = oprofile_hwsampler_stop;
Heinz Graalfsc814d162011-02-15 13:02:14 -0500472
473 return 0;
474}
475
Robert Richterec6b4262011-03-16 12:10:12 +0100476static void oprofile_hwsampler_exit(void)
Heinz Graalfsc814d162011-02-15 13:02:14 -0500477{
Heinz Graalfsc814d162011-02-15 13:02:14 -0500478 hwsampler_shutdown();
479}
480
Heiko Carstens984e8482011-03-23 10:15:00 +0100481#endif /* CONFIG_64BIT */
482
Robert Richterec6b4262011-03-16 12:10:12 +0100483int __init oprofile_arch_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Andreas Krebbeld0f4c162006-01-06 00:19:16 -0800485 ops->backtrace = s390_backtrace;
Heinz Graalfs997dbb42011-01-21 10:06:53 +0000486
Heiko Carstens984e8482011-03-23 10:15:00 +0100487#ifdef CONFIG_64BIT
Andreas Krebbeldd3c4672011-11-25 20:03:05 +0100488
489 /*
490 * -ENODEV is not reported to the caller. The module itself
491 * will use the timer mode sampling as fallback and this is
492 * always available.
493 */
494 hwsampler_available = oprofile_hwsampler_init(ops) == 0;
495
496 return 0;
Heiko Carstens984e8482011-03-23 10:15:00 +0100497#else
498 return -ENODEV;
499#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502void oprofile_arch_exit(void)
503{
Heiko Carstens984e8482011-03-23 10:15:00 +0100504#ifdef CONFIG_64BIT
Heinz Graalfs997dbb42011-01-21 10:06:53 +0000505 oprofile_hwsampler_exit();
Heiko Carstens984e8482011-03-23 10:15:00 +0100506#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}