psci.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 <asm/compiler.h>
  20. #include <asm/cpu_ops.h>
  21. #include <asm/errno.h>
  22. #include <asm/psci.h>
  23. #include <asm/smp_plat.h>
  24. #define PSCI_POWER_STATE_TYPE_STANDBY 0
  25. #define PSCI_POWER_STATE_TYPE_POWER_DOWN 1
  26. struct psci_power_state {
  27. u16 id;
  28. u8 type;
  29. u8 affinity_level;
  30. };
  31. struct psci_operations {
  32. int (*cpu_suspend)(struct psci_power_state state,
  33. unsigned long entry_point);
  34. int (*cpu_off)(struct psci_power_state state);
  35. int (*cpu_on)(unsigned long cpuid, unsigned long entry_point);
  36. int (*migrate)(unsigned long cpuid);
  37. };
  38. static struct psci_operations psci_ops;
  39. static int (*invoke_psci_fn)(u64, u64, u64, u64);
  40. enum psci_function {
  41. PSCI_FN_CPU_SUSPEND,
  42. PSCI_FN_CPU_ON,
  43. PSCI_FN_CPU_OFF,
  44. PSCI_FN_MIGRATE,
  45. PSCI_FN_MAX,
  46. };
  47. static u32 psci_function_id[PSCI_FN_MAX];
  48. #define PSCI_RET_SUCCESS 0
  49. #define PSCI_RET_EOPNOTSUPP -1
  50. #define PSCI_RET_EINVAL -2
  51. #define PSCI_RET_EPERM -3
  52. static int psci_to_linux_errno(int errno)
  53. {
  54. switch (errno) {
  55. case PSCI_RET_SUCCESS:
  56. return 0;
  57. case PSCI_RET_EOPNOTSUPP:
  58. return -EOPNOTSUPP;
  59. case PSCI_RET_EINVAL:
  60. return -EINVAL;
  61. case PSCI_RET_EPERM:
  62. return -EPERM;
  63. };
  64. return -EINVAL;
  65. }
  66. #define PSCI_POWER_STATE_ID_MASK 0xffff
  67. #define PSCI_POWER_STATE_ID_SHIFT 0
  68. #define PSCI_POWER_STATE_TYPE_MASK 0x1
  69. #define PSCI_POWER_STATE_TYPE_SHIFT 16
  70. #define PSCI_POWER_STATE_AFFL_MASK 0x3
  71. #define PSCI_POWER_STATE_AFFL_SHIFT 24
  72. static u32 psci_power_state_pack(struct psci_power_state state)
  73. {
  74. return ((state.id & PSCI_POWER_STATE_ID_MASK)
  75. << PSCI_POWER_STATE_ID_SHIFT) |
  76. ((state.type & PSCI_POWER_STATE_TYPE_MASK)
  77. << PSCI_POWER_STATE_TYPE_SHIFT) |
  78. ((state.affinity_level & PSCI_POWER_STATE_AFFL_MASK)
  79. << PSCI_POWER_STATE_AFFL_SHIFT);
  80. }
  81. /*
  82. * The following two functions are invoked via the invoke_psci_fn pointer
  83. * and will not be inlined, allowing us to piggyback on the AAPCS.
  84. */
  85. static noinline int __invoke_psci_fn_hvc(u64 function_id, u64 arg0, u64 arg1,
  86. u64 arg2)
  87. {
  88. asm volatile(
  89. __asmeq("%0", "x0")
  90. __asmeq("%1", "x1")
  91. __asmeq("%2", "x2")
  92. __asmeq("%3", "x3")
  93. "hvc #0\n"
  94. : "+r" (function_id)
  95. : "r" (arg0), "r" (arg1), "r" (arg2));
  96. return function_id;
  97. }
  98. static noinline int __invoke_psci_fn_smc(u64 function_id, u64 arg0, u64 arg1,
  99. u64 arg2)
  100. {
  101. asm volatile(
  102. __asmeq("%0", "x0")
  103. __asmeq("%1", "x1")
  104. __asmeq("%2", "x2")
  105. __asmeq("%3", "x3")
  106. "smc #0\n"
  107. : "+r" (function_id)
  108. : "r" (arg0), "r" (arg1), "r" (arg2));
  109. return function_id;
  110. }
  111. static int psci_cpu_suspend(struct psci_power_state state,
  112. unsigned long entry_point)
  113. {
  114. int err;
  115. u32 fn, power_state;
  116. fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
  117. power_state = psci_power_state_pack(state);
  118. err = invoke_psci_fn(fn, power_state, entry_point, 0);
  119. return psci_to_linux_errno(err);
  120. }
  121. static int psci_cpu_off(struct psci_power_state state)
  122. {
  123. int err;
  124. u32 fn, power_state;
  125. fn = psci_function_id[PSCI_FN_CPU_OFF];
  126. power_state = psci_power_state_pack(state);
  127. err = invoke_psci_fn(fn, power_state, 0, 0);
  128. return psci_to_linux_errno(err);
  129. }
  130. static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
  131. {
  132. int err;
  133. u32 fn;
  134. fn = psci_function_id[PSCI_FN_CPU_ON];
  135. err = invoke_psci_fn(fn, cpuid, entry_point, 0);
  136. return psci_to_linux_errno(err);
  137. }
  138. static int psci_migrate(unsigned long cpuid)
  139. {
  140. int err;
  141. u32 fn;
  142. fn = psci_function_id[PSCI_FN_MIGRATE];
  143. err = invoke_psci_fn(fn, cpuid, 0, 0);
  144. return psci_to_linux_errno(err);
  145. }
  146. static const struct of_device_id psci_of_match[] __initconst = {
  147. { .compatible = "arm,psci", },
  148. {},
  149. };
  150. void __init psci_init(void)
  151. {
  152. struct device_node *np;
  153. const char *method;
  154. u32 id;
  155. np = of_find_matching_node(NULL, psci_of_match);
  156. if (!np)
  157. return;
  158. pr_info("probing function IDs from device-tree\n");
  159. if (of_property_read_string(np, "method", &method)) {
  160. pr_warning("missing \"method\" property\n");
  161. goto out_put_node;
  162. }
  163. if (!strcmp("hvc", method)) {
  164. invoke_psci_fn = __invoke_psci_fn_hvc;
  165. } else if (!strcmp("smc", method)) {
  166. invoke_psci_fn = __invoke_psci_fn_smc;
  167. } else {
  168. pr_warning("invalid \"method\" property: %s\n", method);
  169. goto out_put_node;
  170. }
  171. if (!of_property_read_u32(np, "cpu_suspend", &id)) {
  172. psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
  173. psci_ops.cpu_suspend = psci_cpu_suspend;
  174. }
  175. if (!of_property_read_u32(np, "cpu_off", &id)) {
  176. psci_function_id[PSCI_FN_CPU_OFF] = id;
  177. psci_ops.cpu_off = psci_cpu_off;
  178. }
  179. if (!of_property_read_u32(np, "cpu_on", &id)) {
  180. psci_function_id[PSCI_FN_CPU_ON] = id;
  181. psci_ops.cpu_on = psci_cpu_on;
  182. }
  183. if (!of_property_read_u32(np, "migrate", &id)) {
  184. psci_function_id[PSCI_FN_MIGRATE] = id;
  185. psci_ops.migrate = psci_migrate;
  186. }
  187. out_put_node:
  188. of_node_put(np);
  189. return;
  190. }
  191. #ifdef CONFIG_SMP
  192. static int __init cpu_psci_cpu_init(struct device_node *dn, unsigned int cpu)
  193. {
  194. return 0;
  195. }
  196. static int __init cpu_psci_cpu_prepare(unsigned int cpu)
  197. {
  198. if (!psci_ops.cpu_on) {
  199. pr_err("no cpu_on method, not booting CPU%d\n", cpu);
  200. return -ENODEV;
  201. }
  202. return 0;
  203. }
  204. static int cpu_psci_cpu_boot(unsigned int cpu)
  205. {
  206. int err = psci_ops.cpu_on(cpu_logical_map(cpu), __pa(secondary_entry));
  207. if (err)
  208. pr_err("failed to boot CPU%d (%d)\n", cpu, err);
  209. return err;
  210. }
  211. #ifdef CONFIG_HOTPLUG_CPU
  212. static int cpu_psci_cpu_disable(unsigned int cpu)
  213. {
  214. /* Fail early if we don't have CPU_OFF support */
  215. if (!psci_ops.cpu_off)
  216. return -EOPNOTSUPP;
  217. return 0;
  218. }
  219. static void cpu_psci_cpu_die(unsigned int cpu)
  220. {
  221. int ret;
  222. /*
  223. * There are no known implementations of PSCI actually using the
  224. * power state field, pass a sensible default for now.
  225. */
  226. struct psci_power_state state = {
  227. .type = PSCI_POWER_STATE_TYPE_POWER_DOWN,
  228. };
  229. ret = psci_ops.cpu_off(state);
  230. pr_crit("unable to power off CPU%u (%d)\n", cpu, ret);
  231. }
  232. #endif
  233. const struct cpu_operations cpu_psci_ops = {
  234. .name = "psci",
  235. .cpu_init = cpu_psci_cpu_init,
  236. .cpu_prepare = cpu_psci_cpu_prepare,
  237. .cpu_boot = cpu_psci_cpu_boot,
  238. #ifdef CONFIG_HOTPLUG_CPU
  239. .cpu_disable = cpu_psci_cpu_disable,
  240. .cpu_die = cpu_psci_cpu_die,
  241. #endif
  242. };
  243. #endif