blob: 16c76def4a9da5818f8cb557c43ec46f1fbe85da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * arch/s390/oprofile/init.c
3 *
4 * S390 Version
5 * Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
6 * Author(s): Thomas Spatzier (tspat@de.ibm.com)
Heinz Graalfsc814d162011-02-15 13:02:14 -05007 * Author(s): Mahesh Salgaonkar (mahesh@linux.vnet.ibm.com)
8 * Author(s): Heinz Graalfs (graalfs@linux.vnet.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
Heinz Graalfsc814d162011-02-15 13:02:14 -050010 * @remark Copyright 2002-2011 OProfile authors
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#include <linux/oprofile.h>
14#include <linux/init.h>
15#include <linux/errno.h>
Heinz Graalfsc814d162011-02-15 13:02:14 -050016#include <linux/oprofile.h>
17#include <linux/errno.h>
18#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Heinz Graalfsc814d162011-02-15 13:02:14 -050020#include "../../../drivers/oprofile/oprof.h"
21#include "hwsampler.h"
22
23#define DEFAULT_INTERVAL 4096
24
25#define DEFAULT_SDBT_BLOCKS 1
26#define DEFAULT_SDB_BLOCKS 511
27
28static unsigned long oprofile_hw_interval = DEFAULT_INTERVAL;
29static unsigned long oprofile_min_interval;
30static unsigned long oprofile_max_interval;
31
32static unsigned long oprofile_sdbt_blocks = DEFAULT_SDBT_BLOCKS;
33static unsigned long oprofile_sdb_blocks = DEFAULT_SDB_BLOCKS;
34
35static int hwsampler_file;
36static int hwsampler_running; /* start_mutex must be held to change */
37
38static struct oprofile_operations timer_ops;
Andreas Krebbeld0f4c162006-01-06 00:19:16 -080039
40extern void s390_backtrace(struct pt_regs * const regs, unsigned int depth);
41
Heinz Graalfsc814d162011-02-15 13:02:14 -050042static int oprofile_hwsampler_start(void)
43{
44 int retval;
45
46 hwsampler_running = hwsampler_file;
47
48 if (!hwsampler_running)
49 return timer_ops.start();
50
51 retval = hwsampler_allocate(oprofile_sdbt_blocks, oprofile_sdb_blocks);
52 if (retval)
53 return retval;
54
55 retval = hwsampler_start_all(oprofile_hw_interval);
56 if (retval)
57 hwsampler_deallocate();
58
59 return retval;
60}
61
62static void oprofile_hwsampler_stop(void)
63{
64 if (!hwsampler_running) {
65 timer_ops.stop();
66 return;
67 }
68
69 hwsampler_stop_all();
70 hwsampler_deallocate();
71 return;
72}
73
74static ssize_t hwsampler_read(struct file *file, char __user *buf,
75 size_t count, loff_t *offset)
76{
77 return oprofilefs_ulong_to_user(hwsampler_file, buf, count, offset);
78}
79
80static ssize_t hwsampler_write(struct file *file, char const __user *buf,
81 size_t count, loff_t *offset)
82{
83 unsigned long val;
84 int retval;
85
86 if (*offset)
87 return -EINVAL;
88
89 retval = oprofilefs_ulong_from_user(&val, buf, count);
90 if (retval)
91 return retval;
92
93 if (oprofile_started)
94 /*
95 * save to do without locking as we set
96 * hwsampler_running in start() when start_mutex is
97 * held
98 */
99 return -EBUSY;
100
101 hwsampler_file = val;
102
103 return count;
104}
105
106static const struct file_operations hwsampler_fops = {
107 .read = hwsampler_read,
108 .write = hwsampler_write,
109};
110
111static int oprofile_create_hwsampling_files(struct super_block *sb,
112 struct dentry *root)
113{
114 struct dentry *hw_dir;
115
116 /* reinitialize default values */
117 hwsampler_file = 1;
118
119 hw_dir = oprofilefs_mkdir(sb, root, "hwsampling");
120 if (!hw_dir)
121 return -EINVAL;
122
123 oprofilefs_create_file(sb, hw_dir, "hwsampler", &hwsampler_fops);
124 oprofilefs_create_ulong(sb, hw_dir, "hw_interval",
125 &oprofile_hw_interval);
126 oprofilefs_create_ro_ulong(sb, hw_dir, "hw_min_interval",
127 &oprofile_min_interval);
128 oprofilefs_create_ro_ulong(sb, hw_dir, "hw_max_interval",
129 &oprofile_max_interval);
130 oprofilefs_create_ulong(sb, hw_dir, "hw_sdbt_blocks",
131 &oprofile_sdbt_blocks);
132
133 return 0;
134}
135
Robert Richterec6b4262011-03-16 12:10:12 +0100136static int oprofile_hwsampler_init(struct oprofile_operations *ops)
Heinz Graalfsc814d162011-02-15 13:02:14 -0500137{
138 if (hwsampler_setup())
139 return -ENODEV;
140
141 /*
142 * create hwsampler files only if hwsampler_setup() succeeds.
143 */
144 oprofile_min_interval = hwsampler_query_min_interval();
145 if (oprofile_min_interval < 0) {
146 oprofile_min_interval = 0;
147 return -ENODEV;
148 }
149 oprofile_max_interval = hwsampler_query_max_interval();
150 if (oprofile_max_interval < 0) {
151 oprofile_max_interval = 0;
152 return -ENODEV;
153 }
154
155 if (oprofile_timer_init(ops))
156 return -ENODEV;
157
158 printk(KERN_INFO "oprofile: using hardware sampling\n");
159
160 memcpy(&timer_ops, ops, sizeof(timer_ops));
161
162 ops->start = oprofile_hwsampler_start;
163 ops->stop = oprofile_hwsampler_stop;
164 ops->create_files = oprofile_create_hwsampling_files;
165
166 return 0;
167}
168
Robert Richterec6b4262011-03-16 12:10:12 +0100169static void oprofile_hwsampler_exit(void)
Heinz Graalfsc814d162011-02-15 13:02:14 -0500170{
171 oprofile_timer_exit();
172 hwsampler_shutdown();
173}
174
Robert Richterec6b4262011-03-16 12:10:12 +0100175int __init oprofile_arch_init(struct oprofile_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Andreas Krebbeld0f4c162006-01-06 00:19:16 -0800177 ops->backtrace = s390_backtrace;
Heinz Graalfs997dbb42011-01-21 10:06:53 +0000178
179 return oprofile_hwsampler_init(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182void oprofile_arch_exit(void)
183{
Heinz Graalfs997dbb42011-01-21 10:06:53 +0000184 oprofile_hwsampler_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}