cpuidle-tegra20.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * CPU idle driver for Tegra CPUs
  3. *
  4. * Copyright (c) 2010-2012, NVIDIA Corporation.
  5. * Copyright (c) 2011 Google, Inc.
  6. * Author: Colin Cross <ccross@android.com>
  7. * Gary King <gking@nvidia.com>
  8. *
  9. * Rework for 3.3 by Peter De Schrijver <pdeschrijver@nvidia.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. * more details.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/cpuidle.h>
  24. #include <linux/cpu_pm.h>
  25. #include <linux/clockchips.h>
  26. #include <linux/clk/tegra.h>
  27. #include <asm/cpuidle.h>
  28. #include <asm/proc-fns.h>
  29. #include <asm/suspend.h>
  30. #include <asm/smp_plat.h>
  31. #include "pm.h"
  32. #include "sleep.h"
  33. #include "iomap.h"
  34. #include "irq.h"
  35. #include "flowctrl.h"
  36. #ifdef CONFIG_PM_SLEEP
  37. static bool abort_flag;
  38. static atomic_t abort_barrier;
  39. static int tegra20_idle_lp2_coupled(struct cpuidle_device *dev,
  40. struct cpuidle_driver *drv,
  41. int index);
  42. #define TEGRA20_MAX_STATES 2
  43. #else
  44. #define TEGRA20_MAX_STATES 1
  45. #endif
  46. static struct cpuidle_driver tegra_idle_driver = {
  47. .name = "tegra_idle",
  48. .owner = THIS_MODULE,
  49. .en_core_tk_irqen = 1,
  50. .states = {
  51. ARM_CPUIDLE_WFI_STATE_PWR(600),
  52. #ifdef CONFIG_PM_SLEEP
  53. {
  54. .enter = tegra20_idle_lp2_coupled,
  55. .exit_latency = 5000,
  56. .target_residency = 10000,
  57. .power_usage = 0,
  58. .flags = CPUIDLE_FLAG_TIME_VALID |
  59. CPUIDLE_FLAG_COUPLED,
  60. .name = "powered-down",
  61. .desc = "CPU power gated",
  62. },
  63. #endif
  64. },
  65. .state_count = TEGRA20_MAX_STATES,
  66. .safe_state_index = 0,
  67. };
  68. static DEFINE_PER_CPU(struct cpuidle_device, tegra_idle_device);
  69. #ifdef CONFIG_PM_SLEEP
  70. #ifdef CONFIG_SMP
  71. static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
  72. static int tegra20_reset_sleeping_cpu_1(void)
  73. {
  74. int ret = 0;
  75. tegra_pen_lock();
  76. if (readl(pmc + PMC_SCRATCH41) == CPU_RESETTABLE)
  77. tegra20_cpu_shutdown(1);
  78. else
  79. ret = -EINVAL;
  80. tegra_pen_unlock();
  81. return ret;
  82. }
  83. static void tegra20_wake_cpu1_from_reset(void)
  84. {
  85. tegra_pen_lock();
  86. tegra20_cpu_clear_resettable();
  87. /* enable cpu clock on cpu */
  88. tegra_enable_cpu_clock(1);
  89. /* take the CPU out of reset */
  90. tegra_cpu_out_of_reset(1);
  91. /* unhalt the cpu */
  92. flowctrl_write_cpu_halt(1, 0);
  93. tegra_pen_unlock();
  94. }
  95. static int tegra20_reset_cpu_1(void)
  96. {
  97. if (!cpu_online(1) || !tegra20_reset_sleeping_cpu_1())
  98. return 0;
  99. tegra20_wake_cpu1_from_reset();
  100. return -EBUSY;
  101. }
  102. #else
  103. static inline void tegra20_wake_cpu1_from_reset(void)
  104. {
  105. }
  106. static inline int tegra20_reset_cpu_1(void)
  107. {
  108. return 0;
  109. }
  110. #endif
  111. static bool tegra20_cpu_cluster_power_down(struct cpuidle_device *dev,
  112. struct cpuidle_driver *drv,
  113. int index)
  114. {
  115. struct cpuidle_state *state = &drv->states[index];
  116. u32 cpu_on_time = state->exit_latency;
  117. u32 cpu_off_time = state->target_residency - state->exit_latency;
  118. while (tegra20_cpu_is_resettable_soon())
  119. cpu_relax();
  120. if (tegra20_reset_cpu_1() || !tegra_cpu_rail_off_ready())
  121. return false;
  122. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
  123. tegra_idle_lp2_last(cpu_on_time, cpu_off_time);
  124. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
  125. if (cpu_online(1))
  126. tegra20_wake_cpu1_from_reset();
  127. return true;
  128. }
  129. #ifdef CONFIG_SMP
  130. static bool tegra20_idle_enter_lp2_cpu_1(struct cpuidle_device *dev,
  131. struct cpuidle_driver *drv,
  132. int index)
  133. {
  134. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
  135. cpu_suspend(0, tegra20_sleep_cpu_secondary_finish);
  136. tegra20_cpu_clear_resettable();
  137. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
  138. return true;
  139. }
  140. #else
  141. static inline bool tegra20_idle_enter_lp2_cpu_1(struct cpuidle_device *dev,
  142. struct cpuidle_driver *drv,
  143. int index)
  144. {
  145. return true;
  146. }
  147. #endif
  148. static int tegra20_idle_lp2_coupled(struct cpuidle_device *dev,
  149. struct cpuidle_driver *drv,
  150. int index)
  151. {
  152. u32 cpu = is_smp() ? cpu_logical_map(dev->cpu) : dev->cpu;
  153. bool entered_lp2 = false;
  154. if (tegra_pending_sgi())
  155. ACCESS_ONCE(abort_flag) = true;
  156. cpuidle_coupled_parallel_barrier(dev, &abort_barrier);
  157. if (abort_flag) {
  158. cpuidle_coupled_parallel_barrier(dev, &abort_barrier);
  159. abort_flag = false; /* clean flag for next coming */
  160. return -EINTR;
  161. }
  162. local_fiq_disable();
  163. tegra_set_cpu_in_lp2(cpu);
  164. cpu_pm_enter();
  165. if (cpu == 0)
  166. entered_lp2 = tegra20_cpu_cluster_power_down(dev, drv, index);
  167. else
  168. entered_lp2 = tegra20_idle_enter_lp2_cpu_1(dev, drv, index);
  169. cpu_pm_exit();
  170. tegra_clear_cpu_in_lp2(cpu);
  171. local_fiq_enable();
  172. smp_rmb();
  173. return entered_lp2 ? index : 0;
  174. }
  175. #endif
  176. int __init tegra20_cpuidle_init(void)
  177. {
  178. int ret;
  179. unsigned int cpu;
  180. struct cpuidle_device *dev;
  181. struct cpuidle_driver *drv = &tegra_idle_driver;
  182. #ifdef CONFIG_PM_SLEEP
  183. tegra_tear_down_cpu = tegra20_tear_down_cpu;
  184. #endif
  185. ret = cpuidle_register_driver(&tegra_idle_driver);
  186. if (ret) {
  187. pr_err("CPUidle driver registration failed\n");
  188. return ret;
  189. }
  190. for_each_possible_cpu(cpu) {
  191. dev = &per_cpu(tegra_idle_device, cpu);
  192. dev->cpu = cpu;
  193. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  194. dev->coupled_cpus = *cpu_possible_mask;
  195. #endif
  196. ret = cpuidle_register_device(dev);
  197. if (ret) {
  198. pr_err("CPU%u: CPUidle device registration failed\n",
  199. cpu);
  200. return ret;
  201. }
  202. }
  203. return 0;
  204. }