1 use core::{ 2 ffi::{c_uint, c_void}, 3 mem::MaybeUninit, 4 sync::atomic::AtomicBool, 5 }; 6 7 use alloc::{ 8 string::{String, ToString}, 9 sync::{Arc, Weak}, 10 vec::Vec, 11 }; 12 use system_error::SystemError; 13 use unified_init::macros::unified_init; 14 15 use crate::{ 16 arch::MMArch, 17 driver::{ 18 base::{ 19 class::Class, 20 device::{ 21 bus::Bus, device_manager, driver::Driver, Device, DeviceState, DeviceType, IdTable, 22 }, 23 kobject::{KObjType, KObject, KObjectState, LockedKObjectState}, 24 kset::KSet, 25 platform::{ 26 platform_device::{platform_device_manager, PlatformDevice}, 27 platform_driver::{platform_driver_manager, PlatformDriver}, 28 CompatibleTable, 29 }, 30 }, 31 serial::serial8250::send_to_default_serial8250_port, 32 video::fbdev::base::{fbmem::frame_buffer_manager, FbVisual, FRAME_BUFFER_SET}, 33 }, 34 filesystem::{ 35 kernfs::KernFSInode, 36 sysfs::{file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport}, 37 vfs::syscall::ModeType, 38 }, 39 include::bindings::bindings::{ 40 multiboot2_get_Framebuffer_info, multiboot2_iter, multiboot_tag_framebuffer_info_t, 41 FRAME_BUFFER_MAPPING_OFFSET, 42 }, 43 init::{boot_params, initcall::INITCALL_DEVICE}, 44 libs::{ 45 align::page_align_up, 46 once::Once, 47 rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}, 48 spinlock::SpinLock, 49 }, 50 mm::{ 51 allocator::page_frame::PageFrameCount, no_init::pseudo_map_phys, MemoryManagementArch, 52 PhysAddr, VirtAddr, 53 }, 54 }; 55 56 use super::base::{ 57 fbmem::FbDevice, BlankMode, BootTimeVideoType, FbAccel, FbActivateFlags, FbId, FbState, FbType, 58 FbVModeFlags, FbVarScreenInfo, FbVideoMode, FixedScreenInfo, FrameBuffer, FrameBufferInfo, 59 FrameBufferInfoData, FrameBufferOps, 60 }; 61 62 /// 当前机器上面是否有vesa帧缓冲区 63 static HAS_VESA_FB: AtomicBool = AtomicBool::new(false); 64 65 lazy_static! { 66 static ref VESAFB_FIX_INFO: RwLock<FixedScreenInfo> = RwLock::new(FixedScreenInfo { 67 id: FixedScreenInfo::name2id("VESA VGA"), 68 fb_type: FbType::PackedPixels, 69 accel: FbAccel::None, 70 ..Default::default() 71 }); 72 static ref VESAFB_DEFINED: RwLock<FbVarScreenInfo> = RwLock::new(FbVarScreenInfo { 73 activate: FbActivateFlags::FB_ACTIVATE_NOW, 74 height: None, 75 width: None, 76 right_margin: 32, 77 upper_margin: 16, 78 lower_margin: 4, 79 vsync_len: 4, 80 vmode: FbVModeFlags::FB_VMODE_NONINTERLACED, 81 82 ..Default::default() 83 }); 84 } 85 86 #[derive(Debug)] 87 #[cast_to([sync] Device)] 88 #[cast_to([sync] PlatformDevice)] 89 pub struct VesaFb { 90 inner: SpinLock<InnerVesaFb>, 91 kobj_state: LockedKObjectState, 92 fb_data: RwLock<FrameBufferInfoData>, 93 } 94 95 impl VesaFb { 96 pub const NAME: &'static str = "vesa_vga"; 97 pub fn new() -> Self { 98 let mut fb_info_data = FrameBufferInfoData::new(); 99 fb_info_data.pesudo_palette.resize(256, 0); 100 return Self { 101 inner: SpinLock::new(InnerVesaFb { 102 bus: None, 103 class: None, 104 driver: None, 105 kern_inode: None, 106 parent: None, 107 kset: None, 108 kobj_type: None, 109 device_state: DeviceState::NotInitialized, 110 pdev_id: 0, 111 pdev_id_auto: false, 112 fb_id: FbId::INIT, 113 fb_device: None, 114 fb_state: FbState::Suspended, 115 }), 116 kobj_state: LockedKObjectState::new(None), 117 fb_data: RwLock::new(fb_info_data), 118 }; 119 } 120 } 121 122 #[derive(Debug)] 123 struct InnerVesaFb { 124 bus: Option<Weak<dyn Bus>>, 125 class: Option<Arc<dyn Class>>, 126 driver: Option<Weak<dyn Driver>>, 127 kern_inode: Option<Arc<KernFSInode>>, 128 parent: Option<Weak<dyn KObject>>, 129 kset: Option<Arc<KSet>>, 130 kobj_type: Option<&'static dyn KObjType>, 131 device_state: DeviceState, 132 pdev_id: i32, 133 pdev_id_auto: bool, 134 fb_id: FbId, 135 fb_device: Option<Arc<FbDevice>>, 136 fb_state: FbState, 137 } 138 139 impl FrameBuffer for VesaFb { 140 fn fb_id(&self) -> FbId { 141 self.inner.lock().fb_id 142 } 143 144 fn set_fb_id(&self, id: FbId) { 145 self.inner.lock().fb_id = id; 146 } 147 } 148 149 impl PlatformDevice for VesaFb { 150 fn pdev_name(&self) -> &str { 151 Self::NAME 152 } 153 154 fn set_pdev_id(&self, id: i32) { 155 self.inner.lock().pdev_id = id; 156 } 157 158 fn set_pdev_id_auto(&self, id_auto: bool) { 159 self.inner.lock().pdev_id_auto = id_auto; 160 } 161 162 fn compatible_table(&self) -> CompatibleTable { 163 todo!() 164 } 165 166 fn is_initialized(&self) -> bool { 167 self.inner.lock().device_state == DeviceState::Initialized 168 } 169 170 fn set_state(&self, set_state: DeviceState) { 171 self.inner.lock().device_state = set_state; 172 } 173 } 174 175 impl Device for VesaFb { 176 fn dev_type(&self) -> DeviceType { 177 DeviceType::Char 178 } 179 180 fn id_table(&self) -> IdTable { 181 IdTable::new(self.name(), None) 182 } 183 184 fn bus(&self) -> Option<Weak<dyn Bus>> { 185 self.inner.lock().bus.clone() 186 } 187 188 fn set_bus(&self, bus: Option<Weak<dyn Bus>>) { 189 self.inner.lock().bus = bus; 190 } 191 192 fn set_class(&self, class: Option<Arc<dyn Class>>) { 193 self.inner.lock().class = class; 194 } 195 196 fn driver(&self) -> Option<Arc<dyn Driver>> { 197 self.inner.lock().driver.clone()?.upgrade() 198 } 199 200 fn set_driver(&self, driver: Option<Weak<dyn Driver>>) { 201 self.inner.lock().driver = driver; 202 } 203 204 fn is_dead(&self) -> bool { 205 false 206 } 207 208 fn can_match(&self) -> bool { 209 true 210 } 211 212 fn set_can_match(&self, _can_match: bool) {} 213 214 fn state_synced(&self) -> bool { 215 true 216 } 217 } 218 219 impl KObject for VesaFb { 220 fn as_any_ref(&self) -> &dyn core::any::Any { 221 self 222 } 223 224 fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 225 self.inner.lock().kern_inode = inode; 226 } 227 228 fn inode(&self) -> Option<Arc<KernFSInode>> { 229 self.inner.lock().kern_inode.clone() 230 } 231 232 fn parent(&self) -> Option<Weak<dyn KObject>> { 233 self.inner.lock().parent.clone() 234 } 235 236 fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 237 self.inner.lock().parent = parent; 238 } 239 240 fn kset(&self) -> Option<Arc<KSet>> { 241 self.inner.lock().kset.clone() 242 } 243 244 fn set_kset(&self, kset: Option<Arc<KSet>>) { 245 self.inner.lock().kset = kset; 246 } 247 248 fn kobj_type(&self) -> Option<&'static dyn KObjType> { 249 self.inner.lock().kobj_type 250 } 251 252 fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 253 self.inner.lock().kobj_type = ktype; 254 } 255 256 fn name(&self) -> String { 257 Self::NAME.to_string() 258 } 259 260 fn set_name(&self, _name: String) { 261 // do nothing 262 } 263 264 fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 265 self.kobj_state.read() 266 } 267 268 fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 269 self.kobj_state.write() 270 } 271 272 fn set_kobj_state(&self, state: KObjectState) { 273 *self.kobj_state.write() = state; 274 } 275 } 276 277 impl FrameBufferOps for VesaFb { 278 fn fb_open(&self, _user: bool) { 279 todo!() 280 } 281 282 fn fb_release(&self, _user: bool) { 283 todo!() 284 } 285 286 fn fb_set_color_register( 287 &self, 288 regno: u16, 289 mut red: u16, 290 mut green: u16, 291 mut blue: u16, 292 ) -> Result<(), SystemError> { 293 let mut fb_data = self.framebuffer_info_data().write(); 294 let var = self.current_fb_var(); 295 if regno as usize >= fb_data.pesudo_palette.len() { 296 return Err(SystemError::E2BIG); 297 } 298 299 if var.bits_per_pixel == 8 { 300 todo!("vesa_setpalette todo"); 301 } else if regno < 16 { 302 match var.bits_per_pixel { 303 16 => { 304 if var.red.offset == 10 { 305 // RGB 1:5:5:5 306 fb_data.pesudo_palette[regno as usize] = ((red as u32 & 0xf800) >> 1) 307 | ((green as u32 & 0xf800) >> 6) 308 | ((blue as u32 & 0xf800) >> 11); 309 } else { 310 fb_data.pesudo_palette[regno as usize] = (red as u32 & 0xf800) 311 | ((green as u32 & 0xfc00) >> 5) 312 | ((blue as u32 & 0xf800) >> 11); 313 } 314 } 315 24 | 32 => { 316 red >>= 8; 317 green >>= 8; 318 blue >>= 8; 319 fb_data.pesudo_palette[regno as usize] = ((red as u32) << var.red.offset) 320 | ((green as u32) << var.green.offset) 321 | ((blue as u32) << var.blue.offset); 322 } 323 _ => {} 324 } 325 } 326 327 Ok(()) 328 } 329 330 fn fb_blank(&self, _blank_mode: BlankMode) -> Result<(), SystemError> { 331 todo!() 332 } 333 334 fn fb_destroy(&self) { 335 todo!() 336 } 337 338 fn fb_read(&self, buf: &mut [u8], pos: usize) -> Result<usize, SystemError> { 339 let bp = boot_params().read(); 340 341 let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?; 342 let size = self.current_fb_fix().smem_len; 343 drop(bp); 344 if pos >= size { 345 return Ok(0); 346 } 347 348 let pos = pos as i64; 349 let size = size as i64; 350 351 let len = core::cmp::min(size - pos, buf.len() as i64) as usize; 352 353 let slice = unsafe { core::slice::from_raw_parts(vaddr.as_ptr::<u8>(), size as usize) }; 354 buf[..len].copy_from_slice(&slice[pos as usize..(pos as usize + len)]); 355 356 return Ok(len); 357 } 358 359 fn fb_write(&self, buf: &[u8], pos: usize) -> Result<usize, SystemError> { 360 let bp = boot_params().read(); 361 362 let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?; 363 let size = self.current_fb_fix().smem_len; 364 365 if pos >= size { 366 return Ok(0); 367 } 368 369 let pos = pos as i64; 370 let size = size as i64; 371 372 let len = core::cmp::min(size - pos, buf.len() as i64) as usize; 373 374 let slice = unsafe { core::slice::from_raw_parts_mut(vaddr.as_ptr::<u8>(), size as usize) }; 375 slice[pos as usize..(pos as usize + len)].copy_from_slice(&buf[..len]); 376 377 return Ok(len); 378 } 379 380 fn fb_image_blit(&self, image: &super::base::FbImage) { 381 self.generic_imageblit(image); 382 } 383 384 /// ## 填充矩形 385 fn fb_fillrect(&self, rect: super::base::FillRectData) -> Result<(), SystemError> { 386 // kwarn!("rect {rect:?}"); 387 388 let boot_param = boot_params().read(); 389 let screen_base = boot_param 390 .screen_info 391 .lfb_virt_base 392 .ok_or(SystemError::ENODEV)?; 393 let fg; 394 if self.current_fb_fix().visual == FbVisual::TrueColor 395 || self.current_fb_fix().visual == FbVisual::DirectColor 396 { 397 fg = self.fb_data.read().pesudo_palette[rect.color as usize]; 398 } else { 399 fg = rect.color; 400 } 401 402 let bpp = self.current_fb_var().bits_per_pixel; 403 // 每行像素数 404 let line_offset = self.current_fb_var().xres; 405 match bpp { 406 32 => { 407 let base = screen_base.as_ptr::<u32>(); 408 409 for y in rect.dy..(rect.dy + rect.height) { 410 for x in rect.dx..(rect.dx + rect.width) { 411 unsafe { *base.add((y * line_offset + x) as usize) = fg }; 412 } 413 } 414 } 415 _ => todo!(), 416 } 417 418 Ok(()) 419 } 420 421 #[inline(never)] 422 fn fb_copyarea(&self, data: super::base::CopyAreaData) { 423 let bp = boot_params().read(); 424 let base = bp.screen_info.lfb_virt_base.unwrap(); 425 let var = self.current_fb_var(); 426 427 // 原区域或者目标区域全在屏幕外,则直接返回 428 if data.sx > var.xres as i32 429 || data.sy > var.yres as i32 430 || data.dx > var.xres as i32 431 || data.dy > var.yres as i32 432 || (data.sx + data.width as i32) < 0 433 || (data.sy + data.height as i32) < 0 434 || (data.dx + data.width as i32) < 0 435 || (data.dy + data.height as i32) < 0 436 { 437 return; 438 } 439 440 // 求两个矩形可视范围交集 441 let (s_visiable_x, s_w) = if data.sx < 0 { 442 (0, (data.width - ((-data.sx) as u32)).min(var.xres)) 443 } else { 444 let w = if data.sx as u32 + data.width > var.xres { 445 var.xres - data.sx as u32 446 } else { 447 data.width 448 }; 449 450 (data.sx, w) 451 }; 452 let (s_visiable_y, s_h) = if data.sy < 0 { 453 (0, (data.height - ((-data.sy) as u32).min(var.yres))) 454 } else { 455 let h = if data.sy as u32 + data.height > var.yres { 456 var.yres - data.sy as u32 457 } else { 458 data.height 459 }; 460 461 (data.sy, h) 462 }; 463 464 let (d_visiable_x, d_w) = if data.dx < 0 { 465 (0, (data.width - ((-data.dx) as u32)).min(var.xres)) 466 } else { 467 let w = if data.dx as u32 + data.width > var.xres { 468 var.xres - data.dx as u32 469 } else { 470 data.width 471 }; 472 473 (data.dx, w) 474 }; 475 let (d_visiable_y, d_h) = if data.dy < 0 { 476 (0, (data.height - ((-data.dy) as u32).min(var.yres))) 477 } else { 478 let h = if data.dy as u32 + data.height > var.yres { 479 var.yres - data.dy as u32 480 } else { 481 data.height 482 }; 483 484 (data.dy, h) 485 }; 486 487 // 可视范围无交集 488 if !(d_h + s_h > data.height && s_w + d_w > data.width) { 489 return; 490 } 491 492 // 可视区域左上角相对于矩形的坐标 493 let s_relative_x = s_visiable_x - data.sx; 494 let s_relative_y = s_visiable_y - data.sy; 495 let d_relative_x = d_visiable_x - data.dx; 496 let d_relative_y = d_visiable_y - data.dy; 497 498 let visiable_x = s_relative_x.max(d_relative_x); 499 let visiable_y = s_relative_y.max(d_relative_y); 500 let visiable_h = d_h + s_h - data.height; 501 let visiable_w = d_w + s_w - data.width; 502 503 let s_real_x = (visiable_x + data.sx) as u32; 504 let s_real_y = (visiable_y + data.sy) as u32; 505 let d_real_x = (visiable_x + data.dx) as u32; 506 let d_real_y = (visiable_y + data.dy) as u32; 507 508 let bytes_per_pixel = var.bits_per_pixel >> 3; 509 let bytes_per_line = var.xres * bytes_per_pixel; 510 511 let src = 512 base + VirtAddr::new((s_real_y * bytes_per_line + s_real_x * bytes_per_pixel) as usize); 513 514 let dst = 515 base + VirtAddr::new((d_real_y * bytes_per_line + d_real_x * bytes_per_pixel) as usize); 516 517 let size = (visiable_h * visiable_w) as usize; 518 519 match bytes_per_pixel { 520 4 => { 521 // 32bpp 522 let mut dst = dst.as_ptr::<u32>(); 523 let mut src = src.as_ptr::<u32>(); 524 let line_offset = var.xres as usize; 525 526 if s_real_x > d_real_x { 527 // 如果src在dst下方,则可以直接拷贝不会出现指针覆盖 528 unsafe { 529 for _ in 0..visiable_h { 530 core::ptr::copy(src, dst, visiable_w as usize); 531 src = src.add(line_offset); 532 dst = dst.add(visiable_w as usize); 533 } 534 } 535 } else { 536 let mut tmp: Vec<u32> = Vec::with_capacity(size); 537 tmp.resize(size, 0); 538 let mut tmp_ptr = tmp.as_mut_ptr(); 539 540 // 这里是一个可以优化的点,现在为了避免指针拷贝时覆盖,统一先拷贝进入buf再拷贝到dst 541 unsafe { 542 for _ in 0..visiable_h { 543 core::ptr::copy(src, tmp_ptr, visiable_w as usize); 544 src = src.add(line_offset); 545 tmp_ptr = tmp_ptr.add(visiable_w as usize); 546 } 547 548 tmp_ptr = tmp_ptr.sub(size); 549 for _ in 0..visiable_h { 550 core::ptr::copy(tmp_ptr, dst, visiable_w as usize); 551 dst = dst.add(line_offset); 552 tmp_ptr = tmp_ptr.add(visiable_w as usize); 553 } 554 } 555 } 556 } 557 _ => { 558 todo!() 559 } 560 } 561 } 562 } 563 564 impl FrameBufferInfo for VesaFb { 565 fn fb_device(&self) -> Option<Arc<FbDevice>> { 566 self.inner.lock().fb_device.clone() 567 } 568 569 fn set_fb_device(&self, device: Option<Arc<FbDevice>>) { 570 self.inner.lock().fb_device = device; 571 } 572 573 fn screen_size(&self) -> usize { 574 todo!() 575 } 576 577 fn current_fb_var(&self) -> FbVarScreenInfo { 578 VESAFB_DEFINED.read().clone() 579 } 580 581 fn current_fb_fix(&self) -> FixedScreenInfo { 582 VESAFB_FIX_INFO.read().clone() 583 } 584 585 fn video_mode(&self) -> Option<&FbVideoMode> { 586 todo!() 587 } 588 589 fn state(&self) -> FbState { 590 self.inner.lock().fb_state 591 } 592 593 fn framebuffer_info_data(&self) -> &RwLock<FrameBufferInfoData> { 594 &self.fb_data 595 } 596 } 597 598 #[derive(Debug)] 599 #[cast_to([sync] PlatformDriver)] 600 struct VesaFbDriver { 601 inner: SpinLock<InnerVesaFbDriver>, 602 kobj_state: LockedKObjectState, 603 } 604 605 impl VesaFbDriver { 606 pub fn new() -> Arc<Self> { 607 let r = Arc::new(Self { 608 inner: SpinLock::new(InnerVesaFbDriver { 609 ktype: None, 610 kset: None, 611 parent: None, 612 kernfs_inode: None, 613 devices: Vec::new(), 614 bus: None, 615 self_ref: Weak::new(), 616 }), 617 kobj_state: LockedKObjectState::new(None), 618 }); 619 620 r.inner.lock().self_ref = Arc::downgrade(&r); 621 622 return r; 623 } 624 } 625 626 #[derive(Debug)] 627 struct InnerVesaFbDriver { 628 ktype: Option<&'static dyn KObjType>, 629 kset: Option<Arc<KSet>>, 630 parent: Option<Weak<dyn KObject>>, 631 kernfs_inode: Option<Arc<KernFSInode>>, 632 devices: Vec<Arc<dyn Device>>, 633 bus: Option<Weak<dyn Bus>>, 634 635 self_ref: Weak<VesaFbDriver>, 636 } 637 638 impl VesaFbDriver { 639 const NAME: &'static str = "vesa-framebuffer"; 640 } 641 642 impl PlatformDriver for VesaFbDriver { 643 fn probe(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 644 let device = device 645 .clone() 646 .arc_any() 647 .downcast::<VesaFb>() 648 .map_err(|_| SystemError::EINVAL)?; 649 650 device.set_driver(Some(self.inner.lock_irqsave().self_ref.clone())); 651 652 return Ok(()); 653 } 654 655 fn remove(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 656 todo!() 657 } 658 659 fn shutdown(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 660 // do nothing 661 return Ok(()); 662 } 663 664 fn suspend(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 665 // do nothing 666 return Ok(()); 667 } 668 669 fn resume(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> { 670 todo!() 671 } 672 } 673 674 impl Driver for VesaFbDriver { 675 fn id_table(&self) -> Option<IdTable> { 676 Some(IdTable::new(VesaFb::NAME.to_string(), None)) 677 } 678 679 fn devices(&self) -> Vec<Arc<dyn Device>> { 680 self.inner.lock().devices.clone() 681 } 682 683 fn add_device(&self, device: Arc<dyn Device>) { 684 let mut guard = self.inner.lock(); 685 // check if the device is already in the list 686 if guard.devices.iter().any(|dev| Arc::ptr_eq(dev, &device)) { 687 return; 688 } 689 690 guard.devices.push(device); 691 } 692 693 fn delete_device(&self, device: &Arc<dyn Device>) { 694 let mut guard = self.inner.lock(); 695 guard.devices.retain(|dev| !Arc::ptr_eq(dev, device)); 696 } 697 698 fn set_bus(&self, bus: Option<Weak<dyn Bus>>) { 699 self.inner.lock().bus = bus; 700 } 701 702 fn bus(&self) -> Option<Weak<dyn Bus>> { 703 self.inner.lock().bus.clone() 704 } 705 706 fn dev_groups(&self) -> &'static [&'static dyn AttributeGroup] { 707 return &[&VesaFbAnonAttributeGroup]; 708 } 709 } 710 711 impl KObject for VesaFbDriver { 712 fn as_any_ref(&self) -> &dyn core::any::Any { 713 self 714 } 715 716 fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 717 self.inner.lock().kernfs_inode = inode; 718 } 719 720 fn inode(&self) -> Option<Arc<KernFSInode>> { 721 self.inner.lock().kernfs_inode.clone() 722 } 723 724 fn parent(&self) -> Option<Weak<dyn KObject>> { 725 self.inner.lock().parent.clone() 726 } 727 728 fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 729 self.inner.lock().parent = parent; 730 } 731 732 fn kset(&self) -> Option<Arc<KSet>> { 733 self.inner.lock().kset.clone() 734 } 735 736 fn set_kset(&self, kset: Option<Arc<KSet>>) { 737 self.inner.lock().kset = kset; 738 } 739 740 fn kobj_type(&self) -> Option<&'static dyn KObjType> { 741 self.inner.lock().ktype 742 } 743 744 fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 745 self.inner.lock().ktype = ktype; 746 } 747 748 fn name(&self) -> String { 749 Self::NAME.to_string() 750 } 751 752 fn set_name(&self, _name: String) { 753 // do nothing 754 } 755 756 fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 757 self.kobj_state.read() 758 } 759 760 fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 761 self.kobj_state.write() 762 } 763 764 fn set_kobj_state(&self, state: KObjectState) { 765 *self.kobj_state.write() = state; 766 } 767 } 768 769 #[derive(Debug)] 770 struct VesaFbAnonAttributeGroup; 771 772 impl AttributeGroup for VesaFbAnonAttributeGroup { 773 fn name(&self) -> Option<&str> { 774 None 775 } 776 777 fn attrs(&self) -> &[&'static dyn Attribute] { 778 &[&AnonAttrPhysAddr as &'static dyn Attribute] 779 } 780 781 fn is_visible( 782 &self, 783 _kobj: Arc<dyn KObject>, 784 attr: &'static dyn Attribute, 785 ) -> Option<ModeType> { 786 Some(attr.mode()) 787 } 788 } 789 790 #[derive(Debug)] 791 struct AnonAttrPhysAddr; 792 793 impl Attribute for AnonAttrPhysAddr { 794 fn name(&self) -> &str { 795 "smem_start" 796 } 797 798 fn mode(&self) -> ModeType { 799 ModeType::S_IRUGO 800 } 801 802 fn support(&self) -> SysFSOpsSupport { 803 SysFSOpsSupport::ATTR_SHOW 804 } 805 806 fn show(&self, _kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 807 sysfs_emit_str( 808 buf, 809 format!( 810 "0x{:x}\n", 811 VESAFB_FIX_INFO 812 .read() 813 .smem_start 814 .unwrap_or(PhysAddr::new(0)) 815 .data() 816 ) 817 .as_str(), 818 ) 819 } 820 } 821 822 #[unified_init(INITCALL_DEVICE)] 823 pub fn vesa_fb_driver_init() -> Result<(), SystemError> { 824 let driver = VesaFbDriver::new(); 825 826 platform_driver_manager().register(driver)?; 827 828 return Ok(()); 829 } 830 831 /// 在内存管理初始化之前,初始化vesafb 832 pub fn vesafb_early_init() -> Result<VirtAddr, SystemError> { 833 let mut _reserved: u32 = 0; 834 835 let mut fb_info: MaybeUninit<multiboot_tag_framebuffer_info_t> = MaybeUninit::uninit(); 836 //从multiboot2中读取帧缓冲区信息至fb_info 837 838 // todo: 换成rust的,并且检测是否成功获取 839 unsafe { 840 multiboot2_iter( 841 Some(multiboot2_get_Framebuffer_info), 842 fb_info.as_mut_ptr() as usize as *mut c_void, 843 &mut _reserved as *mut c_uint, 844 ) 845 }; 846 unsafe { fb_info.assume_init() }; 847 let fb_info: multiboot_tag_framebuffer_info_t = unsafe { core::mem::transmute(fb_info) }; 848 849 // todo: 判断是否有vesa帧缓冲区,这里暂时直接设置true 850 HAS_VESA_FB.store(true, core::sync::atomic::Ordering::SeqCst); 851 852 let width = fb_info.framebuffer_width; 853 let height = fb_info.framebuffer_height; 854 855 let mut boot_params_guard = boot_params().write(); 856 let boottime_screen_info = &mut boot_params_guard.screen_info; 857 858 boottime_screen_info.is_vga = true; 859 860 boottime_screen_info.lfb_base = PhysAddr::new(fb_info.framebuffer_addr as usize); 861 862 if fb_info.framebuffer_type == 2 { 863 //当type=2时,width与height用字符数表示,故depth=8 864 boottime_screen_info.origin_video_cols = width as u8; 865 boottime_screen_info.origin_video_lines = height as u8; 866 boottime_screen_info.video_type = BootTimeVideoType::Mda; 867 boottime_screen_info.lfb_depth = 8; 868 } else { 869 //否则为图像模式,depth应参照帧缓冲区信息里面的每个像素的位数 870 boottime_screen_info.lfb_width = width; 871 boottime_screen_info.lfb_height = height; 872 boottime_screen_info.video_type = BootTimeVideoType::Vlfb; 873 boottime_screen_info.lfb_depth = fb_info.framebuffer_bpp as u8; 874 } 875 876 boottime_screen_info.lfb_size = 877 (width * height * ((fb_info.framebuffer_bpp as u32 + 7) / 8)) as usize; 878 879 // let buf_vaddr = VirtAddr::new(0xffff800003200000); 880 let buf_vaddr = VirtAddr::new( 881 crate::include::bindings::bindings::SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE as usize 882 + FRAME_BUFFER_MAPPING_OFFSET as usize, 883 ); 884 boottime_screen_info.lfb_virt_base = Some(buf_vaddr); 885 886 let init_text = "Video driver to map.\n\0"; 887 send_to_default_serial8250_port(init_text.as_bytes()); 888 889 // 地址映射 890 let paddr = PhysAddr::new(fb_info.framebuffer_addr as usize); 891 let count = 892 PageFrameCount::new(page_align_up(boottime_screen_info.lfb_size) / MMArch::PAGE_SIZE); 893 unsafe { pseudo_map_phys(buf_vaddr, paddr, count) }; 894 return Ok(buf_vaddr); 895 } 896 897 #[unified_init(INITCALL_DEVICE)] 898 fn vesa_fb_device_init() -> Result<(), SystemError> { 899 // 如果没有vesa帧缓冲区,直接返回 900 if !HAS_VESA_FB.load(core::sync::atomic::Ordering::SeqCst) { 901 return Ok(()); 902 } 903 904 static INIT: Once = Once::new(); 905 INIT.call_once(|| { 906 kinfo!("vesa fb device init"); 907 let device = Arc::new(VesaFb::new()); 908 909 let mut fb_fix = VESAFB_FIX_INFO.write_irqsave(); 910 let mut fb_var = VESAFB_DEFINED.write_irqsave(); 911 912 let boot_params_guard = boot_params().read(); 913 let boottime_screen_info = &boot_params_guard.screen_info; 914 915 fb_fix.smem_start = Some(boottime_screen_info.lfb_base); 916 fb_fix.smem_len = boottime_screen_info.lfb_size; 917 918 if boottime_screen_info.video_type == BootTimeVideoType::Mda { 919 fb_fix.visual = FbVisual::Mono10; 920 fb_var.bits_per_pixel = 8; 921 fb_fix.line_length = 922 (boottime_screen_info.origin_video_cols as u32) * (fb_var.bits_per_pixel / 8); 923 fb_var.xres_virtual = boottime_screen_info.origin_video_cols as u32; 924 fb_var.yres_virtual = boottime_screen_info.origin_video_lines as u32; 925 } else { 926 fb_fix.visual = FbVisual::TrueColor; 927 fb_var.bits_per_pixel = boottime_screen_info.lfb_depth as u32; 928 fb_fix.line_length = 929 (boottime_screen_info.lfb_width as u32) * (fb_var.bits_per_pixel / 8); 930 fb_var.xres_virtual = boottime_screen_info.lfb_width as u32; 931 fb_var.yres_virtual = boottime_screen_info.lfb_height as u32; 932 fb_var.xres = boottime_screen_info.lfb_width as u32; 933 fb_var.yres = boottime_screen_info.lfb_height as u32; 934 } 935 936 fb_var.red.length = boottime_screen_info.red_size as u32; 937 fb_var.green.length = boottime_screen_info.green_size as u32; 938 fb_var.blue.length = boottime_screen_info.blue_size as u32; 939 940 fb_var.red.offset = boottime_screen_info.red_pos as u32; 941 fb_var.green.offset = boottime_screen_info.green_pos as u32; 942 fb_var.blue.offset = boottime_screen_info.blue_pos as u32; 943 944 // TODO: 这里是暂时这样写的,初始化为RGB888格式,后续vesa初始化完善后删掉下面 945 fb_var.red.offset = 16; 946 fb_var.green.offset = 8; 947 fb_var.blue.offset = 0; 948 949 if fb_var.bits_per_pixel >= 1 && fb_var.bits_per_pixel <= 8 { 950 fb_var.red.length = fb_var.bits_per_pixel; 951 fb_var.green.length = fb_var.bits_per_pixel; 952 fb_var.blue.length = fb_var.bits_per_pixel; 953 } 954 955 device_manager().device_default_initialize(&(device.clone() as Arc<dyn Device>)); 956 957 platform_device_manager() 958 .device_add(device.clone() as Arc<dyn PlatformDevice>) 959 .expect("vesa_fb_device_init: platform_device_manager().device_add failed"); 960 961 frame_buffer_manager() 962 .register_fb(device.clone() as Arc<dyn FrameBuffer>) 963 .expect("vesa_fb_device_init: frame_buffer_manager().register_fb failed"); 964 965 // 加入全局fb表 966 let mut guard = FRAME_BUFFER_SET.write(); 967 if guard.get(device.fb_id().data() as usize).unwrap().is_some() { 968 kwarn!( 969 "vesa_fb_device_init: There is already an element {:?} in the FRAME_BUFFER_SET", 970 device.fb_id() 971 ); 972 } 973 guard[device.fb_id().data() as usize] = Some(device.clone()); 974 975 // 设置vesa fb的状态为运行中 976 device.inner.lock().fb_state = FbState::Running; 977 }); 978 979 return Ok(()); 980 } 981