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