VirtualBox

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

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

DevHda: Removed the stream mapping and leave such complications to the mixer buffer. (Still some cleanup to be done here.) bugref:9890

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