cpuidle34xx.c 15 KB

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