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, DeviceNumber, 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://opengrok.ringotek.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://opengrok.ringotek.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<Arc<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(), Some(DeviceNumber::new(0))) 154 } 155 156 fn set_bus(&self, bus: Option<Arc<dyn Bus>>) { 157 self.inner.write().bus = bus; 158 } 159 160 fn driver(&self) -> Option<Arc<dyn Driver>> { 161 None 162 } 163 164 fn set_driver(&self, _driver: Option<Weak<dyn Driver>>) { 165 todo!() 166 } 167 168 fn is_dead(&self) -> bool { 169 false 170 } 171 172 fn can_match(&self) -> bool { 173 todo!() 174 } 175 176 fn set_can_match(&self, _can_match: bool) { 177 todo!() 178 } 179 180 fn state_synced(&self) -> bool { 181 true 182 } 183 184 fn set_class(&self, _class: Option<Arc<dyn Class>>) { 185 todo!() 186 } 187 } 188 189 impl KObject for CpuSubSystemFakeRootDevice { 190 fn as_any_ref(&self) -> &dyn Any { 191 self 192 } 193 194 fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 195 self.inner.write().kern_inode = inode; 196 } 197 198 fn inode(&self) -> Option<Arc<KernFSInode>> { 199 self.inner.read().kern_inode.clone() 200 } 201 202 fn parent(&self) -> Option<Weak<dyn KObject>> { 203 self.inner.read().parent_kobj.clone() 204 } 205 206 fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 207 self.inner.write().parent_kobj = parent; 208 } 209 210 fn kset(&self) -> Option<Arc<KSet>> { 211 self.inner.read().kset.clone() 212 } 213 214 fn set_kset(&self, kset: Option<Arc<KSet>>) { 215 self.inner.write().kset = kset; 216 } 217 218 fn kobj_type(&self) -> Option<&'static dyn KObjType> { 219 self.inner.read().ktype 220 } 221 222 fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 223 self.inner.write().ktype = ktype; 224 } 225 226 fn name(&self) -> String { 227 self.inner.read().name.clone() 228 } 229 230 fn set_name(&self, name: String) { 231 self.inner.write().name = name; 232 } 233 234 fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 235 self.kobj_state.read() 236 } 237 238 fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 239 self.kobj_state.write() 240 } 241 242 fn set_kobj_state(&self, state: KObjectState) { 243 *self.kobj_state_mut() = state; 244 } 245 } 246