1*e32effb1SLoGin use alloc::{ 2*e32effb1SLoGin string::{String, ToString}, 3*e32effb1SLoGin sync::{Arc, Weak}, 4*e32effb1SLoGin }; 5*e32effb1SLoGin use ida::IdAllocator; 6*e32effb1SLoGin use intertrait::cast::CastArc; 7*e32effb1SLoGin use system_error::SystemError; 8*e32effb1SLoGin use unified_init::macros::unified_init; 9*e32effb1SLoGin 10*e32effb1SLoGin use crate::{ 11*e32effb1SLoGin driver::base::{ 12*e32effb1SLoGin device::{ 13*e32effb1SLoGin bus::{bus_manager, Bus}, 14*e32effb1SLoGin device_manager, 15*e32effb1SLoGin driver::{driver_manager, Driver}, 16*e32effb1SLoGin Device, 17*e32effb1SLoGin }, 18*e32effb1SLoGin kobject::KObject, 19*e32effb1SLoGin subsys::SubSysPrivate, 20*e32effb1SLoGin }, 21*e32effb1SLoGin filesystem::{ 22*e32effb1SLoGin sysfs::{ 23*e32effb1SLoGin file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport, SYSFS_ATTR_MODE_RO, 24*e32effb1SLoGin }, 25*e32effb1SLoGin vfs::syscall::ModeType, 26*e32effb1SLoGin }, 27*e32effb1SLoGin init::initcall::INITCALL_CORE, 28*e32effb1SLoGin }; 29*e32effb1SLoGin 30*e32effb1SLoGin use super::{VirtIODevice, VirtIODeviceIndex, VirtIODriver}; 31*e32effb1SLoGin 32*e32effb1SLoGin static mut VIRTIO_BUS: Option<Arc<VirtIOBus>> = None; 33*e32effb1SLoGin 34*e32effb1SLoGin #[inline(always)] 35*e32effb1SLoGin pub fn virtio_bus() -> Arc<VirtIOBus> { 36*e32effb1SLoGin unsafe { VIRTIO_BUS.as_ref().unwrap().clone() } 37*e32effb1SLoGin } 38*e32effb1SLoGin 39*e32effb1SLoGin #[derive(Debug)] 40*e32effb1SLoGin pub struct VirtIOBus { 41*e32effb1SLoGin private: SubSysPrivate, 42*e32effb1SLoGin } 43*e32effb1SLoGin 44*e32effb1SLoGin impl VirtIOBus { 45*e32effb1SLoGin pub fn new() -> Arc<Self> { 46*e32effb1SLoGin let w: Weak<Self> = Weak::new(); 47*e32effb1SLoGin let private = SubSysPrivate::new("virtio".to_string(), Some(w), None, &[]); 48*e32effb1SLoGin let bus = Arc::new(Self { private }); 49*e32effb1SLoGin bus.subsystem() 50*e32effb1SLoGin .set_bus(Some(Arc::downgrade(&(bus.clone() as Arc<dyn Bus>)))); 51*e32effb1SLoGin 52*e32effb1SLoGin return bus; 53*e32effb1SLoGin } 54*e32effb1SLoGin } 55*e32effb1SLoGin 56*e32effb1SLoGin impl Bus for VirtIOBus { 57*e32effb1SLoGin fn name(&self) -> String { 58*e32effb1SLoGin self.private.name() 59*e32effb1SLoGin } 60*e32effb1SLoGin 61*e32effb1SLoGin fn dev_name(&self) -> String { 62*e32effb1SLoGin return self.name(); 63*e32effb1SLoGin } 64*e32effb1SLoGin 65*e32effb1SLoGin fn dev_groups(&self) -> &'static [&'static dyn AttributeGroup] { 66*e32effb1SLoGin // todo: VirtIODeviceAttrGroup 67*e32effb1SLoGin return &[]; 68*e32effb1SLoGin } 69*e32effb1SLoGin 70*e32effb1SLoGin fn subsystem(&self) -> &SubSysPrivate { 71*e32effb1SLoGin return &self.private; 72*e32effb1SLoGin } 73*e32effb1SLoGin 74*e32effb1SLoGin fn probe(&self, device: &Arc<dyn Device>) -> Result<(), SystemError> { 75*e32effb1SLoGin let drv = device.driver().ok_or(SystemError::EINVAL)?; 76*e32effb1SLoGin let virtio_drv = drv.cast::<dyn VirtIODriver>().map_err(|_| { 77*e32effb1SLoGin kerror!( 78*e32effb1SLoGin "VirtIOBus::probe() failed: device.driver() is not a VirtioDriver. Device: '{:?}'", 79*e32effb1SLoGin device.name() 80*e32effb1SLoGin ); 81*e32effb1SLoGin SystemError::EINVAL 82*e32effb1SLoGin })?; 83*e32effb1SLoGin 84*e32effb1SLoGin let virtio_dev = device.clone().cast::<dyn VirtIODevice>().map_err(|_| { 85*e32effb1SLoGin kerror!( 86*e32effb1SLoGin "VirtIOBus::probe() failed: device is not a VirtIODevice. Device: '{:?}'", 87*e32effb1SLoGin device.name() 88*e32effb1SLoGin ); 89*e32effb1SLoGin SystemError::EINVAL 90*e32effb1SLoGin })?; 91*e32effb1SLoGin 92*e32effb1SLoGin return virtio_drv.probe(&virtio_dev); 93*e32effb1SLoGin } 94*e32effb1SLoGin 95*e32effb1SLoGin fn remove(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> { 96*e32effb1SLoGin todo!() 97*e32effb1SLoGin } 98*e32effb1SLoGin 99*e32effb1SLoGin fn sync_state(&self, _device: &Arc<dyn Device>) { 100*e32effb1SLoGin todo!() 101*e32effb1SLoGin } 102*e32effb1SLoGin 103*e32effb1SLoGin fn shutdown(&self, _device: &Arc<dyn Device>) { 104*e32effb1SLoGin todo!() 105*e32effb1SLoGin } 106*e32effb1SLoGin 107*e32effb1SLoGin fn resume(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> { 108*e32effb1SLoGin todo!() 109*e32effb1SLoGin } 110*e32effb1SLoGin 111*e32effb1SLoGin fn match_device( 112*e32effb1SLoGin &self, 113*e32effb1SLoGin _device: &Arc<dyn Device>, 114*e32effb1SLoGin _driver: &Arc<dyn Driver>, 115*e32effb1SLoGin ) -> Result<bool, SystemError> { 116*e32effb1SLoGin // todo: https://code.dragonos.org.cn/xref/linux-6.6.21/drivers/virtio/virtio.c#85 117*e32effb1SLoGin todo!("VirtIOBus::match_device() is not implemented") 118*e32effb1SLoGin } 119*e32effb1SLoGin } 120*e32effb1SLoGin 121*e32effb1SLoGin #[unified_init(INITCALL_CORE)] 122*e32effb1SLoGin fn virtio_init() -> Result<(), SystemError> { 123*e32effb1SLoGin let bus = VirtIOBus::new(); 124*e32effb1SLoGin unsafe { 125*e32effb1SLoGin VIRTIO_BUS = Some(bus.clone()); 126*e32effb1SLoGin } 127*e32effb1SLoGin bus_manager() 128*e32effb1SLoGin .register(bus) 129*e32effb1SLoGin .expect("Failed to register virtio bus!"); 130*e32effb1SLoGin Ok(()) 131*e32effb1SLoGin } 132*e32effb1SLoGin 133*e32effb1SLoGin #[inline(always)] 134*e32effb1SLoGin pub fn virtio_driver_manager() -> &'static VirtIODriverManager { 135*e32effb1SLoGin &VirtIODriverManager 136*e32effb1SLoGin } 137*e32effb1SLoGin 138*e32effb1SLoGin pub struct VirtIODriverManager; 139*e32effb1SLoGin 140*e32effb1SLoGin impl VirtIODriverManager { 141*e32effb1SLoGin pub fn register(&self, driver: Arc<dyn VirtIODriver>) -> Result<(), SystemError> { 142*e32effb1SLoGin driver.set_bus(Some(Arc::downgrade(&(virtio_bus() as Arc<dyn Bus>)))); 143*e32effb1SLoGin return driver_manager().register(driver as Arc<dyn Driver>); 144*e32effb1SLoGin } 145*e32effb1SLoGin 146*e32effb1SLoGin #[allow(dead_code)] 147*e32effb1SLoGin pub fn unregister(&self, driver: &Arc<dyn VirtIODriver>) { 148*e32effb1SLoGin driver_manager().unregister(&(driver.clone() as Arc<dyn Driver>)); 149*e32effb1SLoGin } 150*e32effb1SLoGin } 151*e32effb1SLoGin 152*e32effb1SLoGin #[inline(always)] 153*e32effb1SLoGin pub fn virtio_device_manager() -> &'static VirtIODeviceManager { 154*e32effb1SLoGin &VirtIODeviceManager 155*e32effb1SLoGin } 156*e32effb1SLoGin 157*e32effb1SLoGin pub struct VirtIODeviceManager; 158*e32effb1SLoGin 159*e32effb1SLoGin impl VirtIODeviceManager { 160*e32effb1SLoGin pub fn device_add(&self, dev: Arc<dyn VirtIODevice>) -> Result<(), SystemError> { 161*e32effb1SLoGin dev.set_bus(Some(Arc::downgrade(&(virtio_bus() as Arc<dyn Bus>)))); 162*e32effb1SLoGin device_manager().device_default_initialize(&(dev.clone() as Arc<dyn Device>)); 163*e32effb1SLoGin let drv = dev.driver().ok_or(SystemError::EINVAL)?; 164*e32effb1SLoGin 165*e32effb1SLoGin let virtio_drv = drv.cast::<dyn VirtIODriver>().map_err(|_| { 166*e32effb1SLoGin kerror!( 167*e32effb1SLoGin "VirtIODeviceManager::device_add() failed: device.driver() is not a VirtioDriver. Device: '{:?}'", 168*e32effb1SLoGin dev.name() 169*e32effb1SLoGin ); 170*e32effb1SLoGin SystemError::EINVAL 171*e32effb1SLoGin })?; 172*e32effb1SLoGin let virtio_index = VIRTIO_DEVICE_INDEX_MANAGER.alloc(); 173*e32effb1SLoGin dev.set_virtio_device_index(virtio_index); 174*e32effb1SLoGin dev.set_device_name(format!("virtio{}", virtio_index.data())); 175*e32effb1SLoGin virtio_drv.probe(&dev)?; 176*e32effb1SLoGin 177*e32effb1SLoGin device_manager().add_device(dev.clone() as Arc<dyn Device>)?; 178*e32effb1SLoGin device_manager().add_groups(&(dev as Arc<dyn Device>), &[&VirtIODeviceAttrGroup]) 179*e32effb1SLoGin } 180*e32effb1SLoGin 181*e32effb1SLoGin #[allow(dead_code)] 182*e32effb1SLoGin pub fn device_remove(&self, dev: &Arc<dyn VirtIODevice>) -> Result<(), SystemError> { 183*e32effb1SLoGin device_manager().remove(&(dev.clone() as Arc<dyn Device>)); 184*e32effb1SLoGin return Ok(()); 185*e32effb1SLoGin } 186*e32effb1SLoGin } 187*e32effb1SLoGin 188*e32effb1SLoGin static VIRTIO_DEVICE_INDEX_MANAGER: VirtIODeviceIndexManager = VirtIODeviceIndexManager::new(); 189*e32effb1SLoGin 190*e32effb1SLoGin /// VirtIO设备索引管理器 191*e32effb1SLoGin /// 192*e32effb1SLoGin /// VirtIO设备索引管理器用于分配和管理VirtIO设备的唯一索引。 193*e32effb1SLoGin pub struct VirtIODeviceIndexManager { 194*e32effb1SLoGin // ID分配器 195*e32effb1SLoGin /// 196*e32effb1SLoGin /// ID分配器用于分配唯一的索引给VirtIO设备。 197*e32effb1SLoGin ida: IdAllocator, 198*e32effb1SLoGin } 199*e32effb1SLoGin 200*e32effb1SLoGin // VirtIO设备索引管理器的新建实例 201*e32effb1SLoGin impl VirtIODeviceIndexManager { 202*e32effb1SLoGin /// 创建新的VirtIO设备索引管理器实例 203*e32effb1SLoGin /// 204*e32effb1SLoGin /// 创建一个新的VirtIO设备索引管理器实例,初始时分配器从0开始,直到最大usize值。 205*e32effb1SLoGin const fn new() -> Self { 206*e32effb1SLoGin Self { 207*e32effb1SLoGin ida: IdAllocator::new(0, usize::MAX), 208*e32effb1SLoGin } 209*e32effb1SLoGin } 210*e32effb1SLoGin 211*e32effb1SLoGin /// 分配一个新的VirtIO设备索引 212*e32effb1SLoGin /// 213*e32effb1SLoGin /// 分配一个唯一的索引给VirtIO设备。 214*e32effb1SLoGin pub fn alloc(&self) -> VirtIODeviceIndex { 215*e32effb1SLoGin VirtIODeviceIndex(self.ida.alloc().unwrap()) 216*e32effb1SLoGin } 217*e32effb1SLoGin 218*e32effb1SLoGin // 释放一个VirtIO设备索引 219*e32effb1SLoGin /// 220*e32effb1SLoGin /// 释放之前分配的VirtIO设备索引,使其可以被重新使用。 221*e32effb1SLoGin #[allow(dead_code)] 222*e32effb1SLoGin pub fn free(&self, index: VirtIODeviceIndex) { 223*e32effb1SLoGin self.ida.free(index.0); 224*e32effb1SLoGin } 225*e32effb1SLoGin } 226*e32effb1SLoGin 227*e32effb1SLoGin /// VirtIO设备属性组 228*e32effb1SLoGin /// 229*e32effb1SLoGin /// 参考 https://code.dragonos.org.cn/xref/linux-6.6.21/drivers/virtio/virtio.c#64 230*e32effb1SLoGin #[derive(Debug)] 231*e32effb1SLoGin pub struct VirtIODeviceAttrGroup; 232*e32effb1SLoGin 233*e32effb1SLoGin impl AttributeGroup for VirtIODeviceAttrGroup { 234*e32effb1SLoGin fn name(&self) -> Option<&str> { 235*e32effb1SLoGin None 236*e32effb1SLoGin } 237*e32effb1SLoGin 238*e32effb1SLoGin fn attrs(&self) -> &[&'static dyn Attribute] { 239*e32effb1SLoGin &[&AttrDevice, &AttrVendor] 240*e32effb1SLoGin } 241*e32effb1SLoGin } 242*e32effb1SLoGin 243*e32effb1SLoGin #[derive(Debug)] 244*e32effb1SLoGin struct AttrDevice; 245*e32effb1SLoGin 246*e32effb1SLoGin impl Attribute for AttrDevice { 247*e32effb1SLoGin fn name(&self) -> &str { 248*e32effb1SLoGin "device" 249*e32effb1SLoGin } 250*e32effb1SLoGin 251*e32effb1SLoGin fn mode(&self) -> ModeType { 252*e32effb1SLoGin SYSFS_ATTR_MODE_RO 253*e32effb1SLoGin } 254*e32effb1SLoGin 255*e32effb1SLoGin fn support(&self) -> SysFSOpsSupport { 256*e32effb1SLoGin SysFSOpsSupport::ATTR_SHOW 257*e32effb1SLoGin } 258*e32effb1SLoGin 259*e32effb1SLoGin fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 260*e32effb1SLoGin let dev = kobj.cast::<dyn VirtIODevice>().map_err(|_| { 261*e32effb1SLoGin kerror!("AttrDevice::show() failed: kobj is not a VirtIODevice"); 262*e32effb1SLoGin SystemError::EINVAL 263*e32effb1SLoGin })?; 264*e32effb1SLoGin let device_type_id = dev.device_type_id(); 265*e32effb1SLoGin 266*e32effb1SLoGin return sysfs_emit_str(buf, &format!("0x{:04x}\n", device_type_id)); 267*e32effb1SLoGin } 268*e32effb1SLoGin } 269*e32effb1SLoGin 270*e32effb1SLoGin #[derive(Debug)] 271*e32effb1SLoGin struct AttrVendor; 272*e32effb1SLoGin 273*e32effb1SLoGin impl Attribute for AttrVendor { 274*e32effb1SLoGin fn name(&self) -> &str { 275*e32effb1SLoGin "vendor" 276*e32effb1SLoGin } 277*e32effb1SLoGin 278*e32effb1SLoGin fn mode(&self) -> ModeType { 279*e32effb1SLoGin SYSFS_ATTR_MODE_RO 280*e32effb1SLoGin } 281*e32effb1SLoGin 282*e32effb1SLoGin fn support(&self) -> SysFSOpsSupport { 283*e32effb1SLoGin SysFSOpsSupport::ATTR_SHOW 284*e32effb1SLoGin } 285*e32effb1SLoGin 286*e32effb1SLoGin fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 287*e32effb1SLoGin let dev = kobj.cast::<dyn VirtIODevice>().map_err(|_| { 288*e32effb1SLoGin kerror!("AttrVendor::show() failed: kobj is not a VirtIODevice"); 289*e32effb1SLoGin SystemError::EINVAL 290*e32effb1SLoGin })?; 291*e32effb1SLoGin let vendor = dev.vendor(); 292*e32effb1SLoGin 293*e32effb1SLoGin return sysfs_emit_str(buf, &format!("0x{:04x}\n", vendor)); 294*e32effb1SLoGin } 295*e32effb1SLoGin } 296