VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DevHdaStream.h@ 89865

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

DevHda: Do LPIB updates more often. Experimental code for doing DMA work on LPIB read (disabled). bugref:9890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.7 KB
Line 
1/* $Id: DevHdaStream.h 89861 2021-06-23 14:23:42Z vboxsync $ */
2/** @file
3 * Intel HD Audio Controller Emulation - Streams.
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_DevHdaStream_h
19#define VBOX_INCLUDED_SRC_Audio_DevHdaStream_h
20#ifndef RT_WITHOUT_PRAGMA_ONCE
21# pragma once
22#endif
23
24#include "DevHdaCommon.h"
25
26
27/**
28 * Structure containing HDA stream debug stuff, configurable at runtime.
29 */
30typedef struct HDASTREAMDEBUGRT
31{
32 /** Whether debugging is enabled or not. */
33 bool fEnabled;
34 uint8_t Padding[7];
35 /** File for dumping stream reads / writes.
36 * For input streams, this dumps data being written to the device FIFO,
37 * whereas for output streams this dumps data being read from the device FIFO. */
38 R3PTRTYPE(PAUDIOHLPFILE) pFileStream;
39 /** File for dumping raw DMA reads / writes.
40 * For input streams, this dumps data being written to the device DMA,
41 * whereas for output streams this dumps data being read from the device DMA. */
42 R3PTRTYPE(PAUDIOHLPFILE) pFileDMARaw;
43 /** File for dumping mapped (that is, extracted) DMA reads / writes. */
44 R3PTRTYPE(PAUDIOHLPFILE) pFileDMAMapped;
45} HDASTREAMDEBUGRT;
46
47/**
48 * Structure containing HDA stream debug information.
49 */
50typedef struct HDASTREAMDEBUG
51{
52 /** Runtime debug info. */
53 HDASTREAMDEBUGRT Runtime;
54#ifdef DEBUG
55 /** Critical section to serialize access if needed. */
56 RTCRITSECT CritSect;
57 uint32_t Padding0[2];
58 /** Number of total read accesses. */
59 uint64_t cReadsTotal;
60 /** Number of total DMA bytes read. */
61 uint64_t cbReadTotal;
62 /** Timestamp (in ns) of last read access. */
63 uint64_t tsLastReadNs;
64 /** Number of total write accesses. */
65 uint64_t cWritesTotal;
66 /** Number of total DMA bytes written. */
67 uint64_t cbWrittenTotal;
68 /** Number of total write accesses since last iteration (Hz). */
69 uint64_t cWritesHz;
70 /** Number of total DMA bytes written since last iteration (Hz). */
71 uint64_t cbWrittenHz;
72 /** Timestamp (in ns) of beginning a new write slot. */
73 uint64_t tsWriteSlotBegin;
74 /** Number of current silence samples in a (consecutive) row. */
75 uint64_t csSilence;
76 /** Number of silent samples in a row to consider an audio block as audio gap (silence). */
77 uint64_t cSilenceThreshold;
78 /** How many bytes to skip in an audio stream before detecting silence.
79 * (useful for intros and silence at the beginning of a song). */
80 uint64_t cbSilenceReadMin;
81#else
82 uint64_t au64Alignment[2];
83#endif
84} HDASTREAMDEBUG;
85typedef HDASTREAMDEBUG *PHDASTREAMDEBUG;
86
87/**
88 * Internal state of a HDA stream.
89 */
90typedef struct HDASTREAMSTATE
91{
92 /** Flag indicating whether this stream currently is
93 * in reset mode and therefore not acccessible by the guest. */
94 volatile bool fInReset;
95 /** Flag indicating if the stream is in running state or not. */
96 volatile bool fRunning;
97 /** The stream's I/O timer Hz rate. */
98 uint16_t uTimerIoHz;
99 /** How many interrupts are pending due to
100 * BDLE interrupt-on-completion (IOC) bits set. */
101 uint8_t cTransferPendingInterrupts;
102 /** Unused, padding. */
103 uint8_t abPadding1[2];
104 /** Input streams only: Set when we switch from feeding the guest silence and
105 * commits to proving actual audio input bytes. */
106 bool fInputPreBuffered;
107 /** Input streams only: The number of bytes we need to prebuffer. */
108 uint32_t cbInputPreBuffer;
109 uint32_t u32Padding2;
110 /** Timestamp (absolute, in timer ticks) of the last DMA data transfer.
111 * @note This is used for wall clock (WALCLK) calculations. */
112 uint64_t volatile tsTransferLast;
113 /** The stream's current configuration (matches SDnFMT). */
114 PDMAUDIOSTREAMCFG Cfg;
115 /** Timestamp (real time, in ns) of last DMA transfer. */
116 uint64_t tsLastTransferNs;
117 /** Timestamp (real time, in ns) of last stream read (to backends).
118 * When running in async I/O mode, this differs from \a tsLastTransferNs,
119 * because reading / processing will be done in a separate stream. */
120 uint64_t tsLastReadNs;
121
122 /** This is set to the timer clock time when the msInitialDelay period is over.
123 * Once reached, this is set to zero to avoid unnecessary time queries. */
124 uint64_t tsAioDelayEnd;
125 /** The start time for the playback (on the timer clock). */
126 uint64_t tsStart;
127
128 /** @name DMA engine
129 * @{ */
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 uint32_t cbTransferSize;
136 /** The size of an average transfer. */
137 uint32_t cbAvgTransfer;
138
139 /** Current circular buffer read offset (for tracing & logging). */
140 uint64_t offRead;
141 /** Current circular buffer write offset (for tracing & logging). */
142 uint64_t offWrite;
143
144 /** The offset into the current BDLE. */
145 uint32_t offCurBdle;
146 /** LVI + 1 */
147 uint16_t cBdles;
148 /** The index of the current BDLE.
149 * This is the entry which period is currently "running" on the DMA timer. */
150 uint8_t idxCurBdle;
151 /** The number of prologue scheduling steps.
152 * This is used when the tail BDLEs doesn't have IOC set. */
153 uint8_t cSchedulePrologue;
154 /** Number of scheduling steps. */
155 uint16_t cSchedule;
156 /** Current scheduling step. */
157 uint16_t idxSchedule;
158 /** Current loop number within the current scheduling step. */
159 uint32_t idxScheduleLoop;
160
161 /** Buffer descriptors and additional timer scheduling state.
162 * (Same as HDABDLEDESC, with more sensible naming.) */
163 struct
164 {
165 /** The buffer address. */
166 uint64_t GCPhys;
167 /** The buffer size (guest bytes). */
168 uint32_t cb;
169 /** The flags (only bit 0 is defined). */
170 uint32_t fFlags;
171 } aBdl[256];
172 /** Scheduling steps. */
173 struct
174 {
175 /** Number of timer ticks per period.
176 * ASSUMES that we don't need a full second and that the timer resolution
177 * isn't much higher than nanoseconds. */
178 uint32_t cPeriodTicks;
179 /** The period length in host bytes. */
180 uint32_t cbPeriod;
181 /** Number of times to repeat the period. */
182 uint32_t cLoops;
183 /** The BDL index of the first entry. */
184 uint8_t idxFirst;
185 /** The number of BDL entries. */
186 uint8_t cEntries;
187 uint8_t abPadding[2];
188 } aSchedule[512+8];
189
190#ifdef VBOX_HDA_WITH_ON_REG_ACCESS_DMA
191 /** Number of valid bytes in abDma.
192 * @note Volatile to prevent the compiler from re-reading it after we've
193 * validated the value in ring-0. */
194 uint32_t volatile cbDma;
195 /** Total number of bytes going via abDma this timer period. */
196 uint32_t cbDmaTotal;
197 /** DMA bounce buffer for ring-0 register reads (LPIB). */
198 uint8_t abDma[2048 - 8];
199#endif
200 /** @} */
201} HDASTREAMSTATE;
202AssertCompileSizeAlignment(HDASTREAMSTATE, 16);
203AssertCompileMemberAlignment(HDASTREAMSTATE, aBdl, 8);
204AssertCompileMemberAlignment(HDASTREAMSTATE, aBdl, 16);
205AssertCompileMemberAlignment(HDASTREAMSTATE, aSchedule, 16);
206
207/**
208 * An HDA stream (SDI / SDO) - shared.
209 *
210 * @note This HDA stream has nothing to do with a regular audio stream handled
211 * by the audio connector or the audio mixer. This HDA stream is a serial
212 * data in/out stream (SDI/SDO) defined in hardware and can contain
213 * multiple audio streams in one single SDI/SDO (interleaving streams).
214 *
215 * Contains only register values which do *not* change until a stream reset
216 * occurs.
217 */
218typedef struct HDASTREAM
219{
220 /** Internal state of this stream. */
221 HDASTREAMSTATE State;
222
223 /** Stream descriptor number (SDn). */
224 uint8_t u8SD;
225 /** Current channel index.
226 * For a stereo stream, this is u8Channel + 1. */
227 uint8_t u8Channel;
228 /** FIFO Watermark (checked + translated in bytes, FIFOW).
229 * This will be update from hdaRegWriteSDFIFOW() and also copied
230 * hdaR3StreamInit() for some reason. */
231 uint8_t u8FIFOW;
232
233 /** @name Register values at stream setup.
234 * These will all be copied in hdaR3StreamInit().
235 * @{ */
236 /** FIFO Size (checked + translated in bytes, FIFOS).
237 * This is supposedly the max number of bytes we'll be DMA'ing in one chunk
238 * and correspondingly the LPIB & wall clock update jumps. However, we're
239 * not at all being honest with the guest about this. */
240 uint8_t u8FIFOS;
241 /** Cyclic Buffer Length (SDnCBL) - Represents the size of the ring buffer. */
242 uint32_t u32CBL;
243 /** Last Valid Index (SDnLVI). */
244 uint16_t u16LVI;
245 /** Format (SDnFMT). */
246 uint16_t u16FMT;
247 uint8_t abPadding[4];
248 /** DMA base address (SDnBDPU - SDnBDPL). */
249 uint64_t u64BDLBase;
250 /** @} */
251
252 /** The timer for pumping data thru the attached LUN drivers. */
253 TMTIMERHANDLE hTimer;
254
255#if 0
256 /** Pad the structure size to a 64 byte alignment. */
257 uint64_t au64Padding1[2];
258#endif
259} HDASTREAM;
260AssertCompileMemberAlignment(HDASTREAM, State.aBdl, 16);
261AssertCompileMemberAlignment(HDASTREAM, State.aSchedule, 16);
262AssertCompileSizeAlignment(HDASTREAM, 64);
263/** Pointer to an HDA stream (SDI / SDO). */
264typedef HDASTREAM *PHDASTREAM;
265
266
267/**
268 * An HDA stream (SDI / SDO) - ring-3 bits.
269 */
270typedef struct HDASTREAMR3
271{
272 /** Stream descriptor number (SDn). */
273 uint8_t u8SD;
274 uint8_t abPadding[7];
275 /** The shared state for the parent HDA device. */
276 R3PTRTYPE(PHDASTATE) pHDAStateShared;
277 /** The ring-3 state for the parent HDA device. */
278 R3PTRTYPE(PHDASTATER3) pHDAStateR3;
279 /** Pointer to HDA sink this stream is attached to. */
280 R3PTRTYPE(PHDAMIXERSINK) pMixSink;
281 /** Internal state of this stream. */
282 struct
283 {
284 /** Circular buffer (FIFO) for holding DMA'ed data. */
285 R3PTRTYPE(PRTCIRCBUF) pCircBuf;
286#ifdef HDA_USE_DMA_ACCESS_HANDLER
287 /** List of DMA handlers. */
288 RTLISTANCHORR3 lstDMAHandlers;
289#endif
290 /** The mixer sink this stream has registered AIO update callback with.
291 * This is NULL till we register it, typically in hdaR3StreamEnable.
292 * (The problem with following the pMixSink assignment is that hdaR3StreamReset
293 * sets it without updating the HDA sink structure, so things get out of
294 * wack in hdaR3MixerControl later in the initial device reset.) */
295 PAUDMIXSINK pAioRegSink;
296
297 /** Size of the DMA buffer (pCircBuf) in bytes. */
298 uint32_t StatDmaBufSize;
299 /** Number of used bytes in the DMA buffer (pCircBuf). */
300 uint32_t StatDmaBufUsed;
301 /** Counter for all under/overflows problems. */
302 STAMCOUNTER StatDmaFlowProblems;
303 /** Counter for unresovled under/overflows problems. */
304 STAMCOUNTER StatDmaFlowErrors;
305 /** Number of bytes involved in unresolved flow errors. */
306 STAMCOUNTER StatDmaFlowErrorBytes;
307 /** DMA skipped because buffer interrupt pending. */
308 STAMCOUNTER StatDmaSkippedPendingBcis;
309
310 STAMPROFILE StatStart;
311 STAMPROFILE StatReset;
312 STAMPROFILE StatStop;
313 } State;
314 /** Debug bits. */
315 HDASTREAMDEBUG Dbg;
316 uint64_t au64Alignment[3];
317} HDASTREAMR3;
318AssertCompileSizeAlignment(HDASTREAMR3, 64);
319/** Pointer to an HDA stream (SDI / SDO). */
320typedef HDASTREAMR3 *PHDASTREAMR3;
321
322/** @name Stream functions (all contexts).
323 * @{
324 */
325VBOXSTRICTRC hdaStreamDoOnAccessDmaOutput(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared,
326 uint32_t cbToTransfer);
327/** @} */
328
329#ifdef IN_RING3
330
331/** @name Stream functions (ring-3).
332 * @{
333 */
334int hdaR3StreamConstruct(PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, PHDASTATE pThis,
335 PHDASTATER3 pThisCC, uint8_t uSD);
336void hdaR3StreamDestroy(PHDASTREAMR3 pStreamR3);
337int hdaR3StreamSetUp(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared,
338 PHDASTREAMR3 pStreamR3, uint8_t uSD);
339void hdaR3StreamReset(PHDASTATE pThis, PHDASTATER3 pThisCC,
340 PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, uint8_t uSD);
341int hdaR3StreamEnable(PHDASTATE pThis, PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3, bool fEnable);
342void hdaR3StreamMarkStarted(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTREAM pStreamShared, uint64_t tsNow);
343void hdaR3StreamMarkStopped(PHDASTREAM pStreamShared);
344
345uint64_t hdaR3StreamTimerMain(PPDMDEVINS pDevIns, PHDASTATE pThis, PHDASTATER3 pThisCC,
346 PHDASTREAM pStreamShared, PHDASTREAMR3 pStreamR3);
347DECLCALLBACK(void) hdaR3StreamUpdateAsyncIoJob(PPDMDEVINS pDevIns, PAUDMIXSINK pSink, void *pvUser);
348# ifdef HDA_USE_DMA_ACCESS_HANDLER
349bool hdaR3StreamRegisterDMAHandlers(PHDASTREAM pStream);
350void hdaR3StreamUnregisterDMAHandlers(PHDASTREAM pStream);
351# endif
352/** @} */
353
354#endif /* IN_RING3 */
355#endif /* !VBOX_INCLUDED_SRC_Audio_DevHdaStream_h */
356
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