psci.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. int __init psci_init(void)
  151. {
  152. struct device_node *np;
  153. const char *method;
  154. u32 id;
  155. int err = 0;
  156. np = of_find_matching_node(NULL, psci_of_match);
  157. if (!np)
  158. return -ENODEV;
  159. pr_info("probing function IDs from device-tree\n");
  160. if (of_property_read_string(np, "method", &method)) {
  161. pr_warning("missing \"method\" property\n");
  162. err = -ENXIO;
  163. goto out_put_node;
  164. }
  165. if (!strcmp("hvc", method)) {
  166. invoke_psci_fn = __invoke_psci_fn_hvc;
  167. } else if (!strcmp("smc", method)) {
  168. invoke_psci_fn = __invoke_psci_fn_smc;
  169. } else {
  170. pr_warning("invalid \"method\" property: %s\n", method);
  171. err = -EINVAL;
  172. goto out_put_node;
  173. }
  174. if (!of_property_read_u32(np, "cpu_suspend", &id)) {
  175. psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
  176. psci_ops.cpu_suspend = psci_cpu_suspend;
  177. }
  178. if (!of_property_read_u32(np, "cpu_off", &id)) {
  179. psci_function_id[PSCI_FN_CPU_OFF] = id;
  180. psci_ops.cpu_off = psci_cpu_off;
  181. }
  182. if (!of_property_read_u32(np, "cpu_on", &id)) {
  183. psci_function_id[PSCI_FN_CPU_ON] = id;
  184. psci_ops.cpu_on = psci_cpu_on;
  185. }
  186. if (!of_property_read_u32(np, "migrate", &id)) {
  187. psci_function_id[PSCI_FN_MIGRATE] = id;
  188. psci_ops.migrate = psci_migrate;
  189. }
  190. out_put_node:
  191. of_node_put(np);
  192. return err;
  193. }
  194. #ifdef CONFIG_SMP
  195. static int __init cpu_psci_cpu_init(struct device_node *dn, unsigned int cpu)
  196. {
  197. return 0;
  198. }
  199. static int __init cpu_psci_cpu_prepare(unsigned int cpu)
  200. {
  201. if (!psci_ops.cpu_on) {
  202. pr_err("no cpu_on method, not booting CPU%d\n", cpu);
  203. return -ENODEV;
  204. }
  205. return 0;
  206. }
  207. static int cpu_psci_cpu_boot(unsigned int cpu)
  208. {
  209. int err = psci_ops.cpu_on(cpu_logical_map(cpu), __pa(secondary_entry));
  210. if (err)
  211. pr_err("psci: failed to boot CPU%d (%d)\n", cpu, err);
  212. return err;
  213. }
  214. #ifdef CONFIG_HOTPLUG_CPU
  215. static int cpu_psci_cpu_disable(unsigned int cpu)
  216. {
  217. /* Fail early if we don't have CPU_OFF support */
  218. if (!psci_ops.cpu_off)
  219. return -EOPNOTSUPP;
  220. return 0;
  221. }
  222. static void cpu_psci_cpu_die(unsigned int cpu)
  223. {
  224. int ret;
  225. /*
  226. * There are no known implementations of PSCI actually using the
  227. * power state field, pass a sensible default for now.
  228. */
  229. struct psci_power_state state = {
  230. .type = PSCI_POWER_STATE_TYPE_POWER_DOWN,
  231. };
  232. ret = psci_ops.cpu_off(state);
  233. pr_crit("psci: unable to power off CPU%u (%d)\n", cpu, ret);
  234. }
  235. #endif
  236. const struct cpu_operations cpu_psci_ops = {
  237. .name = "psci",
  238. .cpu_init = cpu_psci_cpu_init,
  239. .cpu_prepare = cpu_psci_cpu_prepare,
  240. .cpu_boot = cpu_psci_cpu_boot,
  241. #ifdef CONFIG_HOTPLUG_CPU
  242. .cpu_disable = cpu_psci_cpu_disable,
  243. .cpu_die = cpu_psci_cpu_die,
  244. #endif
  245. };
  246. #endif