cpuidle34xx.c 15 KB

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