Skip to contentSkip to conversation

Several fixes for the Pulse Audio driver, and the HDA device.

PATCH In review Martin Schrodt 4 participants 5 messages Oct 10, 2017 – Oct 11, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Several fixes for the Pulse Audio driver, and the HDA device.

Please see

https://www.reddit.com/r/VFIO/comments/74vokw/improved_pulse_audio_driver_for_qemu/

for motivation, and more coverage.

Changes:

- Remove PA reader/writer threads from paaudio.c, and do IO from the audio timer directly.
- Add 1 millisecond timers which interface with the HDA device, pulling and pushing data to and from it, to enable the guest driver to measure DMA timing by just looking the LPIB registers.
- Expose new configurable settings, such as TLENGTH and FRAGSIZE, plus settings to enable PA_STREAM_ADJUST_LATENCY for input and output device separately.
- Fix the input delay when first using the input device.

---
 audio/audio.c             |   4 +
 audio/audio_int.h         |   2 +
 audio/paaudio.c           | 640 ++++++++++++++++++++--------------------------
 hw/audio/hda-codec.c      | 218 +++++++++++++---
 hw/audio/intel-hda-defs.h |   1 +
 hw/audio/intel-hda.c      |  14 +-
 6 files changed, 468 insertions(+), 411 deletions(-)
audio/audio.c +4 -0
index beafed209b..fba1604c34 100644
@@ -2066,3 +2066,7 @@ void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
         }
     }
 }
+
+int64_t audio_get_timer_ticks(void) {
+    return conf.period.ticks;
+}
audio/audio_int.h +2 -0
index 5bcb1c60e1..2f7fc4f8ac 100644
@@ -214,6 +214,8 @@ extern struct audio_driver pa_audio_driver;
 extern struct audio_driver spice_audio_driver;
 extern const struct mixeng_volume nominal_volume;
 
+int64_t audio_get_timer_ticks(void);
+
 void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as);
 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len);
 
audio/paaudio.c +274 -366

870 lines not on this page · raw message

hw/audio/hda-codec.c +180 -38

287 lines not on this page · raw message

hw/audio/intel-hda-defs.h +1 -0
index 2e37e5b874..900a2695ec 100644
@@ -3,6 +3,7 @@
 
 /* qemu */
 #define HDA_BUFFER_SIZE 256
+#define HDA_TIMER_TICKS (SCALE_MS)
 
 /* --------------------------------------------------------------------- */
 /* from linux/sound/pci/hda/hda_intel.c                                  */
hw/audio/intel-hda.c +7 -7
index 18a50a8f83..9d3da3185b 100644
@@ -407,13 +407,13 @@ 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;
-    }
+//    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.14.2
#1 lore raw
Quoted 8 lines · Martin Schrodt

Please see

https://www.reddit.com/r/VFIO/comments/74vokw/improved_pulse_audio_driver_for_qemu/

for motivation, and more coverage.

Changes:

Quoted 7 lines

- Remove PA reader/writer threads from paaudio.c, and do IO from the audio timer directly.
- Add 1 millisecond timers which interface with the HDA device, pulling and pushing data to and from it, to enable the guest driver to measure DMA timing by just looking the LPIB registers.
- Expose new configurable settings, such as TLENGTH and FRAGSIZE, plus settings to enable PA_STREAM_ADJUST_LATENCY for input and output device separately.
- Fix the input delay when first using the input device.

That's a lot of changes to be slamming in one patch. Any chance you can split it into a series of smaller patches that are easier to review individually? Perhaps one patch per item in your bulleted list is a good start for subdividing this into something that is not so massive.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#2 lore raw
Quoted 4 lines · Eric Blake

That's a lot of changes to be slamming in one patch. Any chance you can split it into a series of smaller patches that are easier to review individually? Perhaps one patch per item in your bulleted list is a good start for subdividing this into something that is not so massive.

I could make a separate request with all my commits separated.

The patch is all these squashed:

https://github.com/qemu/qemu/compare/master...spheenik:master

Problem is, and that's why I decided to hand it over like this, is that the commits are kind of a log of my learning experience, getting to this, and so not really separated better.

These 3 here are only paaudio.c

- Remove PA reader/writer threads from paaudio.c, and do IO from the audio timer directly.
- Expose new configurable settings, such as TLENGTH and FRAGSIZE, plus settings to
enable PA_STREAM_ADJUST_LATENCY for input and output device separately.
- Fix the input delay when first using the input device.

and cannot really be separated, unfortunately.
The third one is really only a consequence of the removal of the additional audio thread.

And this one:

- Add 1 millisecond timers which interface with the HDA device, pulling and pushing data
to and from it, to enable the guest driver to measure DMA timing by
just looking the LPIB registers.

is hda-codec.c.

Hope that helps,

Martin

#3 lore raw
Quoted 10 lines · Martin Schrodt

These 3 here are only paaudio.c

- Remove PA reader/writer threads from paaudio.c, and do IO from the audio timer directly.
- Expose new configurable settings, such as TLENGTH and FRAGSIZE, plus settings to
enable PA_STREAM_ADJUST_LATENCY for input and output device separately.
- Fix the input delay when first using the input device.

and cannot really be separated, unfortunately.

Sure they can! :) This can actually be a good occasion to learn more of git. Say you have this large patch in branch qemu-audio-v1, you can now do

git checkout -b qemu-audio-v2 qemu-audio-v1^
git checkout qemu-audio-v1 -- .
git reset

This will create a branch *without* the patch, while putting the files *with* the patch in your working tree.

Now you can add your changes gradually to the staging area of git (it is called the "index"):

git add -p

This command will ask you interactively about staging each separate hunk, for files that git already tracks. When done, you can run

  git diff --staged

to show the diff that is staged for commit. And also

git diff

to show the diff that is not staged yet.

If you are happy with the staged changes, run:

  git stash --keep-index

and test them. If there is a problem, fix it. Then type

git commit -a -s

to commit the staged changes to your local branch called qemu-audio-v2 (write a good commit message now that the commit is fresh in your mind!).

Now get back to the full changes:

git checkout qemu-audio-v1 -- .
git reset

and restart from the "git add -p" step to get the next part of your patch. Remember, each single one of your patches must build and runs (implementing the next logical step in the feature), and at the last patch, the feature is complete.

Now format the patches as email messages, and send them to the list. Standing in the root of your QEMU directory, run the following:

git fetch origin
rm -f -- *.patch
git format-patch --cover-letter --subject-prefix="PATCH v2" \
origin/master..

This command will generate an email file for each one of your commits. The patch files will be numbered. The file numbered 0000 is a cover letter, which you should edit in-place:

* in the Subject: line, summarize the goal of your series,

* in the message body, describe the changes on a broad level.

Now it's time to mail-bomb the list. You have already used git send-email here so I won't get into further detail.

(Loosely based on
https://github.com/tianocore/tianocore.github.io/wiki/Laszlo's-unkempt-git-guide-for-edk2-contributors-and-maintainers)

Thanks,

Paolo

Quoted 2 lines

The third one is really only a consequence of the removal of the additional audio thread.

#4 lore raw
In reply to Martin Schrodt
This series seems to have some coding style problems. See output below for more information:

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [Qemu-devel] [PATCH] Several fixes for the Pulse Audio driver, and the HDA device.

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

git config --local diff.renamelimit 0
git config --local diff.renames True

commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do

    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))

done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
Switched to a new branch 'test'
e49f94ef7b Several fixes for the Pulse Audio driver, and the HDA device.

=== OUTPUT BEGIN ===
Checking PATCH 1/1: Several fixes for the Pulse Audio driver, and the HDA device....
ERROR: open brace '{' following function declarations go on the next line #34: FILE: audio/audio.c:2070:
+int64_t audio_get_timer_ticks(void) {

ERROR: space prohibited between function name and open parenthesis '(' #146: FILE: audio/paaudio.c:106:
+ *(rerror) = pa_context_errno ((c)->context); \

ERROR: space prohibited between function name and open parenthesis '(' #161: FILE: audio/paaudio.c:118:
+ *(rerror) = pa_context_errno ((c)->context); \

ERROR: space prohibited between function name and open parenthesis '(' #266: FILE: audio/paaudio.c:126:
+static int qpa_run_out (HWVoiceOut *hw, int live)

ERROR: space prohibited between function name and open parenthesis '(' #286: FILE: audio/paaudio.c:139:
+ pa_threaded_mainloop_lock (pa->g->mainloop);

ERROR: space prohibited between function name and open parenthesis '(' #287: FILE: audio/paaudio.c:140:
+ CHECK_DEAD_GOTO (pa->g, pa->stream, &error, fail);

ERROR: space prohibited between function name and open parenthesis '(' #300: FILE: audio/paaudio.c:146:
+ samples = (int)(audio_MIN (avail_bytes, max_bytes)) >> hw->info.shift;

ERROR: do not use C99 // comments
#306: FILE: audio/paaudio.c:148:
+// if (avail_bytes < max_bytes) {

ERROR: unnecessary whitespace before a quoted newline #307: FILE: audio/paaudio.c:149:
+// dolog("avail: %d, wanted: %d \n", (int)avail_bytes, (int)max_bytes);

ERROR: do not use C99 // comments
#307: FILE: audio/paaudio.c:149:
+// dolog("avail: %d, wanted: %d \n", (int)avail_bytes, (int)max_bytes);

ERROR: do not use C99 // comments
#308: FILE: audio/paaudio.c:150:
+// }

ERROR: line over 90 characters
#312: FILE: audio/paaudio.c:152:
+// dolog("TRANSFER avail: %d bytes, max %d bytes -> %d samples from %d\n", (int)avail_bytes, (int)max_bytes, samples, rpos);

ERROR: do not use C99 // comments
#312: FILE: audio/paaudio.c:152:
+// dolog("TRANSFER avail: %d bytes, max %d bytes -> %d samples from %d\n", (int)avail_bytes, (int)max_bytes, samples, rpos);

ERROR: space prohibited between function name and open parenthesis '(' #324: FILE: audio/paaudio.c:157:
+ int convert_samples = audio_MIN (samples, left_till_end_samples);

WARNING: line over 80 characters
#325: FILE: audio/paaudio.c:158:
+ size_t convert_bytes_wanted = (size_t) convert_samples << hw->info.shift;

WARNING: line over 80 characters
#331: FILE: audio/paaudio.c:163:
+ CHECK_SUCCESS_GOTO(pa->g, &error, convert_bytes == convert_bytes_wanted, fail);

ERROR: space prohibited between function name and open parenthesis '(' #339: FILE: audio/paaudio.c:166:
+ hw->clip (pa_dst, src, convert_samples);

ERROR: line over 90 characters
#348: FILE: audio/paaudio.c:168:
+ r = pa_stream_write (pa->stream, pa_dst, convert_bytes, NULL, 0LL, PA_SEEK_RELATIVE);

ERROR: space prohibited between function name and open parenthesis '(' #348: FILE: audio/paaudio.c:168:
+ r = pa_stream_write (pa->stream, pa_dst, convert_bytes, NULL, 0LL, PA_SEEK_RELATIVE);

ERROR: space prohibited between function name and open parenthesis '(' #369: FILE: audio/paaudio.c:177:
+ pa_threaded_mainloop_unlock (pa->g->mainloop);

ERROR: space prohibited between function name and open parenthesis '(' #389: FILE: audio/paaudio.c:183:
+ qpa_logerr (error, "qpa_run_out failed\n");

ERROR: space prohibited between function name and open parenthesis '(' #400: FILE: audio/paaudio.c:192:
+static int qpa_run_in (HWVoiceIn *hw)

ERROR: space prohibited between function name and open parenthesis '(' #415: FILE: audio/paaudio.c:203:
+ pa_threaded_mainloop_lock (pa->g->mainloop);

ERROR: space prohibited between function name and open parenthesis '(' #416: FILE: audio/paaudio.c:204:
+ CHECK_DEAD_GOTO (pa->g, pa->stream, &error, fail);

ERROR: line over 90 characters
#418: FILE: audio/paaudio.c:206:
+ size_t bytes_wanted = ((unsigned int)(hw->samples - audio_pcm_hw_get_live_in(hw)) << hw->info.shift);

ERROR: do not use C99 // comments
#420: FILE: audio/paaudio.c:208:
+ // no room

ERROR: do not use C99 // comments
#432: FILE: audio/paaudio.c:214:
+ //dolog("WANT %d, HAVE %d\n", (int)bytes_wanted, (int) bytes_avail);

ERROR: if this code is redundant consider removing it #442: FILE: audio/paaudio.c:219:
+#if 0

ERROR: space required before the open parenthesis '(' #461: FILE: audio/paaudio.c:232:
+ if(n_dropped) {

WARNING: line over 80 characters
#489: FILE: audio/paaudio.c:250:
+ int chunk = audio_MIN ((int)(pa_avail >> hw->info.shift), hw->samples - wpos);

ERROR: space prohibited between function name and open parenthesis '(' #489: FILE: audio/paaudio.c:250:
+ int chunk = audio_MIN ((int)(pa_avail >> hw->info.shift), hw->samples - wpos);

ERROR: space prohibited between function name and open parenthesis '(' #490: FILE: audio/paaudio.c:251:
+ hw->conv (hw->conv_buf + wpos, pa_src, chunk);

ERROR: space prohibited between function name and open parenthesis '(' #514: FILE: audio/paaudio.c:263:
+ pa_threaded_mainloop_unlock (pa->g->mainloop);

ERROR: space prohibited between function name and open parenthesis '(' #527: FILE: audio/paaudio.c:269:
+ qpa_logerr (error, "qpa_run_in failed\n");

ERROR: line over 90 characters
#581: FILE: audio/paaudio.c:390:
+ | (g->conf.adjust_latency_out ? PA_STREAM_ADJUST_LATENCY : 0)

ERROR: line over 90 characters
#589: FILE: audio/paaudio.c:397:
+ | (g->conf.adjust_latency_in ? PA_STREAM_ADJUST_LATENCY : 0)

WARNING: line over 80 characters
#611: FILE: audio/paaudio.c:431:
+ int64_t timer_tick_duration = audio_MAX(audio_get_timer_ticks(), 1 * SCALE_MS);

ERROR: line over 90 characters
#612: FILE: audio/paaudio.c:432:
+ int64_t frames_per_tick_x1000 = ((timer_tick_duration * as->freq * 1000LL) / NANOSECONDS_PER_SECOND);

WARNING: line over 80 characters
#637: FILE: audio/paaudio.c:457:
+ dolog("OUT adjust latency: %s\n", g->conf.adjust_latency_out ? "yes" : "no");

ERROR: space prohibited between function name and open parenthesis '(' #647: FILE: audio/paaudio.c:459:
+ pa->ss.format = audfmt_to_pa (as->fmt, as->endianness);

ERROR: space prohibited between function name and open parenthesis '(' #652: FILE: audio/paaudio.c:463:
+ pa->ba.tlength = tlength * pa_frame_size (&pa->ss);

ERROR: space prohibited between function name and open parenthesis '(' #657: FILE: audio/paaudio.c:468:
+ obt_as.fmt = pa_to_audfmt (pa->ss.format, &obt_as.endianness);

WARNING: line over 80 characters
#718: FILE: audio/paaudio.c:503:
+ int64_t timer_tick_duration = audio_MAX(audio_get_timer_ticks(), 1 * SCALE_MS);

ERROR: line over 90 characters
#719: FILE: audio/paaudio.c:504:
+ int64_t frames_per_tick_x1000 = ((timer_tick_duration * as->freq * 1000LL) / NANOSECONDS_PER_SECOND);

ERROR: space prohibited between function name and open parenthesis '(' #743: FILE: audio/paaudio.c:527:
+ pa->ss.format = audfmt_to_pa (as->fmt, as->endianness);

ERROR: space prohibited between function name and open parenthesis '(' #747: FILE: audio/paaudio.c:531:
+ pa->ba.fragsize = fragsize * pa_frame_size (&pa->ss);

ERROR: space prohibited between function name and open parenthesis '(' #752: FILE: audio/paaudio.c:536:
+ obt_as.fmt = pa_to_audfmt (pa->ss.format, &obt_as.endianness);

ERROR: space prohibited between function name and open parenthesis '(' #775: FILE: audio/paaudio.c:549:
+ qpa_logerr (error, "pa_simple_new for playback failed\n");

ERROR: switch and case should be at the same indent #854: FILE: audio/paaudio.c:646:

     switch (cmd) {

+ case VOICE_VOLUME:

WARNING: line over 80 characters
#866: FILE: audio/paaudio.c:663:
+ pa_stream_get_index (pa->stream),

ERROR: space prohibited between function name and open parenthesis '(' #866: FILE: audio/paaudio.c:663:
+ pa_stream_get_index (pa->stream),

WARNING: line over 80 characters
#877: FILE: audio/paaudio.c:673:
+ pa_stream_get_index (pa->stream),

ERROR: space prohibited between function name and open parenthesis '(' #877: FILE: audio/paaudio.c:673:
+ pa_stream_get_index (pa->stream),

ERROR: open brace '{' following function declarations go on the next line #969: FILE: hw/audio/hda-codec.c:183:
+static void hda_audio_input_timer(void *opaque) {

WARNING: line over 80 characters
#982: FILE: hw/audio/hda-codec.c:196:
+ int64_t wanted_rpos = (st->as.freq * 4 * (now - buft_start)) / NANOSECONDS_PER_SECOND;

ERROR: do not use C99 // comments
#983: FILE: hw/audio/hda-codec.c:197:
+ wanted_rpos &= -4; // IMPORTANT! clip to frames

ERROR: do not use C99 // comments
#986: FILE: hw/audio/hda-codec.c:200:
+ // we already transmitted the data

ERROR: do not use C99 // comments
#994: FILE: hw/audio/hda-codec.c:208:
+ //dolog("%"PRId64"\n", wpos - rpos);

ERROR: line over 90 characters
#996: FILE: hw/audio/hda-codec.c:210:
+ //dolog("rpos: %"PRId64", wpos: %"PRId64", wanted: %"PRId64"\n", rpos, wpos, wanted_wpos);

ERROR: do not use C99 // comments
#996: FILE: hw/audio/hda-codec.c:210:
+ //dolog("rpos: %"PRId64", wpos: %"PRId64", wanted: %"PRId64"\n", rpos, wpos, wanted_wpos);

ERROR: line over 90 characters
#1001: FILE: hw/audio/hda-codec.c:215:
+ int rc = hda_codec_xfer(&st->state->hda, st->stream, false, st->buf + start, chunk);

ERROR: do not use C99 // comments
#1046: FILE: hw/audio/hda-codec.c:247:
+// int64_t overflow = wpos - rpos - to_transfer - (B_SIZE >> 3);

ERROR: do not use C99 // comments
#1047: FILE: hw/audio/hda-codec.c:248:
+// if (overflow > 0) {

ERROR: do not use C99 // comments
#1048: FILE: hw/audio/hda-codec.c:249:
+// int64_t corr = NANOSECONDS_PER_SECOND * overflow / (4 * st->as.freq);

ERROR: do not use C99 // comments
#1049: FILE: hw/audio/hda-codec.c:250:
+// //dolog("CORR %"PRId64"\n", corr);

ERROR: do not use C99 // comments
#1050: FILE: hw/audio/hda-codec.c:251:
+// atomic_fetch_add(&st->buft_start, corr);

ERROR: do not use C99 // comments
#1051: FILE: hw/audio/hda-codec.c:252:
+// }

ERROR: open brace '{' following function declarations go on the next line #1074: FILE: hw/audio/hda-codec.c:273:
+static void hda_audio_output_timer(void *opaque) {

WARNING: line over 80 characters
#1087: FILE: hw/audio/hda-codec.c:286:
+ int64_t wanted_wpos = (st->as.freq * 4 * (now - buft_start)) / NANOSECONDS_PER_SECOND;

ERROR: do not use C99 // comments
#1088: FILE: hw/audio/hda-codec.c:287:
+ wanted_wpos &= -4; // IMPORTANT! clip to frames

ERROR: do not use C99 // comments
#1091: FILE: hw/audio/hda-codec.c:290:
+ // we already received the data

ERROR: do not use C99 // comments
#1099: FILE: hw/audio/hda-codec.c:298:
+ //dolog("%"PRId64"\n", wpos - rpos);

ERROR: line over 90 characters
#1101: FILE: hw/audio/hda-codec.c:300:
+ //dolog("rpos: %"PRId64", wpos: %"PRId64", wanted: %"PRId64"\n", rpos, wpos, wanted_wpos);

ERROR: do not use C99 // comments
#1101: FILE: hw/audio/hda-codec.c:300:
+ //dolog("rpos: %"PRId64", wpos: %"PRId64", wanted: %"PRId64"\n", rpos, wpos, wanted_wpos);

ERROR: line over 90 characters
#1106: FILE: hw/audio/hda-codec.c:305:
+ int rc = hda_codec_xfer(&st->state->hda, st->stream, true, st->buf + start, chunk);

ERROR: do not use C99 // comments
#1159: FILE: hw/audio/hda-codec.c:339:
+ //dolog("CORR %"PRId64"\n", corr);

ERROR: space prohibited between function name and open parenthesis '(' #1192: FILE: hw/audio/hda-codec.c:376:
+ timer_del (st->buft);

ERROR: space prohibited between function name and open parenthesis '(' #1223: FILE: hw/audio/hda-codec.c:676:
+ timer_del (st->buft);

ERROR: do not use C99 // comments
#1265: FILE: hw/audio/intel-hda.c:410:
+// if (st->ctl & (1 << 26)) {

ERROR: do not use C99 // comments
#1266: FILE: hw/audio/intel-hda.c:411:
+// /*

ERROR: do not use C99 // comments
#1267: FILE: hw/audio/intel-hda.c:412:
+// * Wait with the next DMA xfer until the guest

ERROR: do not use C99 // comments
#1268: FILE: hw/audio/intel-hda.c:413:
+// * has acked the buffer completion interrupt

ERROR: do not use C99 // comments
#1269: FILE: hw/audio/intel-hda.c:414:
+// */

ERROR: do not use C99 // comments
#1270: FILE: hw/audio/intel-hda.c:415:
+// return false;

ERROR: do not use C99 // comments
#1271: FILE: hw/audio/intel-hda.c:416:
+// }

total: 75 errors, 10 warnings, 1200 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

=== OUTPUT END ===

Test command exited with code: 1

---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]