VirtualBox

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

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

Audio/HDA: Added a per-stream FIFOS scratch buffer to avoid intermediate (re-)allocations. ticketoem2ref:36

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