1c566df45SLoGin use core::sync::atomic::{AtomicBool, Ordering}; 2bacd691cSlogin 3bacd691cSlogin use crate::{ 41496ba7bSLoGin arch::MMArch, 5c566df45SLoGin init::boot_params, 61496ba7bSLoGin kinfo, 71496ba7bSLoGin libs::{ 81496ba7bSLoGin align::page_align_up, 91496ba7bSLoGin lib_ui::screen_manager::{ScmBuffer, ScmBufferFlag, ScmBufferInfo}, 101496ba7bSLoGin rwlock::{RwLock, RwLockReadGuard}, 111496ba7bSLoGin spinlock::SpinLock, 121496ba7bSLoGin }, 131496ba7bSLoGin mm::{ 14c566df45SLoGin allocator::page_frame::PageFrameCount, kernel_mapper::KernelMapper, page::PageFlags, 15c75ef4e2SLoGin MemoryManagementArch, 161496ba7bSLoGin }, 171496ba7bSLoGin time::timer::{Timer, TimerFunction}, 18bacd691cSlogin }; 1991e9d4abSLoGin use alloc::{boxed::Box, sync::Arc}; 2091e9d4abSLoGin use system_error::SystemError; 21bacd691cSlogin 2208a2ee40SLoGin pub mod fbdev; 2308a2ee40SLoGin 241496ba7bSLoGin static mut __MAMAGER: Option<VideoRefreshManager> = None; 251496ba7bSLoGin 261496ba7bSLoGin pub fn video_refresh_manager() -> &'static VideoRefreshManager { 271496ba7bSLoGin return unsafe { 28b5b571e0SLoGin __MAMAGER 291496ba7bSLoGin .as_ref() 301496ba7bSLoGin .expect("Video refresh manager has not been initialized yet!") 311496ba7bSLoGin }; 321496ba7bSLoGin } 33b5b571e0SLoGin #[allow(clippy::type_complexity)] 341496ba7bSLoGin ///管理显示刷新变量的结构体 351496ba7bSLoGin pub struct VideoRefreshManager { 361496ba7bSLoGin device_buffer: RwLock<ScmBufferInfo>, 37*2755467cS曾俊 refresh_target: RwLock<Option<Arc<SpinLock<Box<[u8]>>>>>, 3878bf93f0SYJwu2023 running: AtomicBool, 39bacd691cSlogin } 40bacd691cSlogin 411496ba7bSLoGin const REFRESH_INTERVAL: u64 = 30; 421496ba7bSLoGin 431496ba7bSLoGin impl VideoRefreshManager { 441496ba7bSLoGin /** 451496ba7bSLoGin * @brief 启动定时刷新 461496ba7bSLoGin * @return 启动成功: true, 失败: false 471496ba7bSLoGin */ 481496ba7bSLoGin pub fn run_video_refresh(&self) -> bool { 491496ba7bSLoGin //设置Manager运行标志 501496ba7bSLoGin let res = self.set_run(); 511496ba7bSLoGin 521496ba7bSLoGin //设置成功则开始任务,否则直接返回false 531496ba7bSLoGin if res { 541496ba7bSLoGin //第一次将expire_jiffies设置小一点,使得这次刷新尽快开始,后续的刷新将按照REFRESH_INTERVAL间隔进行 551496ba7bSLoGin let timer = Timer::new(VideoRefreshExecutor::new(), 1); 561496ba7bSLoGin //将新一次定时任务加入队列 571496ba7bSLoGin timer.activate(); 581496ba7bSLoGin } 591496ba7bSLoGin return res; 60bacd691cSlogin } 61bacd691cSlogin 621496ba7bSLoGin /// 停止定时刷新 631496ba7bSLoGin #[allow(dead_code)] 641496ba7bSLoGin pub fn stop_video_refresh(&self) { 651496ba7bSLoGin self.running.store(false, Ordering::SeqCst); 66bacd691cSlogin } 67bacd691cSlogin 68bacd691cSlogin fn set_run(&self) -> bool { 691496ba7bSLoGin let res = self 70bacd691cSlogin .running 711496ba7bSLoGin .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst); 72b5b571e0SLoGin return res.is_ok(); 73bacd691cSlogin } 74bacd691cSlogin 751496ba7bSLoGin /** 761496ba7bSLoGin * @brief VBE帧缓存区的地址重新映射 771496ba7bSLoGin * 将帧缓存区映射到地址SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE处 781496ba7bSLoGin */ 791496ba7bSLoGin fn init_frame_buffer(&self) { 801496ba7bSLoGin kinfo!("Re-mapping VBE frame buffer..."); 81c75ef4e2SLoGin let buf_vaddr = boot_params() 82c75ef4e2SLoGin .read_irqsave() 83c75ef4e2SLoGin .screen_info 84c75ef4e2SLoGin .lfb_virt_base 85c75ef4e2SLoGin .unwrap(); 861496ba7bSLoGin 8702343d0bSLoGin let mut frame_buffer_info_guard = self.device_buffer.write(); 8802343d0bSLoGin if let ScmBuffer::DeviceBuffer(vaddr) = &mut (frame_buffer_info_guard).buf { 891496ba7bSLoGin *vaddr = buf_vaddr; 901496ba7bSLoGin } 911496ba7bSLoGin 921496ba7bSLoGin // 地址映射 93c566df45SLoGin let mut paddr = boot_params().read().screen_info.lfb_base; 941496ba7bSLoGin let count = PageFrameCount::new( 9502343d0bSLoGin page_align_up(frame_buffer_info_guard.buf_size()) / MMArch::PAGE_SIZE, 961496ba7bSLoGin ); 971496ba7bSLoGin let page_flags: PageFlags<MMArch> = PageFlags::new().set_execute(true).set_write(true); 981496ba7bSLoGin 991496ba7bSLoGin let mut kernel_mapper = KernelMapper::lock(); 1001496ba7bSLoGin let mut kernel_mapper = kernel_mapper.as_mut(); 1011496ba7bSLoGin assert!(kernel_mapper.is_some()); 1021496ba7bSLoGin let mut vaddr = buf_vaddr; 1031496ba7bSLoGin unsafe { 1041496ba7bSLoGin for _ in 0..count.data() { 1051496ba7bSLoGin let flusher = kernel_mapper 1061496ba7bSLoGin .as_mut() 1071496ba7bSLoGin .unwrap() 1081496ba7bSLoGin .map_phys(vaddr, paddr, page_flags) 1091496ba7bSLoGin .unwrap(); 1101496ba7bSLoGin 1111496ba7bSLoGin flusher.flush(); 1121496ba7bSLoGin vaddr += MMArch::PAGE_SIZE; 1131496ba7bSLoGin paddr += MMArch::PAGE_SIZE; 114bacd691cSlogin } 115bacd691cSlogin } 116bacd691cSlogin 1171496ba7bSLoGin kinfo!("VBE frame buffer successfully Re-mapped!"); 118bacd691cSlogin } 1191496ba7bSLoGin 1201496ba7bSLoGin /** 1211496ba7bSLoGin * @brief 初始化显示模块,需先低级初始化才能高级初始化 1221496ba7bSLoGin * @param level 初始化等级 1231496ba7bSLoGin * false -> 低级初始化:不使用double buffer 1241496ba7bSLoGin * true ->高级初始化:增加double buffer的支持 1251496ba7bSLoGin * @return int 1261496ba7bSLoGin */ 1271496ba7bSLoGin pub fn video_reinitialize(&self, level: bool) -> Result<(), SystemError> { 1281496ba7bSLoGin if !level { 1291496ba7bSLoGin self.init_frame_buffer(); 1301496ba7bSLoGin } else { 1311496ba7bSLoGin // 开启屏幕计时刷新 1321496ba7bSLoGin assert!(self.run_video_refresh()); 1331496ba7bSLoGin } 1341496ba7bSLoGin return Ok(()); 1351496ba7bSLoGin } 1361496ba7bSLoGin 1371496ba7bSLoGin /** 1381496ba7bSLoGin * @brief 设置帧缓冲区刷新目标 1391496ba7bSLoGin * 1401496ba7bSLoGin * @param buf 1411496ba7bSLoGin * @return int 1421496ba7bSLoGin */ 1431496ba7bSLoGin pub fn set_refresh_target(&self, buf_info: &ScmBufferInfo) -> Result<(), SystemError> { 1441496ba7bSLoGin let mut refresh_target = self.refresh_target.write_irqsave(); 1451496ba7bSLoGin if let ScmBuffer::DoubleBuffer(double_buffer) = &buf_info.buf { 1461496ba7bSLoGin *refresh_target = Some(double_buffer.clone()); 1471496ba7bSLoGin return Ok(()); 1481496ba7bSLoGin } 1491496ba7bSLoGin return Err(SystemError::EINVAL); 1501496ba7bSLoGin } 151b5b571e0SLoGin #[allow(clippy::type_complexity)] 1521496ba7bSLoGin #[allow(dead_code)] 153*2755467cS曾俊 pub fn refresh_target(&self) -> RwLockReadGuard<'_, Option<Arc<SpinLock<Box<[u8]>>>>> { 1541496ba7bSLoGin let x = self.refresh_target.read(); 1551496ba7bSLoGin 1561496ba7bSLoGin return x; 1571496ba7bSLoGin } 1581496ba7bSLoGin 1591496ba7bSLoGin pub fn device_buffer(&self) -> RwLockReadGuard<'_, ScmBufferInfo> { 1601496ba7bSLoGin return self.device_buffer.read(); 1611496ba7bSLoGin } 1621496ba7bSLoGin 1631a72a751SLoGin /// 在riscv64平台下暂时不支持 1641a72a751SLoGin #[cfg(target_arch = "riscv64")] 1651a72a751SLoGin pub unsafe fn video_init() -> Result<(), SystemError> { 1661a72a751SLoGin return Err(SystemError::ENOSYS); 1671a72a751SLoGin } 1681a72a751SLoGin 1691a72a751SLoGin /// 此函数用于初始化显示驱动,为后续的图形输出做好准备。 170c566df45SLoGin #[cfg(target_arch = "x86_64")] 1711496ba7bSLoGin pub unsafe fn video_init() -> Result<(), SystemError> { 172c566df45SLoGin use crate::{ 173c566df45SLoGin arch::driver::video::arch_video_early_init, 17452da9a59SGnoCiYeH driver::{ 17552da9a59SGnoCiYeH serial::serial8250::send_to_default_serial8250_port, 17652da9a59SGnoCiYeH video::fbdev::base::BootTimeVideoType, 17752da9a59SGnoCiYeH }, 1781496ba7bSLoGin }; 1791496ba7bSLoGin 180c566df45SLoGin arch_video_early_init()?; 181c566df45SLoGin 182c566df45SLoGin let boot_params_guard = boot_params().read(); 183c566df45SLoGin let screen_info = &boot_params_guard.screen_info; 184c566df45SLoGin let buf_vaddr = screen_info.lfb_virt_base.unwrap(); 185c566df45SLoGin 186c566df45SLoGin let buf_flag: ScmBufferFlag; 187c566df45SLoGin let device_buffer: ScmBufferInfo; 188c566df45SLoGin 189c566df45SLoGin if screen_info.video_type == BootTimeVideoType::Mda { 190c566df45SLoGin buf_flag = ScmBufferFlag::SCM_BF_TEXT | ScmBufferFlag::SCM_BF_FB; 191c566df45SLoGin device_buffer = ScmBufferInfo::new_device_buffer( 192c566df45SLoGin screen_info.origin_video_cols.into(), 193c566df45SLoGin screen_info.origin_video_lines.into(), 194c566df45SLoGin screen_info.lfb_size as u32, 195c566df45SLoGin screen_info.lfb_depth.into(), 196c566df45SLoGin buf_flag, 1971496ba7bSLoGin buf_vaddr, 1981496ba7bSLoGin ) 1991496ba7bSLoGin .unwrap(); 200c566df45SLoGin } else { 201c566df45SLoGin // 图形模式 202c566df45SLoGin buf_flag = ScmBufferFlag::SCM_BF_PIXEL | ScmBufferFlag::SCM_BF_FB; 203c566df45SLoGin device_buffer = ScmBufferInfo::new_device_buffer( 204c566df45SLoGin screen_info.lfb_width, 205c566df45SLoGin screen_info.lfb_height, 206c566df45SLoGin screen_info.lfb_size as u32, 207c566df45SLoGin screen_info.lfb_depth.into(), 208c566df45SLoGin buf_flag, 209c566df45SLoGin buf_vaddr, 210c566df45SLoGin ) 211c566df45SLoGin .unwrap(); 212c566df45SLoGin } 2131496ba7bSLoGin 2141496ba7bSLoGin let result = Self { 2151496ba7bSLoGin device_buffer: RwLock::new(device_buffer), 2161496ba7bSLoGin refresh_target: RwLock::new(None), 2171496ba7bSLoGin running: AtomicBool::new(false), 2181496ba7bSLoGin }; 2191496ba7bSLoGin 2201496ba7bSLoGin __MAMAGER = Some(result); 2211496ba7bSLoGin 2221496ba7bSLoGin let init_text = "Video driver initialized.\n\0"; 223a03c4f9dSLoGin send_to_default_serial8250_port(init_text.as_bytes()); 2241496ba7bSLoGin return Ok(()); 2251496ba7bSLoGin } 2261496ba7bSLoGin } 2271496ba7bSLoGin 2281496ba7bSLoGin //刷新任务执行器 2291496ba7bSLoGin #[derive(Debug)] 2301496ba7bSLoGin struct VideoRefreshExecutor; 2311496ba7bSLoGin 2321496ba7bSLoGin impl VideoRefreshExecutor { 2331496ba7bSLoGin fn new() -> Box<VideoRefreshExecutor> { 2341496ba7bSLoGin return Box::new(VideoRefreshExecutor); 2351496ba7bSLoGin } 2361496ba7bSLoGin } 2371496ba7bSLoGin 2381496ba7bSLoGin impl TimerFunction for VideoRefreshExecutor { 2391496ba7bSLoGin /** 2401496ba7bSLoGin * @brief 交给定时器执行的任务,此方法不应手动调用 2411496ba7bSLoGin * @return Ok(()) 2421496ba7bSLoGin */ 243b5b571e0SLoGin #[allow(clippy::type_complexity)] 2441496ba7bSLoGin fn run(&mut self) -> Result<(), SystemError> { 2451496ba7bSLoGin // 获得Manager 2461496ba7bSLoGin let manager = video_refresh_manager(); 2471496ba7bSLoGin 2481496ba7bSLoGin let start_next_refresh = || { 2491496ba7bSLoGin //判断是否还需要刷新,若需要则继续分配下一次计时任务,否则不分配 2501496ba7bSLoGin if manager.running.load(Ordering::SeqCst) { 2511496ba7bSLoGin let timer = Timer::new(VideoRefreshExecutor::new(), REFRESH_INTERVAL); 2521496ba7bSLoGin //将新一次定时任务加入队列 2531496ba7bSLoGin timer.activate(); 2541496ba7bSLoGin } 2551496ba7bSLoGin }; 2561496ba7bSLoGin 257*2755467cS曾俊 let mut refresh_target: Option<RwLockReadGuard<'_, Option<Arc<SpinLock<Box<[u8]>>>>>> = 2581496ba7bSLoGin None; 2591496ba7bSLoGin const TRY_TIMES: i32 = 2; 2601496ba7bSLoGin for i in 0..TRY_TIMES { 2611496ba7bSLoGin let g = manager.refresh_target.try_read(); 2621496ba7bSLoGin if g.is_none() { 2631496ba7bSLoGin if i == TRY_TIMES - 1 { 2641496ba7bSLoGin start_next_refresh(); 2651496ba7bSLoGin return Ok(()); 2661496ba7bSLoGin } 2671496ba7bSLoGin continue; 2681496ba7bSLoGin } 2691496ba7bSLoGin refresh_target = Some(g.unwrap()); 2701496ba7bSLoGin break; 2711496ba7bSLoGin } 2721496ba7bSLoGin 2731496ba7bSLoGin let refresh_target = refresh_target.unwrap(); 2741496ba7bSLoGin 2751496ba7bSLoGin if let ScmBuffer::DeviceBuffer(vaddr) = manager.device_buffer().buf { 276b5b571e0SLoGin let p: *mut u8 = vaddr.as_ptr(); 2771496ba7bSLoGin let mut target_guard = None; 2781496ba7bSLoGin for _ in 0..2 { 2791496ba7bSLoGin if let Ok(guard) = refresh_target.as_ref().unwrap().try_lock_irqsave() { 2801496ba7bSLoGin target_guard = Some(guard); 2811496ba7bSLoGin break; 2821496ba7bSLoGin } 2831496ba7bSLoGin } 2841496ba7bSLoGin if target_guard.is_none() { 2851496ba7bSLoGin start_next_refresh(); 2861496ba7bSLoGin return Ok(()); 2871496ba7bSLoGin } 2881496ba7bSLoGin let mut target_guard = target_guard.unwrap(); 2891496ba7bSLoGin unsafe { 2901496ba7bSLoGin p.copy_from_nonoverlapping( 291*2755467cS曾俊 target_guard.as_mut_ptr(), 2921496ba7bSLoGin manager.device_buffer().buf_size() as usize, 2931496ba7bSLoGin ) 2941496ba7bSLoGin } 2951496ba7bSLoGin } 2961496ba7bSLoGin 2971496ba7bSLoGin start_next_refresh(); 2981496ba7bSLoGin 2991496ba7bSLoGin return Ok(()); 3001496ba7bSLoGin } 3011496ba7bSLoGin } 3021496ba7bSLoGin 303bacd691cSlogin #[no_mangle] 3041496ba7bSLoGin pub unsafe extern "C" fn rs_video_init() -> i32 { 3051496ba7bSLoGin return VideoRefreshManager::video_init() 3061496ba7bSLoGin .map(|_| 0) 3071496ba7bSLoGin .unwrap_or_else(|e| e.to_posix_errno()); 308bacd691cSlogin } 309