-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathpci_routing.rs
186 lines (173 loc) · 8.6 KB
/
pci_routing.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
182
183
184
185
186
use crate::{
namespace::AmlName,
resource::{self, InterruptPolarity, InterruptTrigger, Irq, Resource},
value::Args,
AmlContext,
AmlError,
AmlType,
AmlValue,
};
use alloc::vec::Vec;
use bit_field::BitField;
use core::str::FromStr;
pub use crate::resource::IrqDescriptor;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Pin {
IntA,
IntB,
IntC,
IntD,
}
#[derive(Debug)]
pub enum PciRouteType {
/// The interrupt is hard-coded to a specific GSI
Gsi(u32),
/// The interrupt is linked to a link object. This object will have `_PRS`, `_CRS` fields and a `_SRS` method
/// that can be used to allocate the interrupt. Note that some platforms (e.g. QEMU's q35 chipset) use link
/// objects but do not support changing the interrupt that it's linked to (i.e. `_SRS` doesn't do anything).
/*
* The actual object itself will just be a `Device`, and we need paths to its children objects to do
* anything useful, so we just store the resolved name here.
*/
LinkObject(AmlName),
}
#[derive(Debug)]
pub struct PciRoute {
device: u16,
function: u16,
pin: Pin,
route_type: PciRouteType,
}
/// A `PciRoutingTable` is used to interpret the data in a `_PRT` object, which provides a mapping
/// from PCI interrupt pins to the inputs of the interrupt controller. One of these objects must be
/// present under each PCI root bridge, and consists of a package of packages, each of which describes the
/// mapping of a single PCI interrupt pin.
#[derive(Debug)]
pub struct PciRoutingTable {
entries: Vec<PciRoute>,
}
impl PciRoutingTable {
/// Construct a `PciRoutingTable` from a path to a `_PRT` object. Returns
/// `AmlError::IncompatibleValueConversion` if the value passed is not a package, or if any of the values
/// within it are not packages. Returns the various `AmlError::Prt*` errors if the internal structure of the
/// entries is invalid.
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 {
/*
* Each inner package has the following structure:
* | Field | Type | Description |
* | -----------|-----------|-----------------------------------------------------------|
* | Address | Dword | Address of the device. Same format as _ADR objects (high |
* | | | word = #device, low word = #function) |
* | -----------|-----------|-----------------------------------------------------------|
* | Pin | Byte | The PCI pin (0 = INTA, 1 = INTB, 2 = INTC, 3 = INTD) |
* | -----------|-----------|-----------------------------------------------------------|
* | Source | Byte or | Name of the device that allocates the interrupt to which |
* | | NamePath | the above pin is connected. Can be fully qualified, |
* | | | relative, or a simple NameSeg that utilizes namespace |
* | | | search rules. Instead, if this is a byte value of 0, the |
* | | | interrupt is allocated out of the GSI pool, and Source |
* | | | Index should be utilised. |
* | -----------|-----------|-----------------------------------------------------------|
* | Source | Dword | Index that indicates which resource descriptor in the |
* | Index | | resource template of the device pointed to in the Source |
* | | | field this interrupt is allocated from. If the Source |
* | | | is zero, then this field is the GSI number to which the |
* | | | pin is connected. |
* | -----------|-----------|-----------------------------------------------------------|
*/
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) => {
/*
* The Source Index field contains the GSI number that this interrupt is attached
* to.
*/
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 })
}
}
/// Get the interrupt input that a given PCI interrupt pin is wired to. Returns `AmlError::PrtNoEntry` if the
/// PRT doesn't contain an entry for the given address + pin.
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: Irq::Single(gsi),
}),
PciRouteType::LinkObject(ref name) => {
let path = AmlName::from_str("_CRS").unwrap().resolve(name)?;
let link_crs = context.invoke_method(&path, Args::EMPTY)?;
let resources = resource::resource_descriptor_list(&link_crs)?;
match resources.as_slice() {
[Resource::Irq(descriptor)] => Ok(descriptor.clone()),
_ => Err(AmlError::UnexpectedResourceType),
}
}
}
}
}