menu.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * menu.c - the menu idle governor
  3. *
  4. * Copyright (C) 2006-2007 Adam Belay <abelay@novell.com>
  5. *
  6. * This code is licenced under the GPL.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/cpuidle.h>
  10. #include <linux/pm_qos_params.h>
  11. #include <linux/time.h>
  12. #include <linux/ktime.h>
  13. #include <linux/hrtimer.h>
  14. #include <linux/tick.h>
  15. #define BREAK_FUZZ 4 /* 4 us */
  16. struct menu_device {
  17. int last_state_idx;
  18. unsigned int expected_us;
  19. unsigned int predicted_us;
  20. unsigned int last_measured_us;
  21. unsigned int elapsed_us;
  22. };
  23. static DEFINE_PER_CPU(struct menu_device, menu_devices);
  24. /**
  25. * menu_select - selects the next idle state to enter
  26. * @dev: the CPU
  27. */
  28. static int menu_select(struct cpuidle_device *dev)
  29. {
  30. struct menu_device *data = &__get_cpu_var(menu_devices);
  31. int latency_req = pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY);
  32. int i;
  33. /* Special case when user has set very strict latency requirement */
  34. if (unlikely(latency_req == 0)) {
  35. data->last_state_idx = 0;
  36. return 0;
  37. }
  38. /* determine the expected residency time */
  39. data->expected_us =
  40. (u32) ktime_to_ns(tick_nohz_get_sleep_length()) / 1000;
  41. /* find the deepest idle state that satisfies our constraints */
  42. for (i = CPUIDLE_DRIVER_STATE_START + 1; i < dev->state_count; i++) {
  43. struct cpuidle_state *s = &dev->states[i];
  44. if (s->target_residency > data->expected_us)
  45. break;
  46. if (s->target_residency > data->predicted_us)
  47. break;
  48. if (s->exit_latency > latency_req)
  49. break;
  50. }
  51. data->last_state_idx = i - 1;
  52. return i - 1;
  53. }
  54. /**
  55. * menu_reflect - attempts to guess what happened after entry
  56. * @dev: the CPU
  57. *
  58. * NOTE: it's important to be fast here because this operation will add to
  59. * the overall exit latency.
  60. */
  61. static void menu_reflect(struct cpuidle_device *dev)
  62. {
  63. struct menu_device *data = &__get_cpu_var(menu_devices);
  64. int last_idx = data->last_state_idx;
  65. unsigned int last_idle_us = cpuidle_get_last_residency(dev);
  66. struct cpuidle_state *target = &dev->states[last_idx];
  67. unsigned int measured_us;
  68. /*
  69. * Ugh, this idle state doesn't support residency measurements, so we
  70. * are basically lost in the dark. As a compromise, assume we slept
  71. * for one full standard timer tick. However, be aware that this
  72. * could potentially result in a suboptimal state transition.
  73. */
  74. if (unlikely(!(target->flags & CPUIDLE_FLAG_TIME_VALID)))
  75. last_idle_us = USEC_PER_SEC / HZ;
  76. /*
  77. * measured_us and elapsed_us are the cumulative idle time, since the
  78. * last time we were woken out of idle by an interrupt.
  79. */
  80. if (data->elapsed_us <= data->elapsed_us + last_idle_us)
  81. measured_us = data->elapsed_us + last_idle_us;
  82. else
  83. measured_us = -1;
  84. /* Predict time until next break event */
  85. data->predicted_us = max(measured_us, data->last_measured_us);
  86. if (last_idle_us + BREAK_FUZZ <
  87. data->expected_us - target->exit_latency) {
  88. data->last_measured_us = measured_us;
  89. data->elapsed_us = 0;
  90. } else {
  91. data->elapsed_us = measured_us;
  92. }
  93. }
  94. /**
  95. * menu_enable_device - scans a CPU's states and does setup
  96. * @dev: the CPU
  97. */
  98. static int menu_enable_device(struct cpuidle_device *dev)
  99. {
  100. struct menu_device *data = &per_cpu(menu_devices, dev->cpu);
  101. memset(data, 0, sizeof(struct menu_device));
  102. return 0;
  103. }
  104. static struct cpuidle_governor menu_governor = {
  105. .name = "menu",
  106. .rating = 20,
  107. .enable = menu_enable_device,
  108. .select = menu_select,
  109. .reflect = menu_reflect,
  110. .owner = THIS_MODULE,
  111. };
  112. /**
  113. * init_menu - initializes the governor
  114. */
  115. static int __init init_menu(void)
  116. {
  117. return cpuidle_register_governor(&menu_governor);
  118. }
  119. /**
  120. * exit_menu - exits the governor
  121. */
  122. static void __exit exit_menu(void)
  123. {
  124. cpuidle_unregister_governor(&menu_governor);
  125. }
  126. MODULE_LICENSE("GPL");
  127. module_init(init_menu);
  128. module_exit(exit_menu);