VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DevHDA.cpp@ 68253

Last change on this file since 68253 was 68137, checked in by vboxsync, 7 years ago

DevHDA: make the SSM revision check more robust if the SVN revision is not available

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 173.8 KB
Line 
1/* $Id: DevHDA.cpp 68137 2017-07-27 10:54:43Z vboxsync $ */
2/** @file
3 * DevHDA.cpp - VBox Intel HD Audio Controller.
4 *
5 * Implemented against the specifications found in "High Definition Audio
6 * Specification", Revision 1.0a June 17, 2010, and "Intel I/O Controller
7 * HUB 6 (ICH6) Family, Datasheet", document number 301473-002.
8 */
9
10/*
11 * Copyright (C) 2006-2017 Oracle Corporation
12 *
13 * This file is part of VirtualBox Open Source Edition (OSE), as
14 * available from http://www.virtualbox.org. This file is free software;
15 * you can redistribute it and/or modify it under the terms of the GNU
16 * General Public License (GPL) as published by the Free Software
17 * Foundation, in version 2 as it comes in the "COPYING" file of the
18 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
19 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
20 */
21
22
23/*********************************************************************************************************************************
24* Header Files *
25*********************************************************************************************************************************/
26#define LOG_GROUP LOG_GROUP_DEV_HDA
27#include <VBox/log.h>
28
29#include <VBox/vmm/pdmdev.h>
30#include <VBox/vmm/pdmaudioifs.h>
31#include <VBox/version.h>
32
33#include <iprt/assert.h>
34#include <iprt/asm.h>
35#include <iprt/asm-math.h>
36#include <iprt/file.h>
37#include <iprt/list.h>
38#ifdef IN_RING3
39# include <iprt/mem.h>
40# include <iprt/semaphore.h>
41# include <iprt/string.h>
42# include <iprt/uuid.h>
43#endif
44
45#include "VBoxDD.h"
46
47#include "AudioMixBuffer.h"
48#include "AudioMixer.h"
49
50#include "DevHDA.h"
51#include "DevHDACommon.h"
52
53#include "HDACodec.h"
54#include "HDAStream.h"
55# if defined(VBOX_WITH_HDA_AUDIO_INTERLEAVING_STREAMS_SUPPORT) || defined(VBOX_WITH_AUDIO_HDA_51_SURROUND)
56# include "HDAStreamChannel.h"
57# endif
58#include "HDAStreamMap.h"
59#include "HDAStreamPeriod.h"
60
61#include "DrvAudio.h"
62
63
64/*********************************************************************************************************************************
65* Defined Constants And Macros *
66*********************************************************************************************************************************/
67//#define HDA_AS_PCI_EXPRESS
68#define VBOX_WITH_INTEL_HDA
69
70/* Installs a DMA access handler (via PGM callback) to monitor
71 * HDA's DMA operations, that is, writing / reading audio stream data.
72 *
73 * !!! Note: Certain guests are *that* timing sensitive that when enabling !!!
74 * !!! such a handler will mess up audio completely (e.g. Windows 7). !!! */
75//#define HDA_USE_DMA_ACCESS_HANDLER
76#ifdef HDA_USE_DMA_ACCESS_HANDLER
77# include <VBox/vmm/pgm.h>
78#endif
79
80/* Uses the DMA access handler to read the written DMA audio (output) data.
81 * Only valid if HDA_USE_DMA_ACCESS_HANDLER is set.
82 *
83 * Also see the note / warning for HDA_USE_DMA_ACCESS_HANDLER. */
84//# define HDA_USE_DMA_ACCESS_HANDLER_WRITING
85
86/* Useful to debug the device' timing. */
87//#define HDA_DEBUG_TIMING
88
89/* To debug silence coming from the guest in form of audio gaps.
90 * Very crude implementation for now. */
91//#define HDA_DEBUG_SILENCE
92
93#if defined(VBOX_WITH_HP_HDA)
94/* HP Pavilion dv4t-1300 */
95# define HDA_PCI_VENDOR_ID 0x103c
96# define HDA_PCI_DEVICE_ID 0x30f7
97#elif defined(VBOX_WITH_INTEL_HDA)
98/* Intel HDA controller */
99# define HDA_PCI_VENDOR_ID 0x8086
100# define HDA_PCI_DEVICE_ID 0x2668
101#elif defined(VBOX_WITH_NVIDIA_HDA)
102/* nVidia HDA controller */
103# define HDA_PCI_VENDOR_ID 0x10de
104# define HDA_PCI_DEVICE_ID 0x0ac0
105#else
106# error "Please specify your HDA device vendor/device IDs"
107#endif
108
109/* Make sure that interleaving streams support is enabled if the 5.1 surround code is being used. */
110#if defined (VBOX_WITH_AUDIO_HDA_51_SURROUND) && !defined(VBOX_WITH_HDA_AUDIO_INTERLEAVING_STREAMS_SUPPORT)
111# define VBOX_WITH_HDA_AUDIO_INTERLEAVING_STREAMS_SUPPORT
112#endif
113
114
115/*********************************************************************************************************************************
116* Structures and Typedefs *
117*********************************************************************************************************************************/
118
119/**
120 * Structure defining a (host backend) driver stream.
121 * Each driver has its own instances of audio mixer streams, which then
122 * can go into the same (or even different) audio mixer sinks.
123 */
124typedef struct HDADRIVERSTREAM
125{
126 union
127 {
128 /** Desired playback destination (for an output stream). */
129 PDMAUDIOPLAYBACKDEST Dest;
130 /** Desired recording source (for an input stream). */
131 PDMAUDIORECSOURCE Source;
132 } DestSource;
133 uint8_t Padding1[4];
134 /** Associated mixer handle. */
135 R3PTRTYPE(PAUDMIXSTREAM) pMixStrm;
136} HDADRIVERSTREAM, *PHDADRIVERSTREAM;
137
138#ifdef HDA_USE_DMA_ACCESS_HANDLER
139/**
140 * Struct for keeping an HDA DMA access handler context.
141 */
142typedef struct HDADMAACCESSHANDLER
143{
144 /** Node for storing this handler in our list in HDASTREAMSTATE. */
145 RTLISTNODER3 Node;
146 /** Pointer to stream to which this access handler is assigned to. */
147 R3PTRTYPE(PHDASTREAM) pStream;
148 /** Access handler type handle. */
149 PGMPHYSHANDLERTYPE hAccessHandlerType;
150 /** First address this handler uses. */
151 RTGCPHYS GCPhysFirst;
152 /** Last address this handler uses. */
153 RTGCPHYS GCPhysLast;
154 /** Actual BDLE address to handle. */
155 RTGCPHYS BDLEAddr;
156 /** Actual BDLE buffer size to handle. */
157 RTGCPHYS BDLESize;
158 /** Whether the access handler has been registered or not. */
159 bool fRegistered;
160 uint8_t Padding[3];
161} HDADMAACCESSHANDLER, *PHDADMAACCESSHANDLER;
162#endif
163
164/**
165 * Struct for maintaining a host backend driver.
166 * This driver must be associated to one, and only one,
167 * HDA codec. The HDA controller does the actual multiplexing
168 * of HDA codec data to various host backend drivers then.
169 *
170 * This HDA device uses a timer in order to synchronize all
171 * read/write accesses across all attached LUNs / backends.
172 */
173typedef struct HDADRIVER
174{
175 /** Node for storing this driver in our device driver list of HDASTATE. */
176 RTLISTNODER3 Node;
177 /** Pointer to HDA controller (state). */
178 R3PTRTYPE(PHDASTATE) pHDAState;
179 /** Driver flags. */
180 PDMAUDIODRVFLAGS fFlags;
181 uint8_t u32Padding0[2];
182 /** LUN to which this driver has been assigned. */
183 uint8_t uLUN;
184 /** Whether this driver is in an attached state or not. */
185 bool fAttached;
186 /** Pointer to attached driver base interface. */
187 R3PTRTYPE(PPDMIBASE) pDrvBase;
188 /** Audio connector interface to the underlying host backend. */
189 R3PTRTYPE(PPDMIAUDIOCONNECTOR) pConnector;
190 /** Mixer stream for line input. */
191 HDADRIVERSTREAM LineIn;
192#ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
193 /** Mixer stream for mic input. */
194 HDADRIVERSTREAM MicIn;
195#endif
196 /** Mixer stream for front output. */
197 HDADRIVERSTREAM Front;
198#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
199 /** Mixer stream for center/LFE output. */
200 HDADRIVERSTREAM CenterLFE;
201 /** Mixer stream for rear output. */
202 HDADRIVERSTREAM Rear;
203#endif
204} HDADRIVER;
205
206
207/*********************************************************************************************************************************
208* Internal Functions *
209*********************************************************************************************************************************/
210#ifndef VBOX_DEVICE_STRUCT_TESTCASE
211#ifdef IN_RING3
212static void hdaGCTLReset(PHDASTATE pThis);
213#endif
214
215/** @name Register read/write stubs.
216 * @{
217 */
218static int hdaRegReadUnimpl(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
219static int hdaRegWriteUnimpl(PHDASTATE pThis, uint32_t iReg, uint32_t pu32Value);
220/** @} */
221
222/** @name Global register set read/write functions.
223 * @{
224 */
225static int hdaRegWriteGCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
226static int hdaRegReadLPIB(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
227static int hdaRegReadWALCLK(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
228static int hdaRegWriteCORBWP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
229static int hdaRegWriteCORBRP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
230static int hdaRegWriteCORBCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
231static int hdaRegWriteCORBSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
232static int hdaRegWriteRIRBWP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
233static int hdaRegWriteRIRBSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
234static int hdaRegWriteSTATESTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
235static int hdaRegWriteIRS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
236static int hdaRegReadIRS(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
237static int hdaRegWriteBase(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
238/** @} */
239
240/** @name {IOB}SDn write functions.
241 * @{
242 */
243static int hdaRegWriteSDCBL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
244static int hdaRegWriteSDCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
245static int hdaRegWriteSDSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
246static int hdaRegWriteSDLVI(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
247static int hdaRegWriteSDFIFOW(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
248static int hdaRegWriteSDFIFOS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
249static int hdaRegWriteSDFMT(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
250static int hdaRegWriteSDBDPL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
251static int hdaRegWriteSDBDPU(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
252/** @} */
253
254/** @name Generic register read/write functions.
255 * @{
256 */
257static int hdaRegReadU32(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
258static int hdaRegWriteU32(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
259static int hdaRegReadU24(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
260#ifdef IN_RING3
261static int hdaRegWriteU24(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
262#endif
263static int hdaRegReadU16(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
264static int hdaRegWriteU16(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
265static int hdaRegReadU8(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
266static int hdaRegWriteU8(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
267/** @} */
268
269/** @name HDA device functions.
270 * @{
271 */
272#ifdef IN_RING3
273# ifdef HDA_USE_DMA_ACCESS_HANDLER
274static DECLCALLBACK(VBOXSTRICTRC) hdaDMAAccessHandler(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, PGMACCESSORIGIN enmOrigin, void *pvUser);
275# endif
276static void hdaDoTransfers(PHDASTATE pThis);
277#endif /* IN_RING3 */
278/** @} */
279
280/** @name Timer functions.
281 * @{
282 */
283#if !defined(VBOX_WITH_AUDIO_HDA_CALLBACKS) && defined(IN_RING3)
284static void hdaTimerMaybeStart(PHDASTATE pThis);
285static void hdaTimerMaybeStop(PHDASTATE pThis);
286static void hdaTimerMain(PHDASTATE pThis);
287#endif
288/** @} */
289
290
291/*********************************************************************************************************************************
292* Global Variables *
293*********************************************************************************************************************************/
294
295/** No register description (RD) flags defined. */
296#define HDA_RD_FLAG_NONE 0
297/** Writes to SD are allowed while RUN bit is set. */
298#define HDA_RD_FLAG_SD_WRITE_RUN RT_BIT(0)
299
300/** Emits a single audio stream register set (e.g. OSD0) at a specified offset. */
301#define HDA_REG_MAP_STRM(offset, name) \
302 /* offset size read mask write mask flags read callback write callback index + abbrev description */ \
303 /* ------- ------- ---------- ---------- ------------------------- -------------- ----------------- ----------------------------- ----------- */ \
304 /* Offset 0x80 (SD0) */ \
305 { offset, 0x00003, 0x00FF001F, 0x00F0001F, HDA_RD_FLAG_SD_WRITE_RUN, hdaRegReadU24 , hdaRegWriteSDCTL , HDA_REG_IDX_STRM(name, CTL) , #name " Stream Descriptor Control" }, \
306 /* Offset 0x83 (SD0) */ \
307 { offset + 0x3, 0x00001, 0x0000003C, 0x0000001C, HDA_RD_FLAG_SD_WRITE_RUN, hdaRegReadU8 , hdaRegWriteSDSTS , HDA_REG_IDX_STRM(name, STS) , #name " Status" }, \
308 /* Offset 0x84 (SD0) */ \
309 { offset + 0x4, 0x00004, 0xFFFFFFFF, 0x00000000, HDA_RD_FLAG_NONE, hdaRegReadLPIB, hdaRegWriteU32 , HDA_REG_IDX_STRM(name, LPIB) , #name " Link Position In Buffer" }, \
310 /* Offset 0x88 (SD0) */ \
311 { offset + 0x8, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, HDA_RD_FLAG_NONE, hdaRegReadU32 , hdaRegWriteSDCBL , HDA_REG_IDX_STRM(name, CBL) , #name " Cyclic Buffer Length" }, \
312 /* Offset 0x8C (SD0) */ \
313 { offset + 0xC, 0x00002, 0x0000FFFF, 0x0000FFFF, HDA_RD_FLAG_NONE, hdaRegReadU16 , hdaRegWriteSDLVI , HDA_REG_IDX_STRM(name, LVI) , #name " Last Valid Index" }, \
314 /* Reserved: FIFO Watermark. ** @todo Document this! */ \
315 { offset + 0xE, 0x00002, 0x00000007, 0x00000007, HDA_RD_FLAG_NONE, hdaRegReadU16 , hdaRegWriteSDFIFOW, HDA_REG_IDX_STRM(name, FIFOW), #name " FIFO Watermark" }, \
316 /* Offset 0x90 (SD0) */ \
317 { offset + 0x10, 0x00002, 0x000000FF, 0x000000FF, HDA_RD_FLAG_NONE, hdaRegReadU16 , hdaRegWriteSDFIFOS, HDA_REG_IDX_STRM(name, FIFOS), #name " FIFO Size" }, \
318 /* Offset 0x92 (SD0) */ \
319 { offset + 0x12, 0x00002, 0x00007F7F, 0x00007F7F, HDA_RD_FLAG_NONE, hdaRegReadU16 , hdaRegWriteSDFMT , HDA_REG_IDX_STRM(name, FMT) , #name " Stream Format" }, \
320 /* Reserved: 0x94 - 0x98. */ \
321 /* Offset 0x98 (SD0) */ \
322 { offset + 0x18, 0x00004, 0xFFFFFF80, 0xFFFFFF80, HDA_RD_FLAG_NONE, hdaRegReadU32 , hdaRegWriteSDBDPL , HDA_REG_IDX_STRM(name, BDPL) , #name " Buffer Descriptor List Pointer-Lower Base Address" }, \
323 /* Offset 0x9C (SD0) */ \
324 { offset + 0x1C, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, HDA_RD_FLAG_NONE, hdaRegReadU32 , hdaRegWriteSDBDPU , HDA_REG_IDX_STRM(name, BDPU) , #name " Buffer Descriptor List Pointer-Upper Base Address" }
325
326/** Defines a single audio stream register set (e.g. OSD0). */
327#define HDA_REG_MAP_DEF_STREAM(index, name) \
328 HDA_REG_MAP_STRM(HDA_REG_DESC_SD0_BASE + (index * 32 /* 0x20 */), name)
329
330/* See 302349 p 6.2. */
331const HDAREGDESC g_aHdaRegMap[HDA_NUM_REGS] =
332{
333 /* offset size read mask write mask flags read callback write callback index + abbrev */
334 /*------- ------- ---------- ---------- ----------------- ---------------- ------------------- ------------------------ */
335 { 0x00000, 0x00002, 0x0000FFFB, 0x00000000, HDA_RD_FLAG_NONE, hdaRegReadU16 , hdaRegWriteUnimpl , HDA_REG_IDX(GCAP) }, /* Global Capabilities */
336 { 0x00002, 0x00001, 0x000000FF, 0x00000000, HDA_RD_FLAG_NONE, hdaRegReadU8 , hdaRegWriteUnimpl , HDA_REG_IDX(VMIN) }, /* Minor Version */
337 { 0x00003, 0x00001, 0x000000FF, 0x00000000, HDA_RD_FLAG_NONE, hdaRegReadU8 , hdaRegWriteUnimpl , HDA_REG_IDX(VMAJ) }, /* Major Version */
338 { 0x00004, 0x00002, 0x0000FFFF, 0x00000000, HDA_RD_FLAG_NONE, hdaRegReadU16 , hdaRegWriteU16 , HDA_REG_IDX(OUTPAY) }, /* Output Payload Capabilities */
339 { 0x00006, 0x00002, 0x0000FFFF, 0x00000000, HDA_RD_FLAG_NONE, hdaRegReadU16 , hdaRegWriteUnimpl , HDA_REG_IDX(INPAY) }, /* Input Payload Capabilities */
340 { 0x00008, 0x00004, 0x00000103, 0x00000103, HDA_RD_FLAG_NONE, hdaRegReadU32 , hdaRegWriteGCTL , HDA_REG_IDX(GCTL) }, /* Global Control */
341 { 0x0000c, 0x00002, 0x00007FFF, 0x00007FFF, HDA_RD_FLAG_NONE, hdaRegReadU16 , hdaRegWriteU16 , HDA_REG_IDX(WAKEEN) }, /* Wake Enable */
342 { 0x0000e, 0x00002, 0x00000007, 0x00000007, HDA_RD_FLAG_NONE, hdaRegReadU8 , hdaRegWriteSTATESTS, HDA_REG_IDX(STATESTS) }, /* State Change Status */
343 { 0x00010, 0x00002, 0xFFFFFFFF, 0x00000000, HDA_RD_FLAG_NONE, hdaRegReadUnimpl, hdaRegWriteUnimpl , HDA_REG_IDX(GSTS) }, /* Global Status */
344 { 0x00018, 0x00002, 0x0000FFFF, 0x00000000, HDA_RD_FLAG_NONE, hdaRegReadU16 , hdaRegWriteU16 , HDA_REG_IDX(OUTSTRMPAY) }, /* Output Stream Payload Capability */
345 { 0x0001A, 0x00002, 0x0000FFFF, 0x00000000, HDA_RD_FLAG_NONE, hdaRegReadU16 , hdaRegWriteUnimpl , HDA_REG_IDX(INSTRMPAY) }, /* Input Stream Payload Capability */
346 { 0x00020, 0x00004, 0xC00000FF, 0xC00000FF, HDA_RD_FLAG_NONE, hdaRegReadU32 , hdaRegWriteU32 , HDA_REG_IDX(INTCTL) }, /* Interrupt Control */
347 { 0x00024, 0x00004, 0xC00000FF, 0x00000000, HDA_RD_FLAG_NONE, hdaRegReadU32 , hdaRegWriteUnimpl , HDA_REG_IDX(INTSTS) }, /* Interrupt Status */
348 { 0x00030, 0x00004, 0xFFFFFFFF, 0x00000000, HDA_RD_FLAG_NONE, hdaRegReadWALCLK, hdaRegWriteUnimpl , HDA_REG_IDX_NOMEM(WALCLK) }, /* Wall Clock Counter */
349 { 0x00034, 0x00004, 0x000000FF, 0x000000FF, HDA_RD_FLAG_NONE, hdaRegReadU32 , hdaRegWriteU32 , HDA_REG_IDX(SSYNC) }, /* Stream Synchronization */
350 { 0x00040, 0x00004, 0xFFFFFF80, 0xFFFFFF80, HDA_RD_FLAG_NONE, hdaRegReadU32 , hdaRegWriteBase , HDA_REG_IDX(CORBLBASE) }, /* CORB Lower Base Address */
351 { 0x00044, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, HDA_RD_FLAG_NONE, hdaRegReadU32 , hdaRegWriteBase , HDA_REG_IDX(CORBUBASE) }, /* CORB Upper Base Address */
352 { 0x00048, 0x00002, 0x000000FF, 0x000000FF, HDA_RD_FLAG_NONE, hdaRegReadU16 , hdaRegWriteCORBWP , HDA_REG_IDX(CORBWP) }, /* CORB Write Pointer */
353 { 0x0004A, 0x00002, 0x000080FF, 0x000080FF, HDA_RD_FLAG_NONE, hdaRegReadU16 , hdaRegWriteCORBRP , HDA_REG_IDX(CORBRP) }, /* CORB Read Pointer */
354 { 0x0004C, 0x00001, 0x00000003, 0x00000003, HDA_RD_FLAG_NONE, hdaRegReadU8 , hdaRegWriteCORBCTL , HDA_REG_IDX(CORBCTL) }, /* CORB Control */
355 { 0x0004D, 0x00001, 0x00000001, 0x00000001, HDA_RD_FLAG_NONE, hdaRegReadU8 , hdaRegWriteCORBSTS , HDA_REG_IDX(CORBSTS) }, /* CORB Status */
356 { 0x0004E, 0x00001, 0x000000F3, 0x00000000, HDA_RD_FLAG_NONE, hdaRegReadU8 , hdaRegWriteUnimpl , HDA_REG_IDX(CORBSIZE) }, /* CORB Size */
357 { 0x00050, 0x00004, 0xFFFFFF80, 0xFFFFFF80, HDA_RD_FLAG_NONE, hdaRegReadU32 , hdaRegWriteBase , HDA_REG_IDX(RIRBLBASE) }, /* RIRB Lower Base Address */
358 { 0x00054, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, HDA_RD_FLAG_NONE, hdaRegReadU32 , hdaRegWriteBase , HDA_REG_IDX(RIRBUBASE) }, /* RIRB Upper Base Address */
359 { 0x00058, 0x00002, 0x000000FF, 0x00008000, HDA_RD_FLAG_NONE, hdaRegReadU8 , hdaRegWriteRIRBWP , HDA_REG_IDX(RIRBWP) }, /* RIRB Write Pointer */
360 { 0x0005A, 0x00002, 0x000000FF, 0x000000FF, HDA_RD_FLAG_NONE, hdaRegReadU16 , hdaRegWriteU16 , HDA_REG_IDX(RINTCNT) }, /* Response Interrupt Count */
361 { 0x0005C, 0x00001, 0x00000007, 0x00000007, HDA_RD_FLAG_NONE, hdaRegReadU8 , hdaRegWriteU8 , HDA_REG_IDX(RIRBCTL) }, /* RIRB Control */
362 { 0x0005D, 0x00001, 0x00000005, 0x00000005, HDA_RD_FLAG_NONE, hdaRegReadU8 , hdaRegWriteRIRBSTS , HDA_REG_IDX(RIRBSTS) }, /* RIRB Status */
363 { 0x0005E, 0x00001, 0x000000F3, 0x00000000, HDA_RD_FLAG_NONE, hdaRegReadU8 , hdaRegWriteUnimpl , HDA_REG_IDX(RIRBSIZE) }, /* RIRB Size */
364 { 0x00060, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, HDA_RD_FLAG_NONE, hdaRegReadU32 , hdaRegWriteU32 , HDA_REG_IDX(IC) }, /* Immediate Command */
365 { 0x00064, 0x00004, 0x00000000, 0xFFFFFFFF, HDA_RD_FLAG_NONE, hdaRegReadU32 , hdaRegWriteUnimpl , HDA_REG_IDX(IR) }, /* Immediate Response */
366 { 0x00068, 0x00002, 0x00000002, 0x00000002, HDA_RD_FLAG_NONE, hdaRegReadIRS , hdaRegWriteIRS , HDA_REG_IDX(IRS) }, /* Immediate Command Status */
367 { 0x00070, 0x00004, 0xFFFFFFFF, 0xFFFFFF81, HDA_RD_FLAG_NONE, hdaRegReadU32 , hdaRegWriteBase , HDA_REG_IDX(DPLBASE) }, /* DMA Position Lower Base */
368 { 0x00074, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, HDA_RD_FLAG_NONE, hdaRegReadU32 , hdaRegWriteBase , HDA_REG_IDX(DPUBASE) }, /* DMA Position Upper Base */
369 /* 4 Serial Data In (SDI). */
370 HDA_REG_MAP_DEF_STREAM(0, SD0),
371 HDA_REG_MAP_DEF_STREAM(1, SD1),
372 HDA_REG_MAP_DEF_STREAM(2, SD2),
373 HDA_REG_MAP_DEF_STREAM(3, SD3),
374 /* 4 Serial Data Out (SDO). */
375 HDA_REG_MAP_DEF_STREAM(4, SD4),
376 HDA_REG_MAP_DEF_STREAM(5, SD5),
377 HDA_REG_MAP_DEF_STREAM(6, SD6),
378 HDA_REG_MAP_DEF_STREAM(7, SD7)
379};
380
381const HDAREGALIAS g_aHdaRegAliases[] =
382{
383 { 0x2084, HDA_REG_SD0LPIB },
384 { 0x20a4, HDA_REG_SD1LPIB },
385 { 0x20c4, HDA_REG_SD2LPIB },
386 { 0x20e4, HDA_REG_SD3LPIB },
387 { 0x2104, HDA_REG_SD4LPIB },
388 { 0x2124, HDA_REG_SD5LPIB },
389 { 0x2144, HDA_REG_SD6LPIB },
390 { 0x2164, HDA_REG_SD7LPIB }
391};
392
393#ifdef IN_RING3
394/** HDABDLEDESC field descriptors for the v7 saved state. */
395static SSMFIELD const g_aSSMBDLEDescFields7[] =
396{
397 SSMFIELD_ENTRY(HDABDLEDESC, u64BufAdr),
398 SSMFIELD_ENTRY(HDABDLEDESC, u32BufSize),
399 SSMFIELD_ENTRY(HDABDLEDESC, fFlags),
400 SSMFIELD_ENTRY_TERM()
401};
402
403/** HDABDLESTATE field descriptors for the v6+ saved state. */
404static SSMFIELD const g_aSSMBDLEStateFields6[] =
405{
406 SSMFIELD_ENTRY(HDABDLESTATE, u32BDLIndex),
407 SSMFIELD_ENTRY(HDABDLESTATE, cbBelowFIFOW),
408 SSMFIELD_ENTRY_OLD(FIFO, HDA_FIFO_MAX), /* Deprecated; now is handled in the stream's circular buffer. */
409 SSMFIELD_ENTRY(HDABDLESTATE, u32BufOff),
410 SSMFIELD_ENTRY_TERM()
411};
412
413/** HDABDLESTATE field descriptors for the v7 saved state. */
414static SSMFIELD const g_aSSMBDLEStateFields7[] =
415{
416 SSMFIELD_ENTRY(HDABDLESTATE, u32BDLIndex),
417 SSMFIELD_ENTRY(HDABDLESTATE, cbBelowFIFOW),
418 SSMFIELD_ENTRY(HDABDLESTATE, u32BufOff),
419 SSMFIELD_ENTRY_TERM()
420};
421
422/** HDASTREAMSTATE field descriptors for the v6 saved state. */
423static SSMFIELD const g_aSSMStreamStateFields6[] =
424{
425 SSMFIELD_ENTRY_OLD(cBDLE, sizeof(uint16_t)), /* Deprecated. */
426 SSMFIELD_ENTRY(HDASTREAMSTATE, uCurBDLE),
427 SSMFIELD_ENTRY_OLD(fStop, 1), /* Deprecated; see SSMR3PutBool(). */
428 SSMFIELD_ENTRY_OLD(fRunning, 1), /* Deprecated; using the HDA_SDCTL_RUN bit is sufficient. */
429 SSMFIELD_ENTRY(HDASTREAMSTATE, fInReset),
430 SSMFIELD_ENTRY_TERM()
431};
432
433/** HDASTREAMSTATE field descriptors for the v7 saved state. */
434static SSMFIELD const g_aSSMStreamStateFields7[] =
435{
436 SSMFIELD_ENTRY(HDASTREAMSTATE, uCurBDLE),
437 SSMFIELD_ENTRY(HDASTREAMSTATE, fInReset),
438 SSMFIELD_ENTRY(HDASTREAMSTATE, uTimerTS),
439 SSMFIELD_ENTRY_TERM()
440};
441
442/** HDASTREAMPERIOD field descriptors for the v7 saved state. */
443static SSMFIELD const g_aSSMStreamPeriodFields7[] =
444{
445 SSMFIELD_ENTRY(HDASTREAMPERIOD, u64StartWalClk),
446 SSMFIELD_ENTRY(HDASTREAMPERIOD, u64ElapsedWalClk),
447 SSMFIELD_ENTRY(HDASTREAMPERIOD, framesTransferred),
448 SSMFIELD_ENTRY(HDASTREAMPERIOD, cIntPending),
449 SSMFIELD_ENTRY_TERM()
450};
451#endif
452
453/**
454 * 32-bit size indexed masks, i.e. g_afMasks[2 bytes] = 0xffff.
455 */
456static uint32_t const g_afMasks[5] =
457{
458 UINT32_C(0), UINT32_C(0x000000ff), UINT32_C(0x0000ffff), UINT32_C(0x00ffffff), UINT32_C(0xffffffff)
459};
460
461
462#ifdef IN_RING3
463/**
464 * Retrieves the number of bytes of a FIFOW register.
465 *
466 * @return Number of bytes of a given FIFOW register.
467 */
468DECLINLINE(uint8_t) hdaSDFIFOWToBytes(uint32_t u32RegFIFOW)
469{
470 uint32_t cb;
471 switch (u32RegFIFOW)
472 {
473 case HDA_SDFIFOW_8B: cb = 8; break;
474 case HDA_SDFIFOW_16B: cb = 16; break;
475 case HDA_SDFIFOW_32B: cb = 32; break;
476 default: cb = 0; break;
477 }
478
479 Assert(RT_IS_POWER_OF_TWO(cb));
480 return cb;
481}
482
483/**
484 * Reschedules pending interrupts for all audio streams which have complete
485 * audio periods but did not have the chance to issue their (pending) interrupts yet.
486 *
487 * @param pThis The HDA device state.
488 */
489static void hdaReschedulePendingInterrupts(PHDASTATE pThis)
490{
491 bool fInterrupt = false;
492
493 for (uint8_t i = 0; i < HDA_MAX_STREAMS; ++i)
494 {
495 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, i);
496 if (!pStream)
497 continue;
498
499 if ( hdaStreamPeriodIsComplete (&pStream->State.Period)
500 && hdaStreamPeriodNeedsInterrupt(&pStream->State.Period)
501 && hdaWalClkSet(pThis, hdaStreamPeriodGetAbsElapsedWalClk(&pStream->State.Period), false /* fForce */))
502 {
503 fInterrupt = true;
504 break;
505 }
506 }
507
508 LogFunc(("fInterrupt=%RTbool\n", fInterrupt));
509
510#ifndef DEBUG
511 hdaProcessInterrupt(pThis);
512#else
513 hdaProcessInterrupt(pThis, __FUNCTION__);
514#endif
515}
516#endif
517
518/**
519 * Looks up a register at the exact offset given by @a offReg.
520 *
521 * @returns Register index on success, -1 if not found.
522 * @param offReg The register offset.
523 */
524static int hdaRegLookup(uint32_t offReg)
525{
526 /*
527 * Aliases.
528 */
529 if (offReg >= g_aHdaRegAliases[0].offReg)
530 {
531 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegAliases); i++)
532 if (offReg == g_aHdaRegAliases[i].offReg)
533 return g_aHdaRegAliases[i].idxAlias;
534 Assert(g_aHdaRegMap[RT_ELEMENTS(g_aHdaRegMap) - 1].offset < offReg);
535 return -1;
536 }
537
538 /*
539 * Binary search the
540 */
541 int idxEnd = RT_ELEMENTS(g_aHdaRegMap);
542 int idxLow = 0;
543 for (;;)
544 {
545 int idxMiddle = idxLow + (idxEnd - idxLow) / 2;
546 if (offReg < g_aHdaRegMap[idxMiddle].offset)
547 {
548 if (idxLow == idxMiddle)
549 break;
550 idxEnd = idxMiddle;
551 }
552 else if (offReg > g_aHdaRegMap[idxMiddle].offset)
553 {
554 idxLow = idxMiddle + 1;
555 if (idxLow >= idxEnd)
556 break;
557 }
558 else
559 return idxMiddle;
560 }
561
562#ifdef RT_STRICT
563 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegMap); i++)
564 Assert(g_aHdaRegMap[i].offset != offReg);
565#endif
566 return -1;
567}
568
569/**
570 * Looks up a register covering the offset given by @a offReg.
571 *
572 * @returns Register index on success, -1 if not found.
573 * @param offReg The register offset.
574 */
575static int hdaRegLookupWithin(uint32_t offReg)
576{
577 /*
578 * Aliases.
579 */
580 if (offReg >= g_aHdaRegAliases[0].offReg)
581 {
582 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegAliases); i++)
583 {
584 uint32_t off = offReg - g_aHdaRegAliases[i].offReg;
585 if (off < 4 && off < g_aHdaRegMap[g_aHdaRegAliases[i].idxAlias].size)
586 return g_aHdaRegAliases[i].idxAlias;
587 }
588 Assert(g_aHdaRegMap[RT_ELEMENTS(g_aHdaRegMap) - 1].offset < offReg);
589 return -1;
590 }
591
592 /*
593 * Binary search the register map.
594 */
595 int idxEnd = RT_ELEMENTS(g_aHdaRegMap);
596 int idxLow = 0;
597 for (;;)
598 {
599 int idxMiddle = idxLow + (idxEnd - idxLow) / 2;
600 if (offReg < g_aHdaRegMap[idxMiddle].offset)
601 {
602 if (idxLow == idxMiddle)
603 break;
604 idxEnd = idxMiddle;
605 }
606 else if (offReg >= g_aHdaRegMap[idxMiddle].offset + g_aHdaRegMap[idxMiddle].size)
607 {
608 idxLow = idxMiddle + 1;
609 if (idxLow >= idxEnd)
610 break;
611 }
612 else
613 return idxMiddle;
614 }
615
616#ifdef RT_STRICT
617 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegMap); i++)
618 Assert(offReg - g_aHdaRegMap[i].offset >= g_aHdaRegMap[i].size);
619#endif
620 return -1;
621}
622
623#ifdef IN_RING3
624/**
625 * Synchronizes the CORB / RIRB buffers between internal <-> device state.
626 *
627 * @returns IPRT status code.
628 * @param pThis HDA state.
629 * @param fLocal Specify true to synchronize HDA state's CORB buffer with the device state,
630 * or false to synchronize the device state's RIRB buffer with the HDA state.
631 *
632 * @todo r=andy Break this up into two functions?
633 */
634static int hdaCmdSync(PHDASTATE pThis, bool fLocal)
635{
636 int rc = VINF_SUCCESS;
637 if (fLocal)
638 {
639 Assert((HDA_REG(pThis, CORBCTL) & HDA_CORBCTL_DMA));
640 Assert(pThis->u64CORBBase);
641 AssertPtr(pThis->pu32CorbBuf);
642 Assert(pThis->cbCorbBuf);
643
644 rc = PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), pThis->u64CORBBase, pThis->pu32CorbBuf, pThis->cbCorbBuf);
645 if (RT_FAILURE(rc))
646 AssertRCReturn(rc, rc);
647# ifdef DEBUG_CMD_BUFFER
648 uint8_t i = 0;
649 do
650 {
651 LogFunc(("CORB%02x: ", i));
652 uint8_t j = 0;
653 do
654 {
655 const char *pszPrefix;
656 if ((i + j) == HDA_REG(pThis, CORBRP));
657 pszPrefix = "[R]";
658 else if ((i + j) == HDA_REG(pThis, CORBWP));
659 pszPrefix = "[W]";
660 else
661 pszPrefix = " "; /* three spaces */
662 LogFunc(("%s%08x", pszPrefix, pThis->pu32CorbBuf[i + j]));
663 j++;
664 } while (j < 8);
665 LogFunc(("\n"));
666 i += 8;
667 } while(i != 0);
668# endif
669 }
670 else
671 {
672 Assert((HDA_REG(pThis, RIRBCTL) & HDA_RIRBCTL_RDMAEN));
673 rc = PDMDevHlpPCIPhysWrite(pThis->CTX_SUFF(pDevIns), pThis->u64RIRBBase, pThis->pu64RirbBuf, pThis->cbRirbBuf);
674 if (RT_FAILURE(rc))
675 AssertRCReturn(rc, rc);
676# ifdef DEBUG_CMD_BUFFER
677 uint8_t i = 0;
678 do {
679 LogFunc(("RIRB%02x: ", i));
680 uint8_t j = 0;
681 do {
682 const char *prefix;
683 if ((i + j) == HDA_REG(pThis, RIRBWP))
684 prefix = "[W]";
685 else
686 prefix = " ";
687 LogFunc((" %s%016lx", prefix, pThis->pu64RirbBuf[i + j]));
688 } while (++j < 8);
689 LogFunc(("\n"));
690 i += 8;
691 } while (i != 0);
692# endif
693 }
694 return rc;
695}
696
697/**
698 * Processes the next CORB buffer command in the queue.
699 * This will invoke the HDA codec verb dispatcher.
700 *
701 * @returns IPRT status code.
702 * @param pThis HDA state.
703 */
704static int hdaCORBCmdProcess(PHDASTATE pThis)
705{
706 int rc = hdaCmdSync(pThis, true);
707 if (RT_FAILURE(rc))
708 AssertRCReturn(rc, rc);
709
710 uint8_t corbRp = HDA_REG(pThis, CORBRP);
711 uint8_t corbWp = HDA_REG(pThis, CORBWP);
712 uint8_t rirbWp = HDA_REG(pThis, RIRBWP);
713
714 Assert((corbWp != corbRp));
715 Log3Func(("CORB(RP:%x, WP:%x) RIRBWP:%x\n", HDA_REG(pThis, CORBRP), HDA_REG(pThis, CORBWP), HDA_REG(pThis, RIRBWP)));
716
717 while (corbRp != corbWp)
718 {
719 uint64_t uResp;
720 uint32_t uCmd = pThis->pu32CorbBuf[++corbRp];
721
722 int rc2 = pThis->pCodec->pfnLookup(pThis->pCodec, HDA_CODEC_CMD(uCmd, 0 /* Codec index */), &uResp);
723 if (RT_FAILURE(rc2))
724 LogFunc(("Codec lookup failed with rc=%Rrc\n", rc2));
725
726 (rirbWp)++;
727
728 if ( (uResp & CODEC_RESPONSE_UNSOLICITED)
729 && !(HDA_REG(pThis, GCTL) & HDA_GCTL_UNSOL))
730 {
731 LogFunc(("Unexpected unsolicited response\n"));
732 HDA_REG(pThis, CORBRP) = corbRp;
733 return rc;
734 }
735
736 pThis->pu64RirbBuf[rirbWp] = uResp;
737
738 pThis->u8RespIntCnt++;
739 if (pThis->u8RespIntCnt == RINTCNT_N(pThis))
740 break;
741 }
742
743 HDA_REG(pThis, CORBRP) = corbRp;
744 HDA_REG(pThis, RIRBWP) = rirbWp;
745
746 rc = hdaCmdSync(pThis, false);
747
748 Log3Func(("CORB(RP:%x, WP:%x) RIRBWP:%x\n",
749 HDA_REG(pThis, CORBRP), HDA_REG(pThis, CORBWP), HDA_REG(pThis, RIRBWP)));
750
751 if (HDA_REG(pThis, RIRBCTL) & HDA_RIRBCTL_RINTCTL) /* Response Interrupt Control (RINTCTL) enabled? */
752 {
753 if (pThis->u8RespIntCnt)
754 {
755 pThis->u8RespIntCnt = 0;
756
757 HDA_REG(pThis, RIRBSTS) |= HDA_RIRBSTS_RINTFL;
758
759#ifndef DEBUG
760 rc = hdaProcessInterrupt(pThis);
761#else
762 rc = hdaProcessInterrupt(pThis, __FUNCTION__);
763#endif
764 }
765 }
766
767 if (RT_FAILURE(rc))
768 AssertRCReturn(rc, rc);
769
770 return rc;
771}
772#endif /* IN_RING3 */
773
774/* Register access handlers. */
775
776static int hdaRegReadUnimpl(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
777{
778 RT_NOREF_PV(pThis); RT_NOREF_PV(iReg);
779 *pu32Value = 0;
780 return VINF_SUCCESS;
781}
782
783static int hdaRegWriteUnimpl(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
784{
785 RT_NOREF_PV(pThis); RT_NOREF_PV(iReg); RT_NOREF_PV(u32Value);
786 return VINF_SUCCESS;
787}
788
789/* U8 */
790static int hdaRegReadU8(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
791{
792 Assert(((pThis->au32Regs[g_aHdaRegMap[iReg].mem_idx] & g_aHdaRegMap[iReg].readable) & 0xffffff00) == 0);
793 return hdaRegReadU32(pThis, iReg, pu32Value);
794}
795
796static int hdaRegWriteU8(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
797{
798 Assert((u32Value & 0xffffff00) == 0);
799 return hdaRegWriteU32(pThis, iReg, u32Value);
800}
801
802/* U16 */
803static int hdaRegReadU16(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
804{
805 Assert(((pThis->au32Regs[g_aHdaRegMap[iReg].mem_idx] & g_aHdaRegMap[iReg].readable) & 0xffff0000) == 0);
806 return hdaRegReadU32(pThis, iReg, pu32Value);
807}
808
809static int hdaRegWriteU16(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
810{
811 Assert((u32Value & 0xffff0000) == 0);
812 return hdaRegWriteU32(pThis, iReg, u32Value);
813}
814
815/* U24 */
816static int hdaRegReadU24(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
817{
818 Assert(((pThis->au32Regs[g_aHdaRegMap[iReg].mem_idx] & g_aHdaRegMap[iReg].readable) & 0xff000000) == 0);
819 return hdaRegReadU32(pThis, iReg, pu32Value);
820}
821
822#ifdef IN_RING3
823static int hdaRegWriteU24(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
824{
825 Assert((u32Value & 0xff000000) == 0);
826 return hdaRegWriteU32(pThis, iReg, u32Value);
827}
828#endif
829
830/* U32 */
831static int hdaRegReadU32(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
832{
833 uint32_t iRegMem = g_aHdaRegMap[iReg].mem_idx;
834
835 *pu32Value = pThis->au32Regs[iRegMem] & g_aHdaRegMap[iReg].readable;
836 return VINF_SUCCESS;
837}
838
839static int hdaRegWriteU32(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
840{
841 uint32_t iRegMem = g_aHdaRegMap[iReg].mem_idx;
842
843 pThis->au32Regs[iRegMem] = (u32Value & g_aHdaRegMap[iReg].writable)
844 | (pThis->au32Regs[iRegMem] & ~g_aHdaRegMap[iReg].writable);
845 return VINF_SUCCESS;
846}
847
848static int hdaRegWriteGCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
849{
850 RT_NOREF_PV(iReg);
851
852 if (u32Value & HDA_GCTL_CRST)
853 {
854 /* Set the CRST bit to indicate that we're leaving reset mode. */
855 HDA_REG(pThis, GCTL) |= HDA_GCTL_CRST;
856 LogFunc(("Guest leaving HDA reset\n"));
857 }
858 else
859 {
860#ifdef IN_RING3
861 /* Enter reset state. */
862 LogFunc(("Guest entering HDA reset with DMA(RIRB:%s, CORB:%s)\n",
863 HDA_REG(pThis, CORBCTL) & HDA_CORBCTL_DMA ? "on" : "off",
864 HDA_REG(pThis, RIRBCTL) & HDA_RIRBCTL_RDMAEN ? "on" : "off"));
865
866 /* Clear the CRST bit to indicate that we're in reset state. */
867 HDA_REG(pThis, GCTL) &= ~HDA_GCTL_CRST;
868
869 hdaGCTLReset(pThis);
870#else
871 return VINF_IOM_R3_MMIO_WRITE;
872#endif
873 }
874
875 if (u32Value & HDA_GCTL_FCNTRL)
876 {
877 /* Flush: GSTS:1 set, see 6.2.6. */
878 HDA_REG(pThis, GSTS) |= HDA_GSTS_FSTS; /* Set the flush status. */
879 /* DPLBASE and DPUBASE should be initialized with initial value (see 6.2.6). */
880 }
881 return VINF_SUCCESS;
882}
883
884static int hdaRegWriteSTATESTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
885{
886 uint32_t v = HDA_REG_IND(pThis, iReg);
887 uint32_t nv = u32Value & HDA_STATESTS_SCSF_MASK;
888
889 HDA_REG(pThis, STATESTS) &= ~(v & nv); /* Write of 1 clears corresponding bit. */
890
891 return VINF_SUCCESS;
892}
893
894static int hdaRegReadLPIB(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
895{
896 const uint8_t uSD = HDA_SD_NUM_FROM_REG(pThis, LPIB, iReg);
897 uint32_t u32LPIB = HDA_STREAM_REG(pThis, LPIB, uSD);
898#ifdef LOG_ENABLED
899 const uint32_t u32CBL = HDA_STREAM_REG(pThis, CBL, uSD);
900 LogFlowFunc(("[SD%RU8] LPIB=%RU32, CBL=%RU32\n", uSD, u32LPIB, u32CBL));
901#endif
902
903 *pu32Value = u32LPIB;
904 return VINF_SUCCESS;
905}
906
907#ifdef IN_RING3
908/**
909 * Returns the current maximum value the wall clock counter can be set to.
910 * This maximum value depends on all currently handled HDA streams and their own current timing.
911 *
912 * @return Current maximum value the wall clock counter can be set to.
913 * @param pThis HDA state.
914 *
915 * @remark Does not actually set the wall clock counter.
916 */
917uint64_t hdaWalClkGetMax(PHDASTATE pThis)
918{
919 const uint64_t u64WalClkCur = ASMAtomicReadU64(&pThis->u64WalClk);
920 const uint64_t u64FrontAbsWalClk = hdaStreamPeriodGetAbsElapsedWalClk(&hdaGetStreamFromSink(pThis, &pThis->SinkFront)->State.Period);
921#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
922# error "Implement me!"
923#endif
924 const uint64_t u64LineInAbsWalClk = hdaStreamPeriodGetAbsElapsedWalClk(&hdaGetStreamFromSink(pThis, &pThis->SinkLineIn)->State.Period);
925#ifdef VBOX_WITH_HDA_MIC_IN
926 const uint64_t u64MicInAbsWalClk = hdaStreamPeriodGetAbsElapsedWalClk(&hdaGetStreamFromSink(pThis, &pThis->SinkMicIn)->State.Period);
927#endif
928
929 uint64_t u64WalClkNew = RT_MAX(u64WalClkCur, u64FrontAbsWalClk);
930#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
931# error "Implement me!"
932#endif
933 u64WalClkNew = RT_MAX(u64WalClkNew, u64LineInAbsWalClk);
934#ifdef VBOX_WITH_HDA_MIC_IN
935 u64WalClkNew = RT_MAX(u64WalClkNew, u64MicInAbsWalClk);
936#endif
937
938 Log3Func(("%RU64 -> Front=%RU64, LineIn=%RU64 -> %RU64\n",
939 u64WalClkCur, u64FrontAbsWalClk, u64LineInAbsWalClk, u64WalClkNew));
940
941 return u64WalClkNew;
942}
943#endif /* IN_RING3 */
944
945static int hdaRegReadWALCLK(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
946{
947#ifdef IN_RING3
948 RT_NOREF(iReg);
949
950 *pu32Value = RT_LO_U32(ASMAtomicReadU64(&pThis->u64WalClk));
951
952 Log3Func(("%RU32 (max @ %RU64)\n",*pu32Value, hdaWalClkGetMax(pThis)));
953
954 return VINF_SUCCESS;
955#else
956 RT_NOREF(pThis, iReg, pu32Value);
957 return VINF_IOM_R3_MMIO_WRITE;
958#endif
959}
960
961static int hdaRegWriteCORBRP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
962{
963 RT_NOREF_PV(iReg);
964
965 if (u32Value & HDA_CORBRP_RST)
966 HDA_REG(pThis, CORBRP) = HDA_CORBRP_RST; /* Clears the pointer. */
967 else
968 HDA_REG(pThis, CORBRP) &= ~HDA_CORBRP_RST; /* Only CORBRP_RST bit is writable. */
969
970 return VINF_SUCCESS;
971}
972
973static int hdaRegWriteCORBCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
974{
975#ifdef IN_RING3
976 int rc = hdaRegWriteU8(pThis, iReg, u32Value);
977 AssertRC(rc);
978 if ( (uint8_t)HDA_REG(pThis, CORBWP) != (uint8_t)HDA_REG(pThis, CORBRP)
979 && (HDA_REG(pThis, CORBCTL) & HDA_CORBCTL_DMA))
980 {
981 return hdaCORBCmdProcess(pThis);
982 }
983 return rc;
984#else
985 RT_NOREF_PV(pThis); RT_NOREF_PV(iReg); RT_NOREF_PV(u32Value);
986 return VINF_IOM_R3_MMIO_WRITE;
987#endif
988}
989
990static int hdaRegWriteCORBSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
991{
992 RT_NOREF_PV(iReg);
993
994 uint32_t v = HDA_REG(pThis, CORBSTS);
995 HDA_REG(pThis, CORBSTS) &= ~(v & u32Value);
996 return VINF_SUCCESS;
997}
998
999static int hdaRegWriteCORBWP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1000{
1001#ifdef IN_RING3
1002 int rc;
1003 rc = hdaRegWriteU16(pThis, iReg, u32Value);
1004 if (RT_FAILURE(rc))
1005 AssertRCReturn(rc, rc);
1006 if ((uint8_t)HDA_REG(pThis, CORBWP) == (uint8_t)HDA_REG(pThis, CORBRP))
1007 return VINF_SUCCESS;
1008 if (!(HDA_REG(pThis, CORBCTL) & HDA_CORBCTL_DMA))
1009 return VINF_SUCCESS;
1010 rc = hdaCORBCmdProcess(pThis);
1011 return rc;
1012#else /* !IN_RING3 */
1013 RT_NOREF_PV(pThis); RT_NOREF_PV(iReg); RT_NOREF_PV(u32Value);
1014 return VINF_IOM_R3_MMIO_WRITE;
1015#endif /* IN_RING3 */
1016}
1017
1018static int hdaRegWriteSDCBL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1019{
1020#ifdef IN_RING3
1021 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, HDA_SD_NUM_FROM_REG(pThis, CBL, iReg));
1022 if (!pStream)
1023 {
1024 LogFunc(("[SD%RU8] Warning: Changing SDCBL on non-attached stream (0x%x)\n",
1025 HDA_SD_NUM_FROM_REG(pThis, CTL, iReg), u32Value));
1026 return hdaRegWriteU32(pThis, iReg, u32Value);
1027 }
1028
1029 pStream->u32CBL = u32Value;
1030
1031 int rc2 = hdaRegWriteU32(pThis, iReg, u32Value);
1032 AssertRC(rc2);
1033
1034 LogFlowFunc(("[SD%RU8] CBL=%RU32\n", pStream->u8SD, u32Value));
1035
1036 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
1037#else /* !IN_RING3 */
1038 RT_NOREF_PV(pThis); RT_NOREF_PV(iReg); RT_NOREF_PV(u32Value);
1039 return VINF_IOM_R3_MMIO_WRITE;
1040#endif /* IN_RING3 */
1041}
1042
1043static int hdaRegWriteSDCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1044{
1045#ifdef IN_RING3
1046 /*
1047 * Some guests write too much (that is, 32-bit with the top 8 bit being junk)
1048 * instead of 24-bit required for SDCTL. So just mask this here to be safe.
1049 */
1050 u32Value = (u32Value & 0x00ffffff);
1051
1052 bool fRun = RT_BOOL(u32Value & HDA_SDCTL_RUN);
1053 bool fInRun = RT_BOOL(HDA_REG_IND(pThis, iReg) & HDA_SDCTL_RUN);
1054
1055 bool fReset = RT_BOOL(u32Value & HDA_SDCTL_SRST);
1056 bool fInReset = RT_BOOL(HDA_REG_IND(pThis, iReg) & HDA_SDCTL_SRST);
1057
1058 /* Get the stream descriptor. */
1059 uint8_t uSD = HDA_SD_NUM_FROM_REG(pThis, CTL, iReg);
1060
1061 LogFunc(("[SD%RU8] fRun=%RTbool, fInRun=%RTbool, fReset=%RTbool, fInReset=%RTbool, %R[sdctl]\n",
1062 uSD, fRun, fInRun, fReset, fInReset, u32Value));
1063
1064 /*
1065 * Extract the stream tag the guest wants to use for this specific
1066 * stream descriptor (SDn). This only can happen if the stream is in a non-running
1067 * state, so we're doing the lookup and assignment here.
1068 *
1069 * So depending on the guest OS, SD3 can use stream tag 4, for example.
1070 */
1071 uint8_t uTag = (u32Value >> HDA_SDCTL_NUM_SHIFT) & HDA_SDCTL_NUM_MASK;
1072 if (uTag > HDA_MAX_TAGS)
1073 {
1074 LogFunc(("[SD%RU8] Warning: Invalid stream tag %RU8 specified!\n", uSD, uTag));
1075 return hdaRegWriteU24(pThis, iReg, u32Value);
1076 }
1077
1078 PHDATAG pTag = &pThis->aTags[uTag];
1079 AssertPtr(pTag);
1080
1081 LogFunc(("[SD%RU8] Using stream tag=%RU8\n", uSD, uTag));
1082
1083 /* Assign new values. */
1084 pTag->uTag = uTag;
1085 pTag->pStream = hdaGetStreamFromSD(pThis, uSD);
1086
1087 PHDASTREAM pStream = pTag->pStream;
1088 AssertPtr(pStream);
1089
1090 if (fInReset)
1091 {
1092 Assert(!fReset);
1093 Assert(!fInRun && !fRun);
1094
1095 /* Exit reset state. */
1096 ASMAtomicXchgBool(&pStream->State.fInReset, false);
1097
1098 /* Report that we're done resetting this stream by clearing SRST. */
1099 HDA_STREAM_REG(pThis, CTL, uSD) &= ~HDA_SDCTL_SRST;
1100
1101 LogFunc(("[SD%RU8] Reset exit\n", uSD));
1102 }
1103 else if (fReset)
1104 {
1105 /* ICH6 datasheet 18.2.33 says that RUN bit should be cleared before initiation of reset. */
1106 Assert(!fInRun && !fRun);
1107
1108 LogFunc(("[SD%RU8] Reset enter\n", pStream->u8SD));
1109
1110 hdaStreamLock(pStream);
1111
1112#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
1113 hdaStreamAsyncIOLock(pStream);
1114 hdaStreamAsyncIOEnable(pStream, false /* fEnable */);
1115#endif
1116 hdaStreamReset(pThis, pStream, pStream->u8SD);
1117
1118#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
1119 hdaStreamAsyncIOUnlock(pStream);
1120#endif
1121 hdaStreamUnlock(pStream);
1122 }
1123 else
1124 {
1125 /*
1126 * We enter here to change DMA states only.
1127 */
1128 if (fInRun != fRun)
1129 {
1130 Assert(!fReset && !fInReset);
1131 LogFunc(("[SD%RU8] State changed (fRun=%RTbool)\n", pStream->u8SD, fRun));
1132
1133 hdaStreamLock(pStream);
1134
1135#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
1136 hdaStreamAsyncIOLock(pStream);
1137 hdaStreamAsyncIOEnable(pStream, fRun /* fEnable */);
1138#endif
1139 /* (Re-)initialize the stream with current values. */
1140 int rc2 = hdaStreamInit(pStream, pStream->u8SD);
1141 AssertRC(rc2);
1142
1143 /* Enable/disable the stream. */
1144 hdaStreamEnable(pStream, fRun /* fEnable */);
1145
1146 if (fRun)
1147 {
1148 /* (Re-)init the stream's period. */
1149 hdaStreamPeriodInit(&pStream->State.Period,
1150 pStream->u8SD, pStream->u16LVI, pStream->u32CBL, &pStream->State.strmCfg);
1151
1152 /* Begin a new period for this stream. */
1153 rc2 = hdaStreamPeriodBegin(&pStream->State.Period, hdaWalClkGetCurrent(pThis)/* Use current wall clock time */);
1154 AssertRC(rc2);
1155 }
1156 else
1157 {
1158 /* Make sure to (re-)schedule outstanding (delayed) interrupts. */
1159 hdaReschedulePendingInterrupts(pThis);
1160
1161 /* Reset the period. */
1162 hdaStreamPeriodReset(&pStream->State.Period);
1163 }
1164
1165#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
1166 hdaStreamAsyncIOUnlock(pStream);
1167#endif
1168 /* Make sure to leave the lock before (eventually) starting the timer. */
1169 hdaStreamUnlock(pStream);
1170
1171#ifndef VBOX_WITH_AUDIO_HDA_CALLBACKS
1172 /* See if we need to start or stop the timer. */
1173 if (!fRun)
1174 hdaTimerMaybeStop(pThis);
1175 else
1176 hdaTimerMaybeStart(pThis);
1177#endif
1178 }
1179 }
1180
1181 int rc2 = hdaRegWriteU24(pThis, iReg, u32Value);
1182 AssertRC(rc2);
1183
1184 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
1185#else /* !IN_RING3 */
1186 RT_NOREF_PV(pThis); RT_NOREF_PV(iReg); RT_NOREF_PV(u32Value);
1187 return VINF_IOM_R3_MMIO_WRITE;
1188#endif /* IN_RING3 */
1189}
1190
1191static int hdaRegWriteSDSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1192{
1193#ifdef IN_RING3
1194 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, HDA_SD_NUM_FROM_REG(pThis, STS, iReg));
1195 if (!pStream)
1196 {
1197 AssertMsgFailed(("[SD%RU8] Warning: Writing SDSTS on non-attached stream (0x%x)\n",
1198 HDA_SD_NUM_FROM_REG(pThis, STS, iReg), u32Value));
1199 return hdaRegWriteU16(pThis, iReg, u32Value);
1200 }
1201
1202 uint32_t v = HDA_REG_IND(pThis, iReg);
1203
1204 /* Clear (zero) FIFOE, DESE and BCIS bits when writing 1 to it (6.2.33). */
1205 HDA_REG_IND(pThis, iReg) &= ~(u32Value & v);
1206
1207 /* Some guests tend to write SDnSTS even if the stream is not running.
1208 * So make sure to check if the RUN bit is set first. */
1209 const bool fInRun = RT_BOOL(HDA_STREAM_REG(pThis, CTL, pStream->u8SD) & HDA_SDCTL_RUN);
1210
1211 Log3Func(("[SD%RU8] fRun=%RTbool %R[sdsts]\n", pStream->u8SD, fInRun, v));
1212
1213 PHDASTREAMPERIOD pPeriod = &pStream->State.Period;
1214
1215 if (hdaStreamPeriodLock(pPeriod))
1216 {
1217 const bool fNeedsInterrupt = hdaStreamPeriodNeedsInterrupt(pPeriod);
1218 if (fNeedsInterrupt)
1219 hdaStreamPeriodReleaseInterrupt(pPeriod);
1220
1221 if (hdaStreamPeriodIsComplete(pPeriod))
1222 {
1223 hdaStreamPeriodEnd(pPeriod);
1224
1225 if (fInRun)
1226 hdaStreamPeriodBegin(pPeriod, hdaWalClkGetCurrent(pThis) /* Use current wall clock time */);
1227 }
1228
1229 hdaStreamPeriodUnlock(pPeriod); /* Unlock before processing interrupt. */
1230
1231 if (fNeedsInterrupt)
1232 {
1233#ifndef DEBUG
1234 hdaProcessInterrupt(pThis);
1235#else
1236 hdaProcessInterrupt(pThis, __FUNCTION__);
1237#endif
1238 }
1239 }
1240
1241 return VINF_SUCCESS;
1242#else /* IN_RING3 */
1243 RT_NOREF(pThis, iReg, u32Value);
1244 return VINF_IOM_R3_MMIO_WRITE;
1245#endif /* !IN_RING3 */
1246}
1247
1248static int hdaRegWriteSDLVI(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1249{
1250#ifdef IN_RING3
1251 if (HDA_REG_IND(pThis, iReg) == u32Value) /* Value already set? */
1252 return VINF_SUCCESS;
1253
1254 uint8_t uSD = HDA_SD_NUM_FROM_REG(pThis, LVI, iReg);
1255
1256 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, uSD);
1257 if (!pStream)
1258 {
1259 AssertMsgFailed(("[SD%RU8] Warning: Changing SDLVI on non-attached stream (0x%x)\n", uSD, u32Value));
1260 return hdaRegWriteU16(pThis, iReg, u32Value);
1261 }
1262
1263 /** @todo Validate LVI. */
1264 pStream->u16LVI = u32Value;
1265 LogFunc(("[SD%RU8] Updating LVI to %RU16\n", uSD, pStream->u16LVI));
1266
1267# ifdef HDA_USE_DMA_ACCESS_HANDLER
1268 if (hdaGetDirFromSD(uSD) == PDMAUDIODIR_OUT)
1269 {
1270 /* Try registering the DMA handlers.
1271 * As we can't be sure in which order LVI + BDL base are set, try registering in both routines. */
1272 if (hdaStreamRegisterDMAHandlers(pThis, pStream))
1273 LogFunc(("[SD%RU8] DMA logging enabled\n", pStream->u8SD));
1274 }
1275# endif
1276
1277 int rc2 = hdaRegWriteU16(pThis, iReg, u32Value);
1278 AssertRC(rc2);
1279
1280 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
1281#else /* !IN_RING3 */
1282 RT_NOREF_PV(pThis); RT_NOREF_PV(iReg); RT_NOREF_PV(u32Value);
1283 return VINF_IOM_R3_MMIO_WRITE;
1284#endif /* IN_RING3 */
1285}
1286
1287static int hdaRegWriteSDFIFOW(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1288{
1289#ifdef IN_RING3
1290 uint8_t uSD = HDA_SD_NUM_FROM_REG(pThis, FIFOW, iReg);
1291
1292 if (hdaGetDirFromSD(uSD) != PDMAUDIODIR_IN) /* FIFOW for input streams only. */
1293 {
1294 LogRel(("HDA: Warning: Guest tried to write read-only FIFOW to output stream #%RU8, ignoring\n", uSD));
1295 return VINF_SUCCESS;
1296 }
1297
1298 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, HDA_SD_NUM_FROM_REG(pThis, FIFOW, iReg));
1299 if (!pStream)
1300 {
1301 AssertMsgFailed(("[SD%RU8] Warning: Changing FIFOW on non-attached stream (0x%x)\n", uSD, u32Value));
1302 return hdaRegWriteU16(pThis, iReg, u32Value);
1303 }
1304
1305 uint32_t u32FIFOW = 0;
1306
1307 switch (u32Value)
1308 {
1309 case HDA_SDFIFOW_8B:
1310 case HDA_SDFIFOW_16B:
1311 case HDA_SDFIFOW_32B:
1312 u32FIFOW = u32Value;
1313 break;
1314 default:
1315 LogRel(("HDA: Warning: Guest tried write unsupported FIFOW (0x%x) to stream #%RU8, defaulting to 32 bytes\n",
1316 u32Value, uSD));
1317 AssertFailed();
1318 u32FIFOW = HDA_SDFIFOW_32B;
1319 break;
1320 }
1321
1322 if (u32FIFOW)
1323 {
1324 pStream->u16FIFOW = hdaSDFIFOWToBytes(u32FIFOW);
1325 LogFunc(("[SD%RU8] Updating FIFOW to %RU32 bytes\n", uSD, pStream->u16FIFOW));
1326
1327 int rc2 = hdaRegWriteU16(pThis, iReg, u32FIFOW);
1328 AssertRC(rc2);
1329 }
1330
1331 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
1332#else /* !IN_RING3 */
1333 RT_NOREF_PV(pThis); RT_NOREF_PV(iReg); RT_NOREF_PV(u32Value);
1334 return VINF_IOM_R3_MMIO_WRITE;
1335#endif /* IN_RING3 */
1336}
1337
1338/**
1339 * @note This method could be called for changing value on Output Streams only (ICH6 datasheet 18.2.39).
1340 */
1341static int hdaRegWriteSDFIFOS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1342{
1343#ifdef IN_RING3
1344 uint8_t uSD = HDA_SD_NUM_FROM_REG(pThis, FIFOS, iReg);
1345
1346 if (hdaGetDirFromSD(uSD) != PDMAUDIODIR_OUT) /* FIFOS for output streams only. */
1347 {
1348 LogRel(("HDA: Warning: Guest tried to write read-only FIFOS to input stream #%RU8, ignoring\n", uSD));
1349 return VINF_SUCCESS;
1350 }
1351
1352 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, uSD);
1353 if (!pStream)
1354 {
1355 AssertMsgFailed(("[SD%RU8] Warning: Changing FIFOS on non-attached stream (0x%x)\n", uSD, u32Value));
1356 return hdaRegWriteU16(pThis, iReg, u32Value);
1357 }
1358
1359 uint32_t u32FIFOS = 0;
1360
1361 switch(u32Value)
1362 {
1363 case HDA_SDOFIFO_16B:
1364 case HDA_SDOFIFO_32B:
1365 case HDA_SDOFIFO_64B:
1366 case HDA_SDOFIFO_128B:
1367 case HDA_SDOFIFO_192B:
1368 case HDA_SDOFIFO_256B:
1369 u32FIFOS = u32Value;
1370 break;
1371
1372 default:
1373 LogRel(("HDA: Warning: Guest tried write unsupported FIFOS (0x%x) to stream #%RU8, defaulting to 192 bytes\n",
1374 u32Value, uSD));
1375 AssertFailed();
1376 u32FIFOS = HDA_SDOFIFO_192B;
1377 break;
1378 }
1379
1380 if (u32FIFOS)
1381 {
1382 pStream->u16FIFOS = u32FIFOS + 1;
1383 LogFunc(("[SD%RU8] Updating FIFOS to %RU32 bytes\n", uSD, pStream->u16FIFOS));
1384
1385 int rc2 = hdaRegWriteU16(pThis, iReg, u32FIFOS);
1386 AssertRC(rc2);
1387 }
1388
1389 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
1390#else /* !IN_RING3 */
1391 RT_NOREF_PV(pThis); RT_NOREF_PV(iReg); RT_NOREF_PV(u32Value);
1392 return VINF_IOM_R3_MMIO_WRITE;
1393#endif /* IN_RING3 */
1394}
1395
1396#ifdef IN_RING3
1397/**
1398 * Adds an audio output stream to the device setup using the given configuration.
1399 *
1400 * @returns IPRT status code.
1401 * @param pThis Device state.
1402 * @param pCfg Stream configuration to use for adding a stream.
1403 */
1404static int hdaAddStreamOut(PHDASTATE pThis, PPDMAUDIOSTREAMCFG pCfg)
1405{
1406 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1407 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
1408
1409 AssertReturn(pCfg->enmDir == PDMAUDIODIR_OUT, VERR_INVALID_PARAMETER);
1410
1411 LogFlowFunc(("Stream=%s\n", pCfg->szName));
1412
1413 int rc = VINF_SUCCESS;
1414
1415 bool fUseFront = true; /* Always use front out by default. */
1416#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
1417 bool fUseRear;
1418 bool fUseCenter;
1419 bool fUseLFE;
1420
1421 fUseRear = fUseCenter = fUseLFE = false;
1422
1423 /*
1424 * Use commonly used setups for speaker configurations.
1425 */
1426
1427 /** @todo Make the following configurable through mixer API and/or CFGM? */
1428 switch (pCfg->Props.cChannels)
1429 {
1430 case 3: /* 2.1: Front (Stereo) + LFE. */
1431 {
1432 fUseLFE = true;
1433 break;
1434 }
1435
1436 case 4: /* Quadrophonic: Front (Stereo) + Rear (Stereo). */
1437 {
1438 fUseRear = true;
1439 break;
1440 }
1441
1442 case 5: /* 4.1: Front (Stereo) + Rear (Stereo) + LFE. */
1443 {
1444 fUseRear = true;
1445 fUseLFE = true;
1446 break;
1447 }
1448
1449 case 6: /* 5.1: Front (Stereo) + Rear (Stereo) + Center/LFE. */
1450 {
1451 fUseRear = true;
1452 fUseCenter = true;
1453 fUseLFE = true;
1454 break;
1455 }
1456
1457 default: /* Unknown; fall back to 2 front channels (stereo). */
1458 {
1459 rc = VERR_NOT_SUPPORTED;
1460 break;
1461 }
1462 }
1463#else /* !VBOX_WITH_AUDIO_HDA_51_SURROUND */
1464 /* Only support mono or stereo channels. */
1465 if ( pCfg->Props.cChannels != 1 /* Mono */
1466 && pCfg->Props.cChannels != 2 /* Stereo */)
1467 {
1468 rc = VERR_NOT_SUPPORTED;
1469 }
1470#endif
1471
1472 if (rc == VERR_NOT_SUPPORTED)
1473 {
1474 LogRel2(("HDA: Unsupported channel count (%RU8), falling back to stereo channels\n", pCfg->Props.cChannels));
1475
1476 /* Fall back to 2 channels (see below in fUseFront block). */
1477 rc = VINF_SUCCESS;
1478 }
1479
1480 do
1481 {
1482 if (RT_FAILURE(rc))
1483 break;
1484
1485 if (fUseFront)
1486 {
1487 RTStrPrintf(pCfg->szName, RT_ELEMENTS(pCfg->szName), "Front");
1488
1489 pCfg->DestSource.Dest = PDMAUDIOPLAYBACKDEST_FRONT;
1490 pCfg->enmLayout = PDMAUDIOSTREAMLAYOUT_NON_INTERLEAVED;
1491
1492 pCfg->Props.cChannels = 2;
1493 pCfg->Props.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pCfg->Props.cBits, pCfg->Props.cChannels);
1494
1495 rc = hdaCodecRemoveStream(pThis->pCodec, PDMAUDIOMIXERCTL_FRONT);
1496 if (RT_SUCCESS(rc))
1497 rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_FRONT, pCfg);
1498 }
1499
1500#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
1501 if ( RT_SUCCESS(rc)
1502 && (fUseCenter || fUseLFE))
1503 {
1504 RTStrPrintf(pCfg->szName, RT_ELEMENTS(pCfg->szName), "Center/LFE");
1505
1506 pCfg->DestSource.Dest = PDMAUDIOPLAYBACKDEST_CENTER_LFE;
1507 pCfg->enmLayout = PDMAUDIOSTREAMLAYOUT_NON_INTERLEAVED;
1508
1509 pCfg->Props.cChannels = (fUseCenter && fUseLFE) ? 2 : 1;
1510 pCfg->Props.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pCfg->Props.cBits, pCfg->Props.cChannels);
1511
1512 rc = hdaCodecRemoveStream(pThis->pCodec, PDMAUDIOMIXERCTL_CENTER_LFE);
1513 if (RT_SUCCESS(rc))
1514 rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_CENTER_LFE, pCfg);
1515 }
1516
1517 if ( RT_SUCCESS(rc)
1518 && fUseRear)
1519 {
1520 RTStrPrintf(pCfg->szName, RT_ELEMENTS(pCfg->szName), "Rear");
1521
1522 pCfg->DestSource.Dest = PDMAUDIOPLAYBACKDEST_REAR;
1523 pCfg->enmLayout = PDMAUDIOSTREAMLAYOUT_NON_INTERLEAVED;
1524
1525 pCfg->Props.cChannels = 2;
1526 pCfg->Props.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pCfg->Props.cBits, pCfg->Props.cChannels);
1527
1528 rc = hdaCodecRemoveStream(pThis->pCodec, PDMAUDIOMIXERCTL_REAR);
1529 if (RT_SUCCESS(rc))
1530 rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_REAR, pCfg);
1531 }
1532#endif /* VBOX_WITH_AUDIO_HDA_51_SURROUND */
1533
1534 } while (0);
1535
1536 LogFlowFuncLeaveRC(rc);
1537 return rc;
1538}
1539
1540/**
1541 * Adds an audio input stream to the device setup using the given configuration.
1542 *
1543 * @returns IPRT status code.
1544 * @param pThis Device state.
1545 * @param pCfg Stream configuration to use for adding a stream.
1546 */
1547static int hdaAddStreamIn(PHDASTATE pThis, PPDMAUDIOSTREAMCFG pCfg)
1548{
1549 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1550 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
1551
1552 AssertReturn(pCfg->enmDir == PDMAUDIODIR_IN, VERR_INVALID_PARAMETER);
1553
1554 LogFlowFunc(("Stream=%s, Source=%ld\n", pCfg->szName, pCfg->DestSource.Source));
1555
1556 int rc;
1557
1558 switch (pCfg->DestSource.Source)
1559 {
1560 case PDMAUDIORECSOURCE_LINE:
1561 {
1562 rc = hdaCodecRemoveStream(pThis->pCodec, PDMAUDIOMIXERCTL_LINE_IN);
1563 if (RT_SUCCESS(rc))
1564 rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_LINE_IN, pCfg);
1565 break;
1566 }
1567#ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
1568 case PDMAUDIORECSOURCE_MIC:
1569 {
1570 rc = hdaCodecRemoveStream(pThis->pCodec, PDMAUDIOMIXERCTL_MIC_IN);
1571 if (RT_SUCCESS(rc))
1572 rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_MIC_IN, pCfg);
1573 break;
1574 }
1575#endif
1576 default:
1577 rc = VERR_NOT_SUPPORTED;
1578 break;
1579 }
1580
1581 LogFlowFuncLeaveRC(rc);
1582 return rc;
1583}
1584
1585/**
1586 * Adds an audio stream to the device setup using the given configuration.
1587 *
1588 * @returns IPRT status code.
1589 * @param pThis Device state.
1590 * @param pCfg Stream configuration to use for adding a stream.
1591 */
1592static int hdaAddStream(PHDASTATE pThis, PPDMAUDIOSTREAMCFG pCfg)
1593{
1594 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1595 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
1596
1597 int rc = VINF_SUCCESS;
1598
1599 PHDADRIVER pDrv;
1600 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
1601 {
1602 int rc2;
1603
1604 switch (pCfg->enmDir)
1605 {
1606 case PDMAUDIODIR_OUT:
1607 rc2 = hdaAddStreamOut(pThis, pCfg);
1608 break;
1609
1610 case PDMAUDIODIR_IN:
1611 rc2 = hdaAddStreamIn(pThis, pCfg);
1612 break;
1613
1614 default:
1615 rc2 = VERR_NOT_SUPPORTED;
1616 AssertFailed();
1617 break;
1618 }
1619
1620 if ( RT_FAILURE(rc2)
1621 && (pDrv->fFlags & PDMAUDIODRVFLAGS_PRIMARY)) /* We only care about primary drivers here, the rest may fail. */
1622 {
1623 if (RT_SUCCESS(rc))
1624 rc = rc2;
1625 /* Keep going. */
1626 }
1627 }
1628
1629 return rc;
1630}
1631#endif /* IN_RING3 */
1632
1633static int hdaRegWriteSDFMT(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1634{
1635#ifdef IN_RING3
1636 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, HDA_SD_NUM_FROM_REG(pThis, FMT, iReg));
1637 if (!pStream)
1638 {
1639 LogFunc(("[SD%RU8] Warning: Changing SDFMT on non-attached stream (0x%x)\n",
1640 HDA_SD_NUM_FROM_REG(pThis, FMT, iReg), u32Value));
1641 return hdaRegWriteU16(pThis, iReg, u32Value);
1642 }
1643
1644 /* Write the wanted stream format into the register in any case.
1645 *
1646 * This is important for e.g. MacOS guests, as those try to initialize streams which are not reported
1647 * by the device emulation (wants 4 channels, only have 2 channels at the moment).
1648 *
1649 * When ignoring those (invalid) formats, this leads to MacOS thinking that the device is malfunctioning
1650 * and therefore disabling the device completely. */
1651 int rc = hdaRegWriteU16(pThis, iReg, u32Value);
1652 AssertRC(rc);
1653
1654 rc = hdaStreamInit(pStream, pStream->u8SD);
1655 if (RT_SUCCESS(rc))
1656 {
1657 /* Add the stream to the device setup. */
1658 rc = hdaAddStream(pThis, &pStream->State.strmCfg);
1659# ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
1660 if (RT_SUCCESS(rc))
1661 rc = hdaStreamAsyncIOCreate(pStream);
1662# endif
1663 }
1664 return VINF_SUCCESS; /* Never return failure. */
1665#else /* !IN_RING3 */
1666 RT_NOREF_PV(pThis); RT_NOREF_PV(iReg); RT_NOREF_PV(u32Value);
1667 return VINF_IOM_R3_MMIO_WRITE;
1668#endif
1669}
1670
1671/* Note: Will be called for both, BDPL and BDPU, registers. */
1672DECLINLINE(int) hdaRegWriteSDBDPX(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value, uint8_t uSD)
1673{
1674#ifdef IN_RING3
1675 int rc2 = hdaRegWriteU32(pThis, iReg, u32Value);
1676 AssertRC(rc2);
1677
1678 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, uSD);
1679 if (!pStream)
1680 return VINF_SUCCESS;
1681
1682 /* Update BDL base. */
1683 pStream->u64BDLBase = RT_MAKE_U64(HDA_STREAM_REG(pThis, BDPL, uSD),
1684 HDA_STREAM_REG(pThis, BDPU, uSD));
1685
1686# ifdef HDA_USE_DMA_ACCESS_HANDLER
1687 if (hdaGetDirFromSD(uSD) == PDMAUDIODIR_OUT)
1688 {
1689 /* Try registering the DMA handlers.
1690 * As we can't be sure in which order LVI + BDL base are set, try registering in both routines. */
1691 if (hdaStreamRegisterDMAHandlers(pThis, pStream))
1692 LogFunc(("[SD%RU8] DMA logging enabled\n", pStream->u8SD));
1693 }
1694# endif
1695
1696 LogFlowFunc(("[SD%RU8] BDLBase=0x%x\n", pStream->u8SD, pStream->u64BDLBase));
1697
1698 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
1699#else /* !IN_RING3 */
1700 RT_NOREF_PV(pThis); RT_NOREF_PV(iReg); RT_NOREF_PV(u32Value); RT_NOREF_PV(uSD);
1701 return VINF_IOM_R3_MMIO_WRITE;
1702#endif /* IN_RING3 */
1703}
1704
1705static int hdaRegWriteSDBDPL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1706{
1707 return hdaRegWriteSDBDPX(pThis, iReg, u32Value, HDA_SD_NUM_FROM_REG(pThis, BDPL, iReg));
1708}
1709
1710static int hdaRegWriteSDBDPU(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1711{
1712 return hdaRegWriteSDBDPX(pThis, iReg, u32Value, HDA_SD_NUM_FROM_REG(pThis, BDPU, iReg));
1713}
1714
1715static int hdaRegReadIRS(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
1716{
1717 /* regarding 3.4.3 we should mark IRS as busy in case CORB is active */
1718 if ( HDA_REG(pThis, CORBWP) != HDA_REG(pThis, CORBRP)
1719 || (HDA_REG(pThis, CORBCTL) & HDA_CORBCTL_DMA))
1720 {
1721 HDA_REG(pThis, IRS) = HDA_IRS_ICB; /* busy */
1722 }
1723
1724 return hdaRegReadU32(pThis, iReg, pu32Value);
1725}
1726
1727static int hdaRegWriteIRS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1728{
1729 RT_NOREF_PV(iReg);
1730
1731 /*
1732 * If the guest set the ICB bit of IRS register, HDA should process the verb in IC register,
1733 * write the response to IR register, and set the IRV (valid in case of success) bit of IRS register.
1734 */
1735 if ( (u32Value & HDA_IRS_ICB)
1736 && !(HDA_REG(pThis, IRS) & HDA_IRS_ICB))
1737 {
1738#ifdef IN_RING3
1739 uint32_t uCmd = HDA_REG(pThis, IC);
1740
1741 if (HDA_REG(pThis, CORBWP) != HDA_REG(pThis, CORBRP))
1742 {
1743 /*
1744 * 3.4.3: Defines behavior of immediate Command status register.
1745 */
1746 LogRel(("HDA: Guest attempted process immediate verb (%x) with active CORB\n", uCmd));
1747 return VINF_SUCCESS;
1748 }
1749
1750 HDA_REG(pThis, IRS) = HDA_IRS_ICB; /* busy */
1751
1752 uint64_t uResp;
1753 int rc2 = pThis->pCodec->pfnLookup(pThis->pCodec,
1754 HDA_CODEC_CMD(uCmd, 0 /* LUN */), &uResp);
1755 if (RT_FAILURE(rc2))
1756 LogFunc(("Codec lookup failed with rc2=%Rrc\n", rc2));
1757
1758 HDA_REG(pThis, IR) = (uint32_t)uResp; /** @todo r=andy Do we need a 64-bit response? */
1759 HDA_REG(pThis, IRS) = HDA_IRS_IRV; /* result is ready */
1760 /** @todo r=michaln We just set the IRS value, why are we clearing unset bits? */
1761 HDA_REG(pThis, IRS) &= ~HDA_IRS_ICB; /* busy is clear */
1762 return VINF_SUCCESS;
1763#else /* !IN_RING3 */
1764 return VINF_IOM_R3_MMIO_WRITE;
1765#endif /* !IN_RING3 */
1766 }
1767
1768 /*
1769 * Once the guest read the response, it should clear the IRV bit of the IRS register.
1770 */
1771 HDA_REG(pThis, IRS) &= ~(u32Value & HDA_IRS_IRV);
1772 return VINF_SUCCESS;
1773}
1774
1775static int hdaRegWriteRIRBWP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1776{
1777 RT_NOREF_PV(iReg);
1778
1779 if (u32Value & HDA_RIRBWP_RST)
1780 HDA_REG(pThis, RIRBWP) = 0;
1781
1782 /* The remaining bits are O, see 6.2.22. */
1783 return VINF_SUCCESS;
1784}
1785
1786static int hdaRegWriteBase(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1787{
1788 uint32_t iRegMem = g_aHdaRegMap[iReg].mem_idx;
1789 int rc = hdaRegWriteU32(pThis, iReg, u32Value);
1790 if (RT_FAILURE(rc))
1791 AssertRCReturn(rc, rc);
1792
1793 switch(iReg)
1794 {
1795 case HDA_REG_CORBLBASE:
1796 pThis->u64CORBBase &= UINT64_C(0xFFFFFFFF00000000);
1797 pThis->u64CORBBase |= pThis->au32Regs[iRegMem];
1798 break;
1799 case HDA_REG_CORBUBASE:
1800 pThis->u64CORBBase &= UINT64_C(0x00000000FFFFFFFF);
1801 pThis->u64CORBBase |= ((uint64_t)pThis->au32Regs[iRegMem] << 32);
1802 break;
1803 case HDA_REG_RIRBLBASE:
1804 pThis->u64RIRBBase &= UINT64_C(0xFFFFFFFF00000000);
1805 pThis->u64RIRBBase |= pThis->au32Regs[iRegMem];
1806 break;
1807 case HDA_REG_RIRBUBASE:
1808 pThis->u64RIRBBase &= UINT64_C(0x00000000FFFFFFFF);
1809 pThis->u64RIRBBase |= ((uint64_t)pThis->au32Regs[iRegMem] << 32);
1810 break;
1811 case HDA_REG_DPLBASE:
1812 {
1813 pThis->u64DPBase = pThis->au32Regs[iRegMem] & DPBASE_ADDR_MASK;
1814 Assert(pThis->u64DPBase % 128 == 0); /* Must be 128-byte aligned. */
1815
1816 /* Also make sure to handle the DMA position enable bit. */
1817 pThis->fDMAPosition = pThis->au32Regs[iRegMem] & RT_BIT_32(0);
1818 LogRel(("HDA: %s DMA position buffer\n", pThis->fDMAPosition ? "Enabled" : "Disabled"));
1819 break;
1820 }
1821 case HDA_REG_DPUBASE:
1822 pThis->u64DPBase = RT_MAKE_U64(RT_LO_U32(pThis->u64DPBase) & DPBASE_ADDR_MASK, pThis->au32Regs[iRegMem]);
1823 break;
1824 default:
1825 AssertMsgFailed(("Invalid index\n"));
1826 break;
1827 }
1828
1829 LogFunc(("CORB base:%llx RIRB base: %llx DP base: %llx\n",
1830 pThis->u64CORBBase, pThis->u64RIRBBase, pThis->u64DPBase));
1831 return rc;
1832}
1833
1834static int hdaRegWriteRIRBSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1835{
1836 RT_NOREF_PV(iReg);
1837
1838 uint8_t v = HDA_REG(pThis, RIRBSTS);
1839 HDA_REG(pThis, RIRBSTS) &= ~(v & u32Value);
1840
1841#ifndef DEBUG
1842 return hdaProcessInterrupt(pThis);
1843#else
1844 return hdaProcessInterrupt(pThis, __FUNCTION__);
1845#endif
1846}
1847
1848#ifdef IN_RING3
1849
1850#ifdef LOG_ENABLED
1851static void hdaBDLEDumpAll(PHDASTATE pThis, uint64_t u64BDLBase, uint16_t cBDLE)
1852{
1853 LogFlowFunc(("BDLEs @ 0x%x (%RU16):\n", u64BDLBase, cBDLE));
1854 if (!u64BDLBase)
1855 return;
1856
1857 uint32_t cbBDLE = 0;
1858 for (uint16_t i = 0; i < cBDLE; i++)
1859 {
1860 HDABDLEDESC bd;
1861 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), u64BDLBase + i * sizeof(HDABDLEDESC), &bd, sizeof(bd));
1862
1863 LogFunc(("\t#%03d BDLE(adr:0x%llx, size:%RU32, ioc:%RTbool)\n",
1864 i, bd.u64BufAdr, bd.u32BufSize, bd.fFlags & HDA_BDLE_FLAG_IOC));
1865
1866 cbBDLE += bd.u32BufSize;
1867 }
1868
1869 LogFlowFunc(("Total: %RU32 bytes\n", cbBDLE));
1870
1871 if (!pThis->u64DPBase) /* No DMA base given? Bail out. */
1872 return;
1873
1874 LogFlowFunc(("DMA counters:\n"));
1875
1876 for (int i = 0; i < cBDLE; i++)
1877 {
1878 uint32_t uDMACnt;
1879 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), (pThis->u64DPBase & DPBASE_ADDR_MASK) + (i * 2 * sizeof(uint32_t)),
1880 &uDMACnt, sizeof(uDMACnt));
1881
1882 LogFlowFunc(("\t#%03d DMA @ 0x%x\n", i , uDMACnt));
1883 }
1884}
1885#endif /* LOG_ENABLED */
1886
1887/**
1888 * Retrieves a corresponding sink for a given mixer control.
1889 * Returns NULL if no sink is found.
1890 *
1891 * @return PHDAMIXERSINK
1892 * @param pThis HDA state.
1893 * @param enmMixerCtl Mixer control to get the corresponding sink for.
1894 */
1895static PHDAMIXERSINK hdaMixerControlToSink(PHDASTATE pThis, PDMAUDIOMIXERCTL enmMixerCtl)
1896{
1897 PHDAMIXERSINK pSink;
1898
1899 switch (enmMixerCtl)
1900 {
1901 case PDMAUDIOMIXERCTL_VOLUME_MASTER:
1902 /* Fall through is intentional. */
1903 case PDMAUDIOMIXERCTL_FRONT:
1904 pSink = &pThis->SinkFront;
1905 break;
1906#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
1907 case PDMAUDIOMIXERCTL_CENTER_LFE:
1908 pSink = &pThis->SinkCenterLFE;
1909 break;
1910 case PDMAUDIOMIXERCTL_REAR:
1911 pSink = &pThis->SinkRear;
1912 break;
1913#endif
1914 case PDMAUDIOMIXERCTL_LINE_IN:
1915 pSink = &pThis->SinkLineIn;
1916 break;
1917#ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
1918 case PDMAUDIOMIXERCTL_MIC_IN:
1919 pSink = &pThis->SinkMicIn;
1920 break;
1921#endif
1922 default:
1923 pSink = NULL;
1924 AssertMsgFailed(("Unhandled mixer control\n"));
1925 break;
1926 }
1927
1928 return pSink;
1929}
1930
1931/**
1932 * Adds audio streams of all attached LUNs to a given HDA audio mixer sink.
1933 *
1934 * @returns IPRT status code.
1935 * @param pThis HDA state.
1936 * @param pSink HDA mixer sink to add audio streams to.
1937 * @param pCfg Audio stream configuration to use for the audio streams to add.
1938 */
1939static DECLCALLBACK(int) hdaMixerAddStream(PHDASTATE pThis, PHDAMIXERSINK pSink, PPDMAUDIOSTREAMCFG pCfg)
1940{
1941 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1942 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
1943 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
1944
1945 LogFunc(("Sink=%s, Stream=%s\n", pSink->pMixSink->pszName, pCfg->szName));
1946
1947 if (!DrvAudioHlpStreamCfgIsValid(pCfg))
1948 {
1949 LogRel(("HDA: Invalid stream configuration used for sink #%RU8: %RU8 bit, %RU8 channel(s) @ %RU32Hz\n",
1950 pSink->uSD, pCfg->Props.cBits, pCfg->Props.cChannels, pCfg->Props.uHz));
1951
1952 AssertFailed(); /* Should not happen. */
1953 return VERR_INVALID_PARAMETER;
1954 }
1955
1956 int rc = AudioMixerSinkSetFormat(pSink->pMixSink, &pCfg->Props);
1957 if (RT_FAILURE(rc))
1958 return rc;
1959
1960 PHDADRIVER pDrv;
1961 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
1962 {
1963 int rc2 = VINF_SUCCESS;
1964 PHDADRIVERSTREAM pDrvStream = NULL;
1965
1966 PPDMAUDIOSTREAMCFG pStreamCfg = DrvAudioHlpStreamCfgDup(pCfg);
1967 if (!pStreamCfg)
1968 {
1969 rc = VERR_NO_MEMORY;
1970 break;
1971 }
1972
1973 /* Include the driver's LUN in the stream name for easier identification. */
1974 RTStrPrintf(pStreamCfg->szName, RT_ELEMENTS(pStreamCfg->szName), "[LUN#%RU8] %s", pDrv->uLUN, pCfg->szName);
1975
1976 if (pStreamCfg->enmDir == PDMAUDIODIR_IN)
1977 {
1978 LogFunc(("enmRecSource=%d\n", pStreamCfg->DestSource.Source));
1979
1980 switch (pStreamCfg->DestSource.Source)
1981 {
1982 case PDMAUDIORECSOURCE_LINE:
1983 pDrvStream = &pDrv->LineIn;
1984 break;
1985#ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
1986 case PDMAUDIORECSOURCE_MIC:
1987 pDrvStream = &pDrv->MicIn;
1988 break;
1989#endif
1990 default:
1991 rc2 = VERR_NOT_SUPPORTED;
1992 break;
1993 }
1994 }
1995 else if (pStreamCfg->enmDir == PDMAUDIODIR_OUT)
1996 {
1997 LogFunc(("enmPlaybackDest=%d\n", pStreamCfg->DestSource.Dest));
1998
1999 switch (pStreamCfg->DestSource.Dest)
2000 {
2001 case PDMAUDIOPLAYBACKDEST_FRONT:
2002 pDrvStream = &pDrv->Front;
2003 break;
2004#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
2005 case PDMAUDIOPLAYBACKDEST_CENTER_LFE:
2006 pDrvStream = &pDrv->CenterLFE;
2007 break;
2008 case PDMAUDIOPLAYBACKDEST_REAR:
2009 pDrvStream = &pDrv->Rear;
2010 break;
2011#endif
2012 default:
2013 rc2 = VERR_NOT_SUPPORTED;
2014 break;
2015 }
2016 }
2017 else
2018 rc2 = VERR_NOT_SUPPORTED;
2019
2020 if (RT_SUCCESS(rc2))
2021 {
2022 AssertPtr(pDrvStream);
2023
2024 AudioMixerSinkRemoveStream(pSink->pMixSink, pDrvStream->pMixStrm);
2025
2026 AudioMixerStreamDestroy(pDrvStream->pMixStrm);
2027 pDrvStream->pMixStrm = NULL;
2028
2029 PAUDMIXSTREAM pMixStrm;
2030 rc2 = AudioMixerSinkCreateStream(pSink->pMixSink, pDrv->pConnector, pStreamCfg, 0 /* fFlags */, &pMixStrm);
2031 if (RT_SUCCESS(rc2))
2032 {
2033 rc2 = AudioMixerSinkAddStream(pSink->pMixSink, pMixStrm);
2034 LogFlowFunc(("LUN#%RU8: Added \"%s\" to sink, rc=%Rrc\n", pDrv->uLUN, pStreamCfg->szName , rc2));
2035 }
2036
2037 if (RT_SUCCESS(rc2))
2038 pDrvStream->pMixStrm = pMixStrm;
2039
2040 /* If creating a stream fails, be forgiving and continue -- don't pass rc2 to rc here. */
2041 }
2042
2043 if (pStreamCfg)
2044 {
2045 RTMemFree(pStreamCfg);
2046 pStreamCfg = NULL;
2047 }
2048 }
2049
2050 LogFlowFuncLeaveRC(rc);
2051 return rc;
2052}
2053
2054/**
2055 * Adds a new audio stream to a specific mixer control.
2056 * Depending on the mixer control the stream then gets assigned to one of the internal
2057 * mixer sinks, which in turn then handle the mixing of all connected streams to that sink.
2058 *
2059 * @return IPRT status code.
2060 * @param pThis HDA state.
2061 * @param enmMixerCtl Mixer control to assign new stream to.
2062 * @param pCfg Stream configuration for the new stream.
2063 */
2064static DECLCALLBACK(int) hdaMixerAddStream(PHDASTATE pThis, PDMAUDIOMIXERCTL enmMixerCtl, PPDMAUDIOSTREAMCFG pCfg)
2065{
2066 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
2067 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
2068
2069 int rc;
2070
2071 PHDAMIXERSINK pSink = hdaMixerControlToSink(pThis, enmMixerCtl);
2072 if (pSink)
2073 {
2074 rc = hdaMixerAddStream(pThis, pSink, pCfg);
2075
2076 AssertPtr(pSink->pMixSink);
2077 LogFlowFunc(("Sink=%s, enmMixerCtl=%d\n", pSink->pMixSink->pszName, enmMixerCtl));
2078 }
2079 else
2080 rc = VERR_NOT_FOUND;
2081
2082 LogFlowFuncLeaveRC(rc);
2083 return rc;
2084}
2085
2086/**
2087 * Removes a specified mixer control from the HDA's mixer.
2088 *
2089 * @return IPRT status code.
2090 * @param pThis HDA state.
2091 * @param enmMixerCtl Mixer control to remove.
2092 *
2093 * @remarks Can be called as a callback by the HDA codec.
2094 */
2095static DECLCALLBACK(int) hdaMixerRemoveStream(PHDASTATE pThis, PDMAUDIOMIXERCTL enmMixerCtl)
2096{
2097 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
2098
2099 int rc;
2100
2101 PHDAMIXERSINK pSink = hdaMixerControlToSink(pThis, enmMixerCtl);
2102 if (pSink)
2103 {
2104 PHDADRIVER pDrv;
2105 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
2106 {
2107 PAUDMIXSTREAM pMixStream = NULL;
2108 switch (enmMixerCtl)
2109 {
2110 /*
2111 * Input.
2112 */
2113 case PDMAUDIOMIXERCTL_LINE_IN:
2114 pMixStream = pDrv->LineIn.pMixStrm;
2115 pDrv->LineIn.pMixStrm = NULL;
2116 break;
2117#ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
2118 case PDMAUDIOMIXERCTL_MIC_IN:
2119 pMixStream = pDrv->MicIn.pMixStrm;
2120 pDrv->MicIn.pMixStrm = NULL;
2121 break;
2122#endif
2123 /*
2124 * Output.
2125 */
2126 case PDMAUDIOMIXERCTL_FRONT:
2127 pMixStream = pDrv->Front.pMixStrm;
2128 pDrv->Front.pMixStrm = NULL;
2129 break;
2130#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
2131 case PDMAUDIOMIXERCTL_CENTER_LFE:
2132 pMixStream = pDrv->CenterLFE.pMixStrm;
2133 pDrv->CenterLFE.pMixStrm = NULL;
2134 break;
2135 case PDMAUDIOMIXERCTL_REAR:
2136 pMixStream = pDrv->Rear.pMixStrm;
2137 pDrv->Rear.pMixStrm = NULL;
2138 break;
2139#endif
2140 default:
2141 AssertMsgFailed(("Mixer control %d not implemented\n", enmMixerCtl));
2142 break;
2143 }
2144
2145 if (pMixStream)
2146 {
2147 AudioMixerSinkRemoveStream(pSink->pMixSink, pMixStream);
2148 AudioMixerStreamDestroy(pMixStream);
2149
2150 pMixStream = NULL;
2151 }
2152 }
2153
2154 AudioMixerSinkRemoveAllStreams(pSink->pMixSink);
2155 rc = VINF_SUCCESS;
2156 }
2157 else
2158 rc = VERR_NOT_FOUND;
2159
2160 LogFlowFunc(("enmMixerCtl=%d, rc=%Rrc\n", enmMixerCtl, rc));
2161 return rc;
2162}
2163
2164/**
2165 * Sets a SDn stream number and channel to a particular mixer control.
2166 *
2167 * @returns IPRT status code.
2168 * @param pThis HDA State.
2169 * @param enmMixerCtl Mixer control to set SD stream number and channel for.
2170 * @param uSD SD stream number (number + 1) to set. Set to 0 for unassign.
2171 * @param uChannel Channel to set. Only valid if a valid SD stream number is specified.
2172 *
2173 * @remarks Can be called as a callback by the HDA codec.
2174 */
2175static DECLCALLBACK(int) hdaMixerSetStream(PHDASTATE pThis, PDMAUDIOMIXERCTL enmMixerCtl, uint8_t uSD, uint8_t uChannel)
2176{
2177 LogFlowFunc(("enmMixerCtl=%RU32, uSD=%RU8, uChannel=%RU8\n", enmMixerCtl, uSD, uChannel));
2178
2179 if (uSD == 0) /* Stream number 0 is reserved. */
2180 {
2181 LogFlowFunc(("Invalid SDn (%RU8) number for mixer control %d, ignoring\n", uSD, enmMixerCtl));
2182 return VINF_SUCCESS;
2183 }
2184 /* uChannel is optional. */
2185
2186 /* SDn0 starts as 1. */
2187 Assert(uSD);
2188 uSD--;
2189
2190 int rc;
2191
2192 PHDAMIXERSINK pSink = hdaMixerControlToSink(pThis, enmMixerCtl);
2193 if (pSink)
2194 {
2195 if ( (uSD < HDA_MAX_SDI)
2196 && AudioMixerSinkGetDir(pSink->pMixSink) == AUDMIXSINKDIR_OUTPUT)
2197 {
2198 uSD += HDA_MAX_SDI;
2199 }
2200
2201 LogFlowFunc(("%s: Setting to stream ID=%RU8, channel=%RU8, enmMixerCtl=%RU32\n",
2202 pSink->pMixSink->pszName, uSD, uChannel, enmMixerCtl));
2203
2204 Assert(uSD < HDA_MAX_STREAMS);
2205
2206 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, uSD);
2207 if (pStream)
2208 {
2209 hdaStreamLock(pStream);
2210
2211 pSink->uSD = uSD;
2212 pSink->uChannel = uChannel;
2213 pStream->pMixSink = pSink;
2214
2215 hdaStreamUnlock(pStream);
2216
2217 rc = VINF_SUCCESS;
2218 }
2219 else
2220 {
2221 LogRel(("HDA: Guest wanted to assign invalid stream ID=%RU8 (channel %RU8) to mixer control %RU32, skipping\n",
2222 uSD, uChannel, enmMixerCtl));
2223 rc = VERR_INVALID_PARAMETER;
2224 }
2225 }
2226 else
2227 rc = VERR_NOT_FOUND;
2228
2229 LogFlowFuncLeaveRC(rc);
2230 return rc;
2231}
2232
2233/**
2234 * Sets the volume of a specified mixer control.
2235 *
2236 * @return IPRT status code.
2237 * @param pThis HDA State.
2238 * @param enmMixerCtl Mixer control to set volume for.
2239 * @param pVol Pointer to volume data to set.
2240 *
2241 * @remarks Can be called as a callback by the HDA codec.
2242 */
2243static DECLCALLBACK(int) hdaMixerSetVolume(PHDASTATE pThis,
2244 PDMAUDIOMIXERCTL enmMixerCtl, PPDMAUDIOVOLUME pVol)
2245{
2246 int rc;
2247
2248 PHDAMIXERSINK pSink = hdaMixerControlToSink(pThis, enmMixerCtl);
2249 if ( pSink
2250 && pSink->pMixSink)
2251 {
2252 LogRel2(("HDA: Setting volume for mixer sink '%s' to %RU8/%RU8 (%s)\n",
2253 pSink->pMixSink->pszName, pVol->uLeft, pVol->uRight, pVol->fMuted ? "Muted" : "Unmuted"));
2254
2255 /* Set the volume.
2256 * We assume that the codec already converted it to the correct range. */
2257 rc = AudioMixerSinkSetVolume(pSink->pMixSink, pVol);
2258 }
2259 else
2260 rc = VERR_NOT_FOUND;
2261
2262 LogFlowFuncLeaveRC(rc);
2263 return rc;
2264}
2265
2266#ifndef VBOX_WITH_AUDIO_HDA_CALLBACKS
2267/**
2268 * Starts the internal audio device timer (if not started yet).
2269 *
2270 * @param pThis HDA state.
2271 */
2272static void hdaTimerMaybeStart(PHDASTATE pThis)
2273{
2274 LogFlowFuncEnter();
2275
2276 if (!pThis->pTimer)
2277 return;
2278
2279 pThis->cStreamsActive++;
2280
2281 /* Only start the timer at the first active stream. */
2282 if (pThis->cStreamsActive == 1)
2283 {
2284 LogRel2(("HDA: Starting transfers\n"));
2285
2286 /* Set timer flag. */
2287 ASMAtomicXchgBool(&pThis->fTimerActive, true);
2288
2289 /* Update current time timestamp. */
2290 pThis->tsTimerExpire = TMTimerGet(pThis->pTimer) + pThis->cTimerTicks;
2291
2292 /* Start transfers. */
2293 hdaTimerMain(pThis);
2294 }
2295}
2296
2297/**
2298 * Stops the internal audio device timer.
2299 *
2300 * @param pThis HDA state.
2301 */
2302static void hdaTimerStop(PHDASTATE pThis)
2303{
2304 LogFlowFuncEnter();
2305
2306 /* Set timer flag. */
2307 ASMAtomicXchgBool(&pThis->fTimerActive, false);
2308
2309 /*
2310 * Stop the timer, if any.
2311 */
2312 if ( pThis->pTimer
2313 && TMTimerIsActive(pThis->pTimer))
2314 {
2315 int rc2 = TMTimerStop(pThis->pTimer);
2316 AssertRC(rc2);
2317 }
2318}
2319
2320/**
2321 * Decreases the active HDA streams count by one and
2322 * then checks if the internal audio device timer can be
2323 * stopped.
2324 *
2325 * @param pThis HDA state.
2326 */
2327static void hdaTimerMaybeStop(PHDASTATE pThis)
2328{
2329 LogFlowFuncEnter();
2330
2331 if (!pThis->pTimer)
2332 return;
2333
2334 if (pThis->cStreamsActive) /* Disable can be called mupltiple times. */
2335 {
2336 pThis->cStreamsActive--;
2337
2338 if (pThis->cStreamsActive == 0)
2339 hdaTimerStop(pThis);
2340 }
2341}
2342
2343/**
2344 * Main routine for the device timer.
2345 *
2346 * @returns IPRT status code.
2347 * @param pThis HDA state.
2348 */
2349static void hdaTimerMain(PHDASTATE pThis)
2350{
2351 AssertPtrReturnVoid(pThis);
2352
2353 STAM_PROFILE_START(&pThis->StatTimer, a);
2354
2355 /* Flag indicating whether to kick the timer again for a
2356 * new data processing round. */
2357 bool fKickTimer = false;
2358
2359 hdaDoTransfers(pThis);
2360
2361 /* Do we need to kick the timer again? */
2362 if ( AudioMixerSinkIsActive(pThis->SinkFront.pMixSink)
2363#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
2364 || AudioMixerSinkIsActive(pThis->SinkCenterLFE.pMixSink)
2365 || AudioMixerSinkIsActive(pThis->SinkRear.pMixSink)
2366#endif
2367 || AudioMixerSinkIsActive(pThis->SinkLineIn.pMixSink)
2368#ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
2369 || AudioMixerSinkIsActive(pThis->SinkMicIn.pMixSink)
2370#endif
2371 )
2372 {
2373 fKickTimer = true;
2374 }
2375
2376 if ( ASMAtomicReadBool(&pThis->fTimerActive)
2377 || fKickTimer)
2378 {
2379 /* Kick the timer again. */
2380 pThis->tsTimerExpire += pThis->cTimerTicks;
2381 TMTimerSet(pThis->pTimer, pThis->tsTimerExpire);
2382 }
2383 else
2384 LogRel2(("HDA: Stopping transfers\n"));
2385
2386 STAM_PROFILE_STOP(&pThis->StatTimer, a);
2387}
2388
2389#ifdef HDA_USE_DMA_ACCESS_HANDLER
2390/**
2391 * HC access handler for the FIFO.
2392 *
2393 * @returns VINF_SUCCESS if the handler have carried out the operation.
2394 * @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
2395 * @param pVM VM Handle.
2396 * @param pVCpu The cross context CPU structure for the calling EMT.
2397 * @param GCPhys The physical address the guest is writing to.
2398 * @param pvPhys The HC mapping of that address.
2399 * @param pvBuf What the guest is reading/writing.
2400 * @param cbBuf How much it's reading/writing.
2401 * @param enmAccessType The access type.
2402 * @param enmOrigin Who is making the access.
2403 * @param pvUser User argument.
2404 */
2405static DECLCALLBACK(VBOXSTRICTRC) hdaDMAAccessHandler(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, void *pvPhys,
2406 void *pvBuf, size_t cbBuf,
2407 PGMACCESSTYPE enmAccessType, PGMACCESSORIGIN enmOrigin, void *pvUser)
2408{
2409 RT_NOREF(pVM, pVCpu, pvPhys, pvBuf, enmOrigin);
2410
2411 PHDADMAACCESSHANDLER pHandler = (PHDADMAACCESSHANDLER)pvUser;
2412 AssertPtr(pHandler);
2413
2414 PHDASTREAM pStream = pHandler->pStream;
2415 AssertPtr(pStream);
2416
2417 Assert(GCPhys >= pHandler->GCPhysFirst);
2418 Assert(GCPhys <= pHandler->GCPhysLast);
2419 Assert(enmAccessType == PGMACCESSTYPE_WRITE);
2420
2421 /* Not within BDLE range? Bail out. */
2422 if ( (GCPhys < pHandler->BDLEAddr)
2423 || (GCPhys + cbBuf > pHandler->BDLEAddr + pHandler->BDLESize))
2424 {
2425 return VINF_PGM_HANDLER_DO_DEFAULT;
2426 }
2427
2428 switch(enmAccessType)
2429 {
2430 case PGMACCESSTYPE_WRITE:
2431 {
2432# ifdef DEBUG
2433 PHDASTREAMDBGINFO pStreamDbg = &pStream->Dbg;
2434
2435 const uint64_t tsNowNs = RTTimeNanoTS();
2436 const uint32_t tsElapsedMs = (tsNowNs - pStreamDbg->tsWriteSlotBegin) / 1000 / 1000;
2437
2438 uint64_t cWritesHz = ASMAtomicReadU64(&pStreamDbg->cWritesHz);
2439 uint64_t cbWrittenHz = ASMAtomicReadU64(&pStreamDbg->cbWrittenHz);
2440
2441 if (tsElapsedMs >= (1000 / HDA_TIMER_HZ))
2442 {
2443 LogFunc(("[SD%RU8] %RU32ms elapsed, cbWritten=%RU64, cWritten=%RU64 -- %RU32 bytes on average per time slot (%zums)\n",
2444 pStream->u8SD, tsElapsedMs, cbWrittenHz, cWritesHz,
2445 ASMDivU64ByU32RetU32(cbWrittenHz, cWritesHz ? cWritesHz : 1), 1000 / HDA_TIMER_HZ));
2446
2447 pStreamDbg->tsWriteSlotBegin = tsNowNs;
2448
2449 cWritesHz = 0;
2450 cbWrittenHz = 0;
2451 }
2452
2453 cWritesHz += 1;
2454 cbWrittenHz += cbBuf;
2455
2456 ASMAtomicIncU64(&pStreamDbg->cWritesTotal);
2457 ASMAtomicAddU64(&pStreamDbg->cbWrittenTotal, cbBuf);
2458
2459 ASMAtomicWriteU64(&pStreamDbg->cWritesHz, cWritesHz);
2460 ASMAtomicWriteU64(&pStreamDbg->cbWrittenHz, cbWrittenHz);
2461
2462 LogFunc(("[SD%RU8] Writing %3zu @ 0x%x (off %zu)\n",
2463 pStream->u8SD, cbBuf, GCPhys, GCPhys - pHandler->BDLEAddr));
2464
2465 LogFunc(("[SD%RU8] cWrites=%RU64, cbWritten=%RU64 -> %RU32 bytes on average\n",
2466 pStream->u8SD, pStreamDbg->cWritesTotal, pStreamDbg->cbWrittenTotal,
2467 ASMDivU64ByU32RetU32(pStreamDbg->cbWrittenTotal, pStreamDbg->cWritesTotal)));
2468# endif
2469
2470# ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
2471 RTFILE fh;
2472 RTFileOpen(&fh, VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "hdaDMAAccessWrite.pcm",
2473 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
2474 RTFileWrite(fh, pvBuf, cbBuf, NULL);
2475 RTFileClose(fh);
2476# endif
2477
2478# ifdef HDA_USE_DMA_ACCESS_HANDLER_WRITING
2479 PRTCIRCBUF pCircBuf = pStream->State.pCircBuf;
2480 AssertPtr(pCircBuf);
2481
2482 uint8_t *pbBuf = (uint8_t *)pvBuf;
2483 while (cbBuf)
2484 {
2485 /* Make sure we only copy as much as the stream's FIFO can hold (SDFIFOS, 18.2.39). */
2486 void *pvChunk;
2487 size_t cbChunk;
2488 RTCircBufAcquireWriteBlock(pCircBuf, cbBuf, &pvChunk, &cbChunk);
2489
2490 if (cbChunk)
2491 {
2492 memcpy(pvChunk, pbBuf, cbChunk);
2493
2494 pbBuf += cbChunk;
2495 Assert(cbBuf >= cbChunk);
2496 cbBuf -= cbChunk;
2497 }
2498 else
2499 {
2500 //AssertMsg(RTCircBufFree(pCircBuf), ("No more space but still %zu bytes to write\n", cbBuf));
2501 break;
2502 }
2503
2504 LogFunc(("[SD%RU8] cbChunk=%zu\n", pStream->u8SD, cbChunk));
2505
2506 RTCircBufReleaseWriteBlock(pCircBuf, cbChunk);
2507 }
2508# endif /* HDA_USE_DMA_ACCESS_HANDLER_WRITING */
2509 break;
2510 }
2511
2512 default:
2513 AssertMsgFailed(("Access type not implemented\n"));
2514 break;
2515 }
2516
2517 return VINF_PGM_HANDLER_DO_DEFAULT;
2518}
2519#endif /* HDA_USE_DMA_ACCESS_HANDLER */
2520
2521/**
2522 * Soft reset of the device triggered via GCTL.
2523 *
2524 * @param pThis HDA state.
2525 *
2526 */
2527static void hdaGCTLReset(PHDASTATE pThis)
2528{
2529 LogFlowFuncEnter();
2530
2531# ifndef VBOX_WITH_AUDIO_HDA_CALLBACKS
2532 /*
2533 * Stop the timer, if any.
2534 */
2535 hdaTimerStop(pThis);
2536
2537 pThis->cStreamsActive = 0;
2538# endif
2539
2540 memset(pThis->au32Regs, 0, sizeof(pThis->au32Regs));
2541 /* See 6.2.1. */
2542 HDA_REG(pThis, GCAP) = HDA_MAKE_GCAP(HDA_MAX_SDO /* Ouput streams */,
2543 HDA_MAX_SDI /* Input streams */,
2544 0 /* Bidirectional output streams */,
2545 0 /* Serial data out signals */,
2546 1 /* 64-bit */);
2547 HDA_REG(pThis, VMIN) = 0x00; /* see 6.2.2 */
2548 HDA_REG(pThis, VMAJ) = 0x01; /* see 6.2.3 */
2549 /* Announce the full 60 words output payload. */
2550 HDA_REG(pThis, OUTPAY) = 0x003C; /* see 6.2.4 */
2551 /* Announce the full 29 words input payload. */
2552 HDA_REG(pThis, INPAY) = 0x001D; /* see 6.2.5 */
2553 HDA_REG(pThis, CORBSIZE) = 0x42; /* see 6.2.1 */
2554 HDA_REG(pThis, RIRBSIZE) = 0x42; /* see 6.2.1 */
2555
2556 /*
2557 * Stop any audio currently playing and/or recording.
2558 */
2559 if (pThis->SinkFront.pMixSink)
2560 AudioMixerSinkReset(pThis->SinkFront.pMixSink);
2561# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
2562 if (pThis->SinkMicIn.pMixSink)
2563 AudioMixerSinkReset(pThis->SinkMicIn.pMixSink);
2564# endif
2565 if (pThis->SinkLineIn.pMixSink)
2566 AudioMixerSinkReset(pThis->SinkLineIn.pMixSink);
2567# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
2568 if (pThis->SinkCenterLFE.pMixSink)
2569 AudioMixerSinkReset(pThis->SinkCenterLFE.pMixSink);
2570 if (pThis->SinkRear.pMixSink)
2571 AudioMixerSinkReset(pThis->SinkRear.pMixSink);
2572# endif
2573
2574 /*
2575 * Reset the codec.
2576 */
2577 if ( pThis->pCodec
2578 && pThis->pCodec->pfnReset)
2579 {
2580 pThis->pCodec->pfnReset(pThis->pCodec);
2581 }
2582
2583 /*
2584 * Set some sensible defaults for which HDA sinks
2585 * are connected to which stream number.
2586 *
2587 * We use SD0 for input and SD4 for output by default.
2588 * These stream numbers can be changed by the guest dynamically lateron.
2589 */
2590#ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
2591 hdaMixerSetStream(pThis, PDMAUDIOMIXERCTL_MIC_IN , 1 /* SD0 */, 0 /* Channel */);
2592#endif
2593 hdaMixerSetStream(pThis, PDMAUDIOMIXERCTL_LINE_IN , 1 /* SD0 */, 0 /* Channel */);
2594
2595 hdaMixerSetStream(pThis, PDMAUDIOMIXERCTL_FRONT , 5 /* SD4 */, 0 /* Channel */);
2596#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
2597 hdaMixerSetStream(pThis, PDMAUDIOMIXERCTL_CENTER_LFE, 5 /* SD4 */, 0 /* Channel */);
2598 hdaMixerSetStream(pThis, PDMAUDIOMIXERCTL_REAR , 5 /* SD4 */, 0 /* Channel */);
2599#endif
2600
2601 pThis->cbCorbBuf = 256 * sizeof(uint32_t); /** @todo Use a define here. */
2602
2603 if (pThis->pu32CorbBuf)
2604 RT_BZERO(pThis->pu32CorbBuf, pThis->cbCorbBuf);
2605 else
2606 pThis->pu32CorbBuf = (uint32_t *)RTMemAllocZ(pThis->cbCorbBuf);
2607
2608 pThis->cbRirbBuf = 256 * sizeof(uint64_t); /** @todo Use a define here. */
2609 if (pThis->pu64RirbBuf)
2610 RT_BZERO(pThis->pu64RirbBuf, pThis->cbRirbBuf);
2611 else
2612 pThis->pu64RirbBuf = (uint64_t *)RTMemAllocZ(pThis->cbRirbBuf);
2613
2614 for (uint8_t uSD = 0; uSD < HDA_MAX_STREAMS; ++uSD)
2615 {
2616 /* Remove the RUN bit from SDnCTL in case the stream was in a running state before. */
2617 HDA_STREAM_REG(pThis, CTL, uSD) &= ~HDA_SDCTL_RUN;
2618 hdaStreamReset(pThis, &pThis->aStreams[uSD], uSD);
2619 }
2620
2621 /* Clear stream tags <-> objects mapping table. */
2622 RT_ZERO(pThis->aTags);
2623
2624 /* Emulation of codec "wake up" (HDA spec 5.5.1 and 6.5). */
2625 HDA_REG(pThis, STATESTS) = 0x1;
2626
2627 LogFlowFuncLeave();
2628 LogRel(("HDA: Reset\n"));
2629}
2630
2631
2632/**
2633 * Timer callback which handles the audio data transfers on a periodic basis.
2634 *
2635 * @param pDevIns Device instance.
2636 * @param pTimer Timer which was used when calling this.
2637 * @param pvUser User argument as PHDASTATE.
2638 */
2639static DECLCALLBACK(void) hdaTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
2640{
2641 RT_NOREF(pDevIns, pTimer);
2642
2643 PHDASTATE pThis = (PHDASTATE)pvUser;
2644 Assert(pThis == PDMINS_2_DATA(pDevIns, PHDASTATE));
2645 AssertPtr(pThis);
2646
2647 hdaTimerMain(pThis);
2648}
2649
2650#else /* VBOX_WITH_AUDIO_HDA_CALLBACKS */
2651
2652static DECLCALLBACK(int) hdaCallbackInput(PDMAUDIOCBTYPE enmType, void *pvCtx, size_t cbCtx, void *pvUser, size_t cbUser)
2653{
2654 Assert(enmType == PDMAUDIOCALLBACKTYPE_INPUT);
2655 AssertPtrReturn(pvCtx, VERR_INVALID_POINTER);
2656 AssertReturn(cbCtx, VERR_INVALID_PARAMETER);
2657 AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
2658 AssertReturn(cbUser, VERR_INVALID_PARAMETER);
2659
2660 PHDACALLBACKCTX pCtx = (PHDACALLBACKCTX)pvCtx;
2661 AssertReturn(cbCtx == sizeof(HDACALLBACKCTX), VERR_INVALID_PARAMETER);
2662
2663 PPDMAUDIOCBDATA_DATA_INPUT pData = (PPDMAUDIOCBDATA_DATA_INPUT)pvUser;
2664 AssertReturn(cbUser == sizeof(PDMAUDIOCBDATA_DATA_INPUT), VERR_INVALID_PARAMETER);
2665
2666 return hdaStreamDoDMA(pCtx->pThis, PI_INDEX, UINT32_MAX, &pData->cbOutRead);
2667}
2668
2669static DECLCALLBACK(int) hdaCallbackOutput(PDMAUDIOCBTYPE enmType, void *pvCtx, size_t cbCtx, void *pvUser, size_t cbUser)
2670{
2671 Assert(enmType == PDMAUDIOCALLBACKTYPE_OUTPUT);
2672 AssertPtrReturn(pvCtx, VERR_INVALID_POINTER);
2673 AssertReturn(cbCtx, VERR_INVALID_PARAMETER);
2674 AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
2675 AssertReturn(cbUser, VERR_INVALID_PARAMETER);
2676
2677 PHDACALLBACKCTX pCtx = (PHDACALLBACKCTX)pvCtx;
2678 AssertReturn(cbCtx == sizeof(HDACALLBACKCTX), VERR_INVALID_PARAMETER);
2679
2680 PPDMAUDIOCBDATA_DATA_OUTPUT pData = (PPDMAUDIOCBDATA_DATA_OUTPUT)pvUser;
2681 AssertReturn(cbUser == sizeof(PDMAUDIOCBDATA_DATA_OUTPUT), VERR_INVALID_PARAMETER);
2682
2683 PHDASTATE pThis = pCtx->pThis;
2684
2685 int rc = hdaStreamDoDMA(pCtx->pThis, PO_INDEX, UINT32_MAX, &pData->cbOutWritten);
2686 if ( RT_SUCCESS(rc)
2687 && pData->cbOutWritten)
2688 {
2689 PHDADRIVER pDrv;
2690 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
2691 {
2692 uint32_t cFramesPlayed;
2693 int rc2 = pDrv->pConnector->pfnPlay(pDrv->pConnector, &cFramesPlayed);
2694 LogFlowFunc(("LUN#%RU8: cFramesPlayed=%RU32, rc=%Rrc\n", pDrv->uLUN, cFramesPlayed, rc2));
2695 }
2696 }
2697}
2698#endif /* VBOX_WITH_AUDIO_HDA_CALLBACKS */
2699
2700/**
2701 * Main routine to perform the actual audio data transfers from the HDA streams
2702 * to the backend(s) and vice versa.
2703 *
2704 * @param pThis HDA state.
2705 */
2706static void hdaDoTransfers(PHDASTATE pThis)
2707{
2708 PHDASTREAM pStreamLineIn = hdaGetStreamFromSink(pThis, &pThis->SinkLineIn);
2709#ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
2710 PHDASTREAM pStreamMicIn = hdaGetStreamFromSink(pThis, &pThis->SinkMicIn);
2711#endif
2712 PHDASTREAM pStreamFront = hdaGetStreamFromSink(pThis, &pThis->SinkFront);
2713#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
2714 /** @todo See note below. */
2715#endif
2716
2717 hdaStreamUpdate(pStreamFront, true /* fInTimer */);
2718#ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
2719 hdaStreamUpdate(pStreamMicIn, true /* fInTimer */);
2720#endif
2721 hdaStreamUpdate(pStreamLineIn, true /* fInTimer */);
2722}
2723
2724#ifdef DEBUG_andy
2725# define HDA_DEBUG_DMA
2726#endif
2727
2728#endif /* IN_RING3 */
2729
2730/* MMIO callbacks */
2731
2732/**
2733 * @callback_method_impl{FNIOMMMIOREAD, Looks up and calls the appropriate handler.}
2734 *
2735 * @note During implementation, we discovered so-called "forgotten" or "hole"
2736 * registers whose description is not listed in the RPM, datasheet, or
2737 * spec.
2738 */
2739PDMBOTHCBDECL(int) hdaMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
2740{
2741 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
2742 int rc;
2743 RT_NOREF_PV(pvUser);
2744
2745 /*
2746 * Look up and log.
2747 */
2748 uint32_t offReg = GCPhysAddr - pThis->MMIOBaseAddr;
2749 int idxRegDsc = hdaRegLookup(offReg); /* Register descriptor index. */
2750#ifdef LOG_ENABLED
2751 unsigned const cbLog = cb;
2752 uint32_t offRegLog = offReg;
2753#endif
2754
2755 Log3Func(("offReg=%#x cb=%#x\n", offReg, cb));
2756 Assert(cb == 4); Assert((offReg & 3) == 0);
2757
2758 if (!(HDA_REG(pThis, GCTL) & HDA_GCTL_CRST) && idxRegDsc != HDA_REG_GCTL)
2759 LogFunc(("Access to registers except GCTL is blocked while reset\n"));
2760
2761 if (idxRegDsc == -1)
2762 LogRel(("HDA: Invalid read access @0x%x (bytes=%u)\n", offReg, cb));
2763
2764 if (idxRegDsc != -1)
2765 {
2766 /* ASSUMES gapless DWORD at end of map. */
2767 if (g_aHdaRegMap[idxRegDsc].size == 4)
2768 {
2769 /*
2770 * Straight forward DWORD access.
2771 */
2772 rc = g_aHdaRegMap[idxRegDsc].pfnRead(pThis, idxRegDsc, (uint32_t *)pv);
2773 Log3Func(("\tRead %s => %x (%Rrc)\n", g_aHdaRegMap[idxRegDsc].abbrev, *(uint32_t *)pv, rc));
2774 }
2775 else
2776 {
2777 /*
2778 * Multi register read (unless there are trailing gaps).
2779 * ASSUMES that only DWORD reads have sideeffects.
2780 */
2781 uint32_t u32Value = 0;
2782 unsigned cbLeft = 4;
2783 do
2784 {
2785 uint32_t const cbReg = g_aHdaRegMap[idxRegDsc].size;
2786 uint32_t u32Tmp = 0;
2787
2788 rc = g_aHdaRegMap[idxRegDsc].pfnRead(pThis, idxRegDsc, &u32Tmp);
2789 Log3Func(("\tRead %s[%db] => %x (%Rrc)*\n", g_aHdaRegMap[idxRegDsc].abbrev, cbReg, u32Tmp, rc));
2790 if (rc != VINF_SUCCESS)
2791 break;
2792 u32Value |= (u32Tmp & g_afMasks[cbReg]) << ((4 - cbLeft) * 8);
2793
2794 cbLeft -= cbReg;
2795 offReg += cbReg;
2796 idxRegDsc++;
2797 } while (cbLeft > 0 && g_aHdaRegMap[idxRegDsc].offset == offReg);
2798
2799 if (rc == VINF_SUCCESS)
2800 *(uint32_t *)pv = u32Value;
2801 else
2802 Assert(!IOM_SUCCESS(rc));
2803 }
2804 }
2805 else
2806 {
2807 rc = VINF_IOM_MMIO_UNUSED_FF;
2808 Log3Func(("\tHole at %x is accessed for read\n", offReg));
2809 }
2810
2811 /*
2812 * Log the outcome.
2813 */
2814#ifdef LOG_ENABLED
2815 if (cbLog == 4)
2816 Log3Func(("\tReturning @%#05x -> %#010x %Rrc\n", offRegLog, *(uint32_t *)pv, rc));
2817 else if (cbLog == 2)
2818 Log3Func(("\tReturning @%#05x -> %#06x %Rrc\n", offRegLog, *(uint16_t *)pv, rc));
2819 else if (cbLog == 1)
2820 Log3Func(("\tReturning @%#05x -> %#04x %Rrc\n", offRegLog, *(uint8_t *)pv, rc));
2821#endif
2822 return rc;
2823}
2824
2825
2826DECLINLINE(int) hdaWriteReg(PHDASTATE pThis, int idxRegDsc, uint32_t u32Value, char const *pszLog)
2827{
2828 if (!(HDA_REG(pThis, GCTL) & HDA_GCTL_CRST) && idxRegDsc != HDA_REG_GCTL)
2829 {
2830 Log(("hdaWriteReg: Warning: Access to %s is blocked while controller is in reset mode\n", g_aHdaRegMap[idxRegDsc].abbrev));
2831 LogRel2(("HDA: Warning: Access to register %s is blocked while controller is in reset mode\n",
2832 g_aHdaRegMap[idxRegDsc].abbrev));
2833 return VINF_SUCCESS;
2834 }
2835
2836 /*
2837 * Handle RD (register description) flags.
2838 */
2839
2840 /* For SDI / SDO: Check if writes to those registers are allowed while SDCTL's RUN bit is set. */
2841 if (idxRegDsc >= HDA_NUM_GENERAL_REGS)
2842 {
2843 const uint32_t uSDCTL = HDA_STREAM_REG(pThis, CTL, HDA_SD_NUM_FROM_REG(pThis, CTL, idxRegDsc));
2844
2845 /*
2846 * Some OSes (like Win 10 AU) violate the spec by writing stuff to registers which are not supposed to be be touched
2847 * while SDCTL's RUN bit is set. So just ignore those values.
2848 */
2849
2850 /* Is the RUN bit currently set? */
2851 if ( RT_BOOL(uSDCTL & HDA_SDCTL_RUN)
2852 /* Are writes to the register denied if RUN bit is set? */
2853 && !(g_aHdaRegMap[idxRegDsc].fFlags & HDA_RD_FLAG_SD_WRITE_RUN))
2854 {
2855 Log(("hdaWriteReg: Warning: Access to %s is blocked! %R[sdctl]\n", g_aHdaRegMap[idxRegDsc].abbrev, uSDCTL));
2856 LogRel2(("HDA: Warning: Access to register %s is blocked while the stream's RUN bit is set\n",
2857 g_aHdaRegMap[idxRegDsc].abbrev));
2858 return VINF_SUCCESS;
2859 }
2860 }
2861
2862#ifdef LOG_ENABLED
2863 uint32_t const idxRegMem = g_aHdaRegMap[idxRegDsc].mem_idx;
2864 uint32_t const u32OldValue = pThis->au32Regs[idxRegMem];
2865#endif
2866 int rc = g_aHdaRegMap[idxRegDsc].pfnWrite(pThis, idxRegDsc, u32Value);
2867 Log3Func(("Written value %#x to %s[%d byte]; %x => %x%s\n", u32Value, g_aHdaRegMap[idxRegDsc].abbrev,
2868 g_aHdaRegMap[idxRegDsc].size, u32OldValue, pThis->au32Regs[idxRegMem], pszLog));
2869 RT_NOREF(pszLog);
2870 return rc;
2871}
2872
2873
2874/**
2875 * @callback_method_impl{FNIOMMMIOWRITE, Looks up and calls the appropriate handler.}
2876 */
2877PDMBOTHCBDECL(int) hdaMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
2878{
2879 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
2880 int rc;
2881 RT_NOREF_PV(pvUser);
2882
2883 /*
2884 * The behavior of accesses that aren't aligned on natural boundraries is
2885 * undefined. Just reject them outright.
2886 */
2887 /** @todo IOM could check this, it could also split the 8 byte accesses for us. */
2888 Assert(cb == 1 || cb == 2 || cb == 4 || cb == 8);
2889 if (GCPhysAddr & (cb - 1))
2890 return PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "misaligned write access: GCPhysAddr=%RGp cb=%u\n", GCPhysAddr, cb);
2891
2892 /*
2893 * Look up and log the access.
2894 */
2895 uint32_t offReg = GCPhysAddr - pThis->MMIOBaseAddr;
2896 int idxRegDsc = hdaRegLookup(offReg);
2897 uint32_t idxRegMem = idxRegDsc != -1 ? g_aHdaRegMap[idxRegDsc].mem_idx : UINT32_MAX;
2898 uint64_t u64Value;
2899 if (cb == 4) u64Value = *(uint32_t const *)pv;
2900 else if (cb == 2) u64Value = *(uint16_t const *)pv;
2901 else if (cb == 1) u64Value = *(uint8_t const *)pv;
2902 else if (cb == 8) u64Value = *(uint64_t const *)pv;
2903 else
2904 {
2905 u64Value = 0; /* shut up gcc. */
2906 AssertReleaseMsgFailed(("%u\n", cb));
2907 }
2908
2909#ifdef LOG_ENABLED
2910 uint32_t const u32LogOldValue = idxRegDsc >= 0 ? pThis->au32Regs[idxRegMem] : UINT32_MAX;
2911 if (idxRegDsc == -1)
2912 Log3Func(("@%#05x u32=%#010x cb=%d\n", offReg, *(uint32_t const *)pv, cb));
2913 else if (cb == 4)
2914 Log3Func(("@%#05x u32=%#010x %s\n", offReg, *(uint32_t *)pv, g_aHdaRegMap[idxRegDsc].abbrev));
2915 else if (cb == 2)
2916 Log3Func(("@%#05x u16=%#06x (%#010x) %s\n", offReg, *(uint16_t *)pv, *(uint32_t *)pv, g_aHdaRegMap[idxRegDsc].abbrev));
2917 else if (cb == 1)
2918 Log3Func(("@%#05x u8=%#04x (%#010x) %s\n", offReg, *(uint8_t *)pv, *(uint32_t *)pv, g_aHdaRegMap[idxRegDsc].abbrev));
2919
2920 if (idxRegDsc >= 0 && g_aHdaRegMap[idxRegDsc].size != cb)
2921 Log3Func(("\tsize=%RU32 != cb=%u!!\n", g_aHdaRegMap[idxRegDsc].size, cb));
2922#endif
2923
2924 /*
2925 * Try for a direct hit first.
2926 */
2927 if (idxRegDsc != -1 && g_aHdaRegMap[idxRegDsc].size == cb)
2928 {
2929 rc = hdaWriteReg(pThis, idxRegDsc, u64Value, "");
2930 Log3Func(("\t%#x -> %#x\n", u32LogOldValue, idxRegMem != UINT32_MAX ? pThis->au32Regs[idxRegMem] : UINT32_MAX));
2931 }
2932 /*
2933 * Partial or multiple register access, loop thru the requested memory.
2934 */
2935 else
2936 {
2937 /*
2938 * If it's an access beyond the start of the register, shift the input
2939 * value and fill in missing bits. Natural alignment rules means we
2940 * will only see 1 or 2 byte accesses of this kind, so no risk of
2941 * shifting out input values.
2942 */
2943 if (idxRegDsc == -1 && (idxRegDsc = hdaRegLookupWithin(offReg)) != -1)
2944 {
2945 uint32_t const cbBefore = offReg - g_aHdaRegMap[idxRegDsc].offset; Assert(cbBefore > 0 && cbBefore < 4);
2946 offReg -= cbBefore;
2947 idxRegMem = g_aHdaRegMap[idxRegDsc].mem_idx;
2948 u64Value <<= cbBefore * 8;
2949 u64Value |= pThis->au32Regs[idxRegMem] & g_afMasks[cbBefore];
2950 Log3Func(("\tWithin register, supplied %u leading bits: %#llx -> %#llx ...\n",
2951 cbBefore * 8, ~g_afMasks[cbBefore] & u64Value, u64Value));
2952 }
2953
2954 /* Loop thru the write area, it may cover multiple registers. */
2955 rc = VINF_SUCCESS;
2956 for (;;)
2957 {
2958 uint32_t cbReg;
2959 if (idxRegDsc != -1)
2960 {
2961 idxRegMem = g_aHdaRegMap[idxRegDsc].mem_idx;
2962 cbReg = g_aHdaRegMap[idxRegDsc].size;
2963 if (cb < cbReg)
2964 {
2965 u64Value |= pThis->au32Regs[idxRegMem] & g_afMasks[cbReg] & ~g_afMasks[cb];
2966 Log3Func(("\tSupplying missing bits (%#x): %#llx -> %#llx ...\n",
2967 g_afMasks[cbReg] & ~g_afMasks[cb], u64Value & g_afMasks[cb], u64Value));
2968 }
2969#ifdef LOG_ENABLED
2970 uint32_t uLogOldVal = pThis->au32Regs[idxRegMem];
2971#endif
2972 rc = hdaWriteReg(pThis, idxRegDsc, u64Value, "*");
2973 Log3Func(("\t%#x -> %#x\n", uLogOldVal, pThis->au32Regs[idxRegMem]));
2974 }
2975 else
2976 {
2977 LogRel(("HDA: Invalid write access @0x%x\n", offReg));
2978 cbReg = 1;
2979 }
2980 if (rc != VINF_SUCCESS)
2981 break;
2982 if (cbReg >= cb)
2983 break;
2984
2985 /* Advance. */
2986 offReg += cbReg;
2987 cb -= cbReg;
2988 u64Value >>= cbReg * 8;
2989 if (idxRegDsc == -1)
2990 idxRegDsc = hdaRegLookup(offReg);
2991 else
2992 {
2993 idxRegDsc++;
2994 if ( (unsigned)idxRegDsc >= RT_ELEMENTS(g_aHdaRegMap)
2995 || g_aHdaRegMap[idxRegDsc].offset != offReg)
2996 {
2997 idxRegDsc = -1;
2998 }
2999 }
3000 }
3001 }
3002
3003 return rc;
3004}
3005
3006
3007/* PCI callback. */
3008
3009#ifdef IN_RING3
3010/**
3011 * @callback_method_impl{FNPCIIOREGIONMAP}
3012 */
3013static DECLCALLBACK(int) hdaPciIoRegionMap(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t iRegion,
3014 RTGCPHYS GCPhysAddress, RTGCPHYS cb, PCIADDRESSSPACE enmType)
3015{
3016 RT_NOREF(iRegion, enmType);
3017 PHDASTATE pThis = RT_FROM_MEMBER(pPciDev, HDASTATE, PciDev);
3018
3019 /*
3020 * 18.2 of the ICH6 datasheet defines the valid access widths as byte, word, and double word.
3021 *
3022 * Let IOM talk DWORDs when reading, saves a lot of complications. On
3023 * writing though, we have to do it all ourselves because of sideeffects.
3024 */
3025 Assert(enmType == PCI_ADDRESS_SPACE_MEM);
3026 int rc = PDMDevHlpMMIORegister(pDevIns, GCPhysAddress, cb, NULL /*pvUser*/,
3027 IOMMMIO_FLAGS_READ_DWORD
3028 | IOMMMIO_FLAGS_WRITE_PASSTHRU,
3029 hdaMMIOWrite, hdaMMIORead, "HDA");
3030
3031 if (RT_FAILURE(rc))
3032 return rc;
3033
3034 if (pThis->fR0Enabled)
3035 {
3036 rc = PDMDevHlpMMIORegisterR0(pDevIns, GCPhysAddress, cb, NIL_RTR0PTR /*pvUser*/,
3037 "hdaMMIOWrite", "hdaMMIORead");
3038 if (RT_FAILURE(rc))
3039 return rc;
3040 }
3041
3042 if (pThis->fRCEnabled)
3043 {
3044 rc = PDMDevHlpMMIORegisterRC(pDevIns, GCPhysAddress, cb, NIL_RTRCPTR /*pvUser*/,
3045 "hdaMMIOWrite", "hdaMMIORead");
3046 if (RT_FAILURE(rc))
3047 return rc;
3048 }
3049
3050 pThis->MMIOBaseAddr = GCPhysAddress;
3051 return VINF_SUCCESS;
3052}
3053
3054
3055/* Saved state callbacks. */
3056
3057static int hdaSaveStream(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, PHDASTREAM pStrm)
3058{
3059 RT_NOREF(pDevIns);
3060#ifdef VBOX_STRICT
3061 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
3062#endif
3063
3064 Log2Func(("[SD%RU8]\n", pStrm->u8SD));
3065
3066 /* Save stream ID. */
3067 int rc = SSMR3PutU8(pSSM, pStrm->u8SD);
3068 AssertRCReturn(rc, rc);
3069 Assert(pStrm->u8SD < HDA_MAX_STREAMS);
3070
3071 rc = SSMR3PutStructEx(pSSM, &pStrm->State, sizeof(HDASTREAMSTATE), 0 /*fFlags*/, g_aSSMStreamStateFields7, NULL);
3072 AssertRCReturn(rc, rc);
3073
3074#ifdef VBOX_STRICT /* Sanity checks. */
3075 uint64_t u64BaseDMA = RT_MAKE_U64(HDA_STREAM_REG(pThis, BDPL, pStrm->u8SD),
3076 HDA_STREAM_REG(pThis, BDPU, pStrm->u8SD));
3077 uint16_t u16LVI = HDA_STREAM_REG(pThis, LVI, pStrm->u8SD);
3078 uint32_t u32CBL = HDA_STREAM_REG(pThis, CBL, pStrm->u8SD);
3079
3080 Assert(u64BaseDMA == pStrm->u64BDLBase);
3081 Assert(u16LVI == pStrm->u16LVI);
3082 Assert(u32CBL == pStrm->u32CBL);
3083#endif
3084
3085 rc = SSMR3PutStructEx(pSSM, &pStrm->State.BDLE.Desc, sizeof(HDABDLEDESC),
3086 0 /*fFlags*/, g_aSSMBDLEDescFields7, NULL);
3087 AssertRCReturn(rc, rc);
3088
3089 rc = SSMR3PutStructEx(pSSM, &pStrm->State.BDLE.State, sizeof(HDABDLESTATE),
3090 0 /*fFlags*/, g_aSSMBDLEStateFields7, NULL);
3091 AssertRCReturn(rc, rc);
3092
3093 rc = SSMR3PutStructEx(pSSM, &pStrm->State.Period, sizeof(HDASTREAMPERIOD),
3094 0 /* fFlags */, g_aSSMStreamPeriodFields7, NULL);
3095 AssertRCReturn(rc, rc);
3096
3097#ifdef VBOX_STRICT /* Sanity checks. */
3098 PHDABDLE pBDLE = &pStrm->State.BDLE;
3099 if (u64BaseDMA)
3100 {
3101 Assert(pStrm->State.uCurBDLE <= u16LVI + 1);
3102
3103 HDABDLE curBDLE;
3104 rc = hdaBDLEFetch(pThis, &curBDLE, u64BaseDMA, pStrm->State.uCurBDLE);
3105 AssertRC(rc);
3106
3107 Assert(curBDLE.Desc.u32BufSize == pBDLE->Desc.u32BufSize);
3108 Assert(curBDLE.Desc.u64BufAdr == pBDLE->Desc.u64BufAdr);
3109 Assert(curBDLE.Desc.fFlags == pBDLE->Desc.fFlags);
3110 }
3111 else
3112 {
3113 Assert(pBDLE->Desc.u64BufAdr == 0);
3114 Assert(pBDLE->Desc.u32BufSize == 0);
3115 }
3116#endif
3117
3118 uint32_t cbCircBufSize = 0;
3119 uint32_t cbCircBufUsed = 0;
3120
3121 if (pStrm->State.pCircBuf)
3122 {
3123 cbCircBufSize = (uint32_t)RTCircBufSize(pStrm->State.pCircBuf);
3124 cbCircBufUsed = (uint32_t)RTCircBufUsed(pStrm->State.pCircBuf);
3125 }
3126
3127 rc = SSMR3PutU32(pSSM, cbCircBufSize);
3128 AssertRCReturn(rc, rc);
3129
3130 rc = SSMR3PutU32(pSSM, cbCircBufUsed);
3131 AssertRCReturn(rc, rc);
3132
3133 if (cbCircBufUsed)
3134 {
3135 /*
3136 * We now need to get the circular buffer's data without actually modifying
3137 * the internal read / used offsets -- otherwise we would end up with broken audio
3138 * data after saving the state.
3139 *
3140 * So get the current read offset and serialize the buffer data manually based on that.
3141 */
3142 size_t cbCircBufOffRead = RTCircBufOffsetRead(pStrm->State.pCircBuf);
3143
3144 void *pvBuf;
3145 size_t cbBuf;
3146 RTCircBufAcquireReadBlock(pStrm->State.pCircBuf, cbCircBufUsed, &pvBuf, &cbBuf);
3147
3148 if (cbBuf)
3149 {
3150 size_t cbToRead = cbCircBufUsed;
3151 size_t cbEnd = 0;
3152
3153 if (cbCircBufUsed > cbCircBufOffRead)
3154 cbEnd = cbCircBufUsed - cbCircBufOffRead;
3155
3156 if (cbEnd) /* Save end of buffer first. */
3157 {
3158 rc = SSMR3PutMem(pSSM, (uint8_t *)pvBuf + cbCircBufSize - cbEnd /* End of buffer */, cbEnd);
3159 AssertRCReturn(rc, rc);
3160
3161 Assert(cbToRead >= cbEnd);
3162 cbToRead -= cbEnd;
3163 }
3164
3165 if (cbToRead) /* Save remaining stuff at start of buffer (if any). */
3166 {
3167 rc = SSMR3PutMem(pSSM, (uint8_t *)pvBuf - cbCircBufUsed /* Start of buffer */, cbToRead);
3168 AssertRCReturn(rc, rc);
3169 }
3170 }
3171
3172 RTCircBufReleaseReadBlock(pStrm->State.pCircBuf, 0 /* Don't advance read pointer -- see comment above */);
3173 }
3174
3175 Log2Func(("[SD%RU8] LPIB=%RU32, CBL=%RU32, LVI=%RU32\n",
3176 pStrm->u8SD,
3177 HDA_STREAM_REG(pThis, LPIB, pStrm->u8SD), HDA_STREAM_REG(pThis, CBL, pStrm->u8SD), HDA_STREAM_REG(pThis, LVI, pStrm->u8SD)));
3178
3179#ifdef LOG_ENABLED
3180 hdaBDLEDumpAll(pThis, pStrm->u64BDLBase, pStrm->u16LVI + 1);
3181#endif
3182
3183 return rc;
3184}
3185
3186/**
3187 * @callback_method_impl{FNSSMDEVSAVEEXEC}
3188 */
3189static DECLCALLBACK(int) hdaSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
3190{
3191 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
3192
3193 /* Save Codec nodes states. */
3194 hdaCodecSaveState(pThis->pCodec, pSSM);
3195
3196 /* Save MMIO registers. */
3197 SSMR3PutU32(pSSM, RT_ELEMENTS(pThis->au32Regs));
3198 SSMR3PutMem(pSSM, pThis->au32Regs, sizeof(pThis->au32Regs));
3199
3200 /* Save controller-specifc internals. */
3201 SSMR3PutU64(pSSM, pThis->u64WalClk);
3202 SSMR3PutU8(pSSM, pThis->u8IRQL);
3203
3204 /* Save number of streams. */
3205 SSMR3PutU32(pSSM, HDA_MAX_STREAMS);
3206
3207 /* Save stream states. */
3208 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
3209 {
3210 int rc = hdaSaveStream(pDevIns, pSSM, &pThis->aStreams[i]);
3211 AssertRCReturn(rc, rc);
3212 }
3213
3214 return VINF_SUCCESS;
3215}
3216
3217/**
3218 * Does required post processing when loading a saved state.
3219 *
3220 * @param pThis Pointer to HDA state.
3221 */
3222static int hdaLoadExecPost(PHDASTATE pThis)
3223{
3224 int rc = VINF_SUCCESS;
3225
3226 bool fStartTimer = false; /* Whether to resume the device timer. */
3227
3228 /*
3229 * Enable all previously active streams.
3230 */
3231 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
3232 {
3233 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, i);
3234 if (pStream)
3235 {
3236 int rc2;
3237
3238 bool fActive = RT_BOOL(HDA_STREAM_REG(pThis, CTL, i) & HDA_SDCTL_RUN);
3239 if (fActive)
3240 {
3241#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
3242 /* Make sure to also create the async I/O thread before actually enabling the stream. */
3243 rc2 = hdaStreamAsyncIOCreate(pStream);
3244 AssertRC(rc2);
3245
3246 /* ... and enabling it. */
3247 hdaStreamAsyncIOEnable(pStream, true /* fEnable */);
3248#endif
3249 /* (Re-)initialize the stream with current values. */
3250 rc2 = hdaStreamInit(pStream, pStream->u8SD);
3251 AssertRC(rc2);
3252
3253 /* Resume the stream's period. */
3254 hdaStreamPeriodResume(&pStream->State.Period);
3255
3256 /* (Re-)enable the stream. */
3257 rc2 = hdaStreamEnable(pStream, true /* fEnable */);
3258 AssertRC(rc2);
3259
3260 /* Add the stream to the device setup. */
3261 rc2 = hdaAddStream(pThis, &pStream->State.strmCfg);
3262 AssertRC(rc2);
3263
3264#ifdef HDA_USE_DMA_ACCESS_HANDLER
3265 /* (Re-)install the DMA handler. */
3266 hdaStreamRegisterDMAHandlers(pThis, pStream);
3267#endif
3268 fStartTimer = true;
3269 }
3270 }
3271 }
3272
3273#ifndef VBOX_WITH_AUDIO_CALLBACKS
3274 /* Start the timer if one of the above streams were active during taking the saved state. */
3275 if (fStartTimer)
3276 hdaTimerMaybeStart(pThis);
3277#endif
3278
3279 LogFlowFuncLeaveRC(rc);
3280 return rc;
3281}
3282
3283
3284/**
3285 * Handles loading of all saved state versions older than the current one.
3286 *
3287 * @param pThis Pointer to HDA state.
3288 * @param pSSM Pointer to SSM handle.
3289 * @param uVersion Saved state version to load.
3290 * @param uPass Loading stage to handle.
3291 */
3292static int hdaLoadExecLegacy(PHDASTATE pThis, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
3293{
3294 RT_NOREF(uPass);
3295
3296 int rc = VINF_SUCCESS;
3297
3298 /*
3299 * Load MMIO registers.
3300 */
3301 uint32_t cRegs;
3302 switch (uVersion)
3303 {
3304 case HDA_SSM_VERSION_1:
3305 /* Starting with r71199, we would save 112 instead of 113
3306 registers due to some code cleanups. This only affected trunk
3307 builds in the 4.1 development period. */
3308 cRegs = 113;
3309 if (SSMR3HandleRevision(pSSM) >= 71199)
3310 {
3311 uint32_t uVer = SSMR3HandleVersion(pSSM);
3312 if ( VBOX_FULL_VERSION_GET_MAJOR(uVer) == 4
3313 && VBOX_FULL_VERSION_GET_MINOR(uVer) == 0
3314 && VBOX_FULL_VERSION_GET_BUILD(uVer) >= 51)
3315 cRegs = 112;
3316 }
3317 break;
3318
3319 case HDA_SSM_VERSION_2:
3320 case HDA_SSM_VERSION_3:
3321 cRegs = 112;
3322 AssertCompile(RT_ELEMENTS(pThis->au32Regs) >= 112);
3323 break;
3324
3325 /* Since version 4 we store the register count to stay flexible. */
3326 case HDA_SSM_VERSION_4:
3327 case HDA_SSM_VERSION_5:
3328 case HDA_SSM_VERSION_6:
3329 rc = SSMR3GetU32(pSSM, &cRegs); AssertRCReturn(rc, rc);
3330 if (cRegs != RT_ELEMENTS(pThis->au32Regs))
3331 LogRel(("HDA: SSM version cRegs is %RU32, expected %RU32\n", cRegs, RT_ELEMENTS(pThis->au32Regs)));
3332 break;
3333
3334 default:
3335 LogRel(("HDA: Unsupported / too new saved state version (%RU32)\n", uVersion));
3336 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
3337 }
3338
3339 if (cRegs >= RT_ELEMENTS(pThis->au32Regs))
3340 {
3341 SSMR3GetMem(pSSM, pThis->au32Regs, sizeof(pThis->au32Regs));
3342 SSMR3Skip(pSSM, sizeof(uint32_t) * (cRegs - RT_ELEMENTS(pThis->au32Regs)));
3343 }
3344 else
3345 SSMR3GetMem(pSSM, pThis->au32Regs, sizeof(uint32_t) * cRegs);
3346
3347 /* Make sure to update the base addresses first before initializing any streams down below. */
3348 pThis->u64CORBBase = RT_MAKE_U64(HDA_REG(pThis, CORBLBASE), HDA_REG(pThis, CORBUBASE));
3349 pThis->u64RIRBBase = RT_MAKE_U64(HDA_REG(pThis, RIRBLBASE), HDA_REG(pThis, RIRBUBASE));
3350 pThis->u64DPBase = RT_MAKE_U64(HDA_REG(pThis, DPLBASE) & DPBASE_ADDR_MASK, HDA_REG(pThis, DPUBASE));
3351
3352 /* Also make sure to update the DMA position bit if this was enabled when saving the state. */
3353 pThis->fDMAPosition = RT_BOOL(HDA_REG(pThis, DPLBASE) & RT_BIT_32(0));
3354
3355 /*
3356 * Note: Saved states < v5 store LVI (u32BdleMaxCvi) for
3357 * *every* BDLE state, whereas it only needs to be stored
3358 * *once* for every stream. Most of the BDLE state we can
3359 * get out of the registers anyway, so just ignore those values.
3360 *
3361 * Also, only the current BDLE was saved, regardless whether
3362 * there were more than one (and there are at least two entries,
3363 * according to the spec).
3364 */
3365#define HDA_SSM_LOAD_BDLE_STATE_PRE_V5(v, x) \
3366 { \
3367 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Begin marker */ \
3368 AssertRCReturn(rc, rc); \
3369 rc = SSMR3GetU64(pSSM, &x.Desc.u64BufAdr); /* u64BdleCviAddr */ \
3370 AssertRCReturn(rc, rc); \
3371 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* u32BdleMaxCvi */ \
3372 AssertRCReturn(rc, rc); \
3373 rc = SSMR3GetU32(pSSM, &x.State.u32BDLIndex); /* u32BdleCvi */ \
3374 AssertRCReturn(rc, rc); \
3375 rc = SSMR3GetU32(pSSM, &x.Desc.u32BufSize); /* u32BdleCviLen */ \
3376 AssertRCReturn(rc, rc); \
3377 rc = SSMR3GetU32(pSSM, &x.State.u32BufOff); /* u32BdleCviPos */ \
3378 AssertRCReturn(rc, rc); \
3379 bool fIOC; \
3380 rc = SSMR3GetBool(pSSM, &fIOC); /* fBdleCviIoc */ \
3381 AssertRCReturn(rc, rc); \
3382 x.Desc.fFlags = fIOC ? HDA_BDLE_FLAG_IOC : 0; \
3383 rc = SSMR3GetU32(pSSM, &x.State.cbBelowFIFOW); /* cbUnderFifoW */ \
3384 AssertRCReturn(rc, rc); \
3385 rc = SSMR3Skip(pSSM, sizeof(uint8_t) * 256); /* FIFO */ \
3386 AssertRCReturn(rc, rc); \
3387 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* End marker */ \
3388 AssertRCReturn(rc, rc); \
3389 }
3390
3391 /*
3392 * Load BDLEs (Buffer Descriptor List Entries) and DMA counters.
3393 */
3394 switch (uVersion)
3395 {
3396 case HDA_SSM_VERSION_1:
3397 case HDA_SSM_VERSION_2:
3398 case HDA_SSM_VERSION_3:
3399 case HDA_SSM_VERSION_4:
3400 {
3401 /* Only load the internal states.
3402 * The rest will be initialized from the saved registers later. */
3403
3404 /* Note 1: Only the *current* BDLE for a stream was saved! */
3405 /* Note 2: The stream's saving order is/was fixed, so don't touch! */
3406
3407 /* Output */
3408 PHDASTREAM pStream = &pThis->aStreams[4];
3409 rc = hdaStreamInit(pStream, 4 /* Stream descriptor, hardcoded */);
3410 if (RT_FAILURE(rc))
3411 break;
3412 HDA_SSM_LOAD_BDLE_STATE_PRE_V5(uVersion, pStream->State.BDLE);
3413 pStream->State.uCurBDLE = pStream->State.BDLE.State.u32BDLIndex;
3414
3415 /* Microphone-In */
3416 pStream = &pThis->aStreams[2];
3417 rc = hdaStreamInit(pStream, 2 /* Stream descriptor, hardcoded */);
3418 if (RT_FAILURE(rc))
3419 break;
3420 HDA_SSM_LOAD_BDLE_STATE_PRE_V5(uVersion, pStream->State.BDLE);
3421 pStream->State.uCurBDLE = pStream->State.BDLE.State.u32BDLIndex;
3422
3423 /* Line-In */
3424 pStream = &pThis->aStreams[0];
3425 rc = hdaStreamInit(pStream, 0 /* Stream descriptor, hardcoded */);
3426 if (RT_FAILURE(rc))
3427 break;
3428 HDA_SSM_LOAD_BDLE_STATE_PRE_V5(uVersion, pStream->State.BDLE);
3429 pStream->State.uCurBDLE = pStream->State.BDLE.State.u32BDLIndex;
3430 break;
3431 }
3432
3433#undef HDA_SSM_LOAD_BDLE_STATE_PRE_V5
3434
3435 default: /* Since v5 we support flexible stream and BDLE counts. */
3436 {
3437 uint32_t cStreams;
3438 rc = SSMR3GetU32(pSSM, &cStreams);
3439 if (RT_FAILURE(rc))
3440 break;
3441
3442 if (cStreams > HDA_MAX_STREAMS)
3443 cStreams = HDA_MAX_STREAMS; /* Sanity. */
3444
3445 /* Load stream states. */
3446 for (uint32_t i = 0; i < cStreams; i++)
3447 {
3448 uint8_t uStreamID;
3449 rc = SSMR3GetU8(pSSM, &uStreamID);
3450 if (RT_FAILURE(rc))
3451 break;
3452
3453 PHDASTREAM pStrm = hdaGetStreamFromSD(pThis, uStreamID);
3454 HDASTREAM StreamDummy;
3455
3456 if (!pStrm)
3457 {
3458 pStrm = &StreamDummy;
3459 LogRel2(("HDA: Warning: Stream ID=%RU32 not supported, skipping to load ...\n", uStreamID));
3460 }
3461
3462 rc = hdaStreamInit(pStrm, uStreamID);
3463 if (RT_FAILURE(rc))
3464 {
3465 LogRel(("HDA: Stream #%RU32: Initialization of stream %RU8 failed, rc=%Rrc\n", i, uStreamID, rc));
3466 break;
3467 }
3468
3469 /*
3470 * Load BDLEs (Buffer Descriptor List Entries) and DMA counters.
3471 */
3472
3473 if (uVersion == HDA_SSM_VERSION_5)
3474 {
3475 /* Get the current BDLE entry and skip the rest. */
3476 uint16_t cBDLE;
3477
3478 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Begin marker */
3479 AssertRC(rc);
3480 rc = SSMR3GetU16(pSSM, &cBDLE); /* cBDLE */
3481 AssertRC(rc);
3482 rc = SSMR3GetU16(pSSM, &pStrm->State.uCurBDLE); /* uCurBDLE */
3483 AssertRC(rc);
3484 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* End marker */
3485 AssertRC(rc);
3486
3487 uint32_t u32BDLEIndex;
3488 for (uint16_t a = 0; a < cBDLE; a++)
3489 {
3490 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Begin marker */
3491 AssertRC(rc);
3492 rc = SSMR3GetU32(pSSM, &u32BDLEIndex); /* u32BDLIndex */
3493 AssertRC(rc);
3494
3495 /* Does the current BDLE index match the current BDLE to process? */
3496 if (u32BDLEIndex == pStrm->State.uCurBDLE)
3497 {
3498 rc = SSMR3GetU32(pSSM, &pStrm->State.BDLE.State.cbBelowFIFOW); /* cbBelowFIFOW */
3499 AssertRC(rc);
3500 rc = SSMR3Skip(pSSM, sizeof(uint8_t) * 256); /* FIFO, deprecated */
3501 AssertRC(rc);
3502 rc = SSMR3GetU32(pSSM, &pStrm->State.BDLE.State.u32BufOff); /* u32BufOff */
3503 AssertRC(rc);
3504 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* End marker */
3505 AssertRC(rc);
3506 }
3507 else /* Skip not current BDLEs. */
3508 {
3509 rc = SSMR3Skip(pSSM, sizeof(uint32_t) /* cbBelowFIFOW */
3510 + sizeof(uint8_t) * 256 /* au8FIFO */
3511 + sizeof(uint32_t) /* u32BufOff */
3512 + sizeof(uint32_t)); /* End marker */
3513 AssertRC(rc);
3514 }
3515 }
3516 }
3517 else
3518 {
3519 rc = SSMR3GetStructEx(pSSM, &pStrm->State, sizeof(HDASTREAMSTATE),
3520 0 /* fFlags */, g_aSSMStreamStateFields6, NULL);
3521 if (RT_FAILURE(rc))
3522 break;
3523
3524 /* Get HDABDLEDESC. */
3525 uint32_t uMarker;
3526 rc = SSMR3GetU32(pSSM, &uMarker); /* Begin marker. */
3527 AssertRC(rc);
3528 Assert(uMarker == UINT32_C(0x19200102) /* SSMR3STRUCT_BEGIN */);
3529 rc = SSMR3GetU64(pSSM, &pStrm->State.BDLE.Desc.u64BufAdr);
3530 AssertRC(rc);
3531 rc = SSMR3GetU32(pSSM, &pStrm->State.BDLE.Desc.u32BufSize);
3532 AssertRC(rc);
3533 bool fFlags = false;
3534 rc = SSMR3GetBool(pSSM, &fFlags); /* Saved states < v7 only stored the IOC as boolean flag. */
3535 AssertRC(rc);
3536 pStrm->State.BDLE.Desc.fFlags = fFlags ? HDA_BDLE_FLAG_IOC : 0;
3537 rc = SSMR3GetU32(pSSM, &uMarker); /* End marker. */
3538 AssertRC(rc);
3539 Assert(uMarker == UINT32_C(0x19920406) /* SSMR3STRUCT_END */);
3540
3541 rc = SSMR3GetStructEx(pSSM, &pStrm->State.BDLE.State, sizeof(HDABDLESTATE),
3542 0 /* fFlags */, g_aSSMBDLEStateFields6, NULL);
3543 if (RT_FAILURE(rc))
3544 break;
3545
3546 Log2Func(("[SD%RU8] LPIB=%RU32, CBL=%RU32, LVI=%RU32\n",
3547 uStreamID,
3548 HDA_STREAM_REG(pThis, LPIB, uStreamID), HDA_STREAM_REG(pThis, CBL, uStreamID), HDA_STREAM_REG(pThis, LVI, uStreamID)));
3549#ifdef LOG_ENABLED
3550 hdaBDLEDumpAll(pThis, pStrm->u64BDLBase, pStrm->u16LVI + 1);
3551#endif
3552 }
3553
3554 } /* for cStreams */
3555 break;
3556 } /* default */
3557 }
3558
3559 return rc;
3560}
3561
3562/**
3563 * @callback_method_impl{FNSSMDEVLOADEXEC}
3564 */
3565static DECLCALLBACK(int) hdaLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
3566{
3567 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
3568
3569 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
3570
3571 LogRel2(("hdaLoadExec: uVersion=%RU32, uPass=0x%x\n", uVersion, uPass));
3572
3573 /*
3574 * Load Codec nodes states.
3575 */
3576 int rc = hdaCodecLoadState(pThis->pCodec, pSSM, uVersion);
3577 if (RT_FAILURE(rc))
3578 {
3579 LogRel(("HDA: Failed loading codec state (version %RU32, pass 0x%x), rc=%Rrc\n", uVersion, uPass, rc));
3580 return rc;
3581 }
3582
3583 if (uVersion < HDA_SSM_VERSION) /* Handle older saved states? */
3584 {
3585 rc = hdaLoadExecLegacy(pThis, pSSM, uVersion, uPass);
3586 if (RT_SUCCESS(rc))
3587 rc = hdaLoadExecPost(pThis);
3588
3589 return rc;
3590 }
3591
3592 /*
3593 * Load MMIO registers.
3594 */
3595 uint32_t cRegs;
3596 rc = SSMR3GetU32(pSSM, &cRegs); AssertRCReturn(rc, rc);
3597 if (cRegs != RT_ELEMENTS(pThis->au32Regs))
3598 LogRel(("HDA: SSM version cRegs is %RU32, expected %RU32\n", cRegs, RT_ELEMENTS(pThis->au32Regs)));
3599
3600 if (cRegs >= RT_ELEMENTS(pThis->au32Regs))
3601 {
3602 SSMR3GetMem(pSSM, pThis->au32Regs, sizeof(pThis->au32Regs));
3603 SSMR3Skip(pSSM, sizeof(uint32_t) * (cRegs - RT_ELEMENTS(pThis->au32Regs)));
3604 }
3605 else
3606 SSMR3GetMem(pSSM, pThis->au32Regs, sizeof(uint32_t) * cRegs);
3607
3608 /* Make sure to update the base addresses first before initializing any streams down below. */
3609 pThis->u64CORBBase = RT_MAKE_U64(HDA_REG(pThis, CORBLBASE), HDA_REG(pThis, CORBUBASE));
3610 pThis->u64RIRBBase = RT_MAKE_U64(HDA_REG(pThis, RIRBLBASE), HDA_REG(pThis, RIRBUBASE));
3611 pThis->u64DPBase = RT_MAKE_U64(HDA_REG(pThis, DPLBASE) & DPBASE_ADDR_MASK, HDA_REG(pThis, DPUBASE));
3612
3613 /* Also make sure to update the DMA position bit if this was enabled when saving the state. */
3614 pThis->fDMAPosition = RT_BOOL(HDA_REG(pThis, DPLBASE) & RT_BIT_32(0));
3615
3616 /*
3617 * Load controller-specifc internals.
3618 * Don't annoy other team mates (forgot this for state v7).
3619 */
3620 if ( SSMR3HandleRevision(pSSM) >= 116273
3621 || SSMR3HandleVersion(pSSM) >= VBOX_FULL_VERSION_MAKE(5, 2, 0))
3622 {
3623 rc = SSMR3GetU64(pSSM, &pThis->u64WalClk);
3624 AssertRC(rc);
3625
3626 rc = SSMR3GetU8(pSSM, &pThis->u8IRQL);
3627 AssertRC(rc);
3628 }
3629
3630 /*
3631 * Load streams.
3632 */
3633 uint32_t cStreams;
3634 rc = SSMR3GetU32(pSSM, &cStreams);
3635 AssertRC(rc);
3636
3637 if (cStreams > HDA_MAX_STREAMS)
3638 cStreams = HDA_MAX_STREAMS; /* Sanity. */
3639
3640 Log2Func(("cStreams=%RU32\n", cStreams));
3641
3642 /* Load stream states. */
3643 for (uint32_t i = 0; i < cStreams; i++)
3644 {
3645 uint8_t uStreamID;
3646 rc = SSMR3GetU8(pSSM, &uStreamID);
3647 AssertRC(rc);
3648
3649 PHDASTREAM pStrm = hdaGetStreamFromSD(pThis, uStreamID);
3650 HDASTREAM StreamDummy;
3651
3652 if (!pStrm)
3653 {
3654 pStrm = &StreamDummy;
3655 LogRel2(("HDA: Warning: Loading of stream #%RU8 not supported, skipping to load ...\n", uStreamID));
3656 }
3657
3658 rc = hdaStreamInit(pStrm, uStreamID);
3659 if (RT_FAILURE(rc))
3660 {
3661 LogRel(("HDA: Stream #%RU8: Loading initialization failed, rc=%Rrc\n", uStreamID, rc));
3662 /* Continue. */
3663 }
3664
3665 /*
3666 * Load BDLEs (Buffer Descriptor List Entries) and DMA counters.
3667 */
3668 rc = SSMR3GetStructEx(pSSM, &pStrm->State, sizeof(HDASTREAMSTATE),
3669 0 /* fFlags */, g_aSSMStreamStateFields7,
3670 NULL);
3671 AssertRC(rc);
3672
3673 rc = SSMR3GetStructEx(pSSM, &pStrm->State.BDLE.Desc, sizeof(HDABDLEDESC),
3674 0 /* fFlags */, g_aSSMBDLEDescFields7, NULL);
3675 AssertRC(rc);
3676
3677 rc = SSMR3GetStructEx(pSSM, &pStrm->State.BDLE.State, sizeof(HDABDLESTATE),
3678 0 /* fFlags */, g_aSSMBDLEStateFields7, NULL);
3679 AssertRC(rc);
3680
3681 Log2Func(("[SD%RU8] %R[bdle]\n", pStrm->u8SD, &pStrm->State.BDLE));
3682
3683 /*
3684 * Load period state.
3685 * Don't annoy other team mates (forgot this for state v7).
3686 */
3687 hdaStreamPeriodInit(&pStrm->State.Period,
3688 pStrm->u8SD, pStrm->u16LVI, pStrm->u32CBL, &pStrm->State.strmCfg);
3689
3690 if ( SSMR3HandleRevision(pSSM) >= 116273
3691 || SSMR3HandleVersion(pSSM) >= VBOX_FULL_VERSION_MAKE(5, 2, 0))
3692 {
3693 rc = SSMR3GetStructEx(pSSM, &pStrm->State.Period, sizeof(HDASTREAMPERIOD),
3694 0 /* fFlags */, g_aSSMStreamPeriodFields7, NULL);
3695 AssertRC(rc);
3696 }
3697
3698 /*
3699 * Load internal (FIFO) buffer.
3700 */
3701
3702 uint32_t cbCircBufSize = 0;
3703 rc = SSMR3GetU32(pSSM, &cbCircBufSize); /* cbCircBuf */
3704 AssertRC(rc);
3705
3706 uint32_t cbCircBufUsed = 0;
3707 rc = SSMR3GetU32(pSSM, &cbCircBufUsed); /* cbCircBuf */
3708 AssertRC(rc);
3709
3710 if (cbCircBufSize) /* If 0, skip the buffer. */
3711 {
3712 /* Paranoia. */
3713 AssertReleaseMsg(cbCircBufSize <= _1M,
3714 ("HDA: Saved state contains bogus DMA buffer size (%RU32) for stream #%RU8",
3715 cbCircBufSize, uStreamID));
3716 AssertReleaseMsg(cbCircBufUsed <= cbCircBufSize,
3717 ("HDA: Saved state contains invalid DMA buffer usage (%RU32/%RU32) for stream #%RU8",
3718 cbCircBufUsed, cbCircBufSize, uStreamID));
3719 AssertPtr(pStrm->State.pCircBuf);
3720
3721 /* Do we need to cre-create the circular buffer do fit the data size? */
3722 if (cbCircBufSize != (uint32_t)RTCircBufSize(pStrm->State.pCircBuf))
3723 {
3724 RTCircBufDestroy(pStrm->State.pCircBuf);
3725 pStrm->State.pCircBuf = NULL;
3726
3727 rc = RTCircBufCreate(&pStrm->State.pCircBuf, cbCircBufSize);
3728 AssertRC(rc);
3729 }
3730
3731 if ( RT_SUCCESS(rc)
3732 && cbCircBufUsed)
3733 {
3734 void *pvBuf;
3735 size_t cbBuf;
3736
3737 RTCircBufAcquireWriteBlock(pStrm->State.pCircBuf, cbCircBufUsed, &pvBuf, &cbBuf);
3738
3739 if (cbBuf)
3740 {
3741 rc = SSMR3GetMem(pSSM, pvBuf, cbBuf);
3742 AssertRC(rc);
3743 }
3744
3745 RTCircBufReleaseWriteBlock(pStrm->State.pCircBuf, cbBuf);
3746
3747 Assert(cbBuf == cbCircBufUsed);
3748 }
3749 }
3750
3751 Log2Func(("[SD%RU8] LPIB=%RU32, CBL=%RU32, LVI=%RU32\n",
3752 uStreamID,
3753 HDA_STREAM_REG(pThis, LPIB, uStreamID), HDA_STREAM_REG(pThis, CBL, uStreamID), HDA_STREAM_REG(pThis, LVI, uStreamID)));
3754#ifdef LOG_ENABLED
3755 hdaBDLEDumpAll(pThis, pStrm->u64BDLBase, pStrm->u16LVI + 1);
3756#endif
3757 /** @todo (Re-)initialize active periods? */
3758
3759 } /* for cStreams */
3760
3761 rc = hdaLoadExecPost(pThis);
3762 AssertRC(rc);
3763
3764 LogFlowFuncLeaveRC(rc);
3765 return rc;
3766}
3767
3768/* Debug and log type formatters. */
3769
3770/**
3771 * @callback_method_impl{FNRTSTRFORMATTYPE}
3772 */
3773static DECLCALLBACK(size_t) hdaDbgFmtBDLE(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
3774 const char *pszType, void const *pvValue,
3775 int cchWidth, int cchPrecision, unsigned fFlags,
3776 void *pvUser)
3777{
3778 RT_NOREF(pszType, cchWidth, cchPrecision, fFlags, pvUser);
3779 PHDABDLE pBDLE = (PHDABDLE)pvValue;
3780 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
3781 "BDLE(idx:%RU32, off:%RU32, fifow:%RU32, IOC:%RTbool, DMA[%RU32 bytes @ 0x%x])",
3782 pBDLE->State.u32BDLIndex, pBDLE->State.u32BufOff, pBDLE->State.cbBelowFIFOW,
3783 pBDLE->Desc.fFlags & HDA_BDLE_FLAG_IOC, pBDLE->Desc.u32BufSize, pBDLE->Desc.u64BufAdr);
3784}
3785
3786/**
3787 * @callback_method_impl{FNRTSTRFORMATTYPE}
3788 */
3789static DECLCALLBACK(size_t) hdaDbgFmtSDCTL(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
3790 const char *pszType, void const *pvValue,
3791 int cchWidth, int cchPrecision, unsigned fFlags,
3792 void *pvUser)
3793{
3794 RT_NOREF(pszType, cchWidth, cchPrecision, fFlags, pvUser);
3795 uint32_t uSDCTL = (uint32_t)(uintptr_t)pvValue;
3796 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
3797 "SDCTL(raw:%#x, DIR:%s, TP:%RTbool, STRIPE:%x, DEIE:%RTbool, FEIE:%RTbool, IOCE:%RTbool, RUN:%RTbool, RESET:%RTbool)",
3798 uSDCTL,
3799 uSDCTL & HDA_SDCTL_DIR ? "OUT" : "IN",
3800 RT_BOOL(uSDCTL & HDA_SDCTL_TP),
3801 (uSDCTL & HDA_SDCTL_STRIPE_MASK) >> HDA_SDCTL_STRIPE_SHIFT,
3802 RT_BOOL(uSDCTL & HDA_SDCTL_DEIE),
3803 RT_BOOL(uSDCTL & HDA_SDCTL_FEIE),
3804 RT_BOOL(uSDCTL & HDA_SDCTL_IOCE),
3805 RT_BOOL(uSDCTL & HDA_SDCTL_RUN),
3806 RT_BOOL(uSDCTL & HDA_SDCTL_SRST));
3807}
3808
3809/**
3810 * @callback_method_impl{FNRTSTRFORMATTYPE}
3811 */
3812static DECLCALLBACK(size_t) hdaDbgFmtSDFIFOS(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
3813 const char *pszType, void const *pvValue,
3814 int cchWidth, int cchPrecision, unsigned fFlags,
3815 void *pvUser)
3816{
3817 RT_NOREF(pszType, cchWidth, cchPrecision, fFlags, pvUser);
3818 uint32_t uSDFIFOS = (uint32_t)(uintptr_t)pvValue;
3819 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "SDFIFOS(raw:%#x, sdfifos:%RU8 B)", uSDFIFOS, uSDFIFOS ? uSDFIFOS + 1 : 0);
3820}
3821
3822/**
3823 * @callback_method_impl{FNRTSTRFORMATTYPE}
3824 */
3825static DECLCALLBACK(size_t) hdaDbgFmtSDFIFOW(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
3826 const char *pszType, void const *pvValue,
3827 int cchWidth, int cchPrecision, unsigned fFlags,
3828 void *pvUser)
3829{
3830 RT_NOREF(pszType, cchWidth, cchPrecision, fFlags, pvUser);
3831 uint32_t uSDFIFOW = (uint32_t)(uintptr_t)pvValue;
3832 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "SDFIFOW(raw: %#0x, sdfifow:%d B)", uSDFIFOW, hdaSDFIFOWToBytes(uSDFIFOW));
3833}
3834
3835/**
3836 * @callback_method_impl{FNRTSTRFORMATTYPE}
3837 */
3838static DECLCALLBACK(size_t) hdaDbgFmtSDSTS(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
3839 const char *pszType, void const *pvValue,
3840 int cchWidth, int cchPrecision, unsigned fFlags,
3841 void *pvUser)
3842{
3843 RT_NOREF(pszType, cchWidth, cchPrecision, fFlags, pvUser);
3844 uint32_t uSdSts = (uint32_t)(uintptr_t)pvValue;
3845 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
3846 "SDSTS(raw:%#0x, fifordy:%RTbool, dese:%RTbool, fifoe:%RTbool, bcis:%RTbool)",
3847 uSdSts,
3848 RT_BOOL(uSdSts & HDA_SDSTS_FIFORDY),
3849 RT_BOOL(uSdSts & HDA_SDSTS_DESE),
3850 RT_BOOL(uSdSts & HDA_SDSTS_FIFOE),
3851 RT_BOOL(uSdSts & HDA_SDSTS_BCIS));
3852}
3853
3854static int hdaDbgLookupRegByName(const char *pszArgs)
3855{
3856 int iReg = 0;
3857 for (; iReg < HDA_NUM_REGS; ++iReg)
3858 if (!RTStrICmp(g_aHdaRegMap[iReg].abbrev, pszArgs))
3859 return iReg;
3860 return -1;
3861}
3862
3863
3864static void hdaDbgPrintRegister(PHDASTATE pThis, PCDBGFINFOHLP pHlp, int iHdaIndex)
3865{
3866 Assert( pThis
3867 && iHdaIndex >= 0
3868 && iHdaIndex < HDA_NUM_REGS);
3869 pHlp->pfnPrintf(pHlp, "%s: 0x%x\n", g_aHdaRegMap[iHdaIndex].abbrev, pThis->au32Regs[g_aHdaRegMap[iHdaIndex].mem_idx]);
3870}
3871
3872/**
3873 * @callback_method_impl{FNDBGFHANDLERDEV}
3874 */
3875static DECLCALLBACK(void) hdaDbgInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
3876{
3877 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
3878 int iHdaRegisterIndex = hdaDbgLookupRegByName(pszArgs);
3879 if (iHdaRegisterIndex != -1)
3880 hdaDbgPrintRegister(pThis, pHlp, iHdaRegisterIndex);
3881 else
3882 {
3883 for(iHdaRegisterIndex = 0; (unsigned int)iHdaRegisterIndex < HDA_NUM_REGS; ++iHdaRegisterIndex)
3884 hdaDbgPrintRegister(pThis, pHlp, iHdaRegisterIndex);
3885 }
3886}
3887
3888static void hdaDbgPrintStream(PHDASTATE pThis, PCDBGFINFOHLP pHlp, int iIdx)
3889{
3890 Assert( pThis
3891 && iIdx >= 0
3892 && iIdx < HDA_MAX_STREAMS);
3893
3894 const PHDASTREAM pStrm = &pThis->aStreams[iIdx];
3895
3896 pHlp->pfnPrintf(pHlp, "Stream #%d:\n", iIdx);
3897 pHlp->pfnPrintf(pHlp, "\tSD%dCTL : %R[sdctl]\n", iIdx, HDA_STREAM_REG(pThis, CTL, iIdx));
3898 pHlp->pfnPrintf(pHlp, "\tSD%dCTS : %R[sdsts]\n", iIdx, HDA_STREAM_REG(pThis, STS, iIdx));
3899 pHlp->pfnPrintf(pHlp, "\tSD%dFIFOS: %R[sdfifos]\n", iIdx, HDA_STREAM_REG(pThis, FIFOS, iIdx));
3900 pHlp->pfnPrintf(pHlp, "\tSD%dFIFOW: %R[sdfifow]\n", iIdx, HDA_STREAM_REG(pThis, FIFOW, iIdx));
3901 pHlp->pfnPrintf(pHlp, "\tBDLE : %R[bdle]\n", &pStrm->State.BDLE);
3902}
3903
3904static void hdaDbgPrintBDLE(PHDASTATE pThis, PCDBGFINFOHLP pHlp, int iIdx)
3905{
3906 Assert( pThis
3907 && iIdx >= 0
3908 && iIdx < HDA_MAX_STREAMS);
3909
3910 const PHDASTREAM pStrm = &pThis->aStreams[iIdx];
3911 const PHDABDLE pBDLE = &pStrm->State.BDLE;
3912
3913 pHlp->pfnPrintf(pHlp, "Stream #%d BDLE:\n", iIdx);
3914
3915 uint64_t u64BaseDMA = RT_MAKE_U64(HDA_STREAM_REG(pThis, BDPL, iIdx),
3916 HDA_STREAM_REG(pThis, BDPU, iIdx));
3917 uint16_t u16LVI = HDA_STREAM_REG(pThis, LVI, iIdx);
3918 uint32_t u32CBL = HDA_STREAM_REG(pThis, CBL, iIdx);
3919
3920 if (!u64BaseDMA)
3921 return;
3922
3923 pHlp->pfnPrintf(pHlp, "\tCurrent: %R[bdle]\n\n", pBDLE);
3924
3925 pHlp->pfnPrintf(pHlp, "\tMemory:\n");
3926
3927 uint32_t cbBDLE = 0;
3928 for (uint16_t i = 0; i < u16LVI + 1; i++)
3929 {
3930 HDABDLEDESC bd;
3931 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), u64BaseDMA + i * sizeof(HDABDLEDESC), &bd, sizeof(bd));
3932
3933 pHlp->pfnPrintf(pHlp, "\t\t%s #%03d BDLE(adr:0x%llx, size:%RU32, ioc:%RTbool)\n",
3934 pBDLE->State.u32BDLIndex == i ? "*" : " ", i, bd.u64BufAdr, bd.u32BufSize, bd.fFlags & HDA_BDLE_FLAG_IOC);
3935
3936 cbBDLE += bd.u32BufSize;
3937 }
3938
3939 pHlp->pfnPrintf(pHlp, "Total: %RU32 bytes\n", cbBDLE);
3940
3941 if (cbBDLE != u32CBL)
3942 pHlp->pfnPrintf(pHlp, "Warning: %RU32 bytes does not match CBL (%RU32)!\n", cbBDLE, u32CBL);
3943
3944 pHlp->pfnPrintf(pHlp, "DMA counters (base @ 0x%llx):\n", u64BaseDMA);
3945 if (!u64BaseDMA) /* No DMA base given? Bail out. */
3946 {
3947 pHlp->pfnPrintf(pHlp, "\tNo counters found\n");
3948 return;
3949 }
3950
3951 for (int i = 0; i < u16LVI + 1; i++)
3952 {
3953 uint32_t uDMACnt;
3954 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), (pThis->u64DPBase & DPBASE_ADDR_MASK) + (i * 2 * sizeof(uint32_t)),
3955 &uDMACnt, sizeof(uDMACnt));
3956
3957 pHlp->pfnPrintf(pHlp, "\t#%03d DMA @ 0x%x\n", i , uDMACnt);
3958 }
3959}
3960
3961static int hdaDbgLookupStrmIdx(PHDASTATE pThis, const char *pszArgs)
3962{
3963 RT_NOREF(pThis, pszArgs);
3964 /** @todo Add args parsing. */
3965 return -1;
3966}
3967
3968/**
3969 * @callback_method_impl{FNDBGFHANDLERDEV}
3970 */
3971static DECLCALLBACK(void) hdaDbgInfoStream(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
3972{
3973 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
3974 int iHdaStreamdex = hdaDbgLookupStrmIdx(pThis, pszArgs);
3975 if (iHdaStreamdex != -1)
3976 hdaDbgPrintStream(pThis, pHlp, iHdaStreamdex);
3977 else
3978 for(iHdaStreamdex = 0; iHdaStreamdex < HDA_MAX_STREAMS; ++iHdaStreamdex)
3979 hdaDbgPrintStream(pThis, pHlp, iHdaStreamdex);
3980}
3981
3982/**
3983 * @callback_method_impl{FNDBGFHANDLERDEV}
3984 */
3985static DECLCALLBACK(void) hdaDbgInfoBDLE(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
3986{
3987 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
3988 int iHdaStreamdex = hdaDbgLookupStrmIdx(pThis, pszArgs);
3989 if (iHdaStreamdex != -1)
3990 hdaDbgPrintBDLE(pThis, pHlp, iHdaStreamdex);
3991 else
3992 for(iHdaStreamdex = 0; iHdaStreamdex < HDA_MAX_STREAMS; ++iHdaStreamdex)
3993 hdaDbgPrintBDLE(pThis, pHlp, iHdaStreamdex);
3994}
3995
3996/**
3997 * @callback_method_impl{FNDBGFHANDLERDEV}
3998 */
3999static DECLCALLBACK(void) hdaDbgInfoCodecNodes(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
4000{
4001 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4002
4003 if (pThis->pCodec->pfnDbgListNodes)
4004 pThis->pCodec->pfnDbgListNodes(pThis->pCodec, pHlp, pszArgs);
4005 else
4006 pHlp->pfnPrintf(pHlp, "Codec implementation doesn't provide corresponding callback\n");
4007}
4008
4009/**
4010 * @callback_method_impl{FNDBGFHANDLERDEV}
4011 */
4012static DECLCALLBACK(void) hdaDbgInfoCodecSelector(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
4013{
4014 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4015
4016 if (pThis->pCodec->pfnDbgSelector)
4017 pThis->pCodec->pfnDbgSelector(pThis->pCodec, pHlp, pszArgs);
4018 else
4019 pHlp->pfnPrintf(pHlp, "Codec implementation doesn't provide corresponding callback\n");
4020}
4021
4022/**
4023 * @callback_method_impl{FNDBGFHANDLERDEV}
4024 */
4025static DECLCALLBACK(void) hdaDbgInfoMixer(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
4026{
4027 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4028
4029 if (pThis->pMixer)
4030 AudioMixerDebug(pThis->pMixer, pHlp, pszArgs);
4031 else
4032 pHlp->pfnPrintf(pHlp, "Mixer not available\n");
4033}
4034
4035
4036/* PDMIBASE */
4037
4038/**
4039 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
4040 */
4041static DECLCALLBACK(void *) hdaQueryInterface(struct PDMIBASE *pInterface, const char *pszIID)
4042{
4043 PHDASTATE pThis = RT_FROM_MEMBER(pInterface, HDASTATE, IBase);
4044 Assert(&pThis->IBase == pInterface);
4045
4046 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->IBase);
4047 return NULL;
4048}
4049
4050
4051/* PDMDEVREG */
4052
4053
4054/**
4055 * @interface_method_impl{PDMDEVREG,pfnReset}
4056 */
4057static DECLCALLBACK(void) hdaReset(PPDMDEVINS pDevIns)
4058{
4059 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4060
4061 LogFlowFuncEnter();
4062 /*
4063 * 18.2.6,7 defines that values of this registers might be cleared on power on/reset
4064 * hdaReset shouldn't affects these registers.
4065 */
4066 HDA_REG(pThis, WAKEEN) = 0x0;
4067
4068 hdaGCTLReset(pThis);
4069
4070 /* Indicate that HDA is not in reset. The firmware is supposed to (un)reset HDA,
4071 * but we can take a shortcut.
4072 */
4073 HDA_REG(pThis, GCTL) = HDA_GCTL_CRST;
4074}
4075
4076/**
4077 * @interface_method_impl{PDMDEVREG,pfnDestruct}
4078 */
4079static DECLCALLBACK(int) hdaDestruct(PPDMDEVINS pDevIns)
4080{
4081 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4082
4083 PHDADRIVER pDrv;
4084 while (!RTListIsEmpty(&pThis->lstDrv))
4085 {
4086 pDrv = RTListGetFirst(&pThis->lstDrv, HDADRIVER, Node);
4087
4088 RTListNodeRemove(&pDrv->Node);
4089 RTMemFree(pDrv);
4090 }
4091
4092 if (pThis->pCodec)
4093 {
4094 hdaCodecDestruct(pThis->pCodec);
4095
4096 RTMemFree(pThis->pCodec);
4097 pThis->pCodec = NULL;
4098 }
4099
4100 RTMemFree(pThis->pu32CorbBuf);
4101 pThis->pu32CorbBuf = NULL;
4102
4103 RTMemFree(pThis->pu64RirbBuf);
4104 pThis->pu64RirbBuf = NULL;
4105
4106 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
4107 hdaStreamDestroy(&pThis->aStreams[i]);
4108
4109 return VINF_SUCCESS;
4110}
4111
4112
4113/**
4114 * Attach command, internal version.
4115 *
4116 * This is called to let the device attach to a driver for a specified LUN
4117 * during runtime. This is not called during VM construction, the device
4118 * constructor has to attach to all the available drivers.
4119 *
4120 * @returns VBox status code.
4121 * @param pDevIns The device instance.
4122 * @param pDrv Driver to (re-)use for (re-)attaching to.
4123 * If NULL is specified, a new driver will be created and appended
4124 * to the driver list.
4125 * @param uLUN The logical unit which is being detached.
4126 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
4127 */
4128static int hdaAttachInternal(PPDMDEVINS pDevIns, PHDADRIVER pDrv, unsigned uLUN, uint32_t fFlags)
4129{
4130 RT_NOREF(fFlags);
4131 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4132
4133 /*
4134 * Attach driver.
4135 */
4136 char *pszDesc;
4137 if (RTStrAPrintf(&pszDesc, "Audio driver port (HDA) for LUN#%u", uLUN) <= 0)
4138 AssertLogRelFailedReturn(VERR_NO_MEMORY);
4139
4140 PPDMIBASE pDrvBase;
4141 int rc = PDMDevHlpDriverAttach(pDevIns, uLUN,
4142 &pThis->IBase, &pDrvBase, pszDesc);
4143 if (RT_SUCCESS(rc))
4144 {
4145 if (pDrv == NULL)
4146 pDrv = (PHDADRIVER)RTMemAllocZ(sizeof(HDADRIVER));
4147 if (pDrv)
4148 {
4149 pDrv->pDrvBase = pDrvBase;
4150 pDrv->pConnector = PDMIBASE_QUERY_INTERFACE(pDrvBase, PDMIAUDIOCONNECTOR);
4151 AssertMsg(pDrv->pConnector != NULL, ("Configuration error: LUN#%u has no host audio interface, rc=%Rrc\n", uLUN, rc));
4152 pDrv->pHDAState = pThis;
4153 pDrv->uLUN = uLUN;
4154
4155 /*
4156 * For now we always set the driver at LUN 0 as our primary
4157 * host backend. This might change in the future.
4158 */
4159 if (pDrv->uLUN == 0)
4160 pDrv->fFlags |= PDMAUDIODRVFLAGS_PRIMARY;
4161
4162 LogFunc(("LUN#%u: pCon=%p, drvFlags=0x%x\n", uLUN, pDrv->pConnector, pDrv->fFlags));
4163
4164 /* Attach to driver list if not attached yet. */
4165 if (!pDrv->fAttached)
4166 {
4167 RTListAppend(&pThis->lstDrv, &pDrv->Node);
4168 pDrv->fAttached = true;
4169 }
4170 }
4171 else
4172 rc = VERR_NO_MEMORY;
4173 }
4174 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
4175 LogFunc(("No attached driver for LUN #%u\n", uLUN));
4176
4177 if (RT_FAILURE(rc))
4178 {
4179 /* Only free this string on failure;
4180 * must remain valid for the live of the driver instance. */
4181 RTStrFree(pszDesc);
4182 }
4183
4184 LogFunc(("uLUN=%u, fFlags=0x%x, rc=%Rrc\n", uLUN, fFlags, rc));
4185 return rc;
4186}
4187
4188/**
4189 * Attach command.
4190 *
4191 * This is called to let the device attach to a driver for a specified LUN
4192 * during runtime. This is not called during VM construction, the device
4193 * constructor has to attach to all the available drivers.
4194 *
4195 * @returns VBox status code.
4196 * @param pDevIns The device instance.
4197 * @param uLUN The logical unit which is being detached.
4198 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
4199 */
4200static DECLCALLBACK(int) hdaAttach(PPDMDEVINS pDevIns, unsigned uLUN, uint32_t fFlags)
4201{
4202 return hdaAttachInternal(pDevIns, NULL /* pDrv */, uLUN, fFlags);
4203}
4204
4205static DECLCALLBACK(void) hdaDetach(PPDMDEVINS pDevIns, unsigned uLUN, uint32_t fFlags)
4206{
4207 RT_NOREF(pDevIns, uLUN, fFlags);
4208 LogFunc(("iLUN=%u, fFlags=0x%x\n", uLUN, fFlags));
4209}
4210
4211/**
4212 * Powers off the device.
4213 *
4214 * @param pDevIns Device instance to power off.
4215 */
4216static DECLCALLBACK(void) hdaPowerOff(PPDMDEVINS pDevIns)
4217{
4218 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4219
4220 LogRel2(("HDA: Powering off ...\n"));
4221
4222 /* Ditto goes for the codec, which in turn uses the mixer. */
4223 hdaCodecPowerOff(pThis->pCodec);
4224
4225 /**
4226 * Note: Destroy the mixer while powering off and *not* in hdaDestruct,
4227 * giving the mixer the chance to release any references held to
4228 * PDM audio streams it maintains.
4229 */
4230 if (pThis->pMixer)
4231 {
4232 AudioMixerDestroy(pThis->pMixer);
4233 pThis->pMixer = NULL;
4234 }
4235}
4236
4237/**
4238 * Re-attaches a new driver to the device's driver chain.
4239 *
4240 * @returns VBox status code.
4241 * @param pThis Device instance to re-attach driver to.
4242 * @param pDrv Driver instance used for attaching to.
4243 * If NULL is specified, a new driver will be created and appended
4244 * to the driver list.
4245 * @param uLUN The logical unit which is being re-detached.
4246 * @param pszDriver Driver name.
4247 */
4248static int hdaReattach(PHDASTATE pThis, PHDADRIVER pDrv, uint8_t uLUN, const char *pszDriver)
4249{
4250 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
4251 AssertPtrReturn(pszDriver, VERR_INVALID_POINTER);
4252
4253 PVM pVM = PDMDevHlpGetVM(pThis->pDevInsR3);
4254 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
4255 PCFGMNODE pDev0 = CFGMR3GetChild(pRoot, "Devices/hda/0/");
4256
4257 /* Remove LUN branch. */
4258 CFGMR3RemoveNode(CFGMR3GetChildF(pDev0, "LUN#%u/", uLUN));
4259
4260 if (pDrv)
4261 {
4262 /* Re-use a driver instance => detach the driver before. */
4263 int rc = PDMDevHlpDriverDetach(pThis->pDevInsR3, PDMIBASE_2_PDMDRV(pDrv->pDrvBase), 0 /* fFlags */);
4264 if (RT_FAILURE(rc))
4265 return rc;
4266 }
4267
4268#define RC_CHECK() if (RT_FAILURE(rc)) { AssertReleaseRC(rc); break; }
4269
4270 int rc = VINF_SUCCESS;
4271 do
4272 {
4273 PCFGMNODE pLunL0;
4274 rc = CFGMR3InsertNodeF(pDev0, &pLunL0, "LUN#%u/", uLUN); RC_CHECK();
4275 rc = CFGMR3InsertString(pLunL0, "Driver", "AUDIO"); RC_CHECK();
4276 rc = CFGMR3InsertNode(pLunL0, "Config/", NULL); RC_CHECK();
4277
4278 PCFGMNODE pLunL1, pLunL2;
4279 rc = CFGMR3InsertNode (pLunL0, "AttachedDriver/", &pLunL1); RC_CHECK();
4280 rc = CFGMR3InsertNode (pLunL1, "Config/", &pLunL2); RC_CHECK();
4281 rc = CFGMR3InsertString(pLunL1, "Driver", pszDriver); RC_CHECK();
4282
4283 rc = CFGMR3InsertString(pLunL2, "AudioDriver", pszDriver); RC_CHECK();
4284
4285 } while (0);
4286
4287 if (RT_SUCCESS(rc))
4288 rc = hdaAttachInternal(pThis->pDevInsR3, pDrv, uLUN, 0 /* fFlags */);
4289
4290 LogFunc(("pThis=%p, uLUN=%u, pszDriver=%s, rc=%Rrc\n", pThis, uLUN, pszDriver, rc));
4291
4292#undef RC_CHECK
4293
4294 return rc;
4295}
4296
4297/**
4298 * @interface_method_impl{PDMDEVREG,pfnConstruct}
4299 */
4300static DECLCALLBACK(int) hdaConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
4301{
4302 RT_NOREF(iInstance);
4303 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
4304 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4305 Assert(iInstance == 0);
4306
4307 /*
4308 * Validations.
4309 */
4310 if (!CFGMR3AreValuesValid(pCfg, "R0Enabled\0"
4311 "RCEnabled\0"
4312 "TimerHz\0"))
4313 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
4314 N_ ("Invalid configuration for the Intel HDA device"));
4315
4316 int rc = CFGMR3QueryBoolDef(pCfg, "RCEnabled", &pThis->fRCEnabled, false);
4317 if (RT_FAILURE(rc))
4318 return PDMDEV_SET_ERROR(pDevIns, rc,
4319 N_("HDA configuration error: failed to read RCEnabled as boolean"));
4320 rc = CFGMR3QueryBoolDef(pCfg, "R0Enabled", &pThis->fR0Enabled, false);
4321 if (RT_FAILURE(rc))
4322 return PDMDEV_SET_ERROR(pDevIns, rc,
4323 N_("HDA configuration error: failed to read R0Enabled as boolean"));
4324#ifndef VBOX_WITH_AUDIO_HDA_CALLBACKS
4325 uint16_t uTimerHz;
4326 rc = CFGMR3QueryU16Def(pCfg, "TimerHz", &uTimerHz, HDA_TIMER_HZ /* Default value, if not set. */);
4327 if (RT_FAILURE(rc))
4328 return PDMDEV_SET_ERROR(pDevIns, rc,
4329 N_("HDA configuration error: failed to read Hertz (Hz) rate as unsigned integer"));
4330#endif
4331
4332 /*
4333 * Initialize data (most of it anyway).
4334 */
4335 pThis->pDevInsR3 = pDevIns;
4336 pThis->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
4337 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
4338 /* IBase */
4339 pThis->IBase.pfnQueryInterface = hdaQueryInterface;
4340
4341 /* PCI Device */
4342 PCIDevSetVendorId (&pThis->PciDev, HDA_PCI_VENDOR_ID); /* nVidia */
4343 PCIDevSetDeviceId (&pThis->PciDev, HDA_PCI_DEVICE_ID); /* HDA */
4344
4345 PCIDevSetCommand (&pThis->PciDev, 0x0000); /* 04 rw,ro - pcicmd. */
4346 PCIDevSetStatus (&pThis->PciDev, VBOX_PCI_STATUS_CAP_LIST); /* 06 rwc?,ro? - pcists. */
4347 PCIDevSetRevisionId (&pThis->PciDev, 0x01); /* 08 ro - rid. */
4348 PCIDevSetClassProg (&pThis->PciDev, 0x00); /* 09 ro - pi. */
4349 PCIDevSetClassSub (&pThis->PciDev, 0x03); /* 0a ro - scc; 03 == HDA. */
4350 PCIDevSetClassBase (&pThis->PciDev, 0x04); /* 0b ro - bcc; 04 == multimedia. */
4351 PCIDevSetHeaderType (&pThis->PciDev, 0x00); /* 0e ro - headtyp. */
4352 PCIDevSetBaseAddress (&pThis->PciDev, 0, /* 10 rw - MMIO */
4353 false /* fIoSpace */, false /* fPrefetchable */, true /* f64Bit */, 0x00000000);
4354 PCIDevSetInterruptLine (&pThis->PciDev, 0x00); /* 3c rw. */
4355 PCIDevSetInterruptPin (&pThis->PciDev, 0x01); /* 3d ro - INTA#. */
4356
4357#if defined(HDA_AS_PCI_EXPRESS)
4358 PCIDevSetCapabilityList (&pThis->PciDev, 0x80);
4359#elif defined(VBOX_WITH_MSI_DEVICES)
4360 PCIDevSetCapabilityList (&pThis->PciDev, 0x60);
4361#else
4362 PCIDevSetCapabilityList (&pThis->PciDev, 0x50); /* ICH6 datasheet 18.1.16 */
4363#endif
4364
4365 /// @todo r=michaln: If there are really no PCIDevSetXx for these, the meaning
4366 /// of these values needs to be properly documented!
4367 /* HDCTL off 0x40 bit 0 selects signaling mode (1-HDA, 0 - Ac97) 18.1.19 */
4368 PCIDevSetByte(&pThis->PciDev, 0x40, 0x01);
4369
4370 /* Power Management */
4371 PCIDevSetByte(&pThis->PciDev, 0x50 + 0, VBOX_PCI_CAP_ID_PM);
4372 PCIDevSetByte(&pThis->PciDev, 0x50 + 1, 0x0); /* next */
4373 PCIDevSetWord(&pThis->PciDev, 0x50 + 2, VBOX_PCI_PM_CAP_DSI | 0x02 /* version, PM1.1 */ );
4374
4375#ifdef HDA_AS_PCI_EXPRESS
4376 /* PCI Express */
4377 PCIDevSetByte(&pThis->PciDev, 0x80 + 0, VBOX_PCI_CAP_ID_EXP); /* PCI_Express */
4378 PCIDevSetByte(&pThis->PciDev, 0x80 + 1, 0x60); /* next */
4379 /* Device flags */
4380 PCIDevSetWord(&pThis->PciDev, 0x80 + 2,
4381 /* version */ 0x1 |
4382 /* Root Complex Integrated Endpoint */ (VBOX_PCI_EXP_TYPE_ROOT_INT_EP << 4) |
4383 /* MSI */ (100) << 9 );
4384 /* Device capabilities */
4385 PCIDevSetDWord(&pThis->PciDev, 0x80 + 4, VBOX_PCI_EXP_DEVCAP_FLRESET);
4386 /* Device control */
4387 PCIDevSetWord( &pThis->PciDev, 0x80 + 8, 0);
4388 /* Device status */
4389 PCIDevSetWord( &pThis->PciDev, 0x80 + 10, 0);
4390 /* Link caps */
4391 PCIDevSetDWord(&pThis->PciDev, 0x80 + 12, 0);
4392 /* Link control */
4393 PCIDevSetWord( &pThis->PciDev, 0x80 + 16, 0);
4394 /* Link status */
4395 PCIDevSetWord( &pThis->PciDev, 0x80 + 18, 0);
4396 /* Slot capabilities */
4397 PCIDevSetDWord(&pThis->PciDev, 0x80 + 20, 0);
4398 /* Slot control */
4399 PCIDevSetWord( &pThis->PciDev, 0x80 + 24, 0);
4400 /* Slot status */
4401 PCIDevSetWord( &pThis->PciDev, 0x80 + 26, 0);
4402 /* Root control */
4403 PCIDevSetWord( &pThis->PciDev, 0x80 + 28, 0);
4404 /* Root capabilities */
4405 PCIDevSetWord( &pThis->PciDev, 0x80 + 30, 0);
4406 /* Root status */
4407 PCIDevSetDWord(&pThis->PciDev, 0x80 + 32, 0);
4408 /* Device capabilities 2 */
4409 PCIDevSetDWord(&pThis->PciDev, 0x80 + 36, 0);
4410 /* Device control 2 */
4411 PCIDevSetQWord(&pThis->PciDev, 0x80 + 40, 0);
4412 /* Link control 2 */
4413 PCIDevSetQWord(&pThis->PciDev, 0x80 + 48, 0);
4414 /* Slot control 2 */
4415 PCIDevSetWord( &pThis->PciDev, 0x80 + 56, 0);
4416#endif
4417
4418 /*
4419 * Register the PCI device.
4420 */
4421 rc = PDMDevHlpPCIRegister(pDevIns, &pThis->PciDev);
4422 if (RT_FAILURE(rc))
4423 return rc;
4424
4425 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 0, 0x4000, PCI_ADDRESS_SPACE_MEM, hdaPciIoRegionMap);
4426 if (RT_FAILURE(rc))
4427 return rc;
4428
4429#ifdef VBOX_WITH_MSI_DEVICES
4430 PDMMSIREG MsiReg;
4431 RT_ZERO(MsiReg);
4432 MsiReg.cMsiVectors = 1;
4433 MsiReg.iMsiCapOffset = 0x60;
4434 MsiReg.iMsiNextOffset = 0x50;
4435 rc = PDMDevHlpPCIRegisterMsi(pDevIns, &MsiReg);
4436 if (RT_FAILURE(rc))
4437 {
4438 /* That's OK, we can work without MSI */
4439 PCIDevSetCapabilityList(&pThis->PciDev, 0x50);
4440 }
4441#endif
4442
4443 rc = PDMDevHlpSSMRegister(pDevIns, HDA_SSM_VERSION, sizeof(*pThis), hdaSaveExec, hdaLoadExec);
4444 if (RT_FAILURE(rc))
4445 return rc;
4446
4447 RTListInit(&pThis->lstDrv);
4448
4449#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
4450 LogRel(("HDA: Asynchronous I/O enabled\n"));
4451#endif
4452
4453 uint8_t uLUN;
4454 for (uLUN = 0; uLUN < UINT8_MAX; ++uLUN)
4455 {
4456 LogFunc(("Trying to attach driver for LUN #%RU32 ...\n", uLUN));
4457 rc = hdaAttachInternal(pDevIns, NULL /* pDrv */, uLUN, 0 /* fFlags */);
4458 if (RT_FAILURE(rc))
4459 {
4460 if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
4461 rc = VINF_SUCCESS;
4462 else if (rc == VERR_AUDIO_BACKEND_INIT_FAILED)
4463 {
4464 hdaReattach(pThis, NULL /* pDrv */, uLUN, "NullAudio");
4465 PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "HostAudioNotResponding",
4466 N_("Host audio backend initialization has failed. Selecting the NULL audio backend "
4467 "with the consequence that no sound is audible"));
4468 /* Attaching to the NULL audio backend will never fail. */
4469 rc = VINF_SUCCESS;
4470 }
4471 break;
4472 }
4473 }
4474
4475 LogFunc(("cLUNs=%RU8, rc=%Rrc\n", uLUN, rc));
4476
4477 if (RT_SUCCESS(rc))
4478 {
4479 rc = AudioMixerCreate("HDA Mixer", 0 /* uFlags */, &pThis->pMixer);
4480 if (RT_SUCCESS(rc))
4481 {
4482 /*
4483 * Add mixer output sinks.
4484 */
4485#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
4486 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] Front",
4487 AUDMIXSINKDIR_OUTPUT, &pThis->SinkFront.pMixSink);
4488 AssertRC(rc);
4489 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] Center / Subwoofer",
4490 AUDMIXSINKDIR_OUTPUT, &pThis->SinkCenterLFE.pMixSink);
4491 AssertRC(rc);
4492 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] Rear",
4493 AUDMIXSINKDIR_OUTPUT, &pThis->SinkRear.pMixSink);
4494 AssertRC(rc);
4495#else
4496 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] PCM Output",
4497 AUDMIXSINKDIR_OUTPUT, &pThis->SinkFront.pMixSink);
4498 AssertRC(rc);
4499#endif
4500 /*
4501 * Add mixer input sinks.
4502 */
4503 rc = AudioMixerCreateSink(pThis->pMixer, "[Recording] Line In",
4504 AUDMIXSINKDIR_INPUT, &pThis->SinkLineIn.pMixSink);
4505 AssertRC(rc);
4506#ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
4507 rc = AudioMixerCreateSink(pThis->pMixer, "[Recording] Microphone In",
4508 AUDMIXSINKDIR_INPUT, &pThis->SinkMicIn.pMixSink);
4509 AssertRC(rc);
4510#endif
4511 /* There is no master volume control. Set the master to max. */
4512 PDMAUDIOVOLUME vol = { false, 255, 255 };
4513 rc = AudioMixerSetMasterVolume(pThis->pMixer, &vol);
4514 AssertRC(rc);
4515 }
4516 }
4517
4518 if (RT_SUCCESS(rc))
4519 {
4520 /* Construct codec. */
4521 pThis->pCodec = (PHDACODEC)RTMemAllocZ(sizeof(HDACODEC));
4522 if (!pThis->pCodec)
4523 return PDMDEV_SET_ERROR(pDevIns, VERR_NO_MEMORY, N_("Out of memory allocating HDA codec state"));
4524
4525 /* Set codec callbacks to this controller. */
4526 pThis->pCodec->pfnCbMixerAddStream = hdaMixerAddStream;
4527 pThis->pCodec->pfnCbMixerRemoveStream = hdaMixerRemoveStream;
4528 pThis->pCodec->pfnCbMixerSetStream = hdaMixerSetStream;
4529 pThis->pCodec->pfnCbMixerSetVolume = hdaMixerSetVolume;
4530
4531 pThis->pCodec->pHDAState = pThis; /* Assign HDA controller state to codec. */
4532
4533 /* Construct the codec. */
4534 rc = hdaCodecConstruct(pDevIns, pThis->pCodec, 0 /* Codec index */, pCfg);
4535 if (RT_FAILURE(rc))
4536 AssertRCReturn(rc, rc);
4537
4538 /* ICH6 datasheet defines 0 values for SVID and SID (18.1.14-15), which together with values returned for
4539 verb F20 should provide device/codec recognition. */
4540 Assert(pThis->pCodec->u16VendorId);
4541 Assert(pThis->pCodec->u16DeviceId);
4542 PCIDevSetSubSystemVendorId(&pThis->PciDev, pThis->pCodec->u16VendorId); /* 2c ro - intel.) */
4543 PCIDevSetSubSystemId( &pThis->PciDev, pThis->pCodec->u16DeviceId); /* 2e ro. */
4544 }
4545
4546 if (RT_SUCCESS(rc))
4547 {
4548 /*
4549 * Create all hardware streams.
4550 */
4551 for (uint8_t i = 0; i < HDA_MAX_STREAMS; ++i)
4552 {
4553 rc = hdaStreamCreate(&pThis->aStreams[i], pThis);
4554 AssertRC(rc);
4555 }
4556
4557#ifdef VBOX_WITH_AUDIO_HDA_ONETIME_INIT
4558 /*
4559 * Initialize the driver chain.
4560 */
4561 PHDADRIVER pDrv;
4562 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
4563 {
4564 /*
4565 * Only primary drivers are critical for the VM to run. Everything else
4566 * might not worth showing an own error message box in the GUI.
4567 */
4568 if (!(pDrv->fFlags & PDMAUDIODRVFLAGS_PRIMARY))
4569 continue;
4570
4571 PPDMIAUDIOCONNECTOR pCon = pDrv->pConnector;
4572 AssertPtr(pCon);
4573
4574 bool fValidLineIn = AudioMixerStreamIsValid(pDrv->LineIn.pMixStrm);
4575# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
4576 bool fValidMicIn = AudioMixerStreamIsValid(pDrv->MicIn.pMixStrm);
4577# endif
4578 bool fValidOut = AudioMixerStreamIsValid(pDrv->Front.pMixStrm);
4579# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
4580 /** @todo Anything to do here? */
4581# endif
4582
4583 if ( !fValidLineIn
4584# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
4585 && !fValidMicIn
4586# endif
4587 && !fValidOut)
4588 {
4589 LogRel(("HDA: Falling back to NULL backend (no sound audible)\n"));
4590
4591 hdaReset(pDevIns);
4592 hdaReattach(pThis, pDrv, pDrv->uLUN, "NullAudio");
4593
4594 PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "HostAudioNotResponding",
4595 N_("No audio devices could be opened. Selecting the NULL audio backend "
4596 "with the consequence that no sound is audible"));
4597 }
4598 else
4599 {
4600 bool fWarn = false;
4601
4602 PDMAUDIOBACKENDCFG backendCfg;
4603 int rc2 = pCon->pfnGetConfig(pCon, &backendCfg);
4604 if (RT_SUCCESS(rc2))
4605 {
4606 if (backendCfg.cMaxStreamsIn)
4607 {
4608# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
4609 /* If the audio backend supports two or more input streams at once,
4610 * warn if one of our two inputs (microphone-in and line-in) failed to initialize. */
4611 if (backendCfg.cMaxStreamsIn >= 2)
4612 fWarn = !fValidLineIn || !fValidMicIn;
4613 /* If the audio backend only supports one input stream at once (e.g. pure ALSA, and
4614 * *not* ALSA via PulseAudio plugin!), only warn if both of our inputs failed to initialize.
4615 * One of the two simply is not in use then. */
4616 else if (backendCfg.cMaxStreamsIn == 1)
4617 fWarn = !fValidLineIn && !fValidMicIn;
4618 /* Don't warn if our backend is not able of supporting any input streams at all. */
4619# else /* !VBOX_WITH_AUDIO_HDA_MIC_IN */
4620 /* We only have line-in as input source. */
4621 fWarn = !fValidLineIn;
4622# endif /* VBOX_WITH_AUDIO_HDA_MIC_IN */
4623 }
4624
4625 if ( !fWarn
4626 && backendCfg.cMaxStreamsOut)
4627 {
4628 fWarn = !fValidOut;
4629 }
4630 }
4631 else
4632 {
4633 LogRel(("HDA: Unable to retrieve audio backend configuration for LUN #%RU8, rc=%Rrc\n", pDrv->uLUN, rc2));
4634 fWarn = true;
4635 }
4636
4637 if (fWarn)
4638 {
4639 char szMissingStreams[255];
4640 size_t len = 0;
4641 if (!fValidLineIn)
4642 {
4643 LogRel(("HDA: WARNING: Unable to open PCM line input for LUN #%RU8!\n", pDrv->uLUN));
4644 len = RTStrPrintf(szMissingStreams, sizeof(szMissingStreams), "PCM Input");
4645 }
4646# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
4647 if (!fValidMicIn)
4648 {
4649 LogRel(("HDA: WARNING: Unable to open PCM microphone input for LUN #%RU8!\n", pDrv->uLUN));
4650 len += RTStrPrintf(szMissingStreams + len,
4651 sizeof(szMissingStreams) - len, len ? ", PCM Microphone" : "PCM Microphone");
4652 }
4653# endif /* VBOX_WITH_AUDIO_HDA_MIC_IN */
4654 if (!fValidOut)
4655 {
4656 LogRel(("HDA: WARNING: Unable to open PCM output for LUN #%RU8!\n", pDrv->uLUN));
4657 len += RTStrPrintf(szMissingStreams + len,
4658 sizeof(szMissingStreams) - len, len ? ", PCM Output" : "PCM Output");
4659 }
4660
4661 PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "HostAudioNotResponding",
4662 N_("Some HDA audio streams (%s) could not be opened. Guest applications generating audio "
4663 "output or depending on audio input may hang. Make sure your host audio device "
4664 "is working properly. Check the logfile for error messages of the audio "
4665 "subsystem"), szMissingStreams);
4666 }
4667 }
4668 }
4669#endif /* VBOX_WITH_AUDIO_HDA_ONETIME_INIT */
4670 }
4671
4672 if (RT_SUCCESS(rc))
4673 {
4674 hdaReset(pDevIns);
4675
4676 /*
4677 * Debug and string formatter types.
4678 */
4679 PDMDevHlpDBGFInfoRegister(pDevIns, "hda", "HDA info. (hda [register case-insensitive])", hdaDbgInfo);
4680 PDMDevHlpDBGFInfoRegister(pDevIns, "hdabdle", "HDA stream BDLE info. (hdabdle [stream number])", hdaDbgInfoBDLE);
4681 PDMDevHlpDBGFInfoRegister(pDevIns, "hdastream", "HDA stream info. (hdastream [stream number])", hdaDbgInfoStream);
4682 PDMDevHlpDBGFInfoRegister(pDevIns, "hdcnodes", "HDA codec nodes.", hdaDbgInfoCodecNodes);
4683 PDMDevHlpDBGFInfoRegister(pDevIns, "hdcselector", "HDA codec's selector states [node number].", hdaDbgInfoCodecSelector);
4684 PDMDevHlpDBGFInfoRegister(pDevIns, "hdamixer", "HDA mixer state.", hdaDbgInfoMixer);
4685
4686 rc = RTStrFormatTypeRegister("bdle", hdaDbgFmtBDLE, NULL);
4687 AssertRC(rc);
4688 rc = RTStrFormatTypeRegister("sdctl", hdaDbgFmtSDCTL, NULL);
4689 AssertRC(rc);
4690 rc = RTStrFormatTypeRegister("sdsts", hdaDbgFmtSDSTS, NULL);
4691 AssertRC(rc);
4692 rc = RTStrFormatTypeRegister("sdfifos", hdaDbgFmtSDFIFOS, NULL);
4693 AssertRC(rc);
4694 rc = RTStrFormatTypeRegister("sdfifow", hdaDbgFmtSDFIFOW, NULL);
4695 AssertRC(rc);
4696
4697 /*
4698 * Some debug assertions.
4699 */
4700 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegMap); i++)
4701 {
4702 struct HDAREGDESC const *pReg = &g_aHdaRegMap[i];
4703 struct HDAREGDESC const *pNextReg = i + 1 < RT_ELEMENTS(g_aHdaRegMap) ? &g_aHdaRegMap[i + 1] : NULL;
4704
4705 /* binary search order. */
4706 AssertReleaseMsg(!pNextReg || pReg->offset + pReg->size <= pNextReg->offset,
4707 ("[%#x] = {%#x LB %#x} vs. [%#x] = {%#x LB %#x}\n",
4708 i, pReg->offset, pReg->size, i + 1, pNextReg->offset, pNextReg->size));
4709
4710 /* alignment. */
4711 AssertReleaseMsg( pReg->size == 1
4712 || (pReg->size == 2 && (pReg->offset & 1) == 0)
4713 || (pReg->size == 3 && (pReg->offset & 3) == 0)
4714 || (pReg->size == 4 && (pReg->offset & 3) == 0),
4715 ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
4716
4717 /* registers are packed into dwords - with 3 exceptions with gaps at the end of the dword. */
4718 AssertRelease(((pReg->offset + pReg->size) & 3) == 0 || pNextReg);
4719 if (pReg->offset & 3)
4720 {
4721 struct HDAREGDESC const *pPrevReg = i > 0 ? &g_aHdaRegMap[i - 1] : NULL;
4722 AssertReleaseMsg(pPrevReg, ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
4723 if (pPrevReg)
4724 AssertReleaseMsg(pPrevReg->offset + pPrevReg->size == pReg->offset,
4725 ("[%#x] = {%#x LB %#x} vs. [%#x] = {%#x LB %#x}\n",
4726 i - 1, pPrevReg->offset, pPrevReg->size, i + 1, pReg->offset, pReg->size));
4727 }
4728#if 0
4729 if ((pReg->offset + pReg->size) & 3)
4730 {
4731 AssertReleaseMsg(pNextReg, ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
4732 if (pNextReg)
4733 AssertReleaseMsg(pReg->offset + pReg->size == pNextReg->offset,
4734 ("[%#x] = {%#x LB %#x} vs. [%#x] = {%#x LB %#x}\n",
4735 i, pReg->offset, pReg->size, i + 1, pNextReg->offset, pNextReg->size));
4736 }
4737#endif
4738 /* The final entry is a full DWORD, no gaps! Allows shortcuts. */
4739 AssertReleaseMsg(pNextReg || ((pReg->offset + pReg->size) & 3) == 0,
4740 ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
4741 }
4742 }
4743
4744# ifndef VBOX_WITH_AUDIO_HDA_CALLBACKS
4745 if (RT_SUCCESS(rc))
4746 {
4747 /* Create the emulation timer.
4748 *
4749 * Note: Use TMCLOCK_VIRTUAL_SYNC here, as the guest's HDA driver
4750 * relies on exact (virtual) DMA timing and uses DMA Position Buffers
4751 * instead of the LPIB registers.
4752 */
4753 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL_SYNC, hdaTimer, pThis,
4754 TMTIMER_FLAGS_NO_CRIT_SECT, "DevHDA", &pThis->pTimer);
4755 AssertRCReturn(rc, rc);
4756
4757 if (RT_SUCCESS(rc))
4758 {
4759 pThis->cTimerTicks = TMTimerGetFreq(pThis->pTimer) / uTimerHz;
4760 LogFunc(("Timer ticks=%RU64 (%RU16 Hz)\n", pThis->cTimerTicks, uTimerHz));
4761 }
4762 }
4763# else
4764 if (RT_SUCCESS(rc))
4765 {
4766 PHDADRIVER pDrv;
4767 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
4768 {
4769 /* Only register primary driver.
4770 * The device emulation does the output multiplexing then. */
4771 if (pDrv->fFlags != PDMAUDIODRVFLAGS_PRIMARY)
4772 continue;
4773
4774 PDMAUDIOCALLBACK AudioCallbacks[2];
4775
4776 HDACALLBACKCTX Ctx = { pThis, pDrv };
4777
4778 AudioCallbacks[0].enmType = PDMAUDIOCALLBACKTYPE_INPUT;
4779 AudioCallbacks[0].pfnCallback = hdaCallbackInput;
4780 AudioCallbacks[0].pvCtx = &Ctx;
4781 AudioCallbacks[0].cbCtx = sizeof(HDACALLBACKCTX);
4782
4783 AudioCallbacks[1].enmType = PDMAUDIOCALLBACKTYPE_OUTPUT;
4784 AudioCallbacks[1].pfnCallback = hdaCallbackOutput;
4785 AudioCallbacks[1].pvCtx = &Ctx;
4786 AudioCallbacks[1].cbCtx = sizeof(HDACALLBACKCTX);
4787
4788 rc = pDrv->pConnector->pfnRegisterCallbacks(pDrv->pConnector, AudioCallbacks, RT_ELEMENTS(AudioCallbacks));
4789 if (RT_FAILURE(rc))
4790 break;
4791 }
4792 }
4793# endif
4794
4795# ifdef VBOX_WITH_STATISTICS
4796 if (RT_SUCCESS(rc))
4797 {
4798 /*
4799 * Register statistics.
4800 */
4801# ifndef VBOX_WITH_AUDIO_HDA_CALLBACKS
4802 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTimer, STAMTYPE_PROFILE, "/Devices/HDA/Timer", STAMUNIT_TICKS_PER_CALL, "Profiling hdaTimer.");
4803# endif
4804 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIn, STAMTYPE_PROFILE, "/Devices/HDA/Input", STAMUNIT_TICKS_PER_CALL, "Profiling input.");
4805 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatOut, STAMTYPE_PROFILE, "/Devices/HDA/Output", STAMUNIT_TICKS_PER_CALL, "Profiling output.");
4806 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, "/Devices/HDA/BytesRead" , STAMUNIT_BYTES, "Bytes read from HDA emulation.");
4807 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, "/Devices/HDA/BytesWritten", STAMUNIT_BYTES, "Bytes written to HDA emulation.");
4808 }
4809# endif
4810
4811#ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
4812 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "hdaDMARead.pcm");
4813 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "hdaDMAWrite.pcm");
4814 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "hdaStreamRead.pcm");
4815 RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "hdaStreamWrite.pcm");
4816#endif
4817
4818 LogFlowFuncLeaveRC(rc);
4819 return rc;
4820}
4821
4822/**
4823 * The device registration structure.
4824 */
4825const PDMDEVREG g_DeviceHDA =
4826{
4827 /* u32Version */
4828 PDM_DEVREG_VERSION,
4829 /* szName */
4830 "hda",
4831 /* szRCMod */
4832 "VBoxDDRC.rc",
4833 /* szR0Mod */
4834 "VBoxDDR0.r0",
4835 /* pszDescription */
4836 "Intel HD Audio Controller",
4837 /* fFlags */
4838 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
4839 /* fClass */
4840 PDM_DEVREG_CLASS_AUDIO,
4841 /* cMaxInstances */
4842 1,
4843 /* cbInstance */
4844 sizeof(HDASTATE),
4845 /* pfnConstruct */
4846 hdaConstruct,
4847 /* pfnDestruct */
4848 hdaDestruct,
4849 /* pfnRelocate */
4850 NULL,
4851 /* pfnMemSetup */
4852 NULL,
4853 /* pfnPowerOn */
4854 NULL,
4855 /* pfnReset */
4856 hdaReset,
4857 /* pfnSuspend */
4858 NULL,
4859 /* pfnResume */
4860 NULL,
4861 /* pfnAttach */
4862 hdaAttach,
4863 /* pfnDetach */
4864 hdaDetach,
4865 /* pfnQueryInterface. */
4866 NULL,
4867 /* pfnInitComplete */
4868 NULL,
4869 /* pfnPowerOff */
4870 hdaPowerOff,
4871 /* pfnSoftReset */
4872 NULL,
4873 /* u32VersionEnd */
4874 PDM_DEVREG_VERSION
4875};
4876
4877#endif /* IN_RING3 */
4878#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
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