psci.c 14 KB

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