cpuidle-cps.c 4.2 KB

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