2 #define ITC_PREEMPT_HACK
5 #include <linux/signal.h>
6 #include <linux/sched.h>
7 #include <linux/interrupt.h>
10 #include <linux/version.h>
11 #include <linux/vmalloc.h>
12 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/smp.h>
19 #include <linux/miscdevice.h>
20 #include <linux/kernel_stat.h>
22 #include <asm/system.h>
23 #include <asm/uaccess.h>
26 #include <asm/machdep.h>
27 #define pm_idle ppc_md.power_save
31 #if !(defined CONFIG_X86 || defined CONFIG_6xx)
32 #error Support for this architecture is not written yet
35 #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
36 #include <linux/wrapper.h>
37 #include <linux/smp_lock.h>
39 #error Support for SMP on 2.4 series of kernels is not written yet
41 #define num_present_cpus() 1
42 #define num_online_cpus() 1
43 /* #define cpu_online(n) 1 */
44 #define cpu_present(n) 1
47 #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 4, 20)
48 #define iminor(inode) MINOR((inode)->i_rdev)
50 #define iminor(inode) minor((inode)->i_rdev)
55 #define itc_enter_bkl() do { \
59 #define itc_leave_bkl() do { \
64 #define itc_enter_bkl lock_kernel
65 #define itc_leave_bkl unlock_kernel
66 #ifdef ITC_PREEMPT_HACK
67 #error Attempt to enable ITC_PREEMPT_HACK on non preemptible kernel
71 MODULE_DESCRIPTION ("Idle time collector");
74 static void (*fidle_func
) (void);
75 static long idle_func
;
76 #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
77 MODULE_PARM (idle_func
, "l");
79 module_param (idle_func
, long, 0644);
81 MODULE_PARM_DESC (idle_func
, "address of default idle function");
85 static spinlock_t lock
= SPIN_LOCK_UNLOCKED
;
87 static void (*orig_pm_idle
) (void);
88 static unsigned int itc_major
;
92 struct timeval cumm_sleep_time
;
93 struct timeval sleep_started
;
98 static struct itc global_itc
[NR_CPUS
];
100 /**********************************************************************
104 **********************************************************************/
106 cpeamb (struct timeval
*c
, struct timeval
*a
, struct timeval
*b
)
108 __typeof (c
->tv_sec
) sec
= a
->tv_sec
- b
->tv_sec
;
109 __typeof (c
->tv_usec
) usec
= a
->tv_usec
- b
->tv_usec
;
114 usec
= 1000000 + usec
;
117 if (c
->tv_usec
> 1000000)
119 c
->tv_sec
+= sec
+ 1;
120 c
->tv_usec
-= 1000000;
128 #if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
130 /**********************************************************************
132 * Dummy to make sure we are unloaded properly
134 **********************************************************************/
136 dummy_wakeup (void *unused
)
138 printk (KERN_DEBUG
"waking up %d\n", smp_processor_id ());
144 /**********************************************************************
148 **********************************************************************/
149 #if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 0)
152 void default_idle (void);
156 itc_monotonic (struct timeval
*tv
)
158 do_gettimeofday (tv
);
165 struct cpu_usage_stat
*cpustat
= &kstat_this_cpu
.cpustat
;
177 struct timeval tv_irq_before
, tv_irq_after
;
178 cputime64_t irq_time_before
, irq_time_after
;
181 #ifdef ITC_PREEMPT_HACK
185 /* printk ("idle in %d\n", smp_processor_id ()); */
186 spin_lock_irqsave (&lock
, flags
);
187 itc
= &global_itc
[smp_processor_id ()];
188 itc_monotonic (&itc
->sleep_started
);
191 irq_time_before
= itc_irq_time ();
193 spin_unlock_irqrestore (&lock
, flags
);
226 spin_lock_irqsave (&lock
, flags
);
230 irq_time_after
= itc_irq_time ();
232 cputime_to_timeval (irq_time_before
, &tv_irq_before
);
233 cputime_to_timeval (irq_time_after
, &tv_irq_after
);
235 cpeamb (&itc
->cumm_sleep_time
, &tv
, &itc
->sleep_started
);
236 cpeamb (&itc
->cumm_sleep_time
, &tv_irq_before
, &tv_irq_after
);
238 cpeamb (&itc
->cumm_sleep_time
, &tv
, &itc
->sleep_started
);
242 spin_unlock_irqrestore (&lock
, flags
);
243 /* printk ("idle out %d\n", smp_processor_id ()); */
245 #ifdef ITC_PREEMPT_HACK
250 /**********************************************************************
254 **********************************************************************/
256 itc_open (struct inode
* inode
, struct file
* file
);
259 itc_release (struct inode
* inode
, struct file
* file
);
262 itc_ioctl (struct inode
* inode
, struct file
* file
,
263 unsigned int cmd
, unsigned long arg
);
266 itc_read (struct file
* file
, char * buf
, size_t count
, loff_t
* ppos
);
268 static struct file_operations itc_fops
=
270 .owner
= THIS_MODULE
,
272 .release
= itc_release
,
278 static struct miscdevice itc_misc_dev
=
280 .minor
= MISC_DYNAMIC_MINOR
,
286 itc_release (struct inode
* inode
, struct file
* filp
)
289 pm_idle
= orig_pm_idle
;
292 #if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
294 on_each_cpu (dummy_wakeup
, NULL
, 0, 1);
300 itc_open (struct inode
* inode
, struct file
* filp
)
303 const struct file_operations
*old_fops
= filp
->f_op
;
304 unsigned int minor
= iminor (inode
);
319 /* old_fops = filp->f_op; */
320 filp
->f_op
= fops_get (&itc_fops
);
324 if (pm_idle
!= itc_idle
)
326 orig_pm_idle
= pm_idle
;
336 itc_read (struct file
*file
, char * buf
, size_t count
, loff_t
* ppos
)
339 size_t itemsize
= sizeof (global_itc
[0].cumm_sleep_time
);
342 struct itc
*itc
= &global_itc
[0];
343 struct timeval tmp
[NR_CPUS
], *tmpp
;
346 if (count
< itemsize
* num_present_cpus ())
349 "attempt to read something funny %d expected %d(%d,%d)\n",
350 count
, itemsize
* num_present_cpus (),
351 itemsize
, num_present_cpus ());
355 spin_lock_irqsave (&lock
, flags
);
356 for (i
= 0; i
< NR_CPUS
; ++i
, ++itc
)
365 cpeamb (&itc
->cumm_sleep_time
, &tv
, &itc
->sleep_started
);
366 itc
->sleep_started
.tv_sec
= tv
.tv_sec
;
367 itc
->sleep_started
.tv_usec
= tv
.tv_usec
;
370 *tmpp
++ = itc
->cumm_sleep_time
;
374 spin_unlock_irqrestore (&lock
, flags
);
376 if (copy_to_user (buf
, tmp
, retval
))
378 printk (KERN_ERR
"failed to write %zu bytes to %p\n",
385 /**********************************************************************
389 **********************************************************************/
391 itc_ioctl (struct inode
* inode
, struct file
* filp
,
392 unsigned int cmd
, unsigned long arg
)
397 /**********************************************************************
401 **********************************************************************/
408 fidle_func
= (void (*) (void)) idle_func
;
420 "itc: no idle function\n"
421 "itc: boot kernel with idle=halt option\n"
422 "itc: or specify idle_func (modprobe itc idle_func=<address>)\n");
429 err
= register_chrdev (itc_major
, DEVNAME
, &itc_fops
);
430 if (err
< 0 || ((itc_major
&& err
) || (!itc_major
&& !err
)))
432 printk (KERN_ERR
"itc: register_chrdev failed itc_major=%d err=%d\n",
444 err
= misc_register (&itc_misc_dev
);
447 printk (KERN_ERR
"itc: misc_register failed err=%d\n", err
);
452 orig_pm_idle
= pm_idle
;
455 "itc: driver loaded pm_idle=%p default_idle=%p"
470 printk (KERN_DEBUG
"itc: CPUs(%d present=%d online=%d)"
480 #ifdef CONFIG_PREEMPT
484 NR_CPUS
, num_present_cpus (), num_online_cpus ());
488 /**********************************************************************
492 **********************************************************************/
496 printk (KERN_DEBUG
"itc: unloading (resetting pm_idle to %p)\n",
500 unregister_chrdev (itc_major
, DEVNAME
);
504 misc_deregister (&itc_misc_dev
);
506 printk (KERN_DEBUG
"itc: unloaded\n");