xref: /DragonOS/kernel/src/driver/video/mod.rs (revision 1ea2daad8121b77ed704e6d7c3a09f478147441d)
1 use core::sync::atomic::{AtomicBool, Ordering};
2 
3 use crate::{
4     arch::MMArch,
5     init::boot_params,
6     libs::{
7         align::page_align_up,
8         lib_ui::screen_manager::{ScmBuffer, ScmBufferFlag, ScmBufferInfo},
9         rwlock::{RwLock, RwLockReadGuard},
10         spinlock::SpinLock,
11     },
12     mm::{
13         allocator::page_frame::PageFrameCount, kernel_mapper::KernelMapper, page::PageFlags,
14         MemoryManagementArch,
15     },
16     time::timer::{Timer, TimerFunction},
17 };
18 use alloc::{boxed::Box, sync::Arc};
19 use log::info;
20 use system_error::SystemError;
21 
22 pub mod console;
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 #[allow(clippy::type_complexity)]
35 ///管理显示刷新变量的结构体
36 pub struct VideoRefreshManager {
37     device_buffer: RwLock<ScmBufferInfo>,
38     refresh_target: RwLock<Option<Arc<SpinLock<Box<[u8]>>>>>,
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         return res.is_ok();
74     }
75 
76     /**
77      * @brief VBE帧缓存区的地址重新映射
78      * 将帧缓存区映射到地址SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE处
79      */
80     fn init_frame_buffer(&self) {
81         info!("Re-mapping VBE frame buffer...");
82         let buf_vaddr = boot_params()
83             .read_irqsave()
84             .screen_info
85             .lfb_virt_base
86             .unwrap();
87 
88         let mut frame_buffer_info_guard = self.device_buffer.write();
89         if let ScmBuffer::DeviceBuffer(vaddr) = &mut (frame_buffer_info_guard).buf {
90             *vaddr = buf_vaddr;
91         }
92 
93         // 地址映射
94         let mut paddr = boot_params().read().screen_info.lfb_base;
95         let count = PageFrameCount::new(
96             page_align_up(frame_buffer_info_guard.buf_size()) / MMArch::PAGE_SIZE,
97         );
98         let page_flags: PageFlags<MMArch> = PageFlags::new().set_execute(true).set_write(true);
99 
100         let mut kernel_mapper = KernelMapper::lock();
101         let mut kernel_mapper = kernel_mapper.as_mut();
102         assert!(kernel_mapper.is_some());
103         let mut vaddr = buf_vaddr;
104         unsafe {
105             for _ in 0..count.data() {
106                 let flusher = kernel_mapper
107                     .as_mut()
108                     .unwrap()
109                     .map_phys(vaddr, paddr, page_flags)
110                     .unwrap();
111 
112                 flusher.flush();
113                 vaddr += MMArch::PAGE_SIZE;
114                 paddr += MMArch::PAGE_SIZE;
115             }
116         }
117 
118         info!("VBE frame buffer successfully Re-mapped!");
119     }
120 
121     /**
122      * @brief 初始化显示模块,需先低级初始化才能高级初始化
123      * @param level 初始化等级
124      * false -> 低级初始化:不使用double buffer
125      * true ->高级初始化:增加double buffer的支持
126      * @return int
127      */
128     pub fn video_reinitialize(&self, level: bool) -> Result<(), SystemError> {
129         if !level {
130             self.init_frame_buffer();
131         } else {
132             // 开启屏幕计时刷新
133             assert!(self.run_video_refresh());
134         }
135         return Ok(());
136     }
137 
138     /**
139      * @brief 设置帧缓冲区刷新目标
140      *
141      * @param buf
142      * @return int
143      */
144     pub fn set_refresh_target(&self, buf_info: &ScmBufferInfo) -> Result<(), SystemError> {
145         let mut refresh_target = self.refresh_target.write_irqsave();
146         if let ScmBuffer::DoubleBuffer(double_buffer) = &buf_info.buf {
147             *refresh_target = Some(double_buffer.clone());
148             return Ok(());
149         }
150         return Err(SystemError::EINVAL);
151     }
152     #[allow(clippy::type_complexity)]
153     #[allow(dead_code)]
154     pub fn refresh_target(&self) -> RwLockReadGuard<'_, Option<Arc<SpinLock<Box<[u8]>>>>> {
155         let x = self.refresh_target.read();
156 
157         return x;
158     }
159 
160     pub fn device_buffer(&self) -> RwLockReadGuard<'_, ScmBufferInfo> {
161         return self.device_buffer.read();
162     }
163 
164     /// 在riscv64平台下暂时不支持
165     #[cfg(target_arch = "riscv64")]
166     pub unsafe fn video_init() -> Result<(), SystemError> {
167         return Err(SystemError::ENOSYS);
168     }
169 
170     /// 此函数用于初始化显示驱动,为后续的图形输出做好准备。
171     #[cfg(target_arch = "x86_64")]
172     pub unsafe fn video_init() -> Result<(), SystemError> {
173         use crate::{
174             arch::driver::video::arch_video_early_init,
175             driver::{
176                 serial::serial8250::send_to_default_serial8250_port,
177                 video::fbdev::base::BootTimeVideoType,
178             },
179         };
180 
181         arch_video_early_init()?;
182 
183         let boot_params_guard = boot_params().read();
184         let screen_info = &boot_params_guard.screen_info;
185         let buf_vaddr = screen_info.lfb_virt_base.unwrap();
186 
187         let buf_flag: ScmBufferFlag;
188         let device_buffer: ScmBufferInfo;
189 
190         if screen_info.video_type == BootTimeVideoType::Mda {
191             buf_flag = ScmBufferFlag::SCM_BF_TEXT | ScmBufferFlag::SCM_BF_FB;
192             device_buffer = ScmBufferInfo::new_device_buffer(
193                 screen_info.origin_video_cols.into(),
194                 screen_info.origin_video_lines.into(),
195                 screen_info.lfb_size as u32,
196                 screen_info.lfb_depth.into(),
197                 buf_flag,
198                 buf_vaddr,
199             )
200             .unwrap();
201         } else {
202             // 图形模式
203             buf_flag = ScmBufferFlag::SCM_BF_PIXEL | ScmBufferFlag::SCM_BF_FB;
204             device_buffer = ScmBufferInfo::new_device_buffer(
205                 screen_info.lfb_width,
206                 screen_info.lfb_height,
207                 screen_info.lfb_size as u32,
208                 screen_info.lfb_depth.into(),
209                 buf_flag,
210                 buf_vaddr,
211             )
212             .unwrap();
213         }
214 
215         let result = Self {
216             device_buffer: RwLock::new(device_buffer),
217             refresh_target: RwLock::new(None),
218             running: AtomicBool::new(false),
219         };
220 
221         __MAMAGER = Some(result);
222 
223         let init_text = "Video driver initialized.\n\0";
224         send_to_default_serial8250_port(init_text.as_bytes());
225         return Ok(());
226     }
227 }
228 
229 //刷新任务执行器
230 #[derive(Debug)]
231 struct VideoRefreshExecutor;
232 
233 impl VideoRefreshExecutor {
234     fn new() -> Box<VideoRefreshExecutor> {
235         return Box::new(VideoRefreshExecutor);
236     }
237 }
238 
239 impl TimerFunction for VideoRefreshExecutor {
240     /**
241      * @brief 交给定时器执行的任务,此方法不应手动调用
242      * @return Ok(())
243      */
244     #[allow(clippy::type_complexity)]
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<[u8]>>>>>> =
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: *mut u8 = vaddr.as_ptr();
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(),
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