virtio/
gpu.rs

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
240
241
242
243
244
245
#[derive(Clone, Copy, PartialEq, Debug)]
#[repr(u32)]
pub enum CtrlType {
    // 2D commands
    CmdGetDisplayInfo = 0x100,
    CmdResourceCreate2D,
    CmdResourceUnref,
    CmdSetScanout,
    CmdResourceFlush,
    CmdTransferToHost2D,
    CmdResourceAttachBacking,
    CmdResourceDetachBacking,
    CmdGetCapsetInfo,
    CmdGetEdid,
    CmdResourceAssignUuid,
    CmdResourceCreateBlob,
    CmdSetScanoutBlob,

    // 3D commands
    CmdCtxCreate = 0x200,
    CmdCtxDestroy,
    CmdCtxAttachResource,
    CmdCtxDetachResource,
    CmdResourceCreate3D,
    CmdTransferToHost3D,
    CmdTransferFromHost3D,
    CmdSubmit3D,
    CmdResourceMapBlob,
    CmdResourceUnmapBlob,

    // Cursor commands
    CmdUpdateCursor = 0x300,
    CmdMoveCursor,

    // Success responses
    OkNoData = 0x1100,
    OkDisplayInfo,
    OkCapsetInfo,
    OkCapset,
    OkEdid,
    OkResourceUuid,
    OkMapInfo,

    // Error responses
    ErrUnspecified = 0x1200,
    ErrOutOfMemory,
    ErrInvalidScanoutId,
    ErrInvalidResourceId,
    ErrInvalidContextId,
    ErrInvalidParameter,
}

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct CtrlHeader {
    pub typ: CtrlType,
    pub flags: u32,
    pub fence_id: u64,
    pub ctx_id: u32,
    pub ring_id: u8,
    _padding: [u8; 3],
}

impl CtrlHeader {
    pub fn new(typ: CtrlType) -> CtrlHeader {
        CtrlHeader { typ, flags: 0, fence_id: 0, ctx_id: 0, ring_id: 0, _padding: [0, 0, 0] }
    }
}

const MAX_SCANOUTS: usize = 16;

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct DisplayMode {
    pub x: u32,
    pub y: u32,
    pub width: u32,
    pub height: u32,
    pub enabled: u32,
    pub flags: u32,
}

#[derive(Clone, Debug)]
#[repr(C)]
pub struct DisplayInfo {
    pub header: CtrlHeader,
    pub modes: [DisplayMode; MAX_SCANOUTS],
}

#[derive(Clone, Copy, PartialEq, Debug)]
#[repr(u32)]
pub enum VirtioGpuFormat {
    B8G8R8A8Unorm = 1,
    B8G8R8X8Unorm = 2,
    A8R8G8B8Unorm = 3,
    X8R8G8B8Unorm = 4,
    R8G8B8A8Unorm = 67,
    X8B8G8R8Unorm = 68,
    A8B8G8R8Unorm = 121,
    R8G8B8X8Unorm = 134,
}

#[repr(C)]
pub struct CreateResource2D {
    pub header: CtrlHeader,
    pub resource_id: u32,
    pub format: VirtioGpuFormat,
    pub width: u32,
    pub height: u32,
}

impl CreateResource2D {
    pub fn new(id: u32, format: VirtioGpuFormat, width: u32, height: u32) -> CreateResource2D {
        CreateResource2D {
            header: CtrlHeader::new(CtrlType::CmdResourceCreate2D),
            resource_id: id,
            format,
            width,
            height,
        }
    }
}

#[derive(Clone, Debug)]
#[repr(C)]
pub struct BackingMemoryEntry {
    pub address: u64,
    pub length: u32,
    _padding: u32,
}

impl BackingMemoryEntry {
    pub fn new(address: u64, length: u32) -> BackingMemoryEntry {
        BackingMemoryEntry { address, length, _padding: 0 }
    }
}

/// Command structure to attach backing pages to a GPU resource. This header is followed by a
/// number of `BackingMemoryEntry` structures.
#[derive(Clone, Debug)]
#[repr(C)]
pub struct ResourceAttachBacking {
    pub header: CtrlHeader,
    pub resource_id: u32,
    pub num_entries: u32,
}

// This is a bit of a hacky way to easily create a `ResourceAttachBacking` with a single memory
// region.
#[derive(Clone, Debug)]
#[repr(C)]
pub struct SimpleResourceAttachBacking {
    pub header: CtrlHeader,
    pub resource_id: u32,
    pub num_entries: u32,
    pub mem_entry: BackingMemoryEntry,
}

impl SimpleResourceAttachBacking {
    pub fn new(resource_id: u32, address: u64, length: u32) -> SimpleResourceAttachBacking {
        SimpleResourceAttachBacking {
            header: CtrlHeader::new(CtrlType::CmdResourceAttachBacking),
            resource_id,
            num_entries: 1,
            mem_entry: BackingMemoryEntry::new(address, length),
        }
    }
}

#[repr(C)]
pub struct SetScanout {
    pub header: CtrlHeader,
    pub x: u32,
    pub y: u32,
    pub width: u32,
    pub height: u32,
    pub scanout_id: u32,
    pub resource_id: u32,
}

impl SetScanout {
    pub fn new(width: u32, height: u32, scanout_id: u32, resource_id: u32) -> SetScanout {
        SetScanout {
            header: CtrlHeader::new(CtrlType::CmdSetScanout),
            x: 0,
            y: 0,
            width,
            height,
            scanout_id,
            resource_id,
        }
    }
}

#[repr(C)]
pub struct TransferToHost2D {
    pub header: CtrlHeader,
    pub x: u32,
    pub y: u32,
    pub width: u32,
    pub height: u32,
    pub offset: u64,
    pub resource_id: u32,
    _padding: u32,
}

impl TransferToHost2D {
    pub fn new(width: u32, height: u32, offset: u64, resource_id: u32) -> TransferToHost2D {
        TransferToHost2D {
            header: CtrlHeader::new(CtrlType::CmdTransferToHost2D),
            x: 0,
            y: 0,
            width,
            height,
            offset,
            resource_id,
            _padding: 0,
        }
    }
}

#[repr(C)]
pub struct FlushResource {
    pub header: CtrlHeader,
    pub x: u32,
    pub y: u32,
    pub width: u32,
    pub height: u32,
    pub resource_id: u32,
    _padding: u32,
}

impl FlushResource {
    pub fn new(resource_id: u32, width: u32, height: u32) -> FlushResource {
        FlushResource {
            header: CtrlHeader::new(CtrlType::CmdResourceFlush),
            x: 0,
            y: 0,
            width,
            height,
            resource_id,
            _padding: 0,
        }
    }
}