1 use core::sync::atomic::{AtomicBool, Ordering}; 2 3 use crate::{ 4 arch::MMArch, 5 init::boot_params, 6 kinfo, 7 libs::{ 8 align::page_align_up, 9 lib_ui::screen_manager::{ScmBuffer, ScmBufferFlag, ScmBufferInfo}, 10 rwlock::{RwLock, RwLockReadGuard}, 11 spinlock::SpinLock, 12 }, 13 mm::{ 14 allocator::page_frame::PageFrameCount, kernel_mapper::KernelMapper, page::PageFlags, 15 MemoryManagementArch, 16 }, 17 time::timer::{Timer, TimerFunction}, 18 }; 19 use alloc::{boxed::Box, sync::Arc}; 20 use system_error::SystemError; 21 22 pub mod fbdev; 23 24 static mut __MAMAGER: Option<VideoRefreshManager> = None; 25 26 pub fn video_refresh_manager() -> &'static VideoRefreshManager { 27 return unsafe { 28 &__MAMAGER 29 .as_ref() 30 .expect("Video refresh manager has not been initialized yet!") 31 }; 32 } 33 34 ///管理显示刷新变量的结构体 35 pub struct VideoRefreshManager { 36 device_buffer: RwLock<ScmBufferInfo>, 37 refresh_target: RwLock<Option<Arc<SpinLock<Box<[u32]>>>>>, 38 running: AtomicBool, 39 } 40 41 const REFRESH_INTERVAL: u64 = 30; 42 43 impl VideoRefreshManager { 44 /** 45 * @brief 启动定时刷新 46 * @return 启动成功: true, 失败: false 47 */ 48 pub fn run_video_refresh(&self) -> bool { 49 //设置Manager运行标志 50 let res = self.set_run(); 51 52 //设置成功则开始任务,否则直接返回false 53 if res { 54 //第一次将expire_jiffies设置小一点,使得这次刷新尽快开始,后续的刷新将按照REFRESH_INTERVAL间隔进行 55 let timer = Timer::new(VideoRefreshExecutor::new(), 1); 56 //将新一次定时任务加入队列 57 timer.activate(); 58 } 59 return res; 60 } 61 62 /// 停止定时刷新 63 #[allow(dead_code)] 64 pub fn stop_video_refresh(&self) { 65 self.running.store(false, Ordering::SeqCst); 66 } 67 68 fn set_run(&self) -> bool { 69 let res = self 70 .running 71 .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst); 72 if res.is_ok() { 73 return true; 74 } else { 75 return false; 76 } 77 } 78 79 /** 80 * @brief VBE帧缓存区的地址重新映射 81 * 将帧缓存区映射到地址SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE处 82 */ 83 fn init_frame_buffer(&self) { 84 kinfo!("Re-mapping VBE frame buffer..."); 85 let buf_vaddr = boot_params() 86 .read_irqsave() 87 .screen_info 88 .lfb_virt_base 89 .unwrap(); 90 91 let mut frame_buffer_info_guard = self.device_buffer.write(); 92 if let ScmBuffer::DeviceBuffer(vaddr) = &mut (frame_buffer_info_guard).buf { 93 *vaddr = buf_vaddr; 94 } 95 96 // 地址映射 97 let mut paddr = boot_params().read().screen_info.lfb_base; 98 let count = PageFrameCount::new( 99 page_align_up(frame_buffer_info_guard.buf_size()) / MMArch::PAGE_SIZE, 100 ); 101 let page_flags: PageFlags<MMArch> = PageFlags::new().set_execute(true).set_write(true); 102 103 let mut kernel_mapper = KernelMapper::lock(); 104 let mut kernel_mapper = kernel_mapper.as_mut(); 105 assert!(kernel_mapper.is_some()); 106 let mut vaddr = buf_vaddr; 107 unsafe { 108 for _ in 0..count.data() { 109 let flusher = kernel_mapper 110 .as_mut() 111 .unwrap() 112 .map_phys(vaddr, paddr, page_flags) 113 .unwrap(); 114 115 flusher.flush(); 116 vaddr += MMArch::PAGE_SIZE; 117 paddr += MMArch::PAGE_SIZE; 118 } 119 } 120 121 kinfo!("VBE frame buffer successfully Re-mapped!"); 122 } 123 124 /** 125 * @brief 初始化显示模块,需先低级初始化才能高级初始化 126 * @param level 初始化等级 127 * false -> 低级初始化:不使用double buffer 128 * true ->高级初始化:增加double buffer的支持 129 * @return int 130 */ 131 pub fn video_reinitialize(&self, level: bool) -> Result<(), SystemError> { 132 if !level { 133 self.init_frame_buffer(); 134 } else { 135 // 开启屏幕计时刷新 136 assert!(self.run_video_refresh()); 137 } 138 return Ok(()); 139 } 140 141 /** 142 * @brief 设置帧缓冲区刷新目标 143 * 144 * @param buf 145 * @return int 146 */ 147 pub fn set_refresh_target(&self, buf_info: &ScmBufferInfo) -> Result<(), SystemError> { 148 let mut refresh_target = self.refresh_target.write_irqsave(); 149 if let ScmBuffer::DoubleBuffer(double_buffer) = &buf_info.buf { 150 *refresh_target = Some(double_buffer.clone()); 151 return Ok(()); 152 } 153 return Err(SystemError::EINVAL); 154 } 155 156 #[allow(dead_code)] 157 pub fn refresh_target(&self) -> RwLockReadGuard<'_, Option<Arc<SpinLock<Box<[u32]>>>>> { 158 let x = self.refresh_target.read(); 159 160 return x; 161 } 162 163 pub fn device_buffer(&self) -> RwLockReadGuard<'_, ScmBufferInfo> { 164 return self.device_buffer.read(); 165 } 166 167 /// 在riscv64平台下暂时不支持 168 #[cfg(target_arch = "riscv64")] 169 pub unsafe fn video_init() -> Result<(), SystemError> { 170 return Err(SystemError::ENOSYS); 171 } 172 173 /// 此函数用于初始化显示驱动,为后续的图形输出做好准备。 174 #[cfg(target_arch = "x86_64")] 175 pub unsafe fn video_init() -> Result<(), SystemError> { 176 use crate::{ 177 arch::driver::video::arch_video_early_init, 178 driver::{ 179 serial::serial8250::send_to_default_serial8250_port, 180 video::fbdev::base::BootTimeVideoType, 181 }, 182 }; 183 184 arch_video_early_init()?; 185 186 let boot_params_guard = boot_params().read(); 187 let screen_info = &boot_params_guard.screen_info; 188 let buf_vaddr = screen_info.lfb_virt_base.unwrap(); 189 190 let buf_flag: ScmBufferFlag; 191 let device_buffer: ScmBufferInfo; 192 193 if screen_info.video_type == BootTimeVideoType::Mda { 194 buf_flag = ScmBufferFlag::SCM_BF_TEXT | ScmBufferFlag::SCM_BF_FB; 195 device_buffer = ScmBufferInfo::new_device_buffer( 196 screen_info.origin_video_cols.into(), 197 screen_info.origin_video_lines.into(), 198 screen_info.lfb_size as u32, 199 screen_info.lfb_depth.into(), 200 buf_flag, 201 buf_vaddr, 202 ) 203 .unwrap(); 204 } else { 205 // 图形模式 206 buf_flag = ScmBufferFlag::SCM_BF_PIXEL | ScmBufferFlag::SCM_BF_FB; 207 device_buffer = ScmBufferInfo::new_device_buffer( 208 screen_info.lfb_width, 209 screen_info.lfb_height, 210 screen_info.lfb_size as u32, 211 screen_info.lfb_depth.into(), 212 buf_flag, 213 buf_vaddr, 214 ) 215 .unwrap(); 216 } 217 218 let result = Self { 219 device_buffer: RwLock::new(device_buffer), 220 refresh_target: RwLock::new(None), 221 running: AtomicBool::new(false), 222 }; 223 224 __MAMAGER = Some(result); 225 226 let init_text = "Video driver initialized.\n\0"; 227 send_to_default_serial8250_port(init_text.as_bytes()); 228 return Ok(()); 229 } 230 } 231 232 //刷新任务执行器 233 #[derive(Debug)] 234 struct VideoRefreshExecutor; 235 236 impl VideoRefreshExecutor { 237 fn new() -> Box<VideoRefreshExecutor> { 238 return Box::new(VideoRefreshExecutor); 239 } 240 } 241 242 impl TimerFunction for VideoRefreshExecutor { 243 /** 244 * @brief 交给定时器执行的任务,此方法不应手动调用 245 * @return Ok(()) 246 */ 247 fn run(&mut self) -> Result<(), SystemError> { 248 // 获得Manager 249 let manager = video_refresh_manager(); 250 251 let start_next_refresh = || { 252 //判断是否还需要刷新,若需要则继续分配下一次计时任务,否则不分配 253 if manager.running.load(Ordering::SeqCst) { 254 let timer = Timer::new(VideoRefreshExecutor::new(), REFRESH_INTERVAL); 255 //将新一次定时任务加入队列 256 timer.activate(); 257 } 258 }; 259 260 let mut refresh_target: Option<RwLockReadGuard<'_, Option<Arc<SpinLock<Box<[u32]>>>>>> = 261 None; 262 const TRY_TIMES: i32 = 2; 263 for i in 0..TRY_TIMES { 264 let g = manager.refresh_target.try_read(); 265 if g.is_none() { 266 if i == TRY_TIMES - 1 { 267 start_next_refresh(); 268 return Ok(()); 269 } 270 continue; 271 } 272 refresh_target = Some(g.unwrap()); 273 break; 274 } 275 276 let refresh_target = refresh_target.unwrap(); 277 278 if let ScmBuffer::DeviceBuffer(vaddr) = manager.device_buffer().buf { 279 let p = vaddr.as_ptr() as *mut u8; 280 let mut target_guard = None; 281 for _ in 0..2 { 282 if let Ok(guard) = refresh_target.as_ref().unwrap().try_lock_irqsave() { 283 target_guard = Some(guard); 284 break; 285 } 286 } 287 if target_guard.is_none() { 288 start_next_refresh(); 289 return Ok(()); 290 } 291 let mut target_guard = target_guard.unwrap(); 292 unsafe { 293 p.copy_from_nonoverlapping( 294 target_guard.as_mut_ptr() as *mut u8, 295 manager.device_buffer().buf_size() as usize, 296 ) 297 } 298 } 299 300 start_next_refresh(); 301 302 return Ok(()); 303 } 304 } 305 306 #[no_mangle] 307 pub unsafe extern "C" fn rs_video_init() -> i32 { 308 return VideoRefreshManager::video_init() 309 .map(|_| 0) 310 .unwrap_or_else(|e| e.to_posix_errno()); 311 } 312