Skip to contentSkip to conversation

Introduce a battery, AC adapter, and lid button

PATCH In review Leonid Bloch 3 participants 9 messages Jan 20, 2021 – Jan 28, 2021 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover Introduce a battery, AC adapter, and lid button

This series introduces the following ACPI devices:

* Battery
* AC adapter
* Laptop lid button

When running QEMU on a laptop, these paravirtualized devices reflect the state of these physical devices onto the guest. This functionality is relevant not only for laptops, but also for any other device which has e.g. a battery. This even allows to insert a ``fake'' battery to the guest, in a form of a file which emulates the behavior of the actual battery in sysfs. A possible use case for such a ``fake'' battery can be limiting the budget of VM usage to a subscriber, in a naturally-visible way. But of course, the main purpose here is addressing the desktop users.

This series was tested with Windows and (desktop) Linux guests, on which indeed the battery icon appears in the corresponding state (full, charging, discharging, time remaining to empty, etc.) and the AC adapter plugging/unplugging behaves as expected. So is the laptop lid button.

For the ease of review, these commits are also available here:
https://github.com/blochl/qemu/pull/1/commits

Thanks,
Leonid.

Leonid Bloch (4):
hw/acpi: Increase the number of possible ACPI interrupts hw/acpi: Introduce the QEMU Battery
hw/acpi: Introduce the QEMU AC adapter
hw/acpi: Introduce the QEMU lid button

 MAINTAINERS                          |  15 +
 docs/specs/acad.txt                  |  24 ++
 docs/specs/battery.txt               |  23 ++
 docs/specs/button.txt                |  35 ++
 hw/acpi/Kconfig                      |  12 +
 hw/acpi/acad.c                       | 318 +++++++++++++++++
 hw/acpi/battery.c                    | 512 +++++++++++++++++++++++++++
 hw/acpi/button.c                     | 327 +++++++++++++++++
 hw/acpi/core.c                       |  17 +-
 hw/acpi/meson.build                  |   3 +
 hw/acpi/trace-events                 |  15 +
 hw/i386/Kconfig                      |   3 +
 hw/i386/acpi-build.c                 | 178 ++++++++++
 include/hw/acpi/acad.h               |  37 ++
 include/hw/acpi/acpi_dev_interface.h |   3 +
 include/hw/acpi/battery.h            |  43 +++
 include/hw/acpi/button.h             |  35 ++
 17 files changed, 1598 insertions(+), 2 deletions(-)
 create mode 100644 docs/specs/acad.txt
 create mode 100644 docs/specs/battery.txt
 create mode 100644 docs/specs/button.txt
 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.30.0
#1 lore raw

PATCH 1/4 hw/acpi: Increase the number of possible ACPI interrupts

Increase the number of possible ACPI interrupts from 8, to the maximum
available: 64 by default.

---
 hw/acpi/core.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
hw/acpi/core.c +15 -2
index 7170bff657..71ba7c17b8 100644
@@ -706,19 +706,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 >>= (sizeof(ar->gpe.sts[0]) * CHAR_BIT);
+    }
+
     acpi_update_sci(ar, irq);
 }
 
 void acpi_update_sci(ACPIREGS *regs, qemu_irq irq)
 {
     int sci_level, pm1a_sts;
+    uint64_t gpe_sci = 0;
+    int i;
 
     pm1a_sts = acpi_pm1_evt_get_sts(regs);
 
+    for (i = 0; i < regs->gpe.len / 2; i++) {
+        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 != 0);
 
     qemu_set_irq(irq, sci_level);
 
Signature · 1 line
2.30.0
#2 lore raw

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

In reply to Leonid Bloch

The battery device communicates the host's battery state to the guest. The probing of the host's battery state occurs on guest ACPI requests, as well as on timed intervals. If a change of the host's battery state is detected on one of the timed probes (charging/discharging, rate, charge) an ACPI notification is sent to the guest, so it will be able to update its battery status accordingly.

The time interval between periodic probes is 2 seconds by default. A property 'probe_interval' allows to modify this value. The value should be provided in milliseconds. A zero value disables the periodic probes, and makes the battery state updates occur on guest requests only.

The host's battery information is taken from the sysfs battery data, located in:

/sys/class/power_supply/[device of type "Battery"]

If the sysfs path differs, a different battery needs to be probed, or even if a "fake" host battery is to be provided, a 'sysfs_path' property allows to override the default one.

---
 MAINTAINERS                          |   5 +
 docs/specs/battery.txt               |  23 ++
 hw/acpi/Kconfig                      |   4 +
 hw/acpi/battery.c                    | 512 +++++++++++++++++++++++++++
 hw/acpi/meson.build                  |   1 +
 hw/acpi/trace-events                 |   5 +
 hw/i386/Kconfig                      |   1 +
 hw/i386/acpi-build.c                 |  97 +++++
 include/hw/acpi/acpi_dev_interface.h |   1 +
 include/hw/acpi/battery.h            |  43 +++
 10 files changed, 692 insertions(+)
 create mode 100644 docs/specs/battery.txt
 create mode 100644 hw/acpi/battery.c
 create mode 100644 include/hw/acpi/battery.h
MAINTAINERS +5 -0
index 3216387521..33eea28c22 100644
@@ -2141,6 +2141,11 @@ F: net/can/*
 F: hw/net/can/*
 F: include/net/can_*.h
 
+Battery
+M: Leonid Bloch <[email protected]>
+S: Maintained
+F: hw/acpi/battery.*
+
 Subsystems
 ----------
 Audio
docs/specs/battery.txt new file +23 -0
new file mode 100644
index 0000000000..e90324ac03
@@ -0,0 +1,23 @@
+BATTERY DEVICE
+==============
+
+The battery device communicates the host's battery state to the guest.
+The probing of the host's battery state occurs on guest ACPI requests,
+as well as on timed intervals. If a change of the host's battery state is
+detected on one of the timed probes (charging/discharging, rate, charge)
+an ACPI notification is sent to the guest, so it will be able to
+update its battery status accordingly.
+
+The time interval between periodic probes is 2 s by default. A property
+'probe_interval' allows to modify this value. The value should be
+provided in milliseconds. A zero value disables the periodic probes,
+and makes the battery state updates occur on guest requests only.
+
+The host's battery information is taken from the sysfs battery data,
+located in:
+
+/sys/class/power_supply/[device of type "Battery"]
+
+If the sysfs path differs, a different battery needs to be probed,
+or even if a "fake" host battery is to be provided, a 'sysfs_path'
+property allows to override the default one.
hw/acpi/Kconfig +4 -0
index 1932f66af8..6b4c41037a 100644
@@ -41,4 +41,8 @@ config ACPI_VMGENID
     default y
     depends on PC
 
+config BATTERY
+    bool
+    depends on ACPI
+
 config ACPI_HW_REDUCED
hw/acpi/battery.c new file +512 -0

512 lines not on this page · raw message

hw/acpi/meson.build +1 -0
index dd69577212..485eab33b6 100644
@@ -19,6 +19,7 @@ acpi_ss.add(when: 'CONFIG_ACPI_X86_ICH', if_true: files('ich9.c', 'tco.c'))
 acpi_ss.add(when: 'CONFIG_IPMI', if_true: files('ipmi.c'), if_false: files('ipmi-stub.c'))
 acpi_ss.add(when: 'CONFIG_PC', if_false: files('acpi-x86-stub.c'))
 acpi_ss.add(when: 'CONFIG_TPM', if_true: files('tpm.c'))
+acpi_ss.add(when: 'CONFIG_BATTERY', if_true: files('battery.c'))
 softmmu_ss.add(when: 'CONFIG_ACPI', if_false: files('acpi-stub.c', 'aml-build-stub.c'))
 softmmu_ss.add_all(when: 'CONFIG_ACPI', if_true: acpi_ss)
 softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('acpi-stub.c', 'aml-build-stub.c',
hw/acpi/trace-events +5 -0
index f91ced477d..5d2c606496 100644
@@ -53,3 +53,8 @@ piix4_gpe_writeb(uint64_t addr, unsigned width, uint64_t val) "addr: 0x%" PRIx64
 # tco.c
 tco_timer_reload(int ticks, int msec) "ticks=%d (%d ms)"
 tco_timer_expired(int timeouts_no, bool strap, bool no_reboot) "timeouts_no=%d no_reboot=%d/%d"
+
+# 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 eea059ffef..b081be7a0f 100644
@@ -23,6 +23,7 @@ config PC
     imply TPM_TIS_ISA
     imply VGA_PCI
     imply VIRTIO_VGA
+    imply BATTERY
     select FDC
     select I8259
     select I8254
hw/i386/acpi-build.c +97 -0

121 lines not on this page · raw message

include/hw/acpi/acpi_dev_interface.h +1 -0
index 769ff55c7e..3d98d5636a 100644
@@ -14,6 +14,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 +43 -0

43 lines not on this page · raw message

Signature · 1 line
2.30.0
#3 lore raw

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

In reply to Leonid Bloch

The AC adapter device communicates the host's AC adapter state to the guest. The probing of the host's AC adapter state occurs on guest ACPI requests, as well as on timed intervals. If a change of the host's AC adapter state is detected on one of the timed probes (connected/disconnected) an ACPI notification is sent to the guest, so it will be able to update its AC adapter status accordingly.

The time interval between the periodic probes is 2 seconds by default. A property 'probe_interval' allows to modify this value. The value should be provided in milliseconds. A zero value disables the periodic probes, and makes the AC adapter state updates occur on guest requests only.

The host's AC adapter information is taken from the sysfs AC adapter data, located in:

/sys/class/power_supply/[device of type "Mains"]

If the sysfs path differs, a different AC adapter needs to be probed, or even if a "fake" host AC adapter is to be provided, a 'sysfs_path' property allows to override the default one.

---
 MAINTAINERS                          |   5 +
 docs/specs/acad.txt                  |  24 ++
 hw/acpi/Kconfig                      |   4 +
 hw/acpi/acad.c                       | 318 +++++++++++++++++++++++++++
 hw/acpi/meson.build                  |   1 +
 hw/acpi/trace-events                 |   5 +
 hw/i386/Kconfig                      |   1 +
 hw/i386/acpi-build.c                 |  52 +++++
 include/hw/acpi/acad.h               |  37 ++++
 include/hw/acpi/acpi_dev_interface.h |   1 +
 10 files changed, 448 insertions(+)
 create mode 100644 docs/specs/acad.txt
 create mode 100644 hw/acpi/acad.c
 create mode 100644 include/hw/acpi/acad.h
MAINTAINERS +5 -0
index 33eea28c22..1a3fc1dd56 100644
@@ -2146,6 +2146,11 @@ M: Leonid Bloch <[email protected]>
 S: Maintained
 F: hw/acpi/battery.*
 
+AC Adapter
+M: Leonid Bloch <[email protected]>
+S: Maintained
+F: hw/acpi/acad.*
+
 Subsystems
 ----------
 Audio
docs/specs/acad.txt new file +24 -0
new file mode 100644
index 0000000000..0d563a7b50
@@ -0,0 +1,24 @@
+AC ADAPTER DEVICE
+=================
+
+The AC adapter device communicates the host's AC adapter state to the
+guest. The probing of the host's AC adapter state occurs on guest ACPI
+requests, as well as on timed intervals. If a change of the host's AC
+adapter state is detected on one of the timed probes (connected/disconnected)
+an ACPI notification is sent to the guest, so it will be able to
+update its AC adapter status accordingly.
+
+The time interval between the periodic probes is 2 s by default.
+A property 'probe_interval' allows to modify this value. The value
+should be provided in milliseconds. A zero value disables the periodic
+probes, and makes the AC adapter state updates occur on guest requests
+only.
+
+The host's AC adapter information is taken from the sysfs AC adapter
+data, located in:
+
+/sys/class/power_supply/[device of type "Mains"]
+
+If the sysfs path differs, a different AC adapter needs to be probed,
+or even if a "fake" host AC adapter is to be provided, a 'sysfs_path'
+property allows to override the default one.
hw/acpi/Kconfig +4 -0
index 6b4c41037a..d35331fbf7 100644
@@ -45,4 +45,8 @@ config BATTERY
     bool
     depends on ACPI
 
+config AC_ADAPTER
+    bool
+    depends on ACPI
+
 config ACPI_HW_REDUCED
hw/acpi/acad.c new file +318 -0

318 lines not on this page · raw message

hw/acpi/meson.build +1 -0
index 485eab33b6..2ae756c6ef 100644
@@ -20,6 +20,7 @@ acpi_ss.add(when: 'CONFIG_IPMI', if_true: files('ipmi.c'), if_false: files('ipmi
 acpi_ss.add(when: 'CONFIG_PC', if_false: files('acpi-x86-stub.c'))
 acpi_ss.add(when: 'CONFIG_TPM', if_true: files('tpm.c'))
 acpi_ss.add(when: 'CONFIG_BATTERY', if_true: files('battery.c'))
+acpi_ss.add(when: 'CONFIG_AC_ADAPTER', if_true: files('acad.c'))
 softmmu_ss.add(when: 'CONFIG_ACPI', if_false: files('acpi-stub.c', 'aml-build-stub.c'))
 softmmu_ss.add_all(when: 'CONFIG_ACPI', if_true: acpi_ss)
 softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('acpi-stub.c', 'aml-build-stub.c',
hw/acpi/trace-events +5 -0
index 5d2c606496..496a282cdc 100644
@@ -58,3 +58,8 @@ tco_timer_expired(int timeouts_no, bool strap, bool no_reboot) "timeouts_no=%d n
 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 b081be7a0f..483b3ccec8 100644
@@ -24,6 +24,7 @@ config PC
     imply VGA_PCI
     imply VIRTIO_VGA
     imply BATTERY
+    imply AC_ADAPTER
     select FDC
     select I8259
     select I8254
hw/i386/acpi-build.c +52 -0

76 lines not on this page · raw message

include/hw/acpi/acad.h new file +37 -0
new file mode 100644
index 0000000000..be61c57064
@@ -0,0 +1,37 @@
+/*
+ * 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]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory for details.
+ *
+ */
+
+#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
+
+static inline uint16_t acad_port(void)
+{
+    Object *o = object_resolve_path_type("", TYPE_AC_ADAPTER, NULL);
+    if (!o) {
+        return 0;
+    }
+    return object_property_get_uint(o, AC_ADAPTER_IOPORT_PROP, NULL);
+}
+
+#endif
include/hw/acpi/acpi_dev_interface.h +1 -0
index 3d98d5636a..b577a4db07 100644
@@ -15,6 +15,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"
Signature · 1 line
2.30.0
#4 lore raw

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

In reply to Leonid Bloch

The button device communicates the host's "lid button" state to the guest. The probing of the host's lid button state occurs on timed intervals. If a change of the host's lid button state is detected (open/closed) an ACPI notification is sent to the guest, so it will be able to act accordingly.

The time interval between the periodic probes is 2 s by default. A property 'probe_interval' allows to modify this value. The value is provided in milliseconds.

The host's lid button information is taken from:

/proc/acpi/button/lid/*/state

And this file is expected to be formatted as:
'state: open' (if the lid is open)
or:
'state: closed' (if the lid is closed)

These are based on the Linux 'button' driver.

If the above procfs path differs, or even if a "fake" host lid button is to be provided, a 'procfs_path' property allows to override the default path.

Usage example (default values):

'-device button'

Is the same as:

'-device button,procfs_path=/proc/acpi/button,probe_interval=2000'

---
 MAINTAINERS                          |   5 +
 docs/specs/button.txt                |  35 +++
 hw/acpi/Kconfig                      |   4 +
 hw/acpi/button.c                     | 327 +++++++++++++++++++++++++++
 hw/acpi/meson.build                  |   1 +
 hw/acpi/trace-events                 |   5 +
 hw/i386/Kconfig                      |   1 +
 hw/i386/acpi-build.c                 |  29 +++
 include/hw/acpi/acpi_dev_interface.h |   1 +
 include/hw/acpi/button.h             |  35 +++
 10 files changed, 443 insertions(+)
 create mode 100644 docs/specs/button.txt
 create mode 100644 hw/acpi/button.c
 create mode 100644 include/hw/acpi/button.h
MAINTAINERS +5 -0
index 1a3fc1dd56..72bead7023 100644
@@ -2151,6 +2151,11 @@ M: Leonid Bloch <[email protected]>
 S: Maintained
 F: hw/acpi/acad.*
 
+Button
+M: Leonid Bloch <[email protected]>
+S: Maintained
+F: hw/acpi/button.*
+
 Subsystems
 ----------
 Audio
docs/specs/button.txt new file +35 -0
new file mode 100644
index 0000000000..48e9f3388d
@@ -0,0 +1,35 @@
+BUTTON DEVICE
+=================
+
+The button device communicates the host's "lid button" state to the guest.
+The probing of the host's lid button state occurs on timed intervals.
+If a change of the host's lid button state is detected (open/closed)
+an ACPI notification is sent to the guest, so it will be able to act
+accordingly.
+
+The time interval between the periodic probes is 2 s by default.
+A property 'probe_interval' allows to modify this value. The value
+is provided in milliseconds.
+
+The host's lid button information is taken from:
+
+/proc/acpi/button/lid/*/state
+
+And this file is expected to be formatted as:
+'state:      open' (if the lid is open)
+or:
+'state:      closed' (if the lid is closed)
+
+These are based on the Linux 'button' driver.
+
+If the above procfs path differs, or even if a "fake" host lid button
+is to be provided, a 'procfs_path' property allows to override the
+default path.
+
+Usage example (default values):
+
+'-device button'
+
+Is the same as:
+
+'-device button,procfs_path=/proc/acpi/button,probe_interval=2000'
hw/acpi/Kconfig +4 -0
index d35331fbf7..0e78560f0f 100644
@@ -49,4 +49,8 @@ config AC_ADAPTER
     bool
     depends on ACPI
 
+config BUTTON
+    bool
+    depends on ACPI
+
 config ACPI_HW_REDUCED
hw/acpi/button.c new file +327 -0

327 lines not on this page · raw message

hw/acpi/meson.build +1 -0
index 2ae756c6ef..0f66766dd3 100644
@@ -21,6 +21,7 @@ acpi_ss.add(when: 'CONFIG_PC', if_false: files('acpi-x86-stub.c'))
 acpi_ss.add(when: 'CONFIG_TPM', if_true: files('tpm.c'))
 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'))
 softmmu_ss.add(when: 'CONFIG_ACPI', if_false: files('acpi-stub.c', 'aml-build-stub.c'))
 softmmu_ss.add_all(when: 'CONFIG_ACPI', if_true: acpi_ss)
 softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('acpi-stub.c', 'aml-build-stub.c',
hw/acpi/trace-events +5 -0
index 496a282cdc..7554f0c8b6 100644
@@ -63,3 +63,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 483b3ccec8..2a09e6f742 100644
@@ -25,6 +25,7 @@ config PC
     imply VIRTIO_VGA
     imply BATTERY
     imply AC_ADAPTER
+    imply BUTTON
     select FDC
     select I8259
     select I8254
hw/i386/acpi-build.c +29 -0
index 96dbeed22f..ee5537a1b4 100644
@@ -37,6 +37,7 @@
 #include "hw/acpi/cpu.h"
 #include "hw/acpi/battery.h"
 #include "hw/acpi/acad.h"
+#include "hw/acpi/button.h"
 #include "hw/nvram/fw_cfg.h"
 #include "hw/acpi/bios-linker-loader.h"
 #include "hw/isa/isa.h"
@@ -116,6 +117,7 @@ typedef struct AcpiMiscInfo {
     uint16_t pvpanic_port;
     uint16_t battery_port;
     uint16_t acad_port;
+    uint16_t button_port;
     uint16_t applesmc_io_base;
 } AcpiMiscInfo;
 
@@ -283,6 +285,7 @@ static void acpi_get_misc_info(AcpiMiscInfo *info)
     info->pvpanic_port = pvpanic_port();
     info->battery_port = battery_port();
     info->acad_port = acad_port();
+    info->button_port = button_port();
     info->applesmc_io_base = applesmc_port();
 }
 
@@ -1780,6 +1783,32 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
         aml_append(dsdt, method);
     }
 
+    if (misc->button_port) {
+        Aml *button_state  = aml_local(0);
+
+        dev = aml_device("LID0");
+        aml_append(dev, aml_name_decl("_HID", aml_string("PNP0C0D")));
+
+        aml_append(dev, aml_operation_region("LSTA", AML_SYSTEM_IO,
+                                             aml_int(misc->button_port),
+                                             BUTTON_LEN));
+        field = aml_field("LSTA", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
+        aml_append(field, aml_named_field("LIDS", 8));
+        aml_append(dev, field);
+
+        method = aml_method("_LID", 0, AML_NOTSERIALIZED);
+        aml_append(method, aml_store(aml_name("LIDS"), button_state));
+        aml_append(method, aml_return(button_state));
+        aml_append(dev, method);
+
+        aml_append(sb_scope, dev);
+
+        /* Status Change */
+        method = aml_method("\\_GPE._E0B", 0, AML_NOTSERIALIZED);
+        aml_append(method, aml_notify(aml_name("\\_SB.LID0"), aml_int(0x80)));
+        aml_append(dsdt, method);
+    }
+
     aml_append(dsdt, sb_scope);
 
     /* copy AML table into ACPI tables blob and patch header there */
include/hw/acpi/acpi_dev_interface.h +1 -0
index b577a4db07..5ba37a42d2 100644
@@ -16,6 +16,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 +35 -0
new file mode 100644
index 0000000000..6da17d7cee
@@ -0,0 +1,35 @@
+/*
+ * 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]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory for details.
+ *
+ */
+
+#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
+
+static inline uint16_t button_port(void)
+{
+    Object *o = object_resolve_path_type("", TYPE_BUTTON, NULL);
+    if (!o) {
+        return 0;
+    }
+    return object_property_get_uint(o, BUTTON_IOPORT_PROP, NULL);
+}
+
+#endif
Signature · 1 line
2.30.0
#5 lore raw
In reply to Leonid Bloch

Hi Leonid, Marcel,

Quoted 6 lines · Leonid Bloch

This series introduces the following ACPI devices:

* Battery
* AC adapter
* Laptop lid button

Quoted 7 lines

When running QEMU on a laptop, these paravirtualized devices reflect the state of these physical devices onto the guest. This functionality is relevant not only for laptops, but also for any other device which has e.g. a battery. This even allows to insert a ``fake'' battery to the guest, in a form of a file which emulates the behavior of the actual battery in sysfs. A possible use case for such a ``fake'' battery can be limiting the budget of VM usage to a subscriber, in a naturally-visible way.

Your series looks good. Now for this feature to be even more useful for the community, it would be better to

1/ Have a generic (kind of abstract QDev) battery model.
Your model would be the ISA implementation. But we could add LPC, SPI or I2C implementations for example.

2/ Make it a model backend accepting various kind of frontends:
- host Linux sysfs mirroring is a particular frontend implementation
- mirroring on Windows would be another
- any connection (TCP) to battery simulator (Octave, ...)

Meanwhile 2/ is not available, it would be useful to have QMP commands to set the battery charge and state (also max capacity).

Ditto QMP command to set the LID/AC adapter state.

Quoted 6 lines

But of course, the main purpose here is addressing the desktop users.

This series was tested with Windows and (desktop) Linux guests, on which indeed the battery icon appears in the corresponding state (full, charging, discharging, time remaining to empty, etc.) and the AC adapter plugging/unplugging behaves as expected. So is the laptop lid button.

[...]

In patch #2 you comment 'if a "fake" host battery is to be provided, a 'sysfs_path' property allows to override the default one.'.

Eventually you'd provide a such fake file as example, ideally used by a QTest.

Another question. If the battery is disconnected, is there an event propagated to the guest?

Thanks for contributing these patches :)

Phil.

#6 lore raw

Hi Phil,

Thanks for your feedback! Please see below.

Quoted 18 lines · Philippe Mathieu-Daudé

Hi Leonid, Marcel,

Quoted 6 lines · Leonid Bloch

This series introduces the following ACPI devices:

* Battery
* AC adapter
* Laptop lid button

Quoted 7 lines

When running QEMU on a laptop, these paravirtualized devices reflect the state of these physical devices onto the guest. This functionality is relevant not only for laptops, but also for any other device which has e.g. a battery. This even allows to insert a ``fake'' battery to the guest, in a form of a file which emulates the behavior of the actual battery in sysfs. A possible use case for such a ``fake'' battery can be limiting the budget of VM usage to a subscriber, in a naturally-visible way.

Quoted 6 lines

Your series looks good. Now for this feature to be even more useful for the community, it would be better to

1/ Have a generic (kind of abstract QDev) battery model.
Your model would be the ISA implementation. But we could add LPC, SPI or I2C implementations for example.

It definitely feels that it needs to be more generic, and I thought how to make it so, but so far it is what I came up with. I'll think some more, but any ideas are welcome, cause nowadays I'm doing this in my free time only.

Quoted 4 lines

2/ Make it a model backend accepting various kind of frontends:
- host Linux sysfs mirroring is a particular frontend implementation
- mirroring on Windows would be another
- any connection (TCP) to battery simulator (Octave, ...)

Well, it does accept an arbitrary file to represent a battery, so this covers the battery simulator, does it? As for Windows - indeed, it would be nice to have.

Quoted 2 lines

Meanwhile 2/ is not available, it would be useful to have QMP commands to set the battery charge and state (also max capacity).

But the battery state is determined by the physical battery, or by an externally provided file. Do you mean introducing another source for battery information which will be controlled by QMP commands? As for the max capacity, as with an actual battery, the "QEMU battery" has it set "by the manufacturer". It is not passed through from the host, for simplicity sake, and only the percentage is passed. How will it help if we allow to set the max capacity? It's something pretty much transparent to the user. (But if there is a use case, of course it can be done.)

Quoted 8 lines

Ditto QMP command to set the LID/AC adapter state.

Quoted 6 lines

But of course, the main purpose here is addressing the desktop users.

This series was tested with Windows and (desktop) Linux guests, on which indeed the battery icon appears in the corresponding state (full, charging, discharging, time remaining to empty, etc.) and the AC adapter plugging/unplugging behaves as expected. So is the laptop lid button.

Quoted 7 lines

[...]

In patch #2 you comment 'if a "fake" host battery is to be provided, a 'sysfs_path' property allows to override the default one.'.

Eventually you'd provide a such fake file as example, ideally used by a QTest.

Sure! I will - it's definitely a good idea.

Quoted 2 lines

Another question. If the battery is disconnected, is there an event propagated to the guest?

No. I definitely need to add! Thanks!

Quoted 1 line

Thanks for contributing these patches :)

Thank you!
Leonid.

Quoted 1 line

Phil.

#7 lore raw
Quoted 35 lines · Leonid Bloch

Hi Phil,

Thanks for your feedback! Please see below.

Quoted 18 lines · Philippe Mathieu-Daudé

Hi Leonid, Marcel,

Quoted 6 lines · Leonid Bloch

This series introduces the following ACPI devices:

* Battery
* AC adapter
* Laptop lid button

Quoted 7 lines

When running QEMU on a laptop, these paravirtualized devices reflect the state of these physical devices onto the guest. This functionality is relevant not only for laptops, but also for any other device which has e.g. a battery. This even allows to insert a ``fake'' battery to the guest, in a form of a file which emulates the behavior of the actual battery in sysfs. A possible use case for such a ``fake'' battery can be limiting the budget of VM usage to a subscriber, in a naturally-visible way.

Quoted 6 lines

Your series looks good. Now for this feature to be even more useful for the community, it would be better to

1/ Have a generic (kind of abstract QDev) battery model.
Your model would be the ISA implementation. But we could add LPC, SPI or I2C implementations for example.

It definitely feels that it needs to be more generic, and I thought how to make it so, but so far it is what I came up with. I'll think some more, but any ideas are welcome, cause nowadays I'm doing this in my free time only.

Quoted 8 lines
Quoted 4 lines

2/ Make it a model backend accepting various kind of frontends:
- host Linux sysfs mirroring is a particular frontend implementation
- mirroring on Windows would be another
- any connection (TCP) to battery simulator (Octave, ...)

Well, it does accept an arbitrary file to represent a battery, so this covers the battery simulator, does it? As for Windows - indeed, it would be nice to have.

Poking at sysfs from QEMU poses a bunch of issues, for example, security, migration, etc. Running timers on the host is also not nice since it causes exits from VM ...

So I agree, as a starting point let's just let user control the battery level through QMP.

Quoted 42 lines
Quoted 2 lines

Meanwhile 2/ is not available, it would be useful to have QMP commands to set the battery charge and state (also max capacity).

But the battery state is determined by the physical battery, or by an externally provided file. Do you mean introducing another source for battery information which will be controlled by QMP commands? As for the max capacity, as with an actual battery, the "QEMU battery" has it set "by the manufacturer". It is not passed through from the host, for simplicity sake, and only the percentage is passed. How will it help if we allow to set the max capacity? It's something pretty much transparent to the user. (But if there is a use case, of course it can be done.)

Quoted 8 lines

Ditto QMP command to set the LID/AC adapter state.

Quoted 6 lines

But of course, the main purpose here is addressing the desktop users.

This series was tested with Windows and (desktop) Linux guests, on which indeed the battery icon appears in the corresponding state (full, charging, discharging, time remaining to empty, etc.) and the AC adapter plugging/unplugging behaves as expected. So is the laptop lid button.

Quoted 7 lines

[...]

In patch #2 you comment 'if a "fake" host battery is to be provided, a 'sysfs_path' property allows to override the default one.'.

Eventually you'd provide a such fake file as example, ideally used by a QTest.

Sure! I will - it's definitely a good idea.

Quoted 2 lines

Another question. If the battery is disconnected, is there an event propagated to the guest?

No. I definitely need to add! Thanks!

Quoted 1 line

Thanks for contributing these patches :)

Thank you!
Leonid.

Quoted 1 line

Phil.

#8 lore raw
Quoted 8 lines · Michael S. Tsirkin

Poking at sysfs from QEMU poses a bunch of issues, for example, security, migration, etc. Running timers on the host is also not nice since it causes exits from VM ...

So I agree, as a starting point let's just let user control the battery level through QMP.

Thanks for the review, Michael. Yep, I fully agree - indeed looking at the host's battery is better to be left for the user, and QEMU should just set what the guest sees. Will modify accordingly, and send a v2.

Cheers,
Leonid.