1c566df45SLoGin use core::intrinsics::unlikely; 2c566df45SLoGin 308a2ee40SLoGin use alloc::{ 4c566df45SLoGin string::{String, ToString}, 508a2ee40SLoGin sync::{Arc, Weak}, 602343d0bSLoGin vec::Vec, 708a2ee40SLoGin }; 8c566df45SLoGin 991e9d4abSLoGin use system_error::SystemError; 1091e9d4abSLoGin use unified_init::macros::unified_init; 1108a2ee40SLoGin 12c566df45SLoGin use crate::{ 13c566df45SLoGin driver::base::{ 1408a2ee40SLoGin class::{class_manager, Class}, 15c566df45SLoGin device::{ 16c566df45SLoGin bus::Bus, 17c566df45SLoGin device_manager, 18c566df45SLoGin device_number::{DeviceNumber, Major}, 19c566df45SLoGin driver::Driver, 20c566df45SLoGin sys_dev_char_kset, Device, DeviceType, IdTable, 21c566df45SLoGin }, 22c566df45SLoGin kobject::{KObjType, KObject, KObjectState, LockedKObjectState}, 23c566df45SLoGin kset::KSet, 2408a2ee40SLoGin subsys::SubSysPrivate, 25c566df45SLoGin }, 2602343d0bSLoGin filesystem::{ 2702343d0bSLoGin devfs::{devfs_register, DevFS, DeviceINode}, 2802343d0bSLoGin kernfs::KernFSInode, 2902343d0bSLoGin sysfs::AttributeGroup, 3002343d0bSLoGin vfs::{ 3102343d0bSLoGin file::FileMode, syscall::ModeType, FilePrivateData, FileSystem, FileType, IndexNode, 3202343d0bSLoGin Metadata, 3302343d0bSLoGin }, 3402343d0bSLoGin }, 35c566df45SLoGin init::initcall::INITCALL_SUBSYS, 36c566df45SLoGin libs::{ 37c566df45SLoGin rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}, 3802343d0bSLoGin spinlock::{SpinLock, SpinLockGuard}, 39c566df45SLoGin }, 4008a2ee40SLoGin }; 4108a2ee40SLoGin 42c566df45SLoGin use super::{fbcon::fb_console_init, fbsysfs::FbDeviceAttrGroup, FbId, FrameBuffer}; 4308a2ee40SLoGin 4408a2ee40SLoGin /// `/sys/class/graphics` 的 class 实例 4508a2ee40SLoGin static mut CLASS_GRAPHICS_INSTANCE: Option<Arc<GraphicsClass>> = None; 4608a2ee40SLoGin 47c566df45SLoGin lazy_static! { 48c566df45SLoGin /// 帧缓冲区管理器 49c566df45SLoGin static ref FRAME_BUFFER_MANAGER: FrameBufferManager = FrameBufferManager::new(); 50c566df45SLoGin } 51c566df45SLoGin 5208a2ee40SLoGin /// 获取 `/sys/class/graphics` 的 class 实例 5308a2ee40SLoGin #[inline(always)] 5408a2ee40SLoGin #[allow(dead_code)] 5508a2ee40SLoGin pub fn sys_class_graphics_instance() -> Option<&'static Arc<GraphicsClass>> { 5608a2ee40SLoGin unsafe { CLASS_GRAPHICS_INSTANCE.as_ref() } 5708a2ee40SLoGin } 5808a2ee40SLoGin 59c566df45SLoGin #[inline(always)] 60c566df45SLoGin pub fn frame_buffer_manager() -> &'static FrameBufferManager { 61c566df45SLoGin &FRAME_BUFFER_MANAGER 62c566df45SLoGin } 63c566df45SLoGin 6408a2ee40SLoGin /// 初始化帧缓冲区子系统 65c566df45SLoGin #[unified_init(INITCALL_SUBSYS)] 6608a2ee40SLoGin pub fn fbmem_init() -> Result<(), SystemError> { 6708a2ee40SLoGin let graphics_class = GraphicsClass::new(); 6808a2ee40SLoGin class_manager().class_register(&(graphics_class.clone() as Arc<dyn Class>))?; 6908a2ee40SLoGin 7008a2ee40SLoGin unsafe { 7108a2ee40SLoGin CLASS_GRAPHICS_INSTANCE = Some(graphics_class); 7208a2ee40SLoGin } 7308a2ee40SLoGin 7408a2ee40SLoGin fb_console_init()?; 7508a2ee40SLoGin return Ok(()); 7608a2ee40SLoGin } 7708a2ee40SLoGin 7808a2ee40SLoGin /// `/sys/class/graphics` 类 7908a2ee40SLoGin #[derive(Debug)] 8008a2ee40SLoGin pub struct GraphicsClass { 8108a2ee40SLoGin subsystem: SubSysPrivate, 8208a2ee40SLoGin } 8308a2ee40SLoGin 8408a2ee40SLoGin impl GraphicsClass { 8508a2ee40SLoGin const NAME: &'static str = "graphics"; 8608a2ee40SLoGin pub fn new() -> Arc<Self> { 8708a2ee40SLoGin let r = Self { 8808a2ee40SLoGin subsystem: SubSysPrivate::new(Self::NAME.to_string(), None, None, &[]), 8908a2ee40SLoGin }; 9008a2ee40SLoGin let r = Arc::new(r); 9108a2ee40SLoGin r.subsystem() 9208a2ee40SLoGin .set_class(Some(Arc::downgrade(&r) as Weak<dyn Class>)); 9308a2ee40SLoGin 9408a2ee40SLoGin return r; 9508a2ee40SLoGin } 9608a2ee40SLoGin } 9708a2ee40SLoGin 9808a2ee40SLoGin impl Class for GraphicsClass { 9908a2ee40SLoGin fn name(&self) -> &'static str { 10008a2ee40SLoGin return Self::NAME; 10108a2ee40SLoGin } 10208a2ee40SLoGin 10308a2ee40SLoGin fn dev_kobj(&self) -> Option<Arc<dyn KObject>> { 10408a2ee40SLoGin Some(sys_dev_char_kset() as Arc<dyn KObject>) 10508a2ee40SLoGin } 10608a2ee40SLoGin 10708a2ee40SLoGin fn set_dev_kobj(&self, _kobj: Arc<dyn KObject>) { 10808a2ee40SLoGin unimplemented!("GraphicsClass::set_dev_kobj"); 10908a2ee40SLoGin } 11008a2ee40SLoGin 11108a2ee40SLoGin fn subsystem(&self) -> &SubSysPrivate { 11208a2ee40SLoGin return &self.subsystem; 11308a2ee40SLoGin } 11408a2ee40SLoGin } 115c566df45SLoGin 116c566df45SLoGin /// 帧缓冲区管理器 117c566df45SLoGin #[derive(Debug)] 118c566df45SLoGin pub struct FrameBufferManager { 119c566df45SLoGin inner: RwLock<InnerFrameBufferManager>, 120c566df45SLoGin } 121c566df45SLoGin 122c566df45SLoGin #[derive(Debug)] 123c566df45SLoGin struct InnerFrameBufferManager { 124c566df45SLoGin /// 已经注册的帧缓冲区 125c566df45SLoGin registered_fbs: [Option<Arc<dyn FrameBuffer>>; FrameBufferManager::FB_MAX], 126c566df45SLoGin } 127c566df45SLoGin 128c566df45SLoGin impl FrameBufferManager { 129c566df45SLoGin pub const FB_MAX: usize = 32; 130c566df45SLoGin pub fn new() -> Self { 131c566df45SLoGin Self { 132c566df45SLoGin inner: RwLock::new(InnerFrameBufferManager { 133c566df45SLoGin registered_fbs: Default::default(), 134c566df45SLoGin }), 135c566df45SLoGin } 136c566df45SLoGin } 137c566df45SLoGin 138c566df45SLoGin /// 注册一个帧缓冲区 139c566df45SLoGin /// 140c566df45SLoGin /// # 参数 141c566df45SLoGin /// 142c566df45SLoGin /// - fb: 帧缓冲区 143c566df45SLoGin pub fn register_fb(&self, fb: Arc<dyn FrameBuffer>) -> Result<FbId, SystemError> { 144c566df45SLoGin let id = self.generate_fb_id().expect("no more fb id"); 145c566df45SLoGin fb.set_fb_id(id); 146c566df45SLoGin let fb_device = FbDevice::new(Arc::downgrade(&fb) as Weak<dyn FrameBuffer>, id); 147c566df45SLoGin device_manager().device_default_initialize(&(fb_device.clone() as Arc<dyn Device>)); 148c566df45SLoGin fb_device.set_parent(Some(Arc::downgrade(&(fb.clone() as Arc<dyn KObject>)))); 149c566df45SLoGin 150c566df45SLoGin fb.set_fb_device(Some(fb_device.clone())); 151c566df45SLoGin 152c566df45SLoGin device_manager().add_device(fb_device.clone() as Arc<dyn Device>)?; 15302343d0bSLoGin // 添加到devfs 15402343d0bSLoGin devfs_register(&fb_device.name(), fb_device.clone()).map_err(|e| { 15502343d0bSLoGin kerror!( 15602343d0bSLoGin "register fb device '{}' to devfs failed: {:?}", 15702343d0bSLoGin fb_device.name(), 15802343d0bSLoGin e 15902343d0bSLoGin ); 16002343d0bSLoGin device_manager().remove(&(fb_device.clone() as Arc<dyn Device>)); 16102343d0bSLoGin e 16202343d0bSLoGin })?; 163c566df45SLoGin 164c566df45SLoGin // todo: 从Modedb中获取信息 165c566df45SLoGin // 参考: https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbmem.c#1584 166c566df45SLoGin 167c566df45SLoGin let mut inner = self.inner.write(); 168c566df45SLoGin inner.registered_fbs[id.data() as usize] = Some(fb.clone() as Arc<dyn FrameBuffer>); 169c566df45SLoGin 170c566df45SLoGin // todo: 把fb跟fbcon关联起来 171c566df45SLoGin return Ok(id); 172c566df45SLoGin } 173c566df45SLoGin 174c566df45SLoGin /// 注销一个帧缓冲区 175c566df45SLoGin /// 176c566df45SLoGin /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbmem.c#1726 177c566df45SLoGin #[allow(dead_code)] 178c566df45SLoGin pub fn unregister_fb(&self, _fb: Arc<dyn FrameBuffer>) -> Result<(), SystemError> { 179c566df45SLoGin todo!("unregister_fb") 180c566df45SLoGin } 181c566df45SLoGin 182c566df45SLoGin /// 根据id查找帧缓冲区 183c566df45SLoGin pub fn find_fb_by_id(&self, id: FbId) -> Result<Option<Arc<dyn FrameBuffer>>, SystemError> { 184c566df45SLoGin if unlikely(!id.is_valid()) { 185c566df45SLoGin return Err(SystemError::EINVAL); 186c566df45SLoGin } 187c566df45SLoGin 188c566df45SLoGin let inner = self.inner.read(); 189c566df45SLoGin return Ok(inner.registered_fbs[id.data() as usize].clone()); 190c566df45SLoGin } 191c566df45SLoGin 192c566df45SLoGin fn generate_fb_id(&self) -> Option<FbId> { 193c566df45SLoGin for i in 0..Self::FB_MAX { 194c566df45SLoGin if self.inner.read().registered_fbs[i].is_none() { 195c566df45SLoGin return Some(FbId::new(i as u32)); 196c566df45SLoGin } 197c566df45SLoGin } 198c566df45SLoGin return None; 199c566df45SLoGin } 200c566df45SLoGin } 201c566df45SLoGin 202c566df45SLoGin /// 抽象的帧缓冲区设备 203c566df45SLoGin /// 204c566df45SLoGin /// 对应于`/sys/class/graphics/fb(x)`目录下的设备, 其中`(x)`为帧缓冲区的id 205c566df45SLoGin /// 206c566df45SLoGin /// 该设备的父设备为真实的帧缓冲区设备 207c566df45SLoGin #[derive(Debug)] 208c566df45SLoGin #[cast_to([sync] Device)] 209c566df45SLoGin pub struct FbDevice { 210c566df45SLoGin inner: SpinLock<InnerFbDevice>, 211c566df45SLoGin kobj_state: LockedKObjectState, 212c566df45SLoGin } 213c566df45SLoGin 214c566df45SLoGin impl FbDevice { 215c566df45SLoGin pub const BASENAME: &'static str = "fb"; 216c566df45SLoGin fn new(fb: Weak<dyn FrameBuffer>, id: FbId) -> Arc<Self> { 21702343d0bSLoGin let r = Arc::new(Self { 218c566df45SLoGin inner: SpinLock::new(InnerFbDevice { 219c566df45SLoGin fb, 220c566df45SLoGin kern_inode: None, 221c566df45SLoGin parent: None, 222c566df45SLoGin kset: None, 223c566df45SLoGin ktype: None, 224c566df45SLoGin fb_id: id, 22502343d0bSLoGin device_inode_fs: None, 22602343d0bSLoGin devfs_metadata: Metadata::new( 22702343d0bSLoGin FileType::FramebufferDevice, 22802343d0bSLoGin ModeType::from_bits_truncate(0o666), 22902343d0bSLoGin ), 230c566df45SLoGin }), 231c566df45SLoGin kobj_state: LockedKObjectState::new(None), 23202343d0bSLoGin }); 23302343d0bSLoGin 23402343d0bSLoGin let mut inner_guard = r.inner.lock(); 23502343d0bSLoGin 23602343d0bSLoGin inner_guard.devfs_metadata.raw_dev = r.do_device_number(&inner_guard); 23702343d0bSLoGin drop(inner_guard); 23802343d0bSLoGin 23902343d0bSLoGin return r; 240c566df45SLoGin } 241c566df45SLoGin 242c566df45SLoGin pub fn framebuffer(&self) -> Option<Arc<dyn FrameBuffer>> { 243c566df45SLoGin self.inner.lock().fb.upgrade() 244c566df45SLoGin } 24502343d0bSLoGin 24602343d0bSLoGin /// 获取设备号 24702343d0bSLoGin pub fn device_number(&self) -> DeviceNumber { 24802343d0bSLoGin let inner_guard = self.inner.lock(); 24902343d0bSLoGin self.do_device_number(&inner_guard) 25002343d0bSLoGin } 25102343d0bSLoGin 25202343d0bSLoGin fn do_device_number(&self, inner_guard: &SpinLockGuard<'_, InnerFbDevice>) -> DeviceNumber { 25302343d0bSLoGin DeviceNumber::new(Major::FB_MAJOR, inner_guard.fb_id.data()) 25402343d0bSLoGin } 255c566df45SLoGin } 256c566df45SLoGin 257c566df45SLoGin #[derive(Debug)] 258c566df45SLoGin struct InnerFbDevice { 259c566df45SLoGin fb: Weak<dyn FrameBuffer>, 260c566df45SLoGin kern_inode: Option<Arc<KernFSInode>>, 261c566df45SLoGin parent: Option<Weak<dyn KObject>>, 262c566df45SLoGin kset: Option<Arc<KSet>>, 263c566df45SLoGin ktype: Option<&'static dyn KObjType>, 264c566df45SLoGin /// 帧缓冲区id 265c566df45SLoGin fb_id: FbId, 26602343d0bSLoGin 26702343d0bSLoGin /// device inode要求的字段 26802343d0bSLoGin device_inode_fs: Option<Weak<DevFS>>, 26902343d0bSLoGin devfs_metadata: Metadata, 270c566df45SLoGin } 271c566df45SLoGin 272c566df45SLoGin impl KObject for FbDevice { 273c566df45SLoGin fn as_any_ref(&self) -> &dyn core::any::Any { 274c566df45SLoGin self 275c566df45SLoGin } 276c566df45SLoGin 277c566df45SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 278c566df45SLoGin self.inner.lock().kern_inode = inode; 279c566df45SLoGin } 280c566df45SLoGin 281c566df45SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 282c566df45SLoGin self.inner.lock().kern_inode.clone() 283c566df45SLoGin } 284c566df45SLoGin 285c566df45SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 286c566df45SLoGin self.inner.lock().parent.clone() 287c566df45SLoGin } 288c566df45SLoGin 289c566df45SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 290c566df45SLoGin self.inner.lock().parent = parent; 291c566df45SLoGin } 292c566df45SLoGin 293c566df45SLoGin fn kset(&self) -> Option<Arc<KSet>> { 294c566df45SLoGin self.inner.lock().kset.clone() 295c566df45SLoGin } 296c566df45SLoGin 297c566df45SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 298c566df45SLoGin self.inner.lock().kset = kset; 299c566df45SLoGin } 300c566df45SLoGin 301c566df45SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 302c566df45SLoGin self.inner.lock().ktype 303c566df45SLoGin } 304c566df45SLoGin 305c566df45SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 306c566df45SLoGin self.inner.lock().ktype = ktype; 307c566df45SLoGin } 308c566df45SLoGin 309c566df45SLoGin fn name(&self) -> String { 310c566df45SLoGin format!("{}{}", Self::BASENAME, self.inner.lock().fb_id.data()) 311c566df45SLoGin } 312c566df45SLoGin 313c566df45SLoGin fn set_name(&self, _name: String) { 314c566df45SLoGin // do nothing 315c566df45SLoGin } 316c566df45SLoGin 317c566df45SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 318c566df45SLoGin self.kobj_state.read() 319c566df45SLoGin } 320c566df45SLoGin 321c566df45SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 322c566df45SLoGin self.kobj_state.write() 323c566df45SLoGin } 324c566df45SLoGin 325c566df45SLoGin fn set_kobj_state(&self, state: KObjectState) { 326c566df45SLoGin *self.kobj_state.write() = state; 327c566df45SLoGin } 328c566df45SLoGin } 329c566df45SLoGin 330c566df45SLoGin impl Device for FbDevice { 331c566df45SLoGin fn dev_type(&self) -> DeviceType { 332c566df45SLoGin DeviceType::Char 333c566df45SLoGin } 334c566df45SLoGin 335c566df45SLoGin fn id_table(&self) -> IdTable { 33602343d0bSLoGin IdTable::new(Self::BASENAME.to_string(), Some(self.device_number())) 337c566df45SLoGin } 338c566df45SLoGin 339c566df45SLoGin fn set_bus(&self, _bus: Option<Weak<dyn Bus>>) { 340c566df45SLoGin todo!() 341c566df45SLoGin } 342c566df45SLoGin 343c566df45SLoGin fn class(&self) -> Option<Arc<dyn Class>> { 344c566df45SLoGin sys_class_graphics_instance().map(|ins| ins.clone() as Arc<dyn Class>) 345c566df45SLoGin } 3464256da7fSLoGin fn set_class(&self, _class: Option<Weak<dyn Class>>) { 347c566df45SLoGin // do nothing 348c566df45SLoGin } 349c566df45SLoGin 350c566df45SLoGin fn driver(&self) -> Option<Arc<dyn Driver>> { 351c566df45SLoGin None 352c566df45SLoGin } 353c566df45SLoGin 354c566df45SLoGin fn set_driver(&self, _driver: Option<Weak<dyn Driver>>) { 355c566df45SLoGin // do nothing 356c566df45SLoGin } 357c566df45SLoGin 358c566df45SLoGin fn is_dead(&self) -> bool { 359c566df45SLoGin false 360c566df45SLoGin } 361c566df45SLoGin 362c566df45SLoGin fn can_match(&self) -> bool { 363c566df45SLoGin false 364c566df45SLoGin } 365c566df45SLoGin 366c566df45SLoGin fn set_can_match(&self, _can_match: bool) { 367c566df45SLoGin // do nothing 368c566df45SLoGin } 369c566df45SLoGin 370c566df45SLoGin fn state_synced(&self) -> bool { 371c566df45SLoGin true 372c566df45SLoGin } 373c566df45SLoGin 374c566df45SLoGin fn attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]> { 375c566df45SLoGin Some(&[&FbDeviceAttrGroup]) 376c566df45SLoGin } 377c566df45SLoGin } 37802343d0bSLoGin 37902343d0bSLoGin impl DeviceINode for FbDevice { 38002343d0bSLoGin fn set_fs(&self, fs: Weak<DevFS>) { 38102343d0bSLoGin self.inner.lock().device_inode_fs = Some(fs); 38202343d0bSLoGin } 38302343d0bSLoGin } 38402343d0bSLoGin 38502343d0bSLoGin impl IndexNode for FbDevice { 386*dfe53cf0SGnoCiYeH fn open( 387*dfe53cf0SGnoCiYeH &self, 388*dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 389*dfe53cf0SGnoCiYeH _mode: &FileMode, 390*dfe53cf0SGnoCiYeH ) -> Result<(), SystemError> { 39102343d0bSLoGin Ok(()) 39202343d0bSLoGin } 39302343d0bSLoGin 394*dfe53cf0SGnoCiYeH fn close(&self, _data: SpinLockGuard<FilePrivateData>) -> Result<(), SystemError> { 39502343d0bSLoGin Ok(()) 39602343d0bSLoGin } 39702343d0bSLoGin fn read_at( 39802343d0bSLoGin &self, 39902343d0bSLoGin offset: usize, 40002343d0bSLoGin len: usize, 40102343d0bSLoGin buf: &mut [u8], 402*dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 40302343d0bSLoGin ) -> Result<usize, SystemError> { 40402343d0bSLoGin let fb = self.inner.lock().fb.upgrade().unwrap(); 40502343d0bSLoGin return fb.fb_read(&mut buf[0..len], offset); 40602343d0bSLoGin } 40702343d0bSLoGin 40802343d0bSLoGin fn write_at( 40902343d0bSLoGin &self, 41002343d0bSLoGin offset: usize, 41102343d0bSLoGin len: usize, 41202343d0bSLoGin buf: &[u8], 413*dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 41402343d0bSLoGin ) -> Result<usize, SystemError> { 41502343d0bSLoGin let fb = self.inner.lock().fb.upgrade().unwrap(); 41602343d0bSLoGin return fb.fb_write(&buf[0..len], offset); 41702343d0bSLoGin } 41802343d0bSLoGin 41902343d0bSLoGin fn fs(&self) -> Arc<dyn FileSystem> { 42002343d0bSLoGin self.inner 42102343d0bSLoGin .lock() 42202343d0bSLoGin .device_inode_fs 42302343d0bSLoGin .as_ref() 42402343d0bSLoGin .unwrap() 42502343d0bSLoGin .upgrade() 42602343d0bSLoGin .unwrap() 42702343d0bSLoGin } 42802343d0bSLoGin 42902343d0bSLoGin fn as_any_ref(&self) -> &dyn core::any::Any { 43002343d0bSLoGin self 43102343d0bSLoGin } 43202343d0bSLoGin 43302343d0bSLoGin fn list(&self) -> Result<Vec<String>, SystemError> { 43402343d0bSLoGin todo!() 43502343d0bSLoGin } 43602343d0bSLoGin 43702343d0bSLoGin fn metadata(&self) -> Result<Metadata, SystemError> { 43802343d0bSLoGin Ok(self.inner.lock().devfs_metadata.clone()) 43902343d0bSLoGin } 44002343d0bSLoGin 44102343d0bSLoGin fn resize(&self, _len: usize) -> Result<(), SystemError> { 44202343d0bSLoGin return Ok(()); 44302343d0bSLoGin } 44402343d0bSLoGin } 445