kdpmd.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <linux/version.h>
  2. #include <linux/module.h>
  3. #if defined(MODVERSIONS)
  4. #include <linux/modversions.h>
  5. #endif
  6. #include <linux/kernel.h>
  7. #include <linux/init.h>
  8. #include <linux/errno.h>
  9. #include <linux/kthread.h>
  10. #include <linux/wait.h>
  11. #include <asm/arch/kdpmd.h>
  12. #include <asm/arch/pd.h>
  13. #define _DEBUG 1
  14. //#undef _DEBUG
  15. #ifdef _DEBUG
  16. #include <linux/time.h>
  17. #endif
  18. /* global variables for kdpmd */
  19. DECLARE_MUTEX(kdpmd_sem);
  20. unsigned int kdpmd_ev;
  21. wait_queue_head_t kdpmd_wq;
  22. unsigned int dd_ev;
  23. wait_queue_head_t dd_wq;
  24. unsigned int wakeup_source;
  25. struct task_struct *kp;
  26. void handle_timeout(void)
  27. {
  28. printk(KERN_INFO "kdpmd timeout\n");
  29. /* dvfs code should go here */
  30. }
  31. /* this function should be called before device drivers turn on
  32. their clocks */
  33. void handle_drvopen(struct pm_pdtype *pd)
  34. {
  35. if (pd->state == PDSTATE_OFF) {
  36. printk("Turning on %s pd\n", pd->name);
  37. /* power on and device initializaion */
  38. pd_on(pd);
  39. }
  40. /* clock control is done by the device driver */
  41. }
  42. /* this function should be called after device drivers turn off
  43. their clocks and update pm_devtype->state value
  44. If this function takes too much time, we may employ separate
  45. lists for RUNNING devices and IDLE devices in a power domain */
  46. void handle_drvclose(struct pm_pdtype *pd)
  47. {
  48. struct list_head *temp;
  49. struct pm_devtype *pdev;
  50. /* check this power domain */
  51. list_for_each(temp, &pd->devhead) {
  52. pdev = list_entry(temp, struct pm_devtype, entry);
  53. if (pdev->state == DEV_RUNNING)
  54. return;
  55. }
  56. /* if all devices under this power domain are not RUNNING */
  57. pd_off(pd);
  58. }
  59. /* this is the thread function that we are executing */
  60. static int kdpmd_thread(void *data)
  61. {
  62. struct pm_pdtype *pd;
  63. unsigned int i = 0;
  64. #ifdef _DEBUG
  65. struct timeval tv1, tv2;
  66. #endif
  67. printk("Kernel DPM daemon thread started\n");
  68. init_waitqueue_head(&kdpmd_wq);
  69. init_waitqueue_head(&dd_wq);
  70. /* an endless loop in which we are doing our work */
  71. for (;;) {
  72. i++;
  73. /* fall asleep, wait for other processes to write to kdpmd_ev */
  74. if (wait_event_interruptible(kdpmd_wq, kdpmd_ev != 0) < 0) {
  75. printk(KERN_INFO "kdpmd wait returned -\n");
  76. return -ERESTARTSYS;
  77. }
  78. #ifdef _DEBUG
  79. do_gettimeofday(&tv1);
  80. #endif
  81. /* What if some other device opens up while servicing other */
  82. kdpmd_lock();
  83. /* We need to do a memory barrier here to be sure that
  84. the flags are visible on all CPUs.
  85. */
  86. mb();
  87. /* here we are back from sleep, either due to the timeout
  88. (2 seconds), or because we caught a signal.
  89. */
  90. if (kthread_should_stop())
  91. break;
  92. printk(KERN_INFO "kdpmd woke up: %3d\n", i);
  93. /* find out who woke me up */
  94. pd = pd_hashtbl[kdpmd_ev];
  95. /* find out why woke me up */
  96. switch (wakeup_source) {
  97. case KDPMD_TIMEOUT :
  98. handle_timeout();
  99. goto sleepagain;
  100. case KDPMD_DRVOPEN :
  101. handle_drvopen(pd);
  102. break;
  103. case KDPMD_DRVCLOSE :
  104. handle_drvclose(pd);
  105. break;
  106. case KDPMD_REMOVE : // handle rmmod call
  107. /* wait for 2 sec hoping that kthread_should_stop()
  108. will evaluate to true */
  109. wait_event_timeout(kdpmd_wq, kdpmd_ev != 0, 2 * HZ);
  110. goto sleepagain;
  111. default :
  112. break;
  113. }
  114. dd_ev = kdpmd_ev;
  115. wake_up_interruptible(&dd_wq);
  116. sleepagain:
  117. kdpmd_ev = 0;
  118. kdpmd_unlock();
  119. #ifdef _DEBUG
  120. do_gettimeofday(&tv2);
  121. printk(KERN_DEBUG " %d sec %d usec\n\n",
  122. (int)tv2.tv_sec - (int)tv1.tv_sec,
  123. (int)tv2.tv_usec - (int)tv1.tv_usec);
  124. #endif
  125. }
  126. /* here we go only in case of termination of the thread */
  127. /* returning from the thread here calls the exit functions */
  128. return (0);
  129. }
  130. void kdpmd_set_event(unsigned int devid, unsigned int drv_op)
  131. {
  132. kdpmd_lock();
  133. wakeup_source = drv_op;
  134. kdpmd_ev = devid;
  135. kdpmd_unlock();
  136. }
  137. void kdpmd_wakeup(void)
  138. {
  139. wake_up_interruptible(&kdpmd_wq);
  140. }
  141. int kdpmd_wait(unsigned int devid)
  142. {
  143. int ret;
  144. ret = wait_event_interruptible(dd_wq, dd_ev==devid);
  145. dd_ev = 0;
  146. return ret;
  147. }
  148. int __init kdpmd_init(void)
  149. {
  150. init_MUTEX(&kdpmd_sem);
  151. kp = kthread_run(kdpmd_thread, NULL, "kdpmd");
  152. printk(KERN_INFO "kdpmd created\n");
  153. return (0);
  154. }
  155. void __exit kdpmd_exit(void)
  156. {
  157. printk(KERN_INFO "kdpmd being destroyed\n");
  158. kdpmd_set_event(KDPMD_REMOVE, KDPMD_REMOVE);
  159. kdpmd_wakeup();
  160. /* terminate the kernel thread */
  161. kthread_stop(kp);
  162. return;
  163. }
  164. #ifdef MODULE
  165. module_init(kdpmd_init);
  166. module_exit(kdpmd_exit);
  167. #else
  168. __initcall(kdpmd_init);
  169. #endif
  170. EXPORT_SYMBOL(kdpmd_set_event);
  171. EXPORT_SYMBOL(kdpmd_wakeup);
  172. EXPORT_SYMBOL(kdpmd_wait);
  173. MODULE_AUTHOR("Ikhwan Lee");
  174. MODULE_LICENSE("Dual BSD/GPL");