psci.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 DEFINE_PER_CPU_READ_MOSTLY(u32 *, psci_power_state);
  29. static int __maybe_unused cpu_psci_cpu_init_idle(unsigned int cpu)
  30. {
  31. int i, ret, count = 0;
  32. u32 *psci_states;
  33. struct device_node *state_node, *cpu_node;
  34. cpu_node = of_get_cpu_node(cpu, NULL);
  35. if (!cpu_node)
  36. return -ENODEV;
  37. /*
  38. * If the PSCI cpu_suspend function hook has not been initialized
  39. * idle states must not be enabled, so bail out
  40. */
  41. if (!psci_ops.cpu_suspend)
  42. return -EOPNOTSUPP;
  43. /* Count idle states */
  44. while ((state_node = of_parse_phandle(cpu_node, "cpu-idle-states",
  45. count))) {
  46. count++;
  47. of_node_put(state_node);
  48. }
  49. if (!count)
  50. return -ENODEV;
  51. psci_states = kcalloc(count, sizeof(*psci_states), GFP_KERNEL);
  52. if (!psci_states)
  53. return -ENOMEM;
  54. for (i = 0; i < count; i++) {
  55. u32 state;
  56. state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
  57. ret = of_property_read_u32(state_node,
  58. "arm,psci-suspend-param",
  59. &state);
  60. if (ret) {
  61. pr_warn(" * %s missing arm,psci-suspend-param property\n",
  62. state_node->full_name);
  63. of_node_put(state_node);
  64. goto free_mem;
  65. }
  66. of_node_put(state_node);
  67. pr_debug("psci-power-state %#x index %d\n", state, i);
  68. if (!psci_power_state_is_valid(state)) {
  69. pr_warn("Invalid PSCI power state %#x\n", state);
  70. ret = -EINVAL;
  71. goto free_mem;
  72. }
  73. psci_states[i] = state;
  74. }
  75. /* Idle states parsed correctly, initialize per-cpu pointer */
  76. per_cpu(psci_power_state, cpu) = psci_states;
  77. return 0;
  78. free_mem:
  79. kfree(psci_states);
  80. return ret;
  81. }
  82. static int __init cpu_psci_cpu_init(unsigned int cpu)
  83. {
  84. return 0;
  85. }
  86. static int __init cpu_psci_cpu_prepare(unsigned int cpu)
  87. {
  88. if (!psci_ops.cpu_on) {
  89. pr_err("no cpu_on method, not booting CPU%d\n", cpu);
  90. return -ENODEV;
  91. }
  92. return 0;
  93. }
  94. static int cpu_psci_cpu_boot(unsigned int cpu)
  95. {
  96. int err = psci_ops.cpu_on(cpu_logical_map(cpu), __pa(secondary_entry));
  97. if (err)
  98. pr_err("failed to boot CPU%d (%d)\n", cpu, err);
  99. return err;
  100. }
  101. #ifdef CONFIG_HOTPLUG_CPU
  102. static int cpu_psci_cpu_disable(unsigned int cpu)
  103. {
  104. /* Fail early if we don't have CPU_OFF support */
  105. if (!psci_ops.cpu_off)
  106. return -EOPNOTSUPP;
  107. /* Trusted OS will deny CPU_OFF */
  108. if (psci_tos_resident_on(cpu))
  109. return -EPERM;
  110. return 0;
  111. }
  112. static void cpu_psci_cpu_die(unsigned int cpu)
  113. {
  114. int ret;
  115. /*
  116. * There are no known implementations of PSCI actually using the
  117. * power state field, pass a sensible default for now.
  118. */
  119. u32 state = PSCI_POWER_STATE_TYPE_POWER_DOWN <<
  120. PSCI_0_2_POWER_STATE_TYPE_SHIFT;
  121. ret = psci_ops.cpu_off(state);
  122. pr_crit("unable to power off CPU%u (%d)\n", cpu, ret);
  123. }
  124. static int cpu_psci_cpu_kill(unsigned int cpu)
  125. {
  126. int err, i;
  127. if (!psci_ops.affinity_info)
  128. return 0;
  129. /*
  130. * cpu_kill could race with cpu_die and we can
  131. * potentially end up declaring this cpu undead
  132. * while it is dying. So, try again a few times.
  133. */
  134. for (i = 0; i < 10; i++) {
  135. err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
  136. if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
  137. pr_info("CPU%d killed.\n", cpu);
  138. return 0;
  139. }
  140. msleep(10);
  141. pr_info("Retrying again to check for CPU kill\n");
  142. }
  143. pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
  144. cpu, err);
  145. return -ETIMEDOUT;
  146. }
  147. #endif
  148. static int psci_suspend_finisher(unsigned long index)
  149. {
  150. u32 *state = __this_cpu_read(psci_power_state);
  151. return psci_ops.cpu_suspend(state[index - 1],
  152. virt_to_phys(cpu_resume));
  153. }
  154. static int __maybe_unused cpu_psci_cpu_suspend(unsigned long index)
  155. {
  156. int ret;
  157. u32 *state = __this_cpu_read(psci_power_state);
  158. /*
  159. * idle state index 0 corresponds to wfi, should never be called
  160. * from the cpu_suspend operations
  161. */
  162. if (WARN_ON_ONCE(!index))
  163. return -EINVAL;
  164. if (!psci_power_state_loses_context(state[index - 1]))
  165. ret = psci_ops.cpu_suspend(state[index - 1], 0);
  166. else
  167. ret = cpu_suspend(index, psci_suspend_finisher);
  168. return ret;
  169. }
  170. const struct cpu_operations cpu_psci_ops = {
  171. .name = "psci",
  172. #ifdef CONFIG_CPU_IDLE
  173. .cpu_init_idle = cpu_psci_cpu_init_idle,
  174. .cpu_suspend = cpu_psci_cpu_suspend,
  175. #endif
  176. .cpu_init = cpu_psci_cpu_init,
  177. .cpu_prepare = cpu_psci_cpu_prepare,
  178. .cpu_boot = cpu_psci_cpu_boot,
  179. #ifdef CONFIG_HOTPLUG_CPU
  180. .cpu_disable = cpu_psci_cpu_disable,
  181. .cpu_die = cpu_psci_cpu_die,
  182. .cpu_kill = cpu_psci_cpu_kill,
  183. #endif
  184. };