psci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. #include <mtk_hibernate_core.h>
  32. #ifdef MTK_IRQ_NEW_DESIGN
  33. #include <linux/irqchip/mtk-gic-extend.h>
  34. #endif
  35. #define PSCI_POWER_STATE_TYPE_STANDBY 0
  36. #define PSCI_POWER_STATE_TYPE_POWER_DOWN 1
  37. struct psci_power_state {
  38. u16 id;
  39. u8 type;
  40. u8 affinity_level;
  41. };
  42. struct psci_operations {
  43. int (*cpu_suspend)(struct psci_power_state state,
  44. unsigned long entry_point);
  45. int (*cpu_off)(struct psci_power_state state);
  46. int (*cpu_on)(unsigned long cpuid, unsigned long entry_point);
  47. int (*migrate)(unsigned long cpuid);
  48. int (*affinity_info)(unsigned long target_affinity,
  49. unsigned long lowest_affinity_level);
  50. int (*migrate_info_type)(void);
  51. };
  52. static struct psci_operations psci_ops;
  53. static int (*invoke_psci_fn)(u64, u64, u64, u64);
  54. typedef int (*psci_initcall_t)(const struct device_node *);
  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. /*
  101. * The following two functions are invoked via the invoke_psci_fn pointer
  102. * and will not be inlined, allowing us to piggyback on the AAPCS.
  103. */
  104. static noinline int __invoke_psci_fn_hvc(u64 function_id, u64 arg0, u64 arg1,
  105. u64 arg2)
  106. {
  107. asm volatile(
  108. __asmeq("%0", "x0")
  109. __asmeq("%1", "x1")
  110. __asmeq("%2", "x2")
  111. __asmeq("%3", "x3")
  112. "hvc #0\n"
  113. : "+r" (function_id)
  114. : "r" (arg0), "r" (arg1), "r" (arg2));
  115. return function_id;
  116. }
  117. static noinline int __invoke_psci_fn_smc(u64 function_id, u64 arg0, u64 arg1,
  118. u64 arg2)
  119. {
  120. asm volatile(
  121. __asmeq("%0", "x0")
  122. __asmeq("%1", "x1")
  123. __asmeq("%2", "x2")
  124. __asmeq("%3", "x3")
  125. "smc #0\n"
  126. : "+r" (function_id)
  127. : "r" (arg0), "r" (arg1), "r" (arg2));
  128. return function_id;
  129. }
  130. static int psci_get_version(void)
  131. {
  132. int err;
  133. err = invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
  134. return err;
  135. }
  136. static int psci_cpu_suspend(struct psci_power_state state,
  137. unsigned long entry_point)
  138. {
  139. int err;
  140. u32 fn, power_state;
  141. fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
  142. power_state = psci_power_state_pack(state);
  143. err = invoke_psci_fn(fn, power_state, entry_point, 0);
  144. return psci_to_linux_errno(err);
  145. }
  146. static int psci_cpu_off(struct psci_power_state state)
  147. {
  148. int err;
  149. u32 fn, power_state;
  150. fn = psci_function_id[PSCI_FN_CPU_OFF];
  151. power_state = psci_power_state_pack(state);
  152. err = invoke_psci_fn(fn, power_state, 0, 0);
  153. return psci_to_linux_errno(err);
  154. }
  155. static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
  156. {
  157. int err;
  158. u32 fn;
  159. fn = psci_function_id[PSCI_FN_CPU_ON];
  160. err = invoke_psci_fn(fn, cpuid, entry_point, 0);
  161. return psci_to_linux_errno(err);
  162. }
  163. static int psci_migrate(unsigned long cpuid)
  164. {
  165. int err;
  166. u32 fn;
  167. fn = psci_function_id[PSCI_FN_MIGRATE];
  168. err = invoke_psci_fn(fn, cpuid, 0, 0);
  169. return psci_to_linux_errno(err);
  170. }
  171. static int psci_affinity_info(unsigned long target_affinity,
  172. unsigned long lowest_affinity_level)
  173. {
  174. int err;
  175. u32 fn;
  176. fn = psci_function_id[PSCI_FN_AFFINITY_INFO];
  177. err = invoke_psci_fn(fn, target_affinity, lowest_affinity_level, 0);
  178. return err;
  179. }
  180. static int psci_migrate_info_type(void)
  181. {
  182. int err;
  183. u32 fn;
  184. fn = psci_function_id[PSCI_FN_MIGRATE_INFO_TYPE];
  185. err = invoke_psci_fn(fn, 0, 0, 0);
  186. return err;
  187. }
  188. static int __maybe_unused cpu_psci_cpu_init_idle(struct device_node *cpu_node,
  189. unsigned int cpu)
  190. {
  191. int i, ret, count = 0;
  192. struct psci_power_state *psci_states;
  193. struct device_node *state_node;
  194. /*
  195. * If the PSCI cpu_suspend function hook has not been initialized
  196. * idle states must not be enabled, so bail out
  197. */
  198. if (!psci_ops.cpu_suspend)
  199. return -EOPNOTSUPP;
  200. /* Count idle states */
  201. while ((state_node = of_parse_phandle(cpu_node, "cpu-idle-states",
  202. count))) {
  203. count++;
  204. of_node_put(state_node);
  205. }
  206. if (!count)
  207. return -ENODEV;
  208. psci_states = kcalloc(count, sizeof(*psci_states), GFP_KERNEL);
  209. if (!psci_states)
  210. return -ENOMEM;
  211. for (i = 0; i < count; i++) {
  212. u32 psci_power_state;
  213. state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
  214. ret = of_property_read_u32(state_node,
  215. "arm,psci-suspend-param",
  216. &psci_power_state);
  217. if (ret) {
  218. pr_warn(" * %s missing arm,psci-suspend-param property\n",
  219. state_node->full_name);
  220. of_node_put(state_node);
  221. goto free_mem;
  222. }
  223. of_node_put(state_node);
  224. pr_debug("psci-power-state %#x index %d\n", psci_power_state,
  225. i);
  226. psci_power_state_unpack(psci_power_state, &psci_states[i]);
  227. }
  228. /* Idle states parsed correctly, initialize per-cpu pointer */
  229. per_cpu(psci_power_state, cpu) = psci_states;
  230. return 0;
  231. free_mem:
  232. kfree(psci_states);
  233. return ret;
  234. }
  235. static int get_set_conduit_method(struct device_node *np)
  236. {
  237. const char *method;
  238. pr_info("probing for conduit method from DT.\n");
  239. if (of_property_read_string(np, "method", &method)) {
  240. pr_warn("missing \"method\" property\n");
  241. return -ENXIO;
  242. }
  243. if (!strcmp("hvc", method)) {
  244. invoke_psci_fn = __invoke_psci_fn_hvc;
  245. } else if (!strcmp("smc", method)) {
  246. invoke_psci_fn = __invoke_psci_fn_smc;
  247. } else {
  248. pr_warn("invalid \"method\" property: %s\n", method);
  249. return -EINVAL;
  250. }
  251. return 0;
  252. }
  253. static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd)
  254. {
  255. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  256. }
  257. static void psci_sys_poweroff(void)
  258. {
  259. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  260. }
  261. /*
  262. * PSCI Function IDs for v0.2+ are well defined so use
  263. * standard values.
  264. */
  265. static int __init psci_0_2_init(struct device_node *np)
  266. {
  267. int err, ver;
  268. err = get_set_conduit_method(np);
  269. if (err)
  270. goto out_put_node;
  271. ver = psci_get_version();
  272. if (ver == PSCI_RET_NOT_SUPPORTED) {
  273. /* PSCI v0.2 mandates implementation of PSCI_ID_VERSION. */
  274. pr_err("PSCI firmware does not comply with the v0.2 spec.\n");
  275. err = -EOPNOTSUPP;
  276. goto out_put_node;
  277. } else {
  278. pr_info("PSCIv%d.%d detected in firmware.\n",
  279. PSCI_VERSION_MAJOR(ver),
  280. PSCI_VERSION_MINOR(ver));
  281. if (PSCI_VERSION_MAJOR(ver) == 0 &&
  282. PSCI_VERSION_MINOR(ver) < 2) {
  283. err = -EINVAL;
  284. pr_err("Conflicting PSCI version detected.\n");
  285. goto out_put_node;
  286. }
  287. }
  288. pr_info("Using standard PSCI v0.2 function IDs\n");
  289. psci_function_id[PSCI_FN_CPU_SUSPEND] = PSCI_0_2_FN64_CPU_SUSPEND;
  290. psci_ops.cpu_suspend = psci_cpu_suspend;
  291. psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF;
  292. psci_ops.cpu_off = psci_cpu_off;
  293. psci_function_id[PSCI_FN_CPU_ON] = PSCI_0_2_FN64_CPU_ON;
  294. psci_ops.cpu_on = psci_cpu_on;
  295. psci_function_id[PSCI_FN_MIGRATE] = PSCI_0_2_FN64_MIGRATE;
  296. psci_ops.migrate = psci_migrate;
  297. psci_function_id[PSCI_FN_AFFINITY_INFO] = PSCI_0_2_FN64_AFFINITY_INFO;
  298. psci_ops.affinity_info = psci_affinity_info;
  299. psci_function_id[PSCI_FN_MIGRATE_INFO_TYPE] =
  300. PSCI_0_2_FN_MIGRATE_INFO_TYPE;
  301. psci_ops.migrate_info_type = psci_migrate_info_type;
  302. arm_pm_restart = psci_sys_reset;
  303. pm_power_off = psci_sys_poweroff;
  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_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_SMP
  356. static int __init cpu_psci_cpu_init(struct device_node *dn, unsigned int cpu)
  357. {
  358. return 0;
  359. }
  360. static int __init cpu_psci_cpu_prepare(unsigned int cpu)
  361. {
  362. if (!psci_ops.cpu_on) {
  363. pr_err("no cpu_on method, not booting CPU%d\n", cpu);
  364. return -ENODEV;
  365. }
  366. return 0;
  367. }
  368. static int cpu_psci_cpu_boot(unsigned int cpu)
  369. {
  370. int err = psci_ops.cpu_on(cpu_logical_map(cpu), __pa(secondary_entry));
  371. if (err)
  372. pr_err("failed to boot CPU%d (%d)\n", cpu, err);
  373. #ifdef MTK_IRQ_NEW_DESIGN
  374. gic_clear_primask();
  375. #endif
  376. return err;
  377. }
  378. #ifdef CONFIG_HOTPLUG_CPU
  379. static int cpu_psci_cpu_disable(unsigned int cpu)
  380. {
  381. /* Fail early if we don't have CPU_OFF support */
  382. if (!psci_ops.cpu_off)
  383. return -EOPNOTSUPP;
  384. return 0;
  385. }
  386. static void cpu_psci_cpu_die(unsigned int cpu)
  387. {
  388. int ret;
  389. /*
  390. * There are no known implementations of PSCI actually using the
  391. * power state field, pass a sensible default for now.
  392. */
  393. struct psci_power_state state = {
  394. .type = PSCI_POWER_STATE_TYPE_POWER_DOWN,
  395. };
  396. #ifdef MTK_IRQ_NEW_DESIGN
  397. gic_set_primask();
  398. #endif
  399. ret = psci_ops.cpu_off(state);
  400. pr_crit("unable to power off CPU%u (%d)\n", cpu, ret);
  401. }
  402. static int cpu_psci_cpu_kill(unsigned int cpu)
  403. {
  404. int err, i;
  405. if (!psci_ops.affinity_info)
  406. return 1;
  407. /*
  408. * cpu_kill could race with cpu_die and we can
  409. * potentially end up declaring this cpu undead
  410. * while it is dying. So, try again a few times.
  411. */
  412. for (i = 0; i < 10; i++) {
  413. err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
  414. if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
  415. pr_info("CPU%d killed.\n", cpu);
  416. return 1;
  417. }
  418. msleep(10);
  419. pr_info("Retrying again to check for CPU kill\n");
  420. }
  421. pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
  422. cpu, err);
  423. /* Make op_cpu_kill() fail. */
  424. return 0;
  425. }
  426. #endif
  427. #endif
  428. static int psci_suspend_finisher(unsigned long index)
  429. {
  430. struct psci_power_state *state = __get_cpu_var(psci_power_state);
  431. #ifdef CONFIG_MTK_HIBERNATION
  432. if (index == POWERMODE_HIBERNATE) {
  433. int ret;
  434. pr_warn("%s: hibernating\n", __func__);
  435. ret = swsusp_arch_save_image(0);
  436. if (ret)
  437. pr_err("%s: swsusp_arch_save_image fail: %d", __func__, ret);
  438. return ret;
  439. }
  440. #endif
  441. return psci_ops.cpu_suspend(state[index - 1],
  442. virt_to_phys(cpu_resume));
  443. }
  444. static int __maybe_unused cpu_psci_cpu_suspend(unsigned long index)
  445. {
  446. int ret;
  447. struct psci_power_state *state = __get_cpu_var(psci_power_state);
  448. /*
  449. * idle state index 0 corresponds to wfi, should never be called
  450. * from the cpu_suspend operations
  451. */
  452. if (WARN_ON_ONCE(!index))
  453. return -EINVAL;
  454. #ifdef CONFIG_MTK_HIBERNATION
  455. if (index == POWERMODE_HIBERNATE)
  456. return __cpu_suspend(index, psci_suspend_finisher);
  457. #endif
  458. if (state[index - 1].type == PSCI_POWER_STATE_TYPE_STANDBY)
  459. ret = psci_ops.cpu_suspend(state[index - 1], 0);
  460. else
  461. ret = __cpu_suspend(index, psci_suspend_finisher);
  462. return ret;
  463. }
  464. const struct cpu_operations cpu_psci_ops = {
  465. .name = "psci",
  466. #ifdef CONFIG_CPU_IDLE
  467. .cpu_init_idle = cpu_psci_cpu_init_idle,
  468. .cpu_suspend = cpu_psci_cpu_suspend,
  469. #endif
  470. #ifdef CONFIG_SMP
  471. .cpu_init = cpu_psci_cpu_init,
  472. .cpu_prepare = cpu_psci_cpu_prepare,
  473. .cpu_boot = cpu_psci_cpu_boot,
  474. #ifdef CONFIG_HOTPLUG_CPU
  475. .cpu_disable = cpu_psci_cpu_disable,
  476. .cpu_die = cpu_psci_cpu_die,
  477. .cpu_kill = cpu_psci_cpu_kill,
  478. #endif
  479. #endif
  480. };