VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/HDAStream.cpp@ 68253

Last change on this file since 68253 was 67899, checked in by vboxsync, 7 years ago

Audio/DevHDA.cpp: Modularized code more by also factoring out the HDA stream functions into an own file. No actual code changes.

  • Property svn:executable set to *
File size: 42.8 KB
Line 
1/* $Id$ */
2/** @file
3 * HDAStream.cpp - Stream functions for HD Audio.
4 */
5
6/*
7 * Copyright (C) 2017 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* Header Files *
20*********************************************************************************************************************************/
21#define LOG_GROUP LOG_GROUP_DEV_HDA
22#include <VBox/log.h>
23
24#include <iprt/mem.h>
25#include <iprt/semaphore.h>
26
27#include <VBox/vmm/pdmdev.h>
28#include <VBox/vmm/pdmaudioifs.h>
29
30#include "DrvAudio.h"
31
32#include "DevHDA.h"
33#include "HDAStream.h"
34
35
36#ifdef IN_RING3
37
38/**
39 * Creates an HDA stream.
40 *
41 * @returns IPRT status code.
42 * @param pStream HDA stream to create.
43 * @param pThis HDA state to assign the HDA stream to.
44 */
45int hdaStreamCreate(PHDASTREAM pStream, PHDASTATE pThis)
46{
47 RT_NOREF(pThis);
48 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
49
50 pStream->u8SD = UINT8_MAX;
51 pStream->pMixSink = NULL;
52 pStream->pHDAState = pThis;
53
54 pStream->State.fInReset = false;
55#ifdef HDA_USE_DMA_ACCESS_HANDLER
56 RTListInit(&pStream->State.lstDMAHandlers);
57#endif
58
59 int rc = RTCircBufCreate(&pStream->State.pCircBuf, _64K); /** @todo Make this configurable. */
60 if (RT_SUCCESS(rc))
61 {
62 rc = hdaStreamPeriodCreate(&pStream->State.Period);
63 if (RT_SUCCESS(rc))
64 rc = RTCritSectInit(&pStream->State.CritSect);
65 }
66
67#ifdef DEBUG
68 int rc2 = RTCritSectInit(&pStream->Dbg.CritSect);
69 AssertRC(rc2);
70#endif
71
72 return rc;
73}
74
75/**
76 * Destroys an HDA stream.
77 *
78 * @param pStream HDA stream to destroy.
79 */
80void hdaStreamDestroy(PHDASTREAM pStream)
81{
82 AssertPtrReturnVoid(pStream);
83
84 LogFlowFunc(("[SD%RU8]: Destroying ...\n", pStream->u8SD));
85
86 hdaStreamMapDestroy(&pStream->State.Mapping);
87
88 int rc2;
89
90#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
91 rc2 = hdaStreamAsyncIODestroy(pStream);
92 AssertRC(rc2);
93#else
94 RT_NOREF(pThis);
95#endif
96
97 rc2 = RTCritSectDelete(&pStream->State.CritSect);
98 AssertRC(rc2);
99
100 if (pStream->State.pCircBuf)
101 {
102 RTCircBufDestroy(pStream->State.pCircBuf);
103 pStream->State.pCircBuf = NULL;
104 }
105
106 hdaStreamPeriodDestroy(&pStream->State.Period);
107
108#ifdef DEBUG
109 rc2 = RTCritSectDelete(&pStream->Dbg.CritSect);
110 AssertRC(rc2);
111#endif
112
113 LogFlowFuncLeave();
114}
115
116/**
117 * Initializes an HDA stream.
118 *
119 * @returns IPRT status code.
120 * @param pStream HDA stream to initialize.
121 * @param uSD SD (stream descriptor) number to assign the HDA stream to.
122 */
123int hdaStreamInit(PHDASTREAM pStream, uint8_t uSD)
124{
125 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
126
127 PHDASTATE pThis = pStream->pHDAState;
128 AssertPtr(pThis);
129
130 pStream->u8SD = uSD;
131 pStream->u64BDLBase = RT_MAKE_U64(HDA_STREAM_REG(pThis, BDPL, pStream->u8SD),
132 HDA_STREAM_REG(pThis, BDPU, pStream->u8SD));
133 pStream->u16LVI = HDA_STREAM_REG(pThis, LVI, pStream->u8SD);
134 pStream->u32CBL = HDA_STREAM_REG(pThis, CBL, pStream->u8SD);
135 pStream->u16FIFOS = HDA_STREAM_REG(pThis, FIFOS, pStream->u8SD) + 1;
136
137 /* Make sure to also update the stream's DMA counter (based on its current LPIB value). */
138 hdaStreamUpdateLPIB(pStream, HDA_STREAM_REG(pThis, LPIB, pStream->u8SD));
139
140 PPDMAUDIOSTREAMCFG pCfg = &pStream->State.strmCfg;
141
142 int rc = hdaSDFMTToPCMProps(HDA_STREAM_REG(pThis, FMT, uSD), &pCfg->Props);
143 if (RT_FAILURE(rc))
144 {
145 LogRel(("HDA: Warning: Format 0x%x for stream #%RU8 not supported\n", HDA_STREAM_REG(pThis, FMT, uSD), uSD));
146 return rc;
147 }
148
149 /* Set the stream's direction. */
150 pCfg->enmDir = hdaGetDirFromSD(pStream->u8SD);
151
152 /* The the stream's name, based on the direction. */
153 switch (pCfg->enmDir)
154 {
155 case PDMAUDIODIR_IN:
156# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
157# error "Implement me!"
158# else
159 pCfg->DestSource.Source = PDMAUDIORECSOURCE_LINE;
160 pCfg->enmLayout = PDMAUDIOSTREAMLAYOUT_NON_INTERLEAVED;
161 RTStrCopy(pCfg->szName, sizeof(pCfg->szName), "Line In");
162# endif
163 break;
164
165 case PDMAUDIODIR_OUT:
166 /* Destination(s) will be set in hdaAddStreamOut(),
167 * based on the channels / stream layout. */
168 break;
169
170 default:
171 rc = VERR_NOT_SUPPORTED;
172 break;
173 }
174
175 /*
176 * Initialize the stream mapping in any case, regardless if
177 * we support surround audio or not. This is needed to handle
178 * the supported channels within a single audio stream, e.g. mono/stereo.
179 *
180 * In other words, the stream mapping *always* knows the real
181 * number of channels in a single audio stream.
182 */
183 rc = hdaStreamMapInit(&pStream->State.Mapping, &pCfg->Props);
184 AssertRCReturn(rc, rc);
185
186 LogFunc(("[SD%RU8] DMA @ 0x%x (%RU32 bytes), LVI=%RU16, FIFOS=%RU16, rc=%Rrc\n",
187 pStream->u8SD, pStream->u64BDLBase, pStream->u32CBL, pStream->u16LVI, pStream->u16FIFOS, rc));
188
189 return rc;
190}
191
192/**
193 * Resets an HDA stream.
194 *
195 * @param pThis HDA state.
196 * @param pStream HDA stream to reset.
197 * @param uSD Stream descriptor (SD) number to use for this stream.
198 */
199void hdaStreamReset(PHDASTATE pThis, PHDASTREAM pStream, uint8_t uSD)
200{
201 AssertPtrReturnVoid(pThis);
202 AssertPtrReturnVoid(pStream);
203 AssertReturnVoid(uSD < HDA_MAX_STREAMS);
204
205# ifdef VBOX_STRICT
206 AssertReleaseMsg(!RT_BOOL(HDA_STREAM_REG(pThis, CTL, uSD) & HDA_SDCTL_RUN),
207 ("[SD%RU8] Cannot reset stream while in running state\n", uSD));
208# endif
209
210 LogFunc(("[SD%RU8]: Reset\n", uSD));
211
212 /*
213 * Set reset state.
214 */
215 Assert(ASMAtomicReadBool(&pStream->State.fInReset) == false); /* No nested calls. */
216 ASMAtomicXchgBool(&pStream->State.fInReset, true);
217
218 /*
219 * Second, initialize the registers.
220 */
221 HDA_STREAM_REG(pThis, STS, uSD) = HDA_SDSTS_FIFORDY;
222 /* According to the ICH6 datasheet, 0x40000 is the default value for stream descriptor register 23:20
223 * bits are reserved for stream number 18.2.33, resets SDnCTL except SRST bit. */
224 HDA_STREAM_REG(pThis, CTL, uSD) = 0x40000 | (HDA_STREAM_REG(pThis, CTL, uSD) & HDA_SDCTL_SRST);
225 /* ICH6 defines default values (120 bytes for input and 192 bytes for output descriptors) of FIFO size. 18.2.39. */
226 HDA_STREAM_REG(pThis, FIFOS, uSD) = hdaGetDirFromSD(uSD) == PDMAUDIODIR_IN ? HDA_SDIFIFO_120B : HDA_SDOFIFO_192B;
227 /* See 18.2.38: Always defaults to 0x4 (32 bytes). */
228 HDA_STREAM_REG(pThis, FIFOW, uSD) = HDA_SDFIFOW_32B;
229 HDA_STREAM_REG(pThis, LPIB, uSD) = 0;
230 HDA_STREAM_REG(pThis, CBL, uSD) = 0;
231 HDA_STREAM_REG(pThis, LVI, uSD) = 0;
232 HDA_STREAM_REG(pThis, FMT, uSD) = 0;
233 HDA_STREAM_REG(pThis, BDPU, uSD) = 0;
234 HDA_STREAM_REG(pThis, BDPL, uSD) = 0;
235
236#ifdef HDA_USE_DMA_ACCESS_HANDLER
237 hdaStreamUnregisterDMAHandlers(pThis, pStream);
238#endif
239
240 RT_ZERO(pStream->State.BDLE);
241 pStream->State.uCurBDLE = 0;
242
243 if (pStream->State.pCircBuf)
244 RTCircBufReset(pStream->State.pCircBuf);
245
246 /* Reset stream map. */
247 hdaStreamMapReset(&pStream->State.Mapping);
248
249 /* (Re-)initialize the stream with current values. */
250 int rc2 = hdaStreamInit(pStream, uSD);
251 AssertRC(rc2);
252
253 /* Reset the stream's period. */
254 hdaStreamPeriodReset(&pStream->State.Period);
255
256#ifdef DEBUG
257 pStream->Dbg.cReadsTotal = 0;
258 pStream->Dbg.cbReadTotal = 0;
259 pStream->Dbg.tsLastReadNs = 0;
260 pStream->Dbg.cWritesTotal = 0;
261 pStream->Dbg.cbWrittenTotal = 0;
262 pStream->Dbg.cWritesHz = 0;
263 pStream->Dbg.cbWrittenHz = 0;
264 pStream->Dbg.tsWriteSlotBegin = 0;
265#endif
266
267 /* Report that we're done resetting this stream. */
268 HDA_STREAM_REG(pThis, CTL, uSD) = 0;
269
270 LogFunc(("[SD%RU8] Reset\n", uSD));
271
272 /* Exit reset mode. */
273 ASMAtomicXchgBool(&pStream->State.fInReset, false);
274}
275
276/**
277 * Enables or disables an HDA audio stream.
278 *
279 * @returns IPRT status code.
280 * @param pStream HDA stream to enable or disable.
281 * @param fEnable Whether to enable or disble the stream.
282 */
283int hdaStreamEnable(PHDASTREAM pStream, bool fEnable)
284{
285 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
286
287 LogFunc(("[SD%RU8]: fEnable=%RTbool, pMixSink=%p\n", pStream->u8SD, fEnable, pStream->pMixSink));
288
289 int rc = VINF_SUCCESS;
290
291 if (pStream->pMixSink) /* Stream attached to a sink? */
292 {
293 AUDMIXSINKCMD enmCmd = fEnable
294 ? AUDMIXSINKCMD_ENABLE : AUDMIXSINKCMD_DISABLE;
295
296 /* First, enable or disable the stream and the stream's sink, if any. */
297 if (pStream->pMixSink->pMixSink)
298 rc = AudioMixerSinkCtl(pStream->pMixSink->pMixSink, enmCmd);
299 }
300
301 LogFunc(("[SD%RU8] rc=%Rrc\n", pStream->u8SD, rc));
302 return rc;
303}
304
305/**
306 * Returns the number of outstanding stream data bytes which need to be processed
307 * by the DMA engine assigned to this stream.
308 *
309 * @return Number of bytes for the DMA engine to process.
310 */
311uint32_t hdaStreamGetTransferSize(PHDASTATE pThis, PHDASTREAM pStream)
312{
313 AssertPtrReturn(pThis, 0);
314 AssertPtrReturn(pStream, 0);
315
316 if (!RT_BOOL(HDA_STREAM_REG(pThis, CTL, pStream->u8SD) & HDA_SDCTL_RUN))
317 {
318 AssertFailed(); /* Should never happen. */
319 return 0;
320 }
321
322 /* Determine how much for the current BDL entry we have left to transfer. */
323 PHDABDLE pBDLE = &pStream->State.BDLE;
324 const uint32_t cbBDLE = RT_MIN(pBDLE->Desc.u32BufSize, pBDLE->Desc.u32BufSize - pBDLE->State.u32BufOff);
325
326 /* Determine how much we (still) can stuff in the stream's internal FIFO. */
327 const uint32_t cbCircBuf = (uint32_t)RTCircBufFree(pStream->State.pCircBuf);
328
329 uint32_t cbToTransfer = cbBDLE;
330
331 /* Make sure that we don't transfer more than our FIFO can hold at the moment.
332 * As the host sets the overall pace it needs to process some of the FIFO data first before
333 * we can issue a new DMA data transfer. */
334 if (cbToTransfer > cbCircBuf)
335 cbToTransfer = cbCircBuf;
336
337 Log3Func(("[SD%RU8] LPIB=%RU32 CBL=%RU32 cbCircBuf=%RU32, -> cbToTransfer=%RU32 %R[bdle]\n", pStream->u8SD,
338 HDA_STREAM_REG(pThis, LPIB, pStream->u8SD), pStream->u32CBL, cbCircBuf, cbToTransfer, pBDLE));
339 return cbToTransfer;
340}
341
342/**
343 * Increases the amount of transferred (audio) data of an HDA stream and
344 * reports this as needed to the guest.
345 *
346 * @param pStream HDA stream to increase amount for.
347 * @param cbInc Amount (in bytes) to increase.
348 */
349void hdaStreamTransferInc(PHDASTREAM pStream, uint32_t cbInc)
350{
351 AssertPtrReturnVoid(pStream);
352
353 if (!cbInc)
354 return;
355
356 const PHDASTATE pThis = pStream->pHDAState;
357
358 const uint32_t u32LPIB = HDA_STREAM_REG(pThis, LPIB, pStream->u8SD);
359
360 Log3Func(("[SD%RU8] %RU32 + %RU32 -> %RU32, CBL=%RU32\n",
361 pStream->u8SD, u32LPIB, cbInc, u32LPIB + cbInc, pStream->u32CBL));
362
363 hdaStreamUpdateLPIB(pStream, u32LPIB + cbInc);
364}
365
366/**
367 * Retrieves the available size of (buffered) audio data (in bytes) of a given HDA stream.
368 *
369 * @returns Available data (in bytes).
370 * @param pStream HDA stream to retrieve size for.
371 */
372uint32_t hdaStreamGetUsed(PHDASTREAM pStream)
373{
374 AssertPtrReturn(pStream, 0);
375
376 if (!pStream->State.pCircBuf)
377 return 0;
378
379 return (uint32_t)RTCircBufUsed(pStream->State.pCircBuf);
380}
381
382/**
383 * Retrieves the free size of audio data (in bytes) of a given HDA stream.
384 *
385 * @returns Free data (in bytes).
386 * @param pStream HDA stream to retrieve size for.
387 */
388uint32_t hdaStreamGetFree(PHDASTREAM pStream)
389{
390 AssertPtrReturn(pStream, 0);
391
392 if (!pStream->State.pCircBuf)
393 return 0;
394
395 return (uint32_t)RTCircBufFree(pStream->State.pCircBuf);
396}
397
398
399/**
400 * Writes audio data from a mixer sink into an HDA stream's DMA buffer.
401 *
402 * @returns IPRT status code.
403 * @param pStream HDA stream to write to.
404 * @param cbToWrite Number of bytes to write.
405 * @param pcbWritten Number of bytes written. Optional.
406 */
407int hdaStreamWrite(PHDASTREAM pStream, uint32_t cbToWrite, uint32_t *pcbWritten)
408{
409 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
410 AssertReturn(cbToWrite, VERR_INVALID_PARAMETER);
411 /* pcbWritten is optional. */
412
413 PHDAMIXERSINK pSink = pStream->pMixSink;
414 if (!pSink)
415 {
416 AssertMsgFailed(("[SD%RU8]: Can't write to a stream with no sink attached\n", pStream->u8SD));
417
418 if (pcbWritten)
419 *pcbWritten = 0;
420 return VINF_SUCCESS;
421 }
422
423 PRTCIRCBUF pCircBuf = pStream->State.pCircBuf;
424 AssertPtr(pCircBuf);
425
426 int rc = VINF_SUCCESS;
427
428 uint32_t cbWrittenTotal = 0;
429 uint32_t cbLeft = RT_MIN(cbToWrite, (uint32_t)RTCircBufFree(pCircBuf));
430
431 while (cbLeft)
432 {
433 void *pvDst;
434 size_t cbDst;
435
436 uint32_t cbRead = 0;
437
438 RTCircBufAcquireWriteBlock(pCircBuf, cbToWrite, &pvDst, &cbDst);
439
440 if (cbDst)
441 {
442 rc = AudioMixerSinkRead(pSink->pMixSink, AUDMIXOP_COPY, pvDst, (uint32_t)cbDst, &cbRead);
443 AssertRC(rc);
444
445 Assert(cbDst >= cbRead);
446 Log2Func(("[SD%RU8]: %zu/%zu bytes read\n", pStream->u8SD, cbRead, cbDst));
447
448#ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
449 RTFILE fh;
450 RTFileOpen(&fh, VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "hdaStreamWrite.pcm",
451 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
452 RTFileWrite(fh, pvDst, cbRead, NULL);
453 RTFileClose(fh);
454#endif
455 }
456
457 RTCircBufReleaseWriteBlock(pCircBuf, cbRead);
458
459 if (RT_FAILURE(rc))
460 break;
461
462 Assert(cbLeft >= cbRead);
463 cbLeft -= cbRead;
464
465 cbWrittenTotal += cbRead;
466 }
467
468 if (pcbWritten)
469 *pcbWritten = cbWrittenTotal;
470
471 return rc;
472}
473
474
475/**
476 * Reads audio data from an HDA stream's DMA buffer and writes into a specified mixer sink.
477 *
478 * @returns IPRT status code.
479 * @param pStream HDA stream to read audio data from.
480 * @param cbToRead Number of bytes to read.
481 * @param pcbRead Number of bytes read. Optional.
482 */
483int hdaStreamRead(PHDASTREAM pStream, uint32_t cbToRead, uint32_t *pcbRead)
484{
485 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
486 AssertReturn(cbToRead, VERR_INVALID_PARAMETER);
487 /* pcbWritten is optional. */
488
489 PHDAMIXERSINK pSink = pStream->pMixSink;
490 if (!pSink)
491 {
492 AssertMsgFailed(("[SD%RU8]: Can't read from a stream with no sink attached\n", pStream->u8SD));
493
494 if (pcbRead)
495 *pcbRead = 0;
496 return VINF_SUCCESS;
497 }
498
499 PRTCIRCBUF pCircBuf = pStream->State.pCircBuf;
500 AssertPtr(pCircBuf);
501
502 int rc = VINF_SUCCESS;
503
504 uint32_t cbReadTotal = 0;
505 uint32_t cbLeft = RT_MIN(cbToRead, (uint32_t)RTCircBufUsed(pCircBuf));
506
507 while (cbLeft)
508 {
509 void *pvSrc;
510 size_t cbSrc;
511
512 uint32_t cbWritten = 0;
513
514 RTCircBufAcquireReadBlock(pCircBuf, cbLeft, &pvSrc, &cbSrc);
515
516 if (cbSrc)
517 {
518#ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
519 RTFILE fh;
520 RTFileOpen(&fh, VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "hdaStreamRead.pcm",
521 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
522 RTFileWrite(fh, pvSrc, cbSrc, NULL);
523 RTFileClose(fh);
524#endif
525 rc = AudioMixerSinkWrite(pSink->pMixSink, AUDMIXOP_COPY, pvSrc, (uint32_t)cbSrc, &cbWritten);
526 AssertRC(rc);
527
528 Assert(cbSrc >= cbWritten);
529 Log2Func(("[SD%RU8]: %zu/%zu bytes read\n", pStream->u8SD, cbWritten, cbSrc));
530 }
531
532 RTCircBufReleaseReadBlock(pCircBuf, cbWritten);
533
534 if (RT_FAILURE(rc))
535 break;
536
537 Assert(cbLeft >= cbWritten);
538 cbLeft -= cbWritten;
539
540 cbReadTotal += cbWritten;
541 }
542
543 if (pcbRead)
544 *pcbRead = cbReadTotal;
545
546 return rc;
547}
548
549uint32_t hdaStreamTransferGetElapsed(PHDASTREAM pStream)
550{
551 AssertPtr(pStream->pHDAState->pTimer);
552 const uint64_t cTicksNow = TMTimerGet(pStream->pHDAState->pTimer);
553 const uint64_t cTicksPerSec = TMTimerGetFreq(pStream->pHDAState->pTimer);
554
555 const uint64_t cTicksElapsed = cTicksNow - pStream->State.uTimerTS;
556#ifdef DEBUG
557 const uint64_t cMsElapsed = cTicksElapsed / (cTicksPerSec / 1000);
558#endif
559
560 AssertPtr(pStream->pHDAState->pCodec);
561
562 PPDMAUDIOSTREAMCFG pCfg = &pStream->State.strmCfg;
563
564 /* A stream *always* runs with 48 kHz device-wise, regardless of the actual stream input/output format (Hz) being set. */
565 uint32_t csPerPeriod = (int)((pCfg->Props.cChannels * cTicksElapsed * 48000 /* Hz */ + cTicksPerSec) / cTicksPerSec / 2);
566 uint32_t cbPerPeriod = csPerPeriod << pCfg->Props.cShift;
567
568 Log3Func(("[SD%RU8] %RU64ms (%zu samples, %zu bytes) elapsed\n", pStream->u8SD, cMsElapsed, csPerPeriod, cbPerPeriod));
569
570 return cbPerPeriod;
571}
572
573/**
574 * Transfers data of an HDA stream according to its usage (input / output).
575 *
576 * For an SDO (output) stream this means reading DMA data from the device to
577 * the HDA stream's internal FIFO buffer.
578 *
579 * For an SDI (input) stream this is reading audio data from the HDA stream's
580 * internal FIFO buffer and writing it as DMA data to the device.
581 *
582 * @returns IPRT status code.
583 * @param pStream HDA stream to update.
584 * @param cbToProcessMax Maximum of data (in bytes) to process.
585 */
586int hdaStreamTransfer(PHDASTREAM pStream, uint32_t cbToProcessMax)
587{
588 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
589 AssertReturn(cbToProcessMax, VERR_INVALID_PARAMETER);
590
591 hdaStreamLock(pStream);
592
593 PHDASTATE pThis = pStream->pHDAState;
594 AssertPtr(pThis);
595
596 PHDASTREAMPERIOD pPeriod = &pStream->State.Period;
597 int rc = hdaStreamPeriodLock(pPeriod);
598 AssertRC(rc);
599
600 bool fProceed = true;
601
602 /* Stream not running? */
603 if (!(HDA_STREAM_REG(pThis, CTL, pStream->u8SD) & HDA_SDCTL_RUN))
604 {
605 Log3Func(("[SD%RU8] RUN bit not set\n", pStream->u8SD));
606 fProceed = false;
607 }
608 /* Period complete? */
609 else if (hdaStreamPeriodIsComplete(pPeriod))
610 {
611 Log3Func(("[SD%RU8] Period is complete, nothing to do\n", pStream->u8SD));
612 fProceed = false;
613 }
614
615 if (!fProceed)
616 {
617 hdaStreamPeriodUnlock(pPeriod);
618 hdaStreamUnlock(pStream);
619 return VINF_SUCCESS;
620 }
621
622 /* Sanity checks. */
623 Assert(pStream->u8SD < HDA_MAX_STREAMS);
624 Assert(pStream->u64BDLBase);
625 Assert(pStream->u32CBL);
626
627 /* State sanity checks. */
628 Assert(ASMAtomicReadBool(&pStream->State.fInReset) == false);
629
630 /* Fetch first / next BDL entry. */
631 PHDABDLE pBDLE = &pStream->State.BDLE;
632 if (hdaBDLEIsComplete(pBDLE))
633 {
634 rc = hdaBDLEFetch(pThis, pBDLE, pStream->u64BDLBase, pStream->State.uCurBDLE);
635 AssertRC(rc);
636 }
637
638 const uint32_t cbPeriodRemaining = hdaStreamPeriodGetRemainingFrames(pPeriod) * HDA_FRAME_SIZE;
639 Assert(cbPeriodRemaining); /* Paranoia. */
640
641 const uint32_t cbElapsed = hdaStreamTransferGetElapsed(pStream);
642 Assert(cbElapsed); /* Paranoia. */
643
644 /* Limit the data to read, as this routine could be delayed and therefore
645 * report wrong (e.g. too much) cbElapsed bytes. */
646 uint32_t cbLeft = RT_MIN(RT_MIN(cbPeriodRemaining, cbElapsed), cbToProcessMax);
647
648 Log3Func(("[SD%RU8] cbPeriodRemaining=%RU32, cbElapsed=%RU32, cbToProcessMax=%RU32 -> cbLeft=%RU32\n",
649 pStream->u8SD, cbPeriodRemaining, cbElapsed, cbToProcessMax, cbLeft));
650
651 Assert(cbLeft % HDA_FRAME_SIZE == 0); /* Paranoia. */
652
653 while (cbLeft)
654 {
655 uint32_t cbChunk = RT_MIN(hdaStreamGetTransferSize(pThis, pStream), cbLeft);
656 if (!cbChunk)
657 break;
658
659 uint32_t cbDMA = 0;
660
661 if (hdaGetDirFromSD(pStream->u8SD) == PDMAUDIODIR_OUT) /* Output (SDO). */
662 {
663 STAM_PROFILE_START(&pThis->StatOut, a);
664
665 rc = hdaDMARead(pThis, pStream, cbChunk, &cbDMA /* pcbRead */);
666 if (RT_FAILURE(rc))
667 LogRel(("HDA: Reading from stream #%RU8 DMA failed with %Rrc\n", pStream->u8SD, rc));
668
669 STAM_PROFILE_STOP(&pThis->StatOut, a);
670 }
671 else if (hdaGetDirFromSD(pStream->u8SD) == PDMAUDIODIR_IN) /* Input (SDI). */
672 {
673 STAM_PROFILE_START(&pThis->StatIn, a);
674
675 rc = hdaDMAWrite(pThis, pStream, cbChunk, &cbDMA /* pcbWritten */);
676 if (RT_FAILURE(rc))
677 LogRel(("HDA: Writing to stream #%RU8 DMA failed with %Rrc\n", pStream->u8SD, rc));
678
679 STAM_PROFILE_STOP(&pThis->StatIn, a);
680 }
681 else /** @todo Handle duplex streams? */
682 AssertFailed();
683
684 if (cbDMA)
685 {
686 Assert(cbDMA % HDA_FRAME_SIZE == 0);
687
688 /* We always increment the position of DMA buffer counter because we're always reading
689 * into an intermediate buffer. */
690 pBDLE->State.u32BufOff += (uint32_t)cbDMA;
691 Assert(pBDLE->State.u32BufOff <= pBDLE->Desc.u32BufSize);
692
693 hdaStreamTransferInc(pStream, cbDMA);
694
695 uint32_t framesDMA = cbDMA / HDA_FRAME_SIZE;
696
697 /* Add the transferred frames to the period. */
698 hdaStreamPeriodInc(pPeriod, framesDMA);
699
700 /* Save the timestamp of when the last successful DMA transfer has been for this stream. */
701 pStream->State.uTimerTS = TMTimerGet(pThis->pTimer);
702
703 Assert(cbLeft >= cbDMA);
704 cbLeft -= cbDMA;
705 }
706
707 if (hdaBDLEIsComplete(pBDLE))
708 {
709 Log3Func(("[SD%RU8] Complete: %R[bdle]\n", pStream->u8SD, pBDLE));
710
711 if (hdaBDLENeedsInterrupt(pBDLE))
712 {
713 /* If the IOCE ("Interrupt On Completion Enable") bit of the SDCTL register is set
714 * we need to generate an interrupt.
715 */
716 if (HDA_STREAM_REG(pThis, CTL, pStream->u8SD) & HDA_SDCTL_IOCE)
717 hdaStreamPeriodAcquireInterrupt(pPeriod);
718 }
719
720 if (pStream->State.uCurBDLE == pStream->u16LVI)
721 {
722 Assert(pStream->u32CBL == HDA_STREAM_REG(pThis, LPIB, pStream->u8SD));
723
724 pStream->State.uCurBDLE = 0;
725 hdaStreamUpdateLPIB(pStream, 0 /* LPIB */);
726 }
727 else
728 pStream->State.uCurBDLE++;
729
730 hdaBDLEFetch(pThis, pBDLE, pStream->u64BDLBase, pStream->State.uCurBDLE);
731
732 Log3Func(("[SD%RU8] Fetching: %R[bdle]\n", pStream->u8SD, pBDLE));
733 }
734
735 if (RT_FAILURE(rc))
736 break;
737 }
738
739 if (hdaStreamPeriodIsComplete(pPeriod))
740 {
741 Log3Func(("[SD%RU8] Period complete -- Current: %R[bdle]\n", pStream->u8SD, &pStream->State.BDLE));
742
743 /* Set the stream's BCIS bit.
744 *
745 * Note: This only must be done if the whole period is complete, and not if only
746 * one specific BDL entry is complete (if it has the IOC bit set).
747 *
748 * This will otherwise confuses the guest when it 1) deasserts the interrupt,
749 * 2) reads SDSTS (with BCIS set) and then 3) too early reads a (wrong) WALCLK value.
750 *
751 * snd_hda_intel on Linux will tell. */
752 HDA_STREAM_REG(pThis, STS, pStream->u8SD) |= HDA_SDSTS_BCIS;
753
754 /* Try updating the wall clock. */
755 const uint64_t u64WalClk = hdaStreamPeriodGetAbsElapsedWalClk(pPeriod);
756 const bool fWalClkSet = hdaWalClkSet(pThis, u64WalClk, false /* fForce */);
757
758 /* Does the period have any interrupts outstanding? */
759 if (hdaStreamPeriodNeedsInterrupt(pPeriod))
760 {
761 if (fWalClkSet)
762 {
763 Log3Func(("[SD%RU8] Set WALCLK to %RU64, triggering interrupt\n", pStream->u8SD, u64WalClk));
764
765 /* Trigger an interrupt first and let hdaRegWriteSDSTS() deal with
766 * ending / beginning a period. */
767#ifndef DEBUG
768 hdaProcessInterrupt(pThis);
769#else
770 hdaProcessInterrupt(pThis, __FUNCTION__);
771#endif
772 }
773 }
774 else
775 {
776 /* End the period first ... */
777 hdaStreamPeriodEnd(pPeriod);
778
779 /* ... and immediately begin the next one. */
780 hdaStreamPeriodBegin(pPeriod, hdaWalClkGetCurrent(pThis));
781 }
782 }
783
784 hdaStreamPeriodUnlock(pPeriod);
785
786 Log3Func(("[SD%RU8] Returning %Rrc ==========================================\n", pStream->u8SD, rc));
787
788 if (RT_FAILURE(rc))
789 LogFunc(("[SD%RU8] Failed with rc=%Rrcc\n", pStream->u8SD, rc));
790
791 hdaStreamUnlock(pStream);
792
793 return VINF_SUCCESS;
794}
795
796/**
797 * Updates a HDA stream by doing its required data transfers.
798 * The host sink(s) set the overall pace.
799 *
800 * This routine is called by both, the synchronous and the asynchronous, implementations.
801 *
802 * @param pStream HDA stream to update.
803 * @param fInTimer Whether to this function was called from the timer
804 * context or an asynchronous I/O stream thread (if supported).
805 */
806void hdaStreamUpdate(PHDASTREAM pStream, bool fInTimer)
807{
808 PAUDMIXSINK pSink = NULL;
809 if ( pStream->pMixSink
810 && pStream->pMixSink->pMixSink)
811 {
812 pSink = pStream->pMixSink->pMixSink;
813 }
814
815 if (!AudioMixerSinkIsActive(pSink)) /* No sink available? Bail out. */
816 return;
817
818 int rc2;
819
820 if (hdaGetDirFromSD(pStream->u8SD) == PDMAUDIODIR_OUT) /* Output (SDO). */
821 {
822 /* Is the HDA stream ready to be written (guest output data) to? If so, by how much? */
823 const uint32_t cbFree = hdaStreamGetFree(pStream);
824
825 if ( fInTimer
826 && cbFree)
827 {
828 Log3Func(("[SD%RU8] cbFree=%RU32\n", pStream->u8SD, cbFree));
829
830 /* Do the DMA transfer. */
831 rc2 = hdaStreamTransfer(pStream, cbFree);
832 AssertRC(rc2);
833 }
834
835 /* How much (guest output) data is available at the moment for the HDA stream? */
836 uint32_t cbUsed = hdaStreamGetUsed(pStream);
837
838#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
839 if ( fInTimer
840 && cbUsed)
841 {
842 rc2 = hdaStreamAsyncIONotify(pStream);
843 AssertRC(rc2);
844 }
845 else
846 {
847#endif
848 const uint32_t cbSinkWritable = AudioMixerSinkGetWritable(pSink);
849
850 /* Do not write more than the sink can hold at the moment.
851 * The host sets the overall pace. */
852 if (cbUsed > cbSinkWritable)
853 cbUsed = cbSinkWritable;
854
855 if (cbUsed)
856 {
857 /* Read (guest output) data and write it to the stream's sink. */
858 rc2 = hdaStreamRead(pStream, cbUsed, NULL /* pcbRead */);
859 AssertRC(rc2);
860 }
861
862 /* When running synchronously, update the associated sink here.
863 * Otherwise this will be done in the device timer. */
864 rc2 = AudioMixerSinkUpdate(pSink);
865 AssertRC(rc2);
866
867#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
868 }
869#endif
870 }
871 else /* Input (SDI). */
872 {
873#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
874 if (fInTimer)
875 {
876 rc2 = hdaStreamAsyncIONotify(pStream);
877 AssertRC(rc2);
878 }
879 else
880 {
881#endif
882 rc2 = AudioMixerSinkUpdate(pSink);
883 AssertRC(rc2);
884
885 /* Is the sink ready to be read (host input data) from? If so, by how much? */
886 const uint32_t cbReadable = AudioMixerSinkGetReadable(pSink);
887
888 /* How much (guest input) data is free at the moment? */
889 uint32_t cbFree = hdaStreamGetFree(pStream);
890
891 Log3Func(("[SD%RU8] cbReadable=%RU32, cbFree=%RU32\n", pStream->u8SD, cbReadable, cbFree));
892
893 /* Do not read more than the sink can provide at the moment.
894 * The host sets the overall pace. */
895 if (cbFree > cbReadable)
896 cbFree = cbReadable;
897
898 if (cbFree)
899 {
900 /* Write (guest input) data to the stream which was read from stream's sink before. */
901 rc2 = hdaStreamWrite(pStream, cbFree, NULL /* pcbWritten */);
902 AssertRC(rc2);
903 }
904#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
905 }
906#endif
907
908#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
909 if (fInTimer)
910 {
911#endif
912 const uint32_t cbToTransfer = hdaStreamGetUsed(pStream);
913 if (cbToTransfer)
914 {
915 /* When running synchronously, do the DMA data transfers here.
916 * Otherwise this will be done in the stream's async I/O thread. */
917 rc2 = hdaStreamTransfer(pStream, cbToTransfer);
918 AssertRC(rc2);
919 }
920#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
921 }
922#endif
923 }
924}
925
926/**
927 * Locks an HDA stream for serialized access.
928 *
929 * @returns IPRT status code.
930 * @param pStream HDA stream to lock.
931 */
932void hdaStreamLock(PHDASTREAM pStream)
933{
934 AssertPtrReturnVoid(pStream);
935 int rc2 = RTCritSectEnter(&pStream->State.CritSect);
936 AssertRC(rc2);
937}
938
939/**
940 * Unlocks a formerly locked HDA stream.
941 *
942 * @returns IPRT status code.
943 * @param pStream HDA stream to unlock.
944 */
945void hdaStreamUnlock(PHDASTREAM pStream)
946{
947 AssertPtrReturnVoid(pStream);
948 int rc2 = RTCritSectLeave(&pStream->State.CritSect);
949 AssertRC(rc2);
950}
951
952/**
953 * Updates an HDA stream's current read or write buffer position (depending on the stream type) by
954 * updating its associated LPIB register and DMA position buffer (if enabled).
955 *
956 * @returns Set LPIB value.
957 * @param pStream HDA stream to update read / write position for.
958 * @param u32LPIB New LPIB (position) value to set.
959 */
960uint32_t hdaStreamUpdateLPIB(PHDASTREAM pStream, uint32_t u32LPIB)
961{
962 AssertPtrReturn(pStream, 0);
963
964 AssertMsg(u32LPIB <= pStream->u32CBL,
965 ("[SD%RU8] New LPIB (%RU32) exceeds CBL (%RU32)\n", pStream->u8SD, u32LPIB, pStream->u32CBL));
966
967 const PHDASTATE pThis = pStream->pHDAState;
968
969 u32LPIB = RT_MIN(u32LPIB, pStream->u32CBL);
970
971 LogFlowFunc(("[SD%RU8]: LPIB=%RU32 (DMA Position Buffer Enabled: %RTbool)\n",
972 pStream->u8SD, u32LPIB, pThis->fDMAPosition));
973
974 /* Update LPIB in any case. */
975 HDA_STREAM_REG(pThis, LPIB, pStream->u8SD) = u32LPIB;
976
977 /* Do we need to tell the current DMA position? */
978 if (pThis->fDMAPosition)
979 {
980 int rc2 = PDMDevHlpPCIPhysWrite(pThis->CTX_SUFF(pDevIns),
981 pThis->u64DPBase + (pStream->u8SD * 2 * sizeof(uint32_t)),
982 (void *)&u32LPIB, sizeof(uint32_t));
983 AssertRC(rc2);
984 }
985
986 return u32LPIB;
987}
988
989# ifdef HDA_USE_DMA_ACCESS_HANDLER
990/**
991 * Registers access handlers for a stream's BDLE DMA accesses.
992 *
993 * @returns true if registration was successful, false if not.
994 * @param pStream HDA stream to register BDLE access handlers for.
995 */
996bool hdaStreamRegisterDMAHandlers(PHDASTREAM pStream)
997{
998 /* At least LVI and the BDL base must be set. */
999 if ( !pStream->u16LVI
1000 || !pStream->u64BDLBase)
1001 {
1002 return false;
1003 }
1004
1005 hdaStreamUnregisterDMAHandlers(pStream);
1006
1007 LogFunc(("Registering ...\n"));
1008
1009 int rc = VINF_SUCCESS;
1010
1011 /*
1012 * Create BDLE ranges.
1013 */
1014
1015 struct BDLERANGE
1016 {
1017 RTGCPHYS uAddr;
1018 uint32_t uSize;
1019 } arrRanges[16]; /** @todo Use a define. */
1020
1021 size_t cRanges = 0;
1022
1023 for (uint16_t i = 0; i < pStream->u16LVI + 1; i++)
1024 {
1025 HDABDLE BDLE;
1026 rc = hdaBDLEFetch(pThis, &BDLE, pStream->u64BDLBase, i /* Index */);
1027 if (RT_FAILURE(rc))
1028 break;
1029
1030 bool fAddRange = true;
1031 BDLERANGE *pRange;
1032
1033 if (cRanges)
1034 {
1035 pRange = &arrRanges[cRanges - 1];
1036
1037 /* Is the current range a direct neighbor of the current BLDE? */
1038 if ((pRange->uAddr + pRange->uSize) == BDLE.Desc.u64BufAdr)
1039 {
1040 /* Expand the current range by the current BDLE's size. */
1041 pRange->uSize += BDLE.Desc.u32BufSize;
1042
1043 /* Adding a new range in this case is not needed anymore. */
1044 fAddRange = false;
1045
1046 LogFunc(("Expanding range %zu by %RU32 (%RU32 total now)\n", cRanges - 1, BDLE.Desc.u32BufSize, pRange->uSize));
1047 }
1048 }
1049
1050 /* Do we need to add a new range? */
1051 if ( fAddRange
1052 && cRanges < RT_ELEMENTS(arrRanges))
1053 {
1054 pRange = &arrRanges[cRanges];
1055
1056 pRange->uAddr = BDLE.Desc.u64BufAdr;
1057 pRange->uSize = BDLE.Desc.u32BufSize;
1058
1059 LogFunc(("Adding range %zu - 0x%x (%RU32)\n", cRanges, pRange->uAddr, pRange->uSize));
1060
1061 cRanges++;
1062 }
1063 }
1064
1065 LogFunc(("%zu ranges total\n", cRanges));
1066
1067 /*
1068 * Register all ranges as DMA access handlers.
1069 */
1070
1071 for (size_t i = 0; i < cRanges; i++)
1072 {
1073 BDLERANGE *pRange = &arrRanges[i];
1074
1075 PHDADMAACCESSHANDLER pHandler = (PHDADMAACCESSHANDLER)RTMemAllocZ(sizeof(HDADMAACCESSHANDLER));
1076 if (!pHandler)
1077 {
1078 rc = VERR_NO_MEMORY;
1079 break;
1080 }
1081
1082 RTListAppend(&pStream->State.lstDMAHandlers, &pHandler->Node);
1083
1084 pHandler->pStream = pStream; /* Save a back reference to the owner. */
1085
1086 char szDesc[32];
1087 RTStrPrintf(szDesc, sizeof(szDesc), "HDA[SD%RU8 - RANGE%02zu]", pStream->u8SD, i);
1088
1089 int rc2 = PGMR3HandlerPhysicalTypeRegister(PDMDevHlpGetVM(pStream->pHDAState->pDevInsR3), PGMPHYSHANDLERKIND_WRITE,
1090 hdaDMAAccessHandler,
1091 NULL, NULL, NULL,
1092 NULL, NULL, NULL,
1093 szDesc, &pHandler->hAccessHandlerType);
1094 AssertRCBreak(rc2);
1095
1096 pHandler->BDLEAddr = pRange->uAddr;
1097 pHandler->BDLESize = pRange->uSize;
1098
1099 /* Get first and last pages of the BDLE range. */
1100 RTGCPHYS pgFirst = pRange->uAddr & ~PAGE_OFFSET_MASK;
1101 RTGCPHYS pgLast = RT_ALIGN(pgFirst + pRange->uSize, PAGE_SIZE);
1102
1103 /* Calculate the region size (in pages). */
1104 RTGCPHYS regionSize = RT_ALIGN(pgLast - pgFirst, PAGE_SIZE);
1105
1106 pHandler->GCPhysFirst = pgFirst;
1107 pHandler->GCPhysLast = pHandler->GCPhysFirst + (regionSize - 1);
1108
1109 LogFunc(("\tRegistering region '%s': 0x%x - 0x%x (region size: %zu)\n",
1110 szDesc, pHandler->GCPhysFirst, pHandler->GCPhysLast, regionSize));
1111 LogFunc(("\tBDLE @ 0x%x - 0x%x (%RU32)\n",
1112 pHandler->BDLEAddr, pHandler->BDLEAddr + pHandler->BDLESize, pHandler->BDLESize));
1113
1114 rc2 = PGMHandlerPhysicalRegister(PDMDevHlpGetVM(pStream->pHDAState->pDevInsR3),
1115 pHandler->GCPhysFirst, pHandler->GCPhysLast,
1116 pHandler->hAccessHandlerType, pHandler, NIL_RTR0PTR, NIL_RTRCPTR,
1117 szDesc);
1118 AssertRCBreak(rc2);
1119
1120 pHandler->fRegistered = true;
1121 }
1122
1123 LogFunc(("Registration ended with rc=%Rrc\n", rc));
1124
1125 return RT_SUCCESS(rc);
1126}
1127
1128/**
1129 * Unregisters access handlers of a stream's BDLEs.
1130 *
1131 * @param pStream HDA stream to unregister BDLE access handlers for.
1132 */
1133void hdaStreamUnregisterDMAHandlers(PHDASTREAM pStream)
1134{
1135 LogFunc(("\n"));
1136
1137 PHDADMAACCESSHANDLER pHandler, pHandlerNext;
1138 RTListForEachSafe(&pStream->State.lstDMAHandlers, pHandler, pHandlerNext, HDADMAACCESSHANDLER, Node)
1139 {
1140 if (!pHandler->fRegistered) /* Handler not registered? Skip. */
1141 continue;
1142
1143 LogFunc(("Unregistering 0x%x - 0x%x (%zu)\n",
1144 pHandler->GCPhysFirst, pHandler->GCPhysLast, pHandler->GCPhysLast - pHandler->GCPhysFirst));
1145
1146 int rc2 = PGMHandlerPhysicalDeregister(PDMDevHlpGetVM(pStream->pHDAState->pDevInsR3),
1147 pHandler->GCPhysFirst);
1148 AssertRC(rc2);
1149
1150 RTListNodeRemove(&pHandler->Node);
1151
1152 RTMemFree(pHandler);
1153 pHandler = NULL;
1154 }
1155
1156 Assert(RTListIsEmpty(&pStream->State.lstDMAHandlers));
1157}
1158# endif /* HDA_USE_DMA_ACCESS_HANDLER */
1159
1160# ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
1161/**
1162 * Asynchronous I/O thread for a HDA stream.
1163 * This will do the heavy lifting work for us as soon as it's getting notified by another thread.
1164 *
1165 * @returns IPRT status code.
1166 * @param hThreadSelf Thread handle.
1167 * @param pvUser User argument. Must be of type PHDASTREAMTHREADCTX.
1168 */
1169DECLCALLBACK(int) hdaStreamAsyncIOThread(RTTHREAD hThreadSelf, void *pvUser)
1170{
1171 PHDASTREAMTHREADCTX pCtx = (PHDASTREAMTHREADCTX)pvUser;
1172 AssertPtr(pCtx);
1173
1174 PHDASTREAM pStream = pCtx->pStream;
1175 AssertPtr(pStream);
1176
1177 PHDASTREAMSTATEAIO pAIO = &pCtx->pStream->State.AIO;
1178
1179 ASMAtomicXchgBool(&pAIO->fStarted, true);
1180
1181 RTThreadUserSignal(hThreadSelf);
1182
1183 LogFunc(("[SD%RU8]: Started\n", pStream->u8SD));
1184
1185 for (;;)
1186 {
1187 int rc2 = RTSemEventWait(pAIO->Event, RT_INDEFINITE_WAIT);
1188 if (RT_FAILURE(rc2))
1189 break;
1190
1191 if (ASMAtomicReadBool(&pAIO->fShutdown))
1192 break;
1193
1194 rc2 = RTCritSectEnter(&pAIO->CritSect);
1195 if (RT_SUCCESS(rc2))
1196 {
1197 if (!pAIO->fEnabled)
1198 {
1199 RTCritSectLeave(&pAIO->CritSect);
1200 continue;
1201 }
1202
1203 hdaStreamUpdate(pStream, false /* fInTimer */);
1204
1205 int rc3 = RTCritSectLeave(&pAIO->CritSect);
1206 AssertRC(rc3);
1207 }
1208
1209 AssertRC(rc2);
1210 }
1211
1212 LogFunc(("[SD%RU8]: Ended\n", pStream->u8SD));
1213
1214 ASMAtomicXchgBool(&pAIO->fStarted, false);
1215
1216 return VINF_SUCCESS;
1217}
1218
1219/**
1220 * Creates the async I/O thread for a specific HDA audio stream.
1221 *
1222 * @returns IPRT status code.
1223 * @param pStream HDA audio stream to create the async I/O thread for.
1224 */
1225int hdaStreamAsyncIOCreate(PHDASTREAM pStream)
1226{
1227 PHDASTREAMSTATEAIO pAIO = &pStream->State.AIO;
1228
1229 int rc;
1230
1231 if (!ASMAtomicReadBool(&pAIO->fStarted))
1232 {
1233 pAIO->fShutdown = false;
1234
1235 rc = RTSemEventCreate(&pAIO->Event);
1236 if (RT_SUCCESS(rc))
1237 {
1238 rc = RTCritSectInit(&pAIO->CritSect);
1239 if (RT_SUCCESS(rc))
1240 {
1241 HDASTREAMTHREADCTX Ctx = { pStream->pHDAState, pStream };
1242
1243 char szThreadName[64];
1244 RTStrPrintf2(szThreadName, sizeof(szThreadName), "hdaAIO%RU8", pStream->u8SD);
1245
1246 rc = RTThreadCreate(&pAIO->Thread, hdaStreamAsyncIOThread, &Ctx,
1247 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, szThreadName);
1248 if (RT_SUCCESS(rc))
1249 rc = RTThreadUserWait(pAIO->Thread, 10 * 1000 /* 10s timeout */);
1250 }
1251 }
1252 }
1253 else
1254 rc = VINF_SUCCESS;
1255
1256 LogFunc(("[SD%RU8]: Returning %Rrc\n", pStream->u8SD, rc));
1257 return rc;
1258}
1259
1260/**
1261 * Destroys the async I/O thread of a specific HDA audio stream.
1262 *
1263 * @returns IPRT status code.
1264 * @param pStream HDA audio stream to destroy the async I/O thread for.
1265 */
1266int hdaStreamAsyncIODestroy(PHDASTREAM pStream)
1267{
1268 PHDASTREAMSTATEAIO pAIO = &pStream->State.AIO;
1269
1270 if (!ASMAtomicReadBool(&pAIO->fStarted))
1271 return VINF_SUCCESS;
1272
1273 ASMAtomicWriteBool(&pAIO->fShutdown, true);
1274
1275 int rc = hdaStreamAsyncIONotify(pStream);
1276 AssertRC(rc);
1277
1278 int rcThread;
1279 rc = RTThreadWait(pAIO->Thread, 30 * 1000 /* 30s timeout */, &rcThread);
1280 LogFunc(("Async I/O thread ended with %Rrc (%Rrc)\n", rc, rcThread));
1281
1282 if (RT_SUCCESS(rc))
1283 {
1284 rc = RTCritSectDelete(&pAIO->CritSect);
1285 AssertRC(rc);
1286
1287 rc = RTSemEventDestroy(pAIO->Event);
1288 AssertRC(rc);
1289
1290 pAIO->fStarted = false;
1291 pAIO->fShutdown = false;
1292 pAIO->fEnabled = false;
1293 }
1294
1295 LogFunc(("[SD%RU8]: Returning %Rrc\n", pStream->u8SD, rc));
1296 return rc;
1297}
1298
1299/**
1300 * Lets the stream's async I/O thread know that there is some data to process.
1301 *
1302 * @returns IPRT status code.
1303 * @param pStream HDA stream to notify async I/O thread for.
1304 */
1305int hdaStreamAsyncIONotify(PHDASTREAM pStream)
1306{
1307 return RTSemEventSignal(pStream->State.AIO.Event);
1308}
1309
1310/**
1311 * Locks the async I/O thread of a specific HDA audio stream.
1312 *
1313 * @param pStream HDA stream to lock async I/O thread for.
1314 */
1315void hdaStreamAsyncIOLock(PHDASTREAM pStream)
1316{
1317 PHDASTREAMSTATEAIO pAIO = &pStream->State.AIO;
1318
1319 if (!ASMAtomicReadBool(&pAIO->fStarted))
1320 return;
1321
1322 int rc2 = RTCritSectEnter(&pAIO->CritSect);
1323 AssertRC(rc2);
1324}
1325
1326/**
1327 * Unlocks the async I/O thread of a specific HDA audio stream.
1328 *
1329 * @param pStream HDA stream to unlock async I/O thread for.
1330 */
1331void hdaStreamAsyncIOUnlock(PHDASTREAM pStream)
1332{
1333 PHDASTREAMSTATEAIO pAIO = &pStream->State.AIO;
1334
1335 if (!ASMAtomicReadBool(&pAIO->fStarted))
1336 return;
1337
1338 int rc2 = RTCritSectLeave(&pAIO->CritSect);
1339 AssertRC(rc2);
1340}
1341
1342/**
1343 * Enables (resumes) or disables (pauses) the async I/O thread.
1344 *
1345 * @param pStream HDA stream to enable/disable async I/O thread for.
1346 * @param fEnable Whether to enable or disable the I/O thread.
1347 *
1348 * @remarks Does not do locking.
1349 */
1350void hdaStreamAsyncIOEnable(PHDASTREAM pStream, bool fEnable)
1351{
1352 PHDASTREAMSTATEAIO pAIO = &pStream->State.AIO;
1353 ASMAtomicXchgBool(&pAIO->fEnabled, fEnable);
1354}
1355# endif /* VBOX_WITH_AUDIO_HDA_ASYNC_IO */
1356
1357#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