psci.c 13 KB

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