From: Liju-clr Chen <liju-clr.chen@mediatek.com>
To: Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Jonathan Corbet <corbet@lwn.net>,
"Catalin Marinas" <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
"Steven Rostedt" <rostedt@goodmis.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Richard Cochran <richardcochran@gmail.com>,
Matthias Brugger <matthias.bgg@gmail.com>,
AngeloGioacchino Del Regno
<angelogioacchino.delregno@collabora.com>,
Liju-clr Chen <Liju-clr.Chen@mediatek.com>,
Yingshiuan Pan <Yingshiuan.Pan@mediatek.com>,
Ze-yu Wang <Ze-yu.Wang@mediatek.com>
Cc: <devicetree@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<linux-doc@vger.kernel.org>,
<linux-arm-kernel@lists.infradead.org>,
<linux-trace-kernel@vger.kernel.org>, <netdev@vger.kernel.org>,
<linux-mediatek@lists.infradead.org>,
Shawn Hsiao <shawn.hsiao@mediatek.com>,
PeiLun Suei <PeiLun.Suei@mediatek.com>,
Chi-shen Yeh <Chi-shen.Yeh@mediatek.com>,
Kevenny Hsieh <Kevenny.Hsieh@mediatek.com>
Subject: [PATCH v12 23/24] virt: geniezone: Add support for guest VM CPU idle
Date: Tue, 30 Jul 2024 16:24:35 +0800 [thread overview]
Message-ID: <20240730082436.9151-24-liju-clr.chen@mediatek.com> (raw)
In-Reply-To: <20240730082436.9151-1-liju-clr.chen@mediatek.com>
From: Kevenny Hsieh <kevenny.hsieh@mediatek.com>
Implement CPU idle functionality for guest VMs, allowing the guest
VM's CPU to relinquish control to the host when it enters an idle
state. Enable the host to schedule other processes, thereby reducing
CPU usage and achieving power savings.
Introduce a new capability (`enable_idle_support`) to support the idle
feature. Emulate the WFI (Wait For Interrupt) instruction for the
guest VM when it enters an idle state. Allow the host Linux kernel to
schedule other processes and enable the vCPU to enter the `wait`
state. Ensure that the vCPU can be woken up by interrupts such as the
virtual timer (vtimer) and virtio interrupts.
With the idle feature, the vCPU can efficiently enter a lower power
state, such as decreasing CPU frequency or even entering an idle
state, thereby reducing CPU usage and improving overall system power
efficiency.
Signed-off-by: Kevenny Hsieh <kevenny.hsieh@mediatek.com>
Signed-off-by: Liju Chen <liju-clr.chen@mediatek.com>
---
arch/arm64/geniezone/vm.c | 3 ++
drivers/virt/geniezone/gzvm_exception.c | 46 +++++++++++++++++++++++++
drivers/virt/geniezone/gzvm_vcpu.c | 26 +++++++++++++-
drivers/virt/geniezone/gzvm_vm.c | 15 ++++++++
include/linux/soc/mediatek/gzvm_drv.h | 7 ++++
include/uapi/linux/gzvm.h | 2 ++
6 files changed, 98 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/geniezone/vm.c b/arch/arm64/geniezone/vm.c
index 549d8611c8d2..d9129080536e 100644
--- a/arch/arm64/geniezone/vm.c
+++ b/arch/arm64/geniezone/vm.c
@@ -411,6 +411,9 @@ int gzvm_vm_ioctl_arch_enable_cap(struct gzvm *gzvm,
case GZVM_CAP_BLOCK_BASED_DEMAND_PAGING:
ret = gzvm_vm_arch_enable_cap(gzvm, cap, &res);
return ret;
+ case GZVM_CAP_ENABLE_IDLE:
+ ret = gzvm_vm_arch_enable_cap(gzvm, cap, &res);
+ return ret;
default:
break;
}
diff --git a/drivers/virt/geniezone/gzvm_exception.c b/drivers/virt/geniezone/gzvm_exception.c
index 5ffee863e378..391168a3f737 100644
--- a/drivers/virt/geniezone/gzvm_exception.c
+++ b/drivers/virt/geniezone/gzvm_exception.c
@@ -5,6 +5,8 @@
#include <linux/device.h>
#include <linux/soc/mediatek/gzvm_drv.h>
+#include <linux/sched.h>
+#include <linux/rcuwait.h>
/**
* gzvm_handle_guest_exception() - Handle guest exception
@@ -59,3 +61,47 @@ bool gzvm_handle_guest_hvc(struct gzvm_vcpu *vcpu)
return gzvm_arch_handle_guest_hvc(vcpu);
}
}
+
+static void vcpu_block_wait(struct gzvm_vcpu *vcpu)
+{
+ struct rcuwait *wait = &vcpu->wait;
+
+ prepare_to_rcuwait(wait);
+
+ while (true) {
+ set_current_state(TASK_INTERRUPTIBLE);
+ if (vcpu->idle_events.virtio_irq) {
+ vcpu->idle_events.virtio_irq = 0;
+ break;
+ }
+ if (vcpu->idle_events.vtimer_irq) {
+ vcpu->idle_events.vtimer_irq = 0;
+ break;
+ }
+ if (signal_pending(current))
+ break;
+ schedule();
+ }
+ finish_rcuwait(wait);
+}
+
+/**
+ * gzvm_handle_guest_idle() - Handle guest vm entering idle
+ * @vcpu: Pointer to struct gzvm_vcpu struct
+ * Return:
+ */
+int gzvm_handle_guest_idle(struct gzvm_vcpu *vcpu)
+{
+ int ret = 0;
+ u64 ns = 0;
+
+ ns = gzvm_vcpu_arch_get_timer_delay_ns(vcpu);
+
+ if (ns) {
+ gzvm_vtimer_set(vcpu, ns);
+ vcpu_block_wait(vcpu);
+ gzvm_vtimer_release(vcpu);
+ }
+
+ return ret;
+}
diff --git a/drivers/virt/geniezone/gzvm_vcpu.c b/drivers/virt/geniezone/gzvm_vcpu.c
index ba85a6ae04a0..247848ee126c 100644
--- a/drivers/virt/geniezone/gzvm_vcpu.c
+++ b/drivers/virt/geniezone/gzvm_vcpu.c
@@ -16,8 +16,29 @@
/* maximum size needed for holding an integer */
#define ITOA_MAX_LEN 12
+/**
+ * gzvm_vcpu_wakeup_all - wakes up all vCPUs associated with the specified
+ * gzvm.
+ * @gzvm: Pointer to gzvm structure.
+ */
+void gzvm_vcpu_wakeup_all(struct gzvm *gzvm)
+{
+ for (int i = 0; i < GZVM_MAX_VCPUS; i++) {
+ if (gzvm->vcpus[i]) {
+ gzvm->vcpus[i]->idle_events.virtio_irq += 1;
+ rcuwait_wake_up(&gzvm->vcpus[i]->wait);
+ }
+ }
+}
+
static enum hrtimer_restart gzvm_vtimer_expire(struct hrtimer *hrt)
{
+ struct gzvm_vcpu *vcpu;
+
+ vcpu = container_of(hrt, struct gzvm_vcpu, gzvm_vtimer);
+
+ gzvm_vcpu_wakeup_all(vcpu->gzvm);
+
return HRTIMER_NORESTART;
}
@@ -111,7 +132,7 @@ static bool gzvm_vcpu_handle_mmio(struct gzvm_vcpu *vcpu)
static long gzvm_vcpu_run(struct gzvm_vcpu *vcpu, void __user *argp)
{
bool need_userspace = false;
- u64 exit_reason = 0;
+ u64 exit_reason;
if (copy_from_user(vcpu->run, argp, sizeof(struct gzvm_vcpu_run)))
return -EFAULT;
@@ -160,6 +181,9 @@ static long gzvm_vcpu_run(struct gzvm_vcpu *vcpu, void __user *argp)
fallthrough;
case GZVM_EXIT_GZ:
break;
+ case GZVM_EXIT_IDLE:
+ gzvm_handle_guest_idle(vcpu);
+ break;
case GZVM_EXIT_UNKNOWN:
fallthrough;
default:
diff --git a/drivers/virt/geniezone/gzvm_vm.c b/drivers/virt/geniezone/gzvm_vm.c
index db430e532602..7a5f97e8881c 100644
--- a/drivers/virt/geniezone/gzvm_vm.c
+++ b/drivers/virt/geniezone/gzvm_vm.c
@@ -172,6 +172,7 @@ gzvm_vm_ioctl_set_memory_region(struct gzvm *gzvm,
int gzvm_irqchip_inject_irq(struct gzvm *gzvm, unsigned int vcpu_idx,
u32 irq, bool level)
{
+ gzvm_vcpu_wakeup_all(gzvm);
return gzvm_arch_inject_irq(gzvm, vcpu_idx, irq, level);
}
@@ -556,6 +557,18 @@ static int setup_mem_alloc_mode(struct gzvm *vm)
return 0;
}
+static int enable_idle_support(struct gzvm *vm)
+{
+ int ret;
+ struct gzvm_enable_cap cap = {0};
+
+ cap.cap = GZVM_CAP_ENABLE_IDLE;
+ ret = gzvm_vm_ioctl_enable_cap(vm, &cap, NULL);
+ if (ret)
+ pr_info("Hypervisor doesn't support idle\n");
+ return ret;
+}
+
static struct gzvm *gzvm_create_vm(unsigned long vm_type)
{
int ret;
@@ -603,6 +616,8 @@ static struct gzvm *gzvm_create_vm(unsigned long vm_type)
pr_debug("VM-%u is created\n", gzvm->vm_id);
+ enable_idle_support(gzvm);
+
return gzvm;
}
diff --git a/include/linux/soc/mediatek/gzvm_drv.h b/include/linux/soc/mediatek/gzvm_drv.h
index 3c460281cdd4..61f3ae4ee793 100644
--- a/include/linux/soc/mediatek/gzvm_drv.h
+++ b/include/linux/soc/mediatek/gzvm_drv.h
@@ -108,6 +108,11 @@ struct gzvm_vcpu {
struct gzvm_vcpu_run *run;
struct gzvm_vcpu_hwstate *hwstate;
struct hrtimer gzvm_vtimer;
+ struct {
+ u32 vtimer_irq;
+ u32 virtio_irq;
+ } idle_events;
+ struct rcuwait wait;
};
struct gzvm_pinned_page {
@@ -235,6 +240,8 @@ bool gzvm_handle_guest_exception(struct gzvm_vcpu *vcpu);
int gzvm_handle_relinquish(struct gzvm_vcpu *vcpu, phys_addr_t ipa);
bool gzvm_handle_guest_hvc(struct gzvm_vcpu *vcpu);
bool gzvm_arch_handle_guest_hvc(struct gzvm_vcpu *vcpu);
+int gzvm_handle_guest_idle(struct gzvm_vcpu *vcpu);
+void gzvm_vcpu_wakeup_all(struct gzvm *gzvm);
int gzvm_arch_create_device(u16 vm_id, struct gzvm_create_device *gzvm_dev);
int gzvm_arch_inject_irq(struct gzvm *gzvm, unsigned int vcpu_idx,
diff --git a/include/uapi/linux/gzvm.h b/include/uapi/linux/gzvm.h
index 1cf89213a383..1fe483ef2ed5 100644
--- a/include/uapi/linux/gzvm.h
+++ b/include/uapi/linux/gzvm.h
@@ -21,6 +21,7 @@
/* query hypervisor supported block-based demand page */
#define GZVM_CAP_BLOCK_BASED_DEMAND_PAGING 0x9201
#define GZVM_CAP_ENABLE_DEMAND_PAGING 0x9202
+#define GZVM_CAP_ENABLE_IDLE 0x9203
/* sub-commands put in args[0] for GZVM_CAP_PROTECTED_VM */
#define GZVM_CAP_PVM_SET_PVMFW_GPA 0
@@ -187,6 +188,7 @@ enum {
GZVM_EXIT_SYSTEM_EVENT = 0x92920008,
GZVM_EXIT_SHUTDOWN = 0x92920009,
GZVM_EXIT_GZ = 0x9292000a,
+ GZVM_EXIT_IDLE = 0x9292000b,
};
/* exception definitions of GZVM_EXIT_EXCEPTION */
--
2.18.0
next prev parent reply other threads:[~2024-07-30 8:24 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-30 8:24 [PATCH v12 00/24] GenieZone hypervisor drivers Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 01/24] virt: geniezone: enable gzvm-ko in defconfig Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 02/24] docs: geniezone: Introduce GenieZone hypervisor Liju-clr Chen
2024-07-30 14:13 ` Randy Dunlap
2024-10-22 2:21 ` Liju-clr Chen (陳麗如)
2024-07-30 8:24 ` [PATCH v12 03/24] dt-bindings: hypervisor: Add MediaTek " Liju-clr Chen
2024-08-01 10:36 ` Hillf Danton
2024-10-22 2:47 ` Liju-clr Chen (陳麗如)
2024-07-30 8:24 ` [PATCH v12 04/24] virt: geniezone: Add GenieZone hypervisor driver Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 05/24] virt: geniezone: Add vm support Liju-clr Chen
2024-08-01 10:50 ` Hillf Danton
2024-10-22 3:10 ` Liju-clr Chen (陳麗如)
2024-07-30 8:24 ` [PATCH v12 06/24] virt: geniezone: Add set_user_memory_region for vm Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 07/24] virt: geniezone: Add vm capability check Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 08/24] virt: geniezone: Add vcpu support Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 09/24] virt: geniezone: Add irqchip support for virtual interrupt injection Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 10/24] virt: geniezone: Add irqfd support Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 11/24] virt: geniezone: Add ioeventfd support Liju-clr Chen
2024-07-31 8:57 ` Simon Horman
2024-07-30 8:24 ` [PATCH v12 12/24] virt: geniezone: Add memory region purpose for hypervisor Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 13/24] virt: geniezone: Add dtb config support Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 14/24] virt: geniezone: Optimize performance of protected VM memory Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 15/24] virt: geniezone: Add memory pin/unpin support Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 16/24] virt: geniezone: Add demand paging support Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 17/24] virt: geniezone: Add block-based " Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 18/24] virt: geniezone: Add memory relinquish support Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 19/24] virt: geniezone: Provide individual VM memory statistics within debugfs Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 20/24] virt: geniezone: Add tracing support for hyp call and vcpu exit_reason Liju-clr Chen
2024-08-07 20:02 ` Steven Rostedt
2024-07-30 8:24 ` [PATCH v12 21/24] virt: geniezone: Enable PTP for synchronizing time between host and guest VMs Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 22/24] virt: geniezone: Add support for virtual timer migration Liju-clr Chen
2024-07-30 8:24 ` Liju-clr Chen [this message]
2024-07-30 8:24 ` [PATCH v12 24/24] virt: geniezone: Emulate IPI for guest VM Liju-clr Chen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240730082436.9151-24-liju-clr.chen@mediatek.com \
--to=liju-clr.chen@mediatek.com \
--cc=Chi-shen.Yeh@mediatek.com \
--cc=Kevenny.Hsieh@mediatek.com \
--cc=PeiLun.Suei@mediatek.com \
--cc=Yingshiuan.Pan@mediatek.com \
--cc=Ze-yu.Wang@mediatek.com \
--cc=angelogioacchino.delregno@collabora.com \
--cc=catalin.marinas@arm.com \
--cc=conor+dt@kernel.org \
--cc=corbet@lwn.net \
--cc=devicetree@vger.kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mediatek@lists.infradead.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=matthias.bgg@gmail.com \
--cc=mhiramat@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=richardcochran@gmail.com \
--cc=robh@kernel.org \
--cc=rostedt@goodmis.org \
--cc=shawn.hsiao@mediatek.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®