1 | /* $Id: DevHdaStream.h 90013 2021-07-04 21:10:00Z 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 | #ifndef VBOX_INCLUDED_SRC_Audio_DevHdaStream_h
|
---|
19 | #define VBOX_INCLUDED_SRC_Audio_DevHdaStream_h
|
---|
20 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
21 | # pragma once
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #ifndef VBOX_INCLUDED_SRC_Audio_DevHda_h
|
---|
25 | # error "Only include DevHda.h!"
|
---|
26 | #endif
|
---|
27 |
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Structure containing HDA stream debug stuff, configurable at runtime.
|
---|
31 | */
|
---|
32 | typedef struct HDASTREAMDEBUGRT
|
---|
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. */
|
---|
40 | R3PTRTYPE(PAUDIOHLPFILE) pFileStream;
|
---|
41 | /** File for dumping raw DMA reads / writes.
|
---|
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. */
|
---|
44 | R3PTRTYPE(PAUDIOHLPFILE) pFileDMARaw;
|
---|
45 | /** File for dumping mapped (that is, extracted) DMA reads / writes. */
|
---|
46 | R3PTRTYPE(PAUDIOHLPFILE) pFileDMAMapped;
|
---|
47 | } HDASTREAMDEBUGRT;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Structure containing HDA stream debug information.
|
---|
51 | */
|
---|
52 | typedef struct HDASTREAMDEBUG
|
---|
53 | {
|
---|
54 | /** Runtime debug info. */
|
---|
55 | HDASTREAMDEBUGRT Runtime;
|
---|
56 | uint64_t au64Alignment[2];
|
---|
57 | } HDASTREAMDEBUG;
|
---|
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;
|
---|
67 | /** Flag indicating if the stream is in running state or not. */
|
---|
68 | volatile bool fRunning;
|
---|
69 | /** How many interrupts are pending due to
|
---|
70 | * BDLE interrupt-on-completion (IOC) bits set. */
|
---|
71 | uint8_t cTransferPendingInterrupts;
|
---|
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;
|
---|
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;
|
---|
80 | /** The stream's current configuration (matches SDnFMT). */
|
---|
81 | PDMAUDIOSTREAMCFG Cfg;
|
---|
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;
|
---|
88 |
|
---|
89 | /** The start time for the playback (on the timer clock). */
|
---|
90 | uint64_t tsStart;
|
---|
91 |
|
---|
92 | /** @name DMA engine
|
---|
93 | * @{ */
|
---|
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;
|
---|
98 | /** The size of the current DMA transfer period. */
|
---|
99 | uint32_t cbCurDmaPeriod;
|
---|
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 |
|
---|
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];
|
---|
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
|
---|
164 | /** @} */
|
---|
165 | } HDASTREAMSTATE;
|
---|
166 | AssertCompileSizeAlignment(HDASTREAMSTATE, 16);
|
---|
167 | AssertCompileMemberAlignment(HDASTREAMSTATE, aBdl, 8);
|
---|
168 | AssertCompileMemberAlignment(HDASTREAMSTATE, aBdl, 16);
|
---|
169 | AssertCompileMemberAlignment(HDASTREAMSTATE, aSchedule, 16);
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * An HDA stream (SDI / SDO) - shared.
|
---|
173 | *
|
---|
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).
|
---|
178 | *
|
---|
179 | * Contains only register values which do *not* change until a stream reset
|
---|
180 | * occurs.
|
---|
181 | */
|
---|
182 | typedef struct HDASTREAM
|
---|
183 | {
|
---|
184 | /** Internal state of this stream. */
|
---|
185 | HDASTREAMSTATE State;
|
---|
186 |
|
---|
187 | /** Stream descriptor number (SDn). */
|
---|
188 | uint8_t u8SD;
|
---|
189 | /** Current channel index.
|
---|
190 | * For a stereo stream, this is u8Channel + 1. */
|
---|
191 | uint8_t u8Channel;
|
---|
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 | * @{ */
|
---|
200 | /** FIFO Size (checked + translated in bytes, FIFOS).
|
---|
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. */
|
---|
204 | uint8_t u8FIFOS;
|
---|
205 | /** Cyclic Buffer Length (SDnCBL) - Represents the size of the ring buffer. */
|
---|
206 | uint32_t u32CBL;
|
---|
207 | /** Last Valid Index (SDnLVI). */
|
---|
208 | uint16_t u16LVI;
|
---|
209 | /** Format (SDnFMT). */
|
---|
210 | uint16_t u16FMT;
|
---|
211 | uint8_t abPadding[4];
|
---|
212 | /** DMA base address (SDnBDPU - SDnBDPL). */
|
---|
213 | uint64_t u64BDLBase;
|
---|
214 | /** @} */
|
---|
215 |
|
---|
216 | /** The timer for pumping data thru the attached LUN drivers. */
|
---|
217 | TMTIMERHANDLE hTimer;
|
---|
218 |
|
---|
219 | /** Pad the structure size to a 64 byte alignment. */
|
---|
220 | uint64_t au64Padding1[2];
|
---|
221 | } HDASTREAM;
|
---|
222 | AssertCompileMemberAlignment(HDASTREAM, State.aBdl, 16);
|
---|
223 | AssertCompileMemberAlignment(HDASTREAM, State.aSchedule, 16);
|
---|
224 | AssertCompileSizeAlignment(HDASTREAM, 64);
|
---|
225 | /** Pointer to an HDA stream (SDI / SDO). */
|
---|
226 | typedef HDASTREAM *PHDASTREAM;
|
---|
227 |
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * An HDA stream (SDI / SDO) - ring-3 bits.
|
---|
231 | */
|
---|
232 | typedef struct HDASTREAMR3
|
---|
233 | {
|
---|
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;
|
---|
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 |
|
---|
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;
|
---|
259 | /** Counter for all under/overflows problems. */
|
---|
260 | STAMCOUNTER StatDmaFlowProblems;
|
---|
261 | /** Counter for unresovled under/overflows problems. */
|
---|
262 | STAMCOUNTER StatDmaFlowErrors;
|
---|
263 | /** Number of bytes involved in unresolved flow errors. */
|
---|
264 | STAMCOUNTER StatDmaFlowErrorBytes;
|
---|
265 | /** DMA skipped because buffer interrupt pending. */
|
---|
266 | STAMCOUNTER StatDmaSkippedPendingBcis;
|
---|
267 |
|
---|
268 | STAMPROFILE StatStart;
|
---|
269 | STAMPROFILE StatReset;
|
---|
270 | STAMPROFILE StatStop;
|
---|
271 | } State;
|
---|
272 | /** Debug bits. */
|
---|
273 | HDASTREAMDEBUG Dbg;
|
---|
274 | uint64_t au64Alignment[3];
|
---|
275 | } HDASTREAMR3;
|
---|
276 | AssertCompileSizeAlignment(HDASTREAMR3, 64);
|
---|
277 | /** Pointer to an HDA stream (SDI / SDO). */
|
---|
278 | typedef HDASTREAMR3 *PHDASTREAMR3;
|
---|
279 |
|
---|
280 | /** @name Stream functions (all contexts).
|
---|
281 | * @{
|
---|
282 | */
|
---|
283 | VBOXSTRICTRC hdaStreamDoOnAccessDmaOutput(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared,
|
---|
284 | uint64_t tsNow, uint32_t cbToTransfer);
|
---|
285 | VBOXSTRICTRC hdaStreamMaybeDoOnAccessDmaOutput(PPDMDEVINS pDevIns, PHDASTATE pThis,
|
---|
286 | PHDASTREAM pStreamShared, uint64_t tsNow);
|
---|
287 | /** @} */
|
---|
288 |
|
---|
289 | #ifdef IN_RING3
|
---|
290 |
|
---|
291 | /** @name Stream functions (ring-3).
|
---|
292 | * @{
|
---|
293 | */
|
---|
294 | int hdaR3StreamConstruct(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, PHDASTATE pThis,
|
---|
295 | PHDASTATER3 pThisCC, uint8_t uSD);
|
---|
296 | void hdaR3StreamDestroy(PHDASTREAMR3 pStreamR3);
|
---|
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);
|
---|
301 | int hdaR3StreamEnable(PHDASTATE pThis, PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, bool fEnable);
|
---|
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,
|
---|
306 | PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3);
|
---|
307 | DECLCALLBACK(void) hdaR3StreamUpdateAsyncIoJob(PPDMDEVINS pDevIns, PAUDMIXSINK pSink, void *pvUser);
|
---|
308 | /** @} */
|
---|
309 |
|
---|
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 |
|
---|
318 | #endif /* IN_RING3 */
|
---|
319 | #endif /* !VBOX_INCLUDED_SRC_Audio_DevHdaStream_h */
|
---|
320 |
|
---|