cpuidle-arm.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * ARM/ARM64 generic CPU idle driver.
  3. *
  4. * Copyright (C) 2014 ARM Ltd.
  5. * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #define pr_fmt(fmt) "CPUidle arm: " fmt
  12. #include <linux/cpuidle.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/cpu_pm.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/slab.h>
  19. #include <asm/cpuidle.h>
  20. #include "dt_idle_states.h"
  21. /*
  22. * arm_enter_idle_state - Programs CPU to enter the specified state
  23. *
  24. * dev: cpuidle device
  25. * drv: cpuidle driver
  26. * idx: state index
  27. *
  28. * Called from the CPUidle framework to program the device to the
  29. * specified target state selected by the governor.
  30. */
  31. static int arm_enter_idle_state(struct cpuidle_device *dev,
  32. struct cpuidle_driver *drv, int idx)
  33. {
  34. int ret;
  35. if (!idx) {
  36. cpu_do_idle();
  37. return idx;
  38. }
  39. ret = cpu_pm_enter();
  40. if (!ret) {
  41. /*
  42. * Pass idle state index to cpu_suspend which in turn will
  43. * call the CPU ops suspend protocol with idle index as a
  44. * parameter.
  45. */
  46. ret = arm_cpuidle_suspend(idx);
  47. cpu_pm_exit();
  48. }
  49. return ret ? -1 : idx;
  50. }
  51. static struct cpuidle_driver arm_idle_driver = {
  52. .name = "arm_idle",
  53. .owner = THIS_MODULE,
  54. /*
  55. * State at index 0 is standby wfi and considered standard
  56. * on all ARM platforms. If in some platforms simple wfi
  57. * can't be used as "state 0", DT bindings must be implemented
  58. * to work around this issue and allow installing a special
  59. * handler for idle state index 0.
  60. */
  61. .states[0] = {
  62. .enter = arm_enter_idle_state,
  63. .exit_latency = 1,
  64. .target_residency = 1,
  65. .power_usage = UINT_MAX,
  66. .name = "WFI",
  67. .desc = "ARM WFI",
  68. }
  69. };
  70. static const struct of_device_id arm_idle_state_match[] __initconst = {
  71. { .compatible = "arm,idle-state",
  72. .data = arm_enter_idle_state },
  73. { },
  74. };
  75. /*
  76. * arm_idle_init
  77. *
  78. * Registers the arm specific cpuidle driver with the cpuidle
  79. * framework. It relies on core code to parse the idle states
  80. * and initialize them using driver data structures accordingly.
  81. */
  82. static int __init arm_idle_init(void)
  83. {
  84. int cpu, ret;
  85. struct cpuidle_driver *drv = &arm_idle_driver;
  86. struct cpuidle_device *dev;
  87. /*
  88. * Initialize idle states data, starting at index 1.
  89. * This driver is DT only, if no DT idle states are detected (ret == 0)
  90. * let the driver initialization fail accordingly since there is no
  91. * reason to initialize the idle driver if only wfi is supported.
  92. */
  93. ret = dt_init_idle_driver(drv, arm_idle_state_match, 1);
  94. if (ret <= 0)
  95. return ret ? : -ENODEV;
  96. ret = cpuidle_register_driver(drv);
  97. if (ret) {
  98. pr_err("Failed to register cpuidle driver\n");
  99. return ret;
  100. }
  101. /*
  102. * Call arch CPU operations in order to initialize
  103. * idle states suspend back-end specific data
  104. */
  105. for_each_possible_cpu(cpu) {
  106. ret = arm_cpuidle_init(cpu);
  107. /*
  108. * Skip the cpuidle device initialization if the reported
  109. * failure is a HW misconfiguration/breakage (-ENXIO).
  110. */
  111. if (ret == -ENXIO)
  112. continue;
  113. if (ret) {
  114. pr_err("CPU %d failed to init idle CPU ops\n", cpu);
  115. goto out_fail;
  116. }
  117. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  118. if (!dev) {
  119. pr_err("Failed to allocate cpuidle device\n");
  120. ret = -ENOMEM;
  121. goto out_fail;
  122. }
  123. dev->cpu = cpu;
  124. ret = cpuidle_register_device(dev);
  125. if (ret) {
  126. pr_err("Failed to register cpuidle device for CPU %d\n",
  127. cpu);
  128. kfree(dev);
  129. goto out_fail;
  130. }
  131. }
  132. return 0;
  133. out_fail:
  134. while (--cpu >= 0) {
  135. dev = per_cpu(cpuidle_devices, cpu);
  136. cpuidle_unregister_device(dev);
  137. kfree(dev);
  138. }
  139. cpuidle_unregister_driver(drv);
  140. return ret;
  141. }
  142. device_initcall(arm_idle_init);