178bf93f0SYJwu2023 use core::{ 21496ba7bSLoGin ffi::{c_uint, c_void}, 31496ba7bSLoGin mem::MaybeUninit, 478bf93f0SYJwu2023 sync::atomic::{AtomicBool, Ordering}, 578bf93f0SYJwu2023 }; 6bacd691cSlogin 71496ba7bSLoGin use alloc::{boxed::Box, sync::Arc}; 8bacd691cSlogin 9bacd691cSlogin use crate::{ 101496ba7bSLoGin arch::MMArch, 11*a03c4f9dSLoGin driver::tty::serial::serial8250::send_to_default_serial8250_port, 121496ba7bSLoGin include::bindings::bindings::{ 131496ba7bSLoGin multiboot2_get_Framebuffer_info, multiboot2_iter, multiboot_tag_framebuffer_info_t, 141496ba7bSLoGin FRAME_BUFFER_MAPPING_OFFSET, SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE, 151496ba7bSLoGin }, 161496ba7bSLoGin kinfo, 171496ba7bSLoGin libs::{ 181496ba7bSLoGin align::page_align_up, 191496ba7bSLoGin lib_ui::screen_manager::{ScmBuffer, ScmBufferFlag, ScmBufferInfo}, 201496ba7bSLoGin rwlock::{RwLock, RwLockReadGuard}, 211496ba7bSLoGin spinlock::SpinLock, 221496ba7bSLoGin }, 231496ba7bSLoGin mm::{ 241496ba7bSLoGin allocator::page_frame::PageFrameCount, kernel_mapper::KernelMapper, 251496ba7bSLoGin no_init::pseudo_map_phys, page::PageFlags, MemoryManagementArch, PhysAddr, VirtAddr, 261496ba7bSLoGin }, 271496ba7bSLoGin syscall::SystemError, 281496ba7bSLoGin time::timer::{Timer, TimerFunction}, 29bacd691cSlogin }; 30bacd691cSlogin 311496ba7bSLoGin static mut __MAMAGER: Option<VideoRefreshManager> = None; 321496ba7bSLoGin 331496ba7bSLoGin pub fn video_refresh_manager() -> &'static VideoRefreshManager { 341496ba7bSLoGin return unsafe { 351496ba7bSLoGin &__MAMAGER 361496ba7bSLoGin .as_ref() 371496ba7bSLoGin .expect("Video refresh manager has not been initialized yet!") 381496ba7bSLoGin }; 391496ba7bSLoGin } 401496ba7bSLoGin 411496ba7bSLoGin ///管理显示刷新变量的结构体 421496ba7bSLoGin pub struct VideoRefreshManager { 431496ba7bSLoGin device_buffer: RwLock<ScmBufferInfo>, 441496ba7bSLoGin fb_info: multiboot_tag_framebuffer_info_t, 451496ba7bSLoGin refresh_target: RwLock<Option<Arc<SpinLock<Box<[u32]>>>>>, 4678bf93f0SYJwu2023 running: AtomicBool, 47bacd691cSlogin } 48bacd691cSlogin 491496ba7bSLoGin const REFRESH_INTERVAL: u64 = 30; 501496ba7bSLoGin 511496ba7bSLoGin impl VideoRefreshManager { 521496ba7bSLoGin /** 531496ba7bSLoGin * @brief 启动定时刷新 541496ba7bSLoGin * @return 启动成功: true, 失败: false 551496ba7bSLoGin */ 561496ba7bSLoGin pub fn run_video_refresh(&self) -> bool { 571496ba7bSLoGin //设置Manager运行标志 581496ba7bSLoGin let res = self.set_run(); 591496ba7bSLoGin 601496ba7bSLoGin //设置成功则开始任务,否则直接返回false 611496ba7bSLoGin if res { 621496ba7bSLoGin //第一次将expire_jiffies设置小一点,使得这次刷新尽快开始,后续的刷新将按照REFRESH_INTERVAL间隔进行 631496ba7bSLoGin let timer = Timer::new(VideoRefreshExecutor::new(), 1); 641496ba7bSLoGin //将新一次定时任务加入队列 651496ba7bSLoGin timer.activate(); 661496ba7bSLoGin } 671496ba7bSLoGin return res; 68bacd691cSlogin } 69bacd691cSlogin 701496ba7bSLoGin /// 停止定时刷新 711496ba7bSLoGin #[allow(dead_code)] 721496ba7bSLoGin pub fn stop_video_refresh(&self) { 731496ba7bSLoGin self.running.store(false, Ordering::SeqCst); 74bacd691cSlogin } 75bacd691cSlogin 76bacd691cSlogin fn set_run(&self) -> bool { 771496ba7bSLoGin let res = self 78bacd691cSlogin .running 791496ba7bSLoGin .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst); 801496ba7bSLoGin if res.is_ok() { 81bacd691cSlogin return true; 82bacd691cSlogin } else { 83bacd691cSlogin return false; 84bacd691cSlogin } 85bacd691cSlogin } 86bacd691cSlogin 871496ba7bSLoGin /** 881496ba7bSLoGin * @brief VBE帧缓存区的地址重新映射 891496ba7bSLoGin * 将帧缓存区映射到地址SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE处 901496ba7bSLoGin */ 911496ba7bSLoGin fn init_frame_buffer(&self) { 921496ba7bSLoGin kinfo!("Re-mapping VBE frame buffer..."); 931496ba7bSLoGin let buf_vaddr = VirtAddr::new( 941496ba7bSLoGin SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE as usize + FRAME_BUFFER_MAPPING_OFFSET as usize, 951496ba7bSLoGin ); 961496ba7bSLoGin 971496ba7bSLoGin let mut frame_buffer_info_graud = self.device_buffer.write(); 981496ba7bSLoGin if let ScmBuffer::DeviceBuffer(vaddr) = &mut (frame_buffer_info_graud).buf { 991496ba7bSLoGin *vaddr = buf_vaddr; 1001496ba7bSLoGin } 1011496ba7bSLoGin 1021496ba7bSLoGin // 地址映射 1031496ba7bSLoGin let mut paddr = PhysAddr::new(self.fb_info.framebuffer_addr as usize); 1041496ba7bSLoGin let count = PageFrameCount::new( 1051496ba7bSLoGin page_align_up(frame_buffer_info_graud.buf_size()) / MMArch::PAGE_SIZE, 1061496ba7bSLoGin ); 1071496ba7bSLoGin let page_flags: PageFlags<MMArch> = PageFlags::new().set_execute(true).set_write(true); 1081496ba7bSLoGin 1091496ba7bSLoGin let mut kernel_mapper = KernelMapper::lock(); 1101496ba7bSLoGin let mut kernel_mapper = kernel_mapper.as_mut(); 1111496ba7bSLoGin assert!(kernel_mapper.is_some()); 1121496ba7bSLoGin let mut vaddr = buf_vaddr; 1131496ba7bSLoGin unsafe { 1141496ba7bSLoGin for _ in 0..count.data() { 1151496ba7bSLoGin let flusher = kernel_mapper 1161496ba7bSLoGin .as_mut() 1171496ba7bSLoGin .unwrap() 1181496ba7bSLoGin .map_phys(vaddr, paddr, page_flags) 1191496ba7bSLoGin .unwrap(); 1201496ba7bSLoGin 1211496ba7bSLoGin flusher.flush(); 1221496ba7bSLoGin vaddr += MMArch::PAGE_SIZE; 1231496ba7bSLoGin paddr += MMArch::PAGE_SIZE; 124bacd691cSlogin } 125bacd691cSlogin } 126bacd691cSlogin 1271496ba7bSLoGin kinfo!("VBE frame buffer successfully Re-mapped!"); 128bacd691cSlogin } 1291496ba7bSLoGin 1301496ba7bSLoGin /** 1311496ba7bSLoGin * @brief 初始化显示模块,需先低级初始化才能高级初始化 1321496ba7bSLoGin * @param level 初始化等级 1331496ba7bSLoGin * false -> 低级初始化:不使用double buffer 1341496ba7bSLoGin * true ->高级初始化:增加double buffer的支持 1351496ba7bSLoGin * @return int 1361496ba7bSLoGin */ 1371496ba7bSLoGin pub fn video_reinitialize(&self, level: bool) -> Result<(), SystemError> { 1381496ba7bSLoGin if !level { 1391496ba7bSLoGin self.init_frame_buffer(); 1401496ba7bSLoGin } else { 1411496ba7bSLoGin // 开启屏幕计时刷新 1421496ba7bSLoGin assert!(self.run_video_refresh()); 1431496ba7bSLoGin } 1441496ba7bSLoGin return Ok(()); 1451496ba7bSLoGin } 1461496ba7bSLoGin 1471496ba7bSLoGin /** 1481496ba7bSLoGin * @brief 设置帧缓冲区刷新目标 1491496ba7bSLoGin * 1501496ba7bSLoGin * @param buf 1511496ba7bSLoGin * @return int 1521496ba7bSLoGin */ 1531496ba7bSLoGin pub fn set_refresh_target(&self, buf_info: &ScmBufferInfo) -> Result<(), SystemError> { 1541496ba7bSLoGin let mut refresh_target = self.refresh_target.write_irqsave(); 1551496ba7bSLoGin if let ScmBuffer::DoubleBuffer(double_buffer) = &buf_info.buf { 1561496ba7bSLoGin *refresh_target = Some(double_buffer.clone()); 1571496ba7bSLoGin return Ok(()); 1581496ba7bSLoGin } 1591496ba7bSLoGin return Err(SystemError::EINVAL); 1601496ba7bSLoGin } 1611496ba7bSLoGin 1621496ba7bSLoGin #[allow(dead_code)] 1631496ba7bSLoGin pub fn refresh_target(&self) -> RwLockReadGuard<'_, Option<Arc<SpinLock<Box<[u32]>>>>> { 1641496ba7bSLoGin let x = self.refresh_target.read(); 1651496ba7bSLoGin 1661496ba7bSLoGin return x; 1671496ba7bSLoGin } 1681496ba7bSLoGin 1691496ba7bSLoGin pub fn device_buffer(&self) -> RwLockReadGuard<'_, ScmBufferInfo> { 1701496ba7bSLoGin return self.device_buffer.read(); 1711496ba7bSLoGin } 1721496ba7bSLoGin 1731496ba7bSLoGin /** 1741496ba7bSLoGin * @brief 初始化显示驱动 1751496ba7bSLoGin * 1761496ba7bSLoGin * @return int 1771496ba7bSLoGin */ 1781496ba7bSLoGin pub unsafe fn video_init() -> Result<(), SystemError> { 1791496ba7bSLoGin static INIT: AtomicBool = AtomicBool::new(false); 1801496ba7bSLoGin 1811496ba7bSLoGin if INIT 1821496ba7bSLoGin .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst) 1831496ba7bSLoGin .is_err() 1841496ba7bSLoGin { 1851496ba7bSLoGin panic!("Try to init video twice!"); 1861496ba7bSLoGin } 1871496ba7bSLoGin 1881496ba7bSLoGin let mut _reserved: u32 = 0; 1891496ba7bSLoGin 1901496ba7bSLoGin let mut fb_info: MaybeUninit<multiboot_tag_framebuffer_info_t> = MaybeUninit::uninit(); 1911496ba7bSLoGin //从multiboot2中读取帧缓冲区信息至fb_info 1921496ba7bSLoGin multiboot2_iter( 1931496ba7bSLoGin Some(multiboot2_get_Framebuffer_info), 1941496ba7bSLoGin fb_info.as_mut_ptr() as usize as *mut c_void, 1951496ba7bSLoGin &mut _reserved as *mut c_uint, 1961496ba7bSLoGin ); 1971496ba7bSLoGin fb_info.assume_init(); 1981496ba7bSLoGin let fb_info: multiboot_tag_framebuffer_info_t = core::mem::transmute(fb_info); 1991496ba7bSLoGin 2001496ba7bSLoGin let width = fb_info.framebuffer_width; 2011496ba7bSLoGin let height = fb_info.framebuffer_height; 2021496ba7bSLoGin 2031496ba7bSLoGin //初始化帧缓冲区信息结构体 2041496ba7bSLoGin let (bit_depth, flags) = if fb_info.framebuffer_type == 2 { 2051496ba7bSLoGin //当type=2时,width与height用字符数表示,故depth=8 2061496ba7bSLoGin 2071496ba7bSLoGin (8u32, ScmBufferFlag::SCM_BF_TEXT | ScmBufferFlag::SCM_BF_FB) 2081496ba7bSLoGin } else { 2091496ba7bSLoGin //否则为图像模式,depth应参照帧缓冲区信息里面的每个像素的位数 2101496ba7bSLoGin ( 2111496ba7bSLoGin fb_info.framebuffer_bpp as u32, 2121496ba7bSLoGin ScmBufferFlag::SCM_BF_PIXEL | ScmBufferFlag::SCM_BF_FB, 2131496ba7bSLoGin ) 2141496ba7bSLoGin }; 2151496ba7bSLoGin 2161496ba7bSLoGin let buf_vaddr = VirtAddr::new(0xffff800003200000); 2171496ba7bSLoGin let device_buffer = ScmBufferInfo::new_device_buffer( 2181496ba7bSLoGin width, 2191496ba7bSLoGin height, 2201496ba7bSLoGin width * height * ((bit_depth + 7) / 8), 2211496ba7bSLoGin bit_depth, 2221496ba7bSLoGin flags, 2231496ba7bSLoGin buf_vaddr, 2241496ba7bSLoGin ) 2251496ba7bSLoGin .unwrap(); 2261496ba7bSLoGin 2271496ba7bSLoGin let init_text = "Video driver to map.\n\0"; 228*a03c4f9dSLoGin send_to_default_serial8250_port(init_text.as_bytes()); 2291496ba7bSLoGin 2301496ba7bSLoGin //地址映射 2311496ba7bSLoGin let paddr = PhysAddr::new(fb_info.framebuffer_addr as usize); 2321496ba7bSLoGin let count = PageFrameCount::new( 2331496ba7bSLoGin page_align_up(device_buffer.buf_size() as usize) / MMArch::PAGE_SIZE, 2341496ba7bSLoGin ); 2351496ba7bSLoGin pseudo_map_phys(buf_vaddr, paddr, count); 2361496ba7bSLoGin 2371496ba7bSLoGin let result = Self { 2381496ba7bSLoGin fb_info, 2391496ba7bSLoGin device_buffer: RwLock::new(device_buffer), 2401496ba7bSLoGin refresh_target: RwLock::new(None), 2411496ba7bSLoGin running: AtomicBool::new(false), 2421496ba7bSLoGin }; 2431496ba7bSLoGin 2441496ba7bSLoGin __MAMAGER = Some(result); 2451496ba7bSLoGin 2461496ba7bSLoGin let init_text = "Video driver initialized.\n\0"; 247*a03c4f9dSLoGin send_to_default_serial8250_port(init_text.as_bytes()); 2481496ba7bSLoGin return Ok(()); 2491496ba7bSLoGin } 2501496ba7bSLoGin } 2511496ba7bSLoGin 2521496ba7bSLoGin //刷新任务执行器 2531496ba7bSLoGin #[derive(Debug)] 2541496ba7bSLoGin struct VideoRefreshExecutor; 2551496ba7bSLoGin 2561496ba7bSLoGin impl VideoRefreshExecutor { 2571496ba7bSLoGin fn new() -> Box<VideoRefreshExecutor> { 2581496ba7bSLoGin return Box::new(VideoRefreshExecutor); 2591496ba7bSLoGin } 2601496ba7bSLoGin } 2611496ba7bSLoGin 2621496ba7bSLoGin impl TimerFunction for VideoRefreshExecutor { 2631496ba7bSLoGin /** 2641496ba7bSLoGin * @brief 交给定时器执行的任务,此方法不应手动调用 2651496ba7bSLoGin * @return Ok(()) 2661496ba7bSLoGin */ 2671496ba7bSLoGin fn run(&mut self) -> Result<(), SystemError> { 2681496ba7bSLoGin // 获得Manager 2691496ba7bSLoGin let manager = video_refresh_manager(); 2701496ba7bSLoGin 2711496ba7bSLoGin let start_next_refresh = || { 2721496ba7bSLoGin //判断是否还需要刷新,若需要则继续分配下一次计时任务,否则不分配 2731496ba7bSLoGin if manager.running.load(Ordering::SeqCst) { 2741496ba7bSLoGin let timer = Timer::new(VideoRefreshExecutor::new(), REFRESH_INTERVAL); 2751496ba7bSLoGin //将新一次定时任务加入队列 2761496ba7bSLoGin timer.activate(); 2771496ba7bSLoGin } 2781496ba7bSLoGin }; 2791496ba7bSLoGin 2801496ba7bSLoGin let mut refresh_target: Option<RwLockReadGuard<'_, Option<Arc<SpinLock<Box<[u32]>>>>>> = 2811496ba7bSLoGin None; 2821496ba7bSLoGin const TRY_TIMES: i32 = 2; 2831496ba7bSLoGin for i in 0..TRY_TIMES { 2841496ba7bSLoGin let g = manager.refresh_target.try_read(); 2851496ba7bSLoGin if g.is_none() { 2861496ba7bSLoGin if i == TRY_TIMES - 1 { 2871496ba7bSLoGin start_next_refresh(); 2881496ba7bSLoGin return Ok(()); 2891496ba7bSLoGin } 2901496ba7bSLoGin continue; 2911496ba7bSLoGin } 2921496ba7bSLoGin refresh_target = Some(g.unwrap()); 2931496ba7bSLoGin break; 2941496ba7bSLoGin } 2951496ba7bSLoGin 2961496ba7bSLoGin let refresh_target = refresh_target.unwrap(); 2971496ba7bSLoGin 2981496ba7bSLoGin if let ScmBuffer::DeviceBuffer(vaddr) = manager.device_buffer().buf { 2991496ba7bSLoGin let p = vaddr.as_ptr() as *mut u8; 3001496ba7bSLoGin let mut target_guard = None; 3011496ba7bSLoGin for _ in 0..2 { 3021496ba7bSLoGin if let Ok(guard) = refresh_target.as_ref().unwrap().try_lock_irqsave() { 3031496ba7bSLoGin target_guard = Some(guard); 3041496ba7bSLoGin break; 3051496ba7bSLoGin } 3061496ba7bSLoGin } 3071496ba7bSLoGin if target_guard.is_none() { 3081496ba7bSLoGin start_next_refresh(); 3091496ba7bSLoGin return Ok(()); 3101496ba7bSLoGin } 3111496ba7bSLoGin let mut target_guard = target_guard.unwrap(); 3121496ba7bSLoGin unsafe { 3131496ba7bSLoGin p.copy_from_nonoverlapping( 3141496ba7bSLoGin target_guard.as_mut_ptr() as *mut u8, 3151496ba7bSLoGin manager.device_buffer().buf_size() as usize, 3161496ba7bSLoGin ) 3171496ba7bSLoGin } 3181496ba7bSLoGin } 3191496ba7bSLoGin 3201496ba7bSLoGin start_next_refresh(); 3211496ba7bSLoGin 3221496ba7bSLoGin return Ok(()); 3231496ba7bSLoGin } 3241496ba7bSLoGin } 3251496ba7bSLoGin 326bacd691cSlogin #[no_mangle] 3271496ba7bSLoGin pub unsafe extern "C" fn rs_video_init() -> i32 { 3281496ba7bSLoGin return VideoRefreshManager::video_init() 3291496ba7bSLoGin .map(|_| 0) 3301496ba7bSLoGin .unwrap_or_else(|e| e.to_posix_errno()); 331bacd691cSlogin } 332