xref: /DragonOS/kernel/src/driver/video/mod.rs (revision 2b7818e80e00fcfe4d03533f587cc125ea5e4bec)
1c566df45SLoGin use core::sync::atomic::{AtomicBool, Ordering};
2bacd691cSlogin 
3bacd691cSlogin use crate::{
41496ba7bSLoGin     arch::MMArch,
5c566df45SLoGin     init::boot_params,
61496ba7bSLoGin     libs::{
71496ba7bSLoGin         align::page_align_up,
81496ba7bSLoGin         lib_ui::screen_manager::{ScmBuffer, ScmBufferFlag, ScmBufferInfo},
91496ba7bSLoGin         rwlock::{RwLock, RwLockReadGuard},
101496ba7bSLoGin         spinlock::SpinLock,
111496ba7bSLoGin     },
12*2b7818e8SLoGin     mm::{mmio_buddy::mmio_pool, page::EntryFlags},
131496ba7bSLoGin     time::timer::{Timer, TimerFunction},
14bacd691cSlogin };
1591e9d4abSLoGin use alloc::{boxed::Box, sync::Arc};
162eab6dd7S曾俊 use log::info;
1791e9d4abSLoGin use system_error::SystemError;
18bacd691cSlogin 
19418ad41fSLoGin pub mod console;
2008a2ee40SLoGin pub mod fbdev;
2108a2ee40SLoGin 
221496ba7bSLoGin static mut __MAMAGER: Option<VideoRefreshManager> = None;
231496ba7bSLoGin 
video_refresh_manager() -> &'static VideoRefreshManager241496ba7bSLoGin pub fn video_refresh_manager() -> &'static VideoRefreshManager {
251496ba7bSLoGin     return unsafe {
26b5b571e0SLoGin         __MAMAGER
271496ba7bSLoGin             .as_ref()
281496ba7bSLoGin             .expect("Video refresh manager has not been initialized yet!")
291496ba7bSLoGin     };
301496ba7bSLoGin }
31b5b571e0SLoGin #[allow(clippy::type_complexity)]
321496ba7bSLoGin ///管理显示刷新变量的结构体
331496ba7bSLoGin pub struct VideoRefreshManager {
341496ba7bSLoGin     device_buffer: RwLock<ScmBufferInfo>,
352755467cS曾俊     refresh_target: RwLock<Option<Arc<SpinLock<Box<[u8]>>>>>,
3678bf93f0SYJwu2023     running: AtomicBool,
37bacd691cSlogin }
38bacd691cSlogin 
391496ba7bSLoGin const REFRESH_INTERVAL: u64 = 30;
401496ba7bSLoGin 
411496ba7bSLoGin impl VideoRefreshManager {
421496ba7bSLoGin     /**
431496ba7bSLoGin      * @brief 启动定时刷新
441496ba7bSLoGin      * @return 启动成功: true, 失败: false
451496ba7bSLoGin      */
run_video_refresh(&self) -> bool461496ba7bSLoGin     pub fn run_video_refresh(&self) -> bool {
471496ba7bSLoGin         //设置Manager运行标志
481496ba7bSLoGin         let res = self.set_run();
491496ba7bSLoGin 
501496ba7bSLoGin         //设置成功则开始任务,否则直接返回false
511496ba7bSLoGin         if res {
521496ba7bSLoGin             //第一次将expire_jiffies设置小一点,使得这次刷新尽快开始,后续的刷新将按照REFRESH_INTERVAL间隔进行
531496ba7bSLoGin             let timer = Timer::new(VideoRefreshExecutor::new(), 1);
541496ba7bSLoGin             //将新一次定时任务加入队列
551496ba7bSLoGin             timer.activate();
561496ba7bSLoGin         }
571496ba7bSLoGin         return res;
58bacd691cSlogin     }
59bacd691cSlogin 
601496ba7bSLoGin     /// 停止定时刷新
611496ba7bSLoGin     #[allow(dead_code)]
stop_video_refresh(&self)621496ba7bSLoGin     pub fn stop_video_refresh(&self) {
631496ba7bSLoGin         self.running.store(false, Ordering::SeqCst);
64bacd691cSlogin     }
65bacd691cSlogin 
set_run(&self) -> bool66bacd691cSlogin     fn set_run(&self) -> bool {
671496ba7bSLoGin         let res = self
68bacd691cSlogin             .running
691496ba7bSLoGin             .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst);
70b5b571e0SLoGin         return res.is_ok();
71bacd691cSlogin     }
72bacd691cSlogin 
731496ba7bSLoGin     /**
74*2b7818e8SLoGin      * VBE帧缓存区的地址重新映射
751496ba7bSLoGin      */
init_frame_buffer(&self)761496ba7bSLoGin     fn init_frame_buffer(&self) {
772eab6dd7S曾俊         info!("Re-mapping VBE frame buffer...");
78*2b7818e8SLoGin         let mut bp = boot_params().write_irqsave();
79*2b7818e8SLoGin         let buf_size = bp.screen_info.lfb_size;
801496ba7bSLoGin 
81*2b7818e8SLoGin         let mmio_guard = mmio_pool().create_mmio(page_align_up(buf_size)).unwrap();
82*2b7818e8SLoGin         let mmio_guard = Arc::new(mmio_guard);
83*2b7818e8SLoGin         let buf_vaddr = mmio_guard.vaddr();
84*2b7818e8SLoGin         bp.screen_info.lfb_virt_base = Some(buf_vaddr);
85*2b7818e8SLoGin 
86*2b7818e8SLoGin         let mut frame_buffer_info_guard: crate::libs::rwlock::RwLockWriteGuard<ScmBufferInfo> =
87*2b7818e8SLoGin             self.device_buffer.write();
88*2b7818e8SLoGin         unsafe { frame_buffer_info_guard.set_device_buffer_mmio_guard(mmio_guard.clone()) };
8902343d0bSLoGin         if let ScmBuffer::DeviceBuffer(vaddr) = &mut (frame_buffer_info_guard).buf {
901496ba7bSLoGin             *vaddr = buf_vaddr;
911496ba7bSLoGin         }
921496ba7bSLoGin         // 地址映射
93*2b7818e8SLoGin         let paddr = bp.screen_info.lfb_base;
94cf7f801eSMemoryShore         let page_flags: EntryFlags<MMArch> = EntryFlags::new().set_execute(true).set_write(true);
951496ba7bSLoGin 
961496ba7bSLoGin         unsafe {
97*2b7818e8SLoGin             mmio_guard
98*2b7818e8SLoGin                 .map_phys_with_flags(paddr, page_align_up(buf_size), page_flags)
99*2b7818e8SLoGin                 .expect("Failed to map VBE frame buffer!")
100*2b7818e8SLoGin         };
101bacd691cSlogin 
1022eab6dd7S曾俊         info!("VBE frame buffer successfully Re-mapped!");
103bacd691cSlogin     }
1041496ba7bSLoGin 
1051496ba7bSLoGin     /**
1061496ba7bSLoGin      * @brief 初始化显示模块,需先低级初始化才能高级初始化
1071496ba7bSLoGin      * @param level 初始化等级
1081496ba7bSLoGin      * false -> 低级初始化:不使用double buffer
1091496ba7bSLoGin      * true ->高级初始化:增加double buffer的支持
1101496ba7bSLoGin      * @return int
1111496ba7bSLoGin      */
video_reinitialize(&self, level: bool) -> Result<(), SystemError>1121496ba7bSLoGin     pub fn video_reinitialize(&self, level: bool) -> Result<(), SystemError> {
1131496ba7bSLoGin         if !level {
1141496ba7bSLoGin             self.init_frame_buffer();
1151496ba7bSLoGin         } else {
1161496ba7bSLoGin             // 开启屏幕计时刷新
1171496ba7bSLoGin             assert!(self.run_video_refresh());
1181496ba7bSLoGin         }
1191496ba7bSLoGin         return Ok(());
1201496ba7bSLoGin     }
1211496ba7bSLoGin 
1221496ba7bSLoGin     /**
1231496ba7bSLoGin      * @brief 设置帧缓冲区刷新目标
1241496ba7bSLoGin      *
1251496ba7bSLoGin      * @param buf
1261496ba7bSLoGin      * @return int
1271496ba7bSLoGin      */
set_refresh_target(&self, buf_info: &ScmBufferInfo) -> Result<(), SystemError>1281496ba7bSLoGin     pub fn set_refresh_target(&self, buf_info: &ScmBufferInfo) -> Result<(), SystemError> {
1291496ba7bSLoGin         let mut refresh_target = self.refresh_target.write_irqsave();
1301496ba7bSLoGin         if let ScmBuffer::DoubleBuffer(double_buffer) = &buf_info.buf {
1311496ba7bSLoGin             *refresh_target = Some(double_buffer.clone());
1321496ba7bSLoGin             return Ok(());
1331496ba7bSLoGin         }
1341496ba7bSLoGin         return Err(SystemError::EINVAL);
1351496ba7bSLoGin     }
136b5b571e0SLoGin     #[allow(clippy::type_complexity)]
1371496ba7bSLoGin     #[allow(dead_code)]
refresh_target(&self) -> RwLockReadGuard<'_, Option<Arc<SpinLock<Box<[u8]>>>>>1382755467cS曾俊     pub fn refresh_target(&self) -> RwLockReadGuard<'_, Option<Arc<SpinLock<Box<[u8]>>>>> {
1391496ba7bSLoGin         let x = self.refresh_target.read();
1401496ba7bSLoGin 
1411496ba7bSLoGin         return x;
1421496ba7bSLoGin     }
1431496ba7bSLoGin 
device_buffer(&self) -> RwLockReadGuard<'_, ScmBufferInfo>1441496ba7bSLoGin     pub fn device_buffer(&self) -> RwLockReadGuard<'_, ScmBufferInfo> {
1451496ba7bSLoGin         return self.device_buffer.read();
1461496ba7bSLoGin     }
1471496ba7bSLoGin 
1481a72a751SLoGin     /// 在riscv64平台下暂时不支持
1491a72a751SLoGin     #[cfg(target_arch = "riscv64")]
video_init() -> Result<(), SystemError>1501a72a751SLoGin     pub unsafe fn video_init() -> Result<(), SystemError> {
1511a72a751SLoGin         return Err(SystemError::ENOSYS);
1521a72a751SLoGin     }
1531a72a751SLoGin 
1541a72a751SLoGin     /// 此函数用于初始化显示驱动,为后续的图形输出做好准备。
155c566df45SLoGin     #[cfg(target_arch = "x86_64")]
video_init() -> Result<(), SystemError>1561496ba7bSLoGin     pub unsafe fn video_init() -> Result<(), SystemError> {
157c566df45SLoGin         use crate::{
158c566df45SLoGin             arch::driver::video::arch_video_early_init,
15952da9a59SGnoCiYeH             driver::{
16052da9a59SGnoCiYeH                 serial::serial8250::send_to_default_serial8250_port,
16152da9a59SGnoCiYeH                 video::fbdev::base::BootTimeVideoType,
16252da9a59SGnoCiYeH             },
1631496ba7bSLoGin         };
1641496ba7bSLoGin 
165c566df45SLoGin         arch_video_early_init()?;
166c566df45SLoGin 
167c566df45SLoGin         let boot_params_guard = boot_params().read();
168c566df45SLoGin         let screen_info = &boot_params_guard.screen_info;
169c566df45SLoGin         let buf_vaddr = screen_info.lfb_virt_base.unwrap();
170c566df45SLoGin 
171c566df45SLoGin         let buf_flag: ScmBufferFlag;
172c566df45SLoGin         let device_buffer: ScmBufferInfo;
173c566df45SLoGin 
174c566df45SLoGin         if screen_info.video_type == BootTimeVideoType::Mda {
175c566df45SLoGin             buf_flag = ScmBufferFlag::SCM_BF_TEXT | ScmBufferFlag::SCM_BF_FB;
176c566df45SLoGin             device_buffer = ScmBufferInfo::new_device_buffer(
177c566df45SLoGin                 screen_info.origin_video_cols.into(),
178c566df45SLoGin                 screen_info.origin_video_lines.into(),
179c566df45SLoGin                 screen_info.lfb_size as u32,
180c566df45SLoGin                 screen_info.lfb_depth.into(),
181c566df45SLoGin                 buf_flag,
1821496ba7bSLoGin                 buf_vaddr,
183*2b7818e8SLoGin                 None,
1841496ba7bSLoGin             )
1851496ba7bSLoGin             .unwrap();
186c566df45SLoGin         } else {
187c566df45SLoGin             // 图形模式
188c566df45SLoGin             buf_flag = ScmBufferFlag::SCM_BF_PIXEL | ScmBufferFlag::SCM_BF_FB;
189c566df45SLoGin             device_buffer = ScmBufferInfo::new_device_buffer(
190c566df45SLoGin                 screen_info.lfb_width,
191c566df45SLoGin                 screen_info.lfb_height,
192c566df45SLoGin                 screen_info.lfb_size as u32,
193c566df45SLoGin                 screen_info.lfb_depth.into(),
194c566df45SLoGin                 buf_flag,
195c566df45SLoGin                 buf_vaddr,
196*2b7818e8SLoGin                 None,
197c566df45SLoGin             )
198c566df45SLoGin             .unwrap();
199c566df45SLoGin         }
2001496ba7bSLoGin 
2011496ba7bSLoGin         let result = Self {
2021496ba7bSLoGin             device_buffer: RwLock::new(device_buffer),
2031496ba7bSLoGin             refresh_target: RwLock::new(None),
2041496ba7bSLoGin             running: AtomicBool::new(false),
2051496ba7bSLoGin         };
2061496ba7bSLoGin 
2071496ba7bSLoGin         __MAMAGER = Some(result);
2081496ba7bSLoGin 
2091496ba7bSLoGin         let init_text = "Video driver initialized.\n\0";
210a03c4f9dSLoGin         send_to_default_serial8250_port(init_text.as_bytes());
2111496ba7bSLoGin         return Ok(());
2121496ba7bSLoGin     }
2131496ba7bSLoGin }
2141496ba7bSLoGin 
2151496ba7bSLoGin //刷新任务执行器
2161496ba7bSLoGin #[derive(Debug)]
2171496ba7bSLoGin struct VideoRefreshExecutor;
2181496ba7bSLoGin 
2191496ba7bSLoGin impl VideoRefreshExecutor {
new() -> Box<VideoRefreshExecutor>2201496ba7bSLoGin     fn new() -> Box<VideoRefreshExecutor> {
2211496ba7bSLoGin         return Box::new(VideoRefreshExecutor);
2221496ba7bSLoGin     }
2231496ba7bSLoGin }
2241496ba7bSLoGin 
2251496ba7bSLoGin impl TimerFunction for VideoRefreshExecutor {
2261496ba7bSLoGin     /**
2271496ba7bSLoGin      * @brief 交给定时器执行的任务,此方法不应手动调用
2281496ba7bSLoGin      * @return Ok(())
2291496ba7bSLoGin      */
230b5b571e0SLoGin     #[allow(clippy::type_complexity)]
run(&mut self) -> Result<(), SystemError>2311496ba7bSLoGin     fn run(&mut self) -> Result<(), SystemError> {
2321496ba7bSLoGin         // 获得Manager
2331496ba7bSLoGin         let manager = video_refresh_manager();
2341496ba7bSLoGin 
2351496ba7bSLoGin         let start_next_refresh = || {
2361496ba7bSLoGin             //判断是否还需要刷新,若需要则继续分配下一次计时任务,否则不分配
2371496ba7bSLoGin             if manager.running.load(Ordering::SeqCst) {
2381496ba7bSLoGin                 let timer = Timer::new(VideoRefreshExecutor::new(), REFRESH_INTERVAL);
2391496ba7bSLoGin                 //将新一次定时任务加入队列
2401496ba7bSLoGin                 timer.activate();
2411496ba7bSLoGin             }
2421496ba7bSLoGin         };
2431496ba7bSLoGin 
2442755467cS曾俊         let mut refresh_target: Option<RwLockReadGuard<'_, Option<Arc<SpinLock<Box<[u8]>>>>>> =
2451496ba7bSLoGin             None;
2461496ba7bSLoGin         const TRY_TIMES: i32 = 2;
2471496ba7bSLoGin         for i in 0..TRY_TIMES {
2481496ba7bSLoGin             let g = manager.refresh_target.try_read();
2491496ba7bSLoGin             if g.is_none() {
2501496ba7bSLoGin                 if i == TRY_TIMES - 1 {
2511496ba7bSLoGin                     start_next_refresh();
2521496ba7bSLoGin                     return Ok(());
2531496ba7bSLoGin                 }
2541496ba7bSLoGin                 continue;
2551496ba7bSLoGin             }
2561496ba7bSLoGin             refresh_target = Some(g.unwrap());
2571496ba7bSLoGin             break;
2581496ba7bSLoGin         }
2591496ba7bSLoGin 
2601496ba7bSLoGin         let refresh_target = refresh_target.unwrap();
2611496ba7bSLoGin 
2621496ba7bSLoGin         if let ScmBuffer::DeviceBuffer(vaddr) = manager.device_buffer().buf {
263b5b571e0SLoGin             let p: *mut u8 = vaddr.as_ptr();
2641496ba7bSLoGin             let mut target_guard = None;
2651496ba7bSLoGin             for _ in 0..2 {
2661496ba7bSLoGin                 if let Ok(guard) = refresh_target.as_ref().unwrap().try_lock_irqsave() {
2671496ba7bSLoGin                     target_guard = Some(guard);
2681496ba7bSLoGin                     break;
2691496ba7bSLoGin                 }
2701496ba7bSLoGin             }
2711496ba7bSLoGin             if target_guard.is_none() {
2721496ba7bSLoGin                 start_next_refresh();
2731496ba7bSLoGin                 return Ok(());
2741496ba7bSLoGin             }
2751496ba7bSLoGin             let mut target_guard = target_guard.unwrap();
2761496ba7bSLoGin             unsafe {
2771496ba7bSLoGin                 p.copy_from_nonoverlapping(
2782755467cS曾俊                     target_guard.as_mut_ptr(),
2791496ba7bSLoGin                     manager.device_buffer().buf_size() as usize,
2801496ba7bSLoGin                 )
2811496ba7bSLoGin             }
2821496ba7bSLoGin         }
2831496ba7bSLoGin 
2841496ba7bSLoGin         start_next_refresh();
2851496ba7bSLoGin 
2861496ba7bSLoGin         return Ok(());
2871496ba7bSLoGin     }
2881496ba7bSLoGin }
2891496ba7bSLoGin 
290bacd691cSlogin #[no_mangle]
rs_video_init() -> i322911496ba7bSLoGin pub unsafe extern "C" fn rs_video_init() -> i32 {
2921496ba7bSLoGin     return VideoRefreshManager::video_init()
2931496ba7bSLoGin         .map(|_| 0)
2941496ba7bSLoGin         .unwrap_or_else(|e| e.to_posix_errno());
295bacd691cSlogin }
296