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(crate) 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(crate) struct virtio_blk_discard_write_zeroes {
97        pub sector: Le64,
98        pub num_sectors: Le32,
99        pub flags: Le32,
100    }
101
102    pub(crate) 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
121    pub const VIRTIO_GPU_SHM_ID_HOST_VISIBLE: u8 = 0x0001;
122
123    #[derive(Copy, Clone, Debug, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
124    #[repr(C)]
125    pub struct virtio_gpu_config {
126        pub events_read: Le32,
127        pub events_clear: Le32,
128        pub num_scanouts: Le32,
129        pub num_capsets: Le32,
130    }
131}
132
133pub mod snd {
134    use super::*;
135
136    #[derive(
137        Copy,
138        Clone,
139        Default,
140        FromBytes,
141        Immutable,
142        IntoBytes,
143        KnownLayout,
144        Serialize,
145        Deserialize,
146        PartialEq,
147        Eq,
148        Debug,
149    )]
150    #[repr(C, packed)]
151    pub struct virtio_snd_config {
152        pub jacks: Le32,
153        pub streams: Le32,
154        pub chmaps: Le32,
155        pub controls: Le32,
156    }
157}
158
159pub mod media {
160    const QUEUE_SIZE: u16 = 256;
161    pub const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE, QUEUE_SIZE];
162}
163
164pub mod video {
165    use data_model::Le32;
166    use serde::Deserialize;
167    use serde::Serialize;
168    use serde_keyvalue::FromKeyValues;
169    use zerocopy::FromBytes;
170    use zerocopy::Immutable;
171    use zerocopy::IntoBytes;
172    use zerocopy::KnownLayout;
173
174    pub const CMD_QUEUE_INDEX: usize = 0;
175    pub const EVENT_QUEUE_INDEX: usize = 1;
176    pub const NUM_QUEUES: usize = 2;
177
178    pub const VIRTIO_VIDEO_F_RESOURCE_GUEST_PAGES: u32 = 0;
179    pub const VIRTIO_VIDEO_F_RESOURCE_NON_CONTIG: u32 = 1;
180    pub const VIRTIO_VIDEO_F_RESOURCE_VIRTIO_OBJECT: u32 = 2;
181
182    #[derive(Debug, Clone, Copy)]
183    pub enum VideoDeviceType {
184        Decoder,
185        Encoder,
186    }
187
188    #[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, Serialize)]
189    #[serde(rename_all = "kebab-case")]
190    pub enum VideoBackendType {
191        #[cfg(feature = "libvda")]
192        Libvda,
193        #[cfg(feature = "libvda")]
194        LibvdaVd,
195        #[cfg(feature = "ffmpeg")]
196        Ffmpeg,
197        #[cfg(feature = "vaapi")]
198        Vaapi,
199    }
200
201    #[derive(Debug, Serialize, Deserialize, FromKeyValues)]
202    pub struct VideoDeviceConfig {
203        pub backend: VideoBackendType,
204    }
205
206    /// The same set of virtio features is supported by the ffmpeg decoder and encoder.
207    pub fn ffmpeg_supported_virtio_features() -> u64 {
208        1u64 << VIRTIO_VIDEO_F_RESOURCE_GUEST_PAGES
209            | 1u64 << VIRTIO_VIDEO_F_RESOURCE_NON_CONTIG
210            | 1u64 << VIRTIO_VIDEO_F_RESOURCE_VIRTIO_OBJECT
211    }
212
213    /// The same set of virtio features is supported by the vaapi decoder and encoder.
214    pub fn vaapi_supported_virtio_features() -> u64 {
215        1u64 << VIRTIO_VIDEO_F_RESOURCE_GUEST_PAGES
216            | 1u64 << VIRTIO_VIDEO_F_RESOURCE_NON_CONTIG
217            | 1u64 << VIRTIO_VIDEO_F_RESOURCE_VIRTIO_OBJECT
218    }
219
220    /// The same set of virtio features is supported by the vda decoder and encoder.
221    pub fn vda_supported_virtio_features() -> u64 {
222        1u64 << VIRTIO_VIDEO_F_RESOURCE_NON_CONTIG | 1u64 << VIRTIO_VIDEO_F_RESOURCE_VIRTIO_OBJECT
223    }
224
225    pub fn backend_supported_virtio_features(backend: VideoBackendType) -> u64 {
226        match backend {
227            #[cfg(feature = "libvda")]
228            VideoBackendType::Libvda | VideoBackendType::LibvdaVd => {
229                vda_supported_virtio_features()
230            }
231            #[cfg(feature = "ffmpeg")]
232            VideoBackendType::Ffmpeg => ffmpeg_supported_virtio_features(),
233            #[cfg(feature = "vaapi")]
234            VideoBackendType::Vaapi => vaapi_supported_virtio_features(),
235        }
236    }
237
238    #[repr(C)]
239    #[derive(Debug, Default, Copy, Clone, FromBytes, Immutable, IntoBytes, KnownLayout)]
240    pub struct virtio_video_config {
241        pub version: Le32,
242        pub max_caps_length: Le32,
243        pub max_resp_length: Le32,
244        pub device_name: [u8; 32],
245    }
246}
247
248pub mod vsock {
249    pub const NUM_QUEUES: usize = 3;
250}
251
252pub mod wl {
253    pub const NUM_QUEUES: usize = 2;
254
255    pub const VIRTIO_WL_F_TRANS_FLAGS: u32 = 0x01;
256    pub const VIRTIO_WL_F_SEND_FENCES: u32 = 0x02;
257    pub const VIRTIO_WL_F_USE_SHMEM: u32 = 0x03;
258}
259
260pub mod console {
261    use data_model::Le16;
262    use data_model::Le32;
263    use zerocopy::FromBytes;
264    use zerocopy::Immutable;
265    use zerocopy::IntoBytes;
266    use zerocopy::KnownLayout;
267
268    pub const VIRTIO_CONSOLE_F_SIZE: u32 = 0;
269    pub const VIRTIO_CONSOLE_F_MULTIPORT: u32 = 1;
270    pub const VIRTIO_CONSOLE_F_EMERG_WRITE: u32 = 2;
271
272    #[derive(Copy, Clone, Debug, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
273    #[repr(C)]
274    pub struct virtio_console_config {
275        pub cols: Le16,
276        pub rows: Le16,
277        pub max_nr_ports: Le32,
278        pub emerg_wr: Le32,
279    }
280
281    #[derive(Copy, Clone, Debug, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
282    #[repr(C)]
283    pub struct virtio_console_control {
284        pub id: Le32,
285        pub event: Le16,
286        pub value: Le16,
287    }
288
289    #[derive(Copy, Clone, Debug, Default, FromBytes, Immutable, IntoBytes, KnownLayout)]
290    #[repr(C)]
291    pub struct virtio_console_resize {
292        pub cols: Le16,
293        pub rows: Le16,
294    }
295
296    pub const VIRTIO_CONSOLE_DEVICE_READY: u16 = 0;
297    pub const VIRTIO_CONSOLE_DEVICE_ADD: u16 = 1;
298    pub const VIRTIO_CONSOLE_DEVICE_REMOVE: u16 = 2;
299    pub const VIRTIO_CONSOLE_PORT_READY: u16 = 3;
300    pub const VIRTIO_CONSOLE_CONSOLE_PORT: u16 = 4;
301    pub const VIRTIO_CONSOLE_RESIZE: u16 = 5;
302    pub const VIRTIO_CONSOLE_PORT_OPEN: u16 = 6;
303    pub const VIRTIO_CONSOLE_PORT_NAME: u16 = 7;
304}