1 #ifdef CONFIG_PREEMPT_RT
2 #error Preempt RT kernels are not supported
6 #define ITC_PREEMPT_HACK
9 #include <linux/signal.h>
10 #include <linux/sched.h>
11 #include <linux/interrupt.h>
14 #include <linux/version.h>
15 #include <linux/vmalloc.h>
16 #include <linux/module.h>
17 #include <linux/delay.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/smp.h>
23 #include <linux/miscdevice.h>
24 #include <linux/kernel_stat.h>
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
29 #if defined CONFIG_6xx || defined CONFIG_PPC64
30 #include <asm/machdep.h>
31 #define pm_idle ppc_md.power_save
35 #if !(defined CONFIG_X86 || defined CONFIG_6xx || defined CONFIG_PPC64)
36 #error Support for this architecture is nto written yet
39 #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
40 #include <linux/wrapper.h>
41 #include <linux/smp_lock.h>
43 #error Support for SMP on 2.4 series of kernels is not written yet
45 #define num_present_cpus() 1
46 #define num_online_cpus() 1
47 /* #define cpu_online(n) 1 */
48 #define cpu_present(n) 1
51 #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 4, 20)
52 #define iminor(inode) MINOR((inode)->i_rdev)
54 #define iminor(inode) minor((inode)->i_rdev)
59 #define itc_enter_bkl() do { \
63 #define itc_leave_bkl() do { \
68 #define itc_enter_bkl lock_kernel
69 #define itc_leave_bkl unlock_kernel
70 #ifdef ITC_PREEMPT_HACK
71 #error Attempt to enable ITC_PREEMPT_HACK on non preemptible kernel
75 MODULE_DESCRIPTION ("Idle time collector");
78 static void (*fidle_func
) (void);
79 static long idle_func
;
80 #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
81 MODULE_PARM (idle_func
, "l");
83 module_param (idle_func
, long, 0644);
85 MODULE_PARM_DESC (idle_func
, "address of default idle function");
89 static spinlock_t lock
= SPIN_LOCK_UNLOCKED
;
91 static void (*orig_pm_idle
) (void);
92 static unsigned int itc_major
;
96 struct timeval cumm_sleep_time
;
97 struct timeval sleep_started
;
102 static struct itc global_itc
[NR_CPUS
];
104 /**********************************************************************
108 **********************************************************************/
110 cpeamb (struct timeval
*c
, struct timeval
*a
, struct timeval
*b
)
112 __typeof (c
->tv_sec
) sec
= a
->tv_sec
- b
->tv_sec
;
113 __typeof (c
->tv_usec
) usec
= a
->tv_usec
- b
->tv_usec
;
118 usec
= 1000000 + usec
;
121 if (c
->tv_usec
> 1000000)
123 c
->tv_sec
+= sec
+ 1;
124 c
->tv_usec
-= 1000000;
132 #if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
134 /**********************************************************************
136 * Dummy to make sure we are unloaded properly
138 **********************************************************************/
140 dummy_wakeup (void *unused
)
142 printk (KERN_DEBUG
"waking up %d\n", smp_processor_id ());
148 /**********************************************************************
152 **********************************************************************/
153 #if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 0)
156 void default_idle (void);
160 itc_monotonic (struct timeval
*tv
)
162 do_gettimeofday (tv
);
169 struct cpu_usage_stat
*cpustat
= &kstat_this_cpu
.cpustat
;
181 struct timeval tv_irq_before
, tv_irq_after
;
182 cputime64_t irq_time_before
, irq_time_after
;
185 #ifdef ITC_PREEMPT_HACK
189 /* printk ("idle in %d\n", smp_processor_id ()); */
190 spin_lock_irqsave (&lock
, flags
);
191 itc
= &global_itc
[smp_processor_id ()];
192 itc_monotonic (&itc
->sleep_started
);
195 irq_time_before
= itc_irq_time ();
197 spin_unlock_irqrestore (&lock
, flags
);
230 spin_lock_irqsave (&lock
, flags
);
234 irq_time_after
= itc_irq_time ();
236 cputime_to_timeval (irq_time_before
, &tv_irq_before
);
237 cputime_to_timeval (irq_time_after
, &tv_irq_after
);
239 cpeamb (&itc
->cumm_sleep_time
, &tv
, &itc
->sleep_started
);
240 cpeamb (&itc
->cumm_sleep_time
, &tv_irq_before
, &tv_irq_after
);
242 cpeamb (&itc
->cumm_sleep_time
, &tv
, &itc
->sleep_started
);
246 spin_unlock_irqrestore (&lock
, flags
);
247 /* printk ("idle out %d\n", smp_processor_id ()); */
249 #ifdef ITC_PREEMPT_HACK
254 /**********************************************************************
258 **********************************************************************/
260 itc_open (struct inode
* inode
, struct file
* file
);
263 itc_release (struct inode
* inode
, struct file
* file
);
266 itc_ioctl (struct inode
* inode
, struct file
* file
,
267 unsigned int cmd
, unsigned long arg
);
270 itc_read (struct file
* file
, char * buf
, size_t count
, loff_t
* ppos
);
272 static struct file_operations itc_fops
=
274 .owner
= THIS_MODULE
,
276 .release
= itc_release
,
282 static struct miscdevice itc_misc_dev
=
284 .minor
= MISC_DYNAMIC_MINOR
,
290 itc_release (struct inode
* inode
, struct file
* filp
)
293 pm_idle
= orig_pm_idle
;
296 #if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
298 #if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 26)
299 /* 15c8b6c1aaaf1c4edd67e2f02e4d8e1bd1a51c0d */
300 on_each_cpu (dummy_wakeup
, NULL
, 1);
302 on_each_cpu (dummy_wakeup
, NULL
, 0, 1);
309 itc_open (struct inode
* inode
, struct file
* filp
)
312 const struct file_operations
*old_fops
= filp
->f_op
;
313 unsigned int minor
= iminor (inode
);
328 /* old_fops = filp->f_op; */
329 filp
->f_op
= fops_get (&itc_fops
);
333 if (pm_idle
!= itc_idle
)
335 orig_pm_idle
= pm_idle
;
345 itc_read (struct file
*file
, char * buf
, size_t count
, loff_t
* ppos
)
348 size_t itemsize
= sizeof (global_itc
[0].cumm_sleep_time
);
351 struct itc
*itc
= &global_itc
[0];
352 struct timeval tmp
[NR_CPUS
], *tmpp
;
355 if (count
< itemsize
* num_present_cpus ())
358 "attempt to read something funny %zu expected %zu(%zu,%u)\n",
359 count
, itemsize
* num_present_cpus (),
360 itemsize
, num_present_cpus ());
364 spin_lock_irqsave (&lock
, flags
);
365 for (i
= 0; i
< NR_CPUS
; ++i
, ++itc
)
374 cpeamb (&itc
->cumm_sleep_time
, &tv
, &itc
->sleep_started
);
375 itc
->sleep_started
.tv_sec
= tv
.tv_sec
;
376 itc
->sleep_started
.tv_usec
= tv
.tv_usec
;
379 *tmpp
++ = itc
->cumm_sleep_time
;
383 spin_unlock_irqrestore (&lock
, flags
);
385 if (copy_to_user (buf
, tmp
, retval
))
387 printk (KERN_ERR
"failed to write %zu bytes to %p\n",
394 /**********************************************************************
398 **********************************************************************/
400 itc_ioctl (struct inode
* inode
, struct file
* filp
,
401 unsigned int cmd
, unsigned long arg
)
406 /**********************************************************************
410 **********************************************************************/
417 fidle_func
= (void (*) (void)) idle_func
;
429 "itc: no idle function\n"
430 "itc: boot kernel with idle=halt option\n"
431 "itc: or specify idle_func (modprobe itc idle_func=<address>)\n");
438 err
= register_chrdev (itc_major
, DEVNAME
, &itc_fops
);
439 if (err
< 0 || ((itc_major
&& err
) || (!itc_major
&& !err
)))
441 printk (KERN_ERR
"itc: register_chrdev failed itc_major=%d err=%d\n",
453 err
= misc_register (&itc_misc_dev
);
456 printk (KERN_ERR
"itc: misc_register failed err=%d\n", err
);
461 orig_pm_idle
= pm_idle
;
464 "itc: driver loaded pm_idle=%p default_idle=%p"
479 printk (KERN_DEBUG
"itc: CPUs(%d present=%d online=%d)"
489 #ifdef CONFIG_PREEMPT
493 NR_CPUS
, num_present_cpus (), num_online_cpus ());
497 /**********************************************************************
501 **********************************************************************/
505 printk (KERN_DEBUG
"itc: unloading (resetting pm_idle to %p)\n",
509 unregister_chrdev (itc_major
, DEVNAME
);
513 misc_deregister (&itc_misc_dev
);
515 printk (KERN_DEBUG
"itc: unloaded\n");