2 * arch/s390/appldata/appldata_base.c
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.
8 * Copyright IBM Corp. 2003, 2009
10 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
13 #define KMSG_COMPONENT "appldata"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/errno.h>
20 #include <linux/interrupt.h>
21 #include <linux/proc_fs.h>
23 #include <linux/swap.h>
24 #include <linux/pagemap.h>
25 #include <linux/sysctl.h>
26 #include <linux/notifier.h>
27 #include <linux/cpu.h>
28 #include <linux/workqueue.h>
29 #include <linux/suspend.h>
30 #include <linux/platform_device.h>
31 #include <asm/appldata.h>
32 #include <asm/timer.h>
33 #include <asm/uaccess.h>
40 #define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
44 #define TOD_MICRO 0x01000 /* nr. of TOD clock units
47 static struct platform_device *appldata_pdev;
50 * /proc entries (sysctl)
52 static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata";
53 static int appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
54 void __user *buffer, size_t *lenp, loff_t *ppos);
55 static int appldata_interval_handler(ctl_table *ctl, int write,
58 size_t *lenp, loff_t *ppos);
60 static struct ctl_table_header *appldata_sysctl_header;
61 static struct ctl_table appldata_table[] = {
64 .mode = S_IRUGO | S_IWUSR,
65 .proc_handler = &appldata_timer_handler,
68 .procname = "interval",
69 .mode = S_IRUGO | S_IWUSR,
70 .proc_handler = &appldata_interval_handler,
75 static struct ctl_table appldata_dir_table[] = {
77 .procname = appldata_proc_name,
79 .mode = S_IRUGO | S_IXUGO,
80 .child = appldata_table,
88 static DEFINE_PER_CPU(struct vtimer_list, appldata_timer);
89 static atomic_t appldata_expire_count = ATOMIC_INIT(0);
91 static DEFINE_SPINLOCK(appldata_timer_lock);
92 static int appldata_interval = APPLDATA_CPU_INTERVAL;
93 static int appldata_timer_active;
94 static int appldata_timer_suspended = 0;
99 static struct workqueue_struct *appldata_wq;
100 static void appldata_work_fn(struct work_struct *work);
101 static DECLARE_WORK(appldata_work, appldata_work_fn);
107 static DEFINE_MUTEX(appldata_ops_mutex);
108 static LIST_HEAD(appldata_ops_list);
111 /*************************** timer, work, DIAG *******************************/
113 * appldata_timer_function()
115 * schedule work and reschedule timer
117 static void appldata_timer_function(unsigned long data)
119 if (atomic_dec_and_test(&appldata_expire_count)) {
120 atomic_set(&appldata_expire_count, num_online_cpus());
121 queue_work(appldata_wq, (struct work_struct *) data);
128 * call data gathering function for each (active) module
130 static void appldata_work_fn(struct work_struct *work)
132 struct list_head *lh;
133 struct appldata_ops *ops;
138 mutex_lock(&appldata_ops_mutex);
139 list_for_each(lh, &appldata_ops_list) {
140 ops = list_entry(lh, struct appldata_ops, list);
141 if (ops->active == 1) {
142 ops->callback(ops->data);
145 mutex_unlock(&appldata_ops_mutex);
152 * prepare parameter list, issue DIAG 0xDC
154 int appldata_diag(char record_nr, u16 function, unsigned long buffer,
155 u16 length, char *mod_lvl)
157 struct appldata_product_id id = {
158 .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
159 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
160 .prod_fn = 0xD5D3, /* "NL" */
161 .version_nr = 0xF2F6, /* "26" */
162 .release_nr = 0xF0F1, /* "01" */
165 id.record_nr = record_nr;
166 id.mod_lvl = (mod_lvl[0]) << 8 | mod_lvl[1];
167 return appldata_asm(&id, function, (void *) buffer, length);
169 /************************ timer, work, DIAG <END> ****************************/
172 /****************************** /proc stuff **********************************/
175 * appldata_mod_vtimer_wrap()
177 * wrapper function for mod_virt_timer(), because smp_call_function_single()
178 * accepts only one parameter.
180 static void __appldata_mod_vtimer_wrap(void *p) {
182 struct vtimer_list *timer;
185 mod_virt_timer_periodic(args->timer, args->expires);
188 #define APPLDATA_ADD_TIMER 0
189 #define APPLDATA_DEL_TIMER 1
190 #define APPLDATA_MOD_TIMER 2
193 * __appldata_vtimer_setup()
195 * Add, delete or modify virtual timers on all online cpus.
196 * The caller needs to get the appldata_timer_lock spinlock.
199 __appldata_vtimer_setup(int cmd)
201 u64 per_cpu_interval;
205 case APPLDATA_ADD_TIMER:
206 if (appldata_timer_active)
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;
212 smp_call_function_single(i, add_virt_timer_periodic,
213 &per_cpu(appldata_timer, i),
216 appldata_timer_active = 1;
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)
223 appldata_timer_active = 0;
224 atomic_set(&appldata_expire_count, num_online_cpus());
226 case APPLDATA_MOD_TIMER:
227 per_cpu_interval = (u64) (appldata_interval*1000 /
228 num_online_cpus()) * TOD_MICRO;
229 if (!appldata_timer_active)
231 for_each_online_cpu(i) {
233 struct vtimer_list *timer;
236 args.timer = &per_cpu(appldata_timer, i);
237 args.expires = per_cpu_interval;
238 smp_call_function_single(i, __appldata_mod_vtimer_wrap,
245 * appldata_timer_handler()
247 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
250 appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
251 void __user *buffer, size_t *lenp, loff_t *ppos)
256 if (!*lenp || *ppos) {
261 len = sprintf(buf, appldata_timer_active ? "1\n" : "0\n");
264 if (copy_to_user(buffer, buf, len))
269 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len))
272 spin_lock(&appldata_timer_lock);
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);
286 * appldata_interval_handler()
288 * Set (CPU) timer interval for collection of data (in milliseconds), show
289 * current timer interval.
292 appldata_interval_handler(ctl_table *ctl, int write, struct file *filp,
293 void __user *buffer, size_t *lenp, loff_t *ppos)
298 if (!*lenp || *ppos) {
303 len = sprintf(buf, "%i\n", appldata_interval);
306 if (copy_to_user(buffer, buf, len))
311 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len)) {
315 sscanf(buf, "%i", &interval);
320 spin_lock(&appldata_timer_lock);
321 appldata_interval = interval;
322 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
323 spin_unlock(&appldata_timer_lock);
332 * appldata_generic_handler()
334 * Generic start/stop monitoring and DIAG, show status of
335 * monitoring (0 = not in process, 1 = in process)
338 appldata_generic_handler(ctl_table *ctl, int write, struct file *filp,
339 void __user *buffer, size_t *lenp, loff_t *ppos)
341 struct appldata_ops *ops = NULL, *tmp_ops;
344 struct list_head *lh;
347 mutex_lock(&appldata_ops_mutex);
348 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) {
355 mutex_unlock(&appldata_ops_mutex);
359 if (!try_module_get(ops->owner)) { // protect this function
360 mutex_unlock(&appldata_ops_mutex);
363 mutex_unlock(&appldata_ops_mutex);
365 if (!*lenp || *ppos) {
367 module_put(ops->owner);
371 len = sprintf(buf, ops->active ? "1\n" : "0\n");
374 if (copy_to_user(buffer, buf, len)) {
375 module_put(ops->owner);
381 if (copy_from_user(buf, buffer,
382 len > sizeof(buf) ? sizeof(buf) : len)) {
383 module_put(ops->owner);
387 mutex_lock(&appldata_ops_mutex);
388 if ((buf[0] == '1') && (ops->active == 0)) {
389 // protect work queue callback
390 if (!try_module_get(ops->owner)) {
391 mutex_unlock(&appldata_ops_mutex);
392 module_put(ops->owner);
395 ops->callback(ops->data); // init record
396 rc = appldata_diag(ops->record_nr,
397 APPLDATA_START_INTERVAL_REC,
398 (unsigned long) ops->data, ops->size,
401 pr_err("Starting the data collection for %s "
402 "failed with rc=%d\n", ops->name, rc);
403 module_put(ops->owner);
406 } else if ((buf[0] == '0') && (ops->active == 1)) {
408 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
409 (unsigned long) ops->data, ops->size,
412 pr_err("Stopping the data collection for %s "
413 "failed with rc=%d\n", ops->name, rc);
414 module_put(ops->owner);
416 mutex_unlock(&appldata_ops_mutex);
420 module_put(ops->owner);
424 /*************************** /proc stuff <END> *******************************/
427 /************************* module-ops management *****************************/
429 * appldata_register_ops()
431 * update ops list, register /proc/sys entries
433 int appldata_register_ops(struct appldata_ops *ops)
435 if (ops->size > APPLDATA_MAX_REC_SIZE)
438 ops->ctl_table = kzalloc(4 * sizeof(struct ctl_table), GFP_KERNEL);
442 mutex_lock(&appldata_ops_mutex);
443 list_add(&ops->list, &appldata_ops_list);
444 mutex_unlock(&appldata_ops_mutex);
446 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];
451 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;
456 ops->sysctl_header = register_sysctl_table(ops->ctl_table);
457 if (!ops->sysctl_header)
461 mutex_lock(&appldata_ops_mutex);
462 list_del(&ops->list);
463 mutex_unlock(&appldata_ops_mutex);
464 kfree(ops->ctl_table);
469 * appldata_unregister_ops()
471 * update ops list, unregister /proc entries, stop DIAG if necessary
473 void appldata_unregister_ops(struct appldata_ops *ops)
475 mutex_lock(&appldata_ops_mutex);
476 list_del(&ops->list);
477 mutex_unlock(&appldata_ops_mutex);
478 unregister_sysctl_table(ops->sysctl_header);
479 kfree(ops->ctl_table);
481 /********************** module-ops management <END> **************************/
484 /**************************** suspend / resume *******************************/
485 static int appldata_freeze(struct device *dev)
487 struct appldata_ops *ops;
489 struct list_head *lh;
492 spin_lock(&appldata_timer_lock);
493 if (appldata_timer_active) {
494 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
495 appldata_timer_suspended = 1;
497 spin_unlock(&appldata_timer_lock);
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,
508 pr_err("Stopping the data collection for %s "
509 "failed with rc=%d\n", ops->name, rc);
512 mutex_unlock(&appldata_ops_mutex);
516 static int appldata_restore(struct device *dev)
518 struct appldata_ops *ops;
520 struct list_head *lh;
523 spin_lock(&appldata_timer_lock);
524 if (appldata_timer_suspended) {
525 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
526 appldata_timer_suspended = 0;
528 spin_unlock(&appldata_timer_lock);
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,
541 pr_err("Starting the data collection for %s "
542 "failed with rc=%d\n", ops->name, rc);
546 mutex_unlock(&appldata_ops_mutex);
550 static int appldata_thaw(struct device *dev)
552 return appldata_restore(dev);
555 static struct dev_pm_ops appldata_pm_ops = {
556 .freeze = appldata_freeze,
557 .thaw = appldata_thaw,
558 .restore = appldata_restore,
561 static struct platform_driver appldata_pdrv = {
564 .owner = THIS_MODULE,
565 .pm = &appldata_pm_ops,
568 /************************* suspend / resume <END> ****************************/
571 /******************************* init / exit *********************************/
573 static void __cpuinit appldata_online_cpu(int cpu)
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)
579 atomic_inc(&appldata_expire_count);
580 spin_lock(&appldata_timer_lock);
581 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
582 spin_unlock(&appldata_timer_lock);
585 static void __cpuinit appldata_offline_cpu(int cpu)
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());
590 queue_work(appldata_wq, &appldata_work);
592 spin_lock(&appldata_timer_lock);
593 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
594 spin_unlock(&appldata_timer_lock);
597 static int __cpuinit appldata_cpu_notify(struct notifier_block *self,
598 unsigned long action,
603 case CPU_ONLINE_FROZEN:
604 appldata_online_cpu((long) hcpu);
607 case CPU_DEAD_FROZEN:
608 appldata_offline_cpu((long) hcpu);
616 static struct notifier_block __cpuinitdata appldata_nb = {
617 .notifier_call = appldata_cpu_notify,
623 * init timer, register /proc entries
625 static int __init appldata_init(void)
629 rc = platform_driver_register(&appldata_pdrv);
633 appldata_pdev = platform_device_register_simple("appldata", -1, NULL,
635 if (IS_ERR(appldata_pdev)) {
636 rc = PTR_ERR(appldata_pdev);
639 appldata_wq = create_singlethread_workqueue("appldata");
646 for_each_online_cpu(i)
647 appldata_online_cpu(i);
650 /* Register cpu hotplug notifier */
651 register_hotcpu_notifier(&appldata_nb);
653 appldata_sysctl_header = register_sysctl_table(appldata_dir_table);
657 platform_device_unregister(appldata_pdev);
659 platform_driver_unregister(&appldata_pdrv);
663 __initcall(appldata_init);
665 /**************************** init / exit <END> ******************************/
667 EXPORT_SYMBOL_GPL(appldata_register_ops);
668 EXPORT_SYMBOL_GPL(appldata_unregister_ops);
669 EXPORT_SYMBOL_GPL(appldata_diag);
672 EXPORT_SYMBOL_GPL(si_swapinfo);
674 EXPORT_SYMBOL_GPL(nr_threads);
675 EXPORT_SYMBOL_GPL(nr_running);
676 EXPORT_SYMBOL_GPL(nr_iowait);