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