dt_idle_states.c 6.1 KB

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