psci.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * Copyright (C) 2013 ARM Limited
  12. *
  13. * Author: Will Deacon <will.deacon@arm.com>
  14. */
  15. #define pr_fmt(fmt) "psci: " fmt
  16. #include <linux/init.h>
  17. #include <linux/of.h>
  18. #include <linux/smp.h>
  19. #include <linux/delay.h>
  20. #include <linux/psci.h>
  21. #include <linux/slab.h>
  22. #include <uapi/linux/psci.h>
  23. #include <asm/compiler.h>
  24. #include <asm/cpu_ops.h>
  25. #include <asm/errno.h>
  26. #include <asm/smp_plat.h>
  27. #include <asm/suspend.h>
  28. static bool psci_power_state_loses_context(u32 state)
  29. {
  30. return state & PSCI_0_2_POWER_STATE_TYPE_MASK;
  31. }
  32. static bool psci_power_state_is_valid(u32 state)
  33. {
  34. const u32 valid_mask = PSCI_0_2_POWER_STATE_ID_MASK |
  35. PSCI_0_2_POWER_STATE_TYPE_MASK |
  36. PSCI_0_2_POWER_STATE_AFFL_MASK;
  37. return !(state & ~valid_mask);
  38. }
  39. static DEFINE_PER_CPU_READ_MOSTLY(u32 *, psci_power_state);
  40. static int __maybe_unused cpu_psci_cpu_init_idle(unsigned int cpu)
  41. {
  42. int i, ret, count = 0;
  43. u32 *psci_states;
  44. struct device_node *state_node, *cpu_node;
  45. cpu_node = of_get_cpu_node(cpu, NULL);
  46. if (!cpu_node)
  47. return -ENODEV;
  48. /*
  49. * If the PSCI cpu_suspend function hook has not been initialized
  50. * idle states must not be enabled, so bail out
  51. */
  52. if (!psci_ops.cpu_suspend)
  53. return -EOPNOTSUPP;
  54. /* Count idle states */
  55. while ((state_node = of_parse_phandle(cpu_node, "cpu-idle-states",
  56. count))) {
  57. count++;
  58. of_node_put(state_node);
  59. }
  60. if (!count)
  61. return -ENODEV;
  62. psci_states = kcalloc(count, sizeof(*psci_states), GFP_KERNEL);
  63. if (!psci_states)
  64. return -ENOMEM;
  65. for (i = 0; i < count; i++) {
  66. u32 state;
  67. state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
  68. ret = of_property_read_u32(state_node,
  69. "arm,psci-suspend-param",
  70. &state);
  71. if (ret) {
  72. pr_warn(" * %s missing arm,psci-suspend-param property\n",
  73. state_node->full_name);
  74. of_node_put(state_node);
  75. goto free_mem;
  76. }
  77. of_node_put(state_node);
  78. pr_debug("psci-power-state %#x index %d\n", state, i);
  79. if (!psci_power_state_is_valid(state)) {
  80. pr_warn("Invalid PSCI power state %#x\n", state);
  81. ret = -EINVAL;
  82. goto free_mem;
  83. }
  84. psci_states[i] = state;
  85. }
  86. /* Idle states parsed correctly, initialize per-cpu pointer */
  87. per_cpu(psci_power_state, cpu) = psci_states;
  88. return 0;
  89. free_mem:
  90. kfree(psci_states);
  91. return ret;
  92. }
  93. #ifdef CONFIG_SMP
  94. static int __init cpu_psci_cpu_init(unsigned int cpu)
  95. {
  96. return 0;
  97. }
  98. static int __init cpu_psci_cpu_prepare(unsigned int cpu)
  99. {
  100. if (!psci_ops.cpu_on) {
  101. pr_err("no cpu_on method, not booting CPU%d\n", cpu);
  102. return -ENODEV;
  103. }
  104. return 0;
  105. }
  106. static int cpu_psci_cpu_boot(unsigned int cpu)
  107. {
  108. int err = psci_ops.cpu_on(cpu_logical_map(cpu), __pa(secondary_entry));
  109. if (err)
  110. pr_err("failed to boot CPU%d (%d)\n", cpu, err);
  111. return err;
  112. }
  113. #ifdef CONFIG_HOTPLUG_CPU
  114. static int cpu_psci_cpu_disable(unsigned int cpu)
  115. {
  116. /* Fail early if we don't have CPU_OFF support */
  117. if (!psci_ops.cpu_off)
  118. return -EOPNOTSUPP;
  119. /* Trusted OS will deny CPU_OFF */
  120. if (psci_tos_resident_on(cpu))
  121. return -EPERM;
  122. return 0;
  123. }
  124. static void cpu_psci_cpu_die(unsigned int cpu)
  125. {
  126. int ret;
  127. /*
  128. * There are no known implementations of PSCI actually using the
  129. * power state field, pass a sensible default for now.
  130. */
  131. u32 state = PSCI_POWER_STATE_TYPE_POWER_DOWN <<
  132. PSCI_0_2_POWER_STATE_TYPE_SHIFT;
  133. ret = psci_ops.cpu_off(state);
  134. pr_crit("unable to power off CPU%u (%d)\n", cpu, ret);
  135. }
  136. static int cpu_psci_cpu_kill(unsigned int cpu)
  137. {
  138. int err, i;
  139. if (!psci_ops.affinity_info)
  140. return 0;
  141. /*
  142. * cpu_kill could race with cpu_die and we can
  143. * potentially end up declaring this cpu undead
  144. * while it is dying. So, try again a few times.
  145. */
  146. for (i = 0; i < 10; i++) {
  147. err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
  148. if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
  149. pr_info("CPU%d killed.\n", cpu);
  150. return 0;
  151. }
  152. msleep(10);
  153. pr_info("Retrying again to check for CPU kill\n");
  154. }
  155. pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
  156. cpu, err);
  157. return -ETIMEDOUT;
  158. }
  159. #endif
  160. #endif
  161. static int psci_suspend_finisher(unsigned long index)
  162. {
  163. u32 *state = __this_cpu_read(psci_power_state);
  164. return psci_ops.cpu_suspend(state[index - 1],
  165. virt_to_phys(cpu_resume));
  166. }
  167. static int __maybe_unused cpu_psci_cpu_suspend(unsigned long index)
  168. {
  169. int ret;
  170. u32 *state = __this_cpu_read(psci_power_state);
  171. /*
  172. * idle state index 0 corresponds to wfi, should never be called
  173. * from the cpu_suspend operations
  174. */
  175. if (WARN_ON_ONCE(!index))
  176. return -EINVAL;
  177. if (!psci_power_state_loses_context(state[index - 1]))
  178. ret = psci_ops.cpu_suspend(state[index - 1], 0);
  179. else
  180. ret = cpu_suspend(index, psci_suspend_finisher);
  181. return ret;
  182. }
  183. const struct cpu_operations cpu_psci_ops = {
  184. .name = "psci",
  185. #ifdef CONFIG_CPU_IDLE
  186. .cpu_init_idle = cpu_psci_cpu_init_idle,
  187. .cpu_suspend = cpu_psci_cpu_suspend,
  188. #endif
  189. #ifdef CONFIG_SMP
  190. .cpu_init = cpu_psci_cpu_init,
  191. .cpu_prepare = cpu_psci_cpu_prepare,
  192. .cpu_boot = cpu_psci_cpu_boot,
  193. #ifdef CONFIG_HOTPLUG_CPU
  194. .cpu_disable = cpu_psci_cpu_disable,
  195. .cpu_die = cpu_psci_cpu_die,
  196. .cpu_kill = cpu_psci_cpu_kill,
  197. #endif
  198. #endif
  199. };