blob: 2e1da2449ba9ddc895e86e7d300a5fada9826d92 [file] [log] [blame]
Heinz Graalfs997dbb42011-01-21 10:06:53 +00001/**
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 Richtera0d76242011-02-11 17:31:44 +010011#include "../../../drivers/oprofile/oprof.h"
Heinz Graalfs997dbb42011-01-21 10:06:53 +000012#include "hwsampler.h"
13
14#define DEFAULT_INTERVAL 4096
15
16#define DEFAULT_SDBT_BLOCKS 1
17#define DEFAULT_SDB_BLOCKS 511
18
19static unsigned long oprofile_hw_interval = DEFAULT_INTERVAL;
20static unsigned long oprofile_min_interval;
21static unsigned long oprofile_max_interval;
22
23static unsigned long oprofile_sdbt_blocks = DEFAULT_SDBT_BLOCKS;
24static unsigned long oprofile_sdb_blocks = DEFAULT_SDB_BLOCKS;
25
Robert Richtera0d76242011-02-11 17:31:44 +010026static int hwsampler_file;
27static int hwsampler_running; /* start_mutex must be held to change */
28
29static struct oprofile_operations timer_ops;
Heinz Graalfs997dbb42011-01-21 10:06:53 +000030
31static int oprofile_hwsampler_start(void)
32{
33 int retval;
34
Robert Richtera0d76242011-02-11 17:31:44 +010035 hwsampler_running = hwsampler_file;
36
37 if (!hwsampler_running)
38 return timer_ops.start();
39
Heinz Graalfs997dbb42011-01-21 10:06:53 +000040 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
51static void oprofile_hwsampler_stop(void)
52{
Robert Richtera0d76242011-02-11 17:31:44 +010053 if (!hwsampler_running) {
54 timer_ops.stop();
55 return;
56 }
57
Heinz Graalfs997dbb42011-01-21 10:06:53 +000058 hwsampler_stop_all();
59 hwsampler_deallocate();
60 return;
61}
62
Heinz Graalfs997dbb42011-01-21 10:06:53 +000063static ssize_t hwsampler_read(struct file *file, char __user *buf,
64 size_t count, loff_t *offset)
65{
Robert Richtera0d76242011-02-11 17:31:44 +010066 return oprofilefs_ulong_to_user(hwsampler_file, buf, count, offset);
Heinz Graalfs997dbb42011-01-21 10:06:53 +000067}
68
69static 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 Richtera0d76242011-02-11 17:31:44 +010082 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 Graalfs997dbb42011-01-21 10:06:53 +000089
Robert Richtera0d76242011-02-11 17:31:44 +010090 hwsampler_file = val;
Heinz Graalfs997dbb42011-01-21 10:06:53 +000091
Heinz Graalfs997dbb42011-01-21 10:06:53 +000092 return count;
93}
94
95static const struct file_operations hwsampler_fops = {
96 .read = hwsampler_read,
97 .write = hwsampler_write,
98};
99
100static 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 Richtera0d76242011-02-11 17:31:44 +0100106 hwsampler_file = 1;
Heinz Graalfs997dbb42011-01-21 10:06:53 +0000107
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
125int 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 Graalfs997dbb42011-01-21 10:06:53 +0000133 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 Richtera0d76242011-02-11 17:31:44 +0100143
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 Graalfs997dbb42011-01-21 10:06:53 +0000155 return 0;
156}
157
158void oprofile_hwsampler_exit(void)
159{
Robert Richtera0d76242011-02-11 17:31:44 +0100160 oprofile_timer_exit();
Heinz Graalfs997dbb42011-01-21 10:06:53 +0000161 hwsampler_shutdown();
162}