/arch/arm/mach-msm/cpuidle.c

https://bitbucket.org/sammyz/iscream_thunderc-2.6.35-rebase · C · 197 lines · 140 code · 41 blank · 16 comment · 10 complexity · 416665a8218039c75fb706067bba100d MD5 · raw file

  1. /* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. * 02110-1301, USA.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/cpuidle.h>
  21. #include "cpuidle.h"
  22. #include "pm.h"
  23. static DEFINE_PER_CPU_SHARED_ALIGNED(struct cpuidle_device, msm_cpuidle_devs);
  24. static struct cpuidle_driver msm_cpuidle_driver = {
  25. .name = "msm_idle",
  26. .owner = THIS_MODULE,
  27. };
  28. #ifdef CONFIG_MSM_SLEEP_STATS
  29. static void (*pre_idle_cb)(int cpu, unsigned int microsec);
  30. static void (*post_idle_cb)(int cpu, unsigned int microsec);
  31. static DEFINE_PER_CPU(struct timespec, ts_busy);
  32. static DEFINE_PER_CPU(struct atomic_notifier_head, msm_cpuidle_notifiers);
  33. static int pre_idle(int cpu)
  34. {
  35. struct timespec *ts_notidle = &per_cpu(ts_busy, cpu);
  36. struct timespec ts_now, ts_diff;
  37. getnstimeofday(&ts_now);
  38. ts_diff = timespec_sub(ts_now, *ts_notidle);
  39. if (pre_idle_cb)
  40. pre_idle_cb(cpu, (u32)timespec_to_ns(&ts_diff)/1000);
  41. return 0;
  42. }
  43. static int post_idle(int cpu, unsigned int microsec)
  44. {
  45. struct timespec *ts_notidle = &per_cpu(ts_busy, cpu);
  46. getnstimeofday(ts_notidle);
  47. if (post_idle_cb)
  48. post_idle_cb(cpu, microsec);
  49. return 0;
  50. }
  51. int msm_idle_register_cb(void (*pre)(int, unsigned int),
  52. void (*post)(int, unsigned int))
  53. {
  54. pre_idle_cb = pre;
  55. post_idle_cb = post;
  56. return 0;
  57. }
  58. EXPORT_SYMBOL(msm_idle_register_cb);
  59. int msm_cpuidle_register_notifier(unsigned int cpu, struct notifier_block *nb)
  60. {
  61. struct atomic_notifier_head *head =
  62. &per_cpu(msm_cpuidle_notifiers, cpu);
  63. return atomic_notifier_chain_register(head, nb);
  64. }
  65. EXPORT_SYMBOL(msm_cpuidle_register_notifier);
  66. int msm_cpuidle_unregister_notifier(unsigned int cpu, struct notifier_block *nb)
  67. {
  68. struct atomic_notifier_head *head =
  69. &per_cpu(msm_cpuidle_notifiers, cpu);
  70. return atomic_notifier_chain_unregister(head, nb);
  71. }
  72. EXPORT_SYMBOL(msm_cpuidle_unregister_notifier);
  73. #endif
  74. static int msm_cpuidle_enter(
  75. struct cpuidle_device *dev, struct cpuidle_state *state)
  76. {
  77. int ret;
  78. #ifdef CONFIG_MSM_SLEEP_STATS
  79. struct atomic_notifier_head *head =
  80. &__get_cpu_var(msm_cpuidle_notifiers);
  81. #endif
  82. local_irq_disable();
  83. #ifdef CONFIG_MSM_SLEEP_STATS
  84. pre_idle(dev->cpu);
  85. atomic_notifier_call_chain(head, MSM_CPUIDLE_STATE_ENTER, NULL);
  86. #endif
  87. ret = msm_pm_idle_enter((enum msm_pm_sleep_mode) (state->driver_data));
  88. #ifdef CONFIG_MSM_SLEEP_STATS
  89. post_idle(dev->cpu, ret);
  90. atomic_notifier_call_chain(head, MSM_CPUIDLE_STATE_EXIT, NULL);
  91. #endif
  92. local_irq_enable();
  93. return ret;
  94. }
  95. void __init msm_cpuidle_set_states(struct msm_cpuidle_state *states,
  96. int nr_states, struct msm_pm_platform_data *pm_data)
  97. {
  98. unsigned int cpu;
  99. for_each_possible_cpu(cpu) {
  100. struct cpuidle_device *dev = &per_cpu(msm_cpuidle_devs, cpu);
  101. int i;
  102. dev->cpu = cpu;
  103. dev->prepare = msm_pm_idle_prepare;
  104. for (i = 0; i < nr_states; i++) {
  105. struct msm_cpuidle_state *cstate = &states[i];
  106. struct cpuidle_state *state;
  107. struct msm_pm_platform_data *pm_mode;
  108. if (cstate->cpu != cpu)
  109. continue;
  110. state = &dev->states[cstate->state_nr];
  111. pm_mode = &pm_data[MSM_PM_MODE(cpu, cstate->mode_nr)];
  112. snprintf(state->name, CPUIDLE_NAME_LEN, cstate->name);
  113. snprintf(state->desc, CPUIDLE_DESC_LEN, cstate->desc);
  114. state->driver_data = (void *) cstate->mode_nr;
  115. state->flags = CPUIDLE_FLAG_TIME_VALID;
  116. state->exit_latency = pm_mode->latency;
  117. state->power_usage = 0;
  118. state->target_residency = pm_mode->residency;
  119. state->enter = msm_cpuidle_enter;
  120. }
  121. for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
  122. if (dev->states[i].enter == NULL)
  123. break;
  124. dev->state_count = i + 1;
  125. }
  126. }
  127. }
  128. int __init msm_cpuidle_init(void)
  129. {
  130. unsigned int cpu;
  131. int ret;
  132. ret = cpuidle_register_driver(&msm_cpuidle_driver);
  133. if (ret)
  134. pr_err("%s: failed to register cpuidle driver: %d\n",
  135. __func__, ret);
  136. for_each_possible_cpu(cpu) {
  137. struct cpuidle_device *dev = &per_cpu(msm_cpuidle_devs, cpu);
  138. ret = cpuidle_register_device(dev);
  139. if (ret) {
  140. pr_err("%s: failed to register cpuidle device for "
  141. "cpu %u: %d\n", __func__, cpu, ret);
  142. return ret;
  143. }
  144. }
  145. return 0;
  146. }
  147. static int __init msm_cpuidle_early_init(void)
  148. {
  149. #ifdef CONFIG_MSM_SLEEP_STATS
  150. unsigned int cpu;
  151. for_each_possible_cpu(cpu)
  152. ATOMIC_INIT_NOTIFIER_HEAD(&per_cpu(msm_cpuidle_notifiers, cpu));
  153. #endif
  154. return 0;
  155. }
  156. early_initcall(msm_cpuidle_early_init);