From nobody Thu May 2 01:10:37 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1524144358333738.958552425586; Thu, 19 Apr 2018 06:25:58 -0700 (PDT) Received: from localhost ([::1]:46365 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f99ZN-0000iP-S5 for importer@patchew.org; Thu, 19 Apr 2018 09:25:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41579) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f99L9-0004yq-Jz for qemu-devel@nongnu.org; Thu, 19 Apr 2018 09:11:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f99L4-0000Zk-8v for qemu-devel@nongnu.org; Thu, 19 Apr 2018 09:11:07 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:49462 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1f99L4-0000Xi-0b for qemu-devel@nongnu.org; Thu, 19 Apr 2018 09:11:02 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 13C9A4023156; Thu, 19 Apr 2018 13:10:58 +0000 (UTC) Received: from sirius.home.kraxel.org (ovpn-117-151.ams2.redhat.com [10.36.117.151]) by smtp.corp.redhat.com (Postfix) with ESMTP id A210F2166BC7; Thu, 19 Apr 2018 13:10:52 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id B4A1440DDF; Thu, 19 Apr 2018 15:10:51 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Thu, 19 Apr 2018 15:10:48 +0200 Message-Id: <20180419131051.11249-2-kraxel@redhat.com> In-Reply-To: <20180419131051.11249-1-kraxel@redhat.com> References: <20180419131051.11249-1-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Thu, 19 Apr 2018 13:10:58 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Thu, 19 Apr 2018 13:10:58 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kraxel@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 1/4] audio/hda: create millisecond timers that handle IO X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: martin@schrodt.org, Gerd Hoffmann Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Martin Schrodt Message-id: 20171015184033.2951-3-martin@schrodt.org [ kraxel: keep old code for compatibility with older qemu versions, add property to switch code paths at runtime ] Signed-off-by: Gerd Hoffmann --- include/hw/compat.h | 7 ++ hw/audio/hda-codec.c | 263 ++++++++++++++++++++++++++++++++++++++++++++++-= ---- hw/audio/intel-hda.c | 7 -- 3 files changed, 244 insertions(+), 33 deletions(-) diff --git a/include/hw/compat.h b/include/hw/compat.h index 13242b831a..dced6d81bf 100644 --- a/include/hw/compat.h +++ b/include/hw/compat.h @@ -1,6 +1,13 @@ #ifndef HW_COMPAT_H #define HW_COMPAT_H =20 +#define HW_COMPAT_2_12 \ + {\ + .driver =3D "hda-audio",\ + .property =3D "use-timer",\ + .value =3D "false",\ + }, + #define HW_COMPAT_2_11 \ {\ .driver =3D "hpet",\ diff --git a/hw/audio/hda-codec.c b/hw/audio/hda-codec.c index e8aa7842e6..9ee2f3d55f 100644 --- a/hw/audio/hda-codec.c +++ b/hw/audio/hda-codec.c @@ -18,6 +18,7 @@ */ =20 #include "qemu/osdep.h" +#include "qemu/atomic.h" #include "hw/hw.h" #include "hw/pci/pci.h" #include "intel-hda.h" @@ -126,6 +127,11 @@ static void hda_codec_parse_fmt(uint32_t format, struc= t audsettings *as) #define PARAM nomixemu #include "hda-codec-common.h" =20 +#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) + /* -----------------------------------------------------------------------= --- */ =20 static const char *fmt2name[] =3D { @@ -154,8 +160,13 @@ struct HDAAudioStream { SWVoiceIn *in; SWVoiceOut *out; } voice; - uint8_t buf[HDA_BUFFER_SIZE]; - uint32_t bpos; + uint8_t compat_buf[HDA_BUFFER_SIZE]; + uint32_t compat_bpos; + uint8_t buf[8192]; /* size must be power of two */ + int64_t rpos; + int64_t wpos; + QEMUTimer *buft; + int64_t buft_start; }; =20 #define TYPE_HDA_AUDIO "hda-audio" @@ -174,55 +185,201 @@ struct HDAAudioState { /* properties */ uint32_t debug; bool mixer; + bool use_timer; }; =20 +static inline int64_t hda_bytes_per_second(HDAAudioStream *st) +{ + return 2 * st->as.nchannels * st->as.freq; +} + +static inline void hda_timer_sync_adjust(HDAAudioStream *st, int64_t targe= t_pos) +{ + int64_t corr =3D + NANOSECONDS_PER_SECOND * target_pos / hda_bytes_per_second(st); + if (corr > MAX_CORR) { + corr =3D MAX_CORR; + } else if (corr < -MAX_CORR) { + corr =3D -MAX_CORR; + } + atomic_fetch_add(&st->buft_start, corr); +} + +static void hda_audio_input_timer(void *opaque) +{ + HDAAudioStream *st =3D opaque; + + int64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + + int64_t buft_start =3D atomic_fetch_add(&st->buft_start, 0); + int64_t wpos =3D atomic_fetch_add(&st->wpos, 0); + int64_t rpos =3D atomic_fetch_add(&st->rpos, 0); + + int64_t wanted_rpos =3D hda_bytes_per_second(st) * (now - buft_start) + / NANOSECONDS_PER_SECOND; + wanted_rpos &=3D -4; /* IMPORTANT! clip to frames */ + + if (wanted_rpos <=3D rpos) { + /* we already transmitted the data */ + goto out_timer; + } + + int64_t to_transfer =3D audio_MIN(wpos - rpos, wanted_rpos - rpos); + while (to_transfer) { + uint32_t start =3D (rpos & B_MASK); + uint32_t chunk =3D audio_MIN(B_SIZE - start, to_transfer); + int rc =3D hda_codec_xfer( + &st->state->hda, st->stream, false, st->buf + start, chunk= ); + if (!rc) { + break; + } + rpos +=3D chunk; + to_transfer -=3D chunk; + atomic_fetch_add(&st->rpos, chunk); + } + +out_timer: + + if (st->running) { + timer_mod_anticipate_ns(st->buft, now + HDA_TIMER_TICKS); + } +} + static void hda_audio_input_cb(void *opaque, int avail) { HDAAudioStream *st =3D opaque; + + int64_t wpos =3D atomic_fetch_add(&st->wpos, 0); + int64_t rpos =3D atomic_fetch_add(&st->rpos, 0); + + int64_t to_transfer =3D audio_MIN(B_SIZE - (wpos - rpos), avail); + + hda_timer_sync_adjust(st, -((wpos - rpos) + to_transfer - (B_SIZE >> 1= ))); + + while (to_transfer) { + uint32_t start =3D (uint32_t) (wpos & B_MASK); + uint32_t chunk =3D (uint32_t) audio_MIN(B_SIZE - start, to_transfe= r); + uint32_t read =3D AUD_read(st->voice.in, st->buf + start, chunk); + wpos +=3D read; + to_transfer -=3D read; + atomic_fetch_add(&st->wpos, read); + if (chunk !=3D read) { + break; + } + } +} + +static void hda_audio_output_timer(void *opaque) +{ + HDAAudioStream *st =3D opaque; + + int64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + + int64_t buft_start =3D atomic_fetch_add(&st->buft_start, 0); + int64_t wpos =3D atomic_fetch_add(&st->wpos, 0); + int64_t rpos =3D atomic_fetch_add(&st->rpos, 0); + + int64_t wanted_wpos =3D hda_bytes_per_second(st) * (now - buft_start) + / NANOSECONDS_PER_SECOND; + wanted_wpos &=3D -4; /* IMPORTANT! clip to frames */ + + if (wanted_wpos <=3D wpos) { + /* we already received the data */ + goto out_timer; + } + + int64_t to_transfer =3D audio_MIN(B_SIZE - (wpos - rpos), wanted_wpos = - wpos); + while (to_transfer) { + uint32_t start =3D (wpos & B_MASK); + uint32_t chunk =3D audio_MIN(B_SIZE - start, to_transfer); + int rc =3D hda_codec_xfer( + &st->state->hda, st->stream, true, st->buf + start, chunk); + if (!rc) { + break; + } + wpos +=3D chunk; + to_transfer -=3D chunk; + atomic_fetch_add(&st->wpos, chunk); + } + +out_timer: + + if (st->running) { + timer_mod_anticipate_ns(st->buft, now + HDA_TIMER_TICKS); + } +} + +static void hda_audio_output_cb(void *opaque, int avail) +{ + HDAAudioStream *st =3D opaque; + + int64_t wpos =3D atomic_fetch_add(&st->wpos, 0); + int64_t rpos =3D atomic_fetch_add(&st->rpos, 0); + + int64_t to_transfer =3D audio_MIN(wpos - rpos, avail); + + hda_timer_sync_adjust(st, (wpos - rpos) - to_transfer - (B_SIZE >> 1)); + + while (to_transfer) { + uint32_t start =3D (uint32_t) (rpos & B_MASK); + uint32_t chunk =3D (uint32_t) audio_MIN(B_SIZE - start, to_transfe= r); + uint32_t written =3D AUD_write(st->voice.out, st->buf + start, chu= nk); + rpos +=3D written; + to_transfer -=3D written; + atomic_fetch_add(&st->rpos, written); + if (chunk !=3D written) { + break; + } + } +} + +static void hda_audio_compat_input_cb(void *opaque, int avail) +{ + HDAAudioStream *st =3D opaque; int recv =3D 0; int len; bool rc; =20 - while (avail - recv >=3D sizeof(st->buf)) { - if (st->bpos !=3D sizeof(st->buf)) { - len =3D AUD_read(st->voice.in, st->buf + st->bpos, - sizeof(st->buf) - st->bpos); - st->bpos +=3D len; + while (avail - recv >=3D sizeof(st->compat_buf)) { + if (st->compat_bpos !=3D sizeof(st->compat_buf)) { + len =3D AUD_read(st->voice.in, st->compat_buf + st->compat_bpo= s, + sizeof(st->compat_buf) - st->compat_bpos); + st->compat_bpos +=3D len; recv +=3D len; - if (st->bpos !=3D sizeof(st->buf)) { + if (st->compat_bpos !=3D sizeof(st->compat_buf)) { break; } } rc =3D hda_codec_xfer(&st->state->hda, st->stream, false, - st->buf, sizeof(st->buf)); + st->compat_buf, sizeof(st->compat_buf)); if (!rc) { break; } - st->bpos =3D 0; + st->compat_bpos =3D 0; } } =20 -static void hda_audio_output_cb(void *opaque, int avail) +static void hda_audio_compat_output_cb(void *opaque, int avail) { HDAAudioStream *st =3D opaque; int sent =3D 0; int len; bool rc; =20 - while (avail - sent >=3D sizeof(st->buf)) { - if (st->bpos =3D=3D sizeof(st->buf)) { + while (avail - sent >=3D sizeof(st->compat_buf)) { + if (st->compat_bpos =3D=3D sizeof(st->compat_buf)) { rc =3D hda_codec_xfer(&st->state->hda, st->stream, true, - st->buf, sizeof(st->buf)); + st->compat_buf, sizeof(st->compat_buf)); if (!rc) { break; } - st->bpos =3D 0; + st->compat_bpos =3D 0; } - len =3D AUD_write(st->voice.out, st->buf + st->bpos, - sizeof(st->buf) - st->bpos); - st->bpos +=3D len; + len =3D AUD_write(st->voice.out, st->compat_buf + st->compat_bpos, + sizeof(st->compat_buf) - st->compat_bpos); + st->compat_bpos +=3D len; sent +=3D len; - if (st->bpos !=3D sizeof(st->buf)) { + if (st->compat_bpos !=3D sizeof(st->compat_buf)) { break; } } @@ -239,6 +396,17 @@ static void hda_audio_set_running(HDAAudioStream *st, = bool running) st->running =3D running; dprint(st->state, 1, "%s: %s (stream %d)\n", st->node->name, st->running ? "on" : "off", st->stream); + if (st->state->use_timer) { + if (running) { + int64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + st->rpos =3D 0; + st->wpos =3D 0; + st->buft_start =3D now; + timer_mod_anticipate_ns(st->buft, now + HDA_TIMER_TICKS); + } else { + timer_del(st->buft); + } + } if (st->output) { AUD_set_active_out(st->voice.out, st->running); } else { @@ -274,6 +442,9 @@ static void hda_audio_set_amp(HDAAudioStream *st) =20 static void hda_audio_setup(HDAAudioStream *st) { + bool use_timer =3D st->state->use_timer; + audio_callback_fn cb; + if (st->node =3D=3D NULL) { return; } @@ -283,13 +454,25 @@ static void hda_audio_setup(HDAAudioStream *st) fmt2name[st->as.fmt], st->as.freq); =20 if (st->output) { + if (use_timer) { + cb =3D hda_audio_output_cb; + st->buft =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, + hda_audio_output_timer, st); + } else { + cb =3D hda_audio_compat_output_cb; + } st->voice.out =3D AUD_open_out(&st->state->card, st->voice.out, - st->node->name, st, - hda_audio_output_cb, &st->as); + st->node->name, st, cb, &st->as); } else { + if (use_timer) { + cb =3D hda_audio_input_cb; + st->buft =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, + hda_audio_input_timer, st); + } else { + cb =3D hda_audio_compat_input_cb; + } st->voice.in =3D AUD_open_in(&st->state->card, st->voice.in, - st->node->name, st, - hda_audio_input_cb, &st->as); + st->node->name, st, cb, &st->as); } } =20 @@ -505,7 +688,7 @@ static int hda_audio_init(HDACodecDevice *hda, const st= ruct desc_codec *desc) /* unmute output by default */ st->gain_left =3D QEMU_HDA_AMP_STEPS; st->gain_right =3D QEMU_HDA_AMP_STEPS; - st->bpos =3D sizeof(st->buf); + st->compat_bpos =3D sizeof(st->compat_buf); st->output =3D true; } else { st->output =3D false; @@ -532,6 +715,9 @@ static void hda_audio_exit(HDACodecDevice *hda) if (st->node =3D=3D NULL) { continue; } + if (a->use_timer) { + timer_del(st->buft); + } if (st->output) { AUD_close_out(&a->card, st->voice.out); } else { @@ -581,6 +767,26 @@ static void hda_audio_reset(DeviceState *dev) } } =20 +static bool vmstate_hda_audio_stream_buf_needed(void *opaque) +{ + HDAAudioStream *st =3D opaque; + return st->state->use_timer; +} + +static const VMStateDescription vmstate_hda_audio_stream_buf =3D { + .name =3D "hda-audio-stream/buffer", + .version_id =3D 1, + .needed =3D vmstate_hda_audio_stream_buf_needed, + .fields =3D (VMStateField[]) { + VMSTATE_BUFFER(buf, HDAAudioStream), + VMSTATE_INT64(rpos, HDAAudioStream), + VMSTATE_INT64(wpos, HDAAudioStream), + VMSTATE_TIMER_PTR(buft, HDAAudioStream), + VMSTATE_INT64(buft_start, HDAAudioStream), + VMSTATE_END_OF_LIST() + } +}; + static const VMStateDescription vmstate_hda_audio_stream =3D { .name =3D "hda-audio-stream", .version_id =3D 1, @@ -592,9 +798,13 @@ static const VMStateDescription vmstate_hda_audio_stre= am =3D { VMSTATE_UINT32(gain_right, HDAAudioStream), VMSTATE_BOOL(mute_left, HDAAudioStream), VMSTATE_BOOL(mute_right, HDAAudioStream), - VMSTATE_UINT32(bpos, HDAAudioStream), - VMSTATE_BUFFER(buf, HDAAudioStream), + VMSTATE_UINT32(compat_bpos, HDAAudioStream), + VMSTATE_BUFFER(compat_buf, HDAAudioStream), VMSTATE_END_OF_LIST() + }, + .subsections =3D (const VMStateDescription * []) { + &vmstate_hda_audio_stream_buf, + NULL } }; =20 @@ -615,6 +825,7 @@ static const VMStateDescription vmstate_hda_audio =3D { static Property hda_audio_properties[] =3D { DEFINE_PROP_UINT32("debug", HDAAudioState, debug, 0), DEFINE_PROP_BOOL("mixer", HDAAudioState, mixer, true), + DEFINE_PROP_BOOL("use-timer", HDAAudioState, use_timer, true), DEFINE_PROP_END_OF_LIST(), }; =20 diff --git a/hw/audio/intel-hda.c b/hw/audio/intel-hda.c index 948268afd8..23a2cf6484 100644 --- a/hw/audio/intel-hda.c +++ b/hw/audio/intel-hda.c @@ -407,13 +407,6 @@ static bool intel_hda_xfer(HDACodecDevice *dev, uint32= _t stnr, bool output, if (st->bpl =3D=3D 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; - } =20 left =3D len; s =3D st->bentries; --=20 2.9.3 From nobody Thu May 2 01:10:37 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 15241449472951022.5527962388658; Thu, 19 Apr 2018 06:35:47 -0700 (PDT) Received: from localhost ([::1]:47727 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f99ix-00013Z-GM for importer@patchew.org; Thu, 19 Apr 2018 09:35:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41621) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f99LD-00053C-Sx for qemu-devel@nongnu.org; Thu, 19 Apr 2018 09:11:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f99L4-0000ZM-6G for qemu-devel@nongnu.org; Thu, 19 Apr 2018 09:11:11 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:53692 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1f99L4-0000Xj-0g for qemu-devel@nongnu.org; Thu, 19 Apr 2018 09:11:02 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 48B0E406FA1C; Thu, 19 Apr 2018 13:10:59 +0000 (UTC) Received: from sirius.home.kraxel.org (ovpn-117-151.ams2.redhat.com [10.36.117.151]) by smtp.corp.redhat.com (Postfix) with ESMTP id A226F10F1C05; Thu, 19 Apr 2018 13:10:52 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id BDA5C31FB4; Thu, 19 Apr 2018 15:10:51 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Thu, 19 Apr 2018 15:10:49 +0200 Message-Id: <20180419131051.11249-3-kraxel@redhat.com> In-Reply-To: <20180419131051.11249-1-kraxel@redhat.com> References: <20180419131051.11249-1-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Thu, 19 Apr 2018 13:10:59 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Thu, 19 Apr 2018 13:10:59 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kraxel@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 2/4] audio/hda: turn some dprintfs into trace points X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: martin@schrodt.org, Gerd Hoffmann Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Gerd Hoffmann Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- hw/audio/hda-codec.c | 9 ++++----- hw/audio/trace-events | 4 ++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/hw/audio/hda-codec.c b/hw/audio/hda-codec.c index 9ee2f3d55f..13d4473f18 100644 --- a/hw/audio/hda-codec.c +++ b/hw/audio/hda-codec.c @@ -24,6 +24,7 @@ #include "intel-hda.h" #include "intel-hda-defs.h" #include "audio/audio.h" +#include "trace.h" =20 /* -----------------------------------------------------------------------= --- */ =20 @@ -394,8 +395,7 @@ static void hda_audio_set_running(HDAAudioStream *st, b= ool running) return; } st->running =3D 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 =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); @@ -449,9 +449,8 @@ static void hda_audio_setup(HDAAudioStream *st) return; } =20 - 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); =20 if (st->output) { if (use_timer) { diff --git a/hw/audio/trace-events b/hw/audio/trace-events index fa1646d169..03340b9359 100644 --- a/hw/audio/trace-events +++ b/hw/audio/trace-events @@ -17,3 +17,7 @@ milkymist_ac97_in_cb(int avail, uint32_t remaining) "avai= l %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" --=20 2.9.3 From nobody Thu May 2 01:10:37 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1524144515642745.4743911489235; Thu, 19 Apr 2018 06:28:35 -0700 (PDT) Received: from localhost ([::1]:46769 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f99bz-00037s-0x for importer@patchew.org; Thu, 19 Apr 2018 09:28:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41588) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f99LA-0004zJ-4e for qemu-devel@nongnu.org; Thu, 19 Apr 2018 09:11:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f99L4-0000ZY-6Z for qemu-devel@nongnu.org; Thu, 19 Apr 2018 09:11:08 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:45247 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1f99L4-0000Xl-0t for qemu-devel@nongnu.org; Thu, 19 Apr 2018 09:11:02 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5797F722FC; Thu, 19 Apr 2018 13:10:59 +0000 (UTC) Received: from sirius.home.kraxel.org (ovpn-117-151.ams2.redhat.com [10.36.117.151]) by smtp.corp.redhat.com (Postfix) with ESMTP id A1DB910F1C04; Thu, 19 Apr 2018 13:10:52 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id C61853EB9B; Thu, 19 Apr 2018 15:10:51 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Thu, 19 Apr 2018 15:10:50 +0200 Message-Id: <20180419131051.11249-4-kraxel@redhat.com> In-Reply-To: <20180419131051.11249-1-kraxel@redhat.com> References: <20180419131051.11249-1-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Thu, 19 Apr 2018 13:10:59 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Thu, 19 Apr 2018 13:10:59 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kraxel@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 3/4] audio/hda: tweak timer adjust logic X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: martin@schrodt.org, Gerd Hoffmann Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Gerd Hoffmann --- hw/audio/hda-codec.c | 20 +++++++++++++------- hw/audio/trace-events | 1 + 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/hw/audio/hda-codec.c b/hw/audio/hda-codec.c index 13d4473f18..f6fb1d0fa0 100644 --- a/hw/audio/hda-codec.c +++ b/hw/audio/hda-codec.c @@ -129,7 +129,6 @@ static void hda_codec_parse_fmt(uint32_t format, struct= audsettings *as) #include "hda-codec-common.h" =20 #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) =20 @@ -196,13 +195,20 @@ static inline int64_t hda_bytes_per_second(HDAAudioSt= ream *st) =20 static inline void hda_timer_sync_adjust(HDAAudioStream *st, int64_t targe= t_pos) { - int64_t corr =3D - NANOSECONDS_PER_SECOND * target_pos / hda_bytes_per_second(st); - if (corr > MAX_CORR) { - corr =3D MAX_CORR; - } else if (corr < -MAX_CORR) { - corr =3D -MAX_CORR; + int64_t limit =3D B_SIZE / 8; + int64_t corr =3D 0; + + if (target_pos > limit) { + corr =3D HDA_TIMER_TICKS; } + if (target_pos < -limit) { + corr =3D -HDA_TIMER_TICKS; + } + if (corr =3D=3D 0) { + return; + } + + trace_hda_audio_adjust(st->node->name, target_pos); atomic_fetch_add(&st->buft_start, corr); } =20 diff --git a/hw/audio/trace-events b/hw/audio/trace-events index 03340b9359..30112d97c4 100644 --- a/hw/audio/trace-events +++ b/hw/audio/trace-events @@ -21,3 +21,4 @@ milkymist_ac97_out_cb_transferred(int transferred) "trans= ferred %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" --=20 2.9.3 From nobody Thu May 2 01:10:37 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1524144780272687.3728079410573; Thu, 19 Apr 2018 06:33:00 -0700 (PDT) Received: from localhost ([::1]:47526 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f99gI-00075j-SS for importer@patchew.org; Thu, 19 Apr 2018 09:32:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41591) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f99LA-0004zX-EA for qemu-devel@nongnu.org; Thu, 19 Apr 2018 09:11:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f99L4-0000ZN-63 for qemu-devel@nongnu.org; Thu, 19 Apr 2018 09:11:08 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:49464 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1f99L4-0000Xh-0u for qemu-devel@nongnu.org; Thu, 19 Apr 2018 09:11:02 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4BC0D4023150; Thu, 19 Apr 2018 13:10:57 +0000 (UTC) Received: from sirius.home.kraxel.org (ovpn-117-151.ams2.redhat.com [10.36.117.151]) by smtp.corp.redhat.com (Postfix) with ESMTP id A1DD521A4682; Thu, 19 Apr 2018 13:10:52 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id CE8403EB9C; Thu, 19 Apr 2018 15:10:51 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Thu, 19 Apr 2018 15:10:51 +0200 Message-Id: <20180419131051.11249-5-kraxel@redhat.com> In-Reply-To: <20180419131051.11249-1-kraxel@redhat.com> References: <20180419131051.11249-1-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Thu, 19 Apr 2018 13:10:57 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Thu, 19 Apr 2018 13:10:57 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kraxel@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 4/4] audio/hda: detect output buffer overruns and underruns X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: martin@schrodt.org, Gerd Hoffmann Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Gerd Hoffmann --- hw/audio/hda-codec.c | 22 ++++++++++++++++++++++ hw/audio/trace-events | 2 ++ 2 files changed, 24 insertions(+) diff --git a/hw/audio/hda-codec.c b/hw/audio/hda-codec.c index f6fb1d0fa0..623021b7c7 100644 --- a/hw/audio/hda-codec.c +++ b/hw/audio/hda-codec.c @@ -261,6 +261,13 @@ static void hda_audio_input_cb(void *opaque, int avail) =20 int64_t to_transfer =3D audio_MIN(B_SIZE - (wpos - rpos), avail); =20 + if (to_transfer =3D=3D 0) { + /* reset timer adjust */ + st->buft_start =3D 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= ))); =20 while (to_transfer) { @@ -325,6 +332,21 @@ static void hda_audio_output_cb(void *opaque, int avai= l) =20 int64_t to_transfer =3D audio_MIN(wpos - rpos, avail); =20 + if (to_transfer =3D=3D 0) { + /* reset timer adjust */ + st->buft_start =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + trace_hda_audio_underrun(st->node->name); + return; + } + if (wpos - rpos =3D=3D B_SIZE) { + /* drop buffer, reset timer adjust */ + st->rpos =3D 0; + st->wpos =3D 0; + st->buft_start =3D 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)); =20 while (to_transfer) { diff --git a/hw/audio/trace-events b/hw/audio/trace-events index 30112d97c4..3b2ea2727b 100644 --- a/hw/audio/trace-events +++ b/hw/audio/trace-events @@ -22,3 +22,5 @@ milkymist_ac97_out_cb_transferred(int transferred) "trans= ferred %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" --=20 2.9.3