blob: 264528e4f58d5ea3fa0ff12f534c925df21001a6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/s390/appldata/appldata_base.c
3 *
4 * Base infrastructure for Linux-z/VM Monitor Stream, Stage 1.
5 * Exports appldata_register_ops() and appldata_unregister_ops() for the
6 * data gathering modules.
7 *
Gerald Schaefer524dbcd2009-06-16 10:30:36 +02008 * Copyright IBM Corp. 2003, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
Gerald Schaefer5b5dd212006-06-29 15:08:35 +020010 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Gerald Schaefere7534b02008-12-25 13:39:41 +010013#define KMSG_COMPONENT "appldata"
14#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/interrupt.h>
21#include <linux/proc_fs.h>
Heiko Carstens2dcea572006-09-29 01:58:41 -070022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/swap.h>
24#include <linux/pagemap.h>
25#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/notifier.h>
27#include <linux/cpu.h>
Gerald Schaeferf26d5832005-06-04 15:43:33 -070028#include <linux/workqueue.h>
Gerald Schaefer524dbcd2009-06-16 10:30:36 +020029#include <linux/suspend.h>
30#include <linux/platform_device.h>
Gerald Schaefer1f38d612006-09-20 15:59:26 +020031#include <asm/appldata.h>
32#include <asm/timer.h>
33#include <asm/uaccess.h>
34#include <asm/io.h>
35#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include "appldata.h"
38
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
41 sampling interval in
42 milliseconds */
43
44#define TOD_MICRO 0x01000 /* nr. of TOD clock units
45 for 1 microsecond */
Gerald Schaefer524dbcd2009-06-16 10:30:36 +020046
47static struct platform_device *appldata_pdev;
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * /proc entries (sysctl)
51 */
52static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata";
53static int appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
54 void __user *buffer, size_t *lenp, loff_t *ppos);
55static int appldata_interval_handler(ctl_table *ctl, int write,
56 struct file *filp,
57 void __user *buffer,
58 size_t *lenp, loff_t *ppos);
59
60static struct ctl_table_header *appldata_sysctl_header;
61static struct ctl_table appldata_table[] = {
62 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .procname = "timer",
64 .mode = S_IRUGO | S_IWUSR,
65 .proc_handler = &appldata_timer_handler,
66 },
67 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 .procname = "interval",
69 .mode = S_IRUGO | S_IWUSR,
70 .proc_handler = &appldata_interval_handler,
71 },
Heiko Carstens37e3a6a2007-11-20 11:13:34 +010072 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
75static struct ctl_table appldata_dir_table[] = {
76 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 .procname = appldata_proc_name,
78 .maxlen = 0,
79 .mode = S_IRUGO | S_IXUGO,
80 .child = appldata_table,
81 },
Heiko Carstens37e3a6a2007-11-20 11:13:34 +010082 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
85/*
86 * Timer
87 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +010088static DEFINE_PER_CPU(struct vtimer_list, appldata_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static atomic_t appldata_expire_count = ATOMIC_INIT(0);
90
91static DEFINE_SPINLOCK(appldata_timer_lock);
92static int appldata_interval = APPLDATA_CPU_INTERVAL;
93static int appldata_timer_active;
Gerald Schaefer524dbcd2009-06-16 10:30:36 +020094static int appldata_timer_suspended = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/*
Gerald Schaeferf26d5832005-06-04 15:43:33 -070097 * Work queue
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
Gerald Schaeferf26d5832005-06-04 15:43:33 -070099static struct workqueue_struct *appldata_wq;
David Howells6d5aefb2006-12-05 19:36:26 +0000100static void appldata_work_fn(struct work_struct *work);
101static DECLARE_WORK(appldata_work, appldata_work_fn);
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104/*
105 * Ops list
106 */
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200107static DEFINE_MUTEX(appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static LIST_HEAD(appldata_ops_list);
109
110
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700111/*************************** timer, work, DIAG *******************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*
113 * appldata_timer_function()
114 *
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700115 * schedule work and reschedule timer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200117static void appldata_timer_function(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (atomic_dec_and_test(&appldata_expire_count)) {
120 atomic_set(&appldata_expire_count, num_online_cpus());
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700121 queue_work(appldata_wq, (struct work_struct *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123}
124
125/*
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700126 * appldata_work_fn()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 *
128 * call data gathering function for each (active) module
129 */
David Howells6d5aefb2006-12-05 19:36:26 +0000130static void appldata_work_fn(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct list_head *lh;
133 struct appldata_ops *ops;
134 int i;
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 i = 0;
Gerald Schaefer17605372008-05-30 10:03:28 +0200137 get_online_cpus();
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200138 mutex_lock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 list_for_each(lh, &appldata_ops_list) {
140 ops = list_entry(lh, struct appldata_ops, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (ops->active == 1) {
142 ops->callback(ops->data);
143 }
144 }
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200145 mutex_unlock(&appldata_ops_mutex);
Gerald Schaefer17605372008-05-30 10:03:28 +0200146 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149/*
150 * appldata_diag()
151 *
152 * prepare parameter list, issue DIAG 0xDC
153 */
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200154int appldata_diag(char record_nr, u16 function, unsigned long buffer,
155 u16 length, char *mod_lvl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Gerald Schaefer1f38d612006-09-20 15:59:26 +0200157 struct appldata_product_id id = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
Gerald Schaefer1f38d612006-09-20 15:59:26 +0200159 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
160 .prod_fn = 0xD5D3, /* "NL" */
Gerald Schaefer1f38d612006-09-20 15:59:26 +0200161 .version_nr = 0xF2F6, /* "26" */
162 .release_nr = 0xF0F1, /* "01" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 };
164
Gerald Schaefer925afbd2006-09-28 16:55:23 +0200165 id.record_nr = record_nr;
166 id.mod_lvl = (mod_lvl[0]) << 8 | mod_lvl[1];
Gerald Schaefer1f38d612006-09-20 15:59:26 +0200167 return appldata_asm(&id, function, (void *) buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700169/************************ timer, work, DIAG <END> ****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171
172/****************************** /proc stuff **********************************/
173
174/*
175 * appldata_mod_vtimer_wrap()
176 *
Heiko Carstens3bb447f2007-07-27 12:29:08 +0200177 * wrapper function for mod_virt_timer(), because smp_call_function_single()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * accepts only one parameter.
179 */
180static void __appldata_mod_vtimer_wrap(void *p) {
181 struct {
182 struct vtimer_list *timer;
183 u64 expires;
184 } *args = p;
Gerald Schaefer43ae8a12009-04-14 15:36:21 +0200185 mod_virt_timer_periodic(args->timer, args->expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188#define APPLDATA_ADD_TIMER 0
189#define APPLDATA_DEL_TIMER 1
190#define APPLDATA_MOD_TIMER 2
191
192/*
193 * __appldata_vtimer_setup()
194 *
195 * Add, delete or modify virtual timers on all online cpus.
196 * The caller needs to get the appldata_timer_lock spinlock.
197 */
198static void
199__appldata_vtimer_setup(int cmd)
200{
201 u64 per_cpu_interval;
202 int i;
203
204 switch (cmd) {
205 case APPLDATA_ADD_TIMER:
206 if (appldata_timer_active)
207 break;
208 per_cpu_interval = (u64) (appldata_interval*1000 /
209 num_online_cpus()) * TOD_MICRO;
210 for_each_online_cpu(i) {
211 per_cpu(appldata_timer, i).expires = per_cpu_interval;
Heiko Carstens3bb447f2007-07-27 12:29:08 +0200212 smp_call_function_single(i, add_virt_timer_periodic,
213 &per_cpu(appldata_timer, i),
Jens Axboe8691e5a2008-06-06 11:18:06 +0200214 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216 appldata_timer_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 break;
218 case APPLDATA_DEL_TIMER:
219 for_each_online_cpu(i)
220 del_virt_timer(&per_cpu(appldata_timer, i));
221 if (!appldata_timer_active)
222 break;
223 appldata_timer_active = 0;
224 atomic_set(&appldata_expire_count, num_online_cpus());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 break;
226 case APPLDATA_MOD_TIMER:
227 per_cpu_interval = (u64) (appldata_interval*1000 /
228 num_online_cpus()) * TOD_MICRO;
229 if (!appldata_timer_active)
230 break;
231 for_each_online_cpu(i) {
232 struct {
233 struct vtimer_list *timer;
234 u64 expires;
235 } args;
236 args.timer = &per_cpu(appldata_timer, i);
237 args.expires = per_cpu_interval;
Heiko Carstens3bb447f2007-07-27 12:29:08 +0200238 smp_call_function_single(i, __appldata_mod_vtimer_wrap,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200239 &args, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 }
242}
243
244/*
245 * appldata_timer_handler()
246 *
247 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
248 */
249static int
250appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
251 void __user *buffer, size_t *lenp, loff_t *ppos)
252{
253 int len;
254 char buf[2];
255
256 if (!*lenp || *ppos) {
257 *lenp = 0;
258 return 0;
259 }
260 if (!write) {
261 len = sprintf(buf, appldata_timer_active ? "1\n" : "0\n");
262 if (len > *lenp)
263 len = *lenp;
264 if (copy_to_user(buffer, buf, len))
265 return -EFAULT;
266 goto out;
267 }
268 len = *lenp;
269 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len))
270 return -EFAULT;
Gerald Schaefer17605372008-05-30 10:03:28 +0200271 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 spin_lock(&appldata_timer_lock);
273 if (buf[0] == '1')
274 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
275 else if (buf[0] == '0')
276 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
277 spin_unlock(&appldata_timer_lock);
Gerald Schaefer17605372008-05-30 10:03:28 +0200278 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279out:
280 *lenp = len;
281 *ppos += len;
282 return 0;
283}
284
285/*
286 * appldata_interval_handler()
287 *
288 * Set (CPU) timer interval for collection of data (in milliseconds), show
289 * current timer interval.
290 */
291static int
292appldata_interval_handler(ctl_table *ctl, int write, struct file *filp,
293 void __user *buffer, size_t *lenp, loff_t *ppos)
294{
295 int len, interval;
296 char buf[16];
297
298 if (!*lenp || *ppos) {
299 *lenp = 0;
300 return 0;
301 }
302 if (!write) {
303 len = sprintf(buf, "%i\n", appldata_interval);
304 if (len > *lenp)
305 len = *lenp;
306 if (copy_to_user(buffer, buf, len))
307 return -EFAULT;
308 goto out;
309 }
310 len = *lenp;
311 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len)) {
312 return -EFAULT;
313 }
Gerald Schaefer95425f12006-10-27 12:39:13 +0200314 interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 sscanf(buf, "%i", &interval);
Gerald Schaeferd3ae9422008-07-14 09:59:34 +0200316 if (interval <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Gerald Schaefer17605372008-05-30 10:03:28 +0200319 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 spin_lock(&appldata_timer_lock);
321 appldata_interval = interval;
322 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
323 spin_unlock(&appldata_timer_lock);
Gerald Schaefer17605372008-05-30 10:03:28 +0200324 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325out:
326 *lenp = len;
327 *ppos += len;
328 return 0;
329}
330
331/*
332 * appldata_generic_handler()
333 *
334 * Generic start/stop monitoring and DIAG, show status of
335 * monitoring (0 = not in process, 1 = in process)
336 */
337static int
338appldata_generic_handler(ctl_table *ctl, int write, struct file *filp,
339 void __user *buffer, size_t *lenp, loff_t *ppos)
340{
341 struct appldata_ops *ops = NULL, *tmp_ops;
342 int rc, len, found;
343 char buf[2];
344 struct list_head *lh;
345
346 found = 0;
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200347 mutex_lock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 list_for_each(lh, &appldata_ops_list) {
349 tmp_ops = list_entry(lh, struct appldata_ops, list);
350 if (&tmp_ops->ctl_table[2] == ctl) {
351 found = 1;
352 }
353 }
354 if (!found) {
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200355 mutex_unlock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return -ENODEV;
357 }
358 ops = ctl->data;
359 if (!try_module_get(ops->owner)) { // protect this function
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200360 mutex_unlock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return -ENODEV;
362 }
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200363 mutex_unlock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 if (!*lenp || *ppos) {
366 *lenp = 0;
367 module_put(ops->owner);
368 return 0;
369 }
370 if (!write) {
371 len = sprintf(buf, ops->active ? "1\n" : "0\n");
372 if (len > *lenp)
373 len = *lenp;
374 if (copy_to_user(buffer, buf, len)) {
375 module_put(ops->owner);
376 return -EFAULT;
377 }
378 goto out;
379 }
380 len = *lenp;
381 if (copy_from_user(buf, buffer,
382 len > sizeof(buf) ? sizeof(buf) : len)) {
383 module_put(ops->owner);
384 return -EFAULT;
385 }
386
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200387 mutex_lock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if ((buf[0] == '1') && (ops->active == 0)) {
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700389 // protect work queue callback
390 if (!try_module_get(ops->owner)) {
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200391 mutex_unlock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 module_put(ops->owner);
393 return -ENODEV;
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 ops->callback(ops->data); // init record
396 rc = appldata_diag(ops->record_nr,
397 APPLDATA_START_INTERVAL_REC,
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200398 (unsigned long) ops->data, ops->size,
399 ops->mod_lvl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (rc != 0) {
Gerald Schaefere7534b02008-12-25 13:39:41 +0100401 pr_err("Starting the data collection for %s "
402 "failed with rc=%d\n", ops->name, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 module_put(ops->owner);
Gerald Schaeferd3ae9422008-07-14 09:59:34 +0200404 } else
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200405 ops->active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 } else if ((buf[0] == '0') && (ops->active == 1)) {
407 ops->active = 0;
408 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200409 (unsigned long) ops->data, ops->size,
410 ops->mod_lvl);
Gerald Schaeferd3ae9422008-07-14 09:59:34 +0200411 if (rc != 0)
Gerald Schaefere7534b02008-12-25 13:39:41 +0100412 pr_err("Stopping the data collection for %s "
413 "failed with rc=%d\n", ops->name, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 module_put(ops->owner);
415 }
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200416 mutex_unlock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417out:
418 *lenp = len;
419 *ppos += len;
420 module_put(ops->owner);
421 return 0;
422}
423
424/*************************** /proc stuff <END> *******************************/
425
426
427/************************* module-ops management *****************************/
428/*
429 * appldata_register_ops()
430 *
431 * update ops list, register /proc/sys entries
432 */
433int appldata_register_ops(struct appldata_ops *ops)
434{
Roel Kluin13f8b7c2008-10-28 11:10:18 +0100435 if (ops->size > APPLDATA_MAX_REC_SIZE)
Heiko Carstens37e3a6a2007-11-20 11:13:34 +0100436 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Heiko Carstens37e3a6a2007-11-20 11:13:34 +0100438 ops->ctl_table = kzalloc(4 * sizeof(struct ctl_table), GFP_KERNEL);
439 if (!ops->ctl_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200442 mutex_lock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 list_add(&ops->list, &appldata_ops_list);
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200444 mutex_unlock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 ops->ctl_table[0].procname = appldata_proc_name;
447 ops->ctl_table[0].maxlen = 0;
448 ops->ctl_table[0].mode = S_IRUGO | S_IXUGO;
449 ops->ctl_table[0].child = &ops->ctl_table[2];
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 ops->ctl_table[2].procname = ops->name;
452 ops->ctl_table[2].mode = S_IRUGO | S_IWUSR;
453 ops->ctl_table[2].proc_handler = appldata_generic_handler;
454 ops->ctl_table[2].data = ops;
455
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800456 ops->sysctl_header = register_sysctl_table(ops->ctl_table);
Heiko Carstens37e3a6a2007-11-20 11:13:34 +0100457 if (!ops->sysctl_header)
458 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return 0;
Heiko Carstens37e3a6a2007-11-20 11:13:34 +0100460out:
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200461 mutex_lock(&appldata_ops_mutex);
Heiko Carstens37e3a6a2007-11-20 11:13:34 +0100462 list_del(&ops->list);
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200463 mutex_unlock(&appldata_ops_mutex);
Heiko Carstens37e3a6a2007-11-20 11:13:34 +0100464 kfree(ops->ctl_table);
465 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468/*
469 * appldata_unregister_ops()
470 *
471 * update ops list, unregister /proc entries, stop DIAG if necessary
472 */
473void appldata_unregister_ops(struct appldata_ops *ops)
474{
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200475 mutex_lock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 list_del(&ops->list);
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200477 mutex_unlock(&appldata_ops_mutex);
Al Viro330d57f2005-11-04 10:18:40 +0000478 unregister_sysctl_table(ops->sysctl_header);
Heiko Carstens37e3a6a2007-11-20 11:13:34 +0100479 kfree(ops->ctl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481/********************** module-ops management <END> **************************/
482
483
Gerald Schaefer524dbcd2009-06-16 10:30:36 +0200484/**************************** suspend / resume *******************************/
485static int appldata_freeze(struct device *dev)
486{
487 struct appldata_ops *ops;
488 int rc;
489 struct list_head *lh;
490
491 get_online_cpus();
492 spin_lock(&appldata_timer_lock);
493 if (appldata_timer_active) {
494 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
495 appldata_timer_suspended = 1;
496 }
497 spin_unlock(&appldata_timer_lock);
498 put_online_cpus();
499
500 mutex_lock(&appldata_ops_mutex);
501 list_for_each(lh, &appldata_ops_list) {
502 ops = list_entry(lh, struct appldata_ops, list);
503 if (ops->active == 1) {
504 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
505 (unsigned long) ops->data, ops->size,
506 ops->mod_lvl);
507 if (rc != 0)
508 pr_err("Stopping the data collection for %s "
509 "failed with rc=%d\n", ops->name, rc);
510 }
511 }
512 mutex_unlock(&appldata_ops_mutex);
513 return 0;
514}
515
516static int appldata_restore(struct device *dev)
517{
518 struct appldata_ops *ops;
519 int rc;
520 struct list_head *lh;
521
522 get_online_cpus();
523 spin_lock(&appldata_timer_lock);
524 if (appldata_timer_suspended) {
525 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
526 appldata_timer_suspended = 0;
527 }
528 spin_unlock(&appldata_timer_lock);
529 put_online_cpus();
530
531 mutex_lock(&appldata_ops_mutex);
532 list_for_each(lh, &appldata_ops_list) {
533 ops = list_entry(lh, struct appldata_ops, list);
534 if (ops->active == 1) {
535 ops->callback(ops->data); // init record
536 rc = appldata_diag(ops->record_nr,
537 APPLDATA_START_INTERVAL_REC,
538 (unsigned long) ops->data, ops->size,
539 ops->mod_lvl);
540 if (rc != 0) {
541 pr_err("Starting the data collection for %s "
542 "failed with rc=%d\n", ops->name, rc);
543 }
544 }
545 }
546 mutex_unlock(&appldata_ops_mutex);
547 return 0;
548}
549
550static int appldata_thaw(struct device *dev)
551{
552 return appldata_restore(dev);
553}
554
555static struct dev_pm_ops appldata_pm_ops = {
556 .freeze = appldata_freeze,
557 .thaw = appldata_thaw,
558 .restore = appldata_restore,
559};
560
561static struct platform_driver appldata_pdrv = {
562 .driver = {
563 .name = "appldata",
564 .owner = THIS_MODULE,
565 .pm = &appldata_pm_ops,
566 },
567};
568/************************* suspend / resume <END> ****************************/
569
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571/******************************* init / exit *********************************/
572
Heiko Carstens84b36a82007-06-19 13:10:03 +0200573static void __cpuinit appldata_online_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 init_virt_timer(&per_cpu(appldata_timer, cpu));
576 per_cpu(appldata_timer, cpu).function = appldata_timer_function;
577 per_cpu(appldata_timer, cpu).data = (unsigned long)
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700578 &appldata_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 atomic_inc(&appldata_expire_count);
580 spin_lock(&appldata_timer_lock);
581 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
582 spin_unlock(&appldata_timer_lock);
583}
584
Satyam Sharma076fc802007-10-12 16:11:32 +0200585static void __cpuinit appldata_offline_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 del_virt_timer(&per_cpu(appldata_timer, cpu));
588 if (atomic_dec_and_test(&appldata_expire_count)) {
589 atomic_set(&appldata_expire_count, num_online_cpus());
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700590 queue_work(appldata_wq, &appldata_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592 spin_lock(&appldata_timer_lock);
593 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
594 spin_unlock(&appldata_timer_lock);
595}
596
Satyam Sharma11b8bf02007-10-12 16:11:31 +0200597static int __cpuinit appldata_cpu_notify(struct notifier_block *self,
598 unsigned long action,
599 void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 switch (action) {
602 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700603 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 appldata_online_cpu((long) hcpu);
605 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700607 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 appldata_offline_cpu((long) hcpu);
609 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 default:
611 break;
612 }
613 return NOTIFY_OK;
614}
615
Heiko Carstens84b36a82007-06-19 13:10:03 +0200616static struct notifier_block __cpuinitdata appldata_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 .notifier_call = appldata_cpu_notify,
618};
619
620/*
621 * appldata_init()
622 *
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700623 * init timer, register /proc entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
625static int __init appldata_init(void)
626{
Gerald Schaefer524dbcd2009-06-16 10:30:36 +0200627 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Gerald Schaefer524dbcd2009-06-16 10:30:36 +0200629 rc = platform_driver_register(&appldata_pdrv);
630 if (rc)
631 return rc;
632
633 appldata_pdev = platform_device_register_simple("appldata", -1, NULL,
634 0);
635 if (IS_ERR(appldata_pdev)) {
636 rc = PTR_ERR(appldata_pdev);
637 goto out_driver;
638 }
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700639 appldata_wq = create_singlethread_workqueue("appldata");
Gerald Schaefer524dbcd2009-06-16 10:30:36 +0200640 if (!appldata_wq) {
641 rc = -ENOMEM;
642 goto out_device;
643 }
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700644
Gerald Schaefer17605372008-05-30 10:03:28 +0200645 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 for_each_online_cpu(i)
647 appldata_online_cpu(i);
Gerald Schaefer17605372008-05-30 10:03:28 +0200648 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /* Register cpu hotplug notifier */
Chandra Seetharamanbe6b5a32006-07-30 03:03:37 -0700651 register_hotcpu_notifier(&appldata_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800653 appldata_sysctl_header = register_sysctl_table(appldata_dir_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return 0;
Gerald Schaefer524dbcd2009-06-16 10:30:36 +0200655
656out_device:
657 platform_device_unregister(appldata_pdev);
658out_driver:
659 platform_driver_unregister(&appldata_pdrv);
660 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Satyam Sharma076fc802007-10-12 16:11:32 +0200663__initcall(appldata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/**************************** init / exit <END> ******************************/
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667EXPORT_SYMBOL_GPL(appldata_register_ops);
668EXPORT_SYMBOL_GPL(appldata_unregister_ops);
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200669EXPORT_SYMBOL_GPL(appldata_diag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Gerald Schaefer0c3252d2008-07-14 09:57:27 +0200671#ifdef CONFIG_SWAP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672EXPORT_SYMBOL_GPL(si_swapinfo);
Gerald Schaefer0c3252d2008-07-14 09:57:27 +0200673#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674EXPORT_SYMBOL_GPL(nr_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675EXPORT_SYMBOL_GPL(nr_running);
676EXPORT_SYMBOL_GPL(nr_iowait);