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 class::Class, 17 device::{ 18 bus::{subsystem_manager, Bus}, 19 driver::Driver, 20 Device, DeviceNumber, DeviceType, IdTable, 21 }, 22 kobject::{KObjType, KObject, KObjectState, LockedKObjectState}, 23 kset::KSet, 24 subsys::SubSysPrivate, 25 }; 26 27 #[inline(always)] 28 pub fn cpu_device_manager() -> &'static CpuDeviceManager { 29 return &CpuDeviceManager; 30 } 31 32 #[derive(Debug)] 33 pub struct CpuDeviceManager; 34 35 impl CpuDeviceManager { 36 /// 初始化设备驱动模型的CPU子系统 37 /// 38 /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/base/cpu.c?fi=get_cpu_device#622 39 pub fn init(&self) -> Result<(), SystemError> { 40 let cpu_subsys = CpuSubSystem::new(); 41 let root_device = CpuSubSystemFakeRootDevice::new(); 42 subsystem_manager() 43 .subsys_system_register( 44 &(cpu_subsys as Arc<dyn Bus>), 45 &(root_device as Arc<dyn Device>), 46 ) 47 .expect("register cpu subsys failed"); 48 49 return Ok(()); 50 } 51 } 52 53 /// cpu子系统 54 /// 55 /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/base/cpu.c?fi=get_cpu_device#128 56 #[derive(Debug)] 57 struct CpuSubSystem { 58 subsys_private: SubSysPrivate, 59 } 60 61 impl CpuSubSystem { 62 pub fn new() -> Arc<Self> { 63 let bus = Arc::new(Self { 64 subsys_private: SubSysPrivate::new("cpu".to_string(), None, None, &[]), 65 }); 66 bus.subsystem() 67 .set_bus(Some(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(), Some(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 fn set_class(&self, _class: Option<Arc<dyn Class>>) { 184 todo!() 185 } 186 } 187 188 impl KObject for CpuSubSystemFakeRootDevice { 189 fn as_any_ref(&self) -> &dyn Any { 190 self 191 } 192 193 fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 194 self.inner.write().kern_inode = inode; 195 } 196 197 fn inode(&self) -> Option<Arc<KernFSInode>> { 198 self.inner.read().kern_inode.clone() 199 } 200 201 fn parent(&self) -> Option<Weak<dyn KObject>> { 202 self.inner.read().parent_kobj.clone() 203 } 204 205 fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 206 self.inner.write().parent_kobj = parent; 207 } 208 209 fn kset(&self) -> Option<Arc<KSet>> { 210 self.inner.read().kset.clone() 211 } 212 213 fn set_kset(&self, kset: Option<Arc<KSet>>) { 214 self.inner.write().kset = kset; 215 } 216 217 fn kobj_type(&self) -> Option<&'static dyn KObjType> { 218 self.inner.read().ktype 219 } 220 221 fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 222 self.inner.write().ktype = ktype; 223 } 224 225 fn name(&self) -> String { 226 self.inner.read().name.clone() 227 } 228 229 fn set_name(&self, name: String) { 230 self.inner.write().name = name; 231 } 232 233 fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 234 self.kobj_state.read() 235 } 236 237 fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 238 self.kobj_state.write() 239 } 240 241 fn set_kobj_state(&self, state: KObjectState) { 242 *self.kobj_state_mut() = state; 243 } 244 } 245