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>
20 #include "../../../drivers/oprofile/oprof.h"
22 extern void s390_backtrace(struct pt_regs
* const regs
, unsigned int depth
);
26 #include "hwsampler.h"
27 #include "op_counter.h"
29 #define DEFAULT_INTERVAL 4127518
31 #define DEFAULT_SDBT_BLOCKS 1
32 #define DEFAULT_SDB_BLOCKS 511
34 static unsigned long oprofile_hw_interval
= DEFAULT_INTERVAL
;
35 static unsigned long oprofile_min_interval
;
36 static unsigned long oprofile_max_interval
;
38 static unsigned long oprofile_sdbt_blocks
= DEFAULT_SDBT_BLOCKS
;
39 static unsigned long oprofile_sdb_blocks
= DEFAULT_SDB_BLOCKS
;
41 static int hwsampler_enabled
;
42 static int hwsampler_running
; /* start_mutex must be held to change */
43 static int hwsampler_available
;
45 static struct oprofile_operations timer_ops
;
47 struct op_counter_config counter_config
;
49 enum __force_cpu_type
{
50 reserved
= 0, /* do not force */
53 static int force_cpu_type
;
55 static int set_cpu_type(const char *str
, struct kernel_param
*kp
)
57 if (!strcmp(str
, "timer")) {
58 force_cpu_type
= timer
;
59 printk(KERN_INFO
"oprofile: forcing timer to be returned "
67 module_param_call(cpu_type
, set_cpu_type
, NULL
, NULL
, 0);
68 MODULE_PARM_DESC(cpu_type
, "Force legacy basic mode sampling"
69 "(report cpu_type \"timer\"");
71 static int __oprofile_hwsampler_start(void)
75 retval
= hwsampler_allocate(oprofile_sdbt_blocks
, oprofile_sdb_blocks
);
79 retval
= hwsampler_start_all(oprofile_hw_interval
);
81 hwsampler_deallocate();
86 static int oprofile_hwsampler_start(void)
90 hwsampler_running
= hwsampler_enabled
;
92 if (!hwsampler_running
)
93 return timer_ops
.start();
95 retval
= perf_reserve_sampling();
99 retval
= __oprofile_hwsampler_start();
101 perf_release_sampling();
106 static void oprofile_hwsampler_stop(void)
108 if (!hwsampler_running
) {
113 hwsampler_stop_all();
114 hwsampler_deallocate();
115 perf_release_sampling();
121 * /dev/oprofile/0/enabled
122 * /dev/oprofile/hwsampling/hwsampler (cpu_type = timer)
125 static ssize_t
hwsampler_read(struct file
*file
, char __user
*buf
,
126 size_t count
, loff_t
*offset
)
128 return oprofilefs_ulong_to_user(hwsampler_enabled
, buf
, count
, offset
);
131 static ssize_t
hwsampler_write(struct file
*file
, char const __user
*buf
,
132 size_t count
, loff_t
*offset
)
140 retval
= oprofilefs_ulong_from_user(&val
, buf
, count
);
144 if (val
!= 0 && val
!= 1)
147 if (oprofile_started
)
149 * save to do without locking as we set
150 * hwsampler_running in start() when start_mutex is
155 hwsampler_enabled
= val
;
160 static const struct file_operations hwsampler_fops
= {
161 .read
= hwsampler_read
,
162 .write
= hwsampler_write
,
167 * /dev/oprofile/0/count
168 * /dev/oprofile/hwsampling/hw_interval (cpu_type = timer)
170 * Make sure that the value is within the hardware range.
173 static ssize_t
hw_interval_read(struct file
*file
, char __user
*buf
,
174 size_t count
, loff_t
*offset
)
176 return oprofilefs_ulong_to_user(oprofile_hw_interval
, buf
,
180 static ssize_t
hw_interval_write(struct file
*file
, char const __user
*buf
,
181 size_t count
, loff_t
*offset
)
188 retval
= oprofilefs_ulong_from_user(&val
, buf
, count
);
191 if (val
< oprofile_min_interval
)
192 oprofile_hw_interval
= oprofile_min_interval
;
193 else if (val
> oprofile_max_interval
)
194 oprofile_hw_interval
= oprofile_max_interval
;
196 oprofile_hw_interval
= val
;
201 static const struct file_operations hw_interval_fops
= {
202 .read
= hw_interval_read
,
203 .write
= hw_interval_write
,
208 * /dev/oprofile/0/event
209 * Only a single event with number 0 is supported with this counter.
211 * /dev/oprofile/0/unit_mask
212 * This is a dummy file needed by the user space tools.
213 * No value other than 0 is accepted or returned.
216 static ssize_t
hwsampler_zero_read(struct file
*file
, char __user
*buf
,
217 size_t count
, loff_t
*offset
)
219 return oprofilefs_ulong_to_user(0, buf
, count
, offset
);
222 static ssize_t
hwsampler_zero_write(struct file
*file
, char const __user
*buf
,
223 size_t count
, loff_t
*offset
)
231 retval
= oprofilefs_ulong_from_user(&val
, buf
, count
);
239 static const struct file_operations zero_fops
= {
240 .read
= hwsampler_zero_read
,
241 .write
= hwsampler_zero_write
,
244 /* /dev/oprofile/0/kernel file ops. */
246 static ssize_t
hwsampler_kernel_read(struct file
*file
, char __user
*buf
,
247 size_t count
, loff_t
*offset
)
249 return oprofilefs_ulong_to_user(counter_config
.kernel
,
253 static ssize_t
hwsampler_kernel_write(struct file
*file
, char const __user
*buf
,
254 size_t count
, loff_t
*offset
)
262 retval
= oprofilefs_ulong_from_user(&val
, buf
, count
);
266 if (val
!= 0 && val
!= 1)
269 counter_config
.kernel
= val
;
274 static const struct file_operations kernel_fops
= {
275 .read
= hwsampler_kernel_read
,
276 .write
= hwsampler_kernel_write
,
279 /* /dev/oprofile/0/user file ops. */
281 static ssize_t
hwsampler_user_read(struct file
*file
, char __user
*buf
,
282 size_t count
, loff_t
*offset
)
284 return oprofilefs_ulong_to_user(counter_config
.user
,
288 static ssize_t
hwsampler_user_write(struct file
*file
, char const __user
*buf
,
289 size_t count
, loff_t
*offset
)
297 retval
= oprofilefs_ulong_from_user(&val
, buf
, count
);
301 if (val
!= 0 && val
!= 1)
304 counter_config
.user
= val
;
309 static const struct file_operations user_fops
= {
310 .read
= hwsampler_user_read
,
311 .write
= hwsampler_user_write
,
316 * File ops used for: /dev/oprofile/timer/enabled
317 * The value always has to be the inverted value of hwsampler_enabled. So
318 * no separate variable is created. That way we do not need locking.
321 static ssize_t
timer_enabled_read(struct file
*file
, char __user
*buf
,
322 size_t count
, loff_t
*offset
)
324 return oprofilefs_ulong_to_user(!hwsampler_enabled
, buf
, count
, offset
);
327 static ssize_t
timer_enabled_write(struct file
*file
, char const __user
*buf
,
328 size_t count
, loff_t
*offset
)
336 retval
= oprofilefs_ulong_from_user(&val
, buf
, count
);
340 if (val
!= 0 && val
!= 1)
343 /* Timer cannot be disabled without having hardware sampling. */
344 if (val
== 0 && !hwsampler_available
)
347 if (oprofile_started
)
349 * save to do without locking as we set
350 * hwsampler_running in start() when start_mutex is
355 hwsampler_enabled
= !val
;
360 static const struct file_operations timer_enabled_fops
= {
361 .read
= timer_enabled_read
,
362 .write
= timer_enabled_write
,
366 static int oprofile_create_hwsampling_files(struct dentry
*root
)
370 dir
= oprofilefs_mkdir(root
, "timer");
374 oprofilefs_create_file(dir
, "enabled", &timer_enabled_fops
);
376 if (!hwsampler_available
)
379 /* reinitialize default values */
380 hwsampler_enabled
= 1;
381 counter_config
.kernel
= 1;
382 counter_config
.user
= 1;
384 if (!force_cpu_type
) {
386 * Create the counter file system. A single virtual
387 * counter is created which can be used to
388 * enable/disable hardware sampling dynamically from
389 * user space. The user space will configure a single
390 * counter with a single event. The value of 'event'
391 * and 'unit_mask' are not evaluated by the kernel code
392 * and can only be set to 0.
395 dir
= oprofilefs_mkdir(root
, "0");
399 oprofilefs_create_file(dir
, "enabled", &hwsampler_fops
);
400 oprofilefs_create_file(dir
, "event", &zero_fops
);
401 oprofilefs_create_file(dir
, "count", &hw_interval_fops
);
402 oprofilefs_create_file(dir
, "unit_mask", &zero_fops
);
403 oprofilefs_create_file(dir
, "kernel", &kernel_fops
);
404 oprofilefs_create_file(dir
, "user", &user_fops
);
405 oprofilefs_create_ulong(dir
, "hw_sdbt_blocks",
406 &oprofile_sdbt_blocks
);
410 * Hardware sampling can be used but the cpu_type is
411 * forced to timer in order to deal with legacy user
412 * space tools. The /dev/oprofile/hwsampling fs is
413 * provided in that case.
415 dir
= oprofilefs_mkdir(root
, "hwsampling");
419 oprofilefs_create_file(dir
, "hwsampler",
421 oprofilefs_create_file(dir
, "hw_interval",
423 oprofilefs_create_ro_ulong(dir
, "hw_min_interval",
424 &oprofile_min_interval
);
425 oprofilefs_create_ro_ulong(dir
, "hw_max_interval",
426 &oprofile_max_interval
);
427 oprofilefs_create_ulong(dir
, "hw_sdbt_blocks",
428 &oprofile_sdbt_blocks
);
433 static int oprofile_hwsampler_init(struct oprofile_operations
*ops
)
436 * Initialize the timer mode infrastructure as well in order
437 * to be able to switch back dynamically. oprofile_timer_init
438 * is not supposed to fail.
440 if (oprofile_timer_init(ops
))
443 memcpy(&timer_ops
, ops
, sizeof(timer_ops
));
444 ops
->create_files
= oprofile_create_hwsampling_files
;
447 * If the user space tools do not support newer cpu types,
448 * the force_cpu_type module parameter
449 * can be used to always return \"timer\" as cpu type.
451 if (force_cpu_type
!= timer
) {
456 switch (id
.machine
) {
457 case 0x2097: case 0x2098: ops
->cpu_type
= "s390/z10"; break;
458 case 0x2817: case 0x2818: ops
->cpu_type
= "s390/z196"; break;
459 case 0x2827: case 0x2828: ops
->cpu_type
= "s390/zEC12"; break;
460 default: return -ENODEV
;
464 if (hwsampler_setup())
468 * Query the range for the sampling interval from the
471 oprofile_min_interval
= hwsampler_query_min_interval();
472 if (oprofile_min_interval
== 0)
474 oprofile_max_interval
= hwsampler_query_max_interval();
475 if (oprofile_max_interval
== 0)
478 /* The initial value should be sane */
479 if (oprofile_hw_interval
< oprofile_min_interval
)
480 oprofile_hw_interval
= oprofile_min_interval
;
481 if (oprofile_hw_interval
> oprofile_max_interval
)
482 oprofile_hw_interval
= oprofile_max_interval
;
484 printk(KERN_INFO
"oprofile: System z hardware sampling "
485 "facility found.\n");
487 ops
->start
= oprofile_hwsampler_start
;
488 ops
->stop
= oprofile_hwsampler_stop
;
493 static void oprofile_hwsampler_exit(void)
495 hwsampler_shutdown();
498 #endif /* CONFIG_64BIT */
500 int __init
oprofile_arch_init(struct oprofile_operations
*ops
)
502 ops
->backtrace
= s390_backtrace
;
507 * -ENODEV is not reported to the caller. The module itself
508 * will use the timer mode sampling as fallback and this is
511 hwsampler_available
= oprofile_hwsampler_init(ops
) == 0;
519 void oprofile_arch_exit(void)
522 oprofile_hwsampler_exit();