dt_idle_states.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. bool valid = true;
  90. /*
  91. * Compare idle state phandles for index idx on all CPUs in the
  92. * CPUidle driver cpumask. Start from next logical cpu following
  93. * cpumask_first(cpumask) since that's the CPU state_node was
  94. * retrieved from. If a mismatch is found bail out straight
  95. * away since we certainly hit a firmware misconfiguration.
  96. */
  97. for (cpu = cpumask_next(cpumask_first(cpumask), cpumask);
  98. cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) {
  99. cpu_node = of_cpu_device_node_get(cpu);
  100. curr_state_node = of_get_cpu_state_node(cpu_node, idx);
  101. if (state_node != curr_state_node)
  102. valid = false;
  103. of_node_put(curr_state_node);
  104. of_node_put(cpu_node);
  105. if (!valid)
  106. break;
  107. }
  108. return valid;
  109. }
  110. /**
  111. * dt_init_idle_driver() - Parse the DT idle states and initialize the
  112. * idle driver states array
  113. * @drv: Pointer to CPU idle driver to be initialized
  114. * @matches: Array of of_device_id match structures to search in for
  115. * compatible idle state nodes. The data pointer for each valid
  116. * struct of_device_id entry in the matches array must point to
  117. * a function with the following signature, that corresponds to
  118. * the CPUidle state enter function signature:
  119. *
  120. * int (*)(struct cpuidle_device *dev,
  121. * struct cpuidle_driver *drv,
  122. * int index);
  123. *
  124. * @start_idx: First idle state index to be initialized
  125. *
  126. * If DT idle states are detected and are valid the state count and states
  127. * array entries in the cpuidle driver are initialized accordingly starting
  128. * from index start_idx.
  129. *
  130. * Return: number of valid DT idle states parsed, <0 on failure
  131. */
  132. int dt_init_idle_driver(struct cpuidle_driver *drv,
  133. const struct of_device_id *matches,
  134. unsigned int start_idx)
  135. {
  136. struct cpuidle_state *idle_state;
  137. struct device_node *state_node, *cpu_node;
  138. const struct of_device_id *match_id;
  139. int i, err = 0;
  140. const cpumask_t *cpumask;
  141. unsigned int state_idx = start_idx;
  142. if (state_idx >= CPUIDLE_STATE_MAX)
  143. return -EINVAL;
  144. /*
  145. * We get the idle states for the first logical cpu in the
  146. * driver mask (or cpu_possible_mask if the driver cpumask is not set)
  147. * and we check through idle_state_valid() if they are uniform
  148. * across CPUs, otherwise we hit a firmware misconfiguration.
  149. */
  150. cpumask = drv->cpumask ? : cpu_possible_mask;
  151. cpu_node = of_cpu_device_node_get(cpumask_first(cpumask));
  152. for (i = 0; ; i++) {
  153. state_node = of_get_cpu_state_node(cpu_node, i);
  154. if (!state_node)
  155. break;
  156. match_id = of_match_node(matches, state_node);
  157. if (!match_id) {
  158. err = -ENODEV;
  159. break;
  160. }
  161. if (!of_device_is_available(state_node)) {
  162. of_node_put(state_node);
  163. continue;
  164. }
  165. if (!idle_state_valid(state_node, i, cpumask)) {
  166. pr_warn("%pOF idle state not valid, bailing out\n",
  167. state_node);
  168. err = -EINVAL;
  169. break;
  170. }
  171. if (state_idx == CPUIDLE_STATE_MAX) {
  172. pr_warn("State index reached static CPU idle driver states array size\n");
  173. break;
  174. }
  175. idle_state = &drv->states[state_idx++];
  176. err = init_state_node(idle_state, match_id, state_node);
  177. if (err) {
  178. pr_err("Parsing idle state node %pOF failed with err %d\n",
  179. state_node, err);
  180. err = -EINVAL;
  181. break;
  182. }
  183. of_node_put(state_node);
  184. }
  185. of_node_put(state_node);
  186. of_node_put(cpu_node);
  187. if (err)
  188. return err;
  189. /* Set the number of total supported idle states. */
  190. drv->state_count = state_idx;
  191. /*
  192. * Return the number of present and valid DT idle states, which can
  193. * also be 0 on platforms with missing DT idle states or legacy DT
  194. * configuration predating the DT idle states bindings.
  195. */
  196. return state_idx - start_idx;
  197. }
  198. EXPORT_SYMBOL_GPL(dt_init_idle_driver);