psci.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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) 2012 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 <asm/compiler.h>
  19. #include <asm/errno.h>
  20. #include <asm/opcodes-sec.h>
  21. #include <asm/opcodes-virt.h>
  22. #include <asm/psci.h>
  23. struct psci_operations psci_ops;
  24. static int (*invoke_psci_fn)(u32, u32, u32, u32);
  25. enum psci_function {
  26. PSCI_FN_CPU_SUSPEND,
  27. PSCI_FN_CPU_ON,
  28. PSCI_FN_CPU_OFF,
  29. PSCI_FN_MIGRATE,
  30. PSCI_FN_MAX,
  31. };
  32. static u32 psci_function_id[PSCI_FN_MAX];
  33. #define PSCI_RET_SUCCESS 0
  34. #define PSCI_RET_EOPNOTSUPP -1
  35. #define PSCI_RET_EINVAL -2
  36. #define PSCI_RET_EPERM -3
  37. static int psci_to_linux_errno(int errno)
  38. {
  39. switch (errno) {
  40. case PSCI_RET_SUCCESS:
  41. return 0;
  42. case PSCI_RET_EOPNOTSUPP:
  43. return -EOPNOTSUPP;
  44. case PSCI_RET_EINVAL:
  45. return -EINVAL;
  46. case PSCI_RET_EPERM:
  47. return -EPERM;
  48. };
  49. return -EINVAL;
  50. }
  51. #define PSCI_POWER_STATE_ID_MASK 0xffff
  52. #define PSCI_POWER_STATE_ID_SHIFT 0
  53. #define PSCI_POWER_STATE_TYPE_MASK 0x1
  54. #define PSCI_POWER_STATE_TYPE_SHIFT 16
  55. #define PSCI_POWER_STATE_AFFL_MASK 0x3
  56. #define PSCI_POWER_STATE_AFFL_SHIFT 24
  57. static u32 psci_power_state_pack(struct psci_power_state state)
  58. {
  59. return ((state.id & PSCI_POWER_STATE_ID_MASK)
  60. << PSCI_POWER_STATE_ID_SHIFT) |
  61. ((state.type & PSCI_POWER_STATE_TYPE_MASK)
  62. << PSCI_POWER_STATE_TYPE_SHIFT) |
  63. ((state.affinity_level & PSCI_POWER_STATE_AFFL_MASK)
  64. << PSCI_POWER_STATE_AFFL_SHIFT);
  65. }
  66. /*
  67. * The following two functions are invoked via the invoke_psci_fn pointer
  68. * and will not be inlined, allowing us to piggyback on the AAPCS.
  69. */
  70. static noinline int __invoke_psci_fn_hvc(u32 function_id, u32 arg0, u32 arg1,
  71. u32 arg2)
  72. {
  73. asm volatile(
  74. __asmeq("%0", "r0")
  75. __asmeq("%1", "r1")
  76. __asmeq("%2", "r2")
  77. __asmeq("%3", "r3")
  78. __HVC(0)
  79. : "+r" (function_id)
  80. : "r" (arg0), "r" (arg1), "r" (arg2));
  81. return function_id;
  82. }
  83. static noinline int __invoke_psci_fn_smc(u32 function_id, u32 arg0, u32 arg1,
  84. u32 arg2)
  85. {
  86. asm volatile(
  87. __asmeq("%0", "r0")
  88. __asmeq("%1", "r1")
  89. __asmeq("%2", "r2")
  90. __asmeq("%3", "r3")
  91. __SMC(0)
  92. : "+r" (function_id)
  93. : "r" (arg0), "r" (arg1), "r" (arg2));
  94. return function_id;
  95. }
  96. static int psci_cpu_suspend(struct psci_power_state state,
  97. unsigned long entry_point)
  98. {
  99. int err;
  100. u32 fn, power_state;
  101. fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
  102. power_state = psci_power_state_pack(state);
  103. err = invoke_psci_fn(fn, power_state, entry_point, 0);
  104. return psci_to_linux_errno(err);
  105. }
  106. static int psci_cpu_off(struct psci_power_state state)
  107. {
  108. int err;
  109. u32 fn, power_state;
  110. fn = psci_function_id[PSCI_FN_CPU_OFF];
  111. power_state = psci_power_state_pack(state);
  112. err = invoke_psci_fn(fn, power_state, 0, 0);
  113. return psci_to_linux_errno(err);
  114. }
  115. static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
  116. {
  117. int err;
  118. u32 fn;
  119. fn = psci_function_id[PSCI_FN_CPU_ON];
  120. err = invoke_psci_fn(fn, cpuid, entry_point, 0);
  121. return psci_to_linux_errno(err);
  122. }
  123. static int psci_migrate(unsigned long cpuid)
  124. {
  125. int err;
  126. u32 fn;
  127. fn = psci_function_id[PSCI_FN_MIGRATE];
  128. err = invoke_psci_fn(fn, cpuid, 0, 0);
  129. return psci_to_linux_errno(err);
  130. }
  131. static const struct of_device_id psci_of_match[] __initconst = {
  132. { .compatible = "arm,psci", },
  133. {},
  134. };
  135. static int __init psci_init(void)
  136. {
  137. struct device_node *np;
  138. const char *method;
  139. u32 id;
  140. np = of_find_matching_node(NULL, psci_of_match);
  141. if (!np)
  142. return 0;
  143. pr_info("probing function IDs from device-tree\n");
  144. if (of_property_read_string(np, "method", &method)) {
  145. pr_warning("missing \"method\" property\n");
  146. goto out_put_node;
  147. }
  148. if (!strcmp("hvc", method)) {
  149. invoke_psci_fn = __invoke_psci_fn_hvc;
  150. } else if (!strcmp("smc", method)) {
  151. invoke_psci_fn = __invoke_psci_fn_smc;
  152. } else {
  153. pr_warning("invalid \"method\" property: %s\n", method);
  154. goto out_put_node;
  155. }
  156. if (!of_property_read_u32(np, "cpu_suspend", &id)) {
  157. psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
  158. psci_ops.cpu_suspend = psci_cpu_suspend;
  159. }
  160. if (!of_property_read_u32(np, "cpu_off", &id)) {
  161. psci_function_id[PSCI_FN_CPU_OFF] = id;
  162. psci_ops.cpu_off = psci_cpu_off;
  163. }
  164. if (!of_property_read_u32(np, "cpu_on", &id)) {
  165. psci_function_id[PSCI_FN_CPU_ON] = id;
  166. psci_ops.cpu_on = psci_cpu_on;
  167. }
  168. if (!of_property_read_u32(np, "migrate", &id)) {
  169. psci_function_id[PSCI_FN_MIGRATE] = id;
  170. psci_ops.migrate = psci_migrate;
  171. }
  172. out_put_node:
  173. of_node_put(np);
  174. return 0;
  175. }
  176. early_initcall(psci_init);