ladder.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * ladder.c - the residency ladder algorithm
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. * Copyright (C) 2004, 2005 Dominik Brodowski <linux@brodo.de>
  7. *
  8. * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  9. * Shaohua Li <shaohua.li@intel.com>
  10. * Adam Belay <abelay@novell.com>
  11. *
  12. * This code is licenced under the GPL.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/cpuidle.h>
  16. #include <linux/pm_qos.h>
  17. #include <linux/module.h>
  18. #include <linux/jiffies.h>
  19. #include <asm/io.h>
  20. #include <asm/uaccess.h>
  21. #define PROMOTION_COUNT 4
  22. #define DEMOTION_COUNT 1
  23. struct ladder_device_state {
  24. struct {
  25. u32 promotion_count;
  26. u32 demotion_count;
  27. u32 promotion_time;
  28. u32 demotion_time;
  29. } threshold;
  30. struct {
  31. int promotion_count;
  32. int demotion_count;
  33. } stats;
  34. };
  35. struct ladder_device {
  36. struct ladder_device_state states[CPUIDLE_STATE_MAX];
  37. int last_state_idx;
  38. };
  39. static DEFINE_PER_CPU(struct ladder_device, ladder_devices);
  40. /**
  41. * ladder_do_selection - prepares private data for a state change
  42. * @ldev: the ladder device
  43. * @old_idx: the current state index
  44. * @new_idx: the new target state index
  45. */
  46. static inline void ladder_do_selection(struct ladder_device *ldev,
  47. int old_idx, int new_idx)
  48. {
  49. ldev->states[old_idx].stats.promotion_count = 0;
  50. ldev->states[old_idx].stats.demotion_count = 0;
  51. ldev->last_state_idx = new_idx;
  52. }
  53. /**
  54. * ladder_select_state - selects the next state to enter
  55. * @drv: cpuidle driver
  56. * @dev: the CPU
  57. */
  58. static int ladder_select_state(struct cpuidle_driver *drv,
  59. struct cpuidle_device *dev)
  60. {
  61. struct ladder_device *ldev = &__get_cpu_var(ladder_devices);
  62. struct ladder_device_state *last_state;
  63. int last_residency, last_idx = ldev->last_state_idx;
  64. int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
  65. /* Special case when user has set very strict latency requirement */
  66. if (unlikely(latency_req == 0)) {
  67. ladder_do_selection(ldev, last_idx, 0);
  68. return 0;
  69. }
  70. last_state = &ldev->states[last_idx];
  71. if (drv->states[last_idx].flags & CPUIDLE_FLAG_TIME_VALID) {
  72. last_residency = cpuidle_get_last_residency(dev) - \
  73. drv->states[last_idx].exit_latency;
  74. }
  75. else
  76. last_residency = last_state->threshold.promotion_time + 1;
  77. /* consider promotion */
  78. if (last_idx < drv->state_count - 1 &&
  79. !dev->states_usage[last_idx + 1].disable &&
  80. last_residency > last_state->threshold.promotion_time &&
  81. drv->states[last_idx + 1].exit_latency <= latency_req) {
  82. last_state->stats.promotion_count++;
  83. last_state->stats.demotion_count = 0;
  84. if (last_state->stats.promotion_count >= last_state->threshold.promotion_count) {
  85. ladder_do_selection(ldev, last_idx, last_idx + 1);
  86. return last_idx + 1;
  87. }
  88. }
  89. /* consider demotion */
  90. if (last_idx > CPUIDLE_DRIVER_STATE_START &&
  91. (dev->states_usage[last_idx].disable ||
  92. drv->states[last_idx].exit_latency > latency_req)) {
  93. int i;
  94. for (i = last_idx - 1; i > CPUIDLE_DRIVER_STATE_START; i--) {
  95. if (drv->states[i].exit_latency <= latency_req)
  96. break;
  97. }
  98. ladder_do_selection(ldev, last_idx, i);
  99. return i;
  100. }
  101. if (last_idx > CPUIDLE_DRIVER_STATE_START &&
  102. last_residency < last_state->threshold.demotion_time) {
  103. last_state->stats.demotion_count++;
  104. last_state->stats.promotion_count = 0;
  105. if (last_state->stats.demotion_count >= last_state->threshold.demotion_count) {
  106. ladder_do_selection(ldev, last_idx, last_idx - 1);
  107. return last_idx - 1;
  108. }
  109. }
  110. /* otherwise remain at the current state */
  111. return last_idx;
  112. }
  113. /**
  114. * ladder_enable_device - setup for the governor
  115. * @drv: cpuidle driver
  116. * @dev: the CPU
  117. */
  118. static int ladder_enable_device(struct cpuidle_driver *drv,
  119. struct cpuidle_device *dev)
  120. {
  121. int i;
  122. struct ladder_device *ldev = &per_cpu(ladder_devices, dev->cpu);
  123. struct ladder_device_state *lstate;
  124. struct cpuidle_state *state;
  125. ldev->last_state_idx = CPUIDLE_DRIVER_STATE_START;
  126. for (i = 0; i < drv->state_count; i++) {
  127. state = &drv->states[i];
  128. lstate = &ldev->states[i];
  129. lstate->stats.promotion_count = 0;
  130. lstate->stats.demotion_count = 0;
  131. lstate->threshold.promotion_count = PROMOTION_COUNT;
  132. lstate->threshold.demotion_count = DEMOTION_COUNT;
  133. if (i < drv->state_count - 1)
  134. lstate->threshold.promotion_time = state->exit_latency;
  135. if (i > 0)
  136. lstate->threshold.demotion_time = state->exit_latency;
  137. }
  138. return 0;
  139. }
  140. /**
  141. * ladder_reflect - update the correct last_state_idx
  142. * @dev: the CPU
  143. * @index: the index of actual state entered
  144. */
  145. static void ladder_reflect(struct cpuidle_device *dev, int index)
  146. {
  147. struct ladder_device *ldev = &__get_cpu_var(ladder_devices);
  148. if (index > 0)
  149. ldev->last_state_idx = index;
  150. }
  151. static struct cpuidle_governor ladder_governor = {
  152. .name = "ladder",
  153. .rating = 10,
  154. .enable = ladder_enable_device,
  155. .select = ladder_select_state,
  156. .reflect = ladder_reflect,
  157. .owner = THIS_MODULE,
  158. };
  159. /**
  160. * init_ladder - initializes the governor
  161. */
  162. static int __init init_ladder(void)
  163. {
  164. return cpuidle_register_governor(&ladder_governor);
  165. }
  166. /**
  167. * exit_ladder - exits the governor
  168. */
  169. static void __exit exit_ladder(void)
  170. {
  171. cpuidle_unregister_governor(&ladder_governor);
  172. }
  173. MODULE_LICENSE("GPL");
  174. module_init(init_ladder);
  175. module_exit(exit_ladder);