xref: /DragonOS/kernel/src/driver/base/cpu.rs (revision 83ed0ebc293d5a10245089f627f52770fd5b9dd4)
1 use core::any::Any;
2 
3 use alloc::{
4     string::{String, ToString},
5     sync::{Arc, Weak},
6 };
7 
8 use crate::{
9     driver::acpi::acpi_manager,
10     filesystem::kernfs::KernFSInode,
11     libs::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard},
12     syscall::SystemError,
13 };
14 
15 use super::{
16     device::{
17         bus::{subsystem_manager, Bus},
18         driver::Driver,
19         Device, DeviceNumber, DeviceType, IdTable,
20     },
21     kobject::{KObjType, KObject, KObjectState, LockedKObjectState},
22     kset::KSet,
23     subsys::SubSysPrivate,
24 };
25 
26 #[inline(always)]
27 pub fn cpu_device_manager() -> &'static CpuDeviceManager {
28     return &CpuDeviceManager;
29 }
30 
31 #[derive(Debug)]
32 pub struct CpuDeviceManager;
33 
34 impl CpuDeviceManager {
35     /// 初始化设备驱动模型的CPU子系统
36     ///
37     /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/base/cpu.c?fi=get_cpu_device#622
38     pub fn init(&self) -> Result<(), SystemError> {
39         let cpu_subsys = CpuSubSystem::new();
40         let root_device = CpuSubSystemFakeRootDevice::new();
41         subsystem_manager()
42             .subsys_system_register(
43                 &(cpu_subsys as Arc<dyn Bus>),
44                 &(root_device as Arc<dyn Device>),
45             )
46             .expect("register cpu subsys failed");
47 
48         return Ok(());
49     }
50 }
51 
52 /// cpu子系统
53 ///
54 /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/base/cpu.c?fi=get_cpu_device#128
55 #[derive(Debug)]
56 struct CpuSubSystem {
57     subsys_private: SubSysPrivate,
58 }
59 
60 impl CpuSubSystem {
61     pub fn new() -> Arc<Self> {
62         let default_weak: Weak<Self> = Weak::new();
63         let bus = Arc::new(Self {
64             subsys_private: SubSysPrivate::new("cpu".to_string(), default_weak, &[]),
65         });
66         bus.subsystem()
67             .set_bus(Arc::downgrade(&(bus.clone() as Arc<dyn Bus>)));
68         return bus;
69     }
70 }
71 
72 impl Bus for CpuSubSystem {
73     fn name(&self) -> String {
74         "cpu".to_string()
75     }
76 
77     fn dev_name(&self) -> String {
78         self.name()
79     }
80 
81     fn remove(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> {
82         todo!()
83     }
84 
85     fn shutdown(&self, _device: &Arc<dyn Device>) {
86         todo!()
87     }
88 
89     fn resume(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> {
90         todo!()
91     }
92 
93     fn match_device(
94         &self,
95         device: &Arc<dyn Device>,
96         driver: &Arc<dyn Driver>,
97     ) -> Result<bool, SystemError> {
98         // ACPI style match is the only one that may succeed.
99         return acpi_manager().driver_match_device(driver, device);
100     }
101 
102     fn subsystem(&self) -> &SubSysPrivate {
103         &self.subsys_private
104     }
105 }
106 
107 #[derive(Debug)]
108 #[cast_to([sync] Device)]
109 pub struct CpuSubSystemFakeRootDevice {
110     inner: RwLock<InnerCpuSubSystemFakeRootDevice>,
111     kobj_state: LockedKObjectState,
112 }
113 
114 impl CpuSubSystemFakeRootDevice {
115     pub fn new() -> Arc<Self> {
116         return Arc::new(Self {
117             inner: RwLock::new(InnerCpuSubSystemFakeRootDevice::new()),
118             kobj_state: LockedKObjectState::new(None),
119         });
120     }
121 }
122 
123 #[derive(Debug)]
124 struct InnerCpuSubSystemFakeRootDevice {
125     parent_kobj: Option<Weak<dyn KObject>>,
126     bus: Option<Arc<dyn Bus>>,
127     kset: Option<Arc<KSet>>,
128     name: String,
129     kern_inode: Option<Arc<KernFSInode>>,
130     ktype: Option<&'static dyn KObjType>,
131 }
132 
133 impl InnerCpuSubSystemFakeRootDevice {
134     pub fn new() -> Self {
135         return Self {
136             parent_kobj: None,
137             bus: None,
138             kset: None,
139             name: "".to_string(),
140             kern_inode: None,
141             ktype: None,
142         };
143     }
144 }
145 
146 impl Device for CpuSubSystemFakeRootDevice {
147     fn dev_type(&self) -> DeviceType {
148         todo!()
149     }
150 
151     fn id_table(&self) -> IdTable {
152         IdTable::new("cpu".to_string(), DeviceNumber::new(0))
153     }
154 
155     fn set_bus(&self, bus: Option<Arc<dyn Bus>>) {
156         self.inner.write().bus = bus;
157     }
158 
159     fn driver(&self) -> Option<Arc<dyn Driver>> {
160         None
161     }
162 
163     fn set_driver(&self, _driver: Option<Weak<dyn Driver>>) {
164         todo!()
165     }
166 
167     fn is_dead(&self) -> bool {
168         false
169     }
170 
171     fn can_match(&self) -> bool {
172         todo!()
173     }
174 
175     fn set_can_match(&self, _can_match: bool) {
176         todo!()
177     }
178 
179     fn state_synced(&self) -> bool {
180         true
181     }
182 }
183 
184 impl KObject for CpuSubSystemFakeRootDevice {
185     fn as_any_ref(&self) -> &dyn Any {
186         self
187     }
188 
189     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
190         self.inner.write().kern_inode = inode;
191     }
192 
193     fn inode(&self) -> Option<Arc<KernFSInode>> {
194         self.inner.read().kern_inode.clone()
195     }
196 
197     fn parent(&self) -> Option<Weak<dyn KObject>> {
198         self.inner.read().parent_kobj.clone()
199     }
200 
201     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
202         self.inner.write().parent_kobj = parent;
203     }
204 
205     fn kset(&self) -> Option<Arc<KSet>> {
206         self.inner.read().kset.clone()
207     }
208 
209     fn set_kset(&self, kset: Option<Arc<KSet>>) {
210         self.inner.write().kset = kset;
211     }
212 
213     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
214         self.inner.read().ktype
215     }
216 
217     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
218         self.inner.write().ktype = ktype;
219     }
220 
221     fn name(&self) -> String {
222         self.inner.read().name.clone()
223     }
224 
225     fn set_name(&self, name: String) {
226         self.inner.write().name = name;
227     }
228 
229     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
230         self.kobj_state.read()
231     }
232 
233     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
234         self.kobj_state.write()
235     }
236 
237     fn set_kobj_state(&self, state: KObjectState) {
238         *self.kobj_state_mut() = state;
239     }
240 }
241