cpuidle44xx.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * OMAP4+ CPU idle Routines
  3. *
  4. * Copyright (C) 2011-2013 Texas Instruments, Inc.
  5. * Santosh Shilimkar <santosh.shilimkar@ti.com>
  6. * Rajendra Nayak <rnayak@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/cpuidle.h>
  14. #include <linux/cpu_pm.h>
  15. #include <linux/export.h>
  16. #include <linux/clockchips.h>
  17. #include <asm/cpuidle.h>
  18. #include <asm/proc-fns.h>
  19. #include "common.h"
  20. #include "pm.h"
  21. #include "prm.h"
  22. #include "clockdomain.h"
  23. #define MAX_CPUS 2
  24. /* Machine specific information */
  25. struct idle_statedata {
  26. u32 cpu_state;
  27. u32 mpu_logic_state;
  28. u32 mpu_state;
  29. };
  30. static struct idle_statedata omap4_idle_data[] = {
  31. {
  32. .cpu_state = PWRDM_POWER_ON,
  33. .mpu_state = PWRDM_POWER_ON,
  34. .mpu_logic_state = PWRDM_POWER_RET,
  35. },
  36. {
  37. .cpu_state = PWRDM_POWER_OFF,
  38. .mpu_state = PWRDM_POWER_RET,
  39. .mpu_logic_state = PWRDM_POWER_RET,
  40. },
  41. {
  42. .cpu_state = PWRDM_POWER_OFF,
  43. .mpu_state = PWRDM_POWER_RET,
  44. .mpu_logic_state = PWRDM_POWER_OFF,
  45. },
  46. };
  47. static struct powerdomain *mpu_pd, *cpu_pd[MAX_CPUS];
  48. static struct clockdomain *cpu_clkdm[MAX_CPUS];
  49. static atomic_t abort_barrier;
  50. static bool cpu_done[MAX_CPUS];
  51. static struct idle_statedata *state_ptr = &omap4_idle_data[0];
  52. /* Private functions */
  53. /**
  54. * omap_enter_idle_[simple/coupled] - OMAP4PLUS cpuidle entry functions
  55. * @dev: cpuidle device
  56. * @drv: cpuidle driver
  57. * @index: the index of state to be entered
  58. *
  59. * Called from the CPUidle framework to program the device to the
  60. * specified low power state selected by the governor.
  61. * Returns the amount of time spent in the low power state.
  62. */
  63. static int omap_enter_idle_simple(struct cpuidle_device *dev,
  64. struct cpuidle_driver *drv,
  65. int index)
  66. {
  67. omap_do_wfi();
  68. return index;
  69. }
  70. static int omap_enter_idle_coupled(struct cpuidle_device *dev,
  71. struct cpuidle_driver *drv,
  72. int index)
  73. {
  74. struct idle_statedata *cx = state_ptr + index;
  75. u32 mpuss_can_lose_context = 0;
  76. int cpu_id = smp_processor_id();
  77. /*
  78. * CPU0 has to wait and stay ON until CPU1 is OFF state.
  79. * This is necessary to honour hardware recommondation
  80. * of triggeing all the possible low power modes once CPU1 is
  81. * out of coherency and in OFF mode.
  82. */
  83. if (dev->cpu == 0 && cpumask_test_cpu(1, cpu_online_mask)) {
  84. while (pwrdm_read_pwrst(cpu_pd[1]) != PWRDM_POWER_OFF) {
  85. cpu_relax();
  86. /*
  87. * CPU1 could have already entered & exited idle
  88. * without hitting off because of a wakeup
  89. * or a failed attempt to hit off mode. Check for
  90. * that here, otherwise we could spin forever
  91. * waiting for CPU1 off.
  92. */
  93. if (cpu_done[1])
  94. goto fail;
  95. }
  96. }
  97. mpuss_can_lose_context = (cx->mpu_state == PWRDM_POWER_RET) &&
  98. (cx->mpu_logic_state == PWRDM_POWER_OFF);
  99. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu_id);
  100. /*
  101. * Call idle CPU PM enter notifier chain so that
  102. * VFP and per CPU interrupt context is saved.
  103. */
  104. cpu_pm_enter();
  105. if (dev->cpu == 0) {
  106. pwrdm_set_logic_retst(mpu_pd, cx->mpu_logic_state);
  107. omap_set_pwrdm_state(mpu_pd, cx->mpu_state);
  108. /*
  109. * Call idle CPU cluster PM enter notifier chain
  110. * to save GIC and wakeupgen context.
  111. */
  112. if (mpuss_can_lose_context)
  113. cpu_cluster_pm_enter();
  114. }
  115. omap4_enter_lowpower(dev->cpu, cx->cpu_state);
  116. cpu_done[dev->cpu] = true;
  117. /* Wakeup CPU1 only if it is not offlined */
  118. if (dev->cpu == 0 && cpumask_test_cpu(1, cpu_online_mask)) {
  119. if (IS_PM44XX_ERRATUM(PM_OMAP4_ROM_SMP_BOOT_ERRATUM_GICD) &&
  120. mpuss_can_lose_context)
  121. gic_dist_disable();
  122. clkdm_wakeup(cpu_clkdm[1]);
  123. omap_set_pwrdm_state(cpu_pd[1], PWRDM_POWER_ON);
  124. clkdm_allow_idle(cpu_clkdm[1]);
  125. if (IS_PM44XX_ERRATUM(PM_OMAP4_ROM_SMP_BOOT_ERRATUM_GICD) &&
  126. mpuss_can_lose_context) {
  127. while (gic_dist_disabled()) {
  128. udelay(1);
  129. cpu_relax();
  130. }
  131. gic_timer_retrigger();
  132. }
  133. }
  134. /*
  135. * Call idle CPU PM exit notifier chain to restore
  136. * VFP and per CPU IRQ context.
  137. */
  138. cpu_pm_exit();
  139. /*
  140. * Call idle CPU cluster PM exit notifier chain
  141. * to restore GIC and wakeupgen context.
  142. */
  143. if (dev->cpu == 0 && mpuss_can_lose_context)
  144. cpu_cluster_pm_exit();
  145. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu_id);
  146. fail:
  147. cpuidle_coupled_parallel_barrier(dev, &abort_barrier);
  148. cpu_done[dev->cpu] = false;
  149. return index;
  150. }
  151. /*
  152. * For each cpu, setup the broadcast timer because local timers
  153. * stops for the states above C1.
  154. */
  155. static void omap_setup_broadcast_timer(void *arg)
  156. {
  157. int cpu = smp_processor_id();
  158. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ON, &cpu);
  159. }
  160. static struct cpuidle_driver omap4_idle_driver = {
  161. .name = "omap4_idle",
  162. .owner = THIS_MODULE,
  163. .states = {
  164. {
  165. /* C1 - CPU0 ON + CPU1 ON + MPU ON */
  166. .exit_latency = 2 + 2,
  167. .target_residency = 5,
  168. .flags = CPUIDLE_FLAG_TIME_VALID,
  169. .enter = omap_enter_idle_simple,
  170. .name = "C1",
  171. .desc = "CPUx ON, MPUSS ON"
  172. },
  173. {
  174. /* C2 - CPU0 OFF + CPU1 OFF + MPU CSWR */
  175. .exit_latency = 328 + 440,
  176. .target_residency = 960,
  177. .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_COUPLED,
  178. .enter = omap_enter_idle_coupled,
  179. .name = "C2",
  180. .desc = "CPUx OFF, MPUSS CSWR",
  181. },
  182. {
  183. /* C3 - CPU0 OFF + CPU1 OFF + MPU OSWR */
  184. .exit_latency = 460 + 518,
  185. .target_residency = 1100,
  186. .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_COUPLED,
  187. .enter = omap_enter_idle_coupled,
  188. .name = "C3",
  189. .desc = "CPUx OFF, MPUSS OSWR",
  190. },
  191. },
  192. .state_count = ARRAY_SIZE(omap4_idle_data),
  193. .safe_state_index = 0,
  194. };
  195. /* Public functions */
  196. /**
  197. * omap4_idle_init - Init routine for OMAP4+ idle
  198. *
  199. * Registers the OMAP4+ specific cpuidle driver to the cpuidle
  200. * framework with the valid set of states.
  201. */
  202. int __init omap4_idle_init(void)
  203. {
  204. mpu_pd = pwrdm_lookup("mpu_pwrdm");
  205. cpu_pd[0] = pwrdm_lookup("cpu0_pwrdm");
  206. cpu_pd[1] = pwrdm_lookup("cpu1_pwrdm");
  207. if ((!mpu_pd) || (!cpu_pd[0]) || (!cpu_pd[1]))
  208. return -ENODEV;
  209. cpu_clkdm[0] = clkdm_lookup("mpu0_clkdm");
  210. cpu_clkdm[1] = clkdm_lookup("mpu1_clkdm");
  211. if (!cpu_clkdm[0] || !cpu_clkdm[1])
  212. return -ENODEV;
  213. /* Configure the broadcast timer on each cpu */
  214. on_each_cpu(omap_setup_broadcast_timer, NULL, 1);
  215. return cpuidle_register(&omap4_idle_driver, cpu_online_mask);
  216. }