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