psci.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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/errno.h>
  15. #include <linux/linkage.h>
  16. #include <linux/of.h>
  17. #include <linux/pm.h>
  18. #include <linux/printk.h>
  19. #include <linux/psci.h>
  20. #include <linux/reboot.h>
  21. #include <uapi/linux/psci.h>
  22. #include <asm/cputype.h>
  23. #include <asm/system_misc.h>
  24. #include <asm/smp_plat.h>
  25. /*
  26. * While a 64-bit OS can make calls with SMC32 calling conventions, for some
  27. * calls it is necessary to use SMC64 to pass or return 64-bit values. For such
  28. * calls PSCI_0_2_FN_NATIVE(x) will choose the appropriate (native-width)
  29. * function ID.
  30. */
  31. #ifdef CONFIG_64BIT
  32. #define PSCI_0_2_FN_NATIVE(name) PSCI_0_2_FN64_##name
  33. #else
  34. #define PSCI_0_2_FN_NATIVE(name) PSCI_0_2_FN_##name
  35. #endif
  36. /*
  37. * The CPU any Trusted OS is resident on. The trusted OS may reject CPU_OFF
  38. * calls to its resident CPU, so we must avoid issuing those. We never migrate
  39. * a Trusted OS even if it claims to be capable of migration -- doing so will
  40. * require cooperation with a Trusted OS driver.
  41. */
  42. static int resident_cpu = -1;
  43. bool psci_tos_resident_on(int cpu)
  44. {
  45. return cpu == resident_cpu;
  46. }
  47. struct psci_operations psci_ops;
  48. typedef unsigned long (psci_fn)(unsigned long, unsigned long,
  49. unsigned long, unsigned long);
  50. asmlinkage psci_fn __invoke_psci_fn_hvc;
  51. asmlinkage psci_fn __invoke_psci_fn_smc;
  52. static psci_fn *invoke_psci_fn;
  53. enum psci_function {
  54. PSCI_FN_CPU_SUSPEND,
  55. PSCI_FN_CPU_ON,
  56. PSCI_FN_CPU_OFF,
  57. PSCI_FN_MIGRATE,
  58. PSCI_FN_MAX,
  59. };
  60. static u32 psci_function_id[PSCI_FN_MAX];
  61. static int psci_to_linux_errno(int errno)
  62. {
  63. switch (errno) {
  64. case PSCI_RET_SUCCESS:
  65. return 0;
  66. case PSCI_RET_NOT_SUPPORTED:
  67. return -EOPNOTSUPP;
  68. case PSCI_RET_INVALID_PARAMS:
  69. return -EINVAL;
  70. case PSCI_RET_DENIED:
  71. return -EPERM;
  72. };
  73. return -EINVAL;
  74. }
  75. static u32 psci_get_version(void)
  76. {
  77. return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
  78. }
  79. static int psci_cpu_suspend(u32 state, unsigned long entry_point)
  80. {
  81. int err;
  82. u32 fn;
  83. fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
  84. err = invoke_psci_fn(fn, state, entry_point, 0);
  85. return psci_to_linux_errno(err);
  86. }
  87. static int psci_cpu_off(u32 state)
  88. {
  89. int err;
  90. u32 fn;
  91. fn = psci_function_id[PSCI_FN_CPU_OFF];
  92. err = invoke_psci_fn(fn, state, 0, 0);
  93. return psci_to_linux_errno(err);
  94. }
  95. static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
  96. {
  97. int err;
  98. u32 fn;
  99. fn = psci_function_id[PSCI_FN_CPU_ON];
  100. err = invoke_psci_fn(fn, cpuid, entry_point, 0);
  101. return psci_to_linux_errno(err);
  102. }
  103. static int psci_migrate(unsigned long cpuid)
  104. {
  105. int err;
  106. u32 fn;
  107. fn = psci_function_id[PSCI_FN_MIGRATE];
  108. err = invoke_psci_fn(fn, cpuid, 0, 0);
  109. return psci_to_linux_errno(err);
  110. }
  111. static int psci_affinity_info(unsigned long target_affinity,
  112. unsigned long lowest_affinity_level)
  113. {
  114. return invoke_psci_fn(PSCI_0_2_FN_NATIVE(AFFINITY_INFO),
  115. target_affinity, lowest_affinity_level, 0);
  116. }
  117. static int psci_migrate_info_type(void)
  118. {
  119. return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
  120. }
  121. static unsigned long psci_migrate_info_up_cpu(void)
  122. {
  123. return invoke_psci_fn(PSCI_0_2_FN_NATIVE(MIGRATE_INFO_UP_CPU),
  124. 0, 0, 0);
  125. }
  126. static int get_set_conduit_method(struct device_node *np)
  127. {
  128. const char *method;
  129. pr_info("probing for conduit method from DT.\n");
  130. if (of_property_read_string(np, "method", &method)) {
  131. pr_warn("missing \"method\" property\n");
  132. return -ENXIO;
  133. }
  134. if (!strcmp("hvc", method)) {
  135. invoke_psci_fn = __invoke_psci_fn_hvc;
  136. } else if (!strcmp("smc", method)) {
  137. invoke_psci_fn = __invoke_psci_fn_smc;
  138. } else {
  139. pr_warn("invalid \"method\" property: %s\n", method);
  140. return -EINVAL;
  141. }
  142. return 0;
  143. }
  144. static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd)
  145. {
  146. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
  147. }
  148. static void psci_sys_poweroff(void)
  149. {
  150. invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
  151. }
  152. /*
  153. * Detect the presence of a resident Trusted OS which may cause CPU_OFF to
  154. * return DENIED (which would be fatal).
  155. */
  156. static void __init psci_init_migrate(void)
  157. {
  158. unsigned long cpuid;
  159. int type, cpu = -1;
  160. type = psci_ops.migrate_info_type();
  161. if (type == PSCI_0_2_TOS_MP) {
  162. pr_info("Trusted OS migration not required\n");
  163. return;
  164. }
  165. if (type == PSCI_RET_NOT_SUPPORTED) {
  166. pr_info("MIGRATE_INFO_TYPE not supported.\n");
  167. return;
  168. }
  169. if (type != PSCI_0_2_TOS_UP_MIGRATE &&
  170. type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
  171. pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
  172. return;
  173. }
  174. cpuid = psci_migrate_info_up_cpu();
  175. if (cpuid & ~MPIDR_HWID_BITMASK) {
  176. pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
  177. cpuid);
  178. return;
  179. }
  180. cpu = get_logical_index(cpuid);
  181. resident_cpu = cpu >= 0 ? cpu : -1;
  182. pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid);
  183. }
  184. static void __init psci_0_2_set_functions(void)
  185. {
  186. pr_info("Using standard PSCI v0.2 function IDs\n");
  187. psci_function_id[PSCI_FN_CPU_SUSPEND] = PSCI_0_2_FN_NATIVE(CPU_SUSPEND);
  188. psci_ops.cpu_suspend = psci_cpu_suspend;
  189. psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF;
  190. psci_ops.cpu_off = psci_cpu_off;
  191. psci_function_id[PSCI_FN_CPU_ON] = PSCI_0_2_FN_NATIVE(CPU_ON);
  192. psci_ops.cpu_on = psci_cpu_on;
  193. psci_function_id[PSCI_FN_MIGRATE] = PSCI_0_2_FN_NATIVE(MIGRATE);
  194. psci_ops.migrate = psci_migrate;
  195. psci_ops.affinity_info = psci_affinity_info;
  196. psci_ops.migrate_info_type = psci_migrate_info_type;
  197. arm_pm_restart = psci_sys_reset;
  198. pm_power_off = psci_sys_poweroff;
  199. }
  200. /*
  201. * Probe function for PSCI firmware versions >= 0.2
  202. */
  203. static int __init psci_probe(void)
  204. {
  205. u32 ver = psci_get_version();
  206. pr_info("PSCIv%d.%d detected in firmware.\n",
  207. PSCI_VERSION_MAJOR(ver),
  208. PSCI_VERSION_MINOR(ver));
  209. if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
  210. pr_err("Conflicting PSCI version detected.\n");
  211. return -EINVAL;
  212. }
  213. psci_0_2_set_functions();
  214. psci_init_migrate();
  215. return 0;
  216. }
  217. typedef int (*psci_initcall_t)(const struct device_node *);
  218. /*
  219. * PSCI init function for PSCI versions >=0.2
  220. *
  221. * Probe based on PSCI PSCI_VERSION function
  222. */
  223. static int __init psci_0_2_init(struct device_node *np)
  224. {
  225. int err;
  226. err = get_set_conduit_method(np);
  227. if (err)
  228. goto out_put_node;
  229. /*
  230. * Starting with v0.2, the PSCI specification introduced a call
  231. * (PSCI_VERSION) that allows probing the firmware version, so
  232. * that PSCI function IDs and version specific initialization
  233. * can be carried out according to the specific version reported
  234. * by firmware
  235. */
  236. err = psci_probe();
  237. out_put_node:
  238. of_node_put(np);
  239. return err;
  240. }
  241. /*
  242. * PSCI < v0.2 get PSCI Function IDs via DT.
  243. */
  244. static int __init psci_0_1_init(struct device_node *np)
  245. {
  246. u32 id;
  247. int err;
  248. err = get_set_conduit_method(np);
  249. if (err)
  250. goto out_put_node;
  251. pr_info("Using PSCI v0.1 Function IDs from DT\n");
  252. if (!of_property_read_u32(np, "cpu_suspend", &id)) {
  253. psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
  254. psci_ops.cpu_suspend = psci_cpu_suspend;
  255. }
  256. if (!of_property_read_u32(np, "cpu_off", &id)) {
  257. psci_function_id[PSCI_FN_CPU_OFF] = id;
  258. psci_ops.cpu_off = psci_cpu_off;
  259. }
  260. if (!of_property_read_u32(np, "cpu_on", &id)) {
  261. psci_function_id[PSCI_FN_CPU_ON] = id;
  262. psci_ops.cpu_on = psci_cpu_on;
  263. }
  264. if (!of_property_read_u32(np, "migrate", &id)) {
  265. psci_function_id[PSCI_FN_MIGRATE] = id;
  266. psci_ops.migrate = psci_migrate;
  267. }
  268. out_put_node:
  269. of_node_put(np);
  270. return err;
  271. }
  272. static const struct of_device_id const psci_of_match[] __initconst = {
  273. { .compatible = "arm,psci", .data = psci_0_1_init},
  274. { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
  275. {},
  276. };
  277. int __init psci_dt_init(void)
  278. {
  279. struct device_node *np;
  280. const struct of_device_id *matched_np;
  281. psci_initcall_t init_fn;
  282. np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
  283. if (!np)
  284. return -ENODEV;
  285. init_fn = (psci_initcall_t)matched_np->data;
  286. return init_fn(np);
  287. }
  288. #ifdef CONFIG_ACPI
  289. /*
  290. * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's
  291. * explicitly clarified in SBBR
  292. */
  293. int __init psci_acpi_init(void)
  294. {
  295. if (!acpi_psci_present()) {
  296. pr_info("is not implemented in ACPI.\n");
  297. return -EOPNOTSUPP;
  298. }
  299. pr_info("probing for conduit method from ACPI.\n");
  300. if (acpi_psci_use_hvc())
  301. invoke_psci_fn = __invoke_psci_fn_hvc;
  302. else
  303. invoke_psci_fn = __invoke_psci_fn_smc;
  304. return psci_probe();
  305. }
  306. #endif