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