cpuidle34xx.c 9.3 KB

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