Skip to contentSkip to conversation

Introduce a battery, AC adapter, and lid button

PATCH Superseded Leonid Bloch 4 participants 11 messages Aug 27, 2025 – Oct 23, 2025 Arm/Amlogic Meson SoC support

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 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 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
Signature · 1 line
2.51.0
#1 lore raw

PATCH v3 1/4 hw/acpi: Support extended GPE handling for additional ACPI devices

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 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
index 58f8964e13..3240ec185e 100644
@@ -729,19 +729,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.51.0
#2 lore raw

Re: [PATCH v3 1/4] hw/acpi: Support extended GPE handling for additional ACPI devices

Quoted 20 lines · On Thu, 28 Aug 2025 01:00:47 +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 byte

Quoted 6 lines

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.

---

add after this line a per patch changelog,
describing what has changed.

Quoted 22 lines
 hw/acpi/core.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
hw/acpi/core.c +8 -1
index 58f8964e13..3240ec185e 100644
@@ -729,19 +729,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);
 }

hw/acpi/core.c uint32_t acpi_gpe_ioport_readb(ACPIREGS *ar, uint32_t addr) · line 751

Quoted 10 lines
hw/acpi/core.c +3 -0
 
 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++) {
                    ^^^^^^^^^^^^^^^^^^^^
           vvvv 

hw/acpi/core.c uint32_t acpi_gpe_ioport_readb(ACPIREGS *ar, uint32_t addr) · line 752

Quoted 1 line
hw/acpi/core.c +1 -0
+        gpe_sci = gpe_sci || !!(regs->gpe.sts[i] & regs->gpe.en[i]);

once we decided to generate sci there is no need to scan array further so drop 'gpe_sci ||' and break the loop in the condition above

Quoted 9 lines
hw/acpi/core.c +3 -1
+    }
+
     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);
 
#3 lore raw

PATCH v3 2/4 hw/acpi: Introduce the QEMU Battery

In reply to Leonid Bloch

The battery device communicates battery state to the guest via ACPI. It supports two modes of operation:

1. QMP control mode (default): Battery state is controlled programmatically via QMP commands, making the device deterministic and migration-safe.

2. Host mirroring mode (optional): The device reflects the host's battery state from sysfs. Probing occurs on guest ACPI requests and at timed intervals (default 2 seconds, configurable via 'probe_interval' property in milliseconds). State changes trigger ACPI notifications to the guest.

Properties:
- 'use-qmp': Enable QMP control mode (default: true)
- 'enable-sysfs': Enable host battery mirroring (default: false)
- 'probe_interval': Probe interval in ms for sysfs mode (default: 2000)
- 'sysfs_path': Override default sysfs path /sys/class/power_supply/

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 allows testing without host battery access and provides a stable interface for virtualization management systems.

---
 MAINTAINERS                          |   6 +
 docs/specs/battery.rst               | 225 ++++++++
 docs/specs/index.rst                 |   1 +
 hw/acpi/Kconfig                      |   4 +
 hw/acpi/battery.c                    | 735 +++++++++++++++++++++++++++
 hw/acpi/meson.build                  |   1 +
 hw/acpi/trace-events                 |   5 +
 hw/i386/Kconfig                      |   1 +
 hw/i386/acpi-build.c                 |   1 +
 include/hw/acpi/acpi_dev_interface.h |   1 +
 include/hw/acpi/battery.h            |  33 ++
 qapi/acpi.json                       |  73 +++
 12 files changed, 1086 insertions(+)
 create mode 100644 docs/specs/battery.rst
 create mode 100644 hw/acpi/battery.c
 create mode 100644 include/hw/acpi/battery.h
MAINTAINERS +6 -0
index a64b5b849b..eb71a4a4b7 100644
@@ -2928,6 +2928,12 @@ F: hw/vmapple/*
 F: include/hw/vmapple/*
 F: docs/system/arm/vmapple.rst
 
+Battery
+M: Leonid Bloch <[email protected]>
+S: Maintained
+F: hw/acpi/battery.*
+F: docs/specs/battery.rst
+
 Subsystems
 ----------
 Overall Audio backends
docs/specs/battery.rst new file +225 -0

225 lines not on this page · raw message

docs/specs/index.rst +1 -0
index f19d73c9f6..616e8228cc 100644
@@ -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
hw/acpi/Kconfig +4 -0
index 1d4e9f0845..64403378bd 100644
@@ -83,3 +83,7 @@ config ACPI_ERST
 config ACPI_CXL
     bool
     depends on ACPI
+
+config BATTERY
+    bool
+    depends on ACPI
hw/acpi/battery.c new file +735 -0

735 lines not on this page · raw message

hw/acpi/meson.build +1 -0
index 73f02b9691..10379a7b2c 100644
@@ -31,6 +31,7 @@ acpi_ss.add(when: 'CONFIG_PC', if_false: files('acpi-x86-stub.c'))
 if have_tpm
   acpi_ss.add(files('tpm.c'))
 endif
+acpi_ss.add(when: 'CONFIG_BATTERY', if_true: files('battery.c'))
 system_ss.add(when: 'CONFIG_ACPI', if_false: files('acpi-stub.c', 'aml-build-stub.c', 'ghes-stub.c', 'acpi_interface.c'))
 system_ss.add(when: 'CONFIG_ACPI_PCI_BRIDGE', if_false: files('pci-bridge-stub.c'))
 system_ss.add_all(when: 'CONFIG_ACPI', if_true: acpi_ss)
hw/acpi/trace-events +5 -0
index edc93e703c..dd3e815482 100644
@@ -87,3 +87,8 @@ 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
+battery_ioport_read_unknown(void) "Battery read unknown"
hw/i386/Kconfig +1 -0
index 3a0e2b8ebb..2c878fd112 100644
@@ -39,6 +39,7 @@ config PC
     imply VIRTIO_VGA
     imply NVDIMM
     imply FDC_ISA
+    imply BATTERY
     select I8259
     select I8254
     select PCKBD
hw/i386/acpi-build.c +1 -0
index 423c4959fe..790b16e582 100644
@@ -1248,6 +1248,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
 
         aml_append(sb_scope, dev);
     }
+
     aml_append(dsdt, sb_scope);
 
     if (pm->pcihp_bridge_en || pm->pcihp_root_en) {
include/hw/acpi/acpi_dev_interface.h +1 -0
index 68d9d15f50..3064ef6734 100644
@@ -13,6 +13,7 @@ typedef enum {
     ACPI_NVDIMM_HOTPLUG_STATUS = 16,
     ACPI_VMGENID_CHANGE_STATUS = 32,
     ACPI_POWER_DOWN_STATUS = 64,
+    ACPI_BATTERY_CHANGE_STATUS = 128,
 } AcpiEventStatusBits;
 
 #define TYPE_ACPI_DEVICE_IF "acpi-device-interface"
include/hw/acpi/battery.h new file +33 -0
new file mode 100644
index 0000000000..5c5e83abfa
@@ -0,0 +1,33 @@
+/*
+ * QEMU emulated battery device.
+ *
+ * Copyright (c) 2019 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_PATH_PROP             "sysfs_path"
+#define BATTERY_PROBE_STATE_INTERVAL  "probe_interval"
+
+#define BATTERY_FULL_CAP     10000  /* mWh */
+
+#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 +73 -0

76 lines not on this page · raw message

Signature · 1 line
2.51.0
#4 lore raw

Re: [PATCH v3 2/4] hw/acpi: Introduce the QEMU Battery

Quoted 30 lines · On Thu, 28 Aug 2025 01:00:48 +0300 Leonid Bloch <[email protected]> wrote:

The battery device communicates battery state to the guest via ACPI. It supports two modes of operation:

1. QMP control mode (default): Battery state is controlled programmatically via QMP commands, making the device deterministic and migration-safe.

2. Host mirroring mode (optional): The device reflects the host's battery state from sysfs. Probing occurs on guest ACPI requests and at timed intervals (default 2 seconds, configurable via 'probe_interval' property in milliseconds). State changes trigger ACPI notifications to the guest.

Properties:
- 'use-qmp': Enable QMP control mode (default: true)
- 'enable-sysfs': Enable host battery mirroring (default: false)
- 'probe_interval': Probe interval in ms for sysfs mode (default: 2000)
- 'sysfs_path': Override default sysfs path /sys/class/power_supply/

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 allows testing without host battery access and provides a stable interface for virtualization management systems.

[...]

Quoted 135 lines
hw/acpi/battery.c new file +135 -0
+
+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")));
+
+    method = aml_method("_STA", 0, AML_NOTSERIALIZED);
+    aml_append(method, aml_return(aml_int(0x1F)));
+    aml_append(dev, method);
+
+    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("_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_VAL_UNKNOWN));
+    /* 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_VAL_UNKNOWN));
+    aml_append(dev, aml_name_decl("DBPR", pkg));
+
+    method = aml_method("_BST", 0, AML_NOTSERIALIZED);
+    aml_append(method, aml_store(aml_name("BSTA"), 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);
+
+    /* Device Check */
+    method = aml_method("\\_GPE._E07", 0, AML_NOTSERIALIZED);
+    aml_append(method, aml_notify(aml_name("\\_SB.BAT0"), aml_int(0x01)));
+    aml_append(scope, method);
+
+    /* 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);
+
+    /* Information Change */
+    method = aml_method("\\_GPE._E09", 0, AML_NOTSERIALIZED);
+    aml_append(method, aml_notify(aml_name("\\_SB.BAT0"), aml_int(0x81)));
+    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;
+    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_addr0:
+        return s->state.acc[0];
+    case bsta_addr1:
+        return s->state.acc[1];
+    case bsta_addr2:
+        return s->state.acc[2];
+    case bsta_addr3:
+        return s->state.acc[3];
+    case brte_addr0:
+        return s->rate.acc[0];
+    case brte_addr1:
+        return s->rate.acc[1];
+    case brte_addr2:
+        return s->rate.acc[2];

hw/acpi/battery.c line 625

Quoted 10 lines
hw/acpi/battery.c new file +10 -0
+    case brte_addr3:
+        return s->rate.acc[3];
+    case bcrg_addr0:
+        return s->charge.acc[0];
+    case bcrg_addr1:
+        return s->charge.acc[1];
+    case bcrg_addr2:
+        return s->charge.acc[2];
+    case bcrg_addr3:
+        return s->charge.acc[3];

why do you handle it as byte access when in AML fields are declared as DWORD access?

hw/acpi/battery.c line 627

Quoted 2 lines
hw/acpi/battery.c new file +2 -0
+    default:
+        warn_report("Battery: guest read unknown value.");

abort here, but I doubt that you can get here at all if memory region is correctly sized.

hw/acpi/battery.c line 637

Quoted 10 lines
hw/acpi/battery.c new file +10 -0
+        trace_battery_ioport_read_unknown();
+        return 0;
+    }
+}
+
+static const MemoryRegionOps battery_ops = {
+    .read = battery_ioport_read,
+    .impl = {
+        .min_access_size = 1,
+        .max_access_size = 1,

same question wrt access size.
Suggest making it explicitly 4 byte and little-endinan

Quoted 16 lines
hw/acpi/battery.c new file +16 -0
+    },
+};
+
+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,

hw/acpi/battery.c line 663

Quoted 10 lines
hw/acpi/battery.c new file +10 -0
+    .instance_init = battery_instance_init,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_ACPI_DEV_AML_IF },
+        { },
+    },
+};
+
+static BatteryState *find_battery_device(void)
+{
+    Object *o = object_resolve_path_type("", TYPE_BATTERY, NULL);

also use 3rd argument and error out in case it's true, so user would know what went wrong.

Quoted 253 lines
hw/acpi/battery.c new file +72 -0
+    if (!o) {
+        return NULL;
+    }
+    return BATTERY_DEVICE(o);
+}
+
+void qmp_battery_set_state(BatteryInfo *state, Error **errp)
+{
+    BatteryState *s = find_battery_device();
+
+    if (!s) {
+        error_setg(errp, "No battery device found");
+        return;
+    }
+
+    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;
+    }
+
+    Object *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();
+    BatteryInfo *ret;
+
+    if (!s) {
+        error_setg(errp, "No battery device found");
+        return NULL;
+    }
+
+    ret = g_new0(BatteryInfo, 1);
+
+    if (s->use_qmp_control) {
+        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;
+    } else {
+        battery_get_dynamic_status(s);
+        ret->present = true;
+        ret->charging = !!(s->state.val & BATTERY_CHARGING);
+        ret->discharging = !!(s->state.val & BATTERY_DISCHARGING);
+        ret->charge_percent = (s->charge.val * 100) / BATTERY_FULL_CAP;
+        ret->has_rate = true;
+        ret->rate = s->rate.val;
+    }
+
+    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 +1 -0
index 73f02b9691..10379a7b2c 100644
@@ -31,6 +31,7 @@ acpi_ss.add(when: 'CONFIG_PC', if_false: files('acpi-x86-stub.c'))
 if have_tpm
   acpi_ss.add(files('tpm.c'))
 endif
+acpi_ss.add(when: 'CONFIG_BATTERY', if_true: files('battery.c'))
 system_ss.add(when: 'CONFIG_ACPI', if_false: files('acpi-stub.c', 'aml-build-stub.c', 'ghes-stub.c', 'acpi_interface.c'))
 system_ss.add(when: 'CONFIG_ACPI_PCI_BRIDGE', if_false: files('pci-bridge-stub.c'))
 system_ss.add_all(when: 'CONFIG_ACPI', if_true: acpi_ss)
hw/acpi/trace-events +5 -0
index edc93e703c..dd3e815482 100644
@@ -87,3 +87,8 @@ 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
+battery_ioport_read_unknown(void) "Battery read unknown"
hw/i386/Kconfig +1 -0
index 3a0e2b8ebb..2c878fd112 100644
@@ -39,6 +39,7 @@ config PC
     imply VIRTIO_VGA
     imply NVDIMM
     imply FDC_ISA
+    imply BATTERY
     select I8259
     select I8254
     select PCKBD
hw/i386/acpi-build.c +1 -0
index 423c4959fe..790b16e582 100644
@@ -1248,6 +1248,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
 
         aml_append(sb_scope, dev);
     }
+
     aml_append(dsdt, sb_scope);
 
     if (pm->pcihp_bridge_en || pm->pcihp_root_en) {
include/hw/acpi/acpi_dev_interface.h +1 -0
index 68d9d15f50..3064ef6734 100644
@@ -13,6 +13,7 @@ typedef enum {
     ACPI_NVDIMM_HOTPLUG_STATUS = 16,
     ACPI_VMGENID_CHANGE_STATUS = 32,
     ACPI_POWER_DOWN_STATUS = 64,
+    ACPI_BATTERY_CHANGE_STATUS = 128,
 } AcpiEventStatusBits;
 
 #define TYPE_ACPI_DEVICE_IF "acpi-device-interface"
include/hw/acpi/battery.h new file +33 -0
new file mode 100644
index 0000000000..5c5e83abfa
@@ -0,0 +1,33 @@
+/*
+ * QEMU emulated battery device.
+ *
+ * Copyright (c) 2019 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_PATH_PROP             "sysfs_path"
+#define BATTERY_PROBE_STATE_INTERVAL  "probe_interval"
+
+#define BATTERY_FULL_CAP     10000  /* mWh */
+
+#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 +73 -0
index 906b3687a5..d1ad663bfd 100644
@@ -142,3 +142,76 @@
 ##
 { 'event': 'ACPI_DEVICE_OST',
      'data': { 'info': 'ACPIOSTInfo' } }
+
+##
+# @BatteryInfo:
+#
+# Battery state information
+#
+# @present: whether the battery is present
+#
+# @charging: whether the battery is charging
+#
+# @discharging: whether the battery is discharging
+#
+# @charge-percent: battery charge percentage (0-100)
+#
+# @rate: charge/discharge rate in mW (optional)
+#
+# @remaining-capacity: remaining capacity in mWh (optional)
+#
+# @design-capacity: design capacity in mWh (optional)
+#
+# Since: 10.2
+##
+{ '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: 10.2
+#
+# .. qmp-example::
+#
+#     -> { "execute": "battery-set-state",
+#          "arguments": { "state": { "present": true,
+#                                    "charging": true,
+#                                    "discharging": false,
+#                                    "charge-percent": 85 } } }
+#     <- { "return": {} }
+##
+{ 'command': 'battery-set-state',
+  'data': { 'state': 'BatteryInfo' } }
+
+##
+# @query-battery:
+#
+# Query the current state of the emulated battery device
+#
+# Returns: current battery state
+#
+# Since: 10.2
+#
+# .. qmp-example::
+#
+#     -> { "execute": "query-battery" }
+#     <- { "return": { "present": true,
+#                      "charging": true,
+#                      "discharging": false,
+#                      "charge-percent": 85 } }
+##
+{ 'command': 'query-battery',
+  'returns': 'BatteryInfo' }
#5 lore raw

Re: [PATCH v3 2/4] hw/acpi: Introduce the QEMU Battery

In reply to Leonid Bloch
Quoted 1,232 lines · Leonid Bloch

The battery device communicates battery state to the guest via ACPI. It supports two modes of operation:

1. QMP control mode (default): Battery state is controlled programmatically via QMP commands, making the device deterministic and migration-safe.

2. Host mirroring mode (optional): The device reflects the host's battery state from sysfs. Probing occurs on guest ACPI requests and at timed intervals (default 2 seconds, configurable via 'probe_interval' property in milliseconds). State changes trigger ACPI notifications to the guest.

Properties:
- 'use-qmp': Enable QMP control mode (default: true)
- 'enable-sysfs': Enable host battery mirroring (default: false)
- 'probe_interval': Probe interval in ms for sysfs mode (default: 2000)
- 'sysfs_path': Override default sysfs path /sys/class/power_supply/

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 allows testing without host battery access and provides a stable interface for virtualization management systems.

---
  MAINTAINERS                          |   6 +
  docs/specs/battery.rst               | 225 ++++++++
  docs/specs/index.rst                 |   1 +
  hw/acpi/Kconfig                      |   4 +
  hw/acpi/battery.c                    | 735 +++++++++++++++++++++++++++
  hw/acpi/meson.build                  |   1 +
  hw/acpi/trace-events                 |   5 +
  hw/i386/Kconfig                      |   1 +
  hw/i386/acpi-build.c                 |   1 +
  include/hw/acpi/acpi_dev_interface.h |   1 +
  include/hw/acpi/battery.h            |  33 ++
  qapi/acpi.json                       |  73 +++
  12 files changed, 1086 insertions(+)

create mode 100644 docs/specs/battery.rst
create mode 100644 hw/acpi/battery.c
create mode 100644 include/hw/acpi/battery.h

MAINTAINERS +6 -0
index a64b5b849b..eb71a4a4b7 100644
@@ -2928,6 +2928,12 @@ F: hw/vmapple/*
  F: include/hw/vmapple/*
  F: docs/system/arm/vmapple.rst
 
+Battery
+M: Leonid Bloch <[email protected]>
+S: Maintained
+F: hw/acpi/battery.*
+F: docs/specs/battery.rst
+
  Subsystems
  ----------
  Overall Audio backends
docs/specs/battery.rst new file +225 -0
new file mode 100644
index 0000000000..e426b91ea5
@@ -0,0 +1,225 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+
+==============
+Battery Device
+==============
+
+The battery device provides battery state information to the guest. It supports
+two operating modes:
+
+1. **QMP Control Mode** (default): Battery state is controlled via QMP commands,
+   providing deterministic control for testing and migration safety.
+2. **Sysfs Mode**: Battery state mirrors the host's physical battery, useful
+   for desktop virtualization where the guest should see the host's battery.
+
+Configuration
+-------------
+
+The battery device is created as an ISA device using ``-device battery``.
+
+Operating Modes
+~~~~~~~~~~~~~~~
+
+**QMP Control Mode** (``use-qmp=true``, default)
+  Battery state is controlled via QMP commands. This mode is recommended for:
+
+  * Production environments requiring migration support
+  * Testing with predictable battery states
+  * Environments without host battery access
+  * Security-sensitive deployments
+
+**Sysfs Mode** (``enable-sysfs=true``)
+  Battery mirrors the host's physical battery. This mode is useful for:
+
+  * Desktop virtualization on laptops
+  * Development and testing with real battery behavior
+
+  Note: Sysfs mode reads host files and runs timers, which may impact
+  security and migration. Use with caution in production.
+
+Properties
+~~~~~~~~~~
+
+``ioport`` (default: 0x530)
+  I/O port base address for the battery device registers.
+
+``use-qmp`` (default: true)
+  Enable QMP control mode. When true, battery state is controlled via
+  QMP commands. Cannot be used together with ``enable-sysfs=true``.
+
+``enable-sysfs`` (default: false)
+  Enable sysfs mode to mirror the host's battery. Cannot be used together
+  with ``use-qmp=true``.
+
+``probe_interval`` (default: 2000)
+  Time interval between periodic probes in milliseconds (sysfs mode only).
+  A zero value disables the periodic probes, and makes the battery state
+  updates occur on guest requests only.
+
+``sysfs_path`` (default: auto-detected)
+  Path to the host's battery sysfs directory (sysfs mode only). If not
+  specified, the device will automatically detect the battery from
+  ``/sys/class/power_supply/``. This property allows overriding the default
+  path if:
+
+  * The sysfs path differs from the standard location
+  * A different battery needs to be probed
+  * A "fake" host battery is to be provided for testing
+
+Host Battery Detection
+----------------------
+
+The host's battery information is taken from the sysfs battery data,
+located in::
+
+  /sys/class/power_supply/[device of type "Battery"]
+
+The device automatically scans for the first available battery device
+with type "Battery" in the power_supply directory.
+
+ACPI Interface
+--------------
+
+The battery device exposes itself as an ACPI battery device with:
+
+* **_HID**: ``PNP0C0A`` (Control Method Battery)
+* **Device path**: ``\_SB.BAT0``
+
+The device implements standard ACPI battery methods:
+
+``_STA`` (Status)
+  Returns the battery presence status.
+
+``_BIF`` (Battery Information)
+  Returns static battery information including design capacity,
+  technology, and model information.
+
+``_BST`` (Battery Status)
+  Returns dynamic battery status including current state
+  (charging/discharging), present rate, and remaining capacity.
+
+I/O Interface
+-------------
+
+The battery device exposes 12 bytes of I/O space at the configured
+I/O port address with the following layout:
+
+* **Bytes 0-3**: Battery state (DWORD)
+
+  * 1 = Discharging
+  * 2 = Charging
+
+* **Bytes 4-7**: Battery rate (DWORD)
+
+  Current charge/discharge rate normalized to design capacity.
+
+* **Bytes 8-11**: Battery charge (DWORD)
+
+  Current battery charge level normalized to design capacity.
+
+All values are normalized where the design capacity equals 10000 units.
+Unknown values are represented as 0xFFFFFFFF.
+
+ACPI Notifications
+------------------
+
+The battery device generates ACPI notifications through GPE events:
+
+* **GPE._E07**: Device Check (0x01) - Battery insertion/removal
+* **GPE._E08**: Status Change (0x80) - Battery state change
+* **GPE._E09**: Information Change (0x81) - Battery information update
+
+QMP Commands
+------------
+
+When using QMP control mode (default), the following commands are available:
+
+``battery-set-state``
+  Set the battery state. Parameters:
+
+  * ``present``: Whether the battery is present (boolean)
+  * ``charging``: Whether the battery is charging (boolean)
+  * ``discharging``: Whether the battery is discharging (boolean)
+  * ``charge-percent``: Battery charge percentage 0-100 (integer)
+  * ``rate``: Charge/discharge rate in mW (optional integer)
+
+  Example::
+
+    -> { "execute": "battery-set-state",
+         "arguments": { "state": {
+           "present": true,
+           "charging": true,
+           "discharging": false,
+           "charge-percent": 85,
+           "rate": 500
+         }}}
+    <- { "return": {} }
+
+``query-battery``
+  Query the current battery state. Returns the same fields as above.
+
+  Example::
+
+    -> { "execute": "query-battery" }
+    <- { "return": {
+           "present": true,
+           "charging": true,
+           "discharging": false,
+           "charge-percent": 85,
+           "rate": 500,
+           "design-capacity": 10000
+         }}
+
+Examples
+--------
+
+QMP control mode (default - recommended)::
+
+  # Start with QMP control
+  qemu-system-x86_64 -device battery -qmp tcp:localhost:4444,server,wait=off
+
+  # From another terminal, set battery state via QMP:
+  echo '{"execute":"qmp_capabilities"}
+        {"execute":"battery-set-state",
+         "arguments":{"state":{"present":true,"charging":false,
+                               "discharging":true,"charge-percent":42,
+                               "rate":500}}}' | \
+  nc -N localhost 4444
+
+Sysfs mode (mirror host battery)::
+
+  # Enable sysfs mode to mirror host battery
+  qemu-system-x86_64 -device battery,use-qmp=false,enable-sysfs=true
+
+  # Custom probe interval (5 seconds)
+  qemu-system-x86_64 -device battery,use-qmp=false,enable-sysfs=true,probe_interval=5000
+
+  # Specific battery path
+  qemu-system-x86_64 -device battery,use-qmp=false,enable-sysfs=true,sysfs_path=/sys/class/power_supply/BAT1
+
+Testing with fake battery::
+
+  # Create fake battery files for testing
+  mkdir -p /tmp/fake_battery
+  echo "Battery" > /tmp/fake_battery/type
+  echo "Discharging" > /tmp/fake_battery/status
+  echo "50" > /tmp/fake_battery/capacity
+  echo "1500000" > /tmp/fake_battery/energy_now    # Current energy in μWh
+  echo "3000000" > /tmp/fake_battery/energy_full   # Full capacity in μWh
+  echo "500000" > /tmp/fake_battery/power_now      # Current power in μW
+
+  # Use fake battery in sysfs mode
+  qemu-system-x86_64 -device battery,use-qmp=false,enable-sysfs=true,sysfs_path=/tmp/fake_battery
+
+  # Update battery state while VM is running (from another terminal)
+  # Change to 75% charging:
+  echo "Charging" > /tmp/fake_battery/status
+  echo "75" > /tmp/fake_battery/capacity
+  echo "2250000" > /tmp/fake_battery/energy_now    # 75% of 3000000
+  echo "500000" > /tmp/fake_battery/power_now      # Charging rate (500 mW)
+
+  # Change to 25% discharging:
+  echo "Discharging" > /tmp/fake_battery/status
+  echo "25" > /tmp/fake_battery/capacity
+  echo "750000" > /tmp/fake_battery/energy_now     # 25% of 3000000
+  echo "300000" > /tmp/fake_battery/power_now      # Discharge rate (300 mW)
docs/specs/index.rst +1 -0
index f19d73c9f6..616e8228cc 100644
@@ -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
hw/acpi/Kconfig +4 -0
index 1d4e9f0845..64403378bd 100644
@@ -83,3 +83,7 @@ config ACPI_ERST
  config ACPI_CXL
      bool
      depends on ACPI
+
+config BATTERY
+    bool
+    depends on ACPI
hw/acpi/battery.c new file +735 -0
new file mode 100644
index 0000000000..5ab3ab7472
@@ -0,0 +1,735 @@
+/*
+ * QEMU emulated battery device.
+ *
+ * Copyright (c) 2019 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 "hw/nvram/fw_cfg.h"
+#include "qapi/error.h"
+#include "qemu/error-report.h"
+#include "hw/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 SYSFS_PATH       "/sys/class/power_supply"
+#define BATTERY_TYPE     "Battery"
+
+#define MAX_ALLOWED_STATE_LENGTH  32  /* For convinience when comparing */
+
+#define NORMALIZE_BY_FULL(val, full) \
+    ((full == 0) ? BATTERY_VAL_UNKNOWN \
+     : (uint32_t)(val * BATTERY_FULL_CAP / full))
+
+typedef union bat_metric {
+    uint32_t val;
+    uint8_t acc[4];
+} bat_metric;
+
+typedef struct BatteryState {
+    ISADevice dev;
+    MemoryRegion io;
+    uint16_t ioport;
+    bat_metric state;
+    bat_metric rate;
+    bat_metric charge;
+    uint32_t charge_full;
+    int units;  /* 0 - mWh, 1 - mAh */
+    bool use_qmp_control;
+    bool qmp_present;
+    bool qmp_charging;
+    bool qmp_discharging;
+    int qmp_charge_percent;
+    int qmp_rate;
+    bool enable_sysfs;
+
+    QEMUTimer *probe_state_timer;
+    uint64_t probe_state_interval;
+
+    char *bat_path;
+} BatteryState;
+
+/* Access addresses */
+enum acc_addr {
+    bsta_addr0, bsta_addr1, bsta_addr2, bsta_addr3,
+    brte_addr0, brte_addr1, brte_addr2, brte_addr3,
+    bcrg_addr0, bcrg_addr1, bcrg_addr2, bcrg_addr3
+};
+
+/* Files used when the units are:      mWh             mAh      */
+static const char *full_file[] = { "energy_full", "charge_full" };
+static const char *now_file[]  = { "energy_now",  "charge_now"  };
+static const char *rate_file[] = { "power_now",   "current_now" };
+
+static const char *stat_file = "status";
+static const char *type_file = "type";
+
+static const char *discharging_states[] = { "Discharging", "Not charging" };
+static const char *charging_states[] = { "Charging", "Full", "Unknown" };
+
+static inline bool battery_file_accessible(char *path, const char *file)
+{
+    char full_path[PATH_MAX];
+    int path_len;
+
+    path_len = snprintf(full_path, PATH_MAX, "%s/%s", path, file);
+    if (path_len < 0 || path_len >= PATH_MAX) {
+        return false;
+    }
+    if (access(full_path, R_OK) == 0) {
+        return true;
+    }
+    return false;
+}
+
+static inline int battery_select_file(char *path, const char **file)
+{
+    if (battery_file_accessible(path, file[0])) {
+        return 0;
+    } else if (battery_file_accessible(path, file[1])) {
+        return 1;
+    } else {
+        return -1;
+    }
+}
+
+static void battery_get_full_charge(BatteryState *s, Error **errp)
+{
+    char file_path[PATH_MAX];
+    int path_len;
+    uint32_t val;
+    FILE *ff;
+
+    path_len = snprintf(file_path, PATH_MAX, "%s/%s", s->bat_path,
+                        full_file[s->units]);
+    if (path_len < 0 || path_len >= PATH_MAX) {
+        error_setg(errp, "Full capacity file path is inaccessible.");
+        return;
+    }
+
+    ff = fopen(file_path, "r");
+    if (ff == NULL) {
+        error_setg_errno(errp, errno, "Could not read the full charge file.");
+        return;
+    }
+
+    if (fscanf(ff, "%u", &val) != 1) {
+        error_setg(errp, "Full capacity undetermined.");
+        return;
+    } else {
+        s->charge_full = val;
+    }
+    fclose(ff);
+}
+
+static inline bool battery_is_discharging(char *val)
+{
+    static const int discharging_len = ARRAY_SIZE(discharging_states);
+    int i;
+
+    for (i = 0; i < discharging_len; i++) {
+        if (!strncmp(val, discharging_states[i], MAX_ALLOWED_STATE_LENGTH)) {
+            return true;
+        }
+    }
+    return false;
+}
+
+static inline bool battery_is_charging(char *val)
+{
+    static const int charging_len = ARRAY_SIZE(charging_states);
+    int i;
+
+    for (i = 0; i < charging_len; i++) {
+        if (!strncmp(val, charging_states[i], MAX_ALLOWED_STATE_LENGTH)) {
+            return true;
+        }
+    }
+    return false;
+}
+
+static void battery_get_state(BatteryState *s)
+{
+    char file_path[PATH_MAX];
+    int path_len;
+    char val[MAX_ALLOWED_STATE_LENGTH];
+    FILE *ff;
+
+    path_len = snprintf(file_path, PATH_MAX, "%s/%s", s->bat_path, stat_file);
+    if (path_len < 0 || path_len >= PATH_MAX) {
+        warn_report("Could not read the battery state.");
+        return;
+    }
+
+    ff = fopen(file_path, "r");
+    if (ff == NULL) {
+        warn_report("Could not read the battery state.");
+        return;
+    }
+
+    if (fgets(val, MAX_ALLOWED_STATE_LENGTH, ff) == NULL) {
+        warn_report("Battery state unreadable.");
+    } else {
+        val[strcspn(val, "\n")] = 0;
+        if (battery_is_discharging(val)) {
+            s->state.val = BATTERY_DISCHARGING;
+        } else if (battery_is_charging(val)) {
+            s->state.val = BATTERY_CHARGING;
+        } else {
+            s->state.val = 0;
+            warn_report("Battery state undetermined.");
+        }
+    }
+    fclose(ff);
+}
+
+static void battery_get_rate(BatteryState *s)
+{
+    char file_path[PATH_MAX];
+    int path_len;
+    uint64_t val;
+    FILE *ff;
+
+    path_len = snprintf(file_path, PATH_MAX, "%s/%s", s->bat_path,
+                        rate_file[s->units]);
+    if (path_len < 0 || path_len >= PATH_MAX) {
+        warn_report("Could not read the battery rate.");
+        s->rate.val = BATTERY_VAL_UNKNOWN;
+        return;
+    }
+
+    ff = fopen(file_path, "r");
+    if (ff == NULL) {
+        warn_report("Could not read the battery rate.");
+        s->rate.val = BATTERY_VAL_UNKNOWN;
+        return;
+    }
+
+    if (fscanf(ff, "%lu", &val) != 1) {
+        warn_report("Battery rate undetermined.");
+        s->rate.val = BATTERY_VAL_UNKNOWN;
+    } else {
+        s->rate.val = NORMALIZE_BY_FULL(val, s->charge_full);
+    }
+    fclose(ff);
+}
+
+static void battery_get_charge(BatteryState *s)
+{
+    char file_path[PATH_MAX];
+    int path_len;
+    uint64_t val;
+    FILE *ff;
+
+    path_len = snprintf(file_path, PATH_MAX, "%s/%s", s->bat_path,
+                        now_file[s->units]);
+    if (path_len < 0 || path_len >= PATH_MAX) {
+        warn_report("Could not read the battery charge.");
+        s->charge.val = BATTERY_VAL_UNKNOWN;
+        return;
+    }
+
+    ff = fopen(file_path, "r");
+    if (ff == NULL) {
+        warn_report("Could not read the battery charge.");
+        s->charge.val = BATTERY_VAL_UNKNOWN;
+        return;
+    }
+
+    if (fscanf(ff, "%lu", &val) != 1) {
+        warn_report("Battery charge undetermined.");
+        s->charge.val = BATTERY_VAL_UNKNOWN;
+    } else {
+        s->charge.val = NORMALIZE_BY_FULL(val, s->charge_full);
+    }
+    fclose(ff);
+}
+
+static void battery_get_dynamic_status(BatteryState *s)
+{
+    if (s->use_qmp_control) {
+        s->state.val = 0;
+        if (s->qmp_present) {
+            if (s->qmp_charging) {
+                s->state.val |= BATTERY_CHARGING;
+            }
+            if (s->qmp_discharging) {
+                s->state.val |= BATTERY_DISCHARGING;
+            }
+        }
+        s->rate.val = s->qmp_rate;
+        s->charge.val = (s->qmp_charge_percent * BATTERY_FULL_CAP) / 100;
+    } else if (s->enable_sysfs) {
+        battery_get_state(s);
+        battery_get_rate(s);
+        battery_get_charge(s);
+    } else {
+        s->state.val = 0;
+        s->rate.val = 0;
+        s->charge.val = 0;
+    }
+
+    trace_battery_get_dynamic_status(s->state.val, s->rate.val, s->charge.val);
+}
+
+static void battery_probe_state(void *opaque)
+{
+    BatteryState *s = opaque;
+
+    uint32_t state_before = s->state.val;
+    uint32_t rate_before = s->rate.val;
+    uint32_t charge_before = s->charge.val;
+
+    battery_get_dynamic_status(s);
+
+    if (state_before != s->state.val || rate_before != s->rate.val ||
+        charge_before != s->charge.val) {
+        Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, NULL);
+        switch (charge_before) {
+        case 0:
+            break;  /* Avoid marking initiation as an update */
+        default:
+            acpi_send_event(DEVICE(obj), ACPI_BATTERY_CHANGE_STATUS);
+        }
+    }
+    timer_mod(s->probe_state_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
+              s->probe_state_interval);
+}
+
+static void battery_probe_state_timer_init(BatteryState *s)
+{
+    if (s->enable_sysfs && s->probe_state_interval > 0) {
+        s->probe_state_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
+                                            battery_probe_state, s);
+        timer_mod(s->probe_state_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
+                  s->probe_state_interval);
+    }
+}
+
+static bool battery_verify_sysfs(BatteryState *s, char *path)
+{
+    int units;
+    FILE *ff;
+    char type_path[PATH_MAX];
+    int path_len;
+    char val[MAX_ALLOWED_STATE_LENGTH];
+
+    path_len = snprintf(type_path, PATH_MAX, "%s/%s", path, type_file);
+    if (path_len < 0 || path_len >= PATH_MAX) {
+        return false;
+    }
+    ff = fopen(type_path, "r");
+    if (ff == NULL) {
+        return false;
+    }
+
+    if (fgets(val, MAX_ALLOWED_STATE_LENGTH, ff) == NULL) {
+        fclose(ff);
+        return false;
+    } else {
+        val[strcspn(val, "\n")] = 0;
+        if (strncmp(val, BATTERY_TYPE, MAX_ALLOWED_STATE_LENGTH)) {
+            fclose(ff);
+            return false;
+        }
+    }
+    fclose(ff);
+
+    units = battery_select_file(path, full_file);
+
+    if (units < 0) {
+        return false;
+    } else {
+        s->units = units;
+    }
+
+    return (battery_file_accessible(path, now_file[s->units])
+            & battery_file_accessible(path, rate_file[s->units])
+            & battery_file_accessible(path, stat_file));
+}
+
+static bool get_battery_path(DeviceState *dev)
+{
+    BatteryState *s = BATTERY_DEVICE(dev);
+    DIR *dir;
+    struct dirent *ent;
+    char bp[PATH_MAX];
+    int path_len;
+
+    if (s->bat_path) {
+        return battery_verify_sysfs(s, s->bat_path);
+    }
+
+    dir = opendir(SYSFS_PATH);
+    if (dir == NULL) {
+        return false;
+    }
+
+    ent = readdir(dir);
+    while (ent != NULL) {
+        if (ent->d_name[0] != '.') {
+            path_len = snprintf(bp, PATH_MAX, "%s/%s", SYSFS_PATH,
+                                ent->d_name);
+            if (path_len < 0 || path_len >= PATH_MAX) {
+                return false;
+            }
+            if (battery_verify_sysfs(s, bp)) {
+                qdev_prop_set_string(dev, BATTERY_PATH_PROP, bp);
+                closedir(dir);
+                return true;
+            }
+        }
+        ent = readdir(dir);
+    }
+    closedir(dir);
+
+    return false;
+}
+
+static void battery_realize(DeviceState *dev, Error **errp)
+{
+    ISADevice *d = ISA_DEVICE(dev);
+    BatteryState *s = BATTERY_DEVICE(dev);
+    FWCfgState *fw_cfg = fw_cfg_find();
+    uint16_t *battery_port;
+    char err_details[0x20] = {};
+
+    trace_battery_realize();
+
+    if (s->use_qmp_control && s->enable_sysfs) {
+        error_setg(errp, "Cannot enable both QMP control and sysfs mode");
+        return;
+    }
+
+    /* Initialize QMP state to sensible defaults when in QMP mode */
+    if (s->use_qmp_control) {
+        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 */
+    }
+
+    if (s->enable_sysfs) {
+        if (!s->bat_path) {
+            strcpy(err_details, " Try using 'sysfs_path='");
+        }
+
+        if (!get_battery_path(dev)) {
+            error_setg(errp, "Battery sysfs path not found or unreadable.%s",
+                       err_details);
+            return;
+        }
+        battery_get_full_charge(s, errp);
+    } else {
+        s->charge_full = BATTERY_FULL_CAP;
+    }
+
+    isa_register_ioport(d, &s->io, s->ioport);
+
+    battery_probe_state_timer_init(s);
+
+    if (!fw_cfg) {
+        return;
+    }
+
+    battery_port = g_malloc(sizeof(*battery_port));
+    *battery_port = cpu_to_le16(s->ioport);
+    fw_cfg_add_file(fw_cfg, "etc/battery-port", battery_port,
+                    sizeof(*battery_port));
+}
+
+static const Property battery_device_properties[] = {
+    DEFINE_PROP_UINT16(BATTERY_IOPORT_PROP, BatteryState, ioport, 0x530),
+    DEFINE_PROP_BOOL("use-qmp", BatteryState, use_qmp_control, true),
+    DEFINE_PROP_BOOL("enable-sysfs", BatteryState, enable_sysfs, false),
+    DEFINE_PROP_UINT64(BATTERY_PROBE_STATE_INTERVAL, BatteryState,
+                       probe_state_interval, 2000),
+    DEFINE_PROP_STRING(BATTERY_PATH_PROP, BatteryState, bat_path),
+};
+
+static const VMStateDescription battery_vmstate = {
+    .name = "battery",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT16(ioport, BatteryState),
+        VMSTATE_UINT64(probe_state_interval, 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")));
+
+    method = aml_method("_STA", 0, AML_NOTSERIALIZED);
+    aml_append(method, aml_return(aml_int(0x1F)));
+    aml_append(dev, method);
+
+    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("_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_VAL_UNKNOWN));
+    /* 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_VAL_UNKNOWN));
+    aml_append(dev, aml_name_decl("DBPR", pkg));
+
+    method = aml_method("_BST", 0, AML_NOTSERIALIZED);
+    aml_append(method, aml_store(aml_name("BSTA"), 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);
+
+    /* Device Check */
+    method = aml_method("\\_GPE._E07", 0, AML_NOTSERIALIZED);
+    aml_append(method, aml_notify(aml_name("\\_SB.BAT0"), aml_int(0x01)));
+    aml_append(scope, method);
+
+    /* 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);
+
+    /* Information Change */
+    method = aml_method("\\_GPE._E09", 0, AML_NOTSERIALIZED);
+    aml_append(method, aml_notify(aml_name("\\_SB.BAT0"), aml_int(0x81)));
+    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;
+    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_addr0:
+        return s->state.acc[0];
+    case bsta_addr1:
+        return s->state.acc[1];
+    case bsta_addr2:
+        return s->state.acc[2];
+    case bsta_addr3:
+        return s->state.acc[3];
+    case brte_addr0:
+        return s->rate.acc[0];
+    case brte_addr1:
+        return s->rate.acc[1];
+    case brte_addr2:
+        return s->rate.acc[2];
+    case brte_addr3:
+        return s->rate.acc[3];
+    case bcrg_addr0:
+        return s->charge.acc[0];
+    case bcrg_addr1:
+        return s->charge.acc[1];
+    case bcrg_addr2:
+        return s->charge.acc[2];
+    case bcrg_addr3:
+        return s->charge.acc[3];
+    default:
+        warn_report("Battery: guest read unknown value.");
+        trace_battery_ioport_read_unknown();
+        return 0;
+    }
+}
+
+static const MemoryRegionOps battery_ops = {
+    .read = battery_ioport_read,
+    .impl = {
+        .min_access_size = 1,
+        .max_access_size = 1,
+    },
+};
+
+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(void)
+{
+    Object *o = object_resolve_path_type("", TYPE_BATTERY, NULL);
+    if (!o) {
+        return NULL;
+    }
+    return BATTERY_DEVICE(o);
+}
+
+void qmp_battery_set_state(BatteryInfo *state, Error **errp)
+{
+    BatteryState *s = find_battery_device();
+
+    if (!s) {
+        error_setg(errp, "No battery device found");
+        return;
+    }
+
+    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;
+    }
+
+    Object *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();
+    BatteryInfo *ret;
+
+    if (!s) {
+        error_setg(errp, "No battery device found");
+        return NULL;
+    }
+
+    ret = g_new0(BatteryInfo, 1);
+
+    if (s->use_qmp_control) {
+        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;
+    } else {
+        battery_get_dynamic_status(s);
+        ret->present = true;
+        ret->charging = !!(s->state.val & BATTERY_CHARGING);
+        ret->discharging = !!(s->state.val & BATTERY_DISCHARGING);
+        ret->charge_percent = (s->charge.val * 100) / BATTERY_FULL_CAP;
+        ret->has_rate = true;
+        ret->rate = s->rate.val;
+    }
+
+    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 +1 -0
index 73f02b9691..10379a7b2c 100644
@@ -31,6 +31,7 @@ acpi_ss.add(when: 'CONFIG_PC', if_false: files('acpi-x86-stub.c'))
  if have_tpm
    acpi_ss.add(files('tpm.c'))
  endif
+acpi_ss.add(when: 'CONFIG_BATTERY', if_true: files('battery.c'))
  system_ss.add(when: 'CONFIG_ACPI', if_false: files('acpi-stub.c', 'aml-build-stub.c', 'ghes-stub.c', 'acpi_interface.c'))
  system_ss.add(when: 'CONFIG_ACPI_PCI_BRIDGE', if_false: files('pci-bridge-stub.c'))
  system_ss.add_all(when: 'CONFIG_ACPI', if_true: acpi_ss)
hw/acpi/trace-events +5 -0
index edc93e703c..dd3e815482 100644
@@ -87,3 +87,8 @@ 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
+battery_ioport_read_unknown(void) "Battery read unknown"
hw/i386/Kconfig +1 -0
index 3a0e2b8ebb..2c878fd112 100644
@@ -39,6 +39,7 @@ config PC
      imply VIRTIO_VGA
      imply NVDIMM
      imply FDC_ISA
+    imply BATTERY
      select I8259
      select I8254
      select PCKBD
hw/i386/acpi-build.c +1 -0
index 423c4959fe..790b16e582 100644
@@ -1248,6 +1248,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
 
          aml_append(sb_scope, dev);
      }
+
      aml_append(dsdt, sb_scope);
 
      if (pm->pcihp_bridge_en || pm->pcihp_root_en) {
include/hw/acpi/acpi_dev_interface.h +1 -0
index 68d9d15f50..3064ef6734 100644
@@ -13,6 +13,7 @@ typedef enum {
      ACPI_NVDIMM_HOTPLUG_STATUS = 16,
      ACPI_VMGENID_CHANGE_STATUS = 32,
      ACPI_POWER_DOWN_STATUS = 64,
+    ACPI_BATTERY_CHANGE_STATUS = 128,
  } AcpiEventStatusBits;
 
  #define TYPE_ACPI_DEVICE_IF "acpi-device-interface"
include/hw/acpi/battery.h new file +33 -0
new file mode 100644
index 0000000000..5c5e83abfa
@@ -0,0 +1,33 @@
+/*
+ * QEMU emulated battery device.
+ *
+ * Copyright (c) 2019 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_PATH_PROP             "sysfs_path"
+#define BATTERY_PROBE_STATE_INTERVAL  "probe_interval"
+
+#define BATTERY_FULL_CAP     10000  /* mWh */
+
+#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 +63 -0
index 906b3687a5..d1ad663bfd 100644
@@ -142,3 +142,76 @@
  ##
  { 'event': 'ACPI_DEVICE_OST',
       'data': { 'info': 'ACPIOSTInfo' } }
+
+##
+# @BatteryInfo:
+#
+# Battery state information
+#
+# @present: whether the battery is present
+#
+# @charging: whether the battery is charging
+#
+# @discharging: whether the battery is discharging
+#
+# @charge-percent: battery charge percentage (0-100)
+#
+# @rate: charge/discharge rate in mW (optional)
+#
+# @remaining-capacity: remaining capacity in mWh (optional)
+#
+# @design-capacity: design capacity in mWh (optional)
+#
+# Since: 10.2
+##
+{ '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: 10.2
+#
+# .. qmp-example::
+#
+#     -> { "execute": "battery-set-state",
+#          "arguments": { "state": { "present": true,
+#                                    "charging": true,
+#                                    "discharging": false,
+#                                    "charge-percent": 85 } } }
+#     <- { "return": {} }
+##
+{ 'command': 'battery-set-state',
+  'data': { 'state': 'BatteryInfo' } }
+
+##
+# @query-battery:
+#
+# Query the current state of the emulated battery device
+#
+# Returns: current battery state
+#
+# Since: 10.2
+#

qapi/acpi.json line 217

Quoted 10 lines
qapi/acpi.json +10 -0
+# .. qmp-example::
+#
+#     -> { "execute": "query-battery" }
+#     <- { "return": { "present": true,
+#                      "charging": true,
+#                      "discharging": false,
+#                      "charge-percent": 85 } }
+##
+{ 'command': 'query-battery',
+  'returns': 'BatteryInfo' }

The compilation is broken with this patch for non-x86 targets without CONFIG_ACPI and CONFIG_BATTERY. Linker reports QMP implementation routines as non-available.

The same applies for further patches in the series.

Please take a look to hw/acpi/meson.build for this line:
system_ss.add(when: 'CONFIG_ACPI', if_false: files('acpi-stub.c', 'aml-build-stub.c', 'ghes-stub.c', 'acpi_interface.c')

Something similar is necessary.

Den

#6 lore raw

Re: [PATCH v3 2/4] hw/acpi: Introduce the QEMU Battery

In reply to Leonid Bloch
Quoted 19 lines · Leonid Bloch

The battery device communicates battery state to the guest via ACPI. It supports two modes of operation:

1. QMP control mode (default): Battery state is controlled programmatically via QMP commands, making the device deterministic and migration-safe.

2. Host mirroring mode (optional): The device reflects the host's battery state from sysfs. Probing occurs on guest ACPI requests and at timed intervals (default 2 seconds, configurable via 'probe_interval' property in milliseconds). State changes trigger ACPI notifications to the guest.

Properties:
- 'use-qmp': Enable QMP control mode (default: true)
- 'enable-sysfs': Enable host battery mirroring (default: false)
- 'probe_interval': Probe interval in ms for sysfs mode (default: 2000)
- 'sysfs_path': Override default sysfs path /sys/class/power_supply/

+++ b/hw/acpi/battery.c

hw/acpi/battery.c line 38

Quoted 2 lines
hw/acpi/battery.c new file +2 -0
+
+#define MAX_ALLOWED_STATE_LENGTH  32  /* For convinience when comparing */

convenience

hw/acpi/battery.c line 4

Quoted 6 lines

+++ b/include/hw/acpi/battery.h

hw/acpi/battery.c new file +4 -0
@@ -0,0 +1,33 @@
+/*
+ * QEMU emulated battery device.
+ *
+ * Copyright (c) 2019 Janus Technologies, Inc. (http://janustech.com)

Is the copyright year accurate?

Quoted 6 lines
qapi/acpi.json +2 -0
+++ b/qapi/acpi.json
@@ -142,3 +142,76 @@
 ##
 { 'event': 'ACPI_DEVICE_OST',
      'data': { 'info': 'ACPIOSTInfo' } }
+

qapi/acpi.json line 155

Quoted 10 lines
qapi/acpi.json +10 -0
+##
+# @BatteryInfo:
+#
+# Battery state information
+#
+# @present: whether the battery is present
+#
+# @charging: whether the battery is charging
+#
+# @discharging: whether the battery is discharging

The commit message mentioned that charging and discharging are mutually exclusive; if so, should this be an enum to avoid the user passing in an impossible scenario? For that matter, can present be folded into the enum, for four states total (absent, charging, discharging, and present - where present is neither charging nor discharging)?

Quoted 6 lines
qapi/acpi.json +6 -0
+#
+# @charge-percent: battery charge percentage (0-100)
+#
+# @rate: charge/discharge rate in mW (optional)
+#
+# @remaining-capacity: remaining capacity in mWh (optional)

qapi/acpi.json line 171

Quoted 10 lines
qapi/acpi.json +10 -0
+#
+# @design-capacity: design capacity in mWh (optional)
+#
+# Since: 10.2
+##
+{ 'struct': 'BatteryInfo',
+  'data': { 'present': 'bool',
+            'charging': 'bool',
+            'discharging': 'bool',
+            'charge-percent': 'int',

Does having charge-percent mandatory when present is false make sense? Or should this be a discriminated union, where the additional fields like charge-percent, rate, and so on are only in the branches of the union where the state implies present?

qapi/acpi.json line 184

Quoted 13 lines
qapi/acpi.json +13 -0
+            '*rate': 'int',
+            '*remaining-capacity': 'int',
+            '*design-capacity': 'int' } }
+
+##
+# @battery-set-state:
+#
+# Set the state of the emulated battery device
+#
+# @state: new battery state
+#
+
+#

The blank line looks odd.

Quoted 36 lines
qapi/acpi.json +33 -0
+# Since: 10.2
+#
+# .. qmp-example::
+#
+#     -> { "execute": "battery-set-state",
+#          "arguments": { "state": { "present": true,
+#                                    "charging": true,
+#                                    "discharging": false,
+#                                    "charge-percent": 85 } } }
+#     <- { "return": {} }
+##
+{ 'command': 'battery-set-state',
+  'data': { 'state': 'BatteryInfo' } }
+
+##
+# @query-battery:
+#
+# Query the current state of the emulated battery device
+#
+# Returns: current battery state
+#
+# Since: 10.2
+#
+# .. qmp-example::
+#
+#     -> { "execute": "query-battery" }
+#     <- { "return": { "present": true,
+#                      "charging": true,
+#                      "discharging": false,
+#                      "charge-percent": 85 } }
+##
+{ 'command': 'query-battery',
+  'returns': 'BatteryInfo' }
Signature · 1 line
2.51.0
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#7 lore raw

PATCH v3 3/4 hw/acpi: Introduce the QEMU AC adapter

In reply to Leonid Bloch

The AC adapter device communicates AC power state to the guest via ACPI. It supports two modes of operation:

1. QMP control mode (default): AC adapter state is controlled programmatically via QMP commands, ensuring deterministic behavior.

2. Host mirroring mode (optional): The device reflects the host's AC adapter state from sysfs. Probing occurs on guest ACPI requests and at timed intervals. State changes trigger ACPI notifications.

Properties:
- 'use-qmp': Enable QMP control mode (default: true)
- 'enable-sysfs': Enable host AC adapter mirroring (default: false)
- 'probe_interval': Probe interval in ms for sysfs mode (default: 2000)
- 'sysfs_path': Override default sysfs path /sys/class/power_supply/

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                          |   6 +
 docs/specs/acad.rst                  | 195 ++++++++++++
 docs/specs/index.rst                 |   1 +
 hw/acpi/Kconfig                      |   4 +
 hw/acpi/acad.c                       | 447 +++++++++++++++++++++++++++
 hw/acpi/meson.build                  |   1 +
 hw/acpi/trace-events                 |   5 +
 hw/i386/Kconfig                      |   1 +
 include/hw/acpi/acad.h               |  27 ++
 include/hw/acpi/acpi_dev_interface.h |   1 +
 qapi/acpi.json                       |  49 +++
 11 files changed, 737 insertions(+)
 create mode 100644 docs/specs/acad.rst
 create mode 100644 hw/acpi/acad.c
 create mode 100644 include/hw/acpi/acad.h
MAINTAINERS +6 -0
index eb71a4a4b7..19bea634c7 100644
@@ -2934,6 +2934,12 @@ S: Maintained
 F: hw/acpi/battery.*
 F: docs/specs/battery.rst
 
+AC Adapter
+M: Leonid Bloch <[email protected]>
+S: Maintained
+F: hw/acpi/acad.*
+F: docs/specs/acad.rst
+
 Subsystems
 ----------
 Overall Audio backends
docs/specs/acad.rst new file +195 -0

195 lines not on this page · raw message

docs/specs/index.rst +1 -0
index 616e8228cc..e144afcd90 100644
@@ -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
hw/acpi/Kconfig +4 -0
index 64403378bd..9d28c3addf 100644
@@ -69,6 +69,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.c new file +447 -0

447 lines not on this page · raw message

hw/acpi/meson.build +1 -0
index 10379a7b2c..2b24951f28 100644
@@ -32,6 +32,7 @@ if have_tpm
   acpi_ss.add(files('tpm.c'))
 endif
 acpi_ss.add(when: 'CONFIG_BATTERY', if_true: files('battery.c'))
+acpi_ss.add(when: 'CONFIG_AC_ADAPTER', if_true: files('acad.c'))
 system_ss.add(when: 'CONFIG_ACPI', if_false: files('acpi-stub.c', 'aml-build-stub.c', 'ghes-stub.c', 'acpi_interface.c'))
 system_ss.add(when: 'CONFIG_ACPI_PCI_BRIDGE', if_false: files('pci-bridge-stub.c'))
 system_ss.add_all(when: 'CONFIG_ACPI', if_true: acpi_ss)
hw/acpi/trace-events +5 -0
index dd3e815482..68ac6e9701 100644
@@ -92,3 +92,8 @@ acpi_nvdimm_invalid_revision(uint32_t revision) "Revision 0x%" PRIx32 " is not s
 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
 battery_ioport_read_unknown(void) "Battery read unknown"
+
+# acad.c
+acad_realize(void) "AC adapter device realize entry"
+acad_get_dynamic_status(uint8_t state) "AC adapter read state: %"PRIu8
+acad_ioport_read_unknown(void) "AC adapter read unknown"
hw/i386/Kconfig +1 -0
index 2c878fd112..baab382a2e 100644
@@ -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 +27 -0
new file mode 100644
index 0000000000..a4e7149488
@@ -0,0 +1,27 @@
+/*
+ * QEMU emulated AC adapter device.
+ *
+ * Copyright (c) 2019 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_PATH_PROP             "sysfs_path"
+#define AC_ADAPTER_PROBE_STATE_INTERVAL  "probe_interval"
+
+#define AC_ADAPTER_VAL_UNKNOWN  0xFFFFFFFF
+
+#define AC_ADAPTER_LEN          1
+
+#endif
include/hw/acpi/acpi_dev_interface.h +1 -0
index 3064ef6734..588fbbd05f 100644
@@ -14,6 +14,7 @@ typedef enum {
     ACPI_VMGENID_CHANGE_STATUS = 32,
     ACPI_POWER_DOWN_STATUS = 64,
     ACPI_BATTERY_CHANGE_STATUS = 128,
+    ACPI_AC_ADAPTER_CHANGE_STATUS = 1024,
 } AcpiEventStatusBits;
 
 #define TYPE_ACPI_DEVICE_IF "acpi-device-interface"
qapi/acpi.json +49 -0

52 lines not on this page · raw message

Signature · 1 line
2.51.0
#8 lore raw

Re: [PATCH v3 3/4] hw/acpi: Introduce the QEMU AC adapter

Quoted 10 lines · Leonid Bloch

The AC adapter device communicates AC power state to the guest via ACPI. It supports two modes of operation:

1. QMP control mode (default): AC adapter state is controlled programmatically via QMP commands, ensuring deterministic behavior.

2. Host mirroring mode (optional): The device reflects the host's AC adapter state from sysfs. Probing occurs on guest ACPI requests and at timed intervals. State changes trigger ACPI notifications.

Quoted 5 lines

Properties:
- 'use-qmp': Enable QMP control mode (default: true)
- 'enable-sysfs': Enable host AC adapter mirroring (default: false)
- 'probe_interval': Probe interval in ms for sysfs mode (default: 2000)
- 'sysfs_path': Override default sysfs path /sys/class/power_supply/

Any reason half the properties use - and the other use _ between words?

Quoted 5 lines

+++ b/qapi/acpi.json

qapi/acpi.json +0 -0
@@ -215,3 +215,52 @@
 ##
 { 'command': 'query-battery',
   'returns': 'BatteryInfo' }

qapi/acpi.json line 227

Quoted 10 lines
qapi/acpi.json +10 -0
+
+##
+# @ac-adapter-set-state:
+#
+# Set the state of the emulated AC adapter device
+#
+# @connected: whether the AC adapter is connected
+#
+
+#

Another odd blank line.

Quoted 11 lines
qapi/acpi.json +11 -0
+# Since: 10.2
+#
+# .. qmp-example::
+#
+#     -> { "execute": "ac-adapter-set-state",
+#          "arguments": { "connected": true } }
+#     <- { "return": {} }
+##
+{ 'command': 'ac-adapter-set-state',
+  'data': { 'connected': 'bool' } }
+
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#9 lore raw

PATCH v3 4/4 hw/acpi: Introduce the QEMU lid button

In reply to Leonid Bloch

The lid button device communicates laptop lid state to the guest via ACPI. It supports two modes of operation:

1. QMP control mode (default): Lid state is controlled programmatically via QMP commands for consistent behavior across environments.

2. Host mirroring mode (optional): The device reflects the host's lid button state from procfs (/proc/acpi/button/lid/*/state). State changes trigger ACPI notifications to the guest.

Properties:
- 'use-qmp': Enable QMP control mode (default: true)
- 'enable-procfs': Enable host lid button mirroring (default: false)
- 'probe_interval': Probe interval in ms for procfs mode (default: 2000)
- 'procfs_path': Override default procfs path /proc/acpi/button

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                          |   6 +
 docs/specs/button.rst                | 189 ++++++++++++
 docs/specs/index.rst                 |   1 +
 hw/acpi/Kconfig                      |   4 +
 hw/acpi/button.c                     | 438 +++++++++++++++++++++++++++
 hw/acpi/meson.build                  |   1 +
 hw/acpi/trace-events                 |   5 +
 hw/i386/Kconfig                      |   1 +
 include/hw/acpi/acpi_dev_interface.h |   1 +
 include/hw/acpi/button.h             |  25 ++
 qapi/acpi.json                       |  49 +++
 11 files changed, 720 insertions(+)
 create mode 100644 docs/specs/button.rst
 create mode 100644 hw/acpi/button.c
 create mode 100644 include/hw/acpi/button.h
MAINTAINERS +6 -0
index 19bea634c7..eb876803eb 100644
@@ -2940,6 +2940,12 @@ S: Maintained
 F: hw/acpi/acad.*
 F: docs/specs/acad.rst
 
+Button
+M: Leonid Bloch <[email protected]>
+S: Maintained
+F: hw/acpi/button.*
+F: docs/specs/button.rst
+
 Subsystems
 ----------
 Overall Audio backends
docs/specs/button.rst new file +189 -0

189 lines not on this page · raw message

docs/specs/index.rst +1 -0
index e144afcd90..e1c9b91b7b 100644
@@ -24,6 +24,7 @@ guest hardware that is specific to QEMU.
    acpi_erst
    acad
    battery
+   button
    sev-guest-firmware
    fw_cfg
    fsi
hw/acpi/Kconfig +4 -0
index 9d28c3addf..6600685855 100644
@@ -73,6 +73,10 @@ config AC_ADAPTER
     bool
     depends on ACPI
 
+config BUTTON
+    bool
+    depends on ACPI
+
 config ACPI_HW_REDUCED
     bool
     select ACPI
hw/acpi/button.c new file +438 -0

438 lines not on this page · raw message

hw/acpi/meson.build +1 -0
index 2b24951f28..94993ecb9a 100644
@@ -33,6 +33,7 @@ if have_tpm
 endif
 acpi_ss.add(when: 'CONFIG_BATTERY', if_true: files('battery.c'))
 acpi_ss.add(when: 'CONFIG_AC_ADAPTER', if_true: files('acad.c'))
+acpi_ss.add(when: 'CONFIG_BUTTON', if_true: files('button.c'))
 system_ss.add(when: 'CONFIG_ACPI', if_false: files('acpi-stub.c', 'aml-build-stub.c', 'ghes-stub.c', 'acpi_interface.c'))
 system_ss.add(when: 'CONFIG_ACPI_PCI_BRIDGE', if_false: files('pci-bridge-stub.c'))
 system_ss.add_all(when: 'CONFIG_ACPI', if_true: acpi_ss)
hw/acpi/trace-events +5 -0
index 68ac6e9701..816f027bb3 100644
@@ -97,3 +97,8 @@ battery_ioport_read_unknown(void) "Battery read unknown"
 acad_realize(void) "AC adapter device realize entry"
 acad_get_dynamic_status(uint8_t state) "AC adapter read state: %"PRIu8
 acad_ioport_read_unknown(void) "AC adapter read unknown"
+
+# button.c
+button_realize(void) "Button device realize entry"
+button_get_dynamic_status(void) "Button read dynamic status entry"
+button_ioport_read_unknown(void) "Button read unknown"
hw/i386/Kconfig +1 -0
index baab382a2e..1d2d809028 100644
@@ -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
index 588fbbd05f..4f5bcc15da 100644
@@ -15,6 +15,7 @@ typedef enum {
     ACPI_POWER_DOWN_STATUS = 64,
     ACPI_BATTERY_CHANGE_STATUS = 128,
     ACPI_AC_ADAPTER_CHANGE_STATUS = 1024,
+    ACPI_BUTTON_CHANGE_STATUS = 2048,
 } AcpiEventStatusBits;
 
 #define TYPE_ACPI_DEVICE_IF "acpi-device-interface"
include/hw/acpi/button.h new file +25 -0
new file mode 100644
index 0000000000..fa5f7001b2
@@ -0,0 +1,25 @@
+/*
+ * QEMU emulated button device.
+ *
+ * Copyright (c) 2019 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_PATH_PROP             "procfs_path"
+#define BUTTON_PROBE_STATE_INTERVAL  "probe_interval"
+
+#define BUTTON_LEN                   1
+
+#endif
qapi/acpi.json +49 -0

52 lines not on this page · raw message

Signature · 1 line
2.51.0
#10 lore raw
In reply to Leonid Bloch
Quoted 6 lines · On Thu, 28 Aug 2025 01:00:46 +0300 Leonid Bloch <[email protected]> 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).

Quoted 99 lines

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