cpuidle-mt81xx_v2.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (C) 2015 MediaTek Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/cpu_pm.h>
  9. #include <linux/cpuidle.h>
  10. #include <linux/cpumask.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/slab.h>
  15. #include <asm/cpuidle.h>
  16. #include "dt_idle_states.h"
  17. #define USING_TICK_BROADCAST 1
  18. #define IDLE_TAG "[Power/swap]"
  19. #define idle_dbg(fmt, args...) pr_debug(IDLE_TAG fmt, ##args)
  20. int __attribute__((weak)) dpidle_enter(int cpu)
  21. {
  22. return 1;
  23. }
  24. int __attribute__((weak)) soidle_enter(int cpu)
  25. {
  26. return 1;
  27. }
  28. int __attribute__((weak)) mcidle_enter(int cpu)
  29. {
  30. return 1;
  31. }
  32. int __attribute__((weak)) slidle_enter(int cpu)
  33. {
  34. return 1;
  35. }
  36. int __attribute__((weak)) rgidle_enter(int cpu)
  37. {
  38. return 1;
  39. }
  40. static int mt_dpidle_enter(struct cpuidle_device *dev,
  41. struct cpuidle_driver *drv, int index)
  42. {
  43. return dpidle_enter(smp_processor_id());
  44. }
  45. static int mt_soidle_enter(struct cpuidle_device *dev,
  46. struct cpuidle_driver *drv, int index)
  47. {
  48. return soidle_enter(smp_processor_id());
  49. }
  50. static int mt_mcidle_enter(struct cpuidle_device *dev,
  51. struct cpuidle_driver *drv, int index)
  52. {
  53. return mcidle_enter(smp_processor_id());
  54. }
  55. static int mt_slidle_enter(struct cpuidle_device *dev,
  56. struct cpuidle_driver *drv, int index)
  57. {
  58. return slidle_enter(smp_processor_id());
  59. }
  60. static int mt_rgidle_enter(struct cpuidle_device *dev,
  61. struct cpuidle_driver *drv, int index)
  62. {
  63. return rgidle_enter(smp_processor_id());
  64. }
  65. static struct cpuidle_driver mt81xx_v2_cpuidle_driver = {
  66. .name = "mt81xx_v2_cpuidle",
  67. .owner = THIS_MODULE,
  68. .states[0] = {
  69. .enter = mt_dpidle_enter,
  70. .exit_latency = 2000, /* 2 ms */
  71. .target_residency = 1,
  72. #if USING_TICK_BROADCAST
  73. .flags = CPUIDLE_FLAG_TIMER_STOP,
  74. #else
  75. .flags = CPUIDLE_FLAG_TIME_VALID,
  76. #endif
  77. .name = "dpidle",
  78. .desc = "deepidle",
  79. },
  80. .states[1] = {
  81. .enter = mt_soidle_enter,
  82. .exit_latency = 2000, /* 2 ms */
  83. .target_residency = 1,
  84. #if USING_TICK_BROADCAST
  85. .flags = CPUIDLE_FLAG_TIMER_STOP,
  86. #else
  87. .flags = CPUIDLE_FLAG_TIME_VALID,
  88. #endif
  89. .name = "SODI",
  90. .desc = "SODI",
  91. },
  92. .states[2] = {
  93. .enter = mt_mcidle_enter,
  94. .exit_latency = 2000, /* 2 ms */
  95. .target_residency = 1,
  96. #if USING_TICK_BROADCAST
  97. .flags = CPUIDLE_FLAG_TIMER_STOP,
  98. #else
  99. .flags = CPUIDLE_FLAG_TIME_VALID,
  100. #endif
  101. .name = "MCDI",
  102. .desc = "MCDI",
  103. },
  104. .states[3] = {
  105. .enter = mt_slidle_enter,
  106. .exit_latency = 2000, /* 2 ms */
  107. .target_residency = 1,
  108. #if USING_TICK_BROADCAST
  109. #else
  110. .flags = CPUIDLE_FLAG_TIME_VALID,
  111. #endif
  112. .name = "slidle",
  113. .desc = "slidle",
  114. },
  115. .states[4] = {
  116. .enter = mt_rgidle_enter,
  117. .exit_latency = 2000, /* 2 ms */
  118. .target_residency = 1,
  119. #if USING_TICK_BROADCAST
  120. #else
  121. .flags = CPUIDLE_FLAG_TIME_VALID,
  122. #endif
  123. .name = "rgidle",
  124. .desc = "WFI",
  125. },
  126. .state_count = 5,
  127. .safe_state_index = 0,
  128. };
  129. static const struct of_device_id mt81xx_v2_idle_state_match[] __initconst = {
  130. {.compatible = "arm,idle-state"}, {},
  131. };
  132. /*
  133. * arm64_idle_init
  134. *
  135. * Registers the arm64 specific cpuidle driver with the cpuidle
  136. * framework. It relies on core code to parse the idle states
  137. * and initialize them using driver data structures accordingly.
  138. */
  139. int __init mt81xx_v2_cpuidle_init(void)
  140. {
  141. int cpu, ret;
  142. struct cpuidle_driver *drv = &mt81xx_v2_cpuidle_driver;
  143. struct cpuidle_device *dev;
  144. ret = cpuidle_register_driver(drv);
  145. if (ret) {
  146. pr_debug("Failed to register cpuidle driver\n");
  147. return ret;
  148. }
  149. /*
  150. * Call arch CPU operations in order to initialize
  151. * idle states suspend back-end specific data
  152. */
  153. for_each_possible_cpu(cpu) {
  154. ret = arm_cpuidle_init(cpu);
  155. /*
  156. * Skip the cpuidle device initialization if the reported
  157. * failure is a HW misconfiguration/breakage (-ENXIO).
  158. */
  159. if (ret == -ENXIO)
  160. continue;
  161. if (ret) {
  162. pr_debug("CPU %d failed to init idle CPU ops\n", cpu);
  163. goto out_fail;
  164. }
  165. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  166. if (!dev)
  167. goto out_fail;
  168. dev->cpu = cpu;
  169. ret = cpuidle_register_device(dev);
  170. if (ret) {
  171. pr_debug("Failed to register cpuidle device for CPU %d\n",
  172. cpu);
  173. kfree(dev);
  174. goto out_fail;
  175. }
  176. }
  177. return 0;
  178. out_fail:
  179. while (--cpu >= 0) {
  180. dev = per_cpu(cpuidle_devices, cpu);
  181. cpuidle_unregister_device(dev);
  182. kfree(dev);
  183. }
  184. cpuidle_unregister_driver(drv);
  185. return ret;
  186. }
  187. device_initcall(mt81xx_v2_cpuidle_init);