xref: /DragonOS/kernel/src/driver/pci/subsys.rs (revision 28fe4ad2a0b0d8b5abf1f0cb402b1c3204b42242)
11f4877a4S曾俊 use alloc::{
21f4877a4S曾俊     string::{String, ToString},
31f4877a4S曾俊     sync::{Arc, Weak},
41f4877a4S曾俊 };
51f4877a4S曾俊 use intertrait::cast::CastArc;
62eab6dd7S曾俊 use log::error;
71f4877a4S曾俊 use system_error::SystemError;
81f4877a4S曾俊 
91f4877a4S曾俊 use crate::{
101f4877a4S曾俊     driver::base::{
111f4877a4S曾俊         device::{
121f4877a4S曾俊             bus::{bus_register, Bus},
131f4877a4S曾俊             device_register,
141f4877a4S曾俊             driver::Driver,
151f4877a4S曾俊             sys_devices_kset, Device,
161f4877a4S曾俊         },
171f4877a4S曾俊         kobject::KObject,
181f4877a4S曾俊         subsys::SubSysPrivate,
191f4877a4S曾俊     },
201f4877a4S曾俊     filesystem::sysfs::AttributeGroup,
211f4877a4S曾俊 };
221f4877a4S曾俊 
231f4877a4S曾俊 use super::{
241f4877a4S曾俊     device::{PciBusDevice, PciDevice},
251f4877a4S曾俊     driver::PciDriver,
261f4877a4S曾俊     test::pt_init,
271f4877a4S曾俊 };
281f4877a4S曾俊 
291f4877a4S曾俊 static mut PCI_BUS_DEVICE: Option<Arc<PciBusDevice>> = None;
301f4877a4S曾俊 static mut PCI_BUS: Option<Arc<PciBus>> = None;
311f4877a4S曾俊 
set_pci_bus_device(device: Arc<PciBusDevice>)321f4877a4S曾俊 pub(super) fn set_pci_bus_device(device: Arc<PciBusDevice>) {
331f4877a4S曾俊     unsafe {
341f4877a4S曾俊         PCI_BUS_DEVICE = Some(device);
351f4877a4S曾俊     }
361f4877a4S曾俊 }
371f4877a4S曾俊 
set_pci_bus(bus: Arc<PciBus>)381f4877a4S曾俊 pub(super) fn set_pci_bus(bus: Arc<PciBus>) {
391f4877a4S曾俊     unsafe {
401f4877a4S曾俊         PCI_BUS = Some(bus);
411f4877a4S曾俊     }
421f4877a4S曾俊 }
431f4877a4S曾俊 
pci_bus_device() -> Arc<PciBusDevice>441f4877a4S曾俊 pub fn pci_bus_device() -> Arc<PciBusDevice> {
451f4877a4S曾俊     unsafe {
461f4877a4S曾俊         return PCI_BUS_DEVICE.clone().unwrap();
471f4877a4S曾俊     }
481f4877a4S曾俊 }
491f4877a4S曾俊 
pci_bus() -> Arc<PciBus>501f4877a4S曾俊 pub fn pci_bus() -> Arc<PciBus> {
511f4877a4S曾俊     unsafe {
521f4877a4S曾俊         return PCI_BUS.clone().unwrap();
531f4877a4S曾俊     }
541f4877a4S曾俊 }
551f4877a4S曾俊 
561f4877a4S曾俊 /// # 结构功能
571f4877a4S曾俊 /// 该结构为Pci总线,由于总线也属于设备,故设此结构;
581f4877a4S曾俊 /// 此结构对应/sys/bus/pci
591f4877a4S曾俊 #[derive(Debug)]
601f4877a4S曾俊 pub struct PciBus {
611f4877a4S曾俊     private: SubSysPrivate,
621f4877a4S曾俊 }
631f4877a4S曾俊 
641f4877a4S曾俊 impl PciBus {
new() -> Arc<Self>651f4877a4S曾俊     pub fn new() -> Arc<Self> {
661f4877a4S曾俊         let w: Weak<Self> = Weak::new();
671f4877a4S曾俊         let private = SubSysPrivate::new("pci".to_string(), Some(w), None, &[]);
681f4877a4S曾俊         let bus = Arc::new(Self { private });
691f4877a4S曾俊         bus
701f4877a4S曾俊     }
711f4877a4S曾俊 }
721f4877a4S曾俊 
731f4877a4S曾俊 impl Bus for PciBus {
name(&self) -> String741f4877a4S曾俊     fn name(&self) -> String {
751f4877a4S曾俊         return "pci".to_string();
761f4877a4S曾俊     }
771f4877a4S曾俊 
dev_name(&self) -> String781f4877a4S曾俊     fn dev_name(&self) -> String {
791f4877a4S曾俊         return self.name();
801f4877a4S曾俊     }
811f4877a4S曾俊 
dev_groups(&self) -> &'static [&'static dyn AttributeGroup]821f4877a4S曾俊     fn dev_groups(&self) -> &'static [&'static dyn AttributeGroup] {
831f4877a4S曾俊         return &[&PciDeviceAttrGroup];
841f4877a4S曾俊     }
851f4877a4S曾俊 
subsystem(&self) -> &SubSysPrivate861f4877a4S曾俊     fn subsystem(&self) -> &SubSysPrivate {
871f4877a4S曾俊         return &self.private;
881f4877a4S曾俊     }
891f4877a4S曾俊 
probe(&self, device: &Arc<dyn Device>) -> Result<(), SystemError>901f4877a4S曾俊     fn probe(&self, device: &Arc<dyn Device>) -> Result<(), SystemError> {
911f4877a4S曾俊         let drv = device.driver().ok_or(SystemError::EINVAL)?;
921f4877a4S曾俊         let pci_drv = drv.cast::<dyn PciDriver>().map_err(|_| {
932eab6dd7S曾俊             error!(
941f4877a4S曾俊                 "PciBus::probe() failed: device.driver() is not a PciDriver. Device: '{:?}'",
951f4877a4S曾俊                 device.name()
961f4877a4S曾俊             );
971f4877a4S曾俊             SystemError::EINVAL
981f4877a4S曾俊         })?;
991f4877a4S曾俊         let pci_dev = device.clone().cast::<dyn PciDevice>().map_err(|_| {
1002eab6dd7S曾俊             error!(
1011f4877a4S曾俊                 "PciBus::probe() failed: device is not a PciDevice. Device: '{:?}'",
1021f4877a4S曾俊                 device.name()
1031f4877a4S曾俊             );
1041f4877a4S曾俊             SystemError::EINVAL
1051f4877a4S曾俊         })?;
1061f4877a4S曾俊         //见https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/pci/pci-driver.c#324
1071f4877a4S曾俊         let id = pci_drv.match_dev(&pci_dev).ok_or(SystemError::EINVAL)?;
1081f4877a4S曾俊         pci_drv.probe(&pci_dev, &id)
1091f4877a4S曾俊     }
1101f4877a4S曾俊 
remove(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError>1111f4877a4S曾俊     fn remove(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> {
1121f4877a4S曾俊         todo!()
1131f4877a4S曾俊     }
1141f4877a4S曾俊 
sync_state(&self, _device: &Arc<dyn Device>)1151f4877a4S曾俊     fn sync_state(&self, _device: &Arc<dyn Device>) {
1161f4877a4S曾俊         todo!()
1171f4877a4S曾俊     }
1181f4877a4S曾俊 
shutdown(&self, _device: &Arc<dyn Device>)1191f4877a4S曾俊     fn shutdown(&self, _device: &Arc<dyn Device>) {
1201f4877a4S曾俊         todo!()
1211f4877a4S曾俊     }
1221f4877a4S曾俊 
resume(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError>1231f4877a4S曾俊     fn resume(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> {
1241f4877a4S曾俊         todo!()
1251f4877a4S曾俊     }
1261f4877a4S曾俊 
match_device( &self, device: &Arc<dyn Device>, driver: &Arc<dyn Driver>, ) -> Result<bool, SystemError>1271f4877a4S曾俊     fn match_device(
1281f4877a4S曾俊         &self,
1291f4877a4S曾俊         device: &Arc<dyn Device>,
1301f4877a4S曾俊         driver: &Arc<dyn Driver>,
1311f4877a4S曾俊     ) -> Result<bool, SystemError> {
1321f4877a4S曾俊         //首先将设备和驱动映射为pci设备和pci驱动
1331f4877a4S曾俊         let pci_driver = driver.clone().cast::<dyn PciDriver>().map_err(|_| {
1341f4877a4S曾俊             return SystemError::EINVAL;
1351f4877a4S曾俊         })?;
1361f4877a4S曾俊         let pci_dev = device.clone().cast::<dyn PciDevice>().map_err(|_| {
1371f4877a4S曾俊             return SystemError::EINVAL;
1381f4877a4S曾俊         })?;
1391f4877a4S曾俊         //pci_driver需要实现一个match_dev函数,即driver需要识别是否支持给定的pci设备
1401f4877a4S曾俊         //这是主要的match方式
1411f4877a4S曾俊         if pci_driver.match_dev(&pci_dev).is_some() {
1421f4877a4S曾俊             return Ok(true);
1431f4877a4S曾俊         }
1441f4877a4S曾俊 
1451f4877a4S曾俊         //todo:这里似乎需要一个driver_override_only的支持,但是目前不清楚driver_override_only 的用途,故暂时参考platform总线的match方法
1461f4877a4S曾俊         //override_only相关代码在 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/pci/pci-driver.c#159
1471f4877a4S曾俊         if let Some(driver_id_table) = driver.id_table() {
1481f4877a4S曾俊             if driver_id_table.name().eq(&pci_dev.name()) {
1491f4877a4S曾俊                 return Ok(true);
1501f4877a4S曾俊             }
1511f4877a4S曾俊         };
1521f4877a4S曾俊         return Ok(pci_dev.name().eq(&pci_driver.name()));
1531f4877a4S曾俊     }
154*28fe4ad2S黄铭涛 
root_device(&self) -> Option<Weak<dyn Device>>155*28fe4ad2S黄铭涛     fn root_device(&self) -> Option<Weak<dyn Device>> {
156*28fe4ad2S黄铭涛         let root_device = pci_bus_device() as Arc<dyn Device>;
157*28fe4ad2S黄铭涛         return Some(Arc::downgrade(&root_device));
158*28fe4ad2S黄铭涛     }
1591f4877a4S曾俊 }
1601f4877a4S曾俊 
1611f4877a4S曾俊 #[derive(Debug)]
1621f4877a4S曾俊 pub struct PciDeviceAttrGroup;
1631f4877a4S曾俊 
1641f4877a4S曾俊 impl AttributeGroup for PciDeviceAttrGroup {
name(&self) -> Option<&str>1651f4877a4S曾俊     fn name(&self) -> Option<&str> {
1661f4877a4S曾俊         return None;
1671f4877a4S曾俊     }
1681f4877a4S曾俊 
attrs(&self) -> &[&'static dyn crate::filesystem::sysfs::Attribute]1691f4877a4S曾俊     fn attrs(&self) -> &[&'static dyn crate::filesystem::sysfs::Attribute] {
1701f4877a4S曾俊         return &[];
1711f4877a4S曾俊     }
1721f4877a4S曾俊 
is_visible( &self, _kobj: Arc<dyn crate::driver::base::kobject::KObject>, attr: &'static dyn crate::filesystem::sysfs::Attribute, ) -> Option<crate::filesystem::vfs::syscall::ModeType>1731f4877a4S曾俊     fn is_visible(
1741f4877a4S曾俊         &self,
1751f4877a4S曾俊         _kobj: Arc<dyn crate::driver::base::kobject::KObject>,
1761f4877a4S曾俊         attr: &'static dyn crate::filesystem::sysfs::Attribute,
1771f4877a4S曾俊     ) -> Option<crate::filesystem::vfs::syscall::ModeType> {
1781f4877a4S曾俊         return Some(attr.mode());
1791f4877a4S曾俊     }
1801f4877a4S曾俊 }
1811f4877a4S曾俊 
pci_bus_subsys_init() -> Result<(), SystemError>1821f4877a4S曾俊 pub(super) fn pci_bus_subsys_init() -> Result<(), SystemError> {
1831f4877a4S曾俊     let pci_bus_device: Arc<PciBusDevice> = PciBusDevice::new(Some(Arc::downgrade(
1841f4877a4S曾俊         &(sys_devices_kset() as Arc<dyn KObject>),
1851f4877a4S曾俊     )));
1861f4877a4S曾俊 
1871f4877a4S曾俊     set_pci_bus_device(pci_bus_device.clone());
1881f4877a4S曾俊 
1891f4877a4S曾俊     device_register(pci_bus_device.clone())?;
1901f4877a4S曾俊     let pci_bus = PciBus::new();
1911f4877a4S曾俊 
1921f4877a4S曾俊     set_pci_bus(pci_bus.clone());
1931f4877a4S曾俊     let r = bus_register(pci_bus.clone() as Arc<dyn Bus>);
1941f4877a4S曾俊     pt_init()?;
1951f4877a4S曾俊     return r;
1961f4877a4S曾俊 }
197