xref: /DragonOS/kernel/src/driver/base/cpu.rs (revision f9fe30be89e89499aad4ef52b4648986bef5a7d8)
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, DeviceCommonData, DeviceType, IdTable,
22     },
23     kobject::{KObjType, KObject, KObjectCommonData, 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     kobject_common: KObjectCommonData,
127     device_common: DeviceCommonData,
128     name: String,
129 }
130 
131 impl InnerCpuSubSystemFakeRootDevice {
132     pub fn new() -> Self {
133         return Self {
134             kobject_common: KObjectCommonData::default(),
135             device_common: DeviceCommonData::default(),
136             name: "".to_string(),
137         };
138     }
139 }
140 
141 impl Device for CpuSubSystemFakeRootDevice {
142     fn dev_type(&self) -> DeviceType {
143         todo!()
144     }
145 
146     fn id_table(&self) -> IdTable {
147         IdTable::new("cpu".to_string(), None)
148     }
149 
150     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
151         self.inner.write().device_common.bus = bus;
152     }
153 
154     fn bus(&self) -> Option<Weak<dyn Bus>> {
155         self.inner.read().device_common.bus.clone()
156     }
157 
158     fn driver(&self) -> Option<Arc<dyn Driver>> {
159         None
160     }
161 
162     fn set_driver(&self, _driver: Option<Weak<dyn Driver>>) {
163         todo!()
164     }
165 
166     fn is_dead(&self) -> bool {
167         false
168     }
169 
170     fn can_match(&self) -> bool {
171         todo!()
172     }
173 
174     fn set_can_match(&self, _can_match: bool) {
175         todo!()
176     }
177 
178     fn state_synced(&self) -> bool {
179         true
180     }
181 
182     fn set_class(&self, _class: Option<Weak<dyn Class>>) {
183         todo!()
184     }
185 
186     fn dev_parent(&self) -> Option<Weak<dyn Device>> {
187         self.inner.read().device_common.parent.clone()
188     }
189 
190     fn set_dev_parent(&self, dev_parent: Option<Weak<dyn Device>>) {
191         self.inner.write().device_common.parent = dev_parent;
192     }
193 }
194 
195 impl KObject for CpuSubSystemFakeRootDevice {
196     fn as_any_ref(&self) -> &dyn Any {
197         self
198     }
199 
200     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
201         self.inner.write().kobject_common.kern_inode = inode;
202     }
203 
204     fn inode(&self) -> Option<Arc<KernFSInode>> {
205         self.inner.read().kobject_common.kern_inode.clone()
206     }
207 
208     fn parent(&self) -> Option<Weak<dyn KObject>> {
209         self.inner.read().kobject_common.parent.clone()
210     }
211 
212     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
213         self.inner.write().kobject_common.parent = parent;
214     }
215 
216     fn kset(&self) -> Option<Arc<KSet>> {
217         self.inner.read().kobject_common.kset.clone()
218     }
219 
220     fn set_kset(&self, kset: Option<Arc<KSet>>) {
221         self.inner.write().kobject_common.kset = kset;
222     }
223 
224     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
225         self.inner.read().kobject_common.kobj_type
226     }
227 
228     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
229         self.inner.write().kobject_common.kobj_type = ktype;
230     }
231 
232     fn name(&self) -> String {
233         self.inner.read().name.clone()
234     }
235 
236     fn set_name(&self, name: String) {
237         self.inner.write().name = name;
238     }
239 
240     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
241         self.kobj_state.read()
242     }
243 
244     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
245         self.kobj_state.write()
246     }
247 
248     fn set_kobj_state(&self, state: KObjectState) {
249         *self.kobj_state_mut() = state;
250     }
251 }
252