devices/virtio/
device_constants.rs

1// Copyright 2022 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Contains constants and struct definitions used for implementing vhost-user
6//! frontend devices without compile-time dependencies on their corresponding
7//! backend devices.
8
9use data_model::Le16;
10use data_model::Le32;
11use data_model::Le64;
12use serde::Deserialize;
13use serde::Serialize;
14use zerocopy::FromBytes;
15use zerocopy::Immutable;
16use zerocopy::IntoBytes;
17use zerocopy::KnownLayout;
18
19/// Virtio feature bits that are specific to a device type.
20///
21/// Per virtio 1.2 spec, features 0 to 23 and 50 to 127 are feature bits for the specific device
22/// type. Features beyond 63 are not representable in the current `u64` type used to store sets of
23/// features, so bits 64 to 127 are not included in this mask.
24pub const VIRTIO_DEVICE_TYPE_SPECIFIC_FEATURES_MASK: u64 = 0xfffc_0000_00ff_ffff;
25
26pub mod block {
27    use super::*;
28
29    pub const VIRTIO_BLK_T_IN: u32 = 0;
30    pub const VIRTIO_BLK_T_OUT: u32 = 1;
31    pub const VIRTIO_BLK_T_FLUSH: u32 = 4;
32    pub const VIRTIO_BLK_T_GET_ID: u32 = 8;
33    pub const VIRTIO_BLK_T_DISCARD: u32 = 11;
34    pub const VIRTIO_BLK_T_WRITE_ZEROES: u32 = 13;
35
36    pub const VIRTIO_BLK_S_OK: u8 = 0;
37    pub const VIRTIO_BLK_S_IOERR: u8 = 1;
38    pub const VIRTIO_BLK_S_UNSUPP: u8 = 2;
39
40    pub const VIRTIO_BLK_F_SEG_MAX: u32 = 2;
41    pub const VIRTIO_BLK_F_RO: u32 = 5;
42    pub const VIRTIO_BLK_F_BLK_SIZE: u32 = 6;
43    pub const VIRTIO_BLK_F_FLUSH: u32 = 9;
44    pub const VIRTIO_BLK_F_MQ: u32 = 12;
45    pub const VIRTIO_BLK_F_DISCARD: u32 = 13;
46    pub const VIRTIO_BLK_F_WRITE_ZEROES: u32 = 14;
47
48    #[derive(Copy, Clone, Debug, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
49    #[repr(C)]
50    pub struct virtio_blk_geometry {
51        cylinders: Le16,
52        heads: u8,
53        sectors: u8,
54    }
55
56    #[derive(Copy, Clone, Debug, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
57    #[repr(C)]
58    pub struct virtio_blk_topology {
59        physical_block_exp: u8,
60        alignment_offset: u8,
61        min_io_size: Le16,
62        opt_io_size: Le32,
63    }
64
65    #[derive(Copy, Clone, Debug, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
66    #[repr(C, packed)]
67    pub struct virtio_blk_config {
68        pub capacity: Le64,
69        pub size_max: Le32,
70        pub seg_max: Le32,
71        pub geometry: virtio_blk_geometry,
72        pub blk_size: Le32,
73        pub topology: virtio_blk_topology,
74        pub writeback: u8,
75        pub unused0: u8,
76        pub num_queues: Le16,
77        pub max_discard_sectors: Le32,
78        pub max_discard_seg: Le32,
79        pub discard_sector_alignment: Le32,
80        pub max_write_zeroes_sectors: Le32,
81        pub max_write_zeroes_seg: Le32,
82        pub write_zeroes_may_unmap: u8,
83        pub unused1: [u8; 3],
84    }
85
86    #[derive(Copy, Clone, Debug, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
87    #[repr(C)]
88    pub struct virtio_blk_req_header {
89        pub req_type: Le32,
90        pub reserved: Le32,
91        pub sector: Le64,
92    }
93
94    #[derive(Copy, Clone, Debug, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
95    #[repr(C)]
96    pub struct virtio_blk_discard_write_zeroes {
97        pub sector: Le64,
98        pub num_sectors: Le32,
99        pub flags: Le32,
100    }
101
102    pub const VIRTIO_BLK_DISCARD_WRITE_ZEROES_FLAG_UNMAP: u32 = 1 << 0;
103}
104
105pub mod fs {
106    /// The maximum allowable length of the tag used to identify a specific virtio-fs device.
107    pub const FS_MAX_TAG_LEN: usize = 36;
108}
109
110pub mod gpu {
111    use super::*;
112
113    pub const NUM_QUEUES: usize = 2;
114
115    pub const VIRTIO_GPU_F_VIRGL: u32 = 0;
116    pub const VIRTIO_GPU_F_EDID: u32 = 1;
117    pub const VIRTIO_GPU_F_RESOURCE_UUID: u32 = 2;
118    pub const VIRTIO_GPU_F_RESOURCE_BLOB: u32 = 3;
119    pub const VIRTIO_GPU_F_CONTEXT_INIT: u32 = 4;
120    pub const VIRTIO_GPU_F_BLOB_ALIGNMENT: u32 = 5;
121    // The following capabilities are not upstreamed.
122    //
123    // F_CREATE_GUEST_HANDLE is useful for zero-copy memory mappings on certain hypervisors
124    // that are designed around static allocation of guest memory such as Xen.
125    //
126    // Please see https://github.com/magma-gpu/rutabaga_gfx/issues/66 for a discussion and
127    // tracking bug.
128    pub const VIRTIO_GPU_F_CREATE_GUEST_HANDLE: u32 = 16;
129
130    pub const VIRTIO_GPU_SHM_ID_HOST_VISIBLE: u8 = 0x0001;
131
132    #[derive(Copy, Clone, Debug, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
133    #[repr(C)]
134    pub struct virtio_gpu_config {
135        pub events_read: Le32,
136        pub events_clear: Le32,
137        pub num_scanouts: Le32,
138        pub num_capsets: Le32,
139    }
140}
141
142pub mod snd {
143    use super::*;
144
145    #[derive(
146        Copy,
147        Clone,
148        Default,
149        FromBytes,
150        Immutable,
151        IntoBytes,
152        KnownLayout,
153        Serialize,
154        Deserialize,
155        PartialEq,
156        Eq,
157        Debug,
158    )]
159    #[repr(C, packed)]
160    pub struct virtio_snd_config {
161        pub jacks: Le32,
162        pub streams: Le32,
163        pub chmaps: Le32,
164        pub controls: Le32,
165    }
166}
167
168pub mod media {
169    const QUEUE_SIZE: u16 = 256;
170    pub const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE, QUEUE_SIZE];
171}
172
173pub mod video {
174    use data_model::Le32;
175    use serde::Deserialize;
176    use serde::Serialize;
177    use serde_keyvalue::FromKeyValues;
178    use zerocopy::FromBytes;
179    use zerocopy::Immutable;
180    use zerocopy::IntoBytes;
181    use zerocopy::KnownLayout;
182
183    pub const CMD_QUEUE_INDEX: usize = 0;
184    pub const EVENT_QUEUE_INDEX: usize = 1;
185    pub const NUM_QUEUES: usize = 2;
186
187    pub const VIRTIO_VIDEO_F_RESOURCE_GUEST_PAGES: u32 = 0;
188    pub const VIRTIO_VIDEO_F_RESOURCE_NON_CONTIG: u32 = 1;
189    pub const VIRTIO_VIDEO_F_RESOURCE_VIRTIO_OBJECT: u32 = 2;
190
191    #[derive(Debug, Clone, Copy)]
192    pub enum VideoDeviceType {
193        Decoder,
194        Encoder,
195    }
196
197    #[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, Serialize)]
198    #[serde(rename_all = "kebab-case")]
199    pub enum VideoBackendType {
200        #[cfg(feature = "libvda")]
201        Libvda,
202        #[cfg(feature = "libvda")]
203        LibvdaVd,
204        #[cfg(feature = "ffmpeg")]
205        Ffmpeg,
206        #[cfg(feature = "vaapi")]
207        Vaapi,
208    }
209
210    #[derive(Debug, Serialize, Deserialize, FromKeyValues)]
211    pub struct VideoDeviceConfig {
212        pub backend: VideoBackendType,
213    }
214
215    /// The same set of virtio features is supported by the ffmpeg decoder and encoder.
216    pub fn ffmpeg_supported_virtio_features() -> u64 {
217        1u64 << VIRTIO_VIDEO_F_RESOURCE_GUEST_PAGES
218            | 1u64 << VIRTIO_VIDEO_F_RESOURCE_NON_CONTIG
219            | 1u64 << VIRTIO_VIDEO_F_RESOURCE_VIRTIO_OBJECT
220    }
221
222    /// The same set of virtio features is supported by the vaapi decoder and encoder.
223    pub fn vaapi_supported_virtio_features() -> u64 {
224        1u64 << VIRTIO_VIDEO_F_RESOURCE_GUEST_PAGES
225            | 1u64 << VIRTIO_VIDEO_F_RESOURCE_NON_CONTIG
226            | 1u64 << VIRTIO_VIDEO_F_RESOURCE_VIRTIO_OBJECT
227    }
228
229    /// The same set of virtio features is supported by the vda decoder and encoder.
230    pub fn vda_supported_virtio_features() -> u64 {
231        1u64 << VIRTIO_VIDEO_F_RESOURCE_NON_CONTIG | 1u64 << VIRTIO_VIDEO_F_RESOURCE_VIRTIO_OBJECT
232    }
233
234    pub fn backend_supported_virtio_features(backend: VideoBackendType) -> u64 {
235        match backend {
236            #[cfg(feature = "libvda")]
237            VideoBackendType::Libvda | VideoBackendType::LibvdaVd => {
238                vda_supported_virtio_features()
239            }
240            #[cfg(feature = "ffmpeg")]
241            VideoBackendType::Ffmpeg => ffmpeg_supported_virtio_features(),
242            #[cfg(feature = "vaapi")]
243            VideoBackendType::Vaapi => vaapi_supported_virtio_features(),
244        }
245    }
246
247    #[repr(C)]
248    #[derive(Debug, Default, Copy, Clone, FromBytes, Immutable, IntoBytes, KnownLayout)]
249    pub struct virtio_video_config {
250        pub version: Le32,
251        pub max_caps_length: Le32,
252        pub max_resp_length: Le32,
253        pub device_name: [u8; 32],
254    }
255}
256
257pub mod vsock {
258    pub const NUM_QUEUES: usize = 3;
259}
260
261pub mod wl {
262    pub const NUM_QUEUES: usize = 2;
263
264    pub const VIRTIO_WL_F_TRANS_FLAGS: u32 = 0x01;
265    pub const VIRTIO_WL_F_SEND_FENCES: u32 = 0x02;
266    pub const VIRTIO_WL_F_USE_SHMEM: u32 = 0x03;
267}
268
269pub mod console {
270    use data_model::Le16;
271    use data_model::Le32;
272    use zerocopy::FromBytes;
273    use zerocopy::Immutable;
274    use zerocopy::IntoBytes;
275    use zerocopy::KnownLayout;
276
277    pub const VIRTIO_CONSOLE_F_SIZE: u32 = 0;
278    pub const VIRTIO_CONSOLE_F_MULTIPORT: u32 = 1;
279    pub const VIRTIO_CONSOLE_F_EMERG_WRITE: u32 = 2;
280
281    #[derive(Copy, Clone, Debug, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
282    #[repr(C)]
283    pub struct virtio_console_config {
284        pub cols: Le16,
285        pub rows: Le16,
286        pub max_nr_ports: Le32,
287        pub emerg_wr: Le32,
288    }
289
290    #[derive(Copy, Clone, Debug, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
291    #[repr(C)]
292    pub struct virtio_console_control {
293        pub id: Le32,
294        pub event: Le16,
295        pub value: Le16,
296    }
297
298    #[derive(Copy, Clone, Debug, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
299    #[repr(C)]
300    pub struct virtio_console_resize {
301        pub cols: Le16,
302        pub rows: Le16,
303    }
304
305    pub const VIRTIO_CONSOLE_DEVICE_READY: u16 = 0;
306    pub const VIRTIO_CONSOLE_DEVICE_ADD: u16 = 1;
307    pub const VIRTIO_CONSOLE_DEVICE_REMOVE: u16 = 2;
308    pub const VIRTIO_CONSOLE_PORT_READY: u16 = 3;
309    pub const VIRTIO_CONSOLE_CONSOLE_PORT: u16 = 4;
310    pub const VIRTIO_CONSOLE_RESIZE: u16 = 5;
311    pub const VIRTIO_CONSOLE_PORT_OPEN: u16 = 6;
312    pub const VIRTIO_CONSOLE_PORT_NAME: u16 = 7;
313}