cpuidle34xx.c 11 KB

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