Add VIRTIO sound card
PATCH v5 1/12 Add virtio-sound device stub
In reply to Emmanouil PitsidianakisAdd a new VIRTIO device for the virtio sound device id. Functionality will be added in the following commits.
--- MAINTAINERS | 6 + hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 1 + hw/virtio/trace-events | 9 ++ hw/virtio/virtio-snd.c | 231 +++++++++++++++++++++++++++++++++ include/hw/virtio/virtio-snd.h | 80 ++++++++++++ 6 files changed, 332 insertions(+) create mode 100644 hw/virtio/virtio-snd.c create mode 100644 include/hw/virtio/virtio-snd.h
MAINTAINERS +6 -0
@@ -2245,6 +2245,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
@@ -31,6 +31,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 +231 -0
231 lines not on this page · raw message
include/hw/virtio/virtio-snd.h new file +80 -0
80 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH v5 7/12 virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP}
In reply to Emmanouil PitsidianakisHandle 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.
--- hw/virtio/trace-events | 1 + hw/virtio/virtio-snd.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 41 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 +40 -2
56 lines not on this page · raw message
Signature · 1 line
2.39.2
v5 cover Add VIRTIO sound card
This patch series adds an audio device implementing the recent virtio sound spec (1.2) and a corresponding PCI wrapper device.
https://github.com/epilys/qemu-virtio-snd/tree/sound-v6
Main differences with v4 patch [^v4] <[email protected]>:
- Use ERRP_GUARD() to propagate errors.
- Use virtio_add_feature() instead of XORing constants.
- Use %zu format specifier for size_t.
[^v4]:
https://lore.kernel.org/qemu-devel/[email protected]/
Previously:
[^v3]:
https://lore.kernel.org/qemu-devel/[email protected]/
Emmanouil Pitsidianakis (12):
Add virtio-sound device stub
Add virtio-sound-pci device
virtio-sound: handle control messages and streams
virtio-sound: set PCM stream parameters
virtio-sound: prepare PCM streams
virtio-sound: handle VIRTIO_SND_R_PCM_INFO request
virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP}
virtio-sound: handle VIRTIO_SND_PCM_SET_PARAMS
virtio-sound: handle VIRTIO_SND_R_PCM_PREPARE
virtio-sound: handle VIRTIO_SND_PCM_RELEASE
virtio-sound: implement audio output (TX)
virtio-sound: implement audio capture (RX)
MAINTAINERS | 6 + hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 2 + hw/virtio/trace-events | 20 + hw/virtio/virtio-snd-pci.c | 91 +++ hw/virtio/virtio-snd.c | 1300 ++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 1 + include/hw/virtio/virtio-snd.h | 158 ++++ softmmu/qdev-monitor.c | 1 + 9 files changed, 1584 insertions(+) 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 v4:
1: ae372de565 ! 1: 899b9a06bb Add virtio-sound device stub
@@ hw/virtio/virtio-snd.c (new)
+ * Feature Bits
+ * None currently defined.
+ */
++ VirtIOSound *s = VIRTIO_SND(vdev);
++ features |= s->features;
++
+ trace_virtio_snd_get_features(vdev, features);
-+ return features | 1UL << VIRTIO_F_VERSION_1 | 1UL << VIRTIO_F_IN_ORDER;
++
++ return features;
+}
+
+static void virtio_snd_common_realize(DeviceState *dev,
@@ hw/virtio/virtio-snd.c (new)
+ VirtIOSound *vsnd = VIRTIO_SND(dev);
+
+ virtio_init(vdev, VIRTIO_ID_SOUND, sizeof(virtio_snd_config));
++ virtio_add_feature(&vsnd->features, VIRTIO_F_VERSION_1);
+
+ /* set number of jacks and streams */
+ if (vsnd->snd_conf.jacks > 8) {
@@ hw/virtio/virtio-snd.c (new)
+
+static void virtio_snd_realize(DeviceState *dev, Error **errp)
+{
++ ERRP_GUARD();
+ VirtIOSound *vsnd = VIRTIO_SND(dev);
-+ Error *err = NULL;
+
+ vsnd->vmstate =
+ qemu_add_vm_change_state_handler(virtio_snd_vm_state_change, vsnd);
@@ hw/virtio/virtio-snd.c (new)
+ virtio_snd_handle_queue,
+ virtio_snd_handle_queue,
+ virtio_snd_handle_queue,
-+ &err);
-+ if (err != NULL) {
-+ error_propagate(errp, err);
-+ return;
-+ }
++ errp);
+}
+
+static void virtio_snd_unrealize(DeviceState *dev)
@@ include/hw/virtio/virtio-snd.h (new)
+typedef struct VirtIOSound {
+ VirtIODevice parent_obj;
+ VirtQueue *queues[VIRTIO_SND_VQ_MAX];
++ uint64_t features;
+ QEMUSoundCard card;
+ VMChangeStateEntry *vmstate;
+ virtio_snd_config snd_conf;
2: 5cde5472ea = 2: 035be510f8 Add virtio-sound-pci device
3: dc65cac2f4 ! 3: e6a3624f89 virtio-sound: handle control messages and streams @@ hw/virtio/virtio-snd.c: virtio_snd_set_config(VirtIODevice *vdev, const uint8_t
static uint64_t get_features(VirtIODevice *vdev, uint64_t features,
Error **errp)
@@ hw/virtio/virtio-snd.c: static uint64_t get_features(VirtIODevice *vdev, uint64_t features,
- return features | 1UL << VIRTIO_F_VERSION_1 | 1UL << VIRTIO_F_IN_ORDER;
+ return features;
}
+static void virtio_snd_set_pcm(VirtIOSound *snd)
@@ hw/virtio/virtio-snd.c: static void virtio_snd_common_realize(DeviceState *dev,
+ virtio_snd_set_pcm(vsnd);
+
virtio_init(vdev, VIRTIO_ID_SOUND, sizeof(virtio_snd_config));
+ virtio_add_feature(&vsnd->features, VIRTIO_F_VERSION_1);
- /* set number of jacks and streams */
@@ hw/virtio/virtio-snd.c: static void virtio_snd_common_realize(DeviceState *dev,
vsnd->queues[VIRTIO_SND_VQ_EVENT] = virtio_add_queue(vdev, 64, evt); vsnd->queues[VIRTIO_SND_VQ_TX] = virtio_add_queue(vdev, 64, txq);
@@ hw/virtio/virtio-snd.c: static void virtio_snd_common_realize(DeviceState *dev,
static void
@@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error **errp)
+ ERRP_GUARD();
VirtIOSound *vsnd = VIRTIO_SND(dev);
- Error *err = NULL;
+ vsnd->pcm = NULL;
vsnd->vmstate =
@@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error *
+ virtio_snd_handle_event,
+ virtio_snd_handle_xfer,
+ virtio_snd_handle_xfer,
- &err);
- if (err != NULL) {
- error_propagate(errp, err);
-- return;
- }
+ errp);
}
+/*
@@ include/hw/virtio/virtio-snd.h: typedef struct virtio_snd_pcm_xfer virtio_snd_pc
+struct VirtIOSound {
VirtIODevice parent_obj;
VirtQueue *queues[VIRTIO_SND_VQ_MAX];
+ uint64_t features;
+ VirtIOSoundPCM *pcm;
QEMUSoundCard card;
VMChangeStateEntry *vmstate;
4: bbd1799fc9 = 4: 82c80e2ae4 virtio-sound: set PCM stream parameters
5: 68a13c4385 = 5: 19d95e8411 virtio-sound: prepare PCM streams
6: f267d41957 ! 6: 78777fb46f virtio-sound: handle VIRTIO_SND_R_PCM_INFO request
@@ hw/virtio/virtio-snd.c: static VirtIOSoundPCMParams *virtio_snd_pcm_get_params(V
+
+ if (iov_size(cmd->elem->in_sg, cmd->elem->in_num) <
+ sizeof(virtio_snd_hdr) + req.size * req.count) {
-+ error_report("pcm info: buffer too small, got: %lu, needed: %lu",
++ error_report("pcm info: buffer too small, got: %zu, needed: %zu",
+ iov_size(cmd->elem->in_sg, cmd->elem->in_num),
+ sizeof(virtio_snd_pcm_info));
+ cmd->resp.code = VIRTIO_SND_S_BAD_MSG;
7: 5939a6161e = 7: 7641e21ee8 virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP}
8: 8f78d3a132 = 8: 48f9b776f5 virtio-sound: handle VIRTIO_SND_PCM_SET_PARAMS
9: d2b3854084 = 9: cdb8ab3ee2 virtio-sound: handle VIRTIO_SND_R_PCM_PREPARE
10: 4e14b2d129 = 10: bbc11d0348 virtio-sound: handle VIRTIO_SND_PCM_RELEASE
11: 65442a1cd1 ! 11: d5fb4b058c virtio-sound: implement audio output (TX) @@ Metadata
## Commit message ##
virtio-sound: implement audio output (TX)
- Handle output IO messages in the receive (RX) virtqueue.
+ Handle output IO messages in the transmit (TX) virtqueue.
It allocates a VirtIOSoundPCMBlock for each IO message and copies the data buffer to it. When the IO buffer is written to the host's sound
@@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error *
- virtio_snd_handle_xfer,
+ virtio_snd_handle_tx,
virtio_snd_handle_xfer,
- &err);
- if (err != NULL) {
-@@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error **errp)
- }
+ errp);
}
+/*
12: aeb0caf7dc ! 12: e2c30e2a21 virtio-sound: implement audio capture (RX)
@@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error *
- virtio_snd_handle_xfer,
+ virtio_snd_handle_tx_xfer,
+ virtio_snd_handle_rx_xfer,
- &err);
- if (err != NULL) {
- error_propagate(errp, err);
+ errp);
+ }
+
@@ hw/virtio/virtio-snd.c: static void virtio_snd_pcm_out_cb(void *data, int available)
}
base-commit: ccb86f079a9e4d94918086a9df18c1844347aff8
Signature · 1 line
2.39.2
PATCH v5 8/12 virtio-sound: handle VIRTIO_SND_PCM_SET_PARAMS
Handle the set parameters control request. It reconfigures a stream based on a guest's preference if the values are valid and supported.
--- hw/virtio/trace-events | 1 + hw/virtio/virtio-snd.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 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 +26 -0
@@ -276,6 +276,30 @@ uint32_t virtio_snd_pcm_set_params_impl(VirtIOSound *s,return 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; +size_t sz =iov_to_buf (cmd->elem->out_sg, + cmd->elem->out_num, +0 , + &req, +sizeof (req)); +if (sz !=sizeof (virtio_snd_pcm_set_params)) { + cmd->resp.code =VIRTIO_SND_S_BAD_MSG ; +return ; + } + +trace_virtio_snd_handle_pcm_set_params (req.hdr.stream_id); + cmd->resp.code =virtio_snd_pcm_set_params_impl (s, &req); +} +/* * Get a QEMU Audiosystem compatible format value from a VIRTIO_SND_PCM_FMT_* */
@@ -477,6 +501,8 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd)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 =VIRTIO_SND_S_NOT_SUPP ;
Signature · 1 line
2.39.2
Re: [PATCH v5 05/12] virtio-sound: prepare PCM streams
In reply to Emmanouil PitsidianakisHi
Quoted 43 lines · Emmanouil Pitsidianakis
hw/virtio/virtio-snd.c +43
-0+ +/* +* Prepares a VirtIOSound card stream. +* Returns the response status code. (VIRTIO_SND_S_*). +* +* @s: VirtIOSound device +* @stream_id: stream id +*/ +static uint32_t virtio_snd_pcm_prepare_impl (VirtIOSound *s,uint32_t stream_id) +{ +audsettings as; +VirtIOSoundPCMParams *params; +VirtIOSoundPCMStream *stream; + +if (!s->pcm->streams || + !s->pcm->pcm_params || + !s->pcm->pcm_params[stream_id]) { +return VIRTIO_SND_S_BAD_MSG ; + } + + params =virtio_snd_pcm_get_params (s, stream_id); +if (!params) { +return VIRTIO_SND_S_BAD_MSG ; + } + +virtio_snd_get_qemu_audsettings (&as, params); + + stream =g_new0 (VirtIOSoundPCMStream,1 ); + + stream->id = stream_id; + stream->pcm = s->pcm; + stream->direction = stream_id < s->snd_conf.streams /2 + + (s->snd_conf.streams &1 ) ?VIRTIO_SND_D_OUTPUT :VIRTIO_SND_D_INPUT ; + stream->info.hdr.hda_fn_nid =VIRTIO_SOUND_HDA_FN_NID ; + stream->features =0 ; + stream->channels_min =1 ; + stream->channels_max = as.nchannels; + stream->formats = supported_formats; + stream->rates = supported_rates; + stream->s = s; + + stream->buffer_bytes = params->buffer_bytes; + stream->period_bytes = params->period_bytes;
hw/virtio/virtio-snd.c uint32_t virtio_snd_pcm_set_params_impl(VirtIOSound *s, · line 324
Quoted 10 lines
hw/virtio/virtio-snd.c +10
-0+ + stream->positions[0 ] =VIRTIO_SND_CHMAP_FL ; + stream->positions[1 ] =VIRTIO_SND_CHMAP_FR ; + + stream->as = as; + stream->desired_as = stream->as; +qemu_mutex_init (&stream->queue_mutex); +QSIMPLEQ_INIT (&stream->queue); + + s->pcm->streams[stream_id] = stream;
Same remark as v4:
Shouldn't it close & free the existing stream? Or return an error?
PATCH v5 3/12 virtio-sound: handle control messages and streams
In reply to Emmanouil PitsidianakisReceive 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.
--- hw/virtio/trace-events | 4 + hw/virtio/virtio-snd.c | 221 +++++++++++++++++++++++++++++++-- include/hw/virtio/virtio-snd.h | 71 ++++++++++- 3 files changed, 287 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 +214 -7
285 lines not on this page · raw message
include/hw/virtio/virtio-snd.h +69 -2
81 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH v5 12/12 virtio-sound: implement audio capture (RX)
In reply to Emmanouil PitsidianakisTo 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 | 230 +++++++++++++++++++++++++++++++++++------ 2 files changed, 200 insertions(+), 33 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) "tx queue callback called"
hw/virtio/virtio-snd.c +198 -32
323 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH v5 2/12 Add virtio-sound-pci device
In reply to Emmanouil PitsidianakisThis 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:
-device virtio-sound-pci,disable-legacy=on
And an audio backend listed with -audio driver=help that works on your host machine, e.g.:
Pulseaudio:
-audio driver=pa,model=virtio-sound
or
-audio driver=pa,model=virtio-sound,server=/run/user/1000/pulse/native
sdl:
-audio driver=sdl,model=virtio-sound
coreaudio (macos/darwin):
-audio driver=coreaudio,model=virtio-sound
etc.
--- hw/virtio/meson.build | 1 + hw/virtio/virtio-snd-pci.c | 91 ++++++++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 1 + softmmu/qdev-monitor.c | 1 + 4 files changed, 94 insertions(+) create mode 100644 hw/virtio/virtio-snd-pci.c
hw/virtio/meson.build +1 -0
@@ -63,6 +63,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 +91 -0
91 lines not on this page · raw message
include/hw/pci/pci.h +1 -0
@@ -85,6 +85,7 @@ extern bool pci_available;#define PCI_DEVICE_ID_VIRTIO_RNG 0x1005#define PCI_DEVICE_ID_VIRTIO_9P 0x1009#define PCI_DEVICE_ID_VIRTIO_VSOCK 0x1012 +#define PCI_DEVICE_ID_VIRTIO_SND 0x1019/* * modern virtio-pci devices get their id assigned automatically,
softmmu/qdev-monitor.c +1 -0
@@ -108,6 +108,7 @@ 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}, + { "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 v5 11/12 virtio-sound: implement audio output (TX)
In reply to Emmanouil PitsidianakisHandle output IO messages in the transmit (TX) virtqueue.
It allocates a VirtIOSoundPCMBlock 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.
--- hw/virtio/trace-events | 2 + hw/virtio/virtio-snd.c | 254 ++++++++++++++++++++++++++++++++- include/hw/virtio/virtio-snd.h | 11 ++ 3 files changed, 260 insertions(+), 7 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 +247 -7
317 lines not on this page · raw message
include/hw/virtio/virtio-snd.h +11 -0
@@ -81,6 +81,8 @@ typedef struct VirtIOSoundPCMParams VirtIOSoundPCMParams;typedef struct VirtIOSoundPCM VirtIOSoundPCM ; +typedef struct VirtIOSoundPCMBlock VirtIOSoundPCMBlock ; +/* Stream params */ struct VirtIOSoundPCMParams {uint32_t features;
@@ -91,6 +93,15 @@ struct VirtIOSoundPCMParams {uint8_t rate; }; +struct VirtIOSoundPCMBlock { +QSIMPLEQ_ENTRY (VirtIOSoundPCMBlock) entry; +VirtQueueElement *elem; +VirtQueue *vq; +size_t size; +uint64_t offset; +uint8_t data[]; +}; +struct VirtIOSoundPCM {VirtIOSound *snd;VirtIOSoundPCMParams **pcm_params;
Signature · 1 line
2.39.2
PATCH v5 9/12 virtio-sound: handle VIRTIO_SND_R_PCM_PREPARE
In reply to Emmanouil PitsidianakisHandles the PCM prepare control request. It initializes a PCM stream when the guests asks for it.
--- hw/virtio/virtio-snd.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
hw/virtio/virtio-snd.c +23 -0
@@ -426,6 +426,27 @@ static uint32_t virtio_snd_pcm_prepare_impl(VirtIOSound *s, uint32_t stream_id)return VIRTIO_SND_S_OK ; } +/* +* 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 sz =iov_to_buf (cmd->elem->out_sg, + cmd->elem->out_num, +sizeof (virtio_snd_hdr), + &stream_id, +sizeof (stream_id)); + + cmd->resp.code = sz ==sizeof (uint32_t ) + ?virtio_snd_pcm_prepare_impl (s, stream_id) + :VIRTIO_SND_S_BAD_MSG ; +} +/* * Handles VIRTIO_SND_R_PCM_START. *
@@ -504,6 +525,8 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd)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 =VIRTIO_SND_S_NOT_SUPP ;break ;
Signature · 1 line
2.39.2
PATCH v5 4/12 virtio-sound: set PCM stream parameters
In reply to Emmanouil PitsidianakisThis commit sets the virtio-snd device's default PCM parameters in virtio_snd_pcm_set_params_impl(). The same function will be used to set parameters from the guest with VIRTIO_SND_R_PCM_SET_PARAMS in a follow-up commit.
PCM parameters describe the sound card parameters that the guest's kernel sees as an ALSA device.
--- hw/virtio/virtio-snd.c | 109 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+)
hw/virtio/virtio-snd.c +109 -0
@@ -30,6 +30,29 @@#define VIRTIO_SOUND_CHMAP_DEFAULT 0#define VIRTIO_SOUND_HDA_FN_NID 0 +static uint32_t supported_formats =BIT (VIRTIO_SND_PCM_FMT_S8 ) + |BIT (VIRTIO_SND_PCM_FMT_U8 ) + |BIT (VIRTIO_SND_PCM_FMT_S16 ) + |BIT (VIRTIO_SND_PCM_FMT_U16 ) + |BIT (VIRTIO_SND_PCM_FMT_S32 ) + |BIT (VIRTIO_SND_PCM_FMT_U32 ) + |BIT (VIRTIO_SND_PCM_FMT_FLOAT ); + +static uint32_t supported_rates =BIT (VIRTIO_SND_PCM_RATE_5512 ) + |BIT (VIRTIO_SND_PCM_RATE_8000 ) + |BIT (VIRTIO_SND_PCM_RATE_11025 ) + |BIT (VIRTIO_SND_PCM_RATE_16000 ) + |BIT (VIRTIO_SND_PCM_RATE_22050 ) + |BIT (VIRTIO_SND_PCM_RATE_32000 ) + |BIT (VIRTIO_SND_PCM_RATE_44100 ) + |BIT (VIRTIO_SND_PCM_RATE_48000 ) + |BIT (VIRTIO_SND_PCM_RATE_64000 ) + |BIT (VIRTIO_SND_PCM_RATE_88200 ) + |BIT (VIRTIO_SND_PCM_RATE_96000 ) + |BIT (VIRTIO_SND_PCM_RATE_176400 ) + |BIT (VIRTIO_SND_PCM_RATE_192000 ) + |BIT (VIRTIO_SND_PCM_RATE_384000 ); +static const char *print_code (uint32_t code) {#define CASE (CODE ) \
@@ -111,6 +134,72 @@ virtio_snd_set_config(VirtIODevice *vdev, const uint8_t *config)memcpy (&s->snd_conf, sndconfig,sizeof (s->snd_conf)); } +/* +* Get params for a specific stream. +* +* @s: VirtIOSound device +* @stream_id: stream id +*/ +static VirtIOSoundPCMParams *virtio_snd_pcm_get_params (VirtIOSound *s, +uint32_t stream_id) +{ +return stream_id >= s->snd_conf.streams ?NULL + : s->pcm->pcm_params[stream_id]; +} + +/* +* Set the given stream params. +* Called by both virtio_snd_handle_pcm_set_params and during device +* initialization. +* Returns the response status code. (VIRTIO_SND_S_*). +* +* @s: VirtIOSound device +* @params: The PCM params as defined in the virtio specification +*/ +static +uint32_t virtio_snd_pcm_set_params_impl (VirtIOSound *s, +virtio_snd_pcm_set_params *params) +{ +VirtIOSoundPCMParams *st_params; +uint32_t stream_id = params->hdr.stream_id; + +if (stream_id > s->snd_conf.streams || !(s->pcm->pcm_params)) { +virtio_error (VIRTIO_DEVICE (s),"Streams have not been initialized.\n" ); +return VIRTIO_SND_S_BAD_MSG ; + } + +if (!s->pcm->pcm_params[stream_id]) { + s->pcm->pcm_params[stream_id] =g_new0 (VirtIOSoundPCMParams,1 ); + } + st_params =virtio_snd_pcm_get_params (s, stream_id); + + st_params->features = params->features; + st_params->buffer_bytes = params->buffer_bytes; + st_params->period_bytes = params->period_bytes; + +if (params->channels <1 || params->channels >AUDIO_MAX_CHANNELS ) { +error_report ("Number of channels is not supported." ); +return VIRTIO_SND_S_NOT_SUPP ; + } + st_params->channels = params->channels; + +if (!(supported_formats &BIT (params->format))) { +error_report ("Stream format is not supported." ); +return VIRTIO_SND_S_NOT_SUPP ; + } + st_params->format = params->format; + +if (!(supported_rates &BIT (params->rate))) { +error_report ("Stream rate is not supported." ); +return VIRTIO_SND_S_NOT_SUPP ; + } + st_params->rate = params->rate; + st_params->period_bytes = params->period_bytes; + st_params->buffer_bytes = params->buffer_bytes; + +return VIRTIO_SND_S_OK ; +} +/* * The actual processing done in virtio_snd_process_cmdq(). *
@@ -294,6 +383,8 @@ static void virtio_snd_common_realize(DeviceState *dev, {VirtIODevice *vdev =VIRTIO_DEVICE (dev);VirtIOSound *vsnd =VIRTIO_SND (dev); +virtio_snd_pcm_set_params default_params; +uint32_t status;virtio_snd_set_pcm (vsnd);
@@ -323,12 +414,30 @@ static void virtio_snd_common_realize(DeviceState *dev,AUD_register_card ("virtio-sound" , &vsnd->card); +/* set default params for all streams */ + default_params.features =0 ; + default_params.buffer_bytes =8192 ; + default_params.period_bytes =4096 ; + default_params.channels =2 ; + default_params.format =VIRTIO_SND_PCM_FMT_S16 ; + default_params.rate =VIRTIO_SND_PCM_RATE_44100 ; vsnd->queues[VIRTIO_SND_VQ_CONTROL ] =virtio_add_queue (vdev,64 , ctrl); vsnd->queues[VIRTIO_SND_VQ_EVENT ] =virtio_add_queue (vdev,64 , evt); vsnd->queues[VIRTIO_SND_VQ_TX ] =virtio_add_queue (vdev,64 , txq); vsnd->queues[VIRTIO_SND_VQ_RX ] =virtio_add_queue (vdev,64 , rxq);qemu_mutex_init (&vsnd->cmdq_mutex);QTAILQ_INIT (&vsnd->cmdq); + +for (uint32_t i =0 ; i < vsnd->snd_conf.streams; i++) { + default_params.hdr.stream_id = i; + status =virtio_snd_pcm_set_params_impl (vsnd, &default_params); +if (status !=VIRTIO_SND_S_OK ) { +error_setg (errp, +"Can't initalize stream params, device responded with %s." , +print_code (status)); +return ; + } + } } static void
Signature · 1 line
2.39.2
PATCH v5 5/12 virtio-sound: prepare PCM streams
In reply to Emmanouil PitsidianakisAfter setting PCM parameters, instantiate ("prepare") each stream in virtio_snd_pcm_prepare_impl().
--- hw/virtio/virtio-snd.c | 133 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+)
hw/virtio/virtio-snd.c +133 -0
@@ -200,6 +200,132 @@ uint32_t virtio_snd_pcm_set_params_impl(VirtIOSound *s,return VIRTIO_SND_S_OK ; } +/* +* Get a QEMU Audiosystem compatible format value from a VIRTIO_SND_PCM_FMT_* +*/ +static AudioFormat virtio_snd_get_qemu_format (uint32_t format) +{ +#define CASE (FMT ) \ + case VIRTIO_SND_PCM_FMT_##FMT: \ + return AUDIO_FORMAT_##FMT; + +switch (format) { + CASE(U8) + CASE(S8) + CASE(U16) + CASE(S16) + CASE(U32) + CASE(S32) + case VIRTIO_SND_PCM_FMT_FLOAT: +return AUDIO_FORMAT_F32 ; +default : +g_assert_not_reached (); + } + +#undef CASE +} + +/* +* Get a QEMU Audiosystem compatible frequency value from a +* VIRTIO_SND_PCM_RATE_* +*/ +static uint32_t virtio_snd_get_qemu_freq (uint32_t rate) +{ +#define CASE (RATE ) \ + case VIRTIO_SND_PCM_RATE_##RATE: \ + return RATE; + +switch (rate) { + CASE(5512) + CASE(8000) + CASE(11025) + CASE(16000) + CASE(22050) + CASE(32000) + CASE(44100) + CASE(48000) + CASE(64000) + CASE(88200) + CASE(96000) + CASE(176400) + CASE(192000) + CASE(384000) +default : +g_assert_not_reached (); + } + +#undef CASE +} + +/* +* Get QEMU Audiosystem compatible audsettings from virtio based pcm stream +* params. +*/ +static void virtio_snd_get_qemu_audsettings (audsettings *as, +VirtIOSoundPCMParams *params) +{ + as->nchannels =MIN (AUDIO_MAX_CHANNELS , params->channels); + as->fmt =virtio_snd_get_qemu_format (params->format); + as->freq =virtio_snd_get_qemu_freq (params->rate); + as->endianness =AUDIO_HOST_ENDIANNESS ; +} + +/* +* Prepares a VirtIOSound card stream. +* Returns the response status code. (VIRTIO_SND_S_*). +* +* @s: VirtIOSound device +* @stream_id: stream id +*/ +static uint32_t virtio_snd_pcm_prepare_impl (VirtIOSound *s,uint32_t stream_id) +{ +audsettings as; +VirtIOSoundPCMParams *params; +VirtIOSoundPCMStream *stream; + +if (!s->pcm->streams || + !s->pcm->pcm_params || + !s->pcm->pcm_params[stream_id]) { +return VIRTIO_SND_S_BAD_MSG ; + } + + params =virtio_snd_pcm_get_params (s, stream_id); +if (!params) { +return VIRTIO_SND_S_BAD_MSG ; + } + +virtio_snd_get_qemu_audsettings (&as, params); + + stream =g_new0 (VirtIOSoundPCMStream,1 ); + + stream->id = stream_id; + stream->pcm = s->pcm; + stream->direction = stream_id < s->snd_conf.streams /2 + + (s->snd_conf.streams &1 ) ?VIRTIO_SND_D_OUTPUT :VIRTIO_SND_D_INPUT ; + stream->info.hdr.hda_fn_nid =VIRTIO_SOUND_HDA_FN_NID ; + stream->features =0 ; + stream->channels_min =1 ; + stream->channels_max = as.nchannels; + stream->formats = supported_formats; + stream->rates = supported_rates; + stream->s = s; + + stream->buffer_bytes = params->buffer_bytes; + stream->period_bytes = params->period_bytes; + + stream->positions[0 ] =VIRTIO_SND_CHMAP_FL ; + stream->positions[1 ] =VIRTIO_SND_CHMAP_FR ; + + stream->as = as; + stream->desired_as = stream->as; +qemu_mutex_init (&stream->queue_mutex); +QSIMPLEQ_INIT (&stream->queue); + + s->pcm->streams[stream_id] = stream; + +return VIRTIO_SND_S_OK ; +} +/* * The actual processing done in virtio_snd_process_cmdq(). *
@@ -437,6 +563,13 @@ static void virtio_snd_common_realize(DeviceState *dev, print_code(status));return ; } + status =virtio_snd_pcm_prepare_impl (vsnd, i); +if (status !=VIRTIO_SND_S_OK ) { +error_setg (errp, +"Can't prepare streams, device responded with %s." , +print_code (status)); +return ; + } } }
Signature · 1 line
2.39.2
PATCH v5 10/12 virtio-sound: handle VIRTIO_SND_PCM_RELEASE
In reply to Emmanouil PitsidianakisHandle 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.
--- hw/virtio/trace-events | 1 + hw/virtio/virtio-snd.c | 51 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 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 +50 -1
63 lines not on this page · raw message
Signature · 1 line
2.39.2
Re: [PATCH v5 05/12] virtio-sound: prepare PCM streams
In reply to Marc-André LureauQuoted 6 lines · Marc-André Lureau
Review
Quoted 2 lines
(quoted diff) +2
-0+ + s->pcm->streams[stream_id] = stream;Same remark as v4:
Shouldn't it close & free the existing stream? Or return an error?
Hello, yes you are correct. I missed that comment on the v4 reviews :)
Will send a v6 with a fix, I will wait a bit in case any other patch gets a Reviewed-By this time.
PATCH v5 6/12 virtio-sound: handle VIRTIO_SND_R_PCM_INFO request
In reply to Emmanouil PitsidianakisRespond to the VIRTIO_SND_R_PCM_INFO control request with the parameters of each requested PCM stream.
--- hw/virtio/trace-events | 1 + hw/virtio/virtio-snd.c | 78 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 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 +78 -0
96 lines not on this page · raw message
Signature · 1 line
2.39.2