[PATCH 13/13] acpi: hmp/qmp: Add hmp/qmp support for system_sleep

Annie Li posted 13 patches 5 months, 3 weeks ago
Maintainers: "Dr. David Alan Gilbert" <dave@treblig.org>, "Michael S. Tsirkin" <mst@redhat.com>, Igor Mammedov <imammedo@redhat.com>, Ani Sinha <anisinha@redhat.com>, Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Yanan Wang <wangyanan55@huawei.com>, Zhao Liu <zhao1.liu@intel.com>, Paolo Bonzini <pbonzini@redhat.com>, Richard Henderson <richard.henderson@linaro.org>, Sergio Lopez <slp@redhat.com>, Eric Blake <eblake@redhat.com>, Markus Armbruster <armbru@redhat.com>
There is a newer version of this series
[PATCH 13/13] acpi: hmp/qmp: Add hmp/qmp support for system_sleep
Posted by Annie Li 5 months, 3 weeks ago
Following hmp/qmp commands are implemented for pressing virtual
sleep button,

hmp: system_sleep
qmp: { "execute": "system_sleep" }

These commands put the guest into suspend or other power states
depending on the power settings inside the guest.

These hmp/qmp command are in '*_*' format, it is intended to do
so to align to existing 'system_*' commands.

Signed-off-by: Annie Li <annie.li@oracle.com>
---
 hmp-commands.hx            | 14 ++++++++++++++
 hw/core/machine-hmp-cmds.c |  5 +++++
 hw/core/machine-qmp-cmds.c | 11 +++++++++++
 include/monitor/hmp.h      |  1 +
 qapi/machine.json          | 20 ++++++++++++++++++++
 qapi/pragma.json           |  1 +
 6 files changed, 52 insertions(+)

diff --git a/hmp-commands.hx b/hmp-commands.hx
index 06746f0afc..12f08f3444 100644
--- a/hmp-commands.hx
+++ b/hmp-commands.hx
@@ -639,6 +639,20 @@ SRST
   whether profiling is on or off.
 ERST
 
+    {
+        .name       = "system_sleep",
+        .args_type  = "",
+        .params     = "",
+        .help       = "send system sleep event",
+        .cmd = hmp_system_sleep,
+    },
+
+SRST
+``system_sleep``
+  Push the virtual sleep button; if supported, the system will enter
+  an ACPI sleep state.
+ERST
+
     {
         .name       = "system_reset",
         .args_type  = "",
diff --git a/hw/core/machine-hmp-cmds.c b/hw/core/machine-hmp-cmds.c
index c6325cdcaa..f8a7c1de88 100644
--- a/hw/core/machine-hmp-cmds.c
+++ b/hw/core/machine-hmp-cmds.c
@@ -189,6 +189,11 @@ void hmp_system_reset(Monitor *mon, const QDict *qdict)
     qmp_system_reset(NULL);
 }
 
+void hmp_system_sleep(Monitor *mon, const QDict *qdict)
+{
+    qmp_system_sleep(NULL);
+}
+
 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
 {
     qmp_system_powerdown(NULL);
diff --git a/hw/core/machine-qmp-cmds.c b/hw/core/machine-qmp-cmds.c
index 1bc21b84a4..80b877d8cd 100644
--- a/hw/core/machine-qmp-cmds.c
+++ b/hw/core/machine-qmp-cmds.c
@@ -12,6 +12,7 @@
 #include "hw/boards.h"
 #include "hw/intc/intc.h"
 #include "hw/mem/memory-device.h"
+#include "hw/acpi/acpi.h"
 #include "qapi/error.h"
 #include "qapi/qapi-builtin-visit.h"
 #include "qapi/qapi-commands-machine.h"
@@ -277,6 +278,16 @@ void qmp_system_reset(Error **errp)
     qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET);
 }
 
+void qmp_system_sleep(Error **errp)
+{
+    if (!qemu_wakeup_suspend_enabled()) {
+        error_setg(errp,
+                   "suspend from running is not supported by this machine");
+        return;
+    }
+    acpi_send_sleep_event();
+}
+
 void qmp_system_powerdown(Error **errp)
 {
     qemu_system_powerdown_request();
diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h
index ae116d9804..e543eec109 100644
--- a/include/monitor/hmp.h
+++ b/include/monitor/hmp.h
@@ -43,6 +43,7 @@ void hmp_quit(Monitor *mon, const QDict *qdict);
 void hmp_stop(Monitor *mon, const QDict *qdict);
 void hmp_sync_profile(Monitor *mon, const QDict *qdict);
 void hmp_system_reset(Monitor *mon, const QDict *qdict);
+void hmp_system_sleep(Monitor *mon, const QDict *qdict);
 void hmp_system_powerdown(Monitor *mon, const QDict *qdict);
 void hmp_exit_preconfig(Monitor *mon, const QDict *qdict);
 void hmp_announce_self(Monitor *mon, const QDict *qdict);
diff --git a/qapi/machine.json b/qapi/machine.json
index a6b8795b09..3efaf82f7d 100644
--- a/qapi/machine.json
+++ b/qapi/machine.json
@@ -361,6 +361,26 @@
 ##
 { 'command': 'system_reset' }
 
+##
+# @system_sleep:
+#
+# Requests that the guest perform a ACPI sleep transition by pushing
+# the virtual sleep button.
+#
+# Since:10.0
+#
+# .. note:: A guest may or may not respond to this command.  This
+#        command returning does not indicate that a guest has
+#        accepted the request or that it has gone to sleep.
+#
+# .. qmp-example::
+#
+# -> { "execute": "system_sleep" }
+# <- { "return": {} }
+#
+##
+{ 'command': 'system_sleep' }
+
 ##
 # @system_powerdown:
 #
diff --git a/qapi/pragma.json b/qapi/pragma.json
index 023a2ef7bc..285ce82e9b 100644
--- a/qapi/pragma.json
+++ b/qapi/pragma.json
@@ -23,6 +23,7 @@
         'set_password',
         'system_powerdown',
         'system_reset',
+        'system_sleep',
         'system_wakeup' ],
     # Commands allowed to return a non-dictionary
     'command-returns-exceptions': [
-- 
2.43.5
Re: [PATCH 13/13] acpi: hmp/qmp: Add hmp/qmp support for system_sleep
Posted by Markus Armbruster 5 months, 2 weeks ago
Annie Li <annie.li@oracle.com> writes:

> Following hmp/qmp commands are implemented for pressing virtual
> sleep button,
>
> hmp: system_sleep
> qmp: { "execute": "system_sleep" }
>
> These commands put the guest into suspend or other power states
> depending on the power settings inside the guest.
>
> These hmp/qmp command are in '*_*' format, it is intended to do
> so to align to existing 'system_*' commands.
>
> Signed-off-by: Annie Li <annie.li@oracle.com>

[...]

> diff --git a/qapi/machine.json b/qapi/machine.json
> index a6b8795b09..3efaf82f7d 100644
> --- a/qapi/machine.json
> +++ b/qapi/machine.json
> @@ -361,6 +361,26 @@
>  ##
>  { 'command': 'system_reset' }
>  
> +##
> +# @system_sleep:
> +#
> +# Requests that the guest perform a ACPI sleep transition by pushing
> +# the virtual sleep button.
> +#
> +# Since:10.0

10.1

> +#
> +# .. note:: A guest may or may not respond to this command.  This
> +#        command returning does not indicate that a guest has
> +#        accepted the request or that it has gone to sleep.

Please indent like this for consistency:

   # .. note:: A guest may or may not respond to this command.  This
   #    command returning does not indicate that a guest has accepted the
   #    request or that it has gone to sleep.

The same note on system_powerdown additionally has "Many guests will respond to
this command by prompting the user in some way."  I guess you did not
copy this part, because it's not true here.  Correct?

> +#
> +# .. qmp-example::
> +#
> +# -> { "execute": "system_sleep" }
> +# <- { "return": {} }
> +#
> +##
> +{ 'command': 'system_sleep' }
> +
>  ##
>  # @system_powerdown:
>  #
> diff --git a/qapi/pragma.json b/qapi/pragma.json
> index 023a2ef7bc..285ce82e9b 100644
> --- a/qapi/pragma.json
> +++ b/qapi/pragma.json
> @@ -23,6 +23,7 @@
>          'set_password',
>          'system_powerdown',
>          'system_reset',
> +        'system_sleep',
>          'system_wakeup' ],
>      # Commands allowed to return a non-dictionary
>      'command-returns-exceptions': [
Re: [PATCH 13/13] acpi: hmp/qmp: Add hmp/qmp support for system_sleep
Posted by Annie Li 5 months, 2 weeks ago
Hello Markus,

On 6/2/2025 5:32 AM, Markus Armbruster wrote:
> Annie Li <annie.li@oracle.com> writes:
>
>> Following hmp/qmp commands are implemented for pressing virtual
>> sleep button,
>>
>> hmp: system_sleep
>> qmp: { "execute": "system_sleep" }
>>
>> These commands put the guest into suspend or other power states
>> depending on the power settings inside the guest.
>>
>> These hmp/qmp command are in '*_*' format, it is intended to do
>> so to align to existing 'system_*' commands.
>>
>> Signed-off-by: Annie Li <annie.li@oracle.com>
> [...]
>
>> diff --git a/qapi/machine.json b/qapi/machine.json
>> index a6b8795b09..3efaf82f7d 100644
>> --- a/qapi/machine.json
>> +++ b/qapi/machine.json
>> @@ -361,6 +361,26 @@
>>   ##
>>   { 'command': 'system_reset' }
>>   
>> +##
>> +# @system_sleep:
>> +#
>> +# Requests that the guest perform a ACPI sleep transition by pushing
>> +# the virtual sleep button.
>> +#
>> +# Since:10.0
> 10.1
Good catch.
>
>> +#
>> +# .. note:: A guest may or may not respond to this command.  This
>> +#        command returning does not indicate that a guest has
>> +#        accepted the request or that it has gone to sleep.
> Please indent like this for consistency:
Nod
>
>     # .. note:: A guest may or may not respond to this command.  This
>     #    command returning does not indicate that a guest has accepted the
>     #    request or that it has gone to sleep.
>
> The same note on system_powerdown additionally has "Many guests will respond to
> this command by prompting the user in some way."  I guess you did not
> copy this part, because it's not true here.  Correct?

Very likely(but ignored indentation).

Don't recall clearly since it has been a while since RFC V1 patch :)

Thanks

Annie

>
>> +#
>> +# .. qmp-example::
>> +#
>> +# -> { "execute": "system_sleep" }
>> +# <- { "return": {} }
>> +#
>> +##
>> +{ 'command': 'system_sleep' }
>> +
>>   ##
>>   # @system_powerdown:
>>   #
>> diff --git a/qapi/pragma.json b/qapi/pragma.json
>> index 023a2ef7bc..285ce82e9b 100644
>> --- a/qapi/pragma.json
>> +++ b/qapi/pragma.json
>> @@ -23,6 +23,7 @@
>>           'set_password',
>>           'system_powerdown',
>>           'system_reset',
>> +        'system_sleep',
>>           'system_wakeup' ],
>>       # Commands allowed to return a non-dictionary
>>       'command-returns-exceptions': [