178bf93f0SYJwu2023 #![allow(dead_code)] 278bf93f0SYJwu2023 // 目前仅支持单主桥单Segment 378bf93f0SYJwu2023 41f4877a4S曾俊 use super::device::pci_device_manager; 5cc36cf4aSYJwu2023 use super::pci_irq::{IrqType, PciIrqError}; 61f4877a4S曾俊 use super::raw_device::PciGeneralDevice; 7370472f7SLoGin use super::root::{pci_root_0, PciRoot}; 81f4877a4S曾俊 97ae679ddSLoGin use crate::arch::{PciArch, TraitPciArch}; 101f4877a4S曾俊 use crate::driver::pci::subsys::pci_bus_subsys_init; 11e2841179SLoGin use crate::exception::IrqNumber; 1278bf93f0SYJwu2023 use crate::libs::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}; 132dd9f0c7SLoGin 142dd9f0c7SLoGin use crate::mm::mmio_buddy::{mmio_pool, MMIOSpaceGuard}; 152dd9f0c7SLoGin 16370472f7SLoGin use crate::mm::VirtAddr; 17*2eab6dd7S曾俊 181f4877a4S曾俊 use alloc::string::String; 192dd9f0c7SLoGin use alloc::sync::Arc; 2078bf93f0SYJwu2023 use alloc::vec::Vec; 2178bf93f0SYJwu2023 use alloc::{boxed::Box, collections::LinkedList}; 2226d84a31SYJwu2023 use bitflags::bitflags; 23*2eab6dd7S曾俊 use log::{debug, error, info, warn}; 2440fe15e0SLoGin 2526d84a31SYJwu2023 use core::{ 2626d84a31SYJwu2023 convert::TryFrom, 275c1e552cSYJwu2023 fmt::{self, Debug, Display, Formatter}, 2826d84a31SYJwu2023 }; 2978bf93f0SYJwu2023 // PCI_DEVICE_LINKEDLIST 添加了读写锁的全局链表,里面存储了检索到的PCI设备结构体 3078bf93f0SYJwu2023 lazy_static! { 3178bf93f0SYJwu2023 pub static ref PCI_DEVICE_LINKEDLIST: PciDeviceLinkedList = PciDeviceLinkedList::new(); 322709e017SLoGin } 332709e017SLoGin 345c1e552cSYJwu2023 /// PCI域地址 355c1e552cSYJwu2023 #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)] 365c1e552cSYJwu2023 #[repr(transparent)] 375c1e552cSYJwu2023 pub struct PciAddr(usize); 385c1e552cSYJwu2023 395c1e552cSYJwu2023 impl PciAddr { 405c1e552cSYJwu2023 #[inline(always)] 415c1e552cSYJwu2023 pub const fn new(address: usize) -> Self { 425c1e552cSYJwu2023 Self(address) 435c1e552cSYJwu2023 } 445c1e552cSYJwu2023 455c1e552cSYJwu2023 /// @brief 获取PCI域地址的值 465c1e552cSYJwu2023 #[inline(always)] 475c1e552cSYJwu2023 pub fn data(&self) -> usize { 485c1e552cSYJwu2023 self.0 495c1e552cSYJwu2023 } 505c1e552cSYJwu2023 515c1e552cSYJwu2023 /// @brief 将PCI域地址加上一个偏移量 525c1e552cSYJwu2023 #[inline(always)] 535c1e552cSYJwu2023 pub fn add(self, offset: usize) -> Self { 545c1e552cSYJwu2023 Self(self.0 + offset) 555c1e552cSYJwu2023 } 565c1e552cSYJwu2023 575c1e552cSYJwu2023 /// @brief 判断PCI域地址是否按照指定要求对齐 585c1e552cSYJwu2023 #[inline(always)] 595c1e552cSYJwu2023 pub fn check_aligned(&self, align: usize) -> bool { 605c1e552cSYJwu2023 return self.0 & (align - 1) == 0; 615c1e552cSYJwu2023 } 625c1e552cSYJwu2023 } 635c1e552cSYJwu2023 impl Debug for PciAddr { 645c1e552cSYJwu2023 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 655c1e552cSYJwu2023 write!(f, "PciAddr({:#x})", self.0) 665c1e552cSYJwu2023 } 675c1e552cSYJwu2023 } 6878bf93f0SYJwu2023 6978bf93f0SYJwu2023 /// 添加了读写锁的链表,存储PCI设备结构体 7078bf93f0SYJwu2023 pub struct PciDeviceLinkedList { 7178bf93f0SYJwu2023 list: RwLock<LinkedList<Box<dyn PciDeviceStructure>>>, 7278bf93f0SYJwu2023 } 7378bf93f0SYJwu2023 7478bf93f0SYJwu2023 impl PciDeviceLinkedList { 7578bf93f0SYJwu2023 /// @brief 初始化结构体 7678bf93f0SYJwu2023 fn new() -> Self { 7778bf93f0SYJwu2023 PciDeviceLinkedList { 7878bf93f0SYJwu2023 list: RwLock::new(LinkedList::new()), 7978bf93f0SYJwu2023 } 8078bf93f0SYJwu2023 } 8178bf93f0SYJwu2023 /// @brief 获取可读的linkedlist(读锁守卫) 8278bf93f0SYJwu2023 /// @return RwLockReadGuard<LinkedList<Box<dyn PciDeviceStructure>>> 读锁守卫 8313776c11Slogin pub fn read(&self) -> RwLockReadGuard<LinkedList<Box<dyn PciDeviceStructure>>> { 8478bf93f0SYJwu2023 self.list.read() 8578bf93f0SYJwu2023 } 8678bf93f0SYJwu2023 /// @brief 获取可写的linkedlist(写锁守卫) 8778bf93f0SYJwu2023 /// @return RwLockWriteGuard<LinkedList<Box<dyn PciDeviceStructure>>> 写锁守卫 8813776c11Slogin pub fn write(&self) -> RwLockWriteGuard<LinkedList<Box<dyn PciDeviceStructure>>> { 8978bf93f0SYJwu2023 self.list.write() 9078bf93f0SYJwu2023 } 9178bf93f0SYJwu2023 /// @brief 获取链表中PCI结构体数目 9278bf93f0SYJwu2023 /// @return usize 链表中PCI结构体数目 9378bf93f0SYJwu2023 pub fn num(&self) -> usize { 9478bf93f0SYJwu2023 let list = self.list.read(); 9578bf93f0SYJwu2023 list.len() 9678bf93f0SYJwu2023 } 9778bf93f0SYJwu2023 /// @brief 添加Pci设备结构体到链表中 9878bf93f0SYJwu2023 pub fn add(&self, device: Box<dyn PciDeviceStructure>) { 9978bf93f0SYJwu2023 let mut list = self.list.write(); 10078bf93f0SYJwu2023 list.push_back(device); 10178bf93f0SYJwu2023 } 10278bf93f0SYJwu2023 } 10378bf93f0SYJwu2023 104e32effb1SLoGin /// # 获取具有特定供应商ID的PCI设备结构的引用 105e32effb1SLoGin /// 106e32effb1SLoGin /// 这个函数通过供应商ID搜索PCI设备结构列表,并返回匹配该ID的所有设备结构的引用。 107e32effb1SLoGin /// 108e32effb1SLoGin /// ## 参数 109e32effb1SLoGin /// 110e32effb1SLoGin /// - list: 一个可变的PCI设备结构链表,类型为`&'a mut RwLockWriteGuard<'_, LinkedList<Box<dyn PciDeviceStructure>>>`。 111e32effb1SLoGin /// - vendor_id: 要查找的PCI供应商ID,类型为`u16`。 112e32effb1SLoGin /// 113e32effb1SLoGin /// ## 返回值 114e32effb1SLoGin /// 115e32effb1SLoGin /// - 返回匹配的供应商ID的PCI设备结构的引用。 116e32effb1SLoGin pub fn get_pci_device_structures_mut_by_vendor_id<'a>( 117e32effb1SLoGin list: &'a mut RwLockWriteGuard<'_, LinkedList<Box<dyn PciDeviceStructure>>>, 118e32effb1SLoGin vendor_id: u16, 119e32effb1SLoGin ) -> Vec<&'a mut Box<(dyn PciDeviceStructure)>> { 120e32effb1SLoGin let mut result = Vec::new(); 121e32effb1SLoGin for box_pci_device_structure in list.iter_mut() { 122e32effb1SLoGin let common_header = (*box_pci_device_structure).common_header(); 123e32effb1SLoGin if common_header.vendor_id == vendor_id { 124e32effb1SLoGin result.push(box_pci_device_structure); 125e32effb1SLoGin } 126e32effb1SLoGin } 127e32effb1SLoGin result 128e32effb1SLoGin } 129e32effb1SLoGin 130e32effb1SLoGin /// # get_pci_device_structure_mut - 在链表中寻找满足条件的PCI设备结构体并返回其可变引用 131e32effb1SLoGin /// 132e32effb1SLoGin /// 该函数遍历给定的PCI设备链表,寻找其common_header中class_code和subclass字段与给定值匹配的设备结构体。 133e32effb1SLoGin /// 对于每一个匹配的设备结构体,函数返回一个可变引用。 134e32effb1SLoGin /// 135e32effb1SLoGin /// ## 参数 136e32effb1SLoGin /// 137e32effb1SLoGin /// - list: &'a mut RwLockWriteGuard<'_, LinkedList<Box<dyn PciDeviceStructure>>> — 链表的写锁守卫,用于访问和遍历PCI设备链表。 138e32effb1SLoGin /// - class_code: u8 — PCI设备class code寄存器值,用于分类设备的功能。 139e32effb1SLoGin /// - subclass: u8 — PCI设备subclass寄存器值,与class_code一起确定设备的子类型。 140e32effb1SLoGin /// 141e32effb1SLoGin /// ## 返回值 142e32effb1SLoGin /// - 包含链表中所有满足条件的PCI结构体的可变引用的容器。 14378bf93f0SYJwu2023 pub fn get_pci_device_structure_mut<'a>( 14478bf93f0SYJwu2023 list: &'a mut RwLockWriteGuard<'_, LinkedList<Box<dyn PciDeviceStructure>>>, 14578bf93f0SYJwu2023 class_code: u8, 14678bf93f0SYJwu2023 subclass: u8, 14778bf93f0SYJwu2023 ) -> Vec<&'a mut Box<(dyn PciDeviceStructure)>> { 14878bf93f0SYJwu2023 let mut result = Vec::new(); 14978bf93f0SYJwu2023 for box_pci_device_structure in list.iter_mut() { 15078bf93f0SYJwu2023 let common_header = (*box_pci_device_structure).common_header(); 15178bf93f0SYJwu2023 if (common_header.class_code == class_code) && (common_header.subclass == subclass) { 15278bf93f0SYJwu2023 result.push(box_pci_device_structure); 15378bf93f0SYJwu2023 } 15478bf93f0SYJwu2023 } 15578bf93f0SYJwu2023 result 15678bf93f0SYJwu2023 } 157e32effb1SLoGin 158e32effb1SLoGin /// # get_pci_device_structure - 在链表中寻找满足条件的PCI设备结构体并返回其不可变引用 159e32effb1SLoGin /// 160e32effb1SLoGin /// 该函数遍历给定的PCI设备链表,寻找其common_header中class_code和subclass字段与给定值匹配的设备结构体。 161e32effb1SLoGin /// 对于每一个匹配的设备结构体,函数返回一个可变引用。 162e32effb1SLoGin /// 163e32effb1SLoGin /// ## 参数 164e32effb1SLoGin /// 165e32effb1SLoGin /// - list: &'a mut RwLockWriteGuard<'_, LinkedList<Box<dyn PciDeviceStructure>>> — 链表的写锁守卫,用于访问和遍历PCI设备链表。 166e32effb1SLoGin /// - class_code: u8 — PCI设备class code寄存器值,用于分类设备的功能。 167e32effb1SLoGin /// - subclass: u8 — PCI设备subclass寄存器值,与class_code一起确定设备的子类型。 168e32effb1SLoGin /// 169e32effb1SLoGin /// ## 返回值 170e32effb1SLoGin /// - 包含链表中所有满足条件的PCI结构体的不可变引用的容器。 171b5b571e0SLoGin #[allow(clippy::borrowed_box)] 17278bf93f0SYJwu2023 pub fn get_pci_device_structure<'a>( 17378bf93f0SYJwu2023 list: &'a mut RwLockReadGuard<'_, LinkedList<Box<dyn PciDeviceStructure>>>, 17478bf93f0SYJwu2023 class_code: u8, 17578bf93f0SYJwu2023 subclass: u8, 17678bf93f0SYJwu2023 ) -> Vec<&'a Box<(dyn PciDeviceStructure)>> { 17778bf93f0SYJwu2023 let mut result = Vec::new(); 17878bf93f0SYJwu2023 for box_pci_device_structure in list.iter() { 17978bf93f0SYJwu2023 let common_header = (*box_pci_device_structure).common_header(); 18078bf93f0SYJwu2023 if (common_header.class_code == class_code) && (common_header.subclass == subclass) { 18178bf93f0SYJwu2023 result.push(box_pci_device_structure); 18278bf93f0SYJwu2023 } 18378bf93f0SYJwu2023 } 18478bf93f0SYJwu2023 result 18578bf93f0SYJwu2023 } 18678bf93f0SYJwu2023 18726d84a31SYJwu2023 //Bar0寄存器的offset 18826d84a31SYJwu2023 const BAR0_OFFSET: u8 = 0x10; 18926d84a31SYJwu2023 //Status、Command寄存器的offset 19026d84a31SYJwu2023 const STATUS_COMMAND_OFFSET: u8 = 0x04; 19126d84a31SYJwu2023 /// ID for vendor-specific PCI capabilities.(Virtio Capabilities) 19226d84a31SYJwu2023 pub const PCI_CAP_ID_VNDR: u8 = 0x09; 193cc36cf4aSYJwu2023 pub const PCI_CAP_ID_MSI: u8 = 0x05; 194cc36cf4aSYJwu2023 pub const PCI_CAP_ID_MSIX: u8 = 0x11; 19578bf93f0SYJwu2023 pub const PORT_PCI_CONFIG_ADDRESS: u16 = 0xcf8; 19678bf93f0SYJwu2023 pub const PORT_PCI_CONFIG_DATA: u16 = 0xcfc; 19778bf93f0SYJwu2023 // pci设备分组的id 19878bf93f0SYJwu2023 pub type SegmentGroupNumber = u16; //理论上最多支持65535个Segment_Group 19926d84a31SYJwu2023 20026d84a31SYJwu2023 bitflags! { 20126d84a31SYJwu2023 /// The status register in PCI configuration space. 20226d84a31SYJwu2023 pub struct Status: u16 { 20326d84a31SYJwu2023 // Bits 0-2 are reserved. 20426d84a31SYJwu2023 /// The state of the device's INTx# signal. 20526d84a31SYJwu2023 const INTERRUPT_STATUS = 1 << 3; 20626d84a31SYJwu2023 /// The device has a linked list of capabilities. 20726d84a31SYJwu2023 const CAPABILITIES_LIST = 1 << 4; 20826d84a31SYJwu2023 /// The device is capabile of running at 66 MHz rather than 33 MHz. 20926d84a31SYJwu2023 const MHZ_66_CAPABLE = 1 << 5; 21026d84a31SYJwu2023 // Bit 6 is reserved. 21126d84a31SYJwu2023 /// The device can accept fast back-to-back transactions not from the same agent. 21226d84a31SYJwu2023 const FAST_BACK_TO_BACK_CAPABLE = 1 << 7; 21326d84a31SYJwu2023 /// The bus agent observed a parity error (if parity error handling is enabled). 21426d84a31SYJwu2023 const MASTER_DATA_PARITY_ERROR = 1 << 8; 21526d84a31SYJwu2023 // Bits 9-10 are DEVSEL timing. 21626d84a31SYJwu2023 /// A target device terminated a transaction with target-abort. 21726d84a31SYJwu2023 const SIGNALED_TARGET_ABORT = 1 << 11; 21826d84a31SYJwu2023 /// A master device transaction was terminated with target-abort. 21926d84a31SYJwu2023 const RECEIVED_TARGET_ABORT = 1 << 12; 22026d84a31SYJwu2023 /// A master device transaction was terminated with master-abort. 22126d84a31SYJwu2023 const RECEIVED_MASTER_ABORT = 1 << 13; 22226d84a31SYJwu2023 /// A device asserts SERR#. 22326d84a31SYJwu2023 const SIGNALED_SYSTEM_ERROR = 1 << 14; 22426d84a31SYJwu2023 /// The device detects a parity error, even if parity error handling is disabled. 22526d84a31SYJwu2023 const DETECTED_PARITY_ERROR = 1 << 15; 22626d84a31SYJwu2023 } 22726d84a31SYJwu2023 } 22826d84a31SYJwu2023 22926d84a31SYJwu2023 bitflags! { 23026d84a31SYJwu2023 /// The command register in PCI configuration space. 23178bf93f0SYJwu2023 pub struct Command: u16 { 23226d84a31SYJwu2023 /// The device can respond to I/O Space accesses. 23326d84a31SYJwu2023 const IO_SPACE = 1 << 0; 23426d84a31SYJwu2023 /// The device can respond to Memory Space accesses. 23526d84a31SYJwu2023 const MEMORY_SPACE = 1 << 1; 23626d84a31SYJwu2023 /// The device can behave as a bus master. 23726d84a31SYJwu2023 const BUS_MASTER = 1 << 2; 23826d84a31SYJwu2023 /// The device can monitor Special Cycle operations. 23926d84a31SYJwu2023 const SPECIAL_CYCLES = 1 << 3; 24026d84a31SYJwu2023 /// The device can generate the Memory Write and Invalidate command. 24126d84a31SYJwu2023 const MEMORY_WRITE_AND_INVALIDATE_ENABLE = 1 << 4; 24226d84a31SYJwu2023 /// The device will snoop palette register data. 24326d84a31SYJwu2023 const VGA_PALETTE_SNOOP = 1 << 5; 24426d84a31SYJwu2023 /// The device should take its normal action when a parity error is detected. 24526d84a31SYJwu2023 const PARITY_ERROR_RESPONSE = 1 << 6; 24626d84a31SYJwu2023 // Bit 7 is reserved. 24726d84a31SYJwu2023 /// The SERR# driver is enabled. 24826d84a31SYJwu2023 const SERR_ENABLE = 1 << 8; 24926d84a31SYJwu2023 /// The device is allowed to generate fast back-to-back transactions. 25026d84a31SYJwu2023 const FAST_BACK_TO_BACK_ENABLE = 1 << 9; 25126d84a31SYJwu2023 /// Assertion of the device's INTx# signal is disabled. 25226d84a31SYJwu2023 const INTERRUPT_DISABLE = 1 << 10; 25326d84a31SYJwu2023 } 25426d84a31SYJwu2023 } 25526d84a31SYJwu2023 25678bf93f0SYJwu2023 /// The type of a PCI device function header. 25778bf93f0SYJwu2023 /// 标头类型/设备类型 25878bf93f0SYJwu2023 #[derive(Copy, Clone, Debug, Eq, PartialEq)] 25978bf93f0SYJwu2023 pub enum HeaderType { 26078bf93f0SYJwu2023 /// A normal PCI device. 26178bf93f0SYJwu2023 Standard, 26278bf93f0SYJwu2023 /// A PCI to PCI bridge. 26378bf93f0SYJwu2023 PciPciBridge, 26478bf93f0SYJwu2023 /// A PCI to CardBus bridge. 26578bf93f0SYJwu2023 PciCardbusBridge, 26678bf93f0SYJwu2023 /// Unrecognised header type. 26778bf93f0SYJwu2023 Unrecognised(u8), 26878bf93f0SYJwu2023 } 26978bf93f0SYJwu2023 /// u8到HeaderType的转换 27078bf93f0SYJwu2023 impl From<u8> for HeaderType { 27178bf93f0SYJwu2023 fn from(value: u8) -> Self { 27278bf93f0SYJwu2023 match value { 27378bf93f0SYJwu2023 0x00 => Self::Standard, 27478bf93f0SYJwu2023 0x01 => Self::PciPciBridge, 27578bf93f0SYJwu2023 0x02 => Self::PciCardbusBridge, 27678bf93f0SYJwu2023 _ => Self::Unrecognised(value), 27778bf93f0SYJwu2023 } 27878bf93f0SYJwu2023 } 27978bf93f0SYJwu2023 } 28078bf93f0SYJwu2023 /// Pci可能触发的各种错误 28178bf93f0SYJwu2023 #[derive(Copy, Clone, Debug, Eq, PartialEq)] 28278bf93f0SYJwu2023 pub enum PciError { 28378bf93f0SYJwu2023 /// The device reported an invalid BAR type. 28478bf93f0SYJwu2023 InvalidBarType, 28578bf93f0SYJwu2023 CreateMmioError, 28678bf93f0SYJwu2023 InvalidBusDeviceFunction, 28778bf93f0SYJwu2023 SegmentNotFound, 288cc36cf4aSYJwu2023 McfgTableNotFound, 28978bf93f0SYJwu2023 GetWrongHeader, 29078bf93f0SYJwu2023 UnrecognisedHeaderType, 29178bf93f0SYJwu2023 PciDeviceStructureTransformError, 292cc36cf4aSYJwu2023 PciIrqError(PciIrqError), 29378bf93f0SYJwu2023 } 29478bf93f0SYJwu2023 ///实现PciError的Display trait,使其可以直接输出 29578bf93f0SYJwu2023 impl Display for PciError { 29678bf93f0SYJwu2023 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 29778bf93f0SYJwu2023 match self { 29878bf93f0SYJwu2023 Self::InvalidBarType => write!(f, "Invalid PCI BAR type."), 29978bf93f0SYJwu2023 Self::CreateMmioError => write!(f, "Error occurred while creating mmio."), 30078bf93f0SYJwu2023 Self::InvalidBusDeviceFunction => write!(f, "Found invalid BusDeviceFunction."), 30178bf93f0SYJwu2023 Self::SegmentNotFound => write!(f, "Target segment not found"), 302cc36cf4aSYJwu2023 Self::McfgTableNotFound => write!(f, "ACPI MCFG Table not found"), 30378bf93f0SYJwu2023 Self::GetWrongHeader => write!(f, "GetWrongHeader with vendor id 0xffff"), 30478bf93f0SYJwu2023 Self::UnrecognisedHeaderType => write!(f, "Found device with unrecognised header type"), 30578bf93f0SYJwu2023 Self::PciDeviceStructureTransformError => { 30678bf93f0SYJwu2023 write!(f, "Found None When transform Pci device structure") 30778bf93f0SYJwu2023 } 308cc36cf4aSYJwu2023 Self::PciIrqError(err) => write!(f, "Error occurred while setting irq :{:?}.", err), 30978bf93f0SYJwu2023 } 31078bf93f0SYJwu2023 } 31178bf93f0SYJwu2023 } 31278bf93f0SYJwu2023 31378bf93f0SYJwu2023 /// trait类型Pci_Device_Structure表示pci设备,动态绑定三种具体设备类型:Pci_Device_Structure_General_Device、Pci_Device_Structure_Pci_to_Pci_Bridge、Pci_Device_Structure_Pci_to_Cardbus_Bridge 31478bf93f0SYJwu2023 pub trait PciDeviceStructure: Send + Sync { 31578bf93f0SYJwu2023 /// @brief 获取设备类型 31678bf93f0SYJwu2023 /// @return HeaderType 设备类型 31778bf93f0SYJwu2023 fn header_type(&self) -> HeaderType; 31878bf93f0SYJwu2023 /// @brief 当其为standard设备时返回&Pci_Device_Structure_General_Device,其余情况返回None 319cc36cf4aSYJwu2023 #[inline(always)] 32078bf93f0SYJwu2023 fn as_standard_device(&self) -> Option<&PciDeviceStructureGeneralDevice> { 32178bf93f0SYJwu2023 None 32278bf93f0SYJwu2023 } 32378bf93f0SYJwu2023 /// @brief 当其为pci to pci bridge设备时返回&Pci_Device_Structure_Pci_to_Pci_Bridge,其余情况返回None 324cc36cf4aSYJwu2023 #[inline(always)] 32578bf93f0SYJwu2023 fn as_pci_to_pci_bridge_device(&self) -> Option<&PciDeviceStructurePciToPciBridge> { 32678bf93f0SYJwu2023 None 32778bf93f0SYJwu2023 } 32878bf93f0SYJwu2023 /// @brief 当其为pci to cardbus bridge设备时返回&Pci_Device_Structure_Pci_to_Cardbus_Bridge,其余情况返回None 329cc36cf4aSYJwu2023 #[inline(always)] 33078bf93f0SYJwu2023 fn as_pci_to_carbus_bridge_device(&self) -> Option<&PciDeviceStructurePciToCardbusBridge> { 33178bf93f0SYJwu2023 None 33278bf93f0SYJwu2023 } 33378bf93f0SYJwu2023 /// @brief 获取Pci设备共有的common_header 33478bf93f0SYJwu2023 /// @return 返回其不可变引用 33578bf93f0SYJwu2023 fn common_header(&self) -> &PciDeviceStructureHeader; 33678bf93f0SYJwu2023 /// @brief 当其为standard设备时返回&mut Pci_Device_Structure_General_Device,其余情况返回None 337cc36cf4aSYJwu2023 #[inline(always)] 33878bf93f0SYJwu2023 fn as_standard_device_mut(&mut self) -> Option<&mut PciDeviceStructureGeneralDevice> { 33978bf93f0SYJwu2023 None 34078bf93f0SYJwu2023 } 34178bf93f0SYJwu2023 /// @brief 当其为pci to pci bridge设备时返回&mut Pci_Device_Structure_Pci_to_Pci_Bridge,其余情况返回None 342cc36cf4aSYJwu2023 #[inline(always)] 34378bf93f0SYJwu2023 fn as_pci_to_pci_bridge_device_mut(&mut self) -> Option<&mut PciDeviceStructurePciToPciBridge> { 34478bf93f0SYJwu2023 None 34578bf93f0SYJwu2023 } 34678bf93f0SYJwu2023 /// @brief 当其为pci to cardbus bridge设备时返回&mut Pci_Device_Structure_Pci_to_Cardbus_Bridge,其余情况返回None 347cc36cf4aSYJwu2023 #[inline(always)] 34878bf93f0SYJwu2023 fn as_pci_to_carbus_bridge_device_mut( 34978bf93f0SYJwu2023 &mut self, 35078bf93f0SYJwu2023 ) -> Option<&mut PciDeviceStructurePciToCardbusBridge> { 35178bf93f0SYJwu2023 None 35278bf93f0SYJwu2023 } 35378bf93f0SYJwu2023 /// @brief 返回迭代器,遍历capabilities 35478bf93f0SYJwu2023 fn capabilities(&self) -> Option<CapabilityIterator> { 35578bf93f0SYJwu2023 None 35678bf93f0SYJwu2023 } 35778bf93f0SYJwu2023 /// @brief 获取Status、Command寄存器的值 35878bf93f0SYJwu2023 fn status_command(&self) -> (Status, Command) { 35978bf93f0SYJwu2023 let common_header = self.common_header(); 36078bf93f0SYJwu2023 let status = Status::from_bits_truncate(common_header.status); 36178bf93f0SYJwu2023 let command = Command::from_bits_truncate(common_header.command); 36278bf93f0SYJwu2023 (status, command) 36378bf93f0SYJwu2023 } 36478bf93f0SYJwu2023 /// @brief 设置Command寄存器的值 36578bf93f0SYJwu2023 fn set_command(&mut self, command: Command) { 36678bf93f0SYJwu2023 let common_header = self.common_header_mut(); 36778bf93f0SYJwu2023 let command = command.bits(); 36878bf93f0SYJwu2023 common_header.command = command; 3692709e017SLoGin pci_root_0().write_config( 3702709e017SLoGin common_header.bus_device_function, 3712709e017SLoGin STATUS_COMMAND_OFFSET.into(), 37278bf93f0SYJwu2023 command as u32, 37378bf93f0SYJwu2023 ); 37478bf93f0SYJwu2023 } 37578bf93f0SYJwu2023 /// @brief 获取Pci设备共有的common_header 37678bf93f0SYJwu2023 /// @return 返回其可变引用 37778bf93f0SYJwu2023 fn common_header_mut(&mut self) -> &mut PciDeviceStructureHeader; 378cc36cf4aSYJwu2023 37978bf93f0SYJwu2023 /// @brief 读取standard设备的bar寄存器,映射后将结果加入结构体的standard_device_bar变量 38078bf93f0SYJwu2023 /// @return 只有standard设备才返回成功或者错误,其余返回None 381cc36cf4aSYJwu2023 #[inline(always)] 382cc36cf4aSYJwu2023 fn bar_ioremap(&mut self) -> Option<Result<u8, PciError>> { 38378bf93f0SYJwu2023 None 38478bf93f0SYJwu2023 } 385cc36cf4aSYJwu2023 /// @brief 获取PCI设备的bar寄存器的引用 386cc36cf4aSYJwu2023 /// @return 387cc36cf4aSYJwu2023 #[inline(always)] 388cc36cf4aSYJwu2023 fn bar(&mut self) -> Option<&PciStandardDeviceBar> { 38978bf93f0SYJwu2023 None 39078bf93f0SYJwu2023 } 391cc36cf4aSYJwu2023 /// @brief 通过设置该pci设备的command 39278bf93f0SYJwu2023 fn enable_master(&mut self) { 39378bf93f0SYJwu2023 self.set_command(Command::IO_SPACE | Command::MEMORY_SPACE | Command::BUS_MASTER); 39478bf93f0SYJwu2023 } 395cc36cf4aSYJwu2023 /// @brief 寻找设备的msix空间的offset 396cc36cf4aSYJwu2023 fn msix_capability_offset(&self) -> Option<u8> { 397cc36cf4aSYJwu2023 for capability in self.capabilities()? { 398cc36cf4aSYJwu2023 if capability.id == PCI_CAP_ID_MSIX { 399cc36cf4aSYJwu2023 return Some(capability.offset); 400cc36cf4aSYJwu2023 } 401cc36cf4aSYJwu2023 } 402cc36cf4aSYJwu2023 None 403cc36cf4aSYJwu2023 } 404cc36cf4aSYJwu2023 /// @brief 寻找设备的msi空间的offset 405cc36cf4aSYJwu2023 fn msi_capability_offset(&self) -> Option<u8> { 406cc36cf4aSYJwu2023 for capability in self.capabilities()? { 407cc36cf4aSYJwu2023 if capability.id == PCI_CAP_ID_MSI { 408cc36cf4aSYJwu2023 return Some(capability.offset); 409cc36cf4aSYJwu2023 } 410cc36cf4aSYJwu2023 } 411cc36cf4aSYJwu2023 None 412cc36cf4aSYJwu2023 } 413cc36cf4aSYJwu2023 /// @brief 返回结构体中的irq_type的可变引用 414cc36cf4aSYJwu2023 fn irq_type_mut(&mut self) -> Option<&mut IrqType>; 415cc36cf4aSYJwu2023 /// @brief 返回结构体中的irq_vector的可变引用 416e2841179SLoGin fn irq_vector_mut(&mut self) -> Option<&mut Vec<IrqNumber>>; 41778bf93f0SYJwu2023 } 41878bf93f0SYJwu2023 41978bf93f0SYJwu2023 /// Pci_Device_Structure_Header PCI设备结构体共有的头部 42078bf93f0SYJwu2023 #[derive(Clone, Debug)] 42178bf93f0SYJwu2023 pub struct PciDeviceStructureHeader { 42278bf93f0SYJwu2023 // ==== busdevicefunction变量表示该结构体所处的位置 42378bf93f0SYJwu2023 pub bus_device_function: BusDeviceFunction, 42478bf93f0SYJwu2023 pub vendor_id: u16, // 供应商ID 0xffff是一个无效值,在读取访问不存在的设备的配置空间寄存器时返回 42578bf93f0SYJwu2023 pub device_id: u16, // 设备ID,标志特定设备 42678bf93f0SYJwu2023 pub command: u16, // 提供对设备生成和响应pci周期的能力的控制 向该寄存器写入0时,设备与pci总线断开除配置空间访问以外的所有连接 42778bf93f0SYJwu2023 pub status: u16, // 用于记录pci总线相关时间的状态信息寄存器 42878bf93f0SYJwu2023 pub revision_id: u8, // 修订ID,指定特定设备的修订标志符 42978bf93f0SYJwu2023 pub prog_if: u8, // 编程接口字节,一个只读寄存器,指定设备具有的寄存器级别的编程接口(如果有的话) 43078bf93f0SYJwu2023 pub subclass: u8, // 子类。指定设备执行的特定功能的只读寄存器 43178bf93f0SYJwu2023 pub class_code: u8, // 类代码,一个只读寄存器,指定设备执行的功能类型 43278bf93f0SYJwu2023 pub cache_line_size: u8, // 缓存线大小:以 32 位为单位指定系统缓存线大小。设备可以限制它可以支持的缓存线大小的数量,如果不支持的值写入该字段,设备将表现得好像写入了 0 值 43378bf93f0SYJwu2023 pub latency_timer: u8, // 延迟计时器:以 PCI 总线时钟为单位指定延迟计时器。 43478bf93f0SYJwu2023 pub header_type: u8, // 标头类型 a value of 0x0 specifies a general device, a value of 0x1 specifies a PCI-to-PCI bridge, and a value of 0x2 specifies a CardBus bridge. If bit 7 of this register is set, the device has multiple functions; otherwise, it is a single function device. 43578bf93f0SYJwu2023 pub bist: u8, // Represents that status and allows control of a devices BIST (built-in self test). 43678bf93f0SYJwu2023 // Here is the layout of the BIST register: 43778bf93f0SYJwu2023 // | bit7 | bit6 | Bits 5-4 | Bits 3-0 | 43878bf93f0SYJwu2023 // | BIST Capable | Start BIST | Reserved | Completion Code | 43978bf93f0SYJwu2023 // for more details, please visit https://wiki.osdev.org/PCI 44078bf93f0SYJwu2023 } 44178bf93f0SYJwu2023 44278bf93f0SYJwu2023 /// Pci_Device_Structure_General_Device PCI标准设备结构体 44378bf93f0SYJwu2023 #[derive(Clone, Debug)] 44478bf93f0SYJwu2023 pub struct PciDeviceStructureGeneralDevice { 44578bf93f0SYJwu2023 pub common_header: PciDeviceStructureHeader, 446cc36cf4aSYJwu2023 // 中断结构体,包括legacy,msi,msix三种情况 447cc36cf4aSYJwu2023 pub irq_type: IrqType, 448cc36cf4aSYJwu2023 // 使用的中断号的vec集合 449e2841179SLoGin pub irq_vector: Vec<IrqNumber>, 45078bf93f0SYJwu2023 pub standard_device_bar: PciStandardDeviceBar, 45178bf93f0SYJwu2023 pub cardbus_cis_pointer: u32, // 指向卡信息结构,供在 CardBus 和 PCI 之间共享芯片的设备使用。 45278bf93f0SYJwu2023 pub subsystem_vendor_id: u16, 45378bf93f0SYJwu2023 pub subsystem_id: u16, 45478bf93f0SYJwu2023 pub expansion_rom_base_address: u32, 45578bf93f0SYJwu2023 pub capabilities_pointer: u8, 45678bf93f0SYJwu2023 pub reserved0: u8, 45778bf93f0SYJwu2023 pub reserved1: u16, 45878bf93f0SYJwu2023 pub reserved2: u32, 45978bf93f0SYJwu2023 pub interrupt_line: u8, // 指定设备的中断引脚连接到系统中断控制器的哪个输入,并由任何使用中断引脚的设备实现。对于 x86 架构,此寄存器对应于 PIC IRQ 编号 0-15(而不是 I/O APIC IRQ 编号),并且值0xFF定义为无连接。 46078bf93f0SYJwu2023 pub interrupt_pin: u8, // 指定设备使用的中断引脚。其中值为0x1INTA#、0x2INTB#、0x3INTC#、0x4INTD#,0x0表示设备不使用中断引脚。 46178bf93f0SYJwu2023 pub min_grant: u8, // 一个只读寄存器,用于指定设备所需的突发周期长度(以 1/4 微秒为单位)(假设时钟速率为 33 MHz) 46278bf93f0SYJwu2023 pub max_latency: u8, // 一个只读寄存器,指定设备需要多长时间访问一次 PCI 总线(以 1/4 微秒为单位)。 46378bf93f0SYJwu2023 } 46478bf93f0SYJwu2023 impl PciDeviceStructure for PciDeviceStructureGeneralDevice { 465cc36cf4aSYJwu2023 #[inline(always)] 46678bf93f0SYJwu2023 fn header_type(&self) -> HeaderType { 46778bf93f0SYJwu2023 HeaderType::Standard 46878bf93f0SYJwu2023 } 469cc36cf4aSYJwu2023 #[inline(always)] 47078bf93f0SYJwu2023 fn as_standard_device(&self) -> Option<&PciDeviceStructureGeneralDevice> { 47178bf93f0SYJwu2023 Some(self) 47278bf93f0SYJwu2023 } 473cc36cf4aSYJwu2023 #[inline(always)] 47478bf93f0SYJwu2023 fn as_standard_device_mut(&mut self) -> Option<&mut PciDeviceStructureGeneralDevice> { 47578bf93f0SYJwu2023 Some(self) 47678bf93f0SYJwu2023 } 477cc36cf4aSYJwu2023 #[inline(always)] 47878bf93f0SYJwu2023 fn common_header(&self) -> &PciDeviceStructureHeader { 47978bf93f0SYJwu2023 &self.common_header 48078bf93f0SYJwu2023 } 481cc36cf4aSYJwu2023 #[inline(always)] 48278bf93f0SYJwu2023 fn common_header_mut(&mut self) -> &mut PciDeviceStructureHeader { 48378bf93f0SYJwu2023 &mut self.common_header 48478bf93f0SYJwu2023 } 48578bf93f0SYJwu2023 fn capabilities(&self) -> Option<CapabilityIterator> { 48678bf93f0SYJwu2023 Some(CapabilityIterator { 48778bf93f0SYJwu2023 bus_device_function: self.common_header.bus_device_function, 48878bf93f0SYJwu2023 next_capability_offset: Some(self.capabilities_pointer), 48978bf93f0SYJwu2023 }) 49078bf93f0SYJwu2023 } 491cc36cf4aSYJwu2023 fn bar_ioremap(&mut self) -> Option<Result<u8, PciError>> { 49278bf93f0SYJwu2023 let common_header = &self.common_header; 49378bf93f0SYJwu2023 match pci_bar_init(common_header.bus_device_function) { 49478bf93f0SYJwu2023 Ok(bar) => { 49578bf93f0SYJwu2023 self.standard_device_bar = bar; 49678bf93f0SYJwu2023 Some(Ok(0)) 49778bf93f0SYJwu2023 } 49878bf93f0SYJwu2023 Err(e) => Some(Err(e)), 49978bf93f0SYJwu2023 } 50078bf93f0SYJwu2023 } 501cc36cf4aSYJwu2023 fn bar(&mut self) -> Option<&PciStandardDeviceBar> { 502cc36cf4aSYJwu2023 Some(&self.standard_device_bar) 50378bf93f0SYJwu2023 } 504cc36cf4aSYJwu2023 #[inline(always)] 505cc36cf4aSYJwu2023 fn irq_type_mut(&mut self) -> Option<&mut IrqType> { 506cc36cf4aSYJwu2023 Some(&mut self.irq_type) 507cc36cf4aSYJwu2023 } 508cc36cf4aSYJwu2023 #[inline(always)] 509e2841179SLoGin fn irq_vector_mut(&mut self) -> Option<&mut Vec<IrqNumber>> { 510cc36cf4aSYJwu2023 Some(&mut self.irq_vector) 511cc36cf4aSYJwu2023 } 512cc36cf4aSYJwu2023 } 513cc36cf4aSYJwu2023 51478bf93f0SYJwu2023 /// Pci_Device_Structure_Pci_to_Pci_Bridge pci-to-pci桥设备结构体 51578bf93f0SYJwu2023 #[derive(Clone, Debug)] 51678bf93f0SYJwu2023 pub struct PciDeviceStructurePciToPciBridge { 51778bf93f0SYJwu2023 pub common_header: PciDeviceStructureHeader, 518cc36cf4aSYJwu2023 // 中断结构体,包括legacy,msi,msix三种情况 519cc36cf4aSYJwu2023 pub irq_type: IrqType, 520cc36cf4aSYJwu2023 // 使用的中断号的vec集合 521e2841179SLoGin pub irq_vector: Vec<IrqNumber>, 52278bf93f0SYJwu2023 pub bar0: u32, 52378bf93f0SYJwu2023 pub bar1: u32, 52478bf93f0SYJwu2023 pub primary_bus_number: u8, 52578bf93f0SYJwu2023 pub secondary_bus_number: u8, 52678bf93f0SYJwu2023 pub subordinate_bus_number: u8, 52778bf93f0SYJwu2023 pub secondary_latency_timer: u8, 52878bf93f0SYJwu2023 pub io_base: u8, 52978bf93f0SYJwu2023 pub io_limit: u8, 53078bf93f0SYJwu2023 pub secondary_status: u16, 53178bf93f0SYJwu2023 pub memory_base: u16, 53278bf93f0SYJwu2023 pub memory_limit: u16, 53378bf93f0SYJwu2023 pub prefetchable_memory_base: u16, 53478bf93f0SYJwu2023 pub prefetchable_memory_limit: u16, 53578bf93f0SYJwu2023 pub prefetchable_base_upper_32_bits: u32, 53678bf93f0SYJwu2023 pub prefetchable_limit_upper_32_bits: u32, 53778bf93f0SYJwu2023 pub io_base_upper_16_bits: u16, 53878bf93f0SYJwu2023 pub io_limit_upper_16_bits: u16, 53978bf93f0SYJwu2023 pub capability_pointer: u8, 54078bf93f0SYJwu2023 pub reserved0: u8, 54178bf93f0SYJwu2023 pub reserved1: u16, 54278bf93f0SYJwu2023 pub expansion_rom_base_address: u32, 54378bf93f0SYJwu2023 pub interrupt_line: u8, 54478bf93f0SYJwu2023 pub interrupt_pin: u8, 54578bf93f0SYJwu2023 pub bridge_control: u16, 54678bf93f0SYJwu2023 } 54778bf93f0SYJwu2023 impl PciDeviceStructure for PciDeviceStructurePciToPciBridge { 548cc36cf4aSYJwu2023 #[inline(always)] 54978bf93f0SYJwu2023 fn header_type(&self) -> HeaderType { 55078bf93f0SYJwu2023 HeaderType::PciPciBridge 55178bf93f0SYJwu2023 } 552cc36cf4aSYJwu2023 #[inline(always)] 55378bf93f0SYJwu2023 fn as_pci_to_pci_bridge_device(&self) -> Option<&PciDeviceStructurePciToPciBridge> { 55478bf93f0SYJwu2023 Some(self) 55578bf93f0SYJwu2023 } 556cc36cf4aSYJwu2023 #[inline(always)] 55778bf93f0SYJwu2023 fn as_pci_to_pci_bridge_device_mut(&mut self) -> Option<&mut PciDeviceStructurePciToPciBridge> { 55878bf93f0SYJwu2023 Some(self) 55978bf93f0SYJwu2023 } 560cc36cf4aSYJwu2023 #[inline(always)] 56178bf93f0SYJwu2023 fn common_header(&self) -> &PciDeviceStructureHeader { 56278bf93f0SYJwu2023 &self.common_header 56378bf93f0SYJwu2023 } 564cc36cf4aSYJwu2023 #[inline(always)] 56578bf93f0SYJwu2023 fn common_header_mut(&mut self) -> &mut PciDeviceStructureHeader { 56678bf93f0SYJwu2023 &mut self.common_header 56778bf93f0SYJwu2023 } 568cc36cf4aSYJwu2023 #[inline(always)] 569cc36cf4aSYJwu2023 fn irq_type_mut(&mut self) -> Option<&mut IrqType> { 570cc36cf4aSYJwu2023 Some(&mut self.irq_type) 571cc36cf4aSYJwu2023 } 572cc36cf4aSYJwu2023 #[inline(always)] 573e2841179SLoGin fn irq_vector_mut(&mut self) -> Option<&mut Vec<IrqNumber>> { 574cc36cf4aSYJwu2023 Some(&mut self.irq_vector) 575cc36cf4aSYJwu2023 } 57678bf93f0SYJwu2023 } 57778bf93f0SYJwu2023 /// Pci_Device_Structure_Pci_to_Cardbus_Bridge Pci_to_Cardbus桥设备结构体 57878bf93f0SYJwu2023 #[derive(Clone, Debug)] 57978bf93f0SYJwu2023 pub struct PciDeviceStructurePciToCardbusBridge { 58078bf93f0SYJwu2023 pub common_header: PciDeviceStructureHeader, 58178bf93f0SYJwu2023 pub cardbus_socket_ex_ca_base_address: u32, 58278bf93f0SYJwu2023 pub offset_of_capabilities_list: u8, 58378bf93f0SYJwu2023 pub reserved: u8, 58478bf93f0SYJwu2023 pub secondary_status: u16, 58578bf93f0SYJwu2023 pub pci_bus_number: u8, 58678bf93f0SYJwu2023 pub card_bus_bus_number: u8, 58778bf93f0SYJwu2023 pub subordinate_bus_number: u8, 58878bf93f0SYJwu2023 pub card_bus_latency_timer: u8, 58978bf93f0SYJwu2023 pub memory_base_address0: u32, 59078bf93f0SYJwu2023 pub memory_limit0: u32, 59178bf93f0SYJwu2023 pub memory_base_address1: u32, 59278bf93f0SYJwu2023 pub memory_limit1: u32, 59378bf93f0SYJwu2023 pub io_base_address0: u32, 59478bf93f0SYJwu2023 pub io_limit0: u32, 59578bf93f0SYJwu2023 pub io_base_address1: u32, 59678bf93f0SYJwu2023 pub io_limit1: u32, 59778bf93f0SYJwu2023 pub interrupt_line: u8, 59878bf93f0SYJwu2023 pub interrupt_pin: u8, 59978bf93f0SYJwu2023 pub bridge_control: u16, 60078bf93f0SYJwu2023 pub subsystem_device_id: u16, 60178bf93f0SYJwu2023 pub subsystem_vendor_id: u16, 60278bf93f0SYJwu2023 pub pc_card_legacy_mode_base_address_16_bit: u32, 60378bf93f0SYJwu2023 } 60478bf93f0SYJwu2023 impl PciDeviceStructure for PciDeviceStructurePciToCardbusBridge { 605cc36cf4aSYJwu2023 #[inline(always)] 60678bf93f0SYJwu2023 fn header_type(&self) -> HeaderType { 60778bf93f0SYJwu2023 HeaderType::PciCardbusBridge 60878bf93f0SYJwu2023 } 609cc36cf4aSYJwu2023 #[inline(always)] 61078bf93f0SYJwu2023 fn as_pci_to_carbus_bridge_device(&self) -> Option<&PciDeviceStructurePciToCardbusBridge> { 611b5b571e0SLoGin Some(self) 61278bf93f0SYJwu2023 } 613cc36cf4aSYJwu2023 #[inline(always)] 61478bf93f0SYJwu2023 fn as_pci_to_carbus_bridge_device_mut( 61578bf93f0SYJwu2023 &mut self, 61678bf93f0SYJwu2023 ) -> Option<&mut PciDeviceStructurePciToCardbusBridge> { 61778bf93f0SYJwu2023 Some(self) 61878bf93f0SYJwu2023 } 619cc36cf4aSYJwu2023 #[inline(always)] 62078bf93f0SYJwu2023 fn common_header(&self) -> &PciDeviceStructureHeader { 62178bf93f0SYJwu2023 &self.common_header 62278bf93f0SYJwu2023 } 623cc36cf4aSYJwu2023 #[inline(always)] 62478bf93f0SYJwu2023 fn common_header_mut(&mut self) -> &mut PciDeviceStructureHeader { 62578bf93f0SYJwu2023 &mut self.common_header 62678bf93f0SYJwu2023 } 627cc36cf4aSYJwu2023 #[inline(always)] 628cc36cf4aSYJwu2023 fn irq_type_mut(&mut self) -> Option<&mut IrqType> { 629cc36cf4aSYJwu2023 None 630cc36cf4aSYJwu2023 } 631cc36cf4aSYJwu2023 #[inline(always)] 632e2841179SLoGin fn irq_vector_mut(&mut self) -> Option<&mut Vec<IrqNumber>> { 633cc36cf4aSYJwu2023 None 634cc36cf4aSYJwu2023 } 63578bf93f0SYJwu2023 } 63678bf93f0SYJwu2023 6372709e017SLoGin /// PCI配置空间访问机制 6382709e017SLoGin /// 6392709e017SLoGin /// 用于访问PCI设备的功能配置空间的一组机制。 6402709e017SLoGin #[derive(Copy, Clone, Debug, Eq, PartialEq)] 6412709e017SLoGin pub enum PciCam { 6422709e017SLoGin /// PCI内存映射配置访问机制 6432709e017SLoGin /// 6442709e017SLoGin /// 为每个设备功能提供256字节的配置空间访问。 6452709e017SLoGin MmioCam, 6462709e017SLoGin /// PCIe内存映射增强配置访问机制 6472709e017SLoGin /// 6482709e017SLoGin /// 为每个设备功能提供4千字节(4096字节)的配置空间访问。 6492709e017SLoGin Ecam, 6502709e017SLoGin } 6512709e017SLoGin 6522709e017SLoGin impl PciCam { 6532709e017SLoGin /// Returns the total size in bytes of the memory-mapped region. 6542709e017SLoGin pub const fn size(self) -> u32 { 6552709e017SLoGin match self { 6562709e017SLoGin Self::MmioCam => 0x1000000, 6572709e017SLoGin Self::Ecam => 0x10000000, 6582709e017SLoGin } 6592709e017SLoGin } 6602709e017SLoGin } 6612709e017SLoGin 66226d84a31SYJwu2023 /// Gets the capabilities 'pointer' for the device function, if any. 66326d84a31SYJwu2023 /// @brief 获取第一个capability 的offset 66478bf93f0SYJwu2023 /// @param bus_device_function PCI设备的唯一标识 66526d84a31SYJwu2023 /// @return Option<u8> offset 66678bf93f0SYJwu2023 pub fn capabilities_offset(bus_device_function: BusDeviceFunction) -> Option<u8> { 6672709e017SLoGin let result = pci_root_0().read_config(bus_device_function, STATUS_COMMAND_OFFSET.into()); 66878bf93f0SYJwu2023 let status: Status = Status::from_bits_truncate((result >> 16) as u16); 66926d84a31SYJwu2023 if status.contains(Status::CAPABILITIES_LIST) { 6702709e017SLoGin let cap_pointer = pci_root_0().read_config(bus_device_function, 0x34) as u8 & 0xFC; 67126d84a31SYJwu2023 Some(cap_pointer) 67226d84a31SYJwu2023 } else { 67326d84a31SYJwu2023 None 67426d84a31SYJwu2023 } 67526d84a31SYJwu2023 } 67678bf93f0SYJwu2023 67778bf93f0SYJwu2023 /// @brief 读取pci设备头部 67878bf93f0SYJwu2023 /// @param bus_device_function PCI设备的唯一标识 67978bf93f0SYJwu2023 /// @param add_to_list 是否添加到链表 68078bf93f0SYJwu2023 /// @return 返回的header(trait 类型) 68178bf93f0SYJwu2023 fn pci_read_header( 68278bf93f0SYJwu2023 bus_device_function: BusDeviceFunction, 68378bf93f0SYJwu2023 add_to_list: bool, 68478bf93f0SYJwu2023 ) -> Result<Box<dyn PciDeviceStructure>, PciError> { 68578bf93f0SYJwu2023 // 先读取公共header 6862709e017SLoGin let result = pci_root_0().read_config(bus_device_function, 0x00); 68778bf93f0SYJwu2023 let vendor_id = result as u16; 68878bf93f0SYJwu2023 let device_id = (result >> 16) as u16; 68978bf93f0SYJwu2023 6902709e017SLoGin let result = pci_root_0().read_config(bus_device_function, 0x04); 69178bf93f0SYJwu2023 let command = result as u16; 69278bf93f0SYJwu2023 let status = (result >> 16) as u16; 69378bf93f0SYJwu2023 6942709e017SLoGin let result = pci_root_0().read_config(bus_device_function, 0x08); 69578bf93f0SYJwu2023 let revision_id = result as u8; 69678bf93f0SYJwu2023 let prog_if = (result >> 8) as u8; 69778bf93f0SYJwu2023 let subclass = (result >> 16) as u8; 69878bf93f0SYJwu2023 let class_code = (result >> 24) as u8; 69978bf93f0SYJwu2023 7002709e017SLoGin let result = pci_root_0().read_config(bus_device_function, 0x0c); 70178bf93f0SYJwu2023 let cache_line_size = result as u8; 70278bf93f0SYJwu2023 let latency_timer = (result >> 8) as u8; 70378bf93f0SYJwu2023 let header_type = (result >> 16) as u8; 70478bf93f0SYJwu2023 let bist = (result >> 24) as u8; 70578bf93f0SYJwu2023 if vendor_id == 0xffff { 70678bf93f0SYJwu2023 return Err(PciError::GetWrongHeader); 70778bf93f0SYJwu2023 } 70878bf93f0SYJwu2023 let header = PciDeviceStructureHeader { 70978bf93f0SYJwu2023 bus_device_function, 71078bf93f0SYJwu2023 vendor_id, 71178bf93f0SYJwu2023 device_id, 71278bf93f0SYJwu2023 command, 71378bf93f0SYJwu2023 status, 71478bf93f0SYJwu2023 revision_id, 71578bf93f0SYJwu2023 prog_if, 71678bf93f0SYJwu2023 subclass, 71778bf93f0SYJwu2023 class_code, 71878bf93f0SYJwu2023 cache_line_size, 71978bf93f0SYJwu2023 latency_timer, 72078bf93f0SYJwu2023 header_type, 72178bf93f0SYJwu2023 bist, 72278bf93f0SYJwu2023 }; 72378bf93f0SYJwu2023 match HeaderType::from(header_type & 0x7f) { 72478bf93f0SYJwu2023 HeaderType::Standard => { 7251f4877a4S曾俊 let general_device: PciDeviceStructureGeneralDevice = 7261f4877a4S曾俊 pci_read_general_device_header(header, &bus_device_function); 7271f4877a4S曾俊 let box_general_device = Box::new(general_device.clone()); 72878bf93f0SYJwu2023 let box_general_device_clone = box_general_device.clone(); 72978bf93f0SYJwu2023 if add_to_list { 73078bf93f0SYJwu2023 PCI_DEVICE_LINKEDLIST.add(box_general_device); 7311f4877a4S曾俊 //这里实际上不应该使用clone,因为raw是用于sysfs的结构,但是实际上pci设备是在PCI_DEVICE_LINKEDLIST链表上的, 7321f4877a4S曾俊 //这就导致sysfs呈现的对pci设备的操控接口实际上操控的是pci设备描述符是一个副本 7331f4877a4S曾俊 //但是无奈这里没有使用Arc 7341f4877a4S曾俊 //todo:修改pci设备描述符在静态链表中存在的方式,并修改这里的clone操作 7351f4877a4S曾俊 let raw = PciGeneralDevice::from(&general_device); 7361f4877a4S曾俊 let _ = pci_device_manager().device_add(Arc::new(raw)); 73778bf93f0SYJwu2023 } 73878bf93f0SYJwu2023 Ok(box_general_device_clone) 73978bf93f0SYJwu2023 } 74078bf93f0SYJwu2023 HeaderType::PciPciBridge => { 74178bf93f0SYJwu2023 let pci_to_pci_bridge = pci_read_pci_to_pci_bridge_header(header, &bus_device_function); 74278bf93f0SYJwu2023 let box_pci_to_pci_bridge = Box::new(pci_to_pci_bridge); 74378bf93f0SYJwu2023 let box_pci_to_pci_bridge_clone = box_pci_to_pci_bridge.clone(); 74478bf93f0SYJwu2023 if add_to_list { 74578bf93f0SYJwu2023 PCI_DEVICE_LINKEDLIST.add(box_pci_to_pci_bridge); 74678bf93f0SYJwu2023 } 74778bf93f0SYJwu2023 Ok(box_pci_to_pci_bridge_clone) 74878bf93f0SYJwu2023 } 74978bf93f0SYJwu2023 HeaderType::PciCardbusBridge => { 75078bf93f0SYJwu2023 let pci_cardbus_bridge = 75178bf93f0SYJwu2023 pci_read_pci_to_cardbus_bridge_header(header, &bus_device_function); 75278bf93f0SYJwu2023 let box_pci_cardbus_bridge = Box::new(pci_cardbus_bridge); 75378bf93f0SYJwu2023 let box_pci_cardbus_bridge_clone = box_pci_cardbus_bridge.clone(); 75478bf93f0SYJwu2023 if add_to_list { 75578bf93f0SYJwu2023 PCI_DEVICE_LINKEDLIST.add(box_pci_cardbus_bridge); 75678bf93f0SYJwu2023 } 75778bf93f0SYJwu2023 Ok(box_pci_cardbus_bridge_clone) 75878bf93f0SYJwu2023 } 75978bf93f0SYJwu2023 HeaderType::Unrecognised(_) => Err(PciError::UnrecognisedHeaderType), 76078bf93f0SYJwu2023 } 76178bf93f0SYJwu2023 } 76278bf93f0SYJwu2023 76378bf93f0SYJwu2023 /// @brief 读取type为0x0的pci设备的header 76478bf93f0SYJwu2023 /// 本函数只应被 pci_read_header()调用 76578bf93f0SYJwu2023 /// @param common_header 共有头部 76678bf93f0SYJwu2023 /// @param bus_device_function PCI设备的唯一标识 76778bf93f0SYJwu2023 /// @return Pci_Device_Structure_General_Device 标准设备头部 76878bf93f0SYJwu2023 fn pci_read_general_device_header( 76978bf93f0SYJwu2023 common_header: PciDeviceStructureHeader, 77078bf93f0SYJwu2023 bus_device_function: &BusDeviceFunction, 77178bf93f0SYJwu2023 ) -> PciDeviceStructureGeneralDevice { 77278bf93f0SYJwu2023 let standard_device_bar = PciStandardDeviceBar::default(); 7732709e017SLoGin let cardbus_cis_pointer = pci_root_0().read_config(*bus_device_function, 0x28); 77478bf93f0SYJwu2023 7752709e017SLoGin let result = pci_root_0().read_config(*bus_device_function, 0x2c); 77678bf93f0SYJwu2023 let subsystem_vendor_id = result as u16; 77778bf93f0SYJwu2023 let subsystem_id = (result >> 16) as u16; 77878bf93f0SYJwu2023 7792709e017SLoGin let expansion_rom_base_address = pci_root_0().read_config(*bus_device_function, 0x30); 78078bf93f0SYJwu2023 7812709e017SLoGin let result = pci_root_0().read_config(*bus_device_function, 0x34); 78278bf93f0SYJwu2023 let capabilities_pointer = result as u8; 78378bf93f0SYJwu2023 let reserved0 = (result >> 8) as u8; 78478bf93f0SYJwu2023 let reserved1 = (result >> 16) as u16; 78578bf93f0SYJwu2023 7862709e017SLoGin let reserved2 = pci_root_0().read_config(*bus_device_function, 0x38); 78778bf93f0SYJwu2023 7882709e017SLoGin let result = pci_root_0().read_config(*bus_device_function, 0x3c); 78978bf93f0SYJwu2023 let interrupt_line = result as u8; 79078bf93f0SYJwu2023 let interrupt_pin = (result >> 8) as u8; 79178bf93f0SYJwu2023 let min_grant = (result >> 16) as u8; 79278bf93f0SYJwu2023 let max_latency = (result >> 24) as u8; 79378bf93f0SYJwu2023 PciDeviceStructureGeneralDevice { 79478bf93f0SYJwu2023 common_header, 795cc36cf4aSYJwu2023 irq_type: IrqType::Unused, 796cc36cf4aSYJwu2023 irq_vector: Vec::new(), 79778bf93f0SYJwu2023 standard_device_bar, 79878bf93f0SYJwu2023 cardbus_cis_pointer, 79978bf93f0SYJwu2023 subsystem_vendor_id, 80078bf93f0SYJwu2023 subsystem_id, 80178bf93f0SYJwu2023 expansion_rom_base_address, 80278bf93f0SYJwu2023 capabilities_pointer, 80378bf93f0SYJwu2023 reserved0, 80478bf93f0SYJwu2023 reserved1, 80578bf93f0SYJwu2023 reserved2, 80678bf93f0SYJwu2023 interrupt_line, 80778bf93f0SYJwu2023 interrupt_pin, 80878bf93f0SYJwu2023 min_grant, 80978bf93f0SYJwu2023 max_latency, 81078bf93f0SYJwu2023 } 81178bf93f0SYJwu2023 } 81278bf93f0SYJwu2023 81378bf93f0SYJwu2023 /// @brief 读取type为0x1的pci设备的header 81478bf93f0SYJwu2023 /// 本函数只应被 pci_read_header()调用 81578bf93f0SYJwu2023 /// @param common_header 共有头部 81678bf93f0SYJwu2023 /// @param bus_device_function PCI设备的唯一标识 81778bf93f0SYJwu2023 /// @return Pci_Device_Structure_Pci_to_Pci_Bridge pci-to-pci 桥设备头部 81878bf93f0SYJwu2023 fn pci_read_pci_to_pci_bridge_header( 81978bf93f0SYJwu2023 common_header: PciDeviceStructureHeader, 82078bf93f0SYJwu2023 bus_device_function: &BusDeviceFunction, 82178bf93f0SYJwu2023 ) -> PciDeviceStructurePciToPciBridge { 8222709e017SLoGin let bar0 = pci_root_0().read_config(*bus_device_function, 0x10); 8232709e017SLoGin let bar1 = pci_root_0().read_config(*bus_device_function, 0x14); 82478bf93f0SYJwu2023 8252709e017SLoGin let result = pci_root_0().read_config(*bus_device_function, 0x18); 82678bf93f0SYJwu2023 82778bf93f0SYJwu2023 let primary_bus_number = result as u8; 82878bf93f0SYJwu2023 let secondary_bus_number = (result >> 8) as u8; 82978bf93f0SYJwu2023 let subordinate_bus_number = (result >> 16) as u8; 83078bf93f0SYJwu2023 let secondary_latency_timer = (result >> 24) as u8; 83178bf93f0SYJwu2023 8322709e017SLoGin let result = pci_root_0().read_config(*bus_device_function, 0x1c); 83378bf93f0SYJwu2023 let io_base = result as u8; 83478bf93f0SYJwu2023 let io_limit = (result >> 8) as u8; 83578bf93f0SYJwu2023 let secondary_status = (result >> 16) as u16; 83678bf93f0SYJwu2023 8372709e017SLoGin let result = pci_root_0().read_config(*bus_device_function, 0x20); 83878bf93f0SYJwu2023 let memory_base = result as u16; 83978bf93f0SYJwu2023 let memory_limit = (result >> 16) as u16; 84078bf93f0SYJwu2023 8412709e017SLoGin let result = pci_root_0().read_config(*bus_device_function, 0x24); 84278bf93f0SYJwu2023 let prefetchable_memory_base = result as u16; 84378bf93f0SYJwu2023 let prefetchable_memory_limit = (result >> 16) as u16; 84478bf93f0SYJwu2023 8452709e017SLoGin let prefetchable_base_upper_32_bits = pci_root_0().read_config(*bus_device_function, 0x28); 8462709e017SLoGin let prefetchable_limit_upper_32_bits = pci_root_0().read_config(*bus_device_function, 0x2c); 84778bf93f0SYJwu2023 8482709e017SLoGin let result = pci_root_0().read_config(*bus_device_function, 0x30); 84978bf93f0SYJwu2023 let io_base_upper_16_bits = result as u16; 85078bf93f0SYJwu2023 let io_limit_upper_16_bits = (result >> 16) as u16; 85178bf93f0SYJwu2023 8522709e017SLoGin let result = pci_root_0().read_config(*bus_device_function, 0x34); 85378bf93f0SYJwu2023 let capability_pointer = result as u8; 85478bf93f0SYJwu2023 let reserved0 = (result >> 8) as u8; 85578bf93f0SYJwu2023 let reserved1 = (result >> 16) as u16; 85678bf93f0SYJwu2023 8572709e017SLoGin let expansion_rom_base_address = pci_root_0().read_config(*bus_device_function, 0x38); 85878bf93f0SYJwu2023 8592709e017SLoGin let result = pci_root_0().read_config(*bus_device_function, 0x3c); 86078bf93f0SYJwu2023 let interrupt_line = result as u8; 86178bf93f0SYJwu2023 let interrupt_pin = (result >> 8) as u8; 86278bf93f0SYJwu2023 let bridge_control = (result >> 16) as u16; 86378bf93f0SYJwu2023 PciDeviceStructurePciToPciBridge { 86478bf93f0SYJwu2023 common_header, 865cc36cf4aSYJwu2023 irq_type: IrqType::Unused, 866cc36cf4aSYJwu2023 irq_vector: Vec::new(), 86778bf93f0SYJwu2023 bar0, 86878bf93f0SYJwu2023 bar1, 86978bf93f0SYJwu2023 primary_bus_number, 87078bf93f0SYJwu2023 secondary_bus_number, 87178bf93f0SYJwu2023 subordinate_bus_number, 87278bf93f0SYJwu2023 secondary_latency_timer, 87378bf93f0SYJwu2023 io_base, 87478bf93f0SYJwu2023 io_limit, 87578bf93f0SYJwu2023 secondary_status, 87678bf93f0SYJwu2023 memory_base, 87778bf93f0SYJwu2023 memory_limit, 87878bf93f0SYJwu2023 prefetchable_memory_base, 87978bf93f0SYJwu2023 prefetchable_memory_limit, 88078bf93f0SYJwu2023 prefetchable_base_upper_32_bits, 88178bf93f0SYJwu2023 prefetchable_limit_upper_32_bits, 88278bf93f0SYJwu2023 io_base_upper_16_bits, 88378bf93f0SYJwu2023 io_limit_upper_16_bits, 88478bf93f0SYJwu2023 capability_pointer, 88578bf93f0SYJwu2023 reserved0, 88678bf93f0SYJwu2023 reserved1, 88778bf93f0SYJwu2023 expansion_rom_base_address, 88878bf93f0SYJwu2023 interrupt_line, 88978bf93f0SYJwu2023 interrupt_pin, 89078bf93f0SYJwu2023 bridge_control, 89178bf93f0SYJwu2023 } 89278bf93f0SYJwu2023 } 89378bf93f0SYJwu2023 89478bf93f0SYJwu2023 /// @brief 读取type为0x2的pci设备的header 89578bf93f0SYJwu2023 /// 本函数只应被 pci_read_header()调用 89678bf93f0SYJwu2023 /// @param common_header 共有头部 89778bf93f0SYJwu2023 /// @param bus_device_function PCI设备的唯一标识 898cc36cf4aSYJwu2023 /// @return Pci_Device_Structure_Pci_to_Cardbus_Bridge pci-to-cardbus 桥设备头部 89978bf93f0SYJwu2023 fn pci_read_pci_to_cardbus_bridge_header( 90078bf93f0SYJwu2023 common_header: PciDeviceStructureHeader, 90178bf93f0SYJwu2023 busdevicefunction: &BusDeviceFunction, 90278bf93f0SYJwu2023 ) -> PciDeviceStructurePciToCardbusBridge { 9032709e017SLoGin let cardbus_socket_ex_ca_base_address = pci_root_0().read_config(*busdevicefunction, 0x10); 90478bf93f0SYJwu2023 9052709e017SLoGin let result = pci_root_0().read_config(*busdevicefunction, 0x14); 90678bf93f0SYJwu2023 let offset_of_capabilities_list = result as u8; 90778bf93f0SYJwu2023 let reserved = (result >> 8) as u8; 90878bf93f0SYJwu2023 let secondary_status = (result >> 16) as u16; 90978bf93f0SYJwu2023 9102709e017SLoGin let result = pci_root_0().read_config(*busdevicefunction, 0x18); 91178bf93f0SYJwu2023 let pci_bus_number = result as u8; 91278bf93f0SYJwu2023 let card_bus_bus_number = (result >> 8) as u8; 91378bf93f0SYJwu2023 let subordinate_bus_number = (result >> 16) as u8; 91478bf93f0SYJwu2023 let card_bus_latency_timer = (result >> 24) as u8; 91578bf93f0SYJwu2023 9162709e017SLoGin let memory_base_address0 = pci_root_0().read_config(*busdevicefunction, 0x1c); 9172709e017SLoGin let memory_limit0 = pci_root_0().read_config(*busdevicefunction, 0x20); 9182709e017SLoGin let memory_base_address1 = pci_root_0().read_config(*busdevicefunction, 0x24); 9192709e017SLoGin let memory_limit1 = pci_root_0().read_config(*busdevicefunction, 0x28); 92078bf93f0SYJwu2023 9212709e017SLoGin let io_base_address0 = pci_root_0().read_config(*busdevicefunction, 0x2c); 9222709e017SLoGin let io_limit0 = pci_root_0().read_config(*busdevicefunction, 0x30); 9232709e017SLoGin let io_base_address1 = pci_root_0().read_config(*busdevicefunction, 0x34); 9242709e017SLoGin let io_limit1 = pci_root_0().read_config(*busdevicefunction, 0x38); 9252709e017SLoGin let result = pci_root_0().read_config(*busdevicefunction, 0x3c); 92678bf93f0SYJwu2023 let interrupt_line = result as u8; 92778bf93f0SYJwu2023 let interrupt_pin = (result >> 8) as u8; 92878bf93f0SYJwu2023 let bridge_control = (result >> 16) as u16; 92978bf93f0SYJwu2023 9302709e017SLoGin let result = pci_root_0().read_config(*busdevicefunction, 0x40); 93178bf93f0SYJwu2023 let subsystem_device_id = result as u16; 93278bf93f0SYJwu2023 let subsystem_vendor_id = (result >> 16) as u16; 93378bf93f0SYJwu2023 9342709e017SLoGin let pc_card_legacy_mode_base_address_16_bit = 9352709e017SLoGin pci_root_0().read_config(*busdevicefunction, 0x44); 93678bf93f0SYJwu2023 PciDeviceStructurePciToCardbusBridge { 93778bf93f0SYJwu2023 common_header, 93878bf93f0SYJwu2023 cardbus_socket_ex_ca_base_address, 93978bf93f0SYJwu2023 offset_of_capabilities_list, 94078bf93f0SYJwu2023 reserved, 94178bf93f0SYJwu2023 secondary_status, 94278bf93f0SYJwu2023 pci_bus_number, 94378bf93f0SYJwu2023 card_bus_bus_number, 94478bf93f0SYJwu2023 subordinate_bus_number, 94578bf93f0SYJwu2023 card_bus_latency_timer, 94678bf93f0SYJwu2023 memory_base_address0, 94778bf93f0SYJwu2023 memory_limit0, 94878bf93f0SYJwu2023 memory_base_address1, 94978bf93f0SYJwu2023 memory_limit1, 95078bf93f0SYJwu2023 io_base_address0, 95178bf93f0SYJwu2023 io_limit0, 95278bf93f0SYJwu2023 io_base_address1, 95378bf93f0SYJwu2023 io_limit1, 95478bf93f0SYJwu2023 interrupt_line, 95578bf93f0SYJwu2023 interrupt_pin, 95678bf93f0SYJwu2023 bridge_control, 95778bf93f0SYJwu2023 subsystem_device_id, 95878bf93f0SYJwu2023 subsystem_vendor_id, 95978bf93f0SYJwu2023 pc_card_legacy_mode_base_address_16_bit, 96078bf93f0SYJwu2023 } 96178bf93f0SYJwu2023 } 96278bf93f0SYJwu2023 96378bf93f0SYJwu2023 /// @brief 检查所有bus上的设备并将其加入链表 96478bf93f0SYJwu2023 /// @return 成功返回ok(),失败返回失败原因 96578bf93f0SYJwu2023 fn pci_check_all_buses() -> Result<u8, PciError> { 966*2eab6dd7S曾俊 info!("Checking all devices in PCI bus..."); 96778bf93f0SYJwu2023 let busdevicefunction = BusDeviceFunction { 96878bf93f0SYJwu2023 bus: 0, 96978bf93f0SYJwu2023 device: 0, 97078bf93f0SYJwu2023 function: 0, 97178bf93f0SYJwu2023 }; 97278bf93f0SYJwu2023 let header = pci_read_header(busdevicefunction, false)?; 97378bf93f0SYJwu2023 let common_header = header.common_header(); 97478bf93f0SYJwu2023 pci_check_bus(0)?; 97578bf93f0SYJwu2023 if common_header.header_type & 0x80 != 0 { 97678bf93f0SYJwu2023 for function in 1..8 { 97778bf93f0SYJwu2023 pci_check_bus(function)?; 97878bf93f0SYJwu2023 } 97978bf93f0SYJwu2023 } 98078bf93f0SYJwu2023 Ok(0) 98178bf93f0SYJwu2023 } 98278bf93f0SYJwu2023 /// @brief 检查特定设备并将其加入链表 98378bf93f0SYJwu2023 /// @return 成功返回ok(),失败返回失败原因 98478bf93f0SYJwu2023 fn pci_check_function(busdevicefunction: BusDeviceFunction) -> Result<u8, PciError> { 985*2eab6dd7S曾俊 //debug!("PCI check function {}", busdevicefunction.function); 98678bf93f0SYJwu2023 let header = match pci_read_header(busdevicefunction, true) { 98778bf93f0SYJwu2023 Ok(header) => header, 98878bf93f0SYJwu2023 Err(PciError::GetWrongHeader) => { 98978bf93f0SYJwu2023 return Ok(255); 99078bf93f0SYJwu2023 } 99178bf93f0SYJwu2023 Err(e) => { 99278bf93f0SYJwu2023 return Err(e); 99378bf93f0SYJwu2023 } 99478bf93f0SYJwu2023 }; 99578bf93f0SYJwu2023 let common_header = header.common_header(); 99678bf93f0SYJwu2023 if (common_header.class_code == 0x06) 99778bf93f0SYJwu2023 && (common_header.subclass == 0x04 || common_header.subclass == 0x09) 99878bf93f0SYJwu2023 { 99978bf93f0SYJwu2023 let pci_to_pci_bridge = header 100078bf93f0SYJwu2023 .as_pci_to_pci_bridge_device() 100178bf93f0SYJwu2023 .ok_or(PciError::PciDeviceStructureTransformError)?; 100278bf93f0SYJwu2023 let secondary_bus = pci_to_pci_bridge.secondary_bus_number; 100378bf93f0SYJwu2023 pci_check_bus(secondary_bus)?; 100478bf93f0SYJwu2023 } 100578bf93f0SYJwu2023 Ok(0) 100678bf93f0SYJwu2023 } 100778bf93f0SYJwu2023 100878bf93f0SYJwu2023 /// @brief 检查device上的设备并将其加入链表 100978bf93f0SYJwu2023 /// @return 成功返回ok(),失败返回失败原因 101078bf93f0SYJwu2023 fn pci_check_device(bus: u8, device: u8) -> Result<u8, PciError> { 1011*2eab6dd7S曾俊 //debug!("PCI check device {}", device); 101278bf93f0SYJwu2023 let busdevicefunction = BusDeviceFunction { 101378bf93f0SYJwu2023 bus, 101478bf93f0SYJwu2023 device, 101578bf93f0SYJwu2023 function: 0, 101678bf93f0SYJwu2023 }; 101778bf93f0SYJwu2023 let header = match pci_read_header(busdevicefunction, false) { 101878bf93f0SYJwu2023 Ok(header) => header, 101978bf93f0SYJwu2023 Err(PciError::GetWrongHeader) => { 102078bf93f0SYJwu2023 //设备不存在,直接返回即可,不用终止遍历 102178bf93f0SYJwu2023 return Ok(255); 102278bf93f0SYJwu2023 } 102378bf93f0SYJwu2023 Err(e) => { 102478bf93f0SYJwu2023 return Err(e); 102578bf93f0SYJwu2023 } 102678bf93f0SYJwu2023 }; 102778bf93f0SYJwu2023 pci_check_function(busdevicefunction)?; 102878bf93f0SYJwu2023 let common_header = header.common_header(); 102978bf93f0SYJwu2023 if common_header.header_type & 0x80 != 0 { 1030*2eab6dd7S曾俊 debug!( 103178bf93f0SYJwu2023 "Detected multi func device in bus{},device{}", 1032*2eab6dd7S曾俊 busdevicefunction.bus, busdevicefunction.device 103378bf93f0SYJwu2023 ); 103478bf93f0SYJwu2023 // 这是一个多function的设备,因此查询剩余的function 103578bf93f0SYJwu2023 for function in 1..8 { 103678bf93f0SYJwu2023 let busdevicefunction = BusDeviceFunction { 103778bf93f0SYJwu2023 bus, 103878bf93f0SYJwu2023 device, 103978bf93f0SYJwu2023 function, 104078bf93f0SYJwu2023 }; 104178bf93f0SYJwu2023 pci_check_function(busdevicefunction)?; 104278bf93f0SYJwu2023 } 104378bf93f0SYJwu2023 } 104478bf93f0SYJwu2023 Ok(0) 104578bf93f0SYJwu2023 } 104678bf93f0SYJwu2023 /// @brief 检查该bus上的设备并将其加入链表 104778bf93f0SYJwu2023 /// @return 成功返回ok(),失败返回失败原因 104878bf93f0SYJwu2023 fn pci_check_bus(bus: u8) -> Result<u8, PciError> { 1049*2eab6dd7S曾俊 //debug!("PCI check bus {}", bus); 105078bf93f0SYJwu2023 for device in 0..32 { 105178bf93f0SYJwu2023 pci_check_device(bus, device)?; 105278bf93f0SYJwu2023 } 105378bf93f0SYJwu2023 Ok(0) 105478bf93f0SYJwu2023 } 10555b59005fSLoGin 10565b59005fSLoGin /// pci初始化函数 10575b59005fSLoGin #[inline(never)] 105878bf93f0SYJwu2023 pub fn pci_init() { 1059*2eab6dd7S曾俊 info!("Initializing PCI bus..."); 10601f4877a4S曾俊 pci_bus_subsys_init().expect("Failed to init pci bus subsystem"); 106178bf93f0SYJwu2023 if let Err(e) = pci_check_all_buses() { 1062*2eab6dd7S曾俊 error!("pci init failed when checking bus because of error: {}", e); 106378bf93f0SYJwu2023 return; 106478bf93f0SYJwu2023 } 1065*2eab6dd7S曾俊 info!( 106678bf93f0SYJwu2023 "Total pci device and function num = {}", 106778bf93f0SYJwu2023 PCI_DEVICE_LINKEDLIST.num() 106878bf93f0SYJwu2023 ); 106978bf93f0SYJwu2023 let list = PCI_DEVICE_LINKEDLIST.read(); 107078bf93f0SYJwu2023 for box_pci_device in list.iter() { 107178bf93f0SYJwu2023 let common_header = box_pci_device.common_header(); 107278bf93f0SYJwu2023 match box_pci_device.header_type() { 107378bf93f0SYJwu2023 HeaderType::Standard if common_header.status & 0x10 != 0 => { 1074*2eab6dd7S曾俊 info!("Found pci standard device with class code ={} subclass={} status={:#x} cap_pointer={:#x} vendor={:#x}, device id={:#x},bdf={}", common_header.class_code, common_header.subclass, common_header.status, box_pci_device.as_standard_device().unwrap().capabilities_pointer,common_header.vendor_id, common_header.device_id,common_header.bus_device_function); 107578bf93f0SYJwu2023 } 107678bf93f0SYJwu2023 HeaderType::Standard => { 1077*2eab6dd7S曾俊 info!( 107878bf93f0SYJwu2023 "Found pci standard device with class code ={} subclass={} status={:#x} ", 1079*2eab6dd7S曾俊 common_header.class_code, common_header.subclass, common_header.status 108078bf93f0SYJwu2023 ); 108178bf93f0SYJwu2023 } 108278bf93f0SYJwu2023 HeaderType::PciPciBridge if common_header.status & 0x10 != 0 => { 1083*2eab6dd7S曾俊 info!("Found pci-to-pci bridge device with class code ={} subclass={} status={:#x} cap_pointer={:#x}", common_header.class_code, common_header.subclass, common_header.status, box_pci_device.as_pci_to_pci_bridge_device().unwrap().capability_pointer); 108478bf93f0SYJwu2023 } 108578bf93f0SYJwu2023 HeaderType::PciPciBridge => { 1086*2eab6dd7S曾俊 info!( 108778bf93f0SYJwu2023 "Found pci-to-pci bridge device with class code ={} subclass={} status={:#x} ", 1088*2eab6dd7S曾俊 common_header.class_code, common_header.subclass, common_header.status 108978bf93f0SYJwu2023 ); 109078bf93f0SYJwu2023 } 109178bf93f0SYJwu2023 HeaderType::PciCardbusBridge => { 1092*2eab6dd7S曾俊 info!( 109378bf93f0SYJwu2023 "Found pcicardbus bridge device with class code ={} subclass={} status={:#x} ", 1094*2eab6dd7S曾俊 common_header.class_code, common_header.subclass, common_header.status 109578bf93f0SYJwu2023 ); 109678bf93f0SYJwu2023 } 109778bf93f0SYJwu2023 HeaderType::Unrecognised(_) => {} 109878bf93f0SYJwu2023 } 109978bf93f0SYJwu2023 } 11001f4877a4S曾俊 1101*2eab6dd7S曾俊 info!("PCI bus initialized."); 110278bf93f0SYJwu2023 } 110378bf93f0SYJwu2023 110426d84a31SYJwu2023 /// An identifier for a PCI bus, device and function. 110526d84a31SYJwu2023 /// PCI设备的唯一标识 110626d84a31SYJwu2023 #[derive(Copy, Clone, Debug, Eq, PartialEq)] 110778bf93f0SYJwu2023 pub struct BusDeviceFunction { 110826d84a31SYJwu2023 /// The PCI bus number, between 0 and 255. 110926d84a31SYJwu2023 pub bus: u8, 111026d84a31SYJwu2023 /// The device number on the bus, between 0 and 31. 111126d84a31SYJwu2023 pub device: u8, 111226d84a31SYJwu2023 /// The function number of the device, between 0 and 7. 111326d84a31SYJwu2023 pub function: u8, 111426d84a31SYJwu2023 } 111578bf93f0SYJwu2023 impl BusDeviceFunction { 111626d84a31SYJwu2023 /// Returns whether the device and function numbers are valid, i.e. the device is between 0 and 111778bf93f0SYJwu2023 ///@brief 检测BusDeviceFunction实例是否有效 111826d84a31SYJwu2023 ///@param self 111926d84a31SYJwu2023 ///@return bool 是否有效 112073c607aaSYJwu2023 #[allow(dead_code)] 112126d84a31SYJwu2023 pub fn valid(&self) -> bool { 112226d84a31SYJwu2023 self.device < 32 && self.function < 8 112326d84a31SYJwu2023 } 112426d84a31SYJwu2023 } 11251f4877a4S曾俊 11261f4877a4S曾俊 impl From<BusDeviceFunction> for String { 11271f4877a4S曾俊 /// # 函数的功能 11281f4877a4S曾俊 /// 这里提供一个由BusDeviceFunction到dddd:bb:vv.f字符串的转换函数,主要用于转换成设备的名称(pci设备的名称一般是诸如0000:00:00.1这种) 11291f4877a4S曾俊 fn from(value: BusDeviceFunction) -> Self { 11301f4877a4S曾俊 //需要注意,这里的0000应该是所谓的“域号”(Domain ID),但是尚不知道是如何获得的,故硬编码在这里 11311f4877a4S曾俊 //todo:实现域号的获取 11321f4877a4S曾俊 format!( 11331f4877a4S曾俊 "0000:{:02x}:{:02x}.{}", 11341f4877a4S曾俊 value.bus, value.device, value.function 11351f4877a4S曾俊 ) 11361f4877a4S曾俊 } 11371f4877a4S曾俊 } 113878bf93f0SYJwu2023 ///实现BusDeviceFunction的Display trait,使其可以直接输出 113978bf93f0SYJwu2023 impl Display for BusDeviceFunction { 114026d84a31SYJwu2023 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 11415c1e552cSYJwu2023 write!( 11425c1e552cSYJwu2023 f, 11435c1e552cSYJwu2023 "bus {} device {} function{}", 11445c1e552cSYJwu2023 self.bus, self.device, self.function 11455c1e552cSYJwu2023 ) 114626d84a31SYJwu2023 } 114726d84a31SYJwu2023 } 114826d84a31SYJwu2023 /// The location allowed for a memory BAR. 114926d84a31SYJwu2023 /// memory BAR的三种情况 115026d84a31SYJwu2023 #[derive(Copy, Clone, Debug, Eq, PartialEq)] 115126d84a31SYJwu2023 pub enum MemoryBarType { 115226d84a31SYJwu2023 /// The BAR has a 32-bit address and can be mapped anywhere in 32-bit address space. 115326d84a31SYJwu2023 Width32, 115426d84a31SYJwu2023 /// The BAR must be mapped below 1MiB. 115526d84a31SYJwu2023 Below1MiB, 115626d84a31SYJwu2023 /// The BAR has a 64-bit address and can be mapped anywhere in 64-bit address space. 115726d84a31SYJwu2023 Width64, 115826d84a31SYJwu2023 } 115926d84a31SYJwu2023 ///实现MemoryBarType与u8的类型转换 116026d84a31SYJwu2023 impl From<MemoryBarType> for u8 { 116126d84a31SYJwu2023 fn from(bar_type: MemoryBarType) -> Self { 116226d84a31SYJwu2023 match bar_type { 116326d84a31SYJwu2023 MemoryBarType::Width32 => 0, 116426d84a31SYJwu2023 MemoryBarType::Below1MiB => 1, 116526d84a31SYJwu2023 MemoryBarType::Width64 => 2, 116626d84a31SYJwu2023 } 116726d84a31SYJwu2023 } 116826d84a31SYJwu2023 } 116926d84a31SYJwu2023 ///实现MemoryBarType与u8的类型转换 117026d84a31SYJwu2023 impl TryFrom<u8> for MemoryBarType { 117126d84a31SYJwu2023 type Error = PciError; 117226d84a31SYJwu2023 fn try_from(value: u8) -> Result<Self, Self::Error> { 117326d84a31SYJwu2023 match value { 117426d84a31SYJwu2023 0 => Ok(Self::Width32), 117526d84a31SYJwu2023 1 => Ok(Self::Below1MiB), 117626d84a31SYJwu2023 2 => Ok(Self::Width64), 117726d84a31SYJwu2023 _ => Err(PciError::InvalidBarType), 117826d84a31SYJwu2023 } 117926d84a31SYJwu2023 } 118026d84a31SYJwu2023 } 118126d84a31SYJwu2023 118226d84a31SYJwu2023 /// Information about a PCI Base Address Register. 118326d84a31SYJwu2023 /// BAR的三种类型 Memory/IO/Unused 11842dd9f0c7SLoGin #[derive(Clone, Debug)] 118526d84a31SYJwu2023 pub enum BarInfo { 118626d84a31SYJwu2023 /// The BAR is for a memory region. 118726d84a31SYJwu2023 Memory { 118826d84a31SYJwu2023 /// The size of the BAR address and where it can be located. 118926d84a31SYJwu2023 address_type: MemoryBarType, 119026d84a31SYJwu2023 /// If true, then reading from the region doesn't have side effects. The CPU may cache reads 119126d84a31SYJwu2023 /// and merge repeated stores. 119226d84a31SYJwu2023 prefetchable: bool, 119326d84a31SYJwu2023 /// The memory address, always 16-byte aligned. 119426d84a31SYJwu2023 address: u64, 119526d84a31SYJwu2023 /// The size of the BAR in bytes. 119626d84a31SYJwu2023 size: u32, 119726d84a31SYJwu2023 /// The virtaddress for a memory bar(mapped). 11982dd9f0c7SLoGin mmio_guard: Arc<MMIOSpaceGuard>, 119926d84a31SYJwu2023 }, 120026d84a31SYJwu2023 /// The BAR is for an I/O region. 120126d84a31SYJwu2023 IO { 120226d84a31SYJwu2023 /// The I/O address, always 4-byte aligned. 120326d84a31SYJwu2023 address: u32, 120426d84a31SYJwu2023 /// The size of the BAR in bytes. 120526d84a31SYJwu2023 size: u32, 120626d84a31SYJwu2023 }, 120726d84a31SYJwu2023 Unused, 120826d84a31SYJwu2023 } 120926d84a31SYJwu2023 121026d84a31SYJwu2023 impl BarInfo { 121126d84a31SYJwu2023 /// Returns the address and size of this BAR if it is a memory bar, or `None` if it is an IO 121226d84a31SYJwu2023 /// BAR. 121326d84a31SYJwu2023 ///@brief 得到某个bar的memory_address与size(前提是他的类型为Memory Bar) 121426d84a31SYJwu2023 ///@param self 121526d84a31SYJwu2023 ///@return Option<(u64, u32) 是Memory Bar返回内存地址与大小,不是则返回None 121626d84a31SYJwu2023 pub fn memory_address_size(&self) -> Option<(u64, u32)> { 121726d84a31SYJwu2023 if let Self::Memory { address, size, .. } = self { 121826d84a31SYJwu2023 Some((*address, *size)) 121926d84a31SYJwu2023 } else { 122026d84a31SYJwu2023 None 122126d84a31SYJwu2023 } 122226d84a31SYJwu2023 } 122326d84a31SYJwu2023 ///@brief 得到某个bar的virtaddress(前提是他的类型为Memory Bar) 122426d84a31SYJwu2023 ///@param self 122526d84a31SYJwu2023 ///@return Option<(u64) 是Memory Bar返回映射的虚拟地址,不是则返回None 12262dd9f0c7SLoGin pub fn virtual_address(&self) -> Option<VirtAddr> { 12272dd9f0c7SLoGin if let Self::Memory { mmio_guard, .. } = self { 12282dd9f0c7SLoGin Some(mmio_guard.vaddr()) 122926d84a31SYJwu2023 } else { 123026d84a31SYJwu2023 None 123126d84a31SYJwu2023 } 123226d84a31SYJwu2023 } 123326d84a31SYJwu2023 } 123478bf93f0SYJwu2023 ///实现BarInfo的Display trait,自定义输出 123526d84a31SYJwu2023 impl Display for BarInfo { 123626d84a31SYJwu2023 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 123726d84a31SYJwu2023 match self { 123826d84a31SYJwu2023 Self::Memory { 123926d84a31SYJwu2023 address_type, 124026d84a31SYJwu2023 prefetchable, 124126d84a31SYJwu2023 address, 124226d84a31SYJwu2023 size, 12432dd9f0c7SLoGin mmio_guard, 124426d84a31SYJwu2023 } => write!( 124526d84a31SYJwu2023 f, 12462dd9f0c7SLoGin "Memory space at {:#010x}, size {}, type {:?}, prefetchable {}, mmio_guard: {:?}", 12472dd9f0c7SLoGin address, size, address_type, prefetchable, mmio_guard 124826d84a31SYJwu2023 ), 124926d84a31SYJwu2023 Self::IO { address, size } => { 125026d84a31SYJwu2023 write!(f, "I/O space at {:#010x}, size {}", address, size) 125126d84a31SYJwu2023 } 125226d84a31SYJwu2023 Self::Unused => { 125326d84a31SYJwu2023 write!(f, "Unused bar") 125426d84a31SYJwu2023 } 125526d84a31SYJwu2023 } 125626d84a31SYJwu2023 } 125726d84a31SYJwu2023 } 1258cc36cf4aSYJwu2023 // todo 增加对桥的bar的支持 1259cc36cf4aSYJwu2023 pub trait PciDeviceBar {} 126040fe15e0SLoGin 126178bf93f0SYJwu2023 ///一个普通PCI设备(非桥)有6个BAR寄存器,PciStandardDeviceBar存储其全部信息 12622dd9f0c7SLoGin #[derive(Clone, Debug)] 126378bf93f0SYJwu2023 pub struct PciStandardDeviceBar { 126426d84a31SYJwu2023 bar0: BarInfo, 126526d84a31SYJwu2023 bar1: BarInfo, 126626d84a31SYJwu2023 bar2: BarInfo, 126726d84a31SYJwu2023 bar3: BarInfo, 126826d84a31SYJwu2023 bar4: BarInfo, 126926d84a31SYJwu2023 bar5: BarInfo, 127026d84a31SYJwu2023 } 127126d84a31SYJwu2023 127278bf93f0SYJwu2023 impl PciStandardDeviceBar { 127326d84a31SYJwu2023 ///@brief 得到某个bar的barinfo 127426d84a31SYJwu2023 ///@param self ,bar_index(0-5) 127526d84a31SYJwu2023 ///@return Result<&BarInfo, PciError> bar_index在0-5则返回对应的bar_info结构体,超出范围则返回错误 127626d84a31SYJwu2023 pub fn get_bar(&self, bar_index: u8) -> Result<&BarInfo, PciError> { 127726d84a31SYJwu2023 match bar_index { 127826d84a31SYJwu2023 0 => Ok(&self.bar0), 127926d84a31SYJwu2023 1 => Ok(&self.bar1), 128026d84a31SYJwu2023 2 => Ok(&self.bar2), 128126d84a31SYJwu2023 3 => Ok(&self.bar3), 128226d84a31SYJwu2023 4 => Ok(&self.bar4), 128378bf93f0SYJwu2023 5 => Ok(&self.bar5), 128426d84a31SYJwu2023 _ => Err(PciError::InvalidBarType), 128526d84a31SYJwu2023 } 128626d84a31SYJwu2023 } 128726d84a31SYJwu2023 } 128878bf93f0SYJwu2023 ///实现PciStandardDeviceBar的Display trait,使其可以直接输出 128978bf93f0SYJwu2023 impl Display for PciStandardDeviceBar { 129026d84a31SYJwu2023 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 129126d84a31SYJwu2023 write!( 129226d84a31SYJwu2023 f, 129326d84a31SYJwu2023 "\r\nBar0:{}\r\nBar1:{}\r\nBar2:{}\r\nBar3:{}\r\nBar4:{}\r\nBar5:{}", 129426d84a31SYJwu2023 self.bar0, self.bar1, self.bar2, self.bar3, self.bar4, self.bar5 129526d84a31SYJwu2023 ) 129626d84a31SYJwu2023 } 129726d84a31SYJwu2023 } 129878bf93f0SYJwu2023 ///实现PciStandardDeviceBar的Default trait,使其可以简单初始化 129978bf93f0SYJwu2023 impl Default for PciStandardDeviceBar { 130026d84a31SYJwu2023 fn default() -> Self { 130178bf93f0SYJwu2023 PciStandardDeviceBar { 130226d84a31SYJwu2023 bar0: BarInfo::Unused, 130326d84a31SYJwu2023 bar1: BarInfo::Unused, 130426d84a31SYJwu2023 bar2: BarInfo::Unused, 130526d84a31SYJwu2023 bar3: BarInfo::Unused, 130626d84a31SYJwu2023 bar4: BarInfo::Unused, 130726d84a31SYJwu2023 bar5: BarInfo::Unused, 130826d84a31SYJwu2023 } 130926d84a31SYJwu2023 } 131026d84a31SYJwu2023 } 131126d84a31SYJwu2023 131278bf93f0SYJwu2023 ///@brief 将某个pci设备的bar寄存器读取值后映射到虚拟地址 131378bf93f0SYJwu2023 ///@param self ,bus_device_function PCI设备的唯一标识符 131478bf93f0SYJwu2023 ///@return Result<PciStandardDeviceBar, PciError> 成功则返回对应的PciStandardDeviceBar结构体,失败则返回错误类型 131578bf93f0SYJwu2023 pub fn pci_bar_init( 131678bf93f0SYJwu2023 bus_device_function: BusDeviceFunction, 131778bf93f0SYJwu2023 ) -> Result<PciStandardDeviceBar, PciError> { 131878bf93f0SYJwu2023 let mut device_bar: PciStandardDeviceBar = PciStandardDeviceBar::default(); 131926d84a31SYJwu2023 let mut bar_index_ignore: u8 = 255; 132026d84a31SYJwu2023 for bar_index in 0..6 { 132126d84a31SYJwu2023 if bar_index == bar_index_ignore { 132226d84a31SYJwu2023 continue; 132326d84a31SYJwu2023 } 132426d84a31SYJwu2023 let bar_info; 13252709e017SLoGin let bar_orig = 13262709e017SLoGin pci_root_0().read_config(bus_device_function, (BAR0_OFFSET + 4 * bar_index).into()); 13272709e017SLoGin pci_root_0().write_config( 13282709e017SLoGin bus_device_function, 13292709e017SLoGin (BAR0_OFFSET + 4 * bar_index).into(), 133026d84a31SYJwu2023 0xffffffff, 133126d84a31SYJwu2023 ); 13322709e017SLoGin let size_mask = 13332709e017SLoGin pci_root_0().read_config(bus_device_function, (BAR0_OFFSET + 4 * bar_index).into()); 133426d84a31SYJwu2023 // A wrapping add is necessary to correctly handle the case of unused BARs, which read back 133526d84a31SYJwu2023 // as 0, and should be treated as size 0. 133626d84a31SYJwu2023 let size = (!(size_mask & 0xfffffff0)).wrapping_add(1); 1337*2eab6dd7S曾俊 //debug!("bar_orig:{:#x},size: {:#x}", bar_orig,size); 133826d84a31SYJwu2023 // Restore the original value. 13392709e017SLoGin pci_root_0().write_config( 13402709e017SLoGin bus_device_function, 13412709e017SLoGin (BAR0_OFFSET + 4 * bar_index).into(), 13422709e017SLoGin bar_orig, 13432709e017SLoGin ); 134426d84a31SYJwu2023 if size == 0 { 134526d84a31SYJwu2023 continue; 134626d84a31SYJwu2023 } 134726d84a31SYJwu2023 if bar_orig & 0x00000001 == 0x00000001 { 134826d84a31SYJwu2023 // I/O space 134926d84a31SYJwu2023 let address = bar_orig & 0xfffffffc; 135026d84a31SYJwu2023 bar_info = BarInfo::IO { address, size }; 135126d84a31SYJwu2023 } else { 135226d84a31SYJwu2023 // Memory space 135326d84a31SYJwu2023 let mut address = u64::from(bar_orig & 0xfffffff0); 135426d84a31SYJwu2023 let prefetchable = bar_orig & 0x00000008 != 0; 135526d84a31SYJwu2023 let address_type = MemoryBarType::try_from(((bar_orig & 0x00000006) >> 1) as u8)?; 135626d84a31SYJwu2023 if address_type == MemoryBarType::Width64 { 135726d84a31SYJwu2023 if bar_index >= 5 { 135826d84a31SYJwu2023 return Err(PciError::InvalidBarType); 135926d84a31SYJwu2023 } 13602709e017SLoGin let address_top = pci_root_0().read_config( 13612709e017SLoGin bus_device_function, 13622709e017SLoGin (BAR0_OFFSET + 4 * (bar_index + 1)).into(), 13632709e017SLoGin ); 136426d84a31SYJwu2023 address |= u64::from(address_top) << 32; 136526d84a31SYJwu2023 bar_index_ignore = bar_index + 1; //下个bar跳过,因为64位的memory bar覆盖了两个bar 136626d84a31SYJwu2023 } 13675c1e552cSYJwu2023 let pci_address = PciAddr::new(address as usize); 13682dd9f0c7SLoGin let paddr = PciArch::address_pci_to_physical(pci_address); //PCI总线域物理地址转换为存储器域物理地址 13692dd9f0c7SLoGin 13702dd9f0c7SLoGin let space_guard: Arc<MMIOSpaceGuard>; 137126d84a31SYJwu2023 unsafe { 137240fe15e0SLoGin let size_want = size as usize; 13732dd9f0c7SLoGin let tmp = mmio_pool() 13742dd9f0c7SLoGin .create_mmio(size_want) 13752dd9f0c7SLoGin .map_err(|_| PciError::CreateMmioError)?; 13762dd9f0c7SLoGin space_guard = Arc::new(tmp); 1377*2eab6dd7S曾俊 //debug!("Pci bar init: mmio space: {space_guard:?}, paddr={paddr:?}, size_want={size_want}"); 13782dd9f0c7SLoGin assert!( 13797ae679ddSLoGin space_guard.map_phys(paddr, size_want).is_ok(), 13802dd9f0c7SLoGin "pci_bar_init: map_phys failed" 13812dd9f0c7SLoGin ); 138226d84a31SYJwu2023 } 138326d84a31SYJwu2023 bar_info = BarInfo::Memory { 138426d84a31SYJwu2023 address_type, 138526d84a31SYJwu2023 prefetchable, 138626d84a31SYJwu2023 address, 138726d84a31SYJwu2023 size, 13882dd9f0c7SLoGin mmio_guard: space_guard, 138926d84a31SYJwu2023 }; 139026d84a31SYJwu2023 } 139126d84a31SYJwu2023 match bar_index { 139226d84a31SYJwu2023 0 => { 139326d84a31SYJwu2023 device_bar.bar0 = bar_info; 139426d84a31SYJwu2023 } 139526d84a31SYJwu2023 1 => { 139626d84a31SYJwu2023 device_bar.bar1 = bar_info; 139726d84a31SYJwu2023 } 139826d84a31SYJwu2023 2 => { 139926d84a31SYJwu2023 device_bar.bar2 = bar_info; 140026d84a31SYJwu2023 } 140126d84a31SYJwu2023 3 => { 140226d84a31SYJwu2023 device_bar.bar3 = bar_info; 140326d84a31SYJwu2023 } 140426d84a31SYJwu2023 4 => { 140526d84a31SYJwu2023 device_bar.bar4 = bar_info; 140626d84a31SYJwu2023 } 140726d84a31SYJwu2023 5 => { 140826d84a31SYJwu2023 device_bar.bar5 = bar_info; 140926d84a31SYJwu2023 } 141026d84a31SYJwu2023 _ => {} 141126d84a31SYJwu2023 } 141226d84a31SYJwu2023 } 1413*2eab6dd7S曾俊 //debug!("pci_device_bar:{}", device_bar); 141426d84a31SYJwu2023 return Ok(device_bar); 141526d84a31SYJwu2023 } 141626d84a31SYJwu2023 141726d84a31SYJwu2023 /// Information about a PCI device capability. 141826d84a31SYJwu2023 /// PCI设备的capability的信息 141926d84a31SYJwu2023 #[derive(Debug, Copy, Clone, Eq, PartialEq)] 142026d84a31SYJwu2023 pub struct CapabilityInfo { 142126d84a31SYJwu2023 /// The offset of the capability in the PCI configuration space of the device function. 142226d84a31SYJwu2023 pub offset: u8, 142326d84a31SYJwu2023 /// The ID of the capability. 142426d84a31SYJwu2023 pub id: u8, 142526d84a31SYJwu2023 /// The third and fourth bytes of the capability, to save reading them again. 142626d84a31SYJwu2023 pub private_header: u16, 142726d84a31SYJwu2023 } 142873c607aaSYJwu2023 142926d84a31SYJwu2023 /// Iterator over capabilities for a device. 143026d84a31SYJwu2023 /// 创建迭代器以遍历PCI设备的capability 143126d84a31SYJwu2023 #[derive(Debug)] 143226d84a31SYJwu2023 pub struct CapabilityIterator { 143378bf93f0SYJwu2023 pub bus_device_function: BusDeviceFunction, 143426d84a31SYJwu2023 pub next_capability_offset: Option<u8>, 143526d84a31SYJwu2023 } 143626d84a31SYJwu2023 143726d84a31SYJwu2023 impl Iterator for CapabilityIterator { 143826d84a31SYJwu2023 type Item = CapabilityInfo; 143926d84a31SYJwu2023 fn next(&mut self) -> Option<Self::Item> { 144026d84a31SYJwu2023 let offset = self.next_capability_offset?; 144126d84a31SYJwu2023 144226d84a31SYJwu2023 // Read the first 4 bytes of the capability. 14432709e017SLoGin let capability_header = pci_root_0().read_config(self.bus_device_function, offset.into()); 144426d84a31SYJwu2023 let id = capability_header as u8; 144526d84a31SYJwu2023 let next_offset = (capability_header >> 8) as u8; 144626d84a31SYJwu2023 let private_header = (capability_header >> 16) as u16; 144726d84a31SYJwu2023 144826d84a31SYJwu2023 self.next_capability_offset = if next_offset == 0 { 144926d84a31SYJwu2023 None 145026d84a31SYJwu2023 } else if next_offset < 64 || next_offset & 0x3 != 0 { 1451*2eab6dd7S曾俊 warn!("Invalid next capability offset {:#04x}", next_offset); 145226d84a31SYJwu2023 None 145326d84a31SYJwu2023 } else { 145426d84a31SYJwu2023 Some(next_offset) 145526d84a31SYJwu2023 }; 145626d84a31SYJwu2023 145726d84a31SYJwu2023 Some(CapabilityInfo { 145826d84a31SYJwu2023 offset, 145926d84a31SYJwu2023 id, 146026d84a31SYJwu2023 private_header, 146126d84a31SYJwu2023 }) 146226d84a31SYJwu2023 } 146326d84a31SYJwu2023 } 146473c607aaSYJwu2023 146578bf93f0SYJwu2023 /// Information about a PCIe device capability. 146678bf93f0SYJwu2023 /// PCIe设备的external capability的信息 146778bf93f0SYJwu2023 #[derive(Debug, Copy, Clone, Eq, PartialEq)] 146878bf93f0SYJwu2023 pub struct ExternalCapabilityInfo { 146978bf93f0SYJwu2023 /// The offset of the capability in the PCI configuration space of the device function. 147078bf93f0SYJwu2023 pub offset: u16, 147178bf93f0SYJwu2023 /// The ID of the capability. 147278bf93f0SYJwu2023 pub id: u16, 147378bf93f0SYJwu2023 /// The third and fourth bytes of the capability, to save reading them again. 147478bf93f0SYJwu2023 pub capability_version: u8, 147573c607aaSYJwu2023 } 147678bf93f0SYJwu2023 147778bf93f0SYJwu2023 /// Iterator over capabilities for a device. 147878bf93f0SYJwu2023 /// 创建迭代器以遍历PCIe设备的external capability 147978bf93f0SYJwu2023 #[derive(Debug)] 148078bf93f0SYJwu2023 pub struct ExternalCapabilityIterator<'a> { 148178bf93f0SYJwu2023 pub root: &'a PciRoot, 148278bf93f0SYJwu2023 pub bus_device_function: BusDeviceFunction, 148378bf93f0SYJwu2023 pub next_capability_offset: Option<u16>, 148473c607aaSYJwu2023 } 148578bf93f0SYJwu2023 impl<'a> Iterator for ExternalCapabilityIterator<'a> { 148678bf93f0SYJwu2023 type Item = ExternalCapabilityInfo; 148778bf93f0SYJwu2023 fn next(&mut self) -> Option<Self::Item> { 148878bf93f0SYJwu2023 let offset = self.next_capability_offset?; 148978bf93f0SYJwu2023 149078bf93f0SYJwu2023 // Read the first 4 bytes of the capability. 149178bf93f0SYJwu2023 let capability_header = self.root.read_config(self.bus_device_function, offset); 149278bf93f0SYJwu2023 let id = capability_header as u16; 149378bf93f0SYJwu2023 let next_offset = (capability_header >> 20) as u16; 149478bf93f0SYJwu2023 let capability_version = ((capability_header >> 16) & 0xf) as u8; 149578bf93f0SYJwu2023 149678bf93f0SYJwu2023 self.next_capability_offset = if next_offset == 0 { 149778bf93f0SYJwu2023 None 149878bf93f0SYJwu2023 } else if next_offset < 0x100 || next_offset & 0x3 != 0 { 1499*2eab6dd7S曾俊 warn!("Invalid next capability offset {:#04x}", next_offset); 150078bf93f0SYJwu2023 None 150178bf93f0SYJwu2023 } else { 150278bf93f0SYJwu2023 Some(next_offset) 150378bf93f0SYJwu2023 }; 150478bf93f0SYJwu2023 150578bf93f0SYJwu2023 Some(ExternalCapabilityInfo { 150678bf93f0SYJwu2023 offset, 150778bf93f0SYJwu2023 id, 150878bf93f0SYJwu2023 capability_version, 150978bf93f0SYJwu2023 }) 151078bf93f0SYJwu2023 } 151173c607aaSYJwu2023 } 1512