cpuidle-cps.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2014 Imagination Technologies
  3. * Author: Paul Burton <paul.burton@imgtec.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. */
  10. #include <linux/cpu_pm.h>
  11. #include <linux/cpuidle.h>
  12. #include <linux/init.h>
  13. #include <asm/idle.h>
  14. #include <asm/pm-cps.h>
  15. /* Enumeration of the various idle states this driver may enter */
  16. enum cps_idle_state {
  17. STATE_WAIT = 0, /* MIPS wait instruction, coherent */
  18. STATE_NC_WAIT, /* MIPS wait instruction, non-coherent */
  19. STATE_CLOCK_GATED, /* Core clock gated */
  20. STATE_POWER_GATED, /* Core power gated */
  21. STATE_COUNT
  22. };
  23. static int cps_nc_enter(struct cpuidle_device *dev,
  24. struct cpuidle_driver *drv, int index)
  25. {
  26. enum cps_pm_state pm_state;
  27. int err;
  28. /*
  29. * At least one core must remain powered up & clocked in order for the
  30. * system to have any hope of functioning.
  31. *
  32. * TODO: don't treat core 0 specially, just prevent the final core
  33. * TODO: remap interrupt affinity temporarily
  34. */
  35. if (!cpu_data[dev->cpu].core && (index > STATE_NC_WAIT))
  36. index = STATE_NC_WAIT;
  37. /* Select the appropriate cps_pm_state */
  38. switch (index) {
  39. case STATE_NC_WAIT:
  40. pm_state = CPS_PM_NC_WAIT;
  41. break;
  42. case STATE_CLOCK_GATED:
  43. pm_state = CPS_PM_CLOCK_GATED;
  44. break;
  45. case STATE_POWER_GATED:
  46. pm_state = CPS_PM_POWER_GATED;
  47. break;
  48. default:
  49. BUG();
  50. return -EINVAL;
  51. }
  52. /* Notify listeners the CPU is about to power down */
  53. if ((pm_state == CPS_PM_POWER_GATED) && cpu_pm_enter())
  54. return -EINTR;
  55. /* Enter that state */
  56. err = cps_pm_enter_state(pm_state);
  57. /* Notify listeners the CPU is back up */
  58. if (pm_state == CPS_PM_POWER_GATED)
  59. cpu_pm_exit();
  60. return err ?: index;
  61. }
  62. static struct cpuidle_driver cps_driver = {
  63. .name = "cpc_cpuidle",
  64. .owner = THIS_MODULE,
  65. .states = {
  66. [STATE_WAIT] = MIPS_CPUIDLE_WAIT_STATE,
  67. [STATE_NC_WAIT] = {
  68. .enter = cps_nc_enter,
  69. .exit_latency = 200,
  70. .target_residency = 450,
  71. .name = "nc-wait",
  72. .desc = "non-coherent MIPS wait",
  73. },
  74. [STATE_CLOCK_GATED] = {
  75. .enter = cps_nc_enter,
  76. .exit_latency = 300,
  77. .target_residency = 700,
  78. .flags = CPUIDLE_FLAG_TIMER_STOP,
  79. .name = "clock-gated",
  80. .desc = "core clock gated",
  81. },
  82. [STATE_POWER_GATED] = {
  83. .enter = cps_nc_enter,
  84. .exit_latency = 600,
  85. .target_residency = 1000,
  86. .flags = CPUIDLE_FLAG_TIMER_STOP,
  87. .name = "power-gated",
  88. .desc = "core power gated",
  89. },
  90. },
  91. .state_count = STATE_COUNT,
  92. .safe_state_index = 0,
  93. };
  94. static void __init cps_cpuidle_unregister(void)
  95. {
  96. int cpu;
  97. struct cpuidle_device *device;
  98. for_each_possible_cpu(cpu) {
  99. device = &per_cpu(cpuidle_dev, cpu);
  100. cpuidle_unregister_device(device);
  101. }
  102. cpuidle_unregister_driver(&cps_driver);
  103. }
  104. static int __init cps_cpuidle_init(void)
  105. {
  106. int err, cpu, core, i;
  107. struct cpuidle_device *device;
  108. /* Detect supported states */
  109. if (!cps_pm_support_state(CPS_PM_POWER_GATED))
  110. cps_driver.state_count = STATE_CLOCK_GATED + 1;
  111. if (!cps_pm_support_state(CPS_PM_CLOCK_GATED))
  112. cps_driver.state_count = STATE_NC_WAIT + 1;
  113. if (!cps_pm_support_state(CPS_PM_NC_WAIT))
  114. cps_driver.state_count = STATE_WAIT + 1;
  115. /* Inform the user if some states are unavailable */
  116. if (cps_driver.state_count < STATE_COUNT) {
  117. pr_info("cpuidle-cps: limited to ");
  118. switch (cps_driver.state_count - 1) {
  119. case STATE_WAIT:
  120. pr_cont("coherent wait\n");
  121. break;
  122. case STATE_NC_WAIT:
  123. pr_cont("non-coherent wait\n");
  124. break;
  125. case STATE_CLOCK_GATED:
  126. pr_cont("clock gating\n");
  127. break;
  128. }
  129. }
  130. /*
  131. * Set the coupled flag on the appropriate states if this system
  132. * requires it.
  133. */
  134. if (coupled_coherence)
  135. for (i = STATE_NC_WAIT; i < cps_driver.state_count; i++)
  136. cps_driver.states[i].flags |= CPUIDLE_FLAG_COUPLED;
  137. err = cpuidle_register_driver(&cps_driver);
  138. if (err) {
  139. pr_err("Failed to register CPS cpuidle driver\n");
  140. return err;
  141. }
  142. for_each_possible_cpu(cpu) {
  143. core = cpu_data[cpu].core;
  144. device = &per_cpu(cpuidle_dev, cpu);
  145. device->cpu = cpu;
  146. #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
  147. cpumask_copy(&device->coupled_cpus, &cpu_sibling_map[cpu]);
  148. #endif
  149. err = cpuidle_register_device(device);
  150. if (err) {
  151. pr_err("Failed to register CPU%d cpuidle device\n",
  152. cpu);
  153. goto err_out;
  154. }
  155. }
  156. return 0;
  157. err_out:
  158. cps_cpuidle_unregister();
  159. return err;
  160. }
  161. device_initcall(cps_cpuidle_init);