psci.c 8.6 KB

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