psci.c 11 KB

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