VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/HDAStream.h@ 88112

Last change on this file since 88112 was 88112, checked in by vboxsync, 4 years ago

Audio,DevHDA: Made PDMAUDIOPCMPROPS_F2B and PDMAUDIOPCMPROPS_B2F work for 5.1 and other frame sizes that aren't a power of two. Rewrote hdaR3StreamDoDmaOutput. Mapping is now done using converter functions rather than the complexity in hdaR3StreamTransfer. Probably fixes some recent goofs in the DMA period heuristics for non-stereo guest streams, I've renamed the members to make it clear what's what. bugref:9890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.7 KB
Line 
1/* $Id: HDAStream.h 88112 2021-03-12 20:41:05Z 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 */
33typedef 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. */
50typedef HDASTREAMSTATEAIO *PHDASTREAMSTATEAIO;
51#endif
52
53/**
54 * Structure containing HDA stream debug stuff, configurable at runtime.
55 */
56typedef 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 */
76typedef 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;
109typedef HDASTREAMDEBUG *PHDASTREAMDEBUG;
110
111/**
112 * Internal state of a HDA stream.
113 */
114typedef 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 (absolute, in timer ticks) of the last DMA data transfer. */
129 uint64_t tsTransferLast;
130 /** Timestamp (absolute, in timer ticks) 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 * @note This is in host side frames, in case we're doing any mapping. */
136 uint32_t cbTransferSize;
137 /** Unused, same as cbTransferSize. */
138 uint32_t cbTransferChunk;
139 /** How many interrupts are pending due to
140 * BDLE interrupt-on-completion (IOC) bits set. */
141 uint8_t cTransferPendingInterrupts;
142 uint8_t abPadding2[7];
143 /** The stream's I/O timer Hz rate. */
144 uint16_t uTimerIoHz;
145 /** Number of audio data frames for the position adjustment.
146 * 0 if no position adjustment is needed. */
147 uint16_t cfPosAdjustDefault;
148 /** How many audio data frames are left to be processed
149 * for the position adjustment handling.
150 *
151 * 0 if position adjustment handling is done or inactive. */
152 uint16_t cfPosAdjustLeft;
153 uint16_t u16Padding3;
154 /** (Virtual) clock ticks per byte. */
155 uint64_t cTicksPerByte;
156 /** (Virtual) clock ticks per transfer. */
157 uint64_t cTransferTicks;
158 /** The stream's period. Need for timing. */
159 HDASTREAMPERIOD Period;
160 /** The stream's current host side configuration.
161 * This should match the SDnFMT in all respects but maybe the channel count as
162 * we may need to expand mono or into/from into stereo. The unmodified SDnFMT
163 * properties can be found in HDASTREAMR3::Mapping::PCMProps. */
164 PDMAUDIOSTREAMCFG Cfg;
165 /** Timestamp (real time, in ns) of last DMA transfer. */
166 uint64_t tsLastTransferNs;
167 /** Timestamp (real time, in ns) of last stream read (to backends).
168 * When running in async I/O mode, this differs from \a tsLastTransferNs,
169 * because reading / processing will be done in a separate stream. */
170 uint64_t tsLastReadNs;
171
172 /** This is set to the timer clock time when the msInitialDelay period is over.
173 * Once reached, this is set to zero to avoid unnecessary time queries. */
174 uint64_t tsAioDelayEnd;
175 /** The start time for the playback (on the timer clock). */
176 uint64_t tsStart;
177} HDASTREAMSTATE;
178AssertCompileSizeAlignment(HDASTREAMSTATE, 8);
179
180/**
181 * An HDA stream (SDI / SDO) - shared.
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
185 * data in/out stream (SDI/SDO) defined in hardware and can contain
186 * multiple audio streams 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 stream reset
192 * occurs.
193 */
194typedef 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 abPadding0[6];
202#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
203 /** The stream's shared r0/r3 critical section to serialize access between the async I/O
204 * thread and (basically) the guest. */
205 PDMCRITSECT CritSect;
206#endif
207 /** DMA base address (SDnBDPU - SDnBDPL).
208 * Will be updated in hdaR3StreamInit(). */
209 uint64_t u64BDLBase;
210 /** Cyclic Buffer Length (SDnCBL).
211 * Represents the size of the ring buffer.
212 * Will be updated in hdaR3StreamInit(). */
213 uint32_t u32CBL;
214 /** Format (SDnFMT).
215 * Will be updated in hdaR3StreamInit(). */
216 uint16_t u16FMT;
217 /** FIFO Size (checked + translated in bytes, FIFOS).
218 * Maximum number of bytes that may have been DMA'd into
219 * memory but not yet transmitted on the link.
220 *
221 * Will be updated in hdaR3StreamInit(). */
222 uint8_t u8FIFOS;
223 /** FIFO Watermark (checked + translated in bytes, FIFOW). */
224 uint8_t u8FIFOW;
225 uint8_t abPadding1[2];
226 /** FIFO scratch buffer, to avoid intermediate (re-)allocations. */
227 uint8_t abFIFO[HDA_FIFO_MAX + 1];
228 /** Last Valid Index (SDnLVI).
229 * Will be updated in hdaR3StreamInit(). */
230 uint16_t u16LVI;
231 /** The timer for pumping data thru the attached LUN drivers. */
232 TMTIMERHANDLE hTimer;
233 /** Internal state of this stream. */
234 HDASTREAMSTATE State;
235} HDASTREAM;
236/** Pointer to an HDA stream (SDI / SDO). */
237typedef HDASTREAM *PHDASTREAM;
238
239
240/**
241 * An HDA stream (SDI / SDO) - ring-3 bits.
242 */
243typedef struct HDASTREAMR3
244{
245 /** Stream descriptor number (SDn). */
246 uint8_t u8SD;
247 uint8_t abPadding[7];
248 /** The shared state for the parent HDA device. */
249 R3PTRTYPE(PHDASTATE) pHDAStateShared;
250 /** The ring-3 state for the parent HDA device. */
251 R3PTRTYPE(PHDASTATER3) pHDAStateR3;
252 /** Pointer to HDA sink this stream is attached to. */
253 R3PTRTYPE(PHDAMIXERSINK) pMixSink;
254 /** Internal state of this stream. */
255 struct
256 {
257 /** This stream's data mapping. */
258 HDASTREAMMAP Mapping;
259 /** Circular buffer (FIFO) for holding DMA'ed data. */
260 R3PTRTYPE(PRTCIRCBUF) pCircBuf;
261 /** Current circular buffer read offset (for tracing & logging). */
262 uint64_t offRead;
263 /** Current circular buffer write offset (for tracing & logging). */
264 uint64_t offWrite;
265#ifdef HDA_USE_DMA_ACCESS_HANDLER
266 /** List of DMA handlers. */
267 RTLISTANCHORR3 lstDMAHandlers;
268#endif
269#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
270 /** Asynchronous I/O state members. */
271 HDASTREAMSTATEAIO AIO;
272#endif
273 /** Counter for all under/overflows problems. */
274 STAMCOUNTER StatDmaFlowProblems;
275 /** Counter for unresovled under/overflows problems. */
276 STAMCOUNTER StatDmaFlowErrors;
277 } State;
278 /** Debug bits. */
279 HDASTREAMDEBUG Dbg;
280} HDASTREAMR3;
281/** Pointer to an HDA stream (SDI / SDO). */
282typedef HDASTREAMR3 *PHDASTREAMR3;
283
284/** @name Stream functions (shared).
285 * @{
286 */
287void hdaStreamLock(PHDASTREAM pStreamShared);
288void hdaStreamUnlock(PHDASTREAM pStreamShared);
289/** @} */
290
291#ifdef IN_RING3
292
293/** @name Stream functions (ring-3).
294 * @{
295 */
296int hdaR3StreamConstruct(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, PHDASTATE pThis,
297 PHDASTATER3 pThisCC, uint8_t uSD);
298void hdaR3StreamDestroy(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3);
299int hdaR3StreamSetUp(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared,
300 PHDASTREAMR3 pStreamR3, uint8_t uSD);
301void hdaR3StreamReset(PHDASTATE pThis, PHDASTATER3 pThisCC,
302 PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, uint8_t uSD);
303int hdaR3StreamEnable(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, bool fEnable);
304void hdaR3StreamMarkStarted(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared, uint64_t tsNow);
305void hdaR3StreamMarkStopped(PHDASTREAM pStreamShared);
306
307void hdaR3StreamSetPositionAdd(PHDASTREAM pStreamShared, PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t uToAdd);
308uint64_t hdaR3StreamTimerMain(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTATER3 pThisCC,
309 PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3);
310void hdaR3StreamUpdate(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTATER3 pThisCC,
311 PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, bool fInTimer);
312PHDASTREAM hdaR3StreamR3ToShared(PHDASTREAMR3 pStreamCC);
313# ifdef HDA_USE_DMA_ACCESS_HANDLER
314bool hdaR3StreamRegisterDMAHandlers(PHDASTREAM pStream);
315void hdaR3StreamUnregisterDMAHandlers(PHDASTREAM pStream);
316# endif
317/** @} */
318
319/** @name Async I/O stream functions (ring-3).
320 * @{
321 */
322# ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
323int hdaR3StreamAsyncIOCreate(PHDASTREAMR3 pStreamR3);
324void hdaR3StreamAsyncIOLock(PHDASTREAMR3 pStreamR3);
325void hdaR3StreamAsyncIOUnlock(PHDASTREAMR3 pStreamR3);
326void hdaR3StreamAsyncIOEnable(PHDASTREAMR3 pStreamR3, bool fEnable);
327# endif /* VBOX_WITH_AUDIO_HDA_ASYNC_IO */
328/** @} */
329
330#endif /* IN_RING3 */
331#endif /* !VBOX_INCLUDED_SRC_Audio_HDAStream_h */
332
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette