VirtualBox

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

最後變更 在這個檔案從82255是 82252,由 vboxsync 提交於 5 年 前

vmm/pdmaudioifs.h: Style, docs and other nits. First, it's always _FLAGS_ never _FLAG_. Second, enums generally should start with _INVALID = 0 to ensure we don't mistake zero-initialized memory for valid data. Struct member names shall be indented on a tab (+4) boundrary. PDM is part of the VMM, so it follows the VMM coding guidelines strictly. Skip the 'Structure for keeping a ... around' fluff, the first sentence of a structure (or anything else for that matter) documentation shall be brief and to the point. It is automatically turned into a @brief. Furthermore, additional text should be a separate paragraph as it provides details the reader doesn't necessarily need to read. bugref:9218

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 193.8 KB
 
1/* $Id: DevHDA.cpp 82252 2019-11-27 21:31:53Z 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.alldomusa.eu.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) -- upper 8 bits are reserved */ \
405 { offset + 0xC, 0x00002, 0x0000FFFF, 0x000000FF, 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 DEVHDA_UNLOCK_BOTH(pDevIns, pThis, uSD);
1316 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
1317 }
1318
1319 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, uSD);
1320 if (!pStream)
1321 {
1322 ASSERT_GUEST_LOGREL_MSG_FAILED(("Guest tried writing SDCTL (0x%x) to unhandled stream #%RU8\n", u32Value, uSD));
1323
1324 DEVHDA_UNLOCK_BOTH(pDevIns, pThis, uSD);
1325 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
1326 }
1327
1328 if (fInReset)
1329 {
1330 Assert(!fReset);
1331 Assert(!fInRun && !fRun);
1332
1333 /* Exit reset state. */
1334 ASMAtomicXchgBool(&pStream->State.fInReset, false);
1335
1336 /* Report that we're done resetting this stream by clearing SRST. */
1337 HDA_STREAM_REG(pThis, CTL, uSD) &= ~HDA_SDCTL_SRST;
1338
1339 LogFunc(("[SD%RU8] Reset exit\n", uSD));
1340 }
1341 else if (fReset)
1342 {
1343 /* ICH6 datasheet 18.2.33 says that RUN bit should be cleared before initiation of reset. */
1344 Assert(!fInRun && !fRun);
1345
1346 LogFunc(("[SD%RU8] Reset enter\n", uSD));
1347
1348 hdaR3StreamLock(pStream);
1349
1350# ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
1351 hdaR3StreamAsyncIOLock(pStream);
1352# endif
1353 /* Make sure to remove the run bit before doing the actual stream reset. */
1354 HDA_STREAM_REG(pThis, CTL, uSD) &= ~HDA_SDCTL_RUN;
1355
1356 hdaR3StreamReset(pThis, pStream, pStream->u8SD);
1357
1358# ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
1359 hdaR3StreamAsyncIOUnlock(pStream);
1360# endif
1361 hdaR3StreamUnlock(pStream);
1362 }
1363 else
1364 {
1365 /*
1366 * We enter here to change DMA states only.
1367 */
1368 if (fInRun != fRun)
1369 {
1370 Assert(!fReset && !fInReset);
1371 LogFunc(("[SD%RU8] State changed (fRun=%RTbool)\n", uSD, fRun));
1372
1373 hdaR3StreamLock(pStream);
1374
1375 int rc2 = VINF_SUCCESS;
1376
1377# ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
1378 if (fRun)
1379 rc2 = hdaR3StreamAsyncIOCreate(pStream);
1380
1381 hdaR3StreamAsyncIOLock(pStream);
1382# endif
1383 if (fRun)
1384 {
1385 if (hdaGetDirFromSD(uSD) == PDMAUDIODIR_OUT)
1386 {
1387 const uint8_t uStripeCtl = ((u32Value >> HDA_SDCTL_STRIPE_SHIFT) & HDA_SDCTL_STRIPE_MASK) + 1;
1388 LogFunc(("[SD%RU8] Using %RU8 SDOs (stripe control)\n", uSD, uStripeCtl));
1389 if (uStripeCtl > 1)
1390 LogRel2(("HDA: Warning: Striping output over more than one SDO for stream #%RU8 currently is not implemented " \
1391 "(%RU8 SDOs requested)\n", uSD, uStripeCtl));
1392 }
1393
1394 PHDATAG pTag = &pThis->aTags[uTag];
1395 AssertPtr(pTag);
1396
1397 LogFunc(("[SD%RU8] Using stream tag=%RU8\n", uSD, uTag));
1398
1399 /* Assign new values. */
1400 pTag->uTag = uTag;
1401 pTag->pStream = hdaGetStreamFromSD(pThis, uSD);
1402
1403# ifdef LOG_ENABLED
1404 PDMAUDIOPCMPROPS Props;
1405 rc2 = hdaR3SDFMTToPCMProps(HDA_STREAM_REG(pThis, FMT, pStream->u8SD), &Props);
1406 AssertRC(rc2);
1407 LogFunc(("[SD%RU8] %RU32Hz, %RU8bit, %RU8 channel(s)\n",
1408 pStream->u8SD, Props.uHz, Props.cbSample * 8 /* Bit */, Props.cChannels));
1409# endif
1410 /* (Re-)initialize the stream with current values. */
1411 rc2 = hdaR3StreamInit(pStream, pStream->u8SD);
1412 if ( RT_SUCCESS(rc2)
1413 /* Any vital stream change occurred so that we need to (re-)add the stream to our setup?
1414 * Otherwise just skip this, as this costs a lot of performance. */
1415 && rc2 != VINF_NO_CHANGE)
1416 {
1417 /* Remove the old stream from the device setup. */
1418 rc2 = hdaR3RemoveStream(pThis, &pStream->State.Cfg);
1419 AssertRC(rc2);
1420
1421 /* Add the stream to the device setup. */
1422 rc2 = hdaR3AddStream(pThis, &pStream->State.Cfg);
1423 AssertRC(rc2);
1424 }
1425 }
1426
1427 if (RT_SUCCESS(rc2))
1428 {
1429 /* Enable/disable the stream. */
1430 rc2 = hdaR3StreamEnable(pStream, fRun /* fEnable */);
1431 AssertRC(rc2);
1432
1433 if (fRun)
1434 {
1435 /* Keep track of running streams. */
1436 pThis->cStreamsActive++;
1437
1438 /* (Re-)init the stream's period. */
1439 hdaR3StreamPeriodInit(&pStream->State.Period,
1440 pStream->u8SD, pStream->u16LVI, pStream->u32CBL, &pStream->State.Cfg);
1441
1442 /* Begin a new period for this stream. */
1443 rc2 = hdaR3StreamPeriodBegin(&pStream->State.Period, hdaWalClkGetCurrent(pThis)/* Use current wall clock time */);
1444 AssertRC(rc2);
1445
1446 rc2 = hdaR3TimerSet(pThis, pStream, TMTimerGet(pThis->pTimer[pStream->u8SD]) + pStream->State.cTransferTicks,
1447 false /* fForce */);
1448 AssertRC(rc2);
1449 }
1450 else
1451 {
1452 /* Keep track of running streams. */
1453 Assert(pThis->cStreamsActive);
1454 if (pThis->cStreamsActive)
1455 pThis->cStreamsActive--;
1456
1457 /* Make sure to (re-)schedule outstanding (delayed) interrupts. */
1458 hdaR3ReschedulePendingInterrupts(pThis);
1459
1460 /* Reset the period. */
1461 hdaR3StreamPeriodReset(&pStream->State.Period);
1462 }
1463 }
1464
1465# ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
1466 hdaR3StreamAsyncIOUnlock(pStream);
1467# endif
1468 /* Make sure to leave the lock before (eventually) starting the timer. */
1469 hdaR3StreamUnlock(pStream);
1470 }
1471 }
1472
1473 int rc2 = hdaRegWriteU24(pDevIns, pThis, iReg, u32Value);
1474 AssertRC(rc2);
1475
1476 DEVHDA_UNLOCK_BOTH(pDevIns, pThis, uSD);
1477 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
1478#else /* !IN_RING3 */
1479 RT_NOREF(pDevIns, pThis, iReg, u32Value);
1480 return VINF_IOM_R3_MMIO_WRITE;
1481#endif /* !IN_RING3 */
1482}
1483
1484static int hdaRegWriteSDSTS(PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1485{
1486#ifdef IN_RING3
1487 const uint8_t uSD = HDA_SD_NUM_FROM_REG(pThis, STS, iReg);
1488
1489 DEVHDA_LOCK_BOTH_RETURN(pDevIns, pThis, uSD, VINF_IOM_R3_MMIO_WRITE);
1490
1491 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, uSD);
1492 if (!pStream)
1493 {
1494 ASSERT_GUEST_LOGREL_MSG_FAILED(("Guest tried writing SDSTS (0x%x) to unhandled stream #%RU8\n", u32Value, uSD));
1495 DEVHDA_UNLOCK_BOTH(pDevIns, pThis, uSD);
1496 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
1497 }
1498
1499 hdaR3StreamLock(pStream);
1500
1501 uint32_t v = HDA_REG_IND(pThis, iReg);
1502
1503 /* Clear (zero) FIFOE, DESE and BCIS bits when writing 1 to it (6.2.33). */
1504 HDA_REG_IND(pThis, iReg) &= ~(u32Value & v);
1505
1506 /* Some guests tend to write SDnSTS even if the stream is not running.
1507 * So make sure to check if the RUN bit is set first. */
1508 const bool fRunning = pStream->State.fRunning;
1509
1510 Log3Func(("[SD%RU8] fRunning=%RTbool %R[sdsts]\n", pStream->u8SD, fRunning, v));
1511
1512 PHDASTREAMPERIOD pPeriod = &pStream->State.Period;
1513
1514 if (hdaR3StreamPeriodLock(pPeriod))
1515 {
1516 const bool fNeedsInterrupt = hdaR3StreamPeriodNeedsInterrupt(pPeriod);
1517 if (fNeedsInterrupt)
1518 hdaR3StreamPeriodReleaseInterrupt(pPeriod);
1519
1520 if (hdaR3StreamPeriodIsComplete(pPeriod))
1521 {
1522 /* Make sure to try to update the WALCLK register if a period is complete.
1523 * Use the maximum WALCLK value all (active) streams agree to. */
1524 const uint64_t uWalClkMax = hdaR3WalClkGetMax(pThis);
1525 if (uWalClkMax > hdaWalClkGetCurrent(pThis))
1526 hdaR3WalClkSet(pThis, uWalClkMax, false /* fForce */);
1527
1528 hdaR3StreamPeriodEnd(pPeriod);
1529
1530 if (fRunning)
1531 hdaR3StreamPeriodBegin(pPeriod, hdaWalClkGetCurrent(pThis) /* Use current wall clock time */);
1532 }
1533
1534 hdaR3StreamPeriodUnlock(pPeriod); /* Unlock before processing interrupt. */
1535 }
1536
1537 HDA_PROCESS_INTERRUPT(pDevIns, pThis);
1538
1539 const uint64_t tsNow = TMTimerGet(pThis->pTimer[uSD]);
1540 Assert(tsNow >= pStream->State.tsTransferLast);
1541
1542 const uint64_t cTicksElapsed = tsNow - pStream->State.tsTransferLast;
1543# ifdef LOG_ENABLED
1544 const uint64_t cTicksTransferred = pStream->State.cbTransferProcessed * pStream->State.cTicksPerByte;
1545# endif
1546
1547 uint64_t cTicksToNext = pStream->State.cTransferTicks;
1548 if (cTicksToNext) /* Only do any calculations if the stream currently is set up for transfers. */
1549 {
1550 Log3Func(("[SD%RU8] cTicksElapsed=%RU64, cTicksTransferred=%RU64, cTicksToNext=%RU64\n",
1551 pStream->u8SD, cTicksElapsed, cTicksTransferred, cTicksToNext));
1552
1553 Log3Func(("[SD%RU8] cbTransferProcessed=%RU32, cbTransferChunk=%RU32, cbTransferSize=%RU32\n",
1554 pStream->u8SD, pStream->State.cbTransferProcessed, pStream->State.cbTransferChunk, pStream->State.cbTransferSize));
1555
1556 if (cTicksElapsed <= cTicksToNext)
1557 {
1558 cTicksToNext = cTicksToNext - cTicksElapsed;
1559 }
1560 else /* Catch up. */
1561 {
1562 Log3Func(("[SD%RU8] Warning: Lagging behind (%RU64 ticks elapsed, maximum allowed is %RU64)\n",
1563 pStream->u8SD, cTicksElapsed, cTicksToNext));
1564
1565 LogRelMax2(64, ("HDA: Stream #%RU8 interrupt lagging behind (expected %uus, got %uus), trying to catch up ...\n",
1566 pStream->u8SD,
1567 (TMTimerGetFreq(pThis->pTimer[pStream->u8SD]) / pThis->uTimerHz) / 1000,(tsNow - pStream->State.tsTransferLast) / 1000));
1568
1569 cTicksToNext = 0;
1570 }
1571
1572 Log3Func(("[SD%RU8] -> cTicksToNext=%RU64\n", pStream->u8SD, cTicksToNext));
1573
1574 /* Reset processed data counter. */
1575 pStream->State.cbTransferProcessed = 0;
1576 pStream->State.tsTransferNext = tsNow + cTicksToNext;
1577
1578 /* Only re-arm the timer if there were pending transfer interrupts left
1579 * -- it could happen that we land in here if a guest writes to SDnSTS
1580 * unconditionally. */
1581 if (pStream->State.cTransferPendingInterrupts)
1582 {
1583 pStream->State.cTransferPendingInterrupts--;
1584
1585 /* Re-arm the timer. */
1586 LogFunc(("Timer set SD%RU8\n", pStream->u8SD));
1587 hdaR3TimerSet(pThis, pStream, tsNow + cTicksToNext, false /* fForce */);
1588 }
1589 }
1590
1591 hdaR3StreamUnlock(pStream);
1592
1593 DEVHDA_UNLOCK_BOTH(pDevIns, pThis, uSD);
1594 return VINF_SUCCESS;
1595#else /* !IN_RING3 */
1596 RT_NOREF(pDevIns, pThis, iReg, u32Value);
1597 return VINF_IOM_R3_MMIO_WRITE;
1598#endif /* !IN_RING3 */
1599}
1600
1601static int hdaRegWriteSDLVI(PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1602{
1603 DEVHDA_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_MMIO_WRITE);
1604
1605 const uint8_t uSD = HDA_SD_NUM_FROM_REG(pThis, LVI, iReg);
1606
1607#ifdef HDA_USE_DMA_ACCESS_HANDLER
1608 if (hdaGetDirFromSD(uSD) == PDMAUDIODIR_OUT)
1609 {
1610 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, uSD);
1611
1612 /* Try registering the DMA handlers.
1613 * As we can't be sure in which order LVI + BDL base are set, try registering in both routines. */
1614 if ( pStream
1615 && hdaR3StreamRegisterDMAHandlers(pThis, pStream))
1616 {
1617 LogFunc(("[SD%RU8] DMA logging enabled\n", pStream->u8SD));
1618 }
1619 }
1620#endif
1621
1622 ASSERT_GUEST_LOGREL_MSG(u32Value <= UINT8_MAX, /* Should be covered by the register write mask, but just to make sure. */
1623 ("LVI for stream #%RU8 must not be bigger than %RU8\n", uSD, UINT8_MAX - 1));
1624
1625 int rc2 = hdaRegWriteU16(pDevIns, pThis, iReg, u32Value);
1626 AssertRC(rc2);
1627
1628 DEVHDA_UNLOCK(pDevIns, pThis);
1629 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
1630}
1631
1632static int hdaRegWriteSDFIFOW(PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1633{
1634 DEVHDA_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_MMIO_WRITE);
1635
1636 uint8_t uSD = HDA_SD_NUM_FROM_REG(pThis, FIFOW, iReg);
1637
1638 if (hdaGetDirFromSD(uSD) != PDMAUDIODIR_IN) /* FIFOW for input streams only. */
1639 {
1640#ifndef IN_RING0
1641 LogRel(("HDA: Warning: Guest tried to write read-only FIFOW to output stream #%RU8, ignoring\n", uSD));
1642 DEVHDA_UNLOCK(pDevIns, pThis);
1643 return VINF_SUCCESS;
1644#else
1645 DEVHDA_UNLOCK(pDevIns, pThis);
1646 return VINF_IOM_R3_MMIO_WRITE;
1647#endif
1648 }
1649
1650 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, HDA_SD_NUM_FROM_REG(pThis, FIFOW, iReg));
1651 if (!pStream)
1652 {
1653 DEVHDA_UNLOCK(pDevIns, pThis);
1654 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
1655 }
1656
1657 uint32_t u32FIFOW = 0;
1658
1659 switch (u32Value)
1660 {
1661 case HDA_SDFIFOW_8B:
1662 case HDA_SDFIFOW_16B:
1663 case HDA_SDFIFOW_32B:
1664 u32FIFOW = u32Value;
1665 break;
1666 default:
1667 ASSERT_GUEST_LOGREL_MSG_FAILED(("Guest tried writing unsupported FIFOW (0x%x) to stream #%RU8, defaulting to 32 bytes\n",
1668 u32Value, uSD));
1669 u32FIFOW = HDA_SDFIFOW_32B;
1670 break;
1671 }
1672
1673 if (u32FIFOW)
1674 {
1675 pStream->u16FIFOW = hdaSDFIFOWToBytes(u32FIFOW);
1676 LogFunc(("[SD%RU8] Updating FIFOW to %RU32 bytes\n", uSD, pStream->u16FIFOW));
1677
1678 int rc2 = hdaRegWriteU16(pDevIns, pThis, iReg, u32FIFOW);
1679 AssertRC(rc2);
1680 }
1681
1682 DEVHDA_UNLOCK(pDevIns, pThis);
1683 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
1684}
1685
1686/**
1687 * @note This method could be called for changing value on Output Streams only (ICH6 datasheet 18.2.39).
1688 */
1689static int hdaRegWriteSDFIFOS(PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1690{
1691 DEVHDA_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_MMIO_WRITE);
1692
1693 uint8_t uSD = HDA_SD_NUM_FROM_REG(pThis, FIFOS, iReg);
1694
1695 if (hdaGetDirFromSD(uSD) != PDMAUDIODIR_OUT) /* FIFOS for output streams only. */
1696 {
1697 ASSERT_GUEST_LOGREL_MSG_FAILED(("Guest tried writing read-only FIFOS to input stream #%RU8, ignoring\n", uSD));
1698 DEVHDA_UNLOCK(pDevIns, pThis);
1699 return VINF_SUCCESS;
1700 }
1701
1702 uint32_t u32FIFOS;
1703
1704 switch(u32Value)
1705 {
1706 case HDA_SDOFIFO_16B:
1707 case HDA_SDOFIFO_32B:
1708 case HDA_SDOFIFO_64B:
1709 case HDA_SDOFIFO_128B:
1710 case HDA_SDOFIFO_192B:
1711 case HDA_SDOFIFO_256B:
1712 u32FIFOS = u32Value;
1713 break;
1714
1715 default:
1716 ASSERT_GUEST_LOGREL_MSG_FAILED(("Guest tried writing unsupported FIFOS (0x%x) to stream #%RU8, defaulting to 192 bytes\n",
1717 u32Value, uSD));
1718 u32FIFOS = HDA_SDOFIFO_192B;
1719 break;
1720 }
1721
1722 int rc2 = hdaRegWriteU16(pDevIns, pThis, iReg, u32FIFOS);
1723 AssertRC(rc2);
1724
1725 DEVHDA_UNLOCK(pDevIns, pThis);
1726 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
1727}
1728
1729#ifdef IN_RING3
1730
1731/**
1732 * Adds an audio output stream to the device setup using the given configuration.
1733 *
1734 * @returns IPRT status code.
1735 * @param pThis Device state.
1736 * @param pCfg Stream configuration to use for adding a stream.
1737 */
1738static int hdaR3AddStreamOut(PHDASTATE pThis, PPDMAUDIOSTREAMCFG pCfg)
1739{
1740 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1741 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
1742
1743 AssertReturn(pCfg->enmDir == PDMAUDIODIR_OUT, VERR_INVALID_PARAMETER);
1744
1745 LogFlowFunc(("Stream=%s\n", pCfg->szName));
1746
1747 int rc = VINF_SUCCESS;
1748
1749 bool fUseFront = true; /* Always use front out by default. */
1750# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
1751 bool fUseRear;
1752 bool fUseCenter;
1753 bool fUseLFE;
1754
1755 fUseRear = fUseCenter = fUseLFE = false;
1756
1757 /*
1758 * Use commonly used setups for speaker configurations.
1759 */
1760
1761 /** @todo Make the following configurable through mixer API and/or CFGM? */
1762 switch (pCfg->Props.cChannels)
1763 {
1764 case 3: /* 2.1: Front (Stereo) + LFE. */
1765 {
1766 fUseLFE = true;
1767 break;
1768 }
1769
1770 case 4: /* Quadrophonic: Front (Stereo) + Rear (Stereo). */
1771 {
1772 fUseRear = true;
1773 break;
1774 }
1775
1776 case 5: /* 4.1: Front (Stereo) + Rear (Stereo) + LFE. */
1777 {
1778 fUseRear = true;
1779 fUseLFE = true;
1780 break;
1781 }
1782
1783 case 6: /* 5.1: Front (Stereo) + Rear (Stereo) + Center/LFE. */
1784 {
1785 fUseRear = true;
1786 fUseCenter = true;
1787 fUseLFE = true;
1788 break;
1789 }
1790
1791 default: /* Unknown; fall back to 2 front channels (stereo). */
1792 {
1793 rc = VERR_NOT_SUPPORTED;
1794 break;
1795 }
1796 }
1797# endif /* !VBOX_WITH_AUDIO_HDA_51_SURROUND */
1798
1799 if (rc == VERR_NOT_SUPPORTED)
1800 {
1801 LogRel2(("HDA: Warning: Unsupported channel count (%RU8), falling back to stereo channels (2)\n", pCfg->Props.cChannels));
1802
1803 /* Fall back to 2 channels (see below in fUseFront block). */
1804 rc = VINF_SUCCESS;
1805 }
1806
1807 do
1808 {
1809 if (RT_FAILURE(rc))
1810 break;
1811
1812 if (fUseFront)
1813 {
1814 RTStrPrintf(pCfg->szName, RT_ELEMENTS(pCfg->szName), "Front");
1815
1816 pCfg->u.enmDst = PDMAUDIOPLAYBACKDST_FRONT;
1817 pCfg->enmLayout = PDMAUDIOSTREAMLAYOUT_NON_INTERLEAVED;
1818
1819 pCfg->Props.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pCfg->Props.cbSample, pCfg->Props.cChannels);
1820
1821 rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_FRONT, pCfg);
1822 }
1823
1824# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
1825 if ( RT_SUCCESS(rc)
1826 && (fUseCenter || fUseLFE))
1827 {
1828 RTStrPrintf(pCfg->szName, RT_ELEMENTS(pCfg->szName), "Center/LFE");
1829
1830 pCfg->u.enmDst = PDMAUDIOPLAYBACKDST_CENTER_LFE;
1831 pCfg->enmLayout = PDMAUDIOSTREAMLAYOUT_NON_INTERLEAVED;
1832
1833 pCfg->Props.cChannels = (fUseCenter && fUseLFE) ? 2 : 1;
1834 pCfg->Props.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pCfg->Props.cbSample, pCfg->Props.cChannels);
1835
1836 rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_CENTER_LFE, pCfg);
1837 }
1838
1839 if ( RT_SUCCESS(rc)
1840 && fUseRear)
1841 {
1842 RTStrPrintf(pCfg->szName, RT_ELEMENTS(pCfg->szName), "Rear");
1843
1844 pCfg->u.enmDst = PDMAUDIOPLAYBACKDST_REAR;
1845 pCfg->enmLayout = PDMAUDIOSTREAMLAYOUT_NON_INTERLEAVED;
1846
1847 pCfg->Props.cChannels = 2;
1848 pCfg->Props.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pCfg->Props.cbSample, pCfg->Props.cChannels);
1849
1850 rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_REAR, pCfg);
1851 }
1852# endif /* VBOX_WITH_AUDIO_HDA_51_SURROUND */
1853
1854 } while (0);
1855
1856 LogFlowFuncLeaveRC(rc);
1857 return rc;
1858}
1859
1860/**
1861 * Adds an audio input stream to the device setup using the given configuration.
1862 *
1863 * @returns IPRT status code.
1864 * @param pThis Device state.
1865 * @param pCfg Stream configuration to use for adding a stream.
1866 */
1867static int hdaR3AddStreamIn(PHDASTATE pThis, PPDMAUDIOSTREAMCFG pCfg)
1868{
1869 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1870 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
1871
1872 AssertReturn(pCfg->enmDir == PDMAUDIODIR_IN, VERR_INVALID_PARAMETER);
1873
1874 LogFlowFunc(("Stream=%s, Source=%ld\n", pCfg->szName, pCfg->u.enmSrc));
1875
1876 int rc;
1877
1878 switch (pCfg->u.enmSrc)
1879 {
1880 case PDMAUDIORECSRC_LINE:
1881 {
1882 rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_LINE_IN, pCfg);
1883 break;
1884 }
1885# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
1886 case PDMAUDIORECSRC_MIC:
1887 {
1888 rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_MIC_IN, pCfg);
1889 break;
1890 }
1891# endif
1892 default:
1893 rc = VERR_NOT_SUPPORTED;
1894 break;
1895 }
1896
1897 LogFlowFuncLeaveRC(rc);
1898 return rc;
1899}
1900
1901/**
1902 * Adds an audio stream to the device setup using the given configuration.
1903 *
1904 * @returns IPRT status code.
1905 * @param pThis Device state.
1906 * @param pCfg Stream configuration to use for adding a stream.
1907 */
1908static int hdaR3AddStream(PHDASTATE pThis, PPDMAUDIOSTREAMCFG pCfg)
1909{
1910 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1911 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
1912
1913 int rc;
1914
1915 LogFlowFuncEnter();
1916
1917 switch (pCfg->enmDir)
1918 {
1919 case PDMAUDIODIR_OUT:
1920 rc = hdaR3AddStreamOut(pThis, pCfg);
1921 break;
1922
1923 case PDMAUDIODIR_IN:
1924 rc = hdaR3AddStreamIn(pThis, pCfg);
1925 break;
1926
1927 default:
1928 rc = VERR_NOT_SUPPORTED;
1929 AssertFailed();
1930 break;
1931 }
1932
1933 LogFlowFunc(("Returning %Rrc\n", rc));
1934
1935 return rc;
1936}
1937
1938/**
1939 * Removes an audio stream from the device setup using the given configuration.
1940 *
1941 * @returns IPRT status code.
1942 * @param pThis Device state.
1943 * @param pCfg Stream configuration to use for removing a stream.
1944 */
1945static int hdaR3RemoveStream(PHDASTATE pThis, PPDMAUDIOSTREAMCFG pCfg)
1946{
1947 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1948 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
1949
1950 int rc = VINF_SUCCESS;
1951
1952 PDMAUDIOMIXERCTL enmMixerCtl = PDMAUDIOMIXERCTL_UNKNOWN;
1953 switch (pCfg->enmDir)
1954 {
1955 case PDMAUDIODIR_IN:
1956 {
1957 LogFlowFunc(("Stream=%s, Source=%ld\n", pCfg->szName, pCfg->u.enmSrc));
1958
1959 switch (pCfg->u.enmSrc)
1960 {
1961 case PDMAUDIORECSRC_UNKNOWN: break;
1962 case PDMAUDIORECSRC_LINE: enmMixerCtl = PDMAUDIOMIXERCTL_LINE_IN; break;
1963# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
1964 case PDMAUDIORECSRC_MIC: enmMixerCtl = PDMAUDIOMIXERCTL_MIC_IN; break;
1965# endif
1966 default:
1967 rc = VERR_NOT_SUPPORTED;
1968 break;
1969 }
1970
1971 break;
1972 }
1973
1974 case PDMAUDIODIR_OUT:
1975 {
1976 LogFlowFunc(("Stream=%s, Source=%ld\n", pCfg->szName, pCfg->u.enmDst));
1977
1978 switch (pCfg->u.enmDst)
1979 {
1980 case PDMAUDIOPLAYBACKDST_UNKNOWN: break;
1981 case PDMAUDIOPLAYBACKDST_FRONT: enmMixerCtl = PDMAUDIOMIXERCTL_FRONT; break;
1982# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
1983 case PDMAUDIOPLAYBACKDST_CENTER_LFE: enmMixerCtl = PDMAUDIOMIXERCTL_CENTER_LFE; break;
1984 case PDMAUDIOPLAYBACKDST_REAR: enmMixerCtl = PDMAUDIOMIXERCTL_REAR; break;
1985# endif
1986 default:
1987 rc = VERR_NOT_SUPPORTED;
1988 break;
1989 }
1990 break;
1991 }
1992
1993 default:
1994 rc = VERR_NOT_SUPPORTED;
1995 break;
1996 }
1997
1998 if ( RT_SUCCESS(rc)
1999 && enmMixerCtl != PDMAUDIOMIXERCTL_UNKNOWN)
2000 {
2001 rc = hdaCodecRemoveStream(pThis->pCodec, enmMixerCtl);
2002 }
2003
2004 LogFlowFuncLeaveRC(rc);
2005 return rc;
2006}
2007#endif /* IN_RING3 */
2008
2009static int hdaRegWriteSDFMT(PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2010{
2011 DEVHDA_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_MMIO_WRITE);
2012
2013 /* Write the wanted stream format into the register in any case.
2014 *
2015 * This is important for e.g. MacOS guests, as those try to initialize streams which are not reported
2016 * by the device emulation (wants 4 channels, only have 2 channels at the moment).
2017 *
2018 * When ignoring those (invalid) formats, this leads to MacOS thinking that the device is malfunctioning
2019 * and therefore disabling the device completely. */
2020 int rc = hdaRegWriteU16(pDevIns, pThis, iReg, u32Value);
2021 AssertRC(rc);
2022
2023 DEVHDA_UNLOCK(pDevIns, pThis);
2024 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
2025}
2026
2027/* Note: Will be called for both, BDPL and BDPU, registers. */
2028DECLINLINE(int) hdaRegWriteSDBDPX(PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t iReg, uint32_t u32Value, uint8_t uSD)
2029{
2030#ifdef IN_RING3
2031 DEVHDA_LOCK(pDevIns, pThis);
2032
2033# ifdef HDA_USE_DMA_ACCESS_HANDLER
2034 if (hdaGetDirFromSD(uSD) == PDMAUDIODIR_OUT)
2035 {
2036 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, uSD);
2037
2038 /* Try registering the DMA handlers.
2039 * As we can't be sure in which order LVI + BDL base are set, try registering in both routines. */
2040 if ( pStream
2041 && hdaR3StreamRegisterDMAHandlers(pThis, pStream))
2042 {
2043 LogFunc(("[SD%RU8] DMA logging enabled\n", pStream->u8SD));
2044 }
2045 }
2046# else
2047 RT_NOREF(uSD);
2048# endif
2049
2050 int rc2 = hdaRegWriteU32(pDevIns, pThis, iReg, u32Value);
2051 AssertRC(rc2);
2052
2053 DEVHDA_UNLOCK(pDevIns, pThis);
2054 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
2055#else /* !IN_RING3 */
2056 RT_NOREF(pDevIns, pThis, iReg, u32Value, uSD);
2057 return VINF_IOM_R3_MMIO_WRITE;
2058#endif /* !IN_RING3 */
2059}
2060
2061static int hdaRegWriteSDBDPL(PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2062{
2063 return hdaRegWriteSDBDPX(pDevIns, pThis, iReg, u32Value, HDA_SD_NUM_FROM_REG(pThis, BDPL, iReg));
2064}
2065
2066static int hdaRegWriteSDBDPU(PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2067{
2068 return hdaRegWriteSDBDPX(pDevIns, pThis, iReg, u32Value, HDA_SD_NUM_FROM_REG(pThis, BDPU, iReg));
2069}
2070
2071static int hdaRegReadIRS(PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
2072{
2073 DEVHDA_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_MMIO_READ);
2074
2075 /* regarding 3.4.3 we should mark IRS as busy in case CORB is active */
2076 if ( HDA_REG(pThis, CORBWP) != HDA_REG(pThis, CORBRP)
2077 || (HDA_REG(pThis, CORBCTL) & HDA_CORBCTL_DMA))
2078 {
2079 HDA_REG(pThis, IRS) = HDA_IRS_ICB; /* busy */
2080 }
2081
2082 int rc = hdaRegReadU32(pDevIns, pThis, iReg, pu32Value);
2083 DEVHDA_UNLOCK(pDevIns, pThis);
2084
2085 return rc;
2086}
2087
2088static int hdaRegWriteIRS(PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2089{
2090 RT_NOREF_PV(iReg);
2091 DEVHDA_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_MMIO_WRITE);
2092
2093 /*
2094 * If the guest set the ICB bit of IRS register, HDA should process the verb in IC register,
2095 * write the response to IR register, and set the IRV (valid in case of success) bit of IRS register.
2096 */
2097 if ( (u32Value & HDA_IRS_ICB)
2098 && !(HDA_REG(pThis, IRS) & HDA_IRS_ICB))
2099 {
2100#ifdef IN_RING3
2101 uint32_t uCmd = HDA_REG(pThis, IC);
2102
2103 if (HDA_REG(pThis, CORBWP) != HDA_REG(pThis, CORBRP))
2104 {
2105 DEVHDA_UNLOCK(pDevIns, pThis);
2106
2107 /*
2108 * 3.4.3: Defines behavior of immediate Command status register.
2109 */
2110 LogRel(("HDA: Guest attempted process immediate verb (%x) with active CORB\n", uCmd));
2111 return VINF_SUCCESS;
2112 }
2113
2114 HDA_REG(pThis, IRS) = HDA_IRS_ICB; /* busy */
2115
2116 uint64_t uResp;
2117 int rc2 = pThis->pCodec->pfnLookup(pThis->pCodec,
2118 HDA_CODEC_CMD(uCmd, 0 /* LUN */), &uResp);
2119 if (RT_FAILURE(rc2))
2120 LogFunc(("Codec lookup failed with rc2=%Rrc\n", rc2));
2121
2122 HDA_REG(pThis, IR) = (uint32_t)uResp; /** @todo r=andy Do we need a 64-bit response? */
2123 HDA_REG(pThis, IRS) = HDA_IRS_IRV; /* result is ready */
2124 /** @todo r=michaln We just set the IRS value, why are we clearing unset bits? */
2125 HDA_REG(pThis, IRS) &= ~HDA_IRS_ICB; /* busy is clear */
2126
2127 DEVHDA_UNLOCK(pDevIns, pThis);
2128 return VINF_SUCCESS;
2129#else /* !IN_RING3 */
2130 DEVHDA_UNLOCK(pDevIns, pThis);
2131 return VINF_IOM_R3_MMIO_WRITE;
2132#endif /* !IN_RING3 */
2133 }
2134
2135 /*
2136 * Once the guest read the response, it should clear the IRV bit of the IRS register.
2137 */
2138 HDA_REG(pThis, IRS) &= ~(u32Value & HDA_IRS_IRV);
2139
2140 DEVHDA_UNLOCK(pDevIns, pThis);
2141 return VINF_SUCCESS;
2142}
2143
2144static int hdaRegWriteRIRBWP(PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2145{
2146 RT_NOREF(iReg);
2147 DEVHDA_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_MMIO_WRITE);
2148
2149 if (HDA_REG(pThis, CORBCTL) & HDA_CORBCTL_DMA) /* Ignore request if CORB DMA engine is (still) running. */
2150 {
2151 LogFunc(("CORB DMA (still) running, skipping\n"));
2152
2153 DEVHDA_UNLOCK(pDevIns, pThis);
2154 return VINF_SUCCESS;
2155 }
2156
2157 if (u32Value & HDA_RIRBWP_RST)
2158 {
2159 /* Do a RIRB reset. */
2160 if (pThis->cbRirbBuf)
2161 {
2162 Assert(pThis->pu64RirbBuf);
2163 RT_BZERO((void *)pThis->pu64RirbBuf, pThis->cbRirbBuf);
2164 }
2165
2166 LogRel2(("HDA: RIRB reset\n"));
2167
2168 HDA_REG(pThis, RIRBWP) = 0;
2169 }
2170
2171 /* The remaining bits are O, see 6.2.22. */
2172
2173 DEVHDA_UNLOCK(pDevIns, pThis);
2174 return VINF_SUCCESS;
2175}
2176
2177static int hdaRegWriteRINTCNT(PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2178{
2179 DEVHDA_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_MMIO_WRITE);
2180
2181 if (HDA_REG(pThis, CORBCTL) & HDA_CORBCTL_DMA) /* Ignore request if CORB DMA engine is (still) running. */
2182 {
2183 LogFunc(("CORB DMA is (still) running, skipping\n"));
2184
2185 DEVHDA_UNLOCK(pDevIns, pThis);
2186 return VINF_SUCCESS;
2187 }
2188
2189 int rc = hdaRegWriteU16(pDevIns, pThis, iReg, u32Value);
2190 AssertRC(rc);
2191
2192 LogFunc(("Response interrupt count is now %RU8\n", HDA_REG(pThis, RINTCNT) & 0xFF));
2193
2194 DEVHDA_UNLOCK(pDevIns, pThis);
2195 return rc;
2196}
2197
2198static int hdaRegWriteBase(PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2199{
2200 uint32_t iRegMem = g_aHdaRegMap[iReg].mem_idx;
2201 DEVHDA_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_MMIO_WRITE);
2202
2203 int rc = hdaRegWriteU32(pDevIns, pThis, iReg, u32Value);
2204 AssertRCSuccess(rc);
2205
2206 switch (iReg)
2207 {
2208 case HDA_REG_CORBLBASE:
2209 pThis->u64CORBBase &= UINT64_C(0xFFFFFFFF00000000);
2210 pThis->u64CORBBase |= pThis->au32Regs[iRegMem];
2211 break;
2212 case HDA_REG_CORBUBASE:
2213 pThis->u64CORBBase &= UINT64_C(0x00000000FFFFFFFF);
2214 pThis->u64CORBBase |= ((uint64_t)pThis->au32Regs[iRegMem] << 32);
2215 break;
2216 case HDA_REG_RIRBLBASE:
2217 pThis->u64RIRBBase &= UINT64_C(0xFFFFFFFF00000000);
2218 pThis->u64RIRBBase |= pThis->au32Regs[iRegMem];
2219 break;
2220 case HDA_REG_RIRBUBASE:
2221 pThis->u64RIRBBase &= UINT64_C(0x00000000FFFFFFFF);
2222 pThis->u64RIRBBase |= ((uint64_t)pThis->au32Regs[iRegMem] << 32);
2223 break;
2224 case HDA_REG_DPLBASE:
2225 {
2226 pThis->u64DPBase = pThis->au32Regs[iRegMem] & DPBASE_ADDR_MASK;
2227 Assert(pThis->u64DPBase % 128 == 0); /* Must be 128-byte aligned. */
2228
2229 /* Also make sure to handle the DMA position enable bit. */
2230 pThis->fDMAPosition = pThis->au32Regs[iRegMem] & RT_BIT_32(0);
2231 LogRel(("HDA: %s DMA position buffer\n", pThis->fDMAPosition ? "Enabled" : "Disabled"));
2232 break;
2233 }
2234 case HDA_REG_DPUBASE:
2235 pThis->u64DPBase = RT_MAKE_U64(RT_LO_U32(pThis->u64DPBase) & DPBASE_ADDR_MASK, pThis->au32Regs[iRegMem]);
2236 break;
2237 default:
2238 AssertMsgFailed(("Invalid index\n"));
2239 break;
2240 }
2241
2242 LogFunc(("CORB base:%llx RIRB base: %llx DP base: %llx\n",
2243 pThis->u64CORBBase, pThis->u64RIRBBase, pThis->u64DPBase));
2244
2245 DEVHDA_UNLOCK(pDevIns, pThis);
2246 return rc;
2247}
2248
2249static int hdaRegWriteRIRBSTS(PPDMDEVINS pDevIns, PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2250{
2251 RT_NOREF_PV(iReg);
2252 DEVHDA_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_MMIO_WRITE);
2253
2254 uint8_t v = HDA_REG(pThis, RIRBSTS);
2255 HDA_REG(pThis, RIRBSTS) &= ~(v & u32Value);
2256
2257 int rc = HDA_PROCESS_INTERRUPT(pDevIns, pThis);
2258
2259 DEVHDA_UNLOCK(pDevIns, pThis);
2260 return rc;
2261}
2262
2263#ifdef IN_RING3
2264
2265/**
2266 * Retrieves a corresponding sink for a given mixer control.
2267 * Returns NULL if no sink is found.
2268 *
2269 * @return PHDAMIXERSINK
2270 * @param pThis HDA state.
2271 * @param enmMixerCtl Mixer control to get the corresponding sink for.
2272 */
2273static PHDAMIXERSINK hdaR3MixerControlToSink(PHDASTATE pThis, PDMAUDIOMIXERCTL enmMixerCtl)
2274{
2275 PHDAMIXERSINK pSink;
2276
2277 switch (enmMixerCtl)
2278 {
2279 case PDMAUDIOMIXERCTL_VOLUME_MASTER:
2280 /* Fall through is intentional. */
2281 case PDMAUDIOMIXERCTL_FRONT:
2282 pSink = &pThis->SinkFront;
2283 break;
2284# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
2285 case PDMAUDIOMIXERCTL_CENTER_LFE:
2286 pSink = &pThis->SinkCenterLFE;
2287 break;
2288 case PDMAUDIOMIXERCTL_REAR:
2289 pSink = &pThis->SinkRear;
2290 break;
2291# endif
2292 case PDMAUDIOMIXERCTL_LINE_IN:
2293 pSink = &pThis->SinkLineIn;
2294 break;
2295# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
2296 case PDMAUDIOMIXERCTL_MIC_IN:
2297 pSink = &pThis->SinkMicIn;
2298 break;
2299# endif
2300 default:
2301 pSink = NULL;
2302 AssertMsgFailed(("Unhandled mixer control\n"));
2303 break;
2304 }
2305
2306 return pSink;
2307}
2308
2309/**
2310 * Adds a specific HDA driver to the driver chain.
2311 *
2312 * @return IPRT status code.
2313 * @param pThis HDA state.
2314 * @param pDrv HDA driver to add.
2315 */
2316static int hdaR3MixerAddDrv(PHDASTATE pThis, PHDADRIVER pDrv)
2317{
2318 int rc = VINF_SUCCESS;
2319
2320 PHDASTREAM pStream = hdaR3GetStreamFromSink(pThis, &pThis->SinkLineIn);
2321 if ( pStream
2322 && DrvAudioHlpStreamCfgIsValid(&pStream->State.Cfg))
2323 {
2324 int rc2 = hdaR3MixerAddDrvStream(pThis, pThis->SinkLineIn.pMixSink, &pStream->State.Cfg, pDrv);
2325 if (RT_SUCCESS(rc))
2326 rc = rc2;
2327 }
2328
2329# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
2330 pStream = hdaR3GetStreamFromSink(pThis, &pThis->SinkMicIn);
2331 if ( pStream
2332 && DrvAudioHlpStreamCfgIsValid(&pStream->State.Cfg))
2333 {
2334 int rc2 = hdaR3MixerAddDrvStream(pThis, pThis->SinkMicIn.pMixSink, &pStream->State.Cfg, pDrv);
2335 if (RT_SUCCESS(rc))
2336 rc = rc2;
2337 }
2338# endif
2339
2340 pStream = hdaR3GetStreamFromSink(pThis, &pThis->SinkFront);
2341 if ( pStream
2342 && DrvAudioHlpStreamCfgIsValid(&pStream->State.Cfg))
2343 {
2344 int rc2 = hdaR3MixerAddDrvStream(pThis, pThis->SinkFront.pMixSink, &pStream->State.Cfg, pDrv);
2345 if (RT_SUCCESS(rc))
2346 rc = rc2;
2347 }
2348
2349# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
2350 pStream = hdaR3GetStreamFromSink(pThis, &pThis->SinkCenterLFE);
2351 if ( pStream
2352 && DrvAudioHlpStreamCfgIsValid(&pStream->State.Cfg))
2353 {
2354 int rc2 = hdaR3MixerAddDrvStream(pThis, pThis->SinkCenterLFE.pMixSink, &pStream->State.Cfg, pDrv);
2355 if (RT_SUCCESS(rc))
2356 rc = rc2;
2357 }
2358
2359 pStream = hdaR3GetStreamFromSink(pThis, &pThis->SinkRear);
2360 if ( pStream
2361 && DrvAudioHlpStreamCfgIsValid(&pStream->State.Cfg))
2362 {
2363 int rc2 = hdaR3MixerAddDrvStream(pThis, pThis->SinkRear.pMixSink, &pStream->State.Cfg, pDrv);
2364 if (RT_SUCCESS(rc))
2365 rc = rc2;
2366 }
2367# endif
2368
2369 return rc;
2370}
2371
2372/**
2373 * Removes a specific HDA driver from the driver chain and destroys its
2374 * associated streams.
2375 *
2376 * @param pThis HDA state.
2377 * @param pDrv HDA driver to remove.
2378 */
2379static void hdaR3MixerRemoveDrv(PHDASTATE pThis, PHDADRIVER pDrv)
2380{
2381 AssertPtrReturnVoid(pThis);
2382 AssertPtrReturnVoid(pDrv);
2383
2384 if (pDrv->LineIn.pMixStrm)
2385 {
2386 if (AudioMixerSinkGetRecordingSource(pThis->SinkLineIn.pMixSink) == pDrv->LineIn.pMixStrm)
2387 AudioMixerSinkSetRecordingSource(pThis->SinkLineIn.pMixSink, NULL);
2388
2389 AudioMixerSinkRemoveStream(pThis->SinkLineIn.pMixSink, pDrv->LineIn.pMixStrm);
2390 AudioMixerStreamDestroy(pDrv->LineIn.pMixStrm);
2391 pDrv->LineIn.pMixStrm = NULL;
2392 }
2393
2394# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
2395 if (pDrv->MicIn.pMixStrm)
2396 {
2397 if (AudioMixerSinkGetRecordingSource(pThis->SinkMicIn.pMixSink) == pDrv->MicIn.pMixStrm)
2398 AudioMixerSinkSetRecordingSource(&pThis->SinkMicIn.pMixSink, NULL);
2399
2400 AudioMixerSinkRemoveStream(pThis->SinkMicIn.pMixSink, pDrv->MicIn.pMixStrm);
2401 AudioMixerStreamDestroy(pDrv->MicIn.pMixStrm);
2402 pDrv->MicIn.pMixStrm = NULL;
2403 }
2404# endif
2405
2406 if (pDrv->Front.pMixStrm)
2407 {
2408 AudioMixerSinkRemoveStream(pThis->SinkFront.pMixSink, pDrv->Front.pMixStrm);
2409 AudioMixerStreamDestroy(pDrv->Front.pMixStrm);
2410 pDrv->Front.pMixStrm = NULL;
2411 }
2412
2413# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
2414 if (pDrv->CenterLFE.pMixStrm)
2415 {
2416 AudioMixerSinkRemoveStream(pThis->SinkCenterLFE.pMixSink, pDrv->CenterLFE.pMixStrm);
2417 AudioMixerStreamDestroy(pDrv->CenterLFE.pMixStrm);
2418 pDrv->CenterLFE.pMixStrm = NULL;
2419 }
2420
2421 if (pDrv->Rear.pMixStrm)
2422 {
2423 AudioMixerSinkRemoveStream(pThis->SinkRear.pMixSink, pDrv->Rear.pMixStrm);
2424 AudioMixerStreamDestroy(pDrv->Rear.pMixStrm);
2425 pDrv->Rear.pMixStrm = NULL;
2426 }
2427# endif
2428
2429 RTListNodeRemove(&pDrv->Node);
2430}
2431
2432/**
2433 * Adds a driver stream to a specific mixer sink.
2434 *
2435 * @returns IPRT status code (ignored by caller).
2436 * @param pThis HDA state.
2437 * @param pMixSink Audio mixer sink to add audio streams to.
2438 * @param pCfg Audio stream configuration to use for the audio streams to add.
2439 * @param pDrv Driver stream to add.
2440 */
2441static int hdaR3MixerAddDrvStream(PHDASTATE pThis, PAUDMIXSINK pMixSink, PPDMAUDIOSTREAMCFG pCfg, PHDADRIVER pDrv)
2442{
2443 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
2444 AssertPtrReturn(pMixSink, VERR_INVALID_POINTER);
2445 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
2446
2447 LogFunc(("szSink=%s, szStream=%s, cChannels=%RU8\n", pMixSink->pszName, pCfg->szName, pCfg->Props.cChannels));
2448
2449 PPDMAUDIOSTREAMCFG pStreamCfg = DrvAudioHlpStreamCfgDup(pCfg);
2450 if (!pStreamCfg)
2451 return VERR_NO_MEMORY;
2452
2453 LogFunc(("[LUN#%RU8] %s\n", pDrv->uLUN, pStreamCfg->szName));
2454
2455 int rc = VINF_SUCCESS;
2456
2457 PHDADRIVERSTREAM pDrvStream = NULL;
2458
2459 if (pStreamCfg->enmDir == PDMAUDIODIR_IN)
2460 {
2461 LogFunc(("enmRecSource=%d\n", pStreamCfg->u.enmSrc));
2462
2463 switch (pStreamCfg->u.enmSrc)
2464 {
2465 case PDMAUDIORECSRC_LINE:
2466 pDrvStream = &pDrv->LineIn;
2467 break;
2468# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
2469 case PDMAUDIORECSRC_MIC:
2470 pDrvStream = &pDrv->MicIn;
2471 break;
2472# endif
2473 default:
2474 rc = VERR_NOT_SUPPORTED;
2475 break;
2476 }
2477 }
2478 else if (pStreamCfg->enmDir == PDMAUDIODIR_OUT)
2479 {
2480 LogFunc(("enmPlaybackDest=%d\n", pStreamCfg->u.enmDst));
2481
2482 switch (pStreamCfg->u.enmDst)
2483 {
2484 case PDMAUDIOPLAYBACKDST_FRONT:
2485 pDrvStream = &pDrv->Front;
2486 break;
2487# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
2488 case PDMAUDIOPLAYBACKDST_CENTER_LFE:
2489 pDrvStream = &pDrv->CenterLFE;
2490 break;
2491 case PDMAUDIOPLAYBACKDST_REAR:
2492 pDrvStream = &pDrv->Rear;
2493 break;
2494# endif
2495 default:
2496 rc = VERR_NOT_SUPPORTED;
2497 break;
2498 }
2499 }
2500 else
2501 rc = VERR_NOT_SUPPORTED;
2502
2503 if (RT_SUCCESS(rc))
2504 {
2505 AssertPtr(pDrvStream);
2506 AssertMsg(pDrvStream->pMixStrm == NULL, ("[LUN#%RU8] Driver stream already present when it must not\n", pDrv->uLUN));
2507
2508 PAUDMIXSTREAM pMixStrm;
2509 rc = AudioMixerSinkCreateStream(pMixSink, pDrv->pConnector, pStreamCfg, 0 /* fFlags */, &pMixStrm);
2510 LogFlowFunc(("LUN#%RU8: Created stream \"%s\" for sink, rc=%Rrc\n", pDrv->uLUN, pStreamCfg->szName, rc));
2511 if (RT_SUCCESS(rc))
2512 {
2513 rc = AudioMixerSinkAddStream(pMixSink, pMixStrm);
2514 LogFlowFunc(("LUN#%RU8: Added stream \"%s\" to sink, rc=%Rrc\n", pDrv->uLUN, pStreamCfg->szName, rc));
2515 if (RT_SUCCESS(rc))
2516 {
2517 /* If this is an input stream, always set the latest (added) stream
2518 * as the recording source.
2519 * @todo Make the recording source dynamic (CFGM?). */
2520 if (pStreamCfg->enmDir == PDMAUDIODIR_IN)
2521 {
2522 PDMAUDIOBACKENDCFG Cfg;
2523 rc = pDrv->pConnector->pfnGetConfig(pDrv->pConnector, &Cfg);
2524 if (RT_SUCCESS(rc))
2525 {
2526 if (Cfg.cMaxStreamsIn) /* At least one input source available? */
2527 {
2528 rc = AudioMixerSinkSetRecordingSource(pMixSink, pMixStrm);
2529 LogFlowFunc(("LUN#%RU8: Recording source for '%s' -> '%s', rc=%Rrc\n",
2530 pDrv->uLUN, pStreamCfg->szName, Cfg.szName, rc));
2531
2532 if (RT_SUCCESS(rc))
2533 LogRel(("HDA: Set recording source for '%s' to '%s'\n",
2534 pStreamCfg->szName, Cfg.szName));
2535 }
2536 else
2537 LogRel(("HDA: Backend '%s' currently is not offering any recording source for '%s'\n",
2538 Cfg.szName, pStreamCfg->szName));
2539 }
2540 else if (RT_FAILURE(rc))
2541 LogFunc(("LUN#%RU8: Unable to retrieve backend configuration for '%s', rc=%Rrc\n",
2542 pDrv->uLUN, pStreamCfg->szName, rc));
2543 }
2544 }
2545 }
2546
2547 if (RT_SUCCESS(rc))
2548 pDrvStream->pMixStrm = pMixStrm;
2549 }
2550
2551 DrvAudioHlpStreamCfgFree(pStreamCfg);
2552
2553 LogFlowFuncLeaveRC(rc);
2554 return rc;
2555}
2556
2557/**
2558 * Adds all current driver streams to a specific mixer sink.
2559 *
2560 * @returns IPRT status code.
2561 * @param pThis HDA state.
2562 * @param pMixSink Audio mixer sink to add stream to.
2563 * @param pCfg Audio stream configuration to use for the audio streams to add.
2564 */
2565static int hdaR3MixerAddDrvStreams(PHDASTATE pThis, PAUDMIXSINK pMixSink, PPDMAUDIOSTREAMCFG pCfg)
2566{
2567 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
2568 AssertPtrReturn(pMixSink, VERR_INVALID_POINTER);
2569 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
2570
2571 LogFunc(("Sink=%s, Stream=%s\n", pMixSink->pszName, pCfg->szName));
2572
2573 if (!DrvAudioHlpStreamCfgIsValid(pCfg))
2574 return VERR_INVALID_PARAMETER;
2575
2576 int rc = AudioMixerSinkSetFormat(pMixSink, &pCfg->Props);
2577 if (RT_FAILURE(rc))
2578 return rc;
2579
2580 PHDADRIVER pDrv;
2581 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
2582 {
2583 int rc2 = hdaR3MixerAddDrvStream(pThis, pMixSink, pCfg, pDrv);
2584 if (RT_FAILURE(rc2))
2585 LogFunc(("Attaching stream failed with %Rrc\n", rc2));
2586
2587 /* Do not pass failure to rc here, as there might be drivers which aren't
2588 * configured / ready yet. */
2589 }
2590
2591 return rc;
2592}
2593
2594/**
2595 * @interface_method_impl{HDACODEC,pfnCbMixerAddStream}
2596 *
2597 * Adds a new audio stream to a specific mixer control.
2598 *
2599 * Depending on the mixer control the stream then gets assigned to one of the internal
2600 * mixer sinks, which in turn then handle the mixing of all connected streams to that sink.
2601 *
2602 * @return IPRT status code.
2603 * @param pThis HDA state.
2604 * @param enmMixerCtl Mixer control to assign new stream to.
2605 * @param pCfg Stream configuration for the new stream.
2606 */
2607static DECLCALLBACK(int) hdaR3MixerAddStream(PHDASTATE pThis, PDMAUDIOMIXERCTL enmMixerCtl, PPDMAUDIOSTREAMCFG pCfg)
2608{
2609 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
2610 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
2611
2612 int rc;
2613
2614 PHDAMIXERSINK pSink = hdaR3MixerControlToSink(pThis, enmMixerCtl);
2615 if (pSink)
2616 {
2617 rc = hdaR3MixerAddDrvStreams(pThis, pSink->pMixSink, pCfg);
2618
2619 AssertPtr(pSink->pMixSink);
2620 LogFlowFunc(("Sink=%s, Mixer control=%s\n", pSink->pMixSink->pszName, DrvAudioHlpAudMixerCtlToStr(enmMixerCtl)));
2621 }
2622 else
2623 rc = VERR_NOT_FOUND;
2624
2625 LogFlowFuncLeaveRC(rc);
2626 return rc;
2627}
2628
2629/**
2630 * @interface_method_impl{HDACODEC,pfnCbMixerRemoveStream}
2631 *
2632 * Removes a specified mixer control from the HDA's mixer.
2633 *
2634 * @return IPRT status code.
2635 * @param pThis HDA state.
2636 * @param enmMixerCtl Mixer control to remove.
2637 *
2638 * @remarks Can be called as a callback by the HDA codec.
2639 */
2640static DECLCALLBACK(int) hdaR3MixerRemoveStream(PHDASTATE pThis, PDMAUDIOMIXERCTL enmMixerCtl)
2641{
2642 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
2643
2644 int rc;
2645
2646 PHDAMIXERSINK pSink = hdaR3MixerControlToSink(pThis, enmMixerCtl);
2647 if (pSink)
2648 {
2649 PHDADRIVER pDrv;
2650 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
2651 {
2652 PAUDMIXSTREAM pMixStream = NULL;
2653 switch (enmMixerCtl)
2654 {
2655 /*
2656 * Input.
2657 */
2658 case PDMAUDIOMIXERCTL_LINE_IN:
2659 pMixStream = pDrv->LineIn.pMixStrm;
2660 pDrv->LineIn.pMixStrm = NULL;
2661 break;
2662# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
2663 case PDMAUDIOMIXERCTL_MIC_IN:
2664 pMixStream = pDrv->MicIn.pMixStrm;
2665 pDrv->MicIn.pMixStrm = NULL;
2666 break;
2667# endif
2668 /*
2669 * Output.
2670 */
2671 case PDMAUDIOMIXERCTL_FRONT:
2672 pMixStream = pDrv->Front.pMixStrm;
2673 pDrv->Front.pMixStrm = NULL;
2674 break;
2675# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
2676 case PDMAUDIOMIXERCTL_CENTER_LFE:
2677 pMixStream = pDrv->CenterLFE.pMixStrm;
2678 pDrv->CenterLFE.pMixStrm = NULL;
2679 break;
2680 case PDMAUDIOMIXERCTL_REAR:
2681 pMixStream = pDrv->Rear.pMixStrm;
2682 pDrv->Rear.pMixStrm = NULL;
2683 break;
2684# endif
2685 default:
2686 AssertMsgFailed(("Mixer control %d not implemented\n", enmMixerCtl));
2687 break;
2688 }
2689
2690 if (pMixStream)
2691 {
2692 AudioMixerSinkRemoveStream(pSink->pMixSink, pMixStream);
2693 AudioMixerStreamDestroy(pMixStream);
2694
2695 pMixStream = NULL;
2696 }
2697 }
2698
2699 AudioMixerSinkRemoveAllStreams(pSink->pMixSink);
2700 rc = VINF_SUCCESS;
2701 }
2702 else
2703 rc = VERR_NOT_FOUND;
2704
2705 LogFunc(("Mixer control=%s, rc=%Rrc\n", DrvAudioHlpAudMixerCtlToStr(enmMixerCtl), rc));
2706 return rc;
2707}
2708
2709/**
2710 * @interface_method_impl{HDACODEC,pfnCbMixerControl}
2711 *
2712 * Controls an input / output converter widget, that is, which converter is connected
2713 * to which stream (and channel).
2714 *
2715 * @returns IPRT status code.
2716 * @param pThis HDA State.
2717 * @param enmMixerCtl Mixer control to set SD stream number and channel for.
2718 * @param uSD SD stream number (number + 1) to set. Set to 0 for unassign.
2719 * @param uChannel Channel to set. Only valid if a valid SD stream number is specified.
2720 *
2721 * @remarks Can be called as a callback by the HDA codec.
2722 */
2723static DECLCALLBACK(int) hdaR3MixerControl(PHDASTATE pThis, PDMAUDIOMIXERCTL enmMixerCtl, uint8_t uSD, uint8_t uChannel)
2724{
2725 LogFunc(("enmMixerCtl=%s, uSD=%RU8, uChannel=%RU8\n", DrvAudioHlpAudMixerCtlToStr(enmMixerCtl), uSD, uChannel));
2726
2727 if (uSD == 0) /* Stream number 0 is reserved. */
2728 {
2729 Log2Func(("Invalid SDn (%RU8) number for mixer control '%s', ignoring\n", uSD, DrvAudioHlpAudMixerCtlToStr(enmMixerCtl)));
2730 return VINF_SUCCESS;
2731 }
2732 /* uChannel is optional. */
2733
2734 /* SDn0 starts as 1. */
2735 Assert(uSD);
2736 uSD--;
2737
2738# ifndef VBOX_WITH_AUDIO_HDA_MIC_IN
2739 /* Only SDI0 (Line-In) is supported. */
2740 if ( hdaGetDirFromSD(uSD) == PDMAUDIODIR_IN
2741 && uSD >= 1)
2742 {
2743 LogRel2(("HDA: Dedicated Mic-In support not imlpemented / built-in (stream #%RU8), using Line-In (stream #0) instead\n", uSD));
2744 uSD = 0;
2745 }
2746# endif
2747
2748 int rc = VINF_SUCCESS;
2749
2750 PHDAMIXERSINK pSink = hdaR3MixerControlToSink(pThis, enmMixerCtl);
2751 if (pSink)
2752 {
2753 AssertPtr(pSink->pMixSink);
2754
2755 /* If this an output stream, determine the correct SD#. */
2756 if ( (uSD < HDA_MAX_SDI)
2757 && AudioMixerSinkGetDir(pSink->pMixSink) == AUDMIXSINKDIR_OUTPUT)
2758 {
2759 uSD += HDA_MAX_SDI;
2760 }
2761
2762 /* Detach the existing stream from the sink. */
2763 if ( pSink->pStream
2764 && ( pSink->pStream->u8SD != uSD
2765 || pSink->pStream->u8Channel != uChannel)
2766 )
2767 {
2768 LogFunc(("Sink '%s' was assigned to stream #%RU8 (channel %RU8) before\n",
2769 pSink->pMixSink->pszName, pSink->pStream->u8SD, pSink->pStream->u8Channel));
2770
2771 hdaR3StreamLock(pSink->pStream);
2772
2773 /* Only disable the stream if the stream descriptor # has changed. */
2774 if (pSink->pStream->u8SD != uSD)
2775 hdaR3StreamEnable(pSink->pStream, false);
2776
2777 pSink->pStream->pMixSink = NULL;
2778
2779 hdaR3StreamUnlock(pSink->pStream);
2780
2781 pSink->pStream = NULL;
2782 }
2783
2784 Assert(uSD < HDA_MAX_STREAMS);
2785
2786 /* Attach the new stream to the sink.
2787 * Enabling the stream will be done by the gust via a separate SDnCTL call then. */
2788 if (pSink->pStream == NULL)
2789 {
2790 LogRel2(("HDA: Setting sink '%s' to stream #%RU8 (channel %RU8), mixer control=%s\n",
2791 pSink->pMixSink->pszName, uSD, uChannel, DrvAudioHlpAudMixerCtlToStr(enmMixerCtl)));
2792
2793 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, uSD);
2794 if (pStream)
2795 {
2796 hdaR3StreamLock(pStream);
2797
2798 pSink->pStream = pStream;
2799
2800 pStream->u8Channel = uChannel;
2801 pStream->pMixSink = pSink;
2802
2803 hdaR3StreamUnlock(pStream);
2804
2805 rc = VINF_SUCCESS;
2806 }
2807 else
2808 rc = VERR_NOT_IMPLEMENTED;
2809 }
2810 }
2811 else
2812 rc = VERR_NOT_FOUND;
2813
2814 if (RT_FAILURE(rc))
2815 LogRel(("HDA: Converter control for stream #%RU8 (channel %RU8) / mixer control '%s' failed with %Rrc, skipping\n",
2816 uSD, uChannel, DrvAudioHlpAudMixerCtlToStr(enmMixerCtl), rc));
2817
2818 LogFlowFuncLeaveRC(rc);
2819 return rc;
2820}
2821
2822/**
2823 * @interface_method_impl{HDACODEC,pfnCbMixerSetVolume}
2824 *
2825 * Sets the volume of a specified mixer control.
2826 *
2827 * @return IPRT status code.
2828 * @param pThis HDA State.
2829 * @param enmMixerCtl Mixer control to set volume for.
2830 * @param pVol Pointer to volume data to set.
2831 *
2832 * @remarks Can be called as a callback by the HDA codec.
2833 */
2834static DECLCALLBACK(int) hdaR3MixerSetVolume(PHDASTATE pThis, PDMAUDIOMIXERCTL enmMixerCtl, PPDMAUDIOVOLUME pVol)
2835{
2836 int rc;
2837
2838 PHDAMIXERSINK pSink = hdaR3MixerControlToSink(pThis, enmMixerCtl);
2839 if ( pSink
2840 && pSink->pMixSink)
2841 {
2842 LogRel2(("HDA: Setting volume for mixer sink '%s' to %RU8/%RU8 (%s)\n",
2843 pSink->pMixSink->pszName, pVol->uLeft, pVol->uRight, pVol->fMuted ? "Muted" : "Unmuted"));
2844
2845 /* Set the volume.
2846 * We assume that the codec already converted it to the correct range. */
2847 rc = AudioMixerSinkSetVolume(pSink->pMixSink, pVol);
2848 }
2849 else
2850 rc = VERR_NOT_FOUND;
2851
2852 LogFlowFuncLeaveRC(rc);
2853 return rc;
2854}
2855
2856/**
2857 * Main routine for the stream's timer.
2858 *
2859 * @param pDevIns Device instance.
2860 * @param pTimer Timer this callback was called for.
2861 * @param pvUser Pointer to associated HDASTREAM.
2862 */
2863static DECLCALLBACK(void) hdaR3Timer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
2864{
2865 RT_NOREF(pTimer);
2866
2867 PHDASTREAM pStream = (PHDASTREAM)pvUser;
2868 AssertPtr(pStream);
2869
2870 PHDASTATE pThis = pStream->pHDAState;
2871
2872 DEVHDA_LOCK_BOTH_RETURN_VOID(pDevIns, pStream->pHDAState, pStream->u8SD);
2873
2874 hdaR3StreamUpdate(pStream, true /* fInTimer */);
2875
2876 /* Flag indicating whether to kick the timer again for a new data processing round. */
2877 bool fSinkActive = false;
2878 if (pStream->pMixSink)
2879 fSinkActive = AudioMixerSinkIsActive(pStream->pMixSink->pMixSink);
2880
2881 if (fSinkActive)
2882 {
2883 const bool fTimerScheduled = hdaR3StreamTransferIsScheduled(pStream);
2884 Log3Func(("fSinksActive=%RTbool, fTimerScheduled=%RTbool\n", fSinkActive, fTimerScheduled));
2885 if (!fTimerScheduled)
2886 hdaR3TimerSet(pThis, pStream,
2887 TMTimerGet(pThis->pTimer[pStream->u8SD])
2888 + TMTimerGetFreq(pThis->pTimer[pStream->u8SD]) / pStream->pHDAState->uTimerHz,
2889 true /* fForce */);
2890 }
2891 else
2892 Log3Func(("fSinksActive=%RTbool\n", fSinkActive));
2893
2894 DEVHDA_UNLOCK_BOTH(pDevIns, pThis, pStream->u8SD);
2895}
2896
2897# ifdef HDA_USE_DMA_ACCESS_HANDLER
2898/**
2899 * HC access handler for the FIFO.
2900 *
2901 * @returns VINF_SUCCESS if the handler have carried out the operation.
2902 * @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
2903 * @param pVM VM Handle.
2904 * @param pVCpu The cross context CPU structure for the calling EMT.
2905 * @param GCPhys The physical address the guest is writing to.
2906 * @param pvPhys The HC mapping of that address.
2907 * @param pvBuf What the guest is reading/writing.
2908 * @param cbBuf How much it's reading/writing.
2909 * @param enmAccessType The access type.
2910 * @param enmOrigin Who is making the access.
2911 * @param pvUser User argument.
2912 */
2913static DECLCALLBACK(VBOXSTRICTRC) hdaR3DMAAccessHandler(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, void *pvPhys,
2914 void *pvBuf, size_t cbBuf,
2915 PGMACCESSTYPE enmAccessType, PGMACCESSORIGIN enmOrigin, void *pvUser)
2916{
2917 RT_NOREF(pVM, pVCpu, pvPhys, pvBuf, enmOrigin);
2918
2919 PHDADMAACCESSHANDLER pHandler = (PHDADMAACCESSHANDLER)pvUser;
2920 AssertPtr(pHandler);
2921
2922 PHDASTREAM pStream = pHandler->pStream;
2923 AssertPtr(pStream);
2924
2925 Assert(GCPhys >= pHandler->GCPhysFirst);
2926 Assert(GCPhys <= pHandler->GCPhysLast);
2927 Assert(enmAccessType == PGMACCESSTYPE_WRITE);
2928
2929 /* Not within BDLE range? Bail out. */
2930 if ( (GCPhys < pHandler->BDLEAddr)
2931 || (GCPhys + cbBuf > pHandler->BDLEAddr + pHandler->BDLESize))
2932 {
2933 return VINF_PGM_HANDLER_DO_DEFAULT;
2934 }
2935
2936 switch(enmAccessType)
2937 {
2938 case PGMACCESSTYPE_WRITE:
2939 {
2940# ifdef DEBUG
2941 PHDASTREAMDBGINFO pStreamDbg = &pStream->Dbg;
2942
2943 const uint64_t tsNowNs = RTTimeNanoTS();
2944 const uint32_t tsElapsedMs = (tsNowNs - pStreamDbg->tsWriteSlotBegin) / 1000 / 1000;
2945
2946 uint64_t cWritesHz = ASMAtomicReadU64(&pStreamDbg->cWritesHz);
2947 uint64_t cbWrittenHz = ASMAtomicReadU64(&pStreamDbg->cbWrittenHz);
2948
2949 if (tsElapsedMs >= (1000 / HDA_TIMER_HZ_DEFAULT))
2950 {
2951 LogFunc(("[SD%RU8] %RU32ms elapsed, cbWritten=%RU64, cWritten=%RU64 -- %RU32 bytes on average per time slot (%zums)\n",
2952 pStream->u8SD, tsElapsedMs, cbWrittenHz, cWritesHz,
2953 ASMDivU64ByU32RetU32(cbWrittenHz, cWritesHz ? cWritesHz : 1), 1000 / HDA_TIMER_HZ_DEFAULT));
2954
2955 pStreamDbg->tsWriteSlotBegin = tsNowNs;
2956
2957 cWritesHz = 0;
2958 cbWrittenHz = 0;
2959 }
2960
2961 cWritesHz += 1;
2962 cbWrittenHz += cbBuf;
2963
2964 ASMAtomicIncU64(&pStreamDbg->cWritesTotal);
2965 ASMAtomicAddU64(&pStreamDbg->cbWrittenTotal, cbBuf);
2966
2967 ASMAtomicWriteU64(&pStreamDbg->cWritesHz, cWritesHz);
2968 ASMAtomicWriteU64(&pStreamDbg->cbWrittenHz, cbWrittenHz);
2969
2970 LogFunc(("[SD%RU8] Writing %3zu @ 0x%x (off %zu)\n",
2971 pStream->u8SD, cbBuf, GCPhys, GCPhys - pHandler->BDLEAddr));
2972
2973 LogFunc(("[SD%RU8] cWrites=%RU64, cbWritten=%RU64 -> %RU32 bytes on average\n",
2974 pStream->u8SD, pStreamDbg->cWritesTotal, pStreamDbg->cbWrittenTotal,
2975 ASMDivU64ByU32RetU32(pStreamDbg->cbWrittenTotal, pStreamDbg->cWritesTotal)));
2976# endif
2977
2978 if (pThis->fDebugEnabled)
2979 {
2980 RTFILE fh;
2981 RTFileOpen(&fh, VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "hdaDMAAccessWrite.pcm",
2982 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
2983 RTFileWrite(fh, pvBuf, cbBuf, NULL);
2984 RTFileClose(fh);
2985 }
2986
2987# ifdef HDA_USE_DMA_ACCESS_HANDLER_WRITING
2988 PRTCIRCBUF pCircBuf = pStream->State.pCircBuf;
2989 AssertPtr(pCircBuf);
2990
2991 uint8_t *pbBuf = (uint8_t *)pvBuf;
2992 while (cbBuf)
2993 {
2994 /* Make sure we only copy as much as the stream's FIFO can hold (SDFIFOS, 18.2.39). */
2995 void *pvChunk;
2996 size_t cbChunk;
2997 RTCircBufAcquireWriteBlock(pCircBuf, cbBuf, &pvChunk, &cbChunk);
2998
2999 if (cbChunk)
3000 {
3001 memcpy(pvChunk, pbBuf, cbChunk);
3002
3003 pbBuf += cbChunk;
3004 Assert(cbBuf >= cbChunk);
3005 cbBuf -= cbChunk;
3006 }
3007 else
3008 {
3009 //AssertMsg(RTCircBufFree(pCircBuf), ("No more space but still %zu bytes to write\n", cbBuf));
3010 break;
3011 }
3012
3013 LogFunc(("[SD%RU8] cbChunk=%zu\n", pStream->u8SD, cbChunk));
3014
3015 RTCircBufReleaseWriteBlock(pCircBuf, cbChunk);
3016 }
3017# endif /* HDA_USE_DMA_ACCESS_HANDLER_WRITING */
3018 break;
3019 }
3020
3021 default:
3022 AssertMsgFailed(("Access type not implemented\n"));
3023 break;
3024 }
3025
3026 return VINF_PGM_HANDLER_DO_DEFAULT;
3027}
3028# endif /* HDA_USE_DMA_ACCESS_HANDLER */
3029
3030/**
3031 * Soft reset of the device triggered via GCTL.
3032 *
3033 * @param pThis HDA state.
3034 *
3035 */
3036static void hdaR3GCTLReset(PHDASTATE pThis)
3037{
3038 LogFlowFuncEnter();
3039
3040 pThis->cStreamsActive = 0;
3041
3042 HDA_REG(pThis, GCAP) = HDA_MAKE_GCAP(HDA_MAX_SDO, HDA_MAX_SDI, 0, 0, 1); /* see 6.2.1 */
3043 HDA_REG(pThis, VMIN) = 0x00; /* see 6.2.2 */
3044 HDA_REG(pThis, VMAJ) = 0x01; /* see 6.2.3 */
3045 HDA_REG(pThis, OUTPAY) = 0x003C; /* see 6.2.4 */
3046 HDA_REG(pThis, INPAY) = 0x001D; /* see 6.2.5 */
3047 HDA_REG(pThis, CORBSIZE) = 0x42; /* Up to 256 CORB entries see 6.2.1 */
3048 HDA_REG(pThis, RIRBSIZE) = 0x42; /* Up to 256 RIRB entries see 6.2.1 */
3049 HDA_REG(pThis, CORBRP) = 0x0;
3050 HDA_REG(pThis, CORBWP) = 0x0;
3051 HDA_REG(pThis, RIRBWP) = 0x0;
3052 /* Some guests (like Haiku) don't set RINTCNT explicitly but expect an interrupt after each
3053 * RIRB response -- so initialize RINTCNT to 1 by default. */
3054 HDA_REG(pThis, RINTCNT) = 0x1;
3055
3056 /*
3057 * Stop any audio currently playing and/or recording.
3058 */
3059 pThis->SinkFront.pStream = NULL;
3060 if (pThis->SinkFront.pMixSink)
3061 AudioMixerSinkReset(pThis->SinkFront.pMixSink);
3062# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
3063 pThis->SinkMicIn.pStream = NULL;
3064 if (pThis->SinkMicIn.pMixSink)
3065 AudioMixerSinkReset(pThis->SinkMicIn.pMixSink);
3066# endif
3067 pThis->SinkLineIn.pStream = NULL;
3068 if (pThis->SinkLineIn.pMixSink)
3069 AudioMixerSinkReset(pThis->SinkLineIn.pMixSink);
3070# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
3071 pThis->SinkCenterLFE = NULL;
3072 if (pThis->SinkCenterLFE.pMixSink)
3073 AudioMixerSinkReset(pThis->SinkCenterLFE.pMixSink);
3074 pThis->SinkRear.pStream = NULL;
3075 if (pThis->SinkRear.pMixSink)
3076 AudioMixerSinkReset(pThis->SinkRear.pMixSink);
3077# endif
3078
3079 /*
3080 * Reset the codec.
3081 */
3082 if ( pThis->pCodec
3083 && pThis->pCodec->pfnReset)
3084 {
3085 pThis->pCodec->pfnReset(pThis->pCodec);
3086 }
3087
3088 /*
3089 * Set some sensible defaults for which HDA sinks
3090 * are connected to which stream number.
3091 *
3092 * We use SD0 for input and SD4 for output by default.
3093 * These stream numbers can be changed by the guest dynamically lateron.
3094 */
3095# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
3096 hdaR3MixerControl(pThis, PDMAUDIOMIXERCTL_MIC_IN , 1 /* SD0 */, 0 /* Channel */);
3097# endif
3098 hdaR3MixerControl(pThis, PDMAUDIOMIXERCTL_LINE_IN , 1 /* SD0 */, 0 /* Channel */);
3099
3100 hdaR3MixerControl(pThis, PDMAUDIOMIXERCTL_FRONT , 5 /* SD4 */, 0 /* Channel */);
3101# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
3102 hdaR3MixerControl(pThis, PDMAUDIOMIXERCTL_CENTER_LFE, 5 /* SD4 */, 0 /* Channel */);
3103 hdaR3MixerControl(pThis, PDMAUDIOMIXERCTL_REAR , 5 /* SD4 */, 0 /* Channel */);
3104# endif
3105
3106 /* Reset CORB. */
3107 pThis->cbCorbBuf = HDA_CORB_SIZE * HDA_CORB_ELEMENT_SIZE;
3108 RT_BZERO(pThis->pu32CorbBuf, pThis->cbCorbBuf);
3109
3110 /* Reset RIRB. */
3111 pThis->cbRirbBuf = HDA_RIRB_SIZE * HDA_RIRB_ELEMENT_SIZE;
3112 RT_BZERO(pThis->pu64RirbBuf, pThis->cbRirbBuf);
3113
3114 /* Clear our internal response interrupt counter. */
3115 pThis->u16RespIntCnt = 0;
3116
3117 for (uint8_t uSD = 0; uSD < HDA_MAX_STREAMS; ++uSD)
3118 {
3119 int rc2 = hdaR3StreamEnable(&pThis->aStreams[uSD], false /* fEnable */);
3120 if (RT_SUCCESS(rc2))
3121 {
3122 /* Remove the RUN bit from SDnCTL in case the stream was in a running state before. */
3123 HDA_STREAM_REG(pThis, CTL, uSD) &= ~HDA_SDCTL_RUN;
3124 hdaR3StreamReset(pThis, &pThis->aStreams[uSD], uSD);
3125 }
3126 }
3127
3128 /* Clear stream tags <-> objects mapping table. */
3129 RT_ZERO(pThis->aTags);
3130
3131 /* Emulation of codec "wake up" (HDA spec 5.5.1 and 6.5). */
3132 HDA_REG(pThis, STATESTS) = 0x1;
3133
3134 LogFlowFuncLeave();
3135 LogRel(("HDA: Reset\n"));
3136}
3137
3138#endif /* IN_RING3 */
3139
3140/* MMIO callbacks */
3141
3142/**
3143 * @callback_method_impl{FNIOMMMIOREAD, Looks up and calls the appropriate handler.}
3144 *
3145 * @note During implementation, we discovered so-called "forgotten" or "hole"
3146 * registers whose description is not listed in the RPM, datasheet, or
3147 * spec.
3148 */
3149PDMBOTHCBDECL(int) hdaMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
3150{
3151 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
3152 int rc;
3153 RT_NOREF_PV(pvUser);
3154 Assert(pThis->uAlignmentCheckMagic == HDASTATE_ALIGNMENT_CHECK_MAGIC);
3155
3156 /*
3157 * Look up and log.
3158 */
3159 uint32_t offReg = GCPhysAddr - pThis->MMIOBaseAddr;
3160 int idxRegDsc = hdaRegLookup(offReg); /* Register descriptor index. */
3161#ifdef LOG_ENABLED
3162 unsigned const cbLog = cb;
3163 uint32_t offRegLog = offReg;
3164#endif
3165
3166 Log3Func(("offReg=%#x cb=%#x\n", offReg, cb));
3167 Assert(cb == 4); Assert((offReg & 3) == 0);
3168
3169 DEVHDA_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_MMIO_READ);
3170
3171 if (!(HDA_REG(pThis, GCTL) & HDA_GCTL_CRST) && idxRegDsc != HDA_REG_GCTL)
3172 LogFunc(("Access to registers except GCTL is blocked while reset\n"));
3173
3174 if (idxRegDsc == -1)
3175 LogRel(("HDA: Invalid read access @0x%x (bytes=%u)\n", offReg, cb));
3176
3177 if (idxRegDsc != -1)
3178 {
3179 /* Leave lock before calling read function. */
3180 DEVHDA_UNLOCK(pDevIns, pThis);
3181
3182 /* ASSUMES gapless DWORD at end of map. */
3183 if (g_aHdaRegMap[idxRegDsc].size == 4)
3184 {
3185 /*
3186 * Straight forward DWORD access.
3187 */
3188 rc = g_aHdaRegMap[idxRegDsc].pfnRead(pDevIns, pThis, idxRegDsc, (uint32_t *)pv);
3189 Log3Func(("\tRead %s => %x (%Rrc)\n", g_aHdaRegMap[idxRegDsc].abbrev, *(uint32_t *)pv, rc));
3190 }
3191 else
3192 {
3193 /*
3194 * Multi register read (unless there are trailing gaps).
3195 * ASSUMES that only DWORD reads have sideeffects.
3196 */
3197#ifdef IN_RING3
3198 uint32_t u32Value = 0;
3199 unsigned cbLeft = 4;
3200 do
3201 {
3202 uint32_t const cbReg = g_aHdaRegMap[idxRegDsc].size;
3203 uint32_t u32Tmp = 0;
3204
3205 rc = g_aHdaRegMap[idxRegDsc].pfnRead(pDevIns, pThis, idxRegDsc, &u32Tmp);
3206 Log3Func(("\tRead %s[%db] => %x (%Rrc)*\n", g_aHdaRegMap[idxRegDsc].abbrev, cbReg, u32Tmp, rc));
3207 if (rc != VINF_SUCCESS)
3208 break;
3209 u32Value |= (u32Tmp & g_afMasks[cbReg]) << ((4 - cbLeft) * 8);
3210
3211 cbLeft -= cbReg;
3212 offReg += cbReg;
3213 idxRegDsc++;
3214 } while (cbLeft > 0 && g_aHdaRegMap[idxRegDsc].offset == offReg);
3215
3216 if (rc == VINF_SUCCESS)
3217 *(uint32_t *)pv = u32Value;
3218 else
3219 Assert(!IOM_SUCCESS(rc));
3220#else /* !IN_RING3 */
3221 /* Take the easy way out. */
3222 rc = VINF_IOM_R3_MMIO_READ;
3223#endif /* !IN_RING3 */
3224 }
3225 }
3226 else
3227 {
3228 DEVHDA_UNLOCK(pDevIns, pThis);
3229
3230 rc = VINF_IOM_MMIO_UNUSED_FF;
3231 Log3Func(("\tHole at %x is accessed for read\n", offReg));
3232 }
3233
3234 /*
3235 * Log the outcome.
3236 */
3237#ifdef LOG_ENABLED
3238 if (cbLog == 4)
3239 Log3Func(("\tReturning @%#05x -> %#010x %Rrc\n", offRegLog, *(uint32_t *)pv, rc));
3240 else if (cbLog == 2)
3241 Log3Func(("\tReturning @%#05x -> %#06x %Rrc\n", offRegLog, *(uint16_t *)pv, rc));
3242 else if (cbLog == 1)
3243 Log3Func(("\tReturning @%#05x -> %#04x %Rrc\n", offRegLog, *(uint8_t *)pv, rc));
3244#endif
3245 return rc;
3246}
3247
3248
3249DECLINLINE(int) hdaWriteReg(PPDMDEVINS pDevIns, PHDASTATE pThis, int idxRegDsc, uint32_t u32Value, char const *pszLog)
3250{
3251 DEVHDA_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_MMIO_WRITE);
3252
3253 if (!(HDA_REG(pThis, GCTL) & HDA_GCTL_CRST) && idxRegDsc != HDA_REG_GCTL)
3254 {
3255 Log(("hdaWriteReg: Warning: Access to %s is blocked while controller is in reset mode\n", g_aHdaRegMap[idxRegDsc].abbrev));
3256 LogRel2(("HDA: Warning: Access to register %s is blocked while controller is in reset mode\n",
3257 g_aHdaRegMap[idxRegDsc].abbrev));
3258
3259 DEVHDA_UNLOCK(pDevIns, pThis);
3260 return VINF_SUCCESS;
3261 }
3262
3263 /*
3264 * Handle RD (register description) flags.
3265 */
3266
3267 /* For SDI / SDO: Check if writes to those registers are allowed while SDCTL's RUN bit is set. */
3268 if (idxRegDsc >= HDA_NUM_GENERAL_REGS)
3269 {
3270 const uint32_t uSDCTL = HDA_STREAM_REG(pThis, CTL, HDA_SD_NUM_FROM_REG(pThis, CTL, idxRegDsc));
3271
3272 /*
3273 * Some OSes (like Win 10 AU) violate the spec by writing stuff to registers which are not supposed to be be touched
3274 * while SDCTL's RUN bit is set. So just ignore those values.
3275 */
3276
3277 /* Is the RUN bit currently set? */
3278 if ( RT_BOOL(uSDCTL & HDA_SDCTL_RUN)
3279 /* Are writes to the register denied if RUN bit is set? */
3280 && !(g_aHdaRegMap[idxRegDsc].fFlags & HDA_RD_FLAG_SD_WRITE_RUN))
3281 {
3282 Log(("hdaWriteReg: Warning: Access to %s is blocked! %R[sdctl]\n", g_aHdaRegMap[idxRegDsc].abbrev, uSDCTL));
3283 LogRel2(("HDA: Warning: Access to register %s is blocked while the stream's RUN bit is set\n",
3284 g_aHdaRegMap[idxRegDsc].abbrev));
3285
3286 DEVHDA_UNLOCK(pDevIns, pThis);
3287 return VINF_SUCCESS;
3288 }
3289 }
3290
3291 /* Leave the lock before calling write function. */
3292 /** @todo r=bird: Why do we need to do that?? There is no
3293 * explanation why this is necessary here...
3294 *
3295 * More or less all write functions retake the lock, so why not let
3296 * those who need to drop the lock or take additional locks release
3297 * it? See, releasing a lock you already got always runs the risk
3298 * of someone else grabbing it and forcing you to wait, better to
3299 * do the two-three things a write handle needs to do than enter
3300 * and exit the lock all the time. */
3301 DEVHDA_UNLOCK(pDevIns, pThis);
3302
3303#ifdef LOG_ENABLED
3304 uint32_t const idxRegMem = g_aHdaRegMap[idxRegDsc].mem_idx;
3305 uint32_t const u32OldValue = pThis->au32Regs[idxRegMem];
3306#endif
3307 int rc = g_aHdaRegMap[idxRegDsc].pfnWrite(pDevIns, pThis, idxRegDsc, u32Value);
3308 Log3Func(("Written value %#x to %s[%d byte]; %x => %x%s, rc=%d\n", u32Value, g_aHdaRegMap[idxRegDsc].abbrev,
3309 g_aHdaRegMap[idxRegDsc].size, u32OldValue, pThis->au32Regs[idxRegMem], pszLog, rc));
3310 RT_NOREF(pszLog);
3311 return rc;
3312}
3313
3314
3315/**
3316 * @callback_method_impl{FNIOMMMIOWRITE, Looks up and calls the appropriate handler.}
3317 */
3318PDMBOTHCBDECL(int) hdaMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
3319{
3320 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
3321 int rc;
3322 RT_NOREF_PV(pvUser);
3323 Assert(pThis->uAlignmentCheckMagic == HDASTATE_ALIGNMENT_CHECK_MAGIC);
3324
3325 /*
3326 * The behavior of accesses that aren't aligned on natural boundraries is
3327 * undefined. Just reject them outright.
3328 */
3329 /** @todo IOM could check this, it could also split the 8 byte accesses for us. */
3330 Assert(cb == 1 || cb == 2 || cb == 4 || cb == 8);
3331 if (GCPhysAddr & (cb - 1))
3332 return PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "misaligned write access: GCPhysAddr=%RGp cb=%u\n", GCPhysAddr, cb);
3333
3334 /*
3335 * Look up and log the access.
3336 */
3337 uint32_t offReg = GCPhysAddr - pThis->MMIOBaseAddr;
3338 int idxRegDsc = hdaRegLookup(offReg);
3339#if defined(IN_RING3) || defined(LOG_ENABLED)
3340 uint32_t idxRegMem = idxRegDsc != -1 ? g_aHdaRegMap[idxRegDsc].mem_idx : UINT32_MAX;
3341#endif
3342 uint64_t u64Value;
3343 if (cb == 4) u64Value = *(uint32_t const *)pv;
3344 else if (cb == 2) u64Value = *(uint16_t const *)pv;
3345 else if (cb == 1) u64Value = *(uint8_t const *)pv;
3346 else if (cb == 8) u64Value = *(uint64_t const *)pv;
3347 else
3348 {
3349 u64Value = 0; /* shut up gcc. */
3350 AssertReleaseMsgFailed(("%u\n", cb));
3351 }
3352
3353#ifdef LOG_ENABLED
3354 uint32_t const u32LogOldValue = idxRegDsc >= 0 ? pThis->au32Regs[idxRegMem] : UINT32_MAX;
3355 if (idxRegDsc == -1)
3356 Log3Func(("@%#05x u32=%#010x cb=%d\n", offReg, *(uint32_t const *)pv, cb));
3357 else if (cb == 4)
3358 Log3Func(("@%#05x u32=%#010x %s\n", offReg, *(uint32_t *)pv, g_aHdaRegMap[idxRegDsc].abbrev));
3359 else if (cb == 2)
3360 Log3Func(("@%#05x u16=%#06x (%#010x) %s\n", offReg, *(uint16_t *)pv, *(uint32_t *)pv, g_aHdaRegMap[idxRegDsc].abbrev));
3361 else if (cb == 1)
3362 Log3Func(("@%#05x u8=%#04x (%#010x) %s\n", offReg, *(uint8_t *)pv, *(uint32_t *)pv, g_aHdaRegMap[idxRegDsc].abbrev));
3363
3364 if (idxRegDsc >= 0 && g_aHdaRegMap[idxRegDsc].size != cb)
3365 Log3Func(("\tsize=%RU32 != cb=%u!!\n", g_aHdaRegMap[idxRegDsc].size, cb));
3366#endif
3367
3368 /*
3369 * Try for a direct hit first.
3370 */
3371 if (idxRegDsc != -1 && g_aHdaRegMap[idxRegDsc].size == cb)
3372 {
3373 rc = hdaWriteReg(pDevIns, pThis, idxRegDsc, u64Value, "");
3374 Log3Func(("\t%#x -> %#x\n", u32LogOldValue, idxRegMem != UINT32_MAX ? pThis->au32Regs[idxRegMem] : UINT32_MAX));
3375 }
3376 /*
3377 * Partial or multiple register access, loop thru the requested memory.
3378 */
3379 else
3380 {
3381#ifdef IN_RING3
3382 /*
3383 * If it's an access beyond the start of the register, shift the input
3384 * value and fill in missing bits. Natural alignment rules means we
3385 * will only see 1 or 2 byte accesses of this kind, so no risk of
3386 * shifting out input values.
3387 */
3388 if (idxRegDsc == -1 && (idxRegDsc = hdaR3RegLookupWithin(offReg)) != -1)
3389 {
3390 uint32_t const cbBefore = offReg - g_aHdaRegMap[idxRegDsc].offset; Assert(cbBefore > 0 && cbBefore < 4);
3391 offReg -= cbBefore;
3392 idxRegMem = g_aHdaRegMap[idxRegDsc].mem_idx;
3393 u64Value <<= cbBefore * 8;
3394 u64Value |= pThis->au32Regs[idxRegMem] & g_afMasks[cbBefore];
3395 Log3Func(("\tWithin register, supplied %u leading bits: %#llx -> %#llx ...\n",
3396 cbBefore * 8, ~g_afMasks[cbBefore] & u64Value, u64Value));
3397 }
3398
3399 /* Loop thru the write area, it may cover multiple registers. */
3400 rc = VINF_SUCCESS;
3401 for (;;)
3402 {
3403 uint32_t cbReg;
3404 if (idxRegDsc != -1)
3405 {
3406 idxRegMem = g_aHdaRegMap[idxRegDsc].mem_idx;
3407 cbReg = g_aHdaRegMap[idxRegDsc].size;
3408 if (cb < cbReg)
3409 {
3410 u64Value |= pThis->au32Regs[idxRegMem] & g_afMasks[cbReg] & ~g_afMasks[cb];
3411 Log3Func(("\tSupplying missing bits (%#x): %#llx -> %#llx ...\n",
3412 g_afMasks[cbReg] & ~g_afMasks[cb], u64Value & g_afMasks[cb], u64Value));
3413 }
3414# ifdef LOG_ENABLED
3415 uint32_t uLogOldVal = pThis->au32Regs[idxRegMem];
3416# endif
3417 rc = hdaWriteReg(pDevIns, pThis, idxRegDsc, u64Value, "*");
3418 Log3Func(("\t%#x -> %#x\n", uLogOldVal, pThis->au32Regs[idxRegMem]));
3419 }
3420 else
3421 {
3422 LogRel(("HDA: Invalid write access @0x%x\n", offReg));
3423 cbReg = 1;
3424 }
3425 if (rc != VINF_SUCCESS)
3426 break;
3427 if (cbReg >= cb)
3428 break;
3429
3430 /* Advance. */
3431 offReg += cbReg;
3432 cb -= cbReg;
3433 u64Value >>= cbReg * 8;
3434 if (idxRegDsc == -1)
3435 idxRegDsc = hdaRegLookup(offReg);
3436 else
3437 {
3438 idxRegDsc++;
3439 if ( (unsigned)idxRegDsc >= RT_ELEMENTS(g_aHdaRegMap)
3440 || g_aHdaRegMap[idxRegDsc].offset != offReg)
3441 {
3442 idxRegDsc = -1;
3443 }
3444 }
3445 }
3446
3447#else /* !IN_RING3 */
3448 /* Take the simple way out. */
3449 rc = VINF_IOM_R3_MMIO_WRITE;
3450#endif /* !IN_RING3 */
3451 }
3452
3453 return rc;
3454}
3455
3456
3457/* PCI callback. */
3458
3459#ifdef IN_RING3
3460/**
3461 * @callback_method_impl{FNPCIIOREGIONMAP}
3462 */
3463static DECLCALLBACK(int) hdaR3PciIoRegionMap(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t iRegion,
3464 RTGCPHYS GCPhysAddress, RTGCPHYS cb, PCIADDRESSSPACE enmType)
3465{
3466 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
3467 RT_NOREF(pPciDev, iRegion, enmType);
3468
3469 Assert(enmType == PCI_ADDRESS_SPACE_MEM);
3470 Assert(iRegion == 0);
3471 Assert(pPciDev == pDevIns->apPciDevs[0]);
3472
3473 /*
3474 * 18.2 of the ICH6 datasheet defines the valid access widths as byte, word, and double word.
3475 *
3476 * Let IOM talk DWORDs when reading, saves a lot of complications. On
3477 * writing though, we have to do it all ourselves because of sideeffects.
3478 */
3479 Assert(enmType == PCI_ADDRESS_SPACE_MEM);
3480 int rc = PDMDevHlpMMIORegister(pDevIns, GCPhysAddress, cb, NULL /*pvUser*/,
3481 IOMMMIO_FLAGS_READ_DWORD
3482 | IOMMMIO_FLAGS_WRITE_PASSTHRU,
3483 hdaMMIOWrite, hdaMMIORead, "HDA");
3484
3485 if (RT_FAILURE(rc))
3486 return rc;
3487
3488 if (pThis->fRZEnabled)
3489 {
3490 rc = PDMDevHlpMMIORegisterR0(pDevIns, GCPhysAddress, cb, NIL_RTR0PTR /*pvUser*/,
3491 "hdaMMIOWrite", "hdaMMIORead");
3492 if (RT_FAILURE(rc))
3493 return rc;
3494
3495 rc = PDMDevHlpMMIORegisterRC(pDevIns, GCPhysAddress, cb, NIL_RTRCPTR /*pvUser*/,
3496 "hdaMMIOWrite", "hdaMMIORead");
3497 if (RT_FAILURE(rc))
3498 return rc;
3499 }
3500
3501 pThis->MMIOBaseAddr = GCPhysAddress;
3502 return VINF_SUCCESS;
3503}
3504
3505
3506/* Saved state workers and callbacks. */
3507
3508static int hdaR3SaveStream(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, PHDASTREAM pStream)
3509{
3510 RT_NOREF(pDevIns);
3511#if defined(LOG_ENABLED)
3512 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
3513#endif
3514
3515 Log2Func(("[SD%RU8]\n", pStream->u8SD));
3516
3517 /* Save stream ID. */
3518 int rc = SSMR3PutU8(pSSM, pStream->u8SD);
3519 AssertRCReturn(rc, rc);
3520 Assert(pStream->u8SD < HDA_MAX_STREAMS);
3521
3522 rc = SSMR3PutStructEx(pSSM, &pStream->State, sizeof(HDASTREAMSTATE), 0 /*fFlags*/, g_aSSMStreamStateFields7, NULL);
3523 AssertRCReturn(rc, rc);
3524
3525 rc = SSMR3PutStructEx(pSSM, &pStream->State.BDLE.Desc, sizeof(HDABDLEDESC), 0 /*fFlags*/, g_aSSMBDLEDescFields7, NULL);
3526 AssertRCReturn(rc, rc);
3527
3528 rc = SSMR3PutStructEx(pSSM, &pStream->State.BDLE.State, sizeof(HDABDLESTATE), 0 /*fFlags*/, g_aSSMBDLEStateFields7, NULL);
3529 AssertRCReturn(rc, rc);
3530
3531 rc = SSMR3PutStructEx(pSSM, &pStream->State.Period, sizeof(HDASTREAMPERIOD), 0 /* fFlags */, g_aSSMStreamPeriodFields7, NULL);
3532 AssertRCReturn(rc, rc);
3533
3534 uint32_t cbCircBufSize = 0;
3535 uint32_t cbCircBufUsed = 0;
3536
3537 if (pStream->State.pCircBuf)
3538 {
3539 cbCircBufSize = (uint32_t)RTCircBufSize(pStream->State.pCircBuf);
3540 cbCircBufUsed = (uint32_t)RTCircBufUsed(pStream->State.pCircBuf);
3541 }
3542
3543 rc = SSMR3PutU32(pSSM, cbCircBufSize);
3544 AssertRCReturn(rc, rc);
3545
3546 rc = SSMR3PutU32(pSSM, cbCircBufUsed);
3547 AssertRCReturn(rc, rc);
3548
3549 if (cbCircBufUsed)
3550 {
3551 /*
3552 * We now need to get the circular buffer's data without actually modifying
3553 * the internal read / used offsets -- otherwise we would end up with broken audio
3554 * data after saving the state.
3555 *
3556 * So get the current read offset and serialize the buffer data manually based on that.
3557 */
3558 size_t const offBuf = RTCircBufOffsetRead(pStream->State.pCircBuf);
3559 void *pvBuf;
3560 size_t cbBuf;
3561 RTCircBufAcquireReadBlock(pStream->State.pCircBuf, cbCircBufUsed, &pvBuf, &cbBuf);
3562 Assert(cbBuf);
3563 if (cbBuf)
3564 {
3565 rc = SSMR3PutMem(pSSM, pvBuf, cbBuf);
3566 AssertRC(rc);
3567 if ( RT_SUCCESS(rc)
3568 && cbBuf < cbCircBufUsed)
3569 {
3570 rc = SSMR3PutMem(pSSM, (uint8_t *)pvBuf - offBuf, cbCircBufUsed - cbBuf);
3571 }
3572 }
3573 RTCircBufReleaseReadBlock(pStream->State.pCircBuf, 0 /* Don't advance read pointer -- see comment above */);
3574 }
3575
3576 Log2Func(("[SD%RU8] LPIB=%RU32, CBL=%RU32, LVI=%RU32\n",
3577 pStream->u8SD,
3578 HDA_STREAM_REG(pThis, LPIB, pStream->u8SD), HDA_STREAM_REG(pThis, CBL, pStream->u8SD), HDA_STREAM_REG(pThis, LVI, pStream->u8SD)));
3579
3580#ifdef LOG_ENABLED
3581 hdaR3BDLEDumpAll(pThis, pStream->u64BDLBase, pStream->u16LVI + 1);
3582#endif
3583
3584 return rc;
3585}
3586
3587/**
3588 * @callback_method_impl{FNSSMDEVSAVEEXEC}
3589 */
3590static DECLCALLBACK(int) hdaR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
3591{
3592 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
3593
3594 /* Save Codec nodes states. */
3595 hdaCodecSaveState(pThis->pCodec, pSSM);
3596
3597 /* Save MMIO registers. */
3598 SSMR3PutU32(pSSM, RT_ELEMENTS(pThis->au32Regs));
3599 SSMR3PutMem(pSSM, pThis->au32Regs, sizeof(pThis->au32Regs));
3600
3601 /* Save controller-specifc internals. */
3602 SSMR3PutU64(pSSM, pThis->u64WalClk);
3603 SSMR3PutU8(pSSM, pThis->u8IRQL);
3604
3605 /* Save number of streams. */
3606 SSMR3PutU32(pSSM, HDA_MAX_STREAMS);
3607
3608 /* Save stream states. */
3609 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
3610 {
3611 int rc = hdaR3SaveStream(pDevIns, pSSM, &pThis->aStreams[i]);
3612 AssertRCReturn(rc, rc);
3613 }
3614
3615 return VINF_SUCCESS;
3616}
3617
3618/**
3619 * Does required post processing when loading a saved state.
3620 *
3621 * @param pThis Pointer to HDA state.
3622 */
3623static int hdaR3LoadExecPost(PHDASTATE pThis)
3624{
3625 int rc = VINF_SUCCESS;
3626
3627 /*
3628 * Enable all previously active streams.
3629 */
3630 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
3631 {
3632 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, i);
3633 if (pStream)
3634 {
3635 int rc2;
3636
3637 bool fActive = RT_BOOL(HDA_STREAM_REG(pThis, CTL, i) & HDA_SDCTL_RUN);
3638 if (fActive)
3639 {
3640#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
3641 /* Make sure to also create the async I/O thread before actually enabling the stream. */
3642 rc2 = hdaR3StreamAsyncIOCreate(pStream);
3643 AssertRC(rc2);
3644
3645 /* ... and enabling it. */
3646 hdaR3StreamAsyncIOEnable(pStream, true /* fEnable */);
3647#endif
3648 /* Resume the stream's period. */
3649 hdaR3StreamPeriodResume(&pStream->State.Period);
3650
3651 /* (Re-)enable the stream. */
3652 rc2 = hdaR3StreamEnable(pStream, true /* fEnable */);
3653 AssertRC(rc2);
3654
3655 /* Add the stream to the device setup. */
3656 rc2 = hdaR3AddStream(pThis, &pStream->State.Cfg);
3657 AssertRC(rc2);
3658
3659#ifdef HDA_USE_DMA_ACCESS_HANDLER
3660 /* (Re-)install the DMA handler. */
3661 hdaR3StreamRegisterDMAHandlers(pThis, pStream);
3662#endif
3663 if (hdaR3StreamTransferIsScheduled(pStream))
3664 hdaR3TimerSet(pThis, pStream, hdaR3StreamTransferGetNext(pStream), true /* fForce */);
3665
3666 /* Also keep track of the currently active streams. */
3667 pThis->cStreamsActive++;
3668 }
3669 }
3670 }
3671
3672 LogFlowFuncLeaveRC(rc);
3673 return rc;
3674}
3675
3676
3677/**
3678 * Handles loading of all saved state versions older than the current one.
3679 *
3680 * @param pThis Pointer to HDA state.
3681 * @param pSSM Pointer to SSM handle.
3682 * @param uVersion Saved state version to load.
3683 * @param uPass Loading stage to handle.
3684 */
3685static int hdaR3LoadExecLegacy(PHDASTATE pThis, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
3686{
3687 RT_NOREF(uPass);
3688
3689 int rc = VINF_SUCCESS;
3690
3691 /*
3692 * Load MMIO registers.
3693 */
3694 uint32_t cRegs;
3695 switch (uVersion)
3696 {
3697 case HDA_SSM_VERSION_1:
3698 /* Starting with r71199, we would save 112 instead of 113
3699 registers due to some code cleanups. This only affected trunk
3700 builds in the 4.1 development period. */
3701 cRegs = 113;
3702 if (SSMR3HandleRevision(pSSM) >= 71199)
3703 {
3704 uint32_t uVer = SSMR3HandleVersion(pSSM);
3705 if ( VBOX_FULL_VERSION_GET_MAJOR(uVer) == 4
3706 && VBOX_FULL_VERSION_GET_MINOR(uVer) == 0
3707 && VBOX_FULL_VERSION_GET_BUILD(uVer) >= 51)
3708 cRegs = 112;
3709 }
3710 break;
3711
3712 case HDA_SSM_VERSION_2:
3713 case HDA_SSM_VERSION_3:
3714 cRegs = 112;
3715 AssertCompile(RT_ELEMENTS(pThis->au32Regs) >= 112);
3716 break;
3717
3718 /* Since version 4 we store the register count to stay flexible. */
3719 case HDA_SSM_VERSION_4:
3720 case HDA_SSM_VERSION_5:
3721 case HDA_SSM_VERSION_6:
3722 rc = SSMR3GetU32(pSSM, &cRegs); AssertRCReturn(rc, rc);
3723 if (cRegs != RT_ELEMENTS(pThis->au32Regs))
3724 LogRel(("HDA: SSM version cRegs is %RU32, expected %RU32\n", cRegs, RT_ELEMENTS(pThis->au32Regs)));
3725 break;
3726
3727 default:
3728 LogRel(("HDA: Warning: Unsupported / too new saved state version (%RU32)\n", uVersion));
3729 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
3730 }
3731
3732 if (cRegs >= RT_ELEMENTS(pThis->au32Regs))
3733 {
3734 SSMR3GetMem(pSSM, pThis->au32Regs, sizeof(pThis->au32Regs));
3735 SSMR3Skip(pSSM, sizeof(uint32_t) * (cRegs - RT_ELEMENTS(pThis->au32Regs)));
3736 }
3737 else
3738 SSMR3GetMem(pSSM, pThis->au32Regs, sizeof(uint32_t) * cRegs);
3739
3740 /* Make sure to update the base addresses first before initializing any streams down below. */
3741 pThis->u64CORBBase = RT_MAKE_U64(HDA_REG(pThis, CORBLBASE), HDA_REG(pThis, CORBUBASE));
3742 pThis->u64RIRBBase = RT_MAKE_U64(HDA_REG(pThis, RIRBLBASE), HDA_REG(pThis, RIRBUBASE));
3743 pThis->u64DPBase = RT_MAKE_U64(HDA_REG(pThis, DPLBASE) & DPBASE_ADDR_MASK, HDA_REG(pThis, DPUBASE));
3744
3745 /* Also make sure to update the DMA position bit if this was enabled when saving the state. */
3746 pThis->fDMAPosition = RT_BOOL(HDA_REG(pThis, DPLBASE) & RT_BIT_32(0));
3747
3748 /*
3749 * Note: Saved states < v5 store LVI (u32BdleMaxCvi) for
3750 * *every* BDLE state, whereas it only needs to be stored
3751 * *once* for every stream. Most of the BDLE state we can
3752 * get out of the registers anyway, so just ignore those values.
3753 *
3754 * Also, only the current BDLE was saved, regardless whether
3755 * there were more than one (and there are at least two entries,
3756 * according to the spec).
3757 */
3758#define HDA_SSM_LOAD_BDLE_STATE_PRE_V5(v, x) \
3759 { \
3760 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Begin marker */ \
3761 AssertRCReturn(rc, rc); \
3762 rc = SSMR3GetU64(pSSM, &x.Desc.u64BufAddr); /* u64BdleCviAddr */ \
3763 AssertRCReturn(rc, rc); \
3764 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* u32BdleMaxCvi */ \
3765 AssertRCReturn(rc, rc); \
3766 rc = SSMR3GetU32(pSSM, &x.State.u32BDLIndex); /* u32BdleCvi */ \
3767 AssertRCReturn(rc, rc); \
3768 rc = SSMR3GetU32(pSSM, &x.Desc.u32BufSize); /* u32BdleCviLen */ \
3769 AssertRCReturn(rc, rc); \
3770 rc = SSMR3GetU32(pSSM, &x.State.u32BufOff); /* u32BdleCviPos */ \
3771 AssertRCReturn(rc, rc); \
3772 bool fIOC; \
3773 rc = SSMR3GetBool(pSSM, &fIOC); /* fBdleCviIoc */ \
3774 AssertRCReturn(rc, rc); \
3775 x.Desc.fFlags = fIOC ? HDA_BDLE_FLAG_IOC : 0; \
3776 rc = SSMR3GetU32(pSSM, &x.State.cbBelowFIFOW); /* cbUnderFifoW */ \
3777 AssertRCReturn(rc, rc); \
3778 rc = SSMR3Skip(pSSM, sizeof(uint8_t) * 256); /* FIFO */ \
3779 AssertRCReturn(rc, rc); \
3780 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* End marker */ \
3781 AssertRCReturn(rc, rc); \
3782 }
3783
3784 /*
3785 * Load BDLEs (Buffer Descriptor List Entries) and DMA counters.
3786 */
3787 switch (uVersion)
3788 {
3789 case HDA_SSM_VERSION_1:
3790 case HDA_SSM_VERSION_2:
3791 case HDA_SSM_VERSION_3:
3792 case HDA_SSM_VERSION_4:
3793 {
3794 /* Only load the internal states.
3795 * The rest will be initialized from the saved registers later. */
3796
3797 /* Note 1: Only the *current* BDLE for a stream was saved! */
3798 /* Note 2: The stream's saving order is/was fixed, so don't touch! */
3799
3800 /* Output */
3801 PHDASTREAM pStream = &pThis->aStreams[4];
3802 rc = hdaR3StreamInit(pStream, 4 /* Stream descriptor, hardcoded */);
3803 if (RT_FAILURE(rc))
3804 break;
3805 HDA_SSM_LOAD_BDLE_STATE_PRE_V5(uVersion, pStream->State.BDLE);
3806 pStream->State.uCurBDLE = pStream->State.BDLE.State.u32BDLIndex;
3807
3808 /* Microphone-In */
3809 pStream = &pThis->aStreams[2];
3810 rc = hdaR3StreamInit(pStream, 2 /* Stream descriptor, hardcoded */);
3811 if (RT_FAILURE(rc))
3812 break;
3813 HDA_SSM_LOAD_BDLE_STATE_PRE_V5(uVersion, pStream->State.BDLE);
3814 pStream->State.uCurBDLE = pStream->State.BDLE.State.u32BDLIndex;
3815
3816 /* Line-In */
3817 pStream = &pThis->aStreams[0];
3818 rc = hdaR3StreamInit(pStream, 0 /* Stream descriptor, hardcoded */);
3819 if (RT_FAILURE(rc))
3820 break;
3821 HDA_SSM_LOAD_BDLE_STATE_PRE_V5(uVersion, pStream->State.BDLE);
3822 pStream->State.uCurBDLE = pStream->State.BDLE.State.u32BDLIndex;
3823 break;
3824 }
3825
3826#undef HDA_SSM_LOAD_BDLE_STATE_PRE_V5
3827
3828 default: /* Since v5 we support flexible stream and BDLE counts. */
3829 {
3830 uint32_t cStreams;
3831 rc = SSMR3GetU32(pSSM, &cStreams);
3832 if (RT_FAILURE(rc))
3833 break;
3834
3835 if (cStreams > HDA_MAX_STREAMS)
3836 cStreams = HDA_MAX_STREAMS; /* Sanity. */
3837
3838 /* Load stream states. */
3839 for (uint32_t i = 0; i < cStreams; i++)
3840 {
3841 uint8_t uStreamID;
3842 rc = SSMR3GetU8(pSSM, &uStreamID);
3843 if (RT_FAILURE(rc))
3844 break;
3845
3846 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, uStreamID);
3847 HDASTREAM StreamDummy;
3848
3849 if (!pStream)
3850 {
3851 pStream = &StreamDummy;
3852 LogRel2(("HDA: Warning: Stream ID=%RU32 not supported, skipping to load ...\n", uStreamID));
3853 }
3854
3855 rc = hdaR3StreamInit(pStream, uStreamID);
3856 if (RT_FAILURE(rc))
3857 {
3858 LogRel(("HDA: Stream #%RU32: Initialization of stream %RU8 failed, rc=%Rrc\n", i, uStreamID, rc));
3859 break;
3860 }
3861
3862 /*
3863 * Load BDLEs (Buffer Descriptor List Entries) and DMA counters.
3864 */
3865
3866 if (uVersion == HDA_SSM_VERSION_5)
3867 {
3868 /* Get the current BDLE entry and skip the rest. */
3869 uint16_t cBDLE;
3870
3871 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Begin marker */
3872 AssertRC(rc);
3873 rc = SSMR3GetU16(pSSM, &cBDLE); /* cBDLE */
3874 AssertRC(rc);
3875 rc = SSMR3GetU16(pSSM, &pStream->State.uCurBDLE); /* uCurBDLE */
3876 AssertRC(rc);
3877 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* End marker */
3878 AssertRC(rc);
3879
3880 uint32_t u32BDLEIndex;
3881 for (uint16_t a = 0; a < cBDLE; a++)
3882 {
3883 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Begin marker */
3884 AssertRC(rc);
3885 rc = SSMR3GetU32(pSSM, &u32BDLEIndex); /* u32BDLIndex */
3886 AssertRC(rc);
3887
3888 /* Does the current BDLE index match the current BDLE to process? */
3889 if (u32BDLEIndex == pStream->State.uCurBDLE)
3890 {
3891 rc = SSMR3GetU32(pSSM, &pStream->State.BDLE.State.cbBelowFIFOW); /* cbBelowFIFOW */
3892 AssertRC(rc);
3893 rc = SSMR3Skip(pSSM, sizeof(uint8_t) * 256); /* FIFO, deprecated */
3894 AssertRC(rc);
3895 rc = SSMR3GetU32(pSSM, &pStream->State.BDLE.State.u32BufOff); /* u32BufOff */
3896 AssertRC(rc);
3897 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* End marker */
3898 AssertRC(rc);
3899 }
3900 else /* Skip not current BDLEs. */
3901 {
3902 rc = SSMR3Skip(pSSM, sizeof(uint32_t) /* cbBelowFIFOW */
3903 + sizeof(uint8_t) * 256 /* au8FIFO */
3904 + sizeof(uint32_t) /* u32BufOff */
3905 + sizeof(uint32_t)); /* End marker */
3906 AssertRC(rc);
3907 }
3908 }
3909 }
3910 else
3911 {
3912 rc = SSMR3GetStructEx(pSSM, &pStream->State, sizeof(HDASTREAMSTATE),
3913 0 /* fFlags */, g_aSSMStreamStateFields6, NULL);
3914 if (RT_FAILURE(rc))
3915 break;
3916
3917 /* Get HDABDLEDESC. */
3918 uint32_t uMarker;
3919 rc = SSMR3GetU32(pSSM, &uMarker); /* Begin marker. */
3920 AssertRC(rc);
3921 Assert(uMarker == UINT32_C(0x19200102) /* SSMR3STRUCT_BEGIN */);
3922 rc = SSMR3GetU64(pSSM, &pStream->State.BDLE.Desc.u64BufAddr);
3923 AssertRC(rc);
3924 rc = SSMR3GetU32(pSSM, &pStream->State.BDLE.Desc.u32BufSize);
3925 AssertRC(rc);
3926 bool fFlags = false;
3927 rc = SSMR3GetBool(pSSM, &fFlags); /* Saved states < v7 only stored the IOC as boolean flag. */
3928 AssertRC(rc);
3929 pStream->State.BDLE.Desc.fFlags = fFlags ? HDA_BDLE_FLAG_IOC : 0;
3930 rc = SSMR3GetU32(pSSM, &uMarker); /* End marker. */
3931 AssertRC(rc);
3932 Assert(uMarker == UINT32_C(0x19920406) /* SSMR3STRUCT_END */);
3933
3934 rc = SSMR3GetStructEx(pSSM, &pStream->State.BDLE.State, sizeof(HDABDLESTATE),
3935 0 /* fFlags */, g_aSSMBDLEStateFields6, NULL);
3936 if (RT_FAILURE(rc))
3937 break;
3938
3939 Log2Func(("[SD%RU8] LPIB=%RU32, CBL=%RU32, LVI=%RU32\n",
3940 uStreamID,
3941 HDA_STREAM_REG(pThis, LPIB, uStreamID), HDA_STREAM_REG(pThis, CBL, uStreamID), HDA_STREAM_REG(pThis, LVI, uStreamID)));
3942#ifdef LOG_ENABLED
3943 hdaR3BDLEDumpAll(pThis, pStream->u64BDLBase, pStream->u16LVI + 1);
3944#endif
3945 }
3946
3947 } /* for cStreams */
3948 break;
3949 } /* default */
3950 }
3951
3952 return rc;
3953}
3954
3955/**
3956 * @callback_method_impl{FNSSMDEVLOADEXEC}
3957 */
3958static DECLCALLBACK(int) hdaR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
3959{
3960 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
3961
3962 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
3963
3964 LogRel2(("hdaR3LoadExec: uVersion=%RU32, uPass=0x%x\n", uVersion, uPass));
3965
3966 /*
3967 * Load Codec nodes states.
3968 */
3969 int rc = hdaCodecLoadState(pThis->pCodec, pSSM, uVersion);
3970 if (RT_FAILURE(rc))
3971 {
3972 LogRel(("HDA: Failed loading codec state (version %RU32, pass 0x%x), rc=%Rrc\n", uVersion, uPass, rc));
3973 return rc;
3974 }
3975
3976 if (uVersion < HDA_SSM_VERSION) /* Handle older saved states? */
3977 {
3978 rc = hdaR3LoadExecLegacy(pThis, pSSM, uVersion, uPass);
3979 if (RT_SUCCESS(rc))
3980 rc = hdaR3LoadExecPost(pThis);
3981
3982 return rc;
3983 }
3984
3985 /*
3986 * Load MMIO registers.
3987 */
3988 uint32_t cRegs;
3989 rc = SSMR3GetU32(pSSM, &cRegs); AssertRCReturn(rc, rc);
3990 if (cRegs != RT_ELEMENTS(pThis->au32Regs))
3991 LogRel(("HDA: SSM version cRegs is %RU32, expected %RU32\n", cRegs, RT_ELEMENTS(pThis->au32Regs)));
3992
3993 if (cRegs >= RT_ELEMENTS(pThis->au32Regs))
3994 {
3995 SSMR3GetMem(pSSM, pThis->au32Regs, sizeof(pThis->au32Regs));
3996 SSMR3Skip(pSSM, sizeof(uint32_t) * (cRegs - RT_ELEMENTS(pThis->au32Regs)));
3997 }
3998 else
3999 SSMR3GetMem(pSSM, pThis->au32Regs, sizeof(uint32_t) * cRegs);
4000
4001 /* Make sure to update the base addresses first before initializing any streams down below. */
4002 pThis->u64CORBBase = RT_MAKE_U64(HDA_REG(pThis, CORBLBASE), HDA_REG(pThis, CORBUBASE));
4003 pThis->u64RIRBBase = RT_MAKE_U64(HDA_REG(pThis, RIRBLBASE), HDA_REG(pThis, RIRBUBASE));
4004 pThis->u64DPBase = RT_MAKE_U64(HDA_REG(pThis, DPLBASE) & DPBASE_ADDR_MASK, HDA_REG(pThis, DPUBASE));
4005
4006 /* Also make sure to update the DMA position bit if this was enabled when saving the state. */
4007 pThis->fDMAPosition = RT_BOOL(HDA_REG(pThis, DPLBASE) & RT_BIT_32(0));
4008
4009 /*
4010 * Load controller-specifc internals.
4011 * Don't annoy other team mates (forgot this for state v7).
4012 */
4013 if ( SSMR3HandleRevision(pSSM) >= 116273
4014 || SSMR3HandleVersion(pSSM) >= VBOX_FULL_VERSION_MAKE(5, 2, 0))
4015 {
4016 rc = SSMR3GetU64(pSSM, &pThis->u64WalClk);
4017 AssertRC(rc);
4018
4019 rc = SSMR3GetU8(pSSM, &pThis->u8IRQL);
4020 AssertRC(rc);
4021 }
4022
4023 /*
4024 * Load streams.
4025 */
4026 uint32_t cStreams;
4027 rc = SSMR3GetU32(pSSM, &cStreams);
4028 AssertRC(rc);
4029
4030 if (cStreams > HDA_MAX_STREAMS)
4031 cStreams = HDA_MAX_STREAMS; /* Sanity. */
4032
4033 Log2Func(("cStreams=%RU32\n", cStreams));
4034
4035 /* Load stream states. */
4036 for (uint32_t i = 0; i < cStreams; i++)
4037 {
4038 uint8_t uStreamID;
4039 rc = SSMR3GetU8(pSSM, &uStreamID);
4040 AssertRC(rc);
4041
4042 PHDASTREAM pStream = hdaGetStreamFromSD(pThis, uStreamID);
4043 HDASTREAM StreamDummy;
4044
4045 if (!pStream)
4046 {
4047 pStream = &StreamDummy;
4048 LogRel2(("HDA: Warning: Loading of stream #%RU8 not supported, skipping to load ...\n", uStreamID));
4049 }
4050
4051 rc = hdaR3StreamInit(pStream, uStreamID);
4052 if (RT_FAILURE(rc))
4053 {
4054 LogRel(("HDA: Stream #%RU8: Loading initialization failed, rc=%Rrc\n", uStreamID, rc));
4055 /* Continue. */
4056 }
4057
4058 rc = SSMR3GetStructEx(pSSM, &pStream->State, sizeof(HDASTREAMSTATE),
4059 0 /* fFlags */, g_aSSMStreamStateFields7, NULL);
4060 AssertRC(rc);
4061
4062 /*
4063 * Load BDLEs (Buffer Descriptor List Entries) and DMA counters.
4064 */
4065 rc = SSMR3GetStructEx(pSSM, &pStream->State.BDLE.Desc, sizeof(HDABDLEDESC),
4066 0 /* fFlags */, g_aSSMBDLEDescFields7, NULL);
4067 AssertRC(rc);
4068
4069 rc = SSMR3GetStructEx(pSSM, &pStream->State.BDLE.State, sizeof(HDABDLESTATE),
4070 0 /* fFlags */, g_aSSMBDLEStateFields7, NULL);
4071 AssertRC(rc);
4072
4073 Log2Func(("[SD%RU8] %R[bdle]\n", pStream->u8SD, &pStream->State.BDLE));
4074
4075 /*
4076 * Load period state.
4077 */
4078 hdaR3StreamPeriodInit(&pStream->State.Period,
4079 pStream->u8SD, pStream->u16LVI, pStream->u32CBL, &pStream->State.Cfg);
4080
4081 rc = SSMR3GetStructEx(pSSM, &pStream->State.Period, sizeof(HDASTREAMPERIOD),
4082 0 /* fFlags */, g_aSSMStreamPeriodFields7, NULL);
4083 AssertRC(rc);
4084
4085 /*
4086 * Load internal (FIFO) buffer.
4087 */
4088 uint32_t cbCircBufSize = 0;
4089 rc = SSMR3GetU32(pSSM, &cbCircBufSize); /* cbCircBuf */
4090 AssertRC(rc);
4091
4092 uint32_t cbCircBufUsed = 0;
4093 rc = SSMR3GetU32(pSSM, &cbCircBufUsed); /* cbCircBuf */
4094 AssertRC(rc);
4095
4096 if (cbCircBufSize) /* If 0, skip the buffer. */
4097 {
4098 /* Paranoia. */
4099 AssertReleaseMsg(cbCircBufSize <= _1M,
4100 ("HDA: Saved state contains bogus DMA buffer size (%RU32) for stream #%RU8",
4101 cbCircBufSize, uStreamID));
4102 AssertReleaseMsg(cbCircBufUsed <= cbCircBufSize,
4103 ("HDA: Saved state contains invalid DMA buffer usage (%RU32/%RU32) for stream #%RU8",
4104 cbCircBufUsed, cbCircBufSize, uStreamID));
4105
4106 /* Do we need to cre-create the circular buffer do fit the data size? */
4107 if ( pStream->State.pCircBuf
4108 && cbCircBufSize != (uint32_t)RTCircBufSize(pStream->State.pCircBuf))
4109 {
4110 RTCircBufDestroy(pStream->State.pCircBuf);
4111 pStream->State.pCircBuf = NULL;
4112 }
4113
4114 rc = RTCircBufCreate(&pStream->State.pCircBuf, cbCircBufSize);
4115 AssertRC(rc);
4116
4117 if ( RT_SUCCESS(rc)
4118 && cbCircBufUsed)
4119 {
4120 void *pvBuf;
4121 size_t cbBuf;
4122
4123 RTCircBufAcquireWriteBlock(pStream->State.pCircBuf, cbCircBufUsed, &pvBuf, &cbBuf);
4124
4125 if (cbBuf)
4126 {
4127 rc = SSMR3GetMem(pSSM, pvBuf, cbBuf);
4128 AssertRC(rc);
4129 }
4130
4131 RTCircBufReleaseWriteBlock(pStream->State.pCircBuf, cbBuf);
4132
4133 Assert(cbBuf == cbCircBufUsed);
4134 }
4135 }
4136
4137 Log2Func(("[SD%RU8] LPIB=%RU32, CBL=%RU32, LVI=%RU32\n",
4138 uStreamID,
4139 HDA_STREAM_REG(pThis, LPIB, uStreamID), HDA_STREAM_REG(pThis, CBL, uStreamID), HDA_STREAM_REG(pThis, LVI, uStreamID)));
4140#ifdef LOG_ENABLED
4141 hdaR3BDLEDumpAll(pThis, pStream->u64BDLBase, pStream->u16LVI + 1);
4142#endif
4143 /** @todo (Re-)initialize active periods? */
4144
4145 } /* for cStreams */
4146
4147 rc = hdaR3LoadExecPost(pThis);
4148 AssertRC(rc);
4149
4150 LogFlowFuncLeaveRC(rc);
4151 return rc;
4152}
4153
4154/* IPRT format type handlers. */
4155
4156/**
4157 * @callback_method_impl{FNRTSTRFORMATTYPE}
4158 */
4159static DECLCALLBACK(size_t) hdaR3StrFmtBDLE(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
4160 const char *pszType, void const *pvValue,
4161 int cchWidth, int cchPrecision, unsigned fFlags,
4162 void *pvUser)
4163{
4164 RT_NOREF(pszType, cchWidth, cchPrecision, fFlags, pvUser);
4165 PHDABDLE pBDLE = (PHDABDLE)pvValue;
4166 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
4167 "BDLE(idx:%RU32, off:%RU32, fifow:%RU32, IOC:%RTbool, DMA[%RU32 bytes @ 0x%x])",
4168 pBDLE->State.u32BDLIndex, pBDLE->State.u32BufOff, pBDLE->State.cbBelowFIFOW,
4169 pBDLE->Desc.fFlags & HDA_BDLE_FLAG_IOC, pBDLE->Desc.u32BufSize, pBDLE->Desc.u64BufAddr);
4170}
4171
4172/**
4173 * @callback_method_impl{FNRTSTRFORMATTYPE}
4174 */
4175static DECLCALLBACK(size_t) hdaR3StrFmtSDCTL(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
4176 const char *pszType, void const *pvValue,
4177 int cchWidth, int cchPrecision, unsigned fFlags,
4178 void *pvUser)
4179{
4180 RT_NOREF(pszType, cchWidth, cchPrecision, fFlags, pvUser);
4181 uint32_t uSDCTL = (uint32_t)(uintptr_t)pvValue;
4182 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
4183 "SDCTL(raw:%#x, DIR:%s, TP:%RTbool, STRIPE:%x, DEIE:%RTbool, FEIE:%RTbool, IOCE:%RTbool, RUN:%RTbool, RESET:%RTbool)",
4184 uSDCTL,
4185 uSDCTL & HDA_SDCTL_DIR ? "OUT" : "IN",
4186 RT_BOOL(uSDCTL & HDA_SDCTL_TP),
4187 (uSDCTL & HDA_SDCTL_STRIPE_MASK) >> HDA_SDCTL_STRIPE_SHIFT,
4188 RT_BOOL(uSDCTL & HDA_SDCTL_DEIE),
4189 RT_BOOL(uSDCTL & HDA_SDCTL_FEIE),
4190 RT_BOOL(uSDCTL & HDA_SDCTL_IOCE),
4191 RT_BOOL(uSDCTL & HDA_SDCTL_RUN),
4192 RT_BOOL(uSDCTL & HDA_SDCTL_SRST));
4193}
4194
4195/**
4196 * @callback_method_impl{FNRTSTRFORMATTYPE}
4197 */
4198static DECLCALLBACK(size_t) hdaR3StrFmtSDFIFOS(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
4199 const char *pszType, void const *pvValue,
4200 int cchWidth, int cchPrecision, unsigned fFlags,
4201 void *pvUser)
4202{
4203 RT_NOREF(pszType, cchWidth, cchPrecision, fFlags, pvUser);
4204 uint32_t uSDFIFOS = (uint32_t)(uintptr_t)pvValue;
4205 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "SDFIFOS(raw:%#x, sdfifos:%RU8 B)", uSDFIFOS, uSDFIFOS ? uSDFIFOS + 1 : 0);
4206}
4207
4208/**
4209 * @callback_method_impl{FNRTSTRFORMATTYPE}
4210 */
4211static DECLCALLBACK(size_t) hdaR3StrFmtSDFIFOW(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
4212 const char *pszType, void const *pvValue,
4213 int cchWidth, int cchPrecision, unsigned fFlags,
4214 void *pvUser)
4215{
4216 RT_NOREF(pszType, cchWidth, cchPrecision, fFlags, pvUser);
4217 uint32_t uSDFIFOW = (uint32_t)(uintptr_t)pvValue;
4218 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "SDFIFOW(raw: %#0x, sdfifow:%d B)", uSDFIFOW, hdaSDFIFOWToBytes(uSDFIFOW));
4219}
4220
4221/**
4222 * @callback_method_impl{FNRTSTRFORMATTYPE}
4223 */
4224static DECLCALLBACK(size_t) hdaR3StrFmtSDSTS(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
4225 const char *pszType, void const *pvValue,
4226 int cchWidth, int cchPrecision, unsigned fFlags,
4227 void *pvUser)
4228{
4229 RT_NOREF(pszType, cchWidth, cchPrecision, fFlags, pvUser);
4230 uint32_t uSdSts = (uint32_t)(uintptr_t)pvValue;
4231 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
4232 "SDSTS(raw:%#0x, fifordy:%RTbool, dese:%RTbool, fifoe:%RTbool, bcis:%RTbool)",
4233 uSdSts,
4234 RT_BOOL(uSdSts & HDA_SDSTS_FIFORDY),
4235 RT_BOOL(uSdSts & HDA_SDSTS_DESE),
4236 RT_BOOL(uSdSts & HDA_SDSTS_FIFOE),
4237 RT_BOOL(uSdSts & HDA_SDSTS_BCIS));
4238}
4239
4240/* Debug info dumpers */
4241
4242static int hdaR3DbgLookupRegByName(const char *pszArgs)
4243{
4244 int iReg = 0;
4245 for (; iReg < HDA_NUM_REGS; ++iReg)
4246 if (!RTStrICmp(g_aHdaRegMap[iReg].abbrev, pszArgs))
4247 return iReg;
4248 return -1;
4249}
4250
4251
4252static void hdaR3DbgPrintRegister(PHDASTATE pThis, PCDBGFINFOHLP pHlp, int iHdaIndex)
4253{
4254 Assert( pThis
4255 && iHdaIndex >= 0
4256 && iHdaIndex < HDA_NUM_REGS);
4257 pHlp->pfnPrintf(pHlp, "%s: 0x%x\n", g_aHdaRegMap[iHdaIndex].abbrev, pThis->au32Regs[g_aHdaRegMap[iHdaIndex].mem_idx]);
4258}
4259
4260/**
4261 * @callback_method_impl{FNDBGFHANDLERDEV}
4262 */
4263static DECLCALLBACK(void) hdaR3DbgInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
4264{
4265 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
4266 int iHdaRegisterIndex = hdaR3DbgLookupRegByName(pszArgs);
4267 if (iHdaRegisterIndex != -1)
4268 hdaR3DbgPrintRegister(pThis, pHlp, iHdaRegisterIndex);
4269 else
4270 {
4271 for(iHdaRegisterIndex = 0; (unsigned int)iHdaRegisterIndex < HDA_NUM_REGS; ++iHdaRegisterIndex)
4272 hdaR3DbgPrintRegister(pThis, pHlp, iHdaRegisterIndex);
4273 }
4274}
4275
4276static void hdaR3DbgPrintStream(PHDASTATE pThis, PCDBGFINFOHLP pHlp, int iIdx)
4277{
4278 Assert( pThis
4279 && iIdx >= 0
4280 && iIdx < HDA_MAX_STREAMS);
4281
4282 const PHDASTREAM pStream = &pThis->aStreams[iIdx];
4283
4284 pHlp->pfnPrintf(pHlp, "Stream #%d:\n", iIdx);
4285 pHlp->pfnPrintf(pHlp, "\tSD%dCTL : %R[sdctl]\n", iIdx, HDA_STREAM_REG(pThis, CTL, iIdx));
4286 pHlp->pfnPrintf(pHlp, "\tSD%dCTS : %R[sdsts]\n", iIdx, HDA_STREAM_REG(pThis, STS, iIdx));
4287 pHlp->pfnPrintf(pHlp, "\tSD%dFIFOS: %R[sdfifos]\n", iIdx, HDA_STREAM_REG(pThis, FIFOS, iIdx));
4288 pHlp->pfnPrintf(pHlp, "\tSD%dFIFOW: %R[sdfifow]\n", iIdx, HDA_STREAM_REG(pThis, FIFOW, iIdx));
4289 pHlp->pfnPrintf(pHlp, "\tBDLE : %R[bdle]\n", &pStream->State.BDLE);
4290}
4291
4292static void hdaR3DbgPrintBDLE(PHDASTATE pThis, PCDBGFINFOHLP pHlp, int iIdx)
4293{
4294 Assert( pThis
4295 && iIdx >= 0
4296 && iIdx < HDA_MAX_STREAMS);
4297
4298 const PHDASTREAM pStream = &pThis->aStreams[iIdx];
4299 const PHDABDLE pBDLE = &pStream->State.BDLE;
4300
4301 pHlp->pfnPrintf(pHlp, "Stream #%d BDLE:\n", iIdx);
4302
4303 uint64_t u64BaseDMA = RT_MAKE_U64(HDA_STREAM_REG(pThis, BDPL, iIdx),
4304 HDA_STREAM_REG(pThis, BDPU, iIdx));
4305 uint16_t u16LVI = HDA_STREAM_REG(pThis, LVI, iIdx);
4306 uint32_t u32CBL = HDA_STREAM_REG(pThis, CBL, iIdx);
4307
4308 if (!u64BaseDMA)
4309 return;
4310
4311 pHlp->pfnPrintf(pHlp, "\tCurrent: %R[bdle]\n\n", pBDLE);
4312
4313 pHlp->pfnPrintf(pHlp, "\tMemory:\n");
4314
4315 uint32_t cbBDLE = 0;
4316 for (uint16_t i = 0; i < u16LVI + 1; i++)
4317 {
4318 HDABDLEDESC bd;
4319 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), u64BaseDMA + i * sizeof(HDABDLEDESC), &bd, sizeof(bd));
4320
4321 pHlp->pfnPrintf(pHlp, "\t\t%s #%03d BDLE(adr:0x%llx, size:%RU32, ioc:%RTbool)\n",
4322 pBDLE->State.u32BDLIndex == i ? "*" : " ", i, bd.u64BufAddr, bd.u32BufSize, bd.fFlags & HDA_BDLE_FLAG_IOC);
4323
4324 cbBDLE += bd.u32BufSize;
4325 }
4326
4327 pHlp->pfnPrintf(pHlp, "Total: %RU32 bytes\n", cbBDLE);
4328
4329 if (cbBDLE != u32CBL)
4330 pHlp->pfnPrintf(pHlp, "Warning: %RU32 bytes does not match CBL (%RU32)!\n", cbBDLE, u32CBL);
4331
4332 pHlp->pfnPrintf(pHlp, "DMA counters (base @ 0x%llx):\n", u64BaseDMA);
4333 if (!u64BaseDMA) /* No DMA base given? Bail out. */
4334 {
4335 pHlp->pfnPrintf(pHlp, "\tNo counters found\n");
4336 return;
4337 }
4338
4339 for (int i = 0; i < u16LVI + 1; i++)
4340 {
4341 uint32_t uDMACnt;
4342 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), (pThis->u64DPBase & DPBASE_ADDR_MASK) + (i * 2 * sizeof(uint32_t)),
4343 &uDMACnt, sizeof(uDMACnt));
4344
4345 pHlp->pfnPrintf(pHlp, "\t#%03d DMA @ 0x%x\n", i , uDMACnt);
4346 }
4347}
4348
4349static int hdaR3DbgLookupStrmIdx(PHDASTATE pThis, const char *pszArgs)
4350{
4351 RT_NOREF(pThis, pszArgs);
4352 /** @todo Add args parsing. */
4353 return -1;
4354}
4355
4356/**
4357 * @callback_method_impl{FNDBGFHANDLERDEV}
4358 */
4359static DECLCALLBACK(void) hdaR3DbgInfoStream(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
4360{
4361 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
4362 int iHdaStreamdex = hdaR3DbgLookupStrmIdx(pThis, pszArgs);
4363 if (iHdaStreamdex != -1)
4364 hdaR3DbgPrintStream(pThis, pHlp, iHdaStreamdex);
4365 else
4366 for(iHdaStreamdex = 0; iHdaStreamdex < HDA_MAX_STREAMS; ++iHdaStreamdex)
4367 hdaR3DbgPrintStream(pThis, pHlp, iHdaStreamdex);
4368}
4369
4370/**
4371 * @callback_method_impl{FNDBGFHANDLERDEV}
4372 */
4373static DECLCALLBACK(void) hdaR3DbgInfoBDLE(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
4374{
4375 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
4376 int iHdaStreamdex = hdaR3DbgLookupStrmIdx(pThis, pszArgs);
4377 if (iHdaStreamdex != -1)
4378 hdaR3DbgPrintBDLE(pThis, pHlp, iHdaStreamdex);
4379 else
4380 for (iHdaStreamdex = 0; iHdaStreamdex < HDA_MAX_STREAMS; ++iHdaStreamdex)
4381 hdaR3DbgPrintBDLE(pThis, pHlp, iHdaStreamdex);
4382}
4383
4384/**
4385 * @callback_method_impl{FNDBGFHANDLERDEV}
4386 */
4387static DECLCALLBACK(void) hdaR3DbgInfoCodecNodes(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
4388{
4389 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
4390
4391 if (pThis->pCodec->pfnDbgListNodes)
4392 pThis->pCodec->pfnDbgListNodes(pThis->pCodec, pHlp, pszArgs);
4393 else
4394 pHlp->pfnPrintf(pHlp, "Codec implementation doesn't provide corresponding callback\n");
4395}
4396
4397/**
4398 * @callback_method_impl{FNDBGFHANDLERDEV}
4399 */
4400static DECLCALLBACK(void) hdaR3DbgInfoCodecSelector(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
4401{
4402 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
4403
4404 if (pThis->pCodec->pfnDbgSelector)
4405 pThis->pCodec->pfnDbgSelector(pThis->pCodec, pHlp, pszArgs);
4406 else
4407 pHlp->pfnPrintf(pHlp, "Codec implementation doesn't provide corresponding callback\n");
4408}
4409
4410/**
4411 * @callback_method_impl{FNDBGFHANDLERDEV}
4412 */
4413static DECLCALLBACK(void) hdaR3DbgInfoMixer(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
4414{
4415 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
4416
4417 if (pThis->pMixer)
4418 AudioMixerDebug(pThis->pMixer, pHlp, pszArgs);
4419 else
4420 pHlp->pfnPrintf(pHlp, "Mixer not available\n");
4421}
4422
4423
4424/* PDMIBASE */
4425
4426/**
4427 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
4428 */
4429static DECLCALLBACK(void *) hdaR3QueryInterface(struct PDMIBASE *pInterface, const char *pszIID)
4430{
4431 PHDASTATE pThis = RT_FROM_MEMBER(pInterface, HDASTATE, IBase);
4432 Assert(&pThis->IBase == pInterface);
4433
4434 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->IBase);
4435 return NULL;
4436}
4437
4438
4439/* PDMDEVREG */
4440
4441/**
4442 * Attach command, internal version.
4443 *
4444 * This is called to let the device attach to a driver for a specified LUN
4445 * during runtime. This is not called during VM construction, the device
4446 * constructor has to attach to all the available drivers.
4447 *
4448 * @returns VBox status code.
4449 * @param pThis HDA state.
4450 * @param uLUN The logical unit which is being detached.
4451 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
4452 * @param ppDrv Attached driver instance on success. Optional.
4453 */
4454static int hdaR3AttachInternal(PHDASTATE pThis, unsigned uLUN, uint32_t fFlags, PHDADRIVER *ppDrv)
4455{
4456 RT_NOREF(fFlags);
4457
4458 /*
4459 * Attach driver.
4460 */
4461 char *pszDesc;
4462 if (RTStrAPrintf(&pszDesc, "Audio driver port (HDA) for LUN#%u", uLUN) <= 0)
4463 AssertLogRelFailedReturn(VERR_NO_MEMORY);
4464
4465 PPDMIBASE pDrvBase;
4466 int rc = PDMDevHlpDriverAttach(pThis->pDevInsR3, uLUN,
4467 &pThis->IBase, &pDrvBase, pszDesc);
4468 if (RT_SUCCESS(rc))
4469 {
4470 PHDADRIVER pDrv = (PHDADRIVER)RTMemAllocZ(sizeof(HDADRIVER));
4471 if (pDrv)
4472 {
4473 pDrv->pDrvBase = pDrvBase;
4474 pDrv->pConnector = PDMIBASE_QUERY_INTERFACE(pDrvBase, PDMIAUDIOCONNECTOR);
4475 AssertMsg(pDrv->pConnector != NULL, ("Configuration error: LUN#%u has no host audio interface, rc=%Rrc\n", uLUN, rc));
4476 pDrv->pHDAState = pThis;
4477 pDrv->uLUN = uLUN;
4478
4479 /*
4480 * For now we always set the driver at LUN 0 as our primary
4481 * host backend. This might change in the future.
4482 */
4483 if (pDrv->uLUN == 0)
4484 pDrv->fFlags |= PDMAUDIODRVFLAGS_PRIMARY;
4485
4486 LogFunc(("LUN#%u: pCon=%p, drvFlags=0x%x\n", uLUN, pDrv->pConnector, pDrv->fFlags));
4487
4488 /* Attach to driver list if not attached yet. */
4489 if (!pDrv->fAttached)
4490 {
4491 RTListAppend(&pThis->lstDrv, &pDrv->Node);
4492 pDrv->fAttached = true;
4493 }
4494
4495 if (ppDrv)
4496 *ppDrv = pDrv;
4497 }
4498 else
4499 rc = VERR_NO_MEMORY;
4500 }
4501 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
4502 LogFunc(("No attached driver for LUN #%u\n", uLUN));
4503
4504 if (RT_FAILURE(rc))
4505 {
4506 /* Only free this string on failure;
4507 * must remain valid for the live of the driver instance. */
4508 RTStrFree(pszDesc);
4509 }
4510
4511 LogFunc(("uLUN=%u, fFlags=0x%x, rc=%Rrc\n", uLUN, fFlags, rc));
4512 return rc;
4513}
4514
4515/**
4516 * Detach command, internal version.
4517 *
4518 * This is called to let the device detach from a driver for a specified LUN
4519 * during runtime.
4520 *
4521 * @returns VBox status code.
4522 * @param pThis HDA state.
4523 * @param pDrv Driver to detach from device.
4524 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
4525 */
4526static int hdaR3DetachInternal(PHDASTATE pThis, PHDADRIVER pDrv, uint32_t fFlags)
4527{
4528 RT_NOREF(fFlags);
4529
4530 /* First, remove the driver from our list and destory it's associated streams.
4531 * This also will un-set the driver as a recording source (if associated). */
4532 hdaR3MixerRemoveDrv(pThis, pDrv);
4533
4534 /* Next, search backwards for a capable (attached) driver which now will be the
4535 * new recording source. */
4536 PHDADRIVER pDrvCur;
4537 RTListForEachReverse(&pThis->lstDrv, pDrvCur, HDADRIVER, Node)
4538 {
4539 if (!pDrvCur->pConnector)
4540 continue;
4541
4542 PDMAUDIOBACKENDCFG Cfg;
4543 int rc2 = pDrvCur->pConnector->pfnGetConfig(pDrvCur->pConnector, &Cfg);
4544 if (RT_FAILURE(rc2))
4545 continue;
4546
4547 PHDADRIVERSTREAM pDrvStrm;
4548# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
4549 pDrvStrm = &pDrvCur->MicIn;
4550 if ( pDrvStrm
4551 && pDrvStrm->pMixStrm)
4552 {
4553 rc2 = AudioMixerSinkSetRecordingSource(pThis->SinkMicIn.pMixSink, pDrvStrm->pMixStrm);
4554 if (RT_SUCCESS(rc2))
4555 LogRel2(("HDA: Set new recording source for 'Mic In' to '%s'\n", Cfg.szName));
4556 }
4557# endif
4558 pDrvStrm = &pDrvCur->LineIn;
4559 if ( pDrvStrm
4560 && pDrvStrm->pMixStrm)
4561 {
4562 rc2 = AudioMixerSinkSetRecordingSource(pThis->SinkLineIn.pMixSink, pDrvStrm->pMixStrm);
4563 if (RT_SUCCESS(rc2))
4564 LogRel2(("HDA: Set new recording source for 'Line In' to '%s'\n", Cfg.szName));
4565 }
4566 }
4567
4568 LogFunc(("uLUN=%u, fFlags=0x%x\n", pDrv->uLUN, fFlags));
4569 return VINF_SUCCESS;
4570}
4571
4572/**
4573 * @interface_method_impl{PDMDEVREG,pfnAttach}
4574 */
4575static DECLCALLBACK(int) hdaR3Attach(PPDMDEVINS pDevIns, unsigned uLUN, uint32_t fFlags)
4576{
4577 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
4578
4579 DEVHDA_LOCK_RETURN(pDevIns, pThis, VERR_IGNORED);
4580
4581 LogFunc(("uLUN=%u, fFlags=0x%x\n", uLUN, fFlags));
4582
4583 PHDADRIVER pDrv;
4584 int rc2 = hdaR3AttachInternal(pThis, uLUN, fFlags, &pDrv);
4585 if (RT_SUCCESS(rc2))
4586 rc2 = hdaR3MixerAddDrv(pThis, pDrv);
4587
4588 if (RT_FAILURE(rc2))
4589 LogFunc(("Failed with %Rrc\n", rc2));
4590
4591 DEVHDA_UNLOCK(pDevIns, pThis);
4592
4593 return VINF_SUCCESS;
4594}
4595
4596/**
4597 * @interface_method_impl{PDMDEVREG,pfnDetach}
4598 */
4599static DECLCALLBACK(void) hdaR3Detach(PPDMDEVINS pDevIns, unsigned uLUN, uint32_t fFlags)
4600{
4601 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
4602
4603 DEVHDA_LOCK(pDevIns, pThis);
4604
4605 LogFunc(("uLUN=%u, fFlags=0x%x\n", uLUN, fFlags));
4606
4607 PHDADRIVER pDrv, pDrvNext;
4608 RTListForEachSafe(&pThis->lstDrv, pDrv, pDrvNext, HDADRIVER, Node)
4609 {
4610 if (pDrv->uLUN == uLUN)
4611 {
4612 int rc2 = hdaR3DetachInternal(pThis, pDrv, fFlags);
4613 if (RT_SUCCESS(rc2))
4614 {
4615 RTMemFree(pDrv);
4616 pDrv = NULL;
4617 }
4618
4619 break;
4620 }
4621 }
4622
4623 DEVHDA_UNLOCK(pDevIns, pThis);
4624}
4625
4626/**
4627 * Powers off the device.
4628 *
4629 * @param pDevIns Device instance to power off.
4630 */
4631static DECLCALLBACK(void) hdaR3PowerOff(PPDMDEVINS pDevIns)
4632{
4633 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
4634
4635 DEVHDA_LOCK_RETURN_VOID(pDevIns, pThis);
4636
4637 LogRel2(("HDA: Powering off ...\n"));
4638
4639 /* Ditto goes for the codec, which in turn uses the mixer. */
4640 hdaCodecPowerOff(pThis->pCodec);
4641
4642 /*
4643 * Note: Destroy the mixer while powering off and *not* in hdaR3Destruct,
4644 * giving the mixer the chance to release any references held to
4645 * PDM audio streams it maintains.
4646 */
4647 if (pThis->pMixer)
4648 {
4649 AudioMixerDestroy(pThis->pMixer);
4650 pThis->pMixer = NULL;
4651 }
4652
4653 DEVHDA_UNLOCK(pDevIns, pThis);
4654}
4655
4656/**
4657 * Replaces a driver with a the NullAudio drivers.
4658 *
4659 * @returns VBox status code.
4660 * @param pThis Device instance.
4661 * @param iLun The logical unit which is being replaced.
4662 */
4663static int hdaR3ReconfigLunWithNullAudio(PHDASTATE pThis, unsigned iLun)
4664{
4665 int rc = PDMDevHlpDriverReconfigure2(pThis->pDevInsR3, iLun, "AUDIO", "NullAudio");
4666 if (RT_SUCCESS(rc))
4667 rc = hdaR3AttachInternal(pThis, iLun, 0 /* fFlags */, NULL /* ppDrv */);
4668 LogFunc(("pThis=%p, iLun=%u, rc=%Rrc\n", pThis, iLun, rc));
4669 return rc;
4670}
4671
4672
4673/**
4674 * @interface_method_impl{PDMDEVREG,pfnReset}
4675 */
4676static DECLCALLBACK(void) hdaR3Reset(PPDMDEVINS pDevIns)
4677{
4678 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
4679
4680 LogFlowFuncEnter();
4681
4682 DEVHDA_LOCK_RETURN_VOID(pDevIns, pThis);
4683
4684 /*
4685 * 18.2.6,7 defines that values of this registers might be cleared on power on/reset
4686 * hdaR3Reset shouldn't affects these registers.
4687 */
4688 HDA_REG(pThis, WAKEEN) = 0x0;
4689
4690 hdaR3GCTLReset(pThis);
4691
4692 /* Indicate that HDA is not in reset. The firmware is supposed to (un)reset HDA,
4693 * but we can take a shortcut.
4694 */
4695 HDA_REG(pThis, GCTL) = HDA_GCTL_CRST;
4696
4697 DEVHDA_UNLOCK(pDevIns, pThis);
4698}
4699
4700
4701/**
4702 * @interface_method_impl{PDMDEVREG,pfnDestruct}
4703 */
4704static DECLCALLBACK(int) hdaR3Destruct(PPDMDEVINS pDevIns)
4705{
4706 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns); /* this shall come first */
4707 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
4708 DEVHDA_LOCK(pDevIns, pThis); /** @todo r=bird: this will fail on early constructor failure. */
4709
4710 PHDADRIVER pDrv;
4711 while (!RTListIsEmpty(&pThis->lstDrv))
4712 {
4713 pDrv = RTListGetFirst(&pThis->lstDrv, HDADRIVER, Node);
4714
4715 RTListNodeRemove(&pDrv->Node);
4716 RTMemFree(pDrv);
4717 }
4718
4719 if (pThis->pCodec)
4720 {
4721 hdaCodecDestruct(pThis->pCodec);
4722
4723 RTMemFree(pThis->pCodec);
4724 pThis->pCodec = NULL;
4725 }
4726
4727 RTMemFree(pThis->pu32CorbBuf);
4728 pThis->pu32CorbBuf = NULL;
4729
4730 RTMemFree(pThis->pu64RirbBuf);
4731 pThis->pu64RirbBuf = NULL;
4732
4733 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
4734 hdaR3StreamDestroy(&pThis->aStreams[i]);
4735
4736 DEVHDA_UNLOCK(pDevIns, pThis);
4737 return VINF_SUCCESS;
4738}
4739
4740
4741/**
4742 * @interface_method_impl{PDMDEVREG,pfnConstruct}
4743 */
4744static DECLCALLBACK(int) hdaR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
4745{
4746 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns); /* this shall come first */
4747 PHDASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PHDASTATE);
4748 Assert(iInstance == 0); RT_NOREF(iInstance);
4749
4750 /*
4751 * Initialize the state sufficently to make the destructor work.
4752 */
4753 pThis->uAlignmentCheckMagic = HDASTATE_ALIGNMENT_CHECK_MAGIC;
4754 RTListInit(&pThis->lstDrv);
4755 /** @todo r=bird: There are probably other things which should be
4756 * initialized here before we start failing. */
4757
4758 /*
4759 * Validations.
4760 */
4761 if (!CFGMR3AreValuesValid(pCfg, "RZEnabled\0"
4762 "TimerHz\0"
4763 "PosAdjustEnabled\0"
4764 "PosAdjustFrames\0"
4765 "DebugEnabled\0"
4766 "DebugPathOut\0"))
4767 {
4768 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
4769 N_ ("Invalid configuration for the Intel HDA device"));
4770 }
4771
4772 int rc = CFGMR3QueryBoolDef(pCfg, "RZEnabled", &pThis->fRZEnabled, true);
4773 if (RT_FAILURE(rc))
4774 return PDMDEV_SET_ERROR(pDevIns, rc,
4775 N_("HDA configuration error: failed to read RCEnabled as boolean"));
4776
4777
4778 rc = CFGMR3QueryU16Def(pCfg, "TimerHz", &pThis->uTimerHz, HDA_TIMER_HZ_DEFAULT /* Default value, if not set. */);
4779 if (RT_FAILURE(rc))
4780 return PDMDEV_SET_ERROR(pDevIns, rc,
4781 N_("HDA configuration error: failed to read Hertz (Hz) rate as unsigned integer"));
4782
4783 if (pThis->uTimerHz != HDA_TIMER_HZ_DEFAULT)
4784 LogRel(("HDA: Using custom device timer rate (%RU16Hz)\n", pThis->uTimerHz));
4785
4786 rc = CFGMR3QueryBoolDef(pCfg, "PosAdjustEnabled", &pThis->fPosAdjustEnabled, true);
4787 if (RT_FAILURE(rc))
4788 return PDMDEV_SET_ERROR(pDevIns, rc,
4789 N_("HDA configuration error: failed to read position adjustment enabled as boolean"));
4790
4791 if (!pThis->fPosAdjustEnabled)
4792 LogRel(("HDA: Position adjustment is disabled\n"));
4793
4794 rc = CFGMR3QueryU16Def(pCfg, "PosAdjustFrames", &pThis->cPosAdjustFrames, HDA_POS_ADJUST_DEFAULT);
4795 if (RT_FAILURE(rc))
4796 return PDMDEV_SET_ERROR(pDevIns, rc,
4797 N_("HDA configuration error: failed to read position adjustment frames as unsigned integer"));
4798
4799 if (pThis->cPosAdjustFrames)
4800 LogRel(("HDA: Using custom position adjustment (%RU16 audio frames)\n", pThis->cPosAdjustFrames));
4801
4802 rc = CFGMR3QueryBoolDef(pCfg, "DebugEnabled", &pThis->Dbg.fEnabled, false);
4803 if (RT_FAILURE(rc))
4804 return PDMDEV_SET_ERROR(pDevIns, rc,
4805 N_("HDA configuration error: failed to read debugging enabled flag as boolean"));
4806
4807 rc = CFGMR3QueryStringDef(pCfg, "DebugPathOut", pThis->Dbg.szOutPath, sizeof(pThis->Dbg.szOutPath),
4808 VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH);
4809 if (RT_FAILURE(rc))
4810 return PDMDEV_SET_ERROR(pDevIns, rc,
4811 N_("HDA configuration error: failed to read debugging output path flag as string"));
4812
4813 if (!strlen(pThis->Dbg.szOutPath))
4814 RTStrPrintf(pThis->Dbg.szOutPath, sizeof(pThis->Dbg.szOutPath), VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH);
4815
4816 if (pThis->Dbg.fEnabled)
4817 LogRel2(("HDA: Debug output will be saved to '%s'\n", pThis->Dbg.szOutPath));
4818
4819 /*
4820 * Use our own critical section for the device instead of the default
4821 * one provided by PDM. This allows fine-grained locking in combination
4822 * with TM when timer-specific stuff is being called in e.g. the MMIO handlers.
4823 */
4824 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CritSect, RT_SRC_POS, "HDA");
4825 AssertRCReturn(rc, rc);
4826
4827 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
4828 AssertRCReturn(rc, rc);
4829
4830 /*
4831 * Initialize data (most of it anyway).
4832 */
4833 pThis->pDevInsR3 = pDevIns;
4834 /* IBase */
4835 pThis->IBase.pfnQueryInterface = hdaR3QueryInterface;
4836
4837 /* PCI Device */
4838 PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
4839 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
4840
4841 PDMPciDevSetVendorId( pPciDev, HDA_PCI_VENDOR_ID); /* nVidia */
4842 PDMPciDevSetDeviceId( pPciDev, HDA_PCI_DEVICE_ID); /* HDA */
4843
4844 PDMPciDevSetCommand( pPciDev, 0x0000); /* 04 rw,ro - pcicmd. */
4845 PDMPciDevSetStatus( pPciDev, VBOX_PCI_STATUS_CAP_LIST); /* 06 rwc?,ro? - pcists. */
4846 PDMPciDevSetRevisionId( pPciDev, 0x01); /* 08 ro - rid. */
4847 PDMPciDevSetClassProg( pPciDev, 0x00); /* 09 ro - pi. */
4848 PDMPciDevSetClassSub( pPciDev, 0x03); /* 0a ro - scc; 03 == HDA. */
4849 PDMPciDevSetClassBase( pPciDev, 0x04); /* 0b ro - bcc; 04 == multimedia. */
4850 PDMPciDevSetHeaderType( pPciDev, 0x00); /* 0e ro - headtyp. */
4851 PDMPciDevSetBaseAddress( pPciDev, 0, /* 10 rw - MMIO */
4852 false /* fIoSpace */, false /* fPrefetchable */, true /* f64Bit */, 0x00000000);
4853 PDMPciDevSetInterruptLine( pPciDev, 0x00); /* 3c rw. */
4854 PDMPciDevSetInterruptPin( pPciDev, 0x01); /* 3d ro - INTA#. */
4855
4856#if defined(HDA_AS_PCI_EXPRESS)
4857 PDMPciDevSetCapabilityList(pPciDev, 0x80);
4858#elif defined(VBOX_WITH_MSI_DEVICES)
4859 PDMPciDevSetCapabilityList(pPciDev, 0x60);
4860#else
4861 PDMPciDevSetCapabilityList(pPciDev, 0x50); /* ICH6 datasheet 18.1.16 */
4862#endif
4863
4864 /// @todo r=michaln: If there are really no PDMPciDevSetXx for these, the
4865 /// meaning of these values needs to be properly documented!
4866 /* HDCTL off 0x40 bit 0 selects signaling mode (1-HDA, 0 - Ac97) 18.1.19 */
4867 PDMPciDevSetByte( pPciDev, 0x40, 0x01);
4868
4869 /* Power Management */
4870 PDMPciDevSetByte( pPciDev, 0x50 + 0, VBOX_PCI_CAP_ID_PM);
4871 PDMPciDevSetByte( pPciDev, 0x50 + 1, 0x0); /* next */
4872 PDMPciDevSetWord( pPciDev, 0x50 + 2, VBOX_PCI_PM_CAP_DSI | 0x02 /* version, PM1.1 */ );
4873
4874#ifdef HDA_AS_PCI_EXPRESS
4875 /* PCI Express */
4876 PDMPciDevSetByte( pPciDev, 0x80 + 0, VBOX_PCI_CAP_ID_EXP); /* PCI_Express */
4877 PDMPciDevSetByte( pPciDev, 0x80 + 1, 0x60); /* next */
4878 /* Device flags */
4879 PDMPciDevSetWord( pPciDev, 0x80 + 2,
4880 1 /* version */
4881 | (VBOX_PCI_EXP_TYPE_ROOT_INT_EP << 4) /* Root Complex Integrated Endpoint */
4882 | (100 << 9) /* MSI */ );
4883 /* Device capabilities */
4884 PDMPciDevSetDWord( pPciDev, 0x80 + 4, VBOX_PCI_EXP_DEVCAP_FLRESET);
4885 /* Device control */
4886 PDMPciDevSetWord( pPciDev, 0x80 + 8, 0);
4887 /* Device status */
4888 PDMPciDevSetWord( pPciDev, 0x80 + 10, 0);
4889 /* Link caps */
4890 PDMPciDevSetDWord( pPciDev, 0x80 + 12, 0);
4891 /* Link control */
4892 PDMPciDevSetWord( pPciDev, 0x80 + 16, 0);
4893 /* Link status */
4894 PDMPciDevSetWord( pPciDev, 0x80 + 18, 0);
4895 /* Slot capabilities */
4896 PDMPciDevSetDWord( pPciDev, 0x80 + 20, 0);
4897 /* Slot control */
4898 PDMPciDevSetWord( pPciDev, 0x80 + 24, 0);
4899 /* Slot status */
4900 PDMPciDevSetWord( pPciDev, 0x80 + 26, 0);
4901 /* Root control */
4902 PDMPciDevSetWord( pPciDev, 0x80 + 28, 0);
4903 /* Root capabilities */
4904 PDMPciDevSetWord( pPciDev, 0x80 + 30, 0);
4905 /* Root status */
4906 PDMPciDevSetDWord( pPciDev, 0x80 + 32, 0);
4907 /* Device capabilities 2 */
4908 PDMPciDevSetDWord( pPciDev, 0x80 + 36, 0);
4909 /* Device control 2 */
4910 PDMPciDevSetQWord( pPciDev, 0x80 + 40, 0);
4911 /* Link control 2 */
4912 PDMPciDevSetQWord( pPciDev, 0x80 + 48, 0);
4913 /* Slot control 2 */
4914 PDMPciDevSetWord( pPciDev, 0x80 + 56, 0);
4915#endif
4916
4917 /*
4918 * Register the PCI device.
4919 */
4920 rc = PDMDevHlpPCIRegister(pDevIns, pPciDev);
4921 AssertRCReturn(rc, rc);
4922
4923 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 0, 0x4000, PCI_ADDRESS_SPACE_MEM, hdaR3PciIoRegionMap);
4924 AssertRCReturn(rc, rc);
4925
4926#ifdef VBOX_WITH_MSI_DEVICES
4927 PDMMSIREG MsiReg;
4928 RT_ZERO(MsiReg);
4929 MsiReg.cMsiVectors = 1;
4930 MsiReg.iMsiCapOffset = 0x60;
4931 MsiReg.iMsiNextOffset = 0x50;
4932 rc = PDMDevHlpPCIRegisterMsi(pDevIns, &MsiReg);
4933 if (RT_FAILURE(rc))
4934 {
4935 /* That's OK, we can work without MSI */
4936 PDMPciDevSetCapabilityList(pPciDev, 0x50);
4937 }
4938#endif
4939
4940 rc = PDMDevHlpSSMRegister(pDevIns, HDA_SSM_VERSION, sizeof(*pThis), hdaR3SaveExec, hdaR3LoadExec);
4941 AssertRCReturn(rc, rc);
4942
4943#ifdef VBOX_WITH_AUDIO_HDA_ASYNC_IO
4944 LogRel(("HDA: Asynchronous I/O enabled\n"));
4945#endif
4946
4947 /*
4948 * Attach drivers. We ASSUME they are configured consecutively without any
4949 * gaps, so we stop when we hit the first LUN w/o a driver configured.
4950 */
4951 for (unsigned iLun = 0; ; iLun++)
4952 {
4953 AssertBreak(iLun < UINT8_MAX);
4954 LogFunc(("Trying to attach driver for LUN#%u ...\n", iLun));
4955 rc = hdaR3AttachInternal(pThis, iLun, 0 /* fFlags */, NULL /* ppDrv */);
4956 if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
4957 {
4958 LogFunc(("cLUNs=%u\n", iLun));
4959 break;
4960 }
4961 if (rc == VERR_AUDIO_BACKEND_INIT_FAILED)
4962 {
4963 hdaR3ReconfigLunWithNullAudio(pThis, iLun); /* Pretend attaching to the NULL audio backend will never fail. */
4964 PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "HostAudioNotResponding",
4965 N_("Host audio backend initialization has failed. Selecting the NULL audio backend with the consequence that no sound is audible"));
4966 }
4967 else
4968 AssertLogRelMsgReturn(RT_SUCCESS(rc), ("LUN#%u: rc=%Rrc\n", iLun, rc), rc);
4969 }
4970
4971 /*
4972 * Create the mixer.
4973 */
4974 rc = AudioMixerCreate("HDA Mixer", 0 /* uFlags */, &pThis->pMixer);
4975 AssertRCReturn(rc, rc);
4976
4977 /*
4978 * Add mixer output sinks.
4979 */
4980#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
4981 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] Front", AUDMIXSINKDIR_OUTPUT, &pThis->SinkFront.pMixSink);
4982 AssertRCReturn(rc, rc);
4983 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] Center / Subwoofer", AUDMIXSINKDIR_OUTPUT, &pThis->SinkCenterLFE.pMixSink);
4984 AssertRCReturn(rc, rc);
4985 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] Rear", AUDMIXSINKDIR_OUTPUT, &pThis->SinkRear.pMixSink);
4986 AssertRCReturn(rc, rc);
4987#else
4988 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] PCM Output", AUDMIXSINKDIR_OUTPUT, &pThis->SinkFront.pMixSink);
4989 AssertRCReturn(rc, rc);
4990#endif
4991
4992 /*
4993 * Add mixer input sinks.
4994 */
4995 rc = AudioMixerCreateSink(pThis->pMixer, "[Recording] Line In", AUDMIXSINKDIR_INPUT, &pThis->SinkLineIn.pMixSink);
4996 AssertRCReturn(rc, rc);
4997#ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
4998 rc = AudioMixerCreateSink(pThis->pMixer, "[Recording] Microphone In", AUDMIXSINKDIR_INPUT, &pThis->SinkMicIn.pMixSink);
4999 AssertRCReturn(rc, rc);
5000#endif
5001
5002 /* There is no master volume control. Set the master to max. */
5003 PDMAUDIOVOLUME vol = { false, 255, 255 };
5004 rc = AudioMixerSetMasterVolume(pThis->pMixer, &vol);
5005 AssertRCReturn(rc, rc);
5006
5007 /* Allocate CORB buffer. */
5008 pThis->cbCorbBuf = HDA_CORB_SIZE * HDA_CORB_ELEMENT_SIZE;
5009 pThis->pu32CorbBuf = (uint32_t *)RTMemAllocZ(pThis->cbCorbBuf);
5010 AssertReturn(pThis->pu32CorbBuf, VERR_NO_MEMORY);
5011
5012 /* Allocate RIRB buffer. */
5013 pThis->cbRirbBuf = HDA_RIRB_SIZE * HDA_RIRB_ELEMENT_SIZE;
5014 pThis->pu64RirbBuf = (uint64_t *)RTMemAllocZ(pThis->cbRirbBuf);
5015 AssertReturn(pThis->pu64RirbBuf, VERR_NO_MEMORY);
5016
5017 /* Allocate codec. */
5018 pThis->pCodec = (PHDACODEC)RTMemAllocZ(sizeof(HDACODEC));
5019 AssertReturn(pThis->pCodec, VERR_NO_MEMORY);
5020
5021 /* Set codec callbacks to this controller. */
5022 pThis->pCodec->pfnCbMixerAddStream = hdaR3MixerAddStream;
5023 pThis->pCodec->pfnCbMixerRemoveStream = hdaR3MixerRemoveStream;
5024 pThis->pCodec->pfnCbMixerControl = hdaR3MixerControl;
5025 pThis->pCodec->pfnCbMixerSetVolume = hdaR3MixerSetVolume;
5026
5027 pThis->pCodec->pHDAState = pThis; /* Assign HDA controller state to codec. */
5028
5029 /* Construct the codec. */
5030 rc = hdaCodecConstruct(pDevIns, pThis->pCodec, 0 /* Codec index */, pCfg);
5031 AssertRCReturn(rc, rc);
5032
5033 /* ICH6 datasheet defines 0 values for SVID and SID (18.1.14-15), which together with values returned for
5034 verb F20 should provide device/codec recognition. */
5035 Assert(pThis->pCodec->u16VendorId);
5036 Assert(pThis->pCodec->u16DeviceId);
5037 PDMPciDevSetSubSystemVendorId(pPciDev, pThis->pCodec->u16VendorId); /* 2c ro - intel.) */
5038 PDMPciDevSetSubSystemId( pPciDev, pThis->pCodec->u16DeviceId); /* 2e ro. */
5039
5040 /*
5041 * Create all hardware streams.
5042 */
5043 static const char * const s_apszNames[] =
5044 {
5045 "HDA SD0", "HDA SD1", "HDA SD2", "HDA SD3",
5046 "HDA SD4", "HDA SD5", "HDA SD6", "HDA SD7",
5047 };
5048 AssertCompile(RT_ELEMENTS(s_apszNames) == HDA_MAX_STREAMS);
5049 for (uint8_t i = 0; i < HDA_MAX_STREAMS; ++i)
5050 {
5051 /* Create the emulation timer (per stream).
5052 *
5053 * Note: Use TMCLOCK_VIRTUAL_SYNC here, as the guest's HDA driver
5054 * relies on exact (virtual) DMA timing and uses DMA Position Buffers
5055 * instead of the LPIB registers.
5056 */
5057 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL_SYNC, hdaR3Timer, &pThis->aStreams[i],
5058 TMTIMER_FLAGS_NO_CRIT_SECT, s_apszNames[i], &pThis->pTimer[i]);
5059 AssertRCReturn(rc, rc);
5060
5061 /* Use our own critcal section for the device timer.
5062 * That way we can control more fine-grained when to lock what. */
5063 rc = TMR3TimerSetCritSect(pThis->pTimer[i], &pThis->CritSect);
5064 AssertRCReturn(rc, rc);
5065
5066 rc = hdaR3StreamCreate(&pThis->aStreams[i], pThis, i /* u8SD */);
5067 AssertRCReturn(rc, rc);
5068 }
5069
5070#ifdef VBOX_WITH_AUDIO_HDA_ONETIME_INIT
5071 /*
5072 * Initialize the driver chain.
5073 */
5074 PHDADRIVER pDrv;
5075 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
5076 {
5077 /*
5078 * Only primary drivers are critical for the VM to run. Everything else
5079 * might not worth showing an own error message box in the GUI.
5080 */
5081 if (!(pDrv->fFlags & PDMAUDIODRVFLAGS_PRIMARY))
5082 continue;
5083
5084 PPDMIAUDIOCONNECTOR pCon = pDrv->pConnector;
5085 AssertPtr(pCon);
5086
5087 bool fValidLineIn = AudioMixerStreamIsValid(pDrv->LineIn.pMixStrm);
5088# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
5089 bool fValidMicIn = AudioMixerStreamIsValid(pDrv->MicIn.pMixStrm);
5090# endif
5091 bool fValidOut = AudioMixerStreamIsValid(pDrv->Front.pMixStrm);
5092# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
5093 /** @todo Anything to do here? */
5094# endif
5095
5096 if ( !fValidLineIn
5097# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
5098 && !fValidMicIn
5099# endif
5100 && !fValidOut)
5101 {
5102 LogRel(("HDA: Falling back to NULL backend (no sound audible)\n"));
5103 hdaR3Reset(pDevIns);
5104 hdaR3ReconfigLunWithNullAudio(pThis, pDrv->uLUN);
5105 PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "HostAudioNotResponding",
5106 N_("No audio devices could be opened. "
5107 "Selecting the NULL audio backend with the consequence that no sound is audible"));
5108 }
5109 else
5110 {
5111 bool fWarn = false;
5112
5113 PDMAUDIOBACKENDCFG BackendCfg;
5114 int rc2 = pCon->pfnGetConfig(pCon, &BackendCfg);
5115 if (RT_SUCCESS(rc2))
5116 {
5117 if (BackendCfg.cMaxStreamsIn)
5118 {
5119# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
5120 /* If the audio backend supports two or more input streams at once,
5121 * warn if one of our two inputs (microphone-in and line-in) failed to initialize. */
5122 if (BackendCfg.cMaxStreamsIn >= 2)
5123 fWarn = !fValidLineIn || !fValidMicIn;
5124 /* If the audio backend only supports one input stream at once (e.g. pure ALSA, and
5125 * *not* ALSA via PulseAudio plugin!), only warn if both of our inputs failed to initialize.
5126 * One of the two simply is not in use then. */
5127 else if (BackendCfg.cMaxStreamsIn == 1)
5128 fWarn = !fValidLineIn && !fValidMicIn;
5129 /* Don't warn if our backend is not able of supporting any input streams at all. */
5130# else /* !VBOX_WITH_AUDIO_HDA_MIC_IN */
5131 /* We only have line-in as input source. */
5132 fWarn = !fValidLineIn;
5133# endif /* !VBOX_WITH_AUDIO_HDA_MIC_IN */
5134 }
5135
5136 if ( !fWarn
5137 && BackendCfg.cMaxStreamsOut)
5138 fWarn = !fValidOut;
5139 }
5140 else
5141 {
5142 LogRel(("HDA: Unable to retrieve audio backend configuration for LUN #%RU8, rc=%Rrc\n", pDrv->uLUN, rc2));
5143 fWarn = true;
5144 }
5145
5146 if (fWarn)
5147 {
5148 char szMissingStreams[255];
5149 size_t len = 0;
5150 if (!fValidLineIn)
5151 {
5152 LogRel(("HDA: WARNING: Unable to open PCM line input for LUN #%RU8!\n", pDrv->uLUN));
5153 len = RTStrPrintf(szMissingStreams, sizeof(szMissingStreams), "PCM Input");
5154 }
5155# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
5156 if (!fValidMicIn)
5157 {
5158 LogRel(("HDA: WARNING: Unable to open PCM microphone input for LUN #%RU8!\n", pDrv->uLUN));
5159 len += RTStrPrintf(szMissingStreams + len,
5160 sizeof(szMissingStreams) - len, len ? ", PCM Microphone" : "PCM Microphone");
5161 }
5162# endif
5163 if (!fValidOut)
5164 {
5165 LogRel(("HDA: WARNING: Unable to open PCM output for LUN #%RU8!\n", pDrv->uLUN));
5166 len += RTStrPrintf(szMissingStreams + len,
5167 sizeof(szMissingStreams) - len, len ? ", PCM Output" : "PCM Output");
5168 }
5169
5170 PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "HostAudioNotResponding",
5171 N_("Some HDA audio streams (%s) could not be opened. "
5172 "Guest applications generating audio output or depending on audio input may hang. "
5173 "Make sure your host audio device is working properly. "
5174 "Check the logfile for error messages of the audio subsystem"), szMissingStreams);
5175 }
5176 }
5177 }
5178#endif /* VBOX_WITH_AUDIO_HDA_ONETIME_INIT */
5179
5180 hdaR3Reset(pDevIns);
5181
5182 /*
5183 * Info items and string formatter types. The latter is non-optional as
5184 * the info handles use (at least some of) the custom types and we cannot
5185 * accept screwing formatting.
5186 */
5187 PDMDevHlpDBGFInfoRegister(pDevIns, "hda", "HDA info. (hda [register case-insensitive])", hdaR3DbgInfo);
5188 PDMDevHlpDBGFInfoRegister(pDevIns, "hdabdle", "HDA stream BDLE info. (hdabdle [stream number])", hdaR3DbgInfoBDLE);
5189 PDMDevHlpDBGFInfoRegister(pDevIns, "hdastream", "HDA stream info. (hdastream [stream number])", hdaR3DbgInfoStream);
5190 PDMDevHlpDBGFInfoRegister(pDevIns, "hdcnodes", "HDA codec nodes.", hdaR3DbgInfoCodecNodes);
5191 PDMDevHlpDBGFInfoRegister(pDevIns, "hdcselector", "HDA codec's selector states [node number].", hdaR3DbgInfoCodecSelector);
5192 PDMDevHlpDBGFInfoRegister(pDevIns, "hdamixer", "HDA mixer state.", hdaR3DbgInfoMixer);
5193
5194 rc = RTStrFormatTypeRegister("bdle", hdaR3StrFmtBDLE, NULL);
5195 AssertMsgReturn(RT_SUCCESS(rc) || rc == VERR_ALREADY_EXISTS, ("%Rrc\n", rc), rc);
5196 rc = RTStrFormatTypeRegister("sdctl", hdaR3StrFmtSDCTL, NULL);
5197 AssertMsgReturn(RT_SUCCESS(rc) || rc == VERR_ALREADY_EXISTS, ("%Rrc\n", rc), rc);
5198 rc = RTStrFormatTypeRegister("sdsts", hdaR3StrFmtSDSTS, NULL);
5199 AssertMsgReturn(RT_SUCCESS(rc) || rc == VERR_ALREADY_EXISTS, ("%Rrc\n", rc), rc);
5200 rc = RTStrFormatTypeRegister("sdfifos", hdaR3StrFmtSDFIFOS, NULL);
5201 AssertMsgReturn(RT_SUCCESS(rc) || rc == VERR_ALREADY_EXISTS, ("%Rrc\n", rc), rc);
5202 rc = RTStrFormatTypeRegister("sdfifow", hdaR3StrFmtSDFIFOW, NULL);
5203 AssertMsgReturn(RT_SUCCESS(rc) || rc == VERR_ALREADY_EXISTS, ("%Rrc\n", rc), rc);
5204
5205 /*
5206 * Asserting sanity.
5207 */
5208 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegMap); i++)
5209 {
5210 struct HDAREGDESC const *pReg = &g_aHdaRegMap[i];
5211 struct HDAREGDESC const *pNextReg = i + 1 < RT_ELEMENTS(g_aHdaRegMap) ? &g_aHdaRegMap[i + 1] : NULL;
5212
5213 /* binary search order. */
5214 AssertReleaseMsg(!pNextReg || pReg->offset + pReg->size <= pNextReg->offset,
5215 ("[%#x] = {%#x LB %#x} vs. [%#x] = {%#x LB %#x}\n",
5216 i, pReg->offset, pReg->size, i + 1, pNextReg->offset, pNextReg->size));
5217
5218 /* alignment. */
5219 AssertReleaseMsg( pReg->size == 1
5220 || (pReg->size == 2 && (pReg->offset & 1) == 0)
5221 || (pReg->size == 3 && (pReg->offset & 3) == 0)
5222 || (pReg->size == 4 && (pReg->offset & 3) == 0),
5223 ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
5224
5225 /* registers are packed into dwords - with 3 exceptions with gaps at the end of the dword. */
5226 AssertRelease(((pReg->offset + pReg->size) & 3) == 0 || pNextReg);
5227 if (pReg->offset & 3)
5228 {
5229 struct HDAREGDESC const *pPrevReg = i > 0 ? &g_aHdaRegMap[i - 1] : NULL;
5230 AssertReleaseMsg(pPrevReg, ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
5231 if (pPrevReg)
5232 AssertReleaseMsg(pPrevReg->offset + pPrevReg->size == pReg->offset,
5233 ("[%#x] = {%#x LB %#x} vs. [%#x] = {%#x LB %#x}\n",
5234 i - 1, pPrevReg->offset, pPrevReg->size, i + 1, pReg->offset, pReg->size));
5235 }
5236#if 0
5237 if ((pReg->offset + pReg->size) & 3)
5238 {
5239 AssertReleaseMsg(pNextReg, ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
5240 if (pNextReg)
5241 AssertReleaseMsg(pReg->offset + pReg->size == pNextReg->offset,
5242 ("[%#x] = {%#x LB %#x} vs. [%#x] = {%#x LB %#x}\n",
5243 i, pReg->offset, pReg->size, i + 1, pNextReg->offset, pNextReg->size));
5244 }
5245#endif
5246 /* The final entry is a full DWORD, no gaps! Allows shortcuts. */
5247 AssertReleaseMsg(pNextReg || ((pReg->offset + pReg->size) & 3) == 0,
5248 ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
5249 }
5250
5251# ifdef VBOX_WITH_STATISTICS
5252 /*
5253 * Register statistics.
5254 */
5255 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTimer, STAMTYPE_PROFILE, "/Devices/HDA/Timer", STAMUNIT_TICKS_PER_CALL, "Profiling hdaR3Timer.");
5256 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIn, STAMTYPE_PROFILE, "/Devices/HDA/Input", STAMUNIT_TICKS_PER_CALL, "Profiling input.");
5257 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatOut, STAMTYPE_PROFILE, "/Devices/HDA/Output", STAMUNIT_TICKS_PER_CALL, "Profiling output.");
5258 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, "/Devices/HDA/BytesRead" , STAMUNIT_BYTES, "Bytes read from HDA emulation.");
5259 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, "/Devices/HDA/BytesWritten", STAMUNIT_BYTES, "Bytes written to HDA emulation.");
5260# endif
5261
5262 return VINF_SUCCESS;
5263}
5264
5265#endif /* IN_RING3 */
5266
5267/**
5268 * The device registration structure.
5269 */
5270const PDMDEVREG g_DeviceHDA =
5271{
5272 /* .u32Version = */ PDM_DEVREG_VERSION,
5273 /* .uReserved0 = */ 0,
5274 /* .szName = */ "hda",
5275 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ,
5276 /* .fClass = */ PDM_DEVREG_CLASS_AUDIO,
5277 /* .cMaxInstances = */ 1,
5278 /* .uSharedVersion = */ 42,
5279 /* .cbInstanceShared = */ sizeof(HDASTATE),
5280 /* .cbInstanceCC = */ 0,
5281 /* .cbInstanceRC = */ 0,
5282 /* .cMaxPciDevices = */ 1,
5283 /* .cMaxMsixVectors = */ 0,
5284 /* .pszDescription = */ "Intel HD Audio Controller",
5285#if defined(IN_RING3)
5286 /* .pszRCMod = */ "VBoxDDRC.rc",
5287 /* .pszR0Mod = */ "VBoxDDR0.r0",
5288 /* .pfnConstruct = */ hdaR3Construct,
5289 /* .pfnDestruct = */ hdaR3Destruct,
5290 /* .pfnRelocate = */ NULL,
5291 /* .pfnMemSetup = */ NULL,
5292 /* .pfnPowerOn = */ NULL,
5293 /* .pfnReset = */ hdaR3Reset,
5294 /* .pfnSuspend = */ NULL,
5295 /* .pfnResume = */ NULL,
5296 /* .pfnAttach = */ hdaR3Attach,
5297 /* .pfnDetach = */ hdaR3Detach,
5298 /* .pfnQueryInterface = */ NULL,
5299 /* .pfnInitComplete = */ NULL,
5300 /* .pfnPowerOff = */ hdaR3PowerOff,
5301 /* .pfnSoftReset = */ NULL,
5302 /* .pfnReserved0 = */ NULL,
5303 /* .pfnReserved1 = */ NULL,
5304 /* .pfnReserved2 = */ NULL,
5305 /* .pfnReserved3 = */ NULL,
5306 /* .pfnReserved4 = */ NULL,
5307 /* .pfnReserved5 = */ NULL,
5308 /* .pfnReserved6 = */ NULL,
5309 /* .pfnReserved7 = */ NULL,
5310#elif defined(IN_RING0)
5311 /* .pfnEarlyConstruct = */ NULL,
5312 /* .pfnConstruct = */ NULL,
5313 /* .pfnDestruct = */ NULL,
5314 /* .pfnFinalDestruct = */ NULL,
5315 /* .pfnRequest = */ NULL,
5316 /* .pfnReserved0 = */ NULL,
5317 /* .pfnReserved1 = */ NULL,
5318 /* .pfnReserved2 = */ NULL,
5319 /* .pfnReserved3 = */ NULL,
5320 /* .pfnReserved4 = */ NULL,
5321 /* .pfnReserved5 = */ NULL,
5322 /* .pfnReserved6 = */ NULL,
5323 /* .pfnReserved7 = */ NULL,
5324#elif defined(IN_RC)
5325 /* .pfnConstruct = */ NULL,
5326 /* .pfnReserved0 = */ NULL,
5327 /* .pfnReserved1 = */ NULL,
5328 /* .pfnReserved2 = */ NULL,
5329 /* .pfnReserved3 = */ NULL,
5330 /* .pfnReserved4 = */ NULL,
5331 /* .pfnReserved5 = */ NULL,
5332 /* .pfnReserved6 = */ NULL,
5333 /* .pfnReserved7 = */ NULL,
5334#else
5335# error "Not in IN_RING3, IN_RING0 or IN_RC!"
5336#endif
5337 /* .u32VersionEnd = */ PDM_DEVREG_VERSION
5338};
5339
5340#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
5341
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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