Skip to contentSkip to conversation

audio/hda: improve windows guest audio quality.

PATCH Superseded Gerd Hoffmann 3 participants 11 messages Apr 19, 2018 – May 14, 2018

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover audio/hda: improve windows guest audio quality.

Based on a patch by Martin Schrodt.

Gerd Hoffmann (4):
audio/hda: create millisecond timers that handle IO audio/hda: turn some dprintfs into trace points audio/hda: tweak timer adjust logic audio/hda: detect output buffer overruns and underruns

 include/hw/compat.h   |   7 ++
 hw/audio/hda-codec.c  | 300 ++++++++++++++++++++++++++++++++++++++++++++------
 hw/audio/intel-hda.c  |   7 --
 hw/audio/trace-events |   7 ++
 4 files changed, 283 insertions(+), 38 deletions(-)
Signature · 1 line
2.9.3
#5 lore raw

PATCH 4/4 audio/hda: detect output buffer overruns and underruns

If some event caused some larger playback hickup the fine-grained timer adjust isn't able to recover. Use a buffer overruns and underruns as indicator for that. Reset timer adjust logic in case we detected one.

---
 hw/audio/hda-codec.c  | 22 ++++++++++++++++++++++
 hw/audio/trace-events |  2 ++
 2 files changed, 24 insertions(+)
hw/audio/hda-codec.c +22 -0
index f6fb1d0fa0..623021b7c7 100644
@@ -261,6 +261,13 @@ static void hda_audio_input_cb(void *opaque, int avail)
 
     int64_t to_transfer = audio_MIN(B_SIZE - (wpos - rpos), avail);
 
+    if (to_transfer == 0) {
+        /* reset timer adjust */
+        st->buft_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+        trace_hda_audio_underrun(st->node->name);
+        return;
+    }
+
     hda_timer_sync_adjust(st, -((wpos - rpos) + to_transfer - (B_SIZE >> 1)));
 
     while (to_transfer) {
@@ -325,6 +332,21 @@ static void hda_audio_output_cb(void *opaque, int avail)
 
     int64_t to_transfer = audio_MIN(wpos - rpos, avail);
 
+    if (to_transfer == 0) {
+        /* reset timer adjust */
+        st->buft_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+        trace_hda_audio_underrun(st->node->name);
+        return;
+    }
+    if (wpos - rpos == B_SIZE) {
+        /* drop buffer, reset timer adjust */
+        st->rpos = 0;
+        st->wpos = 0;
+        st->buft_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+        trace_hda_audio_overrun(st->node->name);
+        return;
+    }
+
     hda_timer_sync_adjust(st, (wpos - rpos) - to_transfer - (B_SIZE >> 1));
 
     while (to_transfer) {
hw/audio/trace-events +2 -0
index 30112d97c4..3b2ea2727b 100644
@@ -22,3 +22,5 @@ milkymist_ac97_out_cb_transferred(int transferred) "transferred %d"
 hda_audio_running(const char *stream, int nr, bool running) "st %s, nr %d, run %d"
 hda_audio_format(const char *stream, int chan, const char *fmt, int freq) "st %s, %d x %s @ %d Hz"
 hda_audio_adjust(const char *stream, int pos) "st %s, pos %d"
+hda_audio_overrun(const char *stream) "st %s"
+hda_audio_underrun(const char *stream) "st %s"
Signature · 1 line
2.9.3
#1 lore raw

PATCH 1/4 audio/hda: create millisecond timers that handle IO

In reply to Gerd Hoffmann

Currently, the HDA device tries to sync itself with the QEMU audio backend by waiting for the guest driver to handle buffer completion interrupts. This causes the backend to often read too much data from the device, as well as running out of data whenever the guest takes too long to handle the interrupt.

According to the HDA specification, the guest is also not required to use interrupts, but can also sync itself by polling the LPIB registers.

This patch will introduce high frequency (1000Hz) timers that interface with the device and allow for much smoother emulation of the LPIB registers. Since the timing is now provided by these timers, the need to wait for buffer completion interrupts also ceases.

Message-id: [email protected]

[ kraxel: keep old code for compatibility with older qemu versions,
add property to switch code paths at runtime ]

---
 include/hw/compat.h  |   7 ++
 hw/audio/hda-codec.c | 263 ++++++++++++++++++++++++++++++++++++++++++++++-----
 hw/audio/intel-hda.c |   7 --
 3 files changed, 244 insertions(+), 33 deletions(-)
include/hw/compat.h +7 -0
index 13242b831a..dced6d81bf 100644
@@ -1,6 +1,13 @@
 #ifndef HW_COMPAT_H
 #define HW_COMPAT_H
 
+#define HW_COMPAT_2_12 \
+    {\
+        .driver   = "hda-audio",\
+        .property = "use-timer",\
+        .value    = "false",\
+    },
+
 #define HW_COMPAT_2_11 \
     {\
         .driver   = "hpet",\
hw/audio/hda-codec.c +237 -26

371 lines not on this page · raw message

hw/audio/intel-hda.c +0 -7
index 948268afd8..23a2cf6484 100644
@@ -407,13 +407,6 @@ static bool intel_hda_xfer(HDACodecDevice *dev, uint32_t stnr, bool output,
     if (st->bpl == NULL) {
         return false;
     }
-    if (st->ctl & (1 << 26)) {
-        /*
-         * Wait with the next DMA xfer until the guest
-         * has acked the buffer completion interrupt
-         */
-        return false;
-    }
 
     left = len;
     s = st->bentries;
Signature · 1 line
2.9.3
#4 lore raw

PATCH 3/4 audio/hda: tweak timer adjust logic

In reply to Gerd Hoffmann

We have some jitter in the audio timer call frequency and buffer sizes. So it is rather pointless trying to be very exact, effect is a constant up+down adjustment. So adjust only in case we are off too much.

---
 hw/audio/hda-codec.c  | 20 +++++++++++++-------
 hw/audio/trace-events |  1 +
 2 files changed, 14 insertions(+), 7 deletions(-)
hw/audio/hda-codec.c +13 -7
index 13d4473f18..f6fb1d0fa0 100644
@@ -129,7 +129,6 @@ static void hda_codec_parse_fmt(uint32_t format, struct audsettings *as)
 #include  "hda-codec-common.h"
 
 #define HDA_TIMER_TICKS (SCALE_MS)
-#define MAX_CORR (SCALE_US * 100)
 #define B_SIZE sizeof(st->buf)
 #define B_MASK (sizeof(st->buf) - 1)
 
@@ -196,13 +195,20 @@ static inline int64_t hda_bytes_per_second(HDAAudioStream *st)
 
 static inline void hda_timer_sync_adjust(HDAAudioStream *st, int64_t target_pos)
 {
-    int64_t corr =
-        NANOSECONDS_PER_SECOND * target_pos / hda_bytes_per_second(st);
-    if (corr > MAX_CORR) {
-        corr = MAX_CORR;
-    } else if (corr < -MAX_CORR) {
-        corr = -MAX_CORR;
+    int64_t limit = B_SIZE / 8;
+    int64_t corr = 0;
+
+    if (target_pos > limit) {
+        corr = HDA_TIMER_TICKS;
     }
+    if (target_pos < -limit) {
+        corr = -HDA_TIMER_TICKS;
+    }
+    if (corr == 0) {
+        return;
+    }
+
+    trace_hda_audio_adjust(st->node->name, target_pos);
     atomic_fetch_add(&st->buft_start, corr);
 }
 
hw/audio/trace-events +1 -0
index 03340b9359..30112d97c4 100644
@@ -21,3 +21,4 @@ milkymist_ac97_out_cb_transferred(int transferred) "transferred %d"
 # hw/audio/hda-codec.c
 hda_audio_running(const char *stream, int nr, bool running) "st %s, nr %d, run %d"
 hda_audio_format(const char *stream, int chan, const char *fmt, int freq) "st %s, %d x %s @ %d Hz"
+hda_audio_adjust(const char *stream, int pos) "st %s, pos %d"
Signature · 1 line
2.9.3
#2 lore raw

PATCH 2/4 audio/hda: turn some dprintfs into trace points

In reply to Gerd Hoffmann
---
 hw/audio/hda-codec.c  | 9 ++++-----
 hw/audio/trace-events | 4 ++++
 2 files changed, 8 insertions(+), 5 deletions(-)
hw/audio/hda-codec.c +4 -5
index 9ee2f3d55f..13d4473f18 100644
@@ -24,6 +24,7 @@
 #include "intel-hda.h"
 #include "intel-hda-defs.h"
 #include "audio/audio.h"
+#include "trace.h"
 
 /* -------------------------------------------------------------------------- */
 
@@ -394,8 +395,7 @@ static void hda_audio_set_running(HDAAudioStream *st, bool running)
         return;
     }
     st->running = running;
-    dprint(st->state, 1, "%s: %s (stream %d)\n", st->node->name,
-           st->running ? "on" : "off", st->stream);
+    trace_hda_audio_running(st->node->name, st->stream, st->running);
     if (st->state->use_timer) {
         if (running) {
             int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
@@ -449,9 +449,8 @@ static void hda_audio_setup(HDAAudioStream *st)
         return;
     }
 
-    dprint(st->state, 1, "%s: format: %d x %s @ %d Hz\n",
-           st->node->name, st->as.nchannels,
-           fmt2name[st->as.fmt], st->as.freq);
+    trace_hda_audio_format(st->node->name, st->as.nchannels,
+                           fmt2name[st->as.fmt], st->as.freq);
 
     if (st->output) {
         if (use_timer) {
hw/audio/trace-events +4 -0
index fa1646d169..03340b9359 100644
@@ -17,3 +17,7 @@ milkymist_ac97_in_cb(int avail, uint32_t remaining) "avail %d remaining %u"
 milkymist_ac97_in_cb_transferred(int transferred) "transferred %d"
 milkymist_ac97_out_cb(int free, uint32_t remaining) "free %d remaining %u"
 milkymist_ac97_out_cb_transferred(int transferred) "transferred %d"
+
+# hw/audio/hda-codec.c
+hda_audio_running(const char *stream, int nr, bool running) "st %s, nr %d, run %d"
+hda_audio_format(const char *stream, int chan, const char *fmt, int freq) "st %s, %d x %s @ %d Hz"
Signature · 1 line
2.9.3
Reviewed-by on v1 2/4
#3 lore raw

Re: [Qemu-devel] [PATCH 2/4] audio/hda: turn some dprintfs into trace points

Quoted 1 line · Gerd Hoffmann
Quoted 52 lines
---
 hw/audio/hda-codec.c  | 9 ++++-----
 hw/audio/trace-events | 4 ++++
 2 files changed, 8 insertions(+), 5 deletions(-)
hw/audio/hda-codec.c +4 -5
index 9ee2f3d55f..13d4473f18 100644
@@ -24,6 +24,7 @@
 #include "intel-hda.h"
 #include "intel-hda-defs.h"
 #include "audio/audio.h"
+#include "trace.h"
 
 /* -------------------------------------------------------------------------- */
 
@@ -394,8 +395,7 @@ static void hda_audio_set_running(HDAAudioStream *st, bool running)
         return;
     }
     st->running = running;
-    dprint(st->state, 1, "%s: %s (stream %d)\n", st->node->name,
-           st->running ? "on" : "off", st->stream);
+    trace_hda_audio_running(st->node->name, st->stream, st->running);
     if (st->state->use_timer) {
         if (running) {
             int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
@@ -449,9 +449,8 @@ static void hda_audio_setup(HDAAudioStream *st)
         return;
     }
 
-    dprint(st->state, 1, "%s: format: %d x %s @ %d Hz\n",
-           st->node->name, st->as.nchannels,
-           fmt2name[st->as.fmt], st->as.freq);
+    trace_hda_audio_format(st->node->name, st->as.nchannels,
+                           fmt2name[st->as.fmt], st->as.freq);
 
     if (st->output) {
         if (use_timer) {
hw/audio/trace-events +4 -0
index fa1646d169..03340b9359 100644
@@ -17,3 +17,7 @@ milkymist_ac97_in_cb(int avail, uint32_t remaining) "avail %d remaining %u"
 milkymist_ac97_in_cb_transferred(int transferred) "transferred %d"
 milkymist_ac97_out_cb(int free, uint32_t remaining) "free %d remaining %u"
 milkymist_ac97_out_cb_transferred(int transferred) "transferred %d"
+
+# hw/audio/hda-codec.c
+hda_audio_running(const char *stream, int nr, bool running) "st %s, nr %d, run %d"
+hda_audio_format(const char *stream, int chan, const char *fmt, int freq) "st %s, %d x %s @ %d Hz"
#6 lore raw

Re: [Qemu-devel] [PATCH 4/4] audio/hda: detect output buffer overruns and underruns

In reply to Gerd Hoffmann
Quoted 3 lines · Gerd Hoffmann

If some event caused some larger playback hickup the fine-grained timer adjust isn't able to recover. Use a buffer overruns and underruns as indicator for that. Reset timer adjust logic in case we detected one.

It seems that this patch causes big lockup of my Win10 KVM guest when it tries to output a sound.
The three previous patches improved considerably the audio output though!

Quoted 26 lines
---
  hw/audio/hda-codec.c  | 22 ++++++++++++++++++++++
  hw/audio/trace-events |  2 ++
  2 files changed, 24 insertions(+)
hw/audio/hda-codec.c +7 -0
index f6fb1d0fa0..623021b7c7 100644
@@ -261,6 +261,13 @@ static void hda_audio_input_cb(void *opaque, int avail)
 
      int64_t to_transfer = audio_MIN(B_SIZE - (wpos - rpos), avail);
 
+    if (to_transfer == 0) {
+        /* reset timer adjust */
+        st->buft_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+        trace_hda_audio_underrun(st->node->name);
+        return;
+    }
+
      hda_timer_sync_adjust(st, -((wpos - rpos) + to_transfer - (B_SIZE >> 1)));
 
      while (to_transfer) {

hw/audio/hda-codec.c static void hda_audio_output_cb(void *opaque, int avail) · line 340

Quoted 9 lines
hw/audio/hda-codec.c +6 -0
@@ -325,6 +332,21 @@ static void hda_audio_output_cb(void *opaque, int avail)
 
      int64_t to_transfer = audio_MIN(wpos - rpos, avail);
 
+    if (to_transfer == 0) {
+        /* reset timer adjust */
+        st->buft_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+        trace_hda_audio_underrun(st->node->name);
+        return;
+    }

The lock happen here:

hda_audio_output_cb is called a lot and keeps running in underrun. While the 1kHz callback is called regularly and always "already have the data". So I wonder if the adjust here is not confusing the 1kHz callback which refuse to get new data.

Regards,
Fred

#7 lore raw

Re: [Qemu-devel] [PATCH 4/4] audio/hda: detect output buffer overruns and underruns

Quoted 11 lines · KONRAD Frederic
Quoted 3 lines · Gerd Hoffmann

If some event caused some larger playback hickup the fine-grained timer adjust isn't able to recover. Use a buffer overruns and underruns as indicator for that. Reset timer adjust logic in case we detected one.

It seems that this patch causes big lockup of my Win10 KVM guest when it tries to output a sound.
The three previous patches improved considerably the audio output though!

Can you try drop the "underrun" chunks, keeping only the "overrun"?

thanks,
Gerd

#8 lore raw

Re: [Qemu-devel] [PATCH 4/4] audio/hda: detect output buffer overruns and underruns

Hi Gerd,

Sorry I didn't had time to try that yet.

I'll try today!

Thanks,
Fred

Quoted 18 lines · Gerd Hoffmann
Quoted 11 lines · KONRAD Frederic
Quoted 3 lines · Gerd Hoffmann

If some event caused some larger playback hickup the fine-grained timer adjust isn't able to recover. Use a buffer overruns and underruns as indicator for that. Reset timer adjust logic in case we detected one.

It seems that this patch causes big lockup of my Win10 KVM guest when it tries to output a sound.
The three previous patches improved considerably the audio output though!

Can you try drop the "underrun" chunks, keeping only the "overrun"?

thanks,
Gerd

#9 lore raw

Re: [Qemu-devel] [PATCH 4/4] audio/hda: detect output buffer overruns and underruns

In reply to Gerd Hoffmann
Quoted 13 lines · Gerd Hoffmann
Quoted 11 lines · KONRAD Frederic
Quoted 3 lines · Gerd Hoffmann

If some event caused some larger playback hickup the fine-grained timer adjust isn't able to recover. Use a buffer overruns and underruns as indicator for that. Reset timer adjust logic in case we detected one.

It seems that this patch causes big lockup of my Win10 KVM guest when it tries to output a sound.
The three previous patches improved considerably the audio output though!

Quoted 5 lines

Can you try drop the "underrun" chunks, keeping only the "overrun"?

thanks,
Gerd

Hi Gerd,

Tested yesterday it seems that both are causing lockup. The overrun a little less though.

Thanks,
Fred

#10 lore raw

Re: [Qemu-devel] [PATCH 4/4] audio/hda: detect output buffer overruns and underruns

Quoted 18 lines · KONRAD Frederic
Quoted 15 lines · Gerd Hoffmann
Quoted 11 lines · KONRAD Frederic
Quoted 3 lines · Gerd Hoffmann

If some event caused some larger playback hickup the fine-grained timer adjust isn't able to recover. Use a buffer overruns and underruns as indicator for that. Reset timer adjust logic in case we detected one.

It seems that this patch causes big lockup of my Win10 KVM guest when it tries to output a sound.
The three previous patches improved considerably the audio output though!

Can you try drop the "underrun" chunks, keeping only the "overrun"?

Quoted 8 lines
Quoted 3 lines

thanks,
Gerd

Hi Gerd,

Tested yesterday it seems that both are causing lockup. The overrun a little less though.

Hints how to reproduce this? Which guest? Which application?

thanks,
Gerd