1 | /* $Id: HDAStream.h 70964 2018-02-11 21:25:29Z 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 | * Prototypes *
|
---|
28 | *********************************************************************************************************************************/
|
---|
29 |
|
---|
30 | typedef struct HDAMIXERSINK *PHDAMIXERSINK;
|
---|
31 |
|
---|
32 | #ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
33 | /**
|
---|
34 | * Structure keeping the HDA stream's state for asynchronous I/O.
|
---|
35 | */
|
---|
36 | typedef struct HDASTREAMSTATEAIO
|
---|
37 | {
|
---|
38 | /** Thread handle for the actual I/O thread. */
|
---|
39 | RTTHREAD Thread;
|
---|
40 | /** Event for letting the thread know there is some data to process. */
|
---|
41 | RTSEMEVENT Event;
|
---|
42 | /** Critical section for synchronizing access. */
|
---|
43 | RTCRITSECT CritSect;
|
---|
44 | /** Started indicator. */
|
---|
45 | volatile bool fStarted;
|
---|
46 | /** Shutdown indicator. */
|
---|
47 | volatile bool fShutdown;
|
---|
48 | /** Whether the thread should do any data processing or not. */
|
---|
49 | volatile bool fEnabled;
|
---|
50 | uint32_t Padding1;
|
---|
51 | } HDASTREAMSTATEAIO, *PHDASTREAMSTATEAIO;
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Structure containing HDA stream debug stuff, configurable at runtime.
|
---|
56 | */
|
---|
57 | typedef struct HDASTREAMDBGINFORT
|
---|
58 | {
|
---|
59 | /** Whether debugging is enabled or not. */
|
---|
60 | bool fEnabled;
|
---|
61 | uint8_t Padding[7];
|
---|
62 | /** File for dumping stream reads / writes.
|
---|
63 | * For input streams, this dumps data being written to the device FIFO,
|
---|
64 | * whereas for output streams this dumps data being read from the device FIFO. */
|
---|
65 | R3PTRTYPE(PPDMAUDIOFILE) pFileStream;
|
---|
66 | /** File for dumping DMA reads / writes.
|
---|
67 | * For input streams, this dumps data being written to the device DMA,
|
---|
68 | * whereas for output streams this dumps data being read from the device DMA. */
|
---|
69 | R3PTRTYPE(PPDMAUDIOFILE) pFileDMA;
|
---|
70 | } HDASTREAMDBGINFORT, *PHDASTREAMDBGINFORT;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Structure containing HDA stream debug information.
|
---|
74 | */
|
---|
75 | typedef struct HDASTREAMDBGINFO
|
---|
76 | {
|
---|
77 | #ifdef DEBUG
|
---|
78 | /** Critical section to serialize access if needed. */
|
---|
79 | RTCRITSECT CritSect;
|
---|
80 | uint32_t Padding0[2];
|
---|
81 | /** Number of total read accesses. */
|
---|
82 | uint64_t cReadsTotal;
|
---|
83 | /** Number of total DMA bytes read. */
|
---|
84 | uint64_t cbReadTotal;
|
---|
85 | /** Timestamp (in ns) of last read access. */
|
---|
86 | uint64_t tsLastReadNs;
|
---|
87 | /** Number of total write accesses. */
|
---|
88 | uint64_t cWritesTotal;
|
---|
89 | /** Number of total DMA bytes written. */
|
---|
90 | uint64_t cbWrittenTotal;
|
---|
91 | /** Number of total write accesses since last iteration (Hz). */
|
---|
92 | uint64_t cWritesHz;
|
---|
93 | /** Number of total DMA bytes written since last iteration (Hz). */
|
---|
94 | uint64_t cbWrittenHz;
|
---|
95 | /** Timestamp (in ns) of beginning a new write slot. */
|
---|
96 | uint64_t tsWriteSlotBegin;
|
---|
97 | /** Number of current silence samples in a (consecutive) row. */
|
---|
98 | uint64_t csSilence;
|
---|
99 | /** Number of silent samples in a row to consider an audio block as audio gap (silence). */
|
---|
100 | uint64_t cSilenceThreshold;
|
---|
101 | /** How many bytes to skip in an audio stream before detecting silence.
|
---|
102 | * (useful for intros and silence at the beginning of a song). */
|
---|
103 | uint64_t cbSilenceReadMin;
|
---|
104 | #endif
|
---|
105 | /** Runtime debug info. */
|
---|
106 | HDASTREAMDBGINFORT Runtime;
|
---|
107 | } HDASTREAMDBGINFO ,*PHDASTREAMDBGINFO;
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Internal state of a HDA stream.
|
---|
111 | */
|
---|
112 | typedef struct HDASTREAMSTATE
|
---|
113 | {
|
---|
114 | /** Current BDLE to use. Wraps around to 0 if
|
---|
115 | * maximum (cBDLE) is reached. */
|
---|
116 | uint16_t uCurBDLE;
|
---|
117 | /** Flag indicating whether this stream currently is
|
---|
118 | * in reset mode and therefore not acccessible by the guest. */
|
---|
119 | volatile bool fInReset;
|
---|
120 | /** Flag indicating if the stream is in running state or not. */
|
---|
121 | volatile bool fRunning;
|
---|
122 | /** Unused, padding. */
|
---|
123 | uint8_t Padding0[3];
|
---|
124 | #ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
125 | /** Asynchronous I/O state members. */
|
---|
126 | HDASTREAMSTATEAIO AIO;
|
---|
127 | #endif
|
---|
128 | /** This stream's data mapping. */
|
---|
129 | HDASTREAMMAPPING Mapping;
|
---|
130 | /** Current BDLE (Buffer Descriptor List Entry). */
|
---|
131 | HDABDLE BDLE;
|
---|
132 | /** Circular buffer (FIFO) for holding DMA'ed data. */
|
---|
133 | R3PTRTYPE(PRTCIRCBUF) pCircBuf;
|
---|
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 | #ifdef HDA_USE_DMA_ACCESS_HANDLER
|
---|
168 | /** List of DMA handlers. */
|
---|
169 | RTLISTANCHORR3 lstDMAHandlers;
|
---|
170 | #endif
|
---|
171 | /** How much DMA data from a previous transfer is left to be processed (in bytes).
|
---|
172 | * This can happen if the stream's frame size is bigger (e.g. 16 bytes) than
|
---|
173 | * the current DMA FIFO can hold (e.g. 10 bytes). Mostly needed for more complex
|
---|
174 | * stuff like interleaved surround streams. */
|
---|
175 | uint16_t cbDMALeft;
|
---|
176 | /** Unused, padding. */
|
---|
177 | uint8_t Padding3;
|
---|
178 | } HDASTREAMSTATE, *PHDASTREAMSTATE;
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Structure for keeping a HDA stream (SDI / SDO).
|
---|
182 | *
|
---|
183 | * Note: This HDA stream has nothing to do with a regular audio stream handled
|
---|
184 | * by the audio connector or the audio mixer. This HDA stream is a serial data in/out
|
---|
185 | * stream (SDI/SDO) defined in hardware and can contain multiple audio streams
|
---|
186 | * in one single SDI/SDO (interleaving streams).
|
---|
187 | *
|
---|
188 | * How a specific SDI/SDO is mapped to our internal audio streams relies on the
|
---|
189 | * stream channel mappings.
|
---|
190 | *
|
---|
191 | * Contains only register values which do *not* change until a
|
---|
192 | * stream reset occurs.
|
---|
193 | */
|
---|
194 | typedef struct HDASTREAM
|
---|
195 | {
|
---|
196 | /** Stream descriptor number (SDn). */
|
---|
197 | uint8_t u8SD;
|
---|
198 | /** Current channel index.
|
---|
199 | * For a stereo stream, this is u8Channel + 1. */
|
---|
200 | uint8_t u8Channel;
|
---|
201 | uint8_t Padding0[6];
|
---|
202 | /** DMA base address (SDnBDPU - SDnBDPL). */
|
---|
203 | uint64_t u64BDLBase;
|
---|
204 | /** Cyclic Buffer Length (SDnCBL).
|
---|
205 | * Represents the size of the ring buffer. */
|
---|
206 | uint32_t u32CBL;
|
---|
207 | /** Format (SDnFMT). */
|
---|
208 | uint16_t u16FMT;
|
---|
209 | /** FIFO Size (FIFOS).
|
---|
210 | * Maximum number of bytes that may have been DMA'd into
|
---|
211 | * memory but not yet transmitted on the link. */
|
---|
212 | uint16_t u16FIFOS;
|
---|
213 | /** FIFO Watermark. */
|
---|
214 | uint16_t u16FIFOW;
|
---|
215 | /** Last Valid Index (SDnLVI). */
|
---|
216 | uint16_t u16LVI;
|
---|
217 | uint16_t Padding1[2];
|
---|
218 | /** Pointer to the HDA state this stream is attached to. */
|
---|
219 | R3PTRTYPE(PHDASTATE) pHDAState;
|
---|
220 | /** Pointer to HDA sink this stream is attached to. */
|
---|
221 | R3PTRTYPE(PHDAMIXERSINK) pMixSink;
|
---|
222 | /** The stream'S critical section to serialize access. */
|
---|
223 | RTCRITSECT CritSect;
|
---|
224 | /** Pointer to the stream's timer. */
|
---|
225 | PTMTIMERR3 pTimer;
|
---|
226 | /** Internal state of this stream. */
|
---|
227 | HDASTREAMSTATE State;
|
---|
228 | /** Debug information. */
|
---|
229 | HDASTREAMDBGINFO Dbg;
|
---|
230 | } HDASTREAM, *PHDASTREAM;
|
---|
231 |
|
---|
232 | #ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
233 | /**
|
---|
234 | * Structure for keeping a HDA stream thread context.
|
---|
235 | */
|
---|
236 | typedef struct HDASTREAMTHREADCTX
|
---|
237 | {
|
---|
238 | PHDASTATE pThis;
|
---|
239 | PHDASTREAM pStream;
|
---|
240 | } HDASTREAMTHREADCTX, *PHDASTREAMTHREADCTX;
|
---|
241 | #endif
|
---|
242 |
|
---|
243 | #ifdef IN_RING3
|
---|
244 |
|
---|
245 | /** @name Stream functions.
|
---|
246 | * @{
|
---|
247 | */
|
---|
248 | int hdaStreamCreate(PHDASTREAM pStream, PHDASTATE pThis, uint8_t u8SD);
|
---|
249 | void hdaStreamDestroy(PHDASTREAM pStream);
|
---|
250 | int hdaStreamInit(PHDASTREAM pStream, uint8_t uSD);
|
---|
251 | void hdaStreamReset(PHDASTATE pThis, PHDASTREAM pStream, uint8_t uSD);
|
---|
252 | int hdaStreamEnable(PHDASTREAM pStream, bool fEnable);
|
---|
253 | uint32_t hdaStreamGetPosition(PHDASTATE pThis, PHDASTREAM pStream);
|
---|
254 | void hdaStreamSetPosition(PHDASTREAM pStream, uint32_t u32LPIB);
|
---|
255 | uint32_t hdaStreamGetFree(PHDASTREAM pStream);
|
---|
256 | uint32_t hdaStreamGetUsed(PHDASTREAM pStream);
|
---|
257 | bool hdaStreamTransferIsScheduled(PHDASTREAM pStream);
|
---|
258 | uint64_t hdaStreamTransferGetNext(PHDASTREAM pStream);
|
---|
259 | int hdaStreamTransfer(PHDASTREAM pStream, uint32_t cbToProcessMax);
|
---|
260 | void hdaStreamLock(PHDASTREAM pStream);
|
---|
261 | void hdaStreamUnlock(PHDASTREAM pStream);
|
---|
262 | int hdaStreamRead(PHDASTREAM pStream, uint32_t cbToRead, uint32_t *pcbRead);
|
---|
263 | int hdaStreamWrite(PHDASTREAM pStream, const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten);
|
---|
264 | void hdaStreamUpdate(PHDASTREAM pStream, bool fAsync);
|
---|
265 | # ifdef HDA_USE_DMA_ACCESS_HANDLER
|
---|
266 | bool hdaStreamRegisterDMAHandlers(PHDASTREAM pStream);
|
---|
267 | void hdaStreamUnregisterDMAHandlers(PHDASTREAM pStream);
|
---|
268 | # endif /* HDA_USE_DMA_ACCESS_HANDLER */
|
---|
269 | /** @} */
|
---|
270 |
|
---|
271 | /** @name Timer functions.
|
---|
272 | * @{
|
---|
273 | */
|
---|
274 | DECLCALLBACK(void) hdaStreamTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser);
|
---|
275 | /** @} */
|
---|
276 |
|
---|
277 |
|
---|
278 | /** @name Async I/O stream functions.
|
---|
279 | * @{
|
---|
280 | */
|
---|
281 | # ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
282 | DECLCALLBACK(int) hdaStreamAsyncIOThread(RTTHREAD hThreadSelf, void *pvUser);
|
---|
283 | int hdaStreamAsyncIOCreate(PHDASTREAM pStream);
|
---|
284 | int hdaStreamAsyncIODestroy(PHDASTREAM pStream);
|
---|
285 | int hdaStreamAsyncIONotify(PHDASTREAM pStream);
|
---|
286 | void hdaStreamAsyncIOLock(PHDASTREAM pStream);
|
---|
287 | void hdaStreamAsyncIOUnlock(PHDASTREAM pStream);
|
---|
288 | void hdaStreamAsyncIOEnable(PHDASTREAM pStream, bool fEnable);
|
---|
289 | # endif /* VBOX_WITH_AUDIO_HDA_ASYNC_IO */
|
---|
290 | /** @} */
|
---|
291 |
|
---|
292 | #endif /* IN_RING3 */
|
---|
293 | #endif /* !HDA_STREAM_H */
|
---|
294 |
|
---|