cpuidle.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2012 Linaro Ltd.
  4. */
  5. #include <linux/cpuidle.h>
  6. #include <linux/of.h>
  7. #include <linux/of_device.h>
  8. #include <asm/cpuidle.h>
  9. extern struct of_cpuidle_method __cpuidle_method_of_table[];
  10. static const struct of_cpuidle_method __cpuidle_method_of_table_sentinel
  11. __used __section("__cpuidle_method_of_table_end");
  12. static struct cpuidle_ops cpuidle_ops[NR_CPUS] __ro_after_init;
  13. /**
  14. * arm_cpuidle_simple_enter() - a wrapper to cpu_do_idle()
  15. * @dev: not used
  16. * @drv: not used
  17. * @index: not used
  18. *
  19. * A trivial wrapper to allow the cpu_do_idle function to be assigned as a
  20. * cpuidle callback by matching the function signature.
  21. *
  22. * Returns the index passed as parameter
  23. */
  24. int arm_cpuidle_simple_enter(struct cpuidle_device *dev,
  25. struct cpuidle_driver *drv, int index)
  26. {
  27. cpu_do_idle();
  28. return index;
  29. }
  30. /**
  31. * arm_cpuidle_suspend() - function to enter low power idle states
  32. * @index: an integer used as an identifier for the low level PM callbacks
  33. *
  34. * This function calls the underlying arch specific low level PM code as
  35. * registered at the init time.
  36. *
  37. * Returns the result of the suspend callback.
  38. */
  39. int arm_cpuidle_suspend(int index)
  40. {
  41. int cpu = smp_processor_id();
  42. return cpuidle_ops[cpu].suspend(index);
  43. }
  44. /**
  45. * arm_cpuidle_get_ops() - find a registered cpuidle_ops by name
  46. * @method: the method name
  47. *
  48. * Search in the __cpuidle_method_of_table array the cpuidle ops matching the
  49. * method name.
  50. *
  51. * Returns a struct cpuidle_ops pointer, NULL if not found.
  52. */
  53. static const struct cpuidle_ops *__init arm_cpuidle_get_ops(const char *method)
  54. {
  55. struct of_cpuidle_method *m = __cpuidle_method_of_table;
  56. for (; m->method; m++)
  57. if (!strcmp(m->method, method))
  58. return m->ops;
  59. return NULL;
  60. }
  61. /**
  62. * arm_cpuidle_read_ops() - Initialize the cpuidle ops with the device tree
  63. * @dn: a pointer to a struct device node corresponding to a cpu node
  64. * @cpu: the cpu identifier
  65. *
  66. * Get the method name defined in the 'enable-method' property, retrieve the
  67. * associated cpuidle_ops and do a struct copy. This copy is needed because all
  68. * cpuidle_ops are tagged __initconst and will be unloaded after the init
  69. * process.
  70. *
  71. * Return 0 on sucess, -ENOENT if no 'enable-method' is defined, -EOPNOTSUPP if
  72. * no cpuidle_ops is registered for the 'enable-method', or if either init or
  73. * suspend callback isn't defined.
  74. */
  75. static int __init arm_cpuidle_read_ops(struct device_node *dn, int cpu)
  76. {
  77. const char *enable_method;
  78. const struct cpuidle_ops *ops;
  79. enable_method = of_get_property(dn, "enable-method", NULL);
  80. if (!enable_method)
  81. return -ENOENT;
  82. ops = arm_cpuidle_get_ops(enable_method);
  83. if (!ops) {
  84. pr_warn("%pOF: unsupported enable-method property: %s\n",
  85. dn, enable_method);
  86. return -EOPNOTSUPP;
  87. }
  88. if (!ops->init || !ops->suspend) {
  89. pr_warn("cpuidle_ops '%s': no init or suspend callback\n",
  90. enable_method);
  91. return -EOPNOTSUPP;
  92. }
  93. cpuidle_ops[cpu] = *ops; /* structure copy */
  94. pr_notice("cpuidle: enable-method property '%s'"
  95. " found operations\n", enable_method);
  96. return 0;
  97. }
  98. /**
  99. * arm_cpuidle_init() - Initialize cpuidle_ops for a specific cpu
  100. * @cpu: the cpu to be initialized
  101. *
  102. * Initialize the cpuidle ops with the device for the cpu and then call
  103. * the cpu's idle initialization callback. This may fail if the underlying HW
  104. * is not operational.
  105. *
  106. * Returns:
  107. * 0 on success,
  108. * -ENODEV if it fails to find the cpu node in the device tree,
  109. * -EOPNOTSUPP if it does not find a registered and valid cpuidle_ops for
  110. * this cpu,
  111. * -ENOENT if it fails to find an 'enable-method' property,
  112. * -ENXIO if the HW reports a failure or a misconfiguration,
  113. * -ENOMEM if the HW report an memory allocation failure
  114. */
  115. int __init arm_cpuidle_init(int cpu)
  116. {
  117. struct device_node *cpu_node = of_cpu_device_node_get(cpu);
  118. int ret;
  119. if (!cpu_node)
  120. return -ENODEV;
  121. ret = arm_cpuidle_read_ops(cpu_node, cpu);
  122. if (!ret)
  123. ret = cpuidle_ops[cpu].init(cpu_node, cpu);
  124. of_node_put(cpu_node);
  125. return ret;
  126. }