dt_idle_states.c 6.3 KB

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