Skip to content

Commit e8296dc

Browse files
Shuo Liulijinxia
authored andcommitted
hv: Add IO request completion polling feature
This patch introduce a new mode of IO request completion, polling mode. Now, the sketch of ioreq process can be, A. UOS vcpu0 generate PIO/MMIO -> B. pcpu1(vcpu0 of UOS) trap into HV -> C. pcpu1 build ioreq, send IPI and enter idle -> D.1 pcpu0(vcpu0 of SOS) response IPI, D.2 pcpu0 handle the ioreq in HV, kernel, DM, D.3 pcpu0 mark ioreq as complete, D.4 pcpu0 hypercall to enter HV -> E.1 pcpu0 send IPI to wake pcpu1 up E.2 UOS vcpu0 continue running With this change, it skips D.4, E.1 steps. In step C, pcpu1 will enter a polling ioreq state idle after send out the IPI. It can save about ~5000 cpu cycles. In polling mode, we do the polling in idle instead of pause cpu all the time. It will consume more power. A better way is to use monitor/mwait instructions which can put cpu into a sleep state with monitoring a memory address. Unfortunately, APL has bug with monitor. We can gather all ioreqs state into one monitorable memory and take advantage of monitor/mwait for future platform. The way polling or notification is per VM. We can config VMs in different mode. By default, IO request completion will use notification mode for all VMs. We can switch it by Kconfig. Tracked-On: #1821 Signed-off-by: Shuo Liu <shuo.a.liu@intel.com> Reviewed-by: Eddie Dong <eddie.dong@intel.com> Acked-by: Anthony Xu <anthony.xu@intel.com>
1 parent e350abe commit e8296dc

File tree

8 files changed

+103
-4
lines changed

8 files changed

+103
-4
lines changed

devicemodel/include/public/acrn_common.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,18 @@ struct vhm_request {
173173
uint32_t type;
174174

175175
/**
176-
* @brief Reserved.
176+
* @brief Hypervisor will poll completion if set.
177177
*
178178
* Byte offset: 4.
179179
*/
180-
uint32_t reserved0[15];
180+
uint32_t completion_polling;
181+
182+
/**
183+
* @brief Reserved.
184+
*
185+
* Byte offset: 8.
186+
*/
187+
uint32_t reserved0[14];
181188

182189
/**
183190
* @brief Details about this request.

hypervisor/arch/x86/Kconfig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ config PARTITION_MODE
4040

4141
endchoice
4242

43+
choice
44+
prompt "I/O emulation completion mode"
45+
default IOREQ_NOTIFICATION
46+
help
47+
Select the mode of I/O emulation completion
48+
49+
config IOREQ_NOTIFICATION
50+
bool "Notification mode"
51+
help
52+
When I/O request is completed, SOS will mark the completion status and
53+
notify hypervisor via hypercall. Hypervisor will finish the post work
54+
when notification is received.
55+
56+
config IOREQ_POLLING
57+
bool "Polling mode"
58+
help
59+
When I/O request is completed, SOS will only mark completion status
60+
without notifying hypervisor. Hypervisor will poll the completion
61+
status and finish the post work.
62+
63+
endchoice
64+
4365
config PLATFORM
4466
string
4567
default "uefi" if PLATFORM_UEFI

hypervisor/arch/x86/guest/vm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ int create_vm(struct vm_description *vm_desc, struct acrn_vm **rtn_vm)
162162
/* Populate return VM handle */
163163
*rtn_vm = vm;
164164
vm->sw.io_shared_page = NULL;
165+
#ifdef CONFIG_IOREQ_POLLING
166+
/* Now, enable IO completion polling mode for all VMs with CONFIG_IOREQ_POLLING. */
167+
vm->sw.is_completion_polling = true;
168+
#endif
165169

166170
status = set_vcpuid_entries(vm);
167171
if (status != 0) {

hypervisor/common/io_request.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,50 @@ void reset_vm_ioreqs(struct acrn_vm *vm)
7474
}
7575
}
7676

77+
static bool has_complete_ioreq(struct acrn_vcpu *vcpu)
78+
{
79+
union vhm_request_buffer *req_buf = NULL;
80+
struct vhm_request *vhm_req;
81+
struct acrn_vm *vm;
82+
83+
vm = vcpu->vm;
84+
req_buf = (union vhm_request_buffer *)vm->sw.io_shared_page;
85+
if (req_buf != NULL) {
86+
vhm_req = &req_buf->req_queue[vcpu->vcpu_id];
87+
if (vhm_req->valid &&
88+
atomic_load32(&vhm_req->processed)
89+
== REQ_STATE_COMPLETE) {
90+
return true;
91+
92+
}
93+
}
94+
95+
return false;
96+
}
97+
98+
/**
99+
* @brief Handle completed ioreq if any one pending
100+
*
101+
* @param pcpu_id The physical cpu id of vcpu whose IO request to be checked
102+
*
103+
* @return N/A
104+
*/
105+
void handle_complete_ioreq(uint16_t pcpu_id)
106+
{
107+
struct acrn_vcpu *vcpu = get_ever_run_vcpu(pcpu_id);
108+
struct acrn_vm *vm;
109+
110+
if (vcpu != NULL) {
111+
vm = vcpu->vm;
112+
if (vm->sw.is_completion_polling) {
113+
if (has_complete_ioreq(vcpu)) {
114+
/* we have completed ioreq pending */
115+
emulate_io_post(vcpu);
116+
}
117+
}
118+
}
119+
}
120+
77121
/**
78122
* @brief Deliver \p io_req to SOS and suspend \p vcpu till its completion
79123
*
@@ -103,6 +147,9 @@ int32_t acrn_insert_request_wait(struct acrn_vcpu *vcpu, const struct io_request
103147
vhm_req->type = io_req->type;
104148
(void)memcpy_s(&vhm_req->reqs, sizeof(union vhm_io_request),
105149
&io_req->reqs, sizeof(union vhm_io_request));
150+
if (vcpu->vm->sw.is_completion_polling) {
151+
vhm_req->completion_polling = 1U;
152+
}
106153

107154
/* pause vcpu, wait for VHM to handle the MMIO request.
108155
* TODO: when pause_vcpu changed to switch vcpu out directlly, we

hypervisor/common/schedule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ void default_idle(void)
178178
cpu_dead(pcpu_id);
179179
} else {
180180
CPU_IRQ_ENABLE();
181+
handle_complete_ioreq(pcpu_id);
181182
cpu_do_idle();
182183
CPU_IRQ_DISABLE();
183184
}

hypervisor/include/arch/x86/guest/vm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ struct vm_sw_info {
5555
struct sw_linux linux_info;
5656
/* HVA to IO shared page */
5757
void *io_shared_page;
58+
/* If enable IO completion polling mode */
59+
bool is_completion_polling;
5860
};
5961

6062
struct vm_pm_info {

hypervisor/include/arch/x86/ioreq.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,15 @@ int32_t acrn_insert_request_wait(struct acrn_vcpu *vcpu, const struct io_request
296296
*/
297297
void reset_vm_ioreqs(struct acrn_vm *vm);
298298

299+
/**
300+
* @brief Handle completed ioreq if any one pending
301+
*
302+
* @param pcpu_id The physical cpu id of vcpu whose IO request to be checked
303+
*
304+
* @return N/A
305+
*/
306+
void handle_complete_ioreq(uint16_t pcpu_id);
307+
299308
/**
300309
* @}
301310
*/

hypervisor/include/public/acrn_common.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,18 @@ struct vhm_request {
268268
uint32_t type;
269269

270270
/**
271-
* Reserved.
271+
* @brief Hypervisor will poll completion if set.
272272
*
273273
* Byte offset: 4.
274274
*/
275-
uint32_t reserved0[15];
275+
uint32_t completion_polling;
276+
277+
/**
278+
* Reserved.
279+
*
280+
* Byte offset: 8.
281+
*/
282+
uint32_t reserved0[14];
276283

277284
/**
278285
* Details about this request. For REQ_PORTIO, this has type

0 commit comments

Comments
 (0)