Skip to content

Commit 49806aa

Browse files
committed
device/drm: Small DRM fixes
1 parent 781c76c commit 49806aa

4 files changed

Lines changed: 65 additions & 142 deletions

File tree

drivers/video/virtio_gpu/src/device.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ impl VirtioGpuDevice {
241241
ctrl_queue: &SpinMutex<VirtQueue>,
242242
resource_id: u32,
243243
) -> EResult<()> {
244-
let cmd = _VirtioGpuResourceUnref {
244+
let cmd = VirtioGpuResourceUnref {
245245
hdr: VirtioGpuCtrlHdr::new(VIRTIO_GPU_CMD_RESOURCE_UNREF),
246246
resource_id,
247247
padding: 0,
@@ -532,8 +532,8 @@ impl Device for VirtioGpuDevice {
532532
(1, 0, 0)
533533
}
534534

535-
fn driver_info(&self) -> (&[u8], &[u8], &[u8]) {
536-
(b"virtio-gpu", b"VirtIO GPU Driver", b"2026")
535+
fn driver_info(&self) -> (&str, &str, &str) {
536+
("virtio-gpu", "VirtIO GPU Driver", "2026")
537537
}
538538

539539
fn create_dumb(

drivers/video/virtio_gpu/src/spec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub struct VirtioGpuResourceCreate2d {
123123

124124
#[repr(C)]
125125
#[derive(Debug, Clone, Copy)]
126-
pub struct _VirtioGpuResourceUnref {
126+
pub struct VirtioGpuResourceUnref {
127127
pub hdr: VirtioGpuCtrlHdr,
128128
pub resource_id: u32,
129129
pub padding: u32,

kernel/src/device/drm/mod.rs

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use crate::{
77
process::Identity,
88
uapi::{
99
self,
10-
drm::{self, DRM_FORMAT_XRGB8888, drm_mode_modeinfo},
10+
drm::{
11+
self, DRM_FORMAT_XRGB8888, DRM_MODE_CURSOR_BO, DRM_MODE_CURSOR_MOVE, drm_mode_modeinfo,
12+
},
1113
},
1214
util::{event::Event, mutex::spin::SpinMutex},
1315
vfs::{
@@ -71,7 +73,7 @@ pub trait Device: Send + Sync {
7173
fn driver_version(&self) -> (i32, i32, i32);
7274

7375
/// Returns a tuple of (name, description, date).
74-
fn driver_info(&self) -> (&[u8], &[u8], &[u8]);
76+
fn driver_info(&self) -> (&str, &str, &str);
7577

7678
/// Creates a dumb framebuffer. Also returns the pitch in bytes.
7779
fn create_dumb(
@@ -82,7 +84,11 @@ pub trait Device: Send + Sync {
8284
bpp: u32,
8385
) -> EResult<(Arc<dyn BufferObject>, u32)> {
8486
let _ = (file, width, height, bpp);
85-
Err(Errno::ENOSYS)
87+
warn!(
88+
"drm::Device::create_dumb is not implemented for {}!",
89+
self.driver_info().0
90+
);
91+
Err(Errno::ENOTTY)
8692
}
8793

8894
fn create_fb(
@@ -95,7 +101,11 @@ pub trait Device: Send + Sync {
95101
pitch: u32,
96102
) -> EResult<Arc<Framebuffer>> {
97103
let _ = (file, buffer, width, height, format, pitch);
98-
Err(Errno::ENOSYS)
104+
warn!(
105+
"drm::Device::create_fb is not implemented for {}!",
106+
self.driver_info().0
107+
);
108+
Err(Errno::ENOTTY)
99109
}
100110

101111
fn commit(&self, state: &AtomicState);
@@ -105,19 +115,29 @@ pub trait Device: Send + Sync {
105115
/// `hot_x` and `hot_y` are the hotspot offsets (pixels from top-left of cursor image).
106116
fn set_cursor(
107117
&self,
108-
_crtc_id: u32,
109-
_buffer: Option<Arc<dyn BufferObject>>,
110-
_width: u32,
111-
_height: u32,
112-
_hot_x: i32,
113-
_hot_y: i32,
118+
crtc_id: u32,
119+
buffer: Option<Arc<dyn BufferObject>>,
120+
width: u32,
121+
height: u32,
122+
hot_x: i32,
123+
hot_y: i32,
114124
) -> EResult<()> {
115-
Err(Errno::ENOSYS)
125+
let _ = (buffer, crtc_id, width, height, hot_x, hot_y);
126+
warn!(
127+
"drm::Device::set_cursor is not implemented for {}!",
128+
self.driver_info().0
129+
);
130+
Err(Errno::ENOTTY)
116131
}
117132

118133
/// Move the cursor to a new position on the given CRTC.
119-
fn move_cursor(&self, _crtc_id: u32, _x: i32, _y: i32) -> EResult<()> {
120-
Err(Errno::ENOSYS)
134+
fn move_cursor(&self, crtc_id: u32, x: i32, y: i32) -> EResult<()> {
135+
let _ = (crtc_id, x, y);
136+
warn!(
137+
"drm::Device::move_cursor is not implemented for {}!",
138+
self.driver_info().0
139+
);
140+
Err(Errno::ENOTTY)
121141
}
122142
}
123143

@@ -289,19 +309,25 @@ impl FileOps for DrmFile {
289309

290310
if !val.name.is_null() && val.name_len > 0 {
291311
let len = name.len().min(val.name_len);
292-
val.name.write_slice(&name[..len]).ok_or(Errno::EFAULT)?;
312+
val.name
313+
.write_slice(&name.as_bytes()[..len])
314+
.ok_or(Errno::EFAULT)?;
293315
}
294316
val.name_len = name.len();
295317

296318
if !val.date.is_null() && val.date_len > 0 {
297319
let len = date.len().min(val.date_len);
298-
val.date.write_slice(&date[..len]).ok_or(Errno::EFAULT)?;
320+
val.date
321+
.write_slice(&date.as_bytes()[..len])
322+
.ok_or(Errno::EFAULT)?;
299323
}
300324
val.date_len = date.len();
301325

302326
if !val.desc.is_null() && val.desc_len > 0 {
303327
let len = desc.len().min(val.desc_len);
304-
val.desc.write_slice(&desc[..len]).ok_or(Errno::EFAULT)?;
328+
val.desc
329+
.write_slice(&desc.as_bytes()[..len])
330+
.ok_or(Errno::EFAULT)?;
305331
}
306332
val.desc_len = desc.len();
307333

@@ -997,22 +1023,20 @@ impl FileOps for DrmFile {
9971023
let ptr = UserPtr::<drm::drm_mode_cursor>::new(arg);
9981024
let val = ptr.read().ok_or(Errno::EFAULT)?;
9991025

1000-
const DRM_MODE_CURSOR_BO: u32 = 0x01;
1001-
const DRM_MODE_CURSOR_MOVE: u32 = 0x02;
1002-
10031026
if val.flags & DRM_MODE_CURSOR_BO != 0 {
10041027
if val.handle == 0 {
10051028
// Hide cursor
10061029
self.device.set_cursor(val.crtc_id, None, 0, 0, 0, 0)?;
10071030
} else {
10081031
// Set cursor image
1009-
let buffers = self.buffers.lock();
1010-
let buffer = buffers
1011-
.iter()
1012-
.find(|b| b.id() == val.handle)
1013-
.ok_or(Errno::EINVAL)?
1014-
.clone();
1015-
drop(buffers);
1032+
let buffer = {
1033+
let buffers = self.buffers.lock();
1034+
buffers
1035+
.iter()
1036+
.find(|b| b.id() == val.handle)
1037+
.ok_or(Errno::EINVAL)?
1038+
.clone()
1039+
};
10161040
self.device.set_cursor(
10171041
val.crtc_id,
10181042
Some(buffer),
@@ -1031,22 +1055,20 @@ impl FileOps for DrmFile {
10311055
let ptr = UserPtr::<drm::drm_mode_cursor2>::new(arg);
10321056
let val = ptr.read().ok_or(Errno::EFAULT)?;
10331057

1034-
const DRM_MODE_CURSOR_BO: u32 = 0x01;
1035-
const DRM_MODE_CURSOR_MOVE: u32 = 0x02;
1036-
10371058
if val.flags & DRM_MODE_CURSOR_BO != 0 {
10381059
if val.handle == 0 {
10391060
// Hide cursor
10401061
self.device.set_cursor(val.crtc_id, None, 0, 0, 0, 0)?;
10411062
} else {
10421063
// Set cursor image with hotspot
1043-
let buffers = self.buffers.lock();
1044-
let buffer = buffers
1045-
.iter()
1046-
.find(|b| b.id() == val.handle)
1047-
.ok_or(Errno::EINVAL)?
1048-
.clone();
1049-
drop(buffers);
1064+
let buffer = {
1065+
let buffers = self.buffers.lock();
1066+
buffers
1067+
.iter()
1068+
.find(|b| b.id() == val.handle)
1069+
.ok_or(Errno::EINVAL)?
1070+
.clone()
1071+
};
10501072
self.device.set_cursor(
10511073
val.crtc_id,
10521074
Some(buffer),
@@ -1082,7 +1104,7 @@ impl FileOps for DrmFile {
10821104
}
10831105
x => {
10841106
error!("Unknown ioctl {x:x}");
1085-
return Err(Errno::ENOSYS);
1107+
return Err(Errno::ENOTTY);
10861108
}
10871109
}
10881110
Ok(0)

kernel/src/device/drm/plainfb.rs

Lines changed: 2 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,10 @@ use crate::{
1919
DRM_FORMAT_ARGB8888, DRM_FORMAT_XRGB8888, DRM_PLANE_TYPE_CURSOR, DRM_PLANE_TYPE_PRIMARY,
2020
drm_mode_connector_state, drm_mode_connector_type,
2121
},
22-
util::mutex::spin::SpinMutex,
2322
};
2423
use alloc::{sync::Arc, vec};
2524
use core::any::Any;
2625

27-
struct CursorState {
28-
buffer: Option<Arc<dyn BufferObject>>,
29-
x: i32,
30-
y: i32,
31-
width: u32,
32-
height: u32,
33-
hot_x: i32,
34-
hot_y: i32,
35-
}
36-
3726
struct PlainDevice {
3827
state: DeviceState,
3928
width: u32,
@@ -42,51 +31,6 @@ struct PlainDevice {
4231
stride: u32,
4332
addr: MmioView, // Shared DRM object storage (device-global)
4433
obj_counter: IdAllocator,
45-
cursor: SpinMutex<CursorState>,
46-
}
47-
48-
impl PlainDevice {
49-
/// Alpha-blend cursor pixels onto the MMIO framebuffer.
50-
fn composite_cursor(&self, cursor: &CursorState, cbuf: &PlainDumbBuffer) {
51-
let fb_w = self.width as i32;
52-
let fb_h = self.height as i32;
53-
let stride = self.stride as usize;
54-
let cw = cursor.width as i32;
55-
let ch = cursor.height as i32;
56-
57-
let dst_base = self.addr.base() as *mut u8;
58-
let src_base = cbuf.addr.as_hhdm::<u8>();
59-
60-
for cy in 0..ch {
61-
let py = cursor.y + cy;
62-
if py < 0 || py >= fb_h {
63-
continue;
64-
}
65-
for cx in 0..cw {
66-
let px = cursor.x + cx;
67-
if px < 0 || px >= fb_w {
68-
continue;
69-
}
70-
71-
let src_off = (cy as usize * cursor.width as usize + cx as usize) * 4;
72-
let dst_off = py as usize * stride + px as usize * 4;
73-
74-
unsafe {
75-
let sa = *src_base.add(src_off + 3) as u32;
76-
if sa == 0 {
77-
continue;
78-
}
79-
let inv_a = 255 - sa;
80-
81-
for c in 0..3 {
82-
let s = *src_base.add(src_off + c) as u32;
83-
let d = *dst_base.add(dst_off + c) as u32;
84-
*dst_base.add(dst_off + c) = ((s * sa + d * inv_a) / 255) as u8;
85-
}
86-
}
87-
}
88-
}
89-
}
9034
}
9135

9236
impl Device for PlainDevice {
@@ -98,8 +42,8 @@ impl Device for PlainDevice {
9842
(0, 1, 0)
9943
}
10044

101-
fn driver_info(&self) -> (&[u8], &[u8], &[u8]) {
102-
(b"plainfb", b"Plain Framebuffer", b"0")
45+
fn driver_info(&self) -> (&str, &str, &str) {
46+
("plainfb", "Plain Framebuffer", "0")
10347
}
10448

10549
fn create_dumb(
@@ -186,43 +130,9 @@ impl Device for PlainDevice {
186130
);
187131
}
188132
}
189-
190-
// Composite cursor on top if active
191-
let cursor = self.cursor.lock();
192-
if let Some(ref cursor_buf) = cursor.buffer {
193-
let cursor_data = cursor_buf.as_ref() as &dyn Any;
194-
if let Some(cbuf) = cursor_data.downcast_ref::<PlainDumbBuffer>() {
195-
self.composite_cursor(&cursor, cbuf);
196-
}
197-
}
198133
}
199134
}
200135
}
201-
202-
fn set_cursor(
203-
&self,
204-
_crtc_id: u32,
205-
buffer: Option<Arc<dyn BufferObject>>,
206-
width: u32,
207-
height: u32,
208-
hot_x: i32,
209-
hot_y: i32,
210-
) -> EResult<()> {
211-
let mut cursor = self.cursor.lock();
212-
cursor.buffer = buffer;
213-
cursor.width = width;
214-
cursor.height = height;
215-
cursor.hot_x = hot_x;
216-
cursor.hot_y = hot_y;
217-
Ok(())
218-
}
219-
220-
fn move_cursor(&self, _crtc_id: u32, x: i32, y: i32) -> EResult<()> {
221-
let mut cursor = self.cursor.lock();
222-
cursor.x = x;
223-
cursor.y = y;
224-
Ok(())
225-
}
226136
}
227137

228138
struct PlainDumbBuffer {
@@ -299,15 +209,6 @@ fn PLAINFB_STAGE() {
299209
stride: fb.pitch as _,
300210
addr: unsafe { MmioView::new(fb.base, fb.pitch * fb.height) },
301211
obj_counter: IdAllocator::new(),
302-
cursor: SpinMutex::new(CursorState {
303-
buffer: None,
304-
x: 0,
305-
y: 0,
306-
width: 0,
307-
height: 0,
308-
hot_x: 0,
309-
hot_y: 0,
310-
}),
311212
});
312213

313214
// Initialize DRM objects and store them in the device

0 commit comments

Comments
 (0)