cpuidle-arm.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /*
  35. * Pass idle state index to arm_cpuidle_suspend which in turn
  36. * will call the CPU ops suspend protocol with idle index as a
  37. * parameter.
  38. */
  39. return CPU_PM_CPU_IDLE_ENTER(arm_cpuidle_suspend, idx);
  40. }
  41. static struct cpuidle_driver arm_idle_driver = {
  42. .name = "arm_idle",
  43. .owner = THIS_MODULE,
  44. /*
  45. * State at index 0 is standby wfi and considered standard
  46. * on all ARM platforms. If in some platforms simple wfi
  47. * can't be used as "state 0", DT bindings must be implemented
  48. * to work around this issue and allow installing a special
  49. * handler for idle state index 0.
  50. */
  51. .states[0] = {
  52. .enter = arm_enter_idle_state,
  53. .exit_latency = 1,
  54. .target_residency = 1,
  55. .power_usage = UINT_MAX,
  56. .name = "WFI",
  57. .desc = "ARM WFI",
  58. }
  59. };
  60. static const struct of_device_id arm_idle_state_match[] __initconst = {
  61. { .compatible = "arm,idle-state",
  62. .data = arm_enter_idle_state },
  63. { },
  64. };
  65. /*
  66. * arm_idle_init
  67. *
  68. * Registers the arm specific cpuidle driver with the cpuidle
  69. * framework. It relies on core code to parse the idle states
  70. * and initialize them using driver data structures accordingly.
  71. */
  72. static int __init arm_idle_init(void)
  73. {
  74. int cpu, ret;
  75. struct cpuidle_driver *drv = &arm_idle_driver;
  76. struct cpuidle_device *dev;
  77. /*
  78. * Initialize idle states data, starting at index 1.
  79. * This driver is DT only, if no DT idle states are detected (ret == 0)
  80. * let the driver initialization fail accordingly since there is no
  81. * reason to initialize the idle driver if only wfi is supported.
  82. */
  83. ret = dt_init_idle_driver(drv, arm_idle_state_match, 1);
  84. if (ret <= 0)
  85. return ret ? : -ENODEV;
  86. ret = cpuidle_register_driver(drv);
  87. if (ret) {
  88. pr_err("Failed to register cpuidle driver\n");
  89. return ret;
  90. }
  91. /*
  92. * Call arch CPU operations in order to initialize
  93. * idle states suspend back-end specific data
  94. */
  95. for_each_possible_cpu(cpu) {
  96. ret = arm_cpuidle_init(cpu);
  97. /*
  98. * Skip the cpuidle device initialization if the reported
  99. * failure is a HW misconfiguration/breakage (-ENXIO).
  100. */
  101. if (ret == -ENXIO)
  102. continue;
  103. if (ret) {
  104. pr_err("CPU %d failed to init idle CPU ops\n", cpu);
  105. goto out_fail;
  106. }
  107. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  108. if (!dev) {
  109. pr_err("Failed to allocate cpuidle device\n");
  110. ret = -ENOMEM;
  111. goto out_fail;
  112. }
  113. dev->cpu = cpu;
  114. ret = cpuidle_register_device(dev);
  115. if (ret) {
  116. pr_err("Failed to register cpuidle device for CPU %d\n",
  117. cpu);
  118. kfree(dev);
  119. goto out_fail;
  120. }
  121. }
  122. return 0;
  123. out_fail:
  124. while (--cpu >= 0) {
  125. dev = per_cpu(cpuidle_devices, cpu);
  126. cpuidle_unregister_device(dev);
  127. kfree(dev);
  128. }
  129. cpuidle_unregister_driver(drv);
  130. return ret;
  131. }
  132. device_initcall(arm_idle_init);