VirtualBox

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

Last change on this file since 80957 was 80704, checked in by vboxsync, 5 years ago

PDM,Devices: Changed PDM_DEVREG_FLAGS_MSI_X into a registration field giving the max MSI-X vector count config for the device (typically VBOX_MSIX_MAX_ENTRIES). bugref:9218

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