1 | /* $Id: HDAStream.h 88158 2021-03-17 14:39:22Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * HDAStream.h - Streams for HD Audio.
|
---|
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_HDAStream_h
|
---|
19 | #define VBOX_INCLUDED_SRC_Audio_HDAStream_h
|
---|
20 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
21 | # pragma once
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #include "DevHDACommon.h"
|
---|
25 | #include "HDAStreamMap.h"
|
---|
26 | #include "HDAStreamPeriod.h"
|
---|
27 |
|
---|
28 |
|
---|
29 | #ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
30 | /**
|
---|
31 | * HDA stream's state for asynchronous I/O.
|
---|
32 | */
|
---|
33 | typedef struct HDASTREAMSTATEAIO
|
---|
34 | {
|
---|
35 | /** Thread handle for the actual I/O thread. */
|
---|
36 | RTTHREAD hThread;
|
---|
37 | /** Event for letting the thread know there is some data to process. */
|
---|
38 | RTSEMEVENT hEvent;
|
---|
39 | /** Critical section for synchronizing access. */
|
---|
40 | RTCRITSECT CritSect;
|
---|
41 | /** Started indicator. */
|
---|
42 | volatile bool fStarted;
|
---|
43 | /** Shutdown indicator. */
|
---|
44 | volatile bool fShutdown;
|
---|
45 | /** Whether the thread should do any data processing or not. */
|
---|
46 | volatile bool fEnabled;
|
---|
47 | bool afPadding[1+4];
|
---|
48 | } HDASTREAMSTATEAIO;
|
---|
49 | /** Pointer to a HDA stream's asynchronous I/O state. */
|
---|
50 | typedef HDASTREAMSTATEAIO *PHDASTREAMSTATEAIO;
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Structure containing HDA stream debug stuff, configurable at runtime.
|
---|
55 | */
|
---|
56 | typedef struct HDASTREAMDEBUGRT
|
---|
57 | {
|
---|
58 | /** Whether debugging is enabled or not. */
|
---|
59 | bool fEnabled;
|
---|
60 | uint8_t Padding[7];
|
---|
61 | /** File for dumping stream reads / writes.
|
---|
62 | * For input streams, this dumps data being written to the device FIFO,
|
---|
63 | * whereas for output streams this dumps data being read from the device FIFO. */
|
---|
64 | R3PTRTYPE(PPDMAUDIOFILE) pFileStream;
|
---|
65 | /** File for dumping raw DMA reads / writes.
|
---|
66 | * For input streams, this dumps data being written to the device DMA,
|
---|
67 | * whereas for output streams this dumps data being read from the device DMA. */
|
---|
68 | R3PTRTYPE(PPDMAUDIOFILE) pFileDMARaw;
|
---|
69 | /** File for dumping mapped (that is, extracted) DMA reads / writes. */
|
---|
70 | R3PTRTYPE(PPDMAUDIOFILE) pFileDMAMapped;
|
---|
71 | } HDASTREAMDEBUGRT;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Structure containing HDA stream debug information.
|
---|
75 | */
|
---|
76 | typedef struct HDASTREAMDEBUG
|
---|
77 | {
|
---|
78 | #ifdef DEBUG
|
---|
79 | /** Critical section to serialize access if needed. */
|
---|
80 | RTCRITSECT CritSect;
|
---|
81 | uint32_t Padding0[2];
|
---|
82 | /** Number of total read accesses. */
|
---|
83 | uint64_t cReadsTotal;
|
---|
84 | /** Number of total DMA bytes read. */
|
---|
85 | uint64_t cbReadTotal;
|
---|
86 | /** Timestamp (in ns) of last read access. */
|
---|
87 | uint64_t tsLastReadNs;
|
---|
88 | /** Number of total write accesses. */
|
---|
89 | uint64_t cWritesTotal;
|
---|
90 | /** Number of total DMA bytes written. */
|
---|
91 | uint64_t cbWrittenTotal;
|
---|
92 | /** Number of total write accesses since last iteration (Hz). */
|
---|
93 | uint64_t cWritesHz;
|
---|
94 | /** Number of total DMA bytes written since last iteration (Hz). */
|
---|
95 | uint64_t cbWrittenHz;
|
---|
96 | /** Timestamp (in ns) of beginning a new write slot. */
|
---|
97 | uint64_t tsWriteSlotBegin;
|
---|
98 | /** Number of current silence samples in a (consecutive) row. */
|
---|
99 | uint64_t csSilence;
|
---|
100 | /** Number of silent samples in a row to consider an audio block as audio gap (silence). */
|
---|
101 | uint64_t cSilenceThreshold;
|
---|
102 | /** How many bytes to skip in an audio stream before detecting silence.
|
---|
103 | * (useful for intros and silence at the beginning of a song). */
|
---|
104 | uint64_t cbSilenceReadMin;
|
---|
105 | #endif
|
---|
106 | /** Runtime debug info. */
|
---|
107 | HDASTREAMDEBUGRT Runtime;
|
---|
108 | } HDASTREAMDEBUG;
|
---|
109 | typedef HDASTREAMDEBUG *PHDASTREAMDEBUG;
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Internal state of a HDA stream.
|
---|
113 | */
|
---|
114 | typedef struct HDASTREAMSTATE
|
---|
115 | {
|
---|
116 | /** Flag indicating whether this stream currently is
|
---|
117 | * in reset mode and therefore not acccessible by the guest. */
|
---|
118 | volatile bool fInReset;
|
---|
119 | /** Flag indicating if the stream is in running state or not. */
|
---|
120 | volatile bool fRunning;
|
---|
121 | /** The stream's I/O timer Hz rate. */
|
---|
122 | uint16_t uTimerIoHz;
|
---|
123 | /** How many interrupts are pending due to
|
---|
124 | * BDLE interrupt-on-completion (IOC) bits set. */
|
---|
125 | uint8_t cTransferPendingInterrupts;
|
---|
126 | /** Unused, padding. */
|
---|
127 | uint8_t abPadding1[2];
|
---|
128 | /** Input streams only: Set when we switch from feeding the guest silence and
|
---|
129 | * commits to proving actual audio input bytes. */
|
---|
130 | bool fInputPreBuffered;
|
---|
131 | /** Input streams only: The number of bytes we need to prebuffer. */
|
---|
132 | uint32_t cbInputPreBuffer;
|
---|
133 | uint32_t u32Padding2;
|
---|
134 | /** Timestamp (absolute, in timer ticks) of the last DMA data transfer. */
|
---|
135 | uint64_t tsTransferLast;
|
---|
136 | /** Timestamp (absolute, in timer ticks) of the next DMA data transfer.
|
---|
137 | * Next for determining the next scheduling window.
|
---|
138 | * Can be 0 if no next transfer is scheduled. */
|
---|
139 | uint64_t tsTransferNext;
|
---|
140 | /** Total transfer size (in bytes) of a transfer period.
|
---|
141 | * @note This is in host side frames, in case we're doing any mapping. */
|
---|
142 | uint32_t cbTransferSize;
|
---|
143 | /** The size of an average transfer. */
|
---|
144 | uint32_t cbAvgTransfer;
|
---|
145 | /** The stream's period. Need for timing. */
|
---|
146 | HDASTREAMPERIOD Period;
|
---|
147 | /** The stream's current host side configuration.
|
---|
148 | * This should match the SDnFMT in all respects but maybe the channel count as
|
---|
149 | * we may need to expand mono or into/from into stereo. The unmodified SDnFMT
|
---|
150 | * properties can be found in HDASTREAMR3::Mapping::PCMProps. */
|
---|
151 | PDMAUDIOSTREAMCFG Cfg;
|
---|
152 | /** Timestamp (real time, in ns) of last DMA transfer. */
|
---|
153 | uint64_t tsLastTransferNs;
|
---|
154 | /** Timestamp (real time, in ns) of last stream read (to backends).
|
---|
155 | * When running in async I/O mode, this differs from \a tsLastTransferNs,
|
---|
156 | * because reading / processing will be done in a separate stream. */
|
---|
157 | uint64_t tsLastReadNs;
|
---|
158 |
|
---|
159 | /** This is set to the timer clock time when the msInitialDelay period is over.
|
---|
160 | * Once reached, this is set to zero to avoid unnecessary time queries. */
|
---|
161 | uint64_t tsAioDelayEnd;
|
---|
162 | /** The start time for the playback (on the timer clock). */
|
---|
163 | uint64_t tsStart;
|
---|
164 |
|
---|
165 | uint64_t au64Padding[3];
|
---|
166 |
|
---|
167 | /** @name DMA engine
|
---|
168 | * @{ */
|
---|
169 | /** The offset into the current BDLE. */
|
---|
170 | uint32_t offCurBdle;
|
---|
171 | /** LVI + 1 */
|
---|
172 | uint16_t cBdles;
|
---|
173 | /** The index of the current BDLE.
|
---|
174 | * This is the entry which period is currently "running" on the DMA timer. */
|
---|
175 | uint8_t idxCurBdle;
|
---|
176 | /** The number of prologue scheduling steps.
|
---|
177 | * This is used when the tail BDLEs doesn't have IOC set. */
|
---|
178 | uint8_t cSchedulePrologue;
|
---|
179 | /** Number of scheduling steps. */
|
---|
180 | uint16_t cSchedule;
|
---|
181 | /** Current scheduling step. */
|
---|
182 | uint16_t idxSchedule;
|
---|
183 | /** Current loop number within the current scheduling step. */
|
---|
184 | uint32_t idxScheduleLoop;
|
---|
185 |
|
---|
186 | /** Buffer descriptors and additional timer scheduling state.
|
---|
187 | * (Same as HDABDLEDESC, with more sensible naming.) */
|
---|
188 | struct
|
---|
189 | {
|
---|
190 | /** The buffer address. */
|
---|
191 | uint64_t GCPhys;
|
---|
192 | /** The buffer size (guest bytes). */
|
---|
193 | uint32_t cb;
|
---|
194 | /** The flags (only bit 0 is defined). */
|
---|
195 | uint32_t fFlags;
|
---|
196 | } aBdl[256];
|
---|
197 | /** Scheduling steps. */
|
---|
198 | struct
|
---|
199 | {
|
---|
200 | /** Number of timer ticks per period.
|
---|
201 | * ASSUMES that we don't need a full second and that the timer resolution
|
---|
202 | * isn't much higher than nanoseconds. */
|
---|
203 | uint32_t cPeriodTicks;
|
---|
204 | /** The period length in host bytes. */
|
---|
205 | uint32_t cbPeriod;
|
---|
206 | /** Number of times to repeat the period. */
|
---|
207 | uint32_t cLoops;
|
---|
208 | /** The BDL index of the first entry. */
|
---|
209 | uint8_t idxFirst;
|
---|
210 | /** The number of BDL entries. */
|
---|
211 | uint8_t cEntries;
|
---|
212 | uint8_t abPadding[2];
|
---|
213 | } aSchedule[512+8];
|
---|
214 | /** @} */
|
---|
215 | } HDASTREAMSTATE;
|
---|
216 | AssertCompileSizeAlignment(HDASTREAMSTATE, 8);
|
---|
217 | AssertCompileMemberAlignment(HDASTREAMSTATE, aBdl, 16);
|
---|
218 | AssertCompileMemberAlignment(HDASTREAMSTATE, aSchedule, 16);
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * An HDA stream (SDI / SDO) - shared.
|
---|
222 | *
|
---|
223 | * @note This HDA stream has nothing to do with a regular audio stream handled
|
---|
224 | * by the audio connector or the audio mixer. This HDA stream is a serial
|
---|
225 | * data in/out stream (SDI/SDO) defined in hardware and can contain
|
---|
226 | * multiple audio streams in one single SDI/SDO (interleaving streams).
|
---|
227 | *
|
---|
228 | * How a specific SDI/SDO is mapped to our internal audio streams relies on the
|
---|
229 | * stream channel mappings.
|
---|
230 | *
|
---|
231 | * Contains only register values which do *not* change until a stream reset
|
---|
232 | * occurs.
|
---|
233 | */
|
---|
234 | typedef struct HDASTREAM
|
---|
235 | {
|
---|
236 | /** Stream descriptor number (SDn). */
|
---|
237 | uint8_t u8SD;
|
---|
238 | /** Current channel index.
|
---|
239 | * For a stereo stream, this is u8Channel + 1. */
|
---|
240 | uint8_t u8Channel;
|
---|
241 | uint8_t abPadding0[6];
|
---|
242 | #ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
243 | /** The stream's shared r0/r3 critical section to serialize access between the async I/O
|
---|
244 | * thread and (basically) the guest. */
|
---|
245 | PDMCRITSECT CritSect;
|
---|
246 | #endif
|
---|
247 | /** DMA base address (SDnBDPU - SDnBDPL).
|
---|
248 | * Will be updated in hdaR3StreamInit(). */
|
---|
249 | uint64_t u64BDLBase;
|
---|
250 | /** Cyclic Buffer Length (SDnCBL).
|
---|
251 | * Represents the size of the ring buffer.
|
---|
252 | * Will be updated in hdaR3StreamInit(). */
|
---|
253 | uint32_t u32CBL;
|
---|
254 | /** Format (SDnFMT).
|
---|
255 | * Will be updated in hdaR3StreamInit(). */
|
---|
256 | uint16_t u16FMT;
|
---|
257 | /** FIFO Size (checked + translated in bytes, FIFOS).
|
---|
258 | * Maximum number of bytes that may have been DMA'd into
|
---|
259 | * memory but not yet transmitted on the link.
|
---|
260 | *
|
---|
261 | * Will be updated in hdaR3StreamInit(). */
|
---|
262 | uint8_t u8FIFOS;
|
---|
263 | /** FIFO Watermark (checked + translated in bytes, FIFOW). */
|
---|
264 | uint8_t u8FIFOW;
|
---|
265 | uint8_t abPadding1[2];
|
---|
266 | /** FIFO scratch buffer, to avoid intermediate (re-)allocations. */
|
---|
267 | uint8_t abFIFO[HDA_FIFO_MAX + 1];
|
---|
268 | /** Last Valid Index (SDnLVI).
|
---|
269 | * Will be updated in hdaR3StreamInit(). */
|
---|
270 | uint16_t u16LVI;
|
---|
271 | /** The timer for pumping data thru the attached LUN drivers. */
|
---|
272 | TMTIMERHANDLE hTimer;
|
---|
273 | /** Internal state of this stream. */
|
---|
274 | HDASTREAMSTATE State;
|
---|
275 | } HDASTREAM;
|
---|
276 | /** Pointer to an HDA stream (SDI / SDO). */
|
---|
277 | typedef HDASTREAM *PHDASTREAM;
|
---|
278 |
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * An HDA stream (SDI / SDO) - ring-3 bits.
|
---|
282 | */
|
---|
283 | typedef struct HDASTREAMR3
|
---|
284 | {
|
---|
285 | /** Stream descriptor number (SDn). */
|
---|
286 | uint8_t u8SD;
|
---|
287 | uint8_t abPadding[7];
|
---|
288 | /** The shared state for the parent HDA device. */
|
---|
289 | R3PTRTYPE(PHDASTATE) pHDAStateShared;
|
---|
290 | /** The ring-3 state for the parent HDA device. */
|
---|
291 | R3PTRTYPE(PHDASTATER3) pHDAStateR3;
|
---|
292 | /** Pointer to HDA sink this stream is attached to. */
|
---|
293 | R3PTRTYPE(PHDAMIXERSINK) pMixSink;
|
---|
294 | /** Internal state of this stream. */
|
---|
295 | struct
|
---|
296 | {
|
---|
297 | /** This stream's data mapping. */
|
---|
298 | HDASTREAMMAP Mapping;
|
---|
299 | /** Circular buffer (FIFO) for holding DMA'ed data. */
|
---|
300 | R3PTRTYPE(PRTCIRCBUF) pCircBuf;
|
---|
301 | /** Current circular buffer read offset (for tracing & logging). */
|
---|
302 | uint64_t offRead;
|
---|
303 | /** Current circular buffer write offset (for tracing & logging). */
|
---|
304 | uint64_t offWrite;
|
---|
305 | #ifdef HDA_USE_DMA_ACCESS_HANDLER
|
---|
306 | /** List of DMA handlers. */
|
---|
307 | RTLISTANCHORR3 lstDMAHandlers;
|
---|
308 | #endif
|
---|
309 | #ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
310 | /** Asynchronous I/O state members. */
|
---|
311 | HDASTREAMSTATEAIO AIO;
|
---|
312 | #endif
|
---|
313 | /** Counter for all under/overflows problems. */
|
---|
314 | STAMCOUNTER StatDmaFlowProblems;
|
---|
315 | /** Counter for unresovled under/overflows problems. */
|
---|
316 | STAMCOUNTER StatDmaFlowErrors;
|
---|
317 | /** Number of bytes involved in unresolved flow errors. */
|
---|
318 | STAMCOUNTER StatDmaFlowErrorBytes;
|
---|
319 | } State;
|
---|
320 | /** Debug bits. */
|
---|
321 | HDASTREAMDEBUG Dbg;
|
---|
322 | } HDASTREAMR3;
|
---|
323 | /** Pointer to an HDA stream (SDI / SDO). */
|
---|
324 | typedef HDASTREAMR3 *PHDASTREAMR3;
|
---|
325 |
|
---|
326 | /** @name Stream functions (shared).
|
---|
327 | * @{
|
---|
328 | */
|
---|
329 | void hdaStreamLock(PHDASTREAM pStreamShared);
|
---|
330 | void hdaStreamUnlock(PHDASTREAM pStreamShared);
|
---|
331 | /** @} */
|
---|
332 |
|
---|
333 | #ifdef IN_RING3
|
---|
334 |
|
---|
335 | /** @name Stream functions (ring-3).
|
---|
336 | * @{
|
---|
337 | */
|
---|
338 | int hdaR3StreamConstruct(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, PHDASTATE pThis,
|
---|
339 | PHDASTATER3 pThisCC, uint8_t uSD);
|
---|
340 | void hdaR3StreamDestroy(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3);
|
---|
341 | int hdaR3StreamSetUp(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared,
|
---|
342 | PHDASTREAMR3 pStreamR3, uint8_t uSD);
|
---|
343 | void hdaR3StreamReset(PHDASTATE pThis, PHDASTATER3 pThisCC,
|
---|
344 | PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, uint8_t uSD);
|
---|
345 | int hdaR3StreamEnable(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, bool fEnable);
|
---|
346 | void hdaR3StreamMarkStarted(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared, uint64_t tsNow);
|
---|
347 | void hdaR3StreamMarkStopped(PHDASTREAM pStreamShared);
|
---|
348 |
|
---|
349 | void hdaR3StreamSetPositionAdd(PHDASTREAM pStreamShared, PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t uToAdd);
|
---|
350 | uint64_t hdaR3StreamTimerMain(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTATER3 pThisCC,
|
---|
351 | PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3);
|
---|
352 | void hdaR3StreamUpdate(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTATER3 pThisCC,
|
---|
353 | PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, bool fInTimer);
|
---|
354 | PHDASTREAM hdaR3StreamR3ToShared(PHDASTREAMR3 pStreamCC);
|
---|
355 | # ifdef HDA_USE_DMA_ACCESS_HANDLER
|
---|
356 | bool hdaR3StreamRegisterDMAHandlers(PHDASTREAM pStream);
|
---|
357 | void hdaR3StreamUnregisterDMAHandlers(PHDASTREAM pStream);
|
---|
358 | # endif
|
---|
359 | /** @} */
|
---|
360 |
|
---|
361 | /** @name Async I/O stream functions (ring-3).
|
---|
362 | * @{
|
---|
363 | */
|
---|
364 | # ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
365 | int hdaR3StreamAsyncIOCreate(PHDASTREAMR3 pStreamR3);
|
---|
366 | void hdaR3StreamAsyncIOLock(PHDASTREAMR3 pStreamR3);
|
---|
367 | void hdaR3StreamAsyncIOUnlock(PHDASTREAMR3 pStreamR3);
|
---|
368 | void hdaR3StreamAsyncIOEnable(PHDASTREAMR3 pStreamR3, bool fEnable);
|
---|
369 | int hdaR3StreamAsyncIONotify(PHDASTREAMR3 pStreamR3);
|
---|
370 | # endif /* VBOX_WITH_AUDIO_HDA_ASYNC_IO */
|
---|
371 | /** @} */
|
---|
372 |
|
---|
373 | #endif /* IN_RING3 */
|
---|
374 | #endif /* !VBOX_INCLUDED_SRC_Audio_HDAStream_h */
|
---|
375 |
|
---|