3 * Copyright IBM Corp. 2002, 2011
4 * Author(s): Thomas Spatzier (tspat@de.ibm.com)
5 * Author(s): Mahesh Salgaonkar (mahesh@linux.vnet.ibm.com)
6 * Author(s): Heinz Graalfs (graalfs@linux.vnet.ibm.com)
7 * Author(s): Andreas Krebbel (krebbel@linux.vnet.ibm.com)
9 * @remark Copyright 2002-2011 OProfile authors
12 #include <linux/oprofile.h>
13 #include <linux/perf_event.h>
14 #include <linux/init.h>
15 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <asm/processor.h>
19 #include <asm/perf_event.h>
21 #include "../../../drivers/oprofile/oprof.h"
23 extern void s390_backtrace(struct pt_regs
* const regs
, unsigned int depth
);
25 #include "hwsampler.h"
26 #include "op_counter.h"
28 #define DEFAULT_INTERVAL 4127518
30 #define DEFAULT_SDBT_BLOCKS 1
31 #define DEFAULT_SDB_BLOCKS 511
33 static unsigned long oprofile_hw_interval
= DEFAULT_INTERVAL
;
34 static unsigned long oprofile_min_interval
;
35 static unsigned long oprofile_max_interval
;
37 static unsigned long oprofile_sdbt_blocks
= DEFAULT_SDBT_BLOCKS
;
38 static unsigned long oprofile_sdb_blocks
= DEFAULT_SDB_BLOCKS
;
40 static int hwsampler_enabled
;
41 static int hwsampler_running
; /* start_mutex must be held to change */
42 static int hwsampler_available
;
44 static struct oprofile_operations timer_ops
;
46 struct op_counter_config counter_config
;
48 enum __force_cpu_type
{
49 reserved
= 0, /* do not force */
52 static int force_cpu_type
;
54 static int set_cpu_type(const char *str
, struct kernel_param
*kp
)
56 if (!strcmp(str
, "timer")) {
57 force_cpu_type
= timer
;
58 printk(KERN_INFO
"oprofile: forcing timer to be returned "
66 module_param_call(cpu_type
, set_cpu_type
, NULL
, NULL
, 0);
67 MODULE_PARM_DESC(cpu_type
, "Force legacy basic mode sampling"
68 "(report cpu_type \"timer\"");
70 static int __oprofile_hwsampler_start(void)
74 retval
= hwsampler_allocate(oprofile_sdbt_blocks
, oprofile_sdb_blocks
);
78 retval
= hwsampler_start_all(oprofile_hw_interval
);
80 hwsampler_deallocate();
85 static int oprofile_hwsampler_start(void)
89 hwsampler_running
= hwsampler_enabled
;
91 if (!hwsampler_running
)
92 return timer_ops
.start();
94 retval
= perf_reserve_sampling();
98 retval
= __oprofile_hwsampler_start();
100 perf_release_sampling();
105 static void oprofile_hwsampler_stop(void)
107 if (!hwsampler_running
) {
112 hwsampler_stop_all();
113 hwsampler_deallocate();
114 perf_release_sampling();
120 * /dev/oprofile/0/enabled
121 * /dev/oprofile/hwsampling/hwsampler (cpu_type = timer)
124 static ssize_t
hwsampler_read(struct file
*file
, char __user
*buf
,
125 size_t count
, loff_t
*offset
)
127 return oprofilefs_ulong_to_user(hwsampler_enabled
, buf
, count
, offset
);
130 static ssize_t
hwsampler_write(struct file
*file
, char const __user
*buf
,
131 size_t count
, loff_t
*offset
)
139 retval
= oprofilefs_ulong_from_user(&val
, buf
, count
);
143 if (val
!= 0 && val
!= 1)
146 if (oprofile_started
)
148 * save to do without locking as we set
149 * hwsampler_running in start() when start_mutex is
154 hwsampler_enabled
= val
;
159 static const struct file_operations hwsampler_fops
= {
160 .read
= hwsampler_read
,
161 .write
= hwsampler_write
,
166 * /dev/oprofile/0/count
167 * /dev/oprofile/hwsampling/hw_interval (cpu_type = timer)
169 * Make sure that the value is within the hardware range.
172 static ssize_t
hw_interval_read(struct file
*file
, char __user
*buf
,
173 size_t count
, loff_t
*offset
)
175 return oprofilefs_ulong_to_user(oprofile_hw_interval
, buf
,
179 static ssize_t
hw_interval_write(struct file
*file
, char const __user
*buf
,
180 size_t count
, loff_t
*offset
)
187 retval
= oprofilefs_ulong_from_user(&val
, buf
, count
);
190 if (val
< oprofile_min_interval
)
191 oprofile_hw_interval
= oprofile_min_interval
;
192 else if (val
> oprofile_max_interval
)
193 oprofile_hw_interval
= oprofile_max_interval
;
195 oprofile_hw_interval
= val
;
200 static const struct file_operations hw_interval_fops
= {
201 .read
= hw_interval_read
,
202 .write
= hw_interval_write
,
207 * /dev/oprofile/0/event
208 * Only a single event with number 0 is supported with this counter.
210 * /dev/oprofile/0/unit_mask
211 * This is a dummy file needed by the user space tools.
212 * No value other than 0 is accepted or returned.
215 static ssize_t
hwsampler_zero_read(struct file
*file
, char __user
*buf
,
216 size_t count
, loff_t
*offset
)
218 return oprofilefs_ulong_to_user(0, buf
, count
, offset
);
221 static ssize_t
hwsampler_zero_write(struct file
*file
, char const __user
*buf
,
222 size_t count
, loff_t
*offset
)
230 retval
= oprofilefs_ulong_from_user(&val
, buf
, count
);
238 static const struct file_operations zero_fops
= {
239 .read
= hwsampler_zero_read
,
240 .write
= hwsampler_zero_write
,
243 /* /dev/oprofile/0/kernel file ops. */
245 static ssize_t
hwsampler_kernel_read(struct file
*file
, char __user
*buf
,
246 size_t count
, loff_t
*offset
)
248 return oprofilefs_ulong_to_user(counter_config
.kernel
,
252 static ssize_t
hwsampler_kernel_write(struct file
*file
, char const __user
*buf
,
253 size_t count
, loff_t
*offset
)
261 retval
= oprofilefs_ulong_from_user(&val
, buf
, count
);
265 if (val
!= 0 && val
!= 1)
268 counter_config
.kernel
= val
;
273 static const struct file_operations kernel_fops
= {
274 .read
= hwsampler_kernel_read
,
275 .write
= hwsampler_kernel_write
,
278 /* /dev/oprofile/0/user file ops. */
280 static ssize_t
hwsampler_user_read(struct file
*file
, char __user
*buf
,
281 size_t count
, loff_t
*offset
)
283 return oprofilefs_ulong_to_user(counter_config
.user
,
287 static ssize_t
hwsampler_user_write(struct file
*file
, char const __user
*buf
,
288 size_t count
, loff_t
*offset
)
296 retval
= oprofilefs_ulong_from_user(&val
, buf
, count
);
300 if (val
!= 0 && val
!= 1)
303 counter_config
.user
= val
;
308 static const struct file_operations user_fops
= {
309 .read
= hwsampler_user_read
,
310 .write
= hwsampler_user_write
,
315 * File ops used for: /dev/oprofile/timer/enabled
316 * The value always has to be the inverted value of hwsampler_enabled. So
317 * no separate variable is created. That way we do not need locking.
320 static ssize_t
timer_enabled_read(struct file
*file
, char __user
*buf
,
321 size_t count
, loff_t
*offset
)
323 return oprofilefs_ulong_to_user(!hwsampler_enabled
, buf
, count
, offset
);
326 static ssize_t
timer_enabled_write(struct file
*file
, char const __user
*buf
,
327 size_t count
, loff_t
*offset
)
335 retval
= oprofilefs_ulong_from_user(&val
, buf
, count
);
339 if (val
!= 0 && val
!= 1)
342 /* Timer cannot be disabled without having hardware sampling. */
343 if (val
== 0 && !hwsampler_available
)
346 if (oprofile_started
)
348 * save to do without locking as we set
349 * hwsampler_running in start() when start_mutex is
354 hwsampler_enabled
= !val
;
359 static const struct file_operations timer_enabled_fops
= {
360 .read
= timer_enabled_read
,
361 .write
= timer_enabled_write
,
365 static int oprofile_create_hwsampling_files(struct dentry
*root
)
369 dir
= oprofilefs_mkdir(root
, "timer");
373 oprofilefs_create_file(dir
, "enabled", &timer_enabled_fops
);
375 if (!hwsampler_available
)
378 /* reinitialize default values */
379 hwsampler_enabled
= 1;
380 counter_config
.kernel
= 1;
381 counter_config
.user
= 1;
383 if (!force_cpu_type
) {
385 * Create the counter file system. A single virtual
386 * counter is created which can be used to
387 * enable/disable hardware sampling dynamically from
388 * user space. The user space will configure a single
389 * counter with a single event. The value of 'event'
390 * and 'unit_mask' are not evaluated by the kernel code
391 * and can only be set to 0.
394 dir
= oprofilefs_mkdir(root
, "0");
398 oprofilefs_create_file(dir
, "enabled", &hwsampler_fops
);
399 oprofilefs_create_file(dir
, "event", &zero_fops
);
400 oprofilefs_create_file(dir
, "count", &hw_interval_fops
);
401 oprofilefs_create_file(dir
, "unit_mask", &zero_fops
);
402 oprofilefs_create_file(dir
, "kernel", &kernel_fops
);
403 oprofilefs_create_file(dir
, "user", &user_fops
);
404 oprofilefs_create_ulong(dir
, "hw_sdbt_blocks",
405 &oprofile_sdbt_blocks
);
409 * Hardware sampling can be used but the cpu_type is
410 * forced to timer in order to deal with legacy user
411 * space tools. The /dev/oprofile/hwsampling fs is
412 * provided in that case.
414 dir
= oprofilefs_mkdir(root
, "hwsampling");
418 oprofilefs_create_file(dir
, "hwsampler",
420 oprofilefs_create_file(dir
, "hw_interval",
422 oprofilefs_create_ro_ulong(dir
, "hw_min_interval",
423 &oprofile_min_interval
);
424 oprofilefs_create_ro_ulong(dir
, "hw_max_interval",
425 &oprofile_max_interval
);
426 oprofilefs_create_ulong(dir
, "hw_sdbt_blocks",
427 &oprofile_sdbt_blocks
);
432 static int oprofile_hwsampler_init(struct oprofile_operations
*ops
)
435 * Initialize the timer mode infrastructure as well in order
436 * to be able to switch back dynamically. oprofile_timer_init
437 * is not supposed to fail.
439 if (oprofile_timer_init(ops
))
442 memcpy(&timer_ops
, ops
, sizeof(timer_ops
));
443 ops
->create_files
= oprofile_create_hwsampling_files
;
446 * If the user space tools do not support newer cpu types,
447 * the force_cpu_type module parameter
448 * can be used to always return \"timer\" as cpu type.
450 if (force_cpu_type
!= timer
) {
455 switch (id
.machine
) {
456 case 0x2097: case 0x2098: ops
->cpu_type
= "s390/z10"; break;
457 case 0x2817: case 0x2818: ops
->cpu_type
= "s390/z196"; break;
458 case 0x2827: case 0x2828: ops
->cpu_type
= "s390/zEC12"; break;
459 default: return -ENODEV
;
463 if (hwsampler_setup())
467 * Query the range for the sampling interval from the
470 oprofile_min_interval
= hwsampler_query_min_interval();
471 if (oprofile_min_interval
== 0)
473 oprofile_max_interval
= hwsampler_query_max_interval();
474 if (oprofile_max_interval
== 0)
477 /* The initial value should be sane */
478 if (oprofile_hw_interval
< oprofile_min_interval
)
479 oprofile_hw_interval
= oprofile_min_interval
;
480 if (oprofile_hw_interval
> oprofile_max_interval
)
481 oprofile_hw_interval
= oprofile_max_interval
;
483 printk(KERN_INFO
"oprofile: System z hardware sampling "
484 "facility found.\n");
486 ops
->start
= oprofile_hwsampler_start
;
487 ops
->stop
= oprofile_hwsampler_stop
;
492 static void oprofile_hwsampler_exit(void)
494 hwsampler_shutdown();
497 int __init
oprofile_arch_init(struct oprofile_operations
*ops
)
499 ops
->backtrace
= s390_backtrace
;
502 * -ENODEV is not reported to the caller. The module itself
503 * will use the timer mode sampling as fallback and this is
506 hwsampler_available
= oprofile_hwsampler_init(ops
) == 0;
511 void oprofile_arch_exit(void)
513 oprofile_hwsampler_exit();