cpuidle34xx.c 12 KB

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