2019-08-29 13:58:25 +00:00
|
|
|
// Copyright © 2019 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2021-05-11 14:19:04 +00:00
|
|
|
use super::AcpiNotificationFlags;
|
2020-02-25 07:20:59 +00:00
|
|
|
use acpi_tables::{aml, aml::Aml};
|
2020-12-04 09:23:47 +00:00
|
|
|
use std::sync::{Arc, Barrier};
|
2020-07-23 09:17:07 +00:00
|
|
|
use std::time::Instant;
|
2020-01-20 15:00:18 +00:00
|
|
|
use vm_device::interrupt::InterruptSourceGroup;
|
2020-09-09 14:30:31 +00:00
|
|
|
use vm_device::BusDevice;
|
2021-01-20 15:32:10 +00:00
|
|
|
use vm_memory::GuestAddress;
|
2019-08-29 13:58:25 +00:00
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
|
|
|
|
2021-01-20 15:32:10 +00:00
|
|
|
pub const GED_DEVICE_ACPI_SIZE: usize = 0x1;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 09:23:47 +00:00
|
|
|
fn write(&mut self, _base: u64, _offset: u64, data: &[u8]) -> Option<Arc<Barrier>> {
|
2019-08-29 13:58:25 +00:00
|
|
|
if data[0] == 1 {
|
2021-09-03 09:27:21 +00:00
|
|
|
info!("ACPI Reboot signalled");
|
2019-08-29 13:58:25 +00:00
|
|
|
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) {
|
2021-09-03 09:27:21 +00:00
|
|
|
info!("ACPI Shutdown signalled");
|
2019-08-29 13:58:25 +00:00
|
|
|
if let Err(e) = self.exit_evt.write(1) {
|
|
|
|
error!("Error triggering ACPI shutdown event: {}", e);
|
|
|
|
}
|
|
|
|
}
|
2020-12-04 09:23:47 +00:00
|
|
|
None
|
2019-08-29 13:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-27 15:27:09 +00:00
|
|
|
|
|
|
|
/// A device for handling ACPI GED event generation
|
2021-03-25 17:01:21 +00:00
|
|
|
pub struct AcpiGedDevice {
|
2021-07-29 09:15:10 +00:00
|
|
|
interrupt: Arc<dyn InterruptSourceGroup>,
|
2021-01-12 15:10:05 +00:00
|
|
|
notification_type: AcpiNotificationFlags,
|
2019-12-09 15:07:31 +00:00
|
|
|
ged_irq: u32,
|
2021-01-20 15:32:10 +00:00
|
|
|
address: GuestAddress,
|
2019-11-27 15:27:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 17:01:21 +00:00
|
|
|
impl AcpiGedDevice {
|
2021-01-20 15:32:10 +00:00
|
|
|
pub fn new(
|
2021-07-29 09:15:10 +00:00
|
|
|
interrupt: Arc<dyn InterruptSourceGroup>,
|
2021-01-20 15:32:10 +00:00
|
|
|
ged_irq: u32,
|
|
|
|
address: GuestAddress,
|
2021-03-25 17:01:21 +00:00
|
|
|
) -> AcpiGedDevice {
|
|
|
|
AcpiGedDevice {
|
2019-11-27 15:27:09 +00:00
|
|
|
interrupt,
|
2021-01-12 15:10:05 +00:00
|
|
|
notification_type: AcpiNotificationFlags::NO_DEVICES_CHANGED,
|
2019-12-09 15:07:31 +00:00
|
|
|
ged_irq,
|
2021-01-20 15:32:10 +00:00
|
|
|
address,
|
2019-11-27 15:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn notify(
|
|
|
|
&mut self,
|
2021-01-12 15:10:05 +00:00
|
|
|
notification_type: AcpiNotificationFlags,
|
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;
|
2020-01-20 15:00:18 +00:00
|
|
|
self.interrupt.trigger(0)
|
2019-11-27 15:27:09 +00:00
|
|
|
}
|
2019-12-09 15:07:31 +00:00
|
|
|
|
|
|
|
pub fn irq(&self) -> u32 {
|
|
|
|
self.ged_irq
|
|
|
|
}
|
2019-11-27 15:27:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 10:36:39 +00:00
|
|
|
// I/O port reports what type of notification was made
|
2021-03-25 17:01:21 +00:00
|
|
|
impl BusDevice for AcpiGedDevice {
|
2019-11-27 15:27:09 +00:00
|
|
|
// 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();
|
2021-01-12 15:10:05 +00:00
|
|
|
self.notification_type = AcpiNotificationFlags::NO_DEVICES_CHANGED;
|
2019-11-27 15:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-25 07:20:59 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "acpi")]
|
2021-03-25 17:01:21 +00:00
|
|
|
impl Aml for AcpiGedDevice {
|
2020-02-25 07:20:59 +00:00
|
|
|
fn to_aml_bytes(&self) -> Vec<u8> {
|
|
|
|
aml::Device::new(
|
|
|
|
"_SB_.GED_".into(),
|
|
|
|
vec![
|
|
|
|
&aml::Name::new("_HID".into(), &"ACPI0013"),
|
|
|
|
&aml::Name::new("_UID".into(), &aml::ZERO),
|
|
|
|
&aml::Name::new(
|
|
|
|
"_CRS".into(),
|
|
|
|
&aml::ResourceTemplate::new(vec![&aml::Interrupt::new(
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
self.ged_irq,
|
|
|
|
)]),
|
|
|
|
),
|
2021-01-20 15:32:10 +00:00
|
|
|
&aml::OpRegion::new(
|
|
|
|
"GDST".into(),
|
|
|
|
aml::OpRegionSpace::SystemMemory,
|
|
|
|
self.address.0 as usize,
|
|
|
|
GED_DEVICE_ACPI_SIZE,
|
|
|
|
),
|
2020-02-25 07:20:59 +00:00
|
|
|
&aml::Field::new(
|
|
|
|
"GDST".into(),
|
|
|
|
aml::FieldAccessType::Byte,
|
|
|
|
aml::FieldUpdateRule::WriteAsZeroes,
|
|
|
|
vec![aml::FieldEntry::Named(*b"GDAT", 8)],
|
|
|
|
),
|
|
|
|
&aml::Method::new(
|
|
|
|
"_EVT".into(),
|
|
|
|
1,
|
|
|
|
true,
|
|
|
|
vec![
|
|
|
|
&aml::Store::new(&aml::Local(0), &aml::Path::new("GDAT")),
|
|
|
|
&aml::And::new(&aml::Local(1), &aml::Local(0), &aml::ONE),
|
|
|
|
&aml::If::new(
|
|
|
|
&aml::Equal::new(&aml::Local(1), &aml::ONE),
|
|
|
|
vec![&aml::MethodCall::new("\\_SB_.CPUS.CSCN".into(), vec![])],
|
|
|
|
),
|
|
|
|
&aml::And::new(&aml::Local(1), &aml::Local(0), &2usize),
|
|
|
|
&aml::If::new(
|
|
|
|
&aml::Equal::new(&aml::Local(1), &2usize),
|
|
|
|
vec![&aml::MethodCall::new("\\_SB_.MHPC.MSCN".into(), vec![])],
|
|
|
|
),
|
2020-02-26 16:45:06 +00:00
|
|
|
&aml::And::new(&aml::Local(1), &aml::Local(0), &4usize),
|
|
|
|
&aml::If::new(
|
|
|
|
&aml::Equal::new(&aml::Local(1), &4usize),
|
|
|
|
vec![&aml::MethodCall::new("\\_SB_.PCI0.PCNT".into(), vec![])],
|
|
|
|
),
|
2021-01-13 10:02:12 +00:00
|
|
|
&aml::And::new(&aml::Local(1), &aml::Local(0), &8usize),
|
|
|
|
&aml::If::new(
|
|
|
|
&aml::Equal::new(&aml::Local(1), &8usize),
|
|
|
|
vec![&aml::Notify::new(
|
|
|
|
&aml::Path::new("\\_SB_.PWRB"),
|
|
|
|
&0x80usize,
|
|
|
|
)],
|
|
|
|
),
|
2020-02-25 07:20:59 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
.to_aml_bytes()
|
|
|
|
}
|
|
|
|
}
|
2020-07-23 09:17:07 +00:00
|
|
|
|
2021-03-25 17:01:21 +00:00
|
|
|
pub struct AcpiPmTimerDevice {
|
2020-07-23 09:17:07 +00:00
|
|
|
start: Instant,
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:01:21 +00:00
|
|
|
impl AcpiPmTimerDevice {
|
2020-07-23 09:17:07 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
start: Instant::now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:01:21 +00:00
|
|
|
impl Default for AcpiPmTimerDevice {
|
2020-07-23 09:17:07 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:01:21 +00:00
|
|
|
impl BusDevice for AcpiPmTimerDevice {
|
2020-07-23 09:17:07 +00:00
|
|
|
fn read(&mut self, _base: u64, _offset: u64, data: &mut [u8]) {
|
|
|
|
let now = Instant::now();
|
|
|
|
let since = now.duration_since(self.start);
|
|
|
|
let nanos = since.as_nanos();
|
|
|
|
|
|
|
|
const PM_TIMER_FREQUENCY_HZ: u128 = 3_579_545;
|
|
|
|
const NANOS_PER_SECOND: u128 = 1_000_000_000;
|
|
|
|
|
|
|
|
let counter = (nanos * PM_TIMER_FREQUENCY_HZ) / NANOS_PER_SECOND;
|
|
|
|
let counter: u32 = (counter & 0xffff_ffff) as u32;
|
|
|
|
|
|
|
|
data.copy_from_slice(&counter.to_le_bytes());
|
|
|
|
}
|
|
|
|
}
|