psci.c 13 KB

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