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

Leonid Bloch posted 4 patches 2 months, 3 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20250821174554.40607-1-lb.workbox@gmail.com
Maintainers: Leonid Bloch <lb.workbox@gmail.com>, Paolo Bonzini <pbonzini@redhat.com>, "Michael S. Tsirkin" <mst@redhat.com>, Igor Mammedov <imammedo@redhat.com>, Ani Sinha <anisinha@redhat.com>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Richard Henderson <richard.henderson@linaro.org>, Eduardo Habkost <eduardo@habkost.net>, Eric Blake <eblake@redhat.com>, Markus Armbruster <armbru@redhat.com>
There is a newer version of this series
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 v2 0/4] Introduce a battery, AC adapter, and lid button
Posted by Leonid Bloch 2 months, 3 weeks ago
First, I must apologize for the extremely long delay since v1 - it has been
nearly 4 years, which is unacceptable. Life circumstances intervened.

This series introduces three ACPI devices that are particularly useful
for laptop/mobile virtualization:

* Battery
* AC adapter
* Laptop lid button

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: Increase the number of possible ACPI interrupts
  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.50.1


Re: [PATCH v2 0/4] Introduce a battery, AC adapter, and lid button
Posted by Leonid Bloch 2 months, 3 weeks ago
GitHub PR for ease of review: https://github.com/blochl/qemu/pull/2
Link to v1:
https://lists.gnu.org/archive/html/qemu-devel/2021-01/msg05246.html


On Thu, Aug 21, 2025 at 8:46 PM Leonid Bloch <lb.workbox@gmail.com> wrote:

> First, I must apologize for the extremely long delay since v1 - it has been
> nearly 4 years, which is unacceptable. Life circumstances intervened.
>
> This series introduces three ACPI devices that are particularly useful
> for laptop/mobile virtualization:
>
> * Battery
> * AC adapter
> * Laptop lid button
>
> 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: Increase the number of possible ACPI interrupts
>   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.50.1
>
>