xref: /DragonOS/kernel/src/driver/video/fbdev/vesafb.rs (revision 4256da7fb6ad25a3caab6f656607aaf047cb6446)
1c566df45SLoGin use core::{
2c566df45SLoGin     ffi::{c_uint, c_void},
3c566df45SLoGin     mem::MaybeUninit,
4c566df45SLoGin     sync::atomic::AtomicBool,
5c566df45SLoGin };
6c566df45SLoGin 
7c566df45SLoGin use alloc::{
8c566df45SLoGin     string::{String, ToString},
9c566df45SLoGin     sync::{Arc, Weak},
10c566df45SLoGin     vec::Vec,
11c566df45SLoGin };
12c566df45SLoGin use system_error::SystemError;
13c566df45SLoGin use unified_init::macros::unified_init;
14c566df45SLoGin 
15c566df45SLoGin use crate::{
16c566df45SLoGin     arch::MMArch,
17c566df45SLoGin     driver::{
18c566df45SLoGin         base::{
19c566df45SLoGin             class::Class,
20c566df45SLoGin             device::{
21c566df45SLoGin                 bus::Bus, device_manager, driver::Driver, Device, DeviceState, DeviceType, IdTable,
22c566df45SLoGin             },
23c566df45SLoGin             kobject::{KObjType, KObject, KObjectState, LockedKObjectState},
24c566df45SLoGin             kset::KSet,
25c566df45SLoGin             platform::{
26c566df45SLoGin                 platform_device::{platform_device_manager, PlatformDevice},
27c566df45SLoGin                 platform_driver::{platform_driver_manager, PlatformDriver},
28c566df45SLoGin                 CompatibleTable,
29c566df45SLoGin             },
30c566df45SLoGin         },
3152da9a59SGnoCiYeH         serial::serial8250::send_to_default_serial8250_port,
3252da9a59SGnoCiYeH         video::fbdev::base::{fbmem::frame_buffer_manager, FbVisual, FRAME_BUFFER_SET},
33c566df45SLoGin     },
34c566df45SLoGin     filesystem::{
35c566df45SLoGin         kernfs::KernFSInode,
36c566df45SLoGin         sysfs::{file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport},
37c566df45SLoGin         vfs::syscall::ModeType,
38c566df45SLoGin     },
39c566df45SLoGin     include::bindings::bindings::{
40c566df45SLoGin         multiboot2_get_Framebuffer_info, multiboot2_iter, multiboot_tag_framebuffer_info_t,
41c75ef4e2SLoGin         FRAME_BUFFER_MAPPING_OFFSET,
42c566df45SLoGin     },
43c566df45SLoGin     init::{boot_params, initcall::INITCALL_DEVICE},
44c566df45SLoGin     libs::{
45c566df45SLoGin         align::page_align_up,
46c566df45SLoGin         once::Once,
47c566df45SLoGin         rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard},
48c566df45SLoGin         spinlock::SpinLock,
49c566df45SLoGin     },
50c566df45SLoGin     mm::{
51c566df45SLoGin         allocator::page_frame::PageFrameCount, no_init::pseudo_map_phys, MemoryManagementArch,
52c566df45SLoGin         PhysAddr, VirtAddr,
53c566df45SLoGin     },
54c566df45SLoGin };
55c566df45SLoGin 
56c566df45SLoGin use super::base::{
57c566df45SLoGin     fbmem::FbDevice, BlankMode, BootTimeVideoType, FbAccel, FbActivateFlags, FbId, FbState, FbType,
58c566df45SLoGin     FbVModeFlags, FbVarScreenInfo, FbVideoMode, FixedScreenInfo, FrameBuffer, FrameBufferInfo,
5952da9a59SGnoCiYeH     FrameBufferInfoData, FrameBufferOps,
60c566df45SLoGin };
61c566df45SLoGin 
62c566df45SLoGin /// 当前机器上面是否有vesa帧缓冲区
63c566df45SLoGin static HAS_VESA_FB: AtomicBool = AtomicBool::new(false);
64c566df45SLoGin 
65c566df45SLoGin lazy_static! {
66c566df45SLoGin     static ref VESAFB_FIX_INFO: RwLock<FixedScreenInfo> = RwLock::new(FixedScreenInfo {
67c566df45SLoGin         id: FixedScreenInfo::name2id("VESA VGA"),
68c566df45SLoGin         fb_type: FbType::PackedPixels,
69c566df45SLoGin         accel: FbAccel::None,
70c566df45SLoGin         ..Default::default()
71c566df45SLoGin     });
72c566df45SLoGin     static ref VESAFB_DEFINED: RwLock<FbVarScreenInfo> = RwLock::new(FbVarScreenInfo {
73c566df45SLoGin         activate: FbActivateFlags::FB_ACTIVATE_NOW,
74c566df45SLoGin         height: None,
75c566df45SLoGin         width: None,
76c566df45SLoGin         right_margin: 32,
77c566df45SLoGin         upper_margin: 16,
78c566df45SLoGin         lower_margin: 4,
79c566df45SLoGin         vsync_len: 4,
80c566df45SLoGin         vmode: FbVModeFlags::FB_VMODE_NONINTERLACED,
81c566df45SLoGin 
82c566df45SLoGin         ..Default::default()
83c566df45SLoGin     });
84c566df45SLoGin }
85c566df45SLoGin 
86c566df45SLoGin #[derive(Debug)]
87c566df45SLoGin #[cast_to([sync] Device)]
88c566df45SLoGin #[cast_to([sync] PlatformDevice)]
89c566df45SLoGin pub struct VesaFb {
90c566df45SLoGin     inner: SpinLock<InnerVesaFb>,
91c566df45SLoGin     kobj_state: LockedKObjectState,
9252da9a59SGnoCiYeH     fb_data: RwLock<FrameBufferInfoData>,
93c566df45SLoGin }
94c566df45SLoGin 
95c566df45SLoGin impl VesaFb {
96c566df45SLoGin     pub const NAME: &'static str = "vesa_vga";
97c566df45SLoGin     pub fn new() -> Self {
9852da9a59SGnoCiYeH         let mut fb_info_data = FrameBufferInfoData::new();
9952da9a59SGnoCiYeH         fb_info_data.pesudo_palette.resize(256, 0);
100c566df45SLoGin         return Self {
101c566df45SLoGin             inner: SpinLock::new(InnerVesaFb {
102c566df45SLoGin                 bus: None,
103c566df45SLoGin                 class: None,
104c566df45SLoGin                 driver: None,
105c566df45SLoGin                 kern_inode: None,
106c566df45SLoGin                 parent: None,
107c566df45SLoGin                 kset: None,
108c566df45SLoGin                 kobj_type: None,
109c566df45SLoGin                 device_state: DeviceState::NotInitialized,
110c566df45SLoGin                 pdev_id: 0,
111c566df45SLoGin                 pdev_id_auto: false,
112c566df45SLoGin                 fb_id: FbId::INIT,
113c566df45SLoGin                 fb_device: None,
114c566df45SLoGin                 fb_state: FbState::Suspended,
115c566df45SLoGin             }),
116c566df45SLoGin             kobj_state: LockedKObjectState::new(None),
11752da9a59SGnoCiYeH             fb_data: RwLock::new(fb_info_data),
118c566df45SLoGin         };
119c566df45SLoGin     }
120c566df45SLoGin }
121c566df45SLoGin 
122c566df45SLoGin #[derive(Debug)]
123c566df45SLoGin struct InnerVesaFb {
124c566df45SLoGin     bus: Option<Weak<dyn Bus>>,
125*4256da7fSLoGin     class: Option<Weak<dyn Class>>,
126c566df45SLoGin     driver: Option<Weak<dyn Driver>>,
127c566df45SLoGin     kern_inode: Option<Arc<KernFSInode>>,
128c566df45SLoGin     parent: Option<Weak<dyn KObject>>,
129c566df45SLoGin     kset: Option<Arc<KSet>>,
130c566df45SLoGin     kobj_type: Option<&'static dyn KObjType>,
131c566df45SLoGin     device_state: DeviceState,
132c566df45SLoGin     pdev_id: i32,
133c566df45SLoGin     pdev_id_auto: bool,
134c566df45SLoGin     fb_id: FbId,
135c566df45SLoGin     fb_device: Option<Arc<FbDevice>>,
136c566df45SLoGin     fb_state: FbState,
137c566df45SLoGin }
138c566df45SLoGin 
139c566df45SLoGin impl FrameBuffer for VesaFb {
140c566df45SLoGin     fn fb_id(&self) -> FbId {
141c566df45SLoGin         self.inner.lock().fb_id
142c566df45SLoGin     }
143c566df45SLoGin 
144c566df45SLoGin     fn set_fb_id(&self, id: FbId) {
145c566df45SLoGin         self.inner.lock().fb_id = id;
146c566df45SLoGin     }
147c566df45SLoGin }
148c566df45SLoGin 
149c566df45SLoGin impl PlatformDevice for VesaFb {
150c566df45SLoGin     fn pdev_name(&self) -> &str {
151c566df45SLoGin         Self::NAME
152c566df45SLoGin     }
153c566df45SLoGin 
154c566df45SLoGin     fn set_pdev_id(&self, id: i32) {
155c566df45SLoGin         self.inner.lock().pdev_id = id;
156c566df45SLoGin     }
157c566df45SLoGin 
158c566df45SLoGin     fn set_pdev_id_auto(&self, id_auto: bool) {
159c566df45SLoGin         self.inner.lock().pdev_id_auto = id_auto;
160c566df45SLoGin     }
161c566df45SLoGin 
162c566df45SLoGin     fn compatible_table(&self) -> CompatibleTable {
163c566df45SLoGin         todo!()
164c566df45SLoGin     }
165c566df45SLoGin 
166c566df45SLoGin     fn is_initialized(&self) -> bool {
167c566df45SLoGin         self.inner.lock().device_state == DeviceState::Initialized
168c566df45SLoGin     }
169c566df45SLoGin 
170c566df45SLoGin     fn set_state(&self, set_state: DeviceState) {
171c566df45SLoGin         self.inner.lock().device_state = set_state;
172c566df45SLoGin     }
173c566df45SLoGin }
174c566df45SLoGin 
175c566df45SLoGin impl Device for VesaFb {
176c566df45SLoGin     fn dev_type(&self) -> DeviceType {
177c566df45SLoGin         DeviceType::Char
178c566df45SLoGin     }
179c566df45SLoGin 
180c566df45SLoGin     fn id_table(&self) -> IdTable {
181c566df45SLoGin         IdTable::new(self.name(), None)
182c566df45SLoGin     }
183c566df45SLoGin 
184c566df45SLoGin     fn bus(&self) -> Option<Weak<dyn Bus>> {
185c566df45SLoGin         self.inner.lock().bus.clone()
186c566df45SLoGin     }
187c566df45SLoGin 
188c566df45SLoGin     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
189c566df45SLoGin         self.inner.lock().bus = bus;
190c566df45SLoGin     }
191c566df45SLoGin 
192*4256da7fSLoGin     fn set_class(&self, class: Option<Weak<dyn Class>>) {
193c566df45SLoGin         self.inner.lock().class = class;
194c566df45SLoGin     }
195c566df45SLoGin 
196*4256da7fSLoGin     fn class(&self) -> Option<Arc<dyn Class>> {
197*4256da7fSLoGin         let mut guard = self.inner.lock();
198*4256da7fSLoGin 
199*4256da7fSLoGin         let r = guard.class.clone()?.upgrade();
200*4256da7fSLoGin         if r.is_none() {
201*4256da7fSLoGin             // 为了让弱引用失效
202*4256da7fSLoGin             guard.class = None;
203*4256da7fSLoGin         }
204*4256da7fSLoGin 
205*4256da7fSLoGin         return r;
206*4256da7fSLoGin     }
207*4256da7fSLoGin 
208c566df45SLoGin     fn driver(&self) -> Option<Arc<dyn Driver>> {
209c566df45SLoGin         self.inner.lock().driver.clone()?.upgrade()
210c566df45SLoGin     }
211c566df45SLoGin 
212c566df45SLoGin     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
213c566df45SLoGin         self.inner.lock().driver = driver;
214c566df45SLoGin     }
215c566df45SLoGin 
216c566df45SLoGin     fn is_dead(&self) -> bool {
217c566df45SLoGin         false
218c566df45SLoGin     }
219c566df45SLoGin 
220c566df45SLoGin     fn can_match(&self) -> bool {
221c566df45SLoGin         true
222c566df45SLoGin     }
223c566df45SLoGin 
224c566df45SLoGin     fn set_can_match(&self, _can_match: bool) {}
225c566df45SLoGin 
226c566df45SLoGin     fn state_synced(&self) -> bool {
227c566df45SLoGin         true
228c566df45SLoGin     }
229c566df45SLoGin }
230c566df45SLoGin 
231c566df45SLoGin impl KObject for VesaFb {
232c566df45SLoGin     fn as_any_ref(&self) -> &dyn core::any::Any {
233c566df45SLoGin         self
234c566df45SLoGin     }
235c566df45SLoGin 
236c566df45SLoGin     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
237c566df45SLoGin         self.inner.lock().kern_inode = inode;
238c566df45SLoGin     }
239c566df45SLoGin 
240c566df45SLoGin     fn inode(&self) -> Option<Arc<KernFSInode>> {
241c566df45SLoGin         self.inner.lock().kern_inode.clone()
242c566df45SLoGin     }
243c566df45SLoGin 
244c566df45SLoGin     fn parent(&self) -> Option<Weak<dyn KObject>> {
245c566df45SLoGin         self.inner.lock().parent.clone()
246c566df45SLoGin     }
247c566df45SLoGin 
248c566df45SLoGin     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
249c566df45SLoGin         self.inner.lock().parent = parent;
250c566df45SLoGin     }
251c566df45SLoGin 
252c566df45SLoGin     fn kset(&self) -> Option<Arc<KSet>> {
253c566df45SLoGin         self.inner.lock().kset.clone()
254c566df45SLoGin     }
255c566df45SLoGin 
256c566df45SLoGin     fn set_kset(&self, kset: Option<Arc<KSet>>) {
257c566df45SLoGin         self.inner.lock().kset = kset;
258c566df45SLoGin     }
259c566df45SLoGin 
260c566df45SLoGin     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
261c566df45SLoGin         self.inner.lock().kobj_type
262c566df45SLoGin     }
263c566df45SLoGin 
264c566df45SLoGin     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
265c566df45SLoGin         self.inner.lock().kobj_type = ktype;
266c566df45SLoGin     }
267c566df45SLoGin 
268c566df45SLoGin     fn name(&self) -> String {
269c566df45SLoGin         Self::NAME.to_string()
270c566df45SLoGin     }
271c566df45SLoGin 
272c566df45SLoGin     fn set_name(&self, _name: String) {
273c566df45SLoGin         // do nothing
274c566df45SLoGin     }
275c566df45SLoGin 
276c566df45SLoGin     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
277c566df45SLoGin         self.kobj_state.read()
278c566df45SLoGin     }
279c566df45SLoGin 
280c566df45SLoGin     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
281c566df45SLoGin         self.kobj_state.write()
282c566df45SLoGin     }
283c566df45SLoGin 
284c566df45SLoGin     fn set_kobj_state(&self, state: KObjectState) {
285c566df45SLoGin         *self.kobj_state.write() = state;
286c566df45SLoGin     }
287c566df45SLoGin }
288c566df45SLoGin 
289c566df45SLoGin impl FrameBufferOps for VesaFb {
290c566df45SLoGin     fn fb_open(&self, _user: bool) {
291c566df45SLoGin         todo!()
292c566df45SLoGin     }
293c566df45SLoGin 
294c566df45SLoGin     fn fb_release(&self, _user: bool) {
295c566df45SLoGin         todo!()
296c566df45SLoGin     }
297c566df45SLoGin 
298c566df45SLoGin     fn fb_set_color_register(
299c566df45SLoGin         &self,
30052da9a59SGnoCiYeH         regno: u16,
30152da9a59SGnoCiYeH         mut red: u16,
30252da9a59SGnoCiYeH         mut green: u16,
30352da9a59SGnoCiYeH         mut blue: u16,
304c566df45SLoGin     ) -> Result<(), SystemError> {
30552da9a59SGnoCiYeH         let mut fb_data = self.framebuffer_info_data().write();
30652da9a59SGnoCiYeH         let var = self.current_fb_var();
30752da9a59SGnoCiYeH         if regno as usize >= fb_data.pesudo_palette.len() {
30852da9a59SGnoCiYeH             return Err(SystemError::E2BIG);
30952da9a59SGnoCiYeH         }
31052da9a59SGnoCiYeH 
31152da9a59SGnoCiYeH         if var.bits_per_pixel == 8 {
31252da9a59SGnoCiYeH             todo!("vesa_setpalette todo");
31352da9a59SGnoCiYeH         } else if regno < 16 {
31452da9a59SGnoCiYeH             match var.bits_per_pixel {
31552da9a59SGnoCiYeH                 16 => {
31652da9a59SGnoCiYeH                     if var.red.offset == 10 {
31752da9a59SGnoCiYeH                         // RGB 1:5:5:5
31852da9a59SGnoCiYeH                         fb_data.pesudo_palette[regno as usize] = ((red as u32 & 0xf800) >> 1)
31952da9a59SGnoCiYeH                             | ((green as u32 & 0xf800) >> 6)
32052da9a59SGnoCiYeH                             | ((blue as u32 & 0xf800) >> 11);
32152da9a59SGnoCiYeH                     } else {
32252da9a59SGnoCiYeH                         fb_data.pesudo_palette[regno as usize] = (red as u32 & 0xf800)
32352da9a59SGnoCiYeH                             | ((green as u32 & 0xfc00) >> 5)
32452da9a59SGnoCiYeH                             | ((blue as u32 & 0xf800) >> 11);
32552da9a59SGnoCiYeH                     }
32652da9a59SGnoCiYeH                 }
32752da9a59SGnoCiYeH                 24 | 32 => {
32852da9a59SGnoCiYeH                     red >>= 8;
32952da9a59SGnoCiYeH                     green >>= 8;
33052da9a59SGnoCiYeH                     blue >>= 8;
33152da9a59SGnoCiYeH                     fb_data.pesudo_palette[regno as usize] = ((red as u32) << var.red.offset)
33252da9a59SGnoCiYeH                         | ((green as u32) << var.green.offset)
33352da9a59SGnoCiYeH                         | ((blue as u32) << var.blue.offset);
33452da9a59SGnoCiYeH                 }
33552da9a59SGnoCiYeH                 _ => {}
33652da9a59SGnoCiYeH             }
33752da9a59SGnoCiYeH         }
33852da9a59SGnoCiYeH 
33952da9a59SGnoCiYeH         Ok(())
340c566df45SLoGin     }
341c566df45SLoGin 
342c566df45SLoGin     fn fb_blank(&self, _blank_mode: BlankMode) -> Result<(), SystemError> {
343c566df45SLoGin         todo!()
344c566df45SLoGin     }
345c566df45SLoGin 
346c566df45SLoGin     fn fb_destroy(&self) {
347c566df45SLoGin         todo!()
348c566df45SLoGin     }
34902343d0bSLoGin 
35002343d0bSLoGin     fn fb_read(&self, buf: &mut [u8], pos: usize) -> Result<usize, SystemError> {
35102343d0bSLoGin         let bp = boot_params().read();
35202343d0bSLoGin 
35302343d0bSLoGin         let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?;
35402343d0bSLoGin         let size = self.current_fb_fix().smem_len;
35502343d0bSLoGin         drop(bp);
35602343d0bSLoGin         if pos >= size {
35702343d0bSLoGin             return Ok(0);
35802343d0bSLoGin         }
35902343d0bSLoGin 
36002343d0bSLoGin         let pos = pos as i64;
36102343d0bSLoGin         let size = size as i64;
36202343d0bSLoGin 
36302343d0bSLoGin         let len = core::cmp::min(size - pos, buf.len() as i64) as usize;
36402343d0bSLoGin 
36502343d0bSLoGin         let slice = unsafe { core::slice::from_raw_parts(vaddr.as_ptr::<u8>(), size as usize) };
36602343d0bSLoGin         buf[..len].copy_from_slice(&slice[pos as usize..(pos as usize + len)]);
36702343d0bSLoGin 
36802343d0bSLoGin         return Ok(len);
36902343d0bSLoGin     }
37002343d0bSLoGin 
37102343d0bSLoGin     fn fb_write(&self, buf: &[u8], pos: usize) -> Result<usize, SystemError> {
37202343d0bSLoGin         let bp = boot_params().read();
37302343d0bSLoGin 
37402343d0bSLoGin         let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?;
37502343d0bSLoGin         let size = self.current_fb_fix().smem_len;
37602343d0bSLoGin 
37702343d0bSLoGin         if pos >= size {
37802343d0bSLoGin             return Ok(0);
37902343d0bSLoGin         }
38002343d0bSLoGin 
38102343d0bSLoGin         let pos = pos as i64;
38202343d0bSLoGin         let size = size as i64;
38302343d0bSLoGin 
38402343d0bSLoGin         let len = core::cmp::min(size - pos, buf.len() as i64) as usize;
38502343d0bSLoGin 
38602343d0bSLoGin         let slice = unsafe { core::slice::from_raw_parts_mut(vaddr.as_ptr::<u8>(), size as usize) };
38702343d0bSLoGin         slice[pos as usize..(pos as usize + len)].copy_from_slice(&buf[..len]);
38802343d0bSLoGin 
38902343d0bSLoGin         return Ok(len);
39002343d0bSLoGin     }
39152da9a59SGnoCiYeH 
39252da9a59SGnoCiYeH     fn fb_image_blit(&self, image: &super::base::FbImage) {
39352da9a59SGnoCiYeH         self.generic_imageblit(image);
39452da9a59SGnoCiYeH     }
39552da9a59SGnoCiYeH 
39652da9a59SGnoCiYeH     /// ## 填充矩形
39752da9a59SGnoCiYeH     fn fb_fillrect(&self, rect: super::base::FillRectData) -> Result<(), SystemError> {
39852da9a59SGnoCiYeH         // kwarn!("rect {rect:?}");
39952da9a59SGnoCiYeH 
40052da9a59SGnoCiYeH         let boot_param = boot_params().read();
40152da9a59SGnoCiYeH         let screen_base = boot_param
40252da9a59SGnoCiYeH             .screen_info
40352da9a59SGnoCiYeH             .lfb_virt_base
40452da9a59SGnoCiYeH             .ok_or(SystemError::ENODEV)?;
405b5b571e0SLoGin 
406b5b571e0SLoGin         let fg = if self.current_fb_fix().visual == FbVisual::TrueColor
40752da9a59SGnoCiYeH             || self.current_fb_fix().visual == FbVisual::DirectColor
40852da9a59SGnoCiYeH         {
409b5b571e0SLoGin             self.fb_data.read().pesudo_palette[rect.color as usize]
41052da9a59SGnoCiYeH         } else {
411b5b571e0SLoGin             rect.color
412b5b571e0SLoGin         };
41352da9a59SGnoCiYeH 
41452da9a59SGnoCiYeH         let bpp = self.current_fb_var().bits_per_pixel;
41552da9a59SGnoCiYeH         // 每行像素数
41652da9a59SGnoCiYeH         let line_offset = self.current_fb_var().xres;
41752da9a59SGnoCiYeH         match bpp {
41852da9a59SGnoCiYeH             32 => {
41952da9a59SGnoCiYeH                 let base = screen_base.as_ptr::<u32>();
42052da9a59SGnoCiYeH 
42152da9a59SGnoCiYeH                 for y in rect.dy..(rect.dy + rect.height) {
42252da9a59SGnoCiYeH                     for x in rect.dx..(rect.dx + rect.width) {
42352da9a59SGnoCiYeH                         unsafe { *base.add((y * line_offset + x) as usize) = fg };
42452da9a59SGnoCiYeH                     }
42552da9a59SGnoCiYeH                 }
42652da9a59SGnoCiYeH             }
42752da9a59SGnoCiYeH             _ => todo!(),
42852da9a59SGnoCiYeH         }
42952da9a59SGnoCiYeH 
43052da9a59SGnoCiYeH         Ok(())
43152da9a59SGnoCiYeH     }
43252da9a59SGnoCiYeH 
43352bcb59eSGnoCiYeH     #[inline(never)]
43452bcb59eSGnoCiYeH     fn fb_copyarea(&self, data: super::base::CopyAreaData) {
43552da9a59SGnoCiYeH         let bp = boot_params().read();
43652bcb59eSGnoCiYeH         let base = bp.screen_info.lfb_virt_base.unwrap();
43752da9a59SGnoCiYeH         let var = self.current_fb_var();
43852da9a59SGnoCiYeH 
43952bcb59eSGnoCiYeH         // 原区域或者目标区域全在屏幕外,则直接返回
44052bcb59eSGnoCiYeH         if data.sx > var.xres as i32
44152bcb59eSGnoCiYeH             || data.sy > var.yres as i32
44252bcb59eSGnoCiYeH             || data.dx > var.xres as i32
44352bcb59eSGnoCiYeH             || data.dy > var.yres as i32
44452bcb59eSGnoCiYeH             || (data.sx + data.width as i32) < 0
44552bcb59eSGnoCiYeH             || (data.sy + data.height as i32) < 0
44652bcb59eSGnoCiYeH             || (data.dx + data.width as i32) < 0
44752bcb59eSGnoCiYeH             || (data.dy + data.height as i32) < 0
44852da9a59SGnoCiYeH         {
44952bcb59eSGnoCiYeH             return;
45052da9a59SGnoCiYeH         }
45152da9a59SGnoCiYeH 
45252bcb59eSGnoCiYeH         // 求两个矩形可视范围交集
45352bcb59eSGnoCiYeH         let (s_visiable_x, s_w) = if data.sx < 0 {
45452bcb59eSGnoCiYeH             (0, (data.width - ((-data.sx) as u32)).min(var.xres))
45552bcb59eSGnoCiYeH         } else {
45652bcb59eSGnoCiYeH             let w = if data.sx as u32 + data.width > var.xres {
45752bcb59eSGnoCiYeH                 var.xres - data.sx as u32
45852bcb59eSGnoCiYeH             } else {
45952bcb59eSGnoCiYeH                 data.width
46052bcb59eSGnoCiYeH             };
46152bcb59eSGnoCiYeH 
46252bcb59eSGnoCiYeH             (data.sx, w)
46352bcb59eSGnoCiYeH         };
46452bcb59eSGnoCiYeH         let (s_visiable_y, s_h) = if data.sy < 0 {
46552bcb59eSGnoCiYeH             (0, (data.height - ((-data.sy) as u32).min(var.yres)))
46652bcb59eSGnoCiYeH         } else {
46752bcb59eSGnoCiYeH             let h = if data.sy as u32 + data.height > var.yres {
46852bcb59eSGnoCiYeH                 var.yres - data.sy as u32
46952bcb59eSGnoCiYeH             } else {
47052bcb59eSGnoCiYeH                 data.height
47152bcb59eSGnoCiYeH             };
47252bcb59eSGnoCiYeH 
47352bcb59eSGnoCiYeH             (data.sy, h)
47452bcb59eSGnoCiYeH         };
47552bcb59eSGnoCiYeH 
47652bcb59eSGnoCiYeH         let (d_visiable_x, d_w) = if data.dx < 0 {
47752bcb59eSGnoCiYeH             (0, (data.width - ((-data.dx) as u32)).min(var.xres))
47852bcb59eSGnoCiYeH         } else {
47952bcb59eSGnoCiYeH             let w = if data.dx as u32 + data.width > var.xres {
48052bcb59eSGnoCiYeH                 var.xres - data.dx as u32
48152bcb59eSGnoCiYeH             } else {
48252bcb59eSGnoCiYeH                 data.width
48352bcb59eSGnoCiYeH             };
48452bcb59eSGnoCiYeH 
48552bcb59eSGnoCiYeH             (data.dx, w)
48652bcb59eSGnoCiYeH         };
48752bcb59eSGnoCiYeH         let (d_visiable_y, d_h) = if data.dy < 0 {
48852bcb59eSGnoCiYeH             (0, (data.height - ((-data.dy) as u32).min(var.yres)))
48952bcb59eSGnoCiYeH         } else {
49052bcb59eSGnoCiYeH             let h = if data.dy as u32 + data.height > var.yres {
49152bcb59eSGnoCiYeH                 var.yres - data.dy as u32
49252bcb59eSGnoCiYeH             } else {
49352bcb59eSGnoCiYeH                 data.height
49452bcb59eSGnoCiYeH             };
49552bcb59eSGnoCiYeH 
49652bcb59eSGnoCiYeH             (data.dy, h)
49752bcb59eSGnoCiYeH         };
49852bcb59eSGnoCiYeH 
49952bcb59eSGnoCiYeH         // 可视范围无交集
50052bcb59eSGnoCiYeH         if !(d_h + s_h > data.height && s_w + d_w > data.width) {
50152bcb59eSGnoCiYeH             return;
50252bcb59eSGnoCiYeH         }
50352bcb59eSGnoCiYeH 
50452bcb59eSGnoCiYeH         // 可视区域左上角相对于矩形的坐标
50552bcb59eSGnoCiYeH         let s_relative_x = s_visiable_x - data.sx;
50652bcb59eSGnoCiYeH         let s_relative_y = s_visiable_y - data.sy;
50752bcb59eSGnoCiYeH         let d_relative_x = d_visiable_x - data.dx;
50852bcb59eSGnoCiYeH         let d_relative_y = d_visiable_y - data.dy;
50952bcb59eSGnoCiYeH 
51052bcb59eSGnoCiYeH         let visiable_x = s_relative_x.max(d_relative_x);
51152bcb59eSGnoCiYeH         let visiable_y = s_relative_y.max(d_relative_y);
51252bcb59eSGnoCiYeH         let visiable_h = d_h + s_h - data.height;
51352bcb59eSGnoCiYeH         let visiable_w = d_w + s_w - data.width;
51452bcb59eSGnoCiYeH 
51552bcb59eSGnoCiYeH         let s_real_x = (visiable_x + data.sx) as u32;
51652bcb59eSGnoCiYeH         let s_real_y = (visiable_y + data.sy) as u32;
51752bcb59eSGnoCiYeH         let d_real_x = (visiable_x + data.dx) as u32;
51852bcb59eSGnoCiYeH         let d_real_y = (visiable_y + data.dy) as u32;
51952bcb59eSGnoCiYeH 
52052da9a59SGnoCiYeH         let bytes_per_pixel = var.bits_per_pixel >> 3;
52152da9a59SGnoCiYeH         let bytes_per_line = var.xres * bytes_per_pixel;
52252da9a59SGnoCiYeH 
52352bcb59eSGnoCiYeH         let src =
52452bcb59eSGnoCiYeH             base + VirtAddr::new((s_real_y * bytes_per_line + s_real_x * bytes_per_pixel) as usize);
52552da9a59SGnoCiYeH 
52652bcb59eSGnoCiYeH         let dst =
52752bcb59eSGnoCiYeH             base + VirtAddr::new((d_real_y * bytes_per_line + d_real_x * bytes_per_pixel) as usize);
52852da9a59SGnoCiYeH 
52952bcb59eSGnoCiYeH         let size = (visiable_h * visiable_w) as usize;
53052da9a59SGnoCiYeH 
53152da9a59SGnoCiYeH         match bytes_per_pixel {
53252da9a59SGnoCiYeH             4 => {
53352da9a59SGnoCiYeH                 // 32bpp
53452da9a59SGnoCiYeH                 let mut dst = dst.as_ptr::<u32>();
53552da9a59SGnoCiYeH                 let mut src = src.as_ptr::<u32>();
53652bcb59eSGnoCiYeH                 let line_offset = var.xres as usize;
53752da9a59SGnoCiYeH 
53852bcb59eSGnoCiYeH                 if s_real_x > d_real_x {
53952bcb59eSGnoCiYeH                     // 如果src在dst下方,则可以直接拷贝不会出现指针覆盖
54052da9a59SGnoCiYeH                     unsafe {
54152bcb59eSGnoCiYeH                         for _ in 0..visiable_h {
54252bcb59eSGnoCiYeH                             core::ptr::copy(src, dst, visiable_w as usize);
54352bcb59eSGnoCiYeH                             src = src.add(line_offset);
54452bcb59eSGnoCiYeH                             dst = dst.add(visiable_w as usize);
54552da9a59SGnoCiYeH                         }
54652da9a59SGnoCiYeH                     }
54752da9a59SGnoCiYeH                 } else {
548b5b571e0SLoGin                     let mut tmp: Vec<u32> = vec![0; size];
54952bcb59eSGnoCiYeH                     let mut tmp_ptr = tmp.as_mut_ptr();
55052bcb59eSGnoCiYeH 
55152bcb59eSGnoCiYeH                     // 这里是一个可以优化的点,现在为了避免指针拷贝时覆盖,统一先拷贝进入buf再拷贝到dst
55252da9a59SGnoCiYeH                     unsafe {
55352bcb59eSGnoCiYeH                         for _ in 0..visiable_h {
55452bcb59eSGnoCiYeH                             core::ptr::copy(src, tmp_ptr, visiable_w as usize);
55552bcb59eSGnoCiYeH                             src = src.add(line_offset);
55652bcb59eSGnoCiYeH                             tmp_ptr = tmp_ptr.add(visiable_w as usize);
55752bcb59eSGnoCiYeH                         }
55852bcb59eSGnoCiYeH 
55952bcb59eSGnoCiYeH                         tmp_ptr = tmp_ptr.sub(size);
56052bcb59eSGnoCiYeH                         for _ in 0..visiable_h {
56152bcb59eSGnoCiYeH                             core::ptr::copy(tmp_ptr, dst, visiable_w as usize);
56252bcb59eSGnoCiYeH                             dst = dst.add(line_offset);
56352bcb59eSGnoCiYeH                             tmp_ptr = tmp_ptr.add(visiable_w as usize);
56452da9a59SGnoCiYeH                         }
56552da9a59SGnoCiYeH                     }
56652da9a59SGnoCiYeH                 }
56752da9a59SGnoCiYeH             }
56852da9a59SGnoCiYeH             _ => {
56952da9a59SGnoCiYeH                 todo!()
57052da9a59SGnoCiYeH             }
57152da9a59SGnoCiYeH         }
57252da9a59SGnoCiYeH     }
573c566df45SLoGin }
574c566df45SLoGin 
575c566df45SLoGin impl FrameBufferInfo for VesaFb {
576c566df45SLoGin     fn fb_device(&self) -> Option<Arc<FbDevice>> {
577c566df45SLoGin         self.inner.lock().fb_device.clone()
578c566df45SLoGin     }
579c566df45SLoGin 
580c566df45SLoGin     fn set_fb_device(&self, device: Option<Arc<FbDevice>>) {
581c566df45SLoGin         self.inner.lock().fb_device = device;
582c566df45SLoGin     }
583c566df45SLoGin 
584c566df45SLoGin     fn screen_size(&self) -> usize {
585c566df45SLoGin         todo!()
586c566df45SLoGin     }
587c566df45SLoGin 
588c566df45SLoGin     fn current_fb_var(&self) -> FbVarScreenInfo {
589b5b571e0SLoGin         *VESAFB_DEFINED.read()
590c566df45SLoGin     }
591c566df45SLoGin 
592c566df45SLoGin     fn current_fb_fix(&self) -> FixedScreenInfo {
593b5b571e0SLoGin         *VESAFB_FIX_INFO.read()
594c566df45SLoGin     }
595c566df45SLoGin 
596c566df45SLoGin     fn video_mode(&self) -> Option<&FbVideoMode> {
597c566df45SLoGin         todo!()
598c566df45SLoGin     }
599c566df45SLoGin 
600c566df45SLoGin     fn state(&self) -> FbState {
601c566df45SLoGin         self.inner.lock().fb_state
602c566df45SLoGin     }
60352da9a59SGnoCiYeH 
60452da9a59SGnoCiYeH     fn framebuffer_info_data(&self) -> &RwLock<FrameBufferInfoData> {
60552da9a59SGnoCiYeH         &self.fb_data
60652da9a59SGnoCiYeH     }
607c566df45SLoGin }
608c566df45SLoGin 
609c566df45SLoGin #[derive(Debug)]
610c566df45SLoGin #[cast_to([sync] PlatformDriver)]
611c566df45SLoGin struct VesaFbDriver {
612c566df45SLoGin     inner: SpinLock<InnerVesaFbDriver>,
613c566df45SLoGin     kobj_state: LockedKObjectState,
614c566df45SLoGin }
615c566df45SLoGin 
616c566df45SLoGin impl VesaFbDriver {
617c566df45SLoGin     pub fn new() -> Arc<Self> {
618c566df45SLoGin         let r = Arc::new(Self {
619c566df45SLoGin             inner: SpinLock::new(InnerVesaFbDriver {
620c566df45SLoGin                 ktype: None,
621c566df45SLoGin                 kset: None,
622c566df45SLoGin                 parent: None,
623c566df45SLoGin                 kernfs_inode: None,
624c566df45SLoGin                 devices: Vec::new(),
625c566df45SLoGin                 bus: None,
626c566df45SLoGin                 self_ref: Weak::new(),
627c566df45SLoGin             }),
628c566df45SLoGin             kobj_state: LockedKObjectState::new(None),
629c566df45SLoGin         });
630c566df45SLoGin 
631c566df45SLoGin         r.inner.lock().self_ref = Arc::downgrade(&r);
632c566df45SLoGin 
633c566df45SLoGin         return r;
634c566df45SLoGin     }
635c566df45SLoGin }
636c566df45SLoGin 
637c566df45SLoGin #[derive(Debug)]
638c566df45SLoGin struct InnerVesaFbDriver {
639c566df45SLoGin     ktype: Option<&'static dyn KObjType>,
640c566df45SLoGin     kset: Option<Arc<KSet>>,
641c566df45SLoGin     parent: Option<Weak<dyn KObject>>,
642c566df45SLoGin     kernfs_inode: Option<Arc<KernFSInode>>,
643c566df45SLoGin     devices: Vec<Arc<dyn Device>>,
644c566df45SLoGin     bus: Option<Weak<dyn Bus>>,
645c566df45SLoGin 
646c566df45SLoGin     self_ref: Weak<VesaFbDriver>,
647c566df45SLoGin }
648c566df45SLoGin 
649c566df45SLoGin impl VesaFbDriver {
650c566df45SLoGin     const NAME: &'static str = "vesa-framebuffer";
651c566df45SLoGin }
652c566df45SLoGin 
653c566df45SLoGin impl PlatformDriver for VesaFbDriver {
654c566df45SLoGin     fn probe(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
655c566df45SLoGin         let device = device
656c566df45SLoGin             .clone()
657c566df45SLoGin             .arc_any()
658c566df45SLoGin             .downcast::<VesaFb>()
659c566df45SLoGin             .map_err(|_| SystemError::EINVAL)?;
660c566df45SLoGin 
661c566df45SLoGin         device.set_driver(Some(self.inner.lock_irqsave().self_ref.clone()));
662c566df45SLoGin 
663c566df45SLoGin         return Ok(());
664c566df45SLoGin     }
665c566df45SLoGin 
666c566df45SLoGin     fn remove(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
667c566df45SLoGin         todo!()
668c566df45SLoGin     }
669c566df45SLoGin 
670c566df45SLoGin     fn shutdown(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
671c566df45SLoGin         // do nothing
672c566df45SLoGin         return Ok(());
673c566df45SLoGin     }
674c566df45SLoGin 
675c566df45SLoGin     fn suspend(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
676c566df45SLoGin         // do nothing
677c566df45SLoGin         return Ok(());
678c566df45SLoGin     }
679c566df45SLoGin 
680c566df45SLoGin     fn resume(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
681c566df45SLoGin         todo!()
682c566df45SLoGin     }
683c566df45SLoGin }
684c566df45SLoGin 
685c566df45SLoGin impl Driver for VesaFbDriver {
686c566df45SLoGin     fn id_table(&self) -> Option<IdTable> {
687c566df45SLoGin         Some(IdTable::new(VesaFb::NAME.to_string(), None))
688c566df45SLoGin     }
689c566df45SLoGin 
690c566df45SLoGin     fn devices(&self) -> Vec<Arc<dyn Device>> {
691c566df45SLoGin         self.inner.lock().devices.clone()
692c566df45SLoGin     }
693c566df45SLoGin 
694c566df45SLoGin     fn add_device(&self, device: Arc<dyn Device>) {
695c566df45SLoGin         let mut guard = self.inner.lock();
696c566df45SLoGin         // check if the device is already in the list
697c566df45SLoGin         if guard.devices.iter().any(|dev| Arc::ptr_eq(dev, &device)) {
698c566df45SLoGin             return;
699c566df45SLoGin         }
700c566df45SLoGin 
701c566df45SLoGin         guard.devices.push(device);
702c566df45SLoGin     }
703c566df45SLoGin 
704c566df45SLoGin     fn delete_device(&self, device: &Arc<dyn Device>) {
705c566df45SLoGin         let mut guard = self.inner.lock();
706c566df45SLoGin         guard.devices.retain(|dev| !Arc::ptr_eq(dev, device));
707c566df45SLoGin     }
708c566df45SLoGin 
709c566df45SLoGin     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
710c566df45SLoGin         self.inner.lock().bus = bus;
711c566df45SLoGin     }
712c566df45SLoGin 
713c566df45SLoGin     fn bus(&self) -> Option<Weak<dyn Bus>> {
714c566df45SLoGin         self.inner.lock().bus.clone()
715c566df45SLoGin     }
716c566df45SLoGin 
717c566df45SLoGin     fn dev_groups(&self) -> &'static [&'static dyn AttributeGroup] {
718c566df45SLoGin         return &[&VesaFbAnonAttributeGroup];
719c566df45SLoGin     }
720c566df45SLoGin }
721c566df45SLoGin 
722c566df45SLoGin impl KObject for VesaFbDriver {
723c566df45SLoGin     fn as_any_ref(&self) -> &dyn core::any::Any {
724c566df45SLoGin         self
725c566df45SLoGin     }
726c566df45SLoGin 
727c566df45SLoGin     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
728c566df45SLoGin         self.inner.lock().kernfs_inode = inode;
729c566df45SLoGin     }
730c566df45SLoGin 
731c566df45SLoGin     fn inode(&self) -> Option<Arc<KernFSInode>> {
732c566df45SLoGin         self.inner.lock().kernfs_inode.clone()
733c566df45SLoGin     }
734c566df45SLoGin 
735c566df45SLoGin     fn parent(&self) -> Option<Weak<dyn KObject>> {
736c566df45SLoGin         self.inner.lock().parent.clone()
737c566df45SLoGin     }
738c566df45SLoGin 
739c566df45SLoGin     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
740c566df45SLoGin         self.inner.lock().parent = parent;
741c566df45SLoGin     }
742c566df45SLoGin 
743c566df45SLoGin     fn kset(&self) -> Option<Arc<KSet>> {
744c566df45SLoGin         self.inner.lock().kset.clone()
745c566df45SLoGin     }
746c566df45SLoGin 
747c566df45SLoGin     fn set_kset(&self, kset: Option<Arc<KSet>>) {
748c566df45SLoGin         self.inner.lock().kset = kset;
749c566df45SLoGin     }
750c566df45SLoGin 
751c566df45SLoGin     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
752c566df45SLoGin         self.inner.lock().ktype
753c566df45SLoGin     }
754c566df45SLoGin 
755c566df45SLoGin     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
756c566df45SLoGin         self.inner.lock().ktype = ktype;
757c566df45SLoGin     }
758c566df45SLoGin 
759c566df45SLoGin     fn name(&self) -> String {
760c566df45SLoGin         Self::NAME.to_string()
761c566df45SLoGin     }
762c566df45SLoGin 
763c566df45SLoGin     fn set_name(&self, _name: String) {
764c566df45SLoGin         // do nothing
765c566df45SLoGin     }
766c566df45SLoGin 
767c566df45SLoGin     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
768c566df45SLoGin         self.kobj_state.read()
769c566df45SLoGin     }
770c566df45SLoGin 
771c566df45SLoGin     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
772c566df45SLoGin         self.kobj_state.write()
773c566df45SLoGin     }
774c566df45SLoGin 
775c566df45SLoGin     fn set_kobj_state(&self, state: KObjectState) {
776c566df45SLoGin         *self.kobj_state.write() = state;
777c566df45SLoGin     }
778c566df45SLoGin }
779c566df45SLoGin 
780c566df45SLoGin #[derive(Debug)]
781c566df45SLoGin struct VesaFbAnonAttributeGroup;
782c566df45SLoGin 
783c566df45SLoGin impl AttributeGroup for VesaFbAnonAttributeGroup {
784c566df45SLoGin     fn name(&self) -> Option<&str> {
785c566df45SLoGin         None
786c566df45SLoGin     }
787c566df45SLoGin 
788c566df45SLoGin     fn attrs(&self) -> &[&'static dyn Attribute] {
789c566df45SLoGin         &[&AnonAttrPhysAddr as &'static dyn Attribute]
790c566df45SLoGin     }
791c566df45SLoGin 
792c566df45SLoGin     fn is_visible(
793c566df45SLoGin         &self,
794c566df45SLoGin         _kobj: Arc<dyn KObject>,
795c566df45SLoGin         attr: &'static dyn Attribute,
796c566df45SLoGin     ) -> Option<ModeType> {
797c566df45SLoGin         Some(attr.mode())
798c566df45SLoGin     }
799c566df45SLoGin }
800c566df45SLoGin 
801c566df45SLoGin #[derive(Debug)]
802c566df45SLoGin struct AnonAttrPhysAddr;
803c566df45SLoGin 
804c566df45SLoGin impl Attribute for AnonAttrPhysAddr {
805c566df45SLoGin     fn name(&self) -> &str {
806c566df45SLoGin         "smem_start"
807c566df45SLoGin     }
808c566df45SLoGin 
809c566df45SLoGin     fn mode(&self) -> ModeType {
810c566df45SLoGin         ModeType::S_IRUGO
811c566df45SLoGin     }
812c566df45SLoGin 
813c566df45SLoGin     fn support(&self) -> SysFSOpsSupport {
814196b75dcSLoGin         SysFSOpsSupport::ATTR_SHOW
815c566df45SLoGin     }
816c566df45SLoGin 
817c566df45SLoGin     fn show(&self, _kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> {
818c566df45SLoGin         sysfs_emit_str(
819c566df45SLoGin             buf,
820c566df45SLoGin             format!(
821c566df45SLoGin                 "0x{:x}\n",
822c566df45SLoGin                 VESAFB_FIX_INFO
823c566df45SLoGin                     .read()
824c566df45SLoGin                     .smem_start
825c566df45SLoGin                     .unwrap_or(PhysAddr::new(0))
826c566df45SLoGin                     .data()
827c566df45SLoGin             )
828c566df45SLoGin             .as_str(),
829c566df45SLoGin         )
830c566df45SLoGin     }
831c566df45SLoGin }
832c566df45SLoGin 
833c566df45SLoGin #[unified_init(INITCALL_DEVICE)]
834c566df45SLoGin pub fn vesa_fb_driver_init() -> Result<(), SystemError> {
835c566df45SLoGin     let driver = VesaFbDriver::new();
836c566df45SLoGin 
837c566df45SLoGin     platform_driver_manager().register(driver)?;
838c566df45SLoGin 
839c566df45SLoGin     return Ok(());
840c566df45SLoGin }
841c566df45SLoGin 
842c566df45SLoGin /// 在内存管理初始化之前,初始化vesafb
843c566df45SLoGin pub fn vesafb_early_init() -> Result<VirtAddr, SystemError> {
844c566df45SLoGin     let mut _reserved: u32 = 0;
845c566df45SLoGin 
846c566df45SLoGin     let mut fb_info: MaybeUninit<multiboot_tag_framebuffer_info_t> = MaybeUninit::uninit();
847c566df45SLoGin     //从multiboot2中读取帧缓冲区信息至fb_info
848c566df45SLoGin 
849c566df45SLoGin     // todo: 换成rust的,并且检测是否成功获取
850c566df45SLoGin     unsafe {
851c566df45SLoGin         multiboot2_iter(
852c566df45SLoGin             Some(multiboot2_get_Framebuffer_info),
853c566df45SLoGin             fb_info.as_mut_ptr() as usize as *mut c_void,
854c566df45SLoGin             &mut _reserved as *mut c_uint,
855c566df45SLoGin         )
856c566df45SLoGin     };
857c566df45SLoGin     unsafe { fb_info.assume_init() };
858c566df45SLoGin     let fb_info: multiboot_tag_framebuffer_info_t = unsafe { core::mem::transmute(fb_info) };
859c566df45SLoGin 
860c566df45SLoGin     // todo: 判断是否有vesa帧缓冲区,这里暂时直接设置true
861c566df45SLoGin     HAS_VESA_FB.store(true, core::sync::atomic::Ordering::SeqCst);
862c566df45SLoGin 
863c566df45SLoGin     let width = fb_info.framebuffer_width;
864c566df45SLoGin     let height = fb_info.framebuffer_height;
865c566df45SLoGin 
866c566df45SLoGin     let mut boot_params_guard = boot_params().write();
867c566df45SLoGin     let boottime_screen_info = &mut boot_params_guard.screen_info;
868c566df45SLoGin 
869c566df45SLoGin     boottime_screen_info.is_vga = true;
870c566df45SLoGin 
871c566df45SLoGin     boottime_screen_info.lfb_base = PhysAddr::new(fb_info.framebuffer_addr as usize);
872c566df45SLoGin 
873c566df45SLoGin     if fb_info.framebuffer_type == 2 {
874c566df45SLoGin         //当type=2时,width与height用字符数表示,故depth=8
875c566df45SLoGin         boottime_screen_info.origin_video_cols = width as u8;
876c566df45SLoGin         boottime_screen_info.origin_video_lines = height as u8;
877c566df45SLoGin         boottime_screen_info.video_type = BootTimeVideoType::Mda;
878c566df45SLoGin         boottime_screen_info.lfb_depth = 8;
879c566df45SLoGin     } else {
880c566df45SLoGin         //否则为图像模式,depth应参照帧缓冲区信息里面的每个像素的位数
881c566df45SLoGin         boottime_screen_info.lfb_width = width;
882c566df45SLoGin         boottime_screen_info.lfb_height = height;
883c566df45SLoGin         boottime_screen_info.video_type = BootTimeVideoType::Vlfb;
884c566df45SLoGin         boottime_screen_info.lfb_depth = fb_info.framebuffer_bpp as u8;
885c566df45SLoGin     }
886c566df45SLoGin 
887c566df45SLoGin     boottime_screen_info.lfb_size =
888c566df45SLoGin         (width * height * ((fb_info.framebuffer_bpp as u32 + 7) / 8)) as usize;
889c566df45SLoGin 
890c75ef4e2SLoGin     // let buf_vaddr = VirtAddr::new(0xffff800003200000);
891c75ef4e2SLoGin     let buf_vaddr = VirtAddr::new(
892c75ef4e2SLoGin         crate::include::bindings::bindings::SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE as usize
893c75ef4e2SLoGin             + FRAME_BUFFER_MAPPING_OFFSET as usize,
894c75ef4e2SLoGin     );
895c566df45SLoGin     boottime_screen_info.lfb_virt_base = Some(buf_vaddr);
896c566df45SLoGin 
897c566df45SLoGin     let init_text = "Video driver to map.\n\0";
898c566df45SLoGin     send_to_default_serial8250_port(init_text.as_bytes());
899c566df45SLoGin 
900c566df45SLoGin     // 地址映射
901c566df45SLoGin     let paddr = PhysAddr::new(fb_info.framebuffer_addr as usize);
902c566df45SLoGin     let count =
903c566df45SLoGin         PageFrameCount::new(page_align_up(boottime_screen_info.lfb_size) / MMArch::PAGE_SIZE);
904c566df45SLoGin     unsafe { pseudo_map_phys(buf_vaddr, paddr, count) };
905c566df45SLoGin     return Ok(buf_vaddr);
906c566df45SLoGin }
907c566df45SLoGin 
908c566df45SLoGin #[unified_init(INITCALL_DEVICE)]
909c566df45SLoGin fn vesa_fb_device_init() -> Result<(), SystemError> {
910c566df45SLoGin     // 如果没有vesa帧缓冲区,直接返回
911c566df45SLoGin     if !HAS_VESA_FB.load(core::sync::atomic::Ordering::SeqCst) {
912c566df45SLoGin         return Ok(());
913c566df45SLoGin     }
914c566df45SLoGin 
915c566df45SLoGin     static INIT: Once = Once::new();
916c566df45SLoGin     INIT.call_once(|| {
917c566df45SLoGin         kinfo!("vesa fb device init");
91852da9a59SGnoCiYeH         let device = Arc::new(VesaFb::new());
919c566df45SLoGin 
92052da9a59SGnoCiYeH         let mut fb_fix = VESAFB_FIX_INFO.write_irqsave();
92152da9a59SGnoCiYeH         let mut fb_var = VESAFB_DEFINED.write_irqsave();
922c566df45SLoGin 
923c566df45SLoGin         let boot_params_guard = boot_params().read();
924c566df45SLoGin         let boottime_screen_info = &boot_params_guard.screen_info;
925c566df45SLoGin 
92652da9a59SGnoCiYeH         fb_fix.smem_start = Some(boottime_screen_info.lfb_base);
92752da9a59SGnoCiYeH         fb_fix.smem_len = boottime_screen_info.lfb_size;
928c566df45SLoGin 
929c566df45SLoGin         if boottime_screen_info.video_type == BootTimeVideoType::Mda {
93052da9a59SGnoCiYeH             fb_fix.visual = FbVisual::Mono10;
93152da9a59SGnoCiYeH             fb_var.bits_per_pixel = 8;
93252da9a59SGnoCiYeH             fb_fix.line_length =
93352da9a59SGnoCiYeH                 (boottime_screen_info.origin_video_cols as u32) * (fb_var.bits_per_pixel / 8);
93452da9a59SGnoCiYeH             fb_var.xres_virtual = boottime_screen_info.origin_video_cols as u32;
93552da9a59SGnoCiYeH             fb_var.yres_virtual = boottime_screen_info.origin_video_lines as u32;
936c566df45SLoGin         } else {
93752da9a59SGnoCiYeH             fb_fix.visual = FbVisual::TrueColor;
93852da9a59SGnoCiYeH             fb_var.bits_per_pixel = boottime_screen_info.lfb_depth as u32;
93952da9a59SGnoCiYeH             fb_fix.line_length =
94052da9a59SGnoCiYeH                 (boottime_screen_info.lfb_width as u32) * (fb_var.bits_per_pixel / 8);
94152da9a59SGnoCiYeH             fb_var.xres_virtual = boottime_screen_info.lfb_width as u32;
94252da9a59SGnoCiYeH             fb_var.yres_virtual = boottime_screen_info.lfb_height as u32;
94352da9a59SGnoCiYeH             fb_var.xres = boottime_screen_info.lfb_width as u32;
94452da9a59SGnoCiYeH             fb_var.yres = boottime_screen_info.lfb_height as u32;
945c566df45SLoGin         }
946c566df45SLoGin 
94752da9a59SGnoCiYeH         fb_var.red.length = boottime_screen_info.red_size as u32;
94852da9a59SGnoCiYeH         fb_var.green.length = boottime_screen_info.green_size as u32;
94952da9a59SGnoCiYeH         fb_var.blue.length = boottime_screen_info.blue_size as u32;
950c566df45SLoGin 
95152da9a59SGnoCiYeH         fb_var.red.offset = boottime_screen_info.red_pos as u32;
95252da9a59SGnoCiYeH         fb_var.green.offset = boottime_screen_info.green_pos as u32;
95352da9a59SGnoCiYeH         fb_var.blue.offset = boottime_screen_info.blue_pos as u32;
95452da9a59SGnoCiYeH 
95552da9a59SGnoCiYeH         // TODO: 这里是暂时这样写的,初始化为RGB888格式,后续vesa初始化完善后删掉下面
95652da9a59SGnoCiYeH         fb_var.red.offset = 16;
95752da9a59SGnoCiYeH         fb_var.green.offset = 8;
95852da9a59SGnoCiYeH         fb_var.blue.offset = 0;
95952da9a59SGnoCiYeH 
96052da9a59SGnoCiYeH         if fb_var.bits_per_pixel >= 1 && fb_var.bits_per_pixel <= 8 {
96152da9a59SGnoCiYeH             fb_var.red.length = fb_var.bits_per_pixel;
96252da9a59SGnoCiYeH             fb_var.green.length = fb_var.bits_per_pixel;
96352da9a59SGnoCiYeH             fb_var.blue.length = fb_var.bits_per_pixel;
96452da9a59SGnoCiYeH         }
96552da9a59SGnoCiYeH 
966c566df45SLoGin         device_manager().device_default_initialize(&(device.clone() as Arc<dyn Device>));
967c566df45SLoGin 
968c566df45SLoGin         platform_device_manager()
969c566df45SLoGin             .device_add(device.clone() as Arc<dyn PlatformDevice>)
970c566df45SLoGin             .expect("vesa_fb_device_init: platform_device_manager().device_add failed");
971c566df45SLoGin 
972c566df45SLoGin         frame_buffer_manager()
973c566df45SLoGin             .register_fb(device.clone() as Arc<dyn FrameBuffer>)
974c566df45SLoGin             .expect("vesa_fb_device_init: frame_buffer_manager().register_fb failed");
975c566df45SLoGin 
97652da9a59SGnoCiYeH         // 加入全局fb表
97752da9a59SGnoCiYeH         let mut guard = FRAME_BUFFER_SET.write();
97852da9a59SGnoCiYeH         if guard.get(device.fb_id().data() as usize).unwrap().is_some() {
97952da9a59SGnoCiYeH             kwarn!(
98052da9a59SGnoCiYeH                 "vesa_fb_device_init: There is already an element {:?} in the FRAME_BUFFER_SET",
98152da9a59SGnoCiYeH                 device.fb_id()
98252da9a59SGnoCiYeH             );
98352da9a59SGnoCiYeH         }
98452da9a59SGnoCiYeH         guard[device.fb_id().data() as usize] = Some(device.clone());
98552da9a59SGnoCiYeH 
986c566df45SLoGin         // 设置vesa fb的状态为运行中
987c566df45SLoGin         device.inner.lock().fb_state = FbState::Running;
988c566df45SLoGin     });
989c566df45SLoGin 
990c566df45SLoGin     return Ok(());
991c566df45SLoGin }
992