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>
25 #include <linux/smp_lock.h>
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
30 #if defined CONFIG_6xx || defined CONFIG_PPC64
31 #include <asm/machdep.h>
32 #define pm_idle ppc_md.power_save
36 #if !(defined CONFIG_X86 || defined CONFIG_6xx || defined CONFIG_PPC64)
37 #error Support for this architecture is nto written yet
40 #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
41 #include <linux/wrapper.h>
42 #include <linux/smp_lock.h>
44 #error Support for SMP on 2.4 series of kernels is not written yet
46 #define num_present_cpus() 1
47 #define num_online_cpus() 1
48 /* #define cpu_online(n) 1 */
49 #define cpu_present(n) 1
52 #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 4, 20)
53 #define iminor(inode) MINOR((inode)->i_rdev)
55 #define iminor(inode) minor((inode)->i_rdev)
60 #define itc_enter_bkl() do { \
64 #define itc_leave_bkl() do { \
69 #define itc_enter_bkl lock_kernel
70 #define itc_leave_bkl unlock_kernel
71 #ifdef ITC_PREEMPT_HACK
72 #error Attempt to enable ITC_PREEMPT_HACK on non preemptible kernel
76 MODULE_DESCRIPTION ("Idle time collector");
79 static void (*fidle_func
) (void);
80 static long idle_func
;
81 #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
82 MODULE_PARM (idle_func
, "l");
84 module_param (idle_func
, long, 0644);
86 MODULE_PARM_DESC (idle_func
, "address of default idle function");
90 static spinlock_t lock
= SPIN_LOCK_UNLOCKED
;
92 static void (*orig_pm_idle
) (void);
93 static unsigned int itc_major
;
97 struct timeval cumm_sleep_time
;
98 struct timeval sleep_started
;
103 static struct itc global_itc
[NR_CPUS
];
105 /**********************************************************************
109 **********************************************************************/
111 cpeamb (struct timeval
*c
, struct timeval
*a
, struct timeval
*b
)
113 __typeof (c
->tv_sec
) sec
= a
->tv_sec
- b
->tv_sec
;
114 __typeof (c
->tv_usec
) usec
= a
->tv_usec
- b
->tv_usec
;
119 usec
= 1000000 + usec
;
122 if (c
->tv_usec
> 1000000)
124 c
->tv_sec
+= sec
+ 1;
125 c
->tv_usec
-= 1000000;
133 #if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
135 /**********************************************************************
137 * Dummy to make sure we are unloaded properly
139 **********************************************************************/
141 dummy_wakeup (void *unused
)
143 printk (KERN_DEBUG
"waking up %d\n", smp_processor_id ());
149 /**********************************************************************
153 **********************************************************************/
154 #if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 0)
157 void default_idle (void);
161 itc_monotonic (struct timeval
*tv
)
163 do_gettimeofday (tv
);
170 struct cpu_usage_stat
*cpustat
= &kstat_this_cpu
.cpustat
;
182 struct timeval tv_irq_before
, tv_irq_after
;
183 cputime64_t irq_time_before
, irq_time_after
;
186 #ifdef ITC_PREEMPT_HACK
190 /* printk ("idle in %d\n", smp_processor_id ()); */
191 spin_lock_irqsave (&lock
, flags
);
192 itc
= &global_itc
[smp_processor_id ()];
193 itc_monotonic (&itc
->sleep_started
);
196 irq_time_before
= itc_irq_time ();
198 spin_unlock_irqrestore (&lock
, flags
);
231 spin_lock_irqsave (&lock
, flags
);
235 irq_time_after
= itc_irq_time ();
237 cputime_to_timeval (irq_time_before
, &tv_irq_before
);
238 cputime_to_timeval (irq_time_after
, &tv_irq_after
);
240 cpeamb (&itc
->cumm_sleep_time
, &tv
, &itc
->sleep_started
);
241 cpeamb (&itc
->cumm_sleep_time
, &tv_irq_before
, &tv_irq_after
);
243 cpeamb (&itc
->cumm_sleep_time
, &tv
, &itc
->sleep_started
);
247 spin_unlock_irqrestore (&lock
, flags
);
248 /* printk ("idle out %d\n", smp_processor_id ()); */
250 #ifdef ITC_PREEMPT_HACK
255 /**********************************************************************
259 **********************************************************************/
261 itc_open (struct inode
* inode
, struct file
* file
);
264 itc_release (struct inode
* inode
, struct file
* file
);
267 itc_read (struct file
* file
, char * buf
, size_t count
, loff_t
* ppos
);
269 static struct file_operations itc_fops
=
271 .owner
= THIS_MODULE
,
273 .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 #if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 26)
295 /* 15c8b6c1aaaf1c4edd67e2f02e4d8e1bd1a51c0d */
296 on_each_cpu (dummy_wakeup
, NULL
, 1);
298 on_each_cpu (dummy_wakeup
, NULL
, 0, 1);
305 itc_open (struct inode
* inode
, struct file
* filp
)
308 const struct file_operations
*old_fops
= filp
->f_op
;
309 unsigned int minor
= iminor (inode
);
324 /* old_fops = filp->f_op; */
325 filp
->f_op
= fops_get (&itc_fops
);
329 if (pm_idle
!= itc_idle
)
331 orig_pm_idle
= pm_idle
;
341 itc_read (struct file
*file
, char * buf
, size_t count
, loff_t
* ppos
)
344 size_t itemsize
= sizeof (global_itc
[0].cumm_sleep_time
);
347 struct itc
*itc
= &global_itc
[0];
348 struct timeval tmp
[NR_CPUS
], *tmpp
;
351 if (count
< itemsize
* num_present_cpus ())
354 "attempt to read something funny %zu expected %zu(%zu,%u)\n",
355 count
, itemsize
* num_present_cpus (),
356 itemsize
, num_present_cpus ());
360 spin_lock_irqsave (&lock
, flags
);
361 for (i
= 0; i
< NR_CPUS
; ++i
, ++itc
)
370 cpeamb (&itc
->cumm_sleep_time
, &tv
, &itc
->sleep_started
);
371 itc
->sleep_started
.tv_sec
= tv
.tv_sec
;
372 itc
->sleep_started
.tv_usec
= tv
.tv_usec
;
375 *tmpp
++ = itc
->cumm_sleep_time
;
379 spin_unlock_irqrestore (&lock
, flags
);
381 if (copy_to_user (buf
, tmp
, retval
))
383 printk (KERN_ERR
"failed to write %zu bytes to %p\n",
390 /**********************************************************************
394 **********************************************************************/
401 fidle_func
= (void (*) (void)) idle_func
;
413 "itc: no idle function\n"
414 "itc: boot kernel with idle=halt option\n"
415 "itc: or specify idle_func (modprobe itc idle_func=<address>)\n");
422 err
= register_chrdev (itc_major
, DEVNAME
, &itc_fops
);
423 if (err
< 0 || ((itc_major
&& err
) || (!itc_major
&& !err
)))
425 printk (KERN_ERR
"itc: register_chrdev failed itc_major=%d err=%d\n",
437 err
= misc_register (&itc_misc_dev
);
440 printk (KERN_ERR
"itc: misc_register failed err=%d\n", err
);
445 orig_pm_idle
= pm_idle
;
448 "itc: driver loaded pm_idle=%p default_idle=%p"
463 printk (KERN_DEBUG
"itc: CPUs(%d present=%d online=%d)"
473 #ifdef CONFIG_PREEMPT
477 NR_CPUS
, num_present_cpus (), num_online_cpus ());
481 /**********************************************************************
485 **********************************************************************/
489 printk (KERN_DEBUG
"itc: unloading (resetting pm_idle to %p)\n",
493 unregister_chrdev (itc_major
, DEVNAME
);
497 misc_deregister (&itc_misc_dev
);
499 printk (KERN_DEBUG
"itc: unloaded\n");