cpuidle-arm64.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * 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 arm64: " 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 <asm/cpuidle.h>
  19. #include <asm/suspend.h>
  20. #include "dt_idle_states.h"
  21. /*
  22. * arm64_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 arm64_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 = cpu_suspend(idx);
  47. cpu_pm_exit();
  48. }
  49. return ret ? -1 : idx;
  50. }
  51. static struct cpuidle_driver arm64_idle_driver = {
  52. .name = "arm64_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 = arm64_enter_idle_state,
  63. .exit_latency = 1,
  64. .target_residency = 1,
  65. .power_usage = UINT_MAX,
  66. .flags = CPUIDLE_FLAG_TIME_VALID,
  67. .name = "WFI",
  68. .desc = "ARM64 WFI",
  69. }
  70. };
  71. static const struct of_device_id arm64_idle_state_match[] __initconst = {
  72. { .compatible = "arm,idle-state",
  73. .data = arm64_enter_idle_state },
  74. { },
  75. };
  76. /*
  77. * arm64_idle_init
  78. *
  79. * Registers the arm64 specific cpuidle driver with the cpuidle
  80. * framework. It relies on core code to parse the idle states
  81. * and initialize them using driver data structures accordingly.
  82. */
  83. static int __init arm64_idle_init(void)
  84. {
  85. int cpu, ret;
  86. struct cpuidle_driver *drv = &arm64_idle_driver;
  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, arm64_idle_state_match, 1);
  94. if (ret <= 0) {
  95. if (ret)
  96. pr_err("failed to initialize idle states\n");
  97. return ret ? : -ENODEV;
  98. }
  99. /*
  100. * Call arch CPU operations in order to initialize
  101. * idle states suspend back-end specific data
  102. */
  103. for_each_possible_cpu(cpu) {
  104. ret = cpu_init_idle(cpu);
  105. if (ret) {
  106. pr_err("CPU %d failed to init idle CPU ops\n", cpu);
  107. return ret;
  108. }
  109. }
  110. ret = cpuidle_register(drv, NULL);
  111. if (ret) {
  112. pr_err("failed to register cpuidle driver\n");
  113. return ret;
  114. }
  115. return 0;
  116. }
  117. device_initcall(arm64_idle_init);