-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
acpi_handler.rs
181 lines (153 loc) · 7.22 KB
/
acpi_handler.rs
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
use crate::kernel_map;
use acpi::{AcpiHandler, PhysicalMapping};
use bit_field::BitField;
use core::ptr::NonNull;
use hal::memory::PAddr;
use hal_x86_64::hw::port::Port;
use mulch::math::align_down;
use pci_types::{ConfigRegionAccess, PciAddress};
use tracing::debug;
#[derive(Clone)]
pub struct PoplarAcpiHandler;
impl AcpiHandler for PoplarAcpiHandler {
unsafe fn map_physical_region<T>(&self, physical_address: usize, size: usize) -> PhysicalMapping<Self, T> {
let virtual_address = kernel_map::physical_to_virtual(PAddr::new(physical_address).unwrap());
PhysicalMapping::new(
usize::from(physical_address),
NonNull::new(virtual_address.mut_ptr()).unwrap(),
size,
size,
PoplarAcpiHandler,
)
}
fn unmap_physical_region<T>(_region: &PhysicalMapping<Self, T>) {}
}
pub struct AmlHandler<A>
where
A: ConfigRegionAccess,
{
pci_access: A,
}
impl<A> AmlHandler<A>
where
A: ConfigRegionAccess,
{
pub fn new(pci_access: A) -> AmlHandler<A> {
AmlHandler { pci_access }
}
}
impl<A> aml::Handler for AmlHandler<A>
where
A: ConfigRegionAccess + Send + Sync,
{
fn read_u8(&self, address: usize) -> u8 {
debug!("AML: Reading byte from {:#x}", address);
let address = hal_x86_64::kernel_map::physical_to_virtual(PAddr::new(address).unwrap());
assert!(address.is_aligned(1));
unsafe { core::ptr::read_volatile(address.ptr()) }
}
fn read_u16(&self, address: usize) -> u16 {
debug!("AML: Reading word from {:#x}", address);
let address = hal_x86_64::kernel_map::physical_to_virtual(PAddr::new(address).unwrap());
assert!(address.is_aligned(2));
unsafe { core::ptr::read_volatile(address.ptr()) }
}
fn read_u32(&self, address: usize) -> u32 {
debug!("AML: Reading dword from {:#x}", address);
let address = hal_x86_64::kernel_map::physical_to_virtual(PAddr::new(address).unwrap());
assert!(address.is_aligned(4));
unsafe { core::ptr::read_volatile(address.ptr()) }
}
fn read_u64(&self, address: usize) -> u64 {
debug!("AML: Reading qword from {:#x}", address);
let address = hal_x86_64::kernel_map::physical_to_virtual(PAddr::new(address).unwrap());
assert!(address.is_aligned(8));
unsafe { core::ptr::read_volatile(address.ptr()) }
}
fn write_u8(&mut self, address: usize, value: u8) {
debug!("AML: Writing byte to {:#x}: {:#x}", address, value);
let address = hal_x86_64::kernel_map::physical_to_virtual(PAddr::new(address).unwrap());
assert!(address.is_aligned(1));
unsafe { core::ptr::write_volatile(address.mut_ptr(), value) }
}
fn write_u16(&mut self, address: usize, value: u16) {
debug!("AML: Writing word to {:#x}: {:#x}", address, value);
let address = hal_x86_64::kernel_map::physical_to_virtual(PAddr::new(address).unwrap());
assert!(address.is_aligned(2));
unsafe { core::ptr::write_volatile(address.mut_ptr(), value) }
}
fn write_u32(&mut self, address: usize, value: u32) {
debug!("AML: Writing dword to {:#x}: {:#x}", address, value);
let address = hal_x86_64::kernel_map::physical_to_virtual(PAddr::new(address).unwrap());
assert!(address.is_aligned(4));
unsafe { core::ptr::write_volatile(address.mut_ptr(), value) }
}
fn write_u64(&mut self, address: usize, value: u64) {
debug!("AML: Writing qword to {:#x}: {:#x}", address, value);
let address = hal_x86_64::kernel_map::physical_to_virtual(PAddr::new(address).unwrap());
assert!(address.is_aligned(8));
unsafe { core::ptr::write_volatile(address.mut_ptr(), value) }
}
fn read_io_u8(&self, port: u16) -> u8 {
debug!("AML: Reading IO byte from port {:#x}", port);
unsafe { Port::new(port).read() }
}
fn read_io_u16(&self, port: u16) -> u16 {
debug!("AML: Reading IO word from port {:#x}", port);
unsafe { Port::new(port).read() }
}
fn read_io_u32(&self, port: u16) -> u32 {
debug!("AML: Reading IO dword from port {:#x}", port);
unsafe { Port::new(port).read() }
}
fn write_io_u8(&self, port: u16, value: u8) {
debug!("AML: Writing IO byte to port {:#x}: {:#x}", port, value);
unsafe { Port::new(port).write(value) }
}
fn write_io_u16(&self, port: u16, value: u16) {
debug!("AML: Writing IO word to port {:#x}: {:#x}", port, value);
unsafe { Port::new(port).write(value) }
}
fn write_io_u32(&self, port: u16, value: u32) {
debug!("AML: Writing IO dword to port {:#x}: {:#x}", port, value);
unsafe { Port::new(port).write(value) }
}
fn read_pci_u8(&self, segment: u16, bus: u8, device: u8, function: u8, offset: u16) -> u8 {
debug!("AML: Reading byte from PCI config space (segment={:#x},bus={:#x},device={:#x},function={:#x},offset={:#x})", segment, bus, device, function, offset);
let dword_read = unsafe {
self.pci_access.read(PciAddress::new(segment, bus, device, function), align_down(offset, 0x20))
};
let start_bit = (offset % 0x20) as usize;
dword_read.get_bits(start_bit..(start_bit + 8)) as u8
}
fn read_pci_u16(&self, segment: u16, bus: u8, device: u8, function: u8, offset: u16) -> u16 {
debug!("AML: Reading word from PCI config space (segment={:#x},bus={:#x},device={:#x},function={:#x},offset={:#x})", segment, bus, device, function, offset);
let dword_read = unsafe {
self.pci_access.read(PciAddress::new(segment, bus, device, function), align_down(offset, 0x20))
};
let start_bit = (offset % 0x20) as usize;
dword_read.get_bits(start_bit..(start_bit + 16)) as u16
}
fn read_pci_u32(&self, segment: u16, bus: u8, device: u8, function: u8, offset: u16) -> u32 {
debug!("AML: Reading dword from PCI config space (segment={:#x},bus={:#x},device={:#x},function={:#x},offset={:#x})", segment, bus, device, function, offset);
unsafe { self.pci_access.read(PciAddress::new(segment, bus, device, function), offset) }
}
fn write_pci_u8(&self, segment: u16, bus: u8, device: u8, function: u8, offset: u16, value: u8) {
debug!("AML: Writing byte to PCI config space (segment={:#x},bus={:#x},device={:#x},function={:#x},offset={:#x}): {:#x}", segment, bus, device, function, offset, value);
unimplemented!()
}
fn write_pci_u16(&self, segment: u16, bus: u8, device: u8, function: u8, offset: u16, value: u16) {
debug!("AML: Writing word to PCI config space (segment={:#x},bus={:#x},device={:#x},function={:#x},offset={:#x}): {:#x}", segment, bus, device, function, offset, value);
unimplemented!()
}
fn write_pci_u32(&self, segment: u16, bus: u8, device: u8, function: u8, offset: u16, value: u32) {
debug!("AML: Writing dword to PCI config space (segment={:#x},bus={:#x},device={:#x},function={:#x},offset={:#x}): {:#x}", segment, bus, device, function, offset, value);
unsafe { self.pci_access.write(PciAddress::new(segment, bus, device, function), offset, value) }
}
fn stall(&self, _microseconds: u64) {
todo!()
}
fn sleep(&self, _milliseconds: u64) {
todo!()
}
}