[69118] | 1 | /* $Id: DevHdaStream.h 90013 2021-07-04 21:10:00Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[88235] | 3 | * Intel HD Audio Controller Emulation - Streams.
|
---|
[69118] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[82968] | 7 | * Copyright (C) 2017-2020 Oracle Corporation
|
---|
[69118] | 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 |
|
---|
[88228] | 18 | #ifndef VBOX_INCLUDED_SRC_Audio_DevHdaStream_h
|
---|
| 19 | #define VBOX_INCLUDED_SRC_Audio_DevHdaStream_h
|
---|
[76520] | 20 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 21 | # pragma once
|
---|
| 22 | #endif
|
---|
[69118] | 23 |
|
---|
[89869] | 24 | #ifndef VBOX_INCLUDED_SRC_Audio_DevHda_h
|
---|
| 25 | # error "Only include DevHda.h!"
|
---|
| 26 | #endif
|
---|
[69118] | 27 |
|
---|
| 28 |
|
---|
| 29 | /**
|
---|
[70013] | 30 | * Structure containing HDA stream debug stuff, configurable at runtime.
|
---|
| 31 | */
|
---|
[82417] | 32 | typedef struct HDASTREAMDEBUGRT
|
---|
[70013] | 33 | {
|
---|
| 34 | /** Whether debugging is enabled or not. */
|
---|
| 35 | bool fEnabled;
|
---|
| 36 | uint8_t Padding[7];
|
---|
| 37 | /** File for dumping stream reads / writes.
|
---|
| 38 | * For input streams, this dumps data being written to the device FIFO,
|
---|
| 39 | * whereas for output streams this dumps data being read from the device FIFO. */
|
---|
[88357] | 40 | R3PTRTYPE(PAUDIOHLPFILE) pFileStream;
|
---|
[75962] | 41 | /** File for dumping raw DMA reads / writes.
|
---|
[70013] | 42 | * For input streams, this dumps data being written to the device DMA,
|
---|
| 43 | * whereas for output streams this dumps data being read from the device DMA. */
|
---|
[88357] | 44 | R3PTRTYPE(PAUDIOHLPFILE) pFileDMARaw;
|
---|
[75962] | 45 | /** File for dumping mapped (that is, extracted) DMA reads / writes. */
|
---|
[88357] | 46 | R3PTRTYPE(PAUDIOHLPFILE) pFileDMAMapped;
|
---|
[82417] | 47 | } HDASTREAMDEBUGRT;
|
---|
[70013] | 48 |
|
---|
| 49 | /**
|
---|
| 50 | * Structure containing HDA stream debug information.
|
---|
| 51 | */
|
---|
[82417] | 52 | typedef struct HDASTREAMDEBUG
|
---|
[69118] | 53 | {
|
---|
[88165] | 54 | /** Runtime debug info. */
|
---|
| 55 | HDASTREAMDEBUGRT Runtime;
|
---|
| 56 | uint64_t au64Alignment[2];
|
---|
[82417] | 57 | } HDASTREAMDEBUG;
|
---|
[69118] | 58 |
|
---|
| 59 | /**
|
---|
| 60 | * Internal state of a HDA stream.
|
---|
| 61 | */
|
---|
| 62 | typedef struct HDASTREAMSTATE
|
---|
| 63 | {
|
---|
| 64 | /** Flag indicating whether this stream currently is
|
---|
| 65 | * in reset mode and therefore not acccessible by the guest. */
|
---|
| 66 | volatile bool fInReset;
|
---|
[70247] | 67 | /** Flag indicating if the stream is in running state or not. */
|
---|
| 68 | volatile bool fRunning;
|
---|
[88137] | 69 | /** How many interrupts are pending due to
|
---|
| 70 | * BDLE interrupt-on-completion (IOC) bits set. */
|
---|
| 71 | uint8_t cTransferPendingInterrupts;
|
---|
[88158] | 72 | /** Input streams only: Set when we switch from feeding the guest silence and
|
---|
| 73 | * commits to proving actual audio input bytes. */
|
---|
| 74 | bool fInputPreBuffered;
|
---|
| 75 | /** Input streams only: The number of bytes we need to prebuffer. */
|
---|
| 76 | uint32_t cbInputPreBuffer;
|
---|
[88170] | 77 | /** Timestamp (absolute, in timer ticks) of the last DMA data transfer.
|
---|
| 78 | * @note This is used for wall clock (WALCLK) calculations. */
|
---|
| 79 | uint64_t volatile tsTransferLast;
|
---|
[89821] | 80 | /** The stream's current configuration (matches SDnFMT). */
|
---|
[69118] | 81 | PDMAUDIOSTREAMCFG Cfg;
|
---|
[87758] | 82 | /** Timestamp (real time, in ns) of last DMA transfer. */
|
---|
| 83 | uint64_t tsLastTransferNs;
|
---|
| 84 | /** Timestamp (real time, in ns) of last stream read (to backends).
|
---|
| 85 | * When running in async I/O mode, this differs from \a tsLastTransferNs,
|
---|
| 86 | * because reading / processing will be done in a separate stream. */
|
---|
| 87 | uint64_t tsLastReadNs;
|
---|
[88063] | 88 |
|
---|
| 89 | /** The start time for the playback (on the timer clock). */
|
---|
| 90 | uint64_t tsStart;
|
---|
[88137] | 91 |
|
---|
| 92 | /** @name DMA engine
|
---|
| 93 | * @{ */
|
---|
[89861] | 94 | /** Timestamp (absolute, in timer ticks) of the next DMA data transfer.
|
---|
| 95 | * Next for determining the next scheduling window.
|
---|
| 96 | * Can be 0 if no next transfer is scheduled. */
|
---|
| 97 | uint64_t tsTransferNext;
|
---|
[89869] | 98 | /** The size of the current DMA transfer period. */
|
---|
| 99 | uint32_t cbCurDmaPeriod;
|
---|
[89861] | 100 | /** The size of an average transfer. */
|
---|
| 101 | uint32_t cbAvgTransfer;
|
---|
| 102 |
|
---|
| 103 | /** Current circular buffer read offset (for tracing & logging). */
|
---|
| 104 | uint64_t offRead;
|
---|
| 105 | /** Current circular buffer write offset (for tracing & logging). */
|
---|
| 106 | uint64_t offWrite;
|
---|
| 107 |
|
---|
[88137] | 108 | /** The offset into the current BDLE. */
|
---|
| 109 | uint32_t offCurBdle;
|
---|
| 110 | /** LVI + 1 */
|
---|
| 111 | uint16_t cBdles;
|
---|
| 112 | /** The index of the current BDLE.
|
---|
| 113 | * This is the entry which period is currently "running" on the DMA timer. */
|
---|
| 114 | uint8_t idxCurBdle;
|
---|
| 115 | /** The number of prologue scheduling steps.
|
---|
| 116 | * This is used when the tail BDLEs doesn't have IOC set. */
|
---|
| 117 | uint8_t cSchedulePrologue;
|
---|
| 118 | /** Number of scheduling steps. */
|
---|
| 119 | uint16_t cSchedule;
|
---|
| 120 | /** Current scheduling step. */
|
---|
| 121 | uint16_t idxSchedule;
|
---|
| 122 | /** Current loop number within the current scheduling step. */
|
---|
| 123 | uint32_t idxScheduleLoop;
|
---|
| 124 |
|
---|
| 125 | /** Buffer descriptors and additional timer scheduling state.
|
---|
| 126 | * (Same as HDABDLEDESC, with more sensible naming.) */
|
---|
| 127 | struct
|
---|
| 128 | {
|
---|
| 129 | /** The buffer address. */
|
---|
| 130 | uint64_t GCPhys;
|
---|
| 131 | /** The buffer size (guest bytes). */
|
---|
| 132 | uint32_t cb;
|
---|
| 133 | /** The flags (only bit 0 is defined). */
|
---|
| 134 | uint32_t fFlags;
|
---|
| 135 | } aBdl[256];
|
---|
| 136 | /** Scheduling steps. */
|
---|
| 137 | struct
|
---|
| 138 | {
|
---|
| 139 | /** Number of timer ticks per period.
|
---|
| 140 | * ASSUMES that we don't need a full second and that the timer resolution
|
---|
| 141 | * isn't much higher than nanoseconds. */
|
---|
| 142 | uint32_t cPeriodTicks;
|
---|
| 143 | /** The period length in host bytes. */
|
---|
| 144 | uint32_t cbPeriod;
|
---|
| 145 | /** Number of times to repeat the period. */
|
---|
| 146 | uint32_t cLoops;
|
---|
| 147 | /** The BDL index of the first entry. */
|
---|
| 148 | uint8_t idxFirst;
|
---|
| 149 | /** The number of BDL entries. */
|
---|
| 150 | uint8_t cEntries;
|
---|
| 151 | uint8_t abPadding[2];
|
---|
| 152 | } aSchedule[512+8];
|
---|
[89861] | 153 |
|
---|
| 154 | #ifdef VBOX_HDA_WITH_ON_REG_ACCESS_DMA
|
---|
| 155 | /** Number of valid bytes in abDma.
|
---|
| 156 | * @note Volatile to prevent the compiler from re-reading it after we've
|
---|
| 157 | * validated the value in ring-0. */
|
---|
| 158 | uint32_t volatile cbDma;
|
---|
| 159 | /** Total number of bytes going via abDma this timer period. */
|
---|
| 160 | uint32_t cbDmaTotal;
|
---|
| 161 | /** DMA bounce buffer for ring-0 register reads (LPIB). */
|
---|
| 162 | uint8_t abDma[2048 - 8];
|
---|
| 163 | #endif
|
---|
[88137] | 164 | /** @} */
|
---|
[71754] | 165 | } HDASTREAMSTATE;
|
---|
[89861] | 166 | AssertCompileSizeAlignment(HDASTREAMSTATE, 16);
|
---|
[88170] | 167 | AssertCompileMemberAlignment(HDASTREAMSTATE, aBdl, 8);
|
---|
[88137] | 168 | AssertCompileMemberAlignment(HDASTREAMSTATE, aBdl, 16);
|
---|
| 169 | AssertCompileMemberAlignment(HDASTREAMSTATE, aSchedule, 16);
|
---|
[69118] | 170 |
|
---|
| 171 | /**
|
---|
[82450] | 172 | * An HDA stream (SDI / SDO) - shared.
|
---|
[69118] | 173 | *
|
---|
[82331] | 174 | * @note This HDA stream has nothing to do with a regular audio stream handled
|
---|
| 175 | * by the audio connector or the audio mixer. This HDA stream is a serial
|
---|
| 176 | * data in/out stream (SDI/SDO) defined in hardware and can contain
|
---|
| 177 | * multiple audio streams in one single SDI/SDO (interleaving streams).
|
---|
[69118] | 178 | *
|
---|
[82331] | 179 | * Contains only register values which do *not* change until a stream reset
|
---|
| 180 | * occurs.
|
---|
[69118] | 181 | */
|
---|
| 182 | typedef struct HDASTREAM
|
---|
| 183 | {
|
---|
[88164] | 184 | /** Internal state of this stream. */
|
---|
| 185 | HDASTREAMSTATE State;
|
---|
| 186 |
|
---|
[69118] | 187 | /** Stream descriptor number (SDn). */
|
---|
[82417] | 188 | uint8_t u8SD;
|
---|
[70247] | 189 | /** Current channel index.
|
---|
| 190 | * For a stereo stream, this is u8Channel + 1. */
|
---|
[82417] | 191 | uint8_t u8Channel;
|
---|
[88164] | 192 | /** FIFO Watermark (checked + translated in bytes, FIFOW).
|
---|
| 193 | * This will be update from hdaRegWriteSDFIFOW() and also copied
|
---|
| 194 | * hdaR3StreamInit() for some reason. */
|
---|
| 195 | uint8_t u8FIFOW;
|
---|
| 196 |
|
---|
| 197 | /** @name Register values at stream setup.
|
---|
| 198 | * These will all be copied in hdaR3StreamInit().
|
---|
| 199 | * @{ */
|
---|
[87319] | 200 | /** FIFO Size (checked + translated in bytes, FIFOS).
|
---|
[88164] | 201 | * This is supposedly the max number of bytes we'll be DMA'ing in one chunk
|
---|
| 202 | * and correspondingly the LPIB & wall clock update jumps. However, we're
|
---|
| 203 | * not at all being honest with the guest about this. */
|
---|
[87319] | 204 | uint8_t u8FIFOS;
|
---|
[88164] | 205 | /** Cyclic Buffer Length (SDnCBL) - Represents the size of the ring buffer. */
|
---|
| 206 | uint32_t u32CBL;
|
---|
| 207 | /** Last Valid Index (SDnLVI). */
|
---|
[82417] | 208 | uint16_t u16LVI;
|
---|
[88164] | 209 | /** Format (SDnFMT). */
|
---|
| 210 | uint16_t u16FMT;
|
---|
| 211 | uint8_t abPadding[4];
|
---|
| 212 | /** DMA base address (SDnBDPU - SDnBDPL). */
|
---|
| 213 | uint64_t u64BDLBase;
|
---|
| 214 | /** @} */
|
---|
| 215 |
|
---|
[82345] | 216 | /** The timer for pumping data thru the attached LUN drivers. */
|
---|
[82417] | 217 | TMTIMERHANDLE hTimer;
|
---|
[88164] | 218 |
|
---|
| 219 | /** Pad the structure size to a 64 byte alignment. */
|
---|
[89381] | 220 | uint64_t au64Padding1[2];
|
---|
[82331] | 221 | } HDASTREAM;
|
---|
[88164] | 222 | AssertCompileMemberAlignment(HDASTREAM, State.aBdl, 16);
|
---|
| 223 | AssertCompileMemberAlignment(HDASTREAM, State.aSchedule, 16);
|
---|
| 224 | AssertCompileSizeAlignment(HDASTREAM, 64);
|
---|
[82331] | 225 | /** Pointer to an HDA stream (SDI / SDO). */
|
---|
| 226 | typedef HDASTREAM *PHDASTREAM;
|
---|
[69118] | 227 |
|
---|
[82450] | 228 |
|
---|
[69118] | 229 | /**
|
---|
[82450] | 230 | * An HDA stream (SDI / SDO) - ring-3 bits.
|
---|
[69118] | 231 | */
|
---|
[82450] | 232 | typedef struct HDASTREAMR3
|
---|
[69118] | 233 | {
|
---|
[82450] | 234 | /** Stream descriptor number (SDn). */
|
---|
| 235 | uint8_t u8SD;
|
---|
| 236 | uint8_t abPadding[7];
|
---|
| 237 | /** The shared state for the parent HDA device. */
|
---|
| 238 | R3PTRTYPE(PHDASTATE) pHDAStateShared;
|
---|
| 239 | /** The ring-3 state for the parent HDA device. */
|
---|
| 240 | R3PTRTYPE(PHDASTATER3) pHDAStateR3;
|
---|
| 241 | /** Pointer to HDA sink this stream is attached to. */
|
---|
| 242 | R3PTRTYPE(PHDAMIXERSINK) pMixSink;
|
---|
| 243 | /** Internal state of this stream. */
|
---|
| 244 | struct
|
---|
| 245 | {
|
---|
| 246 | /** Circular buffer (FIFO) for holding DMA'ed data. */
|
---|
| 247 | R3PTRTYPE(PRTCIRCBUF) pCircBuf;
|
---|
[88941] | 248 | /** The mixer sink this stream has registered AIO update callback with.
|
---|
| 249 | * This is NULL till we register it, typically in hdaR3StreamEnable.
|
---|
| 250 | * (The problem with following the pMixSink assignment is that hdaR3StreamReset
|
---|
| 251 | * sets it without updating the HDA sink structure, so things get out of
|
---|
| 252 | * wack in hdaR3MixerControl later in the initial device reset.) */
|
---|
| 253 | PAUDMIXSINK pAioRegSink;
|
---|
| 254 |
|
---|
[88307] | 255 | /** Size of the DMA buffer (pCircBuf) in bytes. */
|
---|
| 256 | uint32_t StatDmaBufSize;
|
---|
| 257 | /** Number of used bytes in the DMA buffer (pCircBuf). */
|
---|
| 258 | uint32_t StatDmaBufUsed;
|
---|
[88094] | 259 | /** Counter for all under/overflows problems. */
|
---|
| 260 | STAMCOUNTER StatDmaFlowProblems;
|
---|
| 261 | /** Counter for unresovled under/overflows problems. */
|
---|
[87989] | 262 | STAMCOUNTER StatDmaFlowErrors;
|
---|
[88158] | 263 | /** Number of bytes involved in unresolved flow errors. */
|
---|
| 264 | STAMCOUNTER StatDmaFlowErrorBytes;
|
---|
[89853] | 265 | /** DMA skipped because buffer interrupt pending. */
|
---|
| 266 | STAMCOUNTER StatDmaSkippedPendingBcis;
|
---|
[88662] | 267 |
|
---|
| 268 | STAMPROFILE StatStart;
|
---|
| 269 | STAMPROFILE StatReset;
|
---|
| 270 | STAMPROFILE StatStop;
|
---|
[82450] | 271 | } State;
|
---|
| 272 | /** Debug bits. */
|
---|
| 273 | HDASTREAMDEBUG Dbg;
|
---|
[89861] | 274 | uint64_t au64Alignment[3];
|
---|
[82450] | 275 | } HDASTREAMR3;
|
---|
[88164] | 276 | AssertCompileSizeAlignment(HDASTREAMR3, 64);
|
---|
[82450] | 277 | /** Pointer to an HDA stream (SDI / SDO). */
|
---|
| 278 | typedef HDASTREAMR3 *PHDASTREAMR3;
|
---|
[69118] | 279 |
|
---|
[89861] | 280 | /** @name Stream functions (all contexts).
|
---|
| 281 | * @{
|
---|
| 282 | */
|
---|
| 283 | VBOXSTRICTRC hdaStreamDoOnAccessDmaOutput(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared,
|
---|
[89869] | 284 | uint64_t tsNow, uint32_t cbToTransfer);
|
---|
| 285 | VBOXSTRICTRC hdaStreamMaybeDoOnAccessDmaOutput(PPDMDEVINS pDevIns, PHDASTATE pThis,
|
---|
| 286 | PHDASTREAM pStreamShared, uint64_t tsNow);
|
---|
[89861] | 287 | /** @} */
|
---|
| 288 |
|
---|
[87952] | 289 | #ifdef IN_RING3
|
---|
| 290 |
|
---|
[87830] | 291 | /** @name Stream functions (ring-3).
|
---|
| 292 | * @{
|
---|
| 293 | */
|
---|
[82450] | 294 | int hdaR3StreamConstruct(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, PHDASTATE pThis,
|
---|
| 295 | PHDASTATER3 pThisCC, uint8_t uSD);
|
---|
[89844] | 296 | void hdaR3StreamDestroy(PHDASTREAMR3 pStreamR3);
|
---|
[82450] | 297 | int hdaR3StreamSetUp(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared,
|
---|
| 298 | PHDASTREAMR3 pStreamR3, uint8_t uSD);
|
---|
| 299 | void hdaR3StreamReset(PHDASTATE pThis, PHDASTATER3 pThisCC,
|
---|
| 300 | PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, uint8_t uSD);
|
---|
[88572] | 301 | int hdaR3StreamEnable(PHDASTATE pThis, PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, bool fEnable);
|
---|
[88063] | 302 | void hdaR3StreamMarkStarted(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared, uint64_t tsNow);
|
---|
| 303 | void hdaR3StreamMarkStopped(PHDASTREAM pStreamShared);
|
---|
| 304 |
|
---|
| 305 | uint64_t hdaR3StreamTimerMain(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTATER3 pThisCC,
|
---|
[87944] | 306 | PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3);
|
---|
[88941] | 307 | DECLCALLBACK(void) hdaR3StreamUpdateAsyncIoJob(PPDMDEVINS pDevIns, PAUDMIXSINK pSink, void *pvUser);
|
---|
[69118] | 308 | /** @} */
|
---|
| 309 |
|
---|
[89887] | 310 | /** @name Helper functions associated with the stream code.
|
---|
| 311 | * @{ */
|
---|
| 312 | int hdaR3SDFMTToPCMProps(uint16_t u16SDFMT, PPDMAUDIOPCMPROPS pProps);
|
---|
| 313 | # ifdef LOG_ENABLED
|
---|
| 314 | void hdaR3BDLEDumpAll(PPDMDEVINS pDevIns, PHDASTATE pThis, uint64_t u64BDLBase, uint16_t cBDLE);
|
---|
| 315 | # endif
|
---|
| 316 | /** @} */
|
---|
| 317 |
|
---|
[69118] | 318 | #endif /* IN_RING3 */
|
---|
[88228] | 319 | #endif /* !VBOX_INCLUDED_SRC_Audio_DevHdaStream_h */
|
---|
[69118] | 320 |
|
---|