xref: /DragonOS/kernel/src/driver/pci/ecam.rs (revision dd8e74ef0d7f91a141bd217736bef4fe7dc6df3d)
1 use crate::mm::PhysAddr;
2 
3 use super::{
4     pci::{PciCam, SegmentGroupNumber},
5     root::{pci_root_manager, PciRoot},
6 };
7 
8 #[inline(always)]
9 pub fn pci_ecam_root_info_manager() -> &'static EcamRootInfoManager {
10     &EcamRootInfoManager
11 }
12 
13 /// Ecam pci root info
14 #[derive(Clone, Copy)]
15 pub struct EcamRootInfo {
16     pub segement_group_number: SegmentGroupNumber,
17     pub bus_begin: u8,
18     pub bus_end: u8,
19     pub physical_address_base: PhysAddr,
20 }
21 
22 impl EcamRootInfo {
23     pub fn new(
24         segement_group_number: SegmentGroupNumber,
25         bus_begin: u8,
26         bus_end: u8,
27         physical_address_base: PhysAddr,
28     ) -> Self {
29         Self {
30             segement_group_number,
31             bus_begin,
32             bus_end,
33             physical_address_base,
34         }
35     }
36 }
37 
38 pub struct EcamRootInfoManager;
39 
40 impl EcamRootInfoManager {
41     /// # add_ecam_root_info - 向EcamRootInfoManager添加EcamRootInfo
42     ///
43     /// 将一个新的EcamRootInfo添加到EcamRootInfoManager中。
44     ///
45     /// ## 参数
46     ///
47     /// - `ecam_root_info`: EcamRootInfo - 要添加的EcamRootInfo实例
48     pub fn add_ecam_root_info(&self, ecam_root_info: EcamRootInfo) {
49         if !pci_root_manager().has_root(ecam_root_info.segement_group_number) {
50             let root = PciRoot::new(
51                 ecam_root_info.segement_group_number,
52                 PciCam::Ecam,
53                 ecam_root_info.physical_address_base,
54                 ecam_root_info.bus_begin,
55                 ecam_root_info.bus_end,
56             );
57 
58             if let Err(err) = root {
59                 kerror!("add_ecam_root_info(): failed to create PciRoot: {:?}", err);
60                 return;
61             }
62 
63             pci_root_manager().add_pci_root(root.unwrap());
64         } else {
65             kwarn!(
66                 "add_ecam_root_info(): root {} already exists",
67                 ecam_root_info.segement_group_number
68             );
69         }
70     }
71 }
72