xref: /DragonOS/kernel/src/driver/video/mod.rs (revision 45626c859f95054b76d8b59afcbd24c6b235026f)
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     include::bindings::bindings::{
7         FRAME_BUFFER_MAPPING_OFFSET, SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE,
8     },
9     init::boot_params,
10     kinfo,
11     libs::{
12         align::page_align_up,
13         lib_ui::screen_manager::{ScmBuffer, ScmBufferFlag, ScmBufferInfo},
14         rwlock::{RwLock, RwLockReadGuard},
15         spinlock::SpinLock,
16     },
17     mm::{
18         allocator::page_frame::PageFrameCount, kernel_mapper::KernelMapper, page::PageFlags,
19         MemoryManagementArch, VirtAddr,
20     },
21     time::timer::{Timer, TimerFunction},
22 };
23 use alloc::{boxed::Box, sync::Arc};
24 use system_error::SystemError;
25 
26 pub mod fbdev;
27 
28 static mut __MAMAGER: Option<VideoRefreshManager> = None;
29 
30 pub fn video_refresh_manager() -> &'static VideoRefreshManager {
31     return unsafe {
32         &__MAMAGER
33             .as_ref()
34             .expect("Video refresh manager has not been initialized yet!")
35     };
36 }
37 
38 ///管理显示刷新变量的结构体
39 pub struct VideoRefreshManager {
40     device_buffer: RwLock<ScmBufferInfo>,
41     refresh_target: RwLock<Option<Arc<SpinLock<Box<[u32]>>>>>,
42     running: AtomicBool,
43 }
44 
45 const REFRESH_INTERVAL: u64 = 30;
46 
47 impl VideoRefreshManager {
48     /**
49      * @brief 启动定时刷新
50      * @return 启动成功: true, 失败: false
51      */
52     pub fn run_video_refresh(&self) -> bool {
53         //设置Manager运行标志
54         let res = self.set_run();
55 
56         //设置成功则开始任务,否则直接返回false
57         if res {
58             //第一次将expire_jiffies设置小一点,使得这次刷新尽快开始,后续的刷新将按照REFRESH_INTERVAL间隔进行
59             let timer = Timer::new(VideoRefreshExecutor::new(), 1);
60             //将新一次定时任务加入队列
61             timer.activate();
62         }
63         return res;
64     }
65 
66     /// 停止定时刷新
67     #[allow(dead_code)]
68     pub fn stop_video_refresh(&self) {
69         self.running.store(false, Ordering::SeqCst);
70     }
71 
72     fn set_run(&self) -> bool {
73         let res = self
74             .running
75             .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst);
76         if res.is_ok() {
77             return true;
78         } else {
79             return false;
80         }
81     }
82 
83     /**
84      * @brief VBE帧缓存区的地址重新映射
85      * 将帧缓存区映射到地址SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE处
86      */
87     fn init_frame_buffer(&self) {
88         kinfo!("Re-mapping VBE frame buffer...");
89         let buf_vaddr = VirtAddr::new(
90             SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE as usize + FRAME_BUFFER_MAPPING_OFFSET as usize,
91         );
92         boot_params().write_irqsave().screen_info.lfb_virt_base = Some(buf_vaddr);
93 
94         let mut frame_buffer_info_guard = self.device_buffer.write();
95         if let ScmBuffer::DeviceBuffer(vaddr) = &mut (frame_buffer_info_guard).buf {
96             *vaddr = buf_vaddr;
97         }
98 
99         // 地址映射
100         let mut paddr = boot_params().read().screen_info.lfb_base;
101         let count = PageFrameCount::new(
102             page_align_up(frame_buffer_info_guard.buf_size()) / MMArch::PAGE_SIZE,
103         );
104         let page_flags: PageFlags<MMArch> = PageFlags::new().set_execute(true).set_write(true);
105 
106         let mut kernel_mapper = KernelMapper::lock();
107         let mut kernel_mapper = kernel_mapper.as_mut();
108         assert!(kernel_mapper.is_some());
109         let mut vaddr = buf_vaddr;
110         unsafe {
111             for _ in 0..count.data() {
112                 let flusher = kernel_mapper
113                     .as_mut()
114                     .unwrap()
115                     .map_phys(vaddr, paddr, page_flags)
116                     .unwrap();
117 
118                 flusher.flush();
119                 vaddr += MMArch::PAGE_SIZE;
120                 paddr += MMArch::PAGE_SIZE;
121             }
122         }
123 
124         kinfo!("VBE frame buffer successfully Re-mapped!");
125     }
126 
127     /**
128      * @brief 初始化显示模块,需先低级初始化才能高级初始化
129      * @param level 初始化等级
130      * false -> 低级初始化:不使用double buffer
131      * true ->高级初始化:增加double buffer的支持
132      * @return int
133      */
134     pub fn video_reinitialize(&self, level: bool) -> Result<(), SystemError> {
135         if !level {
136             self.init_frame_buffer();
137         } else {
138             // 开启屏幕计时刷新
139             assert!(self.run_video_refresh());
140         }
141         return Ok(());
142     }
143 
144     /**
145      * @brief 设置帧缓冲区刷新目标
146      *
147      * @param buf
148      * @return int
149      */
150     pub fn set_refresh_target(&self, buf_info: &ScmBufferInfo) -> Result<(), SystemError> {
151         let mut refresh_target = self.refresh_target.write_irqsave();
152         if let ScmBuffer::DoubleBuffer(double_buffer) = &buf_info.buf {
153             *refresh_target = Some(double_buffer.clone());
154             return Ok(());
155         }
156         return Err(SystemError::EINVAL);
157     }
158 
159     #[allow(dead_code)]
160     pub fn refresh_target(&self) -> RwLockReadGuard<'_, Option<Arc<SpinLock<Box<[u32]>>>>> {
161         let x = self.refresh_target.read();
162 
163         return x;
164     }
165 
166     pub fn device_buffer(&self) -> RwLockReadGuard<'_, ScmBufferInfo> {
167         return self.device_buffer.read();
168     }
169 
170     /// 在riscv64平台下暂时不支持
171     #[cfg(target_arch = "riscv64")]
172     pub unsafe fn video_init() -> Result<(), SystemError> {
173         return Err(SystemError::ENOSYS);
174     }
175 
176     /// 此函数用于初始化显示驱动,为后续的图形输出做好准备。
177     #[cfg(target_arch = "x86_64")]
178     pub unsafe fn video_init() -> Result<(), SystemError> {
179         use crate::{
180             arch::driver::video::arch_video_early_init,
181             driver::video::fbdev::base::BootTimeVideoType,
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