dt_idle_states.c 6.3 KB

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