cpuidle-tegra20.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #endif
  43. static struct cpuidle_state tegra_idle_states[] = {
  44. [0] = ARM_CPUIDLE_WFI_STATE_PWR(600),
  45. #ifdef CONFIG_PM_SLEEP
  46. [1] = {
  47. .enter = tegra20_idle_lp2_coupled,
  48. .exit_latency = 5000,
  49. .target_residency = 10000,
  50. .power_usage = 0,
  51. .flags = CPUIDLE_FLAG_TIME_VALID |
  52. CPUIDLE_FLAG_COUPLED,
  53. .name = "powered-down",
  54. .desc = "CPU power gated",
  55. },
  56. #endif
  57. };
  58. static struct cpuidle_driver tegra_idle_driver = {
  59. .name = "tegra_idle",
  60. .owner = THIS_MODULE,
  61. .en_core_tk_irqen = 1,
  62. };
  63. static DEFINE_PER_CPU(struct cpuidle_device, tegra_idle_device);
  64. #ifdef CONFIG_PM_SLEEP
  65. #ifdef CONFIG_SMP
  66. static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
  67. static int tegra20_reset_sleeping_cpu_1(void)
  68. {
  69. int ret = 0;
  70. tegra_pen_lock();
  71. if (readl(pmc + PMC_SCRATCH41) == CPU_RESETTABLE)
  72. tegra20_cpu_shutdown(1);
  73. else
  74. ret = -EINVAL;
  75. tegra_pen_unlock();
  76. return ret;
  77. }
  78. static void tegra20_wake_cpu1_from_reset(void)
  79. {
  80. tegra_pen_lock();
  81. tegra20_cpu_clear_resettable();
  82. /* enable cpu clock on cpu */
  83. tegra_enable_cpu_clock(1);
  84. /* take the CPU out of reset */
  85. tegra_cpu_out_of_reset(1);
  86. /* unhalt the cpu */
  87. flowctrl_write_cpu_halt(1, 0);
  88. tegra_pen_unlock();
  89. }
  90. static int tegra20_reset_cpu_1(void)
  91. {
  92. if (!cpu_online(1) || !tegra20_reset_sleeping_cpu_1())
  93. return 0;
  94. tegra20_wake_cpu1_from_reset();
  95. return -EBUSY;
  96. }
  97. #else
  98. static inline void tegra20_wake_cpu1_from_reset(void)
  99. {
  100. }
  101. static inline int tegra20_reset_cpu_1(void)
  102. {
  103. return 0;
  104. }
  105. #endif
  106. static bool tegra20_cpu_cluster_power_down(struct cpuidle_device *dev,
  107. struct cpuidle_driver *drv,
  108. int index)
  109. {
  110. struct cpuidle_state *state = &drv->states[index];
  111. u32 cpu_on_time = state->exit_latency;
  112. u32 cpu_off_time = state->target_residency - state->exit_latency;
  113. while (tegra20_cpu_is_resettable_soon())
  114. cpu_relax();
  115. if (tegra20_reset_cpu_1() || !tegra_cpu_rail_off_ready())
  116. return false;
  117. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
  118. tegra_idle_lp2_last(cpu_on_time, cpu_off_time);
  119. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
  120. if (cpu_online(1))
  121. tegra20_wake_cpu1_from_reset();
  122. return true;
  123. }
  124. #ifdef CONFIG_SMP
  125. static bool tegra20_idle_enter_lp2_cpu_1(struct cpuidle_device *dev,
  126. struct cpuidle_driver *drv,
  127. int index)
  128. {
  129. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
  130. cpu_suspend(0, tegra20_sleep_cpu_secondary_finish);
  131. tegra20_cpu_clear_resettable();
  132. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
  133. return true;
  134. }
  135. #else
  136. static inline bool tegra20_idle_enter_lp2_cpu_1(struct cpuidle_device *dev,
  137. struct cpuidle_driver *drv,
  138. int index)
  139. {
  140. return true;
  141. }
  142. #endif
  143. static int tegra20_idle_lp2_coupled(struct cpuidle_device *dev,
  144. struct cpuidle_driver *drv,
  145. int index)
  146. {
  147. u32 cpu = is_smp() ? cpu_logical_map(dev->cpu) : dev->cpu;
  148. bool entered_lp2 = false;
  149. if (tegra_pending_sgi())
  150. ACCESS_ONCE(abort_flag) = true;
  151. cpuidle_coupled_parallel_barrier(dev, &abort_barrier);
  152. if (abort_flag) {
  153. cpuidle_coupled_parallel_barrier(dev, &abort_barrier);
  154. abort_flag = false; /* clean flag for next coming */
  155. return -EINTR;
  156. }
  157. local_fiq_disable();
  158. tegra_set_cpu_in_lp2(cpu);
  159. cpu_pm_enter();
  160. if (cpu == 0)
  161. entered_lp2 = tegra20_cpu_cluster_power_down(dev, drv, index);
  162. else
  163. entered_lp2 = tegra20_idle_enter_lp2_cpu_1(dev, drv, index);
  164. cpu_pm_exit();
  165. tegra_clear_cpu_in_lp2(cpu);
  166. local_fiq_enable();
  167. smp_rmb();
  168. return entered_lp2 ? index : 0;
  169. }
  170. #endif
  171. int __init tegra20_cpuidle_init(void)
  172. {
  173. int ret;
  174. unsigned int cpu;
  175. struct cpuidle_device *dev;
  176. struct cpuidle_driver *drv = &tegra_idle_driver;
  177. #ifdef CONFIG_PM_SLEEP
  178. tegra_tear_down_cpu = tegra20_tear_down_cpu;
  179. #endif
  180. drv->state_count = ARRAY_SIZE(tegra_idle_states);
  181. memcpy(drv->states, tegra_idle_states,
  182. drv->state_count * sizeof(drv->states[0]));
  183. ret = cpuidle_register_driver(&tegra_idle_driver);
  184. if (ret) {
  185. pr_err("CPUidle driver registration failed\n");
  186. return ret;
  187. }
  188. for_each_possible_cpu(cpu) {
  189. dev = &per_cpu(tegra_idle_device, cpu);
  190. dev->cpu = cpu;
  191. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  192. dev->coupled_cpus = *cpu_possible_mask;
  193. #endif
  194. dev->state_count = drv->state_count;
  195. ret = cpuidle_register_device(dev);
  196. if (ret) {
  197. pr_err("CPU%u: CPUidle device registration failed\n",
  198. cpu);
  199. return ret;
  200. }
  201. }
  202. return 0;
  203. }