| Heinz Graalfs | 997dbb4 | 2011-01-21 10:06:53 +0000 | [diff] [blame] | 1 | /** |
| 2 | * arch/s390/oprofile/hwsampler_files.c |
| 3 | * |
| 4 | * Copyright IBM Corp. 2010 |
| 5 | * Author: Mahesh Salgaonkar (mahesh@linux.vnet.ibm.com) |
| 6 | */ |
| 7 | #include <linux/oprofile.h> |
| 8 | #include <linux/errno.h> |
| 9 | #include <linux/fs.h> |
| 10 | |
| Robert Richter | a0d7624 | 2011-02-11 17:31:44 +0100 | [diff] [blame^] | 11 | #include "../../../drivers/oprofile/oprof.h" |
| Heinz Graalfs | 997dbb4 | 2011-01-21 10:06:53 +0000 | [diff] [blame] | 12 | #include "hwsampler.h" |
| 13 | |
| 14 | #define DEFAULT_INTERVAL 4096 |
| 15 | |
| 16 | #define DEFAULT_SDBT_BLOCKS 1 |
| 17 | #define DEFAULT_SDB_BLOCKS 511 |
| 18 | |
| 19 | static unsigned long oprofile_hw_interval = DEFAULT_INTERVAL; |
| 20 | static unsigned long oprofile_min_interval; |
| 21 | static unsigned long oprofile_max_interval; |
| 22 | |
| 23 | static unsigned long oprofile_sdbt_blocks = DEFAULT_SDBT_BLOCKS; |
| 24 | static unsigned long oprofile_sdb_blocks = DEFAULT_SDB_BLOCKS; |
| 25 | |
| Robert Richter | a0d7624 | 2011-02-11 17:31:44 +0100 | [diff] [blame^] | 26 | static int hwsampler_file; |
| 27 | static int hwsampler_running; /* start_mutex must be held to change */ |
| 28 | |
| 29 | static struct oprofile_operations timer_ops; |
| Heinz Graalfs | 997dbb4 | 2011-01-21 10:06:53 +0000 | [diff] [blame] | 30 | |
| 31 | static int oprofile_hwsampler_start(void) |
| 32 | { |
| 33 | int retval; |
| 34 | |
| Robert Richter | a0d7624 | 2011-02-11 17:31:44 +0100 | [diff] [blame^] | 35 | hwsampler_running = hwsampler_file; |
| 36 | |
| 37 | if (!hwsampler_running) |
| 38 | return timer_ops.start(); |
| 39 | |
| Heinz Graalfs | 997dbb4 | 2011-01-21 10:06:53 +0000 | [diff] [blame] | 40 | retval = hwsampler_allocate(oprofile_sdbt_blocks, oprofile_sdb_blocks); |
| 41 | if (retval) |
| 42 | return retval; |
| 43 | |
| 44 | retval = hwsampler_start_all(oprofile_hw_interval); |
| 45 | if (retval) |
| 46 | hwsampler_deallocate(); |
| 47 | |
| 48 | return retval; |
| 49 | } |
| 50 | |
| 51 | static void oprofile_hwsampler_stop(void) |
| 52 | { |
| Robert Richter | a0d7624 | 2011-02-11 17:31:44 +0100 | [diff] [blame^] | 53 | if (!hwsampler_running) { |
| 54 | timer_ops.stop(); |
| 55 | return; |
| 56 | } |
| 57 | |
| Heinz Graalfs | 997dbb4 | 2011-01-21 10:06:53 +0000 | [diff] [blame] | 58 | hwsampler_stop_all(); |
| 59 | hwsampler_deallocate(); |
| 60 | return; |
| 61 | } |
| 62 | |
| Heinz Graalfs | 997dbb4 | 2011-01-21 10:06:53 +0000 | [diff] [blame] | 63 | static ssize_t hwsampler_read(struct file *file, char __user *buf, |
| 64 | size_t count, loff_t *offset) |
| 65 | { |
| Robert Richter | a0d7624 | 2011-02-11 17:31:44 +0100 | [diff] [blame^] | 66 | return oprofilefs_ulong_to_user(hwsampler_file, buf, count, offset); |
| Heinz Graalfs | 997dbb4 | 2011-01-21 10:06:53 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static ssize_t hwsampler_write(struct file *file, char const __user *buf, |
| 70 | size_t count, loff_t *offset) |
| 71 | { |
| 72 | unsigned long val; |
| 73 | int retval; |
| 74 | |
| 75 | if (*offset) |
| 76 | return -EINVAL; |
| 77 | |
| 78 | retval = oprofilefs_ulong_from_user(&val, buf, count); |
| 79 | if (retval) |
| 80 | return retval; |
| 81 | |
| Robert Richter | a0d7624 | 2011-02-11 17:31:44 +0100 | [diff] [blame^] | 82 | if (oprofile_started) |
| 83 | /* |
| 84 | * save to do without locking as we set |
| 85 | * hwsampler_running in start() when start_mutex is |
| 86 | * held |
| 87 | */ |
| 88 | return -EBUSY; |
| Heinz Graalfs | 997dbb4 | 2011-01-21 10:06:53 +0000 | [diff] [blame] | 89 | |
| Robert Richter | a0d7624 | 2011-02-11 17:31:44 +0100 | [diff] [blame^] | 90 | hwsampler_file = val; |
| Heinz Graalfs | 997dbb4 | 2011-01-21 10:06:53 +0000 | [diff] [blame] | 91 | |
| Heinz Graalfs | 997dbb4 | 2011-01-21 10:06:53 +0000 | [diff] [blame] | 92 | return count; |
| 93 | } |
| 94 | |
| 95 | static const struct file_operations hwsampler_fops = { |
| 96 | .read = hwsampler_read, |
| 97 | .write = hwsampler_write, |
| 98 | }; |
| 99 | |
| 100 | static int oprofile_create_hwsampling_files(struct super_block *sb, |
| 101 | struct dentry *root) |
| 102 | { |
| 103 | struct dentry *hw_dir; |
| 104 | |
| 105 | /* reinitialize default values */ |
| Robert Richter | a0d7624 | 2011-02-11 17:31:44 +0100 | [diff] [blame^] | 106 | hwsampler_file = 1; |
| Heinz Graalfs | 997dbb4 | 2011-01-21 10:06:53 +0000 | [diff] [blame] | 107 | |
| 108 | hw_dir = oprofilefs_mkdir(sb, root, "hwsampling"); |
| 109 | if (!hw_dir) |
| 110 | return -EINVAL; |
| 111 | |
| 112 | oprofilefs_create_file(sb, hw_dir, "hwsampler", &hwsampler_fops); |
| 113 | oprofilefs_create_ulong(sb, hw_dir, "hw_interval", |
| 114 | &oprofile_hw_interval); |
| 115 | oprofilefs_create_ro_ulong(sb, hw_dir, "hw_min_interval", |
| 116 | &oprofile_min_interval); |
| 117 | oprofilefs_create_ro_ulong(sb, hw_dir, "hw_max_interval", |
| 118 | &oprofile_max_interval); |
| 119 | oprofilefs_create_ulong(sb, hw_dir, "hw_sdbt_blocks", |
| 120 | &oprofile_sdbt_blocks); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | int oprofile_hwsampler_init(struct oprofile_operations* ops) |
| 126 | { |
| 127 | if (hwsampler_setup()) |
| 128 | return -ENODEV; |
| 129 | |
| 130 | /* |
| 131 | * create hwsampler files only if hwsampler_setup() succeeds. |
| 132 | */ |
| Heinz Graalfs | 997dbb4 | 2011-01-21 10:06:53 +0000 | [diff] [blame] | 133 | oprofile_min_interval = hwsampler_query_min_interval(); |
| 134 | if (oprofile_min_interval < 0) { |
| 135 | oprofile_min_interval = 0; |
| 136 | return -ENODEV; |
| 137 | } |
| 138 | oprofile_max_interval = hwsampler_query_max_interval(); |
| 139 | if (oprofile_max_interval < 0) { |
| 140 | oprofile_max_interval = 0; |
| 141 | return -ENODEV; |
| 142 | } |
| Robert Richter | a0d7624 | 2011-02-11 17:31:44 +0100 | [diff] [blame^] | 143 | |
| 144 | if (oprofile_timer_init(ops)) |
| 145 | return -ENODEV; |
| 146 | |
| 147 | printk(KERN_INFO "oprofile: using hardware sampling\n"); |
| 148 | |
| 149 | memcpy(&timer_ops, ops, sizeof(timer_ops)); |
| 150 | |
| 151 | ops->start = oprofile_hwsampler_start; |
| 152 | ops->stop = oprofile_hwsampler_stop; |
| 153 | ops->create_files = oprofile_create_hwsampling_files; |
| 154 | |
| Heinz Graalfs | 997dbb4 | 2011-01-21 10:06:53 +0000 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | void oprofile_hwsampler_exit(void) |
| 159 | { |
| Robert Richter | a0d7624 | 2011-02-11 17:31:44 +0100 | [diff] [blame^] | 160 | oprofile_timer_exit(); |
| Heinz Graalfs | 997dbb4 | 2011-01-21 10:06:53 +0000 | [diff] [blame] | 161 | hwsampler_shutdown(); |
| 162 | } |