1 | /* $Id: HDAStream.h 76565 2019-01-01 04:23:20Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * HDAStream.h - Stream functions for HD Audio.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2019 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 |
|
---|
26 | #include "HDAStreamMap.h"
|
---|
27 | #include "HDAStreamPeriod.h"
|
---|
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 raw 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) pFileDMARaw;
|
---|
70 | /** File for dumping mapped (that is, extracted) DMA reads / writes. */
|
---|
71 | R3PTRTYPE(PPDMAUDIOFILE) pFileDMAMapped;
|
---|
72 | } HDASTREAMDBGINFORT, *PHDASTREAMDBGINFORT;
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Structure containing HDA stream debug information.
|
---|
76 | */
|
---|
77 | typedef struct HDASTREAMDBGINFO
|
---|
78 | {
|
---|
79 | #ifdef DEBUG
|
---|
80 | /** Critical section to serialize access if needed. */
|
---|
81 | RTCRITSECT CritSect;
|
---|
82 | uint32_t Padding0[2];
|
---|
83 | /** Number of total read accesses. */
|
---|
84 | uint64_t cReadsTotal;
|
---|
85 | /** Number of total DMA bytes read. */
|
---|
86 | uint64_t cbReadTotal;
|
---|
87 | /** Timestamp (in ns) of last read access. */
|
---|
88 | uint64_t tsLastReadNs;
|
---|
89 | /** Number of total write accesses. */
|
---|
90 | uint64_t cWritesTotal;
|
---|
91 | /** Number of total DMA bytes written. */
|
---|
92 | uint64_t cbWrittenTotal;
|
---|
93 | /** Number of total write accesses since last iteration (Hz). */
|
---|
94 | uint64_t cWritesHz;
|
---|
95 | /** Number of total DMA bytes written since last iteration (Hz). */
|
---|
96 | uint64_t cbWrittenHz;
|
---|
97 | /** Timestamp (in ns) of beginning a new write slot. */
|
---|
98 | uint64_t tsWriteSlotBegin;
|
---|
99 | /** Number of current silence samples in a (consecutive) row. */
|
---|
100 | uint64_t csSilence;
|
---|
101 | /** Number of silent samples in a row to consider an audio block as audio gap (silence). */
|
---|
102 | uint64_t cSilenceThreshold;
|
---|
103 | /** How many bytes to skip in an audio stream before detecting silence.
|
---|
104 | * (useful for intros and silence at the beginning of a song). */
|
---|
105 | uint64_t cbSilenceReadMin;
|
---|
106 | #endif
|
---|
107 | /** Runtime debug info. */
|
---|
108 | HDASTREAMDBGINFORT Runtime;
|
---|
109 | } HDASTREAMDBGINFO ,*PHDASTREAMDBGINFO;
|
---|
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 Padding0[4];
|
---|
126 | #ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
127 | /** Asynchronous I/O state members. */
|
---|
128 | HDASTREAMSTATEAIO AIO;
|
---|
129 | #endif
|
---|
130 | /** This stream's data mapping. */
|
---|
131 | HDASTREAMMAP Mapping;
|
---|
132 | /** Current BDLE (Buffer Descriptor List Entry). */
|
---|
133 | HDABDLE BDLE;
|
---|
134 | /** Circular buffer (FIFO) for holding DMA'ed data. */
|
---|
135 | R3PTRTYPE(PRTCIRCBUF) pCircBuf;
|
---|
136 | #if HC_ARCH_BITS == 32
|
---|
137 | RTR3PTR Padding1;
|
---|
138 | #endif
|
---|
139 | /** Timestamp of the last DMA data transfer. */
|
---|
140 | uint64_t tsTransferLast;
|
---|
141 | /** Timestamp of the next DMA data transfer.
|
---|
142 | * Next for determining the next scheduling window.
|
---|
143 | * Can be 0 if no next transfer is scheduled. */
|
---|
144 | uint64_t tsTransferNext;
|
---|
145 | /** Total transfer size (in bytes) of a transfer period. */
|
---|
146 | uint32_t cbTransferSize;
|
---|
147 | /** Transfer chunk size (in bytes) of a transfer period. */
|
---|
148 | uint32_t cbTransferChunk;
|
---|
149 | /** How many bytes already have been processed in within
|
---|
150 | * the current transfer period. */
|
---|
151 | uint32_t cbTransferProcessed;
|
---|
152 | /** How many interrupts are pending due to
|
---|
153 | * BDLE interrupt-on-completion (IOC) bits set. */
|
---|
154 | uint8_t cTransferPendingInterrupts;
|
---|
155 | uint8_t Padding2[2];
|
---|
156 | /** The stream's timer Hz rate.
|
---|
157 | * This value can can be different from the device's default Hz rate,
|
---|
158 | * depending on the rate the stream expects (e.g. for 5.1 speaker setups).
|
---|
159 | * Set in hdaR3StreamInit(). */
|
---|
160 | uint16_t uTimerHz;
|
---|
161 | /** Number of audio data frames for the position adjustment.
|
---|
162 | * 0 if no position adjustment is needed. */
|
---|
163 | uint16_t cfPosAdjustDefault;
|
---|
164 | /** How many audio data frames are left to be processed
|
---|
165 | * for the position adjustment handling.
|
---|
166 | *
|
---|
167 | * 0 if position adjustment handling is done or inactive. */
|
---|
168 | uint16_t cfPosAdjustLeft;
|
---|
169 | /** (Virtual) clock ticks per byte. */
|
---|
170 | uint64_t cTicksPerByte;
|
---|
171 | /** (Virtual) clock ticks per transfer. */
|
---|
172 | uint64_t cTransferTicks;
|
---|
173 | /** The stream's period. Need for timing. */
|
---|
174 | HDASTREAMPERIOD Period;
|
---|
175 | /** The stream's current configuration.
|
---|
176 | * Should match SDFMT. */
|
---|
177 | PDMAUDIOSTREAMCFG Cfg;
|
---|
178 | uint32_t Padding4;
|
---|
179 | #ifdef HDA_USE_DMA_ACCESS_HANDLER
|
---|
180 | /** List of DMA handlers. */
|
---|
181 | RTLISTANCHORR3 lstDMAHandlers;
|
---|
182 | #endif
|
---|
183 | /** Timestamp (in ns) of last stream update. */
|
---|
184 | uint64_t tsLastUpdateNs;
|
---|
185 | } HDASTREAMSTATE;
|
---|
186 | AssertCompileSizeAlignment(HDASTREAMSTATE, 8);
|
---|
187 | typedef HDASTREAMSTATE *PHDASTREAMSTATE;
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Structure for keeping a HDA stream (SDI / SDO).
|
---|
191 | *
|
---|
192 | * Note: This HDA stream has nothing to do with a regular audio stream handled
|
---|
193 | * by the audio connector or the audio mixer. This HDA stream is a serial data in/out
|
---|
194 | * stream (SDI/SDO) defined in hardware and can contain multiple audio streams
|
---|
195 | * in one single SDI/SDO (interleaving streams).
|
---|
196 | *
|
---|
197 | * How a specific SDI/SDO is mapped to our internal audio streams relies on the
|
---|
198 | * stream channel mappings.
|
---|
199 | *
|
---|
200 | * Contains only register values which do *not* change until a
|
---|
201 | * stream reset occurs.
|
---|
202 | */
|
---|
203 | typedef struct HDASTREAM
|
---|
204 | {
|
---|
205 | /** Stream descriptor number (SDn). */
|
---|
206 | uint8_t u8SD;
|
---|
207 | /** Current channel index.
|
---|
208 | * For a stereo stream, this is u8Channel + 1. */
|
---|
209 | uint8_t u8Channel;
|
---|
210 | uint8_t Padding0[6];
|
---|
211 | /** DMA base address (SDnBDPU - SDnBDPL).
|
---|
212 | * Will be updated in hdaR3StreamInit(). */
|
---|
213 | uint64_t u64BDLBase;
|
---|
214 | /** Cyclic Buffer Length (SDnCBL).
|
---|
215 | * Represents the size of the ring buffer.
|
---|
216 | * Will be updated in hdaR3StreamInit(). */
|
---|
217 | uint32_t u32CBL;
|
---|
218 | /** Format (SDnFMT).
|
---|
219 | * Will be updated in hdaR3StreamInit(). */
|
---|
220 | uint16_t u16FMT;
|
---|
221 | /** FIFO Size (FIFOS).
|
---|
222 | * Maximum number of bytes that may have been DMA'd into
|
---|
223 | * memory but not yet transmitted on the link.
|
---|
224 | *
|
---|
225 | * Will be updated in hdaR3StreamInit(). */
|
---|
226 | uint16_t u16FIFOS;
|
---|
227 | /** FIFO Watermark. */
|
---|
228 | uint16_t u16FIFOW;
|
---|
229 | /** Last Valid Index (SDnLVI).
|
---|
230 | * Will be updated in hdaR3StreamInit(). */
|
---|
231 | uint16_t u16LVI;
|
---|
232 | uint16_t Padding1[2];
|
---|
233 | /** Pointer to the HDA state this stream is attached to. */
|
---|
234 | R3PTRTYPE(PHDASTATE) pHDAState;
|
---|
235 | /** Pointer to HDA sink this stream is attached to. */
|
---|
236 | R3PTRTYPE(PHDAMIXERSINK) pMixSink;
|
---|
237 | /** The stream'S critical section to serialize access. */
|
---|
238 | RTCRITSECT CritSect;
|
---|
239 | /** Pointer to the stream's timer. */
|
---|
240 | PTMTIMERR3 pTimer;
|
---|
241 | /** Internal state of this stream. */
|
---|
242 | HDASTREAMSTATE State;
|
---|
243 | /** Debug information. */
|
---|
244 | HDASTREAMDBGINFO Dbg;
|
---|
245 | } HDASTREAM, *PHDASTREAM;
|
---|
246 |
|
---|
247 | #ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
248 | /**
|
---|
249 | * Structure for keeping a HDA stream thread context.
|
---|
250 | */
|
---|
251 | typedef struct HDASTREAMTHREADCTX
|
---|
252 | {
|
---|
253 | PHDASTATE pThis;
|
---|
254 | PHDASTREAM pStream;
|
---|
255 | } HDASTREAMTHREADCTX, *PHDASTREAMTHREADCTX;
|
---|
256 | #endif
|
---|
257 |
|
---|
258 | #ifdef IN_RING3
|
---|
259 |
|
---|
260 | /** @name Stream functions.
|
---|
261 | * @{
|
---|
262 | */
|
---|
263 | int hdaR3StreamCreate(PHDASTREAM pStream, PHDASTATE pThis, uint8_t u8SD);
|
---|
264 | void hdaR3StreamDestroy(PHDASTREAM pStream);
|
---|
265 | int hdaR3StreamInit(PHDASTREAM pStream, uint8_t uSD);
|
---|
266 | void hdaR3StreamReset(PHDASTATE pThis, PHDASTREAM pStream, uint8_t uSD);
|
---|
267 | int hdaR3StreamEnable(PHDASTREAM pStream, bool fEnable);
|
---|
268 | uint32_t hdaR3StreamGetPosition(PHDASTATE pThis, PHDASTREAM pStream);
|
---|
269 | void hdaR3StreamSetPosition(PHDASTREAM pStream, uint32_t u32LPIB);
|
---|
270 | uint32_t hdaR3StreamGetFree(PHDASTREAM pStream);
|
---|
271 | uint32_t hdaR3StreamGetUsed(PHDASTREAM pStream);
|
---|
272 | bool hdaR3StreamTransferIsScheduled(PHDASTREAM pStream);
|
---|
273 | uint64_t hdaR3StreamTransferGetNext(PHDASTREAM pStream);
|
---|
274 | int hdaR3StreamTransfer(PHDASTREAM pStream, uint32_t cbToProcessMax);
|
---|
275 | void hdaR3StreamLock(PHDASTREAM pStream);
|
---|
276 | void hdaR3StreamUnlock(PHDASTREAM pStream);
|
---|
277 | int hdaR3StreamRead(PHDASTREAM pStream, uint32_t cbToRead, uint32_t *pcbRead);
|
---|
278 | int hdaR3StreamWrite(PHDASTREAM pStream, const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten);
|
---|
279 | void hdaR3StreamUpdate(PHDASTREAM pStream, bool fAsync);
|
---|
280 | # ifdef HDA_USE_DMA_ACCESS_HANDLER
|
---|
281 | bool hdaR3StreamRegisterDMAHandlers(PHDASTREAM pStream);
|
---|
282 | void hdaR3StreamUnregisterDMAHandlers(PHDASTREAM pStream);
|
---|
283 | # endif /* HDA_USE_DMA_ACCESS_HANDLER */
|
---|
284 | /** @} */
|
---|
285 |
|
---|
286 | /** @name Timer functions.
|
---|
287 | * @{
|
---|
288 | */
|
---|
289 | DECLCALLBACK(void) hdaR3StreamTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser);
|
---|
290 | /** @} */
|
---|
291 |
|
---|
292 |
|
---|
293 | /** @name Async I/O stream functions.
|
---|
294 | * @{
|
---|
295 | */
|
---|
296 | # ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
297 | DECLCALLBACK(int) hdaR3StreamAsyncIOThread(RTTHREAD hThreadSelf, void *pvUser);
|
---|
298 | int hdaR3StreamAsyncIOCreate(PHDASTREAM pStream);
|
---|
299 | int hdaR3StreamAsyncIODestroy(PHDASTREAM pStream);
|
---|
300 | int hdaR3StreamAsyncIONotify(PHDASTREAM pStream);
|
---|
301 | void hdaR3StreamAsyncIOLock(PHDASTREAM pStream);
|
---|
302 | void hdaR3StreamAsyncIOUnlock(PHDASTREAM pStream);
|
---|
303 | void hdaR3StreamAsyncIOEnable(PHDASTREAM pStream, bool fEnable);
|
---|
304 | # endif /* VBOX_WITH_AUDIO_HDA_ASYNC_IO */
|
---|
305 | /** @} */
|
---|
306 |
|
---|
307 | #endif /* IN_RING3 */
|
---|
308 | #endif /* !VBOX_INCLUDED_SRC_Audio_HDAStream_h */
|
---|
309 |
|
---|