dt_idle_states.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * DT idle states parsing code.
  4. *
  5. * Copyright (C) 2014 ARM Ltd.
  6. * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
  7. */
  8. #define pr_fmt(fmt) "DT idle-states: " fmt
  9. #include <linux/cpuidle.h>
  10. #include <linux/cpumask.h>
  11. #include <linux/errno.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include "dt_idle_states.h"
  16. static int init_state_node(struct cpuidle_state *idle_state,
  17. const struct of_device_id *match_id,
  18. struct device_node *state_node)
  19. {
  20. int err;
  21. const char *desc;
  22. /*
  23. * CPUidle drivers are expected to initialize the const void *data
  24. * pointer of the passed in struct of_device_id array to the idle
  25. * state enter function.
  26. */
  27. idle_state->enter = match_id->data;
  28. /*
  29. * Since this is not a "coupled" state, it's safe to assume interrupts
  30. * won't be enabled when it exits allowing the tick to be frozen
  31. * safely. So enter() can be also enter_s2idle() callback.
  32. */
  33. idle_state->enter_s2idle = match_id->data;
  34. err = of_property_read_u32(state_node, "wakeup-latency-us",
  35. &idle_state->exit_latency);
  36. if (err) {
  37. u32 entry_latency, exit_latency;
  38. err = of_property_read_u32(state_node, "entry-latency-us",
  39. &entry_latency);
  40. if (err) {
  41. pr_debug(" * %pOF missing entry-latency-us property\n",
  42. state_node);
  43. return -EINVAL;
  44. }
  45. err = of_property_read_u32(state_node, "exit-latency-us",
  46. &exit_latency);
  47. if (err) {
  48. pr_debug(" * %pOF missing exit-latency-us property\n",
  49. state_node);
  50. return -EINVAL;
  51. }
  52. /*
  53. * If wakeup-latency-us is missing, default to entry+exit
  54. * latencies as defined in idle states bindings
  55. */
  56. idle_state->exit_latency = entry_latency + exit_latency;
  57. }
  58. err = of_property_read_u32(state_node, "min-residency-us",
  59. &idle_state->target_residency);
  60. if (err) {
  61. pr_debug(" * %pOF missing min-residency-us property\n",
  62. state_node);
  63. return -EINVAL;
  64. }
  65. err = of_property_read_string(state_node, "idle-state-name", &desc);
  66. if (err)
  67. desc = state_node->name;
  68. idle_state->flags = CPUIDLE_FLAG_RCU_IDLE;
  69. if (of_property_read_bool(state_node, "local-timer-stop"))
  70. idle_state->flags |= CPUIDLE_FLAG_TIMER_STOP;
  71. /*
  72. * TODO:
  73. * replace with kstrdup and pointer assignment when name
  74. * and desc become string pointers
  75. */
  76. strscpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN);
  77. strscpy(idle_state->desc, desc, CPUIDLE_DESC_LEN);
  78. return 0;
  79. }
  80. /*
  81. * Check that the idle state is uniform across all CPUs in the CPUidle driver
  82. * cpumask
  83. */
  84. static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
  85. const cpumask_t *cpumask)
  86. {
  87. int cpu;
  88. struct device_node *cpu_node, *curr_state_node;
  89. /*
  90. * Compare idle state phandles for index idx on all CPUs in the
  91. * CPUidle driver cpumask. Start from next logical cpu following
  92. * cpumask_first(cpumask) since that's the CPU state_node was
  93. * retrieved from. If a mismatch is found bail out straight
  94. * away since we certainly hit a firmware misconfiguration.
  95. */
  96. cpu = cpumask_first(cpumask) + 1;
  97. for_each_cpu_from(cpu, cpumask) {
  98. cpu_node = of_cpu_device_node_get(cpu);
  99. curr_state_node = of_get_cpu_state_node(cpu_node, idx);
  100. of_node_put(curr_state_node);
  101. of_node_put(cpu_node);
  102. if (state_node != curr_state_node)
  103. return false;
  104. }
  105. return true;
  106. }
  107. /**
  108. * dt_init_idle_driver() - Parse the DT idle states and initialize the
  109. * idle driver states array
  110. * @drv: Pointer to CPU idle driver to be initialized
  111. * @matches: Array of of_device_id match structures to search in for
  112. * compatible idle state nodes. The data pointer for each valid
  113. * struct of_device_id entry in the matches array must point to
  114. * a function with the following signature, that corresponds to
  115. * the CPUidle state enter function signature:
  116. *
  117. * int (*)(struct cpuidle_device *dev,
  118. * struct cpuidle_driver *drv,
  119. * int index);
  120. *
  121. * @start_idx: First idle state index to be initialized
  122. *
  123. * If DT idle states are detected and are valid the state count and states
  124. * array entries in the cpuidle driver are initialized accordingly starting
  125. * from index start_idx.
  126. *
  127. * Return: number of valid DT idle states parsed, <0 on failure
  128. */
  129. int dt_init_idle_driver(struct cpuidle_driver *drv,
  130. const struct of_device_id *matches,
  131. unsigned int start_idx)
  132. {
  133. struct cpuidle_state *idle_state;
  134. struct device_node *state_node, *cpu_node;
  135. const struct of_device_id *match_id;
  136. int i, err = 0;
  137. const cpumask_t *cpumask;
  138. unsigned int state_idx = start_idx;
  139. if (state_idx >= CPUIDLE_STATE_MAX)
  140. return -EINVAL;
  141. /*
  142. * We get the idle states for the first logical cpu in the
  143. * driver mask (or cpu_possible_mask if the driver cpumask is not set)
  144. * and we check through idle_state_valid() if they are uniform
  145. * across CPUs, otherwise we hit a firmware misconfiguration.
  146. */
  147. cpumask = drv->cpumask ? : cpu_possible_mask;
  148. cpu_node = of_cpu_device_node_get(cpumask_first(cpumask));
  149. for (i = 0; ; i++) {
  150. state_node = of_get_cpu_state_node(cpu_node, i);
  151. if (!state_node)
  152. break;
  153. match_id = of_match_node(matches, state_node);
  154. if (!match_id) {
  155. err = -ENODEV;
  156. break;
  157. }
  158. if (!of_device_is_available(state_node)) {
  159. of_node_put(state_node);
  160. continue;
  161. }
  162. if (!idle_state_valid(state_node, i, cpumask)) {
  163. pr_warn("%pOF idle state not valid, bailing out\n",
  164. state_node);
  165. err = -EINVAL;
  166. break;
  167. }
  168. if (state_idx == CPUIDLE_STATE_MAX) {
  169. pr_warn("State index reached static CPU idle driver states array size\n");
  170. break;
  171. }
  172. idle_state = &drv->states[state_idx++];
  173. err = init_state_node(idle_state, match_id, state_node);
  174. if (err) {
  175. pr_err("Parsing idle state node %pOF failed with err %d\n",
  176. state_node, err);
  177. err = -EINVAL;
  178. break;
  179. }
  180. of_node_put(state_node);
  181. }
  182. of_node_put(state_node);
  183. of_node_put(cpu_node);
  184. if (err)
  185. return err;
  186. /* Set the number of total supported idle states. */
  187. drv->state_count = state_idx;
  188. /*
  189. * Return the number of present and valid DT idle states, which can
  190. * also be 0 on platforms with missing DT idle states or legacy DT
  191. * configuration predating the DT idle states bindings.
  192. */
  193. return state_idx - start_idx;
  194. }
  195. EXPORT_SYMBOL_GPL(dt_init_idle_driver);