psci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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/acpi.h>
  15. #include <linux/arm-smccc.h>
  16. #include <linux/cpuidle.h>
  17. #include <linux/errno.h>
  18. #include <linux/linkage.h>
  19. #include <linux/of.h>
  20. #include <linux/pm.h>
  21. #include <linux/printk.h>
  22. #include <linux/psci.h>
  23. #include <linux/reboot.h>
  24. #include <linux/slab.h>
  25. #include <linux/suspend.h>
  26. #include <uapi/linux/psci.h>
  27. #include <asm/cpuidle.h>
  28. #include <asm/cputype.h>
  29. #include <asm/system_misc.h>
  30. #include <asm/smp_plat.h>
  31. #include <asm/suspend.h>
  32. /*
  33. * While a 64-bit OS can make calls with SMC32 calling conventions, for some
  34. * calls it is necessary to use SMC64 to pass or return 64-bit values.
  35. * For such calls PSCI_FN_NATIVE(version, name) will choose the appropriate
  36. * (native-width) function ID.
  37. */
  38. #ifdef CONFIG_64BIT
  39. #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN64_##name
  40. #else
  41. #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN_##name
  42. #endif
  43. /*
  44. * The CPU any Trusted OS is resident on. The trusted OS may reject CPU_OFF
  45. * calls to its resident CPU, so we must avoid issuing those. We never migrate
  46. * a Trusted OS even if it claims to be capable of migration -- doing so will
  47. * require cooperation with a Trusted OS driver.
  48. */
  49. static int resident_cpu = -1;
  50. bool psci_tos_resident_on(int cpu)
  51. {
  52. return cpu == resident_cpu;
  53. }
  54. struct psci_operations psci_ops;
  55. typedef unsigned long (psci_fn)(unsigned long, unsigned long,
  56. unsigned long, unsigned long);
  57. static psci_fn *invoke_psci_fn;
  58. enum psci_function {
  59. PSCI_FN_CPU_SUSPEND,
  60. PSCI_FN_CPU_ON,
  61. PSCI_FN_CPU_OFF,
  62. PSCI_FN_MIGRATE,
  63. PSCI_FN_MAX,
  64. };
  65. static u32 psci_function_id[PSCI_FN_MAX];
  66. #define PSCI_0_2_POWER_STATE_MASK \
  67. (PSCI_0_2_POWER_STATE_ID_MASK | \
  68. PSCI_0_2_POWER_STATE_TYPE_MASK | \
  69. PSCI_0_2_POWER_STATE_AFFL_MASK)
  70. #define PSCI_1_0_EXT_POWER_STATE_MASK \
  71. (PSCI_1_0_EXT_POWER_STATE_ID_MASK | \
  72. PSCI_1_0_EXT_POWER_STATE_TYPE_MASK)
  73. static u32 psci_cpu_suspend_feature;
  74. static inline bool psci_has_ext_power_state(void)
  75. {
  76. return psci_cpu_suspend_feature &
  77. PSCI_1_0_FEATURES_CPU_SUSPEND_PF_MASK;
  78. }
  79. static inline bool psci_power_state_loses_context(u32 state)
  80. {
  81. const u32 mask = psci_has_ext_power_state() ?
  82. PSCI_1_0_EXT_POWER_STATE_TYPE_MASK :
  83. PSCI_0_2_POWER_STATE_TYPE_MASK;
  84. return state & mask;
  85. }
  86. static inline bool psci_power_state_is_valid(u32 state)
  87. {
  88. const u32 valid_mask = psci_has_ext_power_state() ?
  89. PSCI_1_0_EXT_POWER_STATE_MASK :
  90. PSCI_0_2_POWER_STATE_MASK;
  91. return !(state & ~valid_mask);
  92. }
  93. static unsigned long __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 unsigned long __invoke_psci_fn_smc(unsigned long function_id,
  102. unsigned long arg0, unsigned long arg1,
  103. unsigned long arg2)
  104. {
  105. struct arm_smccc_res res;
  106. arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
  107. return res.a0;
  108. }
  109. static int psci_to_linux_errno(int errno)
  110. {
  111. switch (errno) {
  112. case PSCI_RET_SUCCESS:
  113. return 0;
  114. case PSCI_RET_NOT_SUPPORTED:
  115. return -EOPNOTSUPP;
  116. case PSCI_RET_INVALID_PARAMS:
  117. case PSCI_RET_INVALID_ADDRESS:
  118. return -EINVAL;
  119. case PSCI_RET_DENIED:
  120. return -EPERM;
  121. };
  122. return -EINVAL;
  123. }
  124. static u32 psci_get_version(void)
  125. {
  126. return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
  127. }
  128. static int psci_cpu_suspend(u32 state, unsigned long entry_point)
  129. {
  130. int err;
  131. u32 fn;
  132. fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
  133. err = invoke_psci_fn(fn, state, entry_point, 0);
  134. return psci_to_linux_errno(err);
  135. }
  136. static int psci_cpu_off(u32 state)
  137. {
  138. int err;
  139. u32 fn;
  140. fn = psci_function_id[PSCI_FN_CPU_OFF];
  141. err = invoke_psci_fn(fn, state, 0, 0);
  142. return psci_to_linux_errno(err);
  143. }
  144. static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
  145. {
  146. int err;
  147. u32 fn;
  148. fn = psci_function_id[PSCI_FN_CPU_ON];
  149. err = invoke_psci_fn(fn, cpuid, entry_point, 0);
  150. return psci_to_linux_errno(err);
  151. }
  152. static int psci_migrate(unsigned long cpuid)
  153. {
  154. int err;
  155. u32 fn;
  156. fn = psci_function_id[PSCI_FN_MIGRATE];
  157. err = invoke_psci_fn(fn, cpuid, 0, 0);
  158. return psci_to_linux_errno(err);
  159. }
  160. static int psci_affinity_info(unsigned long target_affinity,
  161. unsigned long lowest_affinity_level)
  162. {
  163. return invoke_psci_fn(PSCI_FN_NATIVE(0_2, AFFINITY_INFO),
  164. target_affinity, lowest_affinity_level, 0);
  165. }
  166. static int psci_migrate_info_type(void)
  167. {
  168. return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
  169. }
  170. static unsigned long psci_migrate_info_up_cpu(void)
  171. {
  172. return invoke_psci_fn(PSCI_FN_NATIVE(0_2, MIGRATE_INFO_UP_CPU),
  173. 0, 0, 0);
  174. }
  175. static int get_set_conduit_method(struct device_node *np)
  176. {
  177. const char *method;
  178. pr_info("probing for conduit method from DT.\n");
  179. if (of_property_read_string(np, "method", &method)) {
  180. pr_warn("missing \"method\" property\n");
  181. return -ENXIO;
  182. }
  183. if (!strcmp("hvc", method)) {
  184. invoke_psci_fn = __invoke_psci_fn_hvc;
  185. } else if (!strcmp("smc", method)) {
  186. invoke_psci_fn = __invoke_psci_fn_smc;
  187. } else {
  188. pr_warn("invalid \"method\" property: %s\n", method);
  189. return -EINVAL;
  190. }
  191. return 0;
  192. }
  193. static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd)
  194. {
  195. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  196. }
  197. static void psci_sys_poweroff(void)
  198. {
  199. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  200. }
  201. static int __init psci_features(u32 psci_func_id)
  202. {
  203. return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES,
  204. psci_func_id, 0, 0);
  205. }
  206. #ifdef CONFIG_CPU_IDLE
  207. static DEFINE_PER_CPU_READ_MOSTLY(u32 *, psci_power_state);
  208. static int psci_dt_cpu_init_idle(struct device_node *cpu_node, int cpu)
  209. {
  210. int i, ret, count = 0;
  211. u32 *psci_states;
  212. struct device_node *state_node;
  213. /* Count idle states */
  214. while ((state_node = of_parse_phandle(cpu_node, "cpu-idle-states",
  215. count))) {
  216. count++;
  217. of_node_put(state_node);
  218. }
  219. if (!count)
  220. return -ENODEV;
  221. psci_states = kcalloc(count, sizeof(*psci_states), GFP_KERNEL);
  222. if (!psci_states)
  223. return -ENOMEM;
  224. for (i = 0; i < count; i++) {
  225. u32 state;
  226. state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
  227. ret = of_property_read_u32(state_node,
  228. "arm,psci-suspend-param",
  229. &state);
  230. if (ret) {
  231. pr_warn(" * %s missing arm,psci-suspend-param property\n",
  232. state_node->full_name);
  233. of_node_put(state_node);
  234. goto free_mem;
  235. }
  236. of_node_put(state_node);
  237. pr_debug("psci-power-state %#x index %d\n", state, i);
  238. if (!psci_power_state_is_valid(state)) {
  239. pr_warn("Invalid PSCI power state %#x\n", state);
  240. ret = -EINVAL;
  241. goto free_mem;
  242. }
  243. psci_states[i] = state;
  244. }
  245. /* Idle states parsed correctly, initialize per-cpu pointer */
  246. per_cpu(psci_power_state, cpu) = psci_states;
  247. return 0;
  248. free_mem:
  249. kfree(psci_states);
  250. return ret;
  251. }
  252. #ifdef CONFIG_ACPI
  253. #include <acpi/processor.h>
  254. static int __maybe_unused psci_acpi_cpu_init_idle(unsigned int cpu)
  255. {
  256. int i, count;
  257. u32 *psci_states;
  258. struct acpi_lpi_state *lpi;
  259. struct acpi_processor *pr = per_cpu(processors, cpu);
  260. if (unlikely(!pr || !pr->flags.has_lpi))
  261. return -EINVAL;
  262. count = pr->power.count - 1;
  263. if (count <= 0)
  264. return -ENODEV;
  265. psci_states = kcalloc(count, sizeof(*psci_states), GFP_KERNEL);
  266. if (!psci_states)
  267. return -ENOMEM;
  268. for (i = 0; i < count; i++) {
  269. u32 state;
  270. lpi = &pr->power.lpi_states[i + 1];
  271. /*
  272. * Only bits[31:0] represent a PSCI power_state while
  273. * bits[63:32] must be 0x0 as per ARM ACPI FFH Specification
  274. */
  275. state = lpi->address;
  276. if (!psci_power_state_is_valid(state)) {
  277. pr_warn("Invalid PSCI power state %#x\n", state);
  278. kfree(psci_states);
  279. return -EINVAL;
  280. }
  281. psci_states[i] = state;
  282. }
  283. /* Idle states parsed correctly, initialize per-cpu pointer */
  284. per_cpu(psci_power_state, cpu) = psci_states;
  285. return 0;
  286. }
  287. #else
  288. static int __maybe_unused psci_acpi_cpu_init_idle(unsigned int cpu)
  289. {
  290. return -EINVAL;
  291. }
  292. #endif
  293. int psci_cpu_init_idle(unsigned int cpu)
  294. {
  295. struct device_node *cpu_node;
  296. int ret;
  297. /*
  298. * If the PSCI cpu_suspend function hook has not been initialized
  299. * idle states must not be enabled, so bail out
  300. */
  301. if (!psci_ops.cpu_suspend)
  302. return -EOPNOTSUPP;
  303. if (!acpi_disabled)
  304. return psci_acpi_cpu_init_idle(cpu);
  305. cpu_node = of_get_cpu_node(cpu, NULL);
  306. if (!cpu_node)
  307. return -ENODEV;
  308. ret = psci_dt_cpu_init_idle(cpu_node, cpu);
  309. of_node_put(cpu_node);
  310. return ret;
  311. }
  312. static int psci_suspend_finisher(unsigned long index)
  313. {
  314. u32 *state = __this_cpu_read(psci_power_state);
  315. return psci_ops.cpu_suspend(state[index - 1],
  316. virt_to_phys(cpu_resume));
  317. }
  318. int psci_cpu_suspend_enter(unsigned long index)
  319. {
  320. int ret;
  321. u32 *state = __this_cpu_read(psci_power_state);
  322. /*
  323. * idle state index 0 corresponds to wfi, should never be called
  324. * from the cpu_suspend operations
  325. */
  326. if (WARN_ON_ONCE(!index))
  327. return -EINVAL;
  328. if (!psci_power_state_loses_context(state[index - 1]))
  329. ret = psci_ops.cpu_suspend(state[index - 1], 0);
  330. else
  331. ret = cpu_suspend(index, psci_suspend_finisher);
  332. return ret;
  333. }
  334. /* ARM specific CPU idle operations */
  335. #ifdef CONFIG_ARM
  336. static const struct cpuidle_ops psci_cpuidle_ops __initconst = {
  337. .suspend = psci_cpu_suspend_enter,
  338. .init = psci_dt_cpu_init_idle,
  339. };
  340. CPUIDLE_METHOD_OF_DECLARE(psci, "psci", &psci_cpuidle_ops);
  341. #endif
  342. #endif
  343. static int psci_system_suspend(unsigned long unused)
  344. {
  345. return invoke_psci_fn(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND),
  346. virt_to_phys(cpu_resume), 0, 0);
  347. }
  348. static int psci_system_suspend_enter(suspend_state_t state)
  349. {
  350. return cpu_suspend(0, psci_system_suspend);
  351. }
  352. static const struct platform_suspend_ops psci_suspend_ops = {
  353. .valid = suspend_valid_only_mem,
  354. .enter = psci_system_suspend_enter,
  355. };
  356. static void __init psci_init_system_suspend(void)
  357. {
  358. int ret;
  359. if (!IS_ENABLED(CONFIG_SUSPEND))
  360. return;
  361. ret = psci_features(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND));
  362. if (ret != PSCI_RET_NOT_SUPPORTED)
  363. suspend_set_ops(&psci_suspend_ops);
  364. }
  365. static void __init psci_init_cpu_suspend(void)
  366. {
  367. int feature = psci_features(psci_function_id[PSCI_FN_CPU_SUSPEND]);
  368. if (feature != PSCI_RET_NOT_SUPPORTED)
  369. psci_cpu_suspend_feature = feature;
  370. }
  371. /*
  372. * Detect the presence of a resident Trusted OS which may cause CPU_OFF to
  373. * return DENIED (which would be fatal).
  374. */
  375. static void __init psci_init_migrate(void)
  376. {
  377. unsigned long cpuid;
  378. int type, cpu = -1;
  379. type = psci_ops.migrate_info_type();
  380. if (type == PSCI_0_2_TOS_MP) {
  381. pr_info("Trusted OS migration not required\n");
  382. return;
  383. }
  384. if (type == PSCI_RET_NOT_SUPPORTED) {
  385. pr_info("MIGRATE_INFO_TYPE not supported.\n");
  386. return;
  387. }
  388. if (type != PSCI_0_2_TOS_UP_MIGRATE &&
  389. type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
  390. pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
  391. return;
  392. }
  393. cpuid = psci_migrate_info_up_cpu();
  394. if (cpuid & ~MPIDR_HWID_BITMASK) {
  395. pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
  396. cpuid);
  397. return;
  398. }
  399. cpu = get_logical_index(cpuid);
  400. resident_cpu = cpu >= 0 ? cpu : -1;
  401. pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid);
  402. }
  403. static void __init psci_0_2_set_functions(void)
  404. {
  405. pr_info("Using standard PSCI v0.2 function IDs\n");
  406. psci_function_id[PSCI_FN_CPU_SUSPEND] =
  407. PSCI_FN_NATIVE(0_2, CPU_SUSPEND);
  408. psci_ops.cpu_suspend = psci_cpu_suspend;
  409. psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF;
  410. psci_ops.cpu_off = psci_cpu_off;
  411. psci_function_id[PSCI_FN_CPU_ON] = PSCI_FN_NATIVE(0_2, CPU_ON);
  412. psci_ops.cpu_on = psci_cpu_on;
  413. psci_function_id[PSCI_FN_MIGRATE] = PSCI_FN_NATIVE(0_2, MIGRATE);
  414. psci_ops.migrate = psci_migrate;
  415. psci_ops.affinity_info = psci_affinity_info;
  416. psci_ops.migrate_info_type = psci_migrate_info_type;
  417. arm_pm_restart = psci_sys_reset;
  418. pm_power_off = psci_sys_poweroff;
  419. }
  420. /*
  421. * Probe function for PSCI firmware versions >= 0.2
  422. */
  423. static int __init psci_probe(void)
  424. {
  425. u32 ver = psci_get_version();
  426. pr_info("PSCIv%d.%d detected in firmware.\n",
  427. PSCI_VERSION_MAJOR(ver),
  428. PSCI_VERSION_MINOR(ver));
  429. if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
  430. pr_err("Conflicting PSCI version detected.\n");
  431. return -EINVAL;
  432. }
  433. psci_0_2_set_functions();
  434. psci_init_migrate();
  435. if (PSCI_VERSION_MAJOR(ver) >= 1) {
  436. psci_init_cpu_suspend();
  437. psci_init_system_suspend();
  438. }
  439. return 0;
  440. }
  441. typedef int (*psci_initcall_t)(const struct device_node *);
  442. /*
  443. * PSCI init function for PSCI versions >=0.2
  444. *
  445. * Probe based on PSCI PSCI_VERSION function
  446. */
  447. static int __init psci_0_2_init(struct device_node *np)
  448. {
  449. int err;
  450. err = get_set_conduit_method(np);
  451. if (err)
  452. goto out_put_node;
  453. /*
  454. * Starting with v0.2, the PSCI specification introduced a call
  455. * (PSCI_VERSION) that allows probing the firmware version, so
  456. * that PSCI function IDs and version specific initialization
  457. * can be carried out according to the specific version reported
  458. * by firmware
  459. */
  460. err = psci_probe();
  461. out_put_node:
  462. of_node_put(np);
  463. return err;
  464. }
  465. /*
  466. * PSCI < v0.2 get PSCI Function IDs via DT.
  467. */
  468. static int __init psci_0_1_init(struct device_node *np)
  469. {
  470. u32 id;
  471. int err;
  472. err = get_set_conduit_method(np);
  473. if (err)
  474. goto out_put_node;
  475. pr_info("Using PSCI v0.1 Function IDs from DT\n");
  476. if (!of_property_read_u32(np, "cpu_suspend", &id)) {
  477. psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
  478. psci_ops.cpu_suspend = psci_cpu_suspend;
  479. }
  480. if (!of_property_read_u32(np, "cpu_off", &id)) {
  481. psci_function_id[PSCI_FN_CPU_OFF] = id;
  482. psci_ops.cpu_off = psci_cpu_off;
  483. }
  484. if (!of_property_read_u32(np, "cpu_on", &id)) {
  485. psci_function_id[PSCI_FN_CPU_ON] = id;
  486. psci_ops.cpu_on = psci_cpu_on;
  487. }
  488. if (!of_property_read_u32(np, "migrate", &id)) {
  489. psci_function_id[PSCI_FN_MIGRATE] = id;
  490. psci_ops.migrate = psci_migrate;
  491. }
  492. out_put_node:
  493. of_node_put(np);
  494. return err;
  495. }
  496. static const struct of_device_id psci_of_match[] __initconst = {
  497. { .compatible = "arm,psci", .data = psci_0_1_init},
  498. { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
  499. { .compatible = "arm,psci-1.0", .data = psci_0_2_init},
  500. {},
  501. };
  502. int __init psci_dt_init(void)
  503. {
  504. struct device_node *np;
  505. const struct of_device_id *matched_np;
  506. psci_initcall_t init_fn;
  507. np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
  508. if (!np || !of_device_is_available(np))
  509. return -ENODEV;
  510. init_fn = (psci_initcall_t)matched_np->data;
  511. return init_fn(np);
  512. }
  513. #ifdef CONFIG_ACPI
  514. /*
  515. * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's
  516. * explicitly clarified in SBBR
  517. */
  518. int __init psci_acpi_init(void)
  519. {
  520. if (!acpi_psci_present()) {
  521. pr_info("is not implemented in ACPI.\n");
  522. return -EOPNOTSUPP;
  523. }
  524. pr_info("probing for conduit method from ACPI.\n");
  525. if (acpi_psci_use_hvc())
  526. invoke_psci_fn = __invoke_psci_fn_hvc;
  527. else
  528. invoke_psci_fn = __invoke_psci_fn_smc;
  529. return psci_probe();
  530. }
  531. #endif