psci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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/errno.h>
  11. #include <linux/linkage.h>
  12. #include <linux/of.h>
  13. #include <linux/pm.h>
  14. #include <linux/printk.h>
  15. #include <linux/psci.h>
  16. #include <linux/reboot.h>
  17. #include <linux/slab.h>
  18. #include <linux/suspend.h>
  19. #include <uapi/linux/psci.h>
  20. #include <asm/cpuidle.h>
  21. #include <asm/cputype.h>
  22. #include <asm/system_misc.h>
  23. #include <asm/smp_plat.h>
  24. #include <asm/suspend.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. struct psci_operations psci_ops;
  44. static enum arm_smccc_conduit psci_conduit = SMCCC_CONDUIT_NONE;
  45. bool psci_tos_resident_on(int cpu)
  46. {
  47. return cpu == resident_cpu;
  48. }
  49. typedef unsigned long (psci_fn)(unsigned long, unsigned long,
  50. unsigned long, unsigned long);
  51. static psci_fn *invoke_psci_fn;
  52. static struct psci_0_1_function_ids psci_0_1_function_ids;
  53. struct psci_0_1_function_ids get_psci_0_1_function_ids(void)
  54. {
  55. return psci_0_1_function_ids;
  56. }
  57. #define PSCI_0_2_POWER_STATE_MASK \
  58. (PSCI_0_2_POWER_STATE_ID_MASK | \
  59. PSCI_0_2_POWER_STATE_TYPE_MASK | \
  60. PSCI_0_2_POWER_STATE_AFFL_MASK)
  61. #define PSCI_1_0_EXT_POWER_STATE_MASK \
  62. (PSCI_1_0_EXT_POWER_STATE_ID_MASK | \
  63. PSCI_1_0_EXT_POWER_STATE_TYPE_MASK)
  64. static u32 psci_cpu_suspend_feature;
  65. static bool psci_system_reset2_supported;
  66. static inline bool psci_has_ext_power_state(void)
  67. {
  68. return psci_cpu_suspend_feature &
  69. PSCI_1_0_FEATURES_CPU_SUSPEND_PF_MASK;
  70. }
  71. bool psci_has_osi_support(void)
  72. {
  73. return psci_cpu_suspend_feature & PSCI_1_0_OS_INITIATED;
  74. }
  75. static inline 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_0_1_get_version(void)
  121. {
  122. return PSCI_VERSION(0, 1);
  123. }
  124. static u32 psci_0_2_get_version(void)
  125. {
  126. return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
  127. }
  128. int psci_set_osi_mode(bool enable)
  129. {
  130. unsigned long suspend_mode;
  131. int err;
  132. suspend_mode = enable ? PSCI_1_0_SUSPEND_MODE_OSI :
  133. PSCI_1_0_SUSPEND_MODE_PC;
  134. err = invoke_psci_fn(PSCI_1_0_FN_SET_SUSPEND_MODE, suspend_mode, 0, 0);
  135. return psci_to_linux_errno(err);
  136. }
  137. static int __psci_cpu_suspend(u32 fn, u32 state, unsigned long entry_point)
  138. {
  139. int err;
  140. err = invoke_psci_fn(fn, state, entry_point, 0);
  141. return psci_to_linux_errno(err);
  142. }
  143. static int psci_0_1_cpu_suspend(u32 state, unsigned long entry_point)
  144. {
  145. return __psci_cpu_suspend(psci_0_1_function_ids.cpu_suspend,
  146. state, entry_point);
  147. }
  148. static int psci_0_2_cpu_suspend(u32 state, unsigned long entry_point)
  149. {
  150. return __psci_cpu_suspend(PSCI_FN_NATIVE(0_2, CPU_SUSPEND),
  151. state, entry_point);
  152. }
  153. static int __psci_cpu_off(u32 fn, u32 state)
  154. {
  155. int err;
  156. err = invoke_psci_fn(fn, state, 0, 0);
  157. return psci_to_linux_errno(err);
  158. }
  159. static int psci_0_1_cpu_off(u32 state)
  160. {
  161. return __psci_cpu_off(psci_0_1_function_ids.cpu_off, state);
  162. }
  163. static int psci_0_2_cpu_off(u32 state)
  164. {
  165. return __psci_cpu_off(PSCI_0_2_FN_CPU_OFF, state);
  166. }
  167. static int __psci_cpu_on(u32 fn, unsigned long cpuid, unsigned long entry_point)
  168. {
  169. int err;
  170. err = invoke_psci_fn(fn, cpuid, entry_point, 0);
  171. return psci_to_linux_errno(err);
  172. }
  173. static int psci_0_1_cpu_on(unsigned long cpuid, unsigned long entry_point)
  174. {
  175. return __psci_cpu_on(psci_0_1_function_ids.cpu_on, cpuid, entry_point);
  176. }
  177. static int psci_0_2_cpu_on(unsigned long cpuid, unsigned long entry_point)
  178. {
  179. return __psci_cpu_on(PSCI_FN_NATIVE(0_2, CPU_ON), cpuid, entry_point);
  180. }
  181. static int __psci_migrate(u32 fn, unsigned long cpuid)
  182. {
  183. int err;
  184. err = invoke_psci_fn(fn, cpuid, 0, 0);
  185. return psci_to_linux_errno(err);
  186. }
  187. static int psci_0_1_migrate(unsigned long cpuid)
  188. {
  189. return __psci_migrate(psci_0_1_function_ids.migrate, cpuid);
  190. }
  191. static int psci_0_2_migrate(unsigned long cpuid)
  192. {
  193. return __psci_migrate(PSCI_FN_NATIVE(0_2, MIGRATE), cpuid);
  194. }
  195. static int psci_affinity_info(unsigned long target_affinity,
  196. unsigned long lowest_affinity_level)
  197. {
  198. return invoke_psci_fn(PSCI_FN_NATIVE(0_2, AFFINITY_INFO),
  199. target_affinity, lowest_affinity_level, 0);
  200. }
  201. static int psci_migrate_info_type(void)
  202. {
  203. return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
  204. }
  205. static unsigned long psci_migrate_info_up_cpu(void)
  206. {
  207. return invoke_psci_fn(PSCI_FN_NATIVE(0_2, MIGRATE_INFO_UP_CPU),
  208. 0, 0, 0);
  209. }
  210. static void set_conduit(enum arm_smccc_conduit conduit)
  211. {
  212. switch (conduit) {
  213. case SMCCC_CONDUIT_HVC:
  214. invoke_psci_fn = __invoke_psci_fn_hvc;
  215. break;
  216. case SMCCC_CONDUIT_SMC:
  217. invoke_psci_fn = __invoke_psci_fn_smc;
  218. break;
  219. default:
  220. WARN(1, "Unexpected PSCI conduit %d\n", conduit);
  221. }
  222. psci_conduit = conduit;
  223. }
  224. static int get_set_conduit_method(struct device_node *np)
  225. {
  226. const char *method;
  227. pr_info("probing for conduit method from DT.\n");
  228. if (of_property_read_string(np, "method", &method)) {
  229. pr_warn("missing \"method\" property\n");
  230. return -ENXIO;
  231. }
  232. if (!strcmp("hvc", method)) {
  233. set_conduit(SMCCC_CONDUIT_HVC);
  234. } else if (!strcmp("smc", method)) {
  235. set_conduit(SMCCC_CONDUIT_SMC);
  236. } else {
  237. pr_warn("invalid \"method\" property: %s\n", method);
  238. return -EINVAL;
  239. }
  240. return 0;
  241. }
  242. static int psci_sys_reset(struct notifier_block *nb, unsigned long action,
  243. void *data)
  244. {
  245. if ((reboot_mode == REBOOT_WARM || reboot_mode == REBOOT_SOFT) &&
  246. psci_system_reset2_supported) {
  247. /*
  248. * reset_type[31] = 0 (architectural)
  249. * reset_type[30:0] = 0 (SYSTEM_WARM_RESET)
  250. * cookie = 0 (ignored by the implementation)
  251. */
  252. invoke_psci_fn(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2), 0, 0, 0);
  253. } else {
  254. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  255. }
  256. return NOTIFY_DONE;
  257. }
  258. static struct notifier_block psci_sys_reset_nb = {
  259. .notifier_call = psci_sys_reset,
  260. .priority = 129,
  261. };
  262. static void psci_sys_poweroff(void)
  263. {
  264. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  265. }
  266. static int __init psci_features(u32 psci_func_id)
  267. {
  268. return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES,
  269. psci_func_id, 0, 0);
  270. }
  271. #ifdef CONFIG_CPU_IDLE
  272. static int psci_suspend_finisher(unsigned long state)
  273. {
  274. u32 power_state = state;
  275. return psci_ops.cpu_suspend(power_state, __pa_function(cpu_resume));
  276. }
  277. int psci_cpu_suspend_enter(u32 state)
  278. {
  279. int ret;
  280. if (!psci_power_state_loses_context(state))
  281. ret = psci_ops.cpu_suspend(state, 0);
  282. else
  283. ret = cpu_suspend(state, psci_suspend_finisher);
  284. return ret;
  285. }
  286. #endif
  287. static int psci_system_suspend(unsigned long unused)
  288. {
  289. return invoke_psci_fn(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND),
  290. __pa_function(cpu_resume), 0, 0);
  291. }
  292. static int psci_system_suspend_enter(suspend_state_t state)
  293. {
  294. return cpu_suspend(0, psci_system_suspend);
  295. }
  296. static const struct platform_suspend_ops psci_suspend_ops = {
  297. .valid = suspend_valid_only_mem,
  298. .enter = psci_system_suspend_enter,
  299. };
  300. static void __init psci_init_system_reset2(void)
  301. {
  302. int ret;
  303. ret = psci_features(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2));
  304. if (ret != PSCI_RET_NOT_SUPPORTED)
  305. psci_system_reset2_supported = true;
  306. }
  307. static void __init psci_init_system_suspend(void)
  308. {
  309. int ret;
  310. if (!IS_ENABLED(CONFIG_SUSPEND))
  311. return;
  312. ret = psci_features(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND));
  313. if (ret != PSCI_RET_NOT_SUPPORTED)
  314. suspend_set_ops(&psci_suspend_ops);
  315. }
  316. static void __init psci_init_cpu_suspend(void)
  317. {
  318. int feature = psci_features(PSCI_FN_NATIVE(0_2, CPU_SUSPEND));
  319. if (feature != PSCI_RET_NOT_SUPPORTED)
  320. psci_cpu_suspend_feature = feature;
  321. }
  322. /*
  323. * Detect the presence of a resident Trusted OS which may cause CPU_OFF to
  324. * return DENIED (which would be fatal).
  325. */
  326. static void __init psci_init_migrate(void)
  327. {
  328. unsigned long cpuid;
  329. int type, cpu = -1;
  330. type = psci_ops.migrate_info_type();
  331. if (type == PSCI_0_2_TOS_MP) {
  332. pr_info("Trusted OS migration not required\n");
  333. return;
  334. }
  335. if (type == PSCI_RET_NOT_SUPPORTED) {
  336. pr_info("MIGRATE_INFO_TYPE not supported.\n");
  337. return;
  338. }
  339. if (type != PSCI_0_2_TOS_UP_MIGRATE &&
  340. type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
  341. pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
  342. return;
  343. }
  344. cpuid = psci_migrate_info_up_cpu();
  345. if (cpuid & ~MPIDR_HWID_BITMASK) {
  346. pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
  347. cpuid);
  348. return;
  349. }
  350. cpu = get_logical_index(cpuid);
  351. resident_cpu = cpu >= 0 ? cpu : -1;
  352. pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid);
  353. }
  354. static void __init psci_init_smccc(void)
  355. {
  356. u32 ver = ARM_SMCCC_VERSION_1_0;
  357. int feature;
  358. feature = psci_features(ARM_SMCCC_VERSION_FUNC_ID);
  359. if (feature != PSCI_RET_NOT_SUPPORTED) {
  360. u32 ret;
  361. ret = invoke_psci_fn(ARM_SMCCC_VERSION_FUNC_ID, 0, 0, 0);
  362. if (ret >= ARM_SMCCC_VERSION_1_1) {
  363. arm_smccc_version_init(ret, psci_conduit);
  364. ver = ret;
  365. }
  366. }
  367. /*
  368. * Conveniently, the SMCCC and PSCI versions are encoded the
  369. * same way. No, this isn't accidental.
  370. */
  371. pr_info("SMC Calling Convention v%d.%d\n",
  372. PSCI_VERSION_MAJOR(ver), PSCI_VERSION_MINOR(ver));
  373. }
  374. static void __init psci_0_2_set_functions(void)
  375. {
  376. pr_info("Using standard PSCI v0.2 function IDs\n");
  377. psci_ops = (struct psci_operations){
  378. .get_version = psci_0_2_get_version,
  379. .cpu_suspend = psci_0_2_cpu_suspend,
  380. .cpu_off = psci_0_2_cpu_off,
  381. .cpu_on = psci_0_2_cpu_on,
  382. .migrate = psci_0_2_migrate,
  383. .affinity_info = psci_affinity_info,
  384. .migrate_info_type = psci_migrate_info_type,
  385. };
  386. register_restart_handler(&psci_sys_reset_nb);
  387. pm_power_off = psci_sys_poweroff;
  388. }
  389. /*
  390. * Probe function for PSCI firmware versions >= 0.2
  391. */
  392. static int __init psci_probe(void)
  393. {
  394. u32 ver = psci_0_2_get_version();
  395. pr_info("PSCIv%d.%d detected in firmware.\n",
  396. PSCI_VERSION_MAJOR(ver),
  397. PSCI_VERSION_MINOR(ver));
  398. if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
  399. pr_err("Conflicting PSCI version detected.\n");
  400. return -EINVAL;
  401. }
  402. psci_0_2_set_functions();
  403. psci_init_migrate();
  404. if (PSCI_VERSION_MAJOR(ver) >= 1) {
  405. psci_init_smccc();
  406. psci_init_cpu_suspend();
  407. psci_init_system_suspend();
  408. psci_init_system_reset2();
  409. }
  410. return 0;
  411. }
  412. typedef int (*psci_initcall_t)(const struct device_node *);
  413. /*
  414. * PSCI init function for PSCI versions >=0.2
  415. *
  416. * Probe based on PSCI PSCI_VERSION function
  417. */
  418. static int __init psci_0_2_init(struct device_node *np)
  419. {
  420. int err;
  421. err = get_set_conduit_method(np);
  422. if (err)
  423. return err;
  424. /*
  425. * Starting with v0.2, the PSCI specification introduced a call
  426. * (PSCI_VERSION) that allows probing the firmware version, so
  427. * that PSCI function IDs and version specific initialization
  428. * can be carried out according to the specific version reported
  429. * by firmware
  430. */
  431. return psci_probe();
  432. }
  433. /*
  434. * PSCI < v0.2 get PSCI Function IDs via DT.
  435. */
  436. static int __init psci_0_1_init(struct device_node *np)
  437. {
  438. u32 id;
  439. int err;
  440. err = get_set_conduit_method(np);
  441. if (err)
  442. return err;
  443. pr_info("Using PSCI v0.1 Function IDs from DT\n");
  444. psci_ops.get_version = psci_0_1_get_version;
  445. if (!of_property_read_u32(np, "cpu_suspend", &id)) {
  446. psci_0_1_function_ids.cpu_suspend = id;
  447. psci_ops.cpu_suspend = psci_0_1_cpu_suspend;
  448. }
  449. if (!of_property_read_u32(np, "cpu_off", &id)) {
  450. psci_0_1_function_ids.cpu_off = id;
  451. psci_ops.cpu_off = psci_0_1_cpu_off;
  452. }
  453. if (!of_property_read_u32(np, "cpu_on", &id)) {
  454. psci_0_1_function_ids.cpu_on = id;
  455. psci_ops.cpu_on = psci_0_1_cpu_on;
  456. }
  457. if (!of_property_read_u32(np, "migrate", &id)) {
  458. psci_0_1_function_ids.migrate = id;
  459. psci_ops.migrate = psci_0_1_migrate;
  460. }
  461. return 0;
  462. }
  463. static int __init psci_1_0_init(struct device_node *np)
  464. {
  465. int err;
  466. err = psci_0_2_init(np);
  467. if (err)
  468. return err;
  469. if (psci_has_osi_support()) {
  470. pr_info("OSI mode supported.\n");
  471. /* Default to PC mode. */
  472. psci_set_osi_mode(false);
  473. }
  474. return 0;
  475. }
  476. static const struct of_device_id psci_of_match[] __initconst = {
  477. { .compatible = "arm,psci", .data = psci_0_1_init},
  478. { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
  479. { .compatible = "arm,psci-1.0", .data = psci_1_0_init},
  480. {},
  481. };
  482. int __init psci_dt_init(void)
  483. {
  484. struct device_node *np;
  485. const struct of_device_id *matched_np;
  486. psci_initcall_t init_fn;
  487. int ret;
  488. np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
  489. if (!np || !of_device_is_available(np))
  490. return -ENODEV;
  491. init_fn = (psci_initcall_t)matched_np->data;
  492. ret = init_fn(np);
  493. of_node_put(np);
  494. return ret;
  495. }
  496. #ifdef CONFIG_ACPI
  497. /*
  498. * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's
  499. * explicitly clarified in SBBR
  500. */
  501. int __init psci_acpi_init(void)
  502. {
  503. if (!acpi_psci_present()) {
  504. pr_info("is not implemented in ACPI.\n");
  505. return -EOPNOTSUPP;
  506. }
  507. pr_info("probing for conduit method from ACPI.\n");
  508. if (acpi_psci_use_hvc())
  509. set_conduit(SMCCC_CONDUIT_HVC);
  510. else
  511. set_conduit(SMCCC_CONDUIT_SMC);
  512. return psci_probe();
  513. }
  514. #endif