psci.c 13 KB

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