xref: /DragonOS/kernel/src/driver/pci/pci.rs (revision 17041e0e307eaf9e8d8ddbddfa186cd1f10f1bc0)
1 use crate::include::bindings::bindings::{
2     initial_mm, mm_map, mm_struct, pci_read_config, pci_write_config, VM_DONTCOPY, VM_IO,
3 };
4 use crate::mm::mmio_buddy::MMIO_POOL;
5 use crate::{kdebug, kerror, kwarn};
6 use bitflags::bitflags;
7 use core::{
8     convert::TryFrom,
9     fmt::{self, Display, Formatter},
10     ptr::NonNull,
11 };
12 //Bar0寄存器的offset
13 const BAR0_OFFSET: u8 = 0x10;
14 //Status、Command寄存器的offset
15 const STATUS_COMMAND_OFFSET: u8 = 0x04;
16 /// ID for vendor-specific PCI capabilities.(Virtio Capabilities)
17 pub const PCI_CAP_ID_VNDR: u8 = 0x09;
18 
19 bitflags! {
20     /// The status register in PCI configuration space.
21     pub struct Status: u16 {
22         // Bits 0-2 are reserved.
23         /// The state of the device's INTx# signal.
24         const INTERRUPT_STATUS = 1 << 3;
25         /// The device has a linked list of capabilities.
26         const CAPABILITIES_LIST = 1 << 4;
27         /// The device is capabile of running at 66 MHz rather than 33 MHz.
28         const MHZ_66_CAPABLE = 1 << 5;
29         // Bit 6 is reserved.
30         /// The device can accept fast back-to-back transactions not from the same agent.
31         const FAST_BACK_TO_BACK_CAPABLE = 1 << 7;
32         /// The bus agent observed a parity error (if parity error handling is enabled).
33         const MASTER_DATA_PARITY_ERROR = 1 << 8;
34         // Bits 9-10 are DEVSEL timing.
35         /// A target device terminated a transaction with target-abort.
36         const SIGNALED_TARGET_ABORT = 1 << 11;
37         /// A master device transaction was terminated with target-abort.
38         const RECEIVED_TARGET_ABORT = 1 << 12;
39         /// A master device transaction was terminated with master-abort.
40         const RECEIVED_MASTER_ABORT = 1 << 13;
41         /// A device asserts SERR#.
42         const SIGNALED_SYSTEM_ERROR = 1 << 14;
43         /// The device detects a parity error, even if parity error handling is disabled.
44         const DETECTED_PARITY_ERROR = 1 << 15;
45     }
46 }
47 
48 bitflags! {
49     /// The command register in PCI configuration space.
50     pub struct Command: u16 {
51         /// The device can respond to I/O Space accesses.
52         const IO_SPACE = 1 << 0;
53         /// The device can respond to Memory Space accesses.
54         const MEMORY_SPACE = 1 << 1;
55         /// The device can behave as a bus master.
56         const BUS_MASTER = 1 << 2;
57         /// The device can monitor Special Cycle operations.
58         const SPECIAL_CYCLES = 1 << 3;
59         /// The device can generate the Memory Write and Invalidate command.
60         const MEMORY_WRITE_AND_INVALIDATE_ENABLE = 1 << 4;
61         /// The device will snoop palette register data.
62         const VGA_PALETTE_SNOOP = 1 << 5;
63         /// The device should take its normal action when a parity error is detected.
64         const PARITY_ERROR_RESPONSE = 1 << 6;
65         // Bit 7 is reserved.
66         /// The SERR# driver is enabled.
67         const SERR_ENABLE = 1 << 8;
68         /// The device is allowed to generate fast back-to-back transactions.
69         const FAST_BACK_TO_BACK_ENABLE = 1 << 9;
70         /// Assertion of the device's INTx# signal is disabled.
71         const INTERRUPT_DISABLE = 1 << 10;
72     }
73 }
74 
75 /// Gets the capabilities 'pointer' for the device function, if any.
76 ///@brief 获取第一个capability 的offset
77 ///@param device_function PCI设备的唯一标识
78 ///@return Option<u8> offset
79 pub fn capabilities_offset(device_function: DeviceFunction) -> Option<u8> {
80     let status: Status = unsafe {
81         let temp = pci_read_config(
82             device_function.bus,
83             device_function.device,
84             device_function.function,
85             STATUS_COMMAND_OFFSET,
86         );
87         Status::from_bits_truncate((temp >> 16) as u16)
88     };
89     if status.contains(Status::CAPABILITIES_LIST) {
90         let cap_pointer = unsafe {
91             let temp = pci_read_config(
92                 device_function.bus,
93                 device_function.device,
94                 device_function.function,
95                 0x34,
96             );
97             temp as u8 & 0xFC
98         };
99         Some(cap_pointer)
100     } else {
101         None
102     }
103 }
104 /// An identifier for a PCI bus, device and function.
105 /// PCI设备的唯一标识
106 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
107 pub struct DeviceFunction {
108     /// The PCI bus number, between 0 and 255.
109     pub bus: u8,
110     /// The device number on the bus, between 0 and 31.
111     pub device: u8,
112     /// The function number of the device, between 0 and 7.
113     pub function: u8,
114 }
115 ///PCI的Error
116 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
117 pub enum PciError {
118     /// The device reported an invalid BAR type.
119     InvalidBarType,
120     CreateMmioError,
121 }
122 ///实现PciError的Display trait,使其可以直接输出
123 impl Display for PciError {
124     fn fmt(&self, f: &mut Formatter) -> fmt::Result {
125         match self {
126             Self::InvalidBarType => write!(f, "Invalid PCI BAR type."),
127             Self::CreateMmioError => write!(f, "Error occurred while creating mmio"),
128         }
129     }
130 }
131 
132 impl DeviceFunction {
133     /// Returns whether the device and function numbers are valid, i.e. the device is between 0 and
134     /// 31, and the function is between 0 and 7.
135     ///@brief 检测DeviceFunction实例是否有效
136     ///@param self
137     ///@return bool 是否有效
138     pub fn valid(&self) -> bool {
139         self.device < 32 && self.function < 8
140     }
141 }
142 ///实现DeviceFunction的Display trait,使其可以直接输出
143 impl Display for DeviceFunction {
144     fn fmt(&self, f: &mut Formatter) -> fmt::Result {
145         write!(f, "{:02x}:{:02x}.{}", self.bus, self.device, self.function)
146     }
147 }
148 /// The location allowed for a memory BAR.
149 /// memory BAR的三种情况
150 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
151 pub enum MemoryBarType {
152     /// The BAR has a 32-bit address and can be mapped anywhere in 32-bit address space.
153     Width32,
154     /// The BAR must be mapped below 1MiB.
155     Below1MiB,
156     /// The BAR has a 64-bit address and can be mapped anywhere in 64-bit address space.
157     Width64,
158 }
159 ///实现MemoryBarType与u8的类型转换
160 impl From<MemoryBarType> for u8 {
161     fn from(bar_type: MemoryBarType) -> Self {
162         match bar_type {
163             MemoryBarType::Width32 => 0,
164             MemoryBarType::Below1MiB => 1,
165             MemoryBarType::Width64 => 2,
166         }
167     }
168 }
169 ///实现MemoryBarType与u8的类型转换
170 impl TryFrom<u8> for MemoryBarType {
171     type Error = PciError;
172     fn try_from(value: u8) -> Result<Self, Self::Error> {
173         match value {
174             0 => Ok(Self::Width32),
175             1 => Ok(Self::Below1MiB),
176             2 => Ok(Self::Width64),
177             _ => Err(PciError::InvalidBarType),
178         }
179     }
180 }
181 
182 /// Information about a PCI Base Address Register.
183 /// BAR的三种类型 Memory/IO/Unused
184 #[derive(Clone, Debug, Eq, PartialEq)]
185 pub enum BarInfo {
186     /// The BAR is for a memory region.
187     Memory {
188         /// The size of the BAR address and where it can be located.
189         address_type: MemoryBarType,
190         /// If true, then reading from the region doesn't have side effects. The CPU may cache reads
191         /// and merge repeated stores.
192         prefetchable: bool,
193         /// The memory address, always 16-byte aligned.
194         address: u64,
195         /// The size of the BAR in bytes.
196         size: u32,
197         /// The virtaddress for a memory bar(mapped).
198         virtaddress: u64,
199     },
200     /// The BAR is for an I/O region.
201     IO {
202         /// The I/O address, always 4-byte aligned.
203         address: u32,
204         /// The size of the BAR in bytes.
205         size: u32,
206     },
207     Unused,
208 }
209 
210 impl BarInfo {
211     /// Returns the address and size of this BAR if it is a memory bar, or `None` if it is an IO
212     /// BAR.
213     ///@brief 得到某个bar的memory_address与size(前提是他的类型为Memory Bar)
214     ///@param self
215     ///@return Option<(u64, u32) 是Memory Bar返回内存地址与大小,不是则返回None
216     pub fn memory_address_size(&self) -> Option<(u64, u32)> {
217         if let Self::Memory { address, size, .. } = self {
218             Some((*address, *size))
219         } else {
220             None
221         }
222     }
223     ///@brief 得到某个bar的virtaddress(前提是他的类型为Memory Bar)
224     ///@param self
225     ///@return Option<(u64) 是Memory Bar返回映射的虚拟地址,不是则返回None
226     pub fn virtual_address(&self) -> Option<u64> {
227         if let Self::Memory { virtaddress, .. } = self {
228             Some(*virtaddress)
229         } else {
230             None
231         }
232     }
233 }
234 ///实现BarInfo的Display trait,使其可以直接输出
235 impl Display for BarInfo {
236     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
237         match self {
238             Self::Memory {
239                 address_type,
240                 prefetchable,
241                 address,
242                 size,
243                 virtaddress,
244             } => write!(
245                 f,
246                 "Memory space at {:#010x}, size {}, type {:?}, prefetchable {},mapped at {:#x}",
247                 address, size, address_type, prefetchable, virtaddress
248             ),
249             Self::IO { address, size } => {
250                 write!(f, "I/O space at {:#010x}, size {}", address, size)
251             }
252             Self::Unused => {
253                 write!(f, "Unused bar")
254             }
255         }
256     }
257 }
258 ///一个PCI设备有6个BAR寄存器,PciDeviceBar存储其全部信息
259 #[derive(Clone, Debug, Eq, PartialEq)]
260 pub struct PciDeviceBar {
261     bar0: BarInfo,
262     bar1: BarInfo,
263     bar2: BarInfo,
264     bar3: BarInfo,
265     bar4: BarInfo,
266     bar5: BarInfo,
267 }
268 
269 impl PciDeviceBar {
270     ///@brief 得到某个bar的barinfo
271     ///@param self ,bar_index(0-5)
272     ///@return Result<&BarInfo, PciError> bar_index在0-5则返回对应的bar_info结构体,超出范围则返回错误
273     pub fn get_bar(&self, bar_index: u8) -> Result<&BarInfo, PciError> {
274         match bar_index {
275             0 => Ok(&self.bar0),
276             1 => Ok(&self.bar1),
277             2 => Ok(&self.bar2),
278             3 => Ok(&self.bar3),
279             4 => Ok(&self.bar4),
280             _ => Err(PciError::InvalidBarType),
281         }
282     }
283 }
284 ///实现PciDeviceBar的Display trait,使其可以直接输出
285 impl Display for PciDeviceBar {
286     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
287         write!(
288             f,
289             "\r\nBar0:{}\r\n Bar1:{}\r\n Bar2:{}\r\n Bar3:{}\r\nBar4:{}\r\nBar5:{}",
290             self.bar0, self.bar1, self.bar2, self.bar3, self.bar4, self.bar5
291         )
292     }
293 }
294 ///实现PciDeviceBar的Default trait,使其可以简单初始化
295 impl Default for PciDeviceBar {
296     fn default() -> Self {
297         PciDeviceBar {
298             bar0: BarInfo::Unused,
299             bar1: BarInfo::Unused,
300             bar2: BarInfo::Unused,
301             bar3: BarInfo::Unused,
302             bar4: BarInfo::Unused,
303             bar5: BarInfo::Unused,
304         }
305     }
306 }
307 
308 ///@brief 将某个pci设备的bar全部初始化,memory
309 ///@param self ,device_function PCI设备的唯一标识符
310 ///@return Result<PciDeviceBar, PciError> 成功则返回对应的PciDeviceBar结构体,失败则返回错误类型
311 pub fn pci_bar_init(device_function: DeviceFunction) -> Result<PciDeviceBar, PciError> {
312     let mut device_bar: PciDeviceBar = PciDeviceBar::default();
313     let mut bar_index_ignore: u8 = 255;
314     for bar_index in 0..6 {
315         if bar_index == bar_index_ignore {
316             continue;
317         }
318         let bar_info;
319         let mut virtaddress: u64 = 0;
320         let bar_orig = unsafe {
321             let bar_temp = pci_read_config(
322                 device_function.bus,
323                 device_function.device,
324                 device_function.function,
325                 BAR0_OFFSET + 4 * bar_index,
326             );
327             bar_temp
328         };
329         unsafe {
330             pci_write_config(
331                 device_function.bus,
332                 device_function.device,
333                 device_function.function,
334                 BAR0_OFFSET + 4 * bar_index,
335                 0xffffffff,
336             );
337         }
338         let size_mask = unsafe {
339             let bar_temp = pci_read_config(
340                 device_function.bus,
341                 device_function.device,
342                 device_function.function,
343                 BAR0_OFFSET + 4 * bar_index,
344             );
345             bar_temp
346         };
347         // A wrapping add is necessary to correctly handle the case of unused BARs, which read back
348         // as 0, and should be treated as size 0.
349         let size = (!(size_mask & 0xfffffff0)).wrapping_add(1);
350         //kdebug!("bar_orig:{:#x},size: {:#x}", bar_orig,size);
351         // Restore the original value.
352         unsafe {
353             pci_write_config(
354                 device_function.bus,
355                 device_function.device,
356                 device_function.function,
357                 BAR0_OFFSET + 4 * bar_index,
358                 bar_orig,
359             );
360         }
361         if size == 0 {
362             continue;
363         }
364         if bar_orig & 0x00000001 == 0x00000001 {
365             // I/O space
366             let address = bar_orig & 0xfffffffc;
367             bar_info = BarInfo::IO { address, size };
368         } else {
369             // Memory space
370             let mut address = u64::from(bar_orig & 0xfffffff0);
371             let prefetchable = bar_orig & 0x00000008 != 0;
372             let address_type = MemoryBarType::try_from(((bar_orig & 0x00000006) >> 1) as u8)?;
373             if address_type == MemoryBarType::Width64 {
374                 if bar_index >= 5 {
375                     return Err(PciError::InvalidBarType);
376                 }
377                 let address_top = unsafe {
378                     let bar_temp = pci_read_config(
379                         device_function.bus,
380                         device_function.device,
381                         device_function.function,
382                         BAR0_OFFSET + 4 * (bar_index + 1),
383                     );
384                     bar_temp
385                 };
386                 address |= u64::from(address_top) << 32;
387                 bar_index_ignore = bar_index + 1; //下个bar跳过,因为64位的memory bar覆盖了两个bar
388             }
389             //kdebug!("address={:#x},size={:#x}",address,size);
390             unsafe {
391                 let vaddr_ptr = &mut virtaddress as *mut u64;
392                 let mut virtsize: u64 = 0;
393                 let virtsize_ptr = &mut virtsize as *mut u64;
394                 let initial_mm_ptr = &mut initial_mm as *mut mm_struct;
395                 //kdebug!("size want={:#x}", size);
396                 if let Err(_) = MMIO_POOL.create_mmio(
397                     size,
398                     (VM_IO | VM_DONTCOPY) as u64,
399                     vaddr_ptr,
400                     virtsize_ptr,
401                 ) {
402                     kerror!("Create mmio failed when initing pci bar");
403                     return Err(PciError::CreateMmioError);
404                 };
405                 //kdebug!("virtaddress={:#x},virtsize={:#x}",virtaddress,virtsize);
406                 mm_map(initial_mm_ptr, virtaddress, size as u64, address);
407             }
408             bar_info = BarInfo::Memory {
409                 address_type,
410                 prefetchable,
411                 address,
412                 size,
413                 virtaddress,
414             };
415         }
416         match bar_index {
417             0 => {
418                 device_bar.bar0 = bar_info;
419             }
420             1 => {
421                 device_bar.bar1 = bar_info;
422             }
423             2 => {
424                 device_bar.bar2 = bar_info;
425             }
426             3 => {
427                 device_bar.bar3 = bar_info;
428             }
429             4 => {
430                 device_bar.bar4 = bar_info;
431             }
432             5 => {
433                 device_bar.bar5 = bar_info;
434             }
435             _ => {}
436         }
437     }
438     kdebug!("pci_device_bar:{}", device_bar);
439     return Ok(device_bar);
440 }
441 
442 /// Information about a PCI device capability.
443 /// PCI设备的capability的信息
444 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
445 pub struct CapabilityInfo {
446     /// The offset of the capability in the PCI configuration space of the device function.
447     pub offset: u8,
448     /// The ID of the capability.
449     pub id: u8,
450     /// The third and fourth bytes of the capability, to save reading them again.
451     pub private_header: u16,
452 }
453 /// Iterator over capabilities for a device.
454 /// 创建迭代器以遍历PCI设备的capability
455 #[derive(Debug)]
456 pub struct CapabilityIterator {
457     pub device_function: DeviceFunction,
458     pub next_capability_offset: Option<u8>,
459 }
460 
461 impl Iterator for CapabilityIterator {
462     type Item = CapabilityInfo;
463     fn next(&mut self) -> Option<Self::Item> {
464         let offset = self.next_capability_offset?;
465 
466         // Read the first 4 bytes of the capability.
467         let capability_header = unsafe {
468             let temp = pci_read_config(
469                 self.device_function.bus,
470                 self.device_function.device,
471                 self.device_function.function,
472                 offset,
473             );
474             temp
475         };
476         let id = capability_header as u8;
477         let next_offset = (capability_header >> 8) as u8;
478         let private_header = (capability_header >> 16) as u16;
479 
480         self.next_capability_offset = if next_offset == 0 {
481             None
482         } else if next_offset < 64 || next_offset & 0x3 != 0 {
483             kwarn!("Invalid next capability offset {:#04x}", next_offset);
484             None
485         } else {
486             Some(next_offset)
487         };
488 
489         Some(CapabilityInfo {
490             offset,
491             id,
492             private_header,
493         })
494     }
495 }
496