psci.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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/reboot.h>
  20. #include <linux/pm.h>
  21. #include <linux/delay.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/psci.h>
  27. #include <asm/smp_plat.h>
  28. #include <asm/system_misc.h>
  29. #define PSCI_POWER_STATE_TYPE_STANDBY 0
  30. #define PSCI_POWER_STATE_TYPE_POWER_DOWN 1
  31. struct psci_power_state {
  32. u16 id;
  33. u8 type;
  34. u8 affinity_level;
  35. };
  36. struct psci_operations {
  37. int (*cpu_suspend)(struct psci_power_state state,
  38. unsigned long entry_point);
  39. int (*cpu_off)(struct psci_power_state state);
  40. int (*cpu_on)(unsigned long cpuid, unsigned long entry_point);
  41. int (*migrate)(unsigned long cpuid);
  42. int (*affinity_info)(unsigned long target_affinity,
  43. unsigned long lowest_affinity_level);
  44. int (*migrate_info_type)(void);
  45. };
  46. static struct psci_operations psci_ops;
  47. static int (*invoke_psci_fn)(u64, u64, u64, u64);
  48. typedef int (*psci_initcall_t)(const struct device_node *);
  49. enum psci_function {
  50. PSCI_FN_CPU_SUSPEND,
  51. PSCI_FN_CPU_ON,
  52. PSCI_FN_CPU_OFF,
  53. PSCI_FN_MIGRATE,
  54. PSCI_FN_AFFINITY_INFO,
  55. PSCI_FN_MIGRATE_INFO_TYPE,
  56. PSCI_FN_MAX,
  57. };
  58. static u32 psci_function_id[PSCI_FN_MAX];
  59. static int psci_to_linux_errno(int errno)
  60. {
  61. switch (errno) {
  62. case PSCI_RET_SUCCESS:
  63. return 0;
  64. case PSCI_RET_NOT_SUPPORTED:
  65. return -EOPNOTSUPP;
  66. case PSCI_RET_INVALID_PARAMS:
  67. return -EINVAL;
  68. case PSCI_RET_DENIED:
  69. return -EPERM;
  70. };
  71. return -EINVAL;
  72. }
  73. static u32 psci_power_state_pack(struct psci_power_state state)
  74. {
  75. return ((state.id << PSCI_0_2_POWER_STATE_ID_SHIFT)
  76. & PSCI_0_2_POWER_STATE_ID_MASK) |
  77. ((state.type << PSCI_0_2_POWER_STATE_TYPE_SHIFT)
  78. & PSCI_0_2_POWER_STATE_TYPE_MASK) |
  79. ((state.affinity_level << PSCI_0_2_POWER_STATE_AFFL_SHIFT)
  80. & PSCI_0_2_POWER_STATE_AFFL_MASK);
  81. }
  82. /*
  83. * The following two functions are invoked via the invoke_psci_fn pointer
  84. * and will not be inlined, allowing us to piggyback on the AAPCS.
  85. */
  86. static noinline int __invoke_psci_fn_hvc(u64 function_id, u64 arg0, u64 arg1,
  87. u64 arg2)
  88. {
  89. asm volatile(
  90. __asmeq("%0", "x0")
  91. __asmeq("%1", "x1")
  92. __asmeq("%2", "x2")
  93. __asmeq("%3", "x3")
  94. "hvc #0\n"
  95. : "+r" (function_id)
  96. : "r" (arg0), "r" (arg1), "r" (arg2));
  97. return function_id;
  98. }
  99. static noinline int __invoke_psci_fn_smc(u64 function_id, u64 arg0, u64 arg1,
  100. u64 arg2)
  101. {
  102. asm volatile(
  103. __asmeq("%0", "x0")
  104. __asmeq("%1", "x1")
  105. __asmeq("%2", "x2")
  106. __asmeq("%3", "x3")
  107. "smc #0\n"
  108. : "+r" (function_id)
  109. : "r" (arg0), "r" (arg1), "r" (arg2));
  110. return function_id;
  111. }
  112. static int psci_get_version(void)
  113. {
  114. int err;
  115. err = invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
  116. return err;
  117. }
  118. static int psci_cpu_suspend(struct psci_power_state state,
  119. unsigned long entry_point)
  120. {
  121. int err;
  122. u32 fn, power_state;
  123. fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
  124. power_state = psci_power_state_pack(state);
  125. err = invoke_psci_fn(fn, power_state, entry_point, 0);
  126. return psci_to_linux_errno(err);
  127. }
  128. static int psci_cpu_off(struct psci_power_state state)
  129. {
  130. int err;
  131. u32 fn, power_state;
  132. fn = psci_function_id[PSCI_FN_CPU_OFF];
  133. power_state = psci_power_state_pack(state);
  134. err = invoke_psci_fn(fn, power_state, 0, 0);
  135. return psci_to_linux_errno(err);
  136. }
  137. static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
  138. {
  139. int err;
  140. u32 fn;
  141. fn = psci_function_id[PSCI_FN_CPU_ON];
  142. err = invoke_psci_fn(fn, cpuid, entry_point, 0);
  143. return psci_to_linux_errno(err);
  144. }
  145. static int psci_migrate(unsigned long cpuid)
  146. {
  147. int err;
  148. u32 fn;
  149. fn = psci_function_id[PSCI_FN_MIGRATE];
  150. err = invoke_psci_fn(fn, cpuid, 0, 0);
  151. return psci_to_linux_errno(err);
  152. }
  153. static int psci_affinity_info(unsigned long target_affinity,
  154. unsigned long lowest_affinity_level)
  155. {
  156. int err;
  157. u32 fn;
  158. fn = psci_function_id[PSCI_FN_AFFINITY_INFO];
  159. err = invoke_psci_fn(fn, target_affinity, lowest_affinity_level, 0);
  160. return err;
  161. }
  162. static int psci_migrate_info_type(void)
  163. {
  164. int err;
  165. u32 fn;
  166. fn = psci_function_id[PSCI_FN_MIGRATE_INFO_TYPE];
  167. err = invoke_psci_fn(fn, 0, 0, 0);
  168. return err;
  169. }
  170. static int get_set_conduit_method(struct device_node *np)
  171. {
  172. const char *method;
  173. pr_info("probing for conduit method from DT.\n");
  174. if (of_property_read_string(np, "method", &method)) {
  175. pr_warn("missing \"method\" property\n");
  176. return -ENXIO;
  177. }
  178. if (!strcmp("hvc", method)) {
  179. invoke_psci_fn = __invoke_psci_fn_hvc;
  180. } else if (!strcmp("smc", method)) {
  181. invoke_psci_fn = __invoke_psci_fn_smc;
  182. } else {
  183. pr_warn("invalid \"method\" property: %s\n", method);
  184. return -EINVAL;
  185. }
  186. return 0;
  187. }
  188. static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd)
  189. {
  190. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  191. }
  192. static void psci_sys_poweroff(void)
  193. {
  194. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  195. }
  196. /*
  197. * PSCI Function IDs for v0.2+ are well defined so use
  198. * standard values.
  199. */
  200. static int psci_0_2_init(struct device_node *np)
  201. {
  202. int err, ver;
  203. err = get_set_conduit_method(np);
  204. if (err)
  205. goto out_put_node;
  206. ver = psci_get_version();
  207. if (ver == PSCI_RET_NOT_SUPPORTED) {
  208. /* PSCI v0.2 mandates implementation of PSCI_ID_VERSION. */
  209. pr_err("PSCI firmware does not comply with the v0.2 spec.\n");
  210. err = -EOPNOTSUPP;
  211. goto out_put_node;
  212. } else {
  213. pr_info("PSCIv%d.%d detected in firmware.\n",
  214. PSCI_VERSION_MAJOR(ver),
  215. PSCI_VERSION_MINOR(ver));
  216. if (PSCI_VERSION_MAJOR(ver) == 0 &&
  217. PSCI_VERSION_MINOR(ver) < 2) {
  218. err = -EINVAL;
  219. pr_err("Conflicting PSCI version detected.\n");
  220. goto out_put_node;
  221. }
  222. }
  223. pr_info("Using standard PSCI v0.2 function IDs\n");
  224. psci_function_id[PSCI_FN_CPU_SUSPEND] = PSCI_0_2_FN64_CPU_SUSPEND;
  225. psci_ops.cpu_suspend = psci_cpu_suspend;
  226. psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF;
  227. psci_ops.cpu_off = psci_cpu_off;
  228. psci_function_id[PSCI_FN_CPU_ON] = PSCI_0_2_FN64_CPU_ON;
  229. psci_ops.cpu_on = psci_cpu_on;
  230. psci_function_id[PSCI_FN_MIGRATE] = PSCI_0_2_FN64_MIGRATE;
  231. psci_ops.migrate = psci_migrate;
  232. psci_function_id[PSCI_FN_AFFINITY_INFO] = PSCI_0_2_FN64_AFFINITY_INFO;
  233. psci_ops.affinity_info = psci_affinity_info;
  234. psci_function_id[PSCI_FN_MIGRATE_INFO_TYPE] =
  235. PSCI_0_2_FN_MIGRATE_INFO_TYPE;
  236. psci_ops.migrate_info_type = psci_migrate_info_type;
  237. arm_pm_restart = psci_sys_reset;
  238. pm_power_off = psci_sys_poweroff;
  239. out_put_node:
  240. of_node_put(np);
  241. return err;
  242. }
  243. /*
  244. * PSCI < v0.2 get PSCI Function IDs via DT.
  245. */
  246. static int psci_0_1_init(struct device_node *np)
  247. {
  248. u32 id;
  249. int err;
  250. err = get_set_conduit_method(np);
  251. if (err)
  252. goto out_put_node;
  253. pr_info("Using PSCI v0.1 Function IDs from DT\n");
  254. if (!of_property_read_u32(np, "cpu_suspend", &id)) {
  255. psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
  256. psci_ops.cpu_suspend = psci_cpu_suspend;
  257. }
  258. if (!of_property_read_u32(np, "cpu_off", &id)) {
  259. psci_function_id[PSCI_FN_CPU_OFF] = id;
  260. psci_ops.cpu_off = psci_cpu_off;
  261. }
  262. if (!of_property_read_u32(np, "cpu_on", &id)) {
  263. psci_function_id[PSCI_FN_CPU_ON] = id;
  264. psci_ops.cpu_on = psci_cpu_on;
  265. }
  266. if (!of_property_read_u32(np, "migrate", &id)) {
  267. psci_function_id[PSCI_FN_MIGRATE] = id;
  268. psci_ops.migrate = psci_migrate;
  269. }
  270. out_put_node:
  271. of_node_put(np);
  272. return err;
  273. }
  274. static const struct of_device_id psci_of_match[] __initconst = {
  275. { .compatible = "arm,psci", .data = psci_0_1_init},
  276. { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
  277. {},
  278. };
  279. int __init psci_init(void)
  280. {
  281. struct device_node *np;
  282. const struct of_device_id *matched_np;
  283. psci_initcall_t init_fn;
  284. np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
  285. if (!np)
  286. return -ENODEV;
  287. init_fn = (psci_initcall_t)matched_np->data;
  288. return init_fn(np);
  289. }
  290. #ifdef CONFIG_SMP
  291. static int __init cpu_psci_cpu_init(struct device_node *dn, unsigned int cpu)
  292. {
  293. return 0;
  294. }
  295. static int __init cpu_psci_cpu_prepare(unsigned int cpu)
  296. {
  297. if (!psci_ops.cpu_on) {
  298. pr_err("no cpu_on method, not booting CPU%d\n", cpu);
  299. return -ENODEV;
  300. }
  301. return 0;
  302. }
  303. static int cpu_psci_cpu_boot(unsigned int cpu)
  304. {
  305. int err = psci_ops.cpu_on(cpu_logical_map(cpu), __pa(secondary_entry));
  306. if (err)
  307. pr_err("failed to boot CPU%d (%d)\n", cpu, err);
  308. return err;
  309. }
  310. #ifdef CONFIG_HOTPLUG_CPU
  311. static int cpu_psci_cpu_disable(unsigned int cpu)
  312. {
  313. /* Fail early if we don't have CPU_OFF support */
  314. if (!psci_ops.cpu_off)
  315. return -EOPNOTSUPP;
  316. return 0;
  317. }
  318. static void cpu_psci_cpu_die(unsigned int cpu)
  319. {
  320. int ret;
  321. /*
  322. * There are no known implementations of PSCI actually using the
  323. * power state field, pass a sensible default for now.
  324. */
  325. struct psci_power_state state = {
  326. .type = PSCI_POWER_STATE_TYPE_POWER_DOWN,
  327. };
  328. ret = psci_ops.cpu_off(state);
  329. pr_crit("unable to power off CPU%u (%d)\n", cpu, ret);
  330. }
  331. static int cpu_psci_cpu_kill(unsigned int cpu)
  332. {
  333. int err, i;
  334. if (!psci_ops.affinity_info)
  335. return 1;
  336. /*
  337. * cpu_kill could race with cpu_die and we can
  338. * potentially end up declaring this cpu undead
  339. * while it is dying. So, try again a few times.
  340. */
  341. for (i = 0; i < 10; i++) {
  342. err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
  343. if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
  344. pr_info("CPU%d killed.\n", cpu);
  345. return 1;
  346. }
  347. msleep(10);
  348. pr_info("Retrying again to check for CPU kill\n");
  349. }
  350. pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
  351. cpu, err);
  352. /* Make op_cpu_kill() fail. */
  353. return 0;
  354. }
  355. #endif
  356. const struct cpu_operations cpu_psci_ops = {
  357. .name = "psci",
  358. .cpu_init = cpu_psci_cpu_init,
  359. .cpu_prepare = cpu_psci_cpu_prepare,
  360. .cpu_boot = cpu_psci_cpu_boot,
  361. #ifdef CONFIG_HOTPLUG_CPU
  362. .cpu_disable = cpu_psci_cpu_disable,
  363. .cpu_die = cpu_psci_cpu_die,
  364. .cpu_kill = cpu_psci_cpu_kill,
  365. #endif
  366. };
  367. #endif