[PATCH v3 0/4] Introduce a battery, AC adapter, and lid button

Leonid Bloch posted 4 patches 1 month ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20250827220054.37268-1-lb.workbox@gmail.com
Maintainers: Leonid Bloch <lb.workbox@gmail.com>, "Michael S. Tsirkin" <mst@redhat.com>, Igor Mammedov <imammedo@redhat.com>, Ani Sinha <anisinha@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, Richard Henderson <richard.henderson@linaro.org>, Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Eric Blake <eblake@redhat.com>, Markus Armbruster <armbru@redhat.com>
MAINTAINERS                          |  18 +
docs/specs/acad.rst                  | 195 +++++++
docs/specs/battery.rst               | 225 ++++++++
docs/specs/button.rst                | 189 +++++++
docs/specs/index.rst                 |   3 +
hw/acpi/Kconfig                      |  12 +
hw/acpi/acad.c                       | 447 ++++++++++++++++
hw/acpi/battery.c                    | 735 +++++++++++++++++++++++++++
hw/acpi/button.c                     | 438 ++++++++++++++++
hw/acpi/core.c                       |  17 +-
hw/acpi/meson.build                  |   3 +
hw/acpi/trace-events                 |  15 +
hw/i386/Kconfig                      |   3 +
hw/i386/acpi-build.c                 |   1 +
include/hw/acpi/acad.h               |  27 +
include/hw/acpi/acpi_dev_interface.h |   3 +
include/hw/acpi/battery.h            |  33 ++
include/hw/acpi/button.h             |  25 +
qapi/acpi.json                       | 171 +++++++
19 files changed, 2558 insertions(+), 2 deletions(-)
create mode 100644 docs/specs/acad.rst
create mode 100644 docs/specs/battery.rst
create mode 100644 docs/specs/button.rst
create mode 100644 hw/acpi/acad.c
create mode 100644 hw/acpi/battery.c
create mode 100644 hw/acpi/button.c
create mode 100644 include/hw/acpi/acad.h
create mode 100644 include/hw/acpi/battery.h
create mode 100644 include/hw/acpi/button.h
[PATCH v3 0/4] Introduce a battery, AC adapter, and lid button
Posted by Leonid Bloch 1 month ago
This series introduces three ACPI devices that are particularly useful
for laptop/mobile virtualization:

* Battery
* AC adapter
* Laptop lid button

Link to v2: https://lists.gnu.org/archive/html/qemu-devel/2025-08/msg03166.html
Link to GitHub PR, for ease of review: https://github.com/blochl/qemu/pull/3

Changes in v3:
--------------
* Rebased on latest master
* Addressed the v2 review by Igor Mammedov

Changes in v2:
--------------
Based on the feedback from Philippe Mathieu-Daudé and Michael S. Tsirkin:

* Complete redesign with dual-mode operation:
  - QMP control mode (default): Devices are controlled via QMP commands,
    providing deterministic behavior essential for migration and CI/testing
  - Host mirroring mode (opt-in): Original sysfs/procfs monitoring behavior,
    now disabled by default

* Migrated to modern QEMU ACPI architecture:
  - Devices now implement ACPI_DEV_AML_IF interface
  - AML generation moved from centralized acpi-build.c to device files

* Added a QMP interface:
  - battery-set-state/query-battery
  - ac-adapter-set-state/query-ac-adapter
  - lid-button-set-state/query-lid-button

* Documentation improvements:
  - Converted to .rst format
  - Added examples for both QMP and "fake" sysfs/procfs testing

The dual-mode design ensures these devices are migration-safe and
deterministic by default, while still allowing host state mirroring
when explicitly requested for desktop use cases.

Use cases:
----------
1. Testing: CI systems can programmatically control power states
2. Cloud: Expose virtual battery for usage-based resource limiting
3. Desktop virtualization: Mirror host laptop state to guest (opt-in)
4. Development: Test power management without physical hardware

Example usage:
--------------
# Default QMP-controlled battery
qemu-system-x86_64 -device battery

# Mirror host battery
qemu-system-x86_64 -device battery,use-qmp=false,enable-sysfs=true

# Control via QMP
{"execute": "battery-set-state",
 "arguments": {"state": {"present": true, "charging": false,
                         "discharging": true, "charge-percent": 42,
                         "rate": 500}}}

The series has been tested with Windows and Linux guests, correctly
showing battery status, AC adapter state, and lid button events in
guest UIs and triggering appropriate power management actions.

Thanks again for your patience and feedback.
Leonid.

Leonid Bloch (4):
  hw/acpi: Support extended GPE handling for additional ACPI devices
  hw/acpi: Introduce the QEMU Battery
  hw/acpi: Introduce the QEMU AC adapter
  hw/acpi: Introduce the QEMU lid button

 MAINTAINERS                          |  18 +
 docs/specs/acad.rst                  | 195 +++++++
 docs/specs/battery.rst               | 225 ++++++++
 docs/specs/button.rst                | 189 +++++++
 docs/specs/index.rst                 |   3 +
 hw/acpi/Kconfig                      |  12 +
 hw/acpi/acad.c                       | 447 ++++++++++++++++
 hw/acpi/battery.c                    | 735 +++++++++++++++++++++++++++
 hw/acpi/button.c                     | 438 ++++++++++++++++
 hw/acpi/core.c                       |  17 +-
 hw/acpi/meson.build                  |   3 +
 hw/acpi/trace-events                 |  15 +
 hw/i386/Kconfig                      |   3 +
 hw/i386/acpi-build.c                 |   1 +
 include/hw/acpi/acad.h               |  27 +
 include/hw/acpi/acpi_dev_interface.h |   3 +
 include/hw/acpi/battery.h            |  33 ++
 include/hw/acpi/button.h             |  25 +
 qapi/acpi.json                       | 171 +++++++
 19 files changed, 2558 insertions(+), 2 deletions(-)
 create mode 100644 docs/specs/acad.rst
 create mode 100644 docs/specs/battery.rst
 create mode 100644 docs/specs/button.rst
 create mode 100644 hw/acpi/acad.c
 create mode 100644 hw/acpi/battery.c
 create mode 100644 hw/acpi/button.c
 create mode 100644 include/hw/acpi/acad.h
 create mode 100644 include/hw/acpi/battery.h
 create mode 100644 include/hw/acpi/button.h

-- 
2.51.0


Re: [PATCH v3 0/4] Introduce a battery, AC adapter, and lid button
Posted by Igor Mammedov 3 weeks, 3 days ago
On Thu, 28 Aug 2025 01:00:46 +0300
Leonid Bloch <lb.workbox@gmail.com> wrote:

> This series introduces three ACPI devices that are particularly useful
> for laptop/mobile virtualization:
> 
> * Battery
> * AC adapter
> * Laptop lid button
generic comments that apply to whole series.

1. I'd suggest to split out documentation from the code into a separate patches.
   1st goes a doc patch, then device code, rinse& repeat for the next device.
   That should make patches more readable. (as it's now patches are too big)

2. Fishing out battery/lid/... date from host, is very host specific
   and not exactly QEMU business.
   I think QMP interface is sufficient to make new devices functional.
   And user/mgmt apps can use QMP to set/trigger/update device state when necessary.

   So I suggest to drop everything related to sysfs & co.
   It should be fine to provide in tree a python script as a reference impl.
   that would access sysfs and translate it to QMP for dev purposes.

   That will help reviewabilty of patches, from security pov reduce codebase
   only to bare necessity to emulate/manage devices, and won't require
   giving away access to sysfs files to QEMU.

   If there is a desire to make it available to end-users, then adding that
   to libvirt (which is likely next mgmt layer on for laptop users) looks
   like a best bet to make it accessible to ordinary users.

3. is it possible to use PCI instead of ISA as a base for the devices?
   (with PCI, device will be basically self provisioning with out need to
   manually pick up addresses, which is difficult thing to do and also
   on ACPI level PCI bars can be accessed without need to nail down
   IO region addresses, and possibility to use it machines without ISA bus)

I'll skim through individual patches as the 1st pass (and will do detailed review
on next revision once comments are addressed).
 
> Link to v2: https://lists.gnu.org/archive/html/qemu-devel/2025-08/msg03166.html
> Link to GitHub PR, for ease of review: https://github.com/blochl/qemu/pull/3
> 
> Changes in v3:
> --------------
> * Rebased on latest master
> * Addressed the v2 review by Igor Mammedov
> 
> Changes in v2:
> --------------
> Based on the feedback from Philippe Mathieu-Daudé and Michael S. Tsirkin:
> 
> * Complete redesign with dual-mode operation:
>   - QMP control mode (default): Devices are controlled via QMP commands,
>     providing deterministic behavior essential for migration and CI/testing
>   - Host mirroring mode (opt-in): Original sysfs/procfs monitoring behavior,
>     now disabled by default
> 
> * Migrated to modern QEMU ACPI architecture:
>   - Devices now implement ACPI_DEV_AML_IF interface
>   - AML generation moved from centralized acpi-build.c to device files
> 
> * Added a QMP interface:
>   - battery-set-state/query-battery
>   - ac-adapter-set-state/query-ac-adapter
>   - lid-button-set-state/query-lid-button
> 
> * Documentation improvements:
>   - Converted to .rst format
>   - Added examples for both QMP and "fake" sysfs/procfs testing
> 
> The dual-mode design ensures these devices are migration-safe and
> deterministic by default, while still allowing host state mirroring
> when explicitly requested for desktop use cases.
> 
> Use cases:
> ----------
> 1. Testing: CI systems can programmatically control power states
> 2. Cloud: Expose virtual battery for usage-based resource limiting
> 3. Desktop virtualization: Mirror host laptop state to guest (opt-in)
> 4. Development: Test power management without physical hardware
> 
> Example usage:
> --------------
> # Default QMP-controlled battery
> qemu-system-x86_64 -device battery
> 
> # Mirror host battery
> qemu-system-x86_64 -device battery,use-qmp=false,enable-sysfs=true
> 
> # Control via QMP
> {"execute": "battery-set-state",
>  "arguments": {"state": {"present": true, "charging": false,
>                          "discharging": true, "charge-percent": 42,
>                          "rate": 500}}}
> 
> The series has been tested with Windows and Linux guests, correctly
> showing battery status, AC adapter state, and lid button events in
> guest UIs and triggering appropriate power management actions.
> 
> Thanks again for your patience and feedback.
> Leonid.
> 
> Leonid Bloch (4):
>   hw/acpi: Support extended GPE handling for additional ACPI devices
>   hw/acpi: Introduce the QEMU Battery
>   hw/acpi: Introduce the QEMU AC adapter
>   hw/acpi: Introduce the QEMU lid button
> 
>  MAINTAINERS                          |  18 +
>  docs/specs/acad.rst                  | 195 +++++++
>  docs/specs/battery.rst               | 225 ++++++++
>  docs/specs/button.rst                | 189 +++++++
>  docs/specs/index.rst                 |   3 +
>  hw/acpi/Kconfig                      |  12 +
>  hw/acpi/acad.c                       | 447 ++++++++++++++++
>  hw/acpi/battery.c                    | 735 +++++++++++++++++++++++++++
>  hw/acpi/button.c                     | 438 ++++++++++++++++
>  hw/acpi/core.c                       |  17 +-
>  hw/acpi/meson.build                  |   3 +
>  hw/acpi/trace-events                 |  15 +
>  hw/i386/Kconfig                      |   3 +
>  hw/i386/acpi-build.c                 |   1 +
>  include/hw/acpi/acad.h               |  27 +
>  include/hw/acpi/acpi_dev_interface.h |   3 +
>  include/hw/acpi/battery.h            |  33 ++
>  include/hw/acpi/button.h             |  25 +
>  qapi/acpi.json                       | 171 +++++++
>  19 files changed, 2558 insertions(+), 2 deletions(-)
>  create mode 100644 docs/specs/acad.rst
>  create mode 100644 docs/specs/battery.rst
>  create mode 100644 docs/specs/button.rst
>  create mode 100644 hw/acpi/acad.c
>  create mode 100644 hw/acpi/battery.c
>  create mode 100644 hw/acpi/button.c
>  create mode 100644 include/hw/acpi/acad.h
>  create mode 100644 include/hw/acpi/battery.h
>  create mode 100644 include/hw/acpi/button.h
>