1 | /* $Id: HDAStream.h 82968 2020-02-04 10:35:17Z 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 | /** Current BDLE to use. Wraps around to 0 if
|
---|
117 | * maximum (cBDLE) is reached. */
|
---|
118 | uint16_t uCurBDLE;
|
---|
119 | /** Flag indicating whether this stream currently is
|
---|
120 | * in reset mode and therefore not acccessible by the guest. */
|
---|
121 | volatile bool fInReset;
|
---|
122 | /** Flag indicating if the stream is in running state or not. */
|
---|
123 | volatile bool fRunning;
|
---|
124 | /** Unused, padding. */
|
---|
125 | uint8_t abPadding0[4];
|
---|
126 | /** Current BDLE (Buffer Descriptor List Entry). */
|
---|
127 | HDABDLE BDLE;
|
---|
128 | /** Timestamp of the last DMA data transfer. */
|
---|
129 | uint64_t tsTransferLast;
|
---|
130 | /** Timestamp of the next DMA data transfer.
|
---|
131 | * Next for determining the next scheduling window.
|
---|
132 | * Can be 0 if no next transfer is scheduled. */
|
---|
133 | uint64_t tsTransferNext;
|
---|
134 | /** Total transfer size (in bytes) of a transfer period. */
|
---|
135 | uint32_t cbTransferSize;
|
---|
136 | /** Transfer chunk size (in bytes) of a transfer period. */
|
---|
137 | uint32_t cbTransferChunk;
|
---|
138 | /** How many bytes already have been processed in within
|
---|
139 | * the current transfer period. */
|
---|
140 | uint32_t cbTransferProcessed;
|
---|
141 | /** How many interrupts are pending due to
|
---|
142 | * BDLE interrupt-on-completion (IOC) bits set. */
|
---|
143 | uint8_t cTransferPendingInterrupts;
|
---|
144 | uint8_t abPadding2[3];
|
---|
145 | /** The stream's timer Hz rate.
|
---|
146 | * This value can can be different from the device's default Hz rate,
|
---|
147 | * depending on the rate the stream expects (e.g. for 5.1 speaker setups).
|
---|
148 | * Set in hdaR3StreamInit(). */
|
---|
149 | uint16_t uTimerHz;
|
---|
150 | /** Number of audio data frames for the position adjustment.
|
---|
151 | * 0 if no position adjustment is needed. */
|
---|
152 | uint16_t cfPosAdjustDefault;
|
---|
153 | /** How many audio data frames are left to be processed
|
---|
154 | * for the position adjustment handling.
|
---|
155 | *
|
---|
156 | * 0 if position adjustment handling is done or inactive. */
|
---|
157 | uint16_t cfPosAdjustLeft;
|
---|
158 | uint16_t u16Padding3;
|
---|
159 | /** (Virtual) clock ticks per byte. */
|
---|
160 | uint64_t cTicksPerByte;
|
---|
161 | /** (Virtual) clock ticks per transfer. */
|
---|
162 | uint64_t cTransferTicks;
|
---|
163 | /** The stream's period. Need for timing. */
|
---|
164 | HDASTREAMPERIOD Period;
|
---|
165 | /** The stream's current configuration.
|
---|
166 | * Should match SDFMT. */
|
---|
167 | PDMAUDIOSTREAMCFG Cfg;
|
---|
168 | /** Timestamp (in ns) of last stream update. */
|
---|
169 | uint64_t tsLastUpdateNs;
|
---|
170 | } HDASTREAMSTATE;
|
---|
171 | AssertCompileSizeAlignment(HDASTREAMSTATE, 8);
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * An HDA stream (SDI / SDO) - shared.
|
---|
175 | *
|
---|
176 | * @note This HDA stream has nothing to do with a regular audio stream handled
|
---|
177 | * by the audio connector or the audio mixer. This HDA stream is a serial
|
---|
178 | * data in/out stream (SDI/SDO) defined in hardware and can contain
|
---|
179 | * multiple audio streams in one single SDI/SDO (interleaving streams).
|
---|
180 | *
|
---|
181 | * How a specific SDI/SDO is mapped to our internal audio streams relies on the
|
---|
182 | * stream channel mappings.
|
---|
183 | *
|
---|
184 | * Contains only register values which do *not* change until a stream reset
|
---|
185 | * occurs.
|
---|
186 | */
|
---|
187 | typedef struct HDASTREAM
|
---|
188 | {
|
---|
189 | /** Stream descriptor number (SDn). */
|
---|
190 | uint8_t u8SD;
|
---|
191 | /** Current channel index.
|
---|
192 | * For a stereo stream, this is u8Channel + 1. */
|
---|
193 | uint8_t u8Channel;
|
---|
194 | uint8_t abPadding0[6];
|
---|
195 | /** DMA base address (SDnBDPU - SDnBDPL).
|
---|
196 | * Will be updated in hdaR3StreamInit(). */
|
---|
197 | uint64_t u64BDLBase;
|
---|
198 | /** Cyclic Buffer Length (SDnCBL).
|
---|
199 | * Represents the size of the ring buffer.
|
---|
200 | * Will be updated in hdaR3StreamInit(). */
|
---|
201 | uint32_t u32CBL;
|
---|
202 | /** Format (SDnFMT).
|
---|
203 | * Will be updated in hdaR3StreamInit(). */
|
---|
204 | uint16_t u16FMT;
|
---|
205 | /** FIFO Size (FIFOS).
|
---|
206 | * Maximum number of bytes that may have been DMA'd into
|
---|
207 | * memory but not yet transmitted on the link.
|
---|
208 | *
|
---|
209 | * Will be updated in hdaR3StreamInit(). */
|
---|
210 | uint16_t u16FIFOS;
|
---|
211 | /** FIFO Watermark. */
|
---|
212 | uint16_t u16FIFOW;
|
---|
213 | /** Last Valid Index (SDnLVI).
|
---|
214 | * Will be updated in hdaR3StreamInit(). */
|
---|
215 | uint16_t u16LVI;
|
---|
216 | uint16_t au16Padding1[2];
|
---|
217 | /** The timer for pumping data thru the attached LUN drivers. */
|
---|
218 | TMTIMERHANDLE hTimer;
|
---|
219 | /** Internal state of this stream. */
|
---|
220 | HDASTREAMSTATE State;
|
---|
221 | } HDASTREAM;
|
---|
222 | /** Pointer to an HDA stream (SDI / SDO). */
|
---|
223 | typedef HDASTREAM *PHDASTREAM;
|
---|
224 |
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * An HDA stream (SDI / SDO) - ring-3 bits.
|
---|
228 | */
|
---|
229 | typedef struct HDASTREAMR3
|
---|
230 | {
|
---|
231 | /** Stream descriptor number (SDn). */
|
---|
232 | uint8_t u8SD;
|
---|
233 | uint8_t abPadding[7];
|
---|
234 | /** The shared state for the parent HDA device. */
|
---|
235 | R3PTRTYPE(PHDASTATE) pHDAStateShared;
|
---|
236 | /** The ring-3 state for the parent HDA device. */
|
---|
237 | R3PTRTYPE(PHDASTATER3) pHDAStateR3;
|
---|
238 | /** Pointer to HDA sink this stream is attached to. */
|
---|
239 | R3PTRTYPE(PHDAMIXERSINK) pMixSink;
|
---|
240 | #ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
241 | /** The stream's critical section to serialize access between the async I/O
|
---|
242 | * thread and (basically) the guest. */
|
---|
243 | RTCRITSECT CritSect;
|
---|
244 | #endif
|
---|
245 | /** Internal state of this stream. */
|
---|
246 | struct
|
---|
247 | {
|
---|
248 | /** This stream's data mapping. */
|
---|
249 | HDASTREAMMAP Mapping;
|
---|
250 | /** Circular buffer (FIFO) for holding DMA'ed data. */
|
---|
251 | R3PTRTYPE(PRTCIRCBUF) pCircBuf;
|
---|
252 | #ifdef HDA_USE_DMA_ACCESS_HANDLER
|
---|
253 | /** List of DMA handlers. */
|
---|
254 | RTLISTANCHORR3 lstDMAHandlers;
|
---|
255 | #endif
|
---|
256 | #ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
257 | /** Asynchronous I/O state members. */
|
---|
258 | HDASTREAMSTATEAIO AIO;
|
---|
259 | #endif
|
---|
260 | } State;
|
---|
261 | /** Debug bits. */
|
---|
262 | HDASTREAMDEBUG Dbg;
|
---|
263 | } HDASTREAMR3;
|
---|
264 | /** Pointer to an HDA stream (SDI / SDO). */
|
---|
265 | typedef HDASTREAMR3 *PHDASTREAMR3;
|
---|
266 |
|
---|
267 | #ifdef IN_RING3
|
---|
268 |
|
---|
269 | /** @name Stream functions.
|
---|
270 | * @{
|
---|
271 | */
|
---|
272 | int hdaR3StreamConstruct(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, PHDASTATE pThis,
|
---|
273 | PHDASTATER3 pThisCC, uint8_t uSD);
|
---|
274 | void hdaR3StreamDestroy(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3);
|
---|
275 | int hdaR3StreamSetUp(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared,
|
---|
276 | PHDASTREAMR3 pStreamR3, uint8_t uSD);
|
---|
277 | void hdaR3StreamReset(PHDASTATE pThis, PHDASTATER3 pThisCC,
|
---|
278 | PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, uint8_t uSD);
|
---|
279 | int hdaR3StreamEnable(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, bool fEnable);
|
---|
280 | /* uint32_t hdaR3StreamGetPosition(PHDASTATE pThis, PHDASTREAMR3 pStreamShared); - only used in HDAStream.cpp */
|
---|
281 | /*void hdaR3StreamSetPosition(PHDASTREAM pStream, PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t u32LPIB); - only used in HDAStream.cpp */
|
---|
282 | /*uint32_t hdaR3StreamGetFree(PHDASTREAM pStream); - only used in HDAStream.cpp */
|
---|
283 | /*uint32_t hdaR3StreamGetUsed(PHDASTREAM pStream); - only used in HDAStream.cpp */
|
---|
284 | bool hdaR3StreamTransferIsScheduled(PHDASTREAM pStreamShared, uint64_t tsNow);
|
---|
285 | uint64_t hdaR3StreamTransferGetNext(PHDASTREAM pStreamShared);
|
---|
286 | void hdaR3StreamLock(PHDASTREAMR3 pStreamR3);
|
---|
287 | void hdaR3StreamUnlock(PHDASTREAMR3 pStreamR3);
|
---|
288 | /* int hdaR3StreamRead(PHDASTREAM pStream, uint32_t cbToRead, uint32_t *pcbRead); - only used in HDAStream.cpp */
|
---|
289 | /*int hdaR3StreamWrite(PHDASTREAM pStream, const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten); - only used in HDAStream.cpp */
|
---|
290 | void hdaR3StreamUpdate(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTATER3 pThisCC,
|
---|
291 | PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, bool fInTimer);
|
---|
292 | PHDASTREAM hdaR3StreamR3ToShared(PHDASTREAMR3 pStreamCC);
|
---|
293 | # ifdef HDA_USE_DMA_ACCESS_HANDLER
|
---|
294 | bool hdaR3StreamRegisterDMAHandlers(PHDASTREAM pStream);
|
---|
295 | void hdaR3StreamUnregisterDMAHandlers(PHDASTREAM pStream);
|
---|
296 | # endif
|
---|
297 | /** @} */
|
---|
298 |
|
---|
299 | /** @name Async I/O stream functions.
|
---|
300 | * @{
|
---|
301 | */
|
---|
302 | # ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
303 | int hdaR3StreamAsyncIOCreate(PHDASTREAMR3 pStreamR3);
|
---|
304 | void hdaR3StreamAsyncIOLock(PHDASTREAMR3 pStreamR3);
|
---|
305 | void hdaR3StreamAsyncIOUnlock(PHDASTREAMR3 pStreamR3);
|
---|
306 | void hdaR3StreamAsyncIOEnable(PHDASTREAMR3 pStreamR3, bool fEnable);
|
---|
307 | # endif /* VBOX_WITH_AUDIO_HDA_ASYNC_IO */
|
---|
308 | /** @} */
|
---|
309 |
|
---|
310 | #endif /* IN_RING3 */
|
---|
311 | #endif /* !VBOX_INCLUDED_SRC_Audio_HDAStream_h */
|
---|
312 |
|
---|