cpuidle-tegra.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 <[email protected]>
  8. * Gary King <[email protected]>
  9. *
  10. * Rework for 3.3 by Peter De Schrijver <[email protected]>
  11. *
  12. * Tegra20/124 driver unification by Dmitry Osipenko <[email protected]>
  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 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. RCU_NONIDLE(tegra_pm_set_cpu_in_lp2());
  148. cpu_pm_enter();
  149. switch (index) {
  150. case TEGRA_C7:
  151. err = tegra_cpuidle_c7_enter();
  152. break;
  153. case TEGRA_CC6:
  154. err = tegra_cpuidle_cc6_enter(cpu);
  155. break;
  156. default:
  157. err = -EINVAL;
  158. break;
  159. }
  160. cpu_pm_exit();
  161. RCU_NONIDLE(tegra_pm_clear_cpu_in_lp2());
  162. local_fiq_enable();
  163. return err ?: index;
  164. }
  165. static int tegra_cpuidle_adjust_state_index(int index, unsigned int cpu)
  166. {
  167. /*
  168. * On Tegra30 CPU0 can't be power-gated separately from secondary
  169. * cores because it gates the whole CPU cluster.
  170. */
  171. if (cpu > 0 || index != TEGRA_C7 || tegra_get_chip_id() != TEGRA30)
  172. return index;
  173. /* put CPU0 into C1 if C7 is requested and secondaries are online */
  174. if (!IS_ENABLED(CONFIG_PM_SLEEP) || num_online_cpus() > 1)
  175. index = TEGRA_C1;
  176. else
  177. index = TEGRA_CC6;
  178. return index;
  179. }
  180. static int tegra_cpuidle_enter(struct cpuidle_device *dev,
  181. struct cpuidle_driver *drv,
  182. int index)
  183. {
  184. unsigned int cpu = cpu_logical_map(dev->cpu);
  185. int ret;
  186. index = tegra_cpuidle_adjust_state_index(index, cpu);
  187. if (dev->states_usage[index].disable)
  188. return -1;
  189. if (index == TEGRA_C1)
  190. ret = arm_cpuidle_simple_enter(dev, drv, index);
  191. else
  192. ret = tegra_cpuidle_state_enter(dev, index, cpu);
  193. if (ret < 0) {
  194. if (ret != -EINTR || index != TEGRA_CC6)
  195. pr_err_once("failed to enter state %d err: %d\n",
  196. index, ret);
  197. index = -1;
  198. } else {
  199. index = ret;
  200. }
  201. return index;
  202. }
  203. static int tegra114_enter_s2idle(struct cpuidle_device *dev,
  204. struct cpuidle_driver *drv,
  205. int index)
  206. {
  207. tegra_cpuidle_enter(dev, drv, index);
  208. return 0;
  209. }
  210. /*
  211. * The previous versions of Tegra CPUIDLE driver used a different "legacy"
  212. * terminology for naming of the idling states, while this driver uses the
  213. * new terminology.
  214. *
  215. * Mapping of the old terms into the new ones:
  216. *
  217. * Old | New
  218. * ---------
  219. * LP3 | C1 (CPU core clock gating)
  220. * LP2 | C7 (CPU core power gating)
  221. * LP2 | CC6 (CPU cluster power gating)
  222. *
  223. * Note that that the older CPUIDLE driver versions didn't explicitly
  224. * differentiate the LP2 states because these states either used the same
  225. * code path or because CC6 wasn't supported.
  226. */
  227. static struct cpuidle_driver tegra_idle_driver = {
  228. .name = "tegra_idle",
  229. .states = {
  230. [TEGRA_C1] = ARM_CPUIDLE_WFI_STATE_PWR(600),
  231. [TEGRA_C7] = {
  232. .enter = tegra_cpuidle_enter,
  233. .exit_latency = 2000,
  234. .target_residency = 2200,
  235. .power_usage = 100,
  236. .flags = CPUIDLE_FLAG_TIMER_STOP,
  237. .name = "C7",
  238. .desc = "CPU core powered off",
  239. },
  240. [TEGRA_CC6] = {
  241. .enter = tegra_cpuidle_enter,
  242. .exit_latency = 5000,
  243. .target_residency = 10000,
  244. .power_usage = 0,
  245. .flags = CPUIDLE_FLAG_TIMER_STOP |
  246. CPUIDLE_FLAG_COUPLED,
  247. .name = "CC6",
  248. .desc = "CPU cluster powered off",
  249. },
  250. },
  251. .state_count = TEGRA_STATE_COUNT,
  252. .safe_state_index = TEGRA_C1,
  253. };
  254. static inline void tegra_cpuidle_disable_state(enum tegra_state state)
  255. {
  256. cpuidle_driver_state_disabled(&tegra_idle_driver, state, true);
  257. }
  258. /*
  259. * Tegra20 HW appears to have a bug such that PCIe device interrupts, whether
  260. * they are legacy IRQs or MSI, are lost when CC6 is enabled. To work around
  261. * this, simply disable CC6 if the PCI driver and DT node are both enabled.
  262. */
  263. void tegra_cpuidle_pcie_irqs_in_use(void)
  264. {
  265. struct cpuidle_state *state_cc6 = &tegra_idle_driver.states[TEGRA_CC6];
  266. if ((state_cc6->flags & CPUIDLE_FLAG_UNUSABLE) ||
  267. tegra_get_chip_id() != TEGRA20)
  268. return;
  269. pr_info("disabling CC6 state, since PCIe IRQs are in use\n");
  270. tegra_cpuidle_disable_state(TEGRA_CC6);
  271. }
  272. static void tegra_cpuidle_setup_tegra114_c7_state(void)
  273. {
  274. struct cpuidle_state *s = &tegra_idle_driver.states[TEGRA_C7];
  275. s->enter_s2idle = tegra114_enter_s2idle;
  276. s->target_residency = 1000;
  277. s->exit_latency = 500;
  278. }
  279. static int tegra_cpuidle_probe(struct platform_device *pdev)
  280. {
  281. if (tegra_pmc_get_suspend_mode() == TEGRA_SUSPEND_NOT_READY)
  282. return -EPROBE_DEFER;
  283. /* LP2 could be disabled in device-tree */
  284. if (tegra_pmc_get_suspend_mode() < TEGRA_SUSPEND_LP2)
  285. tegra_cpuidle_disable_state(TEGRA_CC6);
  286. /*
  287. * Required suspend-resume functionality, which is provided by the
  288. * Tegra-arch core and PMC driver, is unavailable if PM-sleep option
  289. * is disabled.
  290. */
  291. if (!IS_ENABLED(CONFIG_PM_SLEEP)) {
  292. tegra_cpuidle_disable_state(TEGRA_C7);
  293. tegra_cpuidle_disable_state(TEGRA_CC6);
  294. }
  295. /*
  296. * Generic WFI state (also known as C1 or LP3) and the coupled CPU
  297. * cluster power-off (CC6 or LP2) states are common for all Tegra SoCs.
  298. */
  299. switch (tegra_get_chip_id()) {
  300. case TEGRA20:
  301. /* Tegra20 isn't capable to power-off individual CPU cores */
  302. tegra_cpuidle_disable_state(TEGRA_C7);
  303. break;
  304. case TEGRA30:
  305. break;
  306. case TEGRA114:
  307. case TEGRA124:
  308. tegra_cpuidle_setup_tegra114_c7_state();
  309. /* coupled CC6 (LP2) state isn't implemented yet */
  310. tegra_cpuidle_disable_state(TEGRA_CC6);
  311. break;
  312. default:
  313. return -EINVAL;
  314. }
  315. return cpuidle_register(&tegra_idle_driver, cpu_possible_mask);
  316. }
  317. static struct platform_driver tegra_cpuidle_driver = {
  318. .probe = tegra_cpuidle_probe,
  319. .driver = {
  320. .name = "tegra-cpuidle",
  321. },
  322. };
  323. builtin_platform_driver(tegra_cpuidle_driver);