1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use core::{any::Any, slice};
#[allow(unused_imports)]
use ax_driver::prelude::DisplayDriverOps;
use ax_errno::AxError;
use ax_hal::mem::virt_to_phys;
use ax_memory_addr::{PhysAddrRange, VirtAddr};
use axfs_ng_vfs::{NodeFlags, VfsError, VfsResult};
use starry_vm::VmMutPtr;
use crate::pseudofs::{DeviceMmap, DeviceOps};
// Types from https://github.com/Tangzh33/asterinas
#[repr(C)]
#[derive(Default, Debug, Clone, Copy)]
pub struct FrameBufferBitfield {
/// The beginning of bitfield.
offset: u32,
/// The length of bitfield.
length: u32,
/// Most significant bit is right(!= 0).
msb_right: u32,
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
struct VarScreenInfo {
pub xres: u32, // Visible resolution
pub yres: u32,
pub xres_virtual: u32, // Virtual resolution
pub yres_virtual: u32,
pub xoffset: u32, // Offset from virtual to visible
pub yoffset: u32,
pub bits_per_pixel: u32, // Guess what
pub grayscale: u32, // 0 = color, 1 = grayscale, >1 = FOURCC
// Add other fields as needed
pub red: FrameBufferBitfield, // Bitfield in framebuffer memory if true color
pub green: FrameBufferBitfield, // Else only length is significant
pub blue: FrameBufferBitfield,
pub transp: FrameBufferBitfield, // Transparency
pub nonstd: u32, // Non-standard pixel format
pub activate: u32, // See FB_ACTIVATE_*
pub height: u32, // Height of picture in mm
pub width: u32, // Width of picture in mm
pub accel_flags: u32, // (OBSOLETE) see fb_info.flags
pub pixclock: u32, // Pixel clock in ps (pico seconds)
pub left_margin: u32, // Time from sync to picture
pub right_margin: u32, // Time from picture to sync
pub upper_margin: u32, // Time from sync to picture
pub lower_margin: u32,
pub hsync_len: u32, // Length of horizontal sync
pub vsync_len: u32, // Length of vertical sync
pub sync: u32, // See FB_SYNC_*
pub vmode: u32, // See FB_VMODE_*
pub rotate: u32, // Angle we rotate counter-clockwise
pub colorspace: u32, // Colorspace for FOURCC-based modes
pub reserved: [u32; 4], // Reserved for future compatibility
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
struct FixScreenInfo {
pub id: [u8; 16], // Identification string, e.g., "TT Builtin"
pub smem_start: u64, // Start of framebuffer memory (physical address)
pub smem_len: u32, // Length of framebuffer memory
pub type_: u32, // See FB_TYPE_*
pub type_aux: u32, // Interleave for interleaved planes
pub visual: u32, // See FB_VISUAL_*
pub xpanstep: u16, // Zero if no hardware panning
pub ypanstep: u16, // Zero if no hardware panning
pub ywrapstep: u16, // Zero if no hardware ywrap
pub line_length: u32, // Length of a line in bytes
pub mmio_start: u64, // Start of Memory Mapped I/O (physical address)
pub mmio_len: u32, // Length of Memory Mapped I/O
pub accel: u32, // Indicate to driver which specific chip/card we have
pub capabilities: u16, // See FB_CAP_*
pub reserved: [u16; 2], // Reserved for future compatibility
}
async fn refresh_task() {
let delay = core::time::Duration::from_secs_f32(1. / 60.);
loop {
if !ax_display::framebuffer_flush() {
warn!("Failed to refresh framebuffer");
}
ax_task::future::sleep(delay).await;
}
}
pub struct FrameBuffer {
base: VirtAddr,
size: usize,
}
impl FrameBuffer {
pub fn new() -> Self {
ax_task::spawn_with_name(
|| ax_task::future::block_on(refresh_task()),
"fb-refresh".into(),
);
let info = ax_display::framebuffer_info();
Self {
base: VirtAddr::from(info.fb_base_vaddr),
size: info.fb_size,
}
}
#[allow(clippy::mut_from_ref)]
fn as_mut_slice(&self) -> &mut [u8] {
unsafe { slice::from_raw_parts_mut(self.base.as_mut_ptr(), self.size) }
}
}
impl DeviceOps for FrameBuffer {
fn read_at(&self, buf: &mut [u8], offset: u64) -> VfsResult<usize> {
let slice = self.as_mut_slice();
let len = buf
.len()
.min((slice.len() as u64).saturating_sub(offset) as usize);
buf[..len].copy_from_slice(&slice[..len]);
Ok(len)
}
fn write_at(&self, buf: &[u8], offset: u64) -> VfsResult<usize> {
let slice = self.as_mut_slice();
if offset >= slice.len() as u64 {
return Err(VfsError::StorageFull);
}
let len = buf.len().min(slice.len() - offset as usize);
slice[..len].copy_from_slice(&buf[..len]);
Ok(len)
}
fn ioctl(&self, cmd: u32, arg: usize) -> VfsResult<usize> {
match cmd {
// FBIOGET_VSCREENINFO
0x4600 => {
let info = ax_display::framebuffer_info();
let line_length = (info.fb_size / info.height as usize) as u32;
let bpp = line_length / info.width;
(arg as *mut VarScreenInfo).vm_write(VarScreenInfo {
xres: info.width,
yres: info.height,
xres_virtual: info.width,
yres_virtual: info.height,
xoffset: 0,
yoffset: 0,
bits_per_pixel: bpp * 8,
grayscale: 0,
red: FrameBufferBitfield {
offset: 16,
length: 8,
msb_right: 0,
},
green: FrameBufferBitfield {
offset: 8,
length: 8,
msb_right: 0,
},
blue: FrameBufferBitfield {
offset: 0,
length: 8,
msb_right: 0,
},
transp: FrameBufferBitfield {
offset: 24,
length: 8,
msb_right: 0,
},
nonstd: 0,
activate: 0,
height: 0,
width: 0,
accel_flags: 0,
pixclock: 10000000 / info.width * 1000 / info.height,
left_margin: (info.width / 8) & 0xf8,
right_margin: 32,
upper_margin: 16,
lower_margin: 4,
hsync_len: (info.width / 8) & 0xf8,
vsync_len: 4,
sync: 0,
vmode: 0,
rotate: 0,
colorspace: 0,
reserved: [0; 4],
})?;
Ok(0)
}
// FBIOPUT_VSCREENINFO
0x4601 => Ok(0),
// FBIOGET_FSCREENINFO
0x4602 => {
let info = ax_display::framebuffer_info();
(arg as *mut FixScreenInfo).vm_write(FixScreenInfo {
id: *b"Virtio Framebuf\0",
smem_start: info.fb_base_vaddr as u64,
smem_len: info.fb_size as u32,
type_: 0,
type_aux: 0,
visual: 2, // FB_VISUAL_TRUECOLOR
xpanstep: 0,
ypanstep: 0,
ywrapstep: 0,
line_length: (info.fb_size / info.height as usize) as u32,
mmio_start: 0,
mmio_len: 0,
accel: 0,
capabilities: 0,
reserved: [0; 2],
})?;
Ok(0)
}
// FBIOGETCMAP
0x4604 => Ok(0),
// FBIOPUTCMAP
0x4605 => Ok(0),
// FBIOPAN_DISPLAY
0x4606 => Err(AxError::InvalidInput),
// FBIOBLANK
0x4611 => Err(AxError::InvalidInput),
_ => Err(AxError::NotATty),
}
}
fn as_any(&self) -> &dyn Any {
self
}
fn mmap(&self, _offset: u64) -> DeviceMmap {
DeviceMmap::Physical(PhysAddrRange::from_start_size(
virt_to_phys(self.base),
self.size,
))
}
fn flags(&self) -> NodeFlags {
NodeFlags::NON_CACHEABLE
}
}