1c566df45SLoGin use core::{ 2c566df45SLoGin ffi::{c_uint, c_void}, 3c566df45SLoGin mem::MaybeUninit, 4c566df45SLoGin sync::atomic::AtomicBool, 5c566df45SLoGin }; 6c566df45SLoGin 7c566df45SLoGin use alloc::{ 8c566df45SLoGin string::{String, ToString}, 9c566df45SLoGin sync::{Arc, Weak}, 10c566df45SLoGin vec::Vec, 11c566df45SLoGin }; 12*2eab6dd7S曾俊 use log::{info, warn}; 13c566df45SLoGin use system_error::SystemError; 14c566df45SLoGin use unified_init::macros::unified_init; 15c566df45SLoGin 16c566df45SLoGin use crate::{ 17c566df45SLoGin arch::MMArch, 18c566df45SLoGin driver::{ 19c566df45SLoGin base::{ 20c566df45SLoGin class::Class, 21c566df45SLoGin device::{ 22c566df45SLoGin bus::Bus, device_manager, driver::Driver, Device, DeviceState, DeviceType, IdTable, 23c566df45SLoGin }, 24c566df45SLoGin kobject::{KObjType, KObject, KObjectState, LockedKObjectState}, 25c566df45SLoGin kset::KSet, 26c566df45SLoGin platform::{ 27c566df45SLoGin platform_device::{platform_device_manager, PlatformDevice}, 28c566df45SLoGin platform_driver::{platform_driver_manager, PlatformDriver}, 29c566df45SLoGin }, 30c566df45SLoGin }, 3152da9a59SGnoCiYeH serial::serial8250::send_to_default_serial8250_port, 3252da9a59SGnoCiYeH video::fbdev::base::{fbmem::frame_buffer_manager, FbVisual, FRAME_BUFFER_SET}, 33c566df45SLoGin }, 34c566df45SLoGin filesystem::{ 35c566df45SLoGin kernfs::KernFSInode, 36c566df45SLoGin sysfs::{file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport}, 37c566df45SLoGin vfs::syscall::ModeType, 38c566df45SLoGin }, 39c566df45SLoGin include::bindings::bindings::{ 40c566df45SLoGin multiboot2_get_Framebuffer_info, multiboot2_iter, multiboot_tag_framebuffer_info_t, 41c75ef4e2SLoGin FRAME_BUFFER_MAPPING_OFFSET, 42c566df45SLoGin }, 43c566df45SLoGin init::{boot_params, initcall::INITCALL_DEVICE}, 44c566df45SLoGin libs::{ 45c566df45SLoGin align::page_align_up, 46c566df45SLoGin once::Once, 47c566df45SLoGin rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}, 48c566df45SLoGin spinlock::SpinLock, 49c566df45SLoGin }, 50c566df45SLoGin mm::{ 51c566df45SLoGin allocator::page_frame::PageFrameCount, no_init::pseudo_map_phys, MemoryManagementArch, 52c566df45SLoGin PhysAddr, VirtAddr, 53c566df45SLoGin }, 54c566df45SLoGin }; 55c566df45SLoGin 56c566df45SLoGin use super::base::{ 57c566df45SLoGin fbmem::FbDevice, BlankMode, BootTimeVideoType, FbAccel, FbActivateFlags, FbId, FbState, FbType, 58c566df45SLoGin FbVModeFlags, FbVarScreenInfo, FbVideoMode, FixedScreenInfo, FrameBuffer, FrameBufferInfo, 5952da9a59SGnoCiYeH FrameBufferInfoData, FrameBufferOps, 60c566df45SLoGin }; 61c566df45SLoGin 62c566df45SLoGin /// 当前机器上面是否有vesa帧缓冲区 63c566df45SLoGin static HAS_VESA_FB: AtomicBool = AtomicBool::new(false); 64c566df45SLoGin 65c566df45SLoGin lazy_static! { 66c566df45SLoGin static ref VESAFB_FIX_INFO: RwLock<FixedScreenInfo> = RwLock::new(FixedScreenInfo { 67c566df45SLoGin id: FixedScreenInfo::name2id("VESA VGA"), 68c566df45SLoGin fb_type: FbType::PackedPixels, 69c566df45SLoGin accel: FbAccel::None, 70c566df45SLoGin ..Default::default() 71c566df45SLoGin }); 72c566df45SLoGin static ref VESAFB_DEFINED: RwLock<FbVarScreenInfo> = RwLock::new(FbVarScreenInfo { 73c566df45SLoGin activate: FbActivateFlags::FB_ACTIVATE_NOW, 74c566df45SLoGin height: None, 75c566df45SLoGin width: None, 76c566df45SLoGin right_margin: 32, 77c566df45SLoGin upper_margin: 16, 78c566df45SLoGin lower_margin: 4, 79c566df45SLoGin vsync_len: 4, 80c566df45SLoGin vmode: FbVModeFlags::FB_VMODE_NONINTERLACED, 81c566df45SLoGin 82c566df45SLoGin ..Default::default() 83c566df45SLoGin }); 84c566df45SLoGin } 85c566df45SLoGin 86c566df45SLoGin #[derive(Debug)] 87c566df45SLoGin #[cast_to([sync] Device)] 88c566df45SLoGin #[cast_to([sync] PlatformDevice)] 89c566df45SLoGin pub struct VesaFb { 90c566df45SLoGin inner: SpinLock<InnerVesaFb>, 91c566df45SLoGin kobj_state: LockedKObjectState, 9252da9a59SGnoCiYeH fb_data: RwLock<FrameBufferInfoData>, 93c566df45SLoGin } 94c566df45SLoGin 95c566df45SLoGin impl VesaFb { 96c566df45SLoGin pub const NAME: &'static str = "vesa_vga"; 97c566df45SLoGin pub fn new() -> Self { 9852da9a59SGnoCiYeH let mut fb_info_data = FrameBufferInfoData::new(); 9952da9a59SGnoCiYeH fb_info_data.pesudo_palette.resize(256, 0); 100c566df45SLoGin return Self { 101c566df45SLoGin inner: SpinLock::new(InnerVesaFb { 102c566df45SLoGin bus: None, 103c566df45SLoGin class: None, 104c566df45SLoGin driver: None, 105c566df45SLoGin kern_inode: None, 106c566df45SLoGin parent: None, 107c566df45SLoGin kset: None, 108c566df45SLoGin kobj_type: None, 109c566df45SLoGin device_state: DeviceState::NotInitialized, 110c566df45SLoGin pdev_id: 0, 111c566df45SLoGin pdev_id_auto: false, 112c566df45SLoGin fb_id: FbId::INIT, 113c566df45SLoGin fb_device: None, 114c566df45SLoGin fb_state: FbState::Suspended, 115c566df45SLoGin }), 116c566df45SLoGin kobj_state: LockedKObjectState::new(None), 11752da9a59SGnoCiYeH fb_data: RwLock::new(fb_info_data), 118c566df45SLoGin }; 119c566df45SLoGin } 120c566df45SLoGin } 121c566df45SLoGin 122c566df45SLoGin #[derive(Debug)] 123c566df45SLoGin struct InnerVesaFb { 124c566df45SLoGin bus: Option<Weak<dyn Bus>>, 1254256da7fSLoGin class: Option<Weak<dyn Class>>, 126c566df45SLoGin driver: Option<Weak<dyn Driver>>, 127c566df45SLoGin kern_inode: Option<Arc<KernFSInode>>, 128c566df45SLoGin parent: Option<Weak<dyn KObject>>, 129c566df45SLoGin kset: Option<Arc<KSet>>, 130c566df45SLoGin kobj_type: Option<&'static dyn KObjType>, 131c566df45SLoGin device_state: DeviceState, 132c566df45SLoGin pdev_id: i32, 133c566df45SLoGin pdev_id_auto: bool, 134c566df45SLoGin fb_id: FbId, 135c566df45SLoGin fb_device: Option<Arc<FbDevice>>, 136c566df45SLoGin fb_state: FbState, 137c566df45SLoGin } 138c566df45SLoGin 139c566df45SLoGin impl FrameBuffer for VesaFb { 140c566df45SLoGin fn fb_id(&self) -> FbId { 141c566df45SLoGin self.inner.lock().fb_id 142c566df45SLoGin } 143c566df45SLoGin 144c566df45SLoGin fn set_fb_id(&self, id: FbId) { 145c566df45SLoGin self.inner.lock().fb_id = id; 146c566df45SLoGin } 147c566df45SLoGin } 148c566df45SLoGin 149c566df45SLoGin impl PlatformDevice for VesaFb { 150c566df45SLoGin fn pdev_name(&self) -> &str { 151c566df45SLoGin Self::NAME 152c566df45SLoGin } 153c566df45SLoGin 154c566df45SLoGin fn set_pdev_id(&self, id: i32) { 155c566df45SLoGin self.inner.lock().pdev_id = id; 156c566df45SLoGin } 157c566df45SLoGin 158c566df45SLoGin fn set_pdev_id_auto(&self, id_auto: bool) { 159c566df45SLoGin self.inner.lock().pdev_id_auto = id_auto; 160c566df45SLoGin } 161c566df45SLoGin 162c566df45SLoGin fn is_initialized(&self) -> bool { 163c566df45SLoGin self.inner.lock().device_state == DeviceState::Initialized 164c566df45SLoGin } 165c566df45SLoGin 166c566df45SLoGin fn set_state(&self, set_state: DeviceState) { 167c566df45SLoGin self.inner.lock().device_state = set_state; 168c566df45SLoGin } 169c566df45SLoGin } 170c566df45SLoGin 171c566df45SLoGin impl Device for VesaFb { 172c566df45SLoGin fn dev_type(&self) -> DeviceType { 173c566df45SLoGin DeviceType::Char 174c566df45SLoGin } 175c566df45SLoGin 176c566df45SLoGin fn id_table(&self) -> IdTable { 177c566df45SLoGin IdTable::new(self.name(), None) 178c566df45SLoGin } 179c566df45SLoGin 180c566df45SLoGin fn bus(&self) -> Option<Weak<dyn Bus>> { 181c566df45SLoGin self.inner.lock().bus.clone() 182c566df45SLoGin } 183c566df45SLoGin 184c566df45SLoGin fn set_bus(&self, bus: Option<Weak<dyn Bus>>) { 185c566df45SLoGin self.inner.lock().bus = bus; 186c566df45SLoGin } 187c566df45SLoGin 1884256da7fSLoGin fn set_class(&self, class: Option<Weak<dyn Class>>) { 189c566df45SLoGin self.inner.lock().class = class; 190c566df45SLoGin } 191c566df45SLoGin 1924256da7fSLoGin fn class(&self) -> Option<Arc<dyn Class>> { 1934256da7fSLoGin let mut guard = self.inner.lock(); 1944256da7fSLoGin 1954256da7fSLoGin let r = guard.class.clone()?.upgrade(); 1964256da7fSLoGin if r.is_none() { 1974256da7fSLoGin // 为了让弱引用失效 1984256da7fSLoGin guard.class = None; 1994256da7fSLoGin } 2004256da7fSLoGin 2014256da7fSLoGin return r; 2024256da7fSLoGin } 2034256da7fSLoGin 204c566df45SLoGin fn driver(&self) -> Option<Arc<dyn Driver>> { 205c566df45SLoGin self.inner.lock().driver.clone()?.upgrade() 206c566df45SLoGin } 207c566df45SLoGin 208c566df45SLoGin fn set_driver(&self, driver: Option<Weak<dyn Driver>>) { 209c566df45SLoGin self.inner.lock().driver = driver; 210c566df45SLoGin } 211c566df45SLoGin 212c566df45SLoGin fn is_dead(&self) -> bool { 213c566df45SLoGin false 214c566df45SLoGin } 215c566df45SLoGin 216c566df45SLoGin fn can_match(&self) -> bool { 217c566df45SLoGin true 218c566df45SLoGin } 219c566df45SLoGin 220c566df45SLoGin fn set_can_match(&self, _can_match: bool) {} 221c566df45SLoGin 222c566df45SLoGin fn state_synced(&self) -> bool { 223c566df45SLoGin true 224c566df45SLoGin } 225c566df45SLoGin } 226c566df45SLoGin 227c566df45SLoGin impl KObject for VesaFb { 228c566df45SLoGin fn as_any_ref(&self) -> &dyn core::any::Any { 229c566df45SLoGin self 230c566df45SLoGin } 231c566df45SLoGin 232c566df45SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 233c566df45SLoGin self.inner.lock().kern_inode = inode; 234c566df45SLoGin } 235c566df45SLoGin 236c566df45SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 237c566df45SLoGin self.inner.lock().kern_inode.clone() 238c566df45SLoGin } 239c566df45SLoGin 240c566df45SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 241c566df45SLoGin self.inner.lock().parent.clone() 242c566df45SLoGin } 243c566df45SLoGin 244c566df45SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 245c566df45SLoGin self.inner.lock().parent = parent; 246c566df45SLoGin } 247c566df45SLoGin 248c566df45SLoGin fn kset(&self) -> Option<Arc<KSet>> { 249c566df45SLoGin self.inner.lock().kset.clone() 250c566df45SLoGin } 251c566df45SLoGin 252c566df45SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 253c566df45SLoGin self.inner.lock().kset = kset; 254c566df45SLoGin } 255c566df45SLoGin 256c566df45SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 257c566df45SLoGin self.inner.lock().kobj_type 258c566df45SLoGin } 259c566df45SLoGin 260c566df45SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 261c566df45SLoGin self.inner.lock().kobj_type = ktype; 262c566df45SLoGin } 263c566df45SLoGin 264c566df45SLoGin fn name(&self) -> String { 265c566df45SLoGin Self::NAME.to_string() 266c566df45SLoGin } 267c566df45SLoGin 268c566df45SLoGin fn set_name(&self, _name: String) { 269c566df45SLoGin // do nothing 270c566df45SLoGin } 271c566df45SLoGin 272c566df45SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 273c566df45SLoGin self.kobj_state.read() 274c566df45SLoGin } 275c566df45SLoGin 276c566df45SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 277c566df45SLoGin self.kobj_state.write() 278c566df45SLoGin } 279c566df45SLoGin 280c566df45SLoGin fn set_kobj_state(&self, state: KObjectState) { 281c566df45SLoGin *self.kobj_state.write() = state; 282c566df45SLoGin } 283c566df45SLoGin } 284c566df45SLoGin 285c566df45SLoGin impl FrameBufferOps for VesaFb { 286c566df45SLoGin fn fb_open(&self, _user: bool) { 287c566df45SLoGin todo!() 288c566df45SLoGin } 289c566df45SLoGin 290c566df45SLoGin fn fb_release(&self, _user: bool) { 291c566df45SLoGin todo!() 292c566df45SLoGin } 293c566df45SLoGin 294c566df45SLoGin fn fb_set_color_register( 295c566df45SLoGin &self, 29652da9a59SGnoCiYeH regno: u16, 29752da9a59SGnoCiYeH mut red: u16, 29852da9a59SGnoCiYeH mut green: u16, 29952da9a59SGnoCiYeH mut blue: u16, 300c566df45SLoGin ) -> Result<(), SystemError> { 30152da9a59SGnoCiYeH let mut fb_data = self.framebuffer_info_data().write(); 30252da9a59SGnoCiYeH let var = self.current_fb_var(); 30352da9a59SGnoCiYeH if regno as usize >= fb_data.pesudo_palette.len() { 30452da9a59SGnoCiYeH return Err(SystemError::E2BIG); 30552da9a59SGnoCiYeH } 30652da9a59SGnoCiYeH 30752da9a59SGnoCiYeH if var.bits_per_pixel == 8 { 30852da9a59SGnoCiYeH todo!("vesa_setpalette todo"); 30952da9a59SGnoCiYeH } else if regno < 16 { 31052da9a59SGnoCiYeH match var.bits_per_pixel { 31152da9a59SGnoCiYeH 16 => { 31252da9a59SGnoCiYeH if var.red.offset == 10 { 31352da9a59SGnoCiYeH // RGB 1:5:5:5 31452da9a59SGnoCiYeH fb_data.pesudo_palette[regno as usize] = ((red as u32 & 0xf800) >> 1) 31552da9a59SGnoCiYeH | ((green as u32 & 0xf800) >> 6) 31652da9a59SGnoCiYeH | ((blue as u32 & 0xf800) >> 11); 31752da9a59SGnoCiYeH } else { 31852da9a59SGnoCiYeH fb_data.pesudo_palette[regno as usize] = (red as u32 & 0xf800) 31952da9a59SGnoCiYeH | ((green as u32 & 0xfc00) >> 5) 32052da9a59SGnoCiYeH | ((blue as u32 & 0xf800) >> 11); 32152da9a59SGnoCiYeH } 32252da9a59SGnoCiYeH } 32352da9a59SGnoCiYeH 24 | 32 => { 32452da9a59SGnoCiYeH red >>= 8; 32552da9a59SGnoCiYeH green >>= 8; 32652da9a59SGnoCiYeH blue >>= 8; 32752da9a59SGnoCiYeH fb_data.pesudo_palette[regno as usize] = ((red as u32) << var.red.offset) 32852da9a59SGnoCiYeH | ((green as u32) << var.green.offset) 32952da9a59SGnoCiYeH | ((blue as u32) << var.blue.offset); 33052da9a59SGnoCiYeH } 33152da9a59SGnoCiYeH _ => {} 33252da9a59SGnoCiYeH } 33352da9a59SGnoCiYeH } 33452da9a59SGnoCiYeH 33552da9a59SGnoCiYeH Ok(()) 336c566df45SLoGin } 337c566df45SLoGin 338c566df45SLoGin fn fb_blank(&self, _blank_mode: BlankMode) -> Result<(), SystemError> { 339c566df45SLoGin todo!() 340c566df45SLoGin } 341c566df45SLoGin 342c566df45SLoGin fn fb_destroy(&self) { 343c566df45SLoGin todo!() 344c566df45SLoGin } 34502343d0bSLoGin 34602343d0bSLoGin fn fb_read(&self, buf: &mut [u8], pos: usize) -> Result<usize, SystemError> { 34702343d0bSLoGin let bp = boot_params().read(); 34802343d0bSLoGin 34902343d0bSLoGin let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?; 35002343d0bSLoGin let size = self.current_fb_fix().smem_len; 35102343d0bSLoGin drop(bp); 35202343d0bSLoGin if pos >= size { 35302343d0bSLoGin return Ok(0); 35402343d0bSLoGin } 35502343d0bSLoGin 35602343d0bSLoGin let pos = pos as i64; 35702343d0bSLoGin let size = size as i64; 35802343d0bSLoGin 35902343d0bSLoGin let len = core::cmp::min(size - pos, buf.len() as i64) as usize; 36002343d0bSLoGin 36102343d0bSLoGin let slice = unsafe { core::slice::from_raw_parts(vaddr.as_ptr::<u8>(), size as usize) }; 36202343d0bSLoGin buf[..len].copy_from_slice(&slice[pos as usize..(pos as usize + len)]); 36302343d0bSLoGin 36402343d0bSLoGin return Ok(len); 36502343d0bSLoGin } 36602343d0bSLoGin 36702343d0bSLoGin fn fb_write(&self, buf: &[u8], pos: usize) -> Result<usize, SystemError> { 36802343d0bSLoGin let bp = boot_params().read(); 36902343d0bSLoGin 37002343d0bSLoGin let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?; 37102343d0bSLoGin let size = self.current_fb_fix().smem_len; 37202343d0bSLoGin 37302343d0bSLoGin if pos >= size { 37402343d0bSLoGin return Ok(0); 37502343d0bSLoGin } 37602343d0bSLoGin 37702343d0bSLoGin let pos = pos as i64; 37802343d0bSLoGin let size = size as i64; 37902343d0bSLoGin 38002343d0bSLoGin let len = core::cmp::min(size - pos, buf.len() as i64) as usize; 38102343d0bSLoGin 38202343d0bSLoGin let slice = unsafe { core::slice::from_raw_parts_mut(vaddr.as_ptr::<u8>(), size as usize) }; 38302343d0bSLoGin slice[pos as usize..(pos as usize + len)].copy_from_slice(&buf[..len]); 38402343d0bSLoGin 38502343d0bSLoGin return Ok(len); 38602343d0bSLoGin } 38752da9a59SGnoCiYeH 38852da9a59SGnoCiYeH fn fb_image_blit(&self, image: &super::base::FbImage) { 38952da9a59SGnoCiYeH self.generic_imageblit(image); 39052da9a59SGnoCiYeH } 39152da9a59SGnoCiYeH 39252da9a59SGnoCiYeH /// ## 填充矩形 39352da9a59SGnoCiYeH fn fb_fillrect(&self, rect: super::base::FillRectData) -> Result<(), SystemError> { 394*2eab6dd7S曾俊 // warn!("rect {rect:?}"); 39552da9a59SGnoCiYeH 39652da9a59SGnoCiYeH let boot_param = boot_params().read(); 39752da9a59SGnoCiYeH let screen_base = boot_param 39852da9a59SGnoCiYeH .screen_info 39952da9a59SGnoCiYeH .lfb_virt_base 40052da9a59SGnoCiYeH .ok_or(SystemError::ENODEV)?; 401b5b571e0SLoGin 402b5b571e0SLoGin let fg = if self.current_fb_fix().visual == FbVisual::TrueColor 40352da9a59SGnoCiYeH || self.current_fb_fix().visual == FbVisual::DirectColor 40452da9a59SGnoCiYeH { 405b5b571e0SLoGin self.fb_data.read().pesudo_palette[rect.color as usize] 40652da9a59SGnoCiYeH } else { 407b5b571e0SLoGin rect.color 408b5b571e0SLoGin }; 40952da9a59SGnoCiYeH 41052da9a59SGnoCiYeH let bpp = self.current_fb_var().bits_per_pixel; 41152da9a59SGnoCiYeH // 每行像素数 41252da9a59SGnoCiYeH let line_offset = self.current_fb_var().xres; 41352da9a59SGnoCiYeH match bpp { 41452da9a59SGnoCiYeH 32 => { 41552da9a59SGnoCiYeH let base = screen_base.as_ptr::<u32>(); 41652da9a59SGnoCiYeH 41752da9a59SGnoCiYeH for y in rect.dy..(rect.dy + rect.height) { 41852da9a59SGnoCiYeH for x in rect.dx..(rect.dx + rect.width) { 41952da9a59SGnoCiYeH unsafe { *base.add((y * line_offset + x) as usize) = fg }; 42052da9a59SGnoCiYeH } 42152da9a59SGnoCiYeH } 42252da9a59SGnoCiYeH } 4232755467cS曾俊 16 => { 4242755467cS曾俊 let base = screen_base.as_ptr::<u16>(); 4252755467cS曾俊 4262755467cS曾俊 for y in rect.dy..(rect.dy + rect.height) { 4272755467cS曾俊 for x in rect.dx..(rect.dx + rect.width) { 4282755467cS曾俊 unsafe { *base.add((y * line_offset + x) as usize) = 0x0000 }; 4292755467cS曾俊 } 4302755467cS曾俊 } 4312755467cS曾俊 } 4322755467cS曾俊 24 => { 4332755467cS曾俊 let base = screen_base.as_ptr::<[u8; 3]>(); 4342755467cS曾俊 4352755467cS曾俊 for y in rect.dy..(rect.dy + rect.height) { 4362755467cS曾俊 for x in rect.dx..(rect.dx + rect.width) { 4372755467cS曾俊 unsafe { *base.add((y * line_offset + x) as usize) = [0, 0, 0] }; 4382755467cS曾俊 } 4392755467cS曾俊 } 4402755467cS曾俊 } 4412755467cS曾俊 _ => { 4422755467cS曾俊 send_to_default_serial8250_port( 4432755467cS曾俊 format!("unsupported bit depth:{}!\n\0", bpp).as_bytes(), 4442755467cS曾俊 ); 4452755467cS曾俊 todo!() 4462755467cS曾俊 } 44752da9a59SGnoCiYeH } 44852da9a59SGnoCiYeH 44952da9a59SGnoCiYeH Ok(()) 45052da9a59SGnoCiYeH } 45152da9a59SGnoCiYeH 45252bcb59eSGnoCiYeH #[inline(never)] 45352bcb59eSGnoCiYeH fn fb_copyarea(&self, data: super::base::CopyAreaData) { 45452da9a59SGnoCiYeH let bp = boot_params().read(); 45552bcb59eSGnoCiYeH let base = bp.screen_info.lfb_virt_base.unwrap(); 45652da9a59SGnoCiYeH let var = self.current_fb_var(); 45752da9a59SGnoCiYeH 45852bcb59eSGnoCiYeH // 原区域或者目标区域全在屏幕外,则直接返回 45952bcb59eSGnoCiYeH if data.sx > var.xres as i32 46052bcb59eSGnoCiYeH || data.sy > var.yres as i32 46152bcb59eSGnoCiYeH || data.dx > var.xres as i32 46252bcb59eSGnoCiYeH || data.dy > var.yres as i32 46352bcb59eSGnoCiYeH || (data.sx + data.width as i32) < 0 46452bcb59eSGnoCiYeH || (data.sy + data.height as i32) < 0 46552bcb59eSGnoCiYeH || (data.dx + data.width as i32) < 0 46652bcb59eSGnoCiYeH || (data.dy + data.height as i32) < 0 46752da9a59SGnoCiYeH { 46852bcb59eSGnoCiYeH return; 46952da9a59SGnoCiYeH } 47052da9a59SGnoCiYeH 47152bcb59eSGnoCiYeH // 求两个矩形可视范围交集 47252bcb59eSGnoCiYeH let (s_visiable_x, s_w) = if data.sx < 0 { 47352bcb59eSGnoCiYeH (0, (data.width - ((-data.sx) as u32)).min(var.xres)) 47452bcb59eSGnoCiYeH } else { 47552bcb59eSGnoCiYeH let w = if data.sx as u32 + data.width > var.xres { 47652bcb59eSGnoCiYeH var.xres - data.sx as u32 47752bcb59eSGnoCiYeH } else { 47852bcb59eSGnoCiYeH data.width 47952bcb59eSGnoCiYeH }; 48052bcb59eSGnoCiYeH 48152bcb59eSGnoCiYeH (data.sx, w) 48252bcb59eSGnoCiYeH }; 48352bcb59eSGnoCiYeH let (s_visiable_y, s_h) = if data.sy < 0 { 48452bcb59eSGnoCiYeH (0, (data.height - ((-data.sy) as u32).min(var.yres))) 48552bcb59eSGnoCiYeH } else { 48652bcb59eSGnoCiYeH let h = if data.sy as u32 + data.height > var.yres { 48752bcb59eSGnoCiYeH var.yres - data.sy as u32 48852bcb59eSGnoCiYeH } else { 48952bcb59eSGnoCiYeH data.height 49052bcb59eSGnoCiYeH }; 49152bcb59eSGnoCiYeH 49252bcb59eSGnoCiYeH (data.sy, h) 49352bcb59eSGnoCiYeH }; 49452bcb59eSGnoCiYeH 49552bcb59eSGnoCiYeH let (d_visiable_x, d_w) = if data.dx < 0 { 49652bcb59eSGnoCiYeH (0, (data.width - ((-data.dx) as u32)).min(var.xres)) 49752bcb59eSGnoCiYeH } else { 49852bcb59eSGnoCiYeH let w = if data.dx as u32 + data.width > var.xres { 49952bcb59eSGnoCiYeH var.xres - data.dx as u32 50052bcb59eSGnoCiYeH } else { 50152bcb59eSGnoCiYeH data.width 50252bcb59eSGnoCiYeH }; 50352bcb59eSGnoCiYeH 50452bcb59eSGnoCiYeH (data.dx, w) 50552bcb59eSGnoCiYeH }; 50652bcb59eSGnoCiYeH let (d_visiable_y, d_h) = if data.dy < 0 { 50752bcb59eSGnoCiYeH (0, (data.height - ((-data.dy) as u32).min(var.yres))) 50852bcb59eSGnoCiYeH } else { 50952bcb59eSGnoCiYeH let h = if data.dy as u32 + data.height > var.yres { 51052bcb59eSGnoCiYeH var.yres - data.dy as u32 51152bcb59eSGnoCiYeH } else { 51252bcb59eSGnoCiYeH data.height 51352bcb59eSGnoCiYeH }; 51452bcb59eSGnoCiYeH 51552bcb59eSGnoCiYeH (data.dy, h) 51652bcb59eSGnoCiYeH }; 51752bcb59eSGnoCiYeH 51852bcb59eSGnoCiYeH // 可视范围无交集 51952bcb59eSGnoCiYeH if !(d_h + s_h > data.height && s_w + d_w > data.width) { 52052bcb59eSGnoCiYeH return; 52152bcb59eSGnoCiYeH } 52252bcb59eSGnoCiYeH 52352bcb59eSGnoCiYeH // 可视区域左上角相对于矩形的坐标 52452bcb59eSGnoCiYeH let s_relative_x = s_visiable_x - data.sx; 52552bcb59eSGnoCiYeH let s_relative_y = s_visiable_y - data.sy; 52652bcb59eSGnoCiYeH let d_relative_x = d_visiable_x - data.dx; 52752bcb59eSGnoCiYeH let d_relative_y = d_visiable_y - data.dy; 52852bcb59eSGnoCiYeH 52952bcb59eSGnoCiYeH let visiable_x = s_relative_x.max(d_relative_x); 53052bcb59eSGnoCiYeH let visiable_y = s_relative_y.max(d_relative_y); 53152bcb59eSGnoCiYeH let visiable_h = d_h + s_h - data.height; 53252bcb59eSGnoCiYeH let visiable_w = d_w + s_w - data.width; 53352bcb59eSGnoCiYeH 53452bcb59eSGnoCiYeH let s_real_x = (visiable_x + data.sx) as u32; 53552bcb59eSGnoCiYeH let s_real_y = (visiable_y + data.sy) as u32; 53652bcb59eSGnoCiYeH let d_real_x = (visiable_x + data.dx) as u32; 53752bcb59eSGnoCiYeH let d_real_y = (visiable_y + data.dy) as u32; 53852bcb59eSGnoCiYeH 53952da9a59SGnoCiYeH let bytes_per_pixel = var.bits_per_pixel >> 3; 54052da9a59SGnoCiYeH let bytes_per_line = var.xres * bytes_per_pixel; 54152da9a59SGnoCiYeH 54252bcb59eSGnoCiYeH let src = 54352bcb59eSGnoCiYeH base + VirtAddr::new((s_real_y * bytes_per_line + s_real_x * bytes_per_pixel) as usize); 54452da9a59SGnoCiYeH 54552bcb59eSGnoCiYeH let dst = 54652bcb59eSGnoCiYeH base + VirtAddr::new((d_real_y * bytes_per_line + d_real_x * bytes_per_pixel) as usize); 54752da9a59SGnoCiYeH 54852bcb59eSGnoCiYeH let size = (visiable_h * visiable_w) as usize; 54952da9a59SGnoCiYeH 55052da9a59SGnoCiYeH match bytes_per_pixel { 55152da9a59SGnoCiYeH 4 => { 55252da9a59SGnoCiYeH // 32bpp 55352da9a59SGnoCiYeH let mut dst = dst.as_ptr::<u32>(); 55452da9a59SGnoCiYeH let mut src = src.as_ptr::<u32>(); 55552bcb59eSGnoCiYeH let line_offset = var.xres as usize; 55652da9a59SGnoCiYeH 55752bcb59eSGnoCiYeH if s_real_x > d_real_x { 55852bcb59eSGnoCiYeH // 如果src在dst下方,则可以直接拷贝不会出现指针覆盖 55952da9a59SGnoCiYeH unsafe { 56052bcb59eSGnoCiYeH for _ in 0..visiable_h { 56152bcb59eSGnoCiYeH core::ptr::copy(src, dst, visiable_w as usize); 56252bcb59eSGnoCiYeH src = src.add(line_offset); 56352bcb59eSGnoCiYeH dst = dst.add(visiable_w as usize); 56452da9a59SGnoCiYeH } 56552da9a59SGnoCiYeH } 56652da9a59SGnoCiYeH } else { 567b5b571e0SLoGin let mut tmp: Vec<u32> = vec![0; size]; 56852bcb59eSGnoCiYeH let mut tmp_ptr = tmp.as_mut_ptr(); 56952bcb59eSGnoCiYeH 57052bcb59eSGnoCiYeH // 这里是一个可以优化的点,现在为了避免指针拷贝时覆盖,统一先拷贝进入buf再拷贝到dst 57152da9a59SGnoCiYeH unsafe { 57252bcb59eSGnoCiYeH for _ in 0..visiable_h { 57352bcb59eSGnoCiYeH core::ptr::copy(src, tmp_ptr, visiable_w as usize); 57452bcb59eSGnoCiYeH src = src.add(line_offset); 57552bcb59eSGnoCiYeH tmp_ptr = tmp_ptr.add(visiable_w as usize); 57652bcb59eSGnoCiYeH } 57752bcb59eSGnoCiYeH 57852bcb59eSGnoCiYeH tmp_ptr = tmp_ptr.sub(size); 57952bcb59eSGnoCiYeH for _ in 0..visiable_h { 58052bcb59eSGnoCiYeH core::ptr::copy(tmp_ptr, dst, visiable_w as usize); 58152bcb59eSGnoCiYeH dst = dst.add(line_offset); 58252bcb59eSGnoCiYeH tmp_ptr = tmp_ptr.add(visiable_w as usize); 58352da9a59SGnoCiYeH } 58452da9a59SGnoCiYeH } 58552da9a59SGnoCiYeH } 58652da9a59SGnoCiYeH } 5872755467cS曾俊 2 => { 5882755467cS曾俊 let mut dst = dst.as_ptr::<u16>(); 5892755467cS曾俊 let mut src = src.as_ptr::<u16>(); 5902755467cS曾俊 let line_offset = var.xres as usize; 5912755467cS曾俊 5922755467cS曾俊 if s_real_x > d_real_x { 5932755467cS曾俊 // 如果src在dst下方,则可以直接拷贝不会出现指针覆盖 5942755467cS曾俊 unsafe { 5952755467cS曾俊 for _ in 0..visiable_h { 5962755467cS曾俊 core::ptr::copy(src, dst, visiable_w as usize); 5972755467cS曾俊 src = src.add(line_offset); 5982755467cS曾俊 dst = dst.add(visiable_w as usize); 5992755467cS曾俊 } 6002755467cS曾俊 } 6012755467cS曾俊 } else { 6022755467cS曾俊 let mut tmp: Vec<u16> = vec![0; size]; 6032755467cS曾俊 let mut tmp_ptr = tmp.as_mut_ptr(); 6042755467cS曾俊 6052755467cS曾俊 // 这里是一个可以优化的点,现在为了避免指针拷贝时覆盖,统一先拷贝进入buf再拷贝到dst 6062755467cS曾俊 unsafe { 6072755467cS曾俊 for _ in 0..visiable_h { 6082755467cS曾俊 core::ptr::copy(src, tmp_ptr, visiable_w as usize); 6092755467cS曾俊 src = src.add(line_offset); 6102755467cS曾俊 tmp_ptr = tmp_ptr.add(visiable_w as usize); 6112755467cS曾俊 } 6122755467cS曾俊 6132755467cS曾俊 tmp_ptr = tmp_ptr.sub(size); 6142755467cS曾俊 for _ in 0..visiable_h { 6152755467cS曾俊 core::ptr::copy(tmp_ptr, dst, visiable_w as usize); 6162755467cS曾俊 dst = dst.add(line_offset); 6172755467cS曾俊 tmp_ptr = tmp_ptr.add(visiable_w as usize); 6182755467cS曾俊 } 6192755467cS曾俊 } 6202755467cS曾俊 } 6212755467cS曾俊 } 6222755467cS曾俊 3 => { 6232755467cS曾俊 let mut dst = dst.as_ptr::<[u8; 3]>(); 6242755467cS曾俊 let mut src = src.as_ptr::<[u8; 3]>(); 6252755467cS曾俊 let line_offset = var.xres as usize; 6262755467cS曾俊 6272755467cS曾俊 if s_real_x > d_real_x { 6282755467cS曾俊 // 如果src在dst下方,则可以直接拷贝不会出现指针覆盖 6292755467cS曾俊 unsafe { 6302755467cS曾俊 for _ in 0..visiable_h { 6312755467cS曾俊 core::ptr::copy(src, dst, visiable_w as usize); 6322755467cS曾俊 src = src.add(line_offset); 6332755467cS曾俊 dst = dst.add(visiable_w as usize); 6342755467cS曾俊 } 6352755467cS曾俊 } 6362755467cS曾俊 } else { 6372755467cS曾俊 let mut tmp: Vec<u32> = vec![0; size]; 6382755467cS曾俊 let mut tmp_ptr = tmp.as_mut_ptr() as *mut [u8; 3]; 6392755467cS曾俊 6402755467cS曾俊 // 这里是一个可以优化的点,现在为了避免指针拷贝时覆盖,统一先拷贝进入buf再拷贝到dst 6412755467cS曾俊 unsafe { 6422755467cS曾俊 for _ in 0..visiable_h { 6432755467cS曾俊 core::ptr::copy(src, tmp_ptr, visiable_w as usize); 6442755467cS曾俊 src = src.add(line_offset); 6452755467cS曾俊 tmp_ptr = tmp_ptr.add(visiable_w as usize); 6462755467cS曾俊 } 6472755467cS曾俊 6482755467cS曾俊 tmp_ptr = tmp_ptr.sub(size); 6492755467cS曾俊 for _ in 0..visiable_h { 6502755467cS曾俊 core::ptr::copy(tmp_ptr, dst, visiable_w as usize); 6512755467cS曾俊 dst = dst.add(line_offset); 6522755467cS曾俊 tmp_ptr = tmp_ptr.add(visiable_w as usize); 6532755467cS曾俊 } 6542755467cS曾俊 } 6552755467cS曾俊 } 6562755467cS曾俊 } 6572755467cS曾俊 65852da9a59SGnoCiYeH _ => { 6592755467cS曾俊 send_to_default_serial8250_port( 6602755467cS曾俊 format!("bytes_per_pixel:{}\n\0", bytes_per_pixel).as_bytes(), 6612755467cS曾俊 ); 66252da9a59SGnoCiYeH todo!() 66352da9a59SGnoCiYeH } 66452da9a59SGnoCiYeH } 66552da9a59SGnoCiYeH } 666c566df45SLoGin } 667c566df45SLoGin 668c566df45SLoGin impl FrameBufferInfo for VesaFb { 669c566df45SLoGin fn fb_device(&self) -> Option<Arc<FbDevice>> { 670c566df45SLoGin self.inner.lock().fb_device.clone() 671c566df45SLoGin } 672c566df45SLoGin 673c566df45SLoGin fn set_fb_device(&self, device: Option<Arc<FbDevice>>) { 674c566df45SLoGin self.inner.lock().fb_device = device; 675c566df45SLoGin } 676c566df45SLoGin 677c566df45SLoGin fn screen_size(&self) -> usize { 678c566df45SLoGin todo!() 679c566df45SLoGin } 680c566df45SLoGin 681c566df45SLoGin fn current_fb_var(&self) -> FbVarScreenInfo { 682b5b571e0SLoGin *VESAFB_DEFINED.read() 683c566df45SLoGin } 684c566df45SLoGin 685c566df45SLoGin fn current_fb_fix(&self) -> FixedScreenInfo { 686b5b571e0SLoGin *VESAFB_FIX_INFO.read() 687c566df45SLoGin } 688c566df45SLoGin 689c566df45SLoGin fn video_mode(&self) -> Option<&FbVideoMode> { 690c566df45SLoGin todo!() 691c566df45SLoGin } 692c566df45SLoGin 693c566df45SLoGin fn state(&self) -> FbState { 694c566df45SLoGin self.inner.lock().fb_state 695c566df45SLoGin } 69652da9a59SGnoCiYeH 69752da9a59SGnoCiYeH fn framebuffer_info_data(&self) -> &RwLock<FrameBufferInfoData> { 69852da9a59SGnoCiYeH &self.fb_data 69952da9a59SGnoCiYeH } 700c566df45SLoGin } 701c566df45SLoGin 702c566df45SLoGin #[derive(Debug)] 703c566df45SLoGin #[cast_to([sync] PlatformDriver)] 704c566df45SLoGin struct VesaFbDriver { 705c566df45SLoGin inner: SpinLock<InnerVesaFbDriver>, 706c566df45SLoGin kobj_state: LockedKObjectState, 707c566df45SLoGin } 708c566df45SLoGin 709c566df45SLoGin impl VesaFbDriver { 710c566df45SLoGin pub fn new() -> Arc<Self> { 711c566df45SLoGin let r = Arc::new(Self { 712c566df45SLoGin inner: SpinLock::new(InnerVesaFbDriver { 713c566df45SLoGin ktype: None, 714c566df45SLoGin kset: None, 715c566df45SLoGin parent: None, 716c566df45SLoGin kernfs_inode: None, 717c566df45SLoGin devices: Vec::new(), 718c566df45SLoGin bus: None, 719c566df45SLoGin self_ref: Weak::new(), 720c566df45SLoGin }), 721c566df45SLoGin kobj_state: LockedKObjectState::new(None), 722c566df45SLoGin }); 723c566df45SLoGin 724c566df45SLoGin r.inner.lock().self_ref = Arc::downgrade(&r); 725c566df45SLoGin 726c566df45SLoGin return r; 727c566df45SLoGin } 728c566df45SLoGin } 729c566df45SLoGin 730c566df45SLoGin #[derive(Debug)] 731c566df45SLoGin struct InnerVesaFbDriver { 732c566df45SLoGin ktype: Option<&'static dyn KObjType>, 733c566df45SLoGin kset: Option<Arc<KSet>>, 734c566df45SLoGin parent: Option<Weak<dyn KObject>>, 735c566df45SLoGin kernfs_inode: Option<Arc<KernFSInode>>, 736c566df45SLoGin devices: Vec<Arc<dyn Device>>, 737c566df45SLoGin bus: Option<Weak<dyn Bus>>, 738c566df45SLoGin 739c566df45SLoGin self_ref: Weak<VesaFbDriver>, 740c566df45SLoGin } 741c566df45SLoGin 742c566df45SLoGin impl VesaFbDriver { 743c566df45SLoGin const NAME: &'static str = "vesa-framebuffer"; 744c566df45SLoGin } 745c566df45SLoGin 746c566df45SLoGin impl PlatformDriver for VesaFbDriver { 747c566df45SLoGin fn probe(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 748c566df45SLoGin let device = device 749c566df45SLoGin .clone() 750c566df45SLoGin .arc_any() 751c566df45SLoGin .downcast::<VesaFb>() 752c566df45SLoGin .map_err(|_| SystemError::EINVAL)?; 753c566df45SLoGin 754c566df45SLoGin device.set_driver(Some(self.inner.lock_irqsave().self_ref.clone())); 755c566df45SLoGin 756c566df45SLoGin return Ok(()); 757c566df45SLoGin } 758c566df45SLoGin 759c566df45SLoGin fn remove(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 760c566df45SLoGin todo!() 761c566df45SLoGin } 762c566df45SLoGin 763c566df45SLoGin fn shutdown(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 764c566df45SLoGin // do nothing 765c566df45SLoGin return Ok(()); 766c566df45SLoGin } 767c566df45SLoGin 768c566df45SLoGin fn suspend(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 769c566df45SLoGin // do nothing 770c566df45SLoGin return Ok(()); 771c566df45SLoGin } 772c566df45SLoGin 773c566df45SLoGin fn resume(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 774c566df45SLoGin todo!() 775c566df45SLoGin } 776c566df45SLoGin } 777c566df45SLoGin 778c566df45SLoGin impl Driver for VesaFbDriver { 779c566df45SLoGin fn id_table(&self) -> Option<IdTable> { 780c566df45SLoGin Some(IdTable::new(VesaFb::NAME.to_string(), None)) 781c566df45SLoGin } 782c566df45SLoGin 783c566df45SLoGin fn devices(&self) -> Vec<Arc<dyn Device>> { 784c566df45SLoGin self.inner.lock().devices.clone() 785c566df45SLoGin } 786c566df45SLoGin 787c566df45SLoGin fn add_device(&self, device: Arc<dyn Device>) { 788c566df45SLoGin let mut guard = self.inner.lock(); 789c566df45SLoGin // check if the device is already in the list 790c566df45SLoGin if guard.devices.iter().any(|dev| Arc::ptr_eq(dev, &device)) { 791c566df45SLoGin return; 792c566df45SLoGin } 793c566df45SLoGin 794c566df45SLoGin guard.devices.push(device); 795c566df45SLoGin } 796c566df45SLoGin 797c566df45SLoGin fn delete_device(&self, device: &Arc<dyn Device>) { 798c566df45SLoGin let mut guard = self.inner.lock(); 799c566df45SLoGin guard.devices.retain(|dev| !Arc::ptr_eq(dev, device)); 800c566df45SLoGin } 801c566df45SLoGin 802c566df45SLoGin fn set_bus(&self, bus: Option<Weak<dyn Bus>>) { 803c566df45SLoGin self.inner.lock().bus = bus; 804c566df45SLoGin } 805c566df45SLoGin 806c566df45SLoGin fn bus(&self) -> Option<Weak<dyn Bus>> { 807c566df45SLoGin self.inner.lock().bus.clone() 808c566df45SLoGin } 809c566df45SLoGin 810c566df45SLoGin fn dev_groups(&self) -> &'static [&'static dyn AttributeGroup] { 811c566df45SLoGin return &[&VesaFbAnonAttributeGroup]; 812c566df45SLoGin } 813c566df45SLoGin } 814c566df45SLoGin 815c566df45SLoGin impl KObject for VesaFbDriver { 816c566df45SLoGin fn as_any_ref(&self) -> &dyn core::any::Any { 817c566df45SLoGin self 818c566df45SLoGin } 819c566df45SLoGin 820c566df45SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 821c566df45SLoGin self.inner.lock().kernfs_inode = inode; 822c566df45SLoGin } 823c566df45SLoGin 824c566df45SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 825c566df45SLoGin self.inner.lock().kernfs_inode.clone() 826c566df45SLoGin } 827c566df45SLoGin 828c566df45SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 829c566df45SLoGin self.inner.lock().parent.clone() 830c566df45SLoGin } 831c566df45SLoGin 832c566df45SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 833c566df45SLoGin self.inner.lock().parent = parent; 834c566df45SLoGin } 835c566df45SLoGin 836c566df45SLoGin fn kset(&self) -> Option<Arc<KSet>> { 837c566df45SLoGin self.inner.lock().kset.clone() 838c566df45SLoGin } 839c566df45SLoGin 840c566df45SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 841c566df45SLoGin self.inner.lock().kset = kset; 842c566df45SLoGin } 843c566df45SLoGin 844c566df45SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 845c566df45SLoGin self.inner.lock().ktype 846c566df45SLoGin } 847c566df45SLoGin 848c566df45SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 849c566df45SLoGin self.inner.lock().ktype = ktype; 850c566df45SLoGin } 851c566df45SLoGin 852c566df45SLoGin fn name(&self) -> String { 853c566df45SLoGin Self::NAME.to_string() 854c566df45SLoGin } 855c566df45SLoGin 856c566df45SLoGin fn set_name(&self, _name: String) { 857c566df45SLoGin // do nothing 858c566df45SLoGin } 859c566df45SLoGin 860c566df45SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 861c566df45SLoGin self.kobj_state.read() 862c566df45SLoGin } 863c566df45SLoGin 864c566df45SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 865c566df45SLoGin self.kobj_state.write() 866c566df45SLoGin } 867c566df45SLoGin 868c566df45SLoGin fn set_kobj_state(&self, state: KObjectState) { 869c566df45SLoGin *self.kobj_state.write() = state; 870c566df45SLoGin } 871c566df45SLoGin } 872c566df45SLoGin 873c566df45SLoGin #[derive(Debug)] 874c566df45SLoGin struct VesaFbAnonAttributeGroup; 875c566df45SLoGin 876c566df45SLoGin impl AttributeGroup for VesaFbAnonAttributeGroup { 877c566df45SLoGin fn name(&self) -> Option<&str> { 878c566df45SLoGin None 879c566df45SLoGin } 880c566df45SLoGin 881c566df45SLoGin fn attrs(&self) -> &[&'static dyn Attribute] { 882c566df45SLoGin &[&AnonAttrPhysAddr as &'static dyn Attribute] 883c566df45SLoGin } 884c566df45SLoGin 885c566df45SLoGin fn is_visible( 886c566df45SLoGin &self, 887c566df45SLoGin _kobj: Arc<dyn KObject>, 888c566df45SLoGin attr: &'static dyn Attribute, 889c566df45SLoGin ) -> Option<ModeType> { 890c566df45SLoGin Some(attr.mode()) 891c566df45SLoGin } 892c566df45SLoGin } 893c566df45SLoGin 894c566df45SLoGin #[derive(Debug)] 895c566df45SLoGin struct AnonAttrPhysAddr; 896c566df45SLoGin 897c566df45SLoGin impl Attribute for AnonAttrPhysAddr { 898c566df45SLoGin fn name(&self) -> &str { 899c566df45SLoGin "smem_start" 900c566df45SLoGin } 901c566df45SLoGin 902c566df45SLoGin fn mode(&self) -> ModeType { 903c566df45SLoGin ModeType::S_IRUGO 904c566df45SLoGin } 905c566df45SLoGin 906c566df45SLoGin fn support(&self) -> SysFSOpsSupport { 907196b75dcSLoGin SysFSOpsSupport::ATTR_SHOW 908c566df45SLoGin } 909c566df45SLoGin 910c566df45SLoGin fn show(&self, _kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 911c566df45SLoGin sysfs_emit_str( 912c566df45SLoGin buf, 913c566df45SLoGin format!( 914c566df45SLoGin "0x{:x}\n", 915c566df45SLoGin VESAFB_FIX_INFO 916c566df45SLoGin .read() 917c566df45SLoGin .smem_start 918c566df45SLoGin .unwrap_or(PhysAddr::new(0)) 919c566df45SLoGin .data() 920c566df45SLoGin ) 921c566df45SLoGin .as_str(), 922c566df45SLoGin ) 923c566df45SLoGin } 924c566df45SLoGin } 925c566df45SLoGin 926c566df45SLoGin #[unified_init(INITCALL_DEVICE)] 927c566df45SLoGin pub fn vesa_fb_driver_init() -> Result<(), SystemError> { 928c566df45SLoGin let driver = VesaFbDriver::new(); 929c566df45SLoGin 930c566df45SLoGin platform_driver_manager().register(driver)?; 931c566df45SLoGin 932c566df45SLoGin return Ok(()); 933c566df45SLoGin } 934c566df45SLoGin 935c566df45SLoGin /// 在内存管理初始化之前,初始化vesafb 936c566df45SLoGin pub fn vesafb_early_init() -> Result<VirtAddr, SystemError> { 937c566df45SLoGin let mut _reserved: u32 = 0; 938c566df45SLoGin 939c566df45SLoGin let mut fb_info: MaybeUninit<multiboot_tag_framebuffer_info_t> = MaybeUninit::uninit(); 940c566df45SLoGin //从multiboot2中读取帧缓冲区信息至fb_info 941c566df45SLoGin 942c566df45SLoGin // todo: 换成rust的,并且检测是否成功获取 943c566df45SLoGin unsafe { 944c566df45SLoGin multiboot2_iter( 945c566df45SLoGin Some(multiboot2_get_Framebuffer_info), 946c566df45SLoGin fb_info.as_mut_ptr() as usize as *mut c_void, 947c566df45SLoGin &mut _reserved as *mut c_uint, 948c566df45SLoGin ) 949c566df45SLoGin }; 950c566df45SLoGin unsafe { fb_info.assume_init() }; 951c566df45SLoGin let fb_info: multiboot_tag_framebuffer_info_t = unsafe { core::mem::transmute(fb_info) }; 952c566df45SLoGin 953c566df45SLoGin // todo: 判断是否有vesa帧缓冲区,这里暂时直接设置true 954c566df45SLoGin HAS_VESA_FB.store(true, core::sync::atomic::Ordering::SeqCst); 955c566df45SLoGin 956c566df45SLoGin let width = fb_info.framebuffer_width; 957c566df45SLoGin let height = fb_info.framebuffer_height; 958c566df45SLoGin 959c566df45SLoGin let mut boot_params_guard = boot_params().write(); 960c566df45SLoGin let boottime_screen_info = &mut boot_params_guard.screen_info; 961c566df45SLoGin 962c566df45SLoGin boottime_screen_info.is_vga = true; 963c566df45SLoGin 964c566df45SLoGin boottime_screen_info.lfb_base = PhysAddr::new(fb_info.framebuffer_addr as usize); 965c566df45SLoGin 966c566df45SLoGin if fb_info.framebuffer_type == 2 { 967c566df45SLoGin //当type=2时,width与height用字符数表示,故depth=8 968c566df45SLoGin boottime_screen_info.origin_video_cols = width as u8; 969c566df45SLoGin boottime_screen_info.origin_video_lines = height as u8; 970c566df45SLoGin boottime_screen_info.video_type = BootTimeVideoType::Mda; 971c566df45SLoGin boottime_screen_info.lfb_depth = 8; 972c566df45SLoGin } else { 973c566df45SLoGin //否则为图像模式,depth应参照帧缓冲区信息里面的每个像素的位数 974c566df45SLoGin boottime_screen_info.lfb_width = width; 975c566df45SLoGin boottime_screen_info.lfb_height = height; 976c566df45SLoGin boottime_screen_info.video_type = BootTimeVideoType::Vlfb; 977c566df45SLoGin boottime_screen_info.lfb_depth = fb_info.framebuffer_bpp as u8; 978c566df45SLoGin } 979c566df45SLoGin 980c566df45SLoGin boottime_screen_info.lfb_size = 981c566df45SLoGin (width * height * ((fb_info.framebuffer_bpp as u32 + 7) / 8)) as usize; 982c566df45SLoGin 983c75ef4e2SLoGin // let buf_vaddr = VirtAddr::new(0xffff800003200000); 984c75ef4e2SLoGin let buf_vaddr = VirtAddr::new( 985c75ef4e2SLoGin crate::include::bindings::bindings::SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE as usize 986c75ef4e2SLoGin + FRAME_BUFFER_MAPPING_OFFSET as usize, 987c75ef4e2SLoGin ); 988c566df45SLoGin boottime_screen_info.lfb_virt_base = Some(buf_vaddr); 989c566df45SLoGin 990c566df45SLoGin let init_text = "Video driver to map.\n\0"; 991c566df45SLoGin send_to_default_serial8250_port(init_text.as_bytes()); 992c566df45SLoGin 993c566df45SLoGin // 地址映射 994c566df45SLoGin let paddr = PhysAddr::new(fb_info.framebuffer_addr as usize); 995c566df45SLoGin let count = 996c566df45SLoGin PageFrameCount::new(page_align_up(boottime_screen_info.lfb_size) / MMArch::PAGE_SIZE); 997c566df45SLoGin unsafe { pseudo_map_phys(buf_vaddr, paddr, count) }; 998c566df45SLoGin return Ok(buf_vaddr); 999c566df45SLoGin } 1000c566df45SLoGin 1001c566df45SLoGin #[unified_init(INITCALL_DEVICE)] 1002c566df45SLoGin fn vesa_fb_device_init() -> Result<(), SystemError> { 1003c566df45SLoGin // 如果没有vesa帧缓冲区,直接返回 1004c566df45SLoGin if !HAS_VESA_FB.load(core::sync::atomic::Ordering::SeqCst) { 1005c566df45SLoGin return Ok(()); 1006c566df45SLoGin } 1007c566df45SLoGin 1008c566df45SLoGin static INIT: Once = Once::new(); 1009c566df45SLoGin INIT.call_once(|| { 1010*2eab6dd7S曾俊 info!("vesa fb device init"); 101152da9a59SGnoCiYeH let device = Arc::new(VesaFb::new()); 1012c566df45SLoGin 101352da9a59SGnoCiYeH let mut fb_fix = VESAFB_FIX_INFO.write_irqsave(); 101452da9a59SGnoCiYeH let mut fb_var = VESAFB_DEFINED.write_irqsave(); 1015c566df45SLoGin 1016c566df45SLoGin let boot_params_guard = boot_params().read(); 1017c566df45SLoGin let boottime_screen_info = &boot_params_guard.screen_info; 1018c566df45SLoGin 101952da9a59SGnoCiYeH fb_fix.smem_start = Some(boottime_screen_info.lfb_base); 102052da9a59SGnoCiYeH fb_fix.smem_len = boottime_screen_info.lfb_size; 1021c566df45SLoGin 1022c566df45SLoGin if boottime_screen_info.video_type == BootTimeVideoType::Mda { 102352da9a59SGnoCiYeH fb_fix.visual = FbVisual::Mono10; 102452da9a59SGnoCiYeH fb_var.bits_per_pixel = 8; 102552da9a59SGnoCiYeH fb_fix.line_length = 102652da9a59SGnoCiYeH (boottime_screen_info.origin_video_cols as u32) * (fb_var.bits_per_pixel / 8); 102752da9a59SGnoCiYeH fb_var.xres_virtual = boottime_screen_info.origin_video_cols as u32; 102852da9a59SGnoCiYeH fb_var.yres_virtual = boottime_screen_info.origin_video_lines as u32; 1029c566df45SLoGin } else { 103052da9a59SGnoCiYeH fb_fix.visual = FbVisual::TrueColor; 103152da9a59SGnoCiYeH fb_var.bits_per_pixel = boottime_screen_info.lfb_depth as u32; 103252da9a59SGnoCiYeH fb_fix.line_length = 103352da9a59SGnoCiYeH (boottime_screen_info.lfb_width as u32) * (fb_var.bits_per_pixel / 8); 103452da9a59SGnoCiYeH fb_var.xres_virtual = boottime_screen_info.lfb_width as u32; 103552da9a59SGnoCiYeH fb_var.yres_virtual = boottime_screen_info.lfb_height as u32; 103652da9a59SGnoCiYeH fb_var.xres = boottime_screen_info.lfb_width as u32; 103752da9a59SGnoCiYeH fb_var.yres = boottime_screen_info.lfb_height as u32; 1038c566df45SLoGin } 1039c566df45SLoGin 104052da9a59SGnoCiYeH fb_var.red.length = boottime_screen_info.red_size as u32; 104152da9a59SGnoCiYeH fb_var.green.length = boottime_screen_info.green_size as u32; 104252da9a59SGnoCiYeH fb_var.blue.length = boottime_screen_info.blue_size as u32; 1043c566df45SLoGin 104452da9a59SGnoCiYeH fb_var.red.offset = boottime_screen_info.red_pos as u32; 104552da9a59SGnoCiYeH fb_var.green.offset = boottime_screen_info.green_pos as u32; 104652da9a59SGnoCiYeH fb_var.blue.offset = boottime_screen_info.blue_pos as u32; 104752da9a59SGnoCiYeH 104852da9a59SGnoCiYeH // TODO: 这里是暂时这样写的,初始化为RGB888格式,后续vesa初始化完善后删掉下面 104952da9a59SGnoCiYeH fb_var.red.offset = 16; 105052da9a59SGnoCiYeH fb_var.green.offset = 8; 105152da9a59SGnoCiYeH fb_var.blue.offset = 0; 105252da9a59SGnoCiYeH 105352da9a59SGnoCiYeH if fb_var.bits_per_pixel >= 1 && fb_var.bits_per_pixel <= 8 { 105452da9a59SGnoCiYeH fb_var.red.length = fb_var.bits_per_pixel; 105552da9a59SGnoCiYeH fb_var.green.length = fb_var.bits_per_pixel; 105652da9a59SGnoCiYeH fb_var.blue.length = fb_var.bits_per_pixel; 105752da9a59SGnoCiYeH } 105852da9a59SGnoCiYeH 1059c566df45SLoGin device_manager().device_default_initialize(&(device.clone() as Arc<dyn Device>)); 1060c566df45SLoGin 1061c566df45SLoGin platform_device_manager() 1062c566df45SLoGin .device_add(device.clone() as Arc<dyn PlatformDevice>) 1063c566df45SLoGin .expect("vesa_fb_device_init: platform_device_manager().device_add failed"); 1064c566df45SLoGin 1065c566df45SLoGin frame_buffer_manager() 1066c566df45SLoGin .register_fb(device.clone() as Arc<dyn FrameBuffer>) 1067c566df45SLoGin .expect("vesa_fb_device_init: frame_buffer_manager().register_fb failed"); 1068c566df45SLoGin 106952da9a59SGnoCiYeH // 加入全局fb表 107052da9a59SGnoCiYeH let mut guard = FRAME_BUFFER_SET.write(); 107152da9a59SGnoCiYeH if guard.get(device.fb_id().data() as usize).unwrap().is_some() { 1072*2eab6dd7S曾俊 warn!( 107352da9a59SGnoCiYeH "vesa_fb_device_init: There is already an element {:?} in the FRAME_BUFFER_SET", 107452da9a59SGnoCiYeH device.fb_id() 107552da9a59SGnoCiYeH ); 107652da9a59SGnoCiYeH } 107752da9a59SGnoCiYeH guard[device.fb_id().data() as usize] = Some(device.clone()); 107852da9a59SGnoCiYeH 1079c566df45SLoGin // 设置vesa fb的状态为运行中 1080c566df45SLoGin device.inner.lock().fb_state = FbState::Running; 1081c566df45SLoGin }); 1082c566df45SLoGin 1083c566df45SLoGin return Ok(()); 1084c566df45SLoGin } 1085