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
mod aml;
pub mod tables;

use alloc::format;
use aml::{
    execution::{AmlExecutionError, ExecutionContext},
    Aml,
};
use tables::facp;
pub use tables::init_acpi_tables;
use tracing::{error, info, warn};

use crate::{
    cpu::{
        self,
        idt::{BasicInterruptHandler, InterruptStackFrame64},
        interrupts::apic,
    },
    power,
    sync::once::OnceLock,
};

static ACPI: OnceLock<Acpi> = OnceLock::new();

/// Setup interrupts and request ownership of ACPI
pub fn init() {
    ACPI.set(Acpi::init())
        .expect("ACPI was already initialized");

    info!("ACPI initialized");
}

pub fn sleep(ty: u8) -> Result<(), AcpiError> {
    ACPI.get().sleep(ty)
}

#[derive(Debug)]
pub enum AcpiError {
    InvalidSleepType,
    SleepTypeNotAvailable,
}

/// Stores some items and data related to ACPI
#[derive(Debug)]
struct Acpi {
    /// SLP_TYPa and SLP_TYPb data for \_S1_ until \_S5_
    slp_type_data: [Option<[u8; 2]>; 5],
}

impl Acpi {
    fn init() -> Acpi {
        Self::enable();

        let mut slp_type_data = [None, None, None, None, None];

        for table in tables::get_acpi_tables().rsdt.iter_tables::<tables::Xsdt>() {
            for (i, slp_data) in slp_type_data.iter_mut().enumerate() {
                if slp_data.is_some() {
                    continue;
                }

                if let Some(result) = fetch_s_array(&table.aml, &format!("\\_S{}_", i + 1)) {
                    *slp_data = Some(result);
                }
            }
        }

        info!("SLP_TYPa and SLP_TYPb data: {:?}", slp_type_data);

        Acpi { slp_type_data }
    }

    fn sleep(&self, ty: u8) -> Result<(), AcpiError> {
        if ty == 0 || ty > 5 {
            return Err(AcpiError::InvalidSleepType);
        }

        if let Some(slp_data) = &self.slp_type_data[ty as usize - 1] {
            let facp = tables::get_acpi_tables()
                .rsdt
                .get_table::<tables::Facp>()
                .expect("No Facp");

            let mut ctrl_a = facp.read_pm1_control_a();
            let ctrl_b = facp.read_pm1_control_b();

            ctrl_a = (ctrl_a & !facp::flags::PM_CTRL_SLP_TYP_MASK)
                | ((slp_data[0] as u16) << facp::flags::PM_CTRL_SLP_TYP_SHIFT);

            info!("Entering Sleep mode {ty}!");

            if let Some(mut ctrl_b) = ctrl_b {
                ctrl_b = (ctrl_b & !facp::flags::PM_CTRL_SLP_TYP_MASK)
                    | ((slp_data[1] as u16) << facp::flags::PM_CTRL_SLP_TYP_SHIFT);
                facp.write_pm1_control_b(ctrl_b);
            }

            facp.write_pm1_control_a(ctrl_a | facp::flags::PM_CTRL_SLP_EN);

            // poll WAK_STS until woken
            while facp.read_pm1_status() & facp::flags::PM_STS_WAK == 0 {
                core::hint::spin_loop();
            }

            Ok(())
        } else {
            Err(AcpiError::SleepTypeNotAvailable)
        }
    }

    fn enable() {
        let facp = tables::get_acpi_tables()
            .rsdt
            .get_table::<tables::Facp>()
            .expect("No Facp");

        if facp.is_acpi_enabled() {
            warn!("ACPI already enabled");
            assert!(apic::is_irq_assigned(facp.sci_interrupt()));

            return;
        }

        // disable the events first
        facp.write_pm1_enable(0);

        apic::assign_io_irq(
            acpi_handler as BasicInterruptHandler,
            facp.sci_interrupt(),
            cpu::cpu(),
        );

        facp.enable_acpi();

        let mut i = 0;
        while !facp.is_acpi_enabled() && i < 10000 {
            i += 1;
            core::hint::spin_loop();
        }

        if !facp.is_acpi_enabled() {
            panic!("Failed to enable ACPI");
        }

        // enable all events except timer
        facp.write_pm1_enable(
            facp::flags::PM_EN_GBL
                | facp::flags::PM_EN_PWRBTN
                | facp::flags::PM_EN_SLPBTN
                | facp::flags::PM_EN_RTC,
        );
    }
}

/// Halper function
fn fetch_s_array(aml: &Aml, name: &str) -> Option<[u8; 2]> {
    let mut ctx = ExecutionContext::default();
    match aml.execute(&mut ctx, name, &[]) {
        Ok(obj) => {
            let Some(package) = obj.as_package() else {
                error!("{} is not a package", name);
                return None;
            };

            if package.size() < 2 {
                error!("{} package is ony {} in size", name, package.size());
            }

            let mut slp_data = [0; 2];

            for (i, item) in package
                .iter()
                .take(2)
                .map(|obj| {
                    let Some(data) = obj.as_data() else {
                        error!("{:?} is not a data", obj);
                        return None;
                    };

                    let Some(int) = data.as_integer() else {
                        error!("{:?} is not an integer", data);
                        return None;
                    };

                    if let Some(mut int) = int.as_u8() {
                        if int & !7 != 0 {
                            warn!(
                                "Data {:02X} of {name:?} is more than 3 bits, truncating...",
                                int
                            );
                            int &= 7;
                        }
                        Some(int)
                    } else {
                        error!("{:?} is not a u8", int);
                        None
                    }
                })
                .enumerate()
            {
                if let Some(item) = item {
                    slp_data[i] = item;
                } else {
                    return None;
                }
            }

            Some(slp_data)
        }
        Err(AmlExecutionError::LableNotFound(_)) => None,
        Err(e) => {
            error!("Failed to execute AML for {name}: {:?}", e);
            None
        }
    }
}

extern "x86-interrupt" fn acpi_handler(_frame: InterruptStackFrame64) {
    let facp = tables::get_acpi_tables()
        .rsdt
        .get_table::<tables::Facp>()
        .expect("No Facp");

    let pm1_event = facp.read_pm1_status();

    if pm1_event & facp::flags::PM_EN_GBL != 0 {
        facp.write_pm1_status(facp::flags::PM_EN_GBL);
        warn!("Global ACPI event: {:X}", pm1_event);
    } else if pm1_event & facp::flags::PM_EN_SLPBTN != 0 {
        facp.write_pm1_status(facp::flags::PM_EN_SLPBTN);
        warn!("Sleep button ACPI event: {:X}", pm1_event);
    } else if pm1_event & facp::flags::PM_EN_RTC != 0 {
        facp.write_pm1_status(facp::flags::PM_EN_RTC);
        warn!("RTC ACPI event: {:X}", pm1_event);
    } else if pm1_event & facp::flags::PM_EN_PWRBTN != 0 {
        facp.write_pm1_status(facp::flags::PM_EN_PWRBTN);
        warn!("Power button ACPI event: {:X}", pm1_event);

        // TODO: handle shutdown setup
        power::start_power_sequence(power::PowerCommand::Shutdown);
    } else if pm1_event & facp::flags::PM_EN_TMR != 0 {
        facp.write_pm1_status(facp::flags::PM_EN_TMR);
        warn!("Timer ACPI event: {:X}", pm1_event);
    } else {
        warn!("Unknown ACPI event: {:X}", pm1_event);
    }

    apic::return_from_interrupt();
}