2019-08-29 13:58:25 +00:00
|
|
|
// Copyright © 2019 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
|
|
|
use BusDevice;
|
2020-01-14 10:17:23 +00:00
|
|
|
use HotPlugNotificationFlags;
|
2019-11-27 15:27:09 +00:00
|
|
|
use Interrupt;
|
2019-08-29 13:58:25 +00:00
|
|
|
|
|
|
|
/// A device for handling ACPI shutdown and reboot
|
|
|
|
pub struct AcpiShutdownDevice {
|
|
|
|
exit_evt: EventFd,
|
|
|
|
reset_evt: EventFd,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AcpiShutdownDevice {
|
|
|
|
/// Constructs a device that will signal the given event when the guest requests it.
|
|
|
|
pub fn new(exit_evt: EventFd, reset_evt: EventFd) -> AcpiShutdownDevice {
|
|
|
|
AcpiShutdownDevice {
|
|
|
|
exit_evt,
|
|
|
|
reset_evt,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same I/O port used for shutdown and reboot
|
|
|
|
impl BusDevice for AcpiShutdownDevice {
|
|
|
|
// Spec has all fields as zero
|
|
|
|
fn read(&mut self, _base: u64, _offset: u64, data: &mut [u8]) {
|
|
|
|
for i in data.iter_mut() {
|
|
|
|
*i = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, _base: u64, _offset: u64, data: &[u8]) {
|
|
|
|
if data[0] == 1 {
|
|
|
|
debug!("ACPI Reboot signalled");
|
|
|
|
if let Err(e) = self.reset_evt.write(1) {
|
|
|
|
error!("Error triggering ACPI reset event: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The ACPI DSDT table specifies the S5 sleep state (shutdown) as value 5
|
|
|
|
const S5_SLEEP_VALUE: u8 = 5;
|
|
|
|
const SLEEP_STATUS_EN_BIT: u8 = 5;
|
|
|
|
const SLEEP_VALUE_BIT: u8 = 2;
|
|
|
|
if data[0] == (S5_SLEEP_VALUE << SLEEP_VALUE_BIT) | (1 << SLEEP_STATUS_EN_BIT) {
|
|
|
|
debug!("ACPI Shutdown signalled");
|
2020-01-14 10:17:23 +00:00
|
|
|
extern crate bitflags;
|
2019-08-29 13:58:25 +00:00
|
|
|
if let Err(e) = self.exit_evt.write(1) {
|
|
|
|
error!("Error triggering ACPI shutdown event: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-27 15:27:09 +00:00
|
|
|
|
|
|
|
/// A device for handling ACPI GED event generation
|
|
|
|
pub struct AcpiGEDDevice {
|
|
|
|
interrupt: Box<dyn Interrupt>,
|
2020-01-14 10:17:23 +00:00
|
|
|
notification_type: HotPlugNotificationFlags,
|
2019-12-09 15:07:31 +00:00
|
|
|
ged_irq: u32,
|
2019-11-27 15:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AcpiGEDDevice {
|
2019-12-09 15:07:31 +00:00
|
|
|
pub fn new(interrupt: Box<dyn Interrupt>, ged_irq: u32) -> AcpiGEDDevice {
|
2019-11-27 15:27:09 +00:00
|
|
|
AcpiGEDDevice {
|
|
|
|
interrupt,
|
2020-01-14 10:17:23 +00:00
|
|
|
notification_type: HotPlugNotificationFlags::NO_DEVICES_CHANGED,
|
2019-12-09 15:07:31 +00:00
|
|
|
ged_irq,
|
2019-11-27 15:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn notify(
|
|
|
|
&mut self,
|
2020-01-14 10:17:23 +00:00
|
|
|
notification_type: HotPlugNotificationFlags,
|
2019-11-27 15:27:09 +00:00
|
|
|
) -> Result<(), std::io::Error> {
|
2020-01-14 10:17:23 +00:00
|
|
|
self.notification_type |= notification_type;
|
2019-11-27 15:27:09 +00:00
|
|
|
self.interrupt.deliver()
|
|
|
|
}
|
2019-12-09 15:07:31 +00:00
|
|
|
|
|
|
|
pub fn irq(&self) -> u32 {
|
|
|
|
self.ged_irq
|
|
|
|
}
|
2019-11-27 15:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// I/O port reports what type of notification was made
|
|
|
|
impl BusDevice for AcpiGEDDevice {
|
|
|
|
// Spec has all fields as zero
|
|
|
|
fn read(&mut self, _base: u64, _offset: u64, data: &mut [u8]) {
|
2020-01-14 10:17:23 +00:00
|
|
|
data[0] = self.notification_type.bits();
|
|
|
|
self.notification_type = HotPlugNotificationFlags::NO_DEVICES_CHANGED;
|
2019-11-27 15:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, _base: u64, _offset: u64, _data: &[u8]) {}
|
|
|
|
}
|