dt_idle_states.c 6.5 KB

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