psci-relay.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 - Google LLC
  4. * Author: David Brazdil <[email protected]>
  5. */
  6. #include <asm/kvm_asm.h>
  7. #include <asm/kvm_hyp.h>
  8. #include <asm/kvm_hypevents.h>
  9. #include <asm/kvm_mmu.h>
  10. #include <linux/kvm_host.h>
  11. #include <uapi/linux/psci.h>
  12. #include <nvhe/arm-smccc.h>
  13. #include <nvhe/mem_protect.h>
  14. #include <nvhe/memory.h>
  15. #include <nvhe/pkvm.h>
  16. #include <nvhe/trap_handler.h>
  17. void kvm_hyp_cpu_entry(unsigned long r0);
  18. void kvm_hyp_cpu_resume(unsigned long r0);
  19. void __noreturn __host_enter(struct kvm_cpu_context *host_ctxt);
  20. /* Config options set by the host. */
  21. struct kvm_host_psci_config __ro_after_init kvm_host_psci_config;
  22. static void (*pkvm_psci_notifier)(enum pkvm_psci_notification, struct kvm_cpu_context *);
  23. static void pkvm_psci_notify(enum pkvm_psci_notification notif, struct kvm_cpu_context *host_ctxt)
  24. {
  25. if (smp_load_acquire(&pkvm_psci_notifier))
  26. pkvm_psci_notifier(notif, host_ctxt);
  27. }
  28. #ifdef CONFIG_MODULES
  29. int __pkvm_register_psci_notifier(void (*cb)(enum pkvm_psci_notification, struct kvm_cpu_context *))
  30. {
  31. /*
  32. * Paired with smp_load_acquire(&pkvm_psci_notifier) in
  33. * pkvm_psci_notify(). Ensure memory stores hapenning during a pKVM module
  34. * init are observed before executing the callback.
  35. */
  36. return cmpxchg_release(&pkvm_psci_notifier, NULL, cb) ? -EBUSY : 0;
  37. }
  38. #endif
  39. #define INVALID_CPU_ID UINT_MAX
  40. struct psci_boot_args {
  41. atomic_t lock;
  42. unsigned long pc;
  43. unsigned long r0;
  44. };
  45. #define PSCI_BOOT_ARGS_UNLOCKED 0
  46. #define PSCI_BOOT_ARGS_LOCKED 1
  47. #define PSCI_BOOT_ARGS_INIT \
  48. ((struct psci_boot_args){ \
  49. .lock = ATOMIC_INIT(PSCI_BOOT_ARGS_UNLOCKED), \
  50. })
  51. static DEFINE_PER_CPU(struct psci_boot_args, cpu_on_args) = PSCI_BOOT_ARGS_INIT;
  52. static DEFINE_PER_CPU(struct psci_boot_args, suspend_args) = PSCI_BOOT_ARGS_INIT;
  53. #define is_psci_0_1(what, func_id) \
  54. (kvm_host_psci_config.psci_0_1_ ## what ## _implemented && \
  55. (func_id) == kvm_host_psci_config.function_ids_0_1.what)
  56. static bool is_psci_0_1_call(u64 func_id)
  57. {
  58. return (is_psci_0_1(cpu_suspend, func_id) ||
  59. is_psci_0_1(cpu_on, func_id) ||
  60. is_psci_0_1(cpu_off, func_id) ||
  61. is_psci_0_1(migrate, func_id));
  62. }
  63. static bool is_psci_0_2_call(u64 func_id)
  64. {
  65. /* SMCCC reserves IDs 0x00-1F with the given 32/64-bit base for PSCI. */
  66. return (PSCI_0_2_FN(0) <= func_id && func_id <= PSCI_0_2_FN(31)) ||
  67. (PSCI_0_2_FN64(0) <= func_id && func_id <= PSCI_0_2_FN64(31));
  68. }
  69. static unsigned long psci_call(unsigned long fn, unsigned long arg0,
  70. unsigned long arg1, unsigned long arg2)
  71. {
  72. struct arm_smccc_res res;
  73. arm_smccc_1_1_smc(fn, arg0, arg1, arg2, &res);
  74. return res.a0;
  75. }
  76. static unsigned long psci_forward(struct kvm_cpu_context *host_ctxt)
  77. {
  78. return psci_call(cpu_reg(host_ctxt, 0), cpu_reg(host_ctxt, 1),
  79. cpu_reg(host_ctxt, 2), cpu_reg(host_ctxt, 3));
  80. }
  81. static unsigned int find_cpu_id(u64 mpidr)
  82. {
  83. unsigned int i;
  84. /* Reject invalid MPIDRs */
  85. if (mpidr & ~MPIDR_HWID_BITMASK)
  86. return INVALID_CPU_ID;
  87. for (i = 0; i < NR_CPUS; i++) {
  88. if (cpu_logical_map(i) == mpidr)
  89. return i;
  90. }
  91. return INVALID_CPU_ID;
  92. }
  93. static __always_inline bool try_acquire_boot_args(struct psci_boot_args *args)
  94. {
  95. return atomic_cmpxchg_acquire(&args->lock,
  96. PSCI_BOOT_ARGS_UNLOCKED,
  97. PSCI_BOOT_ARGS_LOCKED) ==
  98. PSCI_BOOT_ARGS_UNLOCKED;
  99. }
  100. static __always_inline void release_boot_args(struct psci_boot_args *args)
  101. {
  102. atomic_set_release(&args->lock, PSCI_BOOT_ARGS_UNLOCKED);
  103. }
  104. static int psci_cpu_on(u64 func_id, struct kvm_cpu_context *host_ctxt)
  105. {
  106. DECLARE_REG(u64, mpidr, host_ctxt, 1);
  107. DECLARE_REG(unsigned long, pc, host_ctxt, 2);
  108. DECLARE_REG(unsigned long, r0, host_ctxt, 3);
  109. unsigned int cpu_id;
  110. struct psci_boot_args *boot_args;
  111. struct kvm_nvhe_init_params *init_params;
  112. int ret;
  113. /*
  114. * Find the logical CPU ID for the given MPIDR. The search set is
  115. * the set of CPUs that were online at the point of KVM initialization.
  116. * Booting other CPUs is rejected because their cpufeatures were not
  117. * checked against the finalized capabilities. This could be relaxed
  118. * by doing the feature checks in hyp.
  119. */
  120. cpu_id = find_cpu_id(mpidr);
  121. if (cpu_id == INVALID_CPU_ID)
  122. return PSCI_RET_INVALID_PARAMS;
  123. boot_args = per_cpu_ptr(&cpu_on_args, cpu_id);
  124. init_params = per_cpu_ptr(&kvm_init_params, cpu_id);
  125. /* Check if the target CPU is already being booted. */
  126. if (!try_acquire_boot_args(boot_args))
  127. return PSCI_RET_ALREADY_ON;
  128. boot_args->pc = pc;
  129. boot_args->r0 = r0;
  130. wmb();
  131. ret = psci_call(func_id, mpidr,
  132. __hyp_pa(&kvm_hyp_cpu_entry),
  133. __hyp_pa(init_params));
  134. /* If successful, the lock will be released by the target CPU. */
  135. if (ret != PSCI_RET_SUCCESS)
  136. release_boot_args(boot_args);
  137. return ret;
  138. }
  139. static int psci_cpu_suspend(u64 func_id, struct kvm_cpu_context *host_ctxt)
  140. {
  141. DECLARE_REG(u64, power_state, host_ctxt, 1);
  142. DECLARE_REG(unsigned long, pc, host_ctxt, 2);
  143. DECLARE_REG(unsigned long, r0, host_ctxt, 3);
  144. int ret;
  145. struct psci_boot_args *boot_args;
  146. struct kvm_nvhe_init_params *init_params;
  147. boot_args = this_cpu_ptr(&suspend_args);
  148. init_params = this_cpu_ptr(&kvm_init_params);
  149. /*
  150. * No need to acquire a lock before writing to boot_args because a core
  151. * can only suspend itself. Racy CPU_ON calls use a separate struct.
  152. */
  153. boot_args->pc = pc;
  154. boot_args->r0 = r0;
  155. pkvm_psci_notify(PKVM_PSCI_CPU_SUSPEND, host_ctxt);
  156. /*
  157. * Will either return if shallow sleep state, or wake up into the entry
  158. * point if it is a deep sleep state.
  159. */
  160. ret = psci_call(func_id, power_state,
  161. __hyp_pa(&kvm_hyp_cpu_resume),
  162. __hyp_pa(init_params));
  163. return ret;
  164. }
  165. static int psci_system_suspend(u64 func_id, struct kvm_cpu_context *host_ctxt)
  166. {
  167. DECLARE_REG(unsigned long, pc, host_ctxt, 1);
  168. DECLARE_REG(unsigned long, r0, host_ctxt, 2);
  169. struct psci_boot_args *boot_args;
  170. struct kvm_nvhe_init_params *init_params;
  171. boot_args = this_cpu_ptr(&suspend_args);
  172. init_params = this_cpu_ptr(&kvm_init_params);
  173. /*
  174. * No need to acquire a lock before writing to boot_args because a core
  175. * can only suspend itself. Racy CPU_ON calls use a separate struct.
  176. */
  177. boot_args->pc = pc;
  178. boot_args->r0 = r0;
  179. pkvm_psci_notify(PKVM_PSCI_SYSTEM_SUSPEND, host_ctxt);
  180. /* Will only return on error. */
  181. return psci_call(func_id,
  182. __hyp_pa(&kvm_hyp_cpu_resume),
  183. __hyp_pa(init_params), 0);
  184. }
  185. asmlinkage void __noreturn kvm_host_psci_cpu_entry(bool is_cpu_on)
  186. {
  187. struct psci_boot_args *boot_args;
  188. struct kvm_cpu_context *host_ctxt;
  189. trace_hyp_enter();
  190. host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
  191. if (is_cpu_on)
  192. boot_args = this_cpu_ptr(&cpu_on_args);
  193. else
  194. boot_args = this_cpu_ptr(&suspend_args);
  195. cpu_reg(host_ctxt, 0) = boot_args->r0;
  196. write_sysreg_el2(boot_args->pc, SYS_ELR);
  197. if (is_cpu_on)
  198. release_boot_args(boot_args);
  199. pkvm_psci_notify(PKVM_PSCI_CPU_ENTRY, host_ctxt);
  200. trace_hyp_exit();
  201. __host_enter(host_ctxt);
  202. }
  203. static DEFINE_HYP_SPINLOCK(mem_protect_lock);
  204. static u64 psci_mem_protect(s64 offset)
  205. {
  206. static u64 cnt;
  207. u64 new = cnt + offset;
  208. hyp_assert_lock_held(&mem_protect_lock);
  209. if (!offset || kvm_host_psci_config.version < PSCI_VERSION(1, 1))
  210. return cnt;
  211. if (!cnt || !new)
  212. psci_call(PSCI_1_1_FN_MEM_PROTECT, offset < 0 ? 0 : 1, 0, 0);
  213. cnt = new;
  214. return cnt;
  215. }
  216. static bool psci_mem_protect_active(void)
  217. {
  218. return psci_mem_protect(0);
  219. }
  220. void psci_mem_protect_inc(u64 n)
  221. {
  222. hyp_spin_lock(&mem_protect_lock);
  223. psci_mem_protect(n);
  224. hyp_spin_unlock(&mem_protect_lock);
  225. }
  226. void psci_mem_protect_dec(u64 n)
  227. {
  228. hyp_spin_lock(&mem_protect_lock);
  229. psci_mem_protect(-n);
  230. hyp_spin_unlock(&mem_protect_lock);
  231. }
  232. static unsigned long psci_0_1_handler(u64 func_id, struct kvm_cpu_context *host_ctxt)
  233. {
  234. if (is_psci_0_1(cpu_off, func_id) || is_psci_0_1(migrate, func_id))
  235. return psci_forward(host_ctxt);
  236. if (is_psci_0_1(cpu_on, func_id))
  237. return psci_cpu_on(func_id, host_ctxt);
  238. if (is_psci_0_1(cpu_suspend, func_id))
  239. return psci_cpu_suspend(func_id, host_ctxt);
  240. return PSCI_RET_NOT_SUPPORTED;
  241. }
  242. static unsigned long psci_0_2_handler(u64 func_id, struct kvm_cpu_context *host_ctxt)
  243. {
  244. switch (func_id) {
  245. case PSCI_0_2_FN_PSCI_VERSION:
  246. case PSCI_0_2_FN_CPU_OFF:
  247. case PSCI_0_2_FN64_AFFINITY_INFO:
  248. case PSCI_0_2_FN64_MIGRATE:
  249. case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
  250. case PSCI_0_2_FN64_MIGRATE_INFO_UP_CPU:
  251. return psci_forward(host_ctxt);
  252. /*
  253. * SYSTEM_OFF/RESET should not return according to the spec.
  254. * Allow it so as to stay robust to broken firmware.
  255. */
  256. case PSCI_0_2_FN_SYSTEM_OFF:
  257. case PSCI_0_2_FN_SYSTEM_RESET:
  258. pkvm_poison_pvmfw_pages();
  259. /* Avoid racing with a MEM_PROTECT call. */
  260. hyp_spin_lock(&mem_protect_lock);
  261. return psci_forward(host_ctxt);
  262. case PSCI_0_2_FN64_CPU_SUSPEND:
  263. return psci_cpu_suspend(func_id, host_ctxt);
  264. case PSCI_0_2_FN64_CPU_ON:
  265. return psci_cpu_on(func_id, host_ctxt);
  266. default:
  267. return PSCI_RET_NOT_SUPPORTED;
  268. }
  269. }
  270. static unsigned long psci_1_0_handler(u64 func_id, struct kvm_cpu_context *host_ctxt)
  271. {
  272. switch (func_id) {
  273. case PSCI_1_1_FN64_SYSTEM_RESET2:
  274. pkvm_poison_pvmfw_pages();
  275. hyp_spin_lock(&mem_protect_lock);
  276. if (psci_mem_protect_active())
  277. cpu_reg(host_ctxt, 0) = PSCI_0_2_FN_SYSTEM_RESET;
  278. fallthrough;
  279. case PSCI_1_0_FN_PSCI_FEATURES:
  280. case PSCI_1_0_FN_SET_SUSPEND_MODE:
  281. return psci_forward(host_ctxt);
  282. case PSCI_1_0_FN64_SYSTEM_SUSPEND:
  283. return psci_system_suspend(func_id, host_ctxt);
  284. default:
  285. return psci_0_2_handler(func_id, host_ctxt);
  286. }
  287. }
  288. bool kvm_host_psci_handler(struct kvm_cpu_context *host_ctxt)
  289. {
  290. DECLARE_REG(u64, func_id, host_ctxt, 0);
  291. unsigned long ret;
  292. switch (kvm_host_psci_config.version) {
  293. case PSCI_VERSION(0, 1):
  294. if (!is_psci_0_1_call(func_id))
  295. return false;
  296. ret = psci_0_1_handler(func_id, host_ctxt);
  297. break;
  298. case PSCI_VERSION(0, 2):
  299. if (!is_psci_0_2_call(func_id))
  300. return false;
  301. ret = psci_0_2_handler(func_id, host_ctxt);
  302. break;
  303. default:
  304. if (!is_psci_0_2_call(func_id))
  305. return false;
  306. ret = psci_1_0_handler(func_id, host_ctxt);
  307. break;
  308. }
  309. cpu_reg(host_ctxt, 0) = ret;
  310. cpu_reg(host_ctxt, 1) = 0;
  311. cpu_reg(host_ctxt, 2) = 0;
  312. cpu_reg(host_ctxt, 3) = 0;
  313. return true;
  314. }