Introduce a battery, AC adapter, and lid button
PATCH v4 2/8 docs/specs: Introduce the QEMU Battery documentation
In reply to Leonid Bloch--- MAINTAINERS | 5 ++ docs/specs/battery.rst | 154 +++++++++++++++++++++++++++++++++++++++++ docs/specs/index.rst | 1 + 3 files changed, 160 insertions(+) create mode 100644 docs/specs/battery.rst
MAINTAINERS +5 -0
@@ -3036,6 +3036,11 @@ F: hw/nitro/ F: include/hw/nitro/ F: docs/system/nitro.rst +Battery +M: Leonid Bloch <[email protected]> +S: Maintained +F: docs/specs/battery.rst + Subsystems ---------- Overall Audio backends
docs/specs/battery.rst new file +154 -0
154 lines not on this page · raw message
docs/specs/index.rst +1 -0
@@ -22,6 +22,7 @@ guest hardware that is specific to QEMU. acpi_pci_hotplug acpi_nvdimm acpi_erst + battery sev-guest-firmware fw_cfg fsi
Signature · 1 line
2.54.0
PATCH v4 5/8 hw/acpi: Introduce the QEMU AC adapter
In reply to Leonid BlochThe AC adapter device communicates AC power state to the guest via ACPI. AC adapter state is controlled programmatically via QMP commands, ensuring deterministic behavior.
Properties:
- 'ioport': I/O port base address (default: 0x53c)
The device implements the ACPI_DEV_AML_IF interface to generate its own AML code, placing the ADP0 device directly under \_SB scope.
QMP commands:
- ac-adapter-set-state: Set AC adapter connection state
- query-ac-adapter: Query current AC adapter state
--- MAINTAINERS | 2 + hw/acpi/Kconfig | 4 + hw/acpi/acad-stub.c | 20 +++ hw/acpi/acad.c | 251 +++++++++++++++++++++++++++ hw/acpi/meson.build | 2 + hw/acpi/trace-events | 4 + hw/i386/Kconfig | 1 + include/hw/acpi/acad.h | 25 +++ include/hw/acpi/acpi_dev_interface.h | 1 + qapi/acpi.json | 47 +++++ 10 files changed, 357 insertions(+) create mode 100644 hw/acpi/acad-stub.c create mode 100644 hw/acpi/acad.c create mode 100644 include/hw/acpi/acad.h
MAINTAINERS +2 -0
@@ -3047,6 +3047,8 @@ AC Adapter M: Leonid Bloch <[email protected]> S: Maintained F: docs/specs/acad.rst +F: hw/acpi/acad* +F: include/hw/acpi/acad.h Subsystems ----------
hw/acpi/Kconfig +4 -0
@@ -74,6 +74,10 @@ config ACPI_VIOT bool depends on ACPI +config AC_ADAPTER + bool + depends on ACPI + config ACPI_HW_REDUCED bool select ACPI
hw/acpi/acad-stub.c new file +20 -0
@@ -0,0 +1,20 @@ +/* +* QEMU emulated AC adapter device - QMP stubs. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qapi/qapi-commands-acpi.h" + +void qmp_ac_adapter_set_state (bool connected,Error **errp) +{ +error_setg (errp,"No AC adapter device found" ); +} + +AcAdapterInfo *qmp_query_ac_adapter (Error **errp) +{ +error_setg (errp,"No AC adapter device found" ); +return NULL ; +}
hw/acpi/acad.c new file +251 -0
251 lines not on this page · raw message
hw/acpi/meson.build +2 -0
@@ -37,6 +37,8 @@ stub_ss.add(files('acpi-stub.c', 'aml-build-stub.c', 'ghes-stub.c')) stub_ss.add(files('pci-bridge-stub.c')) acpi_ss.add(when: 'CONFIG_BATTERY', if_true: files('battery.c')) stub_ss.add(files('battery-stub.c')) +acpi_ss.add(when: 'CONFIG_AC_ADAPTER', if_true: files('acad.c')) +stub_ss.add(files('acad-stub.c')) system_ss.add_all(when: 'CONFIG_ACPI', if_true: acpi_ss) system_ss.add(when: 'CONFIG_GHES_CPER', if_true: files('ghes_cper.c')) stub_ss.add(files('ghes_cper_stub.c'))
hw/acpi/trace-events +4 -0
@@ -91,3 +91,7 @@ acpi_nvdimm_invalid_revision(uint32_t revision) "Revision 0x%" PRIx32 " is not s # battery.c battery_realize(void) "Battery device realize entry" battery_get_dynamic_status(uint32_t state, uint32_t rate, uint32_t charge) "Battery read state: 0x%"PRIx32", rate: %"PRIu32", charge: %"PRIu32 + +# acad.c +acad_realize(void) "AC adapter device realize entry" +acad_get_dynamic_status(uint8_t state) "AC adapter read state: %"PRIu8
hw/i386/Kconfig +1 -0
@@ -40,6 +40,7 @@ config PC imply NVDIMM imply FDC_ISA imply BATTERY + imply AC_ADAPTER select I8259 select I8254 select PCKBD
include/hw/acpi/acad.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* QEMU emulated AC adapter device. +* +* Copyright (c) 2019-2026 Janus Technologies, Inc. (http://janustech.com) +* +* Authors: +* Leonid Bloch <[email protected]> +* Marcel Apfelbaum <[email protected]> +* Dmitry Fleytman <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +* +*/ + +#ifndef HW_ACPI_AC_ADAPTER_H +#define HW_ACPI_AC_ADAPTER_H + +#define TYPE_AC_ADAPTER "acad" +#define AC_ADAPTER_IOPORT_PROP "ioport" + +#define AC_ADAPTER_VAL_UNKNOWN 0xFFFFFFFF + +#define AC_ADAPTER_LEN 1 + +#endif
include/hw/acpi/acpi_dev_interface.h +1 -0
@@ -15,6 +15,7 @@ typedef enum {ACPI_POWER_DOWN_STATUS =64 ,ACPI_GENERIC_ERROR =128 ,ACPI_BATTERY_CHANGE_STATUS =256 , +ACPI_AC_ADAPTER_CHANGE_STATUS =2048 , } AcpiEventStatusBits;#define TYPE_ACPI_DEVICE_IF "acpi-device-interface"
qapi/acpi.json +47 -0
50 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v4 1/8 hw/acpi: Support extended GPE handling for additional ACPI devices
In reply to Leonid BlochThis patch extends the GPE (General Purpose Event) handling to support the maximum number of interrupts available based on the machine's GPE register length, rather than being limited to the first 8 bits.
This change is needed to support additional ACPI devices that will be introduced in subsequent patches (Battery, AC adapter, and button devices). These new devices require GPE event bits beyond the first 8, which were previously not being properly handled by the event sending and SCI (System Control Interrupt) update mechanisms.
The actual number of available GPE interrupts varies by machine type:
- PIIX4: GPE_LEN = 4 (32 bits total across status and enable registers)
- ICH9: ICH9_PMIO_GPE0_LEN = 16 (128 bits total)
The patch modifies:
- acpi_send_gpe_event(): Now properly propagates status bits across all available GPE registers based on the machine's gpe.len value
- acpi_update_sci(): Checks all GPE registers for pending interrupts, not just the first byte
Note: A future enhancement could refactor the GPE handling to use the bitmap API from bitops.h instead of the current manual bit manipulation, which would provide a cleaner interface for these operations.
--- hw/acpi/core.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
hw/acpi/core.c +15 -2
@@ -733,19 +733,32 @@ uint32_t acpi_gpe_ioport_readb(ACPIREGS *ar, uint32_t addr)void acpi_send_gpe_event (ACPIREGS *ar,qemu_irq irq,AcpiEventStatusBits status) { - ar->gpe.sts[0 ] |= status; +int i; +AcpiEventStatusBits st = status; + +for (i =0 ; i < ar->gpe.len /2 ; i++) { + ar->gpe.sts[i] |= st; + st >>=TYPE_WIDTH (ar->gpe.sts[0 ]); + } +acpi_update_sci (ar, irq); }void acpi_update_sci (ACPIREGS *regs,qemu_irq irq) {int sci_level, pm1a_sts; +bool gpe_sci = false; +int i; pm1a_sts =acpi_pm1_evt_get_sts (regs); +for (i =0 ; i < regs->gpe.len /2 ; i++) { + gpe_sci = gpe_sci || !!(regs->gpe.sts[i] & regs->gpe.en[i]); + } + sci_level = ((pm1a_sts & regs->pm1.evt.en &ACPI_BITMASK_PM1_COMMON_ENABLED ) !=0 ) || - ((regs->gpe.sts[0 ] & regs->gpe.en[0 ]) !=0 ); + gpe_sci;qemu_set_irq (irq, sci_level);
Signature · 1 line
2.54.0
PATCH v4 7/8 hw/acpi: Introduce the QEMU lid button
In reply to Leonid BlochThe lid button device communicates laptop lid state to the guest via ACPI. Lid state is controlled programmatically via QMP commands for consistent behavior across environments.
The device implements the ACPI_DEV_AML_IF interface to generate its own AML code, placing the LID0 device directly under \_SB scope.
QMP commands:
- lid-button-set-state: Set lid open/closed state
- query-lid-button: Query current lid state
--- MAINTAINERS | 2 + hw/acpi/Kconfig | 4 + hw/acpi/button-stub.c | 20 +++ hw/acpi/button.c | 227 +++++++++++++++++++++++++++ hw/acpi/meson.build | 2 + hw/acpi/trace-events | 4 + hw/i386/Kconfig | 1 + include/hw/acpi/acpi_dev_interface.h | 1 + include/hw/acpi/button.h | 23 +++ qapi/acpi.json | 47 ++++++ 10 files changed, 331 insertions(+) create mode 100644 hw/acpi/button-stub.c create mode 100644 hw/acpi/button.c create mode 100644 include/hw/acpi/button.h
MAINTAINERS +2 -0
@@ -3054,6 +3054,8 @@ Button M: Leonid Bloch <[email protected]> S: Maintained F: docs/specs/button.rst +F: hw/acpi/button* +F: include/hw/acpi/button.h Subsystems ----------
hw/acpi/Kconfig +4 -0
@@ -78,6 +78,10 @@ config AC_ADAPTER bool depends on ACPI +config BUTTON + bool + depends on ACPI + config ACPI_HW_REDUCED bool select ACPI
hw/acpi/button-stub.c new file +20 -0
@@ -0,0 +1,20 @@ +/* +* QEMU emulated lid button device - QMP stubs. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qapi/qapi-commands-acpi.h" + +void qmp_lid_button_set_state (bool open,Error **errp) +{ +error_setg (errp,"No lid button device found" ); +} + +LidButtonInfo *qmp_query_lid_button (Error **errp) +{ +error_setg (errp,"No lid button device found" ); +return NULL ; +}
hw/acpi/button.c new file +227 -0
227 lines not on this page · raw message
hw/acpi/meson.build +2 -0
@@ -39,6 +39,8 @@ acpi_ss.add(when: 'CONFIG_BATTERY', if_true: files('battery.c')) stub_ss.add(files('battery-stub.c')) acpi_ss.add(when: 'CONFIG_AC_ADAPTER', if_true: files('acad.c')) stub_ss.add(files('acad-stub.c')) +acpi_ss.add(when: 'CONFIG_BUTTON', if_true: files('button.c')) +stub_ss.add(files('button-stub.c')) system_ss.add_all(when: 'CONFIG_ACPI', if_true: acpi_ss) system_ss.add(when: 'CONFIG_GHES_CPER', if_true: files('ghes_cper.c')) stub_ss.add(files('ghes_cper_stub.c'))
hw/acpi/trace-events +4 -0
@@ -95,3 +95,7 @@ battery_get_dynamic_status(uint32_t state, uint32_t rate, uint32_t charge) "Batt # acad.c acad_realize(void) "AC adapter device realize entry" acad_get_dynamic_status(uint8_t state) "AC adapter read state: %"PRIu8 + +# button.c +button_realize(void) "Button device realize entry" +button_get_dynamic_status(void) "Button read dynamic status entry"
hw/i386/Kconfig +1 -0
@@ -41,6 +41,7 @@ config PC imply FDC_ISA imply BATTERY imply AC_ADAPTER + imply BUTTON select I8259 select I8254 select PCKBD
include/hw/acpi/acpi_dev_interface.h +1 -0
@@ -16,6 +16,7 @@ typedef enum {ACPI_GENERIC_ERROR =128 ,ACPI_BATTERY_CHANGE_STATUS =256 ,ACPI_AC_ADAPTER_CHANGE_STATUS =2048 , +ACPI_BUTTON_CHANGE_STATUS =4096 , } AcpiEventStatusBits;#define TYPE_ACPI_DEVICE_IF "acpi-device-interface"
include/hw/acpi/button.h new file +23 -0
@@ -0,0 +1,23 @@ +/* +* QEMU emulated button device. +* +* Copyright (c) 2019-2026 Janus Technologies, Inc. (http://janustech.com) +* +* Authors: +* Leonid Bloch <[email protected]> +* Marcel Apfelbaum <[email protected]> +* Dmitry Fleytman <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +* +*/ + +#ifndef HW_ACPI_BUTTON_H +#define HW_ACPI_BUTTON_H + +#define TYPE_BUTTON "button" +#define BUTTON_IOPORT_PROP "ioport" + +#define BUTTON_LEN 1 + +#endif
qapi/acpi.json +47 -0
50 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v4 3/8 hw/acpi: Introduce the QEMU Battery
In reply to Leonid BlochThe battery device communicates battery state to the guest via ACPI. Battery state is controlled programmatically via QMP commands, making the device deterministic and migration-safe.
Properties:
- 'ioport': I/O port base address (default: 0x530)
The device implements the ACPI_DEV_AML_IF interface to generate its own AML code, placing the BAT0 device directly under \_SB scope as per ACPI specification.
QMP commands:
- battery-set-state: Set battery state (present, charging, capacity, rate)
- query-battery: Query current battery state
This provides a stable interface for virtualization management systems.
--- MAINTAINERS | 2 + hw/acpi/Kconfig | 4 + hw/acpi/battery-stub.c | 20 ++ hw/acpi/battery.c | 364 +++++++++++++++++++++++++++ hw/acpi/meson.build | 2 + hw/acpi/trace-events | 4 + hw/i386/Kconfig | 1 + include/hw/acpi/acpi_dev_interface.h | 1 + include/hw/acpi/battery.h | 32 +++ qapi/acpi.json | 71 ++++++ 10 files changed, 501 insertions(+) create mode 100644 hw/acpi/battery-stub.c create mode 100644 hw/acpi/battery.c create mode 100644 include/hw/acpi/battery.h
MAINTAINERS +2 -0
@@ -3040,6 +3040,8 @@ Battery M: Leonid Bloch <[email protected]> S: Maintained F: docs/specs/battery.rst +F: hw/acpi/battery* +F: include/hw/acpi/battery.h Subsystems ----------
hw/acpi/Kconfig +4 -0
@@ -88,3 +88,7 @@ config ACPI_ERST config ACPI_CXL bool depends on ACPI + +config BATTERY + bool + depends on ACPI
hw/acpi/battery-stub.c new file +20 -0
@@ -0,0 +1,20 @@ +/* +* QEMU emulated battery device - QMP stubs. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qapi/qapi-commands-acpi.h" + +void qmp_battery_set_state (BatteryInfo *state,Error **errp) +{ +error_setg (errp,"No battery device found" ); +} + +BatteryInfo *qmp_query_battery (Error **errp) +{ +error_setg (errp,"No battery device found" ); +return NULL ; +}
hw/acpi/battery.c new file +364 -0
364 lines not on this page · raw message
hw/acpi/meson.build +2 -0
@@ -35,6 +35,8 @@ if have_tpm endif stub_ss.add(files('acpi-stub.c', 'aml-build-stub.c', 'ghes-stub.c')) stub_ss.add(files('pci-bridge-stub.c')) +acpi_ss.add(when: 'CONFIG_BATTERY', if_true: files('battery.c')) +stub_ss.add(files('battery-stub.c')) system_ss.add_all(when: 'CONFIG_ACPI', if_true: acpi_ss) system_ss.add(when: 'CONFIG_GHES_CPER', if_true: files('ghes_cper.c')) stub_ss.add(files('ghes_cper_stub.c'))
hw/acpi/trace-events +4 -0
@@ -87,3 +87,7 @@ acpi_nvdimm_read_io_port(void) "Alert: we never read _DSM IO Port" acpi_nvdimm_dsm_mem_addr(uint64_t dsm_mem_addr) "dsm memory address 0x%" PRIx64 acpi_nvdimm_dsm_info(uint32_t revision, uint32_t handle, uint32_t function) "Revision 0x%" PRIx32 " Handle 0x%" PRIx32 " Function 0x%" PRIx32 acpi_nvdimm_invalid_revision(uint32_t revision) "Revision 0x%" PRIx32 " is not supported, expect 0x1" + +# battery.c +battery_realize(void) "Battery device realize entry" +battery_get_dynamic_status(uint32_t state, uint32_t rate, uint32_t charge) "Battery read state: 0x%"PRIx32", rate: %"PRIu32", charge: %"PRIu32
hw/i386/Kconfig +1 -0
@@ -39,6 +39,7 @@ config PC imply VIRTIO_VGA imply NVDIMM imply FDC_ISA + imply BATTERY select I8259 select I8254 select PCKBD
include/hw/acpi/acpi_dev_interface.h +1 -0
@@ -14,6 +14,7 @@ typedef enum {ACPI_VMGENID_CHANGE_STATUS =32 ,ACPI_POWER_DOWN_STATUS =64 ,ACPI_GENERIC_ERROR =128 , +ACPI_BATTERY_CHANGE_STATUS =256 , } AcpiEventStatusBits;#define TYPE_ACPI_DEVICE_IF "acpi-device-interface"
include/hw/acpi/battery.h new file +32 -0
@@ -0,0 +1,32 @@ +/* +* QEMU emulated battery device. +* +* Copyright (c) 2019-2026 Janus Technologies, Inc. (http://janustech.com) +* +* Authors: +* Leonid Bloch <[email protected]> +* Marcel Apfelbaum <[email protected]> +* Dmitry Fleytman <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +* +*/ + +#ifndef HW_ACPI_BATTERY_H +#define HW_ACPI_BATTERY_H + +#define TYPE_BATTERY "battery" +#define BATTERY_IOPORT_PROP "ioport" + +#define BATTERY_FULL_CAP 10000/* mWh */ +#define BATTERY_DESIGN_VOLTAGE 12000/* mV */ + +#define BATTERY_CAPACITY_OF_WARNING (BATTERY_FULL_CAP / 10)/* 10% */ +#define BATTERY_CAPACITY_OF_LOW (BATTERY_FULL_CAP / 25)/* 4% */ +#define BATTERY_CAPACITY_GRANULARITY (BATTERY_FULL_CAP / 100)/* 1% */ + +#define BATTERY_VAL_UNKNOWN 0xFFFFFFFF + +#define BATTERY_LEN 0x0C + +#endif
qapi/acpi.json +71 -0
74 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v4 4/8 docs/specs: Introduce the QEMU AC adapter documentation
In reply to Leonid Bloch--- MAINTAINERS | 5 ++ docs/specs/acad.rst | 126 +++++++++++++++++++++++++++++++++++++++++++ docs/specs/index.rst | 1 + 3 files changed, 132 insertions(+) create mode 100644 docs/specs/acad.rst
MAINTAINERS +5 -0
@@ -3043,6 +3043,11 @@ F: docs/specs/battery.rst F: hw/acpi/battery* F: include/hw/acpi/battery.h +AC Adapter +M: Leonid Bloch <[email protected]> +S: Maintained +F: docs/specs/acad.rst + Subsystems ---------- Overall Audio backends
docs/specs/acad.rst new file +126 -0
126 lines not on this page · raw message
docs/specs/index.rst +1 -0
@@ -22,6 +22,7 @@ guest hardware that is specific to QEMU. acpi_pci_hotplug acpi_nvdimm acpi_erst + acad battery sev-guest-firmware fw_cfg
Signature · 1 line
2.54.0
PATCH v4 6/8 docs/specs: Introduce the QEMU lid button documentation
In reply to Leonid Bloch--- MAINTAINERS | 5 ++ docs/specs/button.rst | 131 ++++++++++++++++++++++++++++++++++++++++++ docs/specs/index.rst | 1 + 3 files changed, 137 insertions(+) create mode 100644 docs/specs/button.rst
MAINTAINERS +5 -0
@@ -3050,6 +3050,11 @@ F: docs/specs/acad.rst F: hw/acpi/acad* F: include/hw/acpi/acad.h +Button +M: Leonid Bloch <[email protected]> +S: Maintained +F: docs/specs/button.rst + Subsystems ---------- Overall Audio backends
docs/specs/button.rst new file +131 -0
131 lines not on this page · raw message
docs/specs/index.rst +1 -0
@@ -24,6 +24,7 @@ guest hardware that is specific to QEMU. acpi_erst acad battery + button sev-guest-firmware fw_cfg fsi
Signature · 1 line
2.54.0
v4 cover Introduce a battery, AC adapter, and lid button
This series introduces three ACPI devices that are particularly useful for laptop/mobile virtualization:
* Battery
* AC adapter
* Laptop lid button
Link to v3: https://lists.nongnu.org/archive/html/qemu-devel/2025-08/msg04138.html
Link to GitHub PR, for ease of review: https://github.com/blochl/qemu/pull/4
Changes in v4:
--------------
* Rebased on latest master
Based on the feedback on v3:
* Documentation patch split: each device is now introduced by a separate documentation patch followed by the code patch.
* sysfs/procfs host mirroring removed from the C devices: QEMU no longer reads /sys or /proc directly. The devices are now purely QMP-controlled. A reference Python script is added in scripts/laptop-mirror.py that demonstrates how a management layer can translate host sysfs/procfs state into the QMP commands.
* Battery I/O region cleanup: the battery registers are now native DWORD-access only, matching the AML AML_DWORD_ACC field layout.
* find_*_device() now reports ambiguity: when more than one matching device is present, QMP returns "More than one X device present".
* QAPI cleanups:
- Stray blank documentation lines in *-set-state removed.
- All the new types and commands carry "Since: 11.1".
- The C devices no longer #include "qemu/error-report.h" now that they don't call warn_report().
- battery-set-state now rejects impossible combinations (charging && discharging, charge/discharge without present, out-of-range charge-percent and rate).
* I/O port unknown-address handling: the default branches in the ioport_read paths now g_assert_not_reached() instead of logging and returning 0.
* Set the devices not hotpluggable.
* ISA vs. PCI base: kept ISA. ACPI defines PNP0C0A (battery), ACPI0003 (AC adapter) and PNP0C0D (lid) as namespace devices under \_SB with control methods (_BIF/_BST, _PSR, _LID) - see ACPI 6.5 sections 10.2, 10.3 and 9.4
(https://uefi.org/sites/default/files/resources/ACPI_Spec_6_5_Aug29.pdf).
There is no PCI class code defined for any of these in the first place, and no actual laptop puts them on PCI - the signals come out of the embedded controller and surface as fixed-hardware ACPI devices. Guest power-management code matches the same way: Linux uses drivers/acpi/{battery,ac,button}, Windows acpi.sys, macOS and the BSDs follow the same pattern. Making the devices PCI would mean inventing a device class no guest has a driver for.
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 a virtual battery for usage-based resource limiting.
3. Desktop virtualization: Mirror host laptop state to the guest via the laptop-mirror.py reference script (or a libvirt equivalent).
4. Development: Test power management without physical hardware.
Example usage:
--------------
# QMP-controlled battery
qemu-system-x86_64 -device battery \
-qmp unix:/tmp/qmp.sock,server=on,wait=off
# Control via QMP
{"execute": "battery-set-state",
"arguments": {"state": {"present": true, "charging": false,
"discharging": true, "charge-percent": 42,
"rate": 500}}}# Or, to mirror host laptop state through QMP:
$builddir/run scripts/laptop-mirror.py -s /tmp/qmp.sock
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 the reviews.
Leonid.
Leonid Bloch (8):
hw/acpi: Support extended GPE handling for additional ACPI devices docs/specs: Introduce the QEMU Battery documentation hw/acpi: Introduce the QEMU Battery
docs/specs: Introduce the QEMU AC adapter documentation hw/acpi: Introduce the QEMU AC adapter
docs/specs: Introduce the QEMU lid button documentation hw/acpi: Introduce the QEMU lid button
scripts: Add laptop-mirror reference script
MAINTAINERS | 27 ++ docs/specs/acad.rst | 126 ++++++++++ docs/specs/battery.rst | 154 ++++++++++++ docs/specs/button.rst | 131 ++++++++++ docs/specs/index.rst | 3 + docs/tools/index.rst | 1 + docs/tools/laptop-mirror.rst | 82 ++++++ hw/acpi/Kconfig | 12 + hw/acpi/acad-stub.c | 20 ++ hw/acpi/acad.c | 251 ++++++++++++++++++ hw/acpi/battery-stub.c | 20 ++ hw/acpi/battery.c | 364 +++++++++++++++++++++++++++ hw/acpi/button-stub.c | 20 ++ hw/acpi/button.c | 227 +++++++++++++++++ hw/acpi/core.c | 17 +- hw/acpi/meson.build | 6 + hw/acpi/trace-events | 12 + hw/i386/Kconfig | 3 + include/hw/acpi/acad.h | 25 ++ include/hw/acpi/acpi_dev_interface.h | 3 + include/hw/acpi/battery.h | 32 +++ include/hw/acpi/button.h | 23 ++ qapi/acpi.json | 165 ++++++++++++ scripts/laptop-mirror.py | 219 ++++++++++++++++ 24 files changed, 1941 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 docs/tools/laptop-mirror.rst create mode 100644 hw/acpi/acad-stub.c create mode 100644 hw/acpi/acad.c create mode 100644 hw/acpi/battery-stub.c create mode 100644 hw/acpi/battery.c create mode 100644 hw/acpi/button-stub.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 create mode 100755 scripts/laptop-mirror.py
Signature · 1 line
2.54.0
PATCH v4 8/8 scripts: Add laptop-mirror reference script
Add an in-tree Python reference implementation that reads the host's battery, AC adapter and lid state from sysfs/procfs and forwards changes to a running QEMU guest through the battery-set-state, ac-adapter-set-state and lid-button-set-state QMP commands.
This script is intended as an example for management layers (such as libvirt) to follow when wiring host hardware to those devices, not as an end-user deployment tool.
--- MAINTAINERS | 6 + docs/tools/index.rst | 1 + docs/tools/laptop-mirror.rst | 82 +++++++++++++ scripts/laptop-mirror.py | 219 +++++++++++++++++++++++++++++++++++ 4 files changed, 308 insertions(+) create mode 100644 docs/tools/laptop-mirror.rst create mode 100755 scripts/laptop-mirror.py
MAINTAINERS +6 -0
@@ -3057,6 +3057,12 @@ F: docs/specs/button.rst F: hw/acpi/button* F: include/hw/acpi/button.h +Laptop mirror +M: Leonid Bloch <[email protected]> +S: Maintained +F: docs/tools/laptop-mirror.rst +F: scripts/laptop-mirror.py + Subsystems ---------- Overall Audio backends
docs/tools/index.rst +1 -0
@@ -17,3 +17,4 @@ command line utilities and other standalone programs. qemu-trace-stap qemu-vmsr-helper qemu-vnc + laptop-mirror
docs/tools/laptop-mirror.rst new file +82 -0
82 lines not on this page · raw message
scripts/laptop-mirror.py new file +219 -0
219 lines not on this page · raw message
Signature · 1 line
2.54.0
Re: [PATCH v4 3/8] hw/acpi: Introduce the QEMU Battery
In reply to Leonid BlochQuoted 7 lines · Leonid Bloch
The battery device communicates battery state to the guest via ACPI. Battery state is controlled programmatically via QMP commands, making the device deterministic and migration-safe.
Properties:
- 'ioport': I/O port base address (default:0x530)
Quoted 7 lines
The device implements the
ACPI_DEV_AML_IFinterface to generate its own AML code, placing the BAT0 device directly under \_SB scope as per ACPI specification.QMP commands:
- battery-set-state: Set battery state (present, charging, capacity, rate)
- query-battery: Query current battery state
Could we have one patch adding the device, and a second one adding the QMP commands?
Quoted 352 lines
This provides a stable interface for virtualization management systems.
Signed-off-by Leonid Bloch <[email protected]>Signed-off-by Marcel Apfelbaum <[email protected]>--- MAINTAINERS | 2 + hw/acpi/Kconfig | 4 + hw/acpi/battery-stub.c | 20 ++ hw/acpi/battery.c | 364 +++++++++++++++++++++++++++ hw/acpi/meson.build | 2 + hw/acpi/trace-events | 4 + hw/i386/Kconfig | 1 + include/hw/acpi/acpi_dev_interface.h | 1 + include/hw/acpi/battery.h | 32 +++ qapi/acpi.json | 71 ++++++ 10 files changed, 501 insertions(+) create mode 100644 hw/acpi/battery-stub.c create mode 100644 hw/acpi/battery.c create mode 100644 include/hw/acpi/battery.hMAINTAINERS +2
-0@@ -3040,6 +3040,8 @@ Battery M: Leonid Bloch <[email protected]> S: Maintained F: docs/specs/battery.rst +F: hw/acpi/battery* +F: include/hw/acpi/battery.h Subsystems ----------hw/acpi/Kconfig +4
-0@@ -88,3 +88,7 @@ config ACPI_ERST config ACPI_CXL bool depends on ACPI + +config BATTERY + bool + depends on ACPIhw/acpi/battery-stub.c new file +20
-0@@ -0,0 +1,20 @@ +/* +* QEMU emulated battery device - QMP stubs. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qapi/qapi-commands-acpi.h" + +void qmp_battery_set_state (BatteryInfo *state,Error **errp) +{ +error_setg (errp,"No battery device found" ); +} + +BatteryInfo *qmp_query_battery (Error **errp) +{ +error_setg (errp,"No battery device found" ); +return NULL ; +}hw/acpi/battery.c new file +274
-0@@ -0,0 +1,364 @@ +/* +* QEMU emulated battery device. +* +* Copyright (c) 2019-2026 Janus Technologies, Inc. (http://janustech.com) +* +* Authors: +* Leonid Bloch <[email protected]> +* Marcel Apfelbaum <[email protected]> +* Dmitry Fleytman <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +* +*/ + +#include "qemu/osdep.h" +#include "trace.h" +#include "hw/isa/isa.h" +#include "hw/acpi/acpi.h" +#include "qapi/error.h" +#include "hw/core/qdev-properties.h" +#include "migration/vmstate.h" +#include "hw/acpi/acpi_aml_interface.h" +#include "qapi/qapi-commands-acpi.h" + +#include "hw/acpi/battery.h" + +#define BATTERY_DEVICE (obj) OBJECT_CHECK(BatteryState, (obj), TYPE_BATTERY) + +#define BATTERY_DISCHARGING 0x01/* ACPI _BST bit 0 */ +#define BATTERY_CHARGING 0x02/* ACPI _BST bit 1 */ +#define BATTERY_CRITICAL 0x04/* ACPI _BST bit 2 */ +#define BATTERY_PRESENT 0x10/* ACPI _STA bit 4 */ + +typedef struct BatteryState { +ISADevice dev; +MemoryRegion io; +uint16_t ioport; +uint32_t state; +uint32_t rate; +uint32_t charge; +bool qmp_present; +bool qmp_charging; +bool qmp_discharging; +int32_t qmp_charge_percent; +int32_t qmp_rate; +}BatteryState ; + +enum { +BSTA_ADDR =0 , +BRTE_ADDR =4 , +BCRG_ADDR =8 , +}; + +static void battery_get_dynamic_status (BatteryState *s) +{ + s->state =0 ; +if (s->qmp_present) { + s->state |=BATTERY_PRESENT ; +if (s->qmp_charging) { + s->state |=BATTERY_CHARGING ; + } +if (s->qmp_discharging) { + s->state |=BATTERY_DISCHARGING ; + } + } + s->rate = s->qmp_rate; + s->charge = (s->qmp_charge_percent *BATTERY_FULL_CAP ) /100 ; + +trace_battery_get_dynamic_status (s->state, s->rate, s->charge); +} + +static void battery_realize (DeviceState *dev,Error **errp) +{ +ISADevice *d =ISA_DEVICE (dev); +BatteryState *s =BATTERY_DEVICE (dev); +bool ambiguous; + +trace_battery_realize (); + +object_resolve_path_type ("" ,TYPE_BATTERY , &ambiguous); +if (ambiguous) { +error_setg (errp,"at most one %s device is permitted" ,TYPE_BATTERY ); +return ; + } + +/* Initialize QMP state to sensible defaults */ + s->qmp_present = true; + s->qmp_charging = false; + s->qmp_discharging = true; + s->qmp_charge_percent =50 ; + s->qmp_rate =1000 ;/* 1000 mW discharge rate */ + +isa_register_ioport (d, &s->io, s->ioport); +} + +static const Property battery_device_properties[] = { +DEFINE_PROP_UINT16 (BATTERY_IOPORT_PROP , BatteryState, ioport,0x530 ), +}; + +static const VMStateDescription battery_vmstate = { + .name ="battery" , + .version_id =1 , + .minimum_version_id =1 , + .fields = (VMStateField []) { +VMSTATE_BOOL (qmp_present, BatteryState), +VMSTATE_BOOL (qmp_charging, BatteryState), +VMSTATE_BOOL (qmp_discharging, BatteryState), +VMSTATE_INT32 (qmp_charge_percent, BatteryState), +VMSTATE_INT32 (qmp_rate, BatteryState), +VMSTATE_END_OF_LIST () + } +}; + +static void build_battery_aml (AcpiDevAmlIf *adev,Aml *scope) +{ +Aml *dev, *field, *method, *pkg; +Aml *bat_state, *bat_rate, *bat_charge; +Aml *sb_scope; +BatteryState *s =BATTERY_DEVICE (adev); + + bat_state =aml_local (0 ); + bat_rate =aml_local (1 ); + bat_charge =aml_local (2 ); + + sb_scope =aml_scope ("\\_SB" ); + dev =aml_device ("BAT0" ); +aml_append (dev,aml_name_decl ("_HID" ,aml_eisaid ("PNP0C0A" ))); + +aml_append (dev,aml_operation_region ("DBST" ,AML_SYSTEM_IO , +aml_int (s->ioport), +BATTERY_LEN )); + field =aml_field ("DBST" ,AML_DWORD_ACC ,AML_NOLOCK ,AML_PRESERVE ); +aml_append (field,aml_named_field ("BSTA" ,32 )); +aml_append (field,aml_named_field ("BRTE" ,32 )); +aml_append (field,aml_named_field ("BCRG" ,32 )); +aml_append (dev, field); + + method =aml_method ("_STA" ,0 ,AML_NOTSERIALIZED ); +aml_append (method,aml_return (aml_or (aml_int (0x0F ), +aml_and (aml_name ("BSTA" ), +aml_int (0x10 ),NULL ), +NULL ))); +aml_append (dev, method); + + method =aml_method ("_BIF" ,0 ,AML_NOTSERIALIZED ); + pkg =aml_package (13 ); +/* Power Unit */ +aml_append (pkg,aml_int (0 ));/* mW */ +/* Design Capacity */ +aml_append (pkg,aml_int (BATTERY_FULL_CAP )); +/* Last Full Charge Capacity */ +aml_append (pkg,aml_int (BATTERY_FULL_CAP )); +/* Battery Technology */ +aml_append (pkg,aml_int (1 ));/* Secondary */ +/* Design Voltage */ +aml_append (pkg,aml_int (BATTERY_DESIGN_VOLTAGE )); +/* Design Capacity of Warning */ +aml_append (pkg,aml_int (BATTERY_CAPACITY_OF_WARNING )); +/* Design Capacity of Low */ +aml_append (pkg,aml_int (BATTERY_CAPACITY_OF_LOW )); +/* Battery Capacity Granularity 1 */ +aml_append (pkg,aml_int (BATTERY_CAPACITY_GRANULARITY )); +/* Battery Capacity Granularity 2 */ +aml_append (pkg,aml_int (BATTERY_CAPACITY_GRANULARITY )); +/* Model Number */ +aml_append (pkg,aml_string ("QBAT001" ));/* Model Number */ +/* Serial Number */ +aml_append (pkg,aml_string ("SN00000" ));/* Serial Number */ +/* Battery Type */ +aml_append (pkg,aml_string ("Virtual" ));/* Battery Type */ +/* OEM Information */ +aml_append (pkg,aml_string ("QEMU" ));/* OEM Information */ +aml_append (method,aml_return (pkg)); +aml_append (dev, method); + + pkg =aml_package (4 ); +/* Battery State */ +aml_append (pkg,aml_int (0 )); +/* Battery Present Rate */ +aml_append (pkg,aml_int (BATTERY_VAL_UNKNOWN )); +/* Battery Remaining Capacity */ +aml_append (pkg,aml_int (BATTERY_VAL_UNKNOWN )); +/* Battery Present Voltage */ +aml_append (pkg,aml_int (BATTERY_DESIGN_VOLTAGE )); +aml_append (dev,aml_name_decl ("DBPR" , pkg)); + + method =aml_method ("_BST" ,0 ,AML_NOTSERIALIZED ); +aml_append (method,aml_store (aml_and (aml_name ("BSTA" ),aml_int (0x0F ), +NULL ), + bat_state)); +aml_append (method,aml_store (aml_name ("BRTE" ), bat_rate)); +aml_append (method,aml_store (aml_name ("BCRG" ), bat_charge)); +aml_append (method,aml_store (bat_state, +aml_index (aml_name ("DBPR" ),aml_int (0 )))); +aml_append (method,aml_store (bat_rate, +aml_index (aml_name ("DBPR" ),aml_int (1 )))); +aml_append (method,aml_store (bat_charge, +aml_index (aml_name ("DBPR" ),aml_int (2 )))); +aml_append (method,aml_return (aml_name ("DBPR" ))); +aml_append (dev, method); + +aml_append (sb_scope, dev); +aml_append (scope, sb_scope); + +/* Status Change */ + method =aml_method ("\\_GPE._E08" ,0 ,AML_NOTSERIALIZED ); +aml_append (method,aml_notify (aml_name ("\\_SB.BAT0" ),aml_int (0x80 ))); +aml_append (scope, method); +} + +static void battery_class_init (ObjectClass *class,const void *data) +{ +DeviceClass *dc =DEVICE_CLASS (class); +AcpiDevAmlIfClass *adevc =ACPI_DEV_AML_IF_CLASS (class); + + dc->realize = battery_realize; + dc->hotpluggable = false; +device_class_set_props (dc, battery_device_properties); + dc->vmsd = &battery_vmstate; + adevc->build_dev_aml = build_battery_aml; +} + +static uint64_t battery_ioport_read (void *opaque,hwaddr addr,unsigned size) +{ +BatteryState *s = opaque; + +battery_get_dynamic_status (s); + +switch (addr) { +case BSTA_ADDR : +return s->state; +case BRTE_ADDR : +return s->rate; +case BCRG_ADDR : +return s->charge; +default : +g_assert_not_reached (); + } +} + +static const MemoryRegionOps battery_ops = { + .read = battery_ioport_read, + .endianness =DEVICE_LITTLE_ENDIAN , + .valid = { + .min_access_size =4 , + .max_access_size =4 , + }, + .impl = { + .min_access_size =4 , + .max_access_size =4 , + }, +}; + +static void battery_instance_init (Object *obj) +{ +BatteryState *s =BATTERY_DEVICE (obj); + +memory_region_init_io (&s->io, obj, &battery_ops, s,"battery" , +BATTERY_LEN ); +} + +static const TypeInfo battery_info = { + .name =TYPE_BATTERY , + .parent =TYPE_ISA_DEVICE , + .instance_size =sizeof (BatteryState), + .class_init = battery_class_init, + .instance_init = battery_instance_init, + .interfaces = (InterfaceInfo []) { + {TYPE_ACPI_DEV_AML_IF }, + { }, + }, +}; + +static BatteryState *find_battery_device (Error **errp)
hw/acpi/battery.c line 284
Quoted 10 lines
hw/acpi/battery.c new file +10
-0+{ +bool ambiguous; +Object *o =object_resolve_path_type ("" ,TYPE_BATTERY , &ambiguous); + +if (!o) { +error_setg (errp,"No battery device found" ); +return NULL ; + } +if (ambiguous) { +error_setg (errp,"More than one battery device present" );
How can we reach this error? battery_realize() refuses to realize more than one.
Quoted 23 lines
hw/acpi/battery.c new file +23
-0+return NULL ; + } +return BATTERY_DEVICE (o); +} + +void qmp_battery_set_state (BatteryInfo *state,Error **errp) +{ +BatteryState *s =find_battery_device (errp); +Object *obj; + +if (!s) { +return ; + } + +if (state->charging && state->discharging) { +error_setg (errp, +"'charging' and 'discharging' are mutually exclusive" ); +return ; + } +if (!state->present && (state->charging || state->discharging)) { +error_setg (errp, +"'charging'/'discharging' require 'present' to be true" ); +return ;
hw/acpi/battery.c line 317
Quoted 10 lines
hw/acpi/battery.c new file +10
-0+ } +if (state->charge_percent <0 || state->charge_percent >100 ) { +error_setg (errp,"'charge-percent' must be in the range 0..100" ); +return ; + } +if (state->has_rate && (state->rate <0 || state->rate >INT32_MAX )) { +error_setg (errp,"'rate' must be in the range 0..0x%" PRIX32 , + (uint32_t )INT32_MAX ); +return ; + }
I feel some of these error messages need work, but let's nail down the interface first.
Quoted 139 lines
hw/acpi/battery.c new file +47
-0+ + s->qmp_present = state->present; + s->qmp_charging = state->charging; + s->qmp_discharging = state->discharging; + s->qmp_charge_percent = state->charge_percent; + +if (state->has_rate) { + s->qmp_rate = state->rate; + } + + obj =object_resolve_path_type ("" ,TYPE_ACPI_DEVICE_IF ,NULL ); +if (obj) { +acpi_send_event (DEVICE (obj),ACPI_BATTERY_CHANGE_STATUS ); + } +} + +BatteryInfo *qmp_query_battery (Error **errp) +{ +BatteryState *s =find_battery_device (errp); +BatteryInfo *ret; + +if (!s) { +return NULL ; + } + + ret =g_new0 (BatteryInfo,1 ); + + ret->present = s->qmp_present; + ret->charging = s->qmp_charging; + ret->discharging = s->qmp_discharging; + ret->charge_percent = s->qmp_charge_percent; + ret->has_rate = true; + ret->rate = s->qmp_rate; + + ret->has_remaining_capacity = false; + ret->has_design_capacity = true; + ret->design_capacity =BATTERY_FULL_CAP ; + +return ret; +} + +static void battery_register_types (void ) +{ +type_register_static (&battery_info); +} + +type_init (battery_register_types)hw/acpi/meson.build +2
-0@@ -35,6 +35,8 @@ if have_tpm endif stub_ss.add(files('acpi-stub.c', 'aml-build-stub.c', 'ghes-stub.c')) stub_ss.add(files('pci-bridge-stub.c')) +acpi_ss.add(when: 'CONFIG_BATTERY', if_true: files('battery.c')) +stub_ss.add(files('battery-stub.c')) system_ss.add_all(when: 'CONFIG_ACPI', if_true: acpi_ss) system_ss.add(when: 'CONFIG_GHES_CPER', if_true: files('ghes_cper.c')) stub_ss.add(files('ghes_cper_stub.c'))hw/acpi/trace-events +4
-0@@ -87,3 +87,7 @@ acpi_nvdimm_read_io_port(void) "Alert: we never read _DSM IO Port" acpi_nvdimm_dsm_mem_addr(uint64_t dsm_mem_addr) "dsm memory address 0x%" PRIx64 acpi_nvdimm_dsm_info(uint32_t revision, uint32_t handle, uint32_t function) "Revision 0x%" PRIx32 " Handle 0x%" PRIx32 " Function 0x%" PRIx32 acpi_nvdimm_invalid_revision(uint32_t revision) "Revision 0x%" PRIx32 " is not supported, expect 0x1" + +# battery.c +battery_realize(void) "Battery device realize entry" +battery_get_dynamic_status(uint32_t state, uint32_t rate, uint32_t charge) "Battery read state: 0x%"PRIx32", rate: %"PRIu32", charge: %"PRIu32hw/i386/Kconfig +1
-0@@ -39,6 +39,7 @@ config PC imply VIRTIO_VGA imply NVDIMM imply FDC_ISA + imply BATTERY select I8259 select I8254 select PCKBDinclude/hw/acpi/acpi_dev_interface.h +1
-0@@ -14,6 +14,7 @@ typedef enum {ACPI_VMGENID_CHANGE_STATUS =32 ,ACPI_POWER_DOWN_STATUS =64 ,ACPI_GENERIC_ERROR =128 , +ACPI_BATTERY_CHANGE_STATUS =256 , } AcpiEventStatusBits;#define TYPE_ACPI_DEVICE_IF "acpi-device-interface"include/hw/acpi/battery.h new file +32
-0@@ -0,0 +1,32 @@ +/* +* QEMU emulated battery device. +* +* Copyright (c) 2019-2026 Janus Technologies, Inc. (http://janustech.com) +* +* Authors: +* Leonid Bloch <[email protected]> +* Marcel Apfelbaum <[email protected]> +* Dmitry Fleytman <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +* +*/ + +#ifndef HW_ACPI_BATTERY_H +#define HW_ACPI_BATTERY_H + +#define TYPE_BATTERY "battery" +#define BATTERY_IOPORT_PROP "ioport" + +#define BATTERY_FULL_CAP 10000/* mWh */ +#define BATTERY_DESIGN_VOLTAGE 12000/* mV */ + +#define BATTERY_CAPACITY_OF_WARNING (BATTERY_FULL_CAP / 10)/* 10% */ +#define BATTERY_CAPACITY_OF_LOW (BATTERY_FULL_CAP / 25)/* 4% */ +#define BATTERY_CAPACITY_GRANULARITY (BATTERY_FULL_CAP / 100)/* 1% */ + +#define BATTERY_VAL_UNKNOWN 0xFFFFFFFF + +#define BATTERY_LEN 0x0C + +#endif qapi/acpi.json +0
-0
qapi/acpi.json line 151
Quoted 10 lines
qapi/acpi.json +7
-0@@ -142,3 +142,74 @@ ## { 'event': 'ACPI_DEVICE_OST', 'data': { 'info': 'ACPIOSTInfo' } } + +## +# @BatteryInfo: +# +# Battery state information +# +# @present: whether the battery is present
Peeking at the C code... there is a difference between "have no TYPE_BATTERY device" and "have one, but not present". What does the latter mean? The machine has a battery device, but the actual battery pack is not connected?
qapi/acpi.json line 155
Quoted 4 lines
qapi/acpi.json +4
-0+# +# @charging: whether the battery is charging +# +# @discharging: whether the battery is discharging
Peeking at the C code... looks like @charging is initialized to false, @discharging to true, battery-set-state doesn't let you set both to true, and there is no other way to change them. Correct?
If yes, you're encoding a tri-state (charging, discharging, neither) as two bools. Hmm.
Peeking at the C code some more... looks like there's more
entanglement: @charging and @discharging must be false when @present is false.
Let's go through the cases:
present charging discharging meaning
false false false battery pack missing
true false true battery charging
true true false battery discharging
true false false ???Please complete / correct column meaning.
Are there missing rows?
qapi/acpi.json line 159
Quoted 4 lines
qapi/acpi.json +4
-0+# +# @charge-percent: battery charge percentage (0-100) +# +# @rate: charge/discharge rate in mW (optional)
Drop the (optional), please. As is, the description gets rendered like
• rate (int, optional) – charge/discharge rate in mW (optional)
What does absent mean? Hmm, the type is both ther argument of battery-set-state and the return value of query-battery. Let's discuss there.
qapi/acpi.json line 161
Quoted 2 lines
qapi/acpi.json +2
-0+# +# @remaining-capacity: remaining capacity in mWh (optional)
Likewise.
qapi/acpi.json line 163
Quoted 2 lines
qapi/acpi.json +2
-0+# +# @design-capacity: design capacity in mWh (optional)
Likewise.
Quoted 22 lines
qapi/acpi.json +22
-0+# +# Since: 11.1 +## +{ 'struct': 'BatteryInfo', + 'data': { 'present': 'bool', + 'charging': 'bool', + 'discharging': 'bool', + 'charge-percent': 'int', + '*rate': 'int', + '*remaining-capacity': 'int', + '*design-capacity': 'int' } } + +## +# @battery-set-state: +# +# Set the state of the emulated battery device +# +# @state: new battery state +# +# Since: 11.1 +# +# .. qmp-example::
qapi/acpi.json line 195
Quoted 10 lines
qapi/acpi.json +10
-0+# +# -> { "execute": "battery-set-state", +# "arguments": { "state": { "present": true, +# "charging": true, +# "discharging": false, +# "charge-percent": 85 } } } +# <- { "return": {} } +## +{ 'command': 'battery-set-state', + 'data': { 'state': 'BatteryInfo' } }
This interface fundamentally assumes there is at most one battery. Is that what we want? Forever and ever?
Let's avoid the silly "state": { ... }:
{ 'command': 'battery-set-state', 'boxed': true,
'data': 'BatteryInfo' }
Peeking at the command handler code... the command fails when there is no TYPE_BATTERY device or more than one.
Members @charging, @discharging, @charge-percent are mandatory. They must be present even with "present": false. Do they make any sense then?
Should we document the command fails when there is no battery device?
qapi/acpi.json line 202
Quoted 7 lines
qapi/acpi.json +7
-0+ +## +# @query-battery: +# +# Query the current state of the emulated battery device +# +# Returns: current battery state
This gets rendered like
Command query-battery (Since: 11.1)
Query the current state of the emulated battery device
Return:
BatteryInfo – current battery state
If you simply omit the Returns: line, it'll be
Command query-battery (Since: 11.1)
Query the current state of the emulated battery device
Return:
BatteryInfo
Choice is up to you.
Should we document the command fails when there is no battery device?
Quoted 13 lines
qapi/acpi.json +13
-0+# +# Since: 11.1 +# +# .. qmp-example:: +# +# -> { "execute": "query-battery" } +# <- { "return": { "present": true, +# "charging": true, +# "discharging": false, +# "charge-percent": 85 } } +## +{ 'command': 'query-battery', + 'returns': 'BatteryInfo' }
Re: [PATCH v4 5/8] hw/acpi: Introduce the QEMU AC adapter
In reply to Leonid BlochLet's discuss PATCH 3 before I review this one and PATCH 7.
Re: [PATCH v4 1/8] hw/acpi: Support extended GPE handling for additional ACPI devices
In reply to Leonid BlochQuoted 35 lines · On Tue, 26 May 2026 07:29:20 +0300 Leonid Bloch <[email protected]> wrote:
This patch extends the GPE (General Purpose Event) handling to support the maximum number of interrupts available based on the machine's GPE register length, rather than being limited to the first 8 bits.
This change is needed to support additional ACPI devices that will be introduced in subsequent patches (Battery, AC adapter, and button devices). These new devices require GPE event bits beyond the first 8, which were previously not being properly handled by the event sending and SCI (System Control Interrupt) update mechanisms.
The actual number of available GPE interrupts varies by machine type:
- PIIX4:GPE_LEN= 4 (32 bits total across status and enable registers)
- ICH9:ICH9_PMIO_GPE0_LEN= 16 (128 bits total)The patch modifies:
-acpi_send_gpe_event(): Now properly propagates status bits across all available GPE registers based on the machine's gpe.len value
-acpi_update_sci(): Checks all GPE registers for pending interrupts, not just the first byteNote: A future enhancement could refactor the GPE handling to use the bitmap API from
bitops.hinstead of the current manual bit manipulation, which would provide a cleaner interface for these operations.Signed-off-by Leonid Bloch <[email protected]>--- hw/acpi/core.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)hw/acpi/core.c +0
-0@@ -733,19 +733,32 @@ uint32_t acpi_gpe_ioport_readb(ACPIREGS *ar, uint32_t addr) void acpi_send_gpe_event(ACPIREGS *ar, qemu_irq irq,
hw/acpi/core.c uint32_t acpi_gpe_ioport_readb(ACPIREGS *ar, uint32_t addr) · line 742
Quoted 10 lines
hw/acpi/core.c +7
-1AcpiEventStatusBits status) { - ar->gpe.sts[0 ] |= status; +int i; +AcpiEventStatusBits st = status; + +for (i =0 ; i < ar->gpe.len /2 ; i++) { + ar->gpe.sts[i] |= st; + st >>=TYPE_WIDTH (ar->gpe.sts[0 ]); + }
Function is ever called with only one bit set at a time. I'd suggest to add assert to guard for multiple bits and use existing bitops instead of open-codding the loop.
Quoted 22 lines
hw/acpi/core.c +7
-1acpi_update_sci (ar, irq); }void acpi_update_sci (ACPIREGS *regs,qemu_irq irq) {int sci_level, pm1a_sts; +bool gpe_sci = false; +int i; pm1a_sts =acpi_pm1_evt_get_sts (regs); +for (i =0 ; i < regs->gpe.len /2 ; i++) { + gpe_sci = gpe_sci || !!(regs->gpe.sts[i] & regs->gpe.en[i]); + } + sci_level = ((pm1a_sts & regs->pm1.evt.en &ACPI_BITMASK_PM1_COMMON_ENABLED ) !=0 ) || - ((regs->gpe.sts[0 ] & regs->gpe.en[0 ]) !=0 ); + gpe_sci;qemu_set_irq (irq, sci_level);