1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Virtual battery device.
//!
//! This virtual battery device simulates the host's battery state for the
//! guest. Rather than modeling a real device, it's a Hyper-V specific design,
//! tailored to meet the
//! [ACPI Battery and Power Subsystem Firmware Implementation][acpi] and the
//! additional requirements imposed by the
//! [Windows Hardware Design Guidelines][whdg].
//!
//! This device was implemented alongside the ACPI code in Hyper-V UEFI. For
//! more information, refer to the DSDT in the UEFI codebase: [mu_msvm][uefi].
//!
//! For historical context, this device was originally designed for x86 and was
//! later adapted for ARM64.
//!
//! This device uses `LineInterrupts` to signal state changes to interested
//! parties (e.g: the PM device on x86, a system IRQ on Aarch64). A mesh channel
//! is used to receive battery state updates from the host platform.
//!
//! Refer to the following resources for more context:
//! - [ACPI Battery and Power Subsystem Firmware Implementation][acpi]
//! - [UEFI codebase (mu_msvm)][uefi]
//! - [Windows Hardware Design Guidelines][whdg]
//!
//! [acpi]: https://uefi.org/htmlspecs/ACPI_Spec_6_4_html/10_Power_Source_and_Power_Meter_Devices/Power_Source_and_Power_Meter_Devices.html#control-method-batteries
//! [uefi]: https://github.com/Microsoft/mu_msvm
//! [whdg]: https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/acpi-battery-and-power-subsystem-firmware-implementation

pub mod resolver;

use chipset_device::io::IoError;
use chipset_device::io::IoResult;
use chipset_device::mmio::MmioIntercept;
use chipset_device::poll_device::PollDevice;
use chipset_device::ChipsetDevice;
use chipset_resources::battery::HostBatteryUpdate;
use futures::StreamExt;
use inspect::Inspect;
use inspect::InspectMut;
use open_enum::open_enum;
use std::ops::RangeInclusive;
use vmcore::device_state::ChangeDeviceState;
use vmcore::line_interrupt::LineInterrupt;

// Battery MMIO constants
pub const BATTERY_MMIO_REGION_BASE_ADDRESS_X64: u64 = 0xfed3f000;
pub const BATTERY_MMIO_REGION_BASE_ADDRESS_ARM: u64 = 0xEFFEA000;
pub const BATTERY_DEVICE_MMIO_REGION_SIZE: u64 = 0x20;
pub const BATTERY_DEVICE_MMIO_REGION_MASK: u64 = BATTERY_DEVICE_MMIO_REGION_SIZE - 1;

// Battery interrupt lines. For x64, use GPE0 bit 9.
// For ARM64, use IRQ 4 [derived from 4 + 32 (SPI range start) = 36].
pub const BATTERY_STATUS_GPE0_LINE: u32 = 9;
pub const BATTERY_STATUS_IRQ_NO: u32 = 4;

// Bits to set for notifications.
//
// NOTE: ACPI_DEVICE_NOTIFY_BST_CHANGED is deprecated and should not be used.
// It is kept here for reference.
//
// No functionality is lost by not using this constant, but we prefer
// ACPI_DEVICE_NOTIFY_BIX_CHANGED because it tells the guest to check for both
// the BST and BIX registers.
#[allow(unused)]
pub const ACPI_DEVICE_NOTIFY_BST_CHANGED: u32 = 0x1;
pub const ACPI_DEVICE_NOTIFY_BIX_CHANGED: u32 = 0x2;

// Defines what bits are allowed to be set for notifications
pub const ACPI_DEVICE_NOTIFY_VALID_BITS: u32 = 0x3;

// Virtual battery capacity, based on the UEFI DSDT's design capacity
pub const VIRTUAL_BATTERY_CAPACITY: u32 = 5000;

// Battery register offsets
open_enum! {
    #[derive(Inspect)]
    #[inspect(debug)]
    pub enum RegisterOffset: u64 {
        STA_BATTERY_STATUS = 0x0,
        BST_BATTERY_STATE = 0x4,
        BST_BATTERY_PRESENT_RATE = 0x8,
        BST_BATTERY_REMAINING_CAPACITY = 0xc,
        PSR_AC_POWER_STATUS = 0x10,
        BATTERY_ACPI_NOTIFY_STATUS = 0x14,
        BATTERY_ACPI_NOTIFY_CLEAR = 0x18,
    }
}

/// Various runtime objects used by the BatteryDevice
pub struct BatteryRuntimeDeps {
    pub battery_status_recv: mesh::Receiver<HostBatteryUpdate>,
    pub notify_interrupt: LineInterrupt,
}

/// Virtual battery device.
#[derive(InspectMut)]
pub struct BatteryDevice {
    // Runtime glue
    #[inspect(skip)]
    rt: BatteryRuntimeDeps,

    // Static configuration
    #[inspect(skip)]
    mmio_region: (&'static str, RangeInclusive<u64>),
    base_addr: u64,

    // Volatile state
    notify_bits: u32,
    state: HostBatteryUpdate,
}

impl BatteryDevice {
    /// Create a new battery device
    pub fn new(platform: BatteryRuntimeDeps, base_addr: u64) -> Self {
        BatteryDevice {
            rt: platform,
            mmio_region: (
                "battery",
                base_addr..=base_addr + (BATTERY_DEVICE_MMIO_REGION_SIZE - 1),
            ),
            base_addr,
            state: HostBatteryUpdate::default(),
            notify_bits: 0,
        }
    }

    fn read_register(&self, offset: RegisterOffset) -> u32 {
        match offset {
            RegisterOffset::STA_BATTERY_STATUS => {
                if self.state.battery_present {
                    0x1F
                } else {
                    0xF
                }
            }
            RegisterOffset::BST_BATTERY_STATE => {
                if !self.state.battery_present {
                    0
                } else if self.state.charging {
                    0x2
                } else if self.state.discharging {
                    0x1
                } else {
                    // Somehow, we got in some weird state, return default 0.
                    tracelimit::warn_ratelimited!(
                        "BST_BATTERY_STATE encountered a weird state, defaulting to 0"
                    );
                    0
                }
            }
            RegisterOffset::BST_BATTERY_PRESENT_RATE => {
                if self.state.battery_present && self.state.max_capacity != 0 {
                    // Normalize the rate to the virtual battery's capacity.
                    (self.state.rate.saturating_mul(VIRTUAL_BATTERY_CAPACITY))
                        / self.state.max_capacity
                } else {
                    // Unknown rate.
                    0xFFFFFFFF
                }
            }
            RegisterOffset::BST_BATTERY_REMAINING_CAPACITY => {
                if self.state.battery_present && self.state.max_capacity != 0 {
                    // Normalize the remaining capacity to the virtual battery's capacity.
                    (self
                        .state
                        .remaining_capacity
                        .saturating_mul(VIRTUAL_BATTERY_CAPACITY))
                        / self.state.max_capacity
                } else {
                    // Unknown capacity.
                    0xFFFFFFFF
                }
            }
            RegisterOffset::PSR_AC_POWER_STATUS => {
                if self.state.ac_online {
                    0x1
                } else {
                    0x0
                }
            }
            RegisterOffset::BATTERY_ACPI_NOTIFY_STATUS => self.notify_bits,
            _ => 0,
        }
    }

    fn write_register(&mut self, offset: RegisterOffset, value: u32) {
        if offset != RegisterOffset::BATTERY_ACPI_NOTIFY_CLEAR {
            // Only writes allowed are to clear notification bits.
            tracelimit::warn_ratelimited!("Invalid write to battery device at offset {:?}", offset);
            return;
        }

        // Clear any bits that were set to 1
        self.notify_bits &= !value;
        self.notify_bits &= ACPI_DEVICE_NOTIFY_VALID_BITS;

        // Re-evaluate interrupt for any pending bits left
        self.check_interrupt_assertion();
    }

    /// evaluates whether the battery's interrupt should be
    /// asserted or de-asserted
    fn check_interrupt_assertion(&self) {
        self.rt.notify_interrupt.set_level(self.notify_bits != 0)
    }
}

impl ChangeDeviceState for BatteryDevice {
    fn start(&mut self) {}

    async fn stop(&mut self) {}

    async fn reset(&mut self) {
        let Self {
            rt,
            mmio_region: _,
            base_addr: _,
            notify_bits,
            state,
        } = self;
        *state = HostBatteryUpdate::default();
        rt.notify_interrupt.set_level(false);
        *notify_bits = 0;
    }
}

impl ChipsetDevice for BatteryDevice {
    fn supports_mmio(&mut self) -> Option<&mut dyn MmioIntercept> {
        Some(self)
    }

    fn supports_poll_device(&mut self) -> Option<&mut dyn PollDevice> {
        Some(self)
    }
}

impl MmioIntercept for BatteryDevice {
    fn mmio_read(&mut self, address: u64, data: &mut [u8]) -> IoResult {
        assert_eq!(address & !BATTERY_DEVICE_MMIO_REGION_MASK, self.base_addr);
        if data.len() == size_of::<u32>() {
            let value =
                self.read_register(RegisterOffset(address & BATTERY_DEVICE_MMIO_REGION_MASK));
            data.copy_from_slice(&value.to_ne_bytes());
            IoResult::Ok
        } else {
            IoResult::Err(IoError::InvalidAccessSize)
        }
    }

    fn mmio_write(&mut self, address: u64, data: &[u8]) -> IoResult {
        assert_eq!(address & !BATTERY_DEVICE_MMIO_REGION_MASK, self.base_addr);
        if let Ok(x) = data.try_into().map(u32::from_ne_bytes) {
            self.write_register(RegisterOffset(address & BATTERY_DEVICE_MMIO_REGION_MASK), x);
            IoResult::Ok
        } else {
            IoResult::Err(IoError::InvalidAccessSize)
        }
    }

    fn get_static_regions(&mut self) -> &[(&str, RangeInclusive<u64>)] {
        std::slice::from_ref(&self.mmio_region)
    }
}

impl PollDevice for BatteryDevice {
    fn poll_device(&mut self, cx: &mut std::task::Context<'_>) {
        while let std::task::Poll::Ready(Some(update)) =
            self.rt.battery_status_recv.poll_next_unpin(cx)
        {
            self.state = update;
            if self.state.battery_present && self.state.max_capacity == 0 {
                // This configuration makes no sense. Just report no battery,
                // and set AC power accordingly.
                tracelimit::warn_ratelimited!("BATTERY: Invalid state: max_capacity is 0 but battery is present. Marking battery as not present.");
                self.state.battery_present = false;
            }
            // Add the corresponding status bit to notification status
            self.notify_bits |= ACPI_DEVICE_NOTIFY_BIX_CHANGED;
            self.check_interrupt_assertion();
        }
    }
}

mod save_restore {
    use super::*;
    use vmcore::save_restore::RestoreError;
    use vmcore::save_restore::SaveError;
    use vmcore::save_restore::SaveRestore;

    mod state {
        use mesh::payload::Protobuf;
        use vmcore::save_restore::SavedStateRoot;

        #[derive(Protobuf, SavedStateRoot)]
        #[mesh(package = "firmware.battery")]
        pub struct SavedState {
            #[mesh(1)]
            pub notify_bits: u32,
            #[mesh(2)]
            pub battery_state: BatterySavedState,
        }

        #[derive(Protobuf, SavedStateRoot)]
        #[mesh(package = "firmware.battery")]
        pub struct BatterySavedState {
            #[mesh(1)]
            pub battery_present: bool,
            #[mesh(2)]
            pub charging: bool,
            #[mesh(3)]
            pub discharging: bool,
            #[mesh(4)]
            pub rate: u32,
            #[mesh(5)]
            pub remaining_capacity: u32,
            #[mesh(6)]
            pub max_capacity: u32,
            #[mesh(7)]
            pub ac_online: bool,
        }
    }

    impl SaveRestore for BatteryDevice {
        type SavedState = state::SavedState;

        fn save(&mut self) -> Result<Self::SavedState, SaveError> {
            let Self {
                rt: _,
                mmio_region: _,
                base_addr: _,
                notify_bits,
                state:
                    HostBatteryUpdate {
                        battery_present,
                        charging,
                        discharging,
                        rate,
                        remaining_capacity,
                        max_capacity,
                        ac_online,
                    },
            } = *self;

            let saved_state = state::SavedState {
                notify_bits,
                battery_state: state::BatterySavedState {
                    battery_present,
                    charging,
                    discharging,
                    rate,
                    remaining_capacity,
                    max_capacity,
                    ac_online,
                },
            };

            Ok(saved_state)
        }

        fn restore(&mut self, saved_state: Self::SavedState) -> Result<(), RestoreError> {
            let state::SavedState {
                notify_bits,
                battery_state:
                    state::BatterySavedState {
                        battery_present,
                        charging,
                        discharging,
                        rate,
                        remaining_capacity,
                        max_capacity,
                        ac_online,
                    },
            } = saved_state;

            self.notify_bits = notify_bits;
            self.state = HostBatteryUpdate {
                battery_present,
                charging,
                discharging,
                rate,
                remaining_capacity,
                max_capacity,
                ac_online,
            };

            self.check_interrupt_assertion();

            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures::task::Context;
    use vmcore::line_interrupt::LineInterrupt;

    fn create_test_platform() -> (BatteryDevice, mesh::Sender<HostBatteryUpdate>) {
        let (tx, rx) = mesh::channel::<HostBatteryUpdate>();
        let battery = BatteryDevice::new(
            BatteryRuntimeDeps {
                battery_status_recv: {
                    tx.send(HostBatteryUpdate::default());
                    rx
                },
                notify_interrupt: LineInterrupt::detached(),
            },
            BATTERY_MMIO_REGION_BASE_ADDRESS_X64,
        );
        (battery, tx)
    }

    fn mmio_read_helper(battery: &mut BatteryDevice, offset: u64) -> [u8; 4] {
        let mut bytes = [0; 4];
        battery
            .mmio_read(battery.base_addr + offset, &mut bytes)
            .unwrap();
        bytes
    }

    fn check_mmio_read(battery: &mut BatteryDevice, offset: u64, expected_value: u32) {
        assert_eq!(
            u32::from_ne_bytes(mmio_read_helper(battery, offset)),
            expected_value
        );
        assert_eq!(
            battery.read_register(RegisterOffset(offset)),
            expected_value
        );
    }

    fn send_update(
        battery: &mut BatteryDevice,
        update: HostBatteryUpdate,
        sender: &mesh::Sender<HostBatteryUpdate>,
    ) {
        sender.send(update);
        battery.poll_device(&mut Context::from_waker(futures::task::noop_waker_ref()));
    }

    /// Test basic battery mmio behavior
    #[test]
    fn test_basic_battery_mmio() {
        // create battery, send channel, and a blank battery state
        let (mut battery, tx) = create_test_platform();
        let mut state = HostBatteryUpdate::default();

        // test uninitialized state.
        check_mmio_read(&mut battery, RegisterOffset::STA_BATTERY_STATUS.0, 0xF);
        check_mmio_read(&mut battery, RegisterOffset::BST_BATTERY_STATE.0, 0);
        check_mmio_read(
            &mut battery,
            RegisterOffset::BST_BATTERY_PRESENT_RATE.0,
            0xFFFFFFFF,
        );
        check_mmio_read(
            &mut battery,
            RegisterOffset::BST_BATTERY_REMAINING_CAPACITY.0,
            0xFFFFFFFF,
        );
        check_mmio_read(&mut battery, RegisterOffset::PSR_AC_POWER_STATUS.0, 0);
        check_mmio_read(
            &mut battery,
            RegisterOffset::BATTERY_ACPI_NOTIFY_STATUS.0,
            0,
        );

        // set battery to be present, with AC power and full capacity
        state.battery_present = true;
        state.remaining_capacity = 100;
        state.max_capacity = 100;
        state.ac_online = true;
        send_update(&mut battery, state, &tx);

        check_mmio_read(&mut battery, RegisterOffset::STA_BATTERY_STATUS.0, 0x1F);
        check_mmio_read(&mut battery, RegisterOffset::BST_BATTERY_STATE.0, 0);
        check_mmio_read(&mut battery, RegisterOffset::BST_BATTERY_PRESENT_RATE.0, 0);
        check_mmio_read(
            &mut battery,
            RegisterOffset::BST_BATTERY_REMAINING_CAPACITY.0,
            (100 * VIRTUAL_BATTERY_CAPACITY) / 100,
        );
        check_mmio_read(&mut battery, RegisterOffset::PSR_AC_POWER_STATUS.0, 0x1);
        check_mmio_read(
            &mut battery,
            RegisterOffset::BATTERY_ACPI_NOTIFY_STATUS.0,
            2,
        );

        // set battery to be charging, with 50% capacity
        state.charging = true;
        state.remaining_capacity = 50;
        state.rate = 40;
        send_update(&mut battery, state, &tx);

        check_mmio_read(&mut battery, RegisterOffset::STA_BATTERY_STATUS.0, 0x1F);
        check_mmio_read(&mut battery, RegisterOffset::BST_BATTERY_STATE.0, 0x2);
        check_mmio_read(
            &mut battery,
            RegisterOffset::BST_BATTERY_PRESENT_RATE.0,
            (40 * VIRTUAL_BATTERY_CAPACITY) / 100,
        );
        check_mmio_read(
            &mut battery,
            RegisterOffset::BST_BATTERY_REMAINING_CAPACITY.0,
            (50 * VIRTUAL_BATTERY_CAPACITY) / 100,
        );
        check_mmio_read(&mut battery, RegisterOffset::PSR_AC_POWER_STATUS.0, 0x1);
        check_mmio_read(
            &mut battery,
            RegisterOffset::BATTERY_ACPI_NOTIFY_STATUS.0,
            2,
        );

        // set battery to be discharging, no ac, 50% capacity
        state.ac_online = false;
        state.charging = false;
        state.discharging = true;
        state.rate = 45;
        send_update(&mut battery, state, &tx);
        check_mmio_read(&mut battery, RegisterOffset::STA_BATTERY_STATUS.0, 0x1F);
        check_mmio_read(&mut battery, RegisterOffset::BST_BATTERY_STATE.0, 0x1);
        check_mmio_read(
            &mut battery,
            RegisterOffset::BST_BATTERY_PRESENT_RATE.0,
            (45 * VIRTUAL_BATTERY_CAPACITY) / 100,
        );
        check_mmio_read(
            &mut battery,
            RegisterOffset::BST_BATTERY_REMAINING_CAPACITY.0,
            (50 * VIRTUAL_BATTERY_CAPACITY) / 100,
        );
        check_mmio_read(&mut battery, RegisterOffset::PSR_AC_POWER_STATUS.0, 0x0);
        check_mmio_read(
            &mut battery,
            RegisterOffset::BATTERY_ACPI_NOTIFY_STATUS.0,
            2,
        );

        // ensure that mmio_write clears the notify_bits
        let data: u32 = 0x2;
        let _ = battery.mmio_write(
            battery.base_addr + RegisterOffset::BATTERY_ACPI_NOTIFY_CLEAR.0,
            &data.to_ne_bytes(),
        );
        assert_eq!(battery.notify_bits, 0);
    }

    /// Test values when battery is not present
    #[test]
    fn test_battery_not_present() {
        // create battery and send channel
        let (mut battery, tx) = create_test_platform();

        // set ac power online, no battery present
        let state = HostBatteryUpdate {
            ac_online: true,
            ..HostBatteryUpdate::default()
        };
        send_update(&mut battery, state, &tx);
        check_mmio_read(&mut battery, RegisterOffset::STA_BATTERY_STATUS.0, 0xF);
        check_mmio_read(&mut battery, RegisterOffset::BST_BATTERY_STATE.0, 0);
        check_mmio_read(
            &mut battery,
            RegisterOffset::BST_BATTERY_PRESENT_RATE.0,
            0xFFFFFFFF,
        );
        check_mmio_read(
            &mut battery,
            RegisterOffset::BST_BATTERY_REMAINING_CAPACITY.0,
            0xFFFFFFFF,
        );
        check_mmio_read(&mut battery, RegisterOffset::PSR_AC_POWER_STATUS.0, 0x1);
        check_mmio_read(
            &mut battery,
            RegisterOffset::BATTERY_ACPI_NOTIFY_STATUS.0,
            0x2,
        );
    }

    /// Test bad register read/write operations and ensure they error
    #[test]
    fn test_bad_register_read_writes() {
        // create battery and send channel
        let (mut battery, _) = create_test_platform();

        // Test reading with a data size not equal to size_of::<u32>()
        let mut data = vec![0; size_of::<u32>() + 1];
        match battery.mmio_read(battery.base_addr, &mut data) {
            IoResult::Err(e) => assert!(matches!(e, IoError::InvalidAccessSize)),
            _ => panic!("Expected error, but got Ok"),
        }

        // Test writing with a data size not equal to size_of::<u32>()
        let data = vec![0; size_of::<u32>() + 1];
        match battery.mmio_write(battery.base_addr, &data) {
            IoResult::Err(e) => assert!(matches!(e, IoError::InvalidAccessSize)),
            _ => panic!("Expected error, but got Ok"),
        }

        // Test writing with data that cannot be converted into a `u32`
        let data = vec![0; size_of::<u32>() - 1];
        match battery.mmio_write(battery.base_addr, &data) {
            IoResult::Err(e) => assert!(matches!(e, IoError::InvalidAccessSize)),
            _ => panic!("Expected error, but got Ok"),
        }
    }

    /// Test bad capacity on a battery
    #[test]
    fn test_bad_battery_capacity() {
        // create battery and send channel
        let (mut battery, tx) = create_test_platform();

        // set battery present with 50% capacity and 0 max capacity
        let state = HostBatteryUpdate {
            battery_present: true,
            remaining_capacity: 50,
            max_capacity: 0,
            ac_online: true,
            ..HostBatteryUpdate::default()
        };
        send_update(&mut battery, state, &tx);
        check_mmio_read(&mut battery, RegisterOffset::STA_BATTERY_STATUS.0, 0xF);
        check_mmio_read(&mut battery, RegisterOffset::BST_BATTERY_STATE.0, 0x0);
        check_mmio_read(
            &mut battery,
            RegisterOffset::BST_BATTERY_PRESENT_RATE.0,
            0xFFFFFFFF,
        );
        check_mmio_read(
            &mut battery,
            RegisterOffset::BST_BATTERY_REMAINING_CAPACITY.0,
            0xFFFFFFFF,
        );
        check_mmio_read(&mut battery, RegisterOffset::PSR_AC_POWER_STATUS.0, 0x1);
        check_mmio_read(
            &mut battery,
            RegisterOffset::BATTERY_ACPI_NOTIFY_STATUS.0,
            0x2,
        );
    }
}