Add VIRTIO sound card
v11 cover Add VIRTIO sound card
From: Emmanouil Pitsidianakis <[email protected]>
This patch series adds an audio device implementing the recent virtio sound spec (1.2) and a corresponding PCI wrapper device.
v11 can be found online at:
https://gitlab.com/epilys/qemu/-/tree/virtio-snd-v11
Ref 885b01fe272541fdab5583780d4c3a59bfd8e734
Main differences with v10 patch series [^v10] <[email protected]>:
- Rebased against upstream, which has minor changes to the AUD_* API.
- Fixed noise in playback because of invalid bounds when accessing the audio data in the VirtQueueElement.
- Refactor invalid I/O message queue flushing into separate function.
- Removed attempt to write unwritten bytes to QEMU sound backend when
flushing: it should only happen when the stream STARTs.
- Set latency_bytes to buffer size when returning TX I/O message because it happens immediately after writing the last bytes to the QEMU backend, therefore there might be up to <buffer size> bytes to be played before all the buffer data has finished playing.
- Addressed [^v10] review comments:
- Refactored VirtIOSoundPCMBuffer return code into a function instead of using goto labels in output/input audio callbacks. (Suggested by <[email protected]>)
Previously:
[^v10]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v9]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v8]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v7]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v6]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v5]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v4]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v3]:
https://lore.kernel.org/qemu-devel/[email protected]/
Emmanouil Pitsidianakis (11):
Add virtio-sound device stub
Add virtio-sound-pci device
virtio-sound: handle control messages and streams
virtio-sound: handle VIRTIO_SND_R_PCM_INFO request
virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP}
virtio-sound: handle VIRTIO_SND_R_PCM_SET_PARAMS
virtio-sound: handle VIRTIO_SND_R_PCM_PREPARE
virtio-sound: handle VIRTIO_SND_R_PCM_RELEASE
virtio-sound: implement audio output (TX)
virtio-sound: implement audio capture (RX) docs/system: add basic virtio-snd documentation
MAINTAINERS | 7 + docs/system/device-emulation.rst | 1 + docs/system/devices/virtio-snd.rst (new) | 49 + hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 2 + hw/virtio/trace-events | 20 + hw/virtio/virtio-snd-pci.c (new) | 93 ++ hw/virtio/virtio-snd.c (new) | 1409 ++++++++++++++++++++++ include/hw/virtio/virtio-snd.h (new) | 235 ++++ system/qdev-monitor.c | 1 + 10 files changed, 1822 insertions(+) create mode 100644 docs/system/devices/virtio-snd.rst create mode 100644 hw/virtio/virtio-snd-pci.c create mode 100644 hw/virtio/virtio-snd.c create mode 100644 include/hw/virtio/virtio-snd.h
Range-diff against v10:
1: 6e7bdf6dda ! 1: 03ecf1f615 Add virtio-sound device stub
@@ hw/virtio/virtio-snd.c (new)
+ return;
+ }
+
-+ AUD_register_card("virtio-sound", &vsnd->card);
++ AUD_register_card("virtio-sound", &vsnd->card, errp);
+
+ vsnd->queues[VIRTIO_SND_VQ_CONTROL] =
+ virtio_add_queue(vdev, 64, virtio_snd_handle_queue); 2: 82138b9c7d ! 2: ba49f45eb3 Add virtio-sound-pci device
@@ hw/virtio/virtio-snd.c: virtio_snd_set_config(VirtIODevice *vdev, const uint8_t
/*
- ## softmmu/qdev-monitor.c ##
-@@ softmmu/qdev-monitor.c: static const QDevAlias qdev_alias_table[] = {
+ ## system/qdev-monitor.c ##
+@@ system/qdev-monitor.c: static const QDevAlias qdev_alias_table[] = {
{ "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO }, { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW }, { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI},
3: c1a2cb0304 ! 3: 5831b5cfa5 virtio-sound: handle control messages and streams
@@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error *
@@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error **errp)
- AUD_register_card("virtio-sound", &vsnd->card);
+ AUD_register_card("virtio-sound", &vsnd->card, errp);
+ /* set default params for all streams */
+ default_params.features = 0;
4: 28b2ecfa1f = 4: 425cbc2986 virtio-sound: handle VIRTIO_SND_R_PCM_INFO request
5: a52d20b2c3 = 5: d1403721fa virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP}
6: 25fbb2eb25 = 6: 68ac43df35 virtio-sound: handle VIRTIO_SND_R_PCM_SET_PARAMS
7: 9e8d9923ba = 7: 175e6fbe75 virtio-sound: handle VIRTIO_SND_R_PCM_PREPARE
8: b50c94decc = 8: 93a5b96b58 virtio-sound: handle VIRTIO_SND_R_PCM_RELEASE
9: 4cbb908742 ! 9: 34be52e56e virtio-sound: implement audio output (TX)
@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_event(VirtIODevice *vdev,
+ if (!virtio_queue_ready(vq)) {
+ return;
+ }
-+ trace_virtio_snd_handle_tx_xfer();
++ trace_virtio_snd_handle_xfer();
+
+ for (;;) {
+ elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_event(VirtIODevice *vdev,
+ }
+
+ WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
-+ size = iov_size(elem->out_sg, elem->out_num);
++ size = iov_size(elem->out_sg, elem->out_num) - msg_sz;
+
+ buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size);
+ buffer->elem = elem;
-+ buffer->stale = true;
++ buffer->populated = false;
+ buffer->vq = vq;
+ buffer->size = size;
-+ buffer->offset = sizeof(virtio_snd_pcm_xfer);
++ buffer->offset = 0;
+
+ QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry);
+ }
@@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error *
}
}
++static inline void return_tx_buffer(VirtIOSoundPCMStream *stream,
++ VirtIOSoundPCMBuffer *buffer)
++{
++ virtio_snd_pcm_status resp = { 0 };
++ resp.status = cpu_to_le32(VIRTIO_SND_S_OK);
++ resp.latency_bytes = cpu_to_le32((uint32_t)buffer->size);
++ iov_from_buf(buffer->elem->in_sg,
++ buffer->elem->in_num,
++ 0,
++ &resp,
++ sizeof(virtio_snd_pcm_status));
++ virtqueue_push(buffer->vq,
++ buffer->elem,
++ sizeof(virtio_snd_pcm_status));
++ virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq);
++ QSIMPLEQ_REMOVE(&stream->queue,
++ buffer,
++ VirtIOSoundPCMBuffer,
++ entry);
++ virtio_snd_pcm_buffer_free(buffer);
++}
++
+/*
+ * AUD_* output callback.
+ *
@@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error *
+ }
+ if (!stream->active) {
+ /* Stream has stopped, so do not perform AUD_write. */
-+ goto return_tx_buffer;
++ return_tx_buffer(stream, buffer);
++ continue;
+ }
-+ if (buffer->stale) {
++ if (!buffer->populated) {
+ iov_to_buf(buffer->elem->out_sg,
+ buffer->elem->out_num,
-+ buffer->offset,
++ sizeof(virtio_snd_pcm_xfer),
+ buffer->data,
+ buffer->size);
-+ buffer->stale = false;
++ buffer->populated = true;
+ }
+ for (;;) {
+ size = AUD_write(stream->voice.out,
@@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error *
+ buffer->offset += size;
+ available -= size;
+ if (buffer->size < 1) {
-+return_tx_buffer:
-+ virtio_snd_pcm_status resp = { 0 };
-+ resp.status = cpu_to_le32(VIRTIO_SND_S_OK);
-+ resp.latency_bytes = 0;
-+ iov_from_buf(buffer->elem->in_sg,
-+ buffer->elem->in_num,
-+ 0,
-+ &resp,
-+ sizeof(virtio_snd_pcm_status));
-+ virtqueue_push(buffer->vq,
-+ buffer->elem,
-+ sizeof(virtio_snd_pcm_status));
-+ virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq);
-+ QSIMPLEQ_REMOVE_HEAD(&stream->queue, entry);
-+ virtio_snd_pcm_buffer_free(buffer);
++ return_tx_buffer(stream, buffer);
+ break;
+ }
+ if (!available) {
@@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error *
+ *
+ * @stream: VirtIOSoundPCMStream *stream
+ */
-+static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream)
++static inline void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream)
+{
-+ VirtIOSoundPCMBuffer *buffer, *next;
++ VirtIOSoundPCMBuffer *buffer;
+
+ WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
-+ QSIMPLEQ_FOREACH_SAFE(buffer, &stream->queue, entry, next) {
-+ AUD_write(stream->voice.out,
-+ buffer->data + buffer->offset,
-+ buffer->size);
-+ virtqueue_push(buffer->vq,
-+ buffer->elem,
-+ sizeof(VirtQueueElement));
-+ virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq);
-+ QSIMPLEQ_REMOVE(&stream->queue,
-+ buffer,
-+ VirtIOSoundPCMBuffer,
-+ entry);
-+ virtio_snd_pcm_buffer_free(buffer);
++ while (!QSIMPLEQ_EMPTY(&stream->queue)) {
++ buffer = QSIMPLEQ_FIRST(&stream->queue);
++ return_tx_buffer(stream, buffer);
+ }
+ }
+}
@@ include/hw/virtio/virtio-snd.h: typedef struct virtio_snd_ctrl_command virtio_sn
+ *
+ * In the case of TX (i.e. playback) buffers, we defer reading the raw PCM data
+ * from the virtqueue until QEMU's sound backsystem calls the output callback.
-+ * This is tracked by the `bool stale;` field, which is set to false when data
-+ * has been read into our own buffer for consumption.
++ * This is tracked by the `bool populated;` field, which is set to true when
++ * data has been read into our own buffer for consumption.
+ *
+ * VirtIOSoundPCMBuffer has a dynamic size since it includes the raw PCM data
+ * in its allocation. It must be initialized and destroyed as follows:
@@ include/hw/virtio/virtio-snd.h: typedef struct virtio_snd_ctrl_command virtio_sn
+ */
+ uint64_t offset;
+ /* Used for the TX queue for lazy I/O copy from `elem` */
-+ bool stale;
++ bool populated;
+ /*
+ * VirtIOSoundPCMBuffer is an unsized type because it ends with an array of
+ * bytes. The size of `data` is determined from the I/O message's read-only
10: 992b0d5ff4 ! 10: 68f9e0f298 virtio-sound: implement audio capture (RX)
@@ hw/virtio/virtio-snd.c
static void virtio_snd_pcm_out_cb(void *data, int available);
static void virtio_snd_process_cmdq(VirtIOSound *s);
--static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream);
-+static void virtio_snd_pcm_out_flush(VirtIOSoundPCMStream *stream);
-+static void virtio_snd_pcm_in_flush(VirtIOSoundPCMStream *stream);
+ static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream);
+static void virtio_snd_pcm_in_cb(void *data, int available);
static uint32_t supported_formats = BIT(VIRTIO_SND_PCM_FMT_S8)
| BIT(VIRTIO_SND_PCM_FMT_U8)
-@@ hw/virtio/virtio-snd.c: static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
+@@ hw/virtio/virtio-snd.c: static void virtio_snd_get_qemu_audsettings(audsettings *as,
+ static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
{
if (stream) {
++ virtio_snd_pcm_flush(stream);
if (stream->info.direction == VIRTIO_SND_D_OUTPUT) {
- virtio_snd_pcm_flush(stream);
-+ virtio_snd_pcm_out_flush(stream);
AUD_close_out(&stream->pcm->snd->card, stream->voice.out);
stream->voice.out = NULL;
+ } else if (stream->info.direction == VIRTIO_SND_D_INPUT) {
-+ virtio_snd_pcm_in_flush(stream);
+ AUD_close_in(&stream->pcm->snd->card, stream->voice.in);
+ stream->voice.in = NULL;
}
@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_pcm_start_stop(VirtIOSound
}
} else {
error_report("Invalid stream id: %"PRIu32, stream_id);
-@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_pcm_release(VirtIOSound *s,
- * are pending I/O messages for the specified stream ID.
- */
- trace_virtio_snd_pcm_stream_flush(stream_id);
-- virtio_snd_pcm_flush(stream);
-+ if (stream->info.direction == VIRTIO_SND_D_OUTPUT) {
-+ virtio_snd_pcm_out_flush(stream);
-+ } else {
-+ virtio_snd_pcm_in_flush(stream);
-+ }
- }
+@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_event(VirtIODevice *vdev, VirtQueue *vq)
+ trace_virtio_snd_handle_event();
+ }
- cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_OK);
++static inline void empty_invalid_queue(VirtIODevice *vdev, VirtQueue *vq)
++{
++ VirtIOSoundPCMBuffer *buffer = NULL;
++ VirtIOSoundPCMStream *stream = NULL;
++ virtio_snd_pcm_status resp = { 0 };
++ VirtIOSound *vsnd = VIRTIO_SND(vdev);
++ bool any = false;
++
++ for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) {
++ stream = vsnd->pcm->streams[i];
++ if (stream) {
++ any = false;
++ WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
++ while (!QSIMPLEQ_EMPTY(&stream->invalid)) {
++ buffer = QSIMPLEQ_FIRST(&stream->invalid);
++ if (buffer->vq != vq) {
++ break;
++ }
++ any = true;
++ resp.status = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
++ iov_from_buf(buffer->elem->in_sg,
++ buffer->elem->in_num,
++ 0,
++ &resp,
++ sizeof(virtio_snd_pcm_status));
++ virtqueue_push(vq,
++ buffer->elem,
++ sizeof(virtio_snd_pcm_status));
++ QSIMPLEQ_REMOVE_HEAD(&stream->invalid, entry);
++ virtio_snd_pcm_buffer_free(buffer);
++ }
++ if (any) {
++ /*
++ * Notify vq about virtio_snd_pcm_status responses.
++ * Buffer responses must be notified separately later.
++ */
++ virtio_notify(vdev, vq);
++ }
++ }
++ }
++ }
++}
++
+ /*
+ * The tx virtqueue handler. Makes the buffers available to their respective
+ * streams for consumption.
@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_event(VirtIODevice *vdev, VirtQueue *vq)
* @vdev: VirtIOSound device
* @vq: tx virtqueue
@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_event(VirtIODevice *vdev,
{
VirtIOSound *s = VIRTIO_SND(vdev);
VirtIOSoundPCMStream *stream = NULL;
+@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
+ VirtQueueElement *elem;
+ size_t msg_sz, size;
+ virtio_snd_pcm_xfer hdr;
+- virtio_snd_pcm_status resp = { 0 };
+ uint32_t stream_id;
+ /*
+ * If any of the I/O messages are invalid, put them in stream->invalid and
+@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
+ if (!virtio_queue_ready(vq)) {
+ return;
+ }
+- trace_virtio_snd_handle_xfer();
++ trace_virtio_snd_handle_tx_xfer();
+
+ for (;;) {
+ elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
@@ hw/virtio/virtio-snd.c: tx_err:
+ }
+
+ if (must_empty_invalid_queue) {
+- WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
+- while (!QSIMPLEQ_EMPTY(&stream->invalid)) {
+- buffer = QSIMPLEQ_FIRST(&stream->invalid);
+-
+- resp.status = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
+- iov_from_buf(buffer->elem->in_sg,
+- buffer->elem->in_num,
+- 0,
+- &resp,
+- sizeof(virtio_snd_pcm_status));
+- virtqueue_push(vq, buffer->elem, sizeof(virtio_snd_pcm_status));
+- QSIMPLEQ_REMOVE_HEAD(&stream->invalid, entry);
+- virtio_snd_pcm_buffer_free(buffer);
+- }
+- /*
+- * Notify vq about virtio_snd_pcm_status responses.
+- * Buffer responses must be notified separately later.
+- */
+- virtio_notify(vdev, vq);
+- }
++ empty_invalid_queue(vdev, vq);
+ }
}
/*
@@ hw/virtio/virtio-snd.c: tx_err:
*
* @vdev: VirtIOSound device
- * @vq: virtqueue
-+ * @vq: tx virtqueue
++ * @vq: rx virtqueue
*/
-static void virtio_snd_handle_xfer(VirtIODevice *vdev, VirtQueue *vq) {}
+static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq)
@@ hw/virtio/virtio-snd.c: tx_err:
+ VirtQueueElement *elem;
+ size_t msg_sz, size;
+ virtio_snd_pcm_xfer hdr;
-+ virtio_snd_pcm_status resp = { 0 };
+ uint32_t stream_id;
+ /*
+ * if any of the I/O messages are invalid, put them in stream->invalid and
@@ hw/virtio/virtio-snd.c: tx_err:
+ }
+
+ if (must_empty_invalid_queue) {
-+ WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
-+ while (!QSIMPLEQ_EMPTY(&stream->invalid)) {
-+ buffer = QSIMPLEQ_FIRST(&stream->invalid);
-+
-+ resp.status = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
-+ iov_from_buf(buffer->elem->in_sg,
-+ buffer->elem->in_num,
-+ 0,
-+ &resp,
-+ sizeof(virtio_snd_pcm_status));
-+ virtqueue_push(vq, buffer->elem, sizeof(virtio_snd_pcm_status));
-+ QSIMPLEQ_REMOVE_HEAD(&stream->invalid, entry);
-+ virtio_snd_pcm_buffer_free(buffer);
-+ }
-+ /*
-+ * Notify vq about virtio_snd_pcm_status responses.
-+ * Buffer responses must be notified separately later.
-+ */
-+ virtio_notify(vdev, vq);
-+ }
++ empty_invalid_queue(vdev, vq);
+ }
+}
@@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error *
qemu_mutex_init(&vsnd->cmdq_mutex);
QTAILQ_INIT(&vsnd->cmdq);
-@@ hw/virtio/virtio-snd.c: return_tx_buffer:
+@@ hw/virtio/virtio-snd.c: static void virtio_snd_pcm_out_cb(void *data, int available)
}
/*
- * Flush all buffer data from this stream's queue into the driver's virtual
- * queue.
++ * Flush all buffer data from this input stream's queue into the driver's
++ * virtual queue.
++ *
++ * @stream: VirtIOSoundPCMStream *stream
++ */
++static inline void return_rx_buffer(VirtIOSoundPCMStream *stream,
++ VirtIOSoundPCMBuffer *buffer)
++{
++ virtio_snd_pcm_status resp = { 0 };
++ resp.status = cpu_to_le32(VIRTIO_SND_S_OK);
++ resp.latency_bytes = 0;
++ /* Copy data -if any- to guest */
++ iov_from_buf(buffer->elem->in_sg,
++ buffer->elem->in_num,
++ 0,
++ buffer->data,
++ buffer->size);
++ iov_from_buf(buffer->elem->in_sg,
++ buffer->elem->in_num,
++ buffer->size,
++ &resp,
++ sizeof(virtio_snd_pcm_status));
++ virtqueue_push(buffer->vq,
++ buffer->elem,
++ sizeof(virtio_snd_pcm_status) + buffer->size);
++ virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq);
++ QSIMPLEQ_REMOVE(&stream->queue,
++ buffer,
++ VirtIOSoundPCMBuffer,
++ entry);
++ virtio_snd_pcm_buffer_free(buffer);
++}
++
++
++/*
+ * AUD_* input callback.
- *
-- * @stream: VirtIOSoundPCMStream *stream
++ *
+ * @data: VirtIOSoundPCMStream stream
+ * @available: number of bytes that can be read with AUD_read()
- */
--static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream)
++ */
+static void virtio_snd_pcm_in_cb(void *data, int available)
- {
-- VirtIOSoundPCMBuffer *buffer, *next;
++{
+ VirtIOSoundPCMStream *stream = data;
+ VirtIOSoundPCMBuffer *buffer;
-+ virtio_snd_pcm_status resp = { 0 };
+ size_t size;
-
- WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
-- QSIMPLEQ_FOREACH_SAFE(buffer, &stream->queue, entry, next) {
-- AUD_write(stream->voice.out,
-- buffer->data + buffer->offset,
-- buffer->size);
-- virtqueue_push(buffer->vq,
-- buffer->elem,
-- sizeof(VirtQueueElement));
-- virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq);
-- QSIMPLEQ_REMOVE(&stream->queue,
-- buffer,
-- VirtIOSoundPCMBuffer,
-- entry);
-- virtio_snd_pcm_buffer_free(buffer);
++
++ WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
+ while (!QSIMPLEQ_EMPTY(&stream->queue)) {
+ buffer = QSIMPLEQ_FIRST(&stream->queue);
+ if (!virtio_queue_ready(buffer->vq)) {
@@ hw/virtio/virtio-snd.c: return_tx_buffer:
+ }
+ if (!stream->active) {
+ /* Stream has stopped, so do not perform AUD_read. */
-+ goto return_rx_buffer;
++ return_rx_buffer(stream, buffer);
++ continue;
+ }
+
+ for (;;) {
@@ hw/virtio/virtio-snd.c: return_tx_buffer:
+ buffer->size += size;
+ available -= size;
+ if (buffer->size >= stream->params.period_bytes) {
-+return_rx_buffer:
-+ resp.status = cpu_to_le32(VIRTIO_SND_S_OK);
-+ resp.latency_bytes = 0;
-+ /* Copy data -if any- to guest */
-+ iov_from_buf(buffer->elem->in_sg,
-+ buffer->elem->in_num,
-+ 0,
-+ buffer->data,
-+ buffer->size);
-+ iov_from_buf(buffer->elem->in_sg,
-+ buffer->elem->in_num,
-+ buffer->size,
-+ &resp,
-+ sizeof(resp));
-+ virtqueue_push(buffer->vq,
-+ buffer->elem,
-+ sizeof(virtio_snd_pcm_status) +
-+ buffer->size);
-+ virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq);
-+ QSIMPLEQ_REMOVE_HEAD(&stream->queue, entry);
-+ virtio_snd_pcm_buffer_free(buffer);
++ return_rx_buffer(stream, buffer);
+ break;
+ }
+ if (!available) {
@@ hw/virtio/virtio-snd.c: return_tx_buffer:
+ if (!available) {
+ break;
+ }
- }
- }
- }
-
-+#define virtio_snd_pcm_flush(AUD_CB) \
-+ VirtIOSoundPCMBuffer *buffer; \
-+ virtio_snd_pcm_status resp = { 0 }; \
-+ unsigned int len = 0; \
-+ resp.status = cpu_to_le32(VIRTIO_SND_S_OK); \
-+ WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { \
-+ while (!QSIMPLEQ_EMPTY(&stream->queue)) { \
-+ buffer = QSIMPLEQ_FIRST(&stream->queue); \
-+ do { \
-+ AUD_CB; \
-+ } while (0) \
-+ ; \
-+ virtqueue_push(buffer->vq, buffer->elem, len); \
-+ virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq); \
-+ QSIMPLEQ_REMOVE_HEAD(&stream->queue, entry); \
-+ virtio_snd_pcm_buffer_free(buffer); \
-+ } \
++ }
+ }
-+
-+
-+/*
-+ * Flush all buffer data from this output stream's queue into the driver's
-+ * virtual queue.
-+ *
-+ * @stream: VirtIOSoundPCMStream *stream
-+ */
-+static void virtio_snd_pcm_out_flush(VirtIOSoundPCMStream *stream)
-+{
-+ /*
-+ * We should flush the buffers as soon as possible, because it is a
-+ * time-sensitive operation.
-+ *
-+ * TODO: find out if copying leftover flushed data to an intermediate
-+ * buffer is a good approach.
-+ */
-+ size_t written;
-+ virtio_snd_pcm_flush(
-+ if (stream->active && buffer->stale) {
-+ iov_to_buf(buffer->elem->out_sg,
-+ buffer->elem->out_num,
-+ buffer->offset,
-+ buffer->data,
-+ buffer->size);
-+ buffer->stale = false;
-+ }
-+ if (stream->active)
-+ while (buffer->size > 0) {
-+ written = AUD_write(stream->voice.out,
-+ buffer->data + buffer->offset,
-+ buffer->size);
-+ if (written < 1) {
-+ break;
-+ }
-+ buffer->size -= written;
-+ buffer->offset += written;
-+ }
-+ len = sizeof(virtio_snd_pcm_status);
-+ iov_from_buf(buffer->elem->in_sg,
-+ buffer->elem->in_num,
-+ 0,
-+ &resp,
-+ sizeof(virtio_snd_pcm_status));
-+ );
+}
+
+/*
-+ * Flush all buffer data from this input stream's queue into the driver's
++ * Flush all buffer data from this output stream's queue into the driver's
+ * virtual queue.
-+ *
-+ * @stream: VirtIOSoundPCMStream *stream
-+ */
-+static void virtio_snd_pcm_in_flush(VirtIOSoundPCMStream *stream)
-+{
-+ virtio_snd_pcm_flush(
-+ len = sizeof(virtio_snd_pcm_status) + buffer->size;
-+ iov_from_buf(buffer->elem->in_sg,
-+ buffer->elem->in_num,
-+ 0,
-+ buffer->data,
-+ buffer->size);
-+ iov_from_buf(buffer->elem->in_sg,
-+ buffer->elem->in_num,
-+ buffer->size,
-+ &resp,
-+ sizeof(virtio_snd_pcm_status));
-+ );
-+}
-+
- static void virtio_snd_unrealize(DeviceState *dev)
+ *
+ * @stream: VirtIOSoundPCMStream *stream
+ */
+ static inline void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream)
{
- VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+ VirtIOSoundPCMBuffer *buffer;
++ void (*cb)(VirtIOSoundPCMStream *, VirtIOSoundPCMBuffer *) =
++ (stream->info.direction == VIRTIO_SND_D_OUTPUT) ? return_tx_buffer :
++ return_rx_buffer;
+
+ WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
+ while (!QSIMPLEQ_EMPTY(&stream->queue)) {
+ buffer = QSIMPLEQ_FIRST(&stream->queue);
+- return_tx_buffer(stream, buffer);
++ cb(stream, buffer);
+ }
+ }
+ }11: b720e00121 = 11: 885b01fe27 docs/system: add basic virtio-snd documentation
base-commit: cea3ea670fe265421131aad90c36fbb87bc4d206
Signature · 1 line
2.39.2
PATCH v11 1/11 Add virtio-sound device stub
From: Emmanouil Pitsidianakis <[email protected]>
Add a new VIRTIO device for the virtio sound device id. Functionality will be added in the following commits.
Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471
--- MAINTAINERS | 6 + hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 1 + hw/virtio/trace-events | 9 ++ hw/virtio/virtio-snd.c (new) | 223 +++++++++++++++++++++++++++ include/hw/virtio/virtio-snd.h (new) | 79 ++++++++++ 6 files changed, 323 insertions(+)
MAINTAINERS +6 -0
@@ -2254,6 +2254,12 @@ F: hw/virtio/virtio-mem-pci.h F: hw/virtio/virtio-mem-pci.c F: include/hw/virtio/virtio-mem.h +virtio-snd +M: Manos Pitsidianakis <[email protected]> +S: Supported +F: hw/virtio/virtio-snd*.c +F: include/hw/virtio/virtio-snd.h + nvme M: Keith Busch <[email protected]> M: Klaus Jensen <[email protected]>
hw/virtio/Kconfig +5 -0
@@ -17,6 +17,11 @@ config VIRTIO_PCI depends on PCI select VIRTIO +config VIRTIO_SND + bool + default y + depends on VIRTIO + config VIRTIO_MMIO bool select VIRTIO
hw/virtio/meson.build +1 -0
@@ -35,6 +35,7 @@ specific_virtio_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock.c specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_VSOCK', if_true: files('vhost-user-vsock.c')) specific_virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c')) specific_virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c')) +specific_virtio_ss.add(when: 'CONFIG_VIRTIO_SND', if_true: files('virtio-snd.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2c.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_RNG', if_true: files('vhost-user-rng.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_GPIO', if_true: files('vhost-user-gpio.c'))
hw/virtio/trace-events +9 -0
@@ -154,3 +154,12 @@ virtio_pmem_flush_done(int type) "fsync return=%d" virtio_gpio_start(void) "start" virtio_gpio_stop(void) "stop" virtio_gpio_set_status(uint8_t status) "0x%x" + +#virtio-snd.c +virtio_snd_get_config(void *vdev, uint32_t jacks, uint32_t streams, uint32_t chmaps) "snd %p: get_config jacks=%"PRIu32" streams=%"PRIu32" chmaps=%"PRIu32"" +virtio_snd_set_config(void *vdev, uint32_t jacks, uint32_t new_jacks, uint32_t streams, uint32_t new_streams, uint32_t chmaps, uint32_t new_chmaps) "snd %p: set_config jacks from %"PRIu32"->%"PRIu32", streams from %"PRIu32"->%"PRIu32", chmaps from %"PRIu32"->%"PRIu32 +virtio_snd_get_features(void *vdev, uint64_t features) "snd %p: get_features 0x%"PRIx64 +virtio_snd_vm_state_running(void) "vm state running" +virtio_snd_vm_state_stopped(void) "vm state stopped" +virtio_snd_realize(void *snd) "snd %p: realize" +virtio_snd_unrealize(void *snd) "snd %p: unrealize"
hw/virtio/virtio-snd.c new file +223 -0
223 lines not on this page · raw message
include/hw/virtio/virtio-snd.h new file +79 -0
79 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH v11 2/11 Add virtio-sound-pci device
In reply to Manos PitsidianakisFrom: Emmanouil Pitsidianakis <[email protected]>
This patch adds a PCI wrapper device for the virtio-sound device. It is necessary to instantiate a virtio-snd device in a guest. All sound logic will be added to the virtio-snd device in the following commits.
To add this device with a guest, you'll need a >=5.13 kernel compiled with CONFIG_SND_VIRTIO=y, which at the time of writing most distros have off by default.
Use with following flags in the invocation:
Pulseaudio:
-audio driver=pa,model=virtio
or
-audio driver=pa,model=virtio,server=/run/user/1000/pulse/native
sdl:
-audio driver=sdl,model=virtio
coreaudio (macos/darwin):
-audio driver=coreaudio,model=virtio
etc.
Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471
--- hw/virtio/meson.build | 1 + hw/virtio/virtio-snd-pci.c (new) | 93 ++++++++++++++++++++++++++++++++ hw/virtio/virtio-snd.c | 14 ++++- system/qdev-monitor.c | 1 + 4 files changed, 107 insertions(+), 2 deletions(-)
hw/virtio/meson.build +1 -0
@@ -67,6 +67,7 @@ virtio_pci_ss.add(when: 'CONFIG_VIRTIO_SERIAL', if_true: files('virtio-serial-pc virtio_pci_ss.add(when: 'CONFIG_VIRTIO_PMEM', if_true: files('virtio-pmem-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem-pci.c')) +virtio_pci_ss.add(when: 'CONFIG_VIRTIO_SND', if_true: files('virtio-snd-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VHOST_VDPA_DEV', if_true: files('vdpa-dev-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_MD', if_true: files('virtio-md-pci.c'))
hw/virtio/virtio-snd-pci.c new file +93 -0
93 lines not on this page · raw message
hw/virtio/virtio-snd.c +12 -2
@@ -63,36 +63,46 @@ static void virtio_snd_get_config(VirtIODevice *vdev, uint8_t *config) {VirtIOSound *s =VIRTIO_SND (vdev); +virtio_snd_config *sndconfig = + (virtio_snd_config *)config;trace_virtio_snd_get_config (vdev, s->snd_conf.jacks, s->snd_conf.streams, s->snd_conf.chmaps); -memcpy (config, &s->snd_conf,sizeof (s->snd_conf)); +memcpy (sndconfig, &s->snd_conf,sizeof (s->snd_conf)); +cpu_to_le32s (&sndconfig->jacks); +cpu_to_le32s (&sndconfig->streams); +cpu_to_le32s (&sndconfig->chmaps); + }static void virtio_snd_set_config (VirtIODevice *vdev,const uint8_t *config) {VirtIOSound *s =VIRTIO_SND (vdev);const virtio_snd_config *sndconfig = (const virtio_snd_config *)config;trace_virtio_snd_set_config (vdev, s->snd_conf.jacks, sndconfig->jacks, s->snd_conf.streams, sndconfig->streams, s->snd_conf.chmaps, sndconfig->chmaps); -memcpy (&s->snd_conf, sndconfig,sizeof (s->snd_conf)); +memcpy (&s->snd_conf, sndconfig,sizeof (virtio_snd_config)); +le32_to_cpus (&s->snd_conf.jacks); +le32_to_cpus (&s->snd_conf.streams); +le32_to_cpus (&s->snd_conf.chmaps); + }/* * Queue handler stub. * * @vdev: VirtIOSound device * @vq: virtqueue */
system/qdev-monitor.c +1 -0
@@ -68,48 +68,49 @@ typedef struct QDevAlias/* Please keep this table sorted by typename. */ static const QDevAlias qdev_alias_table[] = { {"AC97" ,"ac97" },/* -soundhw name */ {"e1000" ,"e1000-82540em" }, {"ES1370" ,"es1370" },/* -soundhw name */ {"ich9-ahci" ,"ahci" }, {"lsi53c895a" ,"lsi" }, {"virtio-9p-device" ,"virtio-9p" ,QEMU_ARCH_VIRTIO_MMIO }, {"virtio-9p-ccw" ,"virtio-9p" ,QEMU_ARCH_VIRTIO_CCW }, {"virtio-9p-pci" ,"virtio-9p" ,QEMU_ARCH_VIRTIO_PCI }, {"virtio-balloon-device" ,"virtio-balloon" ,QEMU_ARCH_VIRTIO_MMIO }, {"virtio-balloon-ccw" ,"virtio-balloon" ,QEMU_ARCH_VIRTIO_CCW }, {"virtio-balloon-pci" ,"virtio-balloon" ,QEMU_ARCH_VIRTIO_PCI }, {"virtio-blk-device" ,"virtio-blk" ,QEMU_ARCH_VIRTIO_MMIO }, {"virtio-blk-ccw" ,"virtio-blk" ,QEMU_ARCH_VIRTIO_CCW }, {"virtio-blk-pci" ,"virtio-blk" ,QEMU_ARCH_VIRTIO_PCI }, {"virtio-gpu-device" ,"virtio-gpu" ,QEMU_ARCH_VIRTIO_MMIO }, {"virtio-gpu-ccw" ,"virtio-gpu" ,QEMU_ARCH_VIRTIO_CCW }, {"virtio-gpu-pci" ,"virtio-gpu" ,QEMU_ARCH_VIRTIO_PCI }, {"virtio-gpu-gl-device" ,"virtio-gpu-gl" ,QEMU_ARCH_VIRTIO_MMIO }, {"virtio-gpu-gl-pci" ,"virtio-gpu-gl" ,QEMU_ARCH_VIRTIO_PCI }, {"virtio-input-host-device" ,"virtio-input-host" ,QEMU_ARCH_VIRTIO_MMIO }, {"virtio-input-host-ccw" ,"virtio-input-host" ,QEMU_ARCH_VIRTIO_CCW }, {"virtio-input-host-pci" ,"virtio-input-host" ,QEMU_ARCH_VIRTIO_PCI }, {"virtio-iommu-pci" ,"virtio-iommu" ,QEMU_ARCH_VIRTIO_PCI }, {"virtio-keyboard-device" ,"virtio-keyboard" ,QEMU_ARCH_VIRTIO_MMIO }, {"virtio-keyboard-ccw" ,"virtio-keyboard" ,QEMU_ARCH_VIRTIO_CCW }, {"virtio-keyboard-pci" ,"virtio-keyboard" ,QEMU_ARCH_VIRTIO_PCI }, {"virtio-mouse-device" ,"virtio-mouse" ,QEMU_ARCH_VIRTIO_MMIO }, {"virtio-mouse-ccw" ,"virtio-mouse" ,QEMU_ARCH_VIRTIO_CCW }, {"virtio-mouse-pci" ,"virtio-mouse" ,QEMU_ARCH_VIRTIO_PCI }, {"virtio-net-device" ,"virtio-net" ,QEMU_ARCH_VIRTIO_MMIO }, {"virtio-net-ccw" ,"virtio-net" ,QEMU_ARCH_VIRTIO_CCW }, {"virtio-net-pci" ,"virtio-net" ,QEMU_ARCH_VIRTIO_PCI }, {"virtio-rng-device" ,"virtio-rng" ,QEMU_ARCH_VIRTIO_MMIO }, {"virtio-rng-ccw" ,"virtio-rng" ,QEMU_ARCH_VIRTIO_CCW }, {"virtio-rng-pci" ,"virtio-rng" ,QEMU_ARCH_VIRTIO_PCI }, {"virtio-scsi-device" ,"virtio-scsi" ,QEMU_ARCH_VIRTIO_MMIO }, {"virtio-scsi-ccw" ,"virtio-scsi" ,QEMU_ARCH_VIRTIO_CCW }, {"virtio-scsi-pci" ,"virtio-scsi" ,QEMU_ARCH_VIRTIO_PCI }, {"virtio-serial-device" ,"virtio-serial" ,QEMU_ARCH_VIRTIO_MMIO }, {"virtio-serial-ccw" ,"virtio-serial" ,QEMU_ARCH_VIRTIO_CCW }, {"virtio-serial-pci" ,"virtio-serial" ,QEMU_ARCH_VIRTIO_PCI }, + {"virtio-sound-pci" ,"virtio-sound" ,QEMU_ARCH_VIRTIO_PCI }, {"virtio-tablet-device" ,"virtio-tablet" ,QEMU_ARCH_VIRTIO_MMIO }, {"virtio-tablet-ccw" ,"virtio-tablet" ,QEMU_ARCH_VIRTIO_CCW }, {"virtio-tablet-pci" ,"virtio-tablet" ,QEMU_ARCH_VIRTIO_PCI }, { } };
Signature · 1 line
2.39.2
PATCH v11 3/11 virtio-sound: handle control messages and streams
In reply to Manos PitsidianakisFrom: Emmanouil Pitsidianakis <[email protected]>
Receive guest requests in the control (CTRL) queue of the virtio sound device and reply with a NOT SUPPORTED error to all control commands.
The receiving handler is virtio_snd_handle_ctrl(). It stores all control messages in the queue in the device's command queue. Then it calls virtio_snd_process_cmdq() to handle each message.
The handler is process_cmd() which replies with VIRTIO_SND_S_NOT_SUPP.
Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471
--- hw/virtio/trace-events | 4 + hw/virtio/virtio-snd.c | 487 ++++++++++++++++++++++++++++++++- include/hw/virtio/virtio-snd.h | 113 +++++++- 3 files changed, 595 insertions(+), 9 deletions(-)
hw/virtio/trace-events +4 -0
@@ -163,3 +163,7 @@ virtio_snd_vm_state_running(void) "vm state running" virtio_snd_vm_state_stopped(void) "vm state stopped" virtio_snd_realize(void *snd) "snd %p: realize" virtio_snd_unrealize(void *snd) "snd %p: unrealize" +virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event for queue %p" +virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PRIu32" == %s" +virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called" +virtio_snd_handle_event(void) "event queue callback called"
hw/virtio/virtio-snd.c +480 -7
586 lines not on this page · raw message
include/hw/virtio/virtio-snd.h +111 -2
124 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH v11 4/11 virtio-sound: handle VIRTIO_SND_R_PCM_INFO request
In reply to Manos PitsidianakisFrom: Emmanouil Pitsidianakis <[email protected]>
Respond to the VIRTIO_SND_R_PCM_INFO control request with the parameters of each requested PCM stream.
Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471
--- hw/virtio/trace-events | 1 + hw/virtio/virtio-snd.c | 82 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+)
hw/virtio/trace-events +1 -0
@@ -164,6 +164,7 @@ virtio_snd_vm_state_stopped(void) "vm state stopped" virtio_snd_realize(void *snd) "snd %p: realize" virtio_snd_unrealize(void *snd) "snd %p: unrealize" virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event for queue %p" +virtio_snd_handle_pcm_info(uint32_t stream) "VIRTIO_SND_R_PCM_INFO called for stream %"PRIu32 virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PRIu32" == %s" virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called" virtio_snd_handle_event(void) "event queue callback called"
hw/virtio/virtio-snd.c +82 -0
168 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH v11 5/11 virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP}
In reply to Manos PitsidianakisFrom: Emmanouil Pitsidianakis <[email protected]>
Handle the start and stop control messages for a stream_id. This request does nothing at the moment except for replying to it. Audio playback or capture will be started/stopped here in follow-up commits.
Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471
--- hw/virtio/trace-events | 1 + hw/virtio/virtio-snd.c | 49 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-)
hw/virtio/trace-events +1 -0
@@ -165,6 +165,7 @@ virtio_snd_realize(void *snd) "snd %p: realize" virtio_snd_unrealize(void *snd) "snd %p: unrealize" virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event for queue %p" virtio_snd_handle_pcm_info(uint32_t stream) "VIRTIO_SND_R_PCM_INFO called for stream %"PRIu32 +virtio_snd_handle_pcm_start_stop(const char *code, uint32_t stream) "%s called for stream %"PRIu32 virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PRIu32" == %s" virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called" virtio_snd_handle_event(void) "event queue callback called"
hw/virtio/virtio-snd.c +47 -2
142 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH v11 6/11 virtio-sound: handle VIRTIO_SND_R_PCM_SET_PARAMS
In reply to Manos PitsidianakisFrom: Emmanouil Pitsidianakis <[email protected]>
Handle the set parameters control request. It reconfigures a stream based on a guest's preference if the values are valid and supported.
Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471
--- hw/virtio/trace-events | 1 + hw/virtio/virtio-snd.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+)
hw/virtio/trace-events +1 -0
@@ -163,6 +163,7 @@ virtio_snd_vm_state_running(void) "vm state running" virtio_snd_vm_state_stopped(void) "vm state stopped" virtio_snd_realize(void *snd) "snd %p: realize" virtio_snd_unrealize(void *snd) "snd %p: unrealize" +virtio_snd_handle_pcm_set_params(uint32_t stream) "VIRTIO_SND_PCM_SET_PARAMS called for stream %"PRIu32 virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event for queue %p" virtio_snd_handle_pcm_info(uint32_t stream) "VIRTIO_SND_R_PCM_INFO called for stream %"PRIu32 virtio_snd_handle_pcm_start_stop(const char *code, uint32_t stream) "%s called for stream %"PRIu32
hw/virtio/virtio-snd.c +34 -0
@@ -250,43 +250,75 @@ staticuint32_t virtio_snd_set_pcm_params (VirtIOSound *s,uint32_t stream_id,virtio_snd_pcm_set_params *params) {virtio_snd_pcm_set_params *st_params;if (stream_id >= s->snd_conf.streams || s->pcm->pcm_params ==NULL ) {/* * TODO: do we need to set DEVICE_NEEDS_RESET? */ virtio_error (VIRTIO_DEVICE (s),"Streams have not been initialized.\n" );return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); } st_params =virtio_snd_pcm_get_params (s, stream_id);if (params->channels <1 || params->channels >AUDIO_MAX_CHANNELS ) {error_report ("Number of channels is not supported." );return cpu_to_le32 (VIRTIO_SND_S_NOT_SUPP ); }if (!(supported_formats &BIT (params->format))) {error_report ("Stream format is not supported." );return cpu_to_le32 (VIRTIO_SND_S_NOT_SUPP ); }if (!(supported_rates &BIT (params->rate))) {error_report ("Stream rate is not supported." );return cpu_to_le32 (VIRTIO_SND_S_NOT_SUPP ); } st_params->buffer_bytes =le32_to_cpu (params->buffer_bytes); st_params->period_bytes =le32_to_cpu (params->period_bytes); st_params->features =le32_to_cpu (params->features);/* the following are uint8_t, so there's no need to bswap the values. */ st_params->channels = params->channels; st_params->format = params->format; st_params->rate = params->rate;return cpu_to_le32 (VIRTIO_SND_S_OK ); } +/* +* Handles the VIRTIO_SND_R_PCM_SET_PARAMS request. +* +* @s: VirtIOSound device +* @cmd: The request command queue element from VirtIOSound cmdq field +*/ +static void virtio_snd_handle_pcm_set_params (VirtIOSound *s, +virtio_snd_ctrl_command *cmd) +{ +virtio_snd_pcm_set_params req = {0 }; +uint32_t stream_id; +size_t msg_sz =iov_to_buf (cmd->elem->out_sg, + cmd->elem->out_num, +0 , + &req, +sizeof (virtio_snd_pcm_set_params)); + +if (msg_sz !=sizeof (virtio_snd_pcm_set_params)) { +/* +* TODO: do we need to set DEVICE_NEEDS_RESET? +*/ +qemu_log_mask (LOG_GUEST_ERROR , +"%s: virtio-snd command size incorrect %zu vs \ +%zu\n" , __func__, msg_sz,sizeof (virtio_snd_pcm_set_params)); + cmd->resp.code =cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); +return ; + } + stream_id =le32_to_cpu (req.hdr.stream_id); +trace_virtio_snd_handle_pcm_set_params (stream_id); + cmd->resp.code =virtio_snd_set_pcm_params (s, stream_id, &req); +} +/* * Get a QEMU Audiosystem compatible format value from a VIRTIO_SND_PCM_FMT_* */
@@ -497,70 +529,72 @@ static inline void process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd) {uint32_t code;size_t msg_sz =iov_to_buf (cmd->elem->out_sg, cmd->elem->out_num,0 , &cmd->ctrl,sizeof (virtio_snd_hdr));if (msg_sz !=sizeof (virtio_snd_hdr)) {/* * TODO: do we need to set DEVICE_NEEDS_RESET? */ qemu_log_mask (LOG_GUEST_ERROR ,"%s: virtio-snd command size incorrect %zu vs \ %zu\n" , __func__, msg_sz,sizeof (virtio_snd_hdr));return ; } code =le32_to_cpu (cmd->ctrl.code);trace_virtio_snd_handle_code (code,print_code (code));switch (code) {case VIRTIO_SND_R_JACK_INFO :case VIRTIO_SND_R_JACK_REMAP :qemu_log_mask (LOG_UNIMP ,"virtio_snd: jack functionality is unimplemented.\n" ); cmd->resp.code =cpu_to_le32 (VIRTIO_SND_S_NOT_SUPP );break ;case VIRTIO_SND_R_PCM_INFO :virtio_snd_handle_pcm_info (s, cmd);break ;case VIRTIO_SND_R_PCM_START :virtio_snd_handle_pcm_start_stop (s, cmd, true);break ;case VIRTIO_SND_R_PCM_STOP :virtio_snd_handle_pcm_start_stop (s, cmd, false);break ;case VIRTIO_SND_R_PCM_SET_PARAMS : +virtio_snd_handle_pcm_set_params (s, cmd); +break ;case VIRTIO_SND_R_PCM_PREPARE :case VIRTIO_SND_R_PCM_RELEASE : cmd->resp.code =cpu_to_le32 (VIRTIO_SND_S_NOT_SUPP );break ;case VIRTIO_SND_R_CHMAP_INFO :qemu_log_mask (LOG_UNIMP ,"virtio_snd: chmap info functionality is unimplemented.\n" );trace_virtio_snd_handle_chmap_info (); cmd->resp.code =cpu_to_le32 (VIRTIO_SND_S_NOT_SUPP );break ;default :/* error */ error_report ("virtio snd header not recognized: %" PRIu32, code); cmd->resp.code =cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); }iov_from_buf (cmd->elem->in_sg, cmd->elem->in_num,0 , &cmd->resp,sizeof (virtio_snd_hdr));virtqueue_push (cmd->vq, cmd->elem,sizeof (virtio_snd_hdr));virtio_notify (VIRTIO_DEVICE (s), cmd->vq); }/* * Consume all elements in command queue. * * @s: VirtIOSound device */
Signature · 1 line
2.39.2
PATCH v11 7/11 virtio-sound: handle VIRTIO_SND_R_PCM_PREPARE
In reply to Manos PitsidianakisFrom: Emmanouil Pitsidianakis <[email protected]>
Handles the PCM prepare control request. It initializes a PCM stream when the guests asks for it.
Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471
--- hw/virtio/virtio-snd.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
hw/virtio/virtio-snd.c +24 -0
@@ -458,30 +458,52 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)static const char *print_code (uint32_t code) {#define CASE (CODE ) \ case VIRTIO_SND_R_##CODE: \ return "VIRTIO_SND_R_"#CODEswitch (code) {CASE (JACK_INFO );CASE (JACK_REMAP );CASE (PCM_INFO );CASE (PCM_SET_PARAMS );CASE (PCM_PREPARE );CASE (PCM_RELEASE );CASE (PCM_START );CASE (PCM_STOP );CASE (CHMAP_INFO );default :return "invalid code" ; }#undef CASE }; +/* +* Handles VIRTIO_SND_R_PCM_PREPARE. +* +* @s: VirtIOSound device +* @cmd: The request command queue element from VirtIOSound cmdq field +*/ +static void virtio_snd_handle_pcm_prepare (VirtIOSound *s, +virtio_snd_ctrl_command *cmd) +{ +uint32_t stream_id; +size_t msg_sz =iov_to_buf (cmd->elem->out_sg, + cmd->elem->out_num, +sizeof (virtio_snd_hdr), + &stream_id, +sizeof (stream_id)); + + stream_id =le32_to_cpu (stream_id); + cmd->resp.code = msg_sz ==sizeof (stream_id) + ?virtio_snd_pcm_prepare (s, stream_id) + :cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); +} +/* * Handles VIRTIO_SND_R_PCM_START. * * @s: VirtIOSound device * @cmd: The request command queue element from VirtIOSound cmdq field * @start: whether to start or stop the device */
@@ -529,72 +551,74 @@ static inline void process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd) {uint32_t code;size_t msg_sz =iov_to_buf (cmd->elem->out_sg, cmd->elem->out_num,0 , &cmd->ctrl,sizeof (virtio_snd_hdr));if (msg_sz !=sizeof (virtio_snd_hdr)) {/* * TODO: do we need to set DEVICE_NEEDS_RESET? */ qemu_log_mask (LOG_GUEST_ERROR ,"%s: virtio-snd command size incorrect %zu vs \ %zu\n" , __func__, msg_sz,sizeof (virtio_snd_hdr));return ; } code =le32_to_cpu (cmd->ctrl.code);trace_virtio_snd_handle_code (code,print_code (code));switch (code) {case VIRTIO_SND_R_JACK_INFO :case VIRTIO_SND_R_JACK_REMAP :qemu_log_mask (LOG_UNIMP ,"virtio_snd: jack functionality is unimplemented.\n" ); cmd->resp.code =cpu_to_le32 (VIRTIO_SND_S_NOT_SUPP );break ;case VIRTIO_SND_R_PCM_INFO :virtio_snd_handle_pcm_info (s, cmd);break ;case VIRTIO_SND_R_PCM_START :virtio_snd_handle_pcm_start_stop (s, cmd, true);break ;case VIRTIO_SND_R_PCM_STOP :virtio_snd_handle_pcm_start_stop (s, cmd, false);break ;case VIRTIO_SND_R_PCM_SET_PARAMS :virtio_snd_handle_pcm_set_params (s, cmd);break ;case VIRTIO_SND_R_PCM_PREPARE : +virtio_snd_handle_pcm_prepare (s, cmd); +break ;case VIRTIO_SND_R_PCM_RELEASE : cmd->resp.code =cpu_to_le32 (VIRTIO_SND_S_NOT_SUPP );break ;case VIRTIO_SND_R_CHMAP_INFO :qemu_log_mask (LOG_UNIMP ,"virtio_snd: chmap info functionality is unimplemented.\n" );trace_virtio_snd_handle_chmap_info (); cmd->resp.code =cpu_to_le32 (VIRTIO_SND_S_NOT_SUPP );break ;default :/* error */ error_report ("virtio snd header not recognized: %" PRIu32, code); cmd->resp.code =cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); }iov_from_buf (cmd->elem->in_sg, cmd->elem->in_num,0 , &cmd->resp,sizeof (virtio_snd_hdr));virtqueue_push (cmd->vq, cmd->elem,sizeof (virtio_snd_hdr));virtio_notify (VIRTIO_DEVICE (s), cmd->vq); }/* * Consume all elements in command queue. * * @s: VirtIOSound device */
Signature · 1 line
2.39.2
PATCH v11 8/11 virtio-sound: handle VIRTIO_SND_R_PCM_RELEASE
In reply to Manos PitsidianakisFrom: Emmanouil Pitsidianakis <[email protected]>
Handle the PCM release control request, which is necessary for flushing pending sound IO. No IO is handled yet so currently it only replies to the request.
Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471
--- hw/virtio/trace-events | 1 + hw/virtio/virtio-snd.c | 48 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-)
hw/virtio/trace-events +1 -0
@@ -167,6 +167,7 @@ virtio_snd_handle_pcm_set_params(uint32_t stream) "VIRTIO_SND_PCM_SET_PARAMS cal virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event for queue %p" virtio_snd_handle_pcm_info(uint32_t stream) "VIRTIO_SND_R_PCM_INFO called for stream %"PRIu32 virtio_snd_handle_pcm_start_stop(const char *code, uint32_t stream) "%s called for stream %"PRIu32 +virtio_snd_handle_pcm_release(uint32_t stream) "VIRTIO_SND_PCM_RELEASE called for stream %"PRIu32 virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PRIu32" == %s" virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called" virtio_snd_handle_event(void) "event queue callback called"
hw/virtio/virtio-snd.c +47 -1
168 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH v11 9/11 virtio-sound: implement audio output (TX)
In reply to Manos PitsidianakisFrom: Emmanouil Pitsidianakis <[email protected]>
Handle output IO messages in the transmit (TX) virtqueue.
It allocates a VirtIOSoundPCMBuffer for each IO message and copies the data buffer to it. When the IO buffer is written to the host's sound card, the guest will be notified that it has been consumed.
The lifetime of an IO message is:
1. Guest sends IO message to TX virtqueue.
2. QEMU adds it to the appropriate stream's IO buffer queue.
3. Sometime later, the host audio backend calls the output callback, virtio_snd_pcm_out_cb(), which is defined with an AUD_open_out() call. The callback gets an available number of bytes the backend can receive. Then it writes data from the IO buffer queue to the backend. If at any time a buffer is exhausted, it is returned to the guest as completed.
4. If the guest releases the stream, its buffer queue is flushed by attempting to write any leftover data to the audio backend and releasing all IO messages back to the guest. This is how according to the spec the guest knows the release was successful.
Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471
--- hw/virtio/trace-events | 2 + hw/virtio/virtio-snd.c | 288 ++++++++++++++++++++++++++++++++- include/hw/virtio/virtio-snd.h | 47 ++++++ 3 files changed, 332 insertions(+), 5 deletions(-)
hw/virtio/trace-events +2 -0
@@ -171,3 +171,5 @@ virtio_snd_handle_pcm_release(uint32_t stream) "VIRTIO_SND_PCM_RELEASE called fo virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PRIu32" == %s" virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called" virtio_snd_handle_event(void) "event queue callback called" +virtio_snd_pcm_stream_flush(uint32_t stream) "flushing stream %"PRIu32 +virtio_snd_handle_xfer(void) "tx/rx queue callback called"
hw/virtio/virtio-snd.c +283 -5
589 lines not on this page · raw message
include/hw/virtio/virtio-snd.h +47 -0
127 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH v11 10/11 virtio-sound: implement audio capture (RX)
In reply to Manos PitsidianakisFrom: Emmanouil Pitsidianakis <[email protected]>
To perform audio capture we duplicate the TX logic of the previous commit with the following difference: we receive data from the QEMU audio backend and write it in the virt queue IO buffers the guest sends to QEMU. When they are full (i.e. they have period_bytes amount of data) or when recording stops in QEMU's audio backend, the buffer is returned to the guest by notifying it.
--- hw/virtio/trace-events | 3 +- hw/virtio/virtio-snd.c | 262 +++++++++++++++++++++++++++++++++++------ 2 files changed, 230 insertions(+), 35 deletions(-)
hw/virtio/trace-events +2 -1
@@ -172,4 +172,5 @@ virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PR virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called" virtio_snd_handle_event(void) "event queue callback called" virtio_snd_pcm_stream_flush(uint32_t stream) "flushing stream %"PRIu32 -virtio_snd_handle_xfer(void) "tx/rx queue callback called" +virtio_snd_handle_tx_xfer(void) "tx queue callback called" +virtio_snd_handle_rx_xfer(void) "rx queue callback called"
hw/virtio/virtio-snd.c +228 -34
691 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH v11 11/11 docs/system: add basic virtio-snd documentation
In reply to Manos PitsidianakisFrom: Emmanouil Pitsidianakis <[email protected]>
This commit adds basic documentation for using virtio-snd.
--- MAINTAINERS | 1 + docs/system/device-emulation.rst | 1 + docs/system/devices/virtio-snd.rst (new) | 49 ++++++++++++++++++++++++ 3 files changed, 51 insertions(+)
MAINTAINERS +1 -0
@@ -2259,6 +2259,7 @@ M: Manos Pitsidianakis <[email protected]> S: Supported F: hw/virtio/virtio-snd*.c F: include/hw/virtio/virtio-snd.h +F: docs/system/devices/virtio-snd.rst nvme M: Keith Busch <[email protected]>
docs/system/device-emulation.rst +1 -0
@@ -79,20 +79,21 @@ in a PCI slot to the exclusive use of the guest. Emulated Devices ~~~~~~~~~~~~~~~~ .. toctree:: :maxdepth: 1 devices/can.rst devices/ccid.rst devices/cxl.rst devices/ivshmem.rst devices/keyboard.rst devices/net.rst devices/nvme.rst devices/usb.rst devices/vhost-user.rst devices/virtio-pmem.rst + devices/virtio-snd.rst devices/vhost-user-rng.rst devices/canokey.rst devices/usb-u2f.rst devices/igb.rst
docs/system/devices/virtio-snd.rst new file +49 -0
49 lines not on this page · raw message
Signature · 1 line
2.39.2
Quoted 7 lines · Manos Pitsidianakis
This patch series adds an audio device implementing the recent virtio sound spec (1.2) and a corresponding PCI wrapper device.
v11 can be found online at:
https://gitlab.com/epilys/qemu/-/tree/virtio-snd-v11
Ref
885b01fe272541fdab5583780d4c3a59bfd8e734
Hello Gerd, MAINTAINERS says you own the hw/audio subsystem, would you pull this in your tree if/when it is to be merged?
For context, the device was in hw/audio initially but in early list discussions we decided to move it with the rest of virtio devices.
At its current state it LGTM, if there's agreement that there are no issues we can merge this series.
All the best,
--
Manos
Quoted 8 lines · Manos Pitsidianakis
Quoted 7 lines · Manos Pitsidianakis
This patch series adds an audio device implementing the recent virtio sound spec (1.2) and a corresponding PCI wrapper device.
v11 can be found online at:
Quoted 7 lines
Quoted 2 lines
Ref
885b01fe272541fdab5583780d4c3a59bfd8e734Hello Gerd,
MAINTAINERSsays you own the hw/audio subsystem, would you pull this in your tree if/when it is to be merged?For context, the device was in hw/audio initially but in early list discussions we decided to move it with the rest of virtio devices.
Link to these discussions? virtio-gpu is under ./hw/display seems inconsistent. And you are asking Gerd to merge but your MAINTAINERS patch wouldn't even let people know he needs to be Cc'd on changes.
Quoted 7 lines
At its current state it LGTM, if there's agreement that there are no issues we can merge this series.
All the best,
--
Manos
Hello Michael,
Thank you for your reply;
Quoted 11 lines · Michael S. Tsirkin
Quoted 10 lines · Manos Pitsidianakis
Quoted 9 lines · Manos Pitsidianakis
This patch series adds an audio device implementing the recent virtio sound spec (1.2) and a corresponding PCI wrapper device.
v11 can be found online at:
https://gitlab.com/epilys/qemu/-/tree/virtio-snd-v11
Ref
885b01fe272541fdab5583780d4c3a59bfd8e734
Quoted 8 lines
Quoted 5 lines
Hello Gerd,
MAINTAINERSsays you own the hw/audio subsystem, would you pull this in your tree if/when it is to be merged?For context, the device was in hw/audio initially but in early list discussions we decided to move it with the rest of virtio devices.
Link to these discussions? virtio-gpu is under ./hw/display seems inconsistent.
I agree that it is inconsistent, but I do not know if there is a general consensus on this. I am fine with placing it anywhere that is deemed appropriate.
<[email protected]>
https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 3 lines
And you are asking Gerd to merge but
yourMAINTAINERSpatch wouldn't even let people know he needs to be Cc'd on changes.
No, I didn't ask to merge this, I asked if Gerd would be the one merging it because I am not sure myself and wanted more information. What do you think should be the correct process?
--
Manos
Quoted 33 lines · Manos Pitsidianakis
Hello Michael,
Thank you for your reply;
Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Manos Pitsidianakis
Quoted 6 lines · Manos Pitsidianakis
This patch series adds an audio device implementing the recent virtio sound spec (1.2) and a corresponding PCI wrapper device.
Quoted 3 lines
v11 can be found online at:
https://gitlab.com/epilys/qemu/-/tree/virtio-snd-v11
Ref885b01fe272541fdab5583780d4c3a59bfd8e734Hello Gerd,
MAINTAINERSsays you own the hw/audio subsystem, wouldQuoted 8 lines
Quoted 5 lines
you pull
this in your tree if/when it is to be merged?For context, the device was in hw/audio initially but in early list discussions we decided to move it with the rest of virtio devices.
Link to these discussions? virtio-gpu is under ./hw/display seems inconsistent.
I agree that it is inconsistent, but I do not know if there is a general consensus on this. I am fine with placing it anywhere that is deemed appropriate.
<[email protected]>
https://lore.kernel.org/qemu-devel/[email protected]/Quoted 3 lines
And you are asking Gerd to merge but
yourMAINTAINERSpatch wouldn't even let people know he needs to be Cc'd on changes.
Quoted 5 lines
No, I didn't ask to merge this, I asked if Gerd would be the one merging it because I am not sure myself and wanted more information. What do you think should be the correct process?
--
I'd put it under audio and then you get an unambigous maintainer to go through.
Quoted 17 lines · Michael S. Tsirkin
Quoted 17 lines
Quoted 5 lines
Quoted 2 lines
For context, the device was in hw/audio initially but in early list discussions we decided to move it with the rest of virtio devices.
Link to these discussions? virtio-gpu is under ./hw/display seems inconsistent.
I agree that it is inconsistent, but I do not know if there is a general consensus on this. I am fine with placing it anywhere that is deemed appropriate.
<[email protected]>
https://lore.kernel.org/qemu-devel/[email protected]/Quoted 3 lines
And you are asking Gerd to merge but
yourMAINTAINERSpatch wouldn't even let people know he needs to be Cc'd on changes.
Quoted 6 lines
Quoted 3 lines
No, I didn't ask to merge this, I asked if Gerd would be the one merging it because I am not sure myself and wanted more information. What do you think should be the correct process?
I'd put it under audio and then you get an unambigous maintainer to go through.
Thank you! I will do just that.