2 * VIRTIO Sound Device conforming to
4 * "Virtual I/O Device (VIRTIO) Version 1.2
5 * Committee Specification Draft 01
8 * Copyright (c) 2023 Emmanouil Pitsidianakis <manos.pitsidianakis@linaro.org>
9 * Copyright (C) 2019 OpenSynergy GmbH
11 * This work is licensed under the terms of the GNU GPL, version 2 or
12 * (at your option) any later version. See the COPYING file in the
13 * top-level directory.
16 #ifndef QEMU_VIRTIO_SOUND_H
17 #define QEMU_VIRTIO_SOUND_H
19 #include "hw/virtio/virtio.h"
20 #include "audio/audio.h"
21 #include "standard-headers/linux/virtio_ids.h"
22 #include "standard-headers/linux/virtio_snd.h"
24 #define TYPE_VIRTIO_SND "virtio-sound-device"
25 #define VIRTIO_SND(obj) \
26 OBJECT_CHECK(VirtIOSound, (obj), TYPE_VIRTIO_SND)
28 /* CONFIGURATION SPACE */
30 typedef struct virtio_snd_config virtio_snd_config
;
32 /* COMMON DEFINITIONS */
34 /* common header for request/response*/
35 typedef struct virtio_snd_hdr virtio_snd_hdr
;
37 /* event notification */
38 typedef struct virtio_snd_event virtio_snd_event
;
40 /* common control request to query an item information */
41 typedef struct virtio_snd_query_info virtio_snd_query_info
;
43 /* JACK CONTROL MESSAGES */
45 typedef struct virtio_snd_jack_hdr virtio_snd_jack_hdr
;
47 /* jack information structure */
48 typedef struct virtio_snd_jack_info virtio_snd_jack_info
;
50 /* jack remapping control request */
51 typedef struct virtio_snd_jack_remap virtio_snd_jack_remap
;
54 * PCM CONTROL MESSAGES
56 typedef struct virtio_snd_pcm_hdr virtio_snd_pcm_hdr
;
58 /* PCM stream info structure */
59 typedef struct virtio_snd_pcm_info virtio_snd_pcm_info
;
61 /* set PCM stream params */
62 typedef struct virtio_snd_pcm_set_params virtio_snd_pcm_set_params
;
64 /* I/O request header */
65 typedef struct virtio_snd_pcm_xfer virtio_snd_pcm_xfer
;
67 /* I/O request status */
68 typedef struct virtio_snd_pcm_status virtio_snd_pcm_status
;
72 typedef struct VirtIOSound VirtIOSound
;
74 typedef struct VirtIOSoundPCMStream VirtIOSoundPCMStream
;
76 typedef struct virtio_snd_ctrl_command virtio_snd_ctrl_command
;
78 typedef struct VirtIOSoundPCM VirtIOSoundPCM
;
80 typedef struct VirtIOSoundPCMBuffer VirtIOSoundPCMBuffer
;
83 * The VirtIO sound spec reuses layouts and values from the High Definition
84 * Audio spec (virtio/v1.2: 5.14 Sound Device). This struct handles each I/O
85 * message's buffer (virtio/v1.2: 5.14.6.8 PCM I/O Messages).
87 * In the case of TX (i.e. playback) buffers, we defer reading the raw PCM data
88 * from the virtqueue until QEMU's sound backsystem calls the output callback.
89 * This is tracked by the `bool populated;` field, which is set to true when
90 * data has been read into our own buffer for consumption.
92 * VirtIOSoundPCMBuffer has a dynamic size since it includes the raw PCM data
93 * in its allocation. It must be initialized and destroyed as follows:
95 * size_t size = [[derived from owned VQ element descriptor sizes]];
96 * buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size);
97 * buffer->elem = [[owned VQ element]];
101 * g_free(buffer->elem);
104 struct VirtIOSoundPCMBuffer
{
105 QSIMPLEQ_ENTRY(VirtIOSoundPCMBuffer
) entry
;
106 VirtQueueElement
*elem
;
110 * In TX / Plaback, `offset` represents the first unused position inside
111 * `data`. If `offset == size` then there are no unused data left.
114 /* Used for the TX queue for lazy I/O copy from `elem` */
117 * VirtIOSoundPCMBuffer is an unsized type because it ends with an array of
118 * bytes. The size of `data` is determined from the I/O message's read-only
119 * or write-only size when allocating VirtIOSoundPCMBuffer.
124 struct VirtIOSoundPCM
{
127 * PCM parameters are a separate field instead of a VirtIOSoundPCMStream
128 * field, because the operation of PCM control requests is first
129 * VIRTIO_SND_R_PCM_SET_PARAMS and then VIRTIO_SND_R_PCM_PREPARE; this
130 * means that some times we get parameters without having an allocated
133 virtio_snd_pcm_set_params
*pcm_params
;
134 VirtIOSoundPCMStream
**streams
;
137 struct VirtIOSoundPCMStream
{
139 virtio_snd_pcm_info info
;
140 virtio_snd_pcm_set_params params
;
142 /* channel position values (VIRTIO_SND_CHMAP_XXX) */
143 uint8_t positions
[VIRTIO_SND_CHMAP_MAX_SIZE
];
151 QemuMutex queue_mutex
;
153 QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer
) queue
;
157 * PCM stream state machine.
158 * -------------------------
160 * 5.14.6.6.1 PCM Command Lifecycle
161 * ================================
163 * A PCM stream has the following command lifecycle:
165 * The driver negotiates the stream parameters (format, transport, etc) with
167 * Possible valid transitions: `SET PARAMETERS`, `PREPARE`.
169 * The device prepares the stream (allocates resources, etc).
170 * Possible valid transitions: `SET PARAMETERS`, `PREPARE`, `START`,
171 * `RELEASE`. Output only: the driver transfers data for pre-buffing.
173 * The device starts the stream (unmute, putting into running state, etc).
174 * Possible valid transitions: `STOP`.
175 * The driver transfers data to/from the stream.
177 * The device stops the stream (mute, putting into non-running state, etc).
178 * Possible valid transitions: `START`, `RELEASE`.
180 * The device releases the stream (frees resources, etc).
181 * Possible valid transitions: `SET PARAMETERS`, `PREPARE`.
183 * +---------------+ +---------+ +---------+ +-------+ +-------+
184 * | SetParameters | | Prepare | | Release | | Start | | Stop |
185 * +---------------+ +---------+ +---------+ +-------+ +-------+
189 * |------------->| | | |
190 * |<-------------| | | |
194 * | |--------------------->| |
195 * | |---------->| | |
198 * | | |<-------------------|
199 * |<-------------------------| | |
200 * | |<----------| | |
202 * CTRL in the VirtIOSound device
203 * ==============================
205 * The control messages that affect the state of a stream arrive in the
206 * `virtio_snd_handle_ctrl()` queue callback and are of type `struct
207 * virtio_snd_ctrl_command`. They are stored in a queue field in the device
208 * type, `VirtIOSound`. This allows deferring the CTRL request completion if
209 * it's not immediately possible due to locking/state reasons.
211 * The CTRL message is finally handled in `process_cmd()`.
214 VirtIODevice parent_obj
;
216 VirtQueue
*queues
[VIRTIO_SND_VQ_MAX
];
220 VMChangeStateEntry
*vmstate
;
221 virtio_snd_config snd_conf
;
222 QemuMutex cmdq_mutex
;
223 QTAILQ_HEAD(, virtio_snd_ctrl_command
) cmdq
;
224 bool processing_cmdq
;
226 * Convenience queue to keep track of invalid tx/rx queue messages inside
227 * the tx/rx callbacks.
229 * In the callbacks as a first step we are emptying the virtqueue to handle
230 * each message and we cannot add an invalid message back to the queue: we
231 * would re-process it in subsequent loop iterations.
233 * Instead, we add them to this queue and after finishing examining every
234 * virtqueue element, we inform the guest for each invalid message.
236 * This queue must be empty at all times except for inside the tx/rx
239 QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer
) invalid
;
242 struct virtio_snd_ctrl_command
{
243 VirtQueueElement
*elem
;
248 QTAILQ_ENTRY(virtio_snd_ctrl_command
) next
;