psci.c 8.6 KB

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