psci.c 11 KB

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