psci.c 11 KB

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