cpuidle34xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * linux/arch/arm/mach-omap2/cpuidle34xx.c
  3. *
  4. * OMAP3 CPU IDLE Routines
  5. *
  6. * Copyright (C) 2008 Texas Instruments, Inc.
  7. * Rajendra Nayak <rnayak@ti.com>
  8. *
  9. * Copyright (C) 2007 Texas Instruments, Inc.
  10. * Karthik Dasu <karthik-dp@ti.com>
  11. *
  12. * Copyright (C) 2006 Nokia Corporation
  13. * Tony Lindgren <tony@atomide.com>
  14. *
  15. * Copyright (C) 2005 Texas Instruments, Inc.
  16. * Richard Woodruff <r-woodruff2@ti.com>
  17. *
  18. * Based on pm.c for omap2
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License version 2 as
  22. * published by the Free Software Foundation.
  23. */
  24. #include <linux/sched.h>
  25. #include <linux/cpuidle.h>
  26. #include <linux/export.h>
  27. #include <linux/cpu_pm.h>
  28. #include <plat/prcm.h>
  29. #include <plat/irqs.h>
  30. #include "powerdomain.h"
  31. #include "clockdomain.h"
  32. #include <plat/serial.h>
  33. #include "pm.h"
  34. #include "control.h"
  35. #include "common.h"
  36. #ifdef CONFIG_CPU_IDLE
  37. /*
  38. * The latencies/thresholds for various C states have
  39. * to be configured from the respective board files.
  40. * These are some default values (which might not provide
  41. * the best power savings) used on boards which do not
  42. * pass these details from the board file.
  43. */
  44. static struct cpuidle_params cpuidle_params_table[] = {
  45. /* C1 */
  46. {2 + 2, 5, 1},
  47. /* C2 */
  48. {10 + 10, 30, 1},
  49. /* C3 */
  50. {50 + 50, 300, 1},
  51. /* C4 */
  52. {1500 + 1800, 4000, 1},
  53. /* C5 */
  54. {2500 + 7500, 12000, 1},
  55. /* C6 */
  56. {3000 + 8500, 15000, 1},
  57. /* C7 */
  58. {10000 + 30000, 300000, 1},
  59. };
  60. #define OMAP3_NUM_STATES ARRAY_SIZE(cpuidle_params_table)
  61. /* Mach specific information to be recorded in the C-state driver_data */
  62. struct omap3_idle_statedata {
  63. u32 mpu_state;
  64. u32 core_state;
  65. u8 valid;
  66. };
  67. struct omap3_idle_statedata omap3_idle_data[OMAP3_NUM_STATES];
  68. struct powerdomain *mpu_pd, *core_pd, *per_pd, *cam_pd;
  69. static int _cpuidle_allow_idle(struct powerdomain *pwrdm,
  70. struct clockdomain *clkdm)
  71. {
  72. clkdm_allow_idle(clkdm);
  73. return 0;
  74. }
  75. static int _cpuidle_deny_idle(struct powerdomain *pwrdm,
  76. struct clockdomain *clkdm)
  77. {
  78. clkdm_deny_idle(clkdm);
  79. return 0;
  80. }
  81. /**
  82. * omap3_enter_idle - Programs OMAP3 to enter the specified state
  83. * @dev: cpuidle device
  84. * @drv: cpuidle driver
  85. * @index: the index of state to be entered
  86. *
  87. * Called from the CPUidle framework to program the device to the
  88. * specified target state selected by the governor.
  89. */
  90. static int omap3_enter_idle(struct cpuidle_device *dev,
  91. struct cpuidle_driver *drv,
  92. int index)
  93. {
  94. struct omap3_idle_statedata *cx =
  95. cpuidle_get_statedata(&dev->states_usage[index]);
  96. struct timespec ts_preidle, ts_postidle, ts_idle;
  97. u32 mpu_state = cx->mpu_state, core_state = cx->core_state;
  98. int idle_time;
  99. /* Used to keep track of the total time in idle */
  100. getnstimeofday(&ts_preidle);
  101. local_irq_disable();
  102. local_fiq_disable();
  103. pwrdm_set_next_pwrst(mpu_pd, mpu_state);
  104. pwrdm_set_next_pwrst(core_pd, core_state);
  105. if (omap_irq_pending() || need_resched())
  106. goto return_sleep_time;
  107. /* Deny idle for C1 */
  108. if (index == 0) {
  109. pwrdm_for_each_clkdm(mpu_pd, _cpuidle_deny_idle);
  110. pwrdm_for_each_clkdm(core_pd, _cpuidle_deny_idle);
  111. }
  112. /*
  113. * Call idle CPU PM enter notifier chain so that
  114. * VFP context is saved.
  115. */
  116. if (mpu_state == PWRDM_POWER_OFF)
  117. cpu_pm_enter();
  118. /* Execute ARM wfi */
  119. omap_sram_idle();
  120. /*
  121. * Call idle CPU PM enter notifier chain to restore
  122. * VFP context.
  123. */
  124. if (pwrdm_read_prev_pwrst(mpu_pd) == PWRDM_POWER_OFF)
  125. cpu_pm_exit();
  126. /* Re-allow idle for C1 */
  127. if (index == 0) {
  128. pwrdm_for_each_clkdm(mpu_pd, _cpuidle_allow_idle);
  129. pwrdm_for_each_clkdm(core_pd, _cpuidle_allow_idle);
  130. }
  131. return_sleep_time:
  132. getnstimeofday(&ts_postidle);
  133. ts_idle = timespec_sub(ts_postidle, ts_preidle);
  134. local_irq_enable();
  135. local_fiq_enable();
  136. idle_time = ts_idle.tv_nsec / NSEC_PER_USEC + ts_idle.tv_sec * \
  137. USEC_PER_SEC;
  138. /* Update cpuidle counters */
  139. dev->last_residency = idle_time;
  140. return index;
  141. }
  142. /**
  143. * next_valid_state - Find next valid C-state
  144. * @dev: cpuidle device
  145. * @drv: cpuidle driver
  146. * @index: Index of currently selected c-state
  147. *
  148. * If the state corresponding to index is valid, index is returned back
  149. * to the caller. Else, this function searches for a lower c-state which is
  150. * still valid (as defined in omap3_power_states[]) and returns its index.
  151. *
  152. * A state is valid if the 'valid' field is enabled and
  153. * if it satisfies the enable_off_mode condition.
  154. */
  155. static int next_valid_state(struct cpuidle_device *dev,
  156. struct cpuidle_driver *drv,
  157. int index)
  158. {
  159. struct cpuidle_state_usage *curr_usage = &dev->states_usage[index];
  160. struct cpuidle_state *curr = &drv->states[index];
  161. struct omap3_idle_statedata *cx = cpuidle_get_statedata(curr_usage);
  162. u32 mpu_deepest_state = PWRDM_POWER_RET;
  163. u32 core_deepest_state = PWRDM_POWER_RET;
  164. int next_index = -1;
  165. if (enable_off_mode) {
  166. mpu_deepest_state = PWRDM_POWER_OFF;
  167. /*
  168. * Erratum i583: valable for ES rev < Es1.2 on 3630.
  169. * CORE OFF mode is not supported in a stable form, restrict
  170. * instead the CORE state to RET.
  171. */
  172. if (!IS_PM34XX_ERRATUM(PM_SDRC_WAKEUP_ERRATUM_i583))
  173. core_deepest_state = PWRDM_POWER_OFF;
  174. }
  175. /* Check if current state is valid */
  176. if ((cx->valid) &&
  177. (cx->mpu_state >= mpu_deepest_state) &&
  178. (cx->core_state >= core_deepest_state)) {
  179. return index;
  180. } else {
  181. int idx = OMAP3_NUM_STATES - 1;
  182. /* Reach the current state starting at highest C-state */
  183. for (; idx >= 0; idx--) {
  184. if (&drv->states[idx] == curr) {
  185. next_index = idx;
  186. break;
  187. }
  188. }
  189. /* Should never hit this condition */
  190. WARN_ON(next_index == -1);
  191. /*
  192. * Drop to next valid state.
  193. * Start search from the next (lower) state.
  194. */
  195. idx--;
  196. for (; idx >= 0; idx--) {
  197. cx = cpuidle_get_statedata(&dev->states_usage[idx]);
  198. if ((cx->valid) &&
  199. (cx->mpu_state >= mpu_deepest_state) &&
  200. (cx->core_state >= core_deepest_state)) {
  201. next_index = idx;
  202. break;
  203. }
  204. }
  205. /*
  206. * C1 is always valid.
  207. * So, no need to check for 'next_index == -1' outside
  208. * this loop.
  209. */
  210. }
  211. return next_index;
  212. }
  213. /**
  214. * omap3_enter_idle_bm - Checks for any bus activity
  215. * @dev: cpuidle device
  216. * @drv: cpuidle driver
  217. * @index: array index of target state to be programmed
  218. *
  219. * This function checks for any pending activity and then programs
  220. * the device to the specified or a safer state.
  221. */
  222. static int omap3_enter_idle_bm(struct cpuidle_device *dev,
  223. struct cpuidle_driver *drv,
  224. int index)
  225. {
  226. int new_state_idx;
  227. u32 core_next_state, per_next_state = 0, per_saved_state = 0, cam_state;
  228. struct omap3_idle_statedata *cx;
  229. int ret;
  230. if (!omap3_can_sleep()) {
  231. new_state_idx = drv->safe_state_index;
  232. goto select_state;
  233. }
  234. /*
  235. * Prevent idle completely if CAM is active.
  236. * CAM does not have wakeup capability in OMAP3.
  237. */
  238. cam_state = pwrdm_read_pwrst(cam_pd);
  239. if (cam_state == PWRDM_POWER_ON) {
  240. new_state_idx = drv->safe_state_index;
  241. goto select_state;
  242. }
  243. /*
  244. * FIXME: we currently manage device-specific idle states
  245. * for PER and CORE in combination with CPU-specific
  246. * idle states. This is wrong, and device-specific
  247. * idle management needs to be separated out into
  248. * its own code.
  249. */
  250. /*
  251. * Prevent PER off if CORE is not in retention or off as this
  252. * would disable PER wakeups completely.
  253. */
  254. cx = cpuidle_get_statedata(&dev->states_usage[index]);
  255. core_next_state = cx->core_state;
  256. per_next_state = per_saved_state = pwrdm_read_next_pwrst(per_pd);
  257. if ((per_next_state == PWRDM_POWER_OFF) &&
  258. (core_next_state > PWRDM_POWER_RET))
  259. per_next_state = PWRDM_POWER_RET;
  260. /* Are we changing PER target state? */
  261. if (per_next_state != per_saved_state)
  262. pwrdm_set_next_pwrst(per_pd, per_next_state);
  263. new_state_idx = next_valid_state(dev, drv, index);
  264. select_state:
  265. ret = omap3_enter_idle(dev, drv, new_state_idx);
  266. /* Restore original PER state if it was modified */
  267. if (per_next_state != per_saved_state)
  268. pwrdm_set_next_pwrst(per_pd, per_saved_state);
  269. return ret;
  270. }
  271. DEFINE_PER_CPU(struct cpuidle_device, omap3_idle_dev);
  272. void omap3_pm_init_cpuidle(struct cpuidle_params *cpuidle_board_params)
  273. {
  274. int i;
  275. if (!cpuidle_board_params)
  276. return;
  277. for (i = 0; i < OMAP3_NUM_STATES; i++) {
  278. cpuidle_params_table[i].valid = cpuidle_board_params[i].valid;
  279. cpuidle_params_table[i].exit_latency =
  280. cpuidle_board_params[i].exit_latency;
  281. cpuidle_params_table[i].target_residency =
  282. cpuidle_board_params[i].target_residency;
  283. }
  284. return;
  285. }
  286. struct cpuidle_driver omap3_idle_driver = {
  287. .name = "omap3_idle",
  288. .owner = THIS_MODULE,
  289. };
  290. /* Helper to fill the C-state common data*/
  291. static inline void _fill_cstate(struct cpuidle_driver *drv,
  292. int idx, const char *descr)
  293. {
  294. struct cpuidle_state *state = &drv->states[idx];
  295. state->exit_latency = cpuidle_params_table[idx].exit_latency;
  296. state->target_residency = cpuidle_params_table[idx].target_residency;
  297. state->flags = CPUIDLE_FLAG_TIME_VALID;
  298. state->enter = omap3_enter_idle_bm;
  299. sprintf(state->name, "C%d", idx + 1);
  300. strncpy(state->desc, descr, CPUIDLE_DESC_LEN);
  301. }
  302. /* Helper to register the driver_data */
  303. static inline struct omap3_idle_statedata *_fill_cstate_usage(
  304. struct cpuidle_device *dev,
  305. int idx)
  306. {
  307. struct omap3_idle_statedata *cx = &omap3_idle_data[idx];
  308. struct cpuidle_state_usage *state_usage = &dev->states_usage[idx];
  309. cx->valid = cpuidle_params_table[idx].valid;
  310. cpuidle_set_statedata(state_usage, cx);
  311. return cx;
  312. }
  313. /**
  314. * omap3_idle_init - Init routine for OMAP3 idle
  315. *
  316. * Registers the OMAP3 specific cpuidle driver to the cpuidle
  317. * framework with the valid set of states.
  318. */
  319. int __init omap3_idle_init(void)
  320. {
  321. struct cpuidle_device *dev;
  322. struct cpuidle_driver *drv = &omap3_idle_driver;
  323. struct omap3_idle_statedata *cx;
  324. mpu_pd = pwrdm_lookup("mpu_pwrdm");
  325. core_pd = pwrdm_lookup("core_pwrdm");
  326. per_pd = pwrdm_lookup("per_pwrdm");
  327. cam_pd = pwrdm_lookup("cam_pwrdm");
  328. drv->safe_state_index = -1;
  329. dev = &per_cpu(omap3_idle_dev, smp_processor_id());
  330. /* C1 . MPU WFI + Core active */
  331. _fill_cstate(drv, 0, "MPU ON + CORE ON");
  332. (&drv->states[0])->enter = omap3_enter_idle;
  333. drv->safe_state_index = 0;
  334. cx = _fill_cstate_usage(dev, 0);
  335. cx->valid = 1; /* C1 is always valid */
  336. cx->mpu_state = PWRDM_POWER_ON;
  337. cx->core_state = PWRDM_POWER_ON;
  338. /* C2 . MPU WFI + Core inactive */
  339. _fill_cstate(drv, 1, "MPU ON + CORE ON");
  340. cx = _fill_cstate_usage(dev, 1);
  341. cx->mpu_state = PWRDM_POWER_ON;
  342. cx->core_state = PWRDM_POWER_ON;
  343. /* C3 . MPU CSWR + Core inactive */
  344. _fill_cstate(drv, 2, "MPU RET + CORE ON");
  345. cx = _fill_cstate_usage(dev, 2);
  346. cx->mpu_state = PWRDM_POWER_RET;
  347. cx->core_state = PWRDM_POWER_ON;
  348. /* C4 . MPU OFF + Core inactive */
  349. _fill_cstate(drv, 3, "MPU OFF + CORE ON");
  350. cx = _fill_cstate_usage(dev, 3);
  351. cx->mpu_state = PWRDM_POWER_OFF;
  352. cx->core_state = PWRDM_POWER_ON;
  353. /* C5 . MPU RET + Core RET */
  354. _fill_cstate(drv, 4, "MPU RET + CORE RET");
  355. cx = _fill_cstate_usage(dev, 4);
  356. cx->mpu_state = PWRDM_POWER_RET;
  357. cx->core_state = PWRDM_POWER_RET;
  358. /* C6 . MPU OFF + Core RET */
  359. _fill_cstate(drv, 5, "MPU OFF + CORE RET");
  360. cx = _fill_cstate_usage(dev, 5);
  361. cx->mpu_state = PWRDM_POWER_OFF;
  362. cx->core_state = PWRDM_POWER_RET;
  363. /* C7 . MPU OFF + Core OFF */
  364. _fill_cstate(drv, 6, "MPU OFF + CORE OFF");
  365. cx = _fill_cstate_usage(dev, 6);
  366. /*
  367. * Erratum i583: implementation for ES rev < Es1.2 on 3630. We cannot
  368. * enable OFF mode in a stable form for previous revisions.
  369. * We disable C7 state as a result.
  370. */
  371. if (IS_PM34XX_ERRATUM(PM_SDRC_WAKEUP_ERRATUM_i583)) {
  372. cx->valid = 0;
  373. pr_warn("%s: core off state C7 disabled due to i583\n",
  374. __func__);
  375. }
  376. cx->mpu_state = PWRDM_POWER_OFF;
  377. cx->core_state = PWRDM_POWER_OFF;
  378. drv->state_count = OMAP3_NUM_STATES;
  379. cpuidle_register_driver(&omap3_idle_driver);
  380. dev->state_count = OMAP3_NUM_STATES;
  381. if (cpuidle_register_device(dev)) {
  382. printk(KERN_ERR "%s: CPUidle register device failed\n",
  383. __func__);
  384. return -EIO;
  385. }
  386. return 0;
  387. }
  388. #else
  389. int __init omap3_idle_init(void)
  390. {
  391. return 0;
  392. }
  393. #endif /* CONFIG_CPU_IDLE */