cpuidle-tegra.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * CPU idle driver for Tegra CPUs
  4. *
  5. * Copyright (c) 2010-2013, NVIDIA Corporation.
  6. * Copyright (c) 2011 Google, Inc.
  7. * Author: Colin Cross <ccross@android.com>
  8. * Gary King <gking@nvidia.com>
  9. *
  10. * Rework for 3.3 by Peter De Schrijver <pdeschrijver@nvidia.com>
  11. *
  12. * Tegra20/124 driver unification by Dmitry Osipenko <digetx@gmail.com>
  13. */
  14. #define pr_fmt(fmt) "tegra-cpuidle: " fmt
  15. #include <linux/atomic.h>
  16. #include <linux/cpuidle.h>
  17. #include <linux/cpumask.h>
  18. #include <linux/cpu_pm.h>
  19. #include <linux/delay.h>
  20. #include <linux/errno.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/types.h>
  23. #include <linux/clk/tegra.h>
  24. #include <linux/firmware/trusted_foundations.h>
  25. #include <soc/tegra/cpuidle.h>
  26. #include <soc/tegra/flowctrl.h>
  27. #include <soc/tegra/fuse.h>
  28. #include <soc/tegra/irq.h>
  29. #include <soc/tegra/pm.h>
  30. #include <soc/tegra/pmc.h>
  31. #include <asm/cpuidle.h>
  32. #include <asm/firmware.h>
  33. #include <asm/smp_plat.h>
  34. #include <asm/suspend.h>
  35. enum tegra_state {
  36. TEGRA_C1,
  37. TEGRA_C7,
  38. TEGRA_CC6,
  39. TEGRA_STATE_COUNT,
  40. };
  41. static atomic_t tegra_idle_barrier;
  42. static atomic_t tegra_abort_flag;
  43. static void tegra_cpuidle_report_cpus_state(void)
  44. {
  45. unsigned long cpu, lcpu, csr;
  46. for_each_cpu(lcpu, cpu_possible_mask) {
  47. cpu = cpu_logical_map(lcpu);
  48. csr = flowctrl_read_cpu_csr(cpu);
  49. pr_err("cpu%lu: online=%d flowctrl_csr=0x%08lx\n",
  50. cpu, cpu_online(lcpu), csr);
  51. }
  52. }
  53. static int tegra_cpuidle_wait_for_secondary_cpus_parking(void)
  54. {
  55. unsigned int retries = 3;
  56. while (retries--) {
  57. unsigned int delay_us = 10;
  58. unsigned int timeout_us = 500 * 1000 / delay_us;
  59. /*
  60. * The primary CPU0 core shall wait for the secondaries
  61. * shutdown in order to power-off CPU's cluster safely.
  62. * The timeout value depends on the current CPU frequency,
  63. * it takes about 40-150us in average and over 1000us in
  64. * a worst case scenario.
  65. */
  66. do {
  67. if (tegra_cpu_rail_off_ready())
  68. return 0;
  69. udelay(delay_us);
  70. } while (timeout_us--);
  71. pr_err("secondary CPU taking too long to park\n");
  72. tegra_cpuidle_report_cpus_state();
  73. }
  74. pr_err("timed out waiting secondaries to park\n");
  75. return -ETIMEDOUT;
  76. }
  77. static void tegra_cpuidle_unpark_secondary_cpus(void)
  78. {
  79. unsigned int cpu, lcpu;
  80. for_each_cpu(lcpu, cpu_online_mask) {
  81. cpu = cpu_logical_map(lcpu);
  82. if (cpu > 0) {
  83. tegra_enable_cpu_clock(cpu);
  84. tegra_cpu_out_of_reset(cpu);
  85. flowctrl_write_cpu_halt(cpu, 0);
  86. }
  87. }
  88. }
  89. static int tegra_cpuidle_cc6_enter(unsigned int cpu)
  90. {
  91. int ret;
  92. if (cpu > 0) {
  93. ret = cpu_suspend(cpu, tegra_pm_park_secondary_cpu);
  94. } else {
  95. ret = tegra_cpuidle_wait_for_secondary_cpus_parking();
  96. if (!ret)
  97. ret = tegra_pm_enter_lp2();
  98. tegra_cpuidle_unpark_secondary_cpus();
  99. }
  100. return ret;
  101. }
  102. static int tegra_cpuidle_c7_enter(void)
  103. {
  104. int err;
  105. err = call_firmware_op(prepare_idle, TF_PM_MODE_LP2_NOFLUSH_L2);
  106. if (err && err != -ENOSYS)
  107. return err;
  108. return cpu_suspend(0, tegra30_pm_secondary_cpu_suspend);
  109. }
  110. static int tegra_cpuidle_coupled_barrier(struct cpuidle_device *dev)
  111. {
  112. if (tegra_pending_sgi()) {
  113. /*
  114. * CPU got local interrupt that will be lost after GIC's
  115. * shutdown because GIC driver doesn't save/restore the
  116. * pending SGI state across CPU cluster PM. Abort and retry
  117. * next time.
  118. */
  119. atomic_set(&tegra_abort_flag, 1);
  120. }
  121. cpuidle_coupled_parallel_barrier(dev, &tegra_idle_barrier);
  122. if (atomic_read(&tegra_abort_flag)) {
  123. cpuidle_coupled_parallel_barrier(dev, &tegra_idle_barrier);
  124. atomic_set(&tegra_abort_flag, 0);
  125. return -EINTR;
  126. }
  127. return 0;
  128. }
  129. static __cpuidle int tegra_cpuidle_state_enter(struct cpuidle_device *dev,
  130. int index, unsigned int cpu)
  131. {
  132. int err;
  133. /*
  134. * CC6 state is the "CPU cluster power-off" state. In order to
  135. * enter this state, at first the secondary CPU cores need to be
  136. * parked into offline mode, then the last CPU should clean out
  137. * remaining dirty cache lines into DRAM and trigger Flow Controller
  138. * logic that turns off the cluster's power domain (which includes
  139. * CPU cores, GIC and L2 cache).
  140. */
  141. if (index == TEGRA_CC6) {
  142. err = tegra_cpuidle_coupled_barrier(dev);
  143. if (err)
  144. return err;
  145. }
  146. local_fiq_disable();
  147. tegra_pm_set_cpu_in_lp2();
  148. cpu_pm_enter();
  149. ct_cpuidle_enter();
  150. switch (index) {
  151. case TEGRA_C7:
  152. err = tegra_cpuidle_c7_enter();
  153. break;
  154. case TEGRA_CC6:
  155. err = tegra_cpuidle_cc6_enter(cpu);
  156. break;
  157. default:
  158. err = -EINVAL;
  159. break;
  160. }
  161. ct_cpuidle_exit();
  162. cpu_pm_exit();
  163. tegra_pm_clear_cpu_in_lp2();
  164. local_fiq_enable();
  165. return err ?: index;
  166. }
  167. static int tegra_cpuidle_adjust_state_index(int index, unsigned int cpu)
  168. {
  169. /*
  170. * On Tegra30 CPU0 can't be power-gated separately from secondary
  171. * cores because it gates the whole CPU cluster.
  172. */
  173. if (cpu > 0 || index != TEGRA_C7 || tegra_get_chip_id() != TEGRA30)
  174. return index;
  175. /* put CPU0 into C1 if C7 is requested and secondaries are online */
  176. if (!IS_ENABLED(CONFIG_PM_SLEEP) || num_online_cpus() > 1)
  177. index = TEGRA_C1;
  178. else
  179. index = TEGRA_CC6;
  180. return index;
  181. }
  182. static __cpuidle int tegra_cpuidle_enter(struct cpuidle_device *dev,
  183. struct cpuidle_driver *drv,
  184. int index)
  185. {
  186. bool do_rcu = drv->states[index].flags & CPUIDLE_FLAG_RCU_IDLE;
  187. unsigned int cpu = cpu_logical_map(dev->cpu);
  188. int ret;
  189. index = tegra_cpuidle_adjust_state_index(index, cpu);
  190. if (dev->states_usage[index].disable)
  191. return -1;
  192. if (index == TEGRA_C1) {
  193. if (do_rcu)
  194. ct_cpuidle_enter();
  195. ret = arm_cpuidle_simple_enter(dev, drv, index);
  196. if (do_rcu)
  197. ct_cpuidle_exit();
  198. } else
  199. ret = tegra_cpuidle_state_enter(dev, index, cpu);
  200. if (ret < 0) {
  201. if (ret != -EINTR || index != TEGRA_CC6)
  202. pr_err_once("failed to enter state %d err: %d\n",
  203. index, ret);
  204. index = -1;
  205. } else {
  206. index = ret;
  207. }
  208. return index;
  209. }
  210. static int tegra114_enter_s2idle(struct cpuidle_device *dev,
  211. struct cpuidle_driver *drv,
  212. int index)
  213. {
  214. tegra_cpuidle_enter(dev, drv, index);
  215. return 0;
  216. }
  217. /*
  218. * The previous versions of Tegra CPUIDLE driver used a different "legacy"
  219. * terminology for naming of the idling states, while this driver uses the
  220. * new terminology.
  221. *
  222. * Mapping of the old terms into the new ones:
  223. *
  224. * Old | New
  225. * ---------
  226. * LP3 | C1 (CPU core clock gating)
  227. * LP2 | C7 (CPU core power gating)
  228. * LP2 | CC6 (CPU cluster power gating)
  229. *
  230. * Note that that the older CPUIDLE driver versions didn't explicitly
  231. * differentiate the LP2 states because these states either used the same
  232. * code path or because CC6 wasn't supported.
  233. */
  234. static struct cpuidle_driver tegra_idle_driver = {
  235. .name = "tegra_idle",
  236. .states = {
  237. [TEGRA_C1] = ARM_CPUIDLE_WFI_STATE_PWR(600),
  238. [TEGRA_C7] = {
  239. .enter = tegra_cpuidle_enter,
  240. .exit_latency = 2000,
  241. .target_residency = 2200,
  242. .power_usage = 100,
  243. .flags = CPUIDLE_FLAG_TIMER_STOP |
  244. CPUIDLE_FLAG_RCU_IDLE,
  245. .name = "C7",
  246. .desc = "CPU core powered off",
  247. },
  248. [TEGRA_CC6] = {
  249. .enter = tegra_cpuidle_enter,
  250. .exit_latency = 5000,
  251. .target_residency = 10000,
  252. .power_usage = 0,
  253. .flags = CPUIDLE_FLAG_TIMER_STOP |
  254. CPUIDLE_FLAG_RCU_IDLE |
  255. CPUIDLE_FLAG_COUPLED,
  256. .name = "CC6",
  257. .desc = "CPU cluster powered off",
  258. },
  259. },
  260. .state_count = TEGRA_STATE_COUNT,
  261. .safe_state_index = TEGRA_C1,
  262. };
  263. static inline void tegra_cpuidle_disable_state(enum tegra_state state)
  264. {
  265. cpuidle_driver_state_disabled(&tegra_idle_driver, state, true);
  266. }
  267. /*
  268. * Tegra20 HW appears to have a bug such that PCIe device interrupts, whether
  269. * they are legacy IRQs or MSI, are lost when CC6 is enabled. To work around
  270. * this, simply disable CC6 if the PCI driver and DT node are both enabled.
  271. */
  272. void tegra_cpuidle_pcie_irqs_in_use(void)
  273. {
  274. struct cpuidle_state *state_cc6 = &tegra_idle_driver.states[TEGRA_CC6];
  275. if ((state_cc6->flags & CPUIDLE_FLAG_UNUSABLE) ||
  276. tegra_get_chip_id() != TEGRA20)
  277. return;
  278. pr_info("disabling CC6 state, since PCIe IRQs are in use\n");
  279. tegra_cpuidle_disable_state(TEGRA_CC6);
  280. }
  281. static void tegra_cpuidle_setup_tegra114_c7_state(void)
  282. {
  283. struct cpuidle_state *s = &tegra_idle_driver.states[TEGRA_C7];
  284. s->enter_s2idle = tegra114_enter_s2idle;
  285. s->target_residency = 1000;
  286. s->exit_latency = 500;
  287. }
  288. static int tegra_cpuidle_probe(struct platform_device *pdev)
  289. {
  290. if (tegra_pmc_get_suspend_mode() == TEGRA_SUSPEND_NOT_READY)
  291. return -EPROBE_DEFER;
  292. /* LP2 could be disabled in device-tree */
  293. if (tegra_pmc_get_suspend_mode() < TEGRA_SUSPEND_LP2)
  294. tegra_cpuidle_disable_state(TEGRA_CC6);
  295. /*
  296. * Required suspend-resume functionality, which is provided by the
  297. * Tegra-arch core and PMC driver, is unavailable if PM-sleep option
  298. * is disabled.
  299. */
  300. if (!IS_ENABLED(CONFIG_PM_SLEEP)) {
  301. tegra_cpuidle_disable_state(TEGRA_C7);
  302. tegra_cpuidle_disable_state(TEGRA_CC6);
  303. }
  304. /*
  305. * Generic WFI state (also known as C1 or LP3) and the coupled CPU
  306. * cluster power-off (CC6 or LP2) states are common for all Tegra SoCs.
  307. */
  308. switch (tegra_get_chip_id()) {
  309. case TEGRA20:
  310. /* Tegra20 isn't capable to power-off individual CPU cores */
  311. tegra_cpuidle_disable_state(TEGRA_C7);
  312. break;
  313. case TEGRA30:
  314. break;
  315. case TEGRA114:
  316. case TEGRA124:
  317. tegra_cpuidle_setup_tegra114_c7_state();
  318. /* coupled CC6 (LP2) state isn't implemented yet */
  319. tegra_cpuidle_disable_state(TEGRA_CC6);
  320. break;
  321. default:
  322. return -EINVAL;
  323. }
  324. return cpuidle_register(&tegra_idle_driver, cpu_possible_mask);
  325. }
  326. static struct platform_driver tegra_cpuidle_driver = {
  327. .probe = tegra_cpuidle_probe,
  328. .driver = {
  329. .name = "tegra-cpuidle",
  330. },
  331. };
  332. builtin_platform_driver(tegra_cpuidle_driver);