cpuidle34xx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 <plat/powerdomain.h>
  29. #include <plat/clockdomain.h>
  30. #include <plat/control.h>
  31. #include <plat/serial.h>
  32. #include "pm.h"
  33. #ifdef CONFIG_CPU_IDLE
  34. #define OMAP3_MAX_STATES 7
  35. #define OMAP3_STATE_C1 0 /* C1 - MPU WFI + Core active */
  36. #define OMAP3_STATE_C2 1 /* C2 - MPU WFI + Core inactive */
  37. #define OMAP3_STATE_C3 2 /* C3 - MPU CSWR + Core inactive */
  38. #define OMAP3_STATE_C4 3 /* C4 - MPU OFF + Core iactive */
  39. #define OMAP3_STATE_C5 4 /* C5 - MPU RET + Core RET */
  40. #define OMAP3_STATE_C6 5 /* C6 - MPU OFF + Core RET */
  41. #define OMAP3_STATE_C7 6 /* C7 - MPU OFF + Core OFF */
  42. #define OMAP3_STATE_MAX OMAP3_STATE_C7
  43. struct omap3_processor_cx {
  44. u8 valid;
  45. u8 type;
  46. u32 sleep_latency;
  47. u32 wakeup_latency;
  48. u32 mpu_state;
  49. u32 core_state;
  50. u32 threshold;
  51. u32 flags;
  52. };
  53. struct omap3_processor_cx omap3_power_states[OMAP3_MAX_STATES];
  54. struct omap3_processor_cx current_cx_state;
  55. struct powerdomain *mpu_pd, *core_pd;
  56. /*
  57. * The latencies/thresholds for various C states have
  58. * to be configured from the respective board files.
  59. * These are some default values (which might not provide
  60. * the best power savings) used on boards which do not
  61. * pass these details from the board file.
  62. */
  63. static struct cpuidle_params cpuidle_params_table[] = {
  64. /* C1 */
  65. {1, 2, 2, 5},
  66. /* C2 */
  67. {1, 10, 10, 30},
  68. /* C3 */
  69. {1, 50, 50, 300},
  70. /* C4 */
  71. {1, 1500, 1800, 4000},
  72. /* C5 */
  73. {1, 2500, 7500, 12000},
  74. /* C6 */
  75. {1, 3000, 8500, 15000},
  76. /* C7 */
  77. {1, 10000, 30000, 300000},
  78. };
  79. static int omap3_idle_bm_check(void)
  80. {
  81. if (!omap3_can_sleep())
  82. return 1;
  83. return 0;
  84. }
  85. static int _cpuidle_allow_idle(struct powerdomain *pwrdm,
  86. struct clockdomain *clkdm)
  87. {
  88. omap2_clkdm_allow_idle(clkdm);
  89. return 0;
  90. }
  91. static int _cpuidle_deny_idle(struct powerdomain *pwrdm,
  92. struct clockdomain *clkdm)
  93. {
  94. omap2_clkdm_deny_idle(clkdm);
  95. return 0;
  96. }
  97. /**
  98. * omap3_enter_idle - Programs OMAP3 to enter the specified state
  99. * @dev: cpuidle device
  100. * @state: The target state to be programmed
  101. *
  102. * Called from the CPUidle framework to program the device to the
  103. * specified target state selected by the governor.
  104. */
  105. static int omap3_enter_idle(struct cpuidle_device *dev,
  106. struct cpuidle_state *state)
  107. {
  108. struct omap3_processor_cx *cx = cpuidle_get_statedata(state);
  109. struct timespec ts_preidle, ts_postidle, ts_idle;
  110. u32 mpu_state = cx->mpu_state, core_state = cx->core_state;
  111. current_cx_state = *cx;
  112. /* Used to keep track of the total time in idle */
  113. getnstimeofday(&ts_preidle);
  114. local_irq_disable();
  115. local_fiq_disable();
  116. pwrdm_set_next_pwrst(mpu_pd, mpu_state);
  117. pwrdm_set_next_pwrst(core_pd, core_state);
  118. if (omap_irq_pending() || need_resched())
  119. goto return_sleep_time;
  120. if (cx->type == OMAP3_STATE_C1) {
  121. pwrdm_for_each_clkdm(mpu_pd, _cpuidle_deny_idle);
  122. pwrdm_for_each_clkdm(core_pd, _cpuidle_deny_idle);
  123. }
  124. /* Execute ARM wfi */
  125. omap_sram_idle();
  126. if (cx->type == OMAP3_STATE_C1) {
  127. pwrdm_for_each_clkdm(mpu_pd, _cpuidle_allow_idle);
  128. pwrdm_for_each_clkdm(core_pd, _cpuidle_allow_idle);
  129. }
  130. return_sleep_time:
  131. getnstimeofday(&ts_postidle);
  132. ts_idle = timespec_sub(ts_postidle, ts_preidle);
  133. local_irq_enable();
  134. local_fiq_enable();
  135. return ts_idle.tv_nsec / NSEC_PER_USEC + ts_idle.tv_sec * USEC_PER_SEC;
  136. }
  137. /**
  138. * next_valid_state - Find next valid c-state
  139. * @dev: cpuidle device
  140. * @state: Currently selected c-state
  141. *
  142. * If the current state is valid, it is returned back to the caller.
  143. * Else, this function searches for a lower c-state which is still
  144. * valid (as defined in omap3_power_states[]).
  145. */
  146. static struct cpuidle_state *next_valid_state(struct cpuidle_device *dev,
  147. struct cpuidle_state *curr)
  148. {
  149. struct cpuidle_state *next = NULL;
  150. struct omap3_processor_cx *cx;
  151. cx = (struct omap3_processor_cx *)cpuidle_get_statedata(curr);
  152. /* Check if current state is valid */
  153. if (cx->valid) {
  154. return curr;
  155. } else {
  156. u8 idx = OMAP3_STATE_MAX;
  157. /*
  158. * Reach the current state starting at highest C-state
  159. */
  160. for (; idx >= OMAP3_STATE_C1; idx--) {
  161. if (&dev->states[idx] == curr) {
  162. next = &dev->states[idx];
  163. break;
  164. }
  165. }
  166. /*
  167. * Should never hit this condition.
  168. */
  169. WARN_ON(next == NULL);
  170. /*
  171. * Drop to next valid state.
  172. * Start search from the next (lower) state.
  173. */
  174. idx--;
  175. for (; idx >= OMAP3_STATE_C1; idx--) {
  176. struct omap3_processor_cx *cx;
  177. cx = cpuidle_get_statedata(&dev->states[idx]);
  178. if (cx->valid) {
  179. next = &dev->states[idx];
  180. break;
  181. }
  182. }
  183. /*
  184. * C1 and C2 are always valid.
  185. * So, no need to check for 'next==NULL' outside this loop.
  186. */
  187. }
  188. return next;
  189. }
  190. /**
  191. * omap3_enter_idle_bm - Checks for any bus activity
  192. * @dev: cpuidle device
  193. * @state: The target state to be programmed
  194. *
  195. * Used for C states with CPUIDLE_FLAG_CHECK_BM flag set. This
  196. * function checks for any pending activity and then programs the
  197. * device to the specified or a safer state.
  198. */
  199. static int omap3_enter_idle_bm(struct cpuidle_device *dev,
  200. struct cpuidle_state *state)
  201. {
  202. struct cpuidle_state *new_state = next_valid_state(dev, state);
  203. if ((state->flags & CPUIDLE_FLAG_CHECK_BM) && omap3_idle_bm_check()) {
  204. BUG_ON(!dev->safe_state);
  205. new_state = dev->safe_state;
  206. }
  207. dev->last_state = new_state;
  208. return omap3_enter_idle(dev, new_state);
  209. }
  210. DEFINE_PER_CPU(struct cpuidle_device, omap3_idle_dev);
  211. /**
  212. * omap3_cpuidle_update_states - Update the cpuidle states.
  213. *
  214. * Currently, this function toggles the validity of idle states based upon
  215. * the flag 'enable_off_mode'. When the flag is set all states are valid.
  216. * Else, states leading to OFF state set to be invalid.
  217. */
  218. void omap3_cpuidle_update_states(void)
  219. {
  220. int i;
  221. for (i = OMAP3_STATE_C1; i < OMAP3_MAX_STATES; i++) {
  222. struct omap3_processor_cx *cx = &omap3_power_states[i];
  223. if (enable_off_mode) {
  224. cx->valid = 1;
  225. } else {
  226. if ((cx->mpu_state == PWRDM_POWER_OFF) ||
  227. (cx->core_state == PWRDM_POWER_OFF))
  228. cx->valid = 0;
  229. }
  230. }
  231. }
  232. void omap3_pm_init_cpuidle(struct cpuidle_params *cpuidle_board_params)
  233. {
  234. int i;
  235. if (!cpuidle_board_params)
  236. return;
  237. for (i = OMAP3_STATE_C1; i < OMAP3_MAX_STATES; i++) {
  238. cpuidle_params_table[i].valid =
  239. cpuidle_board_params[i].valid;
  240. cpuidle_params_table[i].sleep_latency =
  241. cpuidle_board_params[i].sleep_latency;
  242. cpuidle_params_table[i].wake_latency =
  243. cpuidle_board_params[i].wake_latency;
  244. cpuidle_params_table[i].threshold =
  245. cpuidle_board_params[i].threshold;
  246. }
  247. return;
  248. }
  249. /* omap3_init_power_states - Initialises the OMAP3 specific C states.
  250. *
  251. * Below is the desciption of each C state.
  252. * C1 . MPU WFI + Core active
  253. * C2 . MPU WFI + Core inactive
  254. * C3 . MPU CSWR + Core inactive
  255. * C4 . MPU OFF + Core inactive
  256. * C5 . MPU CSWR + Core CSWR
  257. * C6 . MPU OFF + Core CSWR
  258. * C7 . MPU OFF + Core OFF
  259. */
  260. void omap_init_power_states(void)
  261. {
  262. /* C1 . MPU WFI + Core active */
  263. omap3_power_states[OMAP3_STATE_C1].valid =
  264. cpuidle_params_table[OMAP3_STATE_C1].valid;
  265. omap3_power_states[OMAP3_STATE_C1].type = OMAP3_STATE_C1;
  266. omap3_power_states[OMAP3_STATE_C1].sleep_latency =
  267. cpuidle_params_table[OMAP3_STATE_C1].sleep_latency;
  268. omap3_power_states[OMAP3_STATE_C1].wakeup_latency =
  269. cpuidle_params_table[OMAP3_STATE_C1].wake_latency;
  270. omap3_power_states[OMAP3_STATE_C1].threshold =
  271. cpuidle_params_table[OMAP3_STATE_C1].threshold;
  272. omap3_power_states[OMAP3_STATE_C1].mpu_state = PWRDM_POWER_ON;
  273. omap3_power_states[OMAP3_STATE_C1].core_state = PWRDM_POWER_ON;
  274. omap3_power_states[OMAP3_STATE_C1].flags = CPUIDLE_FLAG_TIME_VALID;
  275. /* C2 . MPU WFI + Core inactive */
  276. omap3_power_states[OMAP3_STATE_C2].valid =
  277. cpuidle_params_table[OMAP3_STATE_C2].valid;
  278. omap3_power_states[OMAP3_STATE_C2].type = OMAP3_STATE_C2;
  279. omap3_power_states[OMAP3_STATE_C2].sleep_latency =
  280. cpuidle_params_table[OMAP3_STATE_C2].sleep_latency;
  281. omap3_power_states[OMAP3_STATE_C2].wakeup_latency =
  282. cpuidle_params_table[OMAP3_STATE_C2].wake_latency;
  283. omap3_power_states[OMAP3_STATE_C2].threshold =
  284. cpuidle_params_table[OMAP3_STATE_C2].threshold;
  285. omap3_power_states[OMAP3_STATE_C2].mpu_state = PWRDM_POWER_ON;
  286. omap3_power_states[OMAP3_STATE_C2].core_state = PWRDM_POWER_ON;
  287. omap3_power_states[OMAP3_STATE_C2].flags = CPUIDLE_FLAG_TIME_VALID;
  288. /* C3 . MPU CSWR + Core inactive */
  289. omap3_power_states[OMAP3_STATE_C3].valid =
  290. cpuidle_params_table[OMAP3_STATE_C3].valid;
  291. omap3_power_states[OMAP3_STATE_C3].type = OMAP3_STATE_C3;
  292. omap3_power_states[OMAP3_STATE_C3].sleep_latency =
  293. cpuidle_params_table[OMAP3_STATE_C3].sleep_latency;
  294. omap3_power_states[OMAP3_STATE_C3].wakeup_latency =
  295. cpuidle_params_table[OMAP3_STATE_C3].wake_latency;
  296. omap3_power_states[OMAP3_STATE_C3].threshold =
  297. cpuidle_params_table[OMAP3_STATE_C3].threshold;
  298. omap3_power_states[OMAP3_STATE_C3].mpu_state = PWRDM_POWER_RET;
  299. omap3_power_states[OMAP3_STATE_C3].core_state = PWRDM_POWER_ON;
  300. omap3_power_states[OMAP3_STATE_C3].flags = CPUIDLE_FLAG_TIME_VALID |
  301. CPUIDLE_FLAG_CHECK_BM;
  302. /* C4 . MPU OFF + Core inactive */
  303. omap3_power_states[OMAP3_STATE_C4].valid =
  304. cpuidle_params_table[OMAP3_STATE_C4].valid;
  305. omap3_power_states[OMAP3_STATE_C4].type = OMAP3_STATE_C4;
  306. omap3_power_states[OMAP3_STATE_C4].sleep_latency =
  307. cpuidle_params_table[OMAP3_STATE_C4].sleep_latency;
  308. omap3_power_states[OMAP3_STATE_C4].wakeup_latency =
  309. cpuidle_params_table[OMAP3_STATE_C4].wake_latency;
  310. omap3_power_states[OMAP3_STATE_C4].threshold =
  311. cpuidle_params_table[OMAP3_STATE_C4].threshold;
  312. omap3_power_states[OMAP3_STATE_C4].mpu_state = PWRDM_POWER_OFF;
  313. omap3_power_states[OMAP3_STATE_C4].core_state = PWRDM_POWER_ON;
  314. omap3_power_states[OMAP3_STATE_C4].flags = CPUIDLE_FLAG_TIME_VALID |
  315. CPUIDLE_FLAG_CHECK_BM;
  316. /* C5 . MPU CSWR + Core CSWR*/
  317. omap3_power_states[OMAP3_STATE_C5].valid =
  318. cpuidle_params_table[OMAP3_STATE_C5].valid;
  319. omap3_power_states[OMAP3_STATE_C5].type = OMAP3_STATE_C5;
  320. omap3_power_states[OMAP3_STATE_C5].sleep_latency =
  321. cpuidle_params_table[OMAP3_STATE_C5].sleep_latency;
  322. omap3_power_states[OMAP3_STATE_C5].wakeup_latency =
  323. cpuidle_params_table[OMAP3_STATE_C5].wake_latency;
  324. omap3_power_states[OMAP3_STATE_C5].threshold =
  325. cpuidle_params_table[OMAP3_STATE_C5].threshold;
  326. omap3_power_states[OMAP3_STATE_C5].mpu_state = PWRDM_POWER_RET;
  327. omap3_power_states[OMAP3_STATE_C5].core_state = PWRDM_POWER_RET;
  328. omap3_power_states[OMAP3_STATE_C5].flags = CPUIDLE_FLAG_TIME_VALID |
  329. CPUIDLE_FLAG_CHECK_BM;
  330. /* C6 . MPU OFF + Core CSWR */
  331. omap3_power_states[OMAP3_STATE_C6].valid =
  332. cpuidle_params_table[OMAP3_STATE_C6].valid;
  333. omap3_power_states[OMAP3_STATE_C6].type = OMAP3_STATE_C6;
  334. omap3_power_states[OMAP3_STATE_C6].sleep_latency =
  335. cpuidle_params_table[OMAP3_STATE_C6].sleep_latency;
  336. omap3_power_states[OMAP3_STATE_C6].wakeup_latency =
  337. cpuidle_params_table[OMAP3_STATE_C6].wake_latency;
  338. omap3_power_states[OMAP3_STATE_C6].threshold =
  339. cpuidle_params_table[OMAP3_STATE_C6].threshold;
  340. omap3_power_states[OMAP3_STATE_C6].mpu_state = PWRDM_POWER_OFF;
  341. omap3_power_states[OMAP3_STATE_C6].core_state = PWRDM_POWER_RET;
  342. omap3_power_states[OMAP3_STATE_C6].flags = CPUIDLE_FLAG_TIME_VALID |
  343. CPUIDLE_FLAG_CHECK_BM;
  344. /* C7 . MPU OFF + Core OFF */
  345. omap3_power_states[OMAP3_STATE_C7].valid =
  346. cpuidle_params_table[OMAP3_STATE_C7].valid;
  347. omap3_power_states[OMAP3_STATE_C7].type = OMAP3_STATE_C7;
  348. omap3_power_states[OMAP3_STATE_C7].sleep_latency =
  349. cpuidle_params_table[OMAP3_STATE_C7].sleep_latency;
  350. omap3_power_states[OMAP3_STATE_C7].wakeup_latency =
  351. cpuidle_params_table[OMAP3_STATE_C7].wake_latency;
  352. omap3_power_states[OMAP3_STATE_C7].threshold =
  353. cpuidle_params_table[OMAP3_STATE_C7].threshold;
  354. omap3_power_states[OMAP3_STATE_C7].mpu_state = PWRDM_POWER_OFF;
  355. omap3_power_states[OMAP3_STATE_C7].core_state = PWRDM_POWER_OFF;
  356. omap3_power_states[OMAP3_STATE_C7].flags = CPUIDLE_FLAG_TIME_VALID |
  357. CPUIDLE_FLAG_CHECK_BM;
  358. }
  359. struct cpuidle_driver omap3_idle_driver = {
  360. .name = "omap3_idle",
  361. .owner = THIS_MODULE,
  362. };
  363. /**
  364. * omap3_idle_init - Init routine for OMAP3 idle
  365. *
  366. * Registers the OMAP3 specific cpuidle driver with the cpuidle
  367. * framework with the valid set of states.
  368. */
  369. int __init omap3_idle_init(void)
  370. {
  371. int i, count = 0;
  372. struct omap3_processor_cx *cx;
  373. struct cpuidle_state *state;
  374. struct cpuidle_device *dev;
  375. mpu_pd = pwrdm_lookup("mpu_pwrdm");
  376. core_pd = pwrdm_lookup("core_pwrdm");
  377. omap_init_power_states();
  378. cpuidle_register_driver(&omap3_idle_driver);
  379. dev = &per_cpu(omap3_idle_dev, smp_processor_id());
  380. for (i = OMAP3_STATE_C1; i < OMAP3_MAX_STATES; i++) {
  381. cx = &omap3_power_states[i];
  382. state = &dev->states[count];
  383. if (!cx->valid)
  384. continue;
  385. cpuidle_set_statedata(state, cx);
  386. state->exit_latency = cx->sleep_latency + cx->wakeup_latency;
  387. state->target_residency = cx->threshold;
  388. state->flags = cx->flags;
  389. state->enter = (state->flags & CPUIDLE_FLAG_CHECK_BM) ?
  390. omap3_enter_idle_bm : omap3_enter_idle;
  391. if (cx->type == OMAP3_STATE_C1)
  392. dev->safe_state = state;
  393. sprintf(state->name, "C%d", count+1);
  394. count++;
  395. }
  396. if (!count)
  397. return -EINVAL;
  398. dev->state_count = count;
  399. omap3_cpuidle_update_states();
  400. if (cpuidle_register_device(dev)) {
  401. printk(KERN_ERR "%s: CPUidle register device failed\n",
  402. __func__);
  403. return -EIO;
  404. }
  405. return 0;
  406. }
  407. #else
  408. int __init omap3_idle_init(void)
  409. {
  410. return 0;
  411. }
  412. #endif /* CONFIG_CPU_IDLE */