psci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2015 ARM Limited
  5. */
  6. #define pr_fmt(fmt) "psci: " fmt
  7. #include <linux/acpi.h>
  8. #include <linux/arm-smccc.h>
  9. #include <linux/cpuidle.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/errno.h>
  12. #include <linux/linkage.h>
  13. #include <linux/of.h>
  14. #include <linux/pm.h>
  15. #include <linux/printk.h>
  16. #include <linux/psci.h>
  17. #include <linux/reboot.h>
  18. #include <linux/slab.h>
  19. #include <linux/suspend.h>
  20. #include <uapi/linux/psci.h>
  21. #include <asm/cpuidle.h>
  22. #include <asm/cputype.h>
  23. #include <asm/hypervisor.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. struct psci_operations psci_ops;
  46. static enum arm_smccc_conduit psci_conduit = SMCCC_CONDUIT_NONE;
  47. bool psci_tos_resident_on(int cpu)
  48. {
  49. return cpu == resident_cpu;
  50. }
  51. typedef unsigned long (psci_fn)(unsigned long, unsigned long,
  52. unsigned long, unsigned long);
  53. static psci_fn *invoke_psci_fn;
  54. static struct psci_0_1_function_ids psci_0_1_function_ids;
  55. struct psci_0_1_function_ids get_psci_0_1_function_ids(void)
  56. {
  57. return psci_0_1_function_ids;
  58. }
  59. #define PSCI_0_2_POWER_STATE_MASK \
  60. (PSCI_0_2_POWER_STATE_ID_MASK | \
  61. PSCI_0_2_POWER_STATE_TYPE_MASK | \
  62. PSCI_0_2_POWER_STATE_AFFL_MASK)
  63. #define PSCI_1_0_EXT_POWER_STATE_MASK \
  64. (PSCI_1_0_EXT_POWER_STATE_ID_MASK | \
  65. PSCI_1_0_EXT_POWER_STATE_TYPE_MASK)
  66. static u32 psci_cpu_suspend_feature;
  67. static bool psci_system_reset2_supported;
  68. static bool psci_system_off2_hibernate_supported;
  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_has_osi_support(void)
  75. {
  76. return psci_cpu_suspend_feature & PSCI_1_0_OS_INITIATED;
  77. }
  78. static inline bool psci_power_state_loses_context(u32 state)
  79. {
  80. const u32 mask = psci_has_ext_power_state() ?
  81. PSCI_1_0_EXT_POWER_STATE_TYPE_MASK :
  82. PSCI_0_2_POWER_STATE_TYPE_MASK;
  83. return state & mask;
  84. }
  85. bool psci_power_state_is_valid(u32 state)
  86. {
  87. const u32 valid_mask = psci_has_ext_power_state() ?
  88. PSCI_1_0_EXT_POWER_STATE_MASK :
  89. PSCI_0_2_POWER_STATE_MASK;
  90. return !(state & ~valid_mask);
  91. }
  92. static __always_inline unsigned long
  93. __invoke_psci_fn_hvc(unsigned long function_id,
  94. unsigned long arg0, unsigned long arg1,
  95. unsigned long arg2)
  96. {
  97. struct arm_smccc_res res;
  98. arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
  99. return res.a0;
  100. }
  101. static __always_inline unsigned long
  102. __invoke_psci_fn_smc(unsigned long function_id,
  103. unsigned long arg0, unsigned long arg1,
  104. unsigned long arg2)
  105. {
  106. struct arm_smccc_res res;
  107. arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
  108. return res.a0;
  109. }
  110. static __always_inline int psci_to_linux_errno(int errno)
  111. {
  112. switch (errno) {
  113. case PSCI_RET_SUCCESS:
  114. return 0;
  115. case PSCI_RET_NOT_SUPPORTED:
  116. return -EOPNOTSUPP;
  117. case PSCI_RET_INVALID_PARAMS:
  118. case PSCI_RET_INVALID_ADDRESS:
  119. return -EINVAL;
  120. case PSCI_RET_DENIED:
  121. return -EPERM;
  122. }
  123. return -EINVAL;
  124. }
  125. static u32 psci_0_1_get_version(void)
  126. {
  127. return PSCI_VERSION(0, 1);
  128. }
  129. static u32 psci_0_2_get_version(void)
  130. {
  131. return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
  132. }
  133. int psci_set_osi_mode(bool enable)
  134. {
  135. unsigned long suspend_mode;
  136. int err;
  137. suspend_mode = enable ? PSCI_1_0_SUSPEND_MODE_OSI :
  138. PSCI_1_0_SUSPEND_MODE_PC;
  139. err = invoke_psci_fn(PSCI_1_0_FN_SET_SUSPEND_MODE, suspend_mode, 0, 0);
  140. if (err < 0)
  141. pr_info(FW_BUG "failed to set %s mode: %d\n",
  142. enable ? "OSI" : "PC", err);
  143. return psci_to_linux_errno(err);
  144. }
  145. static __always_inline int
  146. __psci_cpu_suspend(u32 fn, u32 state, unsigned long entry_point)
  147. {
  148. int err;
  149. err = invoke_psci_fn(fn, state, entry_point, 0);
  150. return psci_to_linux_errno(err);
  151. }
  152. static __always_inline int
  153. psci_0_1_cpu_suspend(u32 state, unsigned long entry_point)
  154. {
  155. return __psci_cpu_suspend(psci_0_1_function_ids.cpu_suspend,
  156. state, entry_point);
  157. }
  158. static __always_inline int
  159. psci_0_2_cpu_suspend(u32 state, unsigned long entry_point)
  160. {
  161. return __psci_cpu_suspend(PSCI_FN_NATIVE(0_2, CPU_SUSPEND),
  162. state, entry_point);
  163. }
  164. static int __psci_cpu_off(u32 fn, u32 state)
  165. {
  166. int err;
  167. err = invoke_psci_fn(fn, state, 0, 0);
  168. return psci_to_linux_errno(err);
  169. }
  170. static int psci_0_1_cpu_off(u32 state)
  171. {
  172. return __psci_cpu_off(psci_0_1_function_ids.cpu_off, state);
  173. }
  174. static int psci_0_2_cpu_off(u32 state)
  175. {
  176. return __psci_cpu_off(PSCI_0_2_FN_CPU_OFF, state);
  177. }
  178. static int __psci_cpu_on(u32 fn, unsigned long cpuid, unsigned long entry_point)
  179. {
  180. int err;
  181. err = invoke_psci_fn(fn, cpuid, entry_point, 0);
  182. return psci_to_linux_errno(err);
  183. }
  184. static int psci_0_1_cpu_on(unsigned long cpuid, unsigned long entry_point)
  185. {
  186. return __psci_cpu_on(psci_0_1_function_ids.cpu_on, cpuid, entry_point);
  187. }
  188. static int psci_0_2_cpu_on(unsigned long cpuid, unsigned long entry_point)
  189. {
  190. return __psci_cpu_on(PSCI_FN_NATIVE(0_2, CPU_ON), cpuid, entry_point);
  191. }
  192. static int __psci_migrate(u32 fn, unsigned long cpuid)
  193. {
  194. int err;
  195. err = invoke_psci_fn(fn, cpuid, 0, 0);
  196. return psci_to_linux_errno(err);
  197. }
  198. static int psci_0_1_migrate(unsigned long cpuid)
  199. {
  200. return __psci_migrate(psci_0_1_function_ids.migrate, cpuid);
  201. }
  202. static int psci_0_2_migrate(unsigned long cpuid)
  203. {
  204. return __psci_migrate(PSCI_FN_NATIVE(0_2, MIGRATE), cpuid);
  205. }
  206. static int psci_affinity_info(unsigned long target_affinity,
  207. unsigned long lowest_affinity_level)
  208. {
  209. return invoke_psci_fn(PSCI_FN_NATIVE(0_2, AFFINITY_INFO),
  210. target_affinity, lowest_affinity_level, 0);
  211. }
  212. static int psci_migrate_info_type(void)
  213. {
  214. return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
  215. }
  216. static unsigned long psci_migrate_info_up_cpu(void)
  217. {
  218. return invoke_psci_fn(PSCI_FN_NATIVE(0_2, MIGRATE_INFO_UP_CPU),
  219. 0, 0, 0);
  220. }
  221. static void set_conduit(enum arm_smccc_conduit conduit)
  222. {
  223. switch (conduit) {
  224. case SMCCC_CONDUIT_HVC:
  225. invoke_psci_fn = __invoke_psci_fn_hvc;
  226. break;
  227. case SMCCC_CONDUIT_SMC:
  228. invoke_psci_fn = __invoke_psci_fn_smc;
  229. break;
  230. default:
  231. WARN(1, "Unexpected PSCI conduit %d\n", conduit);
  232. }
  233. psci_conduit = conduit;
  234. }
  235. static int get_set_conduit_method(const struct device_node *np)
  236. {
  237. const char *method;
  238. pr_info("probing for conduit method from DT.\n");
  239. if (of_property_read_string(np, "method", &method)) {
  240. pr_warn("missing \"method\" property\n");
  241. return -ENXIO;
  242. }
  243. if (!strcmp("hvc", method)) {
  244. set_conduit(SMCCC_CONDUIT_HVC);
  245. } else if (!strcmp("smc", method)) {
  246. set_conduit(SMCCC_CONDUIT_SMC);
  247. } else {
  248. pr_warn("invalid \"method\" property: %s\n", method);
  249. return -EINVAL;
  250. }
  251. return 0;
  252. }
  253. static int psci_sys_reset(struct notifier_block *nb, unsigned long action,
  254. void *data)
  255. {
  256. if ((reboot_mode == REBOOT_WARM || reboot_mode == REBOOT_SOFT) &&
  257. psci_system_reset2_supported) {
  258. /*
  259. * reset_type[31] = 0 (architectural)
  260. * reset_type[30:0] = 0 (SYSTEM_WARM_RESET)
  261. * cookie = 0 (ignored by the implementation)
  262. */
  263. invoke_psci_fn(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2), 0, 0, 0);
  264. } else {
  265. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  266. }
  267. return NOTIFY_DONE;
  268. }
  269. static struct notifier_block psci_sys_reset_nb = {
  270. .notifier_call = psci_sys_reset,
  271. .priority = 129,
  272. };
  273. static void psci_sys_poweroff(void)
  274. {
  275. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  276. }
  277. #ifdef CONFIG_HIBERNATION
  278. static int psci_sys_hibernate(struct sys_off_data *data)
  279. {
  280. /*
  281. * If no hibernate type is specified SYSTEM_OFF2 defaults to selecting
  282. * HIBERNATE_OFF.
  283. *
  284. * There are hypervisors in the wild that do not align with the spec and
  285. * reject calls that explicitly provide a hibernate type. For
  286. * compatibility with these nonstandard implementations, pass 0 as the
  287. * type.
  288. */
  289. if (system_entering_hibernation())
  290. invoke_psci_fn(PSCI_FN_NATIVE(1_3, SYSTEM_OFF2), 0, 0, 0);
  291. return NOTIFY_DONE;
  292. }
  293. static int __init psci_hibernate_init(void)
  294. {
  295. if (psci_system_off2_hibernate_supported) {
  296. /* Higher priority than EFI shutdown, but only for hibernate */
  297. register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
  298. SYS_OFF_PRIO_FIRMWARE + 2,
  299. psci_sys_hibernate, NULL);
  300. }
  301. return 0;
  302. }
  303. subsys_initcall(psci_hibernate_init);
  304. #endif
  305. static int psci_features(u32 psci_func_id)
  306. {
  307. return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES,
  308. psci_func_id, 0, 0);
  309. }
  310. #ifdef CONFIG_DEBUG_FS
  311. #define PSCI_ID(ver, _name) \
  312. { .fn = PSCI_##ver##_FN_##_name, .name = #_name, }
  313. #define PSCI_ID_NATIVE(ver, _name) \
  314. { .fn = PSCI_FN_NATIVE(ver, _name), .name = #_name, }
  315. /* A table of all optional functions */
  316. static const struct {
  317. u32 fn;
  318. const char *name;
  319. } psci_fn_ids[] = {
  320. PSCI_ID_NATIVE(0_2, MIGRATE),
  321. PSCI_ID(0_2, MIGRATE_INFO_TYPE),
  322. PSCI_ID_NATIVE(0_2, MIGRATE_INFO_UP_CPU),
  323. PSCI_ID(1_0, CPU_FREEZE),
  324. PSCI_ID_NATIVE(1_0, CPU_DEFAULT_SUSPEND),
  325. PSCI_ID_NATIVE(1_0, NODE_HW_STATE),
  326. PSCI_ID_NATIVE(1_0, SYSTEM_SUSPEND),
  327. PSCI_ID(1_0, SET_SUSPEND_MODE),
  328. PSCI_ID_NATIVE(1_0, STAT_RESIDENCY),
  329. PSCI_ID_NATIVE(1_0, STAT_COUNT),
  330. PSCI_ID_NATIVE(1_1, SYSTEM_RESET2),
  331. PSCI_ID(1_1, MEM_PROTECT),
  332. PSCI_ID_NATIVE(1_1, MEM_PROTECT_CHECK_RANGE),
  333. PSCI_ID_NATIVE(1_3, SYSTEM_OFF2),
  334. };
  335. static int psci_debugfs_read(struct seq_file *s, void *data)
  336. {
  337. int feature, type, i;
  338. u32 ver;
  339. ver = psci_ops.get_version();
  340. seq_printf(s, "PSCIv%d.%d\n",
  341. PSCI_VERSION_MAJOR(ver),
  342. PSCI_VERSION_MINOR(ver));
  343. /* PSCI_FEATURES is available only starting from 1.0 */
  344. if (PSCI_VERSION_MAJOR(ver) < 1)
  345. return 0;
  346. feature = psci_features(ARM_SMCCC_VERSION_FUNC_ID);
  347. if (feature != PSCI_RET_NOT_SUPPORTED) {
  348. ver = invoke_psci_fn(ARM_SMCCC_VERSION_FUNC_ID, 0, 0, 0);
  349. seq_printf(s, "SMC Calling Convention v%d.%d\n",
  350. PSCI_VERSION_MAJOR(ver),
  351. PSCI_VERSION_MINOR(ver));
  352. } else {
  353. seq_puts(s, "SMC Calling Convention v1.0 is assumed\n");
  354. }
  355. feature = psci_features(PSCI_FN_NATIVE(0_2, CPU_SUSPEND));
  356. if (feature < 0) {
  357. seq_printf(s, "PSCI_FEATURES(CPU_SUSPEND) error (%d)\n", feature);
  358. } else {
  359. seq_printf(s, "OSI is %ssupported\n",
  360. (feature & BIT(0)) ? "" : "not ");
  361. seq_printf(s, "%s StateID format is used\n",
  362. (feature & BIT(1)) ? "Extended" : "Original");
  363. }
  364. type = psci_ops.migrate_info_type();
  365. if (type == PSCI_0_2_TOS_UP_MIGRATE ||
  366. type == PSCI_0_2_TOS_UP_NO_MIGRATE) {
  367. unsigned long cpuid;
  368. seq_printf(s, "Trusted OS %smigrate capable\n",
  369. type == PSCI_0_2_TOS_UP_NO_MIGRATE ? "not " : "");
  370. cpuid = psci_migrate_info_up_cpu();
  371. seq_printf(s, "Trusted OS resident on physical CPU 0x%lx (#%d)\n",
  372. cpuid, resident_cpu);
  373. } else if (type == PSCI_0_2_TOS_MP) {
  374. seq_puts(s, "Trusted OS migration not required\n");
  375. } else {
  376. if (type != PSCI_RET_NOT_SUPPORTED)
  377. seq_printf(s, "MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
  378. }
  379. for (i = 0; i < ARRAY_SIZE(psci_fn_ids); i++) {
  380. feature = psci_features(psci_fn_ids[i].fn);
  381. if (feature == PSCI_RET_NOT_SUPPORTED)
  382. continue;
  383. if (feature < 0)
  384. seq_printf(s, "PSCI_FEATURES(%s) error (%d)\n",
  385. psci_fn_ids[i].name, feature);
  386. else
  387. seq_printf(s, "%s is supported\n", psci_fn_ids[i].name);
  388. }
  389. return 0;
  390. }
  391. static int psci_debugfs_open(struct inode *inode, struct file *f)
  392. {
  393. return single_open(f, psci_debugfs_read, NULL);
  394. }
  395. static const struct file_operations psci_debugfs_ops = {
  396. .owner = THIS_MODULE,
  397. .open = psci_debugfs_open,
  398. .release = single_release,
  399. .read = seq_read,
  400. .llseek = seq_lseek
  401. };
  402. static int __init psci_debugfs_init(void)
  403. {
  404. if (!invoke_psci_fn || !psci_ops.get_version)
  405. return 0;
  406. return PTR_ERR_OR_ZERO(debugfs_create_file("psci", 0444, NULL, NULL,
  407. &psci_debugfs_ops));
  408. }
  409. late_initcall(psci_debugfs_init)
  410. #endif
  411. #ifdef CONFIG_CPU_IDLE
  412. static noinstr int psci_suspend_finisher(unsigned long state)
  413. {
  414. u32 power_state = state;
  415. phys_addr_t pa_cpu_resume;
  416. pa_cpu_resume = __pa_symbol_nodebug((unsigned long)cpu_resume);
  417. return psci_ops.cpu_suspend(power_state, pa_cpu_resume);
  418. }
  419. int psci_cpu_suspend_enter(u32 state)
  420. {
  421. int ret;
  422. if (!psci_power_state_loses_context(state)) {
  423. struct arm_cpuidle_irq_context context;
  424. ct_cpuidle_enter();
  425. arm_cpuidle_save_irq_context(&context);
  426. ret = psci_ops.cpu_suspend(state, 0);
  427. arm_cpuidle_restore_irq_context(&context);
  428. ct_cpuidle_exit();
  429. } else {
  430. /*
  431. * ARM64 cpu_suspend() wants to do ct_cpuidle_*() itself.
  432. */
  433. if (!IS_ENABLED(CONFIG_ARM64))
  434. ct_cpuidle_enter();
  435. ret = cpu_suspend(state, psci_suspend_finisher);
  436. if (!IS_ENABLED(CONFIG_ARM64))
  437. ct_cpuidle_exit();
  438. }
  439. return ret;
  440. }
  441. #endif
  442. static int psci_system_suspend(unsigned long unused)
  443. {
  444. int err;
  445. phys_addr_t pa_cpu_resume = __pa_symbol(cpu_resume);
  446. err = invoke_psci_fn(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND),
  447. pa_cpu_resume, 0, 0);
  448. return psci_to_linux_errno(err);
  449. }
  450. static int psci_system_suspend_enter(suspend_state_t state)
  451. {
  452. return cpu_suspend(0, psci_system_suspend);
  453. }
  454. static const struct platform_suspend_ops psci_suspend_ops = {
  455. .valid = suspend_valid_only_mem,
  456. .enter = psci_system_suspend_enter,
  457. };
  458. static void __init psci_init_system_reset2(void)
  459. {
  460. int ret;
  461. ret = psci_features(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2));
  462. if (ret != PSCI_RET_NOT_SUPPORTED)
  463. psci_system_reset2_supported = true;
  464. }
  465. static void __init psci_init_system_off2(void)
  466. {
  467. int ret;
  468. ret = psci_features(PSCI_FN_NATIVE(1_3, SYSTEM_OFF2));
  469. if (ret < 0)
  470. return;
  471. if (ret & PSCI_1_3_OFF_TYPE_HIBERNATE_OFF)
  472. psci_system_off2_hibernate_supported = true;
  473. }
  474. static void __init psci_init_system_suspend(void)
  475. {
  476. int ret;
  477. if (!IS_ENABLED(CONFIG_SUSPEND))
  478. return;
  479. ret = psci_features(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND));
  480. if (ret != PSCI_RET_NOT_SUPPORTED)
  481. suspend_set_ops(&psci_suspend_ops);
  482. }
  483. static void __init psci_init_cpu_suspend(void)
  484. {
  485. int feature = psci_features(PSCI_FN_NATIVE(0_2, CPU_SUSPEND));
  486. if (feature != PSCI_RET_NOT_SUPPORTED)
  487. psci_cpu_suspend_feature = feature;
  488. }
  489. /*
  490. * Detect the presence of a resident Trusted OS which may cause CPU_OFF to
  491. * return DENIED (which would be fatal).
  492. */
  493. static void __init psci_init_migrate(void)
  494. {
  495. unsigned long cpuid;
  496. int type, cpu = -1;
  497. type = psci_ops.migrate_info_type();
  498. if (type == PSCI_0_2_TOS_MP) {
  499. pr_info("Trusted OS migration not required\n");
  500. return;
  501. }
  502. if (type == PSCI_RET_NOT_SUPPORTED) {
  503. pr_info("MIGRATE_INFO_TYPE not supported.\n");
  504. return;
  505. }
  506. if (type != PSCI_0_2_TOS_UP_MIGRATE &&
  507. type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
  508. pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
  509. return;
  510. }
  511. cpuid = psci_migrate_info_up_cpu();
  512. if (cpuid & ~MPIDR_HWID_BITMASK) {
  513. pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
  514. cpuid);
  515. return;
  516. }
  517. cpu = get_logical_index(cpuid);
  518. resident_cpu = cpu >= 0 ? cpu : -1;
  519. pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid);
  520. }
  521. static void __init psci_init_smccc(void)
  522. {
  523. u32 ver = ARM_SMCCC_VERSION_1_0;
  524. int feature;
  525. feature = psci_features(ARM_SMCCC_VERSION_FUNC_ID);
  526. if (feature != PSCI_RET_NOT_SUPPORTED) {
  527. u32 ret;
  528. ret = invoke_psci_fn(ARM_SMCCC_VERSION_FUNC_ID, 0, 0, 0);
  529. if (ret >= ARM_SMCCC_VERSION_1_1) {
  530. arm_smccc_version_init(ret, psci_conduit);
  531. ver = ret;
  532. }
  533. }
  534. /*
  535. * Conveniently, the SMCCC and PSCI versions are encoded the
  536. * same way. No, this isn't accidental.
  537. */
  538. pr_info("SMC Calling Convention v%d.%d\n",
  539. PSCI_VERSION_MAJOR(ver), PSCI_VERSION_MINOR(ver));
  540. }
  541. static void __init psci_0_2_set_functions(void)
  542. {
  543. pr_info("Using standard PSCI v0.2 function IDs\n");
  544. psci_ops = (struct psci_operations){
  545. .get_version = psci_0_2_get_version,
  546. .cpu_suspend = psci_0_2_cpu_suspend,
  547. .cpu_off = psci_0_2_cpu_off,
  548. .cpu_on = psci_0_2_cpu_on,
  549. .migrate = psci_0_2_migrate,
  550. .affinity_info = psci_affinity_info,
  551. .migrate_info_type = psci_migrate_info_type,
  552. };
  553. register_restart_handler(&psci_sys_reset_nb);
  554. pm_power_off = psci_sys_poweroff;
  555. }
  556. /*
  557. * Probe function for PSCI firmware versions >= 0.2
  558. */
  559. static int __init psci_probe(void)
  560. {
  561. u32 ver = psci_0_2_get_version();
  562. pr_info("PSCIv%d.%d detected in firmware.\n",
  563. PSCI_VERSION_MAJOR(ver),
  564. PSCI_VERSION_MINOR(ver));
  565. if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
  566. pr_err("Conflicting PSCI version detected.\n");
  567. return -EINVAL;
  568. }
  569. psci_0_2_set_functions();
  570. psci_init_migrate();
  571. if (PSCI_VERSION_MAJOR(ver) >= 1) {
  572. psci_init_smccc();
  573. psci_init_cpu_suspend();
  574. psci_init_system_suspend();
  575. psci_init_system_reset2();
  576. psci_init_system_off2();
  577. kvm_init_hyp_services();
  578. }
  579. return 0;
  580. }
  581. typedef int (*psci_initcall_t)(const struct device_node *);
  582. /*
  583. * PSCI init function for PSCI versions >=0.2
  584. *
  585. * Probe based on PSCI PSCI_VERSION function
  586. */
  587. static int __init psci_0_2_init(const struct device_node *np)
  588. {
  589. int err;
  590. err = get_set_conduit_method(np);
  591. if (err)
  592. return err;
  593. /*
  594. * Starting with v0.2, the PSCI specification introduced a call
  595. * (PSCI_VERSION) that allows probing the firmware version, so
  596. * that PSCI function IDs and version specific initialization
  597. * can be carried out according to the specific version reported
  598. * by firmware
  599. */
  600. return psci_probe();
  601. }
  602. /*
  603. * PSCI < v0.2 get PSCI Function IDs via DT.
  604. */
  605. static int __init psci_0_1_init(const struct device_node *np)
  606. {
  607. u32 id;
  608. int err;
  609. err = get_set_conduit_method(np);
  610. if (err)
  611. return err;
  612. pr_info("Using PSCI v0.1 Function IDs from DT\n");
  613. psci_ops.get_version = psci_0_1_get_version;
  614. if (!of_property_read_u32(np, "cpu_suspend", &id)) {
  615. psci_0_1_function_ids.cpu_suspend = id;
  616. psci_ops.cpu_suspend = psci_0_1_cpu_suspend;
  617. }
  618. if (!of_property_read_u32(np, "cpu_off", &id)) {
  619. psci_0_1_function_ids.cpu_off = id;
  620. psci_ops.cpu_off = psci_0_1_cpu_off;
  621. }
  622. if (!of_property_read_u32(np, "cpu_on", &id)) {
  623. psci_0_1_function_ids.cpu_on = id;
  624. psci_ops.cpu_on = psci_0_1_cpu_on;
  625. }
  626. if (!of_property_read_u32(np, "migrate", &id)) {
  627. psci_0_1_function_ids.migrate = id;
  628. psci_ops.migrate = psci_0_1_migrate;
  629. }
  630. return 0;
  631. }
  632. static int __init psci_1_0_init(const struct device_node *np)
  633. {
  634. int err;
  635. err = psci_0_2_init(np);
  636. if (err)
  637. return err;
  638. if (psci_has_osi_support()) {
  639. pr_info("OSI mode supported.\n");
  640. /* Default to PC mode. */
  641. psci_set_osi_mode(false);
  642. }
  643. return 0;
  644. }
  645. static const struct of_device_id psci_of_match[] __initconst = {
  646. { .compatible = "arm,psci", .data = psci_0_1_init},
  647. { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
  648. { .compatible = "arm,psci-1.0", .data = psci_1_0_init},
  649. {},
  650. };
  651. int __init psci_dt_init(void)
  652. {
  653. struct device_node *np;
  654. const struct of_device_id *matched_np;
  655. psci_initcall_t init_fn;
  656. int ret;
  657. np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
  658. if (!np || !of_device_is_available(np)) {
  659. of_node_put(np);
  660. return -ENODEV;
  661. }
  662. init_fn = (psci_initcall_t)matched_np->data;
  663. ret = init_fn(np);
  664. of_node_put(np);
  665. return ret;
  666. }
  667. #ifdef CONFIG_ACPI
  668. /*
  669. * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's
  670. * explicitly clarified in SBBR
  671. */
  672. int __init psci_acpi_init(void)
  673. {
  674. if (!acpi_psci_present()) {
  675. pr_info("is not implemented in ACPI.\n");
  676. return -EOPNOTSUPP;
  677. }
  678. pr_info("probing for conduit method from ACPI.\n");
  679. if (acpi_psci_use_hvc())
  680. set_conduit(SMCCC_CONDUIT_HVC);
  681. else
  682. set_conduit(SMCCC_CONDUIT_SMC);
  683. return psci_probe();
  684. }
  685. #endif