1 | /* $Id: HDAStream.h 82450 2019-12-06 12:39:15Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * HDAStream.h - Streams 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 | #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 | uint32_t Padding4;
|
---|
169 | /** Timestamp (in ns) of last stream update. */
|
---|
170 | uint64_t tsLastUpdateNs;
|
---|
171 | } HDASTREAMSTATE;
|
---|
172 | AssertCompileSizeAlignment(HDASTREAMSTATE, 8);
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * An HDA stream (SDI / SDO) - shared.
|
---|
176 | *
|
---|
177 | * @note This HDA stream has nothing to do with a regular audio stream handled
|
---|
178 | * by the audio connector or the audio mixer. This HDA stream is a serial
|
---|
179 | * data in/out stream (SDI/SDO) defined in hardware and can contain
|
---|
180 | * multiple audio streams in one single SDI/SDO (interleaving streams).
|
---|
181 | *
|
---|
182 | * How a specific SDI/SDO is mapped to our internal audio streams relies on the
|
---|
183 | * stream channel mappings.
|
---|
184 | *
|
---|
185 | * Contains only register values which do *not* change until a stream reset
|
---|
186 | * occurs.
|
---|
187 | */
|
---|
188 | typedef struct HDASTREAM
|
---|
189 | {
|
---|
190 | /** Stream descriptor number (SDn). */
|
---|
191 | uint8_t u8SD;
|
---|
192 | /** Current channel index.
|
---|
193 | * For a stereo stream, this is u8Channel + 1. */
|
---|
194 | uint8_t u8Channel;
|
---|
195 | uint8_t abPadding0[6];
|
---|
196 | /** DMA base address (SDnBDPU - SDnBDPL).
|
---|
197 | * Will be updated in hdaR3StreamInit(). */
|
---|
198 | uint64_t u64BDLBase;
|
---|
199 | /** Cyclic Buffer Length (SDnCBL).
|
---|
200 | * Represents the size of the ring buffer.
|
---|
201 | * Will be updated in hdaR3StreamInit(). */
|
---|
202 | uint32_t u32CBL;
|
---|
203 | /** Format (SDnFMT).
|
---|
204 | * Will be updated in hdaR3StreamInit(). */
|
---|
205 | uint16_t u16FMT;
|
---|
206 | /** FIFO Size (FIFOS).
|
---|
207 | * Maximum number of bytes that may have been DMA'd into
|
---|
208 | * memory but not yet transmitted on the link.
|
---|
209 | *
|
---|
210 | * Will be updated in hdaR3StreamInit(). */
|
---|
211 | uint16_t u16FIFOS;
|
---|
212 | /** FIFO Watermark. */
|
---|
213 | uint16_t u16FIFOW;
|
---|
214 | /** Last Valid Index (SDnLVI).
|
---|
215 | * Will be updated in hdaR3StreamInit(). */
|
---|
216 | uint16_t u16LVI;
|
---|
217 | uint16_t au16Padding1[2];
|
---|
218 | /** The timer for pumping data thru the attached LUN drivers. */
|
---|
219 | TMTIMERHANDLE hTimer;
|
---|
220 | /** Internal state of this stream. */
|
---|
221 | HDASTREAMSTATE State;
|
---|
222 | } HDASTREAM;
|
---|
223 | /** Pointer to an HDA stream (SDI / SDO). */
|
---|
224 | typedef HDASTREAM *PHDASTREAM;
|
---|
225 |
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * An HDA stream (SDI / SDO) - ring-3 bits.
|
---|
229 | */
|
---|
230 | typedef struct HDASTREAMR3
|
---|
231 | {
|
---|
232 | /** Stream descriptor number (SDn). */
|
---|
233 | uint8_t u8SD;
|
---|
234 | uint8_t abPadding[7];
|
---|
235 | /** The shared state for the parent HDA device. */
|
---|
236 | R3PTRTYPE(PHDASTATE) pHDAStateShared;
|
---|
237 | /** The ring-3 state for the parent HDA device. */
|
---|
238 | R3PTRTYPE(PHDASTATER3) pHDAStateR3;
|
---|
239 | /** Pointer to HDA sink this stream is attached to. */
|
---|
240 | R3PTRTYPE(PHDAMIXERSINK) pMixSink;
|
---|
241 | #ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
242 | /** The stream's critical section to serialize access between the async I/O
|
---|
243 | * thread and (basically) the guest. */
|
---|
244 | RTCRITSECT CritSect;
|
---|
245 | #endif
|
---|
246 | /** Internal state of this stream. */
|
---|
247 | struct
|
---|
248 | {
|
---|
249 | /** This stream's data mapping. */
|
---|
250 | HDASTREAMMAP Mapping;
|
---|
251 | /** Circular buffer (FIFO) for holding DMA'ed data. */
|
---|
252 | R3PTRTYPE(PRTCIRCBUF) pCircBuf;
|
---|
253 | #ifdef HDA_USE_DMA_ACCESS_HANDLER
|
---|
254 | /** List of DMA handlers. */
|
---|
255 | RTLISTANCHORR3 lstDMAHandlers;
|
---|
256 | #endif
|
---|
257 | #ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
258 | /** Asynchronous I/O state members. */
|
---|
259 | HDASTREAMSTATEAIO AIO;
|
---|
260 | #endif
|
---|
261 | } State;
|
---|
262 | /** Debug bits. */
|
---|
263 | HDASTREAMDEBUG Dbg;
|
---|
264 | } HDASTREAMR3;
|
---|
265 | /** Pointer to an HDA stream (SDI / SDO). */
|
---|
266 | typedef HDASTREAMR3 *PHDASTREAMR3;
|
---|
267 |
|
---|
268 | #ifdef IN_RING3
|
---|
269 |
|
---|
270 | /** @name Stream functions.
|
---|
271 | * @{
|
---|
272 | */
|
---|
273 | int hdaR3StreamConstruct(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, PHDASTATE pThis,
|
---|
274 | PHDASTATER3 pThisCC, uint8_t uSD);
|
---|
275 | void hdaR3StreamDestroy(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3);
|
---|
276 | int hdaR3StreamSetUp(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared,
|
---|
277 | PHDASTREAMR3 pStreamR3, uint8_t uSD);
|
---|
278 | void hdaR3StreamReset(PHDASTATE pThis, PHDASTATER3 pThisCC,
|
---|
279 | PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, uint8_t uSD);
|
---|
280 | int hdaR3StreamEnable(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, bool fEnable);
|
---|
281 | /* uint32_t hdaR3StreamGetPosition(PHDASTATE pThis, PHDASTREAMR3 pStreamShared); - only used in HDAStream.cpp */
|
---|
282 | /*void hdaR3StreamSetPosition(PHDASTREAM pStream, PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t u32LPIB); - only used in HDAStream.cpp */
|
---|
283 | /*uint32_t hdaR3StreamGetFree(PHDASTREAM pStream); - only used in HDAStream.cpp */
|
---|
284 | /*uint32_t hdaR3StreamGetUsed(PHDASTREAM pStream); - only used in HDAStream.cpp */
|
---|
285 | bool hdaR3StreamTransferIsScheduled(PHDASTREAM pStreamShared, uint64_t tsNow);
|
---|
286 | uint64_t hdaR3StreamTransferGetNext(PHDASTREAM pStreamShared);
|
---|
287 | void hdaR3StreamLock(PHDASTREAMR3 pStreamR3);
|
---|
288 | void hdaR3StreamUnlock(PHDASTREAMR3 pStreamR3);
|
---|
289 | /* int hdaR3StreamRead(PHDASTREAM pStream, uint32_t cbToRead, uint32_t *pcbRead); - only used in HDAStream.cpp */
|
---|
290 | /*int hdaR3StreamWrite(PHDASTREAM pStream, const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten); - only used in HDAStream.cpp */
|
---|
291 | void hdaR3StreamUpdate(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTATER3 pThisCC,
|
---|
292 | PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, bool fInTimer);
|
---|
293 | PHDASTREAM hdaR3StreamR3ToShared(PHDASTREAMR3 pStreamCC);
|
---|
294 | # ifdef HDA_USE_DMA_ACCESS_HANDLER
|
---|
295 | bool hdaR3StreamRegisterDMAHandlers(PHDASTREAM pStream);
|
---|
296 | void hdaR3StreamUnregisterDMAHandlers(PHDASTREAM pStream);
|
---|
297 | # endif
|
---|
298 | /** @} */
|
---|
299 |
|
---|
300 | /** @name Async I/O stream functions.
|
---|
301 | * @{
|
---|
302 | */
|
---|
303 | # ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
|
---|
304 | int hdaR3StreamAsyncIOCreate(PHDASTREAMR3 pStreamR3);
|
---|
305 | void hdaR3StreamAsyncIOLock(PHDASTREAMR3 pStreamR3);
|
---|
306 | void hdaR3StreamAsyncIOUnlock(PHDASTREAMR3 pStreamR3);
|
---|
307 | void hdaR3StreamAsyncIOEnable(PHDASTREAMR3 pStreamR3, bool fEnable);
|
---|
308 | # endif /* VBOX_WITH_AUDIO_HDA_ASYNC_IO */
|
---|
309 | /** @} */
|
---|
310 |
|
---|
311 | #endif /* IN_RING3 */
|
---|
312 | #endif /* !VBOX_INCLUDED_SRC_Audio_HDAStream_h */
|
---|
313 |
|
---|