psci.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (C) 2012 - ARM Ltd
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/preempt.h>
  18. #include <linux/kvm_host.h>
  19. #include <linux/wait.h>
  20. #include <asm/cputype.h>
  21. #include <asm/kvm_emulate.h>
  22. #include <asm/kvm_psci.h>
  23. #include <asm/kvm_host.h>
  24. #include <uapi/linux/psci.h>
  25. /*
  26. * This is an implementation of the Power State Coordination Interface
  27. * as described in ARM document number ARM DEN 0022A.
  28. */
  29. #define AFFINITY_MASK(level) ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1)
  30. static unsigned long psci_affinity_mask(unsigned long affinity_level)
  31. {
  32. if (affinity_level <= 3)
  33. return MPIDR_HWID_BITMASK & AFFINITY_MASK(affinity_level);
  34. return 0;
  35. }
  36. static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)
  37. {
  38. /*
  39. * NOTE: For simplicity, we make VCPU suspend emulation to be
  40. * same-as WFI (Wait-for-interrupt) emulation.
  41. *
  42. * This means for KVM the wakeup events are interrupts and
  43. * this is consistent with intended use of StateID as described
  44. * in section 5.4.1 of PSCI v0.2 specification (ARM DEN 0022A).
  45. *
  46. * Further, we also treat power-down request to be same as
  47. * stand-by request as-per section 5.4.2 clause 3 of PSCI v0.2
  48. * specification (ARM DEN 0022A). This means all suspend states
  49. * for KVM will preserve the register state.
  50. */
  51. kvm_vcpu_block(vcpu);
  52. return PSCI_RET_SUCCESS;
  53. }
  54. static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
  55. {
  56. vcpu->arch.pause = true;
  57. }
  58. static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
  59. {
  60. struct kvm *kvm = source_vcpu->kvm;
  61. struct kvm_vcpu *vcpu = NULL;
  62. wait_queue_head_t *wq;
  63. unsigned long cpu_id;
  64. unsigned long context_id;
  65. phys_addr_t target_pc;
  66. cpu_id = *vcpu_reg(source_vcpu, 1) & MPIDR_HWID_BITMASK;
  67. if (vcpu_mode_is_32bit(source_vcpu))
  68. cpu_id &= ~((u32) 0);
  69. vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
  70. /*
  71. * Make sure the caller requested a valid CPU and that the CPU is
  72. * turned off.
  73. */
  74. if (!vcpu)
  75. return PSCI_RET_INVALID_PARAMS;
  76. if (!vcpu->arch.pause) {
  77. if (kvm_psci_version(source_vcpu) != KVM_ARM_PSCI_0_1)
  78. return PSCI_RET_ALREADY_ON;
  79. else
  80. return PSCI_RET_INVALID_PARAMS;
  81. }
  82. target_pc = *vcpu_reg(source_vcpu, 2);
  83. context_id = *vcpu_reg(source_vcpu, 3);
  84. kvm_reset_vcpu(vcpu);
  85. /* Gracefully handle Thumb2 entry point */
  86. if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
  87. target_pc &= ~((phys_addr_t) 1);
  88. vcpu_set_thumb(vcpu);
  89. }
  90. /* Propagate caller endianness */
  91. if (kvm_vcpu_is_be(source_vcpu))
  92. kvm_vcpu_set_be(vcpu);
  93. *vcpu_pc(vcpu) = target_pc;
  94. /*
  95. * NOTE: We always update r0 (or x0) because for PSCI v0.1
  96. * the general puspose registers are undefined upon CPU_ON.
  97. */
  98. *vcpu_reg(vcpu, 0) = context_id;
  99. vcpu->arch.pause = false;
  100. smp_mb(); /* Make sure the above is visible */
  101. wq = kvm_arch_vcpu_wq(vcpu);
  102. wake_up_interruptible(wq);
  103. return PSCI_RET_SUCCESS;
  104. }
  105. static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
  106. {
  107. int i;
  108. unsigned long mpidr;
  109. unsigned long target_affinity;
  110. unsigned long target_affinity_mask;
  111. unsigned long lowest_affinity_level;
  112. struct kvm *kvm = vcpu->kvm;
  113. struct kvm_vcpu *tmp;
  114. target_affinity = *vcpu_reg(vcpu, 1);
  115. lowest_affinity_level = *vcpu_reg(vcpu, 2);
  116. /* Determine target affinity mask */
  117. target_affinity_mask = psci_affinity_mask(lowest_affinity_level);
  118. if (!target_affinity_mask)
  119. return PSCI_RET_INVALID_PARAMS;
  120. /* Ignore other bits of target affinity */
  121. target_affinity &= target_affinity_mask;
  122. /*
  123. * If one or more VCPU matching target affinity are running
  124. * then ON else OFF
  125. */
  126. kvm_for_each_vcpu(i, tmp, kvm) {
  127. mpidr = kvm_vcpu_get_mpidr_aff(tmp);
  128. if (((mpidr & target_affinity_mask) == target_affinity) &&
  129. !tmp->arch.pause) {
  130. return PSCI_0_2_AFFINITY_LEVEL_ON;
  131. }
  132. }
  133. return PSCI_0_2_AFFINITY_LEVEL_OFF;
  134. }
  135. static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type)
  136. {
  137. int i;
  138. struct kvm_vcpu *tmp;
  139. /*
  140. * The KVM ABI specifies that a system event exit may call KVM_RUN
  141. * again and may perform shutdown/reboot at a later time that when the
  142. * actual request is made. Since we are implementing PSCI and a
  143. * caller of PSCI reboot and shutdown expects that the system shuts
  144. * down or reboots immediately, let's make sure that VCPUs are not run
  145. * after this call is handled and before the VCPUs have been
  146. * re-initialized.
  147. */
  148. kvm_for_each_vcpu(i, tmp, vcpu->kvm) {
  149. tmp->arch.pause = true;
  150. kvm_vcpu_kick(tmp);
  151. }
  152. memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
  153. vcpu->run->system_event.type = type;
  154. vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
  155. }
  156. static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
  157. {
  158. kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN);
  159. }
  160. static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
  161. {
  162. kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET);
  163. }
  164. int kvm_psci_version(struct kvm_vcpu *vcpu)
  165. {
  166. if (test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features))
  167. return KVM_ARM_PSCI_0_2;
  168. return KVM_ARM_PSCI_0_1;
  169. }
  170. static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
  171. {
  172. int ret = 1;
  173. unsigned long psci_fn = *vcpu_reg(vcpu, 0) & ~((u32) 0);
  174. unsigned long val;
  175. switch (psci_fn) {
  176. case PSCI_0_2_FN_PSCI_VERSION:
  177. /*
  178. * Bits[31:16] = Major Version = 0
  179. * Bits[15:0] = Minor Version = 2
  180. */
  181. val = 2;
  182. break;
  183. case PSCI_0_2_FN_CPU_SUSPEND:
  184. case PSCI_0_2_FN64_CPU_SUSPEND:
  185. val = kvm_psci_vcpu_suspend(vcpu);
  186. break;
  187. case PSCI_0_2_FN_CPU_OFF:
  188. kvm_psci_vcpu_off(vcpu);
  189. val = PSCI_RET_SUCCESS;
  190. break;
  191. case PSCI_0_2_FN_CPU_ON:
  192. case PSCI_0_2_FN64_CPU_ON:
  193. val = kvm_psci_vcpu_on(vcpu);
  194. break;
  195. case PSCI_0_2_FN_AFFINITY_INFO:
  196. case PSCI_0_2_FN64_AFFINITY_INFO:
  197. val = kvm_psci_vcpu_affinity_info(vcpu);
  198. break;
  199. case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
  200. /*
  201. * Trusted OS is MP hence does not require migration
  202. * or
  203. * Trusted OS is not present
  204. */
  205. val = PSCI_0_2_TOS_MP;
  206. break;
  207. case PSCI_0_2_FN_SYSTEM_OFF:
  208. kvm_psci_system_off(vcpu);
  209. /*
  210. * We should'nt be going back to guest VCPU after
  211. * receiving SYSTEM_OFF request.
  212. *
  213. * If user space accidently/deliberately resumes
  214. * guest VCPU after SYSTEM_OFF request then guest
  215. * VCPU should see internal failure from PSCI return
  216. * value. To achieve this, we preload r0 (or x0) with
  217. * PSCI return value INTERNAL_FAILURE.
  218. */
  219. val = PSCI_RET_INTERNAL_FAILURE;
  220. ret = 0;
  221. break;
  222. case PSCI_0_2_FN_SYSTEM_RESET:
  223. kvm_psci_system_reset(vcpu);
  224. /*
  225. * Same reason as SYSTEM_OFF for preloading r0 (or x0)
  226. * with PSCI return value INTERNAL_FAILURE.
  227. */
  228. val = PSCI_RET_INTERNAL_FAILURE;
  229. ret = 0;
  230. break;
  231. default:
  232. val = PSCI_RET_NOT_SUPPORTED;
  233. break;
  234. }
  235. *vcpu_reg(vcpu, 0) = val;
  236. return ret;
  237. }
  238. static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
  239. {
  240. unsigned long psci_fn = *vcpu_reg(vcpu, 0) & ~((u32) 0);
  241. unsigned long val;
  242. switch (psci_fn) {
  243. case KVM_PSCI_FN_CPU_OFF:
  244. kvm_psci_vcpu_off(vcpu);
  245. val = PSCI_RET_SUCCESS;
  246. break;
  247. case KVM_PSCI_FN_CPU_ON:
  248. val = kvm_psci_vcpu_on(vcpu);
  249. break;
  250. default:
  251. val = PSCI_RET_NOT_SUPPORTED;
  252. break;
  253. }
  254. *vcpu_reg(vcpu, 0) = val;
  255. return 1;
  256. }
  257. /**
  258. * kvm_psci_call - handle PSCI call if r0 value is in range
  259. * @vcpu: Pointer to the VCPU struct
  260. *
  261. * Handle PSCI calls from guests through traps from HVC instructions.
  262. * The calling convention is similar to SMC calls to the secure world
  263. * where the function number is placed in r0.
  264. *
  265. * This function returns: > 0 (success), 0 (success but exit to user
  266. * space), and < 0 (errors)
  267. *
  268. * Errors:
  269. * -EINVAL: Unrecognized PSCI function
  270. */
  271. int kvm_psci_call(struct kvm_vcpu *vcpu)
  272. {
  273. switch (kvm_psci_version(vcpu)) {
  274. case KVM_ARM_PSCI_0_2:
  275. return kvm_psci_0_2_call(vcpu);
  276. case KVM_ARM_PSCI_0_1:
  277. return kvm_psci_0_1_call(vcpu);
  278. default:
  279. return -EINVAL;
  280. };
  281. }