cpuidle34xx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/mach-omap2/cpuidle34xx.c
  4. *
  5. * OMAP3 CPU IDLE Routines
  6. *
  7. * Copyright (C) 2008 Texas Instruments, Inc.
  8. * Rajendra Nayak <[email protected]>
  9. *
  10. * Copyright (C) 2007 Texas Instruments, Inc.
  11. * Karthik Dasu <[email protected]>
  12. *
  13. * Copyright (C) 2006 Nokia Corporation
  14. * Tony Lindgren <[email protected]>
  15. *
  16. * Copyright (C) 2005 Texas Instruments, Inc.
  17. * Richard Woodruff <[email protected]>
  18. *
  19. * Based on pm.c for omap2
  20. */
  21. #include <linux/sched.h>
  22. #include <linux/cpuidle.h>
  23. #include <linux/export.h>
  24. #include <linux/cpu_pm.h>
  25. #include <asm/cpuidle.h>
  26. #include "powerdomain.h"
  27. #include "clockdomain.h"
  28. #include "pm.h"
  29. #include "control.h"
  30. #include "common.h"
  31. #include "soc.h"
  32. /* Mach specific information to be recorded in the C-state driver_data */
  33. struct omap3_idle_statedata {
  34. u8 mpu_state;
  35. u8 core_state;
  36. u8 per_min_state;
  37. u8 flags;
  38. };
  39. static struct powerdomain *mpu_pd, *core_pd, *per_pd, *cam_pd;
  40. /*
  41. * Possible flag bits for struct omap3_idle_statedata.flags:
  42. *
  43. * OMAP_CPUIDLE_CX_NO_CLKDM_IDLE: don't allow the MPU clockdomain to go
  44. * inactive. This in turn prevents the MPU DPLL from entering autoidle
  45. * mode, so wakeup latency is greatly reduced, at the cost of additional
  46. * energy consumption. This also prevents the CORE clockdomain from
  47. * entering idle.
  48. */
  49. #define OMAP_CPUIDLE_CX_NO_CLKDM_IDLE BIT(0)
  50. /*
  51. * Prevent PER OFF if CORE is not in RETention or OFF as this would
  52. * disable PER wakeups completely.
  53. */
  54. static struct omap3_idle_statedata omap3_idle_data[] = {
  55. {
  56. .mpu_state = PWRDM_POWER_ON,
  57. .core_state = PWRDM_POWER_ON,
  58. /* In C1 do not allow PER state lower than CORE state */
  59. .per_min_state = PWRDM_POWER_ON,
  60. .flags = OMAP_CPUIDLE_CX_NO_CLKDM_IDLE,
  61. },
  62. {
  63. .mpu_state = PWRDM_POWER_ON,
  64. .core_state = PWRDM_POWER_ON,
  65. .per_min_state = PWRDM_POWER_RET,
  66. },
  67. {
  68. .mpu_state = PWRDM_POWER_RET,
  69. .core_state = PWRDM_POWER_ON,
  70. .per_min_state = PWRDM_POWER_RET,
  71. },
  72. {
  73. .mpu_state = PWRDM_POWER_OFF,
  74. .core_state = PWRDM_POWER_ON,
  75. .per_min_state = PWRDM_POWER_RET,
  76. },
  77. {
  78. .mpu_state = PWRDM_POWER_RET,
  79. .core_state = PWRDM_POWER_RET,
  80. .per_min_state = PWRDM_POWER_OFF,
  81. },
  82. {
  83. .mpu_state = PWRDM_POWER_OFF,
  84. .core_state = PWRDM_POWER_RET,
  85. .per_min_state = PWRDM_POWER_OFF,
  86. },
  87. {
  88. .mpu_state = PWRDM_POWER_OFF,
  89. .core_state = PWRDM_POWER_OFF,
  90. .per_min_state = PWRDM_POWER_OFF,
  91. },
  92. };
  93. /**
  94. * omap3_enter_idle - Programs OMAP3 to enter the specified state
  95. * @dev: cpuidle device
  96. * @drv: cpuidle driver
  97. * @index: the index of state to be entered
  98. */
  99. static int omap3_enter_idle(struct cpuidle_device *dev,
  100. struct cpuidle_driver *drv,
  101. int index)
  102. {
  103. struct omap3_idle_statedata *cx = &omap3_idle_data[index];
  104. int error;
  105. if (omap_irq_pending() || need_resched())
  106. goto return_sleep_time;
  107. /* Deny idle for C1 */
  108. if (cx->flags & OMAP_CPUIDLE_CX_NO_CLKDM_IDLE) {
  109. clkdm_deny_idle(mpu_pd->pwrdm_clkdms[0]);
  110. } else {
  111. pwrdm_set_next_pwrst(mpu_pd, cx->mpu_state);
  112. pwrdm_set_next_pwrst(core_pd, cx->core_state);
  113. }
  114. /*
  115. * Call idle CPU PM enter notifier chain so that
  116. * VFP context is saved.
  117. */
  118. if (cx->mpu_state == PWRDM_POWER_OFF) {
  119. error = cpu_pm_enter();
  120. if (error)
  121. goto out_clkdm_set;
  122. }
  123. /* Execute ARM wfi */
  124. omap_sram_idle();
  125. /*
  126. * Call idle CPU PM enter notifier chain to restore
  127. * VFP context.
  128. */
  129. if (cx->mpu_state == PWRDM_POWER_OFF &&
  130. pwrdm_read_prev_pwrst(mpu_pd) == PWRDM_POWER_OFF)
  131. cpu_pm_exit();
  132. out_clkdm_set:
  133. /* Re-allow idle for C1 */
  134. if (cx->flags & OMAP_CPUIDLE_CX_NO_CLKDM_IDLE)
  135. clkdm_allow_idle(mpu_pd->pwrdm_clkdms[0]);
  136. return_sleep_time:
  137. return index;
  138. }
  139. /**
  140. * next_valid_state - Find next valid C-state
  141. * @dev: cpuidle device
  142. * @drv: cpuidle driver
  143. * @index: Index of currently selected c-state
  144. *
  145. * If the state corresponding to index is valid, index is returned back
  146. * to the caller. Else, this function searches for a lower c-state which is
  147. * still valid (as defined in omap3_power_states[]) and returns its index.
  148. *
  149. * A state is valid if the 'valid' field is enabled and
  150. * if it satisfies the enable_off_mode condition.
  151. */
  152. static int next_valid_state(struct cpuidle_device *dev,
  153. struct cpuidle_driver *drv, int index)
  154. {
  155. struct omap3_idle_statedata *cx = &omap3_idle_data[index];
  156. u32 mpu_deepest_state = PWRDM_POWER_RET;
  157. u32 core_deepest_state = PWRDM_POWER_RET;
  158. int idx;
  159. int next_index = 0; /* C1 is the default value */
  160. if (enable_off_mode) {
  161. mpu_deepest_state = PWRDM_POWER_OFF;
  162. /*
  163. * Erratum i583: valable for ES rev < Es1.2 on 3630.
  164. * CORE OFF mode is not supported in a stable form, restrict
  165. * instead the CORE state to RET.
  166. */
  167. if (!IS_PM34XX_ERRATUM(PM_SDRC_WAKEUP_ERRATUM_i583))
  168. core_deepest_state = PWRDM_POWER_OFF;
  169. }
  170. /* Check if current state is valid */
  171. if ((cx->mpu_state >= mpu_deepest_state) &&
  172. (cx->core_state >= core_deepest_state))
  173. return index;
  174. /*
  175. * Drop to next valid state.
  176. * Start search from the next (lower) state.
  177. */
  178. for (idx = index - 1; idx >= 0; idx--) {
  179. cx = &omap3_idle_data[idx];
  180. if ((cx->mpu_state >= mpu_deepest_state) &&
  181. (cx->core_state >= core_deepest_state)) {
  182. next_index = idx;
  183. break;
  184. }
  185. }
  186. return next_index;
  187. }
  188. /**
  189. * omap3_enter_idle_bm - Checks for any bus activity
  190. * @dev: cpuidle device
  191. * @drv: cpuidle driver
  192. * @index: array index of target state to be programmed
  193. *
  194. * This function checks for any pending activity and then programs
  195. * the device to the specified or a safer state.
  196. */
  197. static int omap3_enter_idle_bm(struct cpuidle_device *dev,
  198. struct cpuidle_driver *drv,
  199. int index)
  200. {
  201. int new_state_idx, ret;
  202. u8 per_next_state, per_saved_state;
  203. struct omap3_idle_statedata *cx;
  204. /*
  205. * Use only C1 if CAM is active.
  206. * CAM does not have wakeup capability in OMAP3.
  207. */
  208. if (pwrdm_read_pwrst(cam_pd) == PWRDM_POWER_ON)
  209. new_state_idx = drv->safe_state_index;
  210. else
  211. new_state_idx = next_valid_state(dev, drv, index);
  212. /*
  213. * FIXME: we currently manage device-specific idle states
  214. * for PER and CORE in combination with CPU-specific
  215. * idle states. This is wrong, and device-specific
  216. * idle management needs to be separated out into
  217. * its own code.
  218. */
  219. /* Program PER state */
  220. cx = &omap3_idle_data[new_state_idx];
  221. per_next_state = pwrdm_read_next_pwrst(per_pd);
  222. per_saved_state = per_next_state;
  223. if (per_next_state < cx->per_min_state) {
  224. per_next_state = cx->per_min_state;
  225. pwrdm_set_next_pwrst(per_pd, per_next_state);
  226. }
  227. ret = omap3_enter_idle(dev, drv, new_state_idx);
  228. /* Restore original PER state if it was modified */
  229. if (per_next_state != per_saved_state)
  230. pwrdm_set_next_pwrst(per_pd, per_saved_state);
  231. return ret;
  232. }
  233. static struct cpuidle_driver omap3_idle_driver = {
  234. .name = "omap3_idle",
  235. .owner = THIS_MODULE,
  236. .states = {
  237. {
  238. .enter = omap3_enter_idle_bm,
  239. .exit_latency = 2 + 2,
  240. .target_residency = 5,
  241. .name = "C1",
  242. .desc = "MPU ON + CORE ON",
  243. },
  244. {
  245. .enter = omap3_enter_idle_bm,
  246. .exit_latency = 10 + 10,
  247. .target_residency = 30,
  248. .name = "C2",
  249. .desc = "MPU ON + CORE ON",
  250. },
  251. {
  252. .enter = omap3_enter_idle_bm,
  253. .exit_latency = 50 + 50,
  254. .target_residency = 300,
  255. .name = "C3",
  256. .desc = "MPU RET + CORE ON",
  257. },
  258. {
  259. .enter = omap3_enter_idle_bm,
  260. .exit_latency = 1500 + 1800,
  261. .target_residency = 4000,
  262. .name = "C4",
  263. .desc = "MPU OFF + CORE ON",
  264. },
  265. {
  266. .enter = omap3_enter_idle_bm,
  267. .exit_latency = 2500 + 7500,
  268. .target_residency = 12000,
  269. .name = "C5",
  270. .desc = "MPU RET + CORE RET",
  271. },
  272. {
  273. .enter = omap3_enter_idle_bm,
  274. .exit_latency = 3000 + 8500,
  275. .target_residency = 15000,
  276. .name = "C6",
  277. .desc = "MPU OFF + CORE RET",
  278. },
  279. {
  280. .enter = omap3_enter_idle_bm,
  281. .exit_latency = 10000 + 30000,
  282. .target_residency = 30000,
  283. .name = "C7",
  284. .desc = "MPU OFF + CORE OFF",
  285. },
  286. },
  287. .state_count = ARRAY_SIZE(omap3_idle_data),
  288. .safe_state_index = 0,
  289. };
  290. /*
  291. * Numbers based on measurements made in October 2009 for PM optimized kernel
  292. * with CPU freq enabled on device Nokia N900. Assumes OPP2 (main idle OPP,
  293. * and worst case latencies).
  294. */
  295. static struct cpuidle_driver omap3430_idle_driver = {
  296. .name = "omap3430_idle",
  297. .owner = THIS_MODULE,
  298. .states = {
  299. {
  300. .enter = omap3_enter_idle_bm,
  301. .exit_latency = 110 + 162,
  302. .target_residency = 5,
  303. .name = "C1",
  304. .desc = "MPU ON + CORE ON",
  305. },
  306. {
  307. .enter = omap3_enter_idle_bm,
  308. .exit_latency = 106 + 180,
  309. .target_residency = 309,
  310. .name = "C2",
  311. .desc = "MPU ON + CORE ON",
  312. },
  313. {
  314. .enter = omap3_enter_idle_bm,
  315. .exit_latency = 107 + 410,
  316. .target_residency = 46057,
  317. .name = "C3",
  318. .desc = "MPU RET + CORE ON",
  319. },
  320. {
  321. .enter = omap3_enter_idle_bm,
  322. .exit_latency = 121 + 3374,
  323. .target_residency = 46057,
  324. .name = "C4",
  325. .desc = "MPU OFF + CORE ON",
  326. },
  327. {
  328. .enter = omap3_enter_idle_bm,
  329. .exit_latency = 855 + 1146,
  330. .target_residency = 46057,
  331. .name = "C5",
  332. .desc = "MPU RET + CORE RET",
  333. },
  334. {
  335. .enter = omap3_enter_idle_bm,
  336. .exit_latency = 7580 + 4134,
  337. .target_residency = 484329,
  338. .name = "C6",
  339. .desc = "MPU OFF + CORE RET",
  340. },
  341. {
  342. .enter = omap3_enter_idle_bm,
  343. .exit_latency = 7505 + 15274,
  344. .target_residency = 484329,
  345. .name = "C7",
  346. .desc = "MPU OFF + CORE OFF",
  347. },
  348. },
  349. .state_count = ARRAY_SIZE(omap3_idle_data),
  350. .safe_state_index = 0,
  351. };
  352. /* Public functions */
  353. /**
  354. * omap3_idle_init - Init routine for OMAP3 idle
  355. *
  356. * Registers the OMAP3 specific cpuidle driver to the cpuidle
  357. * framework with the valid set of states.
  358. */
  359. int __init omap3_idle_init(void)
  360. {
  361. mpu_pd = pwrdm_lookup("mpu_pwrdm");
  362. core_pd = pwrdm_lookup("core_pwrdm");
  363. per_pd = pwrdm_lookup("per_pwrdm");
  364. cam_pd = pwrdm_lookup("cam_pwrdm");
  365. if (!mpu_pd || !core_pd || !per_pd || !cam_pd)
  366. return -ENODEV;
  367. if (cpu_is_omap3430())
  368. return cpuidle_register(&omap3430_idle_driver, NULL);
  369. else
  370. return cpuidle_register(&omap3_idle_driver, NULL);
  371. }