cpuidle-tegra20.c 4.8 KB

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