VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DevHDACommon.cpp@ 70363

Last change on this file since 70363 was 70295, checked in by vboxsync, 7 years ago

Audio/HDA: Also check for the running state in hdaStreamTransferIsScheduled().

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.6 KB
Line 
1/* $Id: DevHDACommon.cpp 70295 2017-12-21 17:29:02Z vboxsync $ */
2/** @file
3 * DevHDACommon.cpp - Shared HDA device functions.
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/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <iprt/assert.h>
23#include <iprt/err.h>
24
25#define LOG_GROUP LOG_GROUP_DEV_HDA
26#include <VBox/log.h>
27
28#include "DrvAudio.h"
29
30#include "DevHDA.h"
31#include "DevHDACommon.h"
32
33#include "HDAStream.h"
34
35
36#ifndef DEBUG
37/**
38 * Processes (de/asserts) the interrupt according to the HDA's current state.
39 *
40 * @returns IPRT status code.
41 * @param pThis HDA state.
42 */
43int hdaProcessInterrupt(PHDASTATE pThis)
44#else
45/**
46 * Processes (de/asserts) the interrupt according to the HDA's current state.
47 * Debug version.
48 *
49 * @returns IPRT status code.
50 * @param pThis HDA state.
51 * @param pszSource Caller information.
52 */
53int hdaProcessInterrupt(PHDASTATE pThis, const char *pszSource)
54#endif
55{
56 uint32_t uIntSts = hdaGetINTSTS(pThis);
57
58 HDA_REG(pThis, INTSTS) = uIntSts;
59
60 /* NB: It is possible to have GIS set even when CIE/SIEn are all zero; the GIS bit does
61 * not control the interrupt signal. See Figure 4 on page 54 of the HDA 1.0a spec.
62 */
63 /* Global Interrupt Enable (GIE) set? */
64 if ( (HDA_REG(pThis, INTCTL) & HDA_INTCTL_GIE)
65 && (HDA_REG(pThis, INTSTS) & HDA_REG(pThis, INTCTL) & (HDA_INTCTL_CIE | HDA_STRMINT_MASK)))
66 {
67 Log3Func(("Asserted (%s)\n", pszSource));
68
69 PDMDevHlpPCISetIrq(pThis->CTX_SUFF(pDevIns), 0, 1 /* Assert */);
70 pThis->u8IRQL = 1;
71
72#ifdef DEBUG
73 pThis->Dbg.IRQ.tsAssertedNs = RTTimeNanoTS();
74 pThis->Dbg.IRQ.tsProcessedLastNs = pThis->Dbg.IRQ.tsAssertedNs;
75#endif
76 }
77 else
78 {
79 Log3Func(("Deasserted (%s)\n", pszSource));
80
81 PDMDevHlpPCISetIrq(pThis->CTX_SUFF(pDevIns), 0, 0 /* Deassert */);
82 pThis->u8IRQL = 0;
83 }
84
85 return VINF_SUCCESS;
86}
87
88/**
89 * Retrieves the currently set value for the wall clock.
90 *
91 * @return IPRT status code.
92 * @return Currently set wall clock value.
93 * @param pThis HDA state.
94 *
95 * @remark Operation is atomic.
96 */
97uint64_t hdaWalClkGetCurrent(PHDASTATE pThis)
98{
99 return ASMAtomicReadU64(&pThis->u64WalClk);
100}
101
102#ifdef IN_RING3
103/**
104 * Sets the actual WALCLK register to the specified wall clock value.
105 * The specified wall clock value only will be set (unless fForce is set to true) if all
106 * handled HDA streams have passed (in time) that value. This guarantees that the WALCLK
107 * register stays in sync with all handled HDA streams.
108 *
109 * @return true if the WALCLK register has been updated, false if not.
110 * @param pThis HDA state.
111 * @param u64WalClk Wall clock value to set WALCLK register to.
112 * @param fForce Whether to force setting the wall clock value or not.
113 */
114bool hdaWalClkSet(PHDASTATE pThis, uint64_t u64WalClk, bool fForce)
115{
116 const bool fFrontPassed = hdaStreamPeriodHasPassedAbsWalClk (&hdaGetStreamFromSink(pThis, &pThis->SinkFront)->State.Period,
117 u64WalClk);
118 const uint64_t u64FrontAbsWalClk = hdaStreamPeriodGetAbsElapsedWalClk(&hdaGetStreamFromSink(pThis, &pThis->SinkFront)->State.Period);
119#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
120# error "Implement me!"
121#endif
122
123 const bool fLineInPassed = hdaStreamPeriodHasPassedAbsWalClk (&hdaGetStreamFromSink(pThis, &pThis->SinkLineIn)->State.Period, u64WalClk);
124 const uint64_t u64LineInAbsWalClk = hdaStreamPeriodGetAbsElapsedWalClk(&hdaGetStreamFromSink(pThis, &pThis->SinkLineIn)->State.Period);
125#ifdef VBOX_WITH_HDA_MIC_IN
126 const bool fMicInPassed = hdaStreamPeriodHasPassedAbsWalClk (&hdaGetStreamFromSink(pThis, &pThis->SinkMicIn)->State.Period, u64WalClk);
127 const uint64_t u64MicInAbsWalClk = hdaStreamPeriodGetAbsElapsedWalClk(&hdaGetStreamFromSink(pThis, &pThis->SinkMicIn)->State.Period);
128#endif
129
130#ifdef VBOX_STRICT
131 const uint64_t u64WalClkCur = ASMAtomicReadU64(&pThis->u64WalClk);
132#endif
133
134 /* Only drive the WALCLK register forward if all (active) stream periods have passed
135 * the specified point in time given by u64WalClk. */
136 if ( ( fFrontPassed
137#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
138# error "Implement me!"
139#endif
140 && fLineInPassed
141#ifdef VBOX_WITH_HDA_MIC_IN
142 && fMicInPassed
143#endif
144 )
145 || fForce)
146 {
147 if (!fForce)
148 {
149 /* Get the maximum value of all periods we need to handle.
150 * Not the most elegant solution, but works for now ... */
151 u64WalClk = RT_MAX(u64WalClk, u64FrontAbsWalClk);
152#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
153# error "Implement me!"
154#endif
155 u64WalClk = RT_MAX(u64WalClk, u64LineInAbsWalClk);
156#ifdef VBOX_WITH_HDA_MIC_IN
157 u64WalClk = RT_MAX(u64WalClk, u64MicInAbsWalClk);
158#endif
159
160#ifdef VBOX_STRICT
161 AssertMsg(u64WalClk >= u64WalClkCur,
162 ("Setting WALCLK to a value going backwards does not make any sense (old %RU64 vs. new %RU64)\n",
163 u64WalClkCur, u64WalClk));
164 if (u64WalClk == u64WalClkCur) /* Setting a stale value? */
165 {
166 if (pThis->u8WalClkStaleCnt++ > 3)
167 AssertMsgFailed(("Setting WALCLK to a stale value (%RU64) too often isn't a good idea really. "
168 "Good luck with stuck audio stuff.\n", u64WalClk));
169 }
170 else
171 pThis->u8WalClkStaleCnt = 0;
172#endif
173 }
174
175 /* Set the new WALCLK value. */
176 ASMAtomicWriteU64(&pThis->u64WalClk, u64WalClk);
177 }
178
179 const uint64_t u64WalClkNew = hdaWalClkGetCurrent(pThis);
180
181 Log3Func(("Cur: %RU64, New: %RU64 (force %RTbool) -> %RU64 %s\n",
182 u64WalClkCur, u64WalClk, fForce,
183 u64WalClkNew, u64WalClkNew == u64WalClk ? "[OK]" : "[DELAYED]"));
184
185 return (u64WalClkNew == u64WalClk);
186}
187
188/**
189 * Returns the default (mixer) sink from a given SD#.
190 * Returns NULL if no sink is found.
191 *
192 * @return PHDAMIXERSINK
193 * @param pThis HDA state.
194 * @param uSD SD# to return mixer sink for.
195 * NULL if not found / handled.
196 */
197PHDAMIXERSINK hdaGetDefaultSink(PHDASTATE pThis, uint8_t uSD)
198{
199 if (hdaGetDirFromSD(uSD) == PDMAUDIODIR_IN)
200 {
201 const uint8_t uFirstSDI = 0;
202
203 if (uSD == uFirstSDI) /* First SDI. */
204 return &pThis->SinkLineIn;
205#ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
206 else if (uSD == uFirstSDI + 1)
207 return &pThis->SinkMicIn;
208#else
209 else /* If we don't have a dedicated Mic-In sink, use the always present Line-In sink. */
210 return &pThis->SinkLineIn;
211#endif
212 }
213 else
214 {
215 const uint8_t uFirstSDO = HDA_MAX_SDI;
216
217 if (uSD == uFirstSDO)
218 return &pThis->SinkFront;
219#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
220 else if (uSD == uFirstSDO + 1)
221 return &pThis->SinkCenterLFE;
222 else if (uSD == uFirstSDO + 2)
223 return &pThis->SinkRear;
224#endif
225 }
226
227 return NULL;
228}
229
230/**
231 * Returns the audio direction of a specified stream descriptor.
232 *
233 * The register layout specifies that input streams (SDI) come first,
234 * followed by the output streams (SDO). So every stream ID below HDA_MAX_SDI
235 * is an input stream, whereas everything >= HDA_MAX_SDI is an output stream.
236 *
237 * Note: SDnFMT register does not provide that information, so we have to judge
238 * for ourselves.
239 *
240 * @return Audio direction.
241 */
242PDMAUDIODIR hdaGetDirFromSD(uint8_t uSD)
243{
244 AssertReturn(uSD < HDA_MAX_STREAMS, PDMAUDIODIR_UNKNOWN);
245
246 if (uSD < HDA_MAX_SDI)
247 return PDMAUDIODIR_IN;
248
249 return PDMAUDIODIR_OUT;
250}
251
252/**
253 * Returns the HDA stream of specified stream descriptor number.
254 *
255 * @return Pointer to HDA stream, or NULL if none found.
256 */
257PHDASTREAM hdaGetStreamFromSD(PHDASTATE pThis, uint8_t uSD)
258{
259 AssertPtrReturn(pThis, NULL);
260 AssertReturn(uSD < HDA_MAX_STREAMS, NULL);
261
262 if (uSD >= HDA_MAX_STREAMS)
263 {
264 AssertMsgFailed(("Invalid / non-handled SD%RU8\n", uSD));
265 return NULL;
266 }
267
268 return &pThis->aStreams[uSD];
269}
270
271/**
272 * Returns the HDA stream of specified HDA sink.
273 *
274 * @return Pointer to HDA stream, or NULL if none found.
275 */
276PHDASTREAM hdaGetStreamFromSink(PHDASTATE pThis, PHDAMIXERSINK pSink)
277{
278 AssertPtrReturn(pThis, NULL);
279 AssertPtrReturn(pSink, NULL);
280
281 /** @todo Do something with the channel mapping here? */
282 return pSink->pStream;
283}
284
285/**
286 * Reads DMA data from a given HDA output stream.
287 *
288 * @return IPRT status code.
289 * @param pThis HDA state.
290 * @param pStream HDA output stream to read DMA data from.
291 * @param pvBuf Where to store the read data.
292 * @param cbBuf How much to read in bytes.
293 * @param pcbRead Returns read bytes from DMA. Optional.
294 */
295int hdaDMARead(PHDASTATE pThis, PHDASTREAM pStream, void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
296{
297 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
298 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
299 /* pcbRead is optional. */
300
301 PHDABDLE pBDLE = &pStream->State.BDLE;
302
303 int rc = VINF_SUCCESS;
304
305 uint32_t cbReadTotal = 0;
306 uint32_t cbLeft = RT_MIN(cbBuf, pBDLE->Desc.u32BufSize - pBDLE->State.u32BufOff);
307
308#ifdef HDA_DEBUG_SILENCE
309 uint64_t csSilence = 0;
310
311 pStream->Dbg.cSilenceThreshold = 100;
312 pStream->Dbg.cbSilenceReadMin = _1M;
313#endif
314
315 RTGCPHYS addrChunk = pBDLE->Desc.u64BufAdr + pBDLE->State.u32BufOff;
316
317 while (cbLeft)
318 {
319 uint32_t cbChunk = RT_MIN(cbLeft, pStream->u16FIFOS);
320
321 rc = PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), addrChunk, (uint8_t *)pvBuf + cbReadTotal, cbChunk);
322 if (RT_FAILURE(rc))
323 break;
324
325#ifdef HDA_DEBUG_SILENCE
326 uint16_t *pu16Buf = (uint16_t *)pvBuf;
327 for (size_t i = 0; i < cbChunk / sizeof(uint16_t); i++)
328 {
329 if (*pu16Buf == 0)
330 {
331 csSilence++;
332 }
333 else
334 break;
335 pu16Buf++;
336 }
337#endif
338 if (pStream->Dbg.Runtime.fEnabled)
339 DrvAudioHlpFileWrite(pStream->Dbg.Runtime.pFileDMA, (uint8_t *)pvBuf + cbReadTotal, cbChunk, 0 /* fFlags */);
340
341#ifdef VBOX_WITH_STATISTICS
342 STAM_COUNTER_ADD(&pThis->StatBytesRead, cbChunk);
343#endif
344 addrChunk = (addrChunk + cbChunk) % pBDLE->Desc.u32BufSize;
345
346 Assert(cbLeft >= cbChunk);
347 cbLeft -= cbChunk;
348
349 cbReadTotal += cbChunk;
350 }
351
352#ifdef HDA_DEBUG_SILENCE
353
354 if (csSilence)
355 pStream->Dbg.csSilence += csSilence;
356
357 if ( csSilence == 0
358 && pStream->Dbg.csSilence > pStream->Dbg.cSilenceThreshold
359 && pStream->Dbg.cbReadTotal >= pStream->Dbg.cbSilenceReadMin)
360 {
361 LogFunc(("Silent block detected: %RU64 audio samples\n", pStream->Dbg.csSilence));
362 pStream->Dbg.csSilence = 0;
363 }
364#endif
365
366 if (RT_SUCCESS(rc))
367 {
368 if (pcbRead)
369 *pcbRead = cbReadTotal;
370 }
371
372 return rc;
373}
374
375/**
376 * Writes audio data from an HDA input stream's FIFO to its associated DMA area.
377 *
378 * @return IPRT status code.
379 * @param pThis HDA state.
380 * @param pStream HDA input stream to write audio data to.
381 * @param pvBuf Data to write.
382 * @param cbBuf How much (in bytes) to write.
383 * @param pcbWritten Returns written bytes on success. Optional.
384 */
385int hdaDMAWrite(PHDASTATE pThis, PHDASTREAM pStream, const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
386{
387 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
388 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
389 /* pcbWritten is optional. */
390
391 PHDABDLE pBDLE = &pStream->State.BDLE;
392
393 int rc = VINF_SUCCESS;
394
395 uint32_t cbWrittenTotal = 0;
396 uint32_t cbLeft = RT_MIN(cbBuf, pBDLE->Desc.u32BufSize - pBDLE->State.u32BufOff);
397
398 RTGCPHYS addrChunk = pBDLE->Desc.u64BufAdr + pBDLE->State.u32BufOff;
399
400 while (cbLeft)
401 {
402 uint32_t cbChunk = RT_MIN(cbLeft, pStream->u16FIFOS);
403
404 /* Sanity checks. */
405 Assert(cbChunk <= pBDLE->Desc.u32BufSize - pBDLE->State.u32BufOff);
406 Assert(cbChunk % pStream->State.cbFrameSize == 0);
407 Assert((cbChunk >> 1) >= 1);
408
409 if (pStream->Dbg.Runtime.fEnabled)
410 DrvAudioHlpFileWrite(pStream->Dbg.Runtime.pFileDMA, (uint8_t *)pvBuf + cbWrittenTotal, cbChunk, 0 /* fFlags */);
411
412 rc = PDMDevHlpPCIPhysWrite(pThis->CTX_SUFF(pDevIns),
413 addrChunk, (uint8_t *)pvBuf + cbWrittenTotal, cbChunk);
414 if (RT_FAILURE(rc))
415 break;
416
417#ifdef VBOX_WITH_STATISTICS
418 STAM_COUNTER_ADD(&pThis->StatBytesWritten, cbChunk);
419#endif
420 addrChunk = (addrChunk + cbChunk) % pBDLE->Desc.u32BufSize;
421
422 Assert(cbLeft >= cbChunk);
423 cbLeft -= (uint32_t)cbChunk;
424
425 cbWrittenTotal += (uint32_t)cbChunk;
426 }
427
428 if (RT_SUCCESS(rc))
429 {
430 if (pcbWritten)
431 *pcbWritten = cbWrittenTotal;
432 }
433 else
434 LogFunc(("Failed with %Rrc\n", rc));
435
436 return rc;
437}
438#endif /* IN_RING3 */
439
440/**
441 * Returns a new INTSTS value based on the current device state.
442 *
443 * @returns Determined INTSTS register value.
444 * @param pThis HDA state.
445 *
446 * @remark This function does *not* set INTSTS!
447 */
448uint32_t hdaGetINTSTS(PHDASTATE pThis)
449{
450 uint32_t intSts = 0;
451
452 /* Check controller interrupts (RIRB, STATEST). */
453 if ( (HDA_REG(pThis, RIRBSTS) & HDA_REG(pThis, RIRBCTL) & (HDA_RIRBCTL_ROIC | HDA_RIRBCTL_RINTCTL))
454 /* SDIN State Change Status Flags (SCSF). */
455 || (HDA_REG(pThis, STATESTS) & HDA_STATESTS_SCSF_MASK))
456 {
457 intSts |= HDA_INTSTS_CIS; /* Set the Controller Interrupt Status (CIS). */
458 }
459
460 if (HDA_REG(pThis, STATESTS) & HDA_REG(pThis, WAKEEN))
461 {
462 intSts |= HDA_INTSTS_CIS; /* Touch Controller Interrupt Status (CIS). */
463 }
464
465 /* For each stream, check if any interrupt status bit is set and enabled. */
466 for (uint8_t iStrm = 0; iStrm < HDA_MAX_STREAMS; ++iStrm)
467 {
468 if (HDA_STREAM_REG(pThis, STS, iStrm) & HDA_STREAM_REG(pThis, CTL, iStrm) & (HDA_SDCTL_DEIE | HDA_SDCTL_FEIE | HDA_SDCTL_IOCE))
469 {
470 Log3Func(("[SD%d] interrupt status set\n", iStrm));
471 intSts |= RT_BIT(iStrm);
472 }
473 }
474
475 if (intSts)
476 intSts |= HDA_INTSTS_GIS; /* Set the Global Interrupt Status (GIS). */
477
478 Log3Func(("-> 0x%x\n", intSts));
479
480 return intSts;
481}
482
483/**
484 * Converts an HDA stream's SDFMT register into a given PCM properties structure.
485 *
486 * @return IPRT status code.
487 * @param u32SDFMT The HDA stream's SDFMT value to convert.
488 * @param pProps PCM properties structure to hold converted result on success.
489 */
490int hdaSDFMTToPCMProps(uint32_t u32SDFMT, PPDMAUDIOPCMPROPS pProps)
491{
492 AssertPtrReturn(pProps, VERR_INVALID_POINTER);
493
494# define EXTRACT_VALUE(v, mask, shift) ((v & ((mask) << (shift))) >> (shift))
495
496 int rc = VINF_SUCCESS;
497
498 uint32_t u32Hz = EXTRACT_VALUE(u32SDFMT, HDA_SDFMT_BASE_RATE_MASK, HDA_SDFMT_BASE_RATE_SHIFT)
499 ? 44100 : 48000;
500 uint32_t u32HzMult = 1;
501 uint32_t u32HzDiv = 1;
502
503 switch (EXTRACT_VALUE(u32SDFMT, HDA_SDFMT_MULT_MASK, HDA_SDFMT_MULT_SHIFT))
504 {
505 case 0: u32HzMult = 1; break;
506 case 1: u32HzMult = 2; break;
507 case 2: u32HzMult = 3; break;
508 case 3: u32HzMult = 4; break;
509 default:
510 LogFunc(("Unsupported multiplier %x\n",
511 EXTRACT_VALUE(u32SDFMT, HDA_SDFMT_MULT_MASK, HDA_SDFMT_MULT_SHIFT)));
512 rc = VERR_NOT_SUPPORTED;
513 break;
514 }
515 switch (EXTRACT_VALUE(u32SDFMT, HDA_SDFMT_DIV_MASK, HDA_SDFMT_DIV_SHIFT))
516 {
517 case 0: u32HzDiv = 1; break;
518 case 1: u32HzDiv = 2; break;
519 case 2: u32HzDiv = 3; break;
520 case 3: u32HzDiv = 4; break;
521 case 4: u32HzDiv = 5; break;
522 case 5: u32HzDiv = 6; break;
523 case 6: u32HzDiv = 7; break;
524 case 7: u32HzDiv = 8; break;
525 default:
526 LogFunc(("Unsupported divisor %x\n",
527 EXTRACT_VALUE(u32SDFMT, HDA_SDFMT_DIV_MASK, HDA_SDFMT_DIV_SHIFT)));
528 rc = VERR_NOT_SUPPORTED;
529 break;
530 }
531
532 uint8_t cBits = 0;
533 switch (EXTRACT_VALUE(u32SDFMT, HDA_SDFMT_BITS_MASK, HDA_SDFMT_BITS_SHIFT))
534 {
535 case 0:
536 cBits = 8;
537 break;
538 case 1:
539 cBits = 16;
540 break;
541 case 4:
542 cBits = 32;
543 break;
544 default:
545 AssertMsgFailed(("Unsupported bits per sample %x\n",
546 EXTRACT_VALUE(u32SDFMT, HDA_SDFMT_BITS_MASK, HDA_SDFMT_BITS_SHIFT)));
547 rc = VERR_NOT_SUPPORTED;
548 break;
549 }
550
551 if (RT_SUCCESS(rc))
552 {
553 RT_BZERO(pProps, sizeof(PDMAUDIOPCMPROPS));
554
555 pProps->cBits = cBits;
556 pProps->fSigned = true;
557 pProps->cChannels = (u32SDFMT & 0xf) + 1;
558 pProps->uHz = u32Hz * u32HzMult / u32HzDiv;
559 pProps->cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pProps->cBits, pProps->cChannels);
560 }
561
562# undef EXTRACT_VALUE
563 return rc;
564}
565
566#ifdef IN_RING3
567# ifdef LOG_ENABLED
568void hdaBDLEDumpAll(PHDASTATE pThis, uint64_t u64BDLBase, uint16_t cBDLE)
569{
570 LogFlowFunc(("BDLEs @ 0x%x (%RU16):\n", u64BDLBase, cBDLE));
571 if (!u64BDLBase)
572 return;
573
574 uint32_t cbBDLE = 0;
575 for (uint16_t i = 0; i < cBDLE; i++)
576 {
577 HDABDLEDESC bd;
578 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), u64BDLBase + i * sizeof(HDABDLEDESC), &bd, sizeof(bd));
579
580 LogFunc(("\t#%03d BDLE(adr:0x%llx, size:%RU32, ioc:%RTbool)\n",
581 i, bd.u64BufAdr, bd.u32BufSize, bd.fFlags & HDA_BDLE_FLAG_IOC));
582
583 cbBDLE += bd.u32BufSize;
584 }
585
586 LogFlowFunc(("Total: %RU32 bytes\n", cbBDLE));
587
588 if (!pThis->u64DPBase) /* No DMA base given? Bail out. */
589 return;
590
591 LogFlowFunc(("DMA counters:\n"));
592
593 for (int i = 0; i < cBDLE; i++)
594 {
595 uint32_t uDMACnt;
596 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), (pThis->u64DPBase & DPBASE_ADDR_MASK) + (i * 2 * sizeof(uint32_t)),
597 &uDMACnt, sizeof(uDMACnt));
598
599 LogFlowFunc(("\t#%03d DMA @ 0x%x\n", i , uDMACnt));
600 }
601}
602# endif /* LOG_ENABLED */
603
604/**
605 * Fetches a Bundle Descriptor List Entry (BDLE) from the DMA engine.
606 *
607 * @param pThis Pointer to HDA state.
608 * @param pBDLE Where to store the fetched result.
609 * @param u64BaseDMA Address base of DMA engine to use.
610 * @param u16Entry BDLE entry to fetch.
611 */
612int hdaBDLEFetch(PHDASTATE pThis, PHDABDLE pBDLE, uint64_t u64BaseDMA, uint16_t u16Entry)
613{
614 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
615 AssertPtrReturn(pBDLE, VERR_INVALID_POINTER);
616 AssertReturn(u64BaseDMA, VERR_INVALID_PARAMETER);
617
618 if (!u64BaseDMA)
619 {
620 LogRel2(("HDA: Unable to fetch BDLE #%RU16 - no base DMA address set (yet)\n", u16Entry));
621 return VERR_NOT_FOUND;
622 }
623 /** @todo Compare u16Entry with LVI. */
624
625 int rc = PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), u64BaseDMA + (u16Entry * sizeof(HDABDLEDESC)),
626 &pBDLE->Desc, sizeof(pBDLE->Desc));
627
628 if (RT_SUCCESS(rc))
629 {
630 /* Reset internal state. */
631 RT_ZERO(pBDLE->State);
632 pBDLE->State.u32BDLIndex = u16Entry;
633 }
634
635 Log3Func(("Entry #%d @ 0x%x: %R[bdle], rc=%Rrc\n", u16Entry, u64BaseDMA + (u16Entry * sizeof(HDABDLEDESC)), pBDLE, rc));
636
637
638 return VINF_SUCCESS;
639}
640
641/**
642 * Tells whether a given BDLE is complete or not.
643 *
644 * @return true if BDLE is complete, false if not.
645 * @param pBDLE BDLE to retrieve status for.
646 */
647bool hdaBDLEIsComplete(PHDABDLE pBDLE)
648{
649 bool fIsComplete = false;
650
651 if ( !pBDLE->Desc.u32BufSize /* There can be BDLEs with 0 size. */
652 || (pBDLE->State.u32BufOff >= pBDLE->Desc.u32BufSize))
653 {
654 Assert(pBDLE->State.u32BufOff == pBDLE->Desc.u32BufSize);
655 fIsComplete = true;
656 }
657
658 Log3Func(("%R[bdle] => %s\n", pBDLE, fIsComplete ? "COMPLETE" : "INCOMPLETE"));
659
660 return fIsComplete;
661}
662
663/**
664 * Tells whether a given BDLE needs an interrupt or not.
665 *
666 * @return true if BDLE needs an interrupt, false if not.
667 * @param pBDLE BDLE to retrieve status for.
668 */
669bool hdaBDLENeedsInterrupt(PHDABDLE pBDLE)
670{
671 return (pBDLE->Desc.fFlags & HDA_BDLE_FLAG_IOC);
672}
673
674/**
675 * Sets the virtual device timer to a new expiration time.
676 *
677 * @returns Whether the new expiration time was set or not.
678 * @param pThis HDA state.
679 * @param tsExpire New (virtual) expiration time to set.
680 * @param fForce Whether to force setting the expiration time or not.
681 *
682 * @remark This function takes all active HDA streams and their
683 * current timing into account. This is needed to make sure
684 * that all streams can match their needed timing.
685 *
686 * To achieve this, the earliest (lowest) timestamp of all
687 * active streams found will be used for the next scheduling slot.
688 *
689 * Forcing a new expiration time will override the above mechanism.
690 */
691bool hdaTimerSet(PHDASTATE pThis, uint64_t tsExpire, bool fForce)
692{
693 AssertPtr(pThis->pTimer);
694
695 uint64_t tsExpireMin = tsExpire;
696
697 if (!fForce)
698 {
699 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
700 {
701 PHDASTREAM pStream = &pThis->aStreams[i];
702
703 if (hdaStreamTransferIsScheduled(pStream))
704 tsExpireMin = RT_MIN(tsExpireMin, hdaStreamTransferGetNext(pStream));
705 }
706 }
707
708 const uint64_t tsNow = TMTimerGet(pThis->pTimer);
709
710 if (tsExpireMin < tsNow) /* Make sure to not go backwards in time. */
711 tsExpireMin = tsNow;
712
713 Log3Func(("u64Epxire=%RU64 -> u64ExpireMin=%RU64, fForce=%RTbool [%s]\n",
714 tsExpire, tsExpireMin, fForce, tsExpireMin == tsExpire ? "OK" : "DELAYED"));
715
716 int rc2 = TMTimerSet(pThis->pTimer, tsExpireMin);
717 AssertRC(rc2);
718
719 return tsExpireMin == tsExpire;
720}
721#endif /* IN_RING3 */
722
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