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
use crate::{
namespace::AmlName,
resource::{self, InterruptPolarity, InterruptTrigger, Resource},
value::Args,
AmlContext,
AmlError,
AmlType,
AmlValue,
};
use alloc::vec::Vec;
use bit_field::BitField;
use core::convert::TryInto;
pub use crate::resource::IrqDescriptor;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Pin {
IntA,
IntB,
IntC,
IntD,
}
#[derive(Debug)]
pub enum PciRouteType {
Gsi(u32),
LinkObject(AmlName),
}
#[derive(Debug)]
pub struct PciRoute {
device: u16,
function: u16,
pin: Pin,
route_type: PciRouteType,
}
#[derive(Debug)]
pub struct PciRoutingTable {
entries: Vec<PciRoute>,
}
impl PciRoutingTable {
pub fn from_prt_path(prt_path: &AmlName, context: &mut AmlContext) -> Result<PciRoutingTable, AmlError> {
let mut entries = Vec::new();
let prt = context.invoke_method(&prt_path, Args::default())?;
if let AmlValue::Package(ref inner_values) = prt {
for value in inner_values {
if let AmlValue::Package(ref pin_package) = value {
let address = pin_package[0].as_integer(context)?;
let device = address.get_bits(16..32).try_into().map_err(|_| AmlError::PrtInvalidAddress)?;
let function = address.get_bits(0..16).try_into().map_err(|_| AmlError::PrtInvalidAddress)?;
let pin = match pin_package[1].as_integer(context)? {
0 => Pin::IntA,
1 => Pin::IntB,
2 => Pin::IntC,
3 => Pin::IntD,
_ => return Err(AmlError::PrtInvalidPin),
};
match pin_package[2] {
AmlValue::Integer(0) => {
entries.push(PciRoute {
device,
function,
pin,
route_type: PciRouteType::Gsi(
pin_package[3]
.as_integer(context)?
.try_into()
.map_err(|_| AmlError::PrtInvalidGsi)?,
),
});
}
AmlValue::String(ref name) => {
let link_object_name =
context.namespace.search_for_level(&AmlName::from_str(name)?, &prt_path)?;
entries.push(PciRoute {
device,
function,
pin,
route_type: PciRouteType::LinkObject(link_object_name),
});
}
_ => return Err(AmlError::PrtInvalidSource),
}
} else {
return Err(AmlError::IncompatibleValueConversion {
current: value.type_of(),
target: AmlType::Package,
});
}
}
Ok(PciRoutingTable { entries })
} else {
Err(AmlError::IncompatibleValueConversion { current: prt.type_of(), target: AmlType::Package })
}
}
pub fn route(
&self,
device: u16,
function: u16,
pin: Pin,
context: &mut AmlContext,
) -> Result<IrqDescriptor, AmlError> {
let entry = self
.entries
.iter()
.find(|entry| {
entry.device == device
&& (entry.function == 0xffff || entry.function == function)
&& entry.pin == pin
})
.ok_or(AmlError::PrtNoEntry)?;
match entry.route_type {
PciRouteType::Gsi(gsi) => Ok(IrqDescriptor {
is_consumer: true,
trigger: InterruptTrigger::Level,
polarity: InterruptPolarity::ActiveLow,
is_shared: true,
is_wake_capable: false,
irq: gsi,
}),
PciRouteType::LinkObject(ref name) => {
let link_crs =
context.namespace.get_by_path(&AmlName::from_str("_CRS").unwrap().resolve(name)?)?;
let resources = resource::resource_descriptor_list(link_crs)?;
match resources.as_slice() {
[Resource::Irq(descriptor)] => Ok(descriptor.clone()),
_ => Err(AmlError::UnexpectedResourceType),
}
}
}
}
}