1 /*
2  * Copyright (C) 2020-2023 Intel Corporation.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <errno.h>
8 #include <util.h>
9 #include <acrn_hv_defs.h>
10 #include <asm/guest/vm.h>
11 #include <vm_event.h>
12 #include <sbuf.h>
13 
init_vm_event(struct acrn_vm * vm,uint64_t * hva)14 int32_t init_vm_event(struct acrn_vm *vm, uint64_t *hva)
15 {
16 	struct shared_buf *sbuf = (struct shared_buf *)hva;
17 	int ret = -1;
18 
19 	stac();
20 	if (sbuf != NULL) {
21 		if (sbuf->magic == SBUF_MAGIC) {
22 			vm->sw.vm_event_sbuf = sbuf;
23 			spinlock_init(&vm->vm_event_lock);
24 			ret = 0;
25 		}
26 	}
27 	clac();
28 
29 	return ret;
30 }
31 
send_vm_event(struct acrn_vm * vm,struct vm_event * event)32 int32_t send_vm_event(struct acrn_vm *vm, struct vm_event *event)
33 {
34 	struct shared_buf *sbuf = (struct shared_buf *)vm->sw.vm_event_sbuf;
35 	int32_t ret = -ENODEV;
36 	uint32_t size_sent;
37 
38 	if (sbuf != NULL) {
39 		spinlock_obtain(&vm->vm_event_lock);
40 		size_sent = sbuf_put(sbuf, (uint8_t *)event, sizeof(*event));
41 		spinlock_release(&vm->vm_event_lock);
42 		if (size_sent == sizeof(struct vm_event)) {
43 			arch_fire_hsm_interrupt();
44 			ret = 0;
45 		}
46 	}
47 	return ret;
48 }
49