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
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::sync::Arc;

use acpi_tables::aml;
use acpi_tables::aml::Aml;
use base::warn;
use sync::Condvar;
use sync::Mutex;

use crate::pci::CrosvmDeviceId;
use crate::BusAccessInfo;
use crate::BusDevice;
use crate::DeviceId;
use crate::Suspendable;

/// PMC Virt MMIO offset
const PMC_RESERVED1: u32 = 0;
const _PMC_RESERVED2: u32 = 0x4;
const _PMC_RESERVED3: u32 = 0x8;
const _PMC_RESERVED4: u32 = 0xc;

pub const VPMC_VIRT_MMIO_SIZE: u64 = 0x10;

pub struct VirtualPmc {
    mmio_base: u64,
    guest_suspended_cvar: Arc<(Mutex<bool>, Condvar)>,
}

impl VirtualPmc {
    pub fn new(mmio_base: u64, guest_suspended_cvar: Arc<(Mutex<bool>, Condvar)>) -> Self {
        VirtualPmc {
            mmio_base,
            guest_suspended_cvar,
        }
    }
}

fn handle_s2idle_request(guest_suspended_cvar: &Arc<(Mutex<bool>, Condvar)>) {
    // Wake up blocked thread on condvar, which is awaiting non-privileged guest suspension to
    // finish.
    let (lock, cvar) = &**guest_suspended_cvar;
    let mut guest_suspended = lock.lock();
    *guest_suspended = true;

    cvar.notify_one();
}

impl BusDevice for VirtualPmc {
    fn device_id(&self) -> DeviceId {
        CrosvmDeviceId::VirtualPmc.into()
    }
    fn debug_label(&self) -> String {
        "PmcVirt".to_owned()
    }

    fn read(&mut self, info: BusAccessInfo, data: &mut [u8]) {
        if data.len() != std::mem::size_of::<u32>() {
            warn!(
                "{}: unsupported read length {}, only support 4bytes read",
                self.debug_label(),
                data.len()
            );
            return;
        }

        warn!("{}: unsupported read address {}", self.debug_label(), info);
    }

    fn write(&mut self, info: BusAccessInfo, data: &[u8]) {
        if data.len() != std::mem::size_of::<u32>() {
            warn!(
                "{}: unsupported write length {}, only support 4bytes write",
                self.debug_label(),
                data.len()
            );
            return;
        }

        match info.offset as u32 {
            PMC_RESERVED1 => {
                handle_s2idle_request(&self.guest_suspended_cvar);
            }
            _ => {
                warn!("{}: Bad write to address {}", self.debug_label(), info);
            }
        };
    }
}

impl Aml for VirtualPmc {
    fn to_aml_bytes(&self, bytes: &mut Vec<u8>) {
        let vpmc_uuid = "9ea49ba3-434a-49a6-be30-37cc55c4d397";
        aml::Device::new(
            "VPMC".into(),
            vec![
                &aml::Name::new("_CID".into(), &aml::EISAName::new("PNP0D80")),
                &aml::Name::new("_HID".into(), &"HYPE0001"),
                &aml::Name::new("UUID".into(), &aml::Uuid::new(vpmc_uuid)),
                &aml::OpRegion::new(
                    "VREG".into(),
                    aml::OpRegionSpace::SystemMemory,
                    &self.mmio_base,
                    &(16_u32),
                ),
                &aml::Field::new(
                    "VREG".into(),
                    aml::FieldAccessType::DWord,
                    aml::FieldLockRule::Lock,
                    aml::FieldUpdateRule::Preserve,
                    vec![aml::FieldEntry::Named(*b"RSV1", 32)],
                ),
                &aml::Method::new(
                    "_DSM".into(),
                    4,
                    true,
                    vec![
                        &aml::If::new(
                            &aml::Equal::new(&aml::Arg(0), &aml::Name::new_field_name("UUID")),
                            vec![
                                &aml::If::new(
                                    &aml::Equal::new(&aml::Arg(2), &aml::ZERO),
                                    vec![&aml::Return::new(&aml::BufferData::new(vec![3]))],
                                ),
                                &aml::If::new(
                                    &aml::Equal::new(&aml::Arg(2), &aml::ONE),
                                    vec![&aml::Store::new(
                                        &aml::Name::new_field_name("RSV1"),
                                        &0x3_usize,
                                    )],
                                ),
                            ],
                        ),
                        &aml::Return::new(&aml::BufferData::new(vec![3])),
                    ],
                ),
            ],
        )
        .to_aml_bytes(bytes);
    }
}

impl Suspendable for VirtualPmc {}