1*c566df45SLoGin use core::sync::atomic::{AtomicBool, Ordering}; 2bacd691cSlogin 3bacd691cSlogin use crate::{ 41496ba7bSLoGin arch::MMArch, 5a03c4f9dSLoGin driver::tty::serial::serial8250::send_to_default_serial8250_port, 61496ba7bSLoGin include::bindings::bindings::{ 71496ba7bSLoGin FRAME_BUFFER_MAPPING_OFFSET, SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE, 81496ba7bSLoGin }, 9*c566df45SLoGin init::boot_params, 101496ba7bSLoGin kinfo, 111496ba7bSLoGin libs::{ 121496ba7bSLoGin align::page_align_up, 131496ba7bSLoGin lib_ui::screen_manager::{ScmBuffer, ScmBufferFlag, ScmBufferInfo}, 141496ba7bSLoGin rwlock::{RwLock, RwLockReadGuard}, 151496ba7bSLoGin spinlock::SpinLock, 161496ba7bSLoGin }, 171496ba7bSLoGin mm::{ 18*c566df45SLoGin allocator::page_frame::PageFrameCount, kernel_mapper::KernelMapper, page::PageFlags, 19*c566df45SLoGin MemoryManagementArch, VirtAddr, 201496ba7bSLoGin }, 211496ba7bSLoGin time::timer::{Timer, TimerFunction}, 22bacd691cSlogin }; 2391e9d4abSLoGin use alloc::{boxed::Box, sync::Arc}; 2491e9d4abSLoGin use system_error::SystemError; 25bacd691cSlogin 2608a2ee40SLoGin pub mod fbdev; 2708a2ee40SLoGin 281496ba7bSLoGin static mut __MAMAGER: Option<VideoRefreshManager> = None; 291496ba7bSLoGin 301496ba7bSLoGin pub fn video_refresh_manager() -> &'static VideoRefreshManager { 311496ba7bSLoGin return unsafe { 321496ba7bSLoGin &__MAMAGER 331496ba7bSLoGin .as_ref() 341496ba7bSLoGin .expect("Video refresh manager has not been initialized yet!") 351496ba7bSLoGin }; 361496ba7bSLoGin } 371496ba7bSLoGin 381496ba7bSLoGin ///管理显示刷新变量的结构体 391496ba7bSLoGin pub struct VideoRefreshManager { 401496ba7bSLoGin device_buffer: RwLock<ScmBufferInfo>, 411496ba7bSLoGin refresh_target: RwLock<Option<Arc<SpinLock<Box<[u32]>>>>>, 4278bf93f0SYJwu2023 running: AtomicBool, 43bacd691cSlogin } 44bacd691cSlogin 451496ba7bSLoGin const REFRESH_INTERVAL: u64 = 30; 461496ba7bSLoGin 471496ba7bSLoGin impl VideoRefreshManager { 481496ba7bSLoGin /** 491496ba7bSLoGin * @brief 启动定时刷新 501496ba7bSLoGin * @return 启动成功: true, 失败: false 511496ba7bSLoGin */ 521496ba7bSLoGin pub fn run_video_refresh(&self) -> bool { 531496ba7bSLoGin //设置Manager运行标志 541496ba7bSLoGin let res = self.set_run(); 551496ba7bSLoGin 561496ba7bSLoGin //设置成功则开始任务,否则直接返回false 571496ba7bSLoGin if res { 581496ba7bSLoGin //第一次将expire_jiffies设置小一点,使得这次刷新尽快开始,后续的刷新将按照REFRESH_INTERVAL间隔进行 591496ba7bSLoGin let timer = Timer::new(VideoRefreshExecutor::new(), 1); 601496ba7bSLoGin //将新一次定时任务加入队列 611496ba7bSLoGin timer.activate(); 621496ba7bSLoGin } 631496ba7bSLoGin return res; 64bacd691cSlogin } 65bacd691cSlogin 661496ba7bSLoGin /// 停止定时刷新 671496ba7bSLoGin #[allow(dead_code)] 681496ba7bSLoGin pub fn stop_video_refresh(&self) { 691496ba7bSLoGin self.running.store(false, Ordering::SeqCst); 70bacd691cSlogin } 71bacd691cSlogin 72bacd691cSlogin fn set_run(&self) -> bool { 731496ba7bSLoGin let res = self 74bacd691cSlogin .running 751496ba7bSLoGin .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst); 761496ba7bSLoGin if res.is_ok() { 77bacd691cSlogin return true; 78bacd691cSlogin } else { 79bacd691cSlogin return false; 80bacd691cSlogin } 81bacd691cSlogin } 82bacd691cSlogin 831496ba7bSLoGin /** 841496ba7bSLoGin * @brief VBE帧缓存区的地址重新映射 851496ba7bSLoGin * 将帧缓存区映射到地址SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE处 861496ba7bSLoGin */ 871496ba7bSLoGin fn init_frame_buffer(&self) { 881496ba7bSLoGin kinfo!("Re-mapping VBE frame buffer..."); 891496ba7bSLoGin let buf_vaddr = VirtAddr::new( 901496ba7bSLoGin SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE as usize + FRAME_BUFFER_MAPPING_OFFSET as usize, 911496ba7bSLoGin ); 92*c566df45SLoGin boot_params().write_irqsave().screen_info.lfb_virt_base = Some(buf_vaddr); 931496ba7bSLoGin 941496ba7bSLoGin let mut frame_buffer_info_graud = self.device_buffer.write(); 951496ba7bSLoGin if let ScmBuffer::DeviceBuffer(vaddr) = &mut (frame_buffer_info_graud).buf { 961496ba7bSLoGin *vaddr = buf_vaddr; 971496ba7bSLoGin } 981496ba7bSLoGin 991496ba7bSLoGin // 地址映射 100*c566df45SLoGin let mut paddr = boot_params().read().screen_info.lfb_base; 1011496ba7bSLoGin let count = PageFrameCount::new( 1021496ba7bSLoGin page_align_up(frame_buffer_info_graud.buf_size()) / MMArch::PAGE_SIZE, 1031496ba7bSLoGin ); 1041496ba7bSLoGin let page_flags: PageFlags<MMArch> = PageFlags::new().set_execute(true).set_write(true); 1051496ba7bSLoGin 1061496ba7bSLoGin let mut kernel_mapper = KernelMapper::lock(); 1071496ba7bSLoGin let mut kernel_mapper = kernel_mapper.as_mut(); 1081496ba7bSLoGin assert!(kernel_mapper.is_some()); 1091496ba7bSLoGin let mut vaddr = buf_vaddr; 1101496ba7bSLoGin unsafe { 1111496ba7bSLoGin for _ in 0..count.data() { 1121496ba7bSLoGin let flusher = kernel_mapper 1131496ba7bSLoGin .as_mut() 1141496ba7bSLoGin .unwrap() 1151496ba7bSLoGin .map_phys(vaddr, paddr, page_flags) 1161496ba7bSLoGin .unwrap(); 1171496ba7bSLoGin 1181496ba7bSLoGin flusher.flush(); 1191496ba7bSLoGin vaddr += MMArch::PAGE_SIZE; 1201496ba7bSLoGin paddr += MMArch::PAGE_SIZE; 121bacd691cSlogin } 122bacd691cSlogin } 123bacd691cSlogin 1241496ba7bSLoGin kinfo!("VBE frame buffer successfully Re-mapped!"); 125bacd691cSlogin } 1261496ba7bSLoGin 1271496ba7bSLoGin /** 1281496ba7bSLoGin * @brief 初始化显示模块,需先低级初始化才能高级初始化 1291496ba7bSLoGin * @param level 初始化等级 1301496ba7bSLoGin * false -> 低级初始化:不使用double buffer 1311496ba7bSLoGin * true ->高级初始化:增加double buffer的支持 1321496ba7bSLoGin * @return int 1331496ba7bSLoGin */ 1341496ba7bSLoGin pub fn video_reinitialize(&self, level: bool) -> Result<(), SystemError> { 1351496ba7bSLoGin if !level { 1361496ba7bSLoGin self.init_frame_buffer(); 1371496ba7bSLoGin } else { 1381496ba7bSLoGin // 开启屏幕计时刷新 1391496ba7bSLoGin assert!(self.run_video_refresh()); 1401496ba7bSLoGin } 1411496ba7bSLoGin return Ok(()); 1421496ba7bSLoGin } 1431496ba7bSLoGin 1441496ba7bSLoGin /** 1451496ba7bSLoGin * @brief 设置帧缓冲区刷新目标 1461496ba7bSLoGin * 1471496ba7bSLoGin * @param buf 1481496ba7bSLoGin * @return int 1491496ba7bSLoGin */ 1501496ba7bSLoGin pub fn set_refresh_target(&self, buf_info: &ScmBufferInfo) -> Result<(), SystemError> { 1511496ba7bSLoGin let mut refresh_target = self.refresh_target.write_irqsave(); 1521496ba7bSLoGin if let ScmBuffer::DoubleBuffer(double_buffer) = &buf_info.buf { 1531496ba7bSLoGin *refresh_target = Some(double_buffer.clone()); 1541496ba7bSLoGin return Ok(()); 1551496ba7bSLoGin } 1561496ba7bSLoGin return Err(SystemError::EINVAL); 1571496ba7bSLoGin } 1581496ba7bSLoGin 1591496ba7bSLoGin #[allow(dead_code)] 1601496ba7bSLoGin pub fn refresh_target(&self) -> RwLockReadGuard<'_, Option<Arc<SpinLock<Box<[u32]>>>>> { 1611496ba7bSLoGin let x = self.refresh_target.read(); 1621496ba7bSLoGin 1631496ba7bSLoGin return x; 1641496ba7bSLoGin } 1651496ba7bSLoGin 1661496ba7bSLoGin pub fn device_buffer(&self) -> RwLockReadGuard<'_, ScmBufferInfo> { 1671496ba7bSLoGin return self.device_buffer.read(); 1681496ba7bSLoGin } 1691496ba7bSLoGin 1701a72a751SLoGin /// 在riscv64平台下暂时不支持 1711a72a751SLoGin #[cfg(target_arch = "riscv64")] 1721a72a751SLoGin pub unsafe fn video_init() -> Result<(), SystemError> { 1731a72a751SLoGin return Err(SystemError::ENOSYS); 1741a72a751SLoGin } 1751a72a751SLoGin 1761a72a751SLoGin /// 此函数用于初始化显示驱动,为后续的图形输出做好准备。 177*c566df45SLoGin #[cfg(target_arch = "x86_64")] 1781496ba7bSLoGin pub unsafe fn video_init() -> Result<(), SystemError> { 179*c566df45SLoGin use crate::{ 180*c566df45SLoGin arch::driver::video::arch_video_early_init, 181*c566df45SLoGin driver::video::fbdev::base::BootTimeVideoType, 1821496ba7bSLoGin }; 1831496ba7bSLoGin 184*c566df45SLoGin arch_video_early_init()?; 185*c566df45SLoGin 186*c566df45SLoGin let boot_params_guard = boot_params().read(); 187*c566df45SLoGin let screen_info = &boot_params_guard.screen_info; 188*c566df45SLoGin let buf_vaddr = screen_info.lfb_virt_base.unwrap(); 189*c566df45SLoGin 190*c566df45SLoGin let buf_flag: ScmBufferFlag; 191*c566df45SLoGin let device_buffer: ScmBufferInfo; 192*c566df45SLoGin 193*c566df45SLoGin if screen_info.video_type == BootTimeVideoType::Mda { 194*c566df45SLoGin buf_flag = ScmBufferFlag::SCM_BF_TEXT | ScmBufferFlag::SCM_BF_FB; 195*c566df45SLoGin device_buffer = ScmBufferInfo::new_device_buffer( 196*c566df45SLoGin screen_info.origin_video_cols.into(), 197*c566df45SLoGin screen_info.origin_video_lines.into(), 198*c566df45SLoGin screen_info.lfb_size as u32, 199*c566df45SLoGin screen_info.lfb_depth.into(), 200*c566df45SLoGin buf_flag, 2011496ba7bSLoGin buf_vaddr, 2021496ba7bSLoGin ) 2031496ba7bSLoGin .unwrap(); 204*c566df45SLoGin } else { 205*c566df45SLoGin // 图形模式 206*c566df45SLoGin buf_flag = ScmBufferFlag::SCM_BF_PIXEL | ScmBufferFlag::SCM_BF_FB; 207*c566df45SLoGin device_buffer = ScmBufferInfo::new_device_buffer( 208*c566df45SLoGin screen_info.lfb_width, 209*c566df45SLoGin screen_info.lfb_height, 210*c566df45SLoGin screen_info.lfb_size as u32, 211*c566df45SLoGin screen_info.lfb_depth.into(), 212*c566df45SLoGin buf_flag, 213*c566df45SLoGin buf_vaddr, 214*c566df45SLoGin ) 215*c566df45SLoGin .unwrap(); 216*c566df45SLoGin } 2171496ba7bSLoGin 2181496ba7bSLoGin let result = Self { 2191496ba7bSLoGin device_buffer: RwLock::new(device_buffer), 2201496ba7bSLoGin refresh_target: RwLock::new(None), 2211496ba7bSLoGin running: AtomicBool::new(false), 2221496ba7bSLoGin }; 2231496ba7bSLoGin 2241496ba7bSLoGin __MAMAGER = Some(result); 2251496ba7bSLoGin 2261496ba7bSLoGin let init_text = "Video driver initialized.\n\0"; 227a03c4f9dSLoGin send_to_default_serial8250_port(init_text.as_bytes()); 2281496ba7bSLoGin return Ok(()); 2291496ba7bSLoGin } 2301496ba7bSLoGin } 2311496ba7bSLoGin 2321496ba7bSLoGin //刷新任务执行器 2331496ba7bSLoGin #[derive(Debug)] 2341496ba7bSLoGin struct VideoRefreshExecutor; 2351496ba7bSLoGin 2361496ba7bSLoGin impl VideoRefreshExecutor { 2371496ba7bSLoGin fn new() -> Box<VideoRefreshExecutor> { 2381496ba7bSLoGin return Box::new(VideoRefreshExecutor); 2391496ba7bSLoGin } 2401496ba7bSLoGin } 2411496ba7bSLoGin 2421496ba7bSLoGin impl TimerFunction for VideoRefreshExecutor { 2431496ba7bSLoGin /** 2441496ba7bSLoGin * @brief 交给定时器执行的任务,此方法不应手动调用 2451496ba7bSLoGin * @return Ok(()) 2461496ba7bSLoGin */ 2471496ba7bSLoGin fn run(&mut self) -> Result<(), SystemError> { 2481496ba7bSLoGin // 获得Manager 2491496ba7bSLoGin let manager = video_refresh_manager(); 2501496ba7bSLoGin 2511496ba7bSLoGin let start_next_refresh = || { 2521496ba7bSLoGin //判断是否还需要刷新,若需要则继续分配下一次计时任务,否则不分配 2531496ba7bSLoGin if manager.running.load(Ordering::SeqCst) { 2541496ba7bSLoGin let timer = Timer::new(VideoRefreshExecutor::new(), REFRESH_INTERVAL); 2551496ba7bSLoGin //将新一次定时任务加入队列 2561496ba7bSLoGin timer.activate(); 2571496ba7bSLoGin } 2581496ba7bSLoGin }; 2591496ba7bSLoGin 2601496ba7bSLoGin let mut refresh_target: Option<RwLockReadGuard<'_, Option<Arc<SpinLock<Box<[u32]>>>>>> = 2611496ba7bSLoGin None; 2621496ba7bSLoGin const TRY_TIMES: i32 = 2; 2631496ba7bSLoGin for i in 0..TRY_TIMES { 2641496ba7bSLoGin let g = manager.refresh_target.try_read(); 2651496ba7bSLoGin if g.is_none() { 2661496ba7bSLoGin if i == TRY_TIMES - 1 { 2671496ba7bSLoGin start_next_refresh(); 2681496ba7bSLoGin return Ok(()); 2691496ba7bSLoGin } 2701496ba7bSLoGin continue; 2711496ba7bSLoGin } 2721496ba7bSLoGin refresh_target = Some(g.unwrap()); 2731496ba7bSLoGin break; 2741496ba7bSLoGin } 2751496ba7bSLoGin 2761496ba7bSLoGin let refresh_target = refresh_target.unwrap(); 2771496ba7bSLoGin 2781496ba7bSLoGin if let ScmBuffer::DeviceBuffer(vaddr) = manager.device_buffer().buf { 2791496ba7bSLoGin let p = vaddr.as_ptr() as *mut u8; 2801496ba7bSLoGin let mut target_guard = None; 2811496ba7bSLoGin for _ in 0..2 { 2821496ba7bSLoGin if let Ok(guard) = refresh_target.as_ref().unwrap().try_lock_irqsave() { 2831496ba7bSLoGin target_guard = Some(guard); 2841496ba7bSLoGin break; 2851496ba7bSLoGin } 2861496ba7bSLoGin } 2871496ba7bSLoGin if target_guard.is_none() { 2881496ba7bSLoGin start_next_refresh(); 2891496ba7bSLoGin return Ok(()); 2901496ba7bSLoGin } 2911496ba7bSLoGin let mut target_guard = target_guard.unwrap(); 2921496ba7bSLoGin unsafe { 2931496ba7bSLoGin p.copy_from_nonoverlapping( 2941496ba7bSLoGin target_guard.as_mut_ptr() as *mut u8, 2951496ba7bSLoGin manager.device_buffer().buf_size() as usize, 2961496ba7bSLoGin ) 2971496ba7bSLoGin } 2981496ba7bSLoGin } 2991496ba7bSLoGin 3001496ba7bSLoGin start_next_refresh(); 3011496ba7bSLoGin 3021496ba7bSLoGin return Ok(()); 3031496ba7bSLoGin } 3041496ba7bSLoGin } 3051496ba7bSLoGin 306bacd691cSlogin #[no_mangle] 3071496ba7bSLoGin pub unsafe extern "C" fn rs_video_init() -> i32 { 3081496ba7bSLoGin return VideoRefreshManager::video_init() 3091496ba7bSLoGin .map(|_| 0) 3101496ba7bSLoGin .unwrap_or_else(|e| e.to_posix_errno()); 311bacd691cSlogin } 312