cpuidle-tegra20.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/clk/tegra.h>
  22. #include <linux/tick.h>
  23. #include <linux/cpuidle.h>
  24. #include <linux/cpu_pm.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <asm/cpuidle.h>
  28. #include <asm/smp_plat.h>
  29. #include <asm/suspend.h>
  30. #include "flowctrl.h"
  31. #include "iomap.h"
  32. #include "irq.h"
  33. #include "pm.h"
  34. #include "reset.h"
  35. #include "sleep.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. .states = {
  50. ARM_CPUIDLE_WFI_STATE_PWR(600),
  51. #ifdef CONFIG_PM_SLEEP
  52. {
  53. .enter = tegra20_idle_lp2_coupled,
  54. .exit_latency = 5000,
  55. .target_residency = 10000,
  56. .power_usage = 0,
  57. .flags = CPUIDLE_FLAG_COUPLED,
  58. .name = "powered-down",
  59. .desc = "CPU power gated",
  60. },
  61. #endif
  62. },
  63. .state_count = TEGRA20_MAX_STATES,
  64. .safe_state_index = 0,
  65. };
  66. #ifdef CONFIG_PM_SLEEP
  67. #ifdef CONFIG_SMP
  68. static int tegra20_reset_sleeping_cpu_1(void)
  69. {
  70. int ret = 0;
  71. tegra_pen_lock();
  72. if (readb(tegra20_cpu1_resettable_status) == CPU_RESETTABLE)
  73. tegra20_cpu_shutdown(1);
  74. else
  75. ret = -EINVAL;
  76. tegra_pen_unlock();
  77. return ret;
  78. }
  79. static void tegra20_wake_cpu1_from_reset(void)
  80. {
  81. tegra_pen_lock();
  82. tegra20_cpu_clear_resettable();
  83. /* enable cpu clock on cpu */
  84. tegra_enable_cpu_clock(1);
  85. /* take the CPU out of reset */
  86. tegra_cpu_out_of_reset(1);
  87. /* unhalt the cpu */
  88. flowctrl_write_cpu_halt(1, 0);
  89. tegra_pen_unlock();
  90. }
  91. static int tegra20_reset_cpu_1(void)
  92. {
  93. if (!cpu_online(1) || !tegra20_reset_sleeping_cpu_1())
  94. return 0;
  95. tegra20_wake_cpu1_from_reset();
  96. return -EBUSY;
  97. }
  98. #else
  99. static inline void tegra20_wake_cpu1_from_reset(void)
  100. {
  101. }
  102. static inline int tegra20_reset_cpu_1(void)
  103. {
  104. return 0;
  105. }
  106. #endif
  107. static bool tegra20_cpu_cluster_power_down(struct cpuidle_device *dev,
  108. struct cpuidle_driver *drv,
  109. int index)
  110. {
  111. while (tegra20_cpu_is_resettable_soon())
  112. cpu_relax();
  113. if (tegra20_reset_cpu_1() || !tegra_cpu_rail_off_ready())
  114. return false;
  115. tick_broadcast_enter();
  116. tegra_idle_lp2_last();
  117. tick_broadcast_exit();
  118. if (cpu_online(1))
  119. tegra20_wake_cpu1_from_reset();
  120. return true;
  121. }
  122. #ifdef CONFIG_SMP
  123. static bool tegra20_idle_enter_lp2_cpu_1(struct cpuidle_device *dev,
  124. struct cpuidle_driver *drv,
  125. int index)
  126. {
  127. tick_broadcast_enter();
  128. cpu_suspend(0, tegra20_sleep_cpu_secondary_finish);
  129. tegra20_cpu_clear_resettable();
  130. tick_broadcast_exit();
  131. return true;
  132. }
  133. #else
  134. static inline bool tegra20_idle_enter_lp2_cpu_1(struct cpuidle_device *dev,
  135. struct cpuidle_driver *drv,
  136. int index)
  137. {
  138. return true;
  139. }
  140. #endif
  141. static int tegra20_idle_lp2_coupled(struct cpuidle_device *dev,
  142. struct cpuidle_driver *drv,
  143. int index)
  144. {
  145. bool entered_lp2 = false;
  146. if (tegra_pending_sgi())
  147. ACCESS_ONCE(abort_flag) = true;
  148. cpuidle_coupled_parallel_barrier(dev, &abort_barrier);
  149. if (abort_flag) {
  150. cpuidle_coupled_parallel_barrier(dev, &abort_barrier);
  151. abort_flag = false; /* clean flag for next coming */
  152. return -EINTR;
  153. }
  154. local_fiq_disable();
  155. tegra_set_cpu_in_lp2();
  156. cpu_pm_enter();
  157. if (dev->cpu == 0)
  158. entered_lp2 = tegra20_cpu_cluster_power_down(dev, drv, index);
  159. else
  160. entered_lp2 = tegra20_idle_enter_lp2_cpu_1(dev, drv, index);
  161. cpu_pm_exit();
  162. tegra_clear_cpu_in_lp2();
  163. local_fiq_enable();
  164. smp_rmb();
  165. return entered_lp2 ? index : 0;
  166. }
  167. #endif
  168. /*
  169. * Tegra20 HW appears to have a bug such that PCIe device interrupts, whether
  170. * they are legacy IRQs or MSI, are lost when LP2 is enabled. To work around
  171. * this, simply disable LP2 if the PCI driver and DT node are both enabled.
  172. */
  173. void tegra20_cpuidle_pcie_irqs_in_use(void)
  174. {
  175. pr_info_once(
  176. "Disabling cpuidle LP2 state, since PCIe IRQs are in use\n");
  177. tegra_idle_driver.states[1].disabled = true;
  178. }
  179. int __init tegra20_cpuidle_init(void)
  180. {
  181. return cpuidle_register(&tegra_idle_driver, cpu_possible_mask);
  182. }