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