cpuidle34xx.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 <linux/cpu_pm.h>
  28. #include "powerdomain.h"
  29. #include "clockdomain.h"
  30. #include "pm.h"
  31. #include "control.h"
  32. #include "common.h"
  33. /* Mach specific information to be recorded in the C-state driver_data */
  34. struct omap3_idle_statedata {
  35. u8 mpu_state;
  36. u8 core_state;
  37. u8 per_min_state;
  38. u8 flags;
  39. };
  40. static struct powerdomain *mpu_pd, *core_pd, *per_pd, *cam_pd;
  41. /*
  42. * Possible flag bits for struct omap3_idle_statedata.flags:
  43. *
  44. * OMAP_CPUIDLE_CX_NO_CLKDM_IDLE: don't allow the MPU clockdomain to go
  45. * inactive. This in turn prevents the MPU DPLL from entering autoidle
  46. * mode, so wakeup latency is greatly reduced, at the cost of additional
  47. * energy consumption. This also prevents the CORE clockdomain from
  48. * entering idle.
  49. */
  50. #define OMAP_CPUIDLE_CX_NO_CLKDM_IDLE BIT(0)
  51. /*
  52. * Prevent PER OFF if CORE is not in RETention or OFF as this would
  53. * disable PER wakeups completely.
  54. */
  55. static struct omap3_idle_statedata omap3_idle_data[] = {
  56. {
  57. .mpu_state = PWRDM_POWER_ON,
  58. .core_state = PWRDM_POWER_ON,
  59. /* In C1 do not allow PER state lower than CORE state */
  60. .per_min_state = PWRDM_POWER_ON,
  61. .flags = OMAP_CPUIDLE_CX_NO_CLKDM_IDLE,
  62. },
  63. {
  64. .mpu_state = PWRDM_POWER_ON,
  65. .core_state = PWRDM_POWER_ON,
  66. .per_min_state = PWRDM_POWER_RET,
  67. },
  68. {
  69. .mpu_state = PWRDM_POWER_RET,
  70. .core_state = PWRDM_POWER_ON,
  71. .per_min_state = PWRDM_POWER_RET,
  72. },
  73. {
  74. .mpu_state = PWRDM_POWER_OFF,
  75. .core_state = PWRDM_POWER_ON,
  76. .per_min_state = PWRDM_POWER_RET,
  77. },
  78. {
  79. .mpu_state = PWRDM_POWER_RET,
  80. .core_state = PWRDM_POWER_RET,
  81. .per_min_state = PWRDM_POWER_OFF,
  82. },
  83. {
  84. .mpu_state = PWRDM_POWER_OFF,
  85. .core_state = PWRDM_POWER_RET,
  86. .per_min_state = PWRDM_POWER_OFF,
  87. },
  88. {
  89. .mpu_state = PWRDM_POWER_OFF,
  90. .core_state = PWRDM_POWER_OFF,
  91. .per_min_state = PWRDM_POWER_OFF,
  92. },
  93. };
  94. /* Private functions */
  95. static int __omap3_enter_idle(struct cpuidle_device *dev,
  96. struct cpuidle_driver *drv,
  97. int index)
  98. {
  99. struct omap3_idle_statedata *cx = &omap3_idle_data[index];
  100. local_fiq_disable();
  101. if (omap_irq_pending() || need_resched())
  102. goto return_sleep_time;
  103. /* Deny idle for C1 */
  104. if (cx->flags & OMAP_CPUIDLE_CX_NO_CLKDM_IDLE) {
  105. clkdm_deny_idle(mpu_pd->pwrdm_clkdms[0]);
  106. } else {
  107. pwrdm_set_next_pwrst(mpu_pd, cx->mpu_state);
  108. pwrdm_set_next_pwrst(core_pd, cx->core_state);
  109. }
  110. /*
  111. * Call idle CPU PM enter notifier chain so that
  112. * VFP context is saved.
  113. */
  114. if (cx->mpu_state == PWRDM_POWER_OFF)
  115. cpu_pm_enter();
  116. /* Execute ARM wfi */
  117. omap_sram_idle();
  118. /*
  119. * Call idle CPU PM enter notifier chain to restore
  120. * VFP context.
  121. */
  122. if (cx->mpu_state == PWRDM_POWER_OFF &&
  123. pwrdm_read_prev_pwrst(mpu_pd) == PWRDM_POWER_OFF)
  124. cpu_pm_exit();
  125. /* Re-allow idle for C1 */
  126. if (cx->flags & OMAP_CPUIDLE_CX_NO_CLKDM_IDLE)
  127. clkdm_allow_idle(mpu_pd->pwrdm_clkdms[0]);
  128. return_sleep_time:
  129. local_fiq_enable();
  130. return index;
  131. }
  132. /**
  133. * omap3_enter_idle - Programs OMAP3 to enter the specified state
  134. * @dev: cpuidle device
  135. * @drv: cpuidle driver
  136. * @index: the index of state to be entered
  137. *
  138. * Called from the CPUidle framework to program the device to the
  139. * specified target state selected by the governor.
  140. */
  141. static inline int omap3_enter_idle(struct cpuidle_device *dev,
  142. struct cpuidle_driver *drv,
  143. int index)
  144. {
  145. return cpuidle_wrap_enter(dev, drv, index, __omap3_enter_idle);
  146. }
  147. /**
  148. * next_valid_state - Find next valid C-state
  149. * @dev: cpuidle device
  150. * @drv: cpuidle driver
  151. * @index: Index of currently selected c-state
  152. *
  153. * If the state corresponding to index is valid, index is returned back
  154. * to the caller. Else, this function searches for a lower c-state which is
  155. * still valid (as defined in omap3_power_states[]) and returns its index.
  156. *
  157. * A state is valid if the 'valid' field is enabled and
  158. * if it satisfies the enable_off_mode condition.
  159. */
  160. static int next_valid_state(struct cpuidle_device *dev,
  161. struct cpuidle_driver *drv, int index)
  162. {
  163. struct omap3_idle_statedata *cx = &omap3_idle_data[index];
  164. u32 mpu_deepest_state = PWRDM_POWER_RET;
  165. u32 core_deepest_state = PWRDM_POWER_RET;
  166. int idx;
  167. int next_index = 0; /* C1 is the default value */
  168. if (enable_off_mode) {
  169. mpu_deepest_state = PWRDM_POWER_OFF;
  170. /*
  171. * Erratum i583: valable for ES rev < Es1.2 on 3630.
  172. * CORE OFF mode is not supported in a stable form, restrict
  173. * instead the CORE state to RET.
  174. */
  175. if (!IS_PM34XX_ERRATUM(PM_SDRC_WAKEUP_ERRATUM_i583))
  176. core_deepest_state = PWRDM_POWER_OFF;
  177. }
  178. /* Check if current state is valid */
  179. if ((cx->mpu_state >= mpu_deepest_state) &&
  180. (cx->core_state >= core_deepest_state))
  181. return index;
  182. /*
  183. * Drop to next valid state.
  184. * Start search from the next (lower) state.
  185. */
  186. for (idx = index - 1; idx >= 0; idx--) {
  187. cx = &omap3_idle_data[idx];
  188. if ((cx->mpu_state >= mpu_deepest_state) &&
  189. (cx->core_state >= core_deepest_state)) {
  190. next_index = idx;
  191. break;
  192. }
  193. }
  194. return next_index;
  195. }
  196. /**
  197. * omap3_enter_idle_bm - Checks for any bus activity
  198. * @dev: cpuidle device
  199. * @drv: cpuidle driver
  200. * @index: array index of target state to be programmed
  201. *
  202. * This function checks for any pending activity and then programs
  203. * the device to the specified or a safer state.
  204. */
  205. static int omap3_enter_idle_bm(struct cpuidle_device *dev,
  206. struct cpuidle_driver *drv,
  207. int index)
  208. {
  209. int new_state_idx, ret;
  210. u8 per_next_state, per_saved_state;
  211. struct omap3_idle_statedata *cx;
  212. /*
  213. * Use only C1 if CAM is active.
  214. * CAM does not have wakeup capability in OMAP3.
  215. */
  216. if (pwrdm_read_pwrst(cam_pd) == PWRDM_POWER_ON)
  217. new_state_idx = drv->safe_state_index;
  218. else
  219. new_state_idx = next_valid_state(dev, drv, index);
  220. /*
  221. * FIXME: we currently manage device-specific idle states
  222. * for PER and CORE in combination with CPU-specific
  223. * idle states. This is wrong, and device-specific
  224. * idle management needs to be separated out into
  225. * its own code.
  226. */
  227. /* Program PER state */
  228. cx = &omap3_idle_data[new_state_idx];
  229. per_next_state = pwrdm_read_next_pwrst(per_pd);
  230. per_saved_state = per_next_state;
  231. if (per_next_state < cx->per_min_state) {
  232. per_next_state = cx->per_min_state;
  233. pwrdm_set_next_pwrst(per_pd, per_next_state);
  234. }
  235. ret = omap3_enter_idle(dev, drv, new_state_idx);
  236. /* Restore original PER state if it was modified */
  237. if (per_next_state != per_saved_state)
  238. pwrdm_set_next_pwrst(per_pd, per_saved_state);
  239. return ret;
  240. }
  241. static DEFINE_PER_CPU(struct cpuidle_device, omap3_idle_dev);
  242. static struct cpuidle_driver omap3_idle_driver = {
  243. .name = "omap3_idle",
  244. .owner = THIS_MODULE,
  245. .states = {
  246. {
  247. .enter = omap3_enter_idle_bm,
  248. .exit_latency = 2 + 2,
  249. .target_residency = 5,
  250. .flags = CPUIDLE_FLAG_TIME_VALID,
  251. .name = "C1",
  252. .desc = "MPU ON + CORE ON",
  253. },
  254. {
  255. .enter = omap3_enter_idle_bm,
  256. .exit_latency = 10 + 10,
  257. .target_residency = 30,
  258. .flags = CPUIDLE_FLAG_TIME_VALID,
  259. .name = "C2",
  260. .desc = "MPU ON + CORE ON",
  261. },
  262. {
  263. .enter = omap3_enter_idle_bm,
  264. .exit_latency = 50 + 50,
  265. .target_residency = 300,
  266. .flags = CPUIDLE_FLAG_TIME_VALID,
  267. .name = "C3",
  268. .desc = "MPU RET + CORE ON",
  269. },
  270. {
  271. .enter = omap3_enter_idle_bm,
  272. .exit_latency = 1500 + 1800,
  273. .target_residency = 4000,
  274. .flags = CPUIDLE_FLAG_TIME_VALID,
  275. .name = "C4",
  276. .desc = "MPU OFF + CORE ON",
  277. },
  278. {
  279. .enter = omap3_enter_idle_bm,
  280. .exit_latency = 2500 + 7500,
  281. .target_residency = 12000,
  282. .flags = CPUIDLE_FLAG_TIME_VALID,
  283. .name = "C5",
  284. .desc = "MPU RET + CORE RET",
  285. },
  286. {
  287. .enter = omap3_enter_idle_bm,
  288. .exit_latency = 3000 + 8500,
  289. .target_residency = 15000,
  290. .flags = CPUIDLE_FLAG_TIME_VALID,
  291. .name = "C6",
  292. .desc = "MPU OFF + CORE RET",
  293. },
  294. {
  295. .enter = omap3_enter_idle_bm,
  296. .exit_latency = 10000 + 30000,
  297. .target_residency = 30000,
  298. .flags = CPUIDLE_FLAG_TIME_VALID,
  299. .name = "C7",
  300. .desc = "MPU OFF + CORE OFF",
  301. },
  302. },
  303. .state_count = ARRAY_SIZE(omap3_idle_data),
  304. .safe_state_index = 0,
  305. };
  306. /* Public functions */
  307. /**
  308. * omap3_idle_init - Init routine for OMAP3 idle
  309. *
  310. * Registers the OMAP3 specific cpuidle driver to the cpuidle
  311. * framework with the valid set of states.
  312. */
  313. int __init omap3_idle_init(void)
  314. {
  315. struct cpuidle_device *dev;
  316. mpu_pd = pwrdm_lookup("mpu_pwrdm");
  317. core_pd = pwrdm_lookup("core_pwrdm");
  318. per_pd = pwrdm_lookup("per_pwrdm");
  319. cam_pd = pwrdm_lookup("cam_pwrdm");
  320. if (!mpu_pd || !core_pd || !per_pd || !cam_pd)
  321. return -ENODEV;
  322. cpuidle_register_driver(&omap3_idle_driver);
  323. dev = &per_cpu(omap3_idle_dev, smp_processor_id());
  324. dev->cpu = 0;
  325. if (cpuidle_register_device(dev)) {
  326. printk(KERN_ERR "%s: CPUidle register device failed\n",
  327. __func__);
  328. return -EIO;
  329. }
  330. return 0;
  331. }