pm.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * CPU complex suspend & resume functions for Tegra SoCs
  3. *
  4. * Copyright (c) 2009-2012, NVIDIA Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/io.h>
  21. #include <linux/cpumask.h>
  22. #include <linux/delay.h>
  23. #include <linux/cpu_pm.h>
  24. #include <linux/suspend.h>
  25. #include <linux/err.h>
  26. #include <linux/clk/tegra.h>
  27. #include <asm/smp_plat.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/suspend.h>
  30. #include <asm/idmap.h>
  31. #include <asm/proc-fns.h>
  32. #include <asm/tlbflush.h>
  33. #include "iomap.h"
  34. #include "reset.h"
  35. #include "flowctrl.h"
  36. #include "fuse.h"
  37. #include "pm.h"
  38. #include "pmc.h"
  39. #include "sleep.h"
  40. #ifdef CONFIG_PM_SLEEP
  41. static DEFINE_SPINLOCK(tegra_lp2_lock);
  42. static u32 iram_save_size;
  43. static void *iram_save_addr;
  44. struct tegra_lp1_iram tegra_lp1_iram;
  45. void (*tegra_tear_down_cpu)(void);
  46. void (*tegra_sleep_core_finish)(unsigned long v2p);
  47. static int (*tegra_sleep_func)(unsigned long v2p);
  48. static void tegra_tear_down_cpu_init(void)
  49. {
  50. switch (tegra_chip_id) {
  51. case TEGRA20:
  52. if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
  53. tegra_tear_down_cpu = tegra20_tear_down_cpu;
  54. break;
  55. case TEGRA30:
  56. case TEGRA114:
  57. if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
  58. IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC))
  59. tegra_tear_down_cpu = tegra30_tear_down_cpu;
  60. break;
  61. }
  62. }
  63. /*
  64. * restore_cpu_complex
  65. *
  66. * restores cpu clock setting, clears flow controller
  67. *
  68. * Always called on CPU 0.
  69. */
  70. static void restore_cpu_complex(void)
  71. {
  72. int cpu = smp_processor_id();
  73. BUG_ON(cpu != 0);
  74. #ifdef CONFIG_SMP
  75. cpu = cpu_logical_map(cpu);
  76. #endif
  77. /* Restore the CPU clock settings */
  78. tegra_cpu_clock_resume();
  79. flowctrl_cpu_suspend_exit(cpu);
  80. }
  81. /*
  82. * suspend_cpu_complex
  83. *
  84. * saves pll state for use by restart_plls, prepares flow controller for
  85. * transition to suspend state
  86. *
  87. * Must always be called on cpu 0.
  88. */
  89. static void suspend_cpu_complex(void)
  90. {
  91. int cpu = smp_processor_id();
  92. BUG_ON(cpu != 0);
  93. #ifdef CONFIG_SMP
  94. cpu = cpu_logical_map(cpu);
  95. #endif
  96. /* Save the CPU clock settings */
  97. tegra_cpu_clock_suspend();
  98. flowctrl_cpu_suspend_enter(cpu);
  99. }
  100. void tegra_clear_cpu_in_lp2(void)
  101. {
  102. int phy_cpu_id = cpu_logical_map(smp_processor_id());
  103. u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
  104. spin_lock(&tegra_lp2_lock);
  105. BUG_ON(!(*cpu_in_lp2 & BIT(phy_cpu_id)));
  106. *cpu_in_lp2 &= ~BIT(phy_cpu_id);
  107. spin_unlock(&tegra_lp2_lock);
  108. }
  109. bool tegra_set_cpu_in_lp2(void)
  110. {
  111. int phy_cpu_id = cpu_logical_map(smp_processor_id());
  112. bool last_cpu = false;
  113. cpumask_t *cpu_lp2_mask = tegra_cpu_lp2_mask;
  114. u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
  115. spin_lock(&tegra_lp2_lock);
  116. BUG_ON((*cpu_in_lp2 & BIT(phy_cpu_id)));
  117. *cpu_in_lp2 |= BIT(phy_cpu_id);
  118. if ((phy_cpu_id == 0) && cpumask_equal(cpu_lp2_mask, cpu_online_mask))
  119. last_cpu = true;
  120. else if (tegra_chip_id == TEGRA20 && phy_cpu_id == 1)
  121. tegra20_cpu_set_resettable_soon();
  122. spin_unlock(&tegra_lp2_lock);
  123. return last_cpu;
  124. }
  125. int tegra_cpu_do_idle(void)
  126. {
  127. return cpu_do_idle();
  128. }
  129. static int tegra_sleep_cpu(unsigned long v2p)
  130. {
  131. setup_mm_for_reboot();
  132. tegra_sleep_cpu_finish(v2p);
  133. /* should never here */
  134. BUG();
  135. return 0;
  136. }
  137. void tegra_idle_lp2_last(void)
  138. {
  139. tegra_pmc_pm_set(TEGRA_SUSPEND_LP2);
  140. cpu_cluster_pm_enter();
  141. suspend_cpu_complex();
  142. cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu);
  143. restore_cpu_complex();
  144. cpu_cluster_pm_exit();
  145. }
  146. enum tegra_suspend_mode tegra_pm_validate_suspend_mode(
  147. enum tegra_suspend_mode mode)
  148. {
  149. /*
  150. * The Tegra devices support suspending to LP1 or lower currently.
  151. */
  152. if (mode > TEGRA_SUSPEND_LP1)
  153. return TEGRA_SUSPEND_LP1;
  154. return mode;
  155. }
  156. static int tegra_sleep_core(unsigned long v2p)
  157. {
  158. setup_mm_for_reboot();
  159. tegra_sleep_core_finish(v2p);
  160. /* should never here */
  161. BUG();
  162. return 0;
  163. }
  164. /*
  165. * tegra_lp1_iram_hook
  166. *
  167. * Hooking the address of LP1 reset vector and SDRAM self-refresh code in
  168. * SDRAM. These codes not be copied to IRAM in this fuction. We need to
  169. * copy these code to IRAM before LP0/LP1 suspend and restore the content
  170. * of IRAM after resume.
  171. */
  172. static bool tegra_lp1_iram_hook(void)
  173. {
  174. switch (tegra_chip_id) {
  175. case TEGRA20:
  176. if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
  177. tegra20_lp1_iram_hook();
  178. break;
  179. case TEGRA30:
  180. case TEGRA114:
  181. if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
  182. IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC))
  183. tegra30_lp1_iram_hook();
  184. break;
  185. default:
  186. break;
  187. }
  188. if (!tegra_lp1_iram.start_addr || !tegra_lp1_iram.end_addr)
  189. return false;
  190. iram_save_size = tegra_lp1_iram.end_addr - tegra_lp1_iram.start_addr;
  191. iram_save_addr = kmalloc(iram_save_size, GFP_KERNEL);
  192. if (!iram_save_addr)
  193. return false;
  194. return true;
  195. }
  196. static bool tegra_sleep_core_init(void)
  197. {
  198. switch (tegra_chip_id) {
  199. case TEGRA20:
  200. if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
  201. tegra20_sleep_core_init();
  202. break;
  203. case TEGRA30:
  204. case TEGRA114:
  205. if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
  206. IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC))
  207. tegra30_sleep_core_init();
  208. break;
  209. default:
  210. break;
  211. }
  212. if (!tegra_sleep_core_finish)
  213. return false;
  214. return true;
  215. }
  216. static void tegra_suspend_enter_lp1(void)
  217. {
  218. tegra_pmc_suspend();
  219. /* copy the reset vector & SDRAM shutdown code into IRAM */
  220. memcpy(iram_save_addr, IO_ADDRESS(TEGRA_IRAM_CODE_AREA),
  221. iram_save_size);
  222. memcpy(IO_ADDRESS(TEGRA_IRAM_CODE_AREA), tegra_lp1_iram.start_addr,
  223. iram_save_size);
  224. *((u32 *)tegra_cpu_lp1_mask) = 1;
  225. }
  226. static void tegra_suspend_exit_lp1(void)
  227. {
  228. tegra_pmc_resume();
  229. /* restore IRAM */
  230. memcpy(IO_ADDRESS(TEGRA_IRAM_CODE_AREA), iram_save_addr,
  231. iram_save_size);
  232. *(u32 *)tegra_cpu_lp1_mask = 0;
  233. }
  234. static const char *lp_state[TEGRA_MAX_SUSPEND_MODE] = {
  235. [TEGRA_SUSPEND_NONE] = "none",
  236. [TEGRA_SUSPEND_LP2] = "LP2",
  237. [TEGRA_SUSPEND_LP1] = "LP1",
  238. [TEGRA_SUSPEND_LP0] = "LP0",
  239. };
  240. static int tegra_suspend_enter(suspend_state_t state)
  241. {
  242. enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode();
  243. if (WARN_ON(mode < TEGRA_SUSPEND_NONE ||
  244. mode >= TEGRA_MAX_SUSPEND_MODE))
  245. return -EINVAL;
  246. pr_info("Entering suspend state %s\n", lp_state[mode]);
  247. tegra_pmc_pm_set(mode);
  248. local_fiq_disable();
  249. suspend_cpu_complex();
  250. switch (mode) {
  251. case TEGRA_SUSPEND_LP1:
  252. tegra_suspend_enter_lp1();
  253. break;
  254. case TEGRA_SUSPEND_LP2:
  255. tegra_set_cpu_in_lp2();
  256. break;
  257. default:
  258. break;
  259. }
  260. cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, tegra_sleep_func);
  261. switch (mode) {
  262. case TEGRA_SUSPEND_LP1:
  263. tegra_suspend_exit_lp1();
  264. break;
  265. case TEGRA_SUSPEND_LP2:
  266. tegra_clear_cpu_in_lp2();
  267. break;
  268. default:
  269. break;
  270. }
  271. restore_cpu_complex();
  272. local_fiq_enable();
  273. return 0;
  274. }
  275. static const struct platform_suspend_ops tegra_suspend_ops = {
  276. .valid = suspend_valid_only_mem,
  277. .enter = tegra_suspend_enter,
  278. };
  279. void __init tegra_init_suspend(void)
  280. {
  281. enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode();
  282. if (mode == TEGRA_SUSPEND_NONE)
  283. return;
  284. tegra_tear_down_cpu_init();
  285. tegra_pmc_suspend_init();
  286. if (mode >= TEGRA_SUSPEND_LP1) {
  287. if (!tegra_lp1_iram_hook() || !tegra_sleep_core_init()) {
  288. pr_err("%s: unable to allocate memory for SDRAM"
  289. "self-refresh -- LP0/LP1 unavailable\n",
  290. __func__);
  291. tegra_pmc_set_suspend_mode(TEGRA_SUSPEND_LP2);
  292. mode = TEGRA_SUSPEND_LP2;
  293. }
  294. }
  295. /* set up sleep function for cpu_suspend */
  296. switch (mode) {
  297. case TEGRA_SUSPEND_LP1:
  298. tegra_sleep_func = tegra_sleep_core;
  299. break;
  300. case TEGRA_SUSPEND_LP2:
  301. tegra_sleep_func = tegra_sleep_cpu;
  302. break;
  303. default:
  304. break;
  305. }
  306. suspend_set_ops(&tegra_suspend_ops);
  307. }
  308. #endif