cpuidle.c 4.0 KB

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