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 }; 12c566df45SLoGin use system_error::SystemError; 13c566df45SLoGin use unified_init::macros::unified_init; 14c566df45SLoGin 15c566df45SLoGin use crate::{ 16c566df45SLoGin arch::MMArch, 17c566df45SLoGin driver::{ 18c566df45SLoGin base::{ 19c566df45SLoGin class::Class, 20c566df45SLoGin device::{ 21c566df45SLoGin bus::Bus, device_manager, driver::Driver, Device, DeviceState, DeviceType, IdTable, 22c566df45SLoGin }, 23c566df45SLoGin kobject::{KObjType, KObject, KObjectState, LockedKObjectState}, 24c566df45SLoGin kset::KSet, 25c566df45SLoGin platform::{ 26c566df45SLoGin platform_device::{platform_device_manager, PlatformDevice}, 27c566df45SLoGin platform_driver::{platform_driver_manager, PlatformDriver}, 28c566df45SLoGin CompatibleTable, 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>>, 125c566df45SLoGin class: Option<Arc<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 compatible_table(&self) -> CompatibleTable { 163c566df45SLoGin todo!() 164c566df45SLoGin } 165c566df45SLoGin 166c566df45SLoGin fn is_initialized(&self) -> bool { 167c566df45SLoGin self.inner.lock().device_state == DeviceState::Initialized 168c566df45SLoGin } 169c566df45SLoGin 170c566df45SLoGin fn set_state(&self, set_state: DeviceState) { 171c566df45SLoGin self.inner.lock().device_state = set_state; 172c566df45SLoGin } 173c566df45SLoGin } 174c566df45SLoGin 175c566df45SLoGin impl Device for VesaFb { 176c566df45SLoGin fn dev_type(&self) -> DeviceType { 177c566df45SLoGin DeviceType::Char 178c566df45SLoGin } 179c566df45SLoGin 180c566df45SLoGin fn id_table(&self) -> IdTable { 181c566df45SLoGin IdTable::new(self.name(), None) 182c566df45SLoGin } 183c566df45SLoGin 184c566df45SLoGin fn bus(&self) -> Option<Weak<dyn Bus>> { 185c566df45SLoGin self.inner.lock().bus.clone() 186c566df45SLoGin } 187c566df45SLoGin 188c566df45SLoGin fn set_bus(&self, bus: Option<Weak<dyn Bus>>) { 189c566df45SLoGin self.inner.lock().bus = bus; 190c566df45SLoGin } 191c566df45SLoGin 192c566df45SLoGin fn set_class(&self, class: Option<Arc<dyn Class>>) { 193c566df45SLoGin self.inner.lock().class = class; 194c566df45SLoGin } 195c566df45SLoGin 196c566df45SLoGin fn driver(&self) -> Option<Arc<dyn Driver>> { 197c566df45SLoGin self.inner.lock().driver.clone()?.upgrade() 198c566df45SLoGin } 199c566df45SLoGin 200c566df45SLoGin fn set_driver(&self, driver: Option<Weak<dyn Driver>>) { 201c566df45SLoGin self.inner.lock().driver = driver; 202c566df45SLoGin } 203c566df45SLoGin 204c566df45SLoGin fn is_dead(&self) -> bool { 205c566df45SLoGin false 206c566df45SLoGin } 207c566df45SLoGin 208c566df45SLoGin fn can_match(&self) -> bool { 209c566df45SLoGin true 210c566df45SLoGin } 211c566df45SLoGin 212c566df45SLoGin fn set_can_match(&self, _can_match: bool) {} 213c566df45SLoGin 214c566df45SLoGin fn state_synced(&self) -> bool { 215c566df45SLoGin true 216c566df45SLoGin } 217c566df45SLoGin } 218c566df45SLoGin 219c566df45SLoGin impl KObject for VesaFb { 220c566df45SLoGin fn as_any_ref(&self) -> &dyn core::any::Any { 221c566df45SLoGin self 222c566df45SLoGin } 223c566df45SLoGin 224c566df45SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 225c566df45SLoGin self.inner.lock().kern_inode = inode; 226c566df45SLoGin } 227c566df45SLoGin 228c566df45SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 229c566df45SLoGin self.inner.lock().kern_inode.clone() 230c566df45SLoGin } 231c566df45SLoGin 232c566df45SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 233c566df45SLoGin self.inner.lock().parent.clone() 234c566df45SLoGin } 235c566df45SLoGin 236c566df45SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 237c566df45SLoGin self.inner.lock().parent = parent; 238c566df45SLoGin } 239c566df45SLoGin 240c566df45SLoGin fn kset(&self) -> Option<Arc<KSet>> { 241c566df45SLoGin self.inner.lock().kset.clone() 242c566df45SLoGin } 243c566df45SLoGin 244c566df45SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 245c566df45SLoGin self.inner.lock().kset = kset; 246c566df45SLoGin } 247c566df45SLoGin 248c566df45SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 249c566df45SLoGin self.inner.lock().kobj_type 250c566df45SLoGin } 251c566df45SLoGin 252c566df45SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 253c566df45SLoGin self.inner.lock().kobj_type = ktype; 254c566df45SLoGin } 255c566df45SLoGin 256c566df45SLoGin fn name(&self) -> String { 257c566df45SLoGin Self::NAME.to_string() 258c566df45SLoGin } 259c566df45SLoGin 260c566df45SLoGin fn set_name(&self, _name: String) { 261c566df45SLoGin // do nothing 262c566df45SLoGin } 263c566df45SLoGin 264c566df45SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 265c566df45SLoGin self.kobj_state.read() 266c566df45SLoGin } 267c566df45SLoGin 268c566df45SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 269c566df45SLoGin self.kobj_state.write() 270c566df45SLoGin } 271c566df45SLoGin 272c566df45SLoGin fn set_kobj_state(&self, state: KObjectState) { 273c566df45SLoGin *self.kobj_state.write() = state; 274c566df45SLoGin } 275c566df45SLoGin } 276c566df45SLoGin 277c566df45SLoGin impl FrameBufferOps for VesaFb { 278c566df45SLoGin fn fb_open(&self, _user: bool) { 279c566df45SLoGin todo!() 280c566df45SLoGin } 281c566df45SLoGin 282c566df45SLoGin fn fb_release(&self, _user: bool) { 283c566df45SLoGin todo!() 284c566df45SLoGin } 285c566df45SLoGin 286c566df45SLoGin fn fb_set_color_register( 287c566df45SLoGin &self, 28852da9a59SGnoCiYeH regno: u16, 28952da9a59SGnoCiYeH mut red: u16, 29052da9a59SGnoCiYeH mut green: u16, 29152da9a59SGnoCiYeH mut blue: u16, 292c566df45SLoGin ) -> Result<(), SystemError> { 29352da9a59SGnoCiYeH let mut fb_data = self.framebuffer_info_data().write(); 29452da9a59SGnoCiYeH let var = self.current_fb_var(); 29552da9a59SGnoCiYeH if regno as usize >= fb_data.pesudo_palette.len() { 29652da9a59SGnoCiYeH return Err(SystemError::E2BIG); 29752da9a59SGnoCiYeH } 29852da9a59SGnoCiYeH 29952da9a59SGnoCiYeH if var.bits_per_pixel == 8 { 30052da9a59SGnoCiYeH todo!("vesa_setpalette todo"); 30152da9a59SGnoCiYeH } else if regno < 16 { 30252da9a59SGnoCiYeH match var.bits_per_pixel { 30352da9a59SGnoCiYeH 16 => { 30452da9a59SGnoCiYeH if var.red.offset == 10 { 30552da9a59SGnoCiYeH // RGB 1:5:5:5 30652da9a59SGnoCiYeH fb_data.pesudo_palette[regno as usize] = ((red as u32 & 0xf800) >> 1) 30752da9a59SGnoCiYeH | ((green as u32 & 0xf800) >> 6) 30852da9a59SGnoCiYeH | ((blue as u32 & 0xf800) >> 11); 30952da9a59SGnoCiYeH } else { 31052da9a59SGnoCiYeH fb_data.pesudo_palette[regno as usize] = (red as u32 & 0xf800) 31152da9a59SGnoCiYeH | ((green as u32 & 0xfc00) >> 5) 31252da9a59SGnoCiYeH | ((blue as u32 & 0xf800) >> 11); 31352da9a59SGnoCiYeH } 31452da9a59SGnoCiYeH } 31552da9a59SGnoCiYeH 24 | 32 => { 31652da9a59SGnoCiYeH red >>= 8; 31752da9a59SGnoCiYeH green >>= 8; 31852da9a59SGnoCiYeH blue >>= 8; 31952da9a59SGnoCiYeH fb_data.pesudo_palette[regno as usize] = ((red as u32) << var.red.offset) 32052da9a59SGnoCiYeH | ((green as u32) << var.green.offset) 32152da9a59SGnoCiYeH | ((blue as u32) << var.blue.offset); 32252da9a59SGnoCiYeH } 32352da9a59SGnoCiYeH _ => {} 32452da9a59SGnoCiYeH } 32552da9a59SGnoCiYeH } 32652da9a59SGnoCiYeH 32752da9a59SGnoCiYeH Ok(()) 328c566df45SLoGin } 329c566df45SLoGin 330c566df45SLoGin fn fb_blank(&self, _blank_mode: BlankMode) -> Result<(), SystemError> { 331c566df45SLoGin todo!() 332c566df45SLoGin } 333c566df45SLoGin 334c566df45SLoGin fn fb_destroy(&self) { 335c566df45SLoGin todo!() 336c566df45SLoGin } 33702343d0bSLoGin 33802343d0bSLoGin fn fb_read(&self, buf: &mut [u8], pos: usize) -> Result<usize, SystemError> { 33902343d0bSLoGin let bp = boot_params().read(); 34002343d0bSLoGin 34102343d0bSLoGin let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?; 34202343d0bSLoGin let size = self.current_fb_fix().smem_len; 34302343d0bSLoGin drop(bp); 34402343d0bSLoGin if pos >= size { 34502343d0bSLoGin return Ok(0); 34602343d0bSLoGin } 34702343d0bSLoGin 34802343d0bSLoGin let pos = pos as i64; 34902343d0bSLoGin let size = size as i64; 35002343d0bSLoGin 35102343d0bSLoGin let len = core::cmp::min(size - pos, buf.len() as i64) as usize; 35202343d0bSLoGin 35302343d0bSLoGin let slice = unsafe { core::slice::from_raw_parts(vaddr.as_ptr::<u8>(), size as usize) }; 35402343d0bSLoGin buf[..len].copy_from_slice(&slice[pos as usize..(pos as usize + len)]); 35502343d0bSLoGin 35602343d0bSLoGin return Ok(len); 35702343d0bSLoGin } 35802343d0bSLoGin 35902343d0bSLoGin fn fb_write(&self, buf: &[u8], pos: usize) -> Result<usize, SystemError> { 36002343d0bSLoGin let bp = boot_params().read(); 36102343d0bSLoGin 36202343d0bSLoGin let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?; 36302343d0bSLoGin let size = self.current_fb_fix().smem_len; 36402343d0bSLoGin 36502343d0bSLoGin if pos >= size { 36602343d0bSLoGin return Ok(0); 36702343d0bSLoGin } 36802343d0bSLoGin 36902343d0bSLoGin let pos = pos as i64; 37002343d0bSLoGin let size = size as i64; 37102343d0bSLoGin 37202343d0bSLoGin let len = core::cmp::min(size - pos, buf.len() as i64) as usize; 37302343d0bSLoGin 37402343d0bSLoGin let slice = unsafe { core::slice::from_raw_parts_mut(vaddr.as_ptr::<u8>(), size as usize) }; 37502343d0bSLoGin slice[pos as usize..(pos as usize + len)].copy_from_slice(&buf[..len]); 37602343d0bSLoGin 37702343d0bSLoGin return Ok(len); 37802343d0bSLoGin } 37952da9a59SGnoCiYeH 38052da9a59SGnoCiYeH fn fb_image_blit(&self, image: &super::base::FbImage) { 38152da9a59SGnoCiYeH self.generic_imageblit(image); 38252da9a59SGnoCiYeH } 38352da9a59SGnoCiYeH 38452da9a59SGnoCiYeH /// ## 填充矩形 38552da9a59SGnoCiYeH fn fb_fillrect(&self, rect: super::base::FillRectData) -> Result<(), SystemError> { 38652da9a59SGnoCiYeH // kwarn!("rect {rect:?}"); 38752da9a59SGnoCiYeH 38852da9a59SGnoCiYeH let boot_param = boot_params().read(); 38952da9a59SGnoCiYeH let screen_base = boot_param 39052da9a59SGnoCiYeH .screen_info 39152da9a59SGnoCiYeH .lfb_virt_base 39252da9a59SGnoCiYeH .ok_or(SystemError::ENODEV)?; 393*b5b571e0SLoGin 394*b5b571e0SLoGin let fg = if self.current_fb_fix().visual == FbVisual::TrueColor 39552da9a59SGnoCiYeH || self.current_fb_fix().visual == FbVisual::DirectColor 39652da9a59SGnoCiYeH { 397*b5b571e0SLoGin self.fb_data.read().pesudo_palette[rect.color as usize] 39852da9a59SGnoCiYeH } else { 399*b5b571e0SLoGin rect.color 400*b5b571e0SLoGin }; 40152da9a59SGnoCiYeH 40252da9a59SGnoCiYeH let bpp = self.current_fb_var().bits_per_pixel; 40352da9a59SGnoCiYeH // 每行像素数 40452da9a59SGnoCiYeH let line_offset = self.current_fb_var().xres; 40552da9a59SGnoCiYeH match bpp { 40652da9a59SGnoCiYeH 32 => { 40752da9a59SGnoCiYeH let base = screen_base.as_ptr::<u32>(); 40852da9a59SGnoCiYeH 40952da9a59SGnoCiYeH for y in rect.dy..(rect.dy + rect.height) { 41052da9a59SGnoCiYeH for x in rect.dx..(rect.dx + rect.width) { 41152da9a59SGnoCiYeH unsafe { *base.add((y * line_offset + x) as usize) = fg }; 41252da9a59SGnoCiYeH } 41352da9a59SGnoCiYeH } 41452da9a59SGnoCiYeH } 41552da9a59SGnoCiYeH _ => todo!(), 41652da9a59SGnoCiYeH } 41752da9a59SGnoCiYeH 41852da9a59SGnoCiYeH Ok(()) 41952da9a59SGnoCiYeH } 42052da9a59SGnoCiYeH 42152bcb59eSGnoCiYeH #[inline(never)] 42252bcb59eSGnoCiYeH fn fb_copyarea(&self, data: super::base::CopyAreaData) { 42352da9a59SGnoCiYeH let bp = boot_params().read(); 42452bcb59eSGnoCiYeH let base = bp.screen_info.lfb_virt_base.unwrap(); 42552da9a59SGnoCiYeH let var = self.current_fb_var(); 42652da9a59SGnoCiYeH 42752bcb59eSGnoCiYeH // 原区域或者目标区域全在屏幕外,则直接返回 42852bcb59eSGnoCiYeH if data.sx > var.xres as i32 42952bcb59eSGnoCiYeH || data.sy > var.yres as i32 43052bcb59eSGnoCiYeH || data.dx > var.xres as i32 43152bcb59eSGnoCiYeH || data.dy > var.yres as i32 43252bcb59eSGnoCiYeH || (data.sx + data.width as i32) < 0 43352bcb59eSGnoCiYeH || (data.sy + data.height as i32) < 0 43452bcb59eSGnoCiYeH || (data.dx + data.width as i32) < 0 43552bcb59eSGnoCiYeH || (data.dy + data.height as i32) < 0 43652da9a59SGnoCiYeH { 43752bcb59eSGnoCiYeH return; 43852da9a59SGnoCiYeH } 43952da9a59SGnoCiYeH 44052bcb59eSGnoCiYeH // 求两个矩形可视范围交集 44152bcb59eSGnoCiYeH let (s_visiable_x, s_w) = if data.sx < 0 { 44252bcb59eSGnoCiYeH (0, (data.width - ((-data.sx) as u32)).min(var.xres)) 44352bcb59eSGnoCiYeH } else { 44452bcb59eSGnoCiYeH let w = if data.sx as u32 + data.width > var.xres { 44552bcb59eSGnoCiYeH var.xres - data.sx as u32 44652bcb59eSGnoCiYeH } else { 44752bcb59eSGnoCiYeH data.width 44852bcb59eSGnoCiYeH }; 44952bcb59eSGnoCiYeH 45052bcb59eSGnoCiYeH (data.sx, w) 45152bcb59eSGnoCiYeH }; 45252bcb59eSGnoCiYeH let (s_visiable_y, s_h) = if data.sy < 0 { 45352bcb59eSGnoCiYeH (0, (data.height - ((-data.sy) as u32).min(var.yres))) 45452bcb59eSGnoCiYeH } else { 45552bcb59eSGnoCiYeH let h = if data.sy as u32 + data.height > var.yres { 45652bcb59eSGnoCiYeH var.yres - data.sy as u32 45752bcb59eSGnoCiYeH } else { 45852bcb59eSGnoCiYeH data.height 45952bcb59eSGnoCiYeH }; 46052bcb59eSGnoCiYeH 46152bcb59eSGnoCiYeH (data.sy, h) 46252bcb59eSGnoCiYeH }; 46352bcb59eSGnoCiYeH 46452bcb59eSGnoCiYeH let (d_visiable_x, d_w) = if data.dx < 0 { 46552bcb59eSGnoCiYeH (0, (data.width - ((-data.dx) as u32)).min(var.xres)) 46652bcb59eSGnoCiYeH } else { 46752bcb59eSGnoCiYeH let w = if data.dx as u32 + data.width > var.xres { 46852bcb59eSGnoCiYeH var.xres - data.dx as u32 46952bcb59eSGnoCiYeH } else { 47052bcb59eSGnoCiYeH data.width 47152bcb59eSGnoCiYeH }; 47252bcb59eSGnoCiYeH 47352bcb59eSGnoCiYeH (data.dx, w) 47452bcb59eSGnoCiYeH }; 47552bcb59eSGnoCiYeH let (d_visiable_y, d_h) = if data.dy < 0 { 47652bcb59eSGnoCiYeH (0, (data.height - ((-data.dy) as u32).min(var.yres))) 47752bcb59eSGnoCiYeH } else { 47852bcb59eSGnoCiYeH let h = if data.dy as u32 + data.height > var.yres { 47952bcb59eSGnoCiYeH var.yres - data.dy as u32 48052bcb59eSGnoCiYeH } else { 48152bcb59eSGnoCiYeH data.height 48252bcb59eSGnoCiYeH }; 48352bcb59eSGnoCiYeH 48452bcb59eSGnoCiYeH (data.dy, h) 48552bcb59eSGnoCiYeH }; 48652bcb59eSGnoCiYeH 48752bcb59eSGnoCiYeH // 可视范围无交集 48852bcb59eSGnoCiYeH if !(d_h + s_h > data.height && s_w + d_w > data.width) { 48952bcb59eSGnoCiYeH return; 49052bcb59eSGnoCiYeH } 49152bcb59eSGnoCiYeH 49252bcb59eSGnoCiYeH // 可视区域左上角相对于矩形的坐标 49352bcb59eSGnoCiYeH let s_relative_x = s_visiable_x - data.sx; 49452bcb59eSGnoCiYeH let s_relative_y = s_visiable_y - data.sy; 49552bcb59eSGnoCiYeH let d_relative_x = d_visiable_x - data.dx; 49652bcb59eSGnoCiYeH let d_relative_y = d_visiable_y - data.dy; 49752bcb59eSGnoCiYeH 49852bcb59eSGnoCiYeH let visiable_x = s_relative_x.max(d_relative_x); 49952bcb59eSGnoCiYeH let visiable_y = s_relative_y.max(d_relative_y); 50052bcb59eSGnoCiYeH let visiable_h = d_h + s_h - data.height; 50152bcb59eSGnoCiYeH let visiable_w = d_w + s_w - data.width; 50252bcb59eSGnoCiYeH 50352bcb59eSGnoCiYeH let s_real_x = (visiable_x + data.sx) as u32; 50452bcb59eSGnoCiYeH let s_real_y = (visiable_y + data.sy) as u32; 50552bcb59eSGnoCiYeH let d_real_x = (visiable_x + data.dx) as u32; 50652bcb59eSGnoCiYeH let d_real_y = (visiable_y + data.dy) as u32; 50752bcb59eSGnoCiYeH 50852da9a59SGnoCiYeH let bytes_per_pixel = var.bits_per_pixel >> 3; 50952da9a59SGnoCiYeH let bytes_per_line = var.xres * bytes_per_pixel; 51052da9a59SGnoCiYeH 51152bcb59eSGnoCiYeH let src = 51252bcb59eSGnoCiYeH base + VirtAddr::new((s_real_y * bytes_per_line + s_real_x * bytes_per_pixel) as usize); 51352da9a59SGnoCiYeH 51452bcb59eSGnoCiYeH let dst = 51552bcb59eSGnoCiYeH base + VirtAddr::new((d_real_y * bytes_per_line + d_real_x * bytes_per_pixel) as usize); 51652da9a59SGnoCiYeH 51752bcb59eSGnoCiYeH let size = (visiable_h * visiable_w) as usize; 51852da9a59SGnoCiYeH 51952da9a59SGnoCiYeH match bytes_per_pixel { 52052da9a59SGnoCiYeH 4 => { 52152da9a59SGnoCiYeH // 32bpp 52252da9a59SGnoCiYeH let mut dst = dst.as_ptr::<u32>(); 52352da9a59SGnoCiYeH let mut src = src.as_ptr::<u32>(); 52452bcb59eSGnoCiYeH let line_offset = var.xres as usize; 52552da9a59SGnoCiYeH 52652bcb59eSGnoCiYeH if s_real_x > d_real_x { 52752bcb59eSGnoCiYeH // 如果src在dst下方,则可以直接拷贝不会出现指针覆盖 52852da9a59SGnoCiYeH unsafe { 52952bcb59eSGnoCiYeH for _ in 0..visiable_h { 53052bcb59eSGnoCiYeH core::ptr::copy(src, dst, visiable_w as usize); 53152bcb59eSGnoCiYeH src = src.add(line_offset); 53252bcb59eSGnoCiYeH dst = dst.add(visiable_w as usize); 53352da9a59SGnoCiYeH } 53452da9a59SGnoCiYeH } 53552da9a59SGnoCiYeH } else { 536*b5b571e0SLoGin let mut tmp: Vec<u32> = vec![0; size]; 53752bcb59eSGnoCiYeH let mut tmp_ptr = tmp.as_mut_ptr(); 53852bcb59eSGnoCiYeH 53952bcb59eSGnoCiYeH // 这里是一个可以优化的点,现在为了避免指针拷贝时覆盖,统一先拷贝进入buf再拷贝到dst 54052da9a59SGnoCiYeH unsafe { 54152bcb59eSGnoCiYeH for _ in 0..visiable_h { 54252bcb59eSGnoCiYeH core::ptr::copy(src, tmp_ptr, visiable_w as usize); 54352bcb59eSGnoCiYeH src = src.add(line_offset); 54452bcb59eSGnoCiYeH tmp_ptr = tmp_ptr.add(visiable_w as usize); 54552bcb59eSGnoCiYeH } 54652bcb59eSGnoCiYeH 54752bcb59eSGnoCiYeH tmp_ptr = tmp_ptr.sub(size); 54852bcb59eSGnoCiYeH for _ in 0..visiable_h { 54952bcb59eSGnoCiYeH core::ptr::copy(tmp_ptr, dst, visiable_w as usize); 55052bcb59eSGnoCiYeH dst = dst.add(line_offset); 55152bcb59eSGnoCiYeH tmp_ptr = tmp_ptr.add(visiable_w as usize); 55252da9a59SGnoCiYeH } 55352da9a59SGnoCiYeH } 55452da9a59SGnoCiYeH } 55552da9a59SGnoCiYeH } 55652da9a59SGnoCiYeH _ => { 55752da9a59SGnoCiYeH todo!() 55852da9a59SGnoCiYeH } 55952da9a59SGnoCiYeH } 56052da9a59SGnoCiYeH } 561c566df45SLoGin } 562c566df45SLoGin 563c566df45SLoGin impl FrameBufferInfo for VesaFb { 564c566df45SLoGin fn fb_device(&self) -> Option<Arc<FbDevice>> { 565c566df45SLoGin self.inner.lock().fb_device.clone() 566c566df45SLoGin } 567c566df45SLoGin 568c566df45SLoGin fn set_fb_device(&self, device: Option<Arc<FbDevice>>) { 569c566df45SLoGin self.inner.lock().fb_device = device; 570c566df45SLoGin } 571c566df45SLoGin 572c566df45SLoGin fn screen_size(&self) -> usize { 573c566df45SLoGin todo!() 574c566df45SLoGin } 575c566df45SLoGin 576c566df45SLoGin fn current_fb_var(&self) -> FbVarScreenInfo { 577*b5b571e0SLoGin *VESAFB_DEFINED.read() 578c566df45SLoGin } 579c566df45SLoGin 580c566df45SLoGin fn current_fb_fix(&self) -> FixedScreenInfo { 581*b5b571e0SLoGin *VESAFB_FIX_INFO.read() 582c566df45SLoGin } 583c566df45SLoGin 584c566df45SLoGin fn video_mode(&self) -> Option<&FbVideoMode> { 585c566df45SLoGin todo!() 586c566df45SLoGin } 587c566df45SLoGin 588c566df45SLoGin fn state(&self) -> FbState { 589c566df45SLoGin self.inner.lock().fb_state 590c566df45SLoGin } 59152da9a59SGnoCiYeH 59252da9a59SGnoCiYeH fn framebuffer_info_data(&self) -> &RwLock<FrameBufferInfoData> { 59352da9a59SGnoCiYeH &self.fb_data 59452da9a59SGnoCiYeH } 595c566df45SLoGin } 596c566df45SLoGin 597c566df45SLoGin #[derive(Debug)] 598c566df45SLoGin #[cast_to([sync] PlatformDriver)] 599c566df45SLoGin struct VesaFbDriver { 600c566df45SLoGin inner: SpinLock<InnerVesaFbDriver>, 601c566df45SLoGin kobj_state: LockedKObjectState, 602c566df45SLoGin } 603c566df45SLoGin 604c566df45SLoGin impl VesaFbDriver { 605c566df45SLoGin pub fn new() -> Arc<Self> { 606c566df45SLoGin let r = Arc::new(Self { 607c566df45SLoGin inner: SpinLock::new(InnerVesaFbDriver { 608c566df45SLoGin ktype: None, 609c566df45SLoGin kset: None, 610c566df45SLoGin parent: None, 611c566df45SLoGin kernfs_inode: None, 612c566df45SLoGin devices: Vec::new(), 613c566df45SLoGin bus: None, 614c566df45SLoGin self_ref: Weak::new(), 615c566df45SLoGin }), 616c566df45SLoGin kobj_state: LockedKObjectState::new(None), 617c566df45SLoGin }); 618c566df45SLoGin 619c566df45SLoGin r.inner.lock().self_ref = Arc::downgrade(&r); 620c566df45SLoGin 621c566df45SLoGin return r; 622c566df45SLoGin } 623c566df45SLoGin } 624c566df45SLoGin 625c566df45SLoGin #[derive(Debug)] 626c566df45SLoGin struct InnerVesaFbDriver { 627c566df45SLoGin ktype: Option<&'static dyn KObjType>, 628c566df45SLoGin kset: Option<Arc<KSet>>, 629c566df45SLoGin parent: Option<Weak<dyn KObject>>, 630c566df45SLoGin kernfs_inode: Option<Arc<KernFSInode>>, 631c566df45SLoGin devices: Vec<Arc<dyn Device>>, 632c566df45SLoGin bus: Option<Weak<dyn Bus>>, 633c566df45SLoGin 634c566df45SLoGin self_ref: Weak<VesaFbDriver>, 635c566df45SLoGin } 636c566df45SLoGin 637c566df45SLoGin impl VesaFbDriver { 638c566df45SLoGin const NAME: &'static str = "vesa-framebuffer"; 639c566df45SLoGin } 640c566df45SLoGin 641c566df45SLoGin impl PlatformDriver for VesaFbDriver { 642c566df45SLoGin fn probe(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 643c566df45SLoGin let device = device 644c566df45SLoGin .clone() 645c566df45SLoGin .arc_any() 646c566df45SLoGin .downcast::<VesaFb>() 647c566df45SLoGin .map_err(|_| SystemError::EINVAL)?; 648c566df45SLoGin 649c566df45SLoGin device.set_driver(Some(self.inner.lock_irqsave().self_ref.clone())); 650c566df45SLoGin 651c566df45SLoGin return Ok(()); 652c566df45SLoGin } 653c566df45SLoGin 654c566df45SLoGin fn remove(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 655c566df45SLoGin todo!() 656c566df45SLoGin } 657c566df45SLoGin 658c566df45SLoGin fn shutdown(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 659c566df45SLoGin // do nothing 660c566df45SLoGin return Ok(()); 661c566df45SLoGin } 662c566df45SLoGin 663c566df45SLoGin fn suspend(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 664c566df45SLoGin // do nothing 665c566df45SLoGin return Ok(()); 666c566df45SLoGin } 667c566df45SLoGin 668c566df45SLoGin fn resume(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 669c566df45SLoGin todo!() 670c566df45SLoGin } 671c566df45SLoGin } 672c566df45SLoGin 673c566df45SLoGin impl Driver for VesaFbDriver { 674c566df45SLoGin fn id_table(&self) -> Option<IdTable> { 675c566df45SLoGin Some(IdTable::new(VesaFb::NAME.to_string(), None)) 676c566df45SLoGin } 677c566df45SLoGin 678c566df45SLoGin fn devices(&self) -> Vec<Arc<dyn Device>> { 679c566df45SLoGin self.inner.lock().devices.clone() 680c566df45SLoGin } 681c566df45SLoGin 682c566df45SLoGin fn add_device(&self, device: Arc<dyn Device>) { 683c566df45SLoGin let mut guard = self.inner.lock(); 684c566df45SLoGin // check if the device is already in the list 685c566df45SLoGin if guard.devices.iter().any(|dev| Arc::ptr_eq(dev, &device)) { 686c566df45SLoGin return; 687c566df45SLoGin } 688c566df45SLoGin 689c566df45SLoGin guard.devices.push(device); 690c566df45SLoGin } 691c566df45SLoGin 692c566df45SLoGin fn delete_device(&self, device: &Arc<dyn Device>) { 693c566df45SLoGin let mut guard = self.inner.lock(); 694c566df45SLoGin guard.devices.retain(|dev| !Arc::ptr_eq(dev, device)); 695c566df45SLoGin } 696c566df45SLoGin 697c566df45SLoGin fn set_bus(&self, bus: Option<Weak<dyn Bus>>) { 698c566df45SLoGin self.inner.lock().bus = bus; 699c566df45SLoGin } 700c566df45SLoGin 701c566df45SLoGin fn bus(&self) -> Option<Weak<dyn Bus>> { 702c566df45SLoGin self.inner.lock().bus.clone() 703c566df45SLoGin } 704c566df45SLoGin 705c566df45SLoGin fn dev_groups(&self) -> &'static [&'static dyn AttributeGroup] { 706c566df45SLoGin return &[&VesaFbAnonAttributeGroup]; 707c566df45SLoGin } 708c566df45SLoGin } 709c566df45SLoGin 710c566df45SLoGin impl KObject for VesaFbDriver { 711c566df45SLoGin fn as_any_ref(&self) -> &dyn core::any::Any { 712c566df45SLoGin self 713c566df45SLoGin } 714c566df45SLoGin 715c566df45SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 716c566df45SLoGin self.inner.lock().kernfs_inode = inode; 717c566df45SLoGin } 718c566df45SLoGin 719c566df45SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 720c566df45SLoGin self.inner.lock().kernfs_inode.clone() 721c566df45SLoGin } 722c566df45SLoGin 723c566df45SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 724c566df45SLoGin self.inner.lock().parent.clone() 725c566df45SLoGin } 726c566df45SLoGin 727c566df45SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 728c566df45SLoGin self.inner.lock().parent = parent; 729c566df45SLoGin } 730c566df45SLoGin 731c566df45SLoGin fn kset(&self) -> Option<Arc<KSet>> { 732c566df45SLoGin self.inner.lock().kset.clone() 733c566df45SLoGin } 734c566df45SLoGin 735c566df45SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 736c566df45SLoGin self.inner.lock().kset = kset; 737c566df45SLoGin } 738c566df45SLoGin 739c566df45SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 740c566df45SLoGin self.inner.lock().ktype 741c566df45SLoGin } 742c566df45SLoGin 743c566df45SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 744c566df45SLoGin self.inner.lock().ktype = ktype; 745c566df45SLoGin } 746c566df45SLoGin 747c566df45SLoGin fn name(&self) -> String { 748c566df45SLoGin Self::NAME.to_string() 749c566df45SLoGin } 750c566df45SLoGin 751c566df45SLoGin fn set_name(&self, _name: String) { 752c566df45SLoGin // do nothing 753c566df45SLoGin } 754c566df45SLoGin 755c566df45SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 756c566df45SLoGin self.kobj_state.read() 757c566df45SLoGin } 758c566df45SLoGin 759c566df45SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 760c566df45SLoGin self.kobj_state.write() 761c566df45SLoGin } 762c566df45SLoGin 763c566df45SLoGin fn set_kobj_state(&self, state: KObjectState) { 764c566df45SLoGin *self.kobj_state.write() = state; 765c566df45SLoGin } 766c566df45SLoGin } 767c566df45SLoGin 768c566df45SLoGin #[derive(Debug)] 769c566df45SLoGin struct VesaFbAnonAttributeGroup; 770c566df45SLoGin 771c566df45SLoGin impl AttributeGroup for VesaFbAnonAttributeGroup { 772c566df45SLoGin fn name(&self) -> Option<&str> { 773c566df45SLoGin None 774c566df45SLoGin } 775c566df45SLoGin 776c566df45SLoGin fn attrs(&self) -> &[&'static dyn Attribute] { 777c566df45SLoGin &[&AnonAttrPhysAddr as &'static dyn Attribute] 778c566df45SLoGin } 779c566df45SLoGin 780c566df45SLoGin fn is_visible( 781c566df45SLoGin &self, 782c566df45SLoGin _kobj: Arc<dyn KObject>, 783c566df45SLoGin attr: &'static dyn Attribute, 784c566df45SLoGin ) -> Option<ModeType> { 785c566df45SLoGin Some(attr.mode()) 786c566df45SLoGin } 787c566df45SLoGin } 788c566df45SLoGin 789c566df45SLoGin #[derive(Debug)] 790c566df45SLoGin struct AnonAttrPhysAddr; 791c566df45SLoGin 792c566df45SLoGin impl Attribute for AnonAttrPhysAddr { 793c566df45SLoGin fn name(&self) -> &str { 794c566df45SLoGin "smem_start" 795c566df45SLoGin } 796c566df45SLoGin 797c566df45SLoGin fn mode(&self) -> ModeType { 798c566df45SLoGin ModeType::S_IRUGO 799c566df45SLoGin } 800c566df45SLoGin 801c566df45SLoGin fn support(&self) -> SysFSOpsSupport { 802196b75dcSLoGin SysFSOpsSupport::ATTR_SHOW 803c566df45SLoGin } 804c566df45SLoGin 805c566df45SLoGin fn show(&self, _kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 806c566df45SLoGin sysfs_emit_str( 807c566df45SLoGin buf, 808c566df45SLoGin format!( 809c566df45SLoGin "0x{:x}\n", 810c566df45SLoGin VESAFB_FIX_INFO 811c566df45SLoGin .read() 812c566df45SLoGin .smem_start 813c566df45SLoGin .unwrap_or(PhysAddr::new(0)) 814c566df45SLoGin .data() 815c566df45SLoGin ) 816c566df45SLoGin .as_str(), 817c566df45SLoGin ) 818c566df45SLoGin } 819c566df45SLoGin } 820c566df45SLoGin 821c566df45SLoGin #[unified_init(INITCALL_DEVICE)] 822c566df45SLoGin pub fn vesa_fb_driver_init() -> Result<(), SystemError> { 823c566df45SLoGin let driver = VesaFbDriver::new(); 824c566df45SLoGin 825c566df45SLoGin platform_driver_manager().register(driver)?; 826c566df45SLoGin 827c566df45SLoGin return Ok(()); 828c566df45SLoGin } 829c566df45SLoGin 830c566df45SLoGin /// 在内存管理初始化之前,初始化vesafb 831c566df45SLoGin pub fn vesafb_early_init() -> Result<VirtAddr, SystemError> { 832c566df45SLoGin let mut _reserved: u32 = 0; 833c566df45SLoGin 834c566df45SLoGin let mut fb_info: MaybeUninit<multiboot_tag_framebuffer_info_t> = MaybeUninit::uninit(); 835c566df45SLoGin //从multiboot2中读取帧缓冲区信息至fb_info 836c566df45SLoGin 837c566df45SLoGin // todo: 换成rust的,并且检测是否成功获取 838c566df45SLoGin unsafe { 839c566df45SLoGin multiboot2_iter( 840c566df45SLoGin Some(multiboot2_get_Framebuffer_info), 841c566df45SLoGin fb_info.as_mut_ptr() as usize as *mut c_void, 842c566df45SLoGin &mut _reserved as *mut c_uint, 843c566df45SLoGin ) 844c566df45SLoGin }; 845c566df45SLoGin unsafe { fb_info.assume_init() }; 846c566df45SLoGin let fb_info: multiboot_tag_framebuffer_info_t = unsafe { core::mem::transmute(fb_info) }; 847c566df45SLoGin 848c566df45SLoGin // todo: 判断是否有vesa帧缓冲区,这里暂时直接设置true 849c566df45SLoGin HAS_VESA_FB.store(true, core::sync::atomic::Ordering::SeqCst); 850c566df45SLoGin 851c566df45SLoGin let width = fb_info.framebuffer_width; 852c566df45SLoGin let height = fb_info.framebuffer_height; 853c566df45SLoGin 854c566df45SLoGin let mut boot_params_guard = boot_params().write(); 855c566df45SLoGin let boottime_screen_info = &mut boot_params_guard.screen_info; 856c566df45SLoGin 857c566df45SLoGin boottime_screen_info.is_vga = true; 858c566df45SLoGin 859c566df45SLoGin boottime_screen_info.lfb_base = PhysAddr::new(fb_info.framebuffer_addr as usize); 860c566df45SLoGin 861c566df45SLoGin if fb_info.framebuffer_type == 2 { 862c566df45SLoGin //当type=2时,width与height用字符数表示,故depth=8 863c566df45SLoGin boottime_screen_info.origin_video_cols = width as u8; 864c566df45SLoGin boottime_screen_info.origin_video_lines = height as u8; 865c566df45SLoGin boottime_screen_info.video_type = BootTimeVideoType::Mda; 866c566df45SLoGin boottime_screen_info.lfb_depth = 8; 867c566df45SLoGin } else { 868c566df45SLoGin //否则为图像模式,depth应参照帧缓冲区信息里面的每个像素的位数 869c566df45SLoGin boottime_screen_info.lfb_width = width; 870c566df45SLoGin boottime_screen_info.lfb_height = height; 871c566df45SLoGin boottime_screen_info.video_type = BootTimeVideoType::Vlfb; 872c566df45SLoGin boottime_screen_info.lfb_depth = fb_info.framebuffer_bpp as u8; 873c566df45SLoGin } 874c566df45SLoGin 875c566df45SLoGin boottime_screen_info.lfb_size = 876c566df45SLoGin (width * height * ((fb_info.framebuffer_bpp as u32 + 7) / 8)) as usize; 877c566df45SLoGin 878c75ef4e2SLoGin // let buf_vaddr = VirtAddr::new(0xffff800003200000); 879c75ef4e2SLoGin let buf_vaddr = VirtAddr::new( 880c75ef4e2SLoGin crate::include::bindings::bindings::SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE as usize 881c75ef4e2SLoGin + FRAME_BUFFER_MAPPING_OFFSET as usize, 882c75ef4e2SLoGin ); 883c566df45SLoGin boottime_screen_info.lfb_virt_base = Some(buf_vaddr); 884c566df45SLoGin 885c566df45SLoGin let init_text = "Video driver to map.\n\0"; 886c566df45SLoGin send_to_default_serial8250_port(init_text.as_bytes()); 887c566df45SLoGin 888c566df45SLoGin // 地址映射 889c566df45SLoGin let paddr = PhysAddr::new(fb_info.framebuffer_addr as usize); 890c566df45SLoGin let count = 891c566df45SLoGin PageFrameCount::new(page_align_up(boottime_screen_info.lfb_size) / MMArch::PAGE_SIZE); 892c566df45SLoGin unsafe { pseudo_map_phys(buf_vaddr, paddr, count) }; 893c566df45SLoGin return Ok(buf_vaddr); 894c566df45SLoGin } 895c566df45SLoGin 896c566df45SLoGin #[unified_init(INITCALL_DEVICE)] 897c566df45SLoGin fn vesa_fb_device_init() -> Result<(), SystemError> { 898c566df45SLoGin // 如果没有vesa帧缓冲区,直接返回 899c566df45SLoGin if !HAS_VESA_FB.load(core::sync::atomic::Ordering::SeqCst) { 900c566df45SLoGin return Ok(()); 901c566df45SLoGin } 902c566df45SLoGin 903c566df45SLoGin static INIT: Once = Once::new(); 904c566df45SLoGin INIT.call_once(|| { 905c566df45SLoGin kinfo!("vesa fb device init"); 90652da9a59SGnoCiYeH let device = Arc::new(VesaFb::new()); 907c566df45SLoGin 90852da9a59SGnoCiYeH let mut fb_fix = VESAFB_FIX_INFO.write_irqsave(); 90952da9a59SGnoCiYeH let mut fb_var = VESAFB_DEFINED.write_irqsave(); 910c566df45SLoGin 911c566df45SLoGin let boot_params_guard = boot_params().read(); 912c566df45SLoGin let boottime_screen_info = &boot_params_guard.screen_info; 913c566df45SLoGin 91452da9a59SGnoCiYeH fb_fix.smem_start = Some(boottime_screen_info.lfb_base); 91552da9a59SGnoCiYeH fb_fix.smem_len = boottime_screen_info.lfb_size; 916c566df45SLoGin 917c566df45SLoGin if boottime_screen_info.video_type == BootTimeVideoType::Mda { 91852da9a59SGnoCiYeH fb_fix.visual = FbVisual::Mono10; 91952da9a59SGnoCiYeH fb_var.bits_per_pixel = 8; 92052da9a59SGnoCiYeH fb_fix.line_length = 92152da9a59SGnoCiYeH (boottime_screen_info.origin_video_cols as u32) * (fb_var.bits_per_pixel / 8); 92252da9a59SGnoCiYeH fb_var.xres_virtual = boottime_screen_info.origin_video_cols as u32; 92352da9a59SGnoCiYeH fb_var.yres_virtual = boottime_screen_info.origin_video_lines as u32; 924c566df45SLoGin } else { 92552da9a59SGnoCiYeH fb_fix.visual = FbVisual::TrueColor; 92652da9a59SGnoCiYeH fb_var.bits_per_pixel = boottime_screen_info.lfb_depth as u32; 92752da9a59SGnoCiYeH fb_fix.line_length = 92852da9a59SGnoCiYeH (boottime_screen_info.lfb_width as u32) * (fb_var.bits_per_pixel / 8); 92952da9a59SGnoCiYeH fb_var.xres_virtual = boottime_screen_info.lfb_width as u32; 93052da9a59SGnoCiYeH fb_var.yres_virtual = boottime_screen_info.lfb_height as u32; 93152da9a59SGnoCiYeH fb_var.xres = boottime_screen_info.lfb_width as u32; 93252da9a59SGnoCiYeH fb_var.yres = boottime_screen_info.lfb_height as u32; 933c566df45SLoGin } 934c566df45SLoGin 93552da9a59SGnoCiYeH fb_var.red.length = boottime_screen_info.red_size as u32; 93652da9a59SGnoCiYeH fb_var.green.length = boottime_screen_info.green_size as u32; 93752da9a59SGnoCiYeH fb_var.blue.length = boottime_screen_info.blue_size as u32; 938c566df45SLoGin 93952da9a59SGnoCiYeH fb_var.red.offset = boottime_screen_info.red_pos as u32; 94052da9a59SGnoCiYeH fb_var.green.offset = boottime_screen_info.green_pos as u32; 94152da9a59SGnoCiYeH fb_var.blue.offset = boottime_screen_info.blue_pos as u32; 94252da9a59SGnoCiYeH 94352da9a59SGnoCiYeH // TODO: 这里是暂时这样写的,初始化为RGB888格式,后续vesa初始化完善后删掉下面 94452da9a59SGnoCiYeH fb_var.red.offset = 16; 94552da9a59SGnoCiYeH fb_var.green.offset = 8; 94652da9a59SGnoCiYeH fb_var.blue.offset = 0; 94752da9a59SGnoCiYeH 94852da9a59SGnoCiYeH if fb_var.bits_per_pixel >= 1 && fb_var.bits_per_pixel <= 8 { 94952da9a59SGnoCiYeH fb_var.red.length = fb_var.bits_per_pixel; 95052da9a59SGnoCiYeH fb_var.green.length = fb_var.bits_per_pixel; 95152da9a59SGnoCiYeH fb_var.blue.length = fb_var.bits_per_pixel; 95252da9a59SGnoCiYeH } 95352da9a59SGnoCiYeH 954c566df45SLoGin device_manager().device_default_initialize(&(device.clone() as Arc<dyn Device>)); 955c566df45SLoGin 956c566df45SLoGin platform_device_manager() 957c566df45SLoGin .device_add(device.clone() as Arc<dyn PlatformDevice>) 958c566df45SLoGin .expect("vesa_fb_device_init: platform_device_manager().device_add failed"); 959c566df45SLoGin 960c566df45SLoGin frame_buffer_manager() 961c566df45SLoGin .register_fb(device.clone() as Arc<dyn FrameBuffer>) 962c566df45SLoGin .expect("vesa_fb_device_init: frame_buffer_manager().register_fb failed"); 963c566df45SLoGin 96452da9a59SGnoCiYeH // 加入全局fb表 96552da9a59SGnoCiYeH let mut guard = FRAME_BUFFER_SET.write(); 96652da9a59SGnoCiYeH if guard.get(device.fb_id().data() as usize).unwrap().is_some() { 96752da9a59SGnoCiYeH kwarn!( 96852da9a59SGnoCiYeH "vesa_fb_device_init: There is already an element {:?} in the FRAME_BUFFER_SET", 96952da9a59SGnoCiYeH device.fb_id() 97052da9a59SGnoCiYeH ); 97152da9a59SGnoCiYeH } 97252da9a59SGnoCiYeH guard[device.fb_id().data() as usize] = Some(device.clone()); 97352da9a59SGnoCiYeH 974c566df45SLoGin // 设置vesa fb的状态为运行中 975c566df45SLoGin device.inner.lock().fb_state = FbState::Running; 976c566df45SLoGin }); 977c566df45SLoGin 978c566df45SLoGin return Ok(()); 979c566df45SLoGin } 980