blob: 87a22092b68f8b152a7df862d4c47604e10edee5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Base infrastructure for Linux-z/VM Monitor Stream, Stage 1.
3 * Exports appldata_register_ops() and appldata_unregister_ops() for the
4 * data gathering modules.
5 *
Gerald Schaefer524dbcd2009-06-16 10:30:36 +02006 * Copyright IBM Corp. 2003, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Gerald Schaefer5b5dd212006-06-29 15:08:35 +02008 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Gerald Schaefere7534b02008-12-25 13:39:41 +010011#define KMSG_COMPONENT "appldata"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/slab.h>
17#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/interrupt.h>
19#include <linux/proc_fs.h>
Heiko Carstens2dcea572006-09-29 01:58:41 -070020#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/swap.h>
22#include <linux/pagemap.h>
23#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/notifier.h>
25#include <linux/cpu.h>
Gerald Schaeferf26d5832005-06-04 15:43:33 -070026#include <linux/workqueue.h>
Gerald Schaefer524dbcd2009-06-16 10:30:36 +020027#include <linux/suspend.h>
28#include <linux/platform_device.h>
Gerald Schaefer1f38d612006-09-20 15:59:26 +020029#include <asm/appldata.h>
Martin Schwidefsky27f6b412012-07-20 11:15:08 +020030#include <asm/vtimer.h>
Gerald Schaefer1f38d612006-09-20 15:59:26 +020031#include <asm/uaccess.h>
32#include <asm/io.h>
33#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include "appldata.h"
36
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
39 sampling interval in
40 milliseconds */
41
42#define TOD_MICRO 0x01000 /* nr. of TOD clock units
43 for 1 microsecond */
Gerald Schaefer524dbcd2009-06-16 10:30:36 +020044
45static struct platform_device *appldata_pdev;
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * /proc entries (sysctl)
49 */
50static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata";
Alexey Dobriyan8d65af72009-09-23 15:57:19 -070051static int appldata_timer_handler(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 void __user *buffer, size_t *lenp, loff_t *ppos);
53static int appldata_interval_handler(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 void __user *buffer,
55 size_t *lenp, loff_t *ppos);
56
57static struct ctl_table_header *appldata_sysctl_header;
58static struct ctl_table appldata_table[] = {
59 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 .procname = "timer",
61 .mode = S_IRUGO | S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -080062 .proc_handler = appldata_timer_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 },
64 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 .procname = "interval",
66 .mode = S_IRUGO | S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -080067 .proc_handler = appldata_interval_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 },
Heiko Carstens37e3a6a2007-11-20 11:13:34 +010069 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
72static struct ctl_table appldata_dir_table[] = {
73 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 .procname = appldata_proc_name,
75 .maxlen = 0,
76 .mode = S_IRUGO | S_IXUGO,
77 .child = appldata_table,
78 },
Heiko Carstens37e3a6a2007-11-20 11:13:34 +010079 { },
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
82/*
83 * Timer
84 */
Martin Schwidefsky27f6b412012-07-20 11:15:08 +020085static struct vtimer_list appldata_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87static DEFINE_SPINLOCK(appldata_timer_lock);
88static int appldata_interval = APPLDATA_CPU_INTERVAL;
89static int appldata_timer_active;
Gerald Schaefer524dbcd2009-06-16 10:30:36 +020090static int appldata_timer_suspended = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/*
Gerald Schaeferf26d5832005-06-04 15:43:33 -070093 * Work queue
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
Gerald Schaeferf26d5832005-06-04 15:43:33 -070095static struct workqueue_struct *appldata_wq;
David Howells6d5aefb2006-12-05 19:36:26 +000096static void appldata_work_fn(struct work_struct *work);
97static DECLARE_WORK(appldata_work, appldata_work_fn);
Gerald Schaeferf26d5832005-06-04 15:43:33 -070098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/*
101 * Ops list
102 */
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200103static DEFINE_MUTEX(appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static LIST_HEAD(appldata_ops_list);
105
106
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700107/*************************** timer, work, DIAG *******************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
109 * appldata_timer_function()
110 *
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700111 * schedule work and reschedule timer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
Heiko Carstens9d0a57c2006-10-11 15:31:26 +0200113static void appldata_timer_function(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Martin Schwidefsky27f6b412012-07-20 11:15:08 +0200115 queue_work(appldata_wq, (struct work_struct *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118/*
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700119 * appldata_work_fn()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 *
121 * call data gathering function for each (active) module
122 */
David Howells6d5aefb2006-12-05 19:36:26 +0000123static void appldata_work_fn(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 struct list_head *lh;
126 struct appldata_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200128 mutex_lock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 list_for_each(lh, &appldata_ops_list) {
130 ops = list_entry(lh, struct appldata_ops, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (ops->active == 1) {
132 ops->callback(ops->data);
133 }
134 }
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200135 mutex_unlock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138/*
139 * appldata_diag()
140 *
141 * prepare parameter list, issue DIAG 0xDC
142 */
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200143int appldata_diag(char record_nr, u16 function, unsigned long buffer,
144 u16 length, char *mod_lvl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Gerald Schaefer1f38d612006-09-20 15:59:26 +0200146 struct appldata_product_id id = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
Gerald Schaefer1f38d612006-09-20 15:59:26 +0200148 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
149 .prod_fn = 0xD5D3, /* "NL" */
Gerald Schaefer1f38d612006-09-20 15:59:26 +0200150 .version_nr = 0xF2F6, /* "26" */
151 .release_nr = 0xF0F1, /* "01" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 };
153
Gerald Schaefer925afbd2006-09-28 16:55:23 +0200154 id.record_nr = record_nr;
155 id.mod_lvl = (mod_lvl[0]) << 8 | mod_lvl[1];
Gerald Schaefer1f38d612006-09-20 15:59:26 +0200156 return appldata_asm(&id, function, (void *) buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700158/************************ timer, work, DIAG <END> ****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160
161/****************************** /proc stuff **********************************/
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#define APPLDATA_ADD_TIMER 0
164#define APPLDATA_DEL_TIMER 1
165#define APPLDATA_MOD_TIMER 2
166
167/*
168 * __appldata_vtimer_setup()
169 *
170 * Add, delete or modify virtual timers on all online cpus.
171 * The caller needs to get the appldata_timer_lock spinlock.
172 */
Martin Schwidefsky27f6b412012-07-20 11:15:08 +0200173static void __appldata_vtimer_setup(int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Martin Schwidefsky27f6b412012-07-20 11:15:08 +0200175 u64 timer_interval = (u64) appldata_interval * 1000 * TOD_MICRO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 switch (cmd) {
178 case APPLDATA_ADD_TIMER:
179 if (appldata_timer_active)
180 break;
Martin Schwidefsky27f6b412012-07-20 11:15:08 +0200181 appldata_timer.expires = timer_interval;
182 add_virt_timer_periodic(&appldata_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 appldata_timer_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185 case APPLDATA_DEL_TIMER:
Martin Schwidefsky27f6b412012-07-20 11:15:08 +0200186 del_virt_timer(&appldata_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (!appldata_timer_active)
188 break;
189 appldata_timer_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 break;
191 case APPLDATA_MOD_TIMER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (!appldata_timer_active)
193 break;
Martin Schwidefsky27f6b412012-07-20 11:15:08 +0200194 mod_virt_timer_periodic(&appldata_timer, timer_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196}
197
198/*
199 * appldata_timer_handler()
200 *
201 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
202 */
203static int
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700204appldata_timer_handler(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 void __user *buffer, size_t *lenp, loff_t *ppos)
206{
207 int len;
208 char buf[2];
209
210 if (!*lenp || *ppos) {
211 *lenp = 0;
212 return 0;
213 }
214 if (!write) {
Chen Gangd5b4c2f2013-05-27 17:55:13 +0800215 strncpy(buf, appldata_timer_active ? "1\n" : "0\n",
216 ARRAY_SIZE(buf));
217 len = strnlen(buf, ARRAY_SIZE(buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (len > *lenp)
219 len = *lenp;
220 if (copy_to_user(buffer, buf, len))
221 return -EFAULT;
222 goto out;
223 }
224 len = *lenp;
225 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len))
226 return -EFAULT;
227 spin_lock(&appldata_timer_lock);
228 if (buf[0] == '1')
229 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
230 else if (buf[0] == '0')
231 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
232 spin_unlock(&appldata_timer_lock);
233out:
234 *lenp = len;
235 *ppos += len;
236 return 0;
237}
238
239/*
240 * appldata_interval_handler()
241 *
242 * Set (CPU) timer interval for collection of data (in milliseconds), show
243 * current timer interval.
244 */
245static int
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700246appldata_interval_handler(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 void __user *buffer, size_t *lenp, loff_t *ppos)
248{
249 int len, interval;
250 char buf[16];
251
252 if (!*lenp || *ppos) {
253 *lenp = 0;
254 return 0;
255 }
256 if (!write) {
257 len = sprintf(buf, "%i\n", appldata_interval);
258 if (len > *lenp)
259 len = *lenp;
260 if (copy_to_user(buffer, buf, len))
261 return -EFAULT;
262 goto out;
263 }
264 len = *lenp;
Martin Schwidefsky27f6b412012-07-20 11:15:08 +0200265 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return -EFAULT;
Gerald Schaefer95425f12006-10-27 12:39:13 +0200267 interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 sscanf(buf, "%i", &interval);
Gerald Schaeferd3ae9422008-07-14 09:59:34 +0200269 if (interval <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 spin_lock(&appldata_timer_lock);
273 appldata_interval = interval;
274 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
275 spin_unlock(&appldata_timer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276out:
277 *lenp = len;
278 *ppos += len;
279 return 0;
280}
281
282/*
283 * appldata_generic_handler()
284 *
285 * Generic start/stop monitoring and DIAG, show status of
286 * monitoring (0 = not in process, 1 = in process)
287 */
288static int
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700289appldata_generic_handler(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 void __user *buffer, size_t *lenp, loff_t *ppos)
291{
292 struct appldata_ops *ops = NULL, *tmp_ops;
293 int rc, len, found;
294 char buf[2];
295 struct list_head *lh;
296
297 found = 0;
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200298 mutex_lock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 list_for_each(lh, &appldata_ops_list) {
300 tmp_ops = list_entry(lh, struct appldata_ops, list);
301 if (&tmp_ops->ctl_table[2] == ctl) {
302 found = 1;
303 }
304 }
305 if (!found) {
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200306 mutex_unlock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return -ENODEV;
308 }
309 ops = ctl->data;
310 if (!try_module_get(ops->owner)) { // protect this function
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200311 mutex_unlock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return -ENODEV;
313 }
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200314 mutex_unlock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 if (!*lenp || *ppos) {
317 *lenp = 0;
318 module_put(ops->owner);
319 return 0;
320 }
321 if (!write) {
Chen Gangd5b4c2f2013-05-27 17:55:13 +0800322 strncpy(buf, ops->active ? "1\n" : "0\n", ARRAY_SIZE(buf));
323 len = strnlen(buf, ARRAY_SIZE(buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (len > *lenp)
325 len = *lenp;
326 if (copy_to_user(buffer, buf, len)) {
327 module_put(ops->owner);
328 return -EFAULT;
329 }
330 goto out;
331 }
332 len = *lenp;
333 if (copy_from_user(buf, buffer,
334 len > sizeof(buf) ? sizeof(buf) : len)) {
335 module_put(ops->owner);
336 return -EFAULT;
337 }
338
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200339 mutex_lock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if ((buf[0] == '1') && (ops->active == 0)) {
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700341 // protect work queue callback
342 if (!try_module_get(ops->owner)) {
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200343 mutex_unlock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 module_put(ops->owner);
345 return -ENODEV;
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 ops->callback(ops->data); // init record
348 rc = appldata_diag(ops->record_nr,
349 APPLDATA_START_INTERVAL_REC,
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200350 (unsigned long) ops->data, ops->size,
351 ops->mod_lvl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (rc != 0) {
Gerald Schaefere7534b02008-12-25 13:39:41 +0100353 pr_err("Starting the data collection for %s "
354 "failed with rc=%d\n", ops->name, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 module_put(ops->owner);
Gerald Schaeferd3ae9422008-07-14 09:59:34 +0200356 } else
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200357 ops->active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 } else if ((buf[0] == '0') && (ops->active == 1)) {
359 ops->active = 0;
360 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200361 (unsigned long) ops->data, ops->size,
362 ops->mod_lvl);
Gerald Schaeferd3ae9422008-07-14 09:59:34 +0200363 if (rc != 0)
Gerald Schaefere7534b02008-12-25 13:39:41 +0100364 pr_err("Stopping the data collection for %s "
365 "failed with rc=%d\n", ops->name, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 module_put(ops->owner);
367 }
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200368 mutex_unlock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369out:
370 *lenp = len;
371 *ppos += len;
372 module_put(ops->owner);
373 return 0;
374}
375
376/*************************** /proc stuff <END> *******************************/
377
378
379/************************* module-ops management *****************************/
380/*
381 * appldata_register_ops()
382 *
383 * update ops list, register /proc/sys entries
384 */
385int appldata_register_ops(struct appldata_ops *ops)
386{
Roel Kluin13f8b7c2008-10-28 11:10:18 +0100387 if (ops->size > APPLDATA_MAX_REC_SIZE)
Heiko Carstens37e3a6a2007-11-20 11:13:34 +0100388 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Heiko Carstens37e3a6a2007-11-20 11:13:34 +0100390 ops->ctl_table = kzalloc(4 * sizeof(struct ctl_table), GFP_KERNEL);
391 if (!ops->ctl_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200394 mutex_lock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 list_add(&ops->list, &appldata_ops_list);
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200396 mutex_unlock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 ops->ctl_table[0].procname = appldata_proc_name;
399 ops->ctl_table[0].maxlen = 0;
400 ops->ctl_table[0].mode = S_IRUGO | S_IXUGO;
401 ops->ctl_table[0].child = &ops->ctl_table[2];
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 ops->ctl_table[2].procname = ops->name;
404 ops->ctl_table[2].mode = S_IRUGO | S_IWUSR;
405 ops->ctl_table[2].proc_handler = appldata_generic_handler;
406 ops->ctl_table[2].data = ops;
407
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800408 ops->sysctl_header = register_sysctl_table(ops->ctl_table);
Heiko Carstens37e3a6a2007-11-20 11:13:34 +0100409 if (!ops->sysctl_header)
410 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return 0;
Heiko Carstens37e3a6a2007-11-20 11:13:34 +0100412out:
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200413 mutex_lock(&appldata_ops_mutex);
Heiko Carstens37e3a6a2007-11-20 11:13:34 +0100414 list_del(&ops->list);
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200415 mutex_unlock(&appldata_ops_mutex);
Heiko Carstens37e3a6a2007-11-20 11:13:34 +0100416 kfree(ops->ctl_table);
417 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420/*
421 * appldata_unregister_ops()
422 *
423 * update ops list, unregister /proc entries, stop DIAG if necessary
424 */
425void appldata_unregister_ops(struct appldata_ops *ops)
426{
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200427 mutex_lock(&appldata_ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 list_del(&ops->list);
Gerald Schaeferb1ad1712009-04-23 13:58:07 +0200429 mutex_unlock(&appldata_ops_mutex);
Al Viro330d57f2005-11-04 10:18:40 +0000430 unregister_sysctl_table(ops->sysctl_header);
Heiko Carstens37e3a6a2007-11-20 11:13:34 +0100431 kfree(ops->ctl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433/********************** module-ops management <END> **************************/
434
435
Gerald Schaefer524dbcd2009-06-16 10:30:36 +0200436/**************************** suspend / resume *******************************/
437static int appldata_freeze(struct device *dev)
438{
439 struct appldata_ops *ops;
440 int rc;
441 struct list_head *lh;
442
Gerald Schaefer524dbcd2009-06-16 10:30:36 +0200443 spin_lock(&appldata_timer_lock);
444 if (appldata_timer_active) {
445 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
446 appldata_timer_suspended = 1;
447 }
448 spin_unlock(&appldata_timer_lock);
Gerald Schaefer524dbcd2009-06-16 10:30:36 +0200449
450 mutex_lock(&appldata_ops_mutex);
451 list_for_each(lh, &appldata_ops_list) {
452 ops = list_entry(lh, struct appldata_ops, list);
453 if (ops->active == 1) {
454 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
455 (unsigned long) ops->data, ops->size,
456 ops->mod_lvl);
457 if (rc != 0)
458 pr_err("Stopping the data collection for %s "
459 "failed with rc=%d\n", ops->name, rc);
460 }
461 }
462 mutex_unlock(&appldata_ops_mutex);
463 return 0;
464}
465
466static int appldata_restore(struct device *dev)
467{
468 struct appldata_ops *ops;
469 int rc;
470 struct list_head *lh;
471
Gerald Schaefer524dbcd2009-06-16 10:30:36 +0200472 spin_lock(&appldata_timer_lock);
473 if (appldata_timer_suspended) {
474 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
475 appldata_timer_suspended = 0;
476 }
477 spin_unlock(&appldata_timer_lock);
Gerald Schaefer524dbcd2009-06-16 10:30:36 +0200478
479 mutex_lock(&appldata_ops_mutex);
480 list_for_each(lh, &appldata_ops_list) {
481 ops = list_entry(lh, struct appldata_ops, list);
482 if (ops->active == 1) {
483 ops->callback(ops->data); // init record
484 rc = appldata_diag(ops->record_nr,
485 APPLDATA_START_INTERVAL_REC,
486 (unsigned long) ops->data, ops->size,
487 ops->mod_lvl);
488 if (rc != 0) {
489 pr_err("Starting the data collection for %s "
490 "failed with rc=%d\n", ops->name, rc);
491 }
492 }
493 }
494 mutex_unlock(&appldata_ops_mutex);
495 return 0;
496}
497
498static int appldata_thaw(struct device *dev)
499{
500 return appldata_restore(dev);
501}
502
Alexey Dobriyan47145212009-12-14 18:00:08 -0800503static const struct dev_pm_ops appldata_pm_ops = {
Gerald Schaefer524dbcd2009-06-16 10:30:36 +0200504 .freeze = appldata_freeze,
505 .thaw = appldata_thaw,
506 .restore = appldata_restore,
507};
508
509static struct platform_driver appldata_pdrv = {
510 .driver = {
511 .name = "appldata",
512 .owner = THIS_MODULE,
513 .pm = &appldata_pm_ops,
514 },
515};
516/************************* suspend / resume <END> ****************************/
517
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/******************************* init / exit *********************************/
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521/*
522 * appldata_init()
523 *
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700524 * init timer, register /proc entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
526static int __init appldata_init(void)
527{
Martin Schwidefsky27f6b412012-07-20 11:15:08 +0200528 int rc;
529
530 appldata_timer.function = appldata_timer_function;
531 appldata_timer.data = (unsigned long) &appldata_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Gerald Schaefer524dbcd2009-06-16 10:30:36 +0200533 rc = platform_driver_register(&appldata_pdrv);
534 if (rc)
535 return rc;
536
537 appldata_pdev = platform_device_register_simple("appldata", -1, NULL,
538 0);
539 if (IS_ERR(appldata_pdev)) {
540 rc = PTR_ERR(appldata_pdev);
541 goto out_driver;
542 }
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700543 appldata_wq = create_singlethread_workqueue("appldata");
Gerald Schaefer524dbcd2009-06-16 10:30:36 +0200544 if (!appldata_wq) {
545 rc = -ENOMEM;
546 goto out_device;
547 }
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700548
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800549 appldata_sysctl_header = register_sysctl_table(appldata_dir_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 0;
Gerald Schaefer524dbcd2009-06-16 10:30:36 +0200551
552out_device:
553 platform_device_unregister(appldata_pdev);
554out_driver:
555 platform_driver_unregister(&appldata_pdrv);
556 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Satyam Sharma076fc802007-10-12 16:11:32 +0200559__initcall(appldata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561/**************************** init / exit <END> ******************************/
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563EXPORT_SYMBOL_GPL(appldata_register_ops);
564EXPORT_SYMBOL_GPL(appldata_unregister_ops);
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200565EXPORT_SYMBOL_GPL(appldata_diag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Gerald Schaefer0c3252d2008-07-14 09:57:27 +0200567#ifdef CONFIG_SWAP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568EXPORT_SYMBOL_GPL(si_swapinfo);
Gerald Schaefer0c3252d2008-07-14 09:57:27 +0200569#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570EXPORT_SYMBOL_GPL(nr_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571EXPORT_SYMBOL_GPL(nr_running);
572EXPORT_SYMBOL_GPL(nr_iowait);