1 | /* $Id: HDAStream.h 73833 2018-08-22 15:40:27Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * HDAStream.h - Stream functions for HD Audio.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2018 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 HDA_STREAM_H
|
---|
19 | #define HDA_STREAM_H
|
---|
20 |
|
---|
21 | #include "DevHDACommon.h"
|
---|
22 |
|
---|
23 | #include "HDAStreamMap.h"
|
---|
24 | #include "HDAStreamPeriod.h"
|
---|
25 |
|
---|
26 |
|
---|
27 | typedef struct HDAMIXERSINK *PHDAMIXERSINK;
|
---|
28 |
|
---|
29 | #ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
30 | /**
|
---|
31 | * Structure keeping the 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 Thread;
|
---|
37 | /** Event for letting the thread know there is some data to process. */
|
---|
38 | RTSEMEVENT Event;
|
---|
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 | uint32_t Padding1;
|
---|
48 | } HDASTREAMSTATEAIO, *PHDASTREAMSTATEAIO;
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Structure containing HDA stream debug stuff, configurable at runtime.
|
---|
53 | */
|
---|
54 | typedef struct HDASTREAMDBGINFORT
|
---|
55 | {
|
---|
56 | /** Whether debugging is enabled or not. */
|
---|
57 | bool fEnabled;
|
---|
58 | uint8_t Padding[7];
|
---|
59 | /** File for dumping stream reads / writes.
|
---|
60 | * For input streams, this dumps data being written to the device FIFO,
|
---|
61 | * whereas for output streams this dumps data being read from the device FIFO. */
|
---|
62 | R3PTRTYPE(PPDMAUDIOFILE) pFileStream;
|
---|
63 | /** File for dumping DMA reads / writes.
|
---|
64 | * For input streams, this dumps data being written to the device DMA,
|
---|
65 | * whereas for output streams this dumps data being read from the device DMA. */
|
---|
66 | R3PTRTYPE(PPDMAUDIOFILE) pFileDMA;
|
---|
67 | } HDASTREAMDBGINFORT, *PHDASTREAMDBGINFORT;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Structure containing HDA stream debug information.
|
---|
71 | */
|
---|
72 | typedef struct HDASTREAMDBGINFO
|
---|
73 | {
|
---|
74 | #ifdef DEBUG
|
---|
75 | /** Critical section to serialize access if needed. */
|
---|
76 | RTCRITSECT CritSect;
|
---|
77 | uint32_t Padding0[2];
|
---|
78 | /** Number of total read accesses. */
|
---|
79 | uint64_t cReadsTotal;
|
---|
80 | /** Number of total DMA bytes read. */
|
---|
81 | uint64_t cbReadTotal;
|
---|
82 | /** Timestamp (in ns) of last read access. */
|
---|
83 | uint64_t tsLastReadNs;
|
---|
84 | /** Number of total write accesses. */
|
---|
85 | uint64_t cWritesTotal;
|
---|
86 | /** Number of total DMA bytes written. */
|
---|
87 | uint64_t cbWrittenTotal;
|
---|
88 | /** Number of total write accesses since last iteration (Hz). */
|
---|
89 | uint64_t cWritesHz;
|
---|
90 | /** Number of total DMA bytes written since last iteration (Hz). */
|
---|
91 | uint64_t cbWrittenHz;
|
---|
92 | /** Timestamp (in ns) of beginning a new write slot. */
|
---|
93 | uint64_t tsWriteSlotBegin;
|
---|
94 | /** Number of current silence samples in a (consecutive) row. */
|
---|
95 | uint64_t csSilence;
|
---|
96 | /** Number of silent samples in a row to consider an audio block as audio gap (silence). */
|
---|
97 | uint64_t cSilenceThreshold;
|
---|
98 | /** How many bytes to skip in an audio stream before detecting silence.
|
---|
99 | * (useful for intros and silence at the beginning of a song). */
|
---|
100 | uint64_t cbSilenceReadMin;
|
---|
101 | #endif
|
---|
102 | /** Runtime debug info. */
|
---|
103 | HDASTREAMDBGINFORT Runtime;
|
---|
104 | } HDASTREAMDBGINFO ,*PHDASTREAMDBGINFO;
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Internal state of a HDA stream.
|
---|
108 | */
|
---|
109 | typedef struct HDASTREAMSTATE
|
---|
110 | {
|
---|
111 | /** Current BDLE to use. Wraps around to 0 if
|
---|
112 | * maximum (cBDLE) is reached. */
|
---|
113 | uint16_t uCurBDLE;
|
---|
114 | /** Flag indicating whether this stream currently is
|
---|
115 | * in reset mode and therefore not acccessible by the guest. */
|
---|
116 | volatile bool fInReset;
|
---|
117 | /** Flag indicating if the stream is in running state or not. */
|
---|
118 | volatile bool fRunning;
|
---|
119 | /** Unused, padding. */
|
---|
120 | uint8_t Padding0[4];
|
---|
121 | #ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
122 | /** Asynchronous I/O state members. */
|
---|
123 | HDASTREAMSTATEAIO AIO;
|
---|
124 | #endif
|
---|
125 | /** This stream's data mapping. */
|
---|
126 | HDASTREAMMAPPING Mapping;
|
---|
127 | /** Current BDLE (Buffer Descriptor List Entry). */
|
---|
128 | HDABDLE BDLE;
|
---|
129 | /** Circular buffer (FIFO) for holding DMA'ed data. */
|
---|
130 | R3PTRTYPE(PRTCIRCBUF) pCircBuf;
|
---|
131 | #if HC_ARCH_BITS == 32
|
---|
132 | RTR3PTR Padding1;
|
---|
133 | #endif
|
---|
134 | /** Timestamp of the last DMA data transfer. */
|
---|
135 | uint64_t tsTransferLast;
|
---|
136 | /** Timestamp 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 | uint32_t cbTransferSize;
|
---|
142 | /** Transfer chunk size (in bytes) of a transfer period. */
|
---|
143 | uint32_t cbTransferChunk;
|
---|
144 | /** How many bytes already have been processed in within
|
---|
145 | * the current transfer period. */
|
---|
146 | uint32_t cbTransferProcessed;
|
---|
147 | /** How many interrupts are pending due to
|
---|
148 | * BDLE interrupt-on-completion (IOC) bits set. */
|
---|
149 | uint8_t cTransferPendingInterrupts;
|
---|
150 | /** The stream's current audio frame size (in bytes). */
|
---|
151 | uint32_t cbFrameSize;
|
---|
152 | /** How many audio data frames are left to be processed
|
---|
153 | * for the position adjustment handling.
|
---|
154 | *
|
---|
155 | * 0 if position adjustment handling is done or inactive. */
|
---|
156 | uint16_t cPosAdjustFramesLeft;
|
---|
157 | uint8_t Padding2[2];
|
---|
158 | /** (Virtual) clock ticks per byte. */
|
---|
159 | uint64_t cTicksPerByte;
|
---|
160 | /** (Virtual) clock ticks per transfer. */
|
---|
161 | uint64_t cTransferTicks;
|
---|
162 | /** The stream's period. Need for timing. */
|
---|
163 | HDASTREAMPERIOD Period;
|
---|
164 | /** The stream's current configuration.
|
---|
165 | * Should match SDFMT. */
|
---|
166 | PDMAUDIOSTREAMCFG Cfg;
|
---|
167 | uint32_t Padding3;
|
---|
168 | #ifdef HDA_USE_DMA_ACCESS_HANDLER
|
---|
169 | /** List of DMA handlers. */
|
---|
170 | RTLISTANCHORR3 lstDMAHandlers;
|
---|
171 | #endif
|
---|
172 | /** How much DMA data from a previous transfer is left to be processed (in bytes).
|
---|
173 | * This can happen if the stream's frame size is bigger (e.g. 16 bytes) than
|
---|
174 | * the current DMA FIFO can hold (e.g. 10 bytes). Mostly needed for more complex
|
---|
175 | * stuff like interleaved surround streams. */
|
---|
176 | uint16_t cbDMALeft;
|
---|
177 | /** Unused, padding. */
|
---|
178 | uint8_t abPadding4[2+4];
|
---|
179 | /** Timestamp (in ns) of last stream update. */
|
---|
180 | uint64_t tsLastUpdateNs;
|
---|
181 | } HDASTREAMSTATE;
|
---|
182 | AssertCompileSizeAlignment(HDASTREAMSTATE, 8);
|
---|
183 | typedef HDASTREAMSTATE *PHDASTREAMSTATE;
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Structure for keeping a HDA stream (SDI / SDO).
|
---|
187 | *
|
---|
188 | * Note: This HDA stream has nothing to do with a regular audio stream handled
|
---|
189 | * by the audio connector or the audio mixer. This HDA stream is a serial data in/out
|
---|
190 | * stream (SDI/SDO) defined in hardware and can contain multiple audio streams
|
---|
191 | * in one single SDI/SDO (interleaving streams).
|
---|
192 | *
|
---|
193 | * How a specific SDI/SDO is mapped to our internal audio streams relies on the
|
---|
194 | * stream channel mappings.
|
---|
195 | *
|
---|
196 | * Contains only register values which do *not* change until a
|
---|
197 | * stream reset occurs.
|
---|
198 | */
|
---|
199 | typedef struct HDASTREAM
|
---|
200 | {
|
---|
201 | /** Stream descriptor number (SDn). */
|
---|
202 | uint8_t u8SD;
|
---|
203 | /** Current channel index.
|
---|
204 | * For a stereo stream, this is u8Channel + 1. */
|
---|
205 | uint8_t u8Channel;
|
---|
206 | uint8_t Padding0[6];
|
---|
207 | /** DMA base address (SDnBDPU - SDnBDPL). */
|
---|
208 | uint64_t u64BDLBase;
|
---|
209 | /** Cyclic Buffer Length (SDnCBL).
|
---|
210 | * Represents the size of the ring buffer. */
|
---|
211 | uint32_t u32CBL;
|
---|
212 | /** Format (SDnFMT). */
|
---|
213 | uint16_t u16FMT;
|
---|
214 | /** FIFO Size (FIFOS).
|
---|
215 | * Maximum number of bytes that may have been DMA'd into
|
---|
216 | * memory but not yet transmitted on the link. */
|
---|
217 | uint16_t u16FIFOS;
|
---|
218 | /** FIFO Watermark. */
|
---|
219 | uint16_t u16FIFOW;
|
---|
220 | /** Last Valid Index (SDnLVI). */
|
---|
221 | uint16_t u16LVI;
|
---|
222 | uint16_t Padding1[2];
|
---|
223 | /** Pointer to the HDA state this stream is attached to. */
|
---|
224 | R3PTRTYPE(PHDASTATE) pHDAState;
|
---|
225 | /** Pointer to HDA sink this stream is attached to. */
|
---|
226 | R3PTRTYPE(PHDAMIXERSINK) pMixSink;
|
---|
227 | /** The stream'S critical section to serialize access. */
|
---|
228 | RTCRITSECT CritSect;
|
---|
229 | /** Pointer to the stream's timer. */
|
---|
230 | PTMTIMERR3 pTimer;
|
---|
231 | /** Internal state of this stream. */
|
---|
232 | HDASTREAMSTATE State;
|
---|
233 | /** Debug information. */
|
---|
234 | HDASTREAMDBGINFO Dbg;
|
---|
235 | } HDASTREAM, *PHDASTREAM;
|
---|
236 |
|
---|
237 | #ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
238 | /**
|
---|
239 | * Structure for keeping a HDA stream thread context.
|
---|
240 | */
|
---|
241 | typedef struct HDASTREAMTHREADCTX
|
---|
242 | {
|
---|
243 | PHDASTATE pThis;
|
---|
244 | PHDASTREAM pStream;
|
---|
245 | } HDASTREAMTHREADCTX, *PHDASTREAMTHREADCTX;
|
---|
246 | #endif
|
---|
247 |
|
---|
248 | #ifdef IN_RING3
|
---|
249 |
|
---|
250 | /** @name Stream functions.
|
---|
251 | * @{
|
---|
252 | */
|
---|
253 | int hdaR3StreamCreate(PHDASTREAM pStream, PHDASTATE pThis, uint8_t u8SD);
|
---|
254 | void hdaR3StreamDestroy(PHDASTREAM pStream);
|
---|
255 | int hdaR3StreamInit(PHDASTREAM pStream, uint8_t uSD);
|
---|
256 | void hdaR3StreamReset(PHDASTATE pThis, PHDASTREAM pStream, uint8_t uSD);
|
---|
257 | int hdaR3StreamEnable(PHDASTREAM pStream, bool fEnable);
|
---|
258 | uint32_t hdaR3StreamGetPosition(PHDASTATE pThis, PHDASTREAM pStream);
|
---|
259 | void hdaR3StreamSetPosition(PHDASTREAM pStream, uint32_t u32LPIB);
|
---|
260 | uint32_t hdaR3StreamGetFree(PHDASTREAM pStream);
|
---|
261 | uint32_t hdaR3StreamGetUsed(PHDASTREAM pStream);
|
---|
262 | bool hdaR3StreamTransferIsScheduled(PHDASTREAM pStream);
|
---|
263 | uint64_t hdaR3StreamTransferGetNext(PHDASTREAM pStream);
|
---|
264 | int hdaR3StreamTransfer(PHDASTREAM pStream, uint32_t cbToProcessMax);
|
---|
265 | void hdaR3StreamLock(PHDASTREAM pStream);
|
---|
266 | void hdaR3StreamUnlock(PHDASTREAM pStream);
|
---|
267 | int hdaR3StreamRead(PHDASTREAM pStream, uint32_t cbToRead, uint32_t *pcbRead);
|
---|
268 | int hdaR3StreamWrite(PHDASTREAM pStream, const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten);
|
---|
269 | void hdaR3StreamUpdate(PHDASTREAM pStream, bool fAsync);
|
---|
270 | # ifdef HDA_USE_DMA_ACCESS_HANDLER
|
---|
271 | bool hdaR3StreamRegisterDMAHandlers(PHDASTREAM pStream);
|
---|
272 | void hdaR3StreamUnregisterDMAHandlers(PHDASTREAM pStream);
|
---|
273 | # endif /* HDA_USE_DMA_ACCESS_HANDLER */
|
---|
274 | /** @} */
|
---|
275 |
|
---|
276 | /** @name Timer functions.
|
---|
277 | * @{
|
---|
278 | */
|
---|
279 | DECLCALLBACK(void) hdaR3StreamTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser);
|
---|
280 | /** @} */
|
---|
281 |
|
---|
282 |
|
---|
283 | /** @name Async I/O stream functions.
|
---|
284 | * @{
|
---|
285 | */
|
---|
286 | # ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
287 | DECLCALLBACK(int) hdaR3StreamAsyncIOThread(RTTHREAD hThreadSelf, void *pvUser);
|
---|
288 | int hdaR3StreamAsyncIOCreate(PHDASTREAM pStream);
|
---|
289 | int hdaR3StreamAsyncIODestroy(PHDASTREAM pStream);
|
---|
290 | int hdaR3StreamAsyncIONotify(PHDASTREAM pStream);
|
---|
291 | void hdaR3StreamAsyncIOLock(PHDASTREAM pStream);
|
---|
292 | void hdaR3StreamAsyncIOUnlock(PHDASTREAM pStream);
|
---|
293 | void hdaR3StreamAsyncIOEnable(PHDASTREAM pStream, bool fEnable);
|
---|
294 | # endif /* VBOX_WITH_AUDIO_HDA_ASYNC_IO */
|
---|
295 | /** @} */
|
---|
296 |
|
---|
297 | #endif /* IN_RING3 */
|
---|
298 | #endif /* !HDA_STREAM_H */
|
---|
299 |
|
---|