gh_proxy_sched.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2018 The Hafnium Authors.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * version 2 as 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. * This driver is based on idea from Hafnium Hypervisor Linux Driver,
  15. * but modified to work with Gunyah Hypervisor as needed.
  16. *
  17. * Copyright (c) 2021-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  18. */
  19. #define pr_fmt(fmt) "gh_proxy_sched: " fmt
  20. #include <linux/init.h>
  21. #include <linux/kthread.h>
  22. #include <linux/sched.h>
  23. #include <linux/sched/rt.h>
  24. #include <linux/sched/task.h>
  25. #include <linux/slab.h>
  26. #include <uapi/linux/sched/types.h>
  27. #include <linux/module.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/pm_wakeup.h>
  30. #include <linux/of.h>
  31. #include <linux/mutex.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/wait.h>
  34. #include <linux/sched/signal.h>
  35. #include <linux/workqueue.h>
  36. #include <linux/suspend.h>
  37. #include <linux/gunyah/gh_rm_drv.h>
  38. #include <linux/gunyah/gh_vm.h>
  39. #include "gh_proxy_sched.h"
  40. #define CREATE_TRACE_POINTS
  41. #include "gh_proxy_sched_trace.h"
  42. #define GH_MAX_VMS 5
  43. #define GH_MAX_VCPUS_PER_VM 8
  44. #define GH_MAX_SYSTEM_VCPUS (GH_MAX_VMS * GH_MAX_VCPUS_PER_VM)
  45. /* VCPU is ready to run */
  46. #define GH_VCPU_STATE_READY 0
  47. /* VCPU is sleeping until an interrupt arrives */
  48. #define GH_VCPU_STATE_EXPECTS_WAKEUP 1
  49. /* VCPU is powered off */
  50. #define GH_VCPU_STATE_POWERED_OFF 2
  51. /* VCPU is blocked in EL2 for an unspecified reason */
  52. #define GH_VCPU_STATE_BLOCKED 3
  53. #define GH_VCPU_SUSPEND_STATE_STANDBY 0
  54. #define GH_VCPU_SUSPEND_STATE_POWERDOWN 1
  55. #define SVM_STATE_RUNNING 1
  56. #define SVM_STATE_SYSTEM_SUSPENDED 3
  57. struct gh_proxy_vcpu {
  58. struct gh_proxy_vm *vm;
  59. gh_capid_t cap_id;
  60. gh_label_t idx;
  61. bool abort_sleep;
  62. bool wdog_frozen;
  63. struct task_struct *task;
  64. int virq;
  65. char irq_name[32];
  66. char ws_name[32];
  67. wait_queue_head_t wait_queue;
  68. struct wakeup_source *ws;
  69. bool workqueue_mode;
  70. struct work_struct work;
  71. struct notifier_block suspend_nb;
  72. };
  73. struct gh_proxy_vm {
  74. gh_vmid_t id;
  75. int vcpu_count;
  76. struct gh_proxy_vcpu vcpu[GH_MAX_VCPUS_PER_VM];
  77. bool is_vcpu_info_populated;
  78. bool is_active;
  79. gh_capid_t wdog_cap_id;
  80. gh_capid_t vpmg_cap_id;
  81. int susp_res_irq;
  82. bool is_vpm_group_info_populated;
  83. struct workqueue_struct *vcpu_wq;
  84. };
  85. static struct gh_proxy_vm *gh_vms;
  86. static int nr_vms;
  87. static int nr_vcpus;
  88. static bool init_done;
  89. static DEFINE_MUTEX(gh_vm_mutex);
  90. static DEFINE_SPINLOCK(gh_vm_lock);
  91. /*
  92. * Wakes up the thread responsible for running the given vcpu.
  93. */
  94. static inline void gh_vcpu_wake_up(struct gh_proxy_vcpu *vcpu)
  95. {
  96. vcpu->abort_sleep = true;
  97. wake_up(&vcpu->wait_queue);
  98. }
  99. /*
  100. * Puts the current thread to sleep. The current thread must be responsible for
  101. * running the given vcpu.
  102. */
  103. static inline void gh_vcpu_sleep(struct gh_proxy_vcpu *vcpu)
  104. {
  105. if (!vcpu->abort_sleep && !signal_pending(current))
  106. wait_event_interruptible(vcpu->wait_queue, vcpu->abort_sleep);
  107. }
  108. static void gh_init_wait_queues(struct gh_proxy_vm *vm)
  109. {
  110. gh_label_t j;
  111. for (j = 0; j < vm->vcpu_count; j++)
  112. init_waitqueue_head(&vm->vcpu[j].wait_queue);
  113. }
  114. static inline bool is_vm_supports_proxy(gh_vmid_t gh_vmid)
  115. {
  116. gh_vmid_t vmid;
  117. if ((!ghd_rm_get_vmid(GH_TRUSTED_VM, &vmid) && vmid == gh_vmid) ||
  118. (!ghd_rm_get_vmid(GH_OEM_VM, &vmid) && vmid == gh_vmid))
  119. return true;
  120. return false;
  121. }
  122. static inline struct gh_proxy_vm *gh_get_vm(gh_vmid_t vmid)
  123. {
  124. int i;
  125. struct gh_proxy_vm *vm = NULL;
  126. for (i = 0; i < GH_MAX_VMS; i++) {
  127. vm = &gh_vms[i];
  128. if (vmid == vm->id || vm->id == GH_VMID_INVAL)
  129. break;
  130. }
  131. return vm;
  132. }
  133. static inline struct gh_proxy_vcpu *gh_get_vcpu(struct gh_proxy_vm *vm, gh_capid_t cap_id)
  134. {
  135. int i;
  136. struct gh_proxy_vcpu *vcpu = NULL;
  137. for (i = 0; i < vm->vcpu_count; i++) {
  138. if (vm->vcpu[i].cap_id == cap_id) {
  139. vcpu = &vm->vcpu[i];
  140. break;
  141. }
  142. }
  143. return vcpu;
  144. }
  145. static inline void gh_reset_vm(struct gh_proxy_vm *vm)
  146. {
  147. int j;
  148. vm->id = GH_VMID_INVAL;
  149. vm->vcpu_count = 0;
  150. vm->is_vcpu_info_populated = false;
  151. vm->is_active = false;
  152. vm->susp_res_irq = U32_MAX;
  153. vm->is_vpm_group_info_populated = false;
  154. vm->vpmg_cap_id = GH_CAPID_INVAL;
  155. for (j = 0; j < GH_MAX_VCPUS_PER_VM; j++) {
  156. vm->vcpu[j].cap_id = GH_CAPID_INVAL;
  157. vm->vcpu[j].virq = U32_MAX;
  158. vm->vcpu[j].idx = U32_MAX;
  159. vm->vcpu[j].vm = NULL;
  160. vm->vcpu[j].abort_sleep = false;
  161. vm->vcpu[j].wdog_frozen = false;
  162. vm->vcpu[j].ws = NULL;
  163. strscpy(vm->vcpu[vm->vcpu_count].irq_name, "",
  164. sizeof(vm->vcpu[vm->vcpu_count].irq_name));
  165. strscpy(vm->vcpu[vm->vcpu_count].ws_name, "",
  166. sizeof(vm->vcpu[vm->vcpu_count].ws_name));
  167. }
  168. }
  169. static void gh_init_vms(void)
  170. {
  171. struct gh_proxy_vm *vm;
  172. int i;
  173. for (i = 0; i < GH_MAX_VMS; i++) {
  174. vm = &gh_vms[i];
  175. gh_reset_vm(vm);
  176. }
  177. }
  178. static irqreturn_t gh_vcpu_irq_handler(int irq, void *data)
  179. {
  180. struct gh_proxy_vcpu *vcpu;
  181. struct gh_proxy_vm *vm;
  182. spin_lock(&gh_vm_lock);
  183. vcpu = data;
  184. if (!vcpu || !vcpu->vm || !vcpu->vm->is_vcpu_info_populated)
  185. goto unlock;
  186. vm = vcpu->vm;
  187. trace_gh_vcpu_irq_handler(vcpu->vm->id, vcpu->idx);
  188. if (vcpu->workqueue_mode)
  189. queue_work(vm->vcpu_wq, &vcpu->work);
  190. else
  191. gh_vcpu_wake_up(vcpu);
  192. unlock:
  193. spin_unlock(&gh_vm_lock);
  194. return IRQ_HANDLED;
  195. }
  196. static inline void gh_get_vcpu_prop_name(int vmid, int vcpu_num, char *name)
  197. {
  198. char extrastr[12];
  199. scnprintf(extrastr, 12, "_%d_%d", vmid, vcpu_num);
  200. strlcat(name, extrastr, 32);
  201. }
  202. static int gh_wdog_manage(gh_vmid_t vmid, gh_capid_t cap_id, bool populate)
  203. {
  204. struct gh_proxy_vm *vm;
  205. int ret = 0;
  206. if (!init_done) {
  207. pr_err("Driver probe failed\n");
  208. return -ENXIO;
  209. }
  210. if (!is_vm_supports_proxy(vmid)) {
  211. pr_info("Skip populating VCPU affinity info for VM=%d\n", vmid);
  212. return -EINVAL;
  213. }
  214. mutex_lock(&gh_vm_mutex);
  215. vm = gh_get_vm(vmid);
  216. if (!vm) {
  217. ret = -ENODEV;
  218. goto unlock;
  219. }
  220. if (populate)
  221. vm->wdog_cap_id = cap_id;
  222. else
  223. vm->wdog_cap_id = GH_CAPID_INVAL;
  224. unlock:
  225. mutex_unlock(&gh_vm_mutex);
  226. return ret;
  227. }
  228. /*
  229. * Called when vm_status is STATUS_READY, multiple times before status
  230. * moves to STATUS_RUNNING
  231. */
  232. static int gh_populate_vm_vcpu_info(gh_vmid_t vmid, gh_label_t cpu_idx,
  233. gh_capid_t cap_id, int virq_num)
  234. {
  235. struct gh_proxy_vm *vm;
  236. int ret = 0;
  237. char *vcpu_irq_name;
  238. if (!init_done) {
  239. pr_err("Driver probe failed\n");
  240. ret = -ENXIO;
  241. goto out;
  242. }
  243. if (!is_vm_supports_proxy(vmid)) {
  244. pr_info("Skip populating VCPU affinity info for VM=%d\n", vmid);
  245. goto out;
  246. }
  247. if (nr_vcpus >= GH_MAX_SYSTEM_VCPUS) {
  248. pr_err("Exceeded max vcpus in the system %d\n", nr_vcpus);
  249. ret = -ENXIO;
  250. goto out;
  251. }
  252. if (!virq_num || virq_num == U32_MAX) {
  253. pr_err("Invalid VIRQ, proxy scheduling isn't supported\n");
  254. goto out;
  255. }
  256. mutex_lock(&gh_vm_mutex);
  257. vm = gh_get_vm(vmid);
  258. if (vm && !vm->is_vcpu_info_populated) {
  259. if (vm->vcpu_count >= GH_MAX_VCPUS_PER_VM) {
  260. pr_err("Exceeded max vcpus per VM %d\n", vm->vcpu_count);
  261. ret = -ENXIO;
  262. goto unlock;
  263. }
  264. strscpy(vm->vcpu[vm->vcpu_count].irq_name, "gh_vcpu_irq",
  265. sizeof(vm->vcpu[vm->vcpu_count].irq_name));
  266. gh_get_vcpu_prop_name(vmid, vm->vcpu_count,
  267. vm->vcpu[vm->vcpu_count].irq_name);
  268. ret = request_irq(virq_num, gh_vcpu_irq_handler, 0,
  269. vm->vcpu[vm->vcpu_count].irq_name,
  270. &vm->vcpu[vm->vcpu_count]);
  271. if (ret < 0) {
  272. pr_err("%s: IRQ registration failed ret=%d\n", __func__, ret);
  273. goto err_irq;
  274. }
  275. irq_set_irq_wake(virq_num, 1);
  276. strscpy(vm->vcpu[vm->vcpu_count].ws_name, "gh_vcpu_ws",
  277. sizeof(vm->vcpu[vm->vcpu_count].ws_name));
  278. gh_get_vcpu_prop_name(vmid, vm->vcpu_count,
  279. vm->vcpu[vm->vcpu_count].ws_name);
  280. vm->vcpu[vm->vcpu_count].ws = wakeup_source_register(NULL,
  281. vm->vcpu[vm->vcpu_count].ws_name);
  282. if (!vm->vcpu[vm->vcpu_count].ws) {
  283. pr_err("%s: Wakeup source creation failed\n", __func__);
  284. goto err_ws;
  285. }
  286. vm->id = vmid;
  287. vm->vcpu[vm->vcpu_count].cap_id = cap_id;
  288. vm->vcpu[vm->vcpu_count].virq = virq_num;
  289. vm->vcpu[vm->vcpu_count].idx = cpu_idx;
  290. vm->vcpu[vm->vcpu_count].vm = vm;
  291. vcpu_irq_name = vm->vcpu[vm->vcpu_count].irq_name;
  292. vm->vcpu_count++;
  293. nr_vcpus++;
  294. pr_info("vmid=%d cpu_index:%u vcpu_cap_id:%llu virq_num=%d irq_name=%s nr_vcpus:%d\n",
  295. vmid, cpu_idx, cap_id, virq_num, vcpu_irq_name, nr_vcpus);
  296. }
  297. goto unlock;
  298. err_ws:
  299. strscpy(vm->vcpu[vm->vcpu_count].ws_name, "",
  300. sizeof(vm->vcpu[vm->vcpu_count].ws_name));
  301. free_irq(virq_num, &vm->vcpu[vm->vcpu_count]);
  302. err_irq:
  303. strscpy(vm->vcpu[vm->vcpu_count].irq_name, "",
  304. sizeof(vm->vcpu[vm->vcpu_count].irq_name));
  305. unlock:
  306. mutex_unlock(&gh_vm_mutex);
  307. out:
  308. return ret;
  309. }
  310. static int gh_unpopulate_vm_vcpu_info(gh_vmid_t vmid, gh_label_t cpu_idx,
  311. gh_capid_t cap_id, int *irq)
  312. {
  313. struct gh_proxy_vm *vm;
  314. struct gh_proxy_vcpu *vcpu;
  315. if (!init_done) {
  316. pr_err("Driver probe failed\n");
  317. return -ENXIO;
  318. }
  319. if (!is_vm_supports_proxy(vmid)) {
  320. pr_info("Skip unpopulating VCPU affinity info for VM=%d\n", vmid);
  321. goto out;
  322. }
  323. mutex_lock(&gh_vm_mutex);
  324. vm = gh_get_vm(vmid);
  325. if (vm && vm->is_vcpu_info_populated) {
  326. vcpu = gh_get_vcpu(vm, cap_id);
  327. if (vcpu) {
  328. *irq = vcpu->virq;
  329. free_irq(vcpu->virq, vcpu);
  330. vcpu->virq = U32_MAX;
  331. wakeup_source_unregister(vcpu->ws);
  332. if (nr_vcpus)
  333. nr_vcpus--;
  334. }
  335. }
  336. mutex_unlock(&gh_vm_mutex);
  337. out:
  338. return 0;
  339. }
  340. static inline void gh_get_vpmg_cap_id(int irq, gh_capid_t *vpmg_cap_id)
  341. {
  342. int i;
  343. struct gh_proxy_vm *vm;
  344. for (i = 0; i < GH_MAX_VMS; i++) {
  345. vm = &gh_vms[i];
  346. if (vm->susp_res_irq == irq)
  347. *vpmg_cap_id = vm->vpmg_cap_id;
  348. }
  349. }
  350. static irqreturn_t gh_susp_res_irq_handler(int irq, void *data)
  351. {
  352. int err;
  353. uint64_t vpmg_state;
  354. gh_capid_t vpmg_cap_id;
  355. gh_get_vpmg_cap_id(irq, &vpmg_cap_id);
  356. err = gh_hcall_vpm_group_get_state(vpmg_cap_id, &vpmg_state);
  357. if (err != GH_ERROR_OK) {
  358. pr_err("Failed to get VPM Group state for cap_id=%llu err=%d\n",
  359. vpmg_cap_id, err);
  360. return IRQ_HANDLED;
  361. }
  362. if (vpmg_state == SVM_STATE_RUNNING)
  363. pr_debug("SVM is in running state\n");
  364. else if (vpmg_state == SVM_STATE_SYSTEM_SUSPENDED)
  365. pr_debug("SVM is in system suspend state\n");
  366. else
  367. pr_err("VPM Group state invalid/non-existent\n");
  368. trace_gh_susp_res_irq_handler(vpmg_state);
  369. return IRQ_HANDLED;
  370. }
  371. static int gh_populate_vm_vpm_grp_info(gh_vmid_t vmid, gh_capid_t cap_id, int virq_num)
  372. {
  373. int ret = 0;
  374. struct gh_proxy_vm *vm;
  375. if (!init_done) {
  376. pr_err("%s: Driver probe failed\n", __func__);
  377. ret = -ENXIO;
  378. goto out;
  379. }
  380. if (!is_vm_supports_proxy(vmid)) {
  381. pr_info("Skip populating VPM GRP info for VM=%d\n", vmid);
  382. goto out;
  383. }
  384. if (virq_num < 0) {
  385. pr_err("%s: Invalid IRQ number\n", __func__);
  386. ret = -EINVAL;
  387. goto out;
  388. }
  389. mutex_lock(&gh_vm_mutex);
  390. vm = gh_get_vm(vmid);
  391. if (vm && !vm->is_vpm_group_info_populated) {
  392. ret = request_irq(virq_num, gh_susp_res_irq_handler, 0,
  393. "gh_susp_res_irq", NULL);
  394. if (ret < 0) {
  395. pr_err("%s: IRQ registration failed ret=%d\n", __func__, ret);
  396. goto unlock;
  397. }
  398. vm->vpmg_cap_id = cap_id;
  399. vm->susp_res_irq = virq_num;
  400. vm->is_vpm_group_info_populated = true;
  401. }
  402. unlock:
  403. mutex_unlock(&gh_vm_mutex);
  404. out:
  405. return ret;
  406. }
  407. static int gh_unpopulate_vm_vpm_grp_info(gh_vmid_t vmid, int *irq)
  408. {
  409. struct gh_proxy_vm *vm;
  410. if (!init_done) {
  411. pr_err("%s: Driver probe failed\n", __func__);
  412. return -ENXIO;
  413. }
  414. if (!is_vm_supports_proxy(vmid)) {
  415. pr_info("Skip unpopulating VPM GRP info for VM=%d\n", vmid);
  416. goto out;
  417. }
  418. mutex_lock(&gh_vm_mutex);
  419. vm = gh_get_vm(vmid);
  420. if (vm && vm->is_vpm_group_info_populated) {
  421. *irq = vm->susp_res_irq;
  422. free_irq(vm->susp_res_irq, NULL);
  423. vm->susp_res_irq = U32_MAX;
  424. vm->is_vpm_group_info_populated = false;
  425. }
  426. mutex_unlock(&gh_vm_mutex);
  427. out:
  428. return 0;
  429. }
  430. static void gh_populate_all_res_info(gh_vmid_t vmid, bool res_populated)
  431. {
  432. struct gh_proxy_vm *vm;
  433. char workqueue_name[24];
  434. if (!init_done) {
  435. pr_err("%s: Driver probe failed\n", __func__);
  436. return;
  437. }
  438. if (!is_vm_supports_proxy(vmid)) {
  439. pr_info("Proxy Scheduling isn't supported for VM=%d\n", vmid);
  440. return;
  441. }
  442. if (nr_vms >= GH_MAX_VMS) {
  443. pr_err("Exceeded max VMs in the system %d\n", nr_vms);
  444. return;
  445. }
  446. mutex_lock(&gh_vm_mutex);
  447. vm = gh_get_vm(vmid);
  448. if (!vm)
  449. goto unlock;
  450. if (res_populated && !vm->is_vcpu_info_populated) {
  451. gh_init_wait_queues(vm);
  452. snprintf(workqueue_name, sizeof(workqueue_name), "vm%d_vcpu_wq",
  453. vm->id);
  454. vm->vcpu_wq = create_freezable_workqueue(workqueue_name);
  455. nr_vms++;
  456. vm->is_vcpu_info_populated = true;
  457. vm->is_active = true;
  458. } else if (!res_populated && vm->is_vcpu_info_populated) {
  459. gh_reset_vm(vm);
  460. if (nr_vms)
  461. nr_vms--;
  462. }
  463. unlock:
  464. mutex_unlock(&gh_vm_mutex);
  465. }
  466. int gh_get_nr_vcpus(gh_vmid_t vmid)
  467. {
  468. struct gh_proxy_vm *vm;
  469. vm = gh_get_vm(vmid);
  470. if (vm && vm->is_vcpu_info_populated)
  471. return vm->vcpu_count;
  472. return 0;
  473. }
  474. /* Gets called from VM EXIT notification */
  475. void gh_wakeup_all_vcpus(gh_vmid_t vmid)
  476. {
  477. struct gh_proxy_vm *vm;
  478. int i;
  479. vm = gh_get_vm(vmid);
  480. if (vm && vm->is_active) {
  481. vm->is_active = false;
  482. for (i = 0; i < vm->vcpu_count; i++)
  483. gh_vcpu_wake_up(&vm->vcpu[i]);
  484. }
  485. }
  486. bool gh_vm_supports_proxy_sched(gh_vmid_t vmid)
  487. {
  488. struct gh_proxy_vm *vm;
  489. vm = gh_get_vm(vmid);
  490. if (vm && vm->is_vcpu_info_populated && vm->vcpu_count)
  491. return true;
  492. return false;
  493. }
  494. int gh_poll_vcpu_run(gh_vmid_t vmid)
  495. {
  496. struct gh_hcall_vcpu_run_resp resp;
  497. struct gh_proxy_vcpu *vcpu;
  498. struct gh_proxy_vm *vm;
  499. unsigned int vcpu_id;
  500. int poll_nr_vcpus;
  501. ktime_t start_ts, yield_ts;
  502. int ret = -EPERM;
  503. vm = gh_get_vm(vmid);
  504. if (!vm || !vm->is_active)
  505. return ret;
  506. poll_nr_vcpus = gh_get_nr_vcpus(vmid);
  507. if (poll_nr_vcpus < 0) {
  508. printk_deferred("Failed to get vcpu count for VM %d ret %d\n",
  509. vmid, nr_vcpus);
  510. ret = poll_nr_vcpus;
  511. return ret;
  512. }
  513. for (vcpu_id = 0; vcpu_id < poll_nr_vcpus; vcpu_id++) {
  514. if (vm->vcpu[vcpu_id].cap_id == GH_CAPID_INVAL)
  515. return -EPERM;
  516. vcpu = &vm->vcpu[vcpu_id];
  517. do {
  518. start_ts = ktime_get();
  519. ret = gh_hcall_vcpu_run(vcpu->cap_id, 0, 0, 0, &resp);
  520. yield_ts = ktime_get() - start_ts;
  521. trace_gh_hcall_vcpu_run(ret, vcpu->vm->id, vcpu_id, yield_ts,
  522. resp.vcpu_state, resp.vcpu_suspend_state);
  523. if (ret == GH_ERROR_OK) {
  524. if (resp.vcpu_state > GH_VCPU_STATE_BLOCKED)
  525. printk_deferred("Unknown VCPU STATE: state=%d VCPU=%u of VM=%d\n",
  526. resp.vcpu_state, vcpu_id, vmid);
  527. break;
  528. }
  529. } while (ret == GH_ERROR_RETRY);
  530. }
  531. return ret;
  532. }
  533. EXPORT_SYMBOL(gh_poll_vcpu_run);
  534. void gh_vcpu_work_function(struct work_struct *work)
  535. {
  536. struct gh_proxy_vcpu *vcpu =
  537. container_of(work, struct gh_proxy_vcpu, work);
  538. struct gh_proxy_vm *vm = vcpu->vm;
  539. uint64_t resume_data_0 = 0, resume_data_1 = 0, resume_data_2 = 0;
  540. struct gh_hcall_vcpu_run_resp resp;
  541. ktime_t start_ts, yield_ts;
  542. int ret;
  543. vcpu->abort_sleep = false;
  544. __pm_stay_awake(vcpu->ws);
  545. start_ts = ktime_get();
  546. preempt_disable();
  547. if (vcpu->wdog_frozen) {
  548. gh_hcall_wdog_manage(vm->wdog_cap_id,
  549. WATCHDOG_MANAGE_OP_UNFREEZE);
  550. vcpu->wdog_frozen = false;
  551. }
  552. ret = gh_hcall_vcpu_run(vcpu->cap_id, resume_data_0,
  553. resume_data_1, resume_data_2, &resp);
  554. if (ret == GH_ERROR_OK && resp.vcpu_state == GH_VCPU_STATE_READY) {
  555. gh_hcall_wdog_manage(vm->wdog_cap_id,
  556. WATCHDOG_MANAGE_OP_FREEZE);
  557. vcpu->wdog_frozen = true;
  558. }
  559. preempt_enable();
  560. yield_ts = ktime_get() - start_ts;
  561. trace_gh_hcall_vcpu_run(ret, vcpu->vm->id, vcpu->idx, yield_ts,
  562. resp.vcpu_state,
  563. resp.vcpu_suspend_state);
  564. if (ret == GH_ERROR_OK) {
  565. switch (resp.vcpu_state) {
  566. /* VCPU is preempted by PVM interrupt. */
  567. case GH_VCPU_STATE_READY:
  568. queue_work(vm->vcpu_wq, &vcpu->work);
  569. break;
  570. /* VCPU in WFI or suspended/powered down. */
  571. case GH_VCPU_STATE_EXPECTS_WAKEUP:
  572. if (resp.vcpu_suspend_state)
  573. __pm_relax(vcpu->ws);
  574. if (!vcpu->abort_sleep) {
  575. return;
  576. }
  577. break;
  578. case GH_VCPU_STATE_POWERED_OFF:
  579. __pm_relax(vcpu->ws);
  580. /* once cpu is powered off, the work is done */
  581. if (!vcpu->abort_sleep)
  582. return;
  583. break;
  584. /* VCPU is blocked in EL2 for an unspecified reason */
  585. case GH_VCPU_STATE_BLOCKED:
  586. queue_work(vm->vcpu_wq, &vcpu->work);
  587. break;
  588. }
  589. }
  590. }
  591. int gh_vcpu_pm_notifier_call(struct notifier_block *nb, unsigned long action,
  592. void *data)
  593. {
  594. struct gh_proxy_vcpu *vcpu =
  595. container_of(nb, struct gh_proxy_vcpu, suspend_nb);
  596. struct gh_proxy_vm *vm = vcpu->vm;
  597. if (action == PM_SUSPEND_PREPARE) {
  598. if (!vcpu->wdog_frozen) {
  599. gh_hcall_wdog_manage(vm->wdog_cap_id,
  600. WATCHDOG_MANAGE_OP_FREEZE);
  601. vcpu->wdog_frozen = true;
  602. }
  603. } else if (action == PM_POST_SUSPEND) {
  604. queue_work(vm->vcpu_wq, &vcpu->work);
  605. }
  606. return NOTIFY_OK;
  607. }
  608. int gh_vcpu_create_wq(gh_vmid_t vmid, unsigned int vcpu_id)
  609. {
  610. struct gh_proxy_vm *vm;
  611. struct gh_proxy_vcpu *vcpu;
  612. vm = gh_get_vm(vmid);
  613. if (!vm || !vm->is_active)
  614. return -EINVAL;
  615. if (vm->vcpu[vcpu_id].cap_id == GH_CAPID_INVAL)
  616. return -EINVAL;
  617. vcpu = &vm->vcpu[vcpu_id];
  618. INIT_WORK(&vcpu->work, gh_vcpu_work_function);
  619. vcpu->workqueue_mode = true;
  620. vcpu->suspend_nb.notifier_call = gh_vcpu_pm_notifier_call;
  621. register_pm_notifier(&vcpu->suspend_nb);
  622. /* schedule once incase we miss any interrupt */
  623. schedule_work(&vcpu->work);
  624. queue_work(vm->vcpu_wq, &vcpu->work);
  625. return 0;
  626. }
  627. int gh_vcpu_run(gh_vmid_t vmid, unsigned int vcpu_id, uint64_t resume_data_0,
  628. uint64_t resume_data_1, uint64_t resume_data_2, struct gh_hcall_vcpu_run_resp *resp)
  629. {
  630. struct gh_proxy_vcpu *vcpu;
  631. struct gh_proxy_vm *vm;
  632. int ret;
  633. ktime_t start_ts, yield_ts;
  634. vm = gh_get_vm(vmid);
  635. if (!vm || !vm->is_active)
  636. return -EPERM;
  637. if (vm->vcpu[vcpu_id].cap_id == GH_CAPID_INVAL)
  638. return -EPERM;
  639. vcpu = &vm->vcpu[vcpu_id];
  640. do {
  641. /*
  642. * We're about to run the vcpu, so we can reset the abort-sleep flag.
  643. */
  644. vcpu->abort_sleep = false;
  645. __pm_stay_awake(vcpu->ws);
  646. start_ts = ktime_get();
  647. /* Call into Gunyah to run vcpu. */
  648. preempt_disable();
  649. if (vcpu->wdog_frozen) {
  650. gh_hcall_wdog_manage(vm->wdog_cap_id, WATCHDOG_MANAGE_OP_UNFREEZE);
  651. vcpu->wdog_frozen = false;
  652. }
  653. ret = gh_hcall_vcpu_run(vcpu->cap_id, resume_data_0,
  654. resume_data_1, resume_data_2, resp);
  655. if (ret == GH_ERROR_OK && resp->vcpu_state == GH_VCPU_STATE_READY) {
  656. if (need_resched()) {
  657. gh_hcall_wdog_manage(vm->wdog_cap_id,
  658. WATCHDOG_MANAGE_OP_FREEZE);
  659. vcpu->wdog_frozen = true;
  660. }
  661. }
  662. preempt_enable();
  663. yield_ts = ktime_get() - start_ts;
  664. trace_gh_hcall_vcpu_run(ret, vcpu->vm->id, vcpu_id, yield_ts,
  665. resp->vcpu_state, resp->vcpu_suspend_state);
  666. if (ret == GH_ERROR_OK) {
  667. switch (resp->vcpu_state) {
  668. /*
  669. * The caller's hypervisor timeslice ended, or the caller received an interrupt.
  670. * The caller should retry after handling any pending interrupts.
  671. */
  672. case GH_VCPU_STATE_READY:
  673. if (need_resched())
  674. schedule();
  675. break;
  676. /*
  677. * The VCPU is waiting to receive an interrupt; for example, it may have executed a WFI instruction,
  678. * or made a firmware call requesting entry into a low-power state.
  679. */
  680. case GH_VCPU_STATE_EXPECTS_WAKEUP:
  681. /*
  682. * VCPU requested a firmware call requesting
  683. * entry into a low-power state.
  684. * Release wake lock for non C1 states
  685. */
  686. if (resp->vcpu_suspend_state)
  687. __pm_relax(vcpu->ws);
  688. gh_vcpu_sleep(vcpu);
  689. break;
  690. /*
  691. * The VCPU has not yet been started by calling vcpu_poweron, or has stopped itself by calling vcpu_poweroff,
  692. * or has been terminated due to a reset request from another VM.
  693. */
  694. case GH_VCPU_STATE_POWERED_OFF:
  695. __pm_relax(vcpu->ws);
  696. gh_vcpu_sleep(vcpu);
  697. break;
  698. /*
  699. * The VCPU is temporarily unable to run due to a hypervisor operation.
  700. * This may include a hypercall made by the VCPU that transiently blocks it,
  701. * or by an incomplete migration from another physical CPU. The caller should
  702. * retry after yielding to the calling VM's scheduler.
  703. */
  704. case GH_VCPU_STATE_BLOCKED:
  705. schedule();
  706. break;
  707. /* Unknown VCPU state. */
  708. default:
  709. pr_err("Unknown VCPU STATE: state=%d VCPU=%u of VM=%d state_data_0=0x%llx state_data_1=0x%llx state_data_2=0x%llx\n",
  710. resp->vcpu_state, vcpu_id, vcpu->vm->id,
  711. resp->state_data_0, resp->state_data_1, resp->state_data_2);
  712. schedule();
  713. break;
  714. }
  715. } else if (ret == GH_ERROR_RETRY) {
  716. schedule();
  717. }
  718. if (signal_pending(current)) {
  719. if (!vcpu->wdog_frozen) {
  720. gh_hcall_wdog_manage(vm->wdog_cap_id,
  721. WATCHDOG_MANAGE_OP_FREEZE);
  722. vcpu->wdog_frozen = true;
  723. }
  724. ret = -ERESTARTSYS;
  725. break;
  726. }
  727. } while ((ret == GH_ERROR_OK || ret == GH_ERROR_RETRY) && vm->is_active);
  728. if (ret != -ERESTARTSYS)
  729. ret = gh_error_remap(ret);
  730. return ret;
  731. }
  732. static int gh_proxy_sched_reg_rm_cbs(void)
  733. {
  734. int ret = -EINVAL;
  735. ret = gh_rm_set_wdog_manage_cb(&gh_wdog_manage);
  736. if (ret) {
  737. pr_err("fail to set the WDOG resource callback\n");
  738. return ret;
  739. }
  740. ret = gh_rm_set_vcpu_affinity_cb(&gh_populate_vm_vcpu_info);
  741. if (ret) {
  742. pr_err("fail to set the VM VCPU populate callback\n");
  743. return ret;
  744. }
  745. ret = gh_rm_reset_vcpu_affinity_cb(&gh_unpopulate_vm_vcpu_info);
  746. if (ret) {
  747. pr_err("fail to set the VM VCPU unpopulate callback\n");
  748. return ret;
  749. }
  750. ret = gh_rm_set_vpm_grp_cb(&gh_populate_vm_vpm_grp_info);
  751. if (ret) {
  752. pr_err("fail to set the VM VPM GRP populate callback\n");
  753. return ret;
  754. }
  755. ret = gh_rm_reset_vpm_grp_cb(&gh_unpopulate_vm_vpm_grp_info);
  756. if (ret) {
  757. pr_err("fail to set the VM VPM GRP unpopulate callback\n");
  758. return ret;
  759. }
  760. ret = gh_rm_all_res_populated_cb(&gh_populate_all_res_info);
  761. if (ret) {
  762. pr_err("fail to set the all res populate callback\n");
  763. return ret;
  764. }
  765. return 0;
  766. }
  767. int gh_proxy_sched_init(void)
  768. {
  769. int ret;
  770. gh_vms = kcalloc(GH_MAX_VMS, sizeof(struct gh_proxy_vm), GFP_KERNEL);
  771. if (!gh_vms) {
  772. ret = -ENOMEM;
  773. goto err;
  774. }
  775. ret = gh_proxy_sched_reg_rm_cbs();
  776. if (ret)
  777. goto free_gh_vms;
  778. gh_init_vms();
  779. init_done = true;
  780. return 0;
  781. free_gh_vms:
  782. kfree(gh_vms);
  783. err:
  784. return ret;
  785. }
  786. void gh_proxy_sched_exit(void)
  787. {
  788. kfree(gh_vms);
  789. }