Skip to content

Commit f0be260

Browse files
praveen-pkliuw
authored andcommitted
mshv: Use reboot notifier to configure sleep state
Configure sleep state information from ACPI in MSHV hypervisor using a reboot notifier. This data allows the hypervisor to correctly power off the host during shutdown. Signed-off-by: Praveen K Paladugu <prapal@linux.microsoft.com> Co-developed-by: Anatol Belski <anbelski@linux.microsoft.com> Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com> Reviewed-by: Easwar Hariharan <easwar.hariharan@linux.microsoft.com> Reviewed-by: Stansialv Kinsburskii <skinsburskii@linux.miscrosoft.com> Acked-by: Ingo Molnar <mingo@kernel.org> Reviewed-by: Nuno Das Neves <nunodasneves@linux.microsoft.com> Signed-off-by: Wei Liu <wei.liu@kernel.org>
1 parent 723c47a commit f0be260

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

arch/x86/hyperv/hv_init.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ void __init hyperv_init(void)
555555

556556
hv_remap_tsc_clocksource();
557557
hv_root_crash_init();
558+
hv_sleep_notifiers_register();
558559
} else {
559560
hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
560561
wrmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);

arch/x86/include/asm/mshyperv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ int hyperv_flush_guest_mapping_range(u64 as,
177177
int hyperv_fill_flush_guest_mapping_list(
178178
struct hv_guest_mapping_flush_list *flush,
179179
u64 start_gfn, u64 end_gfn);
180+
void hv_sleep_notifiers_register(void);
180181

181182
#ifdef CONFIG_X86_64
182183
void hv_apic_init(void);

drivers/hv/mshv_common.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#include <asm/mshyperv.h>
1515
#include <linux/resume_user_mode.h>
1616
#include <linux/export.h>
17+
#include <linux/acpi.h>
18+
#include <linux/notifier.h>
19+
#include <linux/reboot.h>
1720

1821
#include "mshv.h"
1922

@@ -138,3 +141,78 @@ int hv_call_get_partition_property(u64 partition_id,
138141
return 0;
139142
}
140143
EXPORT_SYMBOL_GPL(hv_call_get_partition_property);
144+
145+
/*
146+
* Corresponding sleep states have to be initialized in order for a subsequent
147+
* HVCALL_ENTER_SLEEP_STATE call to succeed. Currently only S5 state as per
148+
* ACPI 6.4 chapter 7.4.2 is relevant, while S1, S2 and S3 can be supported.
149+
*
150+
* In order to pass proper PM values to mshv, ACPI should be initialized and
151+
* should support S5 sleep state when this method is invoked.
152+
*/
153+
static int hv_initialize_sleep_states(void)
154+
{
155+
u64 status;
156+
unsigned long flags;
157+
struct hv_input_set_system_property *in;
158+
acpi_status acpi_status;
159+
u8 sleep_type_a, sleep_type_b;
160+
161+
if (!acpi_sleep_state_supported(ACPI_STATE_S5)) {
162+
pr_err("%s: S5 sleep state not supported.\n", __func__);
163+
return -ENODEV;
164+
}
165+
166+
acpi_status = acpi_get_sleep_type_data(ACPI_STATE_S5, &sleep_type_a,
167+
&sleep_type_b);
168+
if (ACPI_FAILURE(acpi_status))
169+
return -ENODEV;
170+
171+
local_irq_save(flags);
172+
in = *this_cpu_ptr(hyperv_pcpu_input_arg);
173+
memset(in, 0, sizeof(*in));
174+
175+
in->property_id = HV_SYSTEM_PROPERTY_SLEEP_STATE;
176+
in->set_sleep_state_info.sleep_state = HV_SLEEP_STATE_S5;
177+
in->set_sleep_state_info.pm1a_slp_typ = sleep_type_a;
178+
in->set_sleep_state_info.pm1b_slp_typ = sleep_type_b;
179+
180+
status = hv_do_hypercall(HVCALL_SET_SYSTEM_PROPERTY, in, NULL);
181+
local_irq_restore(flags);
182+
183+
if (!hv_result_success(status)) {
184+
hv_status_err(status, "\n");
185+
return hv_result_to_errno(status);
186+
}
187+
188+
return 0;
189+
}
190+
191+
/*
192+
* This notifier initializes sleep states in mshv hypervisor which will be
193+
* used during power off.
194+
*/
195+
static int hv_reboot_notifier_handler(struct notifier_block *this,
196+
unsigned long code, void *another)
197+
{
198+
int ret = 0;
199+
200+
if (code == SYS_HALT || code == SYS_POWER_OFF)
201+
ret = hv_initialize_sleep_states();
202+
203+
return ret ? NOTIFY_DONE : NOTIFY_OK;
204+
}
205+
206+
static struct notifier_block hv_reboot_notifier = {
207+
.notifier_call = hv_reboot_notifier_handler,
208+
};
209+
210+
void hv_sleep_notifiers_register(void)
211+
{
212+
int ret;
213+
214+
ret = register_reboot_notifier(&hv_reboot_notifier);
215+
if (ret)
216+
pr_err("%s: cannot register reboot notifier %d\n", __func__,
217+
ret);
218+
}

0 commit comments

Comments
 (0)