VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DevHdaStream.cpp@ 89863

Last change on this file since 89863 was 89863, checked in by vboxsync, 3 years ago

DevHda: Do LPIB updates more often. Experimental code for doing DMA work on LPIB read (disabled). [build fix] bugref:9890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 113.5 KB
Line 
1/* $Id: DevHdaStream.cpp 89863 2021-06-23 14:37:17Z vboxsync $ */
2/** @file
3 * Intel HD Audio Controller Emulation - Streams.
4 */
5
6/*
7 * Copyright (C) 2017-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DEV_HDA
23#include <VBox/log.h>
24
25#include <iprt/mem.h>
26#include <iprt/semaphore.h>
27
28#include <VBox/AssertGuest.h>
29#include <VBox/vmm/pdmdev.h>
30#include <VBox/vmm/pdmaudioifs.h>
31#include <VBox/vmm/pdmaudioinline.h>
32
33#include "AudioHlp.h"
34
35#include "DevHda.h"
36#include "DevHdaStream.h"
37
38#ifdef VBOX_WITH_DTRACE
39# include "dtrace/VBoxDD.h"
40#endif
41
42
43/*********************************************************************************************************************************
44* Internal Functions *
45*********************************************************************************************************************************/
46static void hdaStreamSetPositionAbs(PHDASTREAM pStreamShared, PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t uLPIB);
47#ifdef IN_RING3
48# ifdef VBOX_HDA_WITH_ON_REG_ACCESS_DMA
49static void hdaR3StreamFlushDmaBounceBufferOutput(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3);
50# endif
51static uint32_t hdaR3StreamHandleDmaBufferOverrun(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, PAUDMIXSINK pSink,
52 uint32_t cbNeeded, uint64_t nsNow,
53 const char *pszCaller, uint32_t const cbStreamFree);
54static void hdaR3StreamUpdateDma(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTATER3 pThisCC,
55 PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3);
56#endif
57
58
59#ifdef IN_RING3
60
61/**
62 * Creates an HDA stream.
63 *
64 * @returns VBox status code.
65 * @param pStreamShared The HDA stream to construct - shared bits.
66 * @param pStreamR3 The HDA stream to construct - ring-3 bits.
67 * @param pThis The shared HDA device instance.
68 * @param pThisCC The ring-3 HDA device instance.
69 * @param uSD Stream descriptor number to assign.
70 */
71int hdaR3StreamConstruct(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, PHDASTATE pThis, PHDASTATER3 pThisCC, uint8_t uSD)
72{
73 pStreamR3->u8SD = uSD;
74 pStreamShared->u8SD = uSD;
75 pStreamR3->pMixSink = NULL;
76 pStreamR3->pHDAStateShared = pThis;
77 pStreamR3->pHDAStateR3 = pThisCC;
78 Assert(pStreamShared->hTimer != NIL_TMTIMERHANDLE); /* hdaR3Construct initalized this one already. */
79
80 pStreamShared->State.fInReset = false;
81 pStreamShared->State.fRunning = false;
82# ifdef HDA_USE_DMA_ACCESS_HANDLER
83 RTListInit(&pStreamR3->State.lstDMAHandlers);
84# endif
85
86 AssertPtr(pStreamR3->pHDAStateR3);
87 AssertPtr(pStreamR3->pHDAStateR3->pDevIns);
88
89# ifdef DEBUG
90 int rc = RTCritSectInit(&pStreamR3->Dbg.CritSect);
91 AssertRCReturn(rc, rc);
92# endif
93
94 const bool fIsInput = hdaGetDirFromSD(uSD) == PDMAUDIODIR_IN;
95
96 if (fIsInput)
97 {
98 pStreamShared->State.Cfg.enmPath = PDMAUDIOPATH_UNKNOWN;
99 pStreamShared->State.Cfg.enmDir = PDMAUDIODIR_IN;
100 }
101 else
102 {
103 pStreamShared->State.Cfg.enmPath = PDMAUDIOPATH_UNKNOWN;
104 pStreamShared->State.Cfg.enmDir = PDMAUDIODIR_OUT;
105 }
106
107 pStreamR3->Dbg.Runtime.fEnabled = pThisCC->Dbg.fEnabled;
108
109 if (pStreamR3->Dbg.Runtime.fEnabled)
110 {
111 char szFile[64];
112 char szPath[RTPATH_MAX];
113
114 /* pFileStream */
115 if (fIsInput)
116 RTStrPrintf(szFile, sizeof(szFile), "hdaStreamWriteSD%RU8", uSD);
117 else
118 RTStrPrintf(szFile, sizeof(szFile), "hdaStreamReadSD%RU8", uSD);
119
120 int rc2 = AudioHlpFileNameGet(szPath, sizeof(szPath), pThisCC->Dbg.pszOutPath, szFile,
121 0 /* uInst */, AUDIOHLPFILETYPE_WAV, AUDIOHLPFILENAME_FLAGS_NONE);
122 AssertRC(rc2);
123
124 rc2 = AudioHlpFileCreate(AUDIOHLPFILETYPE_WAV, szPath, AUDIOHLPFILE_FLAGS_NONE, &pStreamR3->Dbg.Runtime.pFileStream);
125 AssertRC(rc2);
126
127 /* pFileDMARaw */
128 if (fIsInput)
129 RTStrPrintf(szFile, sizeof(szFile), "hdaDMARawWriteSD%RU8", uSD);
130 else
131 RTStrPrintf(szFile, sizeof(szFile), "hdaDMARawReadSD%RU8", uSD);
132
133 rc2 = AudioHlpFileNameGet(szPath, sizeof(szPath), pThisCC->Dbg.pszOutPath, szFile,
134 0 /* uInst */, AUDIOHLPFILETYPE_WAV, AUDIOHLPFILENAME_FLAGS_NONE);
135 AssertRC(rc2);
136
137 rc2 = AudioHlpFileCreate(AUDIOHLPFILETYPE_WAV, szPath, AUDIOHLPFILE_FLAGS_NONE, &pStreamR3->Dbg.Runtime.pFileDMARaw);
138 AssertRC(rc2);
139
140 /* pFileDMAMapped */
141 if (fIsInput)
142 RTStrPrintf(szFile, sizeof(szFile), "hdaDMAWriteMappedSD%RU8", uSD);
143 else
144 RTStrPrintf(szFile, sizeof(szFile), "hdaDMAReadMappedSD%RU8", uSD);
145
146 rc2 = AudioHlpFileNameGet(szPath, sizeof(szPath), pThisCC->Dbg.pszOutPath, szFile,
147 0 /* uInst */, AUDIOHLPFILETYPE_WAV, AUDIOHLPFILENAME_FLAGS_NONE);
148 AssertRC(rc2);
149
150 rc2 = AudioHlpFileCreate(AUDIOHLPFILETYPE_WAV, szPath, AUDIOHLPFILE_FLAGS_NONE, &pStreamR3->Dbg.Runtime.pFileDMAMapped);
151 AssertRC(rc2);
152
153 /* Delete stale debugging files from a former run. */
154 AudioHlpFileDelete(pStreamR3->Dbg.Runtime.pFileStream);
155 AudioHlpFileDelete(pStreamR3->Dbg.Runtime.pFileDMARaw);
156 AudioHlpFileDelete(pStreamR3->Dbg.Runtime.pFileDMAMapped);
157 }
158
159 return VINF_SUCCESS;
160}
161
162/**
163 * Destroys an HDA stream.
164 *
165 * @param pStreamR3 The HDA stream to destroy - ring-3 bits.
166 */
167void hdaR3StreamDestroy(PHDASTREAMR3 pStreamR3)
168{
169 LogFlowFunc(("[SD%RU8] Destroying ...\n", pStreamR3->u8SD));
170 int rc2;
171
172 if (pStreamR3->State.pAioRegSink)
173 {
174 rc2 = AudioMixerSinkRemoveUpdateJob(pStreamR3->State.pAioRegSink, hdaR3StreamUpdateAsyncIoJob, pStreamR3);
175 AssertRC(rc2);
176 pStreamR3->State.pAioRegSink = NULL;
177 }
178
179 if (pStreamR3->State.pCircBuf)
180 {
181 RTCircBufDestroy(pStreamR3->State.pCircBuf);
182 pStreamR3->State.pCircBuf = NULL;
183 pStreamR3->State.StatDmaBufSize = 0;
184 pStreamR3->State.StatDmaBufUsed = 0;
185 }
186
187# ifdef DEBUG
188 if (RTCritSectIsInitialized(&pStreamR3->Dbg.CritSect))
189 {
190 rc2 = RTCritSectDelete(&pStreamR3->Dbg.CritSect);
191 AssertRC(rc2);
192 }
193# endif
194
195 if (pStreamR3->Dbg.Runtime.fEnabled)
196 {
197 AudioHlpFileDestroy(pStreamR3->Dbg.Runtime.pFileStream);
198 pStreamR3->Dbg.Runtime.pFileStream = NULL;
199
200 AudioHlpFileDestroy(pStreamR3->Dbg.Runtime.pFileDMARaw);
201 pStreamR3->Dbg.Runtime.pFileDMARaw = NULL;
202
203 AudioHlpFileDestroy(pStreamR3->Dbg.Runtime.pFileDMAMapped);
204 pStreamR3->Dbg.Runtime.pFileDMAMapped = NULL;
205 }
206
207 LogFlowFuncLeave();
208}
209
210
211/**
212 * Appends a item to the scheduler.
213 *
214 * @returns VBox status code.
215 * @param pStreamShared The stream which scheduler should be modified.
216 * @param cbCur The period length in guest bytes.
217 * @param cbMaxPeriod The max period in guest bytes.
218 * @param idxLastBdle The last BDLE in the period.
219 * @param pHostProps The host PCM properties.
220 * @param pGuestProps The guest PCM properties.
221 * @param pcbBorrow Where to account for bytes borrowed across buffers
222 * to align scheduling items on frame boundraries.
223 */
224static int hdaR3StreamAddScheduleItem(PHDASTREAM pStreamShared, uint32_t cbCur, uint32_t cbMaxPeriod, uint32_t idxLastBdle,
225 PCPDMAUDIOPCMPROPS pHostProps, PCPDMAUDIOPCMPROPS pGuestProps, uint32_t *pcbBorrow)
226{
227 /* Check that we've got room (shouldn't ever be a problem). */
228 size_t idx = pStreamShared->State.cSchedule;
229 AssertLogRelReturn(idx + 1 < RT_ELEMENTS(pStreamShared->State.aSchedule), VERR_INTERNAL_ERROR_5);
230
231 /* Figure out the BDLE range for this period. */
232 uint32_t const idxFirstBdle = idx == 0 ? 0
233 : RT_MIN((uint32_t)( pStreamShared->State.aSchedule[idx - 1].idxFirst
234 + pStreamShared->State.aSchedule[idx - 1].cEntries),
235 idxLastBdle);
236
237 pStreamShared->State.aSchedule[idx].idxFirst = (uint8_t)idxFirstBdle;
238 pStreamShared->State.aSchedule[idx].cEntries = idxLastBdle >= idxFirstBdle
239 ? idxLastBdle - idxFirstBdle + 1
240 : pStreamShared->State.cBdles - idxFirstBdle + idxLastBdle + 1;
241
242 /* Deal with borrowing due to unaligned IOC buffers. */
243 uint32_t const cbBorrowed = *pcbBorrow;
244 if (cbBorrowed < cbCur)
245 cbCur -= cbBorrowed;
246 else
247 {
248 /* Note. We can probably gloss over this, but it's not a situation a sane guest would put us, so don't bother for now. */
249 ASSERT_GUEST_MSG_FAILED(("#%u: cbBorrow=%#x cbCur=%#x BDLE[%u..%u]\n",
250 pStreamShared->u8SD, cbBorrowed, cbCur, idxFirstBdle, idxLastBdle));
251 LogRelMax(32, ("HDA: Stream #%u has a scheduling error: cbBorrow=%#x cbCur=%#x BDLE[%u..%u]\n",
252 pStreamShared->u8SD, cbBorrowed, cbCur, idxFirstBdle, idxLastBdle));
253 return VERR_OUT_OF_RANGE;
254 }
255
256 uint32_t cbCurAligned = PDMAudioPropsRoundUpBytesToFrame(pGuestProps, cbCur);
257 *pcbBorrow = cbCurAligned - cbCur;
258
259 /* Do we need to split up the period? */
260 if (cbCurAligned <= cbMaxPeriod)
261 {
262 uint32_t cbHost = PDMAudioPropsFramesToBytes(pHostProps, PDMAudioPropsBytesToFrames(pGuestProps, cbCurAligned));
263 pStreamShared->State.aSchedule[idx].cbPeriod = cbHost;
264 pStreamShared->State.aSchedule[idx].cLoops = 1;
265 }
266 else
267 {
268 /* Reduce till we've below the threshold. */
269 uint32_t cbLoop = cbCurAligned;
270 do
271 cbLoop = cbCurAligned / 2;
272 while (cbLoop > cbMaxPeriod);
273 cbLoop = PDMAudioPropsRoundUpBytesToFrame(pGuestProps, cbLoop);
274
275 /* Complete the scheduling item. */
276 uint32_t cbHost = PDMAudioPropsFramesToBytes(pHostProps, PDMAudioPropsBytesToFrames(pGuestProps, cbLoop));
277 pStreamShared->State.aSchedule[idx].cbPeriod = cbHost;
278 pStreamShared->State.aSchedule[idx].cLoops = cbCurAligned / cbLoop;
279
280 /* If there is a remainder, add it as a separate entry (this is
281 why the schedule must be more than twice the size of the BDL).*/
282 cbCurAligned %= cbLoop;
283 if (cbCurAligned)
284 {
285 pStreamShared->State.aSchedule[idx + 1] = pStreamShared->State.aSchedule[idx];
286 idx++;
287 cbHost = PDMAudioPropsFramesToBytes(pHostProps, PDMAudioPropsBytesToFrames(pGuestProps, cbCurAligned));
288 pStreamShared->State.aSchedule[idx].cbPeriod = cbHost;
289 pStreamShared->State.aSchedule[idx].cLoops = 1;
290 }
291 }
292
293 /* Done. */
294 pStreamShared->State.cSchedule = (uint16_t)(idx + 1);
295
296 return VINF_SUCCESS;
297}
298
299/**
300 * Creates the DMA timer schedule for the stream
301 *
302 * This is called from the stream setup code.
303 *
304 * @returns VBox status code.
305 * @param pStreamShared The stream to create a schedule for. The BDL
306 * must be loaded.
307 * @param cSegments Number of BDL segments.
308 * @param cBufferIrqs Number of the BDLEs with IOC=1.
309 * @param cbTotal The total BDL length in guest bytes.
310 * @param cbMaxPeriod Max period in guest bytes. This is in case the
311 * guest want to play the whole "Der Ring des
312 * Nibelungen" cycle in one go.
313 * @param cTimerTicksPerSec The DMA timer frequency.
314 * @param pHostProps The host PCM properties.
315 * @param pGuestProps The guest PCM properties.
316 */
317static int hdaR3StreamCreateSchedule(PHDASTREAM pStreamShared, uint32_t cSegments, uint32_t cBufferIrqs, uint32_t cbTotal,
318 uint32_t cbMaxPeriod, uint64_t cTimerTicksPerSec,
319 PCPDMAUDIOPCMPROPS pHostProps, PCPDMAUDIOPCMPROPS pGuestProps)
320{
321 int rc;
322
323 /*
324 * Reset scheduling state.
325 */
326 RT_ZERO(pStreamShared->State.aSchedule);
327 pStreamShared->State.cSchedule = 0;
328 pStreamShared->State.cSchedulePrologue = 0;
329 pStreamShared->State.idxSchedule = 0;
330 pStreamShared->State.idxScheduleLoop = 0;
331
332 /*
333 * Do the basic schedule compilation.
334 */
335 uint32_t cPotentialPrologue = 0;
336 uint32_t cbBorrow = 0;
337 uint32_t cbCur = 0;
338 uint32_t cbMin = UINT32_MAX;
339 pStreamShared->State.aSchedule[0].idxFirst = 0;
340 for (uint32_t i = 0; i < cSegments; i++)
341 {
342 cbCur += pStreamShared->State.aBdl[i].cb;
343 if (pStreamShared->State.aBdl[i].cb < cbMin)
344 cbMin = pStreamShared->State.aBdl[i].cb;
345 if (pStreamShared->State.aBdl[i].fFlags & HDA_BDLE_F_IOC)
346 {
347 rc = hdaR3StreamAddScheduleItem(pStreamShared, cbCur, cbMaxPeriod, i, pHostProps, pGuestProps, &cbBorrow);
348 ASSERT_GUEST_RC_RETURN(rc, rc);
349
350 if (cPotentialPrologue == 0)
351 cPotentialPrologue = pStreamShared->State.cSchedule;
352 cbCur = 0;
353 }
354 }
355 AssertLogRelMsgReturn(cbBorrow == 0, ("HDA: Internal scheduling error on stream #%u: cbBorrow=%#x cbTotal=%#x cbCur=%#x\n",
356 pStreamShared->u8SD, cbBorrow, cbTotal, cbCur),
357 VERR_INTERNAL_ERROR_3);
358
359 /*
360 * Deal with any loose ends.
361 */
362 if (cbCur && cBufferIrqs == 0)
363 {
364 /*
365 * No IOC. Vista ends up here, typically with three buffers configured.
366 *
367 * The perferred option here is to aim at processing one average BDLE with
368 * each DMA timer period, since that best matches how we update LPIB at
369 * present.
370 *
371 * The second alternative is to divide the whole span up into 3-4 periods
372 * to try increase our chances of keeping ahead of the guest. We may need
373 * to pick this if there are too few buffer descriptor or they are too small.
374 *
375 * However, what we probably should be doing is to do real DMA work whenever
376 * the guest reads a DMA related register (like LPIB) and just do 3-4 DMA
377 * timer periods, however we'll be postponing the DMA timer every time we
378 * return to ring-3 and signal the AIO, so in the end we'd probably not use
379 * the timer callback at all. (This is assuming a small shared per-stream
380 * buffer for keeping the DMA data in and that it's size will force a return
381 * to ring-3 often enough to keep the AIO thread going at a reasonable rate.)
382 */
383 Assert(cbCur == cbTotal);
384
385 /* Match the BDLEs 1:1 if there are 3 or more and that the smallest one
386 is at least 5ms big. */
387 if (cSegments >= 3 && PDMAudioPropsBytesToMilli(pGuestProps, cbMin) >= 5 /*ms*/)
388 {
389 for (uint32_t i = 0; i < cSegments; i++)
390 {
391 rc = hdaR3StreamAddScheduleItem(pStreamShared, pStreamShared->State.aBdl[i].cb, cbMaxPeriod,
392 i, pHostProps, pGuestProps, &cbBorrow);
393 ASSERT_GUEST_RC_RETURN(rc, rc);
394 }
395 }
396 /* Otherwise, just divide the work into 3 or 4 portions and hope for the best.
397 It seems, though, that this only really work for windows vista if we avoid
398 working accross buffer lines. */
399 /** @todo This can be simplified/relaxed/uncluttered if we do DMA work when LPIB
400 * is read, assuming ofc that LPIB is read before each buffer update. */
401 else
402 {
403 uint32_t const cPeriods = cSegments != 3 && PDMAudioPropsBytesToMilli(pGuestProps, cbCur) >= 4 * 5 /*ms*/
404 ? 4 : cSegments != 2 ? 3 : 2;
405 uint32_t const cbPeriod = PDMAudioPropsFloorBytesToFrame(pGuestProps, cbCur / cPeriods);
406 uint32_t iBdle = 0;
407 uint32_t offBdle = 0;
408 for (uint32_t iPeriod = 0; iPeriod < cPeriods; iPeriod++)
409 {
410 if (iPeriod + 1 < cPeriods)
411 {
412 offBdle += cbPeriod;
413 while (iBdle < cSegments && offBdle >= pStreamShared->State.aBdl[iBdle].cb)
414 offBdle -= pStreamShared->State.aBdl[iBdle++].cb;
415 rc = hdaR3StreamAddScheduleItem(pStreamShared, cbPeriod, cbMaxPeriod, offBdle != 0 ? iBdle : iBdle - 1,
416 pHostProps, pGuestProps, &cbBorrow);
417 }
418 else
419 rc = hdaR3StreamAddScheduleItem(pStreamShared, cbCur - iPeriod * cbPeriod, cbMaxPeriod, cSegments - 1,
420 pHostProps, pGuestProps, &cbBorrow);
421 ASSERT_GUEST_RC_RETURN(rc, rc);
422 }
423
424 }
425 Assert(cbBorrow == 0);
426 }
427 else if (cbCur)
428 {
429 /* The last BDLE didn't have IOC set, so we must continue processing
430 from the start till we hit one that has. */
431 uint32_t i;
432 for (i = 0; i < cSegments; i++)
433 {
434 cbCur += pStreamShared->State.aBdl[i].cb;
435 if (pStreamShared->State.aBdl[i].fFlags & HDA_BDLE_F_IOC)
436 break;
437 }
438 rc = hdaR3StreamAddScheduleItem(pStreamShared, cbCur, cbMaxPeriod, i, pHostProps, pGuestProps, &cbBorrow);
439 ASSERT_GUEST_RC_RETURN(rc, rc);
440
441 /* The initial scheduling items covering the wrap around area are
442 considered a prologue and must not repeated later. */
443 Assert(cPotentialPrologue);
444 pStreamShared->State.cSchedulePrologue = (uint8_t)cPotentialPrologue;
445 }
446
447 /*
448 * If there is just one BDLE with IOC set, we have to make sure
449 * we've got at least two periods scheduled, otherwise there is
450 * a very good chance the guest will overwrite the start of the
451 * buffer before we ever get around to reading it.
452 */
453 if (cBufferIrqs == 1)
454 {
455 uint32_t i = pStreamShared->State.cSchedulePrologue;
456 Assert(i < pStreamShared->State.cSchedule);
457 if ( i + 1 == pStreamShared->State.cSchedule
458 && pStreamShared->State.aSchedule[i].cLoops == 1)
459 {
460 uint32_t const cbFirstHalf = PDMAudioPropsFloorBytesToFrame(pHostProps, pStreamShared->State.aSchedule[i].cbPeriod / 2);
461 uint32_t const cbOtherHalf = pStreamShared->State.aSchedule[i].cbPeriod - cbFirstHalf;
462 pStreamShared->State.aSchedule[i].cbPeriod = cbFirstHalf;
463 if (cbFirstHalf == cbOtherHalf)
464 pStreamShared->State.aSchedule[i].cLoops = 2;
465 else
466 {
467 pStreamShared->State.aSchedule[i + 1] = pStreamShared->State.aSchedule[i];
468 pStreamShared->State.aSchedule[i].cbPeriod = cbOtherHalf;
469 pStreamShared->State.cSchedule++;
470 }
471 }
472 }
473
474 /*
475 * Go over the schduling entries and calculate the timer ticks for each period.
476 */
477 LogRel2(("HDA: Stream #%u schedule: %u items, %u prologue\n",
478 pStreamShared->u8SD, pStreamShared->State.cSchedule, pStreamShared->State.cSchedulePrologue));
479 uint64_t const cbHostPerSec = PDMAudioPropsFramesToBytes(pHostProps, pHostProps->uHz);
480 for (uint32_t i = 0; i < pStreamShared->State.cSchedule; i++)
481 {
482 uint64_t const cTicks = ASMMultU64ByU32DivByU32(cTimerTicksPerSec, pStreamShared->State.aSchedule[i].cbPeriod,
483 cbHostPerSec);
484 AssertLogRelMsgReturn((uint32_t)cTicks == cTicks, ("cTicks=%RU64 (%#RX64)\n", cTicks, cTicks), VERR_INTERNAL_ERROR_4);
485 pStreamShared->State.aSchedule[i].cPeriodTicks = RT_MAX((uint32_t)cTicks, 16);
486 LogRel2(("HDA: #%u: %u ticks / %u bytes, %u loops, BDLE%u L %u\n", i, pStreamShared->State.aSchedule[i].cPeriodTicks,
487 pStreamShared->State.aSchedule[i].cbPeriod, pStreamShared->State.aSchedule[i].cLoops,
488 pStreamShared->State.aSchedule[i].idxFirst, pStreamShared->State.aSchedule[i].cEntries));
489 }
490
491 return VINF_SUCCESS;
492}
493
494
495/**
496 * Sets up ((re-)iniitalizes) an HDA stream.
497 *
498 * @returns VBox status code. VINF_NO_CHANGE if the stream does not need
499 * be set-up again because the stream's (hardware) parameters did
500 * not change.
501 * @param pDevIns The device instance.
502 * @param pThis The shared HDA device state (for HW register
503 * parameters).
504 * @param pStreamShared HDA stream to set up, shared portion.
505 * @param pStreamR3 HDA stream to set up, ring-3 portion.
506 * @param uSD Stream descriptor number to assign it.
507 */
508int hdaR3StreamSetUp(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, uint8_t uSD)
509{
510 /* This must be valid all times. */
511 AssertReturn(uSD < HDA_MAX_STREAMS, VERR_INVALID_PARAMETER);
512
513 /* These member can only change on data corruption, despite what the code does further down (bird). */
514 AssertReturn(pStreamShared->u8SD == uSD, VERR_WRONG_ORDER);
515 AssertReturn(pStreamR3->u8SD == uSD, VERR_WRONG_ORDER);
516
517 const uint64_t u64BDLBase = RT_MAKE_U64(HDA_STREAM_REG(pThis, BDPL, uSD),
518 HDA_STREAM_REG(pThis, BDPU, uSD));
519 const uint16_t u16LVI = HDA_STREAM_REG(pThis, LVI, uSD);
520 const uint32_t u32CBL = HDA_STREAM_REG(pThis, CBL, uSD);
521 const uint8_t u8FIFOS = HDA_STREAM_REG(pThis, FIFOS, uSD) + 1;
522 uint8_t u8FIFOW = hdaSDFIFOWToBytes(HDA_STREAM_REG(pThis, FIFOW, uSD));
523 const uint16_t u16FMT = HDA_STREAM_REG(pThis, FMT, uSD);
524
525 /* Is the bare minimum set of registers configured for the stream?
526 * If not, bail out early, as there's nothing to do here for us (yet). */
527 if ( !u64BDLBase
528 || !u16LVI
529 || !u32CBL
530 || !u8FIFOS
531 || !u8FIFOW
532 || !u16FMT)
533 {
534 LogFunc(("[SD%RU8] Registers not set up yet, skipping (re-)initialization\n", uSD));
535 return VINF_SUCCESS;
536 }
537
538 /*
539 * Convert the config to PDM PCM properties and configure the stream.
540 */
541 PPDMAUDIOSTREAMCFG pCfg = &pStreamShared->State.Cfg;
542 int rc = hdaR3SDFMTToPCMProps(u16FMT, &pCfg->Props);
543 if (RT_SUCCESS(rc))
544 pCfg->enmDir = hdaGetDirFromSD(uSD);
545 else
546 {
547 LogRelMax(32, ("HDA: Warning: Format 0x%x for stream #%RU8 not supported\n", HDA_STREAM_REG(pThis, FMT, uSD), uSD));
548 return rc;
549 }
550
551 ASSERT_GUEST_LOGREL_MSG_RETURN( PDMAudioPropsFrameSize(&pCfg->Props) > 0
552 && u32CBL % PDMAudioPropsFrameSize(&pCfg->Props) == 0,
553 ("CBL for stream #%RU8 does not align to frame size (u32CBL=%u cbFrameSize=%u)\n",
554 uSD, u32CBL, PDMAudioPropsFrameSize(&pCfg->Props)),
555 VERR_INVALID_PARAMETER);
556
557 /* Make sure the guest behaves regarding the stream's FIFO. */
558 ASSERT_GUEST_LOGREL_MSG_STMT(u8FIFOW <= u8FIFOS,
559 ("Guest tried setting a bigger FIFOW (%RU8) than FIFOS (%RU8), limiting\n", u8FIFOW, u8FIFOS),
560 u8FIFOW = u8FIFOS /* ASSUMES that u8FIFOS has been validated. */);
561
562 pStreamShared->u8SD = uSD;
563
564 /* Update all register copies so that we later know that something has changed. */
565 pStreamShared->u64BDLBase = u64BDLBase;
566 pStreamShared->u16LVI = u16LVI;
567 pStreamShared->u32CBL = u32CBL;
568 pStreamShared->u8FIFOS = u8FIFOS;
569 pStreamShared->u8FIFOW = u8FIFOW;
570 pStreamShared->u16FMT = u16FMT;
571
572 /* The the stream's name, based on the direction. */
573 switch (pCfg->enmDir)
574 {
575 case PDMAUDIODIR_IN:
576# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
577# error "Implement me!"
578# else
579 pCfg->enmPath = PDMAUDIOPATH_IN_LINE;
580 RTStrCopy(pCfg->szName, sizeof(pCfg->szName), "Line In");
581# endif
582 break;
583
584 case PDMAUDIODIR_OUT:
585 /* Destination(s) will be set in hdaR3AddStreamOut(),
586 * based on the channels / stream layout. */
587 break;
588
589 default:
590 AssertFailedReturn(VERR_NOT_SUPPORTED);
591 break;
592 }
593
594 LogRel2(("HDA: Stream #%RU8 DMA @ 0x%x (%RU32 bytes = %RU64ms total)\n", uSD, pStreamShared->u64BDLBase,
595 pStreamShared->u32CBL, PDMAudioPropsBytesToMilli(&pCfg->Props, pStreamShared->u32CBL)));
596
597 /*
598 * Load the buffer descriptor list.
599 *
600 * Section 3.6.2 states that "the BDL should not be modified unless the RUN
601 * bit is 0", so it should be within the specs to read it once here and not
602 * re-read any BDLEs later.
603 */
604 /* Reset BDL state. */
605 RT_ZERO(pStreamShared->State.aBdl);
606 pStreamShared->State.offCurBdle = 0;
607 pStreamShared->State.idxCurBdle = 0;
608
609 uint32_t /*const*/ cTransferFragments = (pStreamShared->u16LVI & 0xff) + 1;
610 if (cTransferFragments <= 1)
611 LogRel(("HDA: Warning: Stream #%RU8 transfer buffer count invalid: (%RU16)! Buggy guest audio driver!\n", uSD, pStreamShared->u16LVI));
612 AssertLogRelReturn(cTransferFragments <= RT_ELEMENTS(pStreamShared->State.aBdl), VERR_INTERNAL_ERROR_5);
613 pStreamShared->State.cBdles = cTransferFragments;
614
615 /* Load them. */
616 rc = PDMDevHlpPCIPhysRead(pDevIns, u64BDLBase, pStreamShared->State.aBdl,
617 sizeof(pStreamShared->State.aBdl[0]) * cTransferFragments);
618 AssertRC(rc);
619
620 /* Check what we just loaded. Refuse overly large buffer lists. */
621 uint64_t cbTotal = 0;
622 uint32_t cBufferIrqs = 0;
623 for (uint32_t i = 0; i < cTransferFragments; i++)
624 {
625 if (pStreamShared->State.aBdl[i].fFlags & HDA_BDLE_F_IOC)
626 cBufferIrqs++;
627 cbTotal += pStreamShared->State.aBdl[i].cb;
628 }
629 ASSERT_GUEST_STMT_RETURN(cbTotal < _2G,
630 LogRelMax(32, ("HDA: Error: Stream #%u is configured with an insane amount of buffer space - refusing do work with it: %RU64 (%#RX64) bytes.\n",
631 uSD, cbTotal, cbTotal)),
632 VERR_NOT_SUPPORTED);
633 ASSERT_GUEST_STMT_RETURN(cbTotal == u32CBL,
634 LogRelMax(32, ("HDA: Warning: Stream #%u has a mismatch between CBL and configured buffer space: %RU32 (%#RX32) vs %RU64 (%#RX64)\n",
635 uSD, u32CBL, u32CBL, cbTotal, cbTotal)),
636 VERR_NOT_SUPPORTED);
637
638 /*
639 * Create a DMA timer schedule.
640 */
641 /** @todo clean up this, pGuestProps and pHostProps are the same now. */
642 rc = hdaR3StreamCreateSchedule(pStreamShared, cTransferFragments, cBufferIrqs, (uint32_t)cbTotal,
643 PDMAudioPropsMilliToBytes(&pCfg->Props, 100 /** @todo make configurable */),
644 PDMDevHlpTimerGetFreq(pDevIns, pStreamShared->hTimer),
645 &pCfg->Props, &pCfg->Props);
646 if (RT_FAILURE(rc))
647 return rc;
648
649 pStreamShared->State.cbTransferSize = pStreamShared->State.aSchedule[0].cbPeriod;
650
651 /*
652 * Calculate the transfer Hz for use in the circular buffer calculation.
653 */
654 uint32_t cbMaxPeriod = 0;
655 uint32_t cbMinPeriod = UINT32_MAX;
656 uint32_t cPeriods = 0;
657 for (uint32_t i = 0; i < pStreamShared->State.cSchedule; i++)
658 {
659 uint32_t cbPeriod = pStreamShared->State.aSchedule[i].cbPeriod;
660 cbMaxPeriod = RT_MAX(cbMaxPeriod, cbPeriod);
661 cbMinPeriod = RT_MIN(cbMinPeriod, cbPeriod);
662 cPeriods += pStreamShared->State.aSchedule[i].cLoops;
663 }
664 uint64_t const cbTransferPerSec = RT_MAX(PDMAudioPropsFramesToBytes(&pCfg->Props, pCfg->Props.uHz),
665 4096 /* zero div prevention: min is 6kHz, picked 4k in case I'm mistaken */);
666 unsigned uTransferHz = cbTransferPerSec * 1000 / cbMaxPeriod;
667 LogRel2(("HDA: Stream #%RU8 needs a %u.%03u Hz timer rate (period: %u..%u host bytes)\n",
668 uSD, uTransferHz / 1000, uTransferHz % 1000, cbMinPeriod, cbMaxPeriod));
669 uTransferHz /= 1000;
670
671 if (uTransferHz > 400) /* Anything above 400 Hz looks fishy -- tell the user. */
672 LogRelMax(32, ("HDA: Warning: Calculated transfer Hz rate for stream #%RU8 looks incorrect (%u), please re-run with audio debug mode and report a bug\n",
673 uSD, uTransferHz));
674
675 pStreamShared->State.cbAvgTransfer = (uint32_t)(cbTotal + cPeriods - 1) / cPeriods;
676
677 /* For input streams we must determin a pre-buffering requirement.
678 We use the initial delay as a basis here, though we must have at
679 least two max periods worth of data queued up due to the way we
680 work the AIO thread. */
681 pStreamShared->State.fInputPreBuffered = false;
682 pStreamShared->State.cbInputPreBuffer = PDMAudioPropsMilliToBytes(&pCfg->Props, pThis->msInitialDelay);
683 pStreamShared->State.cbInputPreBuffer = RT_MIN(cbMaxPeriod * 2, pStreamShared->State.cbInputPreBuffer);
684
685 /*
686 * Set up data transfer stuff.
687 */
688
689 /* Assign the global device rate to the stream I/O timer as default. */
690 pStreamShared->State.uTimerIoHz = pThis->uTimerHz;
691 ASSERT_GUEST_LOGREL_MSG_STMT(pStreamShared->State.uTimerIoHz,
692 ("I/O timer Hz rate for stream #%RU8 is invalid\n", uSD),
693 pStreamShared->State.uTimerIoHz = HDA_TIMER_HZ_DEFAULT);
694
695 /* Set I/O scheduling hint for the backends. */
696 /** @todo r=bird: derive this from the schedule instead of using the
697 * uTimerIoHz, as that's almost pure non-sense now. */
698 pCfg->Device.cMsSchedulingHint = RT_MS_1SEC / pStreamShared->State.uTimerIoHz;
699 LogRel2(("HDA: Stream #%RU8 set scheduling hint for the backends to %RU32ms\n", uSD, pCfg->Device.cMsSchedulingHint));
700
701
702 /* Make sure to also update the stream's DMA counter (based on its current LPIB value). */
703 /** @todo r=bird: We use LPIB as-is here, so if it's not zero we have to
704 * locate the right place in the schedule and whatnot... */
705 if (HDA_STREAM_REG(pThis, LPIB, uSD) != 0)
706 LogRel2(("HDA: Warning! Stream #%RU8 is set up with LPIB=%#RX32 instead of zero!\n", uSD, HDA_STREAM_REG(pThis, LPIB, uSD)));
707 hdaStreamSetPositionAbs(pStreamShared, pDevIns, pThis, HDA_STREAM_REG(pThis, LPIB, uSD));
708
709# ifdef LOG_ENABLED
710 hdaR3BDLEDumpAll(pDevIns, pThis, pStreamShared->u64BDLBase, pStreamShared->u16LVI + 1);
711# endif
712
713 /*
714 * Set up internal ring buffer.
715 */
716
717 /* (Re-)Allocate the stream's internal DMA buffer,
718 * based on the timing *and* PCM properties we just got above. */
719 if (pStreamR3->State.pCircBuf)
720 {
721 RTCircBufDestroy(pStreamR3->State.pCircBuf);
722 pStreamR3->State.pCircBuf = NULL;
723 pStreamR3->State.StatDmaBufSize = 0;
724 pStreamR3->State.StatDmaBufUsed = 0;
725 }
726 pStreamShared->State.offWrite = 0;
727 pStreamShared->State.offRead = 0;
728
729 /*
730 * The default internal ring buffer size must be:
731 *
732 * - Large enough for at least three periodic DMA transfers.
733 *
734 * It is critically important that we don't experience underruns
735 * in the DMA OUT code, because it will cause the buffer processing
736 * to get skewed and possibly overlap with what the guest is updating.
737 * At the time of writing (2021-03-05) there is no code for getting
738 * back into sync there.
739 *
740 * - Large enough for at least three I/O scheduling hints.
741 *
742 * We want to lag behind a DMA period or two, but there must be
743 * sufficent space for the AIO thread to get schedule and shuffle
744 * data thru the mixer and onto the host audio hardware.
745 *
746 * - Both above with plenty to spare.
747 *
748 * So, just take the longest of the two periods and multipling it by 6.
749 * We aren't not talking about very large base buffers heres, so size isn't
750 * an issue.
751 *
752 * Note: Use pCfg->Props as PCM properties here, as we only want to store the
753 * samples we actually need, in other words, skipping the interleaved
754 * channels we don't support / need to save space.
755 */
756 uint32_t msCircBuf = RT_MS_1SEC * 6 / RT_MIN(uTransferHz, pStreamShared->State.uTimerIoHz);
757 msCircBuf = RT_MAX(msCircBuf, pThis->msInitialDelay + RT_MS_1SEC * 6 / uTransferHz);
758
759 uint32_t cbCircBuf = PDMAudioPropsMilliToBytes(&pCfg->Props, msCircBuf);
760 LogRel2(("HDA: Stream #%RU8 default ring buffer size is %RU32 bytes / %RU64 ms\n",
761 uSD, cbCircBuf, PDMAudioPropsBytesToMilli(&pCfg->Props, cbCircBuf)));
762
763 uint32_t msCircBufCfg = hdaGetDirFromSD(uSD) == PDMAUDIODIR_IN ? pThis->cMsCircBufIn : pThis->cMsCircBufOut;
764 if (msCircBufCfg) /* Anything set via CFGM? */
765 {
766 cbCircBuf = PDMAudioPropsMilliToBytes(&pCfg->Props, msCircBufCfg);
767 LogRel2(("HDA: Stream #%RU8 is using a custom ring buffer size of %RU32 bytes / %RU64 ms\n",
768 uSD, cbCircBuf, PDMAudioPropsBytesToMilli(&pCfg->Props, cbCircBuf)));
769 }
770
771 /* Serious paranoia: */
772 ASSERT_GUEST_LOGREL_MSG_STMT(cbCircBuf % PDMAudioPropsFrameSize(&pCfg->Props) == 0,
773 ("Ring buffer size (%RU32) for stream #%RU8 not aligned to the (host) frame size (%RU8)\n",
774 cbCircBuf, uSD, PDMAudioPropsFrameSize(&pCfg->Props)),
775 rc = VERR_INVALID_PARAMETER);
776 ASSERT_GUEST_LOGREL_MSG_STMT(cbCircBuf, ("Ring buffer size for stream #%RU8 is invalid\n", uSD),
777 rc = VERR_INVALID_PARAMETER);
778 if (RT_SUCCESS(rc))
779 {
780 rc = RTCircBufCreate(&pStreamR3->State.pCircBuf, cbCircBuf);
781 if (RT_SUCCESS(rc))
782 {
783 pStreamR3->State.StatDmaBufSize = cbCircBuf;
784
785 /*
786 * Forward the timer frequency hint to TM as well for better accuracy on
787 * systems w/o preemption timers (also good for 'info timers').
788 */
789 PDMDevHlpTimerSetFrequencyHint(pDevIns, pStreamShared->hTimer, uTransferHz);
790 }
791 }
792
793 if (RT_FAILURE(rc))
794 LogRelMax(32, ("HDA: Initializing stream #%RU8 failed with %Rrc\n", uSD, rc));
795
796# ifdef VBOX_WITH_DTRACE
797 VBOXDD_HDA_STREAM_SETUP((uint32_t)uSD, rc, pStreamShared->State.Cfg.Props.uHz,
798 pStreamShared->State.aSchedule[pStreamShared->State.cSchedule - 1].cPeriodTicks,
799 pStreamShared->State.aSchedule[pStreamShared->State.cSchedule - 1].cbPeriod);
800# endif
801 return rc;
802}
803
804/**
805 * Resets an HDA stream.
806 *
807 * @param pThis The shared HDA device state.
808 * @param pThisCC The ring-3 HDA device state.
809 * @param pStreamShared HDA stream to reset (shared).
810 * @param pStreamR3 HDA stream to reset (ring-3).
811 * @param uSD Stream descriptor (SD) number to use for this stream.
812 */
813void hdaR3StreamReset(PHDASTATE pThis, PHDASTATER3 pThisCC, PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, uint8_t uSD)
814{
815 LogFunc(("[SD%RU8] Reset\n", uSD));
816
817 /*
818 * Assert some sanity.
819 */
820 AssertPtr(pThis);
821 AssertPtr(pStreamShared);
822 AssertPtr(pStreamR3);
823 Assert(uSD < HDA_MAX_STREAMS);
824 Assert(pStreamShared->u8SD == uSD);
825 Assert(pStreamR3->u8SD == uSD);
826 AssertMsg(!pStreamShared->State.fRunning, ("[SD%RU8] Cannot reset stream while in running state\n", uSD));
827
828 /*
829 * Set reset state.
830 */
831 Assert(ASMAtomicReadBool(&pStreamShared->State.fInReset) == false); /* No nested calls. */
832 ASMAtomicXchgBool(&pStreamShared->State.fInReset, true);
833
834 /*
835 * Second, initialize the registers.
836 */
837 /* See 6.2.33: Clear on reset. */
838 HDA_STREAM_REG(pThis, STS, uSD) = 0;
839 /* According to the ICH6 datasheet, 0x40000 is the default value for stream descriptor register 23:20
840 * bits are reserved for stream number 18.2.33, resets SDnCTL except SRST bit. */
841 HDA_STREAM_REG(pThis, CTL, uSD) = HDA_SDCTL_TP | (HDA_STREAM_REG(pThis, CTL, uSD) & HDA_SDCTL_SRST);
842 /* ICH6 defines default values (120 bytes for input and 192 bytes for output descriptors) of FIFO size. 18.2.39. */
843 HDA_STREAM_REG(pThis, FIFOS, uSD) = hdaGetDirFromSD(uSD) == PDMAUDIODIR_IN ? HDA_SDIFIFO_120B : HDA_SDOFIFO_192B;
844 /* See 18.2.38: Always defaults to 0x4 (32 bytes). */
845 HDA_STREAM_REG(pThis, FIFOW, uSD) = HDA_SDFIFOW_32B;
846 HDA_STREAM_REG(pThis, LPIB, uSD) = 0;
847 HDA_STREAM_REG(pThis, CBL, uSD) = 0;
848 HDA_STREAM_REG(pThis, LVI, uSD) = 0;
849 HDA_STREAM_REG(pThis, FMT, uSD) = 0;
850 HDA_STREAM_REG(pThis, BDPU, uSD) = 0;
851 HDA_STREAM_REG(pThis, BDPL, uSD) = 0;
852
853# ifdef HDA_USE_DMA_ACCESS_HANDLER
854 hdaR3StreamUnregisterDMAHandlers(pThis, pStream);
855# endif
856
857 /* Assign the default mixer sink to the stream. */
858 pStreamR3->pMixSink = hdaR3GetDefaultSink(pThisCC, uSD);
859 if (pStreamR3->State.pAioRegSink)
860 {
861 int rc2 = AudioMixerSinkRemoveUpdateJob(pStreamR3->State.pAioRegSink, hdaR3StreamUpdateAsyncIoJob, pStreamR3);
862 AssertRC(rc2);
863 pStreamR3->State.pAioRegSink = NULL;
864 }
865
866 /* Reset transfer stuff. */
867 pStreamShared->State.cTransferPendingInterrupts = 0;
868 pStreamShared->State.tsTransferLast = 0;
869 pStreamShared->State.tsTransferNext = 0;
870
871 /* Initialize timestamps. */
872 pStreamShared->State.tsLastTransferNs = 0;
873 pStreamShared->State.tsLastReadNs = 0;
874 pStreamShared->State.tsAioDelayEnd = UINT64_MAX;
875 pStreamShared->State.tsStart = 0;
876
877 RT_ZERO(pStreamShared->State.aBdl);
878 RT_ZERO(pStreamShared->State.aSchedule);
879 pStreamShared->State.offCurBdle = 0;
880 pStreamShared->State.cBdles = 0;
881 pStreamShared->State.idxCurBdle = 0;
882 pStreamShared->State.cSchedulePrologue = 0;
883 pStreamShared->State.cSchedule = 0;
884 pStreamShared->State.idxSchedule = 0;
885 pStreamShared->State.idxScheduleLoop = 0;
886 pStreamShared->State.fInputPreBuffered = false;
887
888 if (pStreamR3->State.pCircBuf)
889 RTCircBufReset(pStreamR3->State.pCircBuf);
890 pStreamShared->State.offWrite = 0;
891 pStreamShared->State.offRead = 0;
892
893# ifdef DEBUG
894 pStreamR3->Dbg.cReadsTotal = 0;
895 pStreamR3->Dbg.cbReadTotal = 0;
896 pStreamR3->Dbg.tsLastReadNs = 0;
897 pStreamR3->Dbg.cWritesTotal = 0;
898 pStreamR3->Dbg.cbWrittenTotal = 0;
899 pStreamR3->Dbg.cWritesHz = 0;
900 pStreamR3->Dbg.cbWrittenHz = 0;
901 pStreamR3->Dbg.tsWriteSlotBegin = 0;
902# endif
903
904 /* Report that we're done resetting this stream. */
905 HDA_STREAM_REG(pThis, CTL, uSD) = 0;
906
907# ifdef VBOX_WITH_DTRACE
908 VBOXDD_HDA_STREAM_RESET((uint32_t)uSD);
909# endif
910 LogFunc(("[SD%RU8] Reset\n", uSD));
911
912 /* Exit reset mode. */
913 ASMAtomicXchgBool(&pStreamShared->State.fInReset, false);
914}
915
916/**
917 * Enables or disables an HDA audio stream.
918 *
919 * @returns VBox status code.
920 * @param pThis The shared HDA device state.
921 * @param pStreamShared HDA stream to enable or disable - shared bits.
922 * @param pStreamR3 HDA stream to enable or disable - ring-3 bits.
923 * @param fEnable Whether to enable or disble the stream.
924 */
925int hdaR3StreamEnable(PHDASTATE pThis, PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, bool fEnable)
926{
927 AssertPtr(pStreamR3);
928 AssertPtr(pStreamShared);
929
930 LogFunc(("[SD%RU8] fEnable=%RTbool, pMixSink=%p\n", pStreamShared->u8SD, fEnable, pStreamR3->pMixSink));
931
932 /* First, enable or disable the stream and the stream's sink, if any. */
933 int rc = VINF_SUCCESS;
934 PAUDMIXSINK const pSink = pStreamR3->pMixSink ? pStreamR3->pMixSink->pMixSink : NULL;
935 if (pSink)
936 {
937 if (fEnable)
938 {
939 if (pStreamR3->State.pAioRegSink != pSink)
940 {
941 if (pStreamR3->State.pAioRegSink)
942 {
943 rc = AudioMixerSinkRemoveUpdateJob(pStreamR3->State.pAioRegSink, hdaR3StreamUpdateAsyncIoJob, pStreamR3);
944 AssertRC(rc);
945 }
946 rc = AudioMixerSinkAddUpdateJob(pSink, hdaR3StreamUpdateAsyncIoJob, pStreamR3,
947 pStreamShared->State.Cfg.Device.cMsSchedulingHint);
948 AssertLogRelRC(rc);
949 pStreamR3->State.pAioRegSink = RT_SUCCESS(rc) ? pSink : NULL;
950 }
951 rc = AudioMixerSinkStart(pSink);
952 }
953 else
954 rc = AudioMixerSinkDrainAndStop(pSink,
955 pStreamR3->State.pCircBuf ? (uint32_t)RTCircBufUsed(pStreamR3->State.pCircBuf) : 0);
956 }
957 if ( RT_SUCCESS(rc)
958 && fEnable
959 && pStreamR3->Dbg.Runtime.fEnabled)
960 {
961 Assert(AudioHlpPcmPropsAreValid(&pStreamShared->State.Cfg.Props));
962
963 if (fEnable)
964 {
965 if (!AudioHlpFileIsOpen(pStreamR3->Dbg.Runtime.pFileStream))
966 {
967 int rc2 = AudioHlpFileOpen(pStreamR3->Dbg.Runtime.pFileStream, AUDIOHLPFILE_DEFAULT_OPEN_FLAGS,
968 &pStreamShared->State.Cfg.Props);
969 AssertRC(rc2);
970 }
971
972 if (!AudioHlpFileIsOpen(pStreamR3->Dbg.Runtime.pFileDMARaw))
973 {
974 int rc2 = AudioHlpFileOpen(pStreamR3->Dbg.Runtime.pFileDMARaw, AUDIOHLPFILE_DEFAULT_OPEN_FLAGS,
975 &pStreamShared->State.Cfg.Props);
976 AssertRC(rc2);
977 }
978
979 if (!AudioHlpFileIsOpen(pStreamR3->Dbg.Runtime.pFileDMAMapped))
980 {
981 int rc2 = AudioHlpFileOpen(pStreamR3->Dbg.Runtime.pFileDMAMapped, AUDIOHLPFILE_DEFAULT_OPEN_FLAGS,
982 &pStreamShared->State.Cfg.Props);
983 AssertRC(rc2);
984 }
985 }
986 }
987
988 if (RT_SUCCESS(rc))
989 {
990 if (fEnable)
991 pStreamShared->State.tsTransferLast = 0; /* Make sure it's not stale and messes up WALCLK calculations. */
992 pStreamShared->State.fRunning = fEnable;
993
994 /*
995 * Set the FIFORDY bit when we start running and clear it when stopping.
996 *
997 * This prevents Linux from timing out in snd_hdac_stream_sync when starting
998 * a stream. Technically, Linux also uses the SSYNC feature there, but we
999 * can get away with just setting the FIFORDY bit for now.
1000 */
1001 if (fEnable)
1002 HDA_STREAM_REG(pThis, STS, pStreamShared->u8SD) |= HDA_SDSTS_FIFORDY;
1003 else
1004 HDA_STREAM_REG(pThis, STS, pStreamShared->u8SD) &= ~HDA_SDSTS_FIFORDY;
1005 }
1006
1007 LogFunc(("[SD%RU8] rc=%Rrc\n", pStreamShared->u8SD, rc));
1008 return rc;
1009}
1010
1011/**
1012 * Marks the stream as started.
1013 *
1014 * Used after the stream has been enabled and the DMA timer has been armed.
1015 */
1016void hdaR3StreamMarkStarted(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared, uint64_t tsNow)
1017{
1018 pStreamShared->State.tsLastReadNs = RTTimeNanoTS();
1019 pStreamShared->State.tsStart = tsNow;
1020 pStreamShared->State.tsAioDelayEnd = tsNow + PDMDevHlpTimerFromMilli(pDevIns, pStreamShared->hTimer, pThis->msInitialDelay);
1021 Log3Func(("#%u: tsStart=%RU64 tsAioDelayEnd=%RU64 tsLastReadNs=%RU64\n", pStreamShared->u8SD,
1022 pStreamShared->State.tsStart, pStreamShared->State.tsAioDelayEnd, pStreamShared->State.tsLastReadNs));
1023
1024}
1025
1026/**
1027 * Marks the stream as stopped.
1028 */
1029void hdaR3StreamMarkStopped(PHDASTREAM pStreamShared)
1030{
1031 Log3Func(("#%u\n", pStreamShared->u8SD));
1032 pStreamShared->State.tsAioDelayEnd = UINT64_MAX;
1033}
1034
1035#endif /* IN_RING3 */
1036
1037/**
1038 * Updates an HDA stream's current read or write buffer position (depending on the stream type) by
1039 * setting its associated LPIB register and DMA position buffer (if enabled) to an absolute value.
1040 *
1041 * @param pStreamShared HDA stream to update read / write position for (shared).
1042 * @param pDevIns The device instance.
1043 * @param pThis The shared HDA device state.
1044 * @param uLPIB Absolute position (in bytes) to set current read / write position to.
1045 */
1046static void hdaStreamSetPositionAbs(PHDASTREAM pStreamShared, PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t uLPIB)
1047{
1048 AssertPtrReturnVoid(pStreamShared);
1049 AssertMsgStmt(uLPIB <= pStreamShared->u32CBL, ("%#x\n", uLPIB), uLPIB = pStreamShared->u32CBL);
1050
1051 Log3Func(("[SD%RU8] LPIB=%RU32 (DMA Position Buffer Enabled: %RTbool)\n", pStreamShared->u8SD, uLPIB, pThis->fDMAPosition));
1052
1053 /* Update LPIB in any case. */
1054 HDA_STREAM_REG(pThis, LPIB, pStreamShared->u8SD) = uLPIB;
1055
1056 /* Do we need to tell the current DMA position? */
1057 if (pThis->fDMAPosition)
1058 {
1059 /*
1060 * Linux switched to using the position buffers some time during 2.6.x.
1061 * 2.6.12 used LPIB, 2.6.17 defaulted to DMA position buffers, between
1062 * the two version things were being changing quite a bit.
1063 *
1064 * Since 2.6.17, they will treat a zero DMA position value during the first
1065 * period/IRQ as reason to fall back to LPIB mode (see azx_position_ok in
1066 * 2.6.27+, and azx_pcm_pointer before that). They later also added
1067 * UINT32_MAX to the values causing same.
1068 *
1069 * Since 2.6.35 azx_position_ok will read the wall clock register before
1070 * determining the position.
1071 */
1072 int rc2 = PDMDevHlpPCIPhysWrite(pDevIns,
1073 pThis->u64DPBase + (pStreamShared->u8SD * 2 * sizeof(uint32_t)),
1074 (void *)&uLPIB, sizeof(uint32_t));
1075 AssertRC(rc2);
1076 }
1077}
1078
1079
1080#if defined(IN_RING3) || defined(VBOX_HDA_WITH_ON_REG_ACCESS_DMA)
1081/**
1082 * Updates an HDA stream's current read or write buffer position (depending on the stream type) by
1083 * adding a value to its associated LPIB register and DMA position buffer (if enabled).
1084 *
1085 * @note Handles automatic CBL wrap-around.
1086 *
1087 * @param pStreamShared HDA stream to update read / write position for (shared).
1088 * @param pDevIns The device instance.
1089 * @param pThis The shared HDA device state.
1090 * @param cbToAdd Position (in bytes) to add to the current read / write position.
1091 */
1092static void hdaStreamSetPositionAdd(PHDASTREAM pStreamShared, PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t cbToAdd)
1093{
1094 if (cbToAdd) /* No need to update anything if 0. */
1095 {
1096 uint32_t const uCBL = pStreamShared->u32CBL;
1097 if (uCBL) /* paranoia */
1098 {
1099 uint32_t uNewLpid = HDA_STREAM_REG(pThis, LPIB, pStreamShared->u8SD) + cbToAdd;
1100# if 1 /** @todo r=bird: this is wrong according to the spec */
1101 uNewLpid %= uCBL;
1102# else
1103 /* The spec says it goes to CBL then wraps arpimd to 1, not back to zero. See 3.3.37. */
1104 if (uNewLpid > uCBL)
1105 uNewLpid %= uCBL;
1106# endif
1107 hdaStreamSetPositionAbs(pStreamShared, pDevIns, pThis, uNewLpid);
1108 }
1109 }
1110}
1111#endif /* IN_RING3 || VBOX_HDA_WITH_ON_REG_ACCESS_DMA */
1112
1113#ifdef IN_RING3
1114
1115/**
1116 * Retrieves the available size of (buffered) audio data (in bytes) of a given HDA stream.
1117 *
1118 * @returns Available data (in bytes).
1119 * @param pStreamR3 HDA stream to retrieve size for (ring-3).
1120 */
1121static uint32_t hdaR3StreamGetUsed(PHDASTREAMR3 pStreamR3)
1122{
1123 AssertPtrReturn(pStreamR3, 0);
1124
1125 if (pStreamR3->State.pCircBuf)
1126 return (uint32_t)RTCircBufUsed(pStreamR3->State.pCircBuf);
1127 return 0;
1128}
1129
1130/**
1131 * Retrieves the free size of audio data (in bytes) of a given HDA stream.
1132 *
1133 * @returns Free data (in bytes).
1134 * @param pStreamR3 HDA stream to retrieve size for (ring-3).
1135 */
1136static uint32_t hdaR3StreamGetFree(PHDASTREAMR3 pStreamR3)
1137{
1138 AssertPtrReturn(pStreamR3, 0);
1139
1140 if (pStreamR3->State.pCircBuf)
1141 return (uint32_t)RTCircBufFree(pStreamR3->State.pCircBuf);
1142 return 0;
1143}
1144
1145#endif /* IN_RING3 */
1146#if defined(IN_RING3) || defined(VBOX_HDA_WITH_ON_REG_ACCESS_DMA)
1147
1148/**
1149 * Get the current address and number of bytes left in the current BDLE.
1150 *
1151 * @returns The current physical address.
1152 * @param pStreamShared The stream to check.
1153 * @param pcbLeft The number of bytes left at the returned address.
1154 */
1155DECLINLINE(RTGCPHYS) hdaStreamDmaBufGet(PHDASTREAM pStreamShared, uint32_t *pcbLeft)
1156{
1157 uint8_t idxBdle = pStreamShared->State.idxCurBdle;
1158 AssertStmt(idxBdle < pStreamShared->State.cBdles, idxBdle = 0);
1159
1160 uint32_t const cbCurBdl = pStreamShared->State.aBdl[idxBdle].cb;
1161 uint32_t offCurBdle = pStreamShared->State.offCurBdle;
1162 AssertStmt(pStreamShared->State.offCurBdle <= cbCurBdl, offCurBdle = cbCurBdl);
1163
1164 *pcbLeft = cbCurBdl - offCurBdle;
1165 return pStreamShared->State.aBdl[idxBdle].GCPhys + offCurBdle;
1166}
1167
1168/**
1169 * Get the size of the current BDLE.
1170 *
1171 * @returns The size (in bytes).
1172 * @param pStreamShared The stream to check.
1173 */
1174DECLINLINE(RTGCPHYS) hdaStreamDmaBufGetSize(PHDASTREAM pStreamShared)
1175{
1176 uint8_t idxBdle = pStreamShared->State.idxCurBdle;
1177 AssertStmt(idxBdle < pStreamShared->State.cBdles, idxBdle = 0);
1178 return pStreamShared->State.aBdl[idxBdle].cb;
1179}
1180
1181/**
1182 * Checks if the current BDLE is completed.
1183 *
1184 * @retval true if complete
1185 * @retval false if not.
1186 * @param pStreamShared The stream to check.
1187 */
1188DECLINLINE(bool) hdaStreamDmaBufIsComplete(PHDASTREAM pStreamShared)
1189{
1190 uint8_t const idxBdle = pStreamShared->State.idxCurBdle;
1191 AssertReturn(idxBdle < pStreamShared->State.cBdles, true);
1192
1193 uint32_t const cbCurBdl = pStreamShared->State.aBdl[idxBdle].cb;
1194 uint32_t const offCurBdle = pStreamShared->State.offCurBdle;
1195 Assert(offCurBdle <= cbCurBdl);
1196 return offCurBdle >= cbCurBdl;
1197}
1198
1199/**
1200 * Checks if the current BDLE needs a completion IRQ.
1201 *
1202 * @retval true if IRQ is needed.
1203 * @retval false if not.
1204 * @param pStreamShared The stream to check.
1205 */
1206DECLINLINE(bool) hdaStreamDmaBufNeedsIrq(PHDASTREAM pStreamShared)
1207{
1208 uint8_t const idxBdle = pStreamShared->State.idxCurBdle;
1209 AssertReturn(idxBdle < pStreamShared->State.cBdles, false);
1210 return (pStreamShared->State.aBdl[idxBdle].fFlags & HDA_BDLE_F_IOC) != 0;
1211}
1212
1213/**
1214 * Advances the DMA engine to the next BDLE.
1215 *
1216 * @param pStreamShared The stream which DMA engine is to be updated.
1217 */
1218DECLINLINE(void) hdaStreamDmaBufAdvanceToNext(PHDASTREAM pStreamShared)
1219{
1220 uint8_t idxBdle = pStreamShared->State.idxCurBdle;
1221 Assert(pStreamShared->State.offCurBdle == pStreamShared->State.aBdl[idxBdle].cb);
1222
1223 if (idxBdle < pStreamShared->State.cBdles - 1)
1224 idxBdle++;
1225 else
1226 idxBdle = 0;
1227 pStreamShared->State.idxCurBdle = idxBdle;
1228 pStreamShared->State.offCurBdle = 0;
1229}
1230
1231#endif /* defined(IN_RING3) || defined(VBOX_HDA_WITH_ON_REG_ACCESS_DMA) */
1232#ifdef IN_RING3
1233
1234/**
1235 * Common do-DMA prologue code.
1236 *
1237 * @retval true if DMA processing can take place
1238 * @retval false if caller should return immediately.
1239 * @param pThis The shared HDA device state.
1240 * @param pStreamShared HDA stream to update (shared).
1241 * @param pStreamR3 HDA stream to update (ring-3).
1242 * @param uSD The stream ID (for asserting).
1243 * @param tsNowNs The current RTTimeNano() value.
1244 * @param pszFunction The function name (for logging).
1245 */
1246DECLINLINE(bool) hdaR3StreamDoDmaPrologue(PHDASTATE pThis, PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, uint8_t uSD,
1247 uint64_t tsNowNs, const char *pszFunction)
1248{
1249 RT_NOREF(uSD, pszFunction);
1250
1251 /*
1252 * Check if we should skip town...
1253 */
1254 /* Stream not running (anymore)? */
1255 if (pStreamShared->State.fRunning)
1256 { /* likely */ }
1257 else
1258 {
1259 Log3(("%s: [SD%RU8] Not running, skipping transfer\n", pszFunction, uSD));
1260 return false;
1261 }
1262
1263 if (!(HDA_STREAM_REG(pThis, STS, uSD) & HDA_SDSTS_BCIS))
1264 { /* likely */ }
1265 else
1266 {
1267 /** @todo r=bird: This is a bit fishy. We should make effort the reschedule
1268 * the transfer immediately after the guest clears the interrupt.
1269 * The same fishy code is present in AC'97 with just a little
1270 * explanation as here, see @bugref{9890#c95}.
1271 *
1272 * The reasoning is probably that the developer noticed some windows
1273 * versions don't like having their BCIS interrupts bundled. There were
1274 * comments to that effect elsewhere, probably as a result of a fixed
1275 * uTimerHz approach to DMA scheduling. However, pausing DMA for a
1276 * period isn't going to help us with the host backends, as they don't
1277 * pause and will want samples ASAP. So, we should at least unpause
1278 * DMA as quickly as we possible when BCIS is cleared. We might even
1279 * not skip it iff the DMA work here doesn't involve raising any IOC,
1280 * which is possible although unlikely. */
1281 Log3(("%s: [SD%RU8] BCIS bit set, skipping transfer\n", pszFunction, uSD));
1282 STAM_REL_COUNTER_INC(&pStreamR3->State.StatDmaSkippedPendingBcis);
1283 Log(("%s: [SD%RU8] BCIS bit set, skipping transfer\n", pszFunction, uSD));
1284# ifdef HDA_STRICT
1285 /* Timing emulation bug or guest is misbehaving -- let me know. */
1286 AssertMsgFailed(("%s: BCIS bit for stream #%RU8 still set when it shouldn't\n", pszFunction, uSD));
1287# endif
1288 return false;
1289 }
1290
1291 /*
1292 * Stream sanity checks.
1293 */
1294 /* Register sanity checks. */
1295 Assert(uSD < HDA_MAX_STREAMS);
1296 Assert(pStreamShared->u64BDLBase);
1297 Assert(pStreamShared->u32CBL);
1298 Assert(pStreamShared->u8FIFOS);
1299
1300 /* State sanity checks. */
1301 Assert(ASMAtomicReadBool(&pStreamShared->State.fInReset) == false);
1302 Assert(ASMAtomicReadBool(&pStreamShared->State.fRunning));
1303
1304 /*
1305 * Some timestamp stuff for logging/debugging.
1306 */
1307 /*const uint64_t tsNowNs = RTTimeNanoTS();*/
1308 Log3(("%s: [SD%RU8] tsDeltaNs=%'RU64 ns\n", pszFunction, uSD, tsNowNs - pStreamShared->State.tsLastTransferNs));
1309 pStreamShared->State.tsLastTransferNs = tsNowNs;
1310
1311 return true;
1312}
1313
1314/**
1315 * Common do-DMA epilogue.
1316 *
1317 * @param pDevIns The device instance.
1318 * @param pStreamShared The HDA stream (shared).
1319 * @param pStreamR3 The HDA stream (ring-3).
1320 */
1321DECLINLINE(void) hdaR3StreamDoDmaEpilogue(PPDMDEVINS pDevIns, PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3)
1322{
1323 /*
1324 * We must update this in the epilogue rather than in the prologue
1325 * as it is used for WALCLK calculation and we must make sure the
1326 * guest doesn't think we've processed the current period till we
1327 * actually have.
1328 */
1329 pStreamShared->State.tsTransferLast = PDMDevHlpTimerGet(pDevIns, pStreamShared->hTimer);
1330
1331 /*
1332 * Update the buffer statistics.
1333 */
1334 pStreamR3->State.StatDmaBufUsed = (uint32_t)RTCircBufUsed(pStreamR3->State.pCircBuf);
1335}
1336
1337#endif /* IN_RING3 */
1338
1339#if defined(IN_RING3) || defined(VBOX_HDA_WITH_ON_REG_ACCESS_DMA)
1340/**
1341 * Completes a BDLE at the end of a DMA loop iteration, if possible.
1342 *
1343 * @retval true if buffer completed and new loaded.
1344 * @retval false if buffer not completed.
1345 * @param pDevIns The device instance.
1346 * @param pThis The shared HDA device state.
1347 * @param pStreamShared HDA stream to update (shared).
1348 * @param pszFunction The function name (for logging).
1349 */
1350DECLINLINE(bool) hdaStreamDoDmaMaybeCompleteBuffer(PPDMDEVINS pDevIns, PHDASTATE pThis,
1351 PHDASTREAM pStreamShared, const char *pszFunction)
1352{
1353 RT_NOREF(pszFunction);
1354
1355 /*
1356 * Is the buffer descriptor complete.
1357 */
1358 if (hdaStreamDmaBufIsComplete(pStreamShared))
1359 {
1360 Log3(("%s: [SD%RU8] Completed BDLE%u %#RX64 LB %#RX32 fFlags=%#x\n", pszFunction, pStreamShared->u8SD,
1361 pStreamShared->State.idxCurBdle, pStreamShared->State.aBdl[pStreamShared->State.idxCurBdle].GCPhys,
1362 pStreamShared->State.aBdl[pStreamShared->State.idxCurBdle].cb,
1363 pStreamShared->State.aBdl[pStreamShared->State.idxCurBdle].fFlags));
1364
1365#if 0 /* Moved to the transfer loops */
1366 /*
1367 * Update the stream's current position.
1368 *
1369 * Do this as accurate and close to the actual data transfer as possible.
1370 * All guests rely on this, depending on the mechanism they use (LPIB register or DMA counters).
1371 *
1372 * Note for Windows 10: The OS' driver is *very* picky about *when* the (DMA) positions get updated!
1373 * Not doing this at the right time will result in ugly sound crackles!
1374 */
1375 hdaStreamSetPositionAdd(pStreamShared, pDevIns, pThis, hdaStreamDmaBufGetSize(pStreamShared));
1376#endif
1377
1378 /* Does the current BDLE require an interrupt to be sent? */
1379 if (hdaStreamDmaBufNeedsIrq(pStreamShared))
1380 {
1381 /* If the IOCE ("Interrupt On Completion Enable") bit of the SDCTL
1382 register is set we need to generate an interrupt. */
1383 if (HDA_STREAM_REG(pThis, CTL, pStreamShared->u8SD) & HDA_SDCTL_IOCE)
1384 {
1385 /* Assert the interrupt before actually fetching the next BDLE below. */
1386 pStreamShared->State.cTransferPendingInterrupts = 1;
1387 Log3(("%s: [SD%RU8] Scheduling interrupt\n", pszFunction, pStreamShared->u8SD));
1388
1389 /* Trigger an interrupt first and let hdaRegWriteSDSTS() deal with
1390 * ending / beginning of a period. */
1391 /** @todo r=bird: What does the above comment mean? */
1392 HDA_STREAM_REG(pThis, STS, pStreamShared->u8SD) |= HDA_SDSTS_BCIS;
1393 HDA_PROCESS_INTERRUPT(pDevIns, pThis);
1394 }
1395 }
1396
1397 /*
1398 * Advance to the next BDLE.
1399 */
1400 hdaStreamDmaBufAdvanceToNext(pStreamShared);
1401 return true;
1402 }
1403
1404 Log3(("%s: [SD%RU8] Incomplete BDLE%u %#RX64 LB %#RX32 fFlags=%#x: off=%#RX32\n", pszFunction, pStreamShared->u8SD,
1405 pStreamShared->State.idxCurBdle, pStreamShared->State.aBdl[pStreamShared->State.idxCurBdle].GCPhys,
1406 pStreamShared->State.aBdl[pStreamShared->State.idxCurBdle].cb,
1407 pStreamShared->State.aBdl[pStreamShared->State.idxCurBdle].fFlags, pStreamShared->State.offCurBdle));
1408 return false;
1409}
1410#endif /* IN_RING3 || VBOX_HDA_WITH_ON_REG_ACCESS_DMA */
1411
1412
1413#ifdef IN_RING3
1414
1415/**
1416 * Does DMA transfer for an HDA input stream.
1417 *
1418 * Reads audio data from the HDA stream's internal DMA buffer and writing to
1419 * guest memory.
1420 *
1421 * @param pDevIns The device instance.
1422 * @param pThis The shared HDA device state.
1423 * @param pStreamShared HDA stream to update (shared).
1424 * @param pStreamR3 HDA stream to update (ring-3).
1425 * @param cbToConsume The max amount of data to consume from the
1426 * internal DMA buffer. The caller will make sure
1427 * this is always the transfer size fo the current
1428 * period (unless something is seriously wrong).
1429 * @param fWriteSilence Whether to feed the guest silence rather than
1430 * fetching bytes from the internal DMA buffer.
1431 * This is set initially while we pre-buffer a
1432 * little bit of input, so we can better handle
1433 * time catch-ups and other schduling fun.
1434 * @param tsNowNs The current RTTimeNano() value.
1435 *
1436 * @remarks Caller owns the stream lock.
1437 */
1438static void hdaR3StreamDoDmaInput(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared,
1439 PHDASTREAMR3 pStreamR3, uint32_t const cbToConsume, bool fWriteSilence, uint64_t tsNowNs)
1440{
1441 uint8_t const uSD = pStreamShared->u8SD;
1442 LogFlowFunc(("ENTER - #%u cbToConsume=%#x%s\n", uSD, cbToConsume, fWriteSilence ? " silence" : ""));
1443
1444 /*
1445 * Common prologue.
1446 */
1447 if (hdaR3StreamDoDmaPrologue(pThis, pStreamShared, pStreamR3, uSD, tsNowNs, "hdaR3StreamDoDmaInput"))
1448 { /* likely */ }
1449 else
1450 return;
1451
1452 /*
1453 *
1454 * The DMA copy loop.
1455 *
1456 */
1457 uint8_t abBounce[4096 + 128]; /* Most guest does at most 4KB BDLE. So, 4KB + space for a partial frame to reduce loops. */
1458 uint32_t cbBounce = 0; /* in case of incomplete frames between buffer segments */
1459 PRTCIRCBUF pCircBuf = pStreamR3->State.pCircBuf;
1460 uint32_t cbLeft = cbToConsume;
1461 Assert(cbLeft == pStreamShared->State.cbTransferSize);
1462 Assert(PDMAudioPropsIsSizeAligned(&pStreamShared->State.Cfg.Props, cbLeft));
1463
1464 while (cbLeft > 0)
1465 {
1466 STAM_PROFILE_START(&pThis->StatIn, a);
1467
1468 /*
1469 * Figure out how much we can read & write in this iteration.
1470 */
1471 uint32_t cbChunk = 0;
1472 RTGCPHYS GCPhys = hdaStreamDmaBufGet(pStreamShared, &cbChunk);
1473
1474 /* If we're writing silence. */
1475 uint32_t cbWritten = 0;
1476 if (!fWriteSilence)
1477 {
1478 if (cbChunk <= cbLeft)
1479 { /* very likely */ }
1480 else
1481 cbChunk = cbLeft;
1482
1483 /*
1484 * Write the host data directly into the guest buffers.
1485 */
1486 while (cbChunk > 0)
1487 {
1488 /* Grab internal DMA buffer space and read into it. */
1489 void /*const*/ *pvBufSrc;
1490 size_t cbBufSrc;
1491 RTCircBufAcquireReadBlock(pCircBuf, cbChunk, &pvBufSrc, &cbBufSrc);
1492 AssertBreakStmt(cbBufSrc, RTCircBufReleaseReadBlock(pCircBuf, 0));
1493
1494 int rc2 = PDMDevHlpPCIPhysWrite(pDevIns, GCPhys, pvBufSrc, cbBufSrc);
1495 AssertRC(rc2);
1496
1497# ifdef HDA_DEBUG_SILENCE
1498 fix me if relevant;
1499# endif
1500 if (RT_LIKELY(!pStreamR3->Dbg.Runtime.fEnabled))
1501 { /* likely */ }
1502 else
1503 AudioHlpFileWrite(pStreamR3->Dbg.Runtime.pFileDMARaw, pvBufSrc, cbBufSrc, 0 /* fFlags */);
1504
1505# ifdef VBOX_WITH_DTRACE
1506 VBOXDD_HDA_STREAM_DMA_IN((uint32_t)uSD, (uint32_t)cbBufSrc, pStreamShared->State.offRead);
1507# endif
1508 pStreamShared->State.offRead += cbBufSrc;
1509 RTCircBufReleaseReadBlock(pCircBuf, cbBufSrc);
1510 STAM_COUNTER_ADD(&pThis->StatBytesWritten, cbBufSrc);
1511
1512 /* advance */
1513 cbChunk -= (uint32_t)cbBufSrc;
1514 cbWritten += (uint32_t)cbBufSrc;
1515 GCPhys += cbBufSrc;
1516 pStreamShared->State.offCurBdle += (uint32_t)cbBufSrc;
1517 }
1518 }
1519 /*
1520 * We've got some initial silence to write, or we need to do
1521 * channel mapping. We produce guest output into the bounce buffer,
1522 * which is then copied into guest memory. The bounce buffer may keep
1523 * partial frames there for the next BDLE, if an BDLE isn't frame aligned.
1524 *
1525 * Note! cbLeft is relative to the input (host) frame size.
1526 * cbChunk OTOH is relative to output (guest) size.
1527 */
1528 else
1529 {
1530/** @todo clean up host/guest props distinction, they're the same now w/o the
1531 * mapping done by the mixer rather than us. */
1532 PCPDMAUDIOPCMPROPS pGuestProps = &pStreamShared->State.Cfg.Props;
1533 Assert(PDMAudioPropsIsSizeAligned(&pStreamShared->State.Cfg.Props, cbLeft));
1534 uint32_t const cbLeftGuest = PDMAudioPropsFramesToBytes(pGuestProps,
1535 PDMAudioPropsBytesToFrames(&pStreamShared->State.Cfg.Props,
1536 cbLeft));
1537 if (cbChunk <= cbLeftGuest)
1538 { /* very likely */ }
1539 else
1540 cbChunk = cbLeftGuest;
1541
1542 /*
1543 * Work till we've covered the chunk.
1544 */
1545 Log5Func(("loop0: GCPhys=%RGp cbChunk=%#x + cbBounce=%#x\n", GCPhys, cbChunk, cbBounce));
1546 while (cbChunk > 0)
1547 {
1548 /* Figure out how much we need to convert into the bounce buffer: */
1549 uint32_t cbGuest = PDMAudioPropsRoundUpBytesToFrame(pGuestProps, cbChunk - cbBounce);
1550 uint32_t cFrames = PDMAudioPropsBytesToFrames(pGuestProps, RT_MIN(cbGuest, sizeof(abBounce) - cbBounce));
1551
1552 cbGuest = PDMAudioPropsFramesToBytes(pGuestProps, cFrames);
1553 PDMAudioPropsClearBuffer(pGuestProps, &abBounce[cbBounce], cbGuest, cFrames);
1554 cbGuest += cbBounce;
1555
1556 /* Write it to the guest buffer. */
1557 uint32_t cbGuestActual = RT_MIN(cbGuest, cbChunk);
1558 int rc2 = PDMDevHlpPCIPhysWrite(pDevIns, GCPhys, abBounce, cbGuestActual);
1559 AssertRC(rc2);
1560 STAM_COUNTER_ADD(&pThis->StatBytesWritten, cbGuestActual);
1561
1562 /* advance */
1563 cbWritten += cbGuestActual;
1564 cbChunk -= cbGuestActual;
1565 GCPhys += cbGuestActual;
1566 pStreamShared->State.offCurBdle += cbGuestActual;
1567
1568 cbBounce = cbGuest - cbGuestActual;
1569 if (cbBounce)
1570 memmove(abBounce, &abBounce[cbGuestActual], cbBounce);
1571
1572 Log5Func((" loop1: GCPhys=%RGp cbGuestActual=%#x cbBounce=%#x cFrames=%#x\n", GCPhys, cbGuestActual, cbBounce, cFrames));
1573 }
1574 Log5Func(("loop0: GCPhys=%RGp cbBounce=%#x cbLeft=%#x\n", GCPhys, cbBounce, cbLeft - cbWritten));
1575 }
1576
1577 cbLeft -= cbWritten;
1578 STAM_PROFILE_STOP(&pThis->StatIn, a);
1579
1580 /*
1581 * Complete the buffer if necessary (common with the output DMA code).
1582 *
1583 * Must update the DMA position before we do this as the buffer IRQ may
1584 * fire on another vCPU and run in parallel to us, although it is very
1585 * unlikely it can make much progress as long as we're sitting on the
1586 * lock, it could still read the DMA position (Linux won't, as it reads
1587 * WALCLK and possibly SDnSTS before the DMA position).
1588 */
1589 hdaStreamSetPositionAdd(pStreamShared, pDevIns, pThis, cbWritten);
1590 hdaStreamDoDmaMaybeCompleteBuffer(pDevIns, pThis, pStreamShared, "hdaR3StreamDoDmaInput");
1591 }
1592
1593 Assert(cbLeft == 0); /* There shall be no break statements in the above loop, so cbLeft is always zero here! */
1594 AssertMsg(cbBounce == 0, ("%#x\n", cbBounce));
1595
1596 /*
1597 * Common epilogue.
1598 */
1599 hdaR3StreamDoDmaEpilogue(pDevIns, pStreamShared, pStreamR3);
1600
1601 /*
1602 * Log and leave.
1603 */
1604 Log3Func(("LEAVE - [SD%RU8] %#RX32/%#RX32 @ %#RX64 - cTransferPendingInterrupts=%RU8\n",
1605 uSD, cbToConsume, pStreamShared->State.cbTransferSize, pStreamShared->State.offRead - cbToConsume,
1606 pStreamShared->State.cTransferPendingInterrupts));
1607}
1608
1609
1610/**
1611 * Input streams: Pulls data from the mixer, putting it in the internal DMA
1612 * buffer.
1613 *
1614 * @param pStreamShared HDA stream to update (shared).
1615 * @param pStreamR3 HDA stream to update (ring-3 bits).
1616 * @param pSink The mixer sink to pull from.
1617 */
1618static void hdaR3StreamPullFromMixer(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, PAUDMIXSINK pSink)
1619{
1620# ifdef LOG_ENABLED
1621 uint64_t const offWriteOld = pStreamShared->State.offWrite;
1622# endif
1623 pStreamShared->State.offWrite = AudioMixerSinkTransferToCircBuf(pSink,
1624 pStreamR3->State.pCircBuf,
1625 pStreamShared->State.offWrite,
1626 pStreamR3->u8SD,
1627 pStreamR3->Dbg.Runtime.fEnabled
1628 ? pStreamR3->Dbg.Runtime.pFileStream : NULL);
1629
1630 Log3Func(("[SD%RU8] transferred=%#RX64 bytes -> @%#RX64\n", pStreamR3->u8SD,
1631 pStreamShared->State.offWrite - offWriteOld, pStreamShared->State.offWrite));
1632
1633 /* Update buffer stats. */
1634 pStreamR3->State.StatDmaBufUsed = (uint32_t)RTCircBufUsed(pStreamR3->State.pCircBuf);
1635}
1636
1637
1638/**
1639 * Does DMA transfer for an HDA output stream.
1640 *
1641 * This transfers one DMA timer period worth of data from the guest and into the
1642 * internal DMA buffer.
1643 *
1644 * @param pDevIns The device instance.
1645 * @param pThis The shared HDA device state.
1646 * @param pStreamShared HDA stream to update (shared).
1647 * @param pStreamR3 HDA stream to update (ring-3).
1648 * @param cbToProduce The max amount of data to produce (i.e. put into
1649 * the circular buffer). Unless something is going
1650 * seriously wrong, this will always be transfer
1651 * size for the current period.
1652 * @param tsNowNs The current RTTimeNano() value.
1653 *
1654 * @remarks Caller owns the stream lock.
1655 */
1656static void hdaR3StreamDoDmaOutput(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared,
1657 PHDASTREAMR3 pStreamR3, uint32_t const cbToProduce, uint64_t tsNowNs)
1658{
1659 uint8_t const uSD = pStreamShared->u8SD;
1660 LogFlowFunc(("ENTER - #%u cbToProduce=%#x\n", uSD, cbToProduce));
1661
1662 /*
1663 * Common prologue.
1664 */
1665 if (hdaR3StreamDoDmaPrologue(pThis, pStreamShared, pStreamR3, uSD, tsNowNs, "hdaR3StreamDoDmaOutput"))
1666 { /* likely */ }
1667 else
1668 return;
1669
1670 /*
1671 *
1672 * The DMA copy loop.
1673 *
1674 */
1675# if 0
1676 uint8_t abBounce[4096 + 128]; /* Most guest does at most 4KB BDLE. So, 4KB + space for a partial frame to reduce loops. */
1677 uint32_t cbBounce = 0; /* in case of incomplete frames between buffer segments */
1678# endif
1679 PRTCIRCBUF pCircBuf = pStreamR3->State.pCircBuf;
1680 uint32_t cbLeft = cbToProduce;
1681# ifdef VBOX_HDA_WITH_ON_REG_ACCESS_DMA
1682 Assert(cbLeft <= pStreamShared->State.cbTransferSize); /* a little pointless with the DMA'ing on LPIB read. */
1683# else
1684 Assert(cbLeft == pStreamShared->State.cbTransferSize);
1685# endif
1686 Assert(PDMAudioPropsIsSizeAligned(&pStreamShared->State.Cfg.Props, cbLeft));
1687
1688 while (cbLeft > 0)
1689 {
1690 STAM_PROFILE_START(&pThis->StatOut, a);
1691
1692 /*
1693 * Figure out how much we can read & write in this iteration.
1694 */
1695 uint32_t cbChunk = 0;
1696 RTGCPHYS GCPhys = hdaStreamDmaBufGet(pStreamShared, &cbChunk);
1697
1698 /* Need to diverge if the BDLEs contain misaligned entries. */
1699 uint32_t cbRead = 0;
1700# if 0
1701 if (/** @todo pStreamShared->State.fFrameAlignedBuffers */)
1702# endif
1703 {
1704 if (cbChunk <= cbLeft)
1705 { /* very likely */ }
1706 else
1707 cbChunk = cbLeft;
1708
1709 /*
1710 * Read the guest data directly into the internal DMA buffer.
1711 */
1712 while (cbChunk > 0)
1713 {
1714 /* Grab internal DMA buffer space and read into it. */
1715 void *pvBufDst;
1716 size_t cbBufDst;
1717 RTCircBufAcquireWriteBlock(pCircBuf, cbChunk, &pvBufDst, &cbBufDst);
1718 AssertBreakStmt(cbBufDst, RTCircBufReleaseWriteBlock(pCircBuf, 0));
1719
1720 int rc2 = PDMDevHlpPCIPhysRead(pDevIns, GCPhys, pvBufDst, cbBufDst);
1721 AssertRC(rc2);
1722
1723# ifdef HDA_DEBUG_SILENCE
1724 fix me if relevant;
1725# endif
1726 if (RT_LIKELY(!pStreamR3->Dbg.Runtime.fEnabled))
1727 { /* likely */ }
1728 else
1729 AudioHlpFileWrite(pStreamR3->Dbg.Runtime.pFileDMARaw, pvBufDst, cbBufDst, 0 /* fFlags */);
1730
1731# ifdef VBOX_WITH_DTRACE
1732 VBOXDD_HDA_STREAM_DMA_OUT((uint32_t)uSD, (uint32_t)cbBufDst, pStreamShared->State.offWrite);
1733# endif
1734 pStreamShared->State.offWrite += cbBufDst;
1735 RTCircBufReleaseWriteBlock(pCircBuf, cbBufDst);
1736 STAM_COUNTER_ADD(&pThis->StatBytesRead, cbBufDst);
1737
1738 /* advance */
1739 cbChunk -= (uint32_t)cbBufDst;
1740 cbRead += (uint32_t)cbBufDst;
1741 GCPhys += cbBufDst;
1742 pStreamShared->State.offCurBdle += (uint32_t)cbBufDst;
1743 }
1744 }
1745# if 0
1746 /*
1747 * Need to map the frame content, so we need to read the guest data
1748 * into a temporary buffer, though the output can be directly written
1749 * into the internal buffer as it is assumed to be frame aligned.
1750 *
1751 * Note! cbLeft is relative to the output frame size.
1752 * cbChunk OTOH is relative to input size.
1753 */
1754 else
1755 {
1756/** @todo clean up host/guest props distinction, they're the same now w/o the
1757 * mapping done by the mixer rather than us. */
1758 PCPDMAUDIOPCMPROPS pGuestProps = &pStreamShared->State.Cfg.Props;
1759 Assert(PDMAudioPropsIsSizeAligned(&pStreamShared->State.Cfg.Props, cbLeft));
1760 uint32_t const cbLeftGuest = PDMAudioPropsFramesToBytes(pGuestProps,
1761 PDMAudioPropsBytesToFrames(&pStreamShared->State.Cfg.Props,
1762 cbLeft));
1763 if (cbChunk <= cbLeftGuest)
1764 { /* very likely */ }
1765 else
1766 cbChunk = cbLeftGuest;
1767
1768 /*
1769 * Loop till we've covered the chunk.
1770 */
1771 Log5Func(("loop0: GCPhys=%RGp cbChunk=%#x + cbBounce=%#x\n", GCPhys, cbChunk, cbBounce));
1772 while (cbChunk > 0)
1773 {
1774 /* Read into the bounce buffer. */
1775 uint32_t const cbToRead = RT_MIN(cbChunk, sizeof(abBounce) - cbBounce);
1776 int rc2 = PDMDevHlpPCIPhysRead(pDevIns, GCPhys, &abBounce[cbBounce], cbToRead);
1777 AssertRC(rc2);
1778 cbBounce += cbToRead;
1779
1780 /* Convert the size to whole frames and a remainder. */
1781 uint32_t cFrames = PDMAudioPropsBytesToFrames(pGuestProps, cbBounce);
1782 uint32_t const cbRemainder = cbBounce - PDMAudioPropsFramesToBytes(pGuestProps, cFrames);
1783 Log5Func((" loop1: GCPhys=%RGp cbToRead=%#x cbBounce=%#x cFrames=%#x\n", GCPhys, cbToRead, cbBounce, cFrames));
1784
1785 /*
1786 * Convert from the bounce buffer and into the internal DMA buffer.
1787 */
1788 uint32_t offBounce = 0;
1789 while (cFrames > 0)
1790 {
1791 void *pvBufDst;
1792 size_t cbBufDst;
1793 RTCircBufAcquireWriteBlock(pCircBuf, PDMAudioPropsFramesToBytes(&pStreamShared->State.Cfg.Props, cFrames),
1794 &pvBufDst, &cbBufDst);
1795
1796 uint32_t const cFramesToConvert = PDMAudioPropsBytesToFrames(&pStreamShared->State.Cfg.Props, (uint32_t)cbBufDst);
1797 Assert(PDMAudioPropsFramesToBytes(&pStreamShared->State.Cfg.Props, cFramesToConvert) == cbBufDst);
1798 Assert(cFramesToConvert > 0);
1799 Assert(cFramesToConvert <= cFrames);
1800
1801 pStreamR3->State.Mapping.pfnGuestToHost(pvBufDst, &abBounce[offBounce], cFramesToConvert,
1802 &pStreamR3->State.Mapping);
1803 Log5Func((" loop2: offBounce=%#05x cFramesToConvert=%#05x cbBufDst=%#x%s\n",
1804 offBounce, cFramesToConvert, cbBufDst, ASMMemIsZero(pvBufDst, cbBufDst) ? " all zero" : ""));
1805
1806# ifdef HDA_DEBUG_SILENCE
1807 fix me if relevant;
1808# endif
1809 if (RT_LIKELY(!pStreamR3->Dbg.Runtime.fEnabled))
1810 { /* likely */ }
1811 else
1812 AudioHlpFileWrite(pStreamR3->Dbg.Runtime.pFileDMARaw, pvBufDst, cbBufDst, 0 /* fFlags */);
1813
1814 pStreamR3->State.offWrite += cbBufDst;
1815 RTCircBufReleaseWriteBlock(pCircBuf, cbBufDst);
1816 STAM_COUNTER_ADD(&pThis->StatBytesRead, cbBufDst);
1817
1818 /* advance */
1819 cbLeft -= (uint32_t)cbBufDst;
1820 cFrames -= cFramesToConvert;
1821 offBounce += PDMAudioPropsFramesToBytes(pGuestProps, cFramesToConvert);
1822 }
1823
1824 /* advance */
1825 cbChunk -= cbToRead;
1826 GCPhys += cbToRead;
1827 pStreamShared->State.offCurBdle += cbToRead;
1828 if (cbRemainder)
1829 memmove(&abBounce[0], &abBounce[cbBounce - cbRemainder], cbRemainder);
1830 cbBounce = cbRemainder;
1831 }
1832 Log5Func(("loop0: GCPhys=%RGp cbBounce=%#x cbLeft=%#x\n", GCPhys, cbBounce, cbLeft));
1833 }
1834# endif
1835
1836 cbLeft -= cbRead;
1837 STAM_PROFILE_STOP(&pThis->StatOut, a);
1838
1839 /*
1840 * Complete the buffer if necessary (common with the input DMA code).
1841 *
1842 * Must update the DMA position before we do this as the buffer IRQ may
1843 * fire on another vCPU and run in parallel to us, although it is very
1844 * unlikely it can make much progress as long as we're sitting on the
1845 * lock, it could still read the DMA position (Linux won't, as it reads
1846 * WALCLK and possibly SDnSTS before the DMA position).
1847 */
1848 hdaStreamSetPositionAdd(pStreamShared, pDevIns, pThis, cbRead);
1849 hdaStreamDoDmaMaybeCompleteBuffer(pDevIns, pThis, pStreamShared, "hdaR3StreamDoDmaOutput");
1850 }
1851
1852 Assert(cbLeft == 0); /* There shall be no break statements in the above loop, so cbLeft is always zero here! */
1853# if 0
1854 AssertMsg(cbBounce == 0, ("%#x\n", cbBounce));
1855# endif
1856
1857 /*
1858 * Common epilogue.
1859 */
1860 hdaR3StreamDoDmaEpilogue(pDevIns, pStreamShared, pStreamR3);
1861
1862 /*
1863 * Log and leave.
1864 */
1865 Log3Func(("LEAVE - [SD%RU8] %#RX32/%#RX32 @ %#RX64 - cTransferPendingInterrupts=%RU8\n",
1866 uSD, cbToProduce, pStreamShared->State.cbTransferSize, pStreamShared->State.offWrite - cbToProduce,
1867 pStreamShared->State.cTransferPendingInterrupts));
1868}
1869
1870#endif /* IN_RING3 */
1871
1872#ifdef VBOX_HDA_WITH_ON_REG_ACCESS_DMA
1873/**
1874 * Do DMA output transfer on LPIB register access.
1875 *
1876 * @returns VINF_SUCCESS or VINF_IOM_R3_MMIO_READ.
1877 * @param pDevIns The device instance.
1878 * @param pThis The shared instance data.
1879 * @param pStreamShared The shared stream data.
1880 * @param cbToTransfer How much to transfer.
1881 */
1882VBOXSTRICTRC hdaStreamDoOnAccessDmaOutput(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared, uint32_t cbToTransfer)
1883{
1884 AssertReturn(cbToTransfer > 0, VINF_SUCCESS);
1885 int rc = VINF_SUCCESS;
1886
1887 /*
1888 * Check if we're exceeding the available buffer, go to ring-3 to
1889 * handle that (we would perhaps always take this path when in ring-3).
1890 */
1891 uint32_t cbDma = pStreamShared->State.cbDma;
1892 ASMCompilerBarrier();
1893 if ( cbDma >= sizeof(pStreamShared->State.abDma) /* paranoia */
1894 || cbToTransfer >= sizeof(pStreamShared->State.abDma) /* paranoia */
1895 || cbDma + cbToTransfer > sizeof(pStreamShared->State.abDma))
1896 {
1897# ifndef IN_RING3
1898 STAM_REL_COUNTER_INC(&pThis->StatAccessDmaOutputToR3);
1899 LogFlowFunc(("[SD%RU8] out of DMA buffer space (%#x, need %#x) -> VINF_IOM_R3_MMIO_READ\n",
1900 pStreamShared->u8SD, sizeof(pStreamShared->State.abDma) - pStreamShared->State.cbDma, cbToTransfer));
1901 return VINF_IOM_R3_MMIO_READ;
1902# else /* IN_RING3 */
1903 /*
1904 * Flush the bounce buffer, then do direct transfers to the
1905 * internal DMA buffer (updates LPIB).
1906 */
1907 PHDASTATER3 const pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PHDASTATER3);
1908 uintptr_t const idxStream = pStreamShared->u8SD;
1909 AssertReturn(idxStream < RT_ELEMENTS(pThisCC->aStreams), VERR_INTERNAL_ERROR_4);
1910 PHDASTREAMR3 const pStreamR3 = &pThisCC->aStreams[idxStream];
1911
1912 hdaR3StreamFlushDmaBounceBufferOutput(pStreamShared, pStreamR3);
1913
1914 uint32_t cbStreamFree = hdaR3StreamGetFree(pStreamR3);
1915 if (cbStreamFree >= cbToTransfer)
1916 { /* likely */ }
1917 else
1918 {
1919 PAUDMIXSINK pSink = pStreamR3->pMixSink ? pStreamR3->pMixSink->pMixSink : NULL;
1920 if (pSink)
1921 cbStreamFree = hdaR3StreamHandleDmaBufferOverrun(pStreamShared, pStreamR3, pSink, cbToTransfer, RTTimeNanoTS(),
1922 "hdaStreamDoOnAccessDmaOutput", cbStreamFree);
1923 else
1924 {
1925 LogFunc(("[SD%RU8] No sink and insufficient internal DMA buffer space (%#x) - won't do anything\n",
1926 pStreamShared->u8SD, cbStreamFree));
1927 return VINF_SUCCESS;
1928 }
1929 cbToTransfer = RT_MIN(cbToTransfer, cbStreamFree);
1930 if (cbToTransfer < PDMAudioPropsFrameSize(&pStreamShared->State.Cfg.Props))
1931 {
1932 LogFunc(("[SD%RU8] No internal DMA buffer space (%#x) - won't do anything\n", pStreamShared->u8SD, cbStreamFree));
1933 return VINF_SUCCESS;
1934 }
1935 }
1936 hdaR3StreamDoDmaOutput(pDevIns, pThis, pStreamShared, pStreamR3, cbToTransfer, RTTimeNanoTS());
1937 pStreamShared->State.cbDmaTotal += cbToTransfer;
1938# endif /* IN_RING3 */
1939 }
1940 else
1941 {
1942 /*
1943 * Transfer into the DMA bounce buffer.
1944 */
1945 LogFlowFunc(("[SD%RU8] Transfering %#x bytes to DMA bounce buffer (cbDma=%#x cbDmaTotal=%#x) (%p/%u)\n",
1946 pStreamShared->u8SD, cbToTransfer, cbDma, pStreamShared->State.cbDmaTotal, pStreamShared, pStreamShared->u8SD));
1947 uint32_t cbLeft = cbToTransfer;
1948 do
1949 {
1950 uint32_t cbChunk = 0;
1951 RTGCPHYS GCPhys = hdaStreamDmaBufGet(pStreamShared, &cbChunk);
1952
1953 bool fMustAdvanceBuffer;
1954 if (cbLeft < cbChunk)
1955 {
1956 fMustAdvanceBuffer = false;
1957 cbChunk = cbLeft;
1958 }
1959 else
1960 fMustAdvanceBuffer = true;
1961
1962 /* Read the guest data directly into the DMA bounce buffer. */
1963 int rc2 = PDMDevHlpPCIPhysRead(pDevIns, GCPhys, &pStreamShared->State.abDma[cbDma], cbChunk);
1964 AssertRC(rc2);
1965
1966 /* We update offWrite and StatBytesRead here even if we haven't moved the data
1967 to the internal DMA buffer yet, because we want the dtrace even to fire here. */
1968# ifdef VBOX_WITH_DTRACE
1969 VBOXDD_HDA_STREAM_DMA_OUT((uint32_t)pStreamShared->u8SD, cbChunk, pStreamShared->State.offWrite);
1970# endif
1971 pStreamShared->State.offWrite += cbChunk;
1972 STAM_COUNTER_ADD(&pThis->StatBytesRead, cbChunk);
1973
1974 /* advance */
1975 pStreamShared->State.offCurBdle += cbChunk;
1976 pStreamShared->State.cbDmaTotal += cbChunk;
1977 cbDma += cbChunk;
1978 pStreamShared->State.cbDma = cbDma;
1979 cbLeft -= cbChunk;
1980 Log6Func(("cbLeft=%#x cbDma=%#x cbDmaTotal=%#x offCurBdle=%#x idxCurBdle=%#x (%p/%u)\n",
1981 cbLeft, cbDma, pStreamShared->State.cbDmaTotal, pStreamShared->State.offCurBdle,
1982 pStreamShared->State.idxCurBdle, pStreamShared, pStreamShared->u8SD));
1983
1984 /* Next buffer. */
1985 bool fAdvanced = hdaStreamDoDmaMaybeCompleteBuffer(pDevIns, pThis, pStreamShared, "hdaStreamDoOnAccessDmaOutput");
1986 AssertMsgStmt(fMustAdvanceBuffer == fAdvanced, ("%d %d\n", fMustAdvanceBuffer, fAdvanced), rc = VERR_INTERNAL_ERROR_3);
1987 } while (cbLeft > 0);
1988
1989 /*
1990 * Advance LPIB.
1991 */
1992 hdaStreamSetPositionAdd(pStreamShared, pDevIns, pThis, cbToTransfer - cbLeft);
1993 }
1994
1995# ifdef VBOX_STRICT
1996 uint32_t idxSched = pStreamShared->State.idxSchedule;
1997 AssertStmt(idxSched < RT_MIN(RT_ELEMENTS(pStreamShared->State.aSchedule), pStreamShared->State.cSchedule), idxSched = 0);
1998 uint32_t const cbPeriod = pStreamShared->State.aSchedule[idxSched].cbPeriod;
1999 AssertMsg(pStreamShared->State.cbDmaTotal < cbPeriod, ("%#x vs %#x\n", pStreamShared->State.cbDmaTotal, cbPeriod));
2000# endif
2001
2002 STAM_REL_COUNTER_INC(&pThis->StatAccessDmaOutput);
2003 return rc;
2004}
2005#endif /* VBOX_HDA_WITH_ON_REG_ACCESS_DMA */
2006
2007
2008#ifdef IN_RING3
2009
2010/**
2011 * Output streams: Pushes data to the mixer.
2012 *
2013 * @param pStreamShared HDA stream to update (shared bits).
2014 * @param pStreamR3 HDA stream to update (ring-3 bits).
2015 * @param pSink The mixer sink to push to.
2016 * @param nsNow The current RTTimeNanoTS() value.
2017 */
2018static void hdaR3StreamPushToMixer(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, PAUDMIXSINK pSink, uint64_t nsNow)
2019{
2020# ifdef LOG_ENABLED
2021 uint64_t const offReadOld = pStreamShared->State.offRead;
2022# endif
2023 pStreamShared->State.offRead = AudioMixerSinkTransferFromCircBuf(pSink,
2024 pStreamR3->State.pCircBuf,
2025 pStreamShared->State.offRead,
2026 pStreamR3->u8SD,
2027 pStreamR3->Dbg.Runtime.fEnabled
2028 ? pStreamR3->Dbg.Runtime.pFileStream : NULL);
2029
2030 Assert(nsNow >= pStreamShared->State.tsLastReadNs);
2031 Log3Func(("[SD%RU8] nsDeltaLastRead=%RI64 transferred=%#RX64 bytes -> @%#RX64\n", pStreamR3->u8SD,
2032 nsNow - pStreamShared->State.tsLastReadNs, pStreamShared->State.offRead - offReadOld, pStreamShared->State.offRead));
2033 RT_NOREF(pStreamShared, nsNow);
2034
2035 /* Update buffer stats. */
2036 pStreamR3->State.StatDmaBufUsed = (uint32_t)RTCircBufUsed(pStreamR3->State.pCircBuf);
2037}
2038
2039
2040/**
2041 * Deals with a DMA buffer overrun.
2042 *
2043 * Makes sure we return with @a cbNeeded bytes of free space in pCircBuf.
2044 *
2045 * @returns Number of bytes free in the internal DMA buffer.
2046 * @param pStreamShared The shared data for the HDA stream.
2047 * @param pStreamR3 The ring-3 data for the HDA stream.
2048 * @param pSink The mixer sink (valid).
2049 * @param cbNeeded How much space we need (in bytes).
2050 * @param nsNow Current RTNanoTimeTS() timestamp.
2051 * @param cbStreamFree The current amount of free buffer space.
2052 * @param pszCaller The caller (for logging).
2053 */
2054static uint32_t hdaR3StreamHandleDmaBufferOverrun(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, PAUDMIXSINK pSink,
2055 uint32_t cbNeeded, uint64_t nsNow,
2056 const char *pszCaller, uint32_t const cbStreamFree)
2057{
2058 STAM_REL_COUNTER_INC(&pStreamR3->State.StatDmaFlowProblems);
2059 Log(("%s: Warning! Stream #%u has insufficient space free: %#x bytes, need %#x. Will try move data out of the buffer...\n",
2060 pszCaller, pStreamShared->u8SD, cbStreamFree, cbNeeded));
2061 RT_NOREF(pszCaller, cbStreamFree);
2062
2063 int rc = AudioMixerSinkTryLock(pSink);
2064 if (RT_SUCCESS(rc))
2065 {
2066 hdaR3StreamPushToMixer(pStreamShared, pStreamR3, pSink, nsNow);
2067 AudioMixerSinkUpdate(pSink, 0, 0);
2068 AudioMixerSinkUnlock(pSink);
2069 }
2070 else
2071 RTThreadYield();
2072
2073 uint32_t const cbRet = hdaR3StreamGetFree(pStreamR3);
2074 Log(("%s: Gained %u bytes.\n", pszCaller, cbRet - cbStreamFree));
2075 if (cbRet >= cbNeeded)
2076 return cbRet;
2077
2078 /*
2079 * Unable to make sufficient space. Drop the whole buffer content.
2080 *
2081 * This is needed in order to keep the device emulation running at a
2082 * constant rate, at the cost of losing valid (but too much) data.
2083 */
2084 STAM_REL_COUNTER_INC(&pStreamR3->State.StatDmaFlowErrors);
2085 LogRel2(("HDA: Warning: Hit stream #%RU8 overflow, dropping %u bytes of audio data (%s)\n",
2086 pStreamShared->u8SD, hdaR3StreamGetUsed(pStreamR3), pszCaller));
2087# ifdef HDA_STRICT
2088 AssertMsgFailed(("Hit stream #%RU8 overflow -- timing bug?\n", pStreamShared->u8SD));
2089# endif
2090/**
2091 *
2092 * @todo r=bird: I don't think RTCircBufReset is entirely safe w/o
2093 * owning the AIO lock. See the note in the documentation about it not being
2094 * multi-threading aware (safe). Wish I'd verified this code much earlier.
2095 * Sigh^3!
2096 *
2097 */
2098 RTCircBufReset(pStreamR3->State.pCircBuf);
2099 pStreamShared->State.offWrite = 0;
2100 pStreamShared->State.offRead = 0;
2101 return hdaR3StreamGetFree(pStreamR3);
2102}
2103
2104
2105# ifdef VBOX_HDA_WITH_ON_REG_ACCESS_DMA
2106/**
2107 * Flushes the DMA bounce buffer content to the internal DMA buffer.
2108 *
2109 * @param pStreamShared The shared data of the stream to have its DMA bounce
2110 * buffer flushed.
2111 * @param pStreamR3 The ring-3 stream data for same.
2112 */
2113static void hdaR3StreamFlushDmaBounceBufferOutput(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3)
2114{
2115 uint32_t cbDma = pStreamShared->State.cbDma;
2116 LogFlowFunc(("cbDma=%#x\n", cbDma));
2117 if (cbDma)
2118 {
2119 AssertReturnVoid(cbDma <= sizeof(pStreamShared->State.abDma));
2120 PRTCIRCBUF const pCircBuf = pStreamR3->State.pCircBuf;
2121 if (pCircBuf)
2122 {
2123 uint32_t offDma = 0;
2124 while (offDma < cbDma)
2125 {
2126 uint32_t const cbSrcLeft = cbDma - offDma;
2127
2128 /*
2129 * Grab a chunk of the internal DMA buffer.
2130 */
2131 void *pvBufDst = NULL;
2132 size_t cbBufDst = 0;
2133 RTCircBufAcquireWriteBlock(pCircBuf, cbSrcLeft, &pvBufDst, &cbBufDst);
2134 if (cbBufDst > 0)
2135 { /* likely */ }
2136 else
2137 {
2138 /* We've got buffering trouble. */
2139 RTCircBufReleaseWriteBlock(pCircBuf, 0);
2140
2141 PAUDMIXSINK pSink = pStreamR3->pMixSink ? pStreamR3->pMixSink->pMixSink : NULL;
2142 if (pSink)
2143 hdaR3StreamHandleDmaBufferOverrun(pStreamShared, pStreamR3, pSink, cbSrcLeft, RTTimeNanoTS(),
2144 "hdaR3StreamFlushDmaBounceBufferOutput", 0 /*cbStreamFree*/);
2145 else
2146 {
2147 LogFunc(("Stream #%u has no sink. Dropping the rest of the data\n", pStreamR3->u8SD));
2148 break;
2149 }
2150
2151 RTCircBufAcquireWriteBlock(pCircBuf, cbSrcLeft, &pvBufDst, &cbBufDst);
2152 AssertBreakStmt(cbBufDst, RTCircBufReleaseWriteBlock(pCircBuf, 0));
2153 }
2154
2155 /*
2156 * Copy the samples into it and write it to the debug file if open.
2157 *
2158 * We do not fire the dtrace probe here nor update offRead as that was
2159 * done already (not sure that was a good idea?).
2160 */
2161 memcpy(pvBufDst, &pStreamShared->State.abDma[offDma], cbBufDst);
2162
2163 if (RT_LIKELY(!pStreamR3->Dbg.Runtime.fEnabled))
2164 { /* likely */ }
2165 else
2166 AudioHlpFileWrite(pStreamR3->Dbg.Runtime.pFileDMARaw, pvBufDst, cbBufDst, 0 /* fFlags */);
2167
2168 RTCircBufReleaseWriteBlock(pCircBuf, cbBufDst);
2169
2170 offDma += (uint32_t)cbBufDst;
2171 }
2172 }
2173
2174 /*
2175 * Mark the buffer empty.
2176 */
2177 pStreamShared->State.cbDma = 0;
2178 }
2179}
2180# endif /* VBOX_HDA_WITH_ON_REG_ACCESS_DMA */
2181
2182
2183/**
2184 * The stream's main function when called by the timer.
2185 *
2186 * @note This function also will be called without timer invocation when
2187 * starting (enabling) the stream to minimize startup latency.
2188 *
2189 * @returns Current timer time if the timer is enabled, otherwise zero.
2190 * @param pDevIns The device instance.
2191 * @param pThis The shared HDA device state.
2192 * @param pThisCC The ring-3 HDA device state.
2193 * @param pStreamShared HDA stream to update (shared bits).
2194 * @param pStreamR3 HDA stream to update (ring-3 bits).
2195 */
2196uint64_t hdaR3StreamTimerMain(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTATER3 pThisCC,
2197 PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3)
2198{
2199 Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
2200 Assert(PDMDevHlpTimerIsLockOwner(pDevIns, pStreamShared->hTimer));
2201
2202 /* Do the work: */
2203 hdaR3StreamUpdateDma(pDevIns, pThis, pThisCC, pStreamShared, pStreamR3);
2204
2205 /* Re-arm the timer if the sink is still active: */
2206 if ( pStreamShared->State.fRunning
2207 && pStreamR3->pMixSink
2208 && AudioMixerSinkIsActive(pStreamR3->pMixSink->pMixSink))
2209 {
2210 /* Advance the schduling: */
2211 uint32_t idxSched = pStreamShared->State.idxSchedule;
2212 AssertStmt(idxSched < RT_ELEMENTS(pStreamShared->State.aSchedule), idxSched = 0);
2213 uint32_t idxLoop = pStreamShared->State.idxScheduleLoop + 1;
2214 if (idxLoop >= pStreamShared->State.aSchedule[idxSched].cLoops)
2215 {
2216 idxSched += 1;
2217 if ( idxSched >= pStreamShared->State.cSchedule
2218 || idxSched >= RT_ELEMENTS(pStreamShared->State.aSchedule) /*paranoia^2*/)
2219 {
2220 idxSched = pStreamShared->State.cSchedulePrologue;
2221 AssertStmt(idxSched < RT_ELEMENTS(pStreamShared->State.aSchedule), idxSched = 0);
2222 }
2223 pStreamShared->State.idxSchedule = idxSched;
2224 idxLoop = 0;
2225 }
2226 pStreamShared->State.idxScheduleLoop = (uint16_t)idxLoop;
2227
2228 /* Do the actual timer re-arming. */
2229 uint64_t const tsNow = PDMDevHlpTimerGet(pDevIns, pStreamShared->hTimer); /* (For virtual sync this remains the same for the whole callout IIRC) */
2230 uint64_t const tsTransferNext = tsNow + pStreamShared->State.aSchedule[idxSched].cPeriodTicks;
2231 Log3Func(("[SD%RU8] fSinkActive=true, tsTransferNext=%RU64 (in %RU64)\n",
2232 pStreamShared->u8SD, tsTransferNext, tsTransferNext - tsNow));
2233 int rc = PDMDevHlpTimerSet(pDevIns, pStreamShared->hTimer, tsTransferNext);
2234 AssertRC(rc);
2235
2236 /* Some legacy stuff: */
2237 pStreamShared->State.tsTransferNext = tsTransferNext;
2238 pStreamShared->State.cbTransferSize = pStreamShared->State.aSchedule[idxSched].cbPeriod;
2239
2240 return tsNow;
2241 }
2242
2243 Log3Func(("[SD%RU8] fSinkActive=false\n", pStreamShared->u8SD));
2244 return 0;
2245}
2246
2247
2248/**
2249 * Updates a HDA stream by doing DMA transfers.
2250 *
2251 * Will do mixer transfers too to try fix an overrun/underrun situation.
2252 *
2253 * The host sink(s) set the overall pace (bird: no it doesn't, the DMA timer
2254 * does - we just hope like heck it matches the speed at which the *backend*
2255 * host audio driver processes samples).
2256 *
2257 * @param pDevIns The device instance.
2258 * @param pThis The shared HDA device state.
2259 * @param pThisCC The ring-3 HDA device state.
2260 * @param pStreamShared HDA stream to update (shared bits).
2261 * @param pStreamR3 HDA stream to update (ring-3 bits).
2262 */
2263static void hdaR3StreamUpdateDma(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTATER3 pThisCC,
2264 PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3)
2265{
2266 RT_NOREF(pThisCC);
2267 int rc2;
2268
2269 /*
2270 * Make sure we're running and got an active mixer sink.
2271 */
2272 if (RT_LIKELY(pStreamShared->State.fRunning))
2273 { /* likely */ }
2274 else
2275 return;
2276
2277 PAUDMIXSINK pSink = NULL;
2278 if (pStreamR3->pMixSink)
2279 pSink = pStreamR3->pMixSink->pMixSink;
2280 if (RT_LIKELY(AudioMixerSinkIsActive(pSink)))
2281 { /* likely */ }
2282 else
2283 return;
2284
2285 /*
2286 * Get scheduling info common to both input and output streams.
2287 */
2288 const uint64_t tsNowNs = RTTimeNanoTS();
2289 uint32_t idxSched = pStreamShared->State.idxSchedule;
2290 AssertStmt(idxSched < RT_MIN(RT_ELEMENTS(pStreamShared->State.aSchedule), pStreamShared->State.cSchedule), idxSched = 0);
2291 uint32_t cbPeriod = pStreamShared->State.aSchedule[idxSched].cbPeriod;
2292
2293 /*
2294 * Output streams (SDO).
2295 */
2296 if (hdaGetDirFromSD(pStreamShared->u8SD) == PDMAUDIODIR_OUT)
2297 {
2298# ifdef VBOX_HDA_WITH_ON_REG_ACCESS_DMA
2299 /* Subtract already transferred bytes and flush the DMA bounce buffer. */
2300 uint32_t cbDmaTotal = pStreamShared->State.cbDmaTotal;
2301 if (cbDmaTotal > 0)
2302 {
2303 AssertStmt(cbDmaTotal < cbPeriod, cbDmaTotal = cbPeriod);
2304 cbPeriod -= cbDmaTotal;
2305 pStreamShared->State.cbDmaTotal = 0;
2306 hdaR3StreamFlushDmaBounceBufferOutput(pStreamShared, pStreamR3);
2307 }
2308 else
2309 Assert(pStreamShared->State.cbDma == 0);
2310# endif
2311
2312 /*
2313 * Check how much room we have in our DMA buffer. There should be at
2314 * least one period worth of space there or we're in an overflow situation.
2315 */
2316 uint32_t cbStreamFree = hdaR3StreamGetFree(pStreamR3);
2317 if (cbStreamFree >= cbPeriod)
2318 { /* likely */ }
2319 else
2320 cbStreamFree = hdaR3StreamHandleDmaBufferOverrun(pStreamShared, pStreamR3, pSink, cbPeriod, tsNowNs,
2321 "hdaR3StreamUpdateDma", cbStreamFree);
2322
2323 /*
2324 * Do the DMA transfer.
2325 */
2326 uint64_t const offWriteBefore = pStreamShared->State.offWrite;
2327 hdaR3StreamDoDmaOutput(pDevIns, pThis, pStreamShared, pStreamR3, RT_MIN(cbStreamFree, cbPeriod), tsNowNs);
2328
2329 /*
2330 * Should we push data to down thru the mixer to and to the host drivers?
2331 *
2332 * We initially delay this by pThis->msInitialDelay, but after than we'll
2333 * kick the AIO thread every time we've put more data in the buffer (which is
2334 * every time) as the host audio device needs to get data in a timely manner.
2335 *
2336 * (We used to try only wake up the AIO thread according to pThis->uIoTimer
2337 * and host wall clock, but that meant we would miss a wakup after the DMA
2338 * timer was called a little late or if TM entered into catch-up mode.)
2339 */
2340 bool fKickAioThread;
2341 if (!pStreamShared->State.tsAioDelayEnd)
2342 fKickAioThread = pStreamShared->State.offWrite > offWriteBefore
2343 || hdaR3StreamGetFree(pStreamR3) < pStreamShared->State.cbAvgTransfer * 2;
2344 else if (PDMDevHlpTimerGet(pDevIns, pStreamShared->hTimer) >= pStreamShared->State.tsAioDelayEnd)
2345 {
2346 Log3Func(("Initial delay done: Passed tsAioDelayEnd.\n"));
2347 pStreamShared->State.tsAioDelayEnd = 0;
2348 fKickAioThread = true;
2349 }
2350 else if (hdaR3StreamGetFree(pStreamR3) < pStreamShared->State.cbAvgTransfer * 2)
2351 {
2352 Log3Func(("Initial delay done: Passed running short on buffer.\n"));
2353 pStreamShared->State.tsAioDelayEnd = 0;
2354 fKickAioThread = true;
2355 }
2356 else
2357 {
2358 Log3Func(("Initial delay pending...\n"));
2359 fKickAioThread = false;
2360 }
2361
2362 Log3Func(("msDelta=%RU64 (vs %u) cbStreamFree=%#x (vs %#x) => fKickAioThread=%RTbool\n",
2363 (tsNowNs - pStreamShared->State.tsLastReadNs) / RT_NS_1MS,
2364 pStreamShared->State.Cfg.Device.cMsSchedulingHint, cbStreamFree,
2365 pStreamShared->State.cbAvgTransfer * 2, fKickAioThread));
2366
2367 if (fKickAioThread)
2368 {
2369 /* Notify the async I/O worker thread that there's work to do. */
2370 Log5Func(("Notifying AIO thread\n"));
2371 rc2 = AudioMixerSinkSignalUpdateJob(pSink);
2372 AssertRC(rc2);
2373 /* Update last read timestamp for logging/debugging. */
2374 pStreamShared->State.tsLastReadNs = tsNowNs;
2375 }
2376 }
2377 /*
2378 * Input stream (SDI).
2379 */
2380 else
2381 {
2382 Assert(hdaGetDirFromSD(pStreamShared->u8SD) == PDMAUDIODIR_IN);
2383
2384 /*
2385 * See how much data we've got buffered...
2386 */
2387 bool fWriteSilence = false;
2388 uint32_t cbStreamUsed = hdaR3StreamGetUsed(pStreamR3);
2389 if (pStreamShared->State.fInputPreBuffered && cbStreamUsed >= cbPeriod)
2390 { /*likely*/ }
2391 /*
2392 * Because it may take a while for the input stream to get going (at
2393 * least with pulseaudio), we feed the guest silence till we've
2394 * pre-buffer a reasonable amount of audio.
2395 */
2396 else if (!pStreamShared->State.fInputPreBuffered)
2397 {
2398 if (cbStreamUsed < pStreamShared->State.cbInputPreBuffer)
2399 {
2400 Log3(("hdaR3StreamUpdateDma: Pre-buffering (got %#x out of %#x bytes)...\n",
2401 cbStreamUsed, pStreamShared->State.cbInputPreBuffer));
2402 fWriteSilence = true;
2403 }
2404 else
2405 {
2406 Log3(("hdaR3StreamUpdateDma: Completed pre-buffering (got %#x, needed %#x bytes).\n",
2407 cbStreamUsed, pStreamShared->State.cbInputPreBuffer));
2408 pStreamShared->State.fInputPreBuffered = true;
2409 fWriteSilence = true; /* For now, just do the most conservative thing. */
2410 }
2411 cbStreamUsed = cbPeriod;
2412 }
2413 /*
2414 * When we're low on data, we must really try fetch some ourselves
2415 * as buffer underruns must not happen.
2416 */
2417 else
2418 {
2419 /** @todo We're ending up here to frequently with pulse audio at least (just
2420 * watch the stream stats in the statistcs viewer, and way to often we
2421 * have to inject silence bytes. I suspect part of the problem is
2422 * that the HDA device require a much better latency than what the
2423 * pulse audio is configured for by default (10 ms vs 150ms). */
2424 STAM_REL_COUNTER_INC(&pStreamR3->State.StatDmaFlowProblems);
2425 Log(("hdaR3StreamUpdateDma: Warning! Stream #%u has insufficient data available: %u bytes, need %u. Will try move pull more data into the buffer...\n",
2426 pStreamShared->u8SD, cbStreamUsed, cbPeriod));
2427 int rc = AudioMixerSinkTryLock(pSink);
2428 if (RT_SUCCESS(rc))
2429 {
2430 AudioMixerSinkUpdate(pSink, cbStreamUsed, cbPeriod);
2431 hdaR3StreamPullFromMixer(pStreamShared, pStreamR3, pSink);
2432 AudioMixerSinkUnlock(pSink);
2433 }
2434 else
2435 RTThreadYield();
2436 Log(("hdaR3StreamUpdateDma: Gained %u bytes.\n", hdaR3StreamGetUsed(pStreamR3) - cbStreamUsed));
2437 cbStreamUsed = hdaR3StreamGetUsed(pStreamR3);
2438 if (cbStreamUsed < cbPeriod)
2439 {
2440 /* Unable to find sufficient input data by simple prodding.
2441 In order to keep a constant byte stream following thru the DMA
2442 engine into the guest, we will try again and then fall back on
2443 filling the gap with silence. */
2444 uint32_t cbSilence = 0;
2445 do
2446 {
2447 AudioMixerSinkLock(pSink);
2448
2449 cbStreamUsed = hdaR3StreamGetUsed(pStreamR3);
2450 if (cbStreamUsed < cbPeriod)
2451 {
2452 hdaR3StreamPullFromMixer(pStreamShared, pStreamR3, pSink);
2453 cbStreamUsed = hdaR3StreamGetUsed(pStreamR3);
2454 while (cbStreamUsed < cbPeriod)
2455 {
2456 void *pvDstBuf;
2457 size_t cbDstBuf;
2458 RTCircBufAcquireWriteBlock(pStreamR3->State.pCircBuf, cbPeriod - cbStreamUsed,
2459 &pvDstBuf, &cbDstBuf);
2460 RT_BZERO(pvDstBuf, cbDstBuf);
2461 RTCircBufReleaseWriteBlock(pStreamR3->State.pCircBuf, cbDstBuf);
2462 cbSilence += (uint32_t)cbDstBuf;
2463 cbStreamUsed += (uint32_t)cbDstBuf;
2464 }
2465 }
2466
2467 AudioMixerSinkUnlock(pSink);
2468 } while (cbStreamUsed < cbPeriod);
2469 if (cbSilence > 0)
2470 {
2471 STAM_REL_COUNTER_INC(&pStreamR3->State.StatDmaFlowErrors);
2472 STAM_REL_COUNTER_ADD(&pStreamR3->State.StatDmaFlowErrorBytes, cbSilence);
2473 LogRel2(("HDA: Warning: Stream #%RU8 underrun, added %u bytes of silence (%u us)\n", pStreamShared->u8SD,
2474 cbSilence, PDMAudioPropsBytesToMicro(&pStreamShared->State.Cfg.Props, cbSilence)));
2475 }
2476 }
2477 }
2478
2479 /*
2480 * Do the DMA'ing.
2481 */
2482 if (cbStreamUsed)
2483 hdaR3StreamDoDmaInput(pDevIns, pThis, pStreamShared, pStreamR3,
2484 RT_MIN(cbStreamUsed, cbPeriod), fWriteSilence, tsNowNs);
2485
2486 /*
2487 * We should always kick the AIO thread.
2488 */
2489 /** @todo This isn't entirely ideal. If we get into an underrun situation,
2490 * we ideally want the AIO thread to run right before the DMA timer
2491 * rather than right after it ran. */
2492 Log5Func(("Notifying AIO thread\n"));
2493 rc2 = AudioMixerSinkSignalUpdateJob(pSink);
2494 AssertRC(rc2);
2495 pStreamShared->State.tsLastReadNs = tsNowNs;
2496 }
2497}
2498
2499
2500/**
2501 * @callback_method_impl{FNAUDMIXSINKUPDATE}
2502 *
2503 * For output streams this moves data from the internal DMA buffer (in which
2504 * hdaR3StreamUpdateDma put it), thru the mixer and to the various backend audio
2505 * devices.
2506 *
2507 * For input streams this pulls data from the backend audio device(s), thru the
2508 * mixer and puts it in the internal DMA buffer ready for hdaR3StreamUpdateDma
2509 * to pump into guest memory.
2510 */
2511DECLCALLBACK(void) hdaR3StreamUpdateAsyncIoJob(PPDMDEVINS pDevIns, PAUDMIXSINK pSink, void *pvUser)
2512{
2513 PHDASTATE const pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
2514 PHDASTATER3 const pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PHDASTATER3);
2515 PHDASTREAMR3 const pStreamR3 = (PHDASTREAMR3)pvUser;
2516 PHDASTREAM const pStreamShared = &pThis->aStreams[pStreamR3 - &pThisCC->aStreams[0]];
2517 Assert(pStreamR3 - &pThisCC->aStreams[0] == pStreamR3->u8SD);
2518 Assert(pStreamShared->u8SD == pStreamR3->u8SD);
2519 RT_NOREF(pSink);
2520
2521 /*
2522 * Make sure we haven't change sink and that it's still active (it
2523 * should be or we wouldn't have been called).
2524 */
2525 AssertReturnVoid(pStreamR3->pMixSink && pSink == pStreamR3->pMixSink->pMixSink);
2526 AssertReturnVoid(AudioMixerSinkIsActive(pSink));
2527
2528 /*
2529 * Output streams (SDO).
2530 */
2531 if (hdaGetDirFromSD(pStreamShared->u8SD) == PDMAUDIODIR_OUT)
2532 hdaR3StreamPushToMixer(pStreamShared, pStreamR3, pSink, RTTimeNanoTS());
2533 /*
2534 * Input stream (SDI).
2535 */
2536 else
2537 {
2538 Assert(hdaGetDirFromSD(pStreamShared->u8SD) == PDMAUDIODIR_IN);
2539 hdaR3StreamPullFromMixer(pStreamShared, pStreamR3, pSink);
2540 }
2541}
2542
2543
2544# if 0 /* unused - no prototype even */
2545/**
2546 * Updates an HDA stream's current read or write buffer position (depending on the stream type) by
2547 * updating its associated LPIB register and DMA position buffer (if enabled).
2548 *
2549 * @returns Set LPIB value.
2550 * @param pDevIns The device instance.
2551 * @param pStream HDA stream to update read / write position for.
2552 * @param u32LPIB New LPIB (position) value to set.
2553 */
2554uint32_t hdaR3StreamUpdateLPIB(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared, uint32_t u32LPIB)
2555{
2556 AssertMsg(u32LPIB <= pStreamShared->u32CBL,
2557 ("[SD%RU8] New LPIB (%RU32) exceeds CBL (%RU32)\n", pStreamShared->u8SD, u32LPIB, pStreamShared->u32CBL));
2558
2559 u32LPIB = RT_MIN(u32LPIB, pStreamShared->u32CBL);
2560
2561 LogFlowFunc(("[SD%RU8] LPIB=%RU32 (DMA Position Buffer Enabled: %RTbool)\n",
2562 pStreamShared->u8SD, u32LPIB, pThis->fDMAPosition));
2563
2564 /* Update LPIB in any case. */
2565 HDA_STREAM_REG(pThis, LPIB, pStreamShared->u8SD) = u32LPIB;
2566
2567 /* Do we need to tell the current DMA position? */
2568 if (pThis->fDMAPosition)
2569 {
2570 int rc2 = PDMDevHlpPCIPhysWrite(pDevIns,
2571 pThis->u64DPBase + (pStreamShared->u8SD * 2 * sizeof(uint32_t)),
2572 (void *)&u32LPIB, sizeof(uint32_t));
2573 AssertRC(rc2);
2574 }
2575
2576 return u32LPIB;
2577}
2578# endif
2579
2580# ifdef HDA_USE_DMA_ACCESS_HANDLER
2581/**
2582 * Registers access handlers for a stream's BDLE DMA accesses.
2583 *
2584 * @returns true if registration was successful, false if not.
2585 * @param pStream HDA stream to register BDLE access handlers for.
2586 */
2587bool hdaR3StreamRegisterDMAHandlers(PHDASTREAM pStream)
2588{
2589 /* At least LVI and the BDL base must be set. */
2590 if ( !pStreamShared->u16LVI
2591 || !pStreamShared->u64BDLBase)
2592 {
2593 return false;
2594 }
2595
2596 hdaR3StreamUnregisterDMAHandlers(pStream);
2597
2598 LogFunc(("Registering ...\n"));
2599
2600 int rc = VINF_SUCCESS;
2601
2602 /*
2603 * Create BDLE ranges.
2604 */
2605
2606 struct BDLERANGE
2607 {
2608 RTGCPHYS uAddr;
2609 uint32_t uSize;
2610 } arrRanges[16]; /** @todo Use a define. */
2611
2612 size_t cRanges = 0;
2613
2614 for (uint16_t i = 0; i < pStreamShared->u16LVI + 1; i++)
2615 {
2616 HDABDLE BDLE;
2617 rc = hdaR3BDLEFetch(pDevIns, &BDLE, pStreamShared->u64BDLBase, i /* Index */);
2618 if (RT_FAILURE(rc))
2619 break;
2620
2621 bool fAddRange = true;
2622 BDLERANGE *pRange;
2623
2624 if (cRanges)
2625 {
2626 pRange = &arrRanges[cRanges - 1];
2627
2628 /* Is the current range a direct neighbor of the current BLDE? */
2629 if ((pRange->uAddr + pRange->uSize) == BDLE.Desc.u64BufAddr)
2630 {
2631 /* Expand the current range by the current BDLE's size. */
2632 pRange->uSize += BDLE.Desc.u32BufSize;
2633
2634 /* Adding a new range in this case is not needed anymore. */
2635 fAddRange = false;
2636
2637 LogFunc(("Expanding range %zu by %RU32 (%RU32 total now)\n", cRanges - 1, BDLE.Desc.u32BufSize, pRange->uSize));
2638 }
2639 }
2640
2641 /* Do we need to add a new range? */
2642 if ( fAddRange
2643 && cRanges < RT_ELEMENTS(arrRanges))
2644 {
2645 pRange = &arrRanges[cRanges];
2646
2647 pRange->uAddr = BDLE.Desc.u64BufAddr;
2648 pRange->uSize = BDLE.Desc.u32BufSize;
2649
2650 LogFunc(("Adding range %zu - 0x%x (%RU32)\n", cRanges, pRange->uAddr, pRange->uSize));
2651
2652 cRanges++;
2653 }
2654 }
2655
2656 LogFunc(("%zu ranges total\n", cRanges));
2657
2658 /*
2659 * Register all ranges as DMA access handlers.
2660 */
2661
2662 for (size_t i = 0; i < cRanges; i++)
2663 {
2664 BDLERANGE *pRange = &arrRanges[i];
2665
2666 PHDADMAACCESSHANDLER pHandler = (PHDADMAACCESSHANDLER)RTMemAllocZ(sizeof(HDADMAACCESSHANDLER));
2667 if (!pHandler)
2668 {
2669 rc = VERR_NO_MEMORY;
2670 break;
2671 }
2672
2673 RTListAppend(&pStream->State.lstDMAHandlers, &pHandler->Node);
2674
2675 pHandler->pStream = pStream; /* Save a back reference to the owner. */
2676
2677 char szDesc[32];
2678 RTStrPrintf(szDesc, sizeof(szDesc), "HDA[SD%RU8 - RANGE%02zu]", pStream->u8SD, i);
2679
2680 int rc2 = PGMR3HandlerPhysicalTypeRegister(PDMDevHlpGetVM(pStream->pHDAState->pDevInsR3), PGMPHYSHANDLERKIND_WRITE,
2681 hdaDMAAccessHandler,
2682 NULL, NULL, NULL,
2683 NULL, NULL, NULL,
2684 szDesc, &pHandler->hAccessHandlerType);
2685 AssertRCBreak(rc2);
2686
2687 pHandler->BDLEAddr = pRange->uAddr;
2688 pHandler->BDLESize = pRange->uSize;
2689
2690 /* Get first and last pages of the BDLE range. */
2691 RTGCPHYS pgFirst = pRange->uAddr & ~PAGE_OFFSET_MASK;
2692 RTGCPHYS pgLast = RT_ALIGN(pgFirst + pRange->uSize, PAGE_SIZE);
2693
2694 /* Calculate the region size (in pages). */
2695 RTGCPHYS regionSize = RT_ALIGN(pgLast - pgFirst, PAGE_SIZE);
2696
2697 pHandler->GCPhysFirst = pgFirst;
2698 pHandler->GCPhysLast = pHandler->GCPhysFirst + (regionSize - 1);
2699
2700 LogFunc((" Registering region '%s': %#RGp - %#RGp (region size: %#zx)\n",
2701 szDesc, pHandler->GCPhysFirst, pHandler->GCPhysLast, regionSize));
2702 LogFunc((" BDLE @ %#RGp - %#RGp (%#RX32)\n",
2703 pHandler->BDLEAddr, pHandler->BDLEAddr + pHandler->BDLESize, pHandler->BDLESize));
2704
2705 rc2 = PGMHandlerPhysicalRegister(PDMDevHlpGetVM(pStream->pHDAState->pDevInsR3),
2706 pHandler->GCPhysFirst, pHandler->GCPhysLast,
2707 pHandler->hAccessHandlerType, pHandler, NIL_RTR0PTR, NIL_RTRCPTR,
2708 szDesc);
2709 AssertRCBreak(rc2);
2710
2711 pHandler->fRegistered = true;
2712 }
2713
2714 LogFunc(("Registration ended with rc=%Rrc\n", rc));
2715
2716 return RT_SUCCESS(rc);
2717}
2718
2719/**
2720 * Unregisters access handlers of a stream's BDLEs.
2721 *
2722 * @param pStream HDA stream to unregister BDLE access handlers for.
2723 */
2724void hdaR3StreamUnregisterDMAHandlers(PHDASTREAM pStream)
2725{
2726 LogFunc(("\n"));
2727
2728 PHDADMAACCESSHANDLER pHandler, pHandlerNext;
2729 RTListForEachSafe(&pStream->State.lstDMAHandlers, pHandler, pHandlerNext, HDADMAACCESSHANDLER, Node)
2730 {
2731 if (!pHandler->fRegistered) /* Handler not registered? Skip. */
2732 continue;
2733
2734 LogFunc(("Unregistering 0x%x - 0x%x (%zu)\n",
2735 pHandler->GCPhysFirst, pHandler->GCPhysLast, pHandler->GCPhysLast - pHandler->GCPhysFirst));
2736
2737 int rc2 = PGMHandlerPhysicalDeregister(PDMDevHlpGetVM(pStream->pHDAState->pDevInsR3),
2738 pHandler->GCPhysFirst);
2739 AssertRC(rc2);
2740
2741 RTListNodeRemove(&pHandler->Node);
2742
2743 RTMemFree(pHandler);
2744 pHandler = NULL;
2745 }
2746
2747 Assert(RTListIsEmpty(&pStream->State.lstDMAHandlers));
2748}
2749
2750# endif /* HDA_USE_DMA_ACCESS_HANDLER */
2751
2752#endif /* IN_RING3 */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette