psci.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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) 2015 ARM Limited
  12. */
  13. #define pr_fmt(fmt) "psci: " fmt
  14. #include <linux/errno.h>
  15. #include <linux/linkage.h>
  16. #include <linux/of.h>
  17. #include <linux/pm.h>
  18. #include <linux/printk.h>
  19. #include <linux/psci.h>
  20. #include <linux/reboot.h>
  21. #include <uapi/linux/psci.h>
  22. #include <asm/cputype.h>
  23. #include <asm/system_misc.h>
  24. #include <asm/smp_plat.h>
  25. /*
  26. * While a 64-bit OS can make calls with SMC32 calling conventions, for some
  27. * calls it is necessary to use SMC64 to pass or return 64-bit values.
  28. * For such calls PSCI_FN_NATIVE(version, name) will choose the appropriate
  29. * (native-width) function ID.
  30. */
  31. #ifdef CONFIG_64BIT
  32. #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN64_##name
  33. #else
  34. #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN_##name
  35. #endif
  36. /*
  37. * The CPU any Trusted OS is resident on. The trusted OS may reject CPU_OFF
  38. * calls to its resident CPU, so we must avoid issuing those. We never migrate
  39. * a Trusted OS even if it claims to be capable of migration -- doing so will
  40. * require cooperation with a Trusted OS driver.
  41. */
  42. static int resident_cpu = -1;
  43. bool psci_tos_resident_on(int cpu)
  44. {
  45. return cpu == resident_cpu;
  46. }
  47. struct psci_operations psci_ops;
  48. typedef unsigned long (psci_fn)(unsigned long, unsigned long,
  49. unsigned long, unsigned long);
  50. asmlinkage psci_fn __invoke_psci_fn_hvc;
  51. asmlinkage psci_fn __invoke_psci_fn_smc;
  52. static psci_fn *invoke_psci_fn;
  53. enum psci_function {
  54. PSCI_FN_CPU_SUSPEND,
  55. PSCI_FN_CPU_ON,
  56. PSCI_FN_CPU_OFF,
  57. PSCI_FN_MIGRATE,
  58. PSCI_FN_MAX,
  59. };
  60. static u32 psci_function_id[PSCI_FN_MAX];
  61. #define PSCI_0_2_POWER_STATE_MASK \
  62. (PSCI_0_2_POWER_STATE_ID_MASK | \
  63. PSCI_0_2_POWER_STATE_TYPE_MASK | \
  64. PSCI_0_2_POWER_STATE_AFFL_MASK)
  65. #define PSCI_1_0_EXT_POWER_STATE_MASK \
  66. (PSCI_1_0_EXT_POWER_STATE_ID_MASK | \
  67. PSCI_1_0_EXT_POWER_STATE_TYPE_MASK)
  68. static u32 psci_cpu_suspend_feature;
  69. static inline bool psci_has_ext_power_state(void)
  70. {
  71. return psci_cpu_suspend_feature &
  72. PSCI_1_0_FEATURES_CPU_SUSPEND_PF_MASK;
  73. }
  74. bool psci_power_state_loses_context(u32 state)
  75. {
  76. const u32 mask = psci_has_ext_power_state() ?
  77. PSCI_1_0_EXT_POWER_STATE_TYPE_MASK :
  78. PSCI_0_2_POWER_STATE_TYPE_MASK;
  79. return state & mask;
  80. }
  81. bool psci_power_state_is_valid(u32 state)
  82. {
  83. const u32 valid_mask = psci_has_ext_power_state() ?
  84. PSCI_1_0_EXT_POWER_STATE_MASK :
  85. PSCI_0_2_POWER_STATE_MASK;
  86. return !(state & ~valid_mask);
  87. }
  88. static int psci_to_linux_errno(int errno)
  89. {
  90. switch (errno) {
  91. case PSCI_RET_SUCCESS:
  92. return 0;
  93. case PSCI_RET_NOT_SUPPORTED:
  94. return -EOPNOTSUPP;
  95. case PSCI_RET_INVALID_PARAMS:
  96. case PSCI_RET_INVALID_ADDRESS:
  97. return -EINVAL;
  98. case PSCI_RET_DENIED:
  99. return -EPERM;
  100. };
  101. return -EINVAL;
  102. }
  103. static u32 psci_get_version(void)
  104. {
  105. return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
  106. }
  107. static int psci_cpu_suspend(u32 state, unsigned long entry_point)
  108. {
  109. int err;
  110. u32 fn;
  111. fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
  112. err = invoke_psci_fn(fn, state, entry_point, 0);
  113. return psci_to_linux_errno(err);
  114. }
  115. static int psci_cpu_off(u32 state)
  116. {
  117. int err;
  118. u32 fn;
  119. fn = psci_function_id[PSCI_FN_CPU_OFF];
  120. err = invoke_psci_fn(fn, state, 0, 0);
  121. return psci_to_linux_errno(err);
  122. }
  123. static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
  124. {
  125. int err;
  126. u32 fn;
  127. fn = psci_function_id[PSCI_FN_CPU_ON];
  128. err = invoke_psci_fn(fn, cpuid, entry_point, 0);
  129. return psci_to_linux_errno(err);
  130. }
  131. static int psci_migrate(unsigned long cpuid)
  132. {
  133. int err;
  134. u32 fn;
  135. fn = psci_function_id[PSCI_FN_MIGRATE];
  136. err = invoke_psci_fn(fn, cpuid, 0, 0);
  137. return psci_to_linux_errno(err);
  138. }
  139. static int psci_affinity_info(unsigned long target_affinity,
  140. unsigned long lowest_affinity_level)
  141. {
  142. return invoke_psci_fn(PSCI_FN_NATIVE(0_2, AFFINITY_INFO),
  143. target_affinity, lowest_affinity_level, 0);
  144. }
  145. static int psci_migrate_info_type(void)
  146. {
  147. return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
  148. }
  149. static unsigned long psci_migrate_info_up_cpu(void)
  150. {
  151. return invoke_psci_fn(PSCI_FN_NATIVE(0_2, MIGRATE_INFO_UP_CPU),
  152. 0, 0, 0);
  153. }
  154. static int get_set_conduit_method(struct device_node *np)
  155. {
  156. const char *method;
  157. pr_info("probing for conduit method from DT.\n");
  158. if (of_property_read_string(np, "method", &method)) {
  159. pr_warn("missing \"method\" property\n");
  160. return -ENXIO;
  161. }
  162. if (!strcmp("hvc", method)) {
  163. invoke_psci_fn = __invoke_psci_fn_hvc;
  164. } else if (!strcmp("smc", method)) {
  165. invoke_psci_fn = __invoke_psci_fn_smc;
  166. } else {
  167. pr_warn("invalid \"method\" property: %s\n", method);
  168. return -EINVAL;
  169. }
  170. return 0;
  171. }
  172. static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd)
  173. {
  174. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  175. }
  176. static void psci_sys_poweroff(void)
  177. {
  178. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  179. }
  180. static int __init psci_features(u32 psci_func_id)
  181. {
  182. return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES,
  183. psci_func_id, 0, 0);
  184. }
  185. static void __init psci_init_cpu_suspend(void)
  186. {
  187. int feature = psci_features(psci_function_id[PSCI_FN_CPU_SUSPEND]);
  188. if (feature != PSCI_RET_NOT_SUPPORTED)
  189. psci_cpu_suspend_feature = feature;
  190. }
  191. /*
  192. * Detect the presence of a resident Trusted OS which may cause CPU_OFF to
  193. * return DENIED (which would be fatal).
  194. */
  195. static void __init psci_init_migrate(void)
  196. {
  197. unsigned long cpuid;
  198. int type, cpu = -1;
  199. type = psci_ops.migrate_info_type();
  200. if (type == PSCI_0_2_TOS_MP) {
  201. pr_info("Trusted OS migration not required\n");
  202. return;
  203. }
  204. if (type == PSCI_RET_NOT_SUPPORTED) {
  205. pr_info("MIGRATE_INFO_TYPE not supported.\n");
  206. return;
  207. }
  208. if (type != PSCI_0_2_TOS_UP_MIGRATE &&
  209. type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
  210. pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
  211. return;
  212. }
  213. cpuid = psci_migrate_info_up_cpu();
  214. if (cpuid & ~MPIDR_HWID_BITMASK) {
  215. pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
  216. cpuid);
  217. return;
  218. }
  219. cpu = get_logical_index(cpuid);
  220. resident_cpu = cpu >= 0 ? cpu : -1;
  221. pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid);
  222. }
  223. static void __init psci_0_2_set_functions(void)
  224. {
  225. pr_info("Using standard PSCI v0.2 function IDs\n");
  226. psci_function_id[PSCI_FN_CPU_SUSPEND] =
  227. PSCI_FN_NATIVE(0_2, CPU_SUSPEND);
  228. psci_ops.cpu_suspend = psci_cpu_suspend;
  229. psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF;
  230. psci_ops.cpu_off = psci_cpu_off;
  231. psci_function_id[PSCI_FN_CPU_ON] = PSCI_FN_NATIVE(0_2, CPU_ON);
  232. psci_ops.cpu_on = psci_cpu_on;
  233. psci_function_id[PSCI_FN_MIGRATE] = PSCI_FN_NATIVE(0_2, MIGRATE);
  234. psci_ops.migrate = psci_migrate;
  235. psci_ops.affinity_info = psci_affinity_info;
  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. }
  240. /*
  241. * Probe function for PSCI firmware versions >= 0.2
  242. */
  243. static int __init psci_probe(void)
  244. {
  245. u32 ver = psci_get_version();
  246. pr_info("PSCIv%d.%d detected in firmware.\n",
  247. PSCI_VERSION_MAJOR(ver),
  248. PSCI_VERSION_MINOR(ver));
  249. if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
  250. pr_err("Conflicting PSCI version detected.\n");
  251. return -EINVAL;
  252. }
  253. psci_0_2_set_functions();
  254. psci_init_migrate();
  255. psci_init_cpu_suspend();
  256. return 0;
  257. }
  258. typedef int (*psci_initcall_t)(const struct device_node *);
  259. /*
  260. * PSCI init function for PSCI versions >=0.2
  261. *
  262. * Probe based on PSCI PSCI_VERSION function
  263. */
  264. static int __init psci_0_2_init(struct device_node *np)
  265. {
  266. int err;
  267. err = get_set_conduit_method(np);
  268. if (err)
  269. goto out_put_node;
  270. /*
  271. * Starting with v0.2, the PSCI specification introduced a call
  272. * (PSCI_VERSION) that allows probing the firmware version, so
  273. * that PSCI function IDs and version specific initialization
  274. * can be carried out according to the specific version reported
  275. * by firmware
  276. */
  277. err = psci_probe();
  278. out_put_node:
  279. of_node_put(np);
  280. return err;
  281. }
  282. /*
  283. * PSCI < v0.2 get PSCI Function IDs via DT.
  284. */
  285. static int __init psci_0_1_init(struct device_node *np)
  286. {
  287. u32 id;
  288. int err;
  289. err = get_set_conduit_method(np);
  290. if (err)
  291. goto out_put_node;
  292. pr_info("Using PSCI v0.1 Function IDs from DT\n");
  293. if (!of_property_read_u32(np, "cpu_suspend", &id)) {
  294. psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
  295. psci_ops.cpu_suspend = psci_cpu_suspend;
  296. }
  297. if (!of_property_read_u32(np, "cpu_off", &id)) {
  298. psci_function_id[PSCI_FN_CPU_OFF] = id;
  299. psci_ops.cpu_off = psci_cpu_off;
  300. }
  301. if (!of_property_read_u32(np, "cpu_on", &id)) {
  302. psci_function_id[PSCI_FN_CPU_ON] = id;
  303. psci_ops.cpu_on = psci_cpu_on;
  304. }
  305. if (!of_property_read_u32(np, "migrate", &id)) {
  306. psci_function_id[PSCI_FN_MIGRATE] = id;
  307. psci_ops.migrate = psci_migrate;
  308. }
  309. out_put_node:
  310. of_node_put(np);
  311. return err;
  312. }
  313. static const struct of_device_id const psci_of_match[] __initconst = {
  314. { .compatible = "arm,psci", .data = psci_0_1_init},
  315. { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
  316. { .compatible = "arm,psci-1.0", .data = psci_0_2_init},
  317. {},
  318. };
  319. int __init psci_dt_init(void)
  320. {
  321. struct device_node *np;
  322. const struct of_device_id *matched_np;
  323. psci_initcall_t init_fn;
  324. np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
  325. if (!np)
  326. return -ENODEV;
  327. init_fn = (psci_initcall_t)matched_np->data;
  328. return init_fn(np);
  329. }
  330. #ifdef CONFIG_ACPI
  331. /*
  332. * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's
  333. * explicitly clarified in SBBR
  334. */
  335. int __init psci_acpi_init(void)
  336. {
  337. if (!acpi_psci_present()) {
  338. pr_info("is not implemented in ACPI.\n");
  339. return -EOPNOTSUPP;
  340. }
  341. pr_info("probing for conduit method from ACPI.\n");
  342. if (acpi_psci_use_hvc())
  343. invoke_psci_fn = __invoke_psci_fn_hvc;
  344. else
  345. invoke_psci_fn = __invoke_psci_fn_smc;
  346. return psci_probe();
  347. }
  348. #endif