VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DevIchHda.cpp@ 61888

Last change on this file since 61888 was 61888, checked in by vboxsync, 8 years ago

Audio: Renamed PDMAUDIOMIXERCTL_VOLUME -> PDMAUDIOMIXERCTL_VOLUME_MASTER.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 223.8 KB
Line 
1/* $Id: DevIchHda.cpp 61888 2016-06-27 08:32:31Z vboxsync $ */
2/** @file
3 * DevIchHda - VBox ICH 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-2016 Oracle Corporation
12 *
13 * This file is part of VirtualBox Open Source Edition (OSE), as
14 * available from http://www.virtualbox.org. This file is free software;
15 * you can redistribute it and/or modify it under the terms of the GNU
16 * General Public License (GPL) as published by the Free Software
17 * Foundation, in version 2 as it comes in the "COPYING" file of the
18 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
19 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
20 */
21
22
23/*********************************************************************************************************************************
24* Header Files *
25*********************************************************************************************************************************/
26#define LOG_GROUP LOG_GROUP_DEV_HDA
27#include <VBox/log.h>
28#include <VBox/vmm/pdmdev.h>
29#include <VBox/vmm/pdmaudioifs.h>
30#include <VBox/version.h>
31
32#include <iprt/assert.h>
33#include <iprt/asm.h>
34#include <iprt/asm-math.h>
35#include <iprt/file.h>
36#include <iprt/list.h>
37#ifdef IN_RING3
38# include <iprt/mem.h>
39# include <iprt/semaphore.h>
40# include <iprt/string.h>
41# include <iprt/uuid.h>
42#endif
43
44#include "VBoxDD.h"
45
46#include "AudioMixBuffer.h"
47#include "AudioMixer.h"
48#include "DevIchHdaCodec.h"
49#include "DevIchHdaCommon.h"
50#include "DrvAudio.h"
51
52
53/*********************************************************************************************************************************
54* Defined Constants And Macros *
55*********************************************************************************************************************************/
56//#define HDA_AS_PCI_EXPRESS
57#define VBOX_WITH_INTEL_HDA
58
59#ifdef DEBUG_andy
60/*
61 * HDA_DEBUG_DUMP_PCM_DATA enables dumping the raw PCM data
62 * to a file on the host. Be sure to adjust HDA_DEBUG_DUMP_PCM_DATA_PATH
63 * to your needs before using this!
64 */
65# define HDA_DEBUG_DUMP_PCM_DATA
66# ifdef RT_OS_WINDOWS
67# define HDA_DEBUG_DUMP_PCM_DATA_PATH "c:\\temp\\"
68# else
69# define HDA_DEBUG_DUMP_PCM_DATA_PATH "/tmp/"
70# endif
71
72/* Enables experimental support for separate mic-in handling.
73 Do not enable this yet for regular builds, as this needs more testing first! */
74//# define VBOX_WITH_HDA_MIC_IN
75#endif
76
77#if defined(VBOX_WITH_HP_HDA)
78/* HP Pavilion dv4t-1300 */
79# define HDA_PCI_VENDOR_ID 0x103c
80# define HDA_PCI_DEVICE_ID 0x30f7
81#elif defined(VBOX_WITH_INTEL_HDA)
82/* Intel HDA controller */
83# define HDA_PCI_VENDOR_ID 0x8086
84# define HDA_PCI_DEVICE_ID 0x2668
85#elif defined(VBOX_WITH_NVIDIA_HDA)
86/* nVidia HDA controller */
87# define HDA_PCI_VENDOR_ID 0x10de
88# define HDA_PCI_DEVICE_ID 0x0ac0
89#else
90# error "Please specify your HDA device vendor/device IDs"
91#endif
92
93/** @todo r=bird: Looking at what the linux driver (accidentally?) does when
94 * updating CORBWP, I belive that the ICH6 datahsheet is wrong and that CORBRP
95 * is read only except for bit 15 like the HDA spec states.
96 *
97 * Btw. the CORBRPRST implementation is incomplete according to both docs (sw
98 * writes 1, hw sets it to 1 (after completion), sw reads 1, sw writes 0). */
99#define BIRD_THINKS_CORBRP_IS_MOSTLY_RO
100
101/* Make sure that interleaving streams support is enabled if the 5.1 code is being used. */
102#if defined (VBOX_WITH_HDA_51_SURROUND) && !defined(VBOX_WITH_HDA_INTERLEAVING_STREAMS_SUPPORT)
103# define VBOX_WITH_HDA_INTERLEAVING_STREAMS_SUPPORT
104#endif
105
106/**
107 * At the moment we support 4 input + 4 output streams max, which is 8 in total.
108 * Bidirectional streams are currently *not* supported.
109 *
110 * Note: When changing any of those values, be prepared for some saved state
111 * fixups / trouble!
112 */
113#define HDA_MAX_SDI 4
114#define HDA_MAX_SDO 4
115#define HDA_MAX_STREAMS (HDA_MAX_SDI + HDA_MAX_SDO)
116AssertCompile(HDA_MAX_SDI <= HDA_MAX_SDO);
117
118/** Number of general registers. */
119#define HDA_NUM_GENERAL_REGS 34
120/** Number of total registers in the HDA's register map. */
121#define HDA_NUM_REGS (HDA_NUM_GENERAL_REGS + (HDA_MAX_STREAMS * 10 /* Each stream descriptor has 10 registers */))
122/** Total number of stream tags (channels). Index 0 is reserved / invalid. */
123#define HDA_MAX_TAGS 16
124
125/**
126 * NB: Register values stored in memory (au32Regs[]) are indexed through
127 * the HDA_RMX_xxx macros (also HDA_MEM_IND_NAME()). On the other hand, the
128 * register descriptors in g_aHdaRegMap[] are indexed through the
129 * HDA_REG_xxx macros (also HDA_REG_IND_NAME()).
130 *
131 * The au32Regs[] layout is kept unchanged for saved state
132 * compatibility.
133 */
134
135/* Registers */
136#define HDA_REG_IND_NAME(x) HDA_REG_##x
137#define HDA_MEM_IND_NAME(x) HDA_RMX_##x
138#define HDA_REG_FIELD_MASK(reg, x) HDA_##reg##_##x##_MASK
139#define HDA_REG_FIELD_FLAG_MASK(reg, x) RT_BIT(HDA_##reg##_##x##_SHIFT)
140#define HDA_REG_FIELD_SHIFT(reg, x) HDA_##reg##_##x##_SHIFT
141#define HDA_REG_IND(pThis, x) ((pThis)->au32Regs[g_aHdaRegMap[x].mem_idx])
142#define HDA_REG(pThis, x) (HDA_REG_IND((pThis), HDA_REG_IND_NAME(x)))
143#define HDA_REG_FLAG_VALUE(pThis, reg, val) (HDA_REG((pThis),reg) & (((HDA_REG_FIELD_FLAG_MASK(reg, val)))))
144
145
146#define HDA_REG_GCAP 0 /* range 0x00-0x01*/
147#define HDA_RMX_GCAP 0
148/* GCAP HDASpec 3.3.2 This macro encodes the following information about HDA in a compact manner:
149 * oss (15:12) - number of output streams supported
150 * iss (11:8) - number of input streams supported
151 * bss (7:3) - number of bidirectional streams supported
152 * bds (2:1) - number of serial data out (SDO) signals supported
153 * b64sup (0) - 64 bit addressing supported.
154 */
155#define HDA_MAKE_GCAP(oss, iss, bss, bds, b64sup) \
156 ( (((oss) & 0xF) << 12) \
157 | (((iss) & 0xF) << 8) \
158 | (((bss) & 0x1F) << 3) \
159 | (((bds) & 0x3) << 1) \
160 | ((b64sup) & 1))
161
162#define HDA_REG_VMIN 1 /* 0x02 */
163#define HDA_RMX_VMIN 1
164
165#define HDA_REG_VMAJ 2 /* 0x03 */
166#define HDA_RMX_VMAJ 2
167
168#define HDA_REG_OUTPAY 3 /* 0x04-0x05 */
169#define HDA_RMX_OUTPAY 3
170
171#define HDA_REG_INPAY 4 /* 0x06-0x07 */
172#define HDA_RMX_INPAY 4
173
174#define HDA_REG_GCTL 5 /* 0x08-0x0B */
175#define HDA_RMX_GCTL 5
176#define HDA_GCTL_RST_SHIFT 0
177#define HDA_GCTL_FSH_SHIFT 1
178#define HDA_GCTL_UR_SHIFT 8
179
180#define HDA_REG_WAKEEN 6 /* 0x0C */
181#define HDA_RMX_WAKEEN 6
182
183#define HDA_REG_STATESTS 7 /* 0x0E */
184#define HDA_RMX_STATESTS 7
185#define HDA_STATES_SCSF 0x7
186
187#define HDA_REG_GSTS 8 /* 0x10-0x11*/
188#define HDA_RMX_GSTS 8
189#define HDA_GSTS_FSH_SHIFT 1
190
191#define HDA_REG_OUTSTRMPAY 9 /* 0x18 */
192#define HDA_RMX_OUTSTRMPAY 112
193
194#define HDA_REG_INSTRMPAY 10 /* 0x1a */
195#define HDA_RMX_INSTRMPAY 113
196
197#define HDA_REG_INTCTL 11 /* 0x20 */
198#define HDA_RMX_INTCTL 9
199#define HDA_INTCTL_GIE_SHIFT 31
200#define HDA_INTCTL_CIE_SHIFT 30
201#define HDA_INTCTL_S0_SHIFT 0
202#define HDA_INTCTL_S1_SHIFT 1
203#define HDA_INTCTL_S2_SHIFT 2
204#define HDA_INTCTL_S3_SHIFT 3
205#define HDA_INTCTL_S4_SHIFT 4
206#define HDA_INTCTL_S5_SHIFT 5
207#define HDA_INTCTL_S6_SHIFT 6
208#define HDA_INTCTL_S7_SHIFT 7
209#define INTCTL_SX(pThis, X) (HDA_REG_FLAG_VALUE((pThis), INTCTL, S##X))
210
211#define HDA_REG_INTSTS 12 /* 0x24 */
212#define HDA_RMX_INTSTS 10
213#define HDA_INTSTS_GIS_SHIFT 31
214#define HDA_INTSTS_CIS_SHIFT 30
215#define HDA_INTSTS_S0_SHIFT 0
216#define HDA_INTSTS_S1_SHIFT 1
217#define HDA_INTSTS_S2_SHIFT 2
218#define HDA_INTSTS_S3_SHIFT 3
219#define HDA_INTSTS_S4_SHIFT 4
220#define HDA_INTSTS_S5_SHIFT 5
221#define HDA_INTSTS_S6_SHIFT 6
222#define HDA_INTSTS_S7_SHIFT 7
223#define HDA_INTSTS_S_MASK(num) RT_BIT(HDA_REG_FIELD_SHIFT(S##num))
224
225#define HDA_REG_WALCLK 13 /* 0x30 */
226#define HDA_RMX_WALCLK /* Not defined! */
227
228/* Note: The HDA specification defines a SSYNC register at offset 0x38. The
229 * ICH6/ICH9 datahseet defines SSYNC at offset 0x34. The Linux HDA driver matches
230 * the datasheet.
231 */
232#define HDA_REG_SSYNC 14 /* 0x38 */
233#define HDA_RMX_SSYNC 12
234
235#define HDA_REG_CORBLBASE 15 /* 0x40 */
236#define HDA_RMX_CORBLBASE 13
237
238#define HDA_REG_CORBUBASE 16 /* 0x44 */
239#define HDA_RMX_CORBUBASE 14
240
241#define HDA_REG_CORBWP 17 /* 0x48 */
242#define HDA_RMX_CORBWP 15
243
244#define HDA_REG_CORBRP 18 /* 0x4A */
245#define HDA_RMX_CORBRP 16
246#define HDA_CORBRP_RST_SHIFT 15
247#define HDA_CORBRP_WP_SHIFT 0
248#define HDA_CORBRP_WP_MASK 0xFF
249
250#define HDA_REG_CORBCTL 19 /* 0x4C */
251#define HDA_RMX_CORBCTL 17
252#define HDA_CORBCTL_DMA_SHIFT 1
253#define HDA_CORBCTL_CMEIE_SHIFT 0
254
255#define HDA_REG_CORBSTS 20 /* 0x4D */
256#define HDA_RMX_CORBSTS 18
257#define HDA_CORBSTS_CMEI_SHIFT 0
258
259#define HDA_REG_CORBSIZE 21 /* 0x4E */
260#define HDA_RMX_CORBSIZE 19
261#define HDA_CORBSIZE_SZ_CAP 0xF0
262#define HDA_CORBSIZE_SZ 0x3
263/* till ich 10 sizes of CORB and RIRB are hardcoded to 256 in real hw */
264
265#define HDA_REG_RIRBLBASE 22 /* 0x50 */
266#define HDA_RMX_RIRBLBASE 20
267
268#define HDA_REG_RIRBUBASE 23 /* 0x54 */
269#define HDA_RMX_RIRBUBASE 21
270
271#define HDA_REG_RIRBWP 24 /* 0x58 */
272#define HDA_RMX_RIRBWP 22
273#define HDA_RIRBWP_RST_SHIFT 15
274#define HDA_RIRBWP_WP_MASK 0xFF
275
276#define HDA_REG_RINTCNT 25 /* 0x5A */
277#define HDA_RMX_RINTCNT 23
278#define RINTCNT_N(pThis) (HDA_REG(pThis, RINTCNT) & 0xff)
279
280#define HDA_REG_RIRBCTL 26 /* 0x5C */
281#define HDA_RMX_RIRBCTL 24
282#define HDA_RIRBCTL_RIC_SHIFT 0
283#define HDA_RIRBCTL_DMA_SHIFT 1
284#define HDA_ROI_DMA_SHIFT 2
285
286#define HDA_REG_RIRBSTS 27 /* 0x5D */
287#define HDA_RMX_RIRBSTS 25
288#define HDA_RIRBSTS_RINTFL_SHIFT 0
289#define HDA_RIRBSTS_RIRBOIS_SHIFT 2
290
291#define HDA_REG_RIRBSIZE 28 /* 0x5E */
292#define HDA_RMX_RIRBSIZE 26
293#define HDA_RIRBSIZE_SZ_CAP 0xF0
294#define HDA_RIRBSIZE_SZ 0x3
295
296#define RIRBSIZE_SZ(pThis) (HDA_REG(pThis, HDA_REG_RIRBSIZE) & HDA_RIRBSIZE_SZ)
297#define RIRBSIZE_SZ_CAP(pThis) (HDA_REG(pThis, HDA_REG_RIRBSIZE) & HDA_RIRBSIZE_SZ_CAP)
298
299
300#define HDA_REG_IC 29 /* 0x60 */
301#define HDA_RMX_IC 27
302
303#define HDA_REG_IR 30 /* 0x64 */
304#define HDA_RMX_IR 28
305
306#define HDA_REG_IRS 31 /* 0x68 */
307#define HDA_RMX_IRS 29
308#define HDA_IRS_ICB_SHIFT 0
309#define HDA_IRS_IRV_SHIFT 1
310
311#define HDA_REG_DPLBASE 32 /* 0x70 */
312#define HDA_RMX_DPLBASE 30
313#define DPLBASE(pThis) (HDA_REG((pThis), DPLBASE))
314
315#define HDA_REG_DPUBASE 33 /* 0x74 */
316#define HDA_RMX_DPUBASE 31
317#define DPUBASE(pThis) (HDA_REG((pThis), DPUBASE))
318
319#define DPBASE_ADDR_MASK (~(uint64_t)0x7f)
320
321#define HDA_STREAM_REG_DEF(name, num) (HDA_REG_SD##num##name)
322#define HDA_STREAM_RMX_DEF(name, num) (HDA_RMX_SD##num##name)
323/* Note: sdnum here _MUST_ be stream reg number [0,7]. */
324#define HDA_STREAM_REG(pThis, name, sdnum) (HDA_REG_IND((pThis), HDA_REG_SD0##name + (sdnum) * 10))
325
326#define HDA_SD_NUM_FROM_REG(pThis, func, reg) ((reg - HDA_STREAM_REG_DEF(func, 0)) / 10)
327
328/** @todo Condense marcos! */
329
330#define HDA_REG_SD0CTL HDA_NUM_GENERAL_REGS /* 0x80 */
331#define HDA_REG_SD1CTL (HDA_STREAM_REG_DEF(CTL, 0) + 10) /* 0xA0 */
332#define HDA_REG_SD2CTL (HDA_STREAM_REG_DEF(CTL, 0) + 20) /* 0xC0 */
333#define HDA_REG_SD3CTL (HDA_STREAM_REG_DEF(CTL, 0) + 30) /* 0xE0 */
334#define HDA_REG_SD4CTL (HDA_STREAM_REG_DEF(CTL, 0) + 40) /* 0x100 */
335#define HDA_REG_SD5CTL (HDA_STREAM_REG_DEF(CTL, 0) + 50) /* 0x120 */
336#define HDA_REG_SD6CTL (HDA_STREAM_REG_DEF(CTL, 0) + 60) /* 0x140 */
337#define HDA_REG_SD7CTL (HDA_STREAM_REG_DEF(CTL, 0) + 70) /* 0x160 */
338#define HDA_RMX_SD0CTL 32
339#define HDA_RMX_SD1CTL (HDA_STREAM_RMX_DEF(CTL, 0) + 10)
340#define HDA_RMX_SD2CTL (HDA_STREAM_RMX_DEF(CTL, 0) + 20)
341#define HDA_RMX_SD3CTL (HDA_STREAM_RMX_DEF(CTL, 0) + 30)
342#define HDA_RMX_SD4CTL (HDA_STREAM_RMX_DEF(CTL, 0) + 40)
343#define HDA_RMX_SD5CTL (HDA_STREAM_RMX_DEF(CTL, 0) + 50)
344#define HDA_RMX_SD6CTL (HDA_STREAM_RMX_DEF(CTL, 0) + 60)
345#define HDA_RMX_SD7CTL (HDA_STREAM_RMX_DEF(CTL, 0) + 70)
346
347#define SD(func, num) SD##num##func
348
349#define HDA_SDCTL(pThis, num) HDA_REG((pThis), SD(CTL, num))
350#define HDA_SDCTL_NUM(pThis, num) ((HDA_SDCTL((pThis), num) & HDA_REG_FIELD_MASK(SDCTL,NUM)) >> HDA_REG_FIELD_SHIFT(SDCTL, NUM))
351#define HDA_SDCTL_NUM_MASK 0xF
352#define HDA_SDCTL_NUM_SHIFT 20
353#define HDA_SDCTL_DIR_SHIFT 19
354#define HDA_SDCTL_TP_SHIFT 18
355#define HDA_SDCTL_STRIPE_MASK 0x3
356#define HDA_SDCTL_STRIPE_SHIFT 16
357#define HDA_SDCTL_DEIE_SHIFT 4
358#define HDA_SDCTL_FEIE_SHIFT 3
359#define HDA_SDCTL_ICE_SHIFT 2
360#define HDA_SDCTL_RUN_SHIFT 1
361#define HDA_SDCTL_SRST_SHIFT 0
362
363#define HDA_REG_SD0STS 35 /* 0x83 */
364#define HDA_REG_SD1STS (HDA_STREAM_REG_DEF(STS, 0) + 10) /* 0xA3 */
365#define HDA_REG_SD2STS (HDA_STREAM_REG_DEF(STS, 0) + 20) /* 0xC3 */
366#define HDA_REG_SD3STS (HDA_STREAM_REG_DEF(STS, 0) + 30) /* 0xE3 */
367#define HDA_REG_SD4STS (HDA_STREAM_REG_DEF(STS, 0) + 40) /* 0x103 */
368#define HDA_REG_SD5STS (HDA_STREAM_REG_DEF(STS, 0) + 50) /* 0x123 */
369#define HDA_REG_SD6STS (HDA_STREAM_REG_DEF(STS, 0) + 60) /* 0x143 */
370#define HDA_REG_SD7STS (HDA_STREAM_REG_DEF(STS, 0) + 70) /* 0x163 */
371#define HDA_RMX_SD0STS 33
372#define HDA_RMX_SD1STS (HDA_STREAM_RMX_DEF(STS, 0) + 10)
373#define HDA_RMX_SD2STS (HDA_STREAM_RMX_DEF(STS, 0) + 20)
374#define HDA_RMX_SD3STS (HDA_STREAM_RMX_DEF(STS, 0) + 30)
375#define HDA_RMX_SD4STS (HDA_STREAM_RMX_DEF(STS, 0) + 40)
376#define HDA_RMX_SD5STS (HDA_STREAM_RMX_DEF(STS, 0) + 50)
377#define HDA_RMX_SD6STS (HDA_STREAM_RMX_DEF(STS, 0) + 60)
378#define HDA_RMX_SD7STS (HDA_STREAM_RMX_DEF(STS, 0) + 70)
379
380#define SDSTS(pThis, num) HDA_REG((pThis), SD(STS, num))
381#define HDA_SDSTS_FIFORDY_SHIFT 5
382#define HDA_SDSTS_DE_SHIFT 4
383#define HDA_SDSTS_FE_SHIFT 3
384#define HDA_SDSTS_BCIS_SHIFT 2
385
386#define HDA_REG_SD0LPIB 36 /* 0x84 */
387#define HDA_REG_SD1LPIB (HDA_STREAM_REG_DEF(LPIB, 0) + 10) /* 0xA4 */
388#define HDA_REG_SD2LPIB (HDA_STREAM_REG_DEF(LPIB, 0) + 20) /* 0xC4 */
389#define HDA_REG_SD3LPIB (HDA_STREAM_REG_DEF(LPIB, 0) + 30) /* 0xE4 */
390#define HDA_REG_SD4LPIB (HDA_STREAM_REG_DEF(LPIB, 0) + 40) /* 0x104 */
391#define HDA_REG_SD5LPIB (HDA_STREAM_REG_DEF(LPIB, 0) + 50) /* 0x124 */
392#define HDA_REG_SD6LPIB (HDA_STREAM_REG_DEF(LPIB, 0) + 60) /* 0x144 */
393#define HDA_REG_SD7LPIB (HDA_STREAM_REG_DEF(LPIB, 0) + 70) /* 0x164 */
394#define HDA_RMX_SD0LPIB 34
395#define HDA_RMX_SD1LPIB (HDA_STREAM_RMX_DEF(LPIB, 0) + 10)
396#define HDA_RMX_SD2LPIB (HDA_STREAM_RMX_DEF(LPIB, 0) + 20)
397#define HDA_RMX_SD3LPIB (HDA_STREAM_RMX_DEF(LPIB, 0) + 30)
398#define HDA_RMX_SD4LPIB (HDA_STREAM_RMX_DEF(LPIB, 0) + 40)
399#define HDA_RMX_SD5LPIB (HDA_STREAM_RMX_DEF(LPIB, 0) + 50)
400#define HDA_RMX_SD6LPIB (HDA_STREAM_RMX_DEF(LPIB, 0) + 60)
401#define HDA_RMX_SD7LPIB (HDA_STREAM_RMX_DEF(LPIB, 0) + 70)
402
403#define HDA_REG_SD0CBL 37 /* 0x88 */
404#define HDA_REG_SD1CBL (HDA_STREAM_REG_DEF(CBL, 0) + 10) /* 0xA8 */
405#define HDA_REG_SD2CBL (HDA_STREAM_REG_DEF(CBL, 0) + 20) /* 0xC8 */
406#define HDA_REG_SD3CBL (HDA_STREAM_REG_DEF(CBL, 0) + 30) /* 0xE8 */
407#define HDA_REG_SD4CBL (HDA_STREAM_REG_DEF(CBL, 0) + 40) /* 0x108 */
408#define HDA_REG_SD5CBL (HDA_STREAM_REG_DEF(CBL, 0) + 50) /* 0x128 */
409#define HDA_REG_SD6CBL (HDA_STREAM_REG_DEF(CBL, 0) + 60) /* 0x148 */
410#define HDA_REG_SD7CBL (HDA_STREAM_REG_DEF(CBL, 0) + 70) /* 0x168 */
411#define HDA_RMX_SD0CBL 35
412#define HDA_RMX_SD1CBL (HDA_STREAM_RMX_DEF(CBL, 0) + 10)
413#define HDA_RMX_SD2CBL (HDA_STREAM_RMX_DEF(CBL, 0) + 20)
414#define HDA_RMX_SD3CBL (HDA_STREAM_RMX_DEF(CBL, 0) + 30)
415#define HDA_RMX_SD4CBL (HDA_STREAM_RMX_DEF(CBL, 0) + 40)
416#define HDA_RMX_SD5CBL (HDA_STREAM_RMX_DEF(CBL, 0) + 50)
417#define HDA_RMX_SD6CBL (HDA_STREAM_RMX_DEF(CBL, 0) + 60)
418#define HDA_RMX_SD7CBL (HDA_STREAM_RMX_DEF(CBL, 0) + 70)
419
420#define HDA_REG_SD0LVI 38 /* 0x8C */
421#define HDA_REG_SD1LVI (HDA_STREAM_REG_DEF(LVI, 0) + 10) /* 0xAC */
422#define HDA_REG_SD2LVI (HDA_STREAM_REG_DEF(LVI, 0) + 20) /* 0xCC */
423#define HDA_REG_SD3LVI (HDA_STREAM_REG_DEF(LVI, 0) + 30) /* 0xEC */
424#define HDA_REG_SD4LVI (HDA_STREAM_REG_DEF(LVI, 0) + 40) /* 0x10C */
425#define HDA_REG_SD5LVI (HDA_STREAM_REG_DEF(LVI, 0) + 50) /* 0x12C */
426#define HDA_REG_SD6LVI (HDA_STREAM_REG_DEF(LVI, 0) + 60) /* 0x14C */
427#define HDA_REG_SD7LVI (HDA_STREAM_REG_DEF(LVI, 0) + 70) /* 0x16C */
428#define HDA_RMX_SD0LVI 36
429#define HDA_RMX_SD1LVI (HDA_STREAM_RMX_DEF(LVI, 0) + 10)
430#define HDA_RMX_SD2LVI (HDA_STREAM_RMX_DEF(LVI, 0) + 20)
431#define HDA_RMX_SD3LVI (HDA_STREAM_RMX_DEF(LVI, 0) + 30)
432#define HDA_RMX_SD4LVI (HDA_STREAM_RMX_DEF(LVI, 0) + 40)
433#define HDA_RMX_SD5LVI (HDA_STREAM_RMX_DEF(LVI, 0) + 50)
434#define HDA_RMX_SD6LVI (HDA_STREAM_RMX_DEF(LVI, 0) + 60)
435#define HDA_RMX_SD7LVI (HDA_STREAM_RMX_DEF(LVI, 0) + 70)
436
437#define HDA_REG_SD0FIFOW 39 /* 0x8E */
438#define HDA_REG_SD1FIFOW (HDA_STREAM_REG_DEF(FIFOW, 0) + 10) /* 0xAE */
439#define HDA_REG_SD2FIFOW (HDA_STREAM_REG_DEF(FIFOW, 0) + 20) /* 0xCE */
440#define HDA_REG_SD3FIFOW (HDA_STREAM_REG_DEF(FIFOW, 0) + 30) /* 0xEE */
441#define HDA_REG_SD4FIFOW (HDA_STREAM_REG_DEF(FIFOW, 0) + 40) /* 0x10E */
442#define HDA_REG_SD5FIFOW (HDA_STREAM_REG_DEF(FIFOW, 0) + 50) /* 0x12E */
443#define HDA_REG_SD6FIFOW (HDA_STREAM_REG_DEF(FIFOW, 0) + 60) /* 0x14E */
444#define HDA_REG_SD7FIFOW (HDA_STREAM_REG_DEF(FIFOW, 0) + 70) /* 0x16E */
445#define HDA_RMX_SD0FIFOW 37
446#define HDA_RMX_SD1FIFOW (HDA_STREAM_RMX_DEF(FIFOW, 0) + 10)
447#define HDA_RMX_SD2FIFOW (HDA_STREAM_RMX_DEF(FIFOW, 0) + 20)
448#define HDA_RMX_SD3FIFOW (HDA_STREAM_RMX_DEF(FIFOW, 0) + 30)
449#define HDA_RMX_SD4FIFOW (HDA_STREAM_RMX_DEF(FIFOW, 0) + 40)
450#define HDA_RMX_SD5FIFOW (HDA_STREAM_RMX_DEF(FIFOW, 0) + 50)
451#define HDA_RMX_SD6FIFOW (HDA_STREAM_RMX_DEF(FIFOW, 0) + 60)
452#define HDA_RMX_SD7FIFOW (HDA_STREAM_RMX_DEF(FIFOW, 0) + 70)
453
454/*
455 * ICH6 datasheet defined limits for FIFOW values (18.2.38).
456 */
457#define HDA_SDFIFOW_8B 0x2
458#define HDA_SDFIFOW_16B 0x3
459#define HDA_SDFIFOW_32B 0x4
460
461#define HDA_REG_SD0FIFOS 40 /* 0x90 */
462#define HDA_REG_SD1FIFOS (HDA_STREAM_REG_DEF(FIFOS, 0) + 10) /* 0xB0 */
463#define HDA_REG_SD2FIFOS (HDA_STREAM_REG_DEF(FIFOS, 0) + 20) /* 0xD0 */
464#define HDA_REG_SD3FIFOS (HDA_STREAM_REG_DEF(FIFOS, 0) + 30) /* 0xF0 */
465#define HDA_REG_SD4FIFOS (HDA_STREAM_REG_DEF(FIFOS, 0) + 40) /* 0x110 */
466#define HDA_REG_SD5FIFOS (HDA_STREAM_REG_DEF(FIFOS, 0) + 50) /* 0x130 */
467#define HDA_REG_SD6FIFOS (HDA_STREAM_REG_DEF(FIFOS, 0) + 60) /* 0x150 */
468#define HDA_REG_SD7FIFOS (HDA_STREAM_REG_DEF(FIFOS, 0) + 70) /* 0x170 */
469#define HDA_RMX_SD0FIFOS 38
470#define HDA_RMX_SD1FIFOS (HDA_STREAM_RMX_DEF(FIFOS, 0) + 10)
471#define HDA_RMX_SD2FIFOS (HDA_STREAM_RMX_DEF(FIFOS, 0) + 20)
472#define HDA_RMX_SD3FIFOS (HDA_STREAM_RMX_DEF(FIFOS, 0) + 30)
473#define HDA_RMX_SD4FIFOS (HDA_STREAM_RMX_DEF(FIFOS, 0) + 40)
474#define HDA_RMX_SD5FIFOS (HDA_STREAM_RMX_DEF(FIFOS, 0) + 50)
475#define HDA_RMX_SD6FIFOS (HDA_STREAM_RMX_DEF(FIFOS, 0) + 60)
476#define HDA_RMX_SD7FIFOS (HDA_STREAM_RMX_DEF(FIFOS, 0) + 70)
477
478/*
479 * ICH6 datasheet defines limits for FIFOS registers (18.2.39)
480 * formula: size - 1
481 * Other values not listed are not supported.
482 */
483#define HDA_SDIFIFO_120B 0x77 /* 8-, 16-, 20-, 24-, 32-bit Input Streams */
484#define HDA_SDIFIFO_160B 0x9F /* 20-, 24-bit Input Streams Streams */
485
486#define HDA_SDOFIFO_16B 0x0F /* 8-, 16-, 20-, 24-, 32-bit Output Streams */
487#define HDA_SDOFIFO_32B 0x1F /* 8-, 16-, 20-, 24-, 32-bit Output Streams */
488#define HDA_SDOFIFO_64B 0x3F /* 8-, 16-, 20-, 24-, 32-bit Output Streams */
489#define HDA_SDOFIFO_128B 0x7F /* 8-, 16-, 20-, 24-, 32-bit Output Streams */
490#define HDA_SDOFIFO_192B 0xBF /* 8-, 16-, 20-, 24-, 32-bit Output Streams */
491#define HDA_SDOFIFO_256B 0xFF /* 20-, 24-bit Output Streams */
492#define SDFIFOS(pThis, num) HDA_REG((pThis), SD(FIFOS, num))
493
494#define HDA_REG_SD0FMT 41 /* 0x92 */
495#define HDA_REG_SD1FMT (HDA_STREAM_REG_DEF(FMT, 0) + 10) /* 0xB2 */
496#define HDA_REG_SD2FMT (HDA_STREAM_REG_DEF(FMT, 0) + 20) /* 0xD2 */
497#define HDA_REG_SD3FMT (HDA_STREAM_REG_DEF(FMT, 0) + 30) /* 0xF2 */
498#define HDA_REG_SD4FMT (HDA_STREAM_REG_DEF(FMT, 0) + 40) /* 0x112 */
499#define HDA_REG_SD5FMT (HDA_STREAM_REG_DEF(FMT, 0) + 50) /* 0x132 */
500#define HDA_REG_SD6FMT (HDA_STREAM_REG_DEF(FMT, 0) + 60) /* 0x152 */
501#define HDA_REG_SD7FMT (HDA_STREAM_REG_DEF(FMT, 0) + 70) /* 0x172 */
502#define HDA_RMX_SD0FMT 39
503#define HDA_RMX_SD1FMT (HDA_STREAM_RMX_DEF(FMT, 0) + 10)
504#define HDA_RMX_SD2FMT (HDA_STREAM_RMX_DEF(FMT, 0) + 20)
505#define HDA_RMX_SD3FMT (HDA_STREAM_RMX_DEF(FMT, 0) + 30)
506#define HDA_RMX_SD4FMT (HDA_STREAM_RMX_DEF(FMT, 0) + 40)
507#define HDA_RMX_SD5FMT (HDA_STREAM_RMX_DEF(FMT, 0) + 50)
508#define HDA_RMX_SD6FMT (HDA_STREAM_RMX_DEF(FMT, 0) + 60)
509#define HDA_RMX_SD7FMT (HDA_STREAM_RMX_DEF(FMT, 0) + 70)
510
511#define SDFMT(pThis, num) (HDA_REG((pThis), SD(FMT, num)))
512#define HDA_SDFMT_BASE_RATE(pThis, num) ((SDFMT(pThis, num) & HDA_REG_FIELD_FLAG_MASK(SDFMT, BASE_RATE)) >> HDA_REG_FIELD_SHIFT(SDFMT, BASE_RATE))
513#define HDA_SDFMT_MULT(pThis, num) ((SDFMT((pThis), num) & HDA_REG_FIELD_MASK(SDFMT,MULT)) >> HDA_REG_FIELD_SHIFT(SDFMT, MULT))
514#define HDA_SDFMT_DIV(pThis, num) ((SDFMT((pThis), num) & HDA_REG_FIELD_MASK(SDFMT,DIV)) >> HDA_REG_FIELD_SHIFT(SDFMT, DIV))
515
516#define HDA_REG_SD0BDPL 42 /* 0x98 */
517#define HDA_REG_SD1BDPL (HDA_STREAM_REG_DEF(BDPL, 0) + 10) /* 0xB8 */
518#define HDA_REG_SD2BDPL (HDA_STREAM_REG_DEF(BDPL, 0) + 20) /* 0xD8 */
519#define HDA_REG_SD3BDPL (HDA_STREAM_REG_DEF(BDPL, 0) + 30) /* 0xF8 */
520#define HDA_REG_SD4BDPL (HDA_STREAM_REG_DEF(BDPL, 0) + 40) /* 0x118 */
521#define HDA_REG_SD5BDPL (HDA_STREAM_REG_DEF(BDPL, 0) + 50) /* 0x138 */
522#define HDA_REG_SD6BDPL (HDA_STREAM_REG_DEF(BDPL, 0) + 60) /* 0x158 */
523#define HDA_REG_SD7BDPL (HDA_STREAM_REG_DEF(BDPL, 0) + 70) /* 0x178 */
524#define HDA_RMX_SD0BDPL 40
525#define HDA_RMX_SD1BDPL (HDA_STREAM_RMX_DEF(BDPL, 0) + 10)
526#define HDA_RMX_SD2BDPL (HDA_STREAM_RMX_DEF(BDPL, 0) + 20)
527#define HDA_RMX_SD3BDPL (HDA_STREAM_RMX_DEF(BDPL, 0) + 30)
528#define HDA_RMX_SD4BDPL (HDA_STREAM_RMX_DEF(BDPL, 0) + 40)
529#define HDA_RMX_SD5BDPL (HDA_STREAM_RMX_DEF(BDPL, 0) + 50)
530#define HDA_RMX_SD6BDPL (HDA_STREAM_RMX_DEF(BDPL, 0) + 60)
531#define HDA_RMX_SD7BDPL (HDA_STREAM_RMX_DEF(BDPL, 0) + 70)
532
533#define HDA_REG_SD0BDPU 43 /* 0x9C */
534#define HDA_REG_SD1BDPU (HDA_STREAM_REG_DEF(BDPU, 0) + 10) /* 0xBC */
535#define HDA_REG_SD2BDPU (HDA_STREAM_REG_DEF(BDPU, 0) + 20) /* 0xDC */
536#define HDA_REG_SD3BDPU (HDA_STREAM_REG_DEF(BDPU, 0) + 30) /* 0xFC */
537#define HDA_REG_SD4BDPU (HDA_STREAM_REG_DEF(BDPU, 0) + 40) /* 0x11C */
538#define HDA_REG_SD5BDPU (HDA_STREAM_REG_DEF(BDPU, 0) + 50) /* 0x13C */
539#define HDA_REG_SD6BDPU (HDA_STREAM_REG_DEF(BDPU, 0) + 60) /* 0x15C */
540#define HDA_REG_SD7BDPU (HDA_STREAM_REG_DEF(BDPU, 0) + 70) /* 0x17C */
541#define HDA_RMX_SD0BDPU 41
542#define HDA_RMX_SD1BDPU (HDA_STREAM_RMX_DEF(BDPU, 0) + 10)
543#define HDA_RMX_SD2BDPU (HDA_STREAM_RMX_DEF(BDPU, 0) + 20)
544#define HDA_RMX_SD3BDPU (HDA_STREAM_RMX_DEF(BDPU, 0) + 30)
545#define HDA_RMX_SD4BDPU (HDA_STREAM_RMX_DEF(BDPU, 0) + 40)
546#define HDA_RMX_SD5BDPU (HDA_STREAM_RMX_DEF(BDPU, 0) + 50)
547#define HDA_RMX_SD6BDPU (HDA_STREAM_RMX_DEF(BDPU, 0) + 60)
548#define HDA_RMX_SD7BDPU (HDA_STREAM_RMX_DEF(BDPU, 0) + 70)
549
550#define HDA_CODEC_CAD_SHIFT 28
551/* Encodes the (required) LUN into a codec command. */
552#define HDA_CODEC_CMD(cmd, lun) ((cmd) | (lun << HDA_CODEC_CAD_SHIFT))
553
554
555
556/*********************************************************************************************************************************
557* Structures and Typedefs *
558*********************************************************************************************************************************/
559
560/**
561 * Internal state of a Buffer Descriptor List Entry (BDLE),
562 * needed to keep track of the data needed for the actual device
563 * emulation.
564 */
565typedef struct HDABDLESTATE
566{
567 /** Own index within the BDL (Buffer Descriptor List). */
568 uint32_t u32BDLIndex;
569 /** Number of bytes below the stream's FIFO watermark (SDFIFOW).
570 * Used to check if we need fill up the FIFO again. */
571 uint32_t cbBelowFIFOW;
572 /** The buffer descriptor's internal DMA buffer. */
573 uint8_t au8FIFO[HDA_SDOFIFO_256B + 1];
574 /** Current offset in DMA buffer (in bytes).*/
575 uint32_t u32BufOff;
576 uint32_t Padding;
577} HDABDLESTATE, *PHDABDLESTATE;
578
579/**
580 * Buffer Descriptor List Entry (BDLE) (3.6.3).
581 *
582 * Contains only register values which do *not* change until a
583 * stream reset occurs.
584 */
585typedef struct HDABDLE
586{
587 /** Starting address of the actual buffer. Must be 128-bit aligned. */
588 uint64_t u64BufAdr;
589 /** Size of the actual buffer (in bytes). */
590 uint32_t u32BufSize;
591 /** Interrupt on completion; the controller will generate
592 * an interrupt when the last byte of the buffer has been
593 * fetched by the DMA engine. */
594 bool fIntOnCompletion;
595 /** Internal state of this BDLE.
596 * Not part of the actual BDLE registers. */
597 HDABDLESTATE State;
598} HDABDLE, *PHDABDLE;
599
600/**
601 * Structure for keeping an audio stream data mapping.
602 */
603typedef struct HDASTREAMMAPPING
604{
605 /** The stream's layout. */
606 PDMAUDIOSTREAMLAYOUT enmLayout;
607 /** Number of audio channels in this stream. */
608 uint8_t cChannels;
609 /** Array audio channels. */
610 R3PTRTYPE(PPDMAUDIOSTREAMCHANNEL) paChannels;
611 R3PTRTYPE(PRTCIRCBUF) pCircBuf;
612} HDASTREAMMAPPING, *PHDASTREAMMAPPING;
613
614/**
615 * Internal state of a HDA stream.
616 */
617typedef struct HDASTREAMSTATE
618{
619 /** Current BDLE to use. Wraps around to 0 if
620 * maximum (cBDLE) is reached. */
621 uint16_t uCurBDLE;
622 /** Stop indicator. */
623 volatile bool fDoStop;
624 /** Flag indicating whether this stream is in an
625 * active (operative) state or not. */
626 volatile bool fActive;
627 /** Flag indicating whether this stream currently is
628 * in reset mode and therefore not acccessible by the guest. */
629 volatile bool fInReset;
630 /** Unused, padding. */
631 bool fPadding;
632 /** Mutex semaphore handle to serialize access. */
633 RTSEMMUTEX hMtx;
634 /** Event signalling that the stream's state has been changed. */
635 RTSEMEVENT hStateChangedEvent;
636 /** This stream's data mapping. */
637 HDASTREAMMAPPING Mapping;
638 /** Current BDLE (Buffer Descriptor List Entry). */
639 HDABDLE BDLE;
640} HDASTREAMSTATE, *PHDASTREAMSTATE;
641
642/**
643 * Structure defining an HDA mixer sink.
644 * Its purpose is to know which audio mixer sink is bound to
645 * which SDn (SDI/SDO) device stream.
646 *
647 * This is needed in order to handle interleaved streams
648 * (that is, multiple channels in one stream) or non-interleaved
649 * streams (each channel has a dedicated stream).
650 *
651 * This is only known to the actual device emulation level.
652 */
653typedef struct HDAMIXERSINK
654{
655 /** SDn ID this sink is assigned to. 0 if not assigned. */
656 uint8_t uSD;
657 /** Channel ID of SDn ID. Only valid if SDn ID is valid. */
658 uint8_t uChannel;
659 uint8_t Padding[3];
660 /** Pointer to the actual audio mixer sink. */
661 R3PTRTYPE(PAUDMIXSINK) pMixSink;
662} HDAMIXERSINK, *PHDAMIXERSINK;
663
664/**
665 * Structure for keeping a HDA stream state.
666 *
667 * Contains only register values which do *not* change until a
668 * stream reset occurs.
669 */
670typedef struct HDASTREAM
671{
672 /** Stream descriptor number (SDn). */
673 uint8_t u8SD;
674 uint8_t Padding0[7];
675 /** DMA base address (SDnBDPU - SDnBDPL). */
676 uint64_t u64BDLBase;
677 /** Cyclic Buffer Length (SDnCBL).
678 * Represents the size of the ring buffer. */
679 uint32_t u32CBL;
680 /** Format (SDnFMT). */
681 uint16_t u16FMT;
682 /** FIFO Size (FIFOS).
683 * Maximum number of bytes that may have been DMA'd into
684 * memory but not yet transmitted on the link.
685 *
686 * Must be a power of two. */
687 uint16_t u16FIFOS;
688 /** Last Valid Index (SDnLVI). */
689 uint16_t u16LVI;
690 uint16_t Padding1[3];
691 /** Pointer to HDA sink this stream is attached to. */
692 R3PTRTYPE(PHDAMIXERSINK) pMixSink;
693 /** Internal state of this stream. */
694 HDASTREAMSTATE State;
695} HDASTREAM, *PHDASTREAM;
696
697/**
698 * Structure for mapping a stream tag to an HDA stream.
699 */
700typedef struct HDATAG
701{
702 /** Own stream tag. */
703 uint8_t uTag;
704 uint8_t Padding[7];
705 /** Pointer to associated stream. */
706 R3PTRTYPE(PHDASTREAM) pStrm;
707} HDATAG, *PHDATAG;
708
709/**
710 * Structure defining an HDA mixer stream.
711 * This is being used together with an audio mixer instance.
712 */
713typedef struct HDAMIXERSTREAM
714{
715 union
716 {
717 /** Desired playback destination (for an output stream). */
718 PDMAUDIOPLAYBACKDEST Dest;
719 /** Desired recording source (for an input stream). */
720 PDMAUDIORECSOURCE Source;
721 } DestSource;
722 uint8_t Padding1[4];
723 /** Associated mixer handle. */
724 R3PTRTYPE(PAUDMIXSTREAM) pMixStrm;
725} HDAMIXERSTREAM, *PHDAMIXERSTREAM;
726
727/**
728 * Struct for maintaining a host backend driver.
729 * This driver must be associated to one, and only one,
730 * HDA codec. The HDA controller does the actual multiplexing
731 * of HDA codec data to various host backend drivers then.
732 *
733 * This HDA device uses a timer in order to synchronize all
734 * read/write accesses across all attached LUNs / backends.
735 */
736typedef struct HDADRIVER
737{
738 /** Node for storing this driver in our device driver list of HDASTATE. */
739 RTLISTNODER3 Node;
740 /** Pointer to HDA controller (state). */
741 R3PTRTYPE(PHDASTATE) pHDAState;
742 /** Driver flags. */
743 PDMAUDIODRVFLAGS Flags;
744 uint8_t u32Padding0[2];
745 /** LUN to which this driver has been assigned. */
746 uint8_t uLUN;
747 /** Whether this driver is in an attached state or not. */
748 bool fAttached;
749 /** Pointer to attached driver base interface. */
750 R3PTRTYPE(PPDMIBASE) pDrvBase;
751 /** Audio connector interface to the underlying host backend. */
752 R3PTRTYPE(PPDMIAUDIOCONNECTOR) pConnector;
753 /** Mixer stream for line input. */
754 HDAMIXERSTREAM LineIn;
755#ifdef VBOX_WITH_HDA_MIC_IN
756 /** Mixer stream for mic input. */
757 HDAMIXERSTREAM MicIn;
758#endif
759 /** Mixer stream for front output. */
760 HDAMIXERSTREAM Front;
761#ifdef VBOX_WITH_HDA_51_SURROUND
762 /** Mixer stream for center/LFE output. */
763 HDAMIXERSTREAM CenterLFE;
764 /** Mixer stream for rear output. */
765 HDAMIXERSTREAM Rear;
766#endif
767} HDADRIVER;
768
769/**
770 * ICH Intel HD Audio Controller state.
771 */
772typedef struct HDASTATE
773{
774 /** The PCI device structure. */
775 PCIDevice PciDev;
776 /** R3 Pointer to the device instance. */
777 PPDMDEVINSR3 pDevInsR3;
778 /** R0 Pointer to the device instance. */
779 PPDMDEVINSR0 pDevInsR0;
780 /** R0 Pointer to the device instance. */
781 PPDMDEVINSRC pDevInsRC;
782 /** Padding for alignment. */
783 uint32_t u32Padding;
784 /** The base interface for LUN\#0. */
785 PDMIBASE IBase;
786 RTGCPHYS MMIOBaseAddr;
787 /** The HDA's register set. */
788 uint32_t au32Regs[HDA_NUM_REGS];
789 /** Internal stream states. */
790 HDASTREAM aStreams[HDA_MAX_STREAMS];
791 /** Mapping table between stream tags and stream states. */
792 HDATAG aTags[HDA_MAX_TAGS];
793 /** CORB buffer base address. */
794 uint64_t u64CORBBase;
795 /** RIRB buffer base address. */
796 uint64_t u64RIRBBase;
797 /** DMA base address.
798 * Made out of DPLBASE + DPUBASE (3.3.32 + 3.3.33). */
799 uint64_t u64DPBase;
800 /** DMA position buffer enable bit. */
801 bool fDMAPosition;
802 /** Padding for alignment. */
803 uint8_t u8Padding0[7];
804 /** Pointer to CORB buffer. */
805 R3PTRTYPE(uint32_t *) pu32CorbBuf;
806 /** Size in bytes of CORB buffer. */
807 uint32_t cbCorbBuf;
808 /** Padding for alignment. */
809 uint32_t u32Padding1;
810 /** Pointer to RIRB buffer. */
811 R3PTRTYPE(uint64_t *) pu64RirbBuf;
812 /** Size in bytes of RIRB buffer. */
813 uint32_t cbRirbBuf;
814 /** Indicates if HDA controller is in reset mode. */
815 bool fInReset;
816 /** Flag whether the R0 part is enabled. */
817 bool fR0Enabled;
818 /** Flag whether the RC part is enabled. */
819 bool fRCEnabled;
820 /** Number of active (running) SDn streams. */
821 uint8_t cStreamsActive;
822#ifndef VBOX_WITH_AUDIO_CALLBACKS
823 /** The timer for pumping data thru the attached LUN drivers. */
824 PTMTIMERR3 pTimer;
825 /** Flag indicating whether the timer is active or not. */
826 bool fTimerActive;
827 uint8_t u8Padding1[7];
828 /** Timer ticks per Hz. */
829 uint64_t cTimerTicks;
830 /** Timestamp of the last timer callback (hdaTimer).
831 * Used to calculate the time actually elapsed between two timer callbacks. */
832 uint64_t uTimerTS;
833#endif
834#ifdef VBOX_WITH_STATISTICS
835# ifndef VBOX_WITH_AUDIO_CALLBACKS
836 STAMPROFILE StatTimer;
837# endif
838 STAMCOUNTER StatBytesRead;
839 STAMCOUNTER StatBytesWritten;
840#endif
841 /** Pointer to HDA codec to use. */
842 R3PTRTYPE(PHDACODEC) pCodec;
843 /** List of associated LUN drivers (HDADRIVER). */
844 RTLISTANCHORR3 lstDrv;
845 /** The device' software mixer. */
846 R3PTRTYPE(PAUDIOMIXER) pMixer;
847 /** HDA sink for (front) output. */
848 HDAMIXERSINK SinkFront;
849#ifdef VBOX_WITH_HDA_51_SURROUND
850 /** HDA sink for center / LFE output. */
851 HDAMIXERSINK SinkCenterLFE;
852 /** HDA sink for rear output. */
853 HDAMIXERSINK SinkRear;
854#endif
855 /** HDA mixer sink for line input. */
856 HDAMIXERSINK SinkLineIn;
857#ifdef VBOX_WITH_HDA_MIC_IN
858 /** Audio mixer sink for microphone input. */
859 HDAMIXERSINK SinkMicIn;
860#endif
861 uint64_t u64BaseTS;
862 /** Response Interrupt Count (RINTCNT). */
863 uint8_t u8RespIntCnt;
864 /** Padding for alignment. */
865 uint8_t au8Padding2[7];
866} HDASTATE;
867/** Pointer to the ICH Intel HD Audio Controller state. */
868typedef HDASTATE *PHDASTATE;
869
870#ifdef VBOX_WITH_AUDIO_CALLBACKS
871typedef struct HDACALLBACKCTX
872{
873 PHDASTATE pThis;
874 PHDADRIVER pDriver;
875} HDACALLBACKCTX, *PHDACALLBACKCTX;
876#endif
877
878/*********************************************************************************************************************************
879* Internal Functions *
880*********************************************************************************************************************************/
881#ifndef VBOX_DEVICE_STRUCT_TESTCASE
882static FNPDMDEVRESET hdaReset;
883
884/*
885 * Stubs.
886 */
887static int hdaRegReadUnimpl(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
888static int hdaRegWriteUnimpl(PHDASTATE pThis, uint32_t iReg, uint32_t pu32Value);
889
890/*
891 * Global register set read/write functions.
892 */
893static int hdaRegWriteGCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
894static int hdaRegReadINTSTS(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
895static int hdaRegReadLPIB(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
896static int hdaRegReadWALCLK(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
897static int hdaRegReadSSYNC(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
898static int hdaRegWriteSSYNC(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
899static int hdaRegWriteINTSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
900static int hdaRegWriteCORBWP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
901static int hdaRegWriteCORBRP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
902static int hdaRegWriteCORBCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
903static int hdaRegWriteCORBSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
904static int hdaRegWriteRIRBWP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
905static int hdaRegWriteRIRBSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
906static int hdaRegWriteSTATESTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
907static int hdaRegWriteIRS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
908static int hdaRegReadIRS(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
909static int hdaRegWriteBase(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
910
911/*
912 * {IOB}SDn read/write functions.
913 */
914static int hdaRegWriteSDCBL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
915static int hdaRegWriteSDCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
916static int hdaRegWriteSDSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
917static int hdaRegWriteSDLVI(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
918static int hdaRegWriteSDFIFOW(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
919static int hdaRegWriteSDFIFOS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
920static int hdaRegWriteSDFMT(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
921static int hdaRegWriteSDBDPL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
922static int hdaRegWriteSDBDPU(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
923DECLINLINE(int) hdaRegWriteSDLock(PHDASTATE pThis, PHDASTREAM pStream, uint32_t iReg, uint32_t u32Value);
924DECLINLINE(void) hdaRegWriteSDUnlock(PHDASTREAM pStream);
925
926/*
927 * Generic register read/write functions.
928 */
929static int hdaRegReadU32(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
930static int hdaRegWriteU32(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
931static int hdaRegReadU24(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
932static int hdaRegWriteU24(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
933static int hdaRegReadU16(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
934static int hdaRegWriteU16(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
935static int hdaRegReadU8(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
936static int hdaRegWriteU8(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
937
938#ifdef IN_RING3
939static void hdaStreamDestroy(PHDASTREAM pStream);
940static int hdaStreamSetActive(PHDASTATE pThis, PHDASTREAM pStream, bool fActive);
941static int hdaStreamStart(PHDASTREAM pStream);
942static int hdaStreamStop(PHDASTREAM pStream);
943static int hdaStreamWaitForStateChange(PHDASTREAM pStream, RTMSINTERVAL msTimeout);
944static int hdaTransfer(PHDASTATE pThis, PHDASTREAM pStream, uint32_t cbToProcess, uint32_t *pcbProcessed);
945#endif
946
947#ifdef IN_RING3
948static int hdaStreamMapInit(PHDASTREAMMAPPING pMapping, PPDMAUDIOSTREAMCFG pCfg);
949static void hdaStreamMapDestroy(PHDASTREAMMAPPING pMapping);
950static void hdaStreamMapReset(PHDASTREAMMAPPING pMapping);
951#endif
952
953#ifdef IN_RING3
954static int hdaBDLEFetch(PHDASTATE pThis, PHDABDLE pBDLE, uint64_t u64BaseDMA, uint16_t u16Entry);
955DECLINLINE(uint32_t) hdaStreamUpdateLPIB(PHDASTATE pThis, PHDASTREAM pStream, uint32_t u32LPIB);
956# ifdef LOG_ENABLED
957static void hdaBDLEDumpAll(PHDASTATE pThis, uint64_t u64BaseDMA, uint16_t cBDLE);
958# endif
959#endif
960static int hdaProcessInterrupt(PHDASTATE pThis);
961
962/*
963 * Timer routines.
964 */
965#ifndef VBOX_WITH_AUDIO_CALLBACKS
966static void hdaTimerMaybeStart(PHDASTATE pThis);
967static void hdaTimerMaybeStop(PHDASTATE pThis);
968#endif
969
970/*********************************************************************************************************************************
971* Global Variables *
972*********************************************************************************************************************************/
973
974/** Offset of the SD0 register map. */
975#define HDA_REG_DESC_SD0_BASE 0x80
976
977/** Turn a short global register name into an memory index and a stringized name. */
978#define HDA_REG_IDX(abbrev) HDA_MEM_IND_NAME(abbrev), #abbrev
979
980/** Turns a short stream register name into an memory index and a stringized name. */
981#define HDA_REG_IDX_STRM(reg, suff) HDA_MEM_IND_NAME(reg ## suff), #reg #suff
982
983/** Same as above for a register *not* stored in memory. */
984#define HDA_REG_IDX_LOCAL(abbrev) 0, #abbrev
985
986/** Emits a single audio stream register set (e.g. OSD0) at a specified offset. */
987#define HDA_REG_MAP_STRM(offset, name) \
988 /* offset size read mask write mask read callback write callback index + abbrev description */ \
989 /* ------- ------- ---------- ---------- -------------- ----------------- ------------------------------ ----------- */ \
990 /* Offset 0x80 (SD0) */ \
991 { offset, 0x00003, 0x00FF001F, 0x00F0001F, hdaRegReadU24 , hdaRegWriteSDCTL , HDA_REG_IDX_STRM(name, CTL) , #name " Stream Descriptor Control" }, \
992 /* Offset 0x83 (SD0) */ \
993 { offset + 0x3, 0x00001, 0x0000001C, 0x0000003C, hdaRegReadU8 , hdaRegWriteSDSTS , HDA_REG_IDX_STRM(name, STS) , #name " Status" }, \
994 /* Offset 0x84 (SD0) */ \
995 { offset + 0x4, 0x00004, 0xFFFFFFFF, 0x00000000, hdaRegReadLPIB, hdaRegWriteU32 , HDA_REG_IDX_STRM(name, LPIB) , #name " Link Position In Buffer" }, \
996 /* Offset 0x88 (SD0) */ \
997 { offset + 0x8, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32, hdaRegWriteSDCBL , HDA_REG_IDX_STRM(name, CBL) , #name " Cyclic Buffer Length" }, \
998 /* Offset 0x8C (SD0) */ \
999 { offset + 0xC, 0x00002, 0x0000FFFF, 0x0000FFFF, hdaRegReadU16, hdaRegWriteSDLVI , HDA_REG_IDX_STRM(name, LVI) , #name " Last Valid Index" }, \
1000 /* Reserved: FIFO Watermark. ** @todo Document this! */ \
1001 { offset + 0xE, 0x00002, 0x00000007, 0x00000007, hdaRegReadU16, hdaRegWriteU16, HDA_REG_IDX_STRM(name, FIFOW), #name " FIFO Watermark" }, \
1002 /* Offset 0x90 (SD0) */ \
1003 { offset + 0x10, 0x00002, 0x000000FF, 0x00000000, hdaRegReadU16, hdaRegWriteU16, HDA_REG_IDX_STRM(name, FIFOS), #name " FIFO Size" }, \
1004 /* Offset 0x92 (SD0) */ \
1005 { offset + 0x12, 0x00002, 0x00007F7F, 0x00007F7F, hdaRegReadU16, hdaRegWriteSDFMT , HDA_REG_IDX_STRM(name, FMT) , #name " Stream Format" }, \
1006 /* Reserved: 0x94 - 0x98. */ \
1007 /* Offset 0x98 (SD0) */ \
1008 { offset + 0x18, 0x00004, 0xFFFFFF80, 0xFFFFFF80, hdaRegReadU32, hdaRegWriteSDBDPL , HDA_REG_IDX_STRM(name, BDPL) , #name " Buffer Descriptor List Pointer-Lower Base Address" }, \
1009 /* Offset 0x9C (SD0) */ \
1010 { offset + 0x1C, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32, hdaRegWriteSDBDPU , HDA_REG_IDX_STRM(name, BDPU) , #name " Buffer Descriptor List Pointer-Upper Base Address" }
1011
1012/** Defines a single audio stream register set (e.g. OSD0). */
1013#define HDA_REG_MAP_DEF_STREAM(index, name) \
1014 HDA_REG_MAP_STRM(HDA_REG_DESC_SD0_BASE + (index * 32 /* 0x20 */), name)
1015
1016/* See 302349 p 6.2. */
1017static const struct HDAREGDESC
1018{
1019 /** Register offset in the register space. */
1020 uint32_t offset;
1021 /** Size in bytes. Registers of size > 4 are in fact tables. */
1022 uint32_t size;
1023 /** Readable bits. */
1024 uint32_t readable;
1025 /** Writable bits. */
1026 uint32_t writable;
1027 /** Read callback. */
1028 int (*pfnRead)(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
1029 /** Write callback. */
1030 int (*pfnWrite)(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
1031 /** Index into the register storage array. */
1032 uint32_t mem_idx;
1033 /** Abbreviated name. */
1034 const char *abbrev;
1035 /** Descripton. */
1036 const char *desc;
1037} g_aHdaRegMap[HDA_NUM_REGS] =
1038
1039{
1040 /* offset size read mask write mask read callback write callback index + abbrev */
1041 /*------- ------- ---------- ---------- ----------------------- ---------------------- ---------------- */
1042 { 0x00000, 0x00002, 0x0000FFFB, 0x00000000, hdaRegReadU16 , hdaRegWriteUnimpl , HDA_REG_IDX(GCAP) }, /* Global Capabilities */
1043 { 0x00002, 0x00001, 0x000000FF, 0x00000000, hdaRegReadU8 , hdaRegWriteUnimpl , HDA_REG_IDX(VMIN) }, /* Minor Version */
1044 { 0x00003, 0x00001, 0x000000FF, 0x00000000, hdaRegReadU8 , hdaRegWriteUnimpl , HDA_REG_IDX(VMAJ) }, /* Major Version */
1045 { 0x00004, 0x00002, 0x0000FFFF, 0x00000000, hdaRegReadU16 , hdaRegWriteU16 , HDA_REG_IDX(OUTPAY) }, /* Output Payload Capabilities */
1046 { 0x00006, 0x00002, 0x0000FFFF, 0x00000000, hdaRegReadU16 , hdaRegWriteUnimpl , HDA_REG_IDX(INPAY) }, /* Input Payload Capabilities */
1047 { 0x00008, 0x00004, 0x00000103, 0x00000103, hdaRegReadU32 , hdaRegWriteGCTL , HDA_REG_IDX(GCTL) }, /* Global Control */
1048 { 0x0000c, 0x00002, 0x00007FFF, 0x00007FFF, hdaRegReadU16 , hdaRegWriteU16 , HDA_REG_IDX(WAKEEN) }, /* Wake Enable */
1049 { 0x0000e, 0x00002, 0x00000007, 0x00000007, hdaRegReadU8 , hdaRegWriteSTATESTS , HDA_REG_IDX(STATESTS) }, /* State Change Status */
1050 { 0x00010, 0x00002, 0xFFFFFFFF, 0x00000000, hdaRegReadUnimpl , hdaRegWriteUnimpl , HDA_REG_IDX(GSTS) }, /* Global Status */
1051 { 0x00018, 0x00002, 0x0000FFFF, 0x00000000, hdaRegReadU16 , hdaRegWriteU16 , HDA_REG_IDX(OUTSTRMPAY) }, /* Output Stream Payload Capability */
1052 { 0x0001A, 0x00002, 0x0000FFFF, 0x00000000, hdaRegReadU16 , hdaRegWriteUnimpl , HDA_REG_IDX(INSTRMPAY) }, /* Input Stream Payload Capability */
1053 { 0x00020, 0x00004, 0xC00000FF, 0xC00000FF, hdaRegReadU32 , hdaRegWriteU32 , HDA_REG_IDX(INTCTL) }, /* Interrupt Control */
1054 { 0x00024, 0x00004, 0xC00000FF, 0x00000000, hdaRegReadINTSTS , hdaRegWriteUnimpl , HDA_REG_IDX(INTSTS) }, /* Interrupt Status */
1055 { 0x00030, 0x00004, 0xFFFFFFFF, 0x00000000, hdaRegReadWALCLK , hdaRegWriteUnimpl , HDA_REG_IDX_LOCAL(WALCLK) }, /* Wall Clock Counter */
1056 { 0x00034, 0x00004, 0x000000FF, 0x000000FF, hdaRegReadU32 , hdaRegWriteU32 , HDA_REG_IDX(SSYNC) }, /* Stream Synchronization */
1057 { 0x00040, 0x00004, 0xFFFFFF80, 0xFFFFFF80, hdaRegReadU32 , hdaRegWriteBase , HDA_REG_IDX(CORBLBASE) }, /* CORB Lower Base Address */
1058 { 0x00044, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteBase , HDA_REG_IDX(CORBUBASE) }, /* CORB Upper Base Address */
1059 { 0x00048, 0x00002, 0x000000FF, 0x000000FF, hdaRegReadU16 , hdaRegWriteCORBWP , HDA_REG_IDX(CORBWP) }, /* CORB Write Pointer */
1060 { 0x0004A, 0x00002, 0x000080FF, 0x000080FF, hdaRegReadU16 , hdaRegWriteCORBRP , HDA_REG_IDX(CORBRP) }, /* CORB Read Pointer */
1061 { 0x0004C, 0x00001, 0x00000003, 0x00000003, hdaRegReadU8 , hdaRegWriteCORBCTL , HDA_REG_IDX(CORBCTL) }, /* CORB Control */
1062 { 0x0004D, 0x00001, 0x00000001, 0x00000001, hdaRegReadU8 , hdaRegWriteCORBSTS , HDA_REG_IDX(CORBSTS) }, /* CORB Status */
1063 { 0x0004E, 0x00001, 0x000000F3, 0x00000000, hdaRegReadU8 , hdaRegWriteUnimpl , HDA_REG_IDX(CORBSIZE) }, /* CORB Size */
1064 { 0x00050, 0x00004, 0xFFFFFF80, 0xFFFFFF80, hdaRegReadU32 , hdaRegWriteBase , HDA_REG_IDX(RIRBLBASE) }, /* RIRB Lower Base Address */
1065 { 0x00054, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteBase , HDA_REG_IDX(RIRBUBASE) }, /* RIRB Upper Base Address */
1066 { 0x00058, 0x00002, 0x000000FF, 0x00008000, hdaRegReadU8 , hdaRegWriteRIRBWP , HDA_REG_IDX(RIRBWP) }, /* RIRB Write Pointer */
1067 { 0x0005A, 0x00002, 0x000000FF, 0x000000FF, hdaRegReadU16 , hdaRegWriteU16 , HDA_REG_IDX(RINTCNT) }, /* Response Interrupt Count */
1068 { 0x0005C, 0x00001, 0x00000007, 0x00000007, hdaRegReadU8 , hdaRegWriteU8 , HDA_REG_IDX(RIRBCTL) }, /* RIRB Control */
1069 { 0x0005D, 0x00001, 0x00000005, 0x00000005, hdaRegReadU8 , hdaRegWriteRIRBSTS , HDA_REG_IDX(RIRBSTS) }, /* RIRB Status */
1070 { 0x0005E, 0x00001, 0x000000F3, 0x00000000, hdaRegReadU8 , hdaRegWriteUnimpl , HDA_REG_IDX(RIRBSIZE) }, /* RIRB Size */
1071 { 0x00060, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteU32 , HDA_REG_IDX(IC) }, /* Immediate Command */
1072 { 0x00064, 0x00004, 0x00000000, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteUnimpl , HDA_REG_IDX(IR) }, /* Immediate Response */
1073 { 0x00068, 0x00002, 0x00000002, 0x00000002, hdaRegReadIRS , hdaRegWriteIRS , HDA_REG_IDX(IRS) }, /* Immediate Command Status */
1074 { 0x00070, 0x00004, 0xFFFFFFFF, 0xFFFFFF81, hdaRegReadU32 , hdaRegWriteBase , HDA_REG_IDX(DPLBASE) }, /* DMA Position Lower Base */
1075 { 0x00074, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteBase , HDA_REG_IDX(DPUBASE) }, /* DMA Position Upper Base */
1076 /* 4 Serial Data In (SDI). */
1077 HDA_REG_MAP_DEF_STREAM(0, SD0),
1078 HDA_REG_MAP_DEF_STREAM(1, SD1),
1079 HDA_REG_MAP_DEF_STREAM(2, SD2),
1080 HDA_REG_MAP_DEF_STREAM(3, SD3),
1081 /* 4 Serial Data Out (SDO). */
1082 HDA_REG_MAP_DEF_STREAM(4, SD4),
1083 HDA_REG_MAP_DEF_STREAM(5, SD5),
1084 HDA_REG_MAP_DEF_STREAM(6, SD6),
1085 HDA_REG_MAP_DEF_STREAM(7, SD7)
1086};
1087
1088/**
1089 * HDA register aliases (HDA spec 3.3.45).
1090 * @remarks Sorted by offReg.
1091 */
1092static const struct
1093{
1094 /** The alias register offset. */
1095 uint32_t offReg;
1096 /** The register index. */
1097 int idxAlias;
1098} g_aHdaRegAliases[] =
1099{
1100 { 0x2084, HDA_REG_SD0LPIB },
1101 { 0x20a4, HDA_REG_SD1LPIB },
1102 { 0x20c4, HDA_REG_SD2LPIB },
1103 { 0x20e4, HDA_REG_SD3LPIB },
1104 { 0x2104, HDA_REG_SD4LPIB },
1105 { 0x2124, HDA_REG_SD5LPIB },
1106 { 0x2144, HDA_REG_SD6LPIB },
1107 { 0x2164, HDA_REG_SD7LPIB },
1108};
1109
1110#ifdef IN_RING3
1111/** HDABDLE field descriptors for the v6+ saved state. */
1112static SSMFIELD const g_aSSMBDLEFields6[] =
1113{
1114 SSMFIELD_ENTRY(HDABDLE, u64BufAdr),
1115 SSMFIELD_ENTRY(HDABDLE, u32BufSize),
1116 SSMFIELD_ENTRY(HDABDLE, fIntOnCompletion),
1117 SSMFIELD_ENTRY_TERM()
1118};
1119
1120/** HDABDLESTATE field descriptors for the v6+ saved state. */
1121static SSMFIELD const g_aSSMBDLEStateFields6[] =
1122{
1123 SSMFIELD_ENTRY(HDABDLESTATE, u32BDLIndex),
1124 SSMFIELD_ENTRY(HDABDLESTATE, cbBelowFIFOW),
1125 SSMFIELD_ENTRY(HDABDLESTATE, au8FIFO),
1126 SSMFIELD_ENTRY(HDABDLESTATE, u32BufOff),
1127 SSMFIELD_ENTRY_TERM()
1128};
1129
1130/** HDASTREAMSTATE field descriptors for the v6+ saved state. */
1131static SSMFIELD const g_aSSMStreamStateFields6[] =
1132{
1133 SSMFIELD_ENTRY_OLD(cBDLE, 2),
1134 SSMFIELD_ENTRY(HDASTREAMSTATE, uCurBDLE),
1135 SSMFIELD_ENTRY(HDASTREAMSTATE, fDoStop),
1136 SSMFIELD_ENTRY(HDASTREAMSTATE, fActive),
1137 SSMFIELD_ENTRY(HDASTREAMSTATE, fInReset),
1138 SSMFIELD_ENTRY_TERM()
1139};
1140#endif
1141
1142/**
1143 * 32-bit size indexed masks, i.e. g_afMasks[2 bytes] = 0xffff.
1144 */
1145static uint32_t const g_afMasks[5] =
1146{
1147 UINT32_C(0), UINT32_C(0x000000ff), UINT32_C(0x0000ffff), UINT32_C(0x00ffffff), UINT32_C(0xffffffff)
1148};
1149
1150#ifdef IN_RING3
1151DECLINLINE(uint32_t) hdaStreamUpdateLPIB(PHDASTATE pThis, PHDASTREAM pStream, uint32_t u32LPIB)
1152{
1153 AssertPtrReturn(pThis, 0);
1154 AssertPtrReturn(pStream, 0);
1155
1156 Assert(u32LPIB <= pStream->u32CBL);
1157
1158 LogFlowFunc(("[SD%RU8]: LPIB=%RU32 (DMA Position Buffer Enabled: %RTbool)\n",
1159 pStream->u8SD, u32LPIB, pThis->fDMAPosition));
1160
1161 /* Update LPIB in any case. */
1162 HDA_STREAM_REG(pThis, LPIB, pStream->u8SD) = u32LPIB;
1163
1164 /* Do we need to tell the current DMA position? */
1165 if (pThis->fDMAPosition)
1166 {
1167 int rc2 = PDMDevHlpPCIPhysWrite(pThis->CTX_SUFF(pDevIns),
1168 (pThis->u64DPBase & DPBASE_ADDR_MASK) + (pStream->u8SD * 2 * sizeof(uint32_t)),
1169 (void *)&u32LPIB, sizeof(uint32_t));
1170 AssertRC(rc2);
1171 }
1172
1173 return u32LPIB;
1174}
1175#endif
1176
1177/**
1178 * Retrieves the number of bytes of a FIFOS register.
1179 *
1180 * @return Number of bytes of a given FIFOS register.
1181 */
1182DECLINLINE(uint16_t) hdaSDFIFOSToBytes(uint32_t u32RegFIFOS)
1183{
1184 uint16_t cb;
1185 switch (u32RegFIFOS)
1186 {
1187 /* Input */
1188 case HDA_SDIFIFO_120B: cb = 120; break;
1189 case HDA_SDIFIFO_160B: cb = 160; break;
1190
1191 /* Output */
1192 case HDA_SDOFIFO_16B: cb = 16; break;
1193 case HDA_SDOFIFO_32B: cb = 32; break;
1194 case HDA_SDOFIFO_64B: cb = 64; break;
1195 case HDA_SDOFIFO_128B: cb = 128; break;
1196 case HDA_SDOFIFO_192B: cb = 192; break;
1197 case HDA_SDOFIFO_256B: cb = 256; break;
1198 default:
1199 {
1200 cb = 0; /* Can happen on stream reset. */
1201 break;
1202 }
1203 }
1204
1205 return cb;
1206}
1207
1208/**
1209 * Retrieves the number of bytes of a FIFOW register.
1210 *
1211 * @return Number of bytes of a given FIFOW register.
1212 */
1213DECLINLINE(uint8_t) hdaSDFIFOWToBytes(uint32_t u32RegFIFOW)
1214{
1215 uint32_t cb;
1216 switch (u32RegFIFOW)
1217 {
1218 case HDA_SDFIFOW_8B: cb = 8; break;
1219 case HDA_SDFIFOW_16B: cb = 16; break;
1220 case HDA_SDFIFOW_32B: cb = 32; break;
1221 default: cb = 0; break;
1222 }
1223
1224#ifdef RT_STRICT
1225 Assert(RT_IS_POWER_OF_TWO(cb));
1226#endif
1227 return cb;
1228}
1229
1230#ifdef IN_RING3
1231/**
1232 * Fetches the next BDLE to use for a stream.
1233 *
1234 * @return IPRT status code.
1235 */
1236DECLINLINE(int) hdaStreamGetNextBDLE(PHDASTATE pThis, PHDASTREAM pStream)
1237{
1238 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1239 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1240
1241 NOREF(pThis);
1242
1243 Assert(pStream->State.uCurBDLE < pStream->u16LVI + 1);
1244
1245 LogFlowFuncEnter();
1246
1247#ifdef DEBUG
1248 uint32_t uOldBDLE = pStream->State.uCurBDLE;
1249#endif
1250
1251 PHDABDLE pBDLE = &pStream->State.BDLE;
1252
1253 /*
1254 * Switch to the next BDLE entry and do a wrap around
1255 * if we reached the end of the Buffer Descriptor List (BDL).
1256 */
1257 pStream->State.uCurBDLE++;
1258 if (pStream->State.uCurBDLE == pStream->u16LVI + 1)
1259 {
1260 pStream->State.uCurBDLE = 0;
1261
1262 hdaStreamUpdateLPIB(pThis, pStream, 0);
1263 }
1264
1265 Assert(pStream->State.uCurBDLE < pStream->u16LVI + 1);
1266
1267 /* Fetch the next BDLE entry. */
1268 int rc = hdaBDLEFetch(pThis, pBDLE, pStream->u64BDLBase, pStream->State.uCurBDLE);
1269
1270#ifdef DEBUG
1271 LogFlowFunc(("[SD%RU8]: uOldBDLE=%RU16, uCurBDLE=%RU16, LVI=%RU32, rc=%Rrc, %R[bdle]\n",
1272 pStream->u8SD, uOldBDLE, pStream->State.uCurBDLE, pStream->u16LVI, rc, pBDLE));
1273#endif
1274
1275 return rc;
1276}
1277#endif /* IN_RING3 */
1278
1279/**
1280 * Returns the audio direction of a specified stream descriptor.
1281 *
1282 * The register layout specifies that input streams (SDI) come first,
1283 * followed by the output streams (SDO). So every stream ID below HDA_MAX_SDI
1284 * is an input stream, whereas everything >= HDA_MAX_SDI is an output stream.
1285 *
1286 * Note: SDnFMT register does not provide that information, so we have to judge
1287 * for ourselves.
1288 *
1289 * @return Audio direction.
1290 */
1291DECLINLINE(PDMAUDIODIR) hdaGetDirFromSD(uint8_t uSD)
1292{
1293 AssertReturn(uSD <= HDA_MAX_STREAMS, PDMAUDIODIR_UNKNOWN);
1294
1295 if (uSD < HDA_MAX_SDI)
1296 return PDMAUDIODIR_IN;
1297
1298 return PDMAUDIODIR_OUT;
1299}
1300
1301/**
1302 * Returns the HDA stream of specified stream descriptor number.
1303 *
1304 * @return Pointer to HDA stream, or NULL if none found.
1305 */
1306DECLINLINE(PHDASTREAM) hdaStreamFromSD(PHDASTATE pThis, uint8_t uSD)
1307{
1308 AssertPtrReturn(pThis, NULL);
1309 AssertReturn(uSD <= HDA_MAX_STREAMS, NULL);
1310
1311 if (uSD >= HDA_MAX_STREAMS)
1312 return NULL;
1313
1314 return &pThis->aStreams[uSD];
1315}
1316
1317/**
1318 * Returns the HDA stream of specified HDA sink.
1319 *
1320 * @return Pointer to HDA stream, or NULL if none found.
1321 */
1322DECLINLINE(PHDASTREAM) hdaGetStreamFromSink(PHDASTATE pThis, PHDAMIXERSINK pSink)
1323{
1324 AssertPtrReturn(pThis, NULL);
1325 AssertPtrReturn(pSink, NULL);
1326
1327 /** @todo Do something with the channel mapping here? */
1328 return hdaStreamFromSD(pThis, pSink->uSD);
1329}
1330
1331/**
1332 * Retrieves the minimum number of bytes accumulated/free in the
1333 * FIFO before the controller will start a fetch/eviction of data.
1334 *
1335 * Uses SDFIFOW (FIFO Watermark Register).
1336 *
1337 * @return Number of bytes accumulated/free in the FIFO.
1338 */
1339DECLINLINE(uint8_t) hdaStreamGetFIFOW(PHDASTATE pThis, PHDASTREAM pStream)
1340{
1341 AssertPtrReturn(pThis, 0);
1342 AssertPtrReturn(pStream, 0);
1343
1344#ifdef VBOX_HDA_WITH_FIFO
1345 return hdaSDFIFOWToBytes(HDA_STREAM_REG(pThis, FIFOW, pStream->u8SD));
1346#else
1347 return 0;
1348#endif
1349}
1350
1351static int hdaProcessInterrupt(PHDASTATE pThis)
1352{
1353#define IS_INTERRUPT_OCCURED_AND_ENABLED(pThis, num) \
1354 ( INTCTL_SX((pThis), num) \
1355 && (SDSTS(pThis, num) & HDA_REG_FIELD_FLAG_MASK(SDSTS, BCIS)))
1356
1357 int iLevel = 0;
1358
1359 /** @todo Optimize IRQ handling. */
1360
1361 if (/* Controller Interrupt Enable (CIE). */
1362 HDA_REG_FLAG_VALUE(pThis, INTCTL, CIE)
1363 && ( HDA_REG_FLAG_VALUE(pThis, RIRBSTS, RINTFL)
1364 || HDA_REG_FLAG_VALUE(pThis, RIRBSTS, RIRBOIS)
1365 || (HDA_REG(pThis, STATESTS) & HDA_REG(pThis, WAKEEN))))
1366 {
1367 iLevel = 1;
1368 }
1369
1370 if ( IS_INTERRUPT_OCCURED_AND_ENABLED(pThis, 0)
1371 || IS_INTERRUPT_OCCURED_AND_ENABLED(pThis, 1)
1372 || IS_INTERRUPT_OCCURED_AND_ENABLED(pThis, 2)
1373 || IS_INTERRUPT_OCCURED_AND_ENABLED(pThis, 3)
1374 || IS_INTERRUPT_OCCURED_AND_ENABLED(pThis, 4)
1375 || IS_INTERRUPT_OCCURED_AND_ENABLED(pThis, 5)
1376 || IS_INTERRUPT_OCCURED_AND_ENABLED(pThis, 6)
1377 || IS_INTERRUPT_OCCURED_AND_ENABLED(pThis, 7))
1378 {
1379 iLevel = 1;
1380 }
1381
1382 if (HDA_REG_FLAG_VALUE(pThis, INTCTL, GIE))
1383 {
1384 Log3Func(("Level=%d\n", iLevel));
1385 PDMDevHlpPCISetIrq(pThis->CTX_SUFF(pDevIns), 0 , iLevel);
1386 }
1387
1388#undef IS_INTERRUPT_OCCURED_AND_ENABLED
1389
1390 return VINF_SUCCESS;
1391}
1392
1393/**
1394 * Looks up a register at the exact offset given by @a offReg.
1395 *
1396 * @returns Register index on success, -1 if not found.
1397 * @param pThis The HDA device state.
1398 * @param offReg The register offset.
1399 */
1400static int hdaRegLookup(PHDASTATE pThis, uint32_t offReg)
1401{
1402 /*
1403 * Aliases.
1404 */
1405 if (offReg >= g_aHdaRegAliases[0].offReg)
1406 {
1407 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegAliases); i++)
1408 if (offReg == g_aHdaRegAliases[i].offReg)
1409 return g_aHdaRegAliases[i].idxAlias;
1410 Assert(g_aHdaRegMap[RT_ELEMENTS(g_aHdaRegMap) - 1].offset < offReg);
1411 return -1;
1412 }
1413
1414 /*
1415 * Binary search the
1416 */
1417 int idxEnd = RT_ELEMENTS(g_aHdaRegMap);
1418 int idxLow = 0;
1419 for (;;)
1420 {
1421 int idxMiddle = idxLow + (idxEnd - idxLow) / 2;
1422 if (offReg < g_aHdaRegMap[idxMiddle].offset)
1423 {
1424 if (idxLow == idxMiddle)
1425 break;
1426 idxEnd = idxMiddle;
1427 }
1428 else if (offReg > g_aHdaRegMap[idxMiddle].offset)
1429 {
1430 idxLow = idxMiddle + 1;
1431 if (idxLow >= idxEnd)
1432 break;
1433 }
1434 else
1435 return idxMiddle;
1436 }
1437
1438#ifdef RT_STRICT
1439 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegMap); i++)
1440 Assert(g_aHdaRegMap[i].offset != offReg);
1441#endif
1442 return -1;
1443}
1444
1445/**
1446 * Looks up a register covering the offset given by @a offReg.
1447 *
1448 * @returns Register index on success, -1 if not found.
1449 * @param pThis The HDA device state.
1450 * @param offReg The register offset.
1451 */
1452static int hdaRegLookupWithin(PHDASTATE pThis, uint32_t offReg)
1453{
1454 /*
1455 * Aliases.
1456 */
1457 if (offReg >= g_aHdaRegAliases[0].offReg)
1458 {
1459 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegAliases); i++)
1460 {
1461 uint32_t off = offReg - g_aHdaRegAliases[i].offReg;
1462 if (off < 4 && off < g_aHdaRegMap[g_aHdaRegAliases[i].idxAlias].size)
1463 return g_aHdaRegAliases[i].idxAlias;
1464 }
1465 Assert(g_aHdaRegMap[RT_ELEMENTS(g_aHdaRegMap) - 1].offset < offReg);
1466 return -1;
1467 }
1468
1469 /*
1470 * Binary search the register map.
1471 */
1472 int idxEnd = RT_ELEMENTS(g_aHdaRegMap);
1473 int idxLow = 0;
1474 for (;;)
1475 {
1476 int idxMiddle = idxLow + (idxEnd - idxLow) / 2;
1477 if (offReg < g_aHdaRegMap[idxMiddle].offset)
1478 {
1479 if (idxLow == idxMiddle)
1480 break;
1481 idxEnd = idxMiddle;
1482 }
1483 else if (offReg >= g_aHdaRegMap[idxMiddle].offset + g_aHdaRegMap[idxMiddle].size)
1484 {
1485 idxLow = idxMiddle + 1;
1486 if (idxLow >= idxEnd)
1487 break;
1488 }
1489 else
1490 return idxMiddle;
1491 }
1492
1493#ifdef RT_STRICT
1494 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegMap); i++)
1495 Assert(offReg - g_aHdaRegMap[i].offset >= g_aHdaRegMap[i].size);
1496#endif
1497 return -1;
1498}
1499
1500#ifdef IN_RING3
1501static int hdaCmdSync(PHDASTATE pThis, bool fLocal)
1502{
1503 int rc = VINF_SUCCESS;
1504 if (fLocal)
1505 {
1506 Assert((HDA_REG_FLAG_VALUE(pThis, CORBCTL, DMA)));
1507 Assert(pThis->u64CORBBase);
1508 AssertPtr(pThis->pu32CorbBuf);
1509 Assert(pThis->cbCorbBuf);
1510
1511 rc = PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), pThis->u64CORBBase, pThis->pu32CorbBuf, pThis->cbCorbBuf);
1512 if (RT_FAILURE(rc))
1513 AssertRCReturn(rc, rc);
1514#ifdef DEBUG_CMD_BUFFER
1515 uint8_t i = 0;
1516 do
1517 {
1518 LogFunc(("CORB%02x: ", i));
1519 uint8_t j = 0;
1520 do
1521 {
1522 const char *pszPrefix;
1523 if ((i + j) == HDA_REG(pThis, CORBRP));
1524 pszPrefix = "[R]";
1525 else if ((i + j) == HDA_REG(pThis, CORBWP));
1526 pszPrefix = "[W]";
1527 else
1528 pszPrefix = " "; /* three spaces */
1529 LogFunc(("%s%08x", pszPrefix, pThis->pu32CorbBuf[i + j]));
1530 j++;
1531 } while (j < 8);
1532 LogFunc(("\n"));
1533 i += 8;
1534 } while(i != 0);
1535#endif
1536 }
1537 else
1538 {
1539 Assert((HDA_REG_FLAG_VALUE(pThis, RIRBCTL, DMA)));
1540 rc = PDMDevHlpPCIPhysWrite(pThis->CTX_SUFF(pDevIns), pThis->u64RIRBBase, pThis->pu64RirbBuf, pThis->cbRirbBuf);
1541 if (RT_FAILURE(rc))
1542 AssertRCReturn(rc, rc);
1543#ifdef DEBUG_CMD_BUFFER
1544 uint8_t i = 0;
1545 do {
1546 LogFunc(("RIRB%02x: ", i));
1547 uint8_t j = 0;
1548 do {
1549 const char *prefix;
1550 if ((i + j) == HDA_REG(pThis, RIRBWP))
1551 prefix = "[W]";
1552 else
1553 prefix = " ";
1554 LogFunc((" %s%016lx", prefix, pThis->pu64RirbBuf[i + j]));
1555 } while (++j < 8);
1556 LogFunc(("\n"));
1557 i += 8;
1558 } while (i != 0);
1559#endif
1560 }
1561 return rc;
1562}
1563
1564static int hdaCORBCmdProcess(PHDASTATE pThis)
1565{
1566 int rc = hdaCmdSync(pThis, true);
1567 if (RT_FAILURE(rc))
1568 AssertRCReturn(rc, rc);
1569
1570 uint8_t corbRp = HDA_REG(pThis, CORBRP);
1571 uint8_t corbWp = HDA_REG(pThis, CORBWP);
1572 uint8_t rirbWp = HDA_REG(pThis, RIRBWP);
1573
1574 Assert((corbWp != corbRp));
1575 Log3Func(("CORB(RP:%x, WP:%x) RIRBWP:%x\n", HDA_REG(pThis, CORBRP), HDA_REG(pThis, CORBWP), HDA_REG(pThis, RIRBWP)));
1576
1577 while (corbRp != corbWp)
1578 {
1579 uint64_t uResp;
1580 uint32_t uCmd = pThis->pu32CorbBuf[++corbRp];
1581
1582 int rc2 = pThis->pCodec->pfnLookup(pThis->pCodec, HDA_CODEC_CMD(uCmd, 0 /* Codec index */), &uResp);
1583 if (RT_FAILURE(rc2))
1584 LogFunc(("Codec lookup failed with rc=%Rrc\n", rc2));
1585
1586 (rirbWp)++;
1587
1588 if ( (uResp & CODEC_RESPONSE_UNSOLICITED)
1589 && !HDA_REG_FLAG_VALUE(pThis, GCTL, UR))
1590 {
1591 LogFunc(("Unexpected unsolicited response\n"));
1592 HDA_REG(pThis, CORBRP) = corbRp;
1593 return rc;
1594 }
1595
1596 pThis->pu64RirbBuf[rirbWp] = uResp;
1597
1598 pThis->u8RespIntCnt++;
1599 if (pThis->u8RespIntCnt == RINTCNT_N(pThis))
1600 break;
1601 }
1602
1603 HDA_REG(pThis, CORBRP) = corbRp;
1604 HDA_REG(pThis, RIRBWP) = rirbWp;
1605
1606 rc = hdaCmdSync(pThis, false);
1607
1608 Log3Func(("CORB(RP:%x, WP:%x) RIRBWP:%x\n", HDA_REG(pThis, CORBRP), HDA_REG(pThis, CORBWP), HDA_REG(pThis, RIRBWP)));
1609
1610 if (HDA_REG_FLAG_VALUE(pThis, RIRBCTL, RIC))
1611 {
1612 HDA_REG(pThis, RIRBSTS) |= HDA_REG_FIELD_FLAG_MASK(RIRBSTS,RINTFL);
1613
1614 pThis->u8RespIntCnt = 0;
1615 rc = hdaProcessInterrupt(pThis);
1616 }
1617
1618 if (RT_FAILURE(rc))
1619 AssertRCReturn(rc, rc);
1620 return rc;
1621}
1622
1623static int hdaStreamCreate(PHDASTREAM pStream, uint8_t uSD)
1624{
1625 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1626 AssertReturn(uSD <= HDA_MAX_STREAMS, VERR_INVALID_PARAMETER);
1627
1628 int rc = RTSemEventCreate(&pStream->State.hStateChangedEvent);
1629 if (RT_SUCCESS(rc))
1630 rc = RTSemMutexCreate(&pStream->State.hMtx);
1631
1632 if (RT_SUCCESS(rc))
1633 {
1634 pStream->u8SD = uSD;
1635 pStream->pMixSink = NULL;
1636
1637 pStream->State.fActive = false;
1638 pStream->State.fInReset = false;
1639 pStream->State.fDoStop = false;
1640 }
1641
1642 LogFlowFunc(("uSD=%RU8\n", uSD));
1643 return rc;
1644}
1645
1646static void hdaStreamDestroy(PHDASTREAM pStream)
1647{
1648 AssertPtrReturnVoid(pStream);
1649
1650 LogFlowFunc(("[SD%RU8]: Destroying ...\n", pStream->u8SD));
1651
1652 int rc2 = hdaStreamStop(pStream);
1653 AssertRC(rc2);
1654
1655 hdaStreamMapDestroy(&pStream->State.Mapping);
1656
1657 if (pStream->State.hMtx != NIL_RTSEMMUTEX)
1658 {
1659 rc2 = RTSemMutexDestroy(pStream->State.hMtx);
1660 AssertRC(rc2);
1661 pStream->State.hMtx = NIL_RTSEMMUTEX;
1662 }
1663
1664 if (pStream->State.hStateChangedEvent != NIL_RTSEMEVENT)
1665 {
1666 rc2 = RTSemEventDestroy(pStream->State.hStateChangedEvent);
1667 AssertRC(rc2);
1668 pStream->State.hStateChangedEvent = NIL_RTSEMEVENT;
1669 }
1670
1671 LogFlowFuncLeave();
1672}
1673
1674static int hdaStreamInit(PHDASTATE pThis, PHDASTREAM pStream, uint8_t u8SD)
1675{
1676 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1677 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1678
1679 pStream->u8SD = u8SD;
1680 pStream->u64BDLBase = RT_MAKE_U64(HDA_STREAM_REG(pThis, BDPL, pStream->u8SD),
1681 HDA_STREAM_REG(pThis, BDPU, pStream->u8SD));
1682 pStream->u16LVI = HDA_STREAM_REG(pThis, LVI, pStream->u8SD);
1683 pStream->u32CBL = HDA_STREAM_REG(pThis, CBL, pStream->u8SD);
1684 pStream->u16FIFOS = hdaSDFIFOSToBytes(HDA_STREAM_REG(pThis, FIFOS, pStream->u8SD));
1685
1686 RT_ZERO(pStream->State.BDLE);
1687 pStream->State.uCurBDLE = 0;
1688
1689 hdaStreamMapReset(&pStream->State.Mapping);
1690
1691 LogFlowFunc(("[SD%RU8]: DMA @ 0x%x (%RU32 bytes), LVI=%RU16, FIFOS=%RU16\n",
1692 pStream->u8SD, pStream->u64BDLBase, pStream->u32CBL, pStream->u16LVI, pStream->u16FIFOS));
1693
1694#ifdef DEBUG
1695 uint64_t u64BaseDMA = RT_MAKE_U64(HDA_STREAM_REG(pThis, BDPL, pStream->u8SD),
1696 HDA_STREAM_REG(pThis, BDPU, pStream->u8SD));
1697 uint16_t u16LVI = HDA_STREAM_REG(pThis, LVI, pStream->u8SD);
1698 uint32_t u32CBL = HDA_STREAM_REG(pThis, CBL, pStream->u8SD);
1699
1700 LogFlowFunc(("\t-> DMA @ 0x%x, LVI=%RU16, CBL=%RU32\n", u64BaseDMA, u16LVI, u32CBL));
1701
1702 hdaBDLEDumpAll(pThis, u64BaseDMA, u16LVI + 1);
1703#endif
1704
1705 return VINF_SUCCESS;
1706}
1707
1708static void hdaStreamReset(PHDASTATE pThis, PHDASTREAM pStream)
1709{
1710 AssertPtrReturnVoid(pThis);
1711 AssertPtrReturnVoid(pStream);
1712
1713 const uint8_t uSD = pStream->u8SD;
1714
1715#ifdef VBOX_STRICT
1716 AssertReleaseMsg(!RT_BOOL(HDA_STREAM_REG(pThis, CTL, uSD) & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN)),
1717 ("[SD%RU8] Cannot reset stream while in running state\n", uSD));
1718#endif
1719
1720 LogFunc(("[SD%RU8]: Reset\n", uSD));
1721
1722 /*
1723 * Set reset state.
1724 */
1725 Assert(ASMAtomicReadBool(&pStream->State.fInReset) == false); /* No nested calls. */
1726 ASMAtomicXchgBool(&pStream->State.fInReset, true);
1727
1728 /*
1729 * First, reset the internal stream state.
1730 */
1731 RT_ZERO(pStream->State.BDLE);
1732 pStream->State.uCurBDLE = 0;
1733
1734 /*
1735 * Second, initialize the registers.
1736 */
1737 HDA_STREAM_REG(pThis, STS, uSD) = HDA_REG_FIELD_FLAG_MASK(SDSTS, FIFORDY);
1738 /* According to the ICH6 datasheet, 0x40000 is the default value for stream descriptor register 23:20
1739 * bits are reserved for stream number 18.2.33, resets SDnCTL except SRST bit. */
1740 HDA_STREAM_REG(pThis, CTL, uSD) = 0x40000 | (HDA_STREAM_REG(pThis, CTL, uSD) & HDA_REG_FIELD_FLAG_MASK(SDCTL, SRST));
1741 /* ICH6 defines default values (0x77 for input and 0xBF for output descriptors) of FIFO size. 18.2.39. */
1742 HDA_STREAM_REG(pThis, FIFOS, uSD) = hdaGetDirFromSD(uSD) == PDMAUDIODIR_IN ? HDA_SDIFIFO_120B : HDA_SDOFIFO_192B;
1743 /* See 18.2.38: Always defaults to 0x4 (32 bytes). */
1744 HDA_STREAM_REG(pThis, FIFOW, uSD) = HDA_SDFIFOW_32B;
1745 HDA_STREAM_REG(pThis, LPIB, uSD) = 0;
1746 HDA_STREAM_REG(pThis, CBL, uSD) = 0;
1747 HDA_STREAM_REG(pThis, LVI, uSD) = 0;
1748 HDA_STREAM_REG(pThis, FMT, uSD) = HDA_SDFMT_MAKE(HDA_SDFMT_TYPE_PCM, HDA_SDFMT_BASE_44KHZ,
1749 HDA_SDFMT_MULT_1X, HDA_SDFMT_DIV_1X, HDA_SDFMT_16_BIT,
1750 HDA_SDFMT_CHAN_STEREO);
1751 HDA_STREAM_REG(pThis, BDPU, uSD) = 0;
1752 HDA_STREAM_REG(pThis, BDPL, uSD) = 0;
1753
1754 int rc2 = hdaStreamInit(pThis, pStream, uSD);
1755 AssertRC(rc2);
1756
1757 /* Report that we're done resetting this stream. */
1758 HDA_STREAM_REG(pThis, CTL, uSD) = 0;
1759
1760 /* Exit reset state. */
1761 ASMAtomicXchgBool(&pStream->State.fInReset, false);
1762}
1763
1764static bool hdaStreamIsActive(PHDASTATE pThis, PHDASTREAM pStream)
1765{
1766 AssertPtrReturn(pThis, false);
1767 AssertPtrReturn(pStream, false);
1768
1769 bool fActive = pStream->State.fActive;
1770
1771 LogFlowFunc(("SD=%RU8, fActive=%RTbool\n", pStream->u8SD, fActive));
1772 return fActive;
1773}
1774
1775static int hdaStreamSetActive(PHDASTATE pThis, PHDASTREAM pStream, bool fActive)
1776{
1777 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1778 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1779
1780 LogFlowFunc(("[SD%RU8]: fActive=%RTbool, pMixSink=%p\n", pStream->u8SD, fActive, pStream->pMixSink));
1781
1782 if (pStream->State.fActive == fActive) /* No change required? */
1783 {
1784 LogFlowFunc(("[SD%RU8]: No change\n", pStream->u8SD));
1785 return VINF_SUCCESS;
1786 }
1787
1788 int rc = VINF_SUCCESS;
1789
1790 if (pStream->pMixSink) /* Stream attached to a sink? */
1791 {
1792 AUDMIXSINKCMD enmCmd = fActive
1793 ? AUDMIXSINKCMD_ENABLE : AUDMIXSINKCMD_DISABLE;
1794
1795 /* First, enable or disable the stream and the stream's sink, if any. */
1796 if (pStream->pMixSink->pMixSink)
1797 rc = AudioMixerSinkCtl(pStream->pMixSink->pMixSink, enmCmd);
1798 }
1799 else
1800 rc = VINF_SUCCESS;
1801
1802 if (RT_FAILURE(rc))
1803 {
1804 LogFlowFunc(("Failed with rc=%Rrc\n", rc));
1805 return rc;
1806 }
1807
1808 pStream->State.fActive = fActive;
1809
1810 /* Second, see if we need to start or stop the timer. */
1811 if (!fActive)
1812 {
1813 if (pThis->cStreamsActive) /* Disable can be called mupltiple times. */
1814 pThis->cStreamsActive--;
1815
1816#ifndef VBOX_WITH_AUDIO_CALLBACKS
1817 hdaTimerMaybeStop(pThis);
1818#endif
1819 }
1820 else
1821 {
1822 pThis->cStreamsActive++;
1823#ifndef VBOX_WITH_AUDIO_CALLBACKS
1824 hdaTimerMaybeStart(pThis);
1825#endif
1826 }
1827
1828 LogFlowFunc(("u8Strm=%RU8, fActive=%RTbool, cStreamsActive=%RU8\n", pStream->u8SD, fActive, pThis->cStreamsActive));
1829 return VINF_SUCCESS;
1830}
1831
1832static void hdaStreamAssignToSink(PHDASTREAM pStream, PHDAMIXERSINK pMixSink)
1833{
1834 AssertPtrReturnVoid(pStream);
1835
1836 int rc2 = RTSemMutexRequest(pStream->State.hMtx, RT_INDEFINITE_WAIT);
1837 if (RT_SUCCESS(rc2))
1838 {
1839 pStream->pMixSink = pMixSink;
1840
1841 rc2 = RTSemMutexRelease(pStream->State.hMtx);
1842 AssertRC(rc2);
1843 }
1844}
1845
1846static int hdaStreamStart(PHDASTREAM pStream)
1847{
1848 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1849
1850 ASMAtomicXchgBool(&pStream->State.fDoStop, false);
1851 ASMAtomicXchgBool(&pStream->State.fActive, true);
1852
1853 LogFlowFuncLeave();
1854 return VINF_SUCCESS;
1855}
1856
1857static int hdaStreamStop(PHDASTREAM pStream)
1858{
1859 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1860
1861 /* Already in stopped state? */
1862 bool fActive = ASMAtomicReadBool(&pStream->State.fActive);
1863 if (!fActive)
1864 return VINF_SUCCESS;
1865
1866#if 0 /** @todo Does not work (yet), as EMT deadlocks then. */
1867 /*
1868 * Wait for the stream to stop.
1869 */
1870 ASMAtomicXchgBool(&pStream->State.fDoStop, true);
1871
1872 int rc = hdaStreamWaitForStateChange(pStream, 60 * 1000 /* ms timeout */);
1873 fActive = ASMAtomicReadBool(&pStream->State.fActive);
1874 if ( /* Waiting failed? */
1875 RT_FAILURE(rc)
1876 /* Stream is still active? */
1877 || fActive)
1878 {
1879 AssertRC(rc);
1880 LogRel(("HDA: Warning: Unable to stop stream %RU8 (state: %s), rc=%Rrc\n",
1881 pStream->u8Strm, fActive ? "active" : "stopped", rc));
1882 }
1883#else
1884 int rc = VINF_SUCCESS;
1885#endif
1886
1887 LogFlowFuncLeaveRC(rc);
1888 return rc;
1889}
1890
1891static int hdaStreamChannelExtract(PPDMAUDIOSTREAMCHANNEL pChan, const void *pvBuf, size_t cbBuf)
1892{
1893 AssertPtrReturn(pChan, VERR_INVALID_POINTER);
1894 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1895 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
1896
1897 AssertRelease(pChan->cbOff <= cbBuf);
1898
1899 const uint8_t *pu8Buf = (const uint8_t *)pvBuf;
1900
1901 size_t cbSrc = cbBuf - pChan->cbOff;
1902 const uint8_t *pvSrc = &pu8Buf[pChan->cbOff];
1903
1904 size_t cbDst;
1905 uint8_t *pvDst;
1906 RTCircBufAcquireWriteBlock(pChan->Data.pCircBuf, cbBuf, (void **)&pvDst, &cbDst);
1907
1908 cbSrc = RT_MIN(cbSrc, cbDst);
1909
1910 while (cbSrc)
1911 {
1912 AssertBreak(cbDst >= cbSrc);
1913
1914 /* Enough data for at least one next frame? */
1915 if (cbSrc < pChan->cbFrame)
1916 break;
1917
1918 memcpy(pvDst, pvSrc, pChan->cbFrame);
1919
1920 /* Advance to next channel frame in stream. */
1921 pvSrc += pChan->cbStep;
1922 Assert(cbSrc >= pChan->cbStep);
1923 cbSrc -= pChan->cbStep;
1924
1925 /* Advance destination by one frame. */
1926 pvDst += pChan->cbFrame;
1927 Assert(cbDst >= pChan->cbFrame);
1928 cbDst -= pChan->cbFrame;
1929
1930 /* Adjust offset. */
1931 pChan->cbOff += pChan->cbFrame;
1932 }
1933
1934 RTCircBufReleaseWriteBlock(pChan->Data.pCircBuf, cbDst);
1935
1936 return VINF_SUCCESS;
1937}
1938
1939static int hdaStreamChannelAdvance(PPDMAUDIOSTREAMCHANNEL pChan, size_t cbAdv)
1940{
1941 AssertPtrReturn(pChan, VERR_INVALID_POINTER);
1942
1943 if (!cbAdv)
1944 return VINF_SUCCESS;
1945
1946 return VINF_SUCCESS;
1947}
1948
1949static int hdaStreamChannelDataInit(PPDMAUDIOSTREAMCHANNELDATA pChanData, uint32_t fFlags)
1950{
1951 int rc = RTCircBufCreate(&pChanData->pCircBuf, 256); /** @todo Make this configurable? */
1952 if (RT_SUCCESS(rc))
1953 {
1954 pChanData->fFlags = fFlags;
1955 }
1956
1957 return rc;
1958}
1959
1960/**
1961 * Frees a stream channel data block again.
1962 *
1963 * @param pChanData Pointer to channel data to free.
1964 */
1965static void hdaStreamChannelDataDestroy(PPDMAUDIOSTREAMCHANNELDATA pChanData)
1966{
1967 if (!pChanData)
1968 return;
1969
1970 if (pChanData->pCircBuf)
1971 {
1972 RTCircBufDestroy(pChanData->pCircBuf);
1973 pChanData->pCircBuf = NULL;
1974 }
1975
1976 pChanData->fFlags = PDMAUDIOSTREAMCHANNELDATA_FLAG_NONE;
1977}
1978
1979static int hdaStreamChannelAcquireData(PPDMAUDIOSTREAMCHANNELDATA pChanData, void *pvData, size_t *pcbData)
1980{
1981 AssertPtrReturn(pChanData, VERR_INVALID_POINTER);
1982 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
1983 AssertPtrReturn(pcbData, VERR_INVALID_POINTER);
1984
1985 RTCircBufAcquireReadBlock(pChanData->pCircBuf, 256 /** @todo Make this configurarble? */, &pvData, &pChanData->cbAcq);
1986
1987 *pcbData = pChanData->cbAcq;
1988 return VINF_SUCCESS;
1989}
1990
1991static int hdaStreamChannelReleaseData(PPDMAUDIOSTREAMCHANNELDATA pChanData)
1992{
1993 AssertPtrReturn(pChanData, VERR_INVALID_POINTER);
1994 RTCircBufReleaseReadBlock(pChanData->pCircBuf, pChanData->cbAcq);
1995
1996 return VINF_SUCCESS;
1997}
1998
1999static int hdaStreamWaitForStateChange(PHDASTREAM pStream, RTMSINTERVAL msTimeout)
2000{
2001 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
2002
2003 LogFlowFunc(("[SD%RU8]: msTimeout=%RU32\n", pStream->u8SD, msTimeout));
2004 return RTSemEventWait(pStream->State.hStateChangedEvent, msTimeout);
2005}
2006#endif /* IN_RING3 */
2007
2008/* Register access handlers. */
2009
2010static int hdaRegReadUnimpl(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
2011{
2012 *pu32Value = 0;
2013 return VINF_SUCCESS;
2014}
2015
2016static int hdaRegWriteUnimpl(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2017{
2018 return VINF_SUCCESS;
2019}
2020
2021/* U8 */
2022static int hdaRegReadU8(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
2023{
2024 Assert(((pThis->au32Regs[g_aHdaRegMap[iReg].mem_idx] & g_aHdaRegMap[iReg].readable) & 0xffffff00) == 0);
2025 return hdaRegReadU32(pThis, iReg, pu32Value);
2026}
2027
2028static int hdaRegWriteU8(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2029{
2030 Assert((u32Value & 0xffffff00) == 0);
2031 return hdaRegWriteU32(pThis, iReg, u32Value);
2032}
2033
2034/* U16 */
2035static int hdaRegReadU16(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
2036{
2037 Assert(((pThis->au32Regs[g_aHdaRegMap[iReg].mem_idx] & g_aHdaRegMap[iReg].readable) & 0xffff0000) == 0);
2038 return hdaRegReadU32(pThis, iReg, pu32Value);
2039}
2040
2041static int hdaRegWriteU16(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2042{
2043 Assert((u32Value & 0xffff0000) == 0);
2044 return hdaRegWriteU32(pThis, iReg, u32Value);
2045}
2046
2047/* U24 */
2048static int hdaRegReadU24(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
2049{
2050 Assert(((pThis->au32Regs[g_aHdaRegMap[iReg].mem_idx] & g_aHdaRegMap[iReg].readable) & 0xff000000) == 0);
2051 return hdaRegReadU32(pThis, iReg, pu32Value);
2052}
2053
2054static int hdaRegWriteU24(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2055{
2056 Assert((u32Value & 0xff000000) == 0);
2057 return hdaRegWriteU32(pThis, iReg, u32Value);
2058}
2059
2060/* U32 */
2061static int hdaRegReadU32(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
2062{
2063 uint32_t iRegMem = g_aHdaRegMap[iReg].mem_idx;
2064
2065 *pu32Value = pThis->au32Regs[iRegMem] & g_aHdaRegMap[iReg].readable;
2066 return VINF_SUCCESS;
2067}
2068
2069static int hdaRegWriteU32(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2070{
2071 uint32_t iRegMem = g_aHdaRegMap[iReg].mem_idx;
2072
2073 pThis->au32Regs[iRegMem] = (u32Value & g_aHdaRegMap[iReg].writable)
2074 | (pThis->au32Regs[iRegMem] & ~g_aHdaRegMap[iReg].writable);
2075 return VINF_SUCCESS;
2076}
2077
2078static int hdaRegWriteGCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2079{
2080 if (u32Value & HDA_REG_FIELD_FLAG_MASK(GCTL, RST))
2081 {
2082 /* Set the CRST bit to indicate that we're leaving reset mode. */
2083 HDA_REG(pThis, GCTL) |= HDA_REG_FIELD_FLAG_MASK(GCTL, RST);
2084
2085 if (pThis->fInReset)
2086 {
2087 LogFunc(("Guest leaving HDA reset\n"));
2088 pThis->fInReset = false;
2089 }
2090 }
2091 else
2092 {
2093#ifdef IN_RING3
2094 /* Enter reset state. */
2095 LogFunc(("Guest entering HDA reset with DMA(RIRB:%s, CORB:%s)\n",
2096 HDA_REG_FLAG_VALUE(pThis, CORBCTL, DMA) ? "on" : "off",
2097 HDA_REG_FLAG_VALUE(pThis, RIRBCTL, DMA) ? "on" : "off"));
2098
2099 /* Clear the CRST bit to indicate that we're in reset state. */
2100 HDA_REG(pThis, GCTL) &= ~HDA_REG_FIELD_FLAG_MASK(GCTL, RST);
2101 pThis->fInReset = true;
2102
2103 hdaReset(pThis->CTX_SUFF(pDevIns));
2104#else
2105 return VINF_IOM_R3_MMIO_WRITE;
2106#endif
2107 }
2108
2109 if (u32Value & HDA_REG_FIELD_FLAG_MASK(GCTL, FSH))
2110 {
2111 /* Flush: GSTS:1 set, see 6.2.6. */
2112 HDA_REG(pThis, GSTS) |= HDA_REG_FIELD_FLAG_MASK(GSTS, FSH); /* Set the flush state. */
2113 /* DPLBASE and DPUBASE should be initialized with initial value (see 6.2.6). */
2114 }
2115 return VINF_SUCCESS;
2116}
2117
2118static int hdaRegWriteSTATESTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2119{
2120 uint32_t iRegMem = g_aHdaRegMap[iReg].mem_idx;
2121
2122 uint32_t v = pThis->au32Regs[iRegMem];
2123 uint32_t nv = u32Value & HDA_STATES_SCSF;
2124 pThis->au32Regs[iRegMem] &= ~(v & nv); /* write of 1 clears corresponding bit */
2125 return VINF_SUCCESS;
2126}
2127
2128static int hdaRegReadINTSTS(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
2129{
2130 uint32_t v = 0;
2131 if ( HDA_REG_FLAG_VALUE(pThis, RIRBSTS, RIRBOIS)
2132 || HDA_REG_FLAG_VALUE(pThis, RIRBSTS, RINTFL)
2133 || HDA_REG_FLAG_VALUE(pThis, CORBSTS, CMEI)
2134 || HDA_REG(pThis, STATESTS))
2135 {
2136 v |= RT_BIT(30); /* Touch CIS. */
2137 }
2138
2139#define HDA_MARK_STREAM(x) \
2140 if (/* Descriptor Error */ \
2141 (SDSTS((pThis), x) & HDA_REG_FIELD_FLAG_MASK(SDSTS, DE)) \
2142 /* FIFO Error */ \
2143 || (SDSTS((pThis), x) & HDA_REG_FIELD_FLAG_MASK(SDSTS, FE)) \
2144 /* Buffer Completion Interrupt Status */ \
2145 || (SDSTS((pThis), x) & HDA_REG_FIELD_FLAG_MASK(SDSTS, BCIS))) \
2146 { \
2147 Log3Func(("[SD%RU8] BCIS: Marked\n", x)); \
2148 v |= RT_BIT(x); \
2149 }
2150
2151 HDA_MARK_STREAM(0);
2152 HDA_MARK_STREAM(1);
2153 HDA_MARK_STREAM(2);
2154 HDA_MARK_STREAM(3);
2155 HDA_MARK_STREAM(4);
2156 HDA_MARK_STREAM(5);
2157 HDA_MARK_STREAM(6);
2158 HDA_MARK_STREAM(7);
2159
2160#undef HDA_MARK_STREAM
2161
2162 /* "OR" bit of all interrupt status bits. */
2163 v |= v ? RT_BIT(31) : 0;
2164
2165 *pu32Value = v;
2166 return VINF_SUCCESS;
2167}
2168
2169static int hdaRegReadLPIB(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
2170{
2171 const uint8_t u8Strm = HDA_SD_NUM_FROM_REG(pThis, LPIB, iReg);
2172 uint32_t u32LPIB = HDA_STREAM_REG(pThis, LPIB, u8Strm);
2173 const uint32_t u32CBL = HDA_STREAM_REG(pThis, CBL, u8Strm);
2174
2175 LogFlowFunc(("[SD%RU8]: LPIB=%RU32, CBL=%RU32\n", u8Strm, u32LPIB, u32CBL));
2176
2177 *pu32Value = u32LPIB;
2178 return VINF_SUCCESS;
2179}
2180
2181static int hdaRegReadWALCLK(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
2182{
2183 /* HDA spec (1a): 3.3.16 WALCLK counter ticks with 24Mhz bitclock rate. */
2184 *pu32Value = (uint32_t)ASMMultU64ByU32DivByU32(PDMDevHlpTMTimeVirtGetNano(pThis->CTX_SUFF(pDevIns))
2185 - pThis->u64BaseTS, 24, 1000);
2186 LogFlowFunc(("%RU32\n", *pu32Value));
2187 return VINF_SUCCESS;
2188}
2189
2190static int hdaRegReadSSYNC(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
2191{
2192 /* HDA spec (1a): 3.3.16 WALCLK counter ticks with 24Mhz bitclock rate. */
2193 *pu32Value = HDA_REG(pThis, SSYNC);
2194 LogFlowFunc(("%RU32\n", *pu32Value));
2195 return VINF_SUCCESS;
2196}
2197
2198static int hdaRegWriteSSYNC(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2199{
2200 LogFlowFunc(("%RU32\n", u32Value));
2201 return hdaRegWriteU32(pThis, iReg, u32Value);
2202}
2203
2204static int hdaRegWriteCORBRP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2205{
2206 if (u32Value & HDA_REG_FIELD_FLAG_MASK(CORBRP, RST))
2207 {
2208 HDA_REG(pThis, CORBRP) = 0;
2209 }
2210#ifndef BIRD_THINKS_CORBRP_IS_MOSTLY_RO
2211 else
2212 return hdaRegWriteU8(pThis, iReg, u32Value);
2213#endif
2214 return VINF_SUCCESS;
2215}
2216
2217static int hdaRegWriteCORBCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2218{
2219#ifdef IN_RING3
2220 int rc = hdaRegWriteU8(pThis, iReg, u32Value);
2221 AssertRC(rc);
2222 if ( HDA_REG(pThis, CORBWP) != HDA_REG(pThis, CORBRP)
2223 && HDA_REG_FLAG_VALUE(pThis, CORBCTL, DMA) != 0)
2224 {
2225 return hdaCORBCmdProcess(pThis);
2226 }
2227 return rc;
2228#else
2229 return VINF_IOM_R3_MMIO_WRITE;
2230#endif
2231}
2232
2233static int hdaRegWriteCORBSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2234{
2235 uint32_t v = HDA_REG(pThis, CORBSTS);
2236 HDA_REG(pThis, CORBSTS) &= ~(v & u32Value);
2237 return VINF_SUCCESS;
2238}
2239
2240static int hdaRegWriteCORBWP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2241{
2242#ifdef IN_RING3
2243 int rc;
2244 rc = hdaRegWriteU16(pThis, iReg, u32Value);
2245 if (RT_FAILURE(rc))
2246 AssertRCReturn(rc, rc);
2247 if (HDA_REG(pThis, CORBWP) == HDA_REG(pThis, CORBRP))
2248 return VINF_SUCCESS;
2249 if (!HDA_REG_FLAG_VALUE(pThis, CORBCTL, DMA))
2250 return VINF_SUCCESS;
2251 rc = hdaCORBCmdProcess(pThis);
2252 return rc;
2253#else /* !IN_RING3 */
2254 return VINF_IOM_R3_MMIO_WRITE;
2255#endif /* IN_RING3 */
2256}
2257
2258static int hdaRegWriteSDCBL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2259{
2260#ifdef IN_RING3
2261 if (HDA_REG_IND(pThis, iReg) == u32Value) /* Value already set? */
2262 return VINF_SUCCESS;
2263
2264 PHDASTREAM pStream = hdaStreamFromSD(pThis, HDA_SD_NUM_FROM_REG(pThis, CBL, iReg));
2265 if (!pStream)
2266 {
2267 LogFunc(("[SD%RU8]: Warning: Changing SDCBL on non-attached stream (0x%x)\n", HDA_SD_NUM_FROM_REG(pThis, CTL, iReg), u32Value));
2268 return hdaRegWriteU32(pThis, iReg, u32Value);
2269 }
2270
2271 int rc2 = hdaRegWriteSDLock(pThis, pStream, iReg, u32Value);
2272 AssertRC(rc2);
2273
2274 pStream->u32CBL = u32Value;
2275
2276 /* Reset BDLE state. */
2277 RT_ZERO(pStream->State.BDLE);
2278 pStream->State.uCurBDLE = 0;
2279
2280 rc2 = hdaRegWriteU32(pThis, iReg, u32Value);
2281 AssertRC(rc2);
2282
2283 LogFlowFunc(("[SD%RU8]: CBL=%RU32\n", pStream->u8SD, u32Value));
2284 hdaRegWriteSDUnlock(pStream);
2285
2286 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
2287#else /* !IN_RING3 */
2288 return VINF_IOM_R3_MMIO_WRITE;
2289#endif /* IN_RING3 */
2290}
2291
2292static int hdaRegWriteSDCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2293{
2294 bool fRun = RT_BOOL(u32Value & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN));
2295 bool fInRun = RT_BOOL(HDA_REG_IND(pThis, iReg) & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN));
2296 bool fReset = RT_BOOL(u32Value & HDA_REG_FIELD_FLAG_MASK(SDCTL, SRST));
2297 bool fInReset = RT_BOOL(HDA_REG_IND(pThis, iReg) & HDA_REG_FIELD_FLAG_MASK(SDCTL, SRST));
2298
2299 if (HDA_REG_IND(pThis, iReg) == u32Value) /* Value already set? */
2300 return VINF_SUCCESS;
2301
2302 /* Get the stream descriptor. */
2303 uint8_t uSD = HDA_SD_NUM_FROM_REG(pThis, CTL, iReg);
2304
2305 /*
2306 * Extract the stream tag the guest wants to use for this specific
2307 * stream descriptor (SDn). This only can happen if the stream is in a non-running
2308 * state, so we're doing the lookup and assignment here.
2309 *
2310 * So depending on the guest OS, SD3 can use stream tag 4, for example.
2311 */
2312 uint8_t uTag = (u32Value >> HDA_SDCTL_NUM_SHIFT) & HDA_SDCTL_NUM_MASK;
2313 if (uTag > HDA_MAX_TAGS)
2314 {
2315 LogFunc(("[SD%RU8]: Warning: Invalid stream tag %RU8 specified!\n", uSD, uTag));
2316 return hdaRegWriteU24(pThis, iReg, u32Value);
2317 }
2318
2319#ifdef IN_RING3
2320 PHDATAG pTag = &pThis->aTags[uTag];
2321 AssertPtr(pTag);
2322
2323 LogFunc(("[SD%RU8]: Using stream tag=%RU8\n", uSD, uTag));
2324
2325 /* Assign new values. */
2326 pTag->uTag = uTag;
2327 pTag->pStrm = hdaStreamFromSD(pThis, uSD);
2328
2329 PHDASTREAM pStream = pTag->pStrm;
2330 AssertPtr(pStream);
2331
2332 /* Note: Do not use hdaRegWriteSDLock() here, as SDnCTL might change the RUN bit. */
2333 int rc2 = RTSemMutexRequest(pStream->State.hMtx, RT_INDEFINITE_WAIT);
2334 AssertRC(rc2);
2335#endif /* IN_RING3 */
2336
2337 LogFunc(("[SD%RU8]: fRun=%RTbool, fInRun=%RTbool, fReset=%RTbool, fInReset=%RTbool, %R[sdctl]\n",
2338 uSD, fRun, fInRun, fReset, fInReset, u32Value));
2339
2340 if (fInReset)
2341 {
2342 Assert(!fReset);
2343 Assert(!fInRun && !fRun);
2344
2345 /* Report that we're done resetting this stream by clearing SRST. */
2346 HDA_STREAM_REG(pThis, CTL, uSD) &= ~HDA_REG_FIELD_FLAG_MASK(SDCTL, SRST);
2347
2348 LogFunc(("[SD%RU8]: Guest initiated exit of stream reset\n", uSD));
2349 }
2350 else if (fReset)
2351 {
2352#ifdef IN_RING3
2353 /* ICH6 datasheet 18.2.33 says that RUN bit should be cleared before initiation of reset. */
2354 Assert(!fInRun && !fRun);
2355
2356 LogFunc(("[SD%RU8]: Guest initiated enter to stream reset\n", pStream->u8SD));
2357 hdaStreamReset(pThis, pStream);
2358#endif
2359 }
2360 else
2361 {
2362#ifdef IN_RING3
2363 /*
2364 * We enter here to change DMA states only.
2365 */
2366 if (fInRun != fRun)
2367 {
2368 Assert(!fReset && !fInReset);
2369 LogFunc(("[SD%RU8]: fRun=%RTbool\n", pStream->u8SD, fRun));
2370
2371 hdaStreamSetActive(pThis, pStream, fRun);
2372
2373 if (fRun)
2374 {
2375 /* (Re-)Fetch the current BDLE entry. */
2376 rc2 = hdaBDLEFetch(pThis, &pStream->State.BDLE, pStream->u64BDLBase, pStream->State.uCurBDLE);
2377 AssertRC(rc2);
2378 }
2379 }
2380
2381 if (!fInRun && !fRun)
2382 hdaStreamInit(pThis, pStream, pStream->u8SD);
2383#endif /* IN_RING3 */
2384 }
2385
2386 /* Make sure to handle interrupts here as well. */
2387 hdaProcessInterrupt(pThis);
2388
2389#ifdef IN_RING3
2390 rc2 = hdaRegWriteU24(pThis, iReg, u32Value);
2391 AssertRC(rc2);
2392
2393 hdaRegWriteSDUnlock(pStream);
2394 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
2395#else
2396 return VINF_IOM_R3_MMIO_WRITE;
2397#endif
2398}
2399
2400static int hdaRegWriteSDSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2401{
2402 uint32_t v = HDA_REG_IND(pThis, iReg);
2403 /* Clear (zero) FIFOE and DESE bits when writing 1 to it. */
2404 v &= ~(u32Value & v);
2405
2406 HDA_REG_IND(pThis, iReg) = v;
2407
2408 hdaProcessInterrupt(pThis);
2409 return VINF_SUCCESS;
2410}
2411
2412static int hdaRegWriteSDLVI(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2413{
2414#ifdef IN_RING3
2415 if (HDA_REG_IND(pThis, iReg) == u32Value) /* Value already set? */
2416 return VINF_SUCCESS;
2417
2418 PHDASTREAM pStream = hdaStreamFromSD(pThis, HDA_SD_NUM_FROM_REG(pThis, LVI, iReg));
2419 if (!pStream)
2420 {
2421 LogFunc(("[SD%RU8]: Warning: Changing SDLVI on non-attached stream (0x%x)\n", HDA_SD_NUM_FROM_REG(pThis, CTL, iReg), u32Value));
2422 return hdaRegWriteU16(pThis, iReg, u32Value);
2423 }
2424
2425 int rc2 = hdaRegWriteSDLock(pThis, pStream, iReg, u32Value);
2426 AssertRC(rc2);
2427
2428 /** @todo Validate LVI. */
2429 pStream->u16LVI = u32Value;
2430
2431 /* Reset BDLE state. */
2432 RT_ZERO(pStream->State.BDLE);
2433 pStream->State.uCurBDLE = 0;
2434
2435 rc2 = hdaRegWriteU16(pThis, iReg, u32Value);
2436 AssertRC(rc2);
2437
2438 LogFlowFunc(("[SD%RU8]: LVI=%RU32\n", pStream->u8SD, u32Value));
2439 hdaRegWriteSDUnlock(pStream);
2440
2441 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
2442#else /* !IN_RING3 */
2443 return VINF_IOM_R3_MMIO_WRITE;
2444#endif /* IN_RING3 */
2445}
2446
2447static int hdaRegWriteSDFIFOW(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2448{
2449 uint8_t uSD = HDA_SD_NUM_FROM_REG(pThis, FIFOW, iReg);
2450 /** @todo Only allow updating FIFOS if RUN bit is 0? */
2451 uint32_t u32FIFOW = 0;
2452
2453 if (hdaGetDirFromSD(uSD) != PDMAUDIODIR_IN) /* FIFOW for input streams only. */
2454 {
2455 LogRel(("HDA: Warning: Guest tried to write read-only FIFOW to stream #%RU8, ignoring\n", uSD));
2456 return VINF_SUCCESS;
2457 }
2458
2459 switch (u32Value)
2460 {
2461 case HDA_SDFIFOW_8B:
2462 case HDA_SDFIFOW_16B:
2463 case HDA_SDFIFOW_32B:
2464 u32FIFOW = u32Value;
2465 break;
2466 default:
2467 LogRel(("HDA: Warning: Guest tried write unsupported FIFOW (0x%x) to stream #%RU8, defaulting to 32 bytes\n",
2468 u32Value, uSD));
2469 u32FIFOW = HDA_SDFIFOW_32B;
2470 break;
2471 }
2472
2473 if (u32FIFOW)
2474 {
2475 LogFunc(("[SD%RU8]: Updating FIFOW to %RU32 bytes\n", uSD, hdaSDFIFOSToBytes(u32FIFOW)));
2476 /** @todo Update internal stream state with new FIFOS. */
2477
2478 return hdaRegWriteU16(pThis, iReg, u32FIFOW);
2479 }
2480
2481 return VINF_SUCCESS; /* Never reached. */
2482}
2483
2484/**
2485 * @note This method could be called for changing value on Output Streams
2486 * only (ICH6 datasheet 18.2.39).
2487 */
2488static int hdaRegWriteSDFIFOS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2489{
2490 uint8_t uSD = HDA_SD_NUM_FROM_REG(pThis, FIFOS, iReg);
2491 /** @todo Only allow updating FIFOS if RUN bit is 0? */
2492 uint32_t u32FIFOS = 0;
2493
2494 if (hdaGetDirFromSD(uSD) != PDMAUDIODIR_OUT) /* FIFOS for output streams only. */
2495 {
2496 LogRel(("HDA: Warning: Guest tried to write read-only FIFOS to stream #%RU8, ignoring\n", uSD));
2497 return VINF_SUCCESS;
2498 }
2499
2500 switch(u32Value)
2501 {
2502 case HDA_SDOFIFO_16B:
2503 case HDA_SDOFIFO_32B:
2504 case HDA_SDOFIFO_64B:
2505 case HDA_SDOFIFO_128B:
2506 case HDA_SDOFIFO_192B:
2507 u32FIFOS = u32Value;
2508 break;
2509
2510 case HDA_SDOFIFO_256B: /** @todo r=andy Investigate this. */
2511 LogFunc(("256-bit is unsupported, HDA is switched into 192-bit mode\n"));
2512 /* Fall through is intentional. */
2513 default:
2514 LogRel(("HDA: Warning: Guest tried write unsupported FIFOS (0x%x) to stream #%RU8, defaulting to 192 bytes\n",
2515 u32Value, uSD));
2516 u32FIFOS = HDA_SDOFIFO_192B;
2517 break;
2518 }
2519
2520 if (u32FIFOS)
2521 {
2522 LogFunc(("[SD%RU8]: Updating FIFOS to %RU32 bytes\n",
2523 HDA_SD_NUM_FROM_REG(pThis, FIFOS, iReg), hdaSDFIFOSToBytes(u32FIFOS)));
2524 /** @todo Update internal stream state with new FIFOS. */
2525
2526 return hdaRegWriteU16(pThis, iReg, u32FIFOS);
2527 }
2528
2529 return VINF_SUCCESS;
2530}
2531
2532#ifdef IN_RING3
2533static int hdaSDFMTToStrmCfg(uint32_t u32SDFMT, PPDMAUDIOSTREAMCFG pStrmCfg)
2534{
2535 AssertPtrReturn(pStrmCfg, VERR_INVALID_POINTER);
2536
2537# define EXTRACT_VALUE(v, mask, shift) ((v & ((mask) << (shift))) >> (shift))
2538
2539 int rc = VINF_SUCCESS;
2540
2541 uint32_t u32Hz = EXTRACT_VALUE(u32SDFMT, HDA_SDFMT_BASE_RATE_MASK, HDA_SDFMT_BASE_RATE_SHIFT)
2542 ? 44100 : 48000;
2543 uint32_t u32HzMult = 1;
2544 uint32_t u32HzDiv = 1;
2545
2546 switch (EXTRACT_VALUE(u32SDFMT, HDA_SDFMT_MULT_MASK, HDA_SDFMT_MULT_SHIFT))
2547 {
2548 case 0: u32HzMult = 1; break;
2549 case 1: u32HzMult = 2; break;
2550 case 2: u32HzMult = 3; break;
2551 case 3: u32HzMult = 4; break;
2552 default:
2553 LogFunc(("Unsupported multiplier %x\n",
2554 EXTRACT_VALUE(u32SDFMT, HDA_SDFMT_MULT_MASK, HDA_SDFMT_MULT_SHIFT)));
2555 rc = VERR_NOT_SUPPORTED;
2556 break;
2557 }
2558 switch (EXTRACT_VALUE(u32SDFMT, HDA_SDFMT_DIV_MASK, HDA_SDFMT_DIV_SHIFT))
2559 {
2560 case 0: u32HzDiv = 1; break;
2561 case 1: u32HzDiv = 2; break;
2562 case 2: u32HzDiv = 3; break;
2563 case 3: u32HzDiv = 4; break;
2564 case 4: u32HzDiv = 5; break;
2565 case 5: u32HzDiv = 6; break;
2566 case 6: u32HzDiv = 7; break;
2567 case 7: u32HzDiv = 8; break;
2568 default:
2569 LogFunc(("Unsupported divisor %x\n",
2570 EXTRACT_VALUE(u32SDFMT, HDA_SDFMT_DIV_MASK, HDA_SDFMT_DIV_SHIFT)));
2571 rc = VERR_NOT_SUPPORTED;
2572 break;
2573 }
2574
2575 PDMAUDIOFMT enmFmt;
2576 switch (EXTRACT_VALUE(u32SDFMT, HDA_SDFMT_BITS_MASK, HDA_SDFMT_BITS_SHIFT))
2577 {
2578 case 0:
2579 enmFmt = PDMAUDIOFMT_S8;
2580 break;
2581 case 1:
2582 enmFmt = PDMAUDIOFMT_S16;
2583 break;
2584 case 4:
2585 enmFmt = PDMAUDIOFMT_S32;
2586 break;
2587 default:
2588 AssertMsgFailed(("Unsupported bits per sample %x\n",
2589 EXTRACT_VALUE(u32SDFMT, HDA_SDFMT_BITS_MASK, HDA_SDFMT_BITS_SHIFT)));
2590 rc = VERR_NOT_SUPPORTED;
2591 break;
2592 }
2593
2594 if (RT_SUCCESS(rc))
2595 {
2596 pStrmCfg->uHz = u32Hz * u32HzMult / u32HzDiv;
2597 pStrmCfg->cChannels = (u32SDFMT & 0xf) + 1;
2598 pStrmCfg->enmFormat = enmFmt;
2599 pStrmCfg->enmEndianness = PDMAUDIOHOSTENDIANNESS;
2600 }
2601
2602# undef EXTRACT_VALUE
2603 return rc;
2604}
2605
2606static int hdaAddStreamOut(PHDASTATE pThis, PPDMAUDIOSTREAMCFG pCfg)
2607{
2608 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
2609 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
2610
2611 AssertReturn(pCfg->enmDir == PDMAUDIODIR_OUT, VERR_INVALID_PARAMETER);
2612
2613 LogFlowFunc(("Stream=%s\n", pCfg->szName));
2614
2615 int rc = VINF_SUCCESS;
2616
2617 bool fUseFront = true; /* Always use front out by default. */
2618#ifdef VBOX_WITH_HDA_51_SURROUND
2619 bool fUseRear;
2620 bool fUseCenter;
2621 bool fUseLFE;
2622
2623 fUseRear = fUseCenter = fUseLFE = false;
2624
2625 /*
2626 * Use commonly used setups for speaker configurations.
2627 */
2628
2629 /** @todo Make the following configurable through mixer API and/or CFGM? */
2630 switch (pCfg->cChannels)
2631 {
2632 case 3: /* 2.1: Front (Stereo) + LFE. */
2633 {
2634 fUseLFE = true;
2635 break;
2636 }
2637
2638 case 4: /* Quadrophonic: Front (Stereo) + Rear (Stereo). */
2639 {
2640 fUseRear = true;
2641 break;
2642 }
2643
2644 case 5: /* 4.1: Front (Stereo) + Rear (Stereo) + LFE. */
2645 {
2646 fUseRear = true;
2647 fUseLFE = true;
2648 break;
2649 }
2650
2651 case 6: /* 5.1: Front (Stereo) + Rear (Stereo) + Center/LFE. */
2652 {
2653 fUseRear = true;
2654 fUseCenter = true;
2655 fUseLFE = true;
2656 break;
2657 }
2658
2659 default: /* Unknown; fall back to 2 front channels (stereo). */
2660 {
2661 rc = VERR_NOT_SUPPORTED;
2662 break;
2663 }
2664 }
2665#else /* !VBOX_WITH_HDA_51_SURROUND */
2666 /* Only support mono or stereo channels. */
2667 if ( pCfg->cChannels != 1 /* Mono */
2668 && pCfg->cChannels != 2 /* Stereo */)
2669 {
2670 rc = VERR_NOT_SUPPORTED;
2671 }
2672#endif
2673
2674 if (rc == VERR_NOT_SUPPORTED)
2675 {
2676 LogRel(("HDA: Unsupported channel count (%RU8), falling back to stereo channels\n", pCfg->cChannels));
2677 pCfg->cChannels = 2;
2678
2679 rc = VINF_SUCCESS;
2680 }
2681
2682 do
2683 {
2684 if (RT_FAILURE(rc))
2685 break;
2686
2687 if (fUseFront)
2688 {
2689 RTStrPrintf(pCfg->szName, RT_ELEMENTS(pCfg->szName), "Front");
2690 pCfg->DestSource.Dest = PDMAUDIOPLAYBACKDEST_FRONT;
2691 pCfg->cChannels = 2;
2692
2693 rc = hdaCodecRemoveStream(pThis->pCodec, PDMAUDIOMIXERCTL_FRONT);
2694 if (RT_SUCCESS(rc))
2695 rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_FRONT, pCfg);
2696 }
2697
2698#ifdef VBOX_WITH_HDA_51_SURROUND
2699 if ( RT_SUCCESS(rc)
2700 && (fUseCenter || fUseLFE))
2701 {
2702 RTStrPrintf(pCfg->szName, RT_ELEMENTS(pCfg->szName), "Center/LFE");
2703 pCfg->DestSource.Dest = PDMAUDIOPLAYBACKDEST_CENTER_LFE;
2704 pCfg->cChannels = (fUseCenter && fUseLFE) ? 2 : 1;
2705
2706 rc = hdaCodecRemoveStream(pThis->pCodec, PDMAUDIOMIXERCTL_CENTER_LFE);
2707 if (RT_SUCCESS(rc))
2708 rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_CENTER_LFE, pCfg);
2709 }
2710
2711 if ( RT_SUCCESS(rc)
2712 && fUseRear)
2713 {
2714 RTStrPrintf(pCfg->szName, RT_ELEMENTS(pCfg->szName), "Rear");
2715 pCfg->DestSource.Dest = PDMAUDIOPLAYBACKDEST_REAR;
2716 pCfg->cChannels = 2;
2717
2718 rc = hdaCodecRemoveStream(pThis->pCodec, PDMAUDIOMIXERCTL_REAR);
2719 if (RT_SUCCESS(rc))
2720 rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_REAR, pCfg);
2721 }
2722#endif /* VBOX_WITH_HDA_51_SURROUND */
2723
2724 } while (0);
2725
2726 LogFlowFuncLeaveRC(rc);
2727 return rc;
2728}
2729
2730static int hdaAddStreamIn(PHDASTATE pThis, PPDMAUDIOSTREAMCFG pCfg)
2731{
2732 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
2733 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
2734
2735 AssertReturn(pCfg->enmDir == PDMAUDIODIR_IN, VERR_INVALID_PARAMETER);
2736
2737 LogFlowFunc(("Stream=%s, Source=%ld\n", pCfg->szName, pCfg->DestSource.Source));
2738
2739 int rc;
2740
2741 switch (pCfg->DestSource.Source)
2742 {
2743 case PDMAUDIORECSOURCE_LINE:
2744 {
2745 rc = hdaCodecRemoveStream(pThis->pCodec, PDMAUDIOMIXERCTL_LINE_IN);
2746 if (RT_SUCCESS(rc))
2747 rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_LINE_IN, pCfg);
2748 break;
2749 }
2750#ifdef VBOX_WITH_HDA_MIC_IN
2751 case PDMAUDIORECSOURCE_MIC:
2752 {
2753 rc = hdaCodecRemoveStream(pThis->pCodec, PDMAUDIOMIXERCTL_MIC_IN);
2754 if (RT_SUCCESS(rc))
2755 rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_MIC_IN, pCfg);
2756 break;
2757 }
2758#endif
2759 default:
2760 rc = VERR_NOT_SUPPORTED;
2761 break;
2762 }
2763
2764 LogFlowFuncLeaveRC(rc);
2765 return rc;
2766}
2767#endif /* IN_RING3 */
2768
2769static int hdaRegWriteSDFMT(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2770{
2771#ifdef IN_RING3
2772 PDMAUDIOSTREAMCFG strmCfg;
2773 RT_ZERO(strmCfg);
2774
2775 int rc = hdaSDFMTToStrmCfg(u32Value, &strmCfg);
2776 if (RT_FAILURE(rc))
2777 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
2778
2779 PHDASTREAM pStream = hdaStreamFromSD(pThis, HDA_SD_NUM_FROM_REG(pThis, FMT, iReg));
2780 if (!pStream)
2781 {
2782 LogFunc(("[SD%RU8]: Warning: Changing SDFMT on non-attached stream (0x%x)\n",
2783 HDA_SD_NUM_FROM_REG(pThis, FMT, iReg), u32Value));
2784 return hdaRegWriteU16(pThis, iReg, u32Value);
2785 }
2786
2787 rc = hdaRegWriteSDLock(pThis, pStream, iReg, u32Value);
2788 AssertRC(rc);
2789
2790 LogFunc(("[SD%RU8]: Hz=%RU32, Channels=%RU8, enmFmt=%RU32\n",
2791 pStream->u8SD, strmCfg.uHz, strmCfg.cChannels, strmCfg.enmFormat));
2792
2793 /* Set audio direction. */
2794 strmCfg.enmDir = hdaGetDirFromSD(pStream->u8SD);
2795 switch (strmCfg.enmDir)
2796 {
2797 case PDMAUDIODIR_IN:
2798#ifdef VBOX_WITH_HDA_MIC_IN
2799# error "Implement me!"
2800#else
2801 strmCfg.DestSource.Source = PDMAUDIORECSOURCE_LINE;
2802 RTStrPrintf(strmCfg.szName, RT_ELEMENTS(strmCfg.szName), "Line In");
2803#endif
2804 break;
2805
2806 case PDMAUDIODIR_OUT:
2807 /* Destination(s) will be set in hdaAddStreamOut(),
2808 * based on the channels / stream layout. */
2809 break;
2810
2811 default:
2812 rc = VERR_NOT_SUPPORTED;
2813 break;
2814 }
2815
2816 /*
2817 * Initialize the stream mapping in any case, regardless if
2818 * we support surround audio or not. This is needed to handle
2819 * the supported channels within a single audio stream, e.g. mono/stereo.
2820 *
2821 * In other words, the stream mapping *always* knowns the real
2822 * number of channels in a single audio stream.
2823 */
2824 if (RT_SUCCESS(rc))
2825 {
2826 rc = hdaStreamMapInit(&pStream->State.Mapping, &strmCfg);
2827 AssertRC(rc);
2828 }
2829
2830 if (RT_SUCCESS(rc))
2831 {
2832 int rc2;
2833 PHDADRIVER pDrv;
2834 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
2835 {
2836 switch (strmCfg.enmDir)
2837 {
2838 case PDMAUDIODIR_OUT:
2839 rc2 = hdaAddStreamOut(pThis, &strmCfg);
2840 break;
2841
2842 case PDMAUDIODIR_IN:
2843 rc2 = hdaAddStreamIn(pThis, &strmCfg);
2844 break;
2845
2846 default:
2847 rc2 = VERR_NOT_SUPPORTED;
2848 AssertFailed();
2849 break;
2850 }
2851
2852 if ( RT_FAILURE(rc2)
2853 && (pDrv->Flags & PDMAUDIODRVFLAG_PRIMARY)) /* We only care about primary drivers here, the rest may fail. */
2854 {
2855 if (RT_SUCCESS(rc))
2856 rc = rc2;
2857 /* Keep going. */
2858 }
2859 }
2860
2861 /* If (re-)opening the stream by the codec above failed, don't write the new
2862 * format to the register so that the guest is aware it didn't work. */
2863 if (RT_SUCCESS(rc))
2864 {
2865 rc = hdaRegWriteU16(pThis, iReg, u32Value);
2866 AssertRC(rc);
2867 }
2868 else
2869 LogFunc(("[SD%RU8]: (Re-)Opening stream failed with rc=%Rrc\n", pStream->u8SD, rc));
2870
2871 hdaRegWriteSDUnlock(pStream);
2872 }
2873
2874 return VINF_SUCCESS; /* Never return failure. */
2875#else /* !IN_RING3 */
2876 return VINF_IOM_R3_MMIO_WRITE;
2877#endif
2878}
2879
2880/* Note: Will be called for both, BDPL and BDPU, registers. */
2881DECLINLINE(int) hdaRegWriteSDBDPX(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value, uint8_t u8Strm)
2882{
2883#ifdef IN_RING3
2884 if (HDA_REG_IND(pThis, iReg) == u32Value) /* Value already set? */
2885 return VINF_SUCCESS;
2886
2887 PHDASTREAM pStream = hdaStreamFromSD(pThis, u8Strm);
2888 if (!pStream)
2889 {
2890 LogFunc(("[SD%RU8]: Warning: Changing SDBPL/SDBPU on non-attached stream (0x%x)\n", HDA_SD_NUM_FROM_REG(pThis, CTL, iReg), u32Value));
2891 return hdaRegWriteU32(pThis, iReg, u32Value);
2892 }
2893
2894 int rc2 = hdaRegWriteSDLock(pThis, pStream, iReg, u32Value);
2895 AssertRC(rc2);
2896
2897 rc2 = hdaRegWriteU32(pThis, iReg, u32Value);
2898 AssertRC(rc2);
2899
2900 /* Update BDL base. */
2901 pStream->u64BDLBase = RT_MAKE_U64(HDA_STREAM_REG(pThis, BDPL, u8Strm),
2902 HDA_STREAM_REG(pThis, BDPU, u8Strm));
2903 /* Reset BDLE state. */
2904 RT_ZERO(pStream->State.BDLE);
2905 pStream->State.uCurBDLE = 0;
2906
2907 LogFlowFunc(("[SD%RU8]: BDLBase=0x%x\n", pStream->u8SD, pStream->u64BDLBase));
2908 hdaRegWriteSDUnlock(pStream);
2909
2910 return VINF_SUCCESS; /* Always return success to the MMIO handler. */
2911#else /* !IN_RING3 */
2912 return VINF_IOM_R3_MMIO_WRITE;
2913#endif /* IN_RING3 */
2914}
2915
2916static int hdaRegWriteSDBDPL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2917{
2918 return hdaRegWriteSDBDPX(pThis, iReg, u32Value, HDA_SD_NUM_FROM_REG(pThis, BDPL, iReg));
2919}
2920
2921static int hdaRegWriteSDBDPU(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2922{
2923 return hdaRegWriteSDBDPX(pThis, iReg, u32Value, HDA_SD_NUM_FROM_REG(pThis, BDPU, iReg));
2924}
2925
2926#ifdef IN_RING3
2927/**
2928 * XXX
2929 *
2930 * @return bool Returns @true if write is allowed, @false if not.
2931 * @param pThis Pointer to HDA state.
2932 * @param iReg Register to write.
2933 * @param u32Value Value to write.
2934 */
2935DECLINLINE(int) hdaRegWriteSDLock(PHDASTATE pThis, PHDASTREAM pStream, uint32_t iReg, uint32_t u32Value)
2936{
2937 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
2938 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
2939
2940#ifdef VBOX_STRICT
2941 /* Check if the SD's RUN bit is set. */
2942 uint32_t u32SDCTL = HDA_STREAM_REG(pThis, CTL, pStream->u8SD);
2943 bool fIsRunning = RT_BOOL(u32SDCTL & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN));
2944 if (fIsRunning)
2945 {
2946 LogFunc(("[SD%RU8]: Warning: Cannot write to register 0x%x (0x%x) when RUN bit is set (%R[sdctl])\n",
2947 pStream->u8SD, iReg, u32Value, u32SDCTL));
2948# ifdef DEBUG_andy
2949 AssertFailed();
2950# endif
2951 return VERR_ACCESS_DENIED;
2952 }
2953#endif
2954
2955 return RTSemMutexRequest(pStream->State.hMtx, RT_INDEFINITE_WAIT);
2956}
2957
2958DECLINLINE(void) hdaRegWriteSDUnlock(PHDASTREAM pStream)
2959{
2960 AssertPtrReturnVoid(pStream);
2961
2962 int rc2 = RTSemMutexRelease(pStream->State.hMtx);
2963 AssertRC(rc2);
2964}
2965#endif /* IN_RING3 */
2966
2967static int hdaRegReadIRS(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
2968{
2969 /* regarding 3.4.3 we should mark IRS as busy in case CORB is active */
2970 if ( HDA_REG(pThis, CORBWP) != HDA_REG(pThis, CORBRP)
2971 || HDA_REG_FLAG_VALUE(pThis, CORBCTL, DMA))
2972 {
2973 HDA_REG(pThis, IRS) = HDA_REG_FIELD_FLAG_MASK(IRS, ICB); /* busy */
2974 }
2975
2976 return hdaRegReadU32(pThis, iReg, pu32Value);
2977}
2978
2979static int hdaRegWriteIRS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
2980{
2981 int rc = VINF_SUCCESS;
2982
2983 /*
2984 * If the guest set the ICB bit of IRS register, HDA should process the verb in IC register,
2985 * write the response to IR register, and set the IRV (valid in case of success) bit of IRS register.
2986 */
2987 if ( u32Value & HDA_REG_FIELD_FLAG_MASK(IRS, ICB)
2988 && !HDA_REG_FLAG_VALUE(pThis, IRS, ICB))
2989 {
2990#ifdef IN_RING3
2991 uint32_t uCmd = HDA_REG(pThis, IC);
2992
2993 if (HDA_REG(pThis, CORBWP) != HDA_REG(pThis, CORBRP))
2994 {
2995 /*
2996 * 3.4.3: Defines behavior of immediate Command status register.
2997 */
2998 LogRel(("HDA: Guest attempted process immediate verb (%x) with active CORB\n", uCmd));
2999 return rc;
3000 }
3001
3002 HDA_REG(pThis, IRS) = HDA_REG_FIELD_FLAG_MASK(IRS, ICB); /* busy */
3003
3004 uint64_t uResp;
3005 int rc2 = pThis->pCodec->pfnLookup(pThis->pCodec,
3006 HDA_CODEC_CMD(uCmd, 0 /* LUN */), &uResp);
3007 if (RT_FAILURE(rc2))
3008 LogFunc(("Codec lookup failed with rc=%Rrc\n", rc2));
3009
3010 HDA_REG(pThis, IR) = (uint32_t)uResp; /** @todo r=andy Do we need a 64-bit response? */
3011 HDA_REG(pThis, IRS) = HDA_REG_FIELD_FLAG_MASK(IRS, IRV); /* result is ready */
3012 HDA_REG(pThis, IRS) &= ~HDA_REG_FIELD_FLAG_MASK(IRS, ICB); /* busy is clear */
3013#else /* !IN_RING3 */
3014 rc = VINF_IOM_R3_MMIO_WRITE;
3015#endif
3016 return rc;
3017 }
3018
3019 /*
3020 * Once the guest read the response, it should clean the IRV bit of the IRS register.
3021 */
3022 if ( u32Value & HDA_REG_FIELD_FLAG_MASK(IRS, IRV)
3023 && HDA_REG_FLAG_VALUE(pThis, IRS, IRV))
3024 HDA_REG(pThis, IRS) &= ~HDA_REG_FIELD_FLAG_MASK(IRS, IRV);
3025 return rc;
3026}
3027
3028static int hdaRegWriteRIRBWP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
3029{
3030 if (u32Value & HDA_REG_FIELD_FLAG_MASK(RIRBWP, RST))
3031 HDA_REG(pThis, RIRBWP) = 0;
3032
3033 /* The remaining bits are O, see 6.2.22. */
3034 return VINF_SUCCESS;
3035}
3036
3037static int hdaRegWriteBase(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
3038{
3039 uint32_t iRegMem = g_aHdaRegMap[iReg].mem_idx;
3040 int rc = hdaRegWriteU32(pThis, iReg, u32Value);
3041 if (RT_FAILURE(rc))
3042 AssertRCReturn(rc, rc);
3043
3044 switch(iReg)
3045 {
3046 case HDA_REG_CORBLBASE:
3047 pThis->u64CORBBase &= UINT64_C(0xFFFFFFFF00000000);
3048 pThis->u64CORBBase |= pThis->au32Regs[iRegMem];
3049 break;
3050 case HDA_REG_CORBUBASE:
3051 pThis->u64CORBBase &= UINT64_C(0x00000000FFFFFFFF);
3052 pThis->u64CORBBase |= ((uint64_t)pThis->au32Regs[iRegMem] << 32);
3053 break;
3054 case HDA_REG_RIRBLBASE:
3055 pThis->u64RIRBBase &= UINT64_C(0xFFFFFFFF00000000);
3056 pThis->u64RIRBBase |= pThis->au32Regs[iRegMem];
3057 break;
3058 case HDA_REG_RIRBUBASE:
3059 pThis->u64RIRBBase &= UINT64_C(0x00000000FFFFFFFF);
3060 pThis->u64RIRBBase |= ((uint64_t)pThis->au32Regs[iRegMem] << 32);
3061 break;
3062 case HDA_REG_DPLBASE:
3063 {
3064 pThis->u64DPBase &= UINT64_C(0xFFFFFFFF00000000);
3065 pThis->u64DPBase |= pThis->au32Regs[iRegMem];
3066
3067 /* Also make sure to handle the DMA position enable bit. */
3068 pThis->fDMAPosition = RT_BOOL(pThis->u64DPBase & RT_BIT_64(0));
3069 LogRel(("HDA: %s DMA position buffer\n", pThis->fDMAPosition ? "Enabled" : "Disabled"));
3070 break;
3071 }
3072 case HDA_REG_DPUBASE:
3073 pThis->u64DPBase &= UINT64_C(0x00000000FFFFFFFF);
3074 pThis->u64DPBase |= ((uint64_t)pThis->au32Regs[iRegMem] << 32);
3075 break;
3076 default:
3077 AssertMsgFailed(("Invalid index\n"));
3078 break;
3079 }
3080
3081 LogFunc(("CORB base:%llx RIRB base: %llx DP base: %llx\n",
3082 pThis->u64CORBBase, pThis->u64RIRBBase, pThis->u64DPBase));
3083 return rc;
3084}
3085
3086static int hdaRegWriteRIRBSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
3087{
3088 uint8_t v = HDA_REG(pThis, RIRBSTS);
3089 HDA_REG(pThis, RIRBSTS) &= ~(v & u32Value);
3090
3091 return hdaProcessInterrupt(pThis);
3092}
3093
3094#ifdef IN_RING3
3095#ifdef LOG_ENABLED
3096static void hdaBDLEDumpAll(PHDASTATE pThis, uint64_t u64BDLBase, uint16_t cBDLE)
3097{
3098 LogFlowFunc(("BDLEs @ 0x%x (%RU16):\n", u64BDLBase, cBDLE));
3099 if (!u64BDLBase)
3100 return;
3101
3102 uint32_t cbBDLE = 0;
3103 for (uint16_t i = 0; i < cBDLE; i++)
3104 {
3105 uint8_t bdle[16]; /** @todo Use a define. */
3106 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), u64BDLBase + i * 16, bdle, 16); /** @todo Use a define. */
3107
3108 uint64_t addr = *(uint64_t *)bdle;
3109 uint32_t len = *(uint32_t *)&bdle[8];
3110 uint32_t ioc = *(uint32_t *)&bdle[12];
3111
3112 LogFlowFunc(("\t#%03d BDLE(adr:0x%llx, size:%RU32, ioc:%RTbool)\n",
3113 i, addr, len, RT_BOOL(ioc & 0x1)));
3114
3115 cbBDLE += len;
3116 }
3117
3118 LogFlowFunc(("Total: %RU32 bytes\n", cbBDLE));
3119
3120 if (!pThis->u64DPBase) /* No DMA base given? Bail out. */
3121 return;
3122
3123 LogFlowFunc(("DMA counters:\n"));
3124
3125 for (int i = 0; i < cBDLE; i++)
3126 {
3127 uint32_t uDMACnt;
3128 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), (pThis->u64DPBase & DPBASE_ADDR_MASK) + (i * 2 * sizeof(uint32_t)),
3129 &uDMACnt, sizeof(uDMACnt));
3130
3131 LogFlowFunc(("\t#%03d DMA @ 0x%x\n", i , uDMACnt));
3132 }
3133}
3134#endif
3135
3136/**
3137 * Fetches a Bundle Descriptor List Entry (BDLE) from the DMA engine.
3138 *
3139 * @param pThis Pointer to HDA state.
3140 * @param pBDLE Where to store the fetched result.
3141 * @param u64BaseDMA Address base of DMA engine to use.
3142 * @param u16Entry BDLE entry to fetch.
3143 */
3144static int hdaBDLEFetch(PHDASTATE pThis, PHDABDLE pBDLE, uint64_t u64BaseDMA, uint16_t u16Entry)
3145{
3146 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
3147 AssertPtrReturn(pBDLE, VERR_INVALID_POINTER);
3148 AssertReturn(u64BaseDMA, VERR_INVALID_PARAMETER);
3149
3150 if (!u64BaseDMA)
3151 {
3152 LogRel2(("HDA: Unable to fetch BDLE #%RU16 - no base DMA address set (yet)\n", u16Entry));
3153 return VERR_NOT_FOUND;
3154 }
3155 /** @todo Compare u16Entry with LVI. */
3156
3157 uint8_t uBundleEntry[16]; /** @todo Define a BDLE length. */
3158 int rc = PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), u64BaseDMA + u16Entry * 16, /** @todo Define a BDLE length. */
3159 uBundleEntry, RT_ELEMENTS(uBundleEntry));
3160 if (RT_FAILURE(rc))
3161 return rc;
3162
3163 RT_BZERO(pBDLE, sizeof(HDABDLE));
3164
3165 pBDLE->State.u32BDLIndex = u16Entry;
3166 pBDLE->u64BufAdr = *(uint64_t *) uBundleEntry;
3167 pBDLE->u32BufSize = *(uint32_t *)&uBundleEntry[8];
3168 if (pBDLE->u32BufSize < sizeof(uint16_t)) /* Must be at least one word. */
3169 return VERR_INVALID_STATE;
3170
3171 pBDLE->fIntOnCompletion = (*(uint32_t *)&uBundleEntry[12]) & RT_BIT(0);
3172
3173 return VINF_SUCCESS;
3174}
3175
3176/**
3177 * Returns the number of outstanding stream data bytes which need to be processed
3178 * by the DMA engine assigned to this stream.
3179 *
3180 * @return Number of bytes for the DMA engine to process.
3181 */
3182DECLINLINE(uint32_t) hdaStreamGetTransferSize(PHDASTATE pThis, PHDASTREAM pStream, uint32_t cbMax)
3183{
3184 AssertPtrReturn(pThis, 0);
3185 AssertPtrReturn(pStream, 0);
3186
3187 if (!cbMax)
3188 return 0;
3189
3190 PHDABDLE pBDLE = &pStream->State.BDLE;
3191
3192 uint32_t u32LPIB = HDA_STREAM_REG(pThis, LPIB, pStream->u8SD);
3193 Assert(u32LPIB <= pStream->u32CBL);
3194
3195 uint32_t cbFree = pStream->u32CBL - u32LPIB;
3196 if (cbFree)
3197 {
3198 /* Limit to the available free space of the current BDLE. */
3199 cbFree = RT_MIN(cbFree, pBDLE->u32BufSize - pBDLE->State.u32BufOff);
3200
3201 /* Make sure we only copy as much as the stream's FIFO can hold (SDFIFOS, 18.2.39). */
3202 cbFree = RT_MIN(cbFree, uint32_t(pStream->u16FIFOS));
3203
3204 /* Make sure we only transfer as many bytes as requested. */
3205 cbFree = RT_MIN(cbFree, cbMax);
3206
3207 if (pBDLE->State.cbBelowFIFOW)
3208 {
3209 /* Are we not going to reach (or exceed) the FIFO watermark yet with the data to copy?
3210 * No need to read data from DMA then. */
3211 if (cbFree > pBDLE->State.cbBelowFIFOW)
3212 {
3213 /* Subtract the amount of bytes that still would fit in the stream's FIFO
3214 * and therefore do not need to be processed by DMA. */
3215 cbFree -= pBDLE->State.cbBelowFIFOW;
3216 }
3217 }
3218 }
3219
3220 LogFlowFunc(("[SD%RU8]: CBL=%RU32, LPIB=%RU32, FIFOS=%RU16, cbFree=%RU32, %R[bdle]\n", pStream->u8SD,
3221 pStream->u32CBL, HDA_STREAM_REG(pThis, LPIB, pStream->u8SD), pStream->u16FIFOS, cbFree, pBDLE));
3222 return cbFree;
3223}
3224
3225DECLINLINE(void) hdaBDLEUpdate(PHDABDLE pBDLE, uint32_t cbData, uint32_t cbProcessed)
3226{
3227 AssertPtrReturnVoid(pBDLE);
3228
3229 if (!cbData || !cbProcessed)
3230 return;
3231
3232 /* Fewer than cbBelowFIFOW bytes were copied.
3233 * Probably we need to move the buffer, but it is rather hard to imagine a situation
3234 * where it might happen. */
3235 AssertMsg((cbProcessed == pBDLE->State.cbBelowFIFOW + cbData), /* we assume that we write the entire buffer including unreported bytes */
3236 ("cbProcessed=%RU32 != pBDLE->State.cbBelowFIFOW=%RU32 + cbData=%RU32\n",
3237 cbProcessed, pBDLE->State.cbBelowFIFOW, cbData));
3238
3239#if 0
3240 if ( pBDLE->State.cbBelowFIFOW
3241 && pBDLE->State.cbBelowFIFOW <= cbWritten)
3242 {
3243 LogFlowFunc(("BDLE(cbUnderFifoW:%RU32, off:%RU32, size:%RU32)\n",
3244 pBDLE->State.cbBelowFIFOW, pBDLE->State.u32BufOff, pBDLE->u32BufSize));
3245 }
3246#endif
3247
3248 pBDLE->State.cbBelowFIFOW -= RT_MIN(pBDLE->State.cbBelowFIFOW, cbProcessed);
3249 Assert(pBDLE->State.cbBelowFIFOW == 0);
3250
3251 /* We always increment the position of DMA buffer counter because we're always reading
3252 * into an intermediate buffer. */
3253 pBDLE->State.u32BufOff += cbData;
3254 Assert(pBDLE->State.u32BufOff <= pBDLE->u32BufSize);
3255
3256 LogFlowFunc(("cbData=%RU32, cbProcessed=%RU32, %R[bdle]\n", cbData, cbProcessed, pBDLE));
3257}
3258
3259#ifdef IN_RING3
3260/**
3261 * Initializes a stream mapping structure according to the given stream configuration.
3262 *
3263 * @return IPRT status code.
3264 * @param pMapping Pointer to mapping to initialize.
3265 * @param pCfg Pointer to stream configuration to use.
3266 */
3267static int hdaStreamMapInit(PHDASTREAMMAPPING pMapping, PPDMAUDIOSTREAMCFG pCfg)
3268{
3269 AssertPtrReturn(pMapping, VERR_INVALID_POINTER);
3270 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
3271
3272 AssertReturn(pCfg->cChannels, VERR_INVALID_PARAMETER);
3273
3274 hdaStreamMapReset(pMapping);
3275
3276 pMapping->paChannels = (PPDMAUDIOSTREAMCHANNEL)RTMemAlloc(sizeof(PDMAUDIOSTREAMCHANNEL) * pCfg->cChannels);
3277 if (!pMapping->paChannels)
3278 return VERR_NO_MEMORY;
3279
3280 PDMPCMPROPS Props;
3281 int rc = DrvAudioHlpStreamCfgToProps(pCfg, &Props);
3282 if (RT_FAILURE(rc))
3283 return rc;
3284
3285 Assert(RT_IS_POWER_OF_TWO(Props.cBits));
3286
3287 /** @todo We assume all channels in a stream have the same format. */
3288 PPDMAUDIOSTREAMCHANNEL pChan = pMapping->paChannels;
3289 for (uint8_t i = 0; i < pCfg->cChannels; i++)
3290 {
3291 pChan->uChannel = i;
3292 pChan->cbStep = (Props.cBits / 2);
3293 pChan->cbFrame = pChan->cbStep * pCfg->cChannels;
3294 pChan->cbFirst = i * pChan->cbStep;
3295 pChan->cbOff = pChan->cbFirst;
3296
3297 int rc2 = hdaStreamChannelDataInit(&pChan->Data, PDMAUDIOSTREAMCHANNELDATA_FLAG_NONE);
3298 if (RT_SUCCESS(rc))
3299 rc = rc2;
3300
3301 if (RT_FAILURE(rc))
3302 break;
3303
3304 pChan++;
3305 }
3306
3307 if ( RT_SUCCESS(rc)
3308 /* Create circular buffer if not created yet. */
3309 && !pMapping->pCircBuf)
3310 {
3311 rc = RTCircBufCreate(&pMapping->pCircBuf, _4K); /** @todo Make size configurable? */
3312 }
3313
3314 if (RT_SUCCESS(rc))
3315 {
3316 pMapping->cChannels = pCfg->cChannels;
3317#ifdef VBOX_WITH_HDA_INTERLEAVING_STREAMS_SUPPORT
3318 pMapping->enmLayout = PDMAUDIOSTREAMLAYOUT_INTERLEAVED;
3319#else
3320 pMapping->enmLayout = PDMAUDIOSTREAMLAYOUT_NON_INTERLEAVED;
3321#endif
3322 }
3323
3324 return rc;
3325}
3326
3327/**
3328 * Destroys a given stream mapping.
3329 *
3330 * @param pMapping Pointer to mapping to destroy.
3331 */
3332static void hdaStreamMapDestroy(PHDASTREAMMAPPING pMapping)
3333{
3334 hdaStreamMapReset(pMapping);
3335
3336 if (pMapping->pCircBuf)
3337 {
3338 RTCircBufDestroy(pMapping->pCircBuf);
3339 pMapping->pCircBuf = NULL;
3340 }
3341}
3342
3343/**
3344 * Resets a given stream mapping.
3345 *
3346 * @param pMapping Pointer to mapping to reset.
3347 */
3348static void hdaStreamMapReset(PHDASTREAMMAPPING pMapping)
3349{
3350 AssertPtrReturnVoid(pMapping);
3351
3352 pMapping->enmLayout = PDMAUDIOSTREAMLAYOUT_UNKNOWN;
3353
3354 if (pMapping->cChannels)
3355 {
3356 for (uint8_t i = 0; i < pMapping->cChannels; i++)
3357 hdaStreamChannelDataDestroy(&pMapping->paChannels[i].Data);
3358
3359 AssertPtr(pMapping->paChannels);
3360 RTMemFree(pMapping->paChannels);
3361 pMapping->paChannels = NULL;
3362
3363 pMapping->cChannels = 0;
3364 }
3365}
3366#endif /* IN_RING3 */
3367
3368DECLINLINE(bool) hdaStreamNeedsNextBDLE(PHDASTATE pThis, PHDASTREAM pStream)
3369{
3370 AssertPtrReturn(pThis, false);
3371 AssertPtrReturn(pStream, false);
3372
3373 PHDABDLE pBDLE = &pStream->State.BDLE;
3374 uint32_t u32LPIB = HDA_STREAM_REG(pThis, LPIB, pStream->u8SD);
3375
3376 /* Did we reach the CBL (Cyclic Buffer List) limit? */
3377 bool fCBLLimitReached = u32LPIB >= pStream->u32CBL;
3378
3379 /* Do we need to use the next BDLE entry? Either because we reached
3380 * the CBL limit or our internal DMA buffer is full. */
3381 bool fNeedsNextBDLE = ( fCBLLimitReached
3382 || (pBDLE->State.u32BufOff >= pBDLE->u32BufSize));
3383
3384 Assert(u32LPIB <= pStream->u32CBL);
3385 Assert(pBDLE->State.u32BufOff <= pBDLE->u32BufSize);
3386
3387 LogFlowFunc(("[SD%RU8]: LPIB=%RU32, CBL=%RU32, fCBLLimitReached=%RTbool, fNeedsNextBDLE=%RTbool, %R[bdle]\n",
3388 pStream->u8SD, u32LPIB, pStream->u32CBL, fCBLLimitReached, fNeedsNextBDLE, pBDLE));
3389
3390 return fNeedsNextBDLE;
3391}
3392
3393DECLINLINE(void) hdaStreamTransferUpdate(PHDASTATE pThis, PHDASTREAM pStream, uint32_t cbInc)
3394{
3395 AssertPtrReturnVoid(pThis);
3396 AssertPtrReturnVoid(pStream);
3397
3398 LogFlowFunc(("[SD%RU8]: cbInc=%RU32\n", pStream->u8SD, cbInc));
3399
3400 //Assert(cbInc <= pStream->u16FIFOS);
3401
3402 if (!cbInc) /* Nothing to do? Bail out early. */
3403 return;
3404
3405 PHDABDLE pBDLE = &pStream->State.BDLE;
3406
3407 /*
3408 * If we're below the FIFO watermark (SDFIFOW), it's expected that HDA
3409 * doesn't fetch anything via DMA, so just update LPIB.
3410 * (ICH6 datasheet 18.2.38).
3411 */
3412 if (pBDLE->State.cbBelowFIFOW == 0) /* Did we hit (or exceed) the watermark? */
3413 {
3414 uint32_t u32LPIB = HDA_STREAM_REG(pThis, LPIB, pStream->u8SD);
3415
3416 AssertMsg(((u32LPIB + cbInc) <= pStream->u32CBL),
3417 ("[SD%RU8] Increment (%RU32) exceeds CBL (%RU32): LPIB (%RU32)\n",
3418 pStream->u8SD, cbInc, pStream->u32CBL, u32LPIB));
3419
3420 u32LPIB = RT_MIN(u32LPIB + cbInc, pStream->u32CBL);
3421
3422 LogFlowFunc(("[SD%RU8]: LPIB: %RU32 -> %RU32, CBL=%RU32\n",
3423 pStream->u8SD,
3424 HDA_STREAM_REG(pThis, LPIB, pStream->u8SD), HDA_STREAM_REG(pThis, LPIB, pStream->u8SD) + cbInc,
3425 pStream->u32CBL));
3426
3427 hdaStreamUpdateLPIB(pThis, pStream, u32LPIB);
3428 }
3429}
3430
3431static bool hdaStreamTransferIsComplete(PHDASTATE pThis, PHDASTREAM pStream, bool *pfInterrupt)
3432{
3433 AssertPtrReturn(pThis, true);
3434 AssertPtrReturn(pStream, true);
3435
3436 bool fInterrupt = false;
3437 bool fIsComplete = false;
3438
3439 PHDABDLE pBDLE = &pStream->State.BDLE;
3440 const uint32_t u32LPIB = HDA_STREAM_REG(pThis, LPIB, pStream->u8SD);
3441
3442 /* Check if the current BDLE entry is complete (full). */
3443 if (pBDLE->State.u32BufOff >= pBDLE->u32BufSize)
3444 {
3445 Assert(pBDLE->State.u32BufOff <= pBDLE->u32BufSize);
3446
3447 if (/* IOC (Interrupt On Completion) bit set? */
3448 pBDLE->fIntOnCompletion
3449 /* All data put into the DMA FIFO? */
3450 && pBDLE->State.cbBelowFIFOW == 0
3451 )
3452 {
3453 LogFlowFunc(("[SD%RU8]: %R[bdle] => COMPLETE\n", pStream->u8SD, pBDLE));
3454
3455 /*
3456 * If the ICE (IOCE, "Interrupt On Completion Enable") bit of the SDCTL register is set
3457 * we need to generate an interrupt.
3458 */
3459 if (HDA_STREAM_REG(pThis, CTL, pStream->u8SD) & HDA_REG_FIELD_FLAG_MASK(SDCTL, ICE))
3460 fInterrupt = true;
3461 }
3462
3463 fIsComplete = true;
3464 }
3465
3466 if (pfInterrupt)
3467 *pfInterrupt = fInterrupt;
3468
3469 LogFlowFunc(("[SD%RU8]: u32LPIB=%RU32, CBL=%RU32, fIsComplete=%RTbool, fInterrupt=%RTbool, %R[bdle]\n",
3470 pStream->u8SD, u32LPIB, pStream->u32CBL, fIsComplete, fInterrupt, pBDLE));
3471
3472 return fIsComplete;
3473}
3474
3475/**
3476 * hdaReadAudio - copies samples from audio backend to DMA.
3477 * Note: This function writes to the DMA buffer immediately,
3478 * but "reports bytes" when all conditions are met (FIFOW).
3479 */
3480static int hdaReadAudio(PHDASTATE pThis, PHDASTREAM pStream, uint32_t cbToRead, uint32_t *pcbRead)
3481{
3482 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
3483 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
3484 /* pcbRead is optional. */
3485
3486 int rc;
3487 uint32_t cbRead = 0;
3488
3489 do
3490 {
3491 PHDABDLE pBDLE = &pStream->State.BDLE;
3492
3493 if (!cbToRead)
3494 {
3495 rc = VINF_EOF;
3496 break;
3497 }
3498
3499 AssertPtr(pStream->pMixSink);
3500 AssertPtr(pStream->pMixSink->pMixSink);
3501 rc = AudioMixerSinkRead(pStream->pMixSink->pMixSink, AUDMIXOP_BLEND, pBDLE->State.au8FIFO, cbToRead, &cbRead);
3502 if (RT_FAILURE(rc))
3503 break;
3504
3505 if (!cbRead)
3506 {
3507 rc = VINF_EOF;
3508 break;
3509 }
3510
3511 /* Sanity checks. */
3512 Assert(cbRead <= cbToRead);
3513 Assert(cbRead <= sizeof(pBDLE->State.au8FIFO));
3514 Assert(cbRead <= pBDLE->u32BufSize - pBDLE->State.u32BufOff);
3515
3516 /*
3517 * Write to the BDLE's DMA buffer.
3518 */
3519 rc = PDMDevHlpPCIPhysWrite(pThis->CTX_SUFF(pDevIns),
3520 pBDLE->u64BufAdr + pBDLE->State.u32BufOff,
3521 pBDLE->State.au8FIFO, cbRead);
3522 AssertRC(rc);
3523
3524 if (pBDLE->State.cbBelowFIFOW + cbRead > hdaStreamGetFIFOW(pThis, pStream))
3525 {
3526 Assert(pBDLE->State.u32BufOff + cbRead <= pBDLE->u32BufSize);
3527 pBDLE->State.u32BufOff += cbRead;
3528 pBDLE->State.cbBelowFIFOW = 0;
3529 //hdaBackendReadTransferReported(pBDLE, cbDMAData, cbRead, &cbRead, pcbAvail);
3530 }
3531 else
3532 {
3533 Assert(pBDLE->State.u32BufOff + cbRead <= pBDLE->u32BufSize);
3534 pBDLE->State.u32BufOff += cbRead;
3535 pBDLE->State.cbBelowFIFOW += cbRead;
3536 Assert(pBDLE->State.cbBelowFIFOW <= hdaStreamGetFIFOW(pThis, pStream));
3537 //hdaBackendTransferUnreported(pThis, pBDLE, pStreamDesc, cbRead, pcbAvail);
3538
3539 rc = VERR_NO_DATA;
3540 }
3541
3542 } while (0);
3543
3544 if (RT_SUCCESS(rc))
3545 {
3546 if (pcbRead)
3547 *pcbRead = cbRead;
3548 }
3549
3550 if (RT_FAILURE(rc))
3551 LogFlowFunc(("Failed with %Rrc\n", rc));
3552
3553 return rc;
3554}
3555
3556static int hdaWriteAudio(PHDASTATE pThis, PHDASTREAM pStream, uint32_t cbToWrite, uint32_t *pcbWritten)
3557{
3558 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
3559 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
3560 /* pcbWritten is optional. */
3561
3562 PHDABDLE pBDLE = &pStream->State.BDLE;
3563
3564 uint32_t cbWritten = 0;
3565
3566 /*
3567 * Copy from DMA to the corresponding stream buffer (if there are any bytes from the
3568 * previous unreported transfer we write at offset 'pBDLE->State.cbUnderFifoW').
3569 */
3570 int rc;
3571 if (!cbToWrite)
3572 {
3573 rc = VINF_EOF;
3574 }
3575 else
3576 {
3577 void *pvBuf = pBDLE->State.au8FIFO + pBDLE->State.cbBelowFIFOW;
3578 Assert(cbToWrite >= pBDLE->State.cbBelowFIFOW);
3579 uint32_t cbBuf = cbToWrite - pBDLE->State.cbBelowFIFOW;
3580
3581 /*
3582 * Read from the current BDLE's DMA buffer.
3583 */
3584 rc = PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns),
3585 pBDLE->u64BufAdr + pBDLE->State.u32BufOff,
3586 pvBuf, cbBuf);
3587 AssertRC(rc);
3588
3589#ifdef HDA_DEBUG_DUMP_PCM_DATA
3590 RTFILE fh;
3591 RTFileOpen(&fh, HDA_DEBUG_DUMP_PCM_DATA_PATH "hdaWriteAudio-hda.pcm",
3592 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
3593 RTFileWrite(fh, pvBuf, cbBuf, NULL);
3594 RTFileClose(fh);
3595#endif
3596
3597#ifdef VBOX_WITH_STATISTICS
3598 STAM_COUNTER_ADD(&pThis->StatBytesRead, cbBuf);
3599#endif
3600 /*
3601 * Write to audio backend. We should ensure that we have enough bytes to copy to the backend.
3602 */
3603 if (cbBuf >= hdaStreamGetFIFOW(pThis, pStream))
3604 {
3605 PHDASTREAMMAPPING pMapping = &pStream->State.Mapping;
3606
3607 /** @todo Which channel is which? */
3608#ifdef VBOX_WITH_HDA_INTERLEAVING_STREAMS_SUPPORT
3609 PPDMAUDIOSTREAMCHANNEL pChanFront = &pMapping->paChannels[0];
3610#endif
3611#ifdef VBOX_WITH_HDA_51_SURROUND
3612 PPDMAUDIOSTREAMCHANNEL pChanCenterLFE = &pMapping->paChannels[2]; /** @todo FIX! */
3613 PPDMAUDIOSTREAMCHANNEL pChanRear = &pMapping->paChannels[4]; /** @todo FIX! */
3614#endif
3615 int rc2;
3616
3617 void *pvDataFront = NULL;
3618 size_t cbDataFront;
3619#ifdef VBOX_WITH_HDA_INTERLEAVING_STREAMS_SUPPORT
3620 rc2 = hdaStreamChannelExtract(pChanFront, pvBuf, cbBuf);
3621 AssertRC(rc2);
3622
3623 rc2 = hdaStreamChannelAcquireData(&pChanFront->Data, pvDataFront, &cbDataFront);
3624 AssertRC(rc2);
3625#else
3626 /* Use stuff in the whole FIFO to use for the channel data. */
3627 pvDataFront = pvBuf;
3628 cbDataFront = cbBuf;
3629#endif
3630#ifdef VBOX_WITH_HDA_51_SURROUND
3631 void *pvDataCenterLFE;
3632 size_t cbDataCenterLFE;
3633 rc2 = hdaStreamChannelExtract(pChanCenterLFE, pvBuf, cbBuf);
3634 AssertRC(rc2);
3635
3636 rc2 = hdaStreamChannelAcquireData(&pChanCenterLFE->Data, pvDataCenterLFE, &cbDataCenterLFE);
3637 AssertRC(rc2);
3638
3639 void *pvDataRear;
3640 size_t cbDataRear;
3641 rc2 = hdaStreamChannelExtract(pChanRear, pvBuf, cbBuf);
3642 AssertRC(rc2);
3643
3644 rc2 = hdaStreamChannelAcquireData(&pChanRear->Data, pvDataRear, &cbDataRear);
3645 AssertRC(rc2);
3646#endif
3647 /*
3648 * Write data to according mixer sinks.
3649 */
3650 rc2 = AudioMixerSinkWrite(pThis->SinkFront.pMixSink, AUDMIXOP_COPY, pvDataFront, cbDataFront,
3651 NULL /* pcbWritten */);
3652 AssertRC(rc2);
3653#ifdef VBOX_WITH_HDA_51_SURROUND
3654 rc2 = AudioMixerSinkWrite(pThis->SinkCenterLFE, AUDMIXOP_COPY, pvDataCenterLFE, cbDataCenterLFE,
3655 NULL /* pcbWritten */);
3656 AssertRC(rc2);
3657 rc2 = AudioMixerSinkWrite(pThis->SinkRear, AUDMIXOP_COPY, pvDataRear, cbDataRear,
3658 NULL /* pcbWritten */);
3659 AssertRC(rc2);
3660#endif
3661
3662#ifdef VBOX_WITH_HDA_INTERLEAVING_STREAMS_SUPPORT
3663 hdaStreamChannelReleaseData(&pChanFront->Data);
3664#endif
3665#ifdef VBOX_WITH_HDA_51_SURROUND
3666 hdaStreamChannelReleaseData(&pChanCenterLFE->Data);
3667 hdaStreamChannelReleaseData(&pChanRear->Data);
3668#endif
3669
3670 /* Always report all data as being written;
3671 * backends who were not able to catch up have to deal with it themselves. */
3672 cbWritten = cbToWrite;
3673
3674 hdaBDLEUpdate(pBDLE, cbToWrite, cbWritten);
3675 }
3676 else
3677 {
3678 Assert(pBDLE->State.u32BufOff + cbWritten <= pBDLE->u32BufSize);
3679 pBDLE->State.u32BufOff += cbWritten;
3680 pBDLE->State.cbBelowFIFOW += cbWritten;
3681 Assert(pBDLE->State.cbBelowFIFOW <= hdaStreamGetFIFOW(pThis, pStream));
3682
3683 /* Not enough bytes to be processed and reported, we'll try our luck next time around. */
3684 //hdaBackendTransferUnreported(pThis, pBDLE, pStreamDesc, cbAvail, NULL);
3685 rc = VINF_EOF;
3686 }
3687 }
3688
3689 //Assert(cbWritten <= pStream->u16FIFOS);
3690
3691 if (RT_SUCCESS(rc))
3692 {
3693 if (pcbWritten)
3694 *pcbWritten = cbWritten;
3695 }
3696
3697 if (RT_FAILURE(rc))
3698 LogFlowFunc(("Failed with %Rrc\n", rc));
3699
3700 return rc;
3701}
3702
3703/**
3704 * @interface_method_impl{HDACODEC,pfnReset}
3705 */
3706static DECLCALLBACK(int) hdaCodecReset(PHDACODEC pCodec)
3707{
3708 PHDASTATE pThis = pCodec->pHDAState;
3709 NOREF(pThis);
3710 return VINF_SUCCESS;
3711}
3712
3713/**
3714 * Retrieves a corresponding sink for a given mixer control.
3715 * Returns NULL if no sink is found.
3716 *
3717 * @return PHDAMIXERSINK
3718 * @param pThis HDA state.
3719 * @param enmMixerCtl Mixer control to get the corresponding sink for.
3720 */
3721static PHDAMIXERSINK hdaMixerControlToSink(PHDASTATE pThis, PDMAUDIOMIXERCTL enmMixerCtl)
3722{
3723 PHDAMIXERSINK pSink;
3724
3725 switch (enmMixerCtl)
3726 {
3727 case PDMAUDIOMIXERCTL_VOLUME_MASTER:
3728 /* Fall through is intentional. */
3729 case PDMAUDIOMIXERCTL_FRONT:
3730 pSink = &pThis->SinkFront;
3731 break;
3732#ifdef VBOX_WITH_HDA_51_SURROUND
3733 case PDMAUDIOMIXERCTL_CENTER_LFE:
3734 pSink = &pThis->SinkCenterLFE;
3735 break;
3736 case PDMAUDIOMIXERCTL_REAR:
3737 pSink = &pThis->SinkRear;
3738 break;
3739#endif
3740 case PDMAUDIOMIXERCTL_LINE_IN:
3741 pSink = &pThis->SinkLineIn;
3742 break;
3743#ifdef VBOX_WITH_HDA_MIC_IN
3744 case PDMAUDIOMIXERCTL_MIC_IN:
3745 pSink = &pThis->SinkMicIn;
3746 break;
3747#endif
3748 default:
3749 pSink = NULL;
3750 AssertMsgFailed(("Unhandled mixer control\n"));
3751 break;
3752 }
3753
3754 return pSink;
3755}
3756
3757static DECLCALLBACK(int) hdaMixerAddStream(PHDASTATE pThis, PHDAMIXERSINK pSink, PPDMAUDIOSTREAMCFG pCfg)
3758{
3759 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
3760 AssertPtrReturn(pSink, VERR_INVALID_POINTER);
3761 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
3762
3763 LogFunc(("Sink=%s, Stream=%s\n", pSink->pMixSink->pszName, pCfg->szName));
3764
3765 /* Update the sink's format. */
3766 PDMPCMPROPS PCMProps;
3767 int rc = DrvAudioHlpStreamCfgToProps(pCfg, &PCMProps);
3768 if (RT_SUCCESS(rc))
3769 rc = AudioMixerSinkSetFormat(pSink->pMixSink, &PCMProps);
3770
3771 if (RT_FAILURE(rc))
3772 return rc;
3773
3774 PHDADRIVER pDrv;
3775 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
3776 {
3777 int rc2 = VINF_SUCCESS;
3778 PHDAMIXERSTREAM pStream = NULL;
3779
3780 if (pCfg->enmDir == PDMAUDIODIR_IN)
3781 {
3782 LogFunc(("enmRecSource=%ld\n", pCfg->DestSource.Source));
3783
3784 switch (pCfg->DestSource.Source)
3785 {
3786 case PDMAUDIORECSOURCE_LINE:
3787 pStream = &pDrv->LineIn;
3788 break;
3789#ifdef VBOX_WITH_HDA_MIC_IN
3790 case PDMAUDIORECSOURCE_MIC:
3791 pStream = &pDrv->MicIn;
3792 break;
3793#endif
3794 default:
3795 rc2 = VERR_NOT_SUPPORTED;
3796 break;
3797 }
3798 }
3799 else if (pCfg->enmDir == PDMAUDIODIR_OUT)
3800 {
3801 LogFunc(("enmPlaybackDest=%ld\n", pCfg->DestSource.Dest));
3802
3803 switch (pCfg->DestSource.Dest)
3804 {
3805 case PDMAUDIOPLAYBACKDEST_FRONT:
3806 pStream = &pDrv->Front;
3807 break;
3808#ifdef VBOX_WITH_HDA_51_SURROUND
3809 case PDMAUDIOPLAYBACKDEST_CENTER_LFE:
3810 pStream = &pDrv->CenterLFE;
3811 break;
3812 case PDMAUDIOPLAYBACKDEST_REAR:
3813 pStream = &pDrv->Rear;
3814 break;
3815#endif
3816 default:
3817 rc2 = VERR_NOT_SUPPORTED;
3818 break;
3819 }
3820 }
3821 else
3822 rc2 = VERR_NOT_SUPPORTED;
3823
3824 if (RT_SUCCESS(rc2))
3825 {
3826 AssertPtr(pStream);
3827
3828 AudioMixerSinkRemoveStream(pSink->pMixSink, pStream->pMixStrm);
3829
3830 AudioMixerStreamDestroy(pStream->pMixStrm);
3831 pStream->pMixStrm = NULL;
3832
3833 PAUDMIXSTREAM pMixStrm;
3834 rc2 = AudioMixerSinkCreateStream(pSink->pMixSink, pDrv->pConnector, pCfg, 0 /* fFlags */, &pMixStrm);
3835 if (RT_SUCCESS(rc2))
3836 {
3837 rc2 = AudioMixerSinkAddStream(pSink->pMixSink, pMixStrm);
3838 LogFlowFunc(("LUN#%RU8: Added \"%s\" to sink, rc=%Rrc\n", pDrv->uLUN, pCfg->szName , rc2));
3839 }
3840
3841 if (RT_SUCCESS(rc2))
3842 pStream->pMixStrm = pMixStrm;
3843 }
3844
3845 if (RT_SUCCESS(rc))
3846 rc = rc2;
3847 }
3848
3849 LogFlowFuncLeaveRC(rc);
3850 return rc;
3851}
3852
3853/**
3854 * Adds a new audio stream to a specific mixer control.
3855 * Depending on the mixer control the stream then gets assigned to one of the internal
3856 * mixer sinks, which in turn then handle the mixing of all connected streams to that sink.
3857 *
3858 * @return IPRT status code.
3859 * @param pThis HDA state.
3860 * @param enmMixerCtl Mixer control to assign new stream to.
3861 * @param pCfg Stream configuration for the new stream.
3862 */
3863static DECLCALLBACK(int) hdaMixerAddStream(PHDASTATE pThis, PDMAUDIOMIXERCTL enmMixerCtl, PPDMAUDIOSTREAMCFG pCfg)
3864{
3865 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
3866 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
3867
3868 int rc;
3869
3870 PHDAMIXERSINK pSink = hdaMixerControlToSink(pThis, enmMixerCtl);
3871 if (pSink)
3872 {
3873 rc = hdaMixerAddStream(pThis, pSink, pCfg);
3874
3875 AssertPtr(pSink->pMixSink);
3876 LogFlowFunc(("Sink=%s, enmMixerCtl=%ld\n", pSink->pMixSink->pszName, enmMixerCtl));
3877 }
3878 else
3879 rc = VERR_NOT_FOUND;
3880
3881 LogFlowFuncLeaveRC(rc);
3882 return rc;
3883}
3884
3885/**
3886 * Removes a specified mixer control from the HDA's mixer.
3887 *
3888 * @return IPRT status code.
3889 * @param pThis HDA state.
3890 * @param enmMixerCtl Mixer control to remove.
3891 */
3892static DECLCALLBACK(int) hdaMixerRemoveStream(PHDASTATE pThis, PDMAUDIOMIXERCTL enmMixerCtl)
3893{
3894 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
3895
3896 int rc;
3897
3898 PHDAMIXERSINK pSink = hdaMixerControlToSink(pThis, enmMixerCtl);
3899 if (pSink)
3900 {
3901 PHDADRIVER pDrv;
3902 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
3903 {
3904 PAUDMIXSTREAM pMixStream = NULL;
3905 switch (enmMixerCtl)
3906 {
3907 /*
3908 * Input.
3909 */
3910 case PDMAUDIOMIXERCTL_LINE_IN:
3911 pMixStream = pDrv->LineIn.pMixStrm;
3912 pDrv->LineIn.pMixStrm = NULL;
3913 break;
3914#ifdef VBOX_WITH_HDA_MIC_IN
3915 case PDMAUDIOMIXERCTL_MIC_IN:
3916 pMixStream = pDrv->MicIn.pMixStrm;
3917 pDrv->MicIn.pMixStrm = NULL;
3918 break;
3919#endif
3920 /*
3921 * Output.
3922 */
3923 case PDMAUDIOMIXERCTL_FRONT:
3924 pMixStream = pDrv->Front.pMixStrm;
3925 pDrv->Front.pMixStrm = NULL;
3926 break;
3927#ifdef VBOX_WITH_HDA_51_SURROUND
3928 case PDMAUDIOMIXERCTL_CENTER_LFE:
3929 pMixStream = pDrv->CenterLFE.pMixStrm;
3930 pDrv->CenterLFE.pMixStrm = NULL;
3931 break;
3932 case PDMAUDIOMIXERCTL_REAR:
3933 pMixStream = pDrv->Rear.pMixStrm;
3934 pDrv->Rear.pMixStrm = NULL;
3935 break;
3936#endif
3937 default:
3938 AssertMsgFailed(("Mixer control %ld not implemented\n", enmMixerCtl));
3939 break;
3940 }
3941
3942 if (pMixStream)
3943 {
3944 AudioMixerSinkRemoveStream(pSink->pMixSink, pMixStream);
3945 AudioMixerStreamDestroy(pMixStream);
3946
3947 pMixStream = NULL;
3948 }
3949 }
3950
3951 AudioMixerSinkRemoveAllStreams(pSink->pMixSink);
3952 rc = VINF_SUCCESS;
3953 }
3954 else
3955 rc = VERR_NOT_FOUND;
3956
3957 LogFlowFunc(("enmMixerCtl=%ld, rc=%Rrc\n", enmMixerCtl, rc));
3958 return rc;
3959}
3960
3961/**
3962 * Sets a SDn stream number and channel to a particular mixer control.
3963 *
3964 * @returns IPRT status code.
3965 * @param pThis HDA State.
3966 * @param enmMixerCtl Mixer control to set SD stream number and channel for.
3967 * @param uSD SD stream number (number + 1) to set. Set to 0 for unassign.
3968 * @param uChannel Channel to set. Only valid if a valid SD stream number is specified.
3969 */
3970static DECLCALLBACK(int) hdaMixerSetStream(PHDASTATE pThis,
3971 PDMAUDIOMIXERCTL enmMixerCtl, uint8_t uSD, uint8_t uChannel)
3972{
3973 LogFlowFunc(("enmMixerCtl=%RU32, uSD=%RU8, uChannel=%RU8\n", enmMixerCtl, uSD, uChannel));
3974
3975 if (uSD == 0) /* Stream number 0 is reserved. */
3976 {
3977 LogFlowFunc(("Invalid SDn (%RU8) number for mixer control %ld, ignoring\n", uSD, enmMixerCtl));
3978 return VINF_SUCCESS;
3979 }
3980 /* uChannel is optional. */
3981
3982 /* SDn0 starts as 1. */
3983 Assert(uSD);
3984 uSD--;
3985
3986 int rc;
3987
3988 PHDAMIXERSINK pSink = hdaMixerControlToSink(pThis, enmMixerCtl);
3989 if (pSink)
3990 {
3991 if ( (uSD < HDA_MAX_SDI)
3992 && AudioMixerSinkGetDir(pSink->pMixSink) == AUDMIXSINKDIR_OUTPUT)
3993 {
3994 uSD += HDA_MAX_SDI;
3995 }
3996
3997 LogFlowFunc(("%s: Setting to stream ID=%RU8, channel=%RU8, enmMixerCtl=%RU32\n",
3998 pSink->pMixSink->pszName, uSD, uChannel, enmMixerCtl));
3999
4000 Assert(uSD < HDA_MAX_STREAMS);
4001
4002 PHDASTREAM pStream = hdaStreamFromSD(pThis, uSD);
4003 if (pStream)
4004 {
4005 pSink->uSD = uSD;
4006 pSink->uChannel = uChannel;
4007
4008 /* Make sure that the stream also has this sink set. */
4009 hdaStreamAssignToSink(pStream, pSink);
4010
4011 rc = VINF_SUCCESS;
4012 }
4013 else
4014 {
4015 LogRel(("HDA: Guest wanted to assign invalid stream ID=%RU8 (channel %RU8) to mixer control %RU32, skipping\n",
4016 uSD, uChannel, enmMixerCtl));
4017 rc = VERR_INVALID_PARAMETER;
4018 }
4019 }
4020 else
4021 rc = VERR_NOT_FOUND;
4022
4023 LogFlowFuncLeaveRC(rc);
4024 return rc;
4025}
4026
4027/**
4028 * Sets the volume of a specified mixer control.
4029 *
4030 * @return IPRT status code.
4031 * @param pThis HDA State.
4032 * @param enmMixerCtl Mixer control to set volume for.
4033 * @param pVol Pointer to volume data to set.
4034 */
4035static DECLCALLBACK(int) hdaMixerSetVolume(PHDASTATE pThis,
4036 PDMAUDIOMIXERCTL enmMixerCtl, PPDMAUDIOVOLUME pVol)
4037{
4038 int rc;
4039
4040 PHDAMIXERSINK pSink = hdaMixerControlToSink(pThis, enmMixerCtl);
4041 if (pSink)
4042 {
4043 /* Set the volume.
4044 * We assume that the codec already converted it to the correct range. */
4045 rc = AudioMixerSinkSetVolume(pSink->pMixSink, pVol);
4046 }
4047 else
4048 rc = VERR_NOT_FOUND;
4049
4050 LogFlowFuncLeaveRC(rc);
4051 return rc;
4052}
4053
4054#ifndef VBOX_WITH_AUDIO_CALLBACKS
4055
4056static void hdaTimerMaybeStart(PHDASTATE pThis)
4057{
4058 if (pThis->cStreamsActive == 0) /* Only start the timer if there are no active streams. */
4059 return;
4060
4061 if (!pThis->pTimer)
4062 return;
4063
4064 LogFlowFuncEnter();
4065
4066 LogFlowFunc(("Starting timer\n"));
4067
4068 /* Set timer flag. */
4069 ASMAtomicXchgBool(&pThis->fTimerActive, true);
4070
4071 /* Update current time timestamp. */
4072 pThis->uTimerTS = TMTimerGet(pThis->pTimer);
4073
4074 /* Fire off timer. */
4075 TMTimerSet(pThis->pTimer, TMTimerGet(pThis->pTimer) + pThis->cTimerTicks);
4076}
4077
4078static void hdaTimerMaybeStop(PHDASTATE pThis)
4079{
4080 if (pThis->cStreamsActive) /* Some streams still active? Bail out. */
4081 return;
4082
4083 if (!pThis->pTimer)
4084 return;
4085
4086 LogFlowFunc(("Stopping timer\n"));
4087
4088 /* Set timer flag. */
4089 ASMAtomicXchgBool(&pThis->fTimerActive, false);
4090}
4091
4092static DECLCALLBACK(void) hdaTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
4093{
4094 PHDASTATE pThis = (PHDASTATE)pvUser;
4095 Assert(pThis == PDMINS_2_DATA(pDevIns, PHDASTATE));
4096 AssertPtr(pThis);
4097
4098 STAM_PROFILE_START(&pThis->StatTimer, a);
4099
4100 uint64_t cTicksNow = TMTimerGet(pTimer);
4101 uint64_t cTicksElapsed = cTicksNow - pThis->uTimerTS;
4102
4103 LogFlowFuncEnter();
4104
4105 /* Update current time timestamp. */
4106 pThis->uTimerTS = cTicksNow;
4107
4108 /* Flag indicating whether to kick the timer again for a
4109 * new data processing round. */
4110 bool fKickTimer = false;
4111
4112 PHDASTREAM pStreamLineIn = hdaGetStreamFromSink(pThis, &pThis->SinkLineIn);
4113#ifdef VBOX_WITH_HDA_MIC_IN
4114 PHDASTREAM pStreamMicIn = hdaGetStreamFromSink(pThis, &pThis->SinkMicIn);
4115#endif
4116 PHDASTREAM pStreamFront = hdaGetStreamFromSink(pThis, &pThis->SinkFront);
4117#ifdef VBOX_WITH_HDA_51_SURROUND
4118 /** @todo See note below. */
4119#endif
4120
4121 uint32_t cbToProcess;
4122 int rc = AudioMixerSinkUpdate(pThis->SinkLineIn.pMixSink);
4123 if (RT_SUCCESS(rc))
4124 {
4125 cbToProcess = AudioMixerSinkGetReadable(pThis->SinkLineIn.pMixSink);
4126 if (cbToProcess)
4127 {
4128 rc = hdaTransfer(pThis, pStreamLineIn, cbToProcess, NULL /* pcbProcessed */);
4129 fKickTimer |= RT_SUCCESS(rc);
4130 }
4131 }
4132
4133#ifdef VBOX_WITH_HDA_MIC_IN
4134 rc = AudioMixerSinkUpdate(pThis->SinkMicIn.pMixSink);
4135 if (RT_SUCCESS(rc))
4136 {
4137 cbToProcess = AudioMixerSinkGetReadable(pThis->SinkMicIn.pMixSink);
4138 if (cbToProcess)
4139 {
4140 rc = hdaTransfer(pThis, pStreamMicIn, cbToProcess, NULL /* pcbProcessed */);
4141 fKickTimer |= RT_SUCCESS(rc);
4142 }
4143 }
4144#endif
4145
4146#ifdef VBOX_WITH_HDA_51_SURROUND
4147 rc = AudioMixerSinkUpdate(pThis->SinkCenterLFE.pMixSink);
4148 if (RT_SUCCESS(rc))
4149 {
4150
4151 }
4152
4153 rc = AudioMixerSinkUpdate(pThis->SinkRear.pMixSink);
4154 if (RT_SUCCESS(rc))
4155 {
4156
4157 }
4158 /** @todo Check for stream interleaving and only call hdaTransfer() if required! */
4159
4160 /*
4161 * Only call hdaTransfer if CenterLFE and/or Rear are on different SDs,
4162 * otherwise we have to use the interleaved streams support for getting the data
4163 * out of the Front sink (depending on the mapping layout).
4164 */
4165#endif
4166 rc = AudioMixerSinkUpdate(pThis->SinkFront.pMixSink);
4167 if (RT_SUCCESS(rc))
4168 {
4169 cbToProcess = AudioMixerSinkGetWritable(pThis->SinkFront.pMixSink);
4170 if (cbToProcess)
4171 {
4172 rc = hdaTransfer(pThis, pStreamFront, cbToProcess, NULL /* pcbProcessed */);
4173 fKickTimer |= RT_SUCCESS(rc);
4174 }
4175 }
4176
4177 if ( ASMAtomicReadBool(&pThis->fTimerActive)
4178 || fKickTimer)
4179 {
4180 /* Kick the timer again. */
4181 uint64_t cTicks = pThis->cTimerTicks;
4182 /** @todo adjust cTicks down by now much cbOutMin represents. */
4183 TMTimerSet(pThis->pTimer, cTicksNow + cTicks);
4184 }
4185
4186 LogFlowFuncLeave();
4187
4188 STAM_PROFILE_STOP(&pThis->StatTimer, a);
4189}
4190
4191#else /* VBOX_WITH_AUDIO_CALLBACKS */
4192
4193static DECLCALLBACK(int) hdaCallbackInput(PDMAUDIOCALLBACKTYPE enmType, void *pvCtx, size_t cbCtx, void *pvUser, size_t cbUser)
4194{
4195 Assert(enmType == PDMAUDIOCALLBACKTYPE_INPUT);
4196 AssertPtrReturn(pvCtx, VERR_INVALID_POINTER);
4197 AssertReturn(cbCtx, VERR_INVALID_PARAMETER);
4198 AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
4199 AssertReturn(cbUser, VERR_INVALID_PARAMETER);
4200
4201 PHDACALLBACKCTX pCtx = (PHDACALLBACKCTX)pvCtx;
4202 AssertReturn(cbCtx == sizeof(HDACALLBACKCTX), VERR_INVALID_PARAMETER);
4203
4204 PPDMAUDIOCALLBACKDATAIN pData = (PPDMAUDIOCALLBACKDATAIN)pvUser;
4205 AssertReturn(cbUser == sizeof(PDMAUDIOCALLBACKDATAIN), VERR_INVALID_PARAMETER);
4206
4207 return hdaTransfer(pCtx->pThis, PI_INDEX, UINT32_MAX, &pData->cbOutRead);
4208}
4209
4210static DECLCALLBACK(int) hdaCallbackOutput(PDMAUDIOCALLBACKTYPE enmType, void *pvCtx, size_t cbCtx, void *pvUser, size_t cbUser)
4211{
4212 Assert(enmType == PDMAUDIOCALLBACKTYPE_OUTPUT);
4213 AssertPtrReturn(pvCtx, VERR_INVALID_POINTER);
4214 AssertReturn(cbCtx, VERR_INVALID_PARAMETER);
4215 AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
4216 AssertReturn(cbUser, VERR_INVALID_PARAMETER);
4217
4218 PHDACALLBACKCTX pCtx = (PHDACALLBACKCTX)pvCtx;
4219 AssertReturn(cbCtx == sizeof(HDACALLBACKCTX), VERR_INVALID_PARAMETER);
4220
4221 PPDMAUDIOCALLBACKDATAOUT pData = (PPDMAUDIOCALLBACKDATAOUT)pvUser;
4222 AssertReturn(cbUser == sizeof(PDMAUDIOCALLBACKDATAOUT), VERR_INVALID_PARAMETER);
4223
4224 PHDASTATE pThis = pCtx->pThis;
4225
4226 int rc = hdaTransfer(pCtx->pThis, PO_INDEX, UINT32_MAX, &pData->cbOutWritten);
4227 if ( RT_SUCCESS(rc)
4228 && pData->cbOutWritten)
4229 {
4230 PHDADRIVER pDrv;
4231 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
4232 {
4233 uint32_t cSamplesPlayed;
4234 int rc2 = pDrv->pConnector->pfnPlay(pDrv->pConnector, &cSamplesPlayed);
4235 LogFlowFunc(("LUN#%RU8: cSamplesPlayed=%RU32, rc=%Rrc\n", pDrv->uLUN, cSamplesPlayed, rc2));
4236 }
4237 }
4238}
4239#endif /* VBOX_WITH_AUDIO_CALLBACKS */
4240
4241static int hdaTransfer(PHDASTATE pThis, PHDASTREAM pStream, uint32_t cbToProcess, uint32_t *pcbProcessed)
4242{
4243 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
4244 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
4245 /* pcbProcessed is optional. */
4246
4247 if (ASMAtomicReadBool(&pThis->fInReset)) /* HDA controller in reset mode? Bail out. */
4248 {
4249 LogFlowFunc(("HDA in reset mode, skipping\n"));
4250
4251 if (pcbProcessed)
4252 *pcbProcessed = 0;
4253 return VINF_SUCCESS;
4254 }
4255
4256 bool fProceed = true;
4257 int rc = RTSemMutexRequest(pStream->State.hMtx, RT_INDEFINITE_WAIT);
4258 if (RT_FAILURE(rc))
4259 return rc;
4260
4261 Log3Func(("[SD%RU8] fActive=%RTbool, cbToProcess=%RU32\n", pStream->u8SD, pStream->State.fActive, cbToProcess));
4262
4263 /* Stop request received? */
4264 if ( !pStream->State.fActive
4265 || pStream->State.fDoStop)
4266 {
4267 pStream->State.fActive = false;
4268
4269 rc = RTSemEventSignal(pStream->State.hStateChangedEvent);
4270 AssertRC(rc);
4271
4272 fProceed = false;
4273 }
4274 /* Is the stream not in a running state currently? */
4275 else if (!(HDA_STREAM_REG(pThis, CTL, pStream->u8SD) & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN)))
4276 fProceed = false;
4277 /* Nothing to process? */
4278 else if (!cbToProcess)
4279 fProceed = false;
4280
4281 if ((HDA_STREAM_REG(pThis, STS, pStream->u8SD) & HDA_REG_FIELD_FLAG_MASK(SDSTS, BCIS)))
4282 {
4283 Log3Func(("[SD%RU8]: BCIS set\n", pStream->u8SD));
4284 fProceed = false;
4285 }
4286
4287 if (!fProceed)
4288 {
4289 Log3Func(("[SD%RU8]: Skipping\n", pStream->u8SD));
4290
4291 rc = RTSemMutexRelease(pStream->State.hMtx);
4292 AssertRC(rc);
4293
4294 if (pcbProcessed)
4295 *pcbProcessed = 0;
4296 return VINF_SUCCESS;
4297 }
4298
4299 /* Sanity checks. */
4300 Assert(pStream->u8SD <= HDA_MAX_STREAMS);
4301 Assert(pStream->u64BDLBase);
4302 Assert(pStream->u32CBL);
4303
4304 /* State sanity checks. */
4305 Assert(pStream->State.fInReset == false);
4306
4307 uint32_t u32LPIB = HDA_STREAM_REG(pThis, LPIB, pStream->u8SD);
4308 Assert(u32LPIB <= pStream->u32CBL);
4309
4310 bool fInterrupt = false;
4311
4312#ifdef DEBUG_andy
4313//# define DEBUG_SIMPLE
4314#endif
4315
4316#ifdef DEBUG_SIMPLE
4317 uint8_t u8FIFO[_16K+1];
4318 size_t u8FIFOff = 0;
4319#endif
4320
4321 uint32_t cbLeft = cbToProcess;
4322 uint32_t cbTotal = 0;
4323 uint32_t cbChunk = 0;
4324 uint32_t cbChunkProcessed = 0;
4325
4326 /* Set the FIFORDY bit on the stream while doing the transfer. */
4327 HDA_STREAM_REG(pThis, STS, pStream->u8SD) |= HDA_REG_FIELD_FLAG_MASK(SDSTS, FIFORDY);
4328
4329 while (cbLeft)
4330 {
4331 /* Do we need to fetch the next Buffer Descriptor Entry (BDLE)? */
4332 if (hdaStreamNeedsNextBDLE(pThis, pStream))
4333 {
4334 rc = hdaStreamGetNextBDLE(pThis, pStream);
4335 if (RT_FAILURE(rc))
4336 break;
4337 }
4338
4339 cbChunk = hdaStreamGetTransferSize(pThis, pStream, cbLeft);
4340 cbChunkProcessed = 0;
4341
4342 if (hdaGetDirFromSD(pStream->u8SD) == PDMAUDIODIR_IN)
4343 rc = hdaReadAudio(pThis, pStream, cbChunk, &cbChunkProcessed);
4344 else
4345 {
4346#ifndef DEBUG_SIMPLE
4347 rc = hdaWriteAudio(pThis, pStream, cbChunk, &cbChunkProcessed);
4348#else
4349 void *pvBuf = u8FIFO + u8FIFOff;
4350 int32_t cbBuf = cbChunk;
4351
4352 PHDABDLE pBDLE = &pStream->State.BDLE;
4353
4354 if (cbBuf)
4355 rc = PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns),
4356 pBDLE->u64BufAdr + pBDLE->State.u32BufOff,
4357 pvBuf, cbBuf);
4358
4359 cbChunkProcessed = cbChunk;
4360
4361 hdaBDLEUpdate(pBDLE, cbChunkProcessed, cbChunkProcessed);
4362
4363 u8FIFOff += cbChunkProcessed;
4364 Assert((u8FIFOff & 1) == 0);
4365 Assert(u8FIFOff <= sizeof(u8FIFO));
4366#endif
4367 }
4368
4369 if (RT_FAILURE(rc))
4370 break;
4371
4372 hdaStreamTransferUpdate(pThis, pStream, cbChunkProcessed);
4373
4374 Assert(cbLeft >= cbChunkProcessed);
4375 cbLeft -= cbChunkProcessed;
4376 cbTotal += cbChunkProcessed;
4377
4378 if (rc == VINF_EOF)
4379 break;
4380
4381 if (hdaStreamTransferIsComplete(pThis, pStream, &fInterrupt))
4382 break;
4383 }
4384
4385 /* Remove the FIFORDY bit again. */
4386 HDA_STREAM_REG(pThis, STS, pStream->u8SD) &= ~HDA_REG_FIELD_FLAG_MASK(SDSTS, FIFORDY);
4387
4388 LogFlowFunc(("[SD%RU8]: %RU32 / %RU32, rc=%Rrc\n", pStream->u8SD, cbTotal, cbToProcess, rc));
4389
4390#ifdef DEBUG_SIMPLE
4391# ifdef HDA_DEBUG_DUMP_PCM_DATA
4392 RTFILE fh;
4393 RTFileOpen(&fh, HDA_DEBUG_DUMP_PCM_DATA_PATH "hdaWriteAudio.pcm",
4394 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
4395 RTFileWrite(fh, u8FIFO, u8FIFOff, NULL);
4396 RTFileClose(fh);
4397# endif
4398
4399 AudioMixerSinkWrite(pThis->SinkFront.pMixSink, AUDMIXOP_COPY, u8FIFO, u8FIFOff,
4400 NULL /* pcbWritten */);
4401#endif /* DEBUG_SIMPLE */
4402
4403 if (fInterrupt)
4404 {
4405 /**
4406 * Set the BCIS (Buffer Completion Interrupt Status) flag as the
4407 * last byte of data for the current descriptor has been fetched
4408 * from memory and put into the DMA FIFO.
4409 *
4410 * Speech synthesis works fine on Mac Guest if this bit isn't set
4411 * but in general sound quality gets worse.
4412 *
4413 * This must be set in *any* case.
4414 */
4415 HDA_STREAM_REG(pThis, STS, pStream->u8SD) |= HDA_REG_FIELD_FLAG_MASK(SDSTS, BCIS);
4416 Log3Func(("[SD%RU8]: BCIS: Set\n", pStream->u8SD));
4417
4418 hdaProcessInterrupt(pThis);
4419 }
4420
4421 if (RT_SUCCESS(rc))
4422 {
4423 if (pcbProcessed)
4424 *pcbProcessed = cbTotal;
4425 }
4426
4427 int rc2 = RTSemMutexRelease(pStream->State.hMtx);
4428 if (RT_SUCCESS(rc))
4429 rc = rc2;
4430
4431 return rc;
4432}
4433#endif /* IN_RING3 */
4434
4435/* MMIO callbacks */
4436
4437/**
4438 * @callback_method_impl{FNIOMMMIOREAD, Looks up and calls the appropriate handler.}
4439 *
4440 * @note During implementation, we discovered so-called "forgotten" or "hole"
4441 * registers whose description is not listed in the RPM, datasheet, or
4442 * spec.
4443 */
4444PDMBOTHCBDECL(int) hdaMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
4445{
4446 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4447 int rc;
4448
4449 /*
4450 * Look up and log.
4451 */
4452 uint32_t offReg = GCPhysAddr - pThis->MMIOBaseAddr;
4453 int idxRegDsc = hdaRegLookup(pThis, offReg); /* Register descriptor index. */
4454#ifdef LOG_ENABLED
4455 unsigned const cbLog = cb;
4456 uint32_t offRegLog = offReg;
4457#endif
4458
4459 Log3Func(("offReg=%#x cb=%#x\n", offReg, cb));
4460 Assert(cb == 4); Assert((offReg & 3) == 0);
4461
4462 if (pThis->fInReset && idxRegDsc != HDA_REG_GCTL)
4463 LogFunc(("Access to registers except GCTL is blocked while reset\n"));
4464
4465 if (idxRegDsc == -1)
4466 LogRel(("HDA: Invalid read access @0x%x (bytes=%u)\n", offReg, cb));
4467
4468 if (idxRegDsc != -1)
4469 {
4470 /* ASSUMES gapless DWORD at end of map. */
4471 if (g_aHdaRegMap[idxRegDsc].size == 4)
4472 {
4473 /*
4474 * Straight forward DWORD access.
4475 */
4476 rc = g_aHdaRegMap[idxRegDsc].pfnRead(pThis, idxRegDsc, (uint32_t *)pv);
4477 Log3Func(("\tRead %s => %x (%Rrc)\n", g_aHdaRegMap[idxRegDsc].abbrev, *(uint32_t *)pv, rc));
4478 }
4479 else
4480 {
4481 /*
4482 * Multi register read (unless there are trailing gaps).
4483 * ASSUMES that only DWORD reads have sideeffects.
4484 */
4485 uint32_t u32Value = 0;
4486 unsigned cbLeft = 4;
4487 do
4488 {
4489 uint32_t const cbReg = g_aHdaRegMap[idxRegDsc].size;
4490 uint32_t u32Tmp = 0;
4491
4492 rc = g_aHdaRegMap[idxRegDsc].pfnRead(pThis, idxRegDsc, &u32Tmp);
4493 Log3Func(("\tRead %s[%db] => %x (%Rrc)*\n", g_aHdaRegMap[idxRegDsc].abbrev, cbReg, u32Tmp, rc));
4494 if (rc != VINF_SUCCESS)
4495 break;
4496 u32Value |= (u32Tmp & g_afMasks[cbReg]) << ((4 - cbLeft) * 8);
4497
4498 cbLeft -= cbReg;
4499 offReg += cbReg;
4500 idxRegDsc++;
4501 } while (cbLeft > 0 && g_aHdaRegMap[idxRegDsc].offset == offReg);
4502
4503 if (rc == VINF_SUCCESS)
4504 *(uint32_t *)pv = u32Value;
4505 else
4506 Assert(!IOM_SUCCESS(rc));
4507 }
4508 }
4509 else
4510 {
4511 rc = VINF_IOM_MMIO_UNUSED_FF;
4512 Log3Func(("\tHole at %x is accessed for read\n", offReg));
4513 }
4514
4515 /*
4516 * Log the outcome.
4517 */
4518#ifdef LOG_ENABLED
4519 if (cbLog == 4)
4520 Log3Func(("\tReturning @%#05x -> %#010x %Rrc\n", offRegLog, *(uint32_t *)pv, rc));
4521 else if (cbLog == 2)
4522 Log3Func(("\tReturning @%#05x -> %#06x %Rrc\n", offRegLog, *(uint16_t *)pv, rc));
4523 else if (cbLog == 1)
4524 Log3Func(("\tReturning @%#05x -> %#04x %Rrc\n", offRegLog, *(uint8_t *)pv, rc));
4525#endif
4526 return rc;
4527}
4528
4529
4530DECLINLINE(int) hdaWriteReg(PHDASTATE pThis, int idxRegDsc, uint32_t u32Value, char const *pszLog)
4531{
4532 if (pThis->fInReset && idxRegDsc != HDA_REG_GCTL)
4533 {
4534 LogRel2(("HDA: Warning: Access to register 0x%x is blocked while reset\n", idxRegDsc));
4535 return VINF_SUCCESS;
4536 }
4537
4538 uint32_t idxRegMem = g_aHdaRegMap[idxRegDsc].mem_idx;
4539#ifdef LOG_ENABLED
4540 uint32_t const u32CurValue = pThis->au32Regs[idxRegMem];
4541#endif
4542 int rc = g_aHdaRegMap[idxRegDsc].pfnWrite(pThis, idxRegDsc, u32Value);
4543 Log3Func(("Written value %#x to %s[%d byte]; %x => %x%s\n", u32Value, g_aHdaRegMap[idxRegDsc].abbrev,
4544 g_aHdaRegMap[idxRegDsc].size, u32CurValue, pThis->au32Regs[idxRegMem], pszLog));
4545 return rc;
4546}
4547
4548
4549/**
4550 * @callback_method_impl{FNIOMMMIOWRITE, Looks up and calls the appropriate handler.}
4551 */
4552PDMBOTHCBDECL(int) hdaMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
4553{
4554 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4555 int rc;
4556
4557 /*
4558 * The behavior of accesses that aren't aligned on natural boundraries is
4559 * undefined. Just reject them outright.
4560 */
4561 /** @todo IOM could check this, it could also split the 8 byte accesses for us. */
4562 Assert(cb == 1 || cb == 2 || cb == 4 || cb == 8);
4563 if (GCPhysAddr & (cb - 1))
4564 return PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "misaligned write access: GCPhysAddr=%RGp cb=%u\n", GCPhysAddr, cb);
4565
4566 /*
4567 * Look up and log the access.
4568 */
4569 uint32_t offReg = GCPhysAddr - pThis->MMIOBaseAddr;
4570 int idxRegDsc = hdaRegLookup(pThis, offReg);
4571 uint32_t idxRegMem = idxRegDsc != -1 ? g_aHdaRegMap[idxRegDsc].mem_idx : UINT32_MAX;
4572 uint64_t u64Value;
4573 if (cb == 4) u64Value = *(uint32_t const *)pv;
4574 else if (cb == 2) u64Value = *(uint16_t const *)pv;
4575 else if (cb == 1) u64Value = *(uint8_t const *)pv;
4576 else if (cb == 8) u64Value = *(uint64_t const *)pv;
4577 else
4578 {
4579 u64Value = 0; /* shut up gcc. */
4580 AssertReleaseMsgFailed(("%u\n", cb));
4581 }
4582
4583#ifdef LOG_ENABLED
4584 uint32_t const u32LogOldValue = idxRegDsc >= 0 ? pThis->au32Regs[idxRegMem] : UINT32_MAX;
4585 if (idxRegDsc == -1)
4586 Log3Func(("@%#05x u32=%#010x cb=%d\n", offReg, *(uint32_t const *)pv, cb));
4587 else if (cb == 4)
4588 Log3Func(("@%#05x u32=%#010x %s\n", offReg, *(uint32_t *)pv, g_aHdaRegMap[idxRegDsc].abbrev));
4589 else if (cb == 2)
4590 Log3Func(("@%#05x u16=%#06x (%#010x) %s\n", offReg, *(uint16_t *)pv, *(uint32_t *)pv, g_aHdaRegMap[idxRegDsc].abbrev));
4591 else if (cb == 1)
4592 Log3Func(("@%#05x u8=%#04x (%#010x) %s\n", offReg, *(uint8_t *)pv, *(uint32_t *)pv, g_aHdaRegMap[idxRegDsc].abbrev));
4593
4594 if (idxRegDsc >= 0 && g_aHdaRegMap[idxRegDsc].size != cb)
4595 Log3Func(("\tsize=%RU32 != cb=%u!!\n", g_aHdaRegMap[idxRegDsc].size, cb));
4596#endif
4597
4598 /*
4599 * Try for a direct hit first.
4600 */
4601 if (idxRegDsc != -1 && g_aHdaRegMap[idxRegDsc].size == cb)
4602 {
4603 rc = hdaWriteReg(pThis, idxRegDsc, u64Value, "");
4604 Log3Func(("\t%#x -> %#x\n", u32LogOldValue, idxRegMem != UINT32_MAX ? pThis->au32Regs[idxRegMem] : UINT32_MAX));
4605 }
4606 /*
4607 * Partial or multiple register access, loop thru the requested memory.
4608 */
4609 else
4610 {
4611 /*
4612 * If it's an access beyond the start of the register, shift the input
4613 * value and fill in missing bits. Natural alignment rules means we
4614 * will only see 1 or 2 byte accesses of this kind, so no risk of
4615 * shifting out input values.
4616 */
4617 if (idxRegDsc == -1 && (idxRegDsc = hdaRegLookupWithin(pThis, offReg)) != -1)
4618 {
4619 uint32_t const cbBefore = offReg - g_aHdaRegMap[idxRegDsc].offset; Assert(cbBefore > 0 && cbBefore < 4);
4620 offReg -= cbBefore;
4621 idxRegMem = g_aHdaRegMap[idxRegDsc].mem_idx;
4622 u64Value <<= cbBefore * 8;
4623 u64Value |= pThis->au32Regs[idxRegMem] & g_afMasks[cbBefore];
4624 Log3Func(("\tWithin register, supplied %u leading bits: %#llx -> %#llx ...\n",
4625 cbBefore * 8, ~g_afMasks[cbBefore] & u64Value, u64Value));
4626 }
4627
4628 /* Loop thru the write area, it may cover multiple registers. */
4629 rc = VINF_SUCCESS;
4630 for (;;)
4631 {
4632 uint32_t cbReg;
4633 if (idxRegDsc != -1)
4634 {
4635 idxRegMem = g_aHdaRegMap[idxRegDsc].mem_idx;
4636 cbReg = g_aHdaRegMap[idxRegDsc].size;
4637 if (cb < cbReg)
4638 {
4639 u64Value |= pThis->au32Regs[idxRegMem] & g_afMasks[cbReg] & ~g_afMasks[cb];
4640 Log3Func(("\tSupplying missing bits (%#x): %#llx -> %#llx ...\n",
4641 g_afMasks[cbReg] & ~g_afMasks[cb], u64Value & g_afMasks[cb], u64Value));
4642 }
4643 uint32_t u32LogOldVal = pThis->au32Regs[idxRegMem];
4644 rc = hdaWriteReg(pThis, idxRegDsc, u64Value, "*");
4645 Log3Func(("\t%#x -> %#x\n", u32LogOldVal, pThis->au32Regs[idxRegMem]));
4646 }
4647 else
4648 {
4649 LogRel(("HDA: Invalid write access @0x%x\n", offReg));
4650 cbReg = 1;
4651 }
4652 if (rc != VINF_SUCCESS)
4653 break;
4654 if (cbReg >= cb)
4655 break;
4656
4657 /* Advance. */
4658 offReg += cbReg;
4659 cb -= cbReg;
4660 u64Value >>= cbReg * 8;
4661 if (idxRegDsc == -1)
4662 idxRegDsc = hdaRegLookup(pThis, offReg);
4663 else
4664 {
4665 idxRegDsc++;
4666 if ( (unsigned)idxRegDsc >= RT_ELEMENTS(g_aHdaRegMap)
4667 || g_aHdaRegMap[idxRegDsc].offset != offReg)
4668 {
4669 idxRegDsc = -1;
4670 }
4671 }
4672 }
4673 }
4674
4675 return rc;
4676}
4677
4678
4679/* PCI callback. */
4680
4681#ifdef IN_RING3
4682/**
4683 * @callback_method_impl{FNPCIIOREGIONMAP}
4684 */
4685static DECLCALLBACK(int) hdaPciIoRegionMap(PPCIDEVICE pPciDev, int iRegion, RTGCPHYS GCPhysAddress, uint32_t cb,
4686 PCIADDRESSSPACE enmType)
4687{
4688 PPDMDEVINS pDevIns = pPciDev->pDevIns;
4689 PHDASTATE pThis = RT_FROM_MEMBER(pPciDev, HDASTATE, PciDev);
4690 RTIOPORT Port = (RTIOPORT)GCPhysAddress;
4691 int rc;
4692
4693 /*
4694 * 18.2 of the ICH6 datasheet defines the valid access widths as byte, word, and double word.
4695 *
4696 * Let IOM talk DWORDs when reading, saves a lot of complications. On
4697 * writing though, we have to do it all ourselves because of sideeffects.
4698 */
4699 Assert(enmType == PCI_ADDRESS_SPACE_MEM);
4700 rc = PDMDevHlpMMIORegister(pDevIns, GCPhysAddress, cb, NULL /*pvUser*/,
4701 IOMMMIO_FLAGS_READ_DWORD
4702 | IOMMMIO_FLAGS_WRITE_PASSTHRU,
4703 hdaMMIOWrite, hdaMMIORead, "HDA");
4704
4705 if (RT_FAILURE(rc))
4706 return rc;
4707
4708 if (pThis->fR0Enabled)
4709 {
4710 rc = PDMDevHlpMMIORegisterR0(pDevIns, GCPhysAddress, cb, NIL_RTR0PTR /*pvUser*/,
4711 "hdaMMIOWrite", "hdaMMIORead");
4712 if (RT_FAILURE(rc))
4713 return rc;
4714 }
4715
4716 if (pThis->fRCEnabled)
4717 {
4718 rc = PDMDevHlpMMIORegisterRC(pDevIns, GCPhysAddress, cb, NIL_RTRCPTR /*pvUser*/,
4719 "hdaMMIOWrite", "hdaMMIORead");
4720 if (RT_FAILURE(rc))
4721 return rc;
4722 }
4723
4724 pThis->MMIOBaseAddr = GCPhysAddress;
4725 return VINF_SUCCESS;
4726}
4727
4728
4729/* Saved state callbacks. */
4730
4731static int hdaSaveStream(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, PHDASTREAM pStrm)
4732{
4733 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4734
4735 LogFlowFunc(("[SD%RU8]\n", pStrm->u8SD));
4736
4737 /* Save stream ID. */
4738 int rc = SSMR3PutU8(pSSM, pStrm->u8SD);
4739 AssertRCReturn(rc, rc);
4740 Assert(pStrm->u8SD <= HDA_MAX_STREAMS);
4741
4742 rc = SSMR3PutStructEx(pSSM, &pStrm->State, sizeof(HDASTREAMSTATE), 0 /*fFlags*/, g_aSSMStreamStateFields6, NULL);
4743 AssertRCReturn(rc, rc);
4744
4745#ifdef DEBUG /* Sanity checks. */
4746 uint64_t u64BaseDMA = RT_MAKE_U64(HDA_STREAM_REG(pThis, BDPL, pStrm->u8SD),
4747 HDA_STREAM_REG(pThis, BDPU, pStrm->u8SD));
4748 uint16_t u16LVI = HDA_STREAM_REG(pThis, LVI, pStrm->u8SD);
4749 uint32_t u32CBL = HDA_STREAM_REG(pThis, CBL, pStrm->u8SD);
4750
4751 hdaBDLEDumpAll(pThis, u64BaseDMA, u16LVI + 1);
4752
4753 Assert(u64BaseDMA == pStrm->u64BDLBase);
4754 Assert(u16LVI == pStrm->u16LVI);
4755 Assert(u32CBL == pStrm->u32CBL);
4756#endif
4757
4758 rc = SSMR3PutStructEx(pSSM, &pStrm->State.BDLE, sizeof(HDABDLE),
4759 0 /*fFlags*/, g_aSSMBDLEFields6, NULL);
4760 AssertRCReturn(rc, rc);
4761
4762 rc = SSMR3PutStructEx(pSSM, &pStrm->State.BDLE.State, sizeof(HDABDLESTATE),
4763 0 /*fFlags*/, g_aSSMBDLEStateFields6, NULL);
4764 AssertRCReturn(rc, rc);
4765
4766#ifdef DEBUG /* Sanity checks. */
4767 PHDABDLE pBDLE = &pStrm->State.BDLE;
4768 if (u64BaseDMA)
4769 {
4770 Assert(pStrm->State.uCurBDLE <= u16LVI + 1);
4771
4772 HDABDLE curBDLE;
4773 rc = hdaBDLEFetch(pThis, &curBDLE, u64BaseDMA, pStrm->State.uCurBDLE);
4774 AssertRC(rc);
4775
4776 Assert(curBDLE.u32BufSize == pBDLE->u32BufSize);
4777 Assert(curBDLE.u64BufAdr == pBDLE->u64BufAdr);
4778 Assert(curBDLE.fIntOnCompletion == pBDLE->fIntOnCompletion);
4779 }
4780 else
4781 {
4782 Assert(pBDLE->u64BufAdr == 0);
4783 Assert(pBDLE->u32BufSize == 0);
4784 }
4785#endif
4786 return rc;
4787}
4788
4789/**
4790 * @callback_method_impl{FNSSMDEVSAVEEXEC}
4791 */
4792static DECLCALLBACK(int) hdaSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
4793{
4794 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4795
4796 /* Save Codec nodes states. */
4797 hdaCodecSaveState(pThis->pCodec, pSSM);
4798
4799 /* Save MMIO registers. */
4800 SSMR3PutU32(pSSM, RT_ELEMENTS(pThis->au32Regs));
4801 SSMR3PutMem(pSSM, pThis->au32Regs, sizeof(pThis->au32Regs));
4802
4803 /* Save number of streams. */
4804 SSMR3PutU32(pSSM, HDA_MAX_STREAMS);
4805
4806 /* Save stream states. */
4807 int rc;
4808 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
4809 {
4810 rc = hdaSaveStream(pDevIns, pSSM, &pThis->aStreams[i]);
4811 AssertRCReturn(rc, rc);
4812 }
4813
4814 return rc;
4815}
4816
4817
4818/**
4819 * @callback_method_impl{FNSSMDEVLOADEXEC}
4820 */
4821static DECLCALLBACK(int) hdaLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
4822{
4823 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4824
4825 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
4826
4827 LogRel2(("hdaLoadExec: uVersion=%RU32, uPass=0x%x\n", uVersion, uPass));
4828
4829 /*
4830 * Load Codec nodes states.
4831 */
4832 int rc = hdaCodecLoadState(pThis->pCodec, pSSM, uVersion);
4833 if (RT_FAILURE(rc))
4834 {
4835 LogRel(("HDA: Failed loading codec state (version %RU32, pass 0x%x), rc=%Rrc\n", uVersion, uPass, rc));
4836 return rc;
4837 }
4838
4839 /*
4840 * Load MMIO registers.
4841 */
4842 uint32_t cRegs;
4843 switch (uVersion)
4844 {
4845 case HDA_SSM_VERSION_1:
4846 /* Starting with r71199, we would save 112 instead of 113
4847 registers due to some code cleanups. This only affected trunk
4848 builds in the 4.1 development period. */
4849 cRegs = 113;
4850 if (SSMR3HandleRevision(pSSM) >= 71199)
4851 {
4852 uint32_t uVer = SSMR3HandleVersion(pSSM);
4853 if ( VBOX_FULL_VERSION_GET_MAJOR(uVer) == 4
4854 && VBOX_FULL_VERSION_GET_MINOR(uVer) == 0
4855 && VBOX_FULL_VERSION_GET_BUILD(uVer) >= 51)
4856 cRegs = 112;
4857 }
4858 break;
4859
4860 case HDA_SSM_VERSION_2:
4861 case HDA_SSM_VERSION_3:
4862 cRegs = 112;
4863 AssertCompile(RT_ELEMENTS(pThis->au32Regs) >= 112);
4864 break;
4865
4866 /* Since version 4 we store the register count to stay flexible. */
4867 case HDA_SSM_VERSION_4:
4868 case HDA_SSM_VERSION_5:
4869 case HDA_SSM_VERSION:
4870 rc = SSMR3GetU32(pSSM, &cRegs); AssertRCReturn(rc, rc);
4871 if (cRegs != RT_ELEMENTS(pThis->au32Regs))
4872 LogRel(("HDA: SSM version cRegs is %RU32, expected %RU32\n", cRegs, RT_ELEMENTS(pThis->au32Regs)));
4873 break;
4874
4875 default:
4876 LogRel(("HDA: Unsupported / too new saved state version (%RU32)\n", uVersion));
4877 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
4878 }
4879
4880 if (cRegs >= RT_ELEMENTS(pThis->au32Regs))
4881 {
4882 SSMR3GetMem(pSSM, pThis->au32Regs, sizeof(pThis->au32Regs));
4883 SSMR3Skip(pSSM, sizeof(uint32_t) * (cRegs - RT_ELEMENTS(pThis->au32Regs)));
4884 }
4885 else
4886 SSMR3GetMem(pSSM, pThis->au32Regs, sizeof(uint32_t) * cRegs);
4887
4888 /*
4889 * Note: Saved states < v5 store LVI (u32BdleMaxCvi) for
4890 * *every* BDLE state, whereas it only needs to be stored
4891 * *once* for every stream. Most of the BDLE state we can
4892 * get out of the registers anyway, so just ignore those values.
4893 *
4894 * Also, only the current BDLE was saved, regardless whether
4895 * there were more than one (and there are at least two entries,
4896 * according to the spec).
4897 */
4898#define HDA_SSM_LOAD_BDLE_STATE_PRE_V5(v, x) \
4899 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Begin marker */ \
4900 AssertRCReturn(rc, rc); \
4901 rc = SSMR3GetU64(pSSM, &x.u64BufAdr); /* u64BdleCviAddr */ \
4902 AssertRCReturn(rc, rc); \
4903 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* u32BdleMaxCvi */ \
4904 AssertRCReturn(rc, rc); \
4905 rc = SSMR3GetU32(pSSM, &x.State.u32BDLIndex); /* u32BdleCvi */ \
4906 AssertRCReturn(rc, rc); \
4907 rc = SSMR3GetU32(pSSM, &x.u32BufSize); /* u32BdleCviLen */ \
4908 AssertRCReturn(rc, rc); \
4909 rc = SSMR3GetU32(pSSM, &x.State.u32BufOff); /* u32BdleCviPos */ \
4910 AssertRCReturn(rc, rc); \
4911 rc = SSMR3GetBool(pSSM, &x.fIntOnCompletion); /* fBdleCviIoc */ \
4912 AssertRCReturn(rc, rc); \
4913 rc = SSMR3GetU32(pSSM, &x.State.cbBelowFIFOW); /* cbUnderFifoW */ \
4914 AssertRCReturn(rc, rc); \
4915 rc = SSMR3GetMem(pSSM, &x.State.au8FIFO, sizeof(x.State.au8FIFO)); \
4916 AssertRCReturn(rc, rc); \
4917 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* End marker */ \
4918 AssertRCReturn(rc, rc); \
4919
4920 /*
4921 * Load BDLEs (Buffer Descriptor List Entries) and DMA counters.
4922 */
4923 switch (uVersion)
4924 {
4925 case HDA_SSM_VERSION_1:
4926 case HDA_SSM_VERSION_2:
4927 case HDA_SSM_VERSION_3:
4928 case HDA_SSM_VERSION_4:
4929 {
4930 /* Only load the internal states.
4931 * The rest will be initialized from the saved registers later. */
4932
4933 /* Note 1: Only the *current* BDLE for a stream was saved! */
4934 /* Note 2: The stream's saving order is/was fixed, so don't touch! */
4935
4936 /* Output */
4937 PHDASTREAM pStream = &pThis->aStreams[4];
4938 rc = hdaStreamInit(pThis, pStream, 4 /* Stream descriptor, hardcoded */);
4939 if (RT_FAILURE(rc))
4940 break;
4941 HDA_SSM_LOAD_BDLE_STATE_PRE_V5(uVersion, pStream->State.BDLE);
4942 pStream->State.uCurBDLE = pStream->State.BDLE.State.u32BDLIndex;
4943
4944 /* Microphone-In */
4945 pStream = &pThis->aStreams[2];
4946 rc = hdaStreamInit(pThis, pStream, 2 /* Stream descriptor, hardcoded */);
4947 if (RT_FAILURE(rc))
4948 break;
4949 HDA_SSM_LOAD_BDLE_STATE_PRE_V5(uVersion, pStream->State.BDLE);
4950 pStream->State.uCurBDLE = pStream->State.BDLE.State.u32BDLIndex;
4951
4952 /* Line-In */
4953 pStream = &pThis->aStreams[0];
4954 rc = hdaStreamInit(pThis, pStream, 0 /* Stream descriptor, hardcoded */);
4955 if (RT_FAILURE(rc))
4956 break;
4957 HDA_SSM_LOAD_BDLE_STATE_PRE_V5(uVersion, pStream->State.BDLE);
4958 pStream->State.uCurBDLE = pStream->State.BDLE.State.u32BDLIndex;
4959 break;
4960 }
4961
4962 /* Since v5 we support flexible stream and BDLE counts. */
4963 case HDA_SSM_VERSION_5:
4964 case HDA_SSM_VERSION:
4965 {
4966 uint32_t cStreams;
4967 rc = SSMR3GetU32(pSSM, &cStreams);
4968 if (RT_FAILURE(rc))
4969 break;
4970
4971 LogRel2(("hdaLoadExec: cStreams=%RU32\n", cStreams));
4972
4973 /* Load stream states. */
4974 for (uint32_t i = 0; i < cStreams; i++)
4975 {
4976 uint8_t uSD;
4977 rc = SSMR3GetU8(pSSM, &uSD);
4978 if (RT_FAILURE(rc))
4979 break;
4980
4981 PHDASTREAM pStrm = hdaStreamFromSD(pThis, uSD);
4982 HDASTREAM StreamDummy;
4983
4984 if (!pStrm)
4985 {
4986 RT_ZERO(StreamDummy);
4987 pStrm = &StreamDummy;
4988 LogRel2(("HDA: Warning: Stream ID=%RU32 not supported, skipping to load ...\n", uSD));
4989 break;
4990 }
4991
4992 rc = hdaStreamInit(pThis, pStrm, uSD);
4993 if (RT_FAILURE(rc))
4994 {
4995 LogRel(("HDA: Stream #%RU32: Initialization of stream %RU8 failed, rc=%Rrc\n", i, uSD, rc));
4996 break;
4997 }
4998
4999 if (uVersion == HDA_SSM_VERSION_5)
5000 {
5001 /* Get the current BDLE entry and skip the rest. */
5002 uint16_t cBDLE;
5003
5004 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Begin marker */
5005 AssertRC(rc);
5006 rc = SSMR3GetU16(pSSM, &cBDLE); /* cBDLE */
5007 AssertRC(rc);
5008 rc = SSMR3GetU16(pSSM, &pStrm->State.uCurBDLE); /* uCurBDLE */
5009 AssertRC(rc);
5010 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* End marker */
5011 AssertRC(rc);
5012
5013 uint32_t u32BDLEIndex;
5014 for (uint16_t a = 0; a < cBDLE; a++)
5015 {
5016 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Begin marker */
5017 AssertRC(rc);
5018 rc = SSMR3GetU32(pSSM, &u32BDLEIndex); /* u32BDLIndex */
5019 AssertRC(rc);
5020
5021 /* Does the current BDLE index match the current BDLE to process? */
5022 if (u32BDLEIndex == pStrm->State.uCurBDLE)
5023 {
5024 rc = SSMR3GetU32(pSSM, &pStrm->State.BDLE.State.cbBelowFIFOW); /* cbBelowFIFOW */
5025 AssertRC(rc);
5026 rc = SSMR3GetMem(pSSM,
5027 &pStrm->State.BDLE.State.au8FIFO,
5028 sizeof(pStrm->State.BDLE.State.au8FIFO)); /* au8FIFO */
5029 AssertRC(rc);
5030 rc = SSMR3GetU32(pSSM, &pStrm->State.BDLE.State.u32BufOff); /* u32BufOff */
5031 AssertRC(rc);
5032 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* End marker */
5033 AssertRC(rc);
5034 }
5035 else /* Skip not current BDLEs. */
5036 {
5037 rc = SSMR3Skip(pSSM, sizeof(uint32_t) /* cbBelowFIFOW */
5038 + sizeof(uint8_t) * 256 /* au8FIFO */
5039 + sizeof(uint32_t) /* u32BufOff */
5040 + sizeof(uint32_t)); /* End marker */
5041 AssertRC(rc);
5042 }
5043 }
5044 }
5045 else
5046 {
5047 rc = SSMR3GetStructEx(pSSM, &pStrm->State, sizeof(HDASTREAMSTATE),
5048 0 /* fFlags */, g_aSSMStreamStateFields6, NULL);
5049 if (RT_FAILURE(rc))
5050 break;
5051
5052 rc = SSMR3GetStructEx(pSSM, &pStrm->State.BDLE, sizeof(HDABDLE),
5053 0 /* fFlags */, g_aSSMBDLEFields6, NULL);
5054 if (RT_FAILURE(rc))
5055 break;
5056
5057 rc = SSMR3GetStructEx(pSSM, &pStrm->State.BDLE.State, sizeof(HDABDLESTATE),
5058 0 /* fFlags */, g_aSSMBDLEStateFields6, NULL);
5059 if (RT_FAILURE(rc))
5060 break;
5061 }
5062 }
5063 break;
5064 }
5065
5066 default:
5067 AssertReleaseFailed(); /* Never reached. */
5068 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
5069 }
5070
5071#undef HDA_SSM_LOAD_BDLE_STATE_PRE_V5
5072
5073 if (RT_SUCCESS(rc))
5074 {
5075 pThis->u64CORBBase = RT_MAKE_U64(HDA_REG(pThis, CORBLBASE), HDA_REG(pThis, CORBUBASE));
5076 pThis->u64RIRBBase = RT_MAKE_U64(HDA_REG(pThis, RIRBLBASE), HDA_REG(pThis, RIRBUBASE));
5077 pThis->u64DPBase = RT_MAKE_U64(HDA_REG(pThis, DPLBASE), HDA_REG(pThis, DPUBASE));
5078
5079 /* Also make sure to update the DMA position bit if this was enabled when saving the state. */
5080 pThis->fDMAPosition = RT_BOOL(pThis->u64DPBase & RT_BIT_64(0));
5081 }
5082
5083 if (RT_SUCCESS(rc))
5084 {
5085 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
5086 {
5087 PHDASTREAM pStream = hdaStreamFromSD(pThis, i);
5088 if (pStream)
5089 {
5090 /* Deactive first. */
5091 int rc2 = hdaStreamSetActive(pThis, pStream, false);
5092 AssertRC(rc2);
5093
5094 bool fActive = RT_BOOL(HDA_STREAM_REG(pThis, CTL, i) & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN));
5095
5096 /* Activate, if needed. */
5097 rc2 = hdaStreamSetActive(pThis, pStream, fActive);
5098 AssertRC(rc2);
5099 }
5100 }
5101 }
5102
5103 if (RT_FAILURE(rc))
5104 LogRel(("HDA: Failed loading device state (version %RU32, pass 0x%x), rc=%Rrc\n", uVersion, uPass, rc));
5105
5106 LogFlowFuncLeaveRC(rc);
5107 return rc;
5108}
5109
5110#ifdef DEBUG
5111/* Debug and log type formatters. */
5112
5113/**
5114 * @callback_method_impl{FNRTSTRFORMATTYPE}
5115 */
5116static DECLCALLBACK(size_t) hdaDbgFmtBDLE(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
5117 const char *pszType, void const *pvValue,
5118 int cchWidth, int cchPrecision, unsigned fFlags,
5119 void *pvUser)
5120{
5121 PHDABDLE pBDLE = (PHDABDLE)pvValue;
5122 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
5123 "BDLE(idx:%RU32, off:%RU32, fifow:%RU32, IOC:%RTbool, DMA[%RU32 bytes @ 0x%x])",
5124 pBDLE->State.u32BDLIndex, pBDLE->State.u32BufOff, pBDLE->State.cbBelowFIFOW, pBDLE->fIntOnCompletion,
5125 pBDLE->u32BufSize, pBDLE->u64BufAdr);
5126}
5127
5128/**
5129 * @callback_method_impl{FNRTSTRFORMATTYPE}
5130 */
5131static DECLCALLBACK(size_t) hdaDbgFmtSDCTL(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
5132 const char *pszType, void const *pvValue,
5133 int cchWidth, int cchPrecision, unsigned fFlags,
5134 void *pvUser)
5135{
5136 uint32_t uSDCTL = (uint32_t)(uintptr_t)pvValue;
5137 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
5138 "SDCTL(raw:%#x, DIR:%s, TP:%RTbool, STRIPE:%x, DEIE:%RTbool, FEIE:%RTbool, IOCE:%RTbool, RUN:%RTbool, RESET:%RTbool)",
5139 uSDCTL,
5140 (uSDCTL & HDA_REG_FIELD_FLAG_MASK(SDCTL, DIR)) ? "OUT" : "IN",
5141 RT_BOOL(uSDCTL & HDA_REG_FIELD_FLAG_MASK(SDCTL, TP)),
5142 (uSDCTL & HDA_REG_FIELD_MASK(SDCTL, STRIPE)) >> HDA_SDCTL_STRIPE_SHIFT,
5143 RT_BOOL(uSDCTL & HDA_REG_FIELD_FLAG_MASK(SDCTL, DEIE)),
5144 RT_BOOL(uSDCTL & HDA_REG_FIELD_FLAG_MASK(SDCTL, FEIE)),
5145 RT_BOOL(uSDCTL & HDA_REG_FIELD_FLAG_MASK(SDCTL, ICE)),
5146 RT_BOOL(uSDCTL & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN)),
5147 RT_BOOL(uSDCTL & HDA_REG_FIELD_FLAG_MASK(SDCTL, SRST)));
5148}
5149
5150/**
5151 * @callback_method_impl{FNRTSTRFORMATTYPE}
5152 */
5153static DECLCALLBACK(size_t) hdaDbgFmtSDFIFOS(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
5154 const char *pszType, void const *pvValue,
5155 int cchWidth, int cchPrecision, unsigned fFlags,
5156 void *pvUser)
5157{
5158 uint32_t uSDFIFOS = (uint32_t)(uintptr_t)pvValue;
5159 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "SDFIFOS(raw:%#x, sdfifos:%RU8 B)", uSDFIFOS, hdaSDFIFOSToBytes(uSDFIFOS));
5160}
5161
5162/**
5163 * @callback_method_impl{FNRTSTRFORMATTYPE}
5164 */
5165static DECLCALLBACK(size_t) hdaDbgFmtSDFIFOW(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
5166 const char *pszType, void const *pvValue,
5167 int cchWidth, int cchPrecision, unsigned fFlags,
5168 void *pvUser)
5169{
5170 uint32_t uSDFIFOW = (uint32_t)(uintptr_t)pvValue;
5171 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "SDFIFOW(raw: %#0x, sdfifow:%d B)", uSDFIFOW, hdaSDFIFOWToBytes(uSDFIFOW));
5172}
5173
5174/**
5175 * @callback_method_impl{FNRTSTRFORMATTYPE}
5176 */
5177static DECLCALLBACK(size_t) hdaDbgFmtSDSTS(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
5178 const char *pszType, void const *pvValue,
5179 int cchWidth, int cchPrecision, unsigned fFlags,
5180 void *pvUser)
5181{
5182 uint32_t uSdSts = (uint32_t)(uintptr_t)pvValue;
5183 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
5184 "SDSTS(raw:%#0x, fifordy:%RTbool, dese:%RTbool, fifoe:%RTbool, bcis:%RTbool)",
5185 uSdSts,
5186 RT_BOOL(uSdSts & HDA_REG_FIELD_FLAG_MASK(SDSTS, FIFORDY)),
5187 RT_BOOL(uSdSts & HDA_REG_FIELD_FLAG_MASK(SDSTS, DE)),
5188 RT_BOOL(uSdSts & HDA_REG_FIELD_FLAG_MASK(SDSTS, FE)),
5189 RT_BOOL(uSdSts & HDA_REG_FIELD_FLAG_MASK(SDSTS, BCIS)));
5190}
5191
5192static int hdaDbgLookupRegByName(PHDASTATE pThis, const char *pszArgs)
5193{
5194 int iReg = 0;
5195 for (; iReg < HDA_NUM_REGS; ++iReg)
5196 if (!RTStrICmp(g_aHdaRegMap[iReg].abbrev, pszArgs))
5197 return iReg;
5198 return -1;
5199}
5200
5201
5202static void hdaDbgPrintRegister(PHDASTATE pThis, PCDBGFINFOHLP pHlp, int iHdaIndex)
5203{
5204 Assert( pThis
5205 && iHdaIndex >= 0
5206 && iHdaIndex < HDA_NUM_REGS);
5207 pHlp->pfnPrintf(pHlp, "%s: 0x%x\n", g_aHdaRegMap[iHdaIndex].abbrev, pThis->au32Regs[g_aHdaRegMap[iHdaIndex].mem_idx]);
5208}
5209
5210/**
5211 * @callback_method_impl{FNDBGFHANDLERDEV}
5212 */
5213static DECLCALLBACK(void) hdaDbgInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5214{
5215 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5216 int iHdaRegisterIndex = hdaDbgLookupRegByName(pThis, pszArgs);
5217 if (iHdaRegisterIndex != -1)
5218 hdaDbgPrintRegister(pThis, pHlp, iHdaRegisterIndex);
5219 else
5220 {
5221 for(iHdaRegisterIndex = 0; (unsigned int)iHdaRegisterIndex < HDA_NUM_REGS; ++iHdaRegisterIndex)
5222 hdaDbgPrintRegister(pThis, pHlp, iHdaRegisterIndex);
5223 }
5224}
5225
5226static void hdaDbgPrintStream(PHDASTATE pThis, PCDBGFINFOHLP pHlp, int iIdx)
5227{
5228 Assert( pThis
5229 && iIdx >= 0
5230 && iIdx < HDA_MAX_STREAMS);
5231
5232 const PHDASTREAM pStrm = &pThis->aStreams[iIdx];
5233
5234 pHlp->pfnPrintf(pHlp, "Stream #%d:\n", iIdx);
5235 pHlp->pfnPrintf(pHlp, "\tSD%dCTL : %R[sdctl]\n", iIdx, HDA_STREAM_REG(pThis, CTL, iIdx));
5236 pHlp->pfnPrintf(pHlp, "\tSD%dCTS : %R[sdsts]\n", iIdx, HDA_STREAM_REG(pThis, STS, iIdx));
5237 pHlp->pfnPrintf(pHlp, "\tSD%dFIFOS: %R[sdfifos]\n", iIdx, HDA_STREAM_REG(pThis, FIFOS, iIdx));
5238 pHlp->pfnPrintf(pHlp, "\tSD%dFIFOW: %R[sdfifow]\n", iIdx, HDA_STREAM_REG(pThis, FIFOW, iIdx));
5239 pHlp->pfnPrintf(pHlp, "\tBDLE : %R[bdle]\n", &pStrm->State.BDLE);
5240}
5241
5242static void hdaDbgPrintBDLE(PHDASTATE pThis, PCDBGFINFOHLP pHlp, int iIdx)
5243{
5244 Assert( pThis
5245 && iIdx >= 0
5246 && iIdx < HDA_MAX_STREAMS);
5247
5248 const PHDASTREAM pStrm = &pThis->aStreams[iIdx];
5249 const PHDABDLE pBDLE = &pStrm->State.BDLE;
5250
5251 pHlp->pfnPrintf(pHlp, "Stream #%d BDLE:\n", iIdx);
5252 pHlp->pfnPrintf(pHlp, "\t%R[bdle]\n", pBDLE);
5253
5254 uint64_t u64BaseDMA = RT_MAKE_U64(HDA_STREAM_REG(pThis, BDPL, iIdx),
5255 HDA_STREAM_REG(pThis, BDPU, iIdx));
5256 uint16_t u16LVI = HDA_STREAM_REG(pThis, LVI, iIdx);
5257 uint32_t u32CBL = HDA_STREAM_REG(pThis, CBL, iIdx);
5258
5259 if (!u64BaseDMA)
5260 return;
5261
5262 uint32_t cbBDLE = 0;
5263 for (uint16_t i = 0; i < u16LVI + 1; i++)
5264 {
5265 uint8_t bdle[16]; /** @todo Use a define. */
5266 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), u64BaseDMA + i * 16, bdle, 16); /** @todo Use a define. */
5267
5268 uint64_t addr = *(uint64_t *)bdle;
5269 uint32_t len = *(uint32_t *)&bdle[8];
5270 uint32_t ioc = *(uint32_t *)&bdle[12];
5271
5272 pHlp->pfnPrintf(pHlp, "\t#%03d BDLE(adr:0x%llx, size:%RU32, ioc:%RTbool)\n",
5273 i, addr, len, RT_BOOL(ioc & 0x1));
5274
5275 cbBDLE += len;
5276 }
5277
5278 pHlp->pfnPrintf(pHlp, "Total: %RU32 bytes\n", cbBDLE);
5279
5280 pHlp->pfnPrintf(pHlp, "DMA counters (base @ 0x%llx):\n", pThis->u64DPBase);
5281 if (!pThis->u64DPBase) /* No DMA base given? Bail out. */
5282 {
5283 pHlp->pfnPrintf(pHlp, "No counters found\n");
5284 return;
5285 }
5286
5287 for (int i = 0; i < u16LVI + 1; i++)
5288 {
5289 uint32_t uDMACnt;
5290 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), (pThis->u64DPBase & DPBASE_ADDR_MASK) + (i * 2 * sizeof(uint32_t)),
5291 &uDMACnt, sizeof(uDMACnt));
5292
5293 pHlp->pfnPrintf(pHlp, "\t#%03d DMA @ 0x%x\n", i , uDMACnt);
5294 }
5295}
5296
5297static int hdaDbgLookupStrmIdx(PHDASTATE pThis, const char *pszArgs)
5298{
5299 /** @todo Add args parsing. */
5300 return -1;
5301}
5302
5303/**
5304 * @callback_method_impl{FNDBGFHANDLERDEV}
5305 */
5306static DECLCALLBACK(void) hdaDbgInfoStream(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5307{
5308 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5309 int iHdaStreamdex = hdaDbgLookupStrmIdx(pThis, pszArgs);
5310 if (iHdaStreamdex != -1)
5311 hdaDbgPrintStream(pThis, pHlp, iHdaStreamdex);
5312 else
5313 for(iHdaStreamdex = 0; iHdaStreamdex < HDA_MAX_STREAMS; ++iHdaStreamdex)
5314 hdaDbgPrintStream(pThis, pHlp, iHdaStreamdex);
5315}
5316
5317/**
5318 * @callback_method_impl{FNDBGFHANDLERDEV}
5319 */
5320static DECLCALLBACK(void) hdaDbgInfoBDLE(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5321{
5322 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5323 int iHdaStreamdex = hdaDbgLookupStrmIdx(pThis, pszArgs);
5324 if (iHdaStreamdex != -1)
5325 hdaDbgPrintBDLE(pThis, pHlp, iHdaStreamdex);
5326 else
5327 for(iHdaStreamdex = 0; iHdaStreamdex < HDA_MAX_STREAMS; ++iHdaStreamdex)
5328 hdaDbgPrintBDLE(pThis, pHlp, iHdaStreamdex);
5329}
5330
5331/**
5332 * @callback_method_impl{FNDBGFHANDLERDEV}
5333 */
5334static DECLCALLBACK(void) hdaDbgInfoCodecNodes(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5335{
5336 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5337
5338 if (pThis->pCodec->pfnDbgListNodes)
5339 pThis->pCodec->pfnDbgListNodes(pThis->pCodec, pHlp, pszArgs);
5340 else
5341 pHlp->pfnPrintf(pHlp, "Codec implementation doesn't provide corresponding callback\n");
5342}
5343
5344/**
5345 * @callback_method_impl{FNDBGFHANDLERDEV}
5346 */
5347static DECLCALLBACK(void) hdaDbgInfoCodecSelector(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5348{
5349 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5350
5351 if (pThis->pCodec->pfnDbgSelector)
5352 pThis->pCodec->pfnDbgSelector(pThis->pCodec, pHlp, pszArgs);
5353 else
5354 pHlp->pfnPrintf(pHlp, "Codec implementation doesn't provide corresponding callback\n");
5355}
5356
5357/**
5358 * @callback_method_impl{FNDBGFHANDLERDEV}
5359 */
5360static DECLCALLBACK(void) hdaDbgInfoMixer(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5361{
5362 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5363
5364 if (pThis->pMixer)
5365 AudioMixerDebug(pThis->pMixer, pHlp, pszArgs);
5366 else
5367 pHlp->pfnPrintf(pHlp, "Mixer not available\n");
5368}
5369#endif /* DEBUG */
5370
5371/* PDMIBASE */
5372
5373/**
5374 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
5375 */
5376static DECLCALLBACK(void *) hdaQueryInterface(struct PDMIBASE *pInterface, const char *pszIID)
5377{
5378 PHDASTATE pThis = RT_FROM_MEMBER(pInterface, HDASTATE, IBase);
5379 Assert(&pThis->IBase == pInterface);
5380
5381 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->IBase);
5382 return NULL;
5383}
5384
5385
5386/* PDMDEVREG */
5387
5388/**
5389 * Reset notification.
5390 *
5391 * @returns VBox status code.
5392 * @param pDevIns The device instance data.
5393 *
5394 * @remark The original sources didn't install a reset handler, but it seems to
5395 * make sense to me so we'll do it.
5396 */
5397static DECLCALLBACK(void) hdaReset(PPDMDEVINS pDevIns)
5398{
5399 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5400
5401 LogFlowFuncEnter();
5402
5403# ifndef VBOX_WITH_AUDIO_CALLBACKS
5404 /*
5405 * Stop the timer, if any.
5406 */
5407 hdaTimerMaybeStop(pThis);
5408# endif
5409
5410 /* See 6.2.1. */
5411 HDA_REG(pThis, GCAP) = HDA_MAKE_GCAP(HDA_MAX_SDO /* Ouput streams */,
5412 HDA_MAX_SDI /* Input streams */,
5413 0 /* Bidirectional output streams */,
5414 0 /* Serial data out signals */,
5415 1 /* 64-bit */);
5416 HDA_REG(pThis, VMIN) = 0x00; /* see 6.2.2 */
5417 HDA_REG(pThis, VMAJ) = 0x01; /* see 6.2.3 */
5418 /* Announce the full 60 words output payload. */
5419 HDA_REG(pThis, OUTPAY) = 0x003C; /* see 6.2.4 */
5420 /* Announce the full 29 words input payload. */
5421 HDA_REG(pThis, INPAY) = 0x001D; /* see 6.2.5 */
5422 HDA_REG(pThis, CORBSIZE) = 0x42; /* see 6.2.1 */
5423 HDA_REG(pThis, RIRBSIZE) = 0x42; /* see 6.2.1 */
5424 HDA_REG(pThis, CORBRP) = 0x0;
5425 HDA_REG(pThis, RIRBWP) = 0x0;
5426
5427 /*
5428 * Stop any audio currently playing and/or recording.
5429 */
5430 AudioMixerSinkCtl(pThis->SinkFront.pMixSink, AUDMIXSINKCMD_DISABLE);
5431# ifdef VBOX_WITH_HDA_MIC_IN
5432 AudioMixerSinkCtl(pThis->SinkMicIn.pMixSink, AUDMIXSINKCMD_DISABLE);
5433# endif
5434 AudioMixerSinkCtl(pThis->SinkLineIn.pMixSink, AUDMIXSINKCMD_DISABLE);
5435# ifdef VBOX_WITH_HDA_51_SURROUND
5436 AudioMixerSinkCtl(pThis->SinkCenterLFE.pMixSink, AUDMIXSINKCMD_DISABLE);
5437 AudioMixerSinkCtl(pThis->SinkRear.pMixSink, AUDMIXSINKCMD_DISABLE);
5438# endif
5439
5440 /*
5441 * Set some sensible defaults for which HDA sinks
5442 * are connected to which stream number.
5443 *
5444 * We use SD0 for input and SD4 for output by default.
5445 * These stream numbers can be changed by the guest dynamically lateron.
5446 */
5447#ifdef VBOX_WITH_HDA_MIC_IN
5448 hdaMixerSetStream(pThis, PDMAUDIOMIXERCTL_MIC_IN , 1 /* SD0 */, 0 /* Channel */);
5449#endif
5450 hdaMixerSetStream(pThis, PDMAUDIOMIXERCTL_LINE_IN , 1 /* SD0 */, 0 /* Channel */);
5451
5452 hdaMixerSetStream(pThis, PDMAUDIOMIXERCTL_FRONT , 5 /* SD4 */, 0 /* Channel */);
5453#ifdef VBOX_WITH_HDA_51_SURROUND
5454 hdaMixerSetStream(pThis, PDMAUDIOMIXERCTL_CENTER_LFE, 5 /* SD4 */, 0 /* Channel */);
5455 hdaMixerSetStream(pThis, PDMAUDIOMIXERCTL_REAR , 5 /* SD4 */, 0 /* Channel */);
5456#endif
5457
5458 pThis->cbCorbBuf = 256 * sizeof(uint32_t); /** @todo Use a define here. */
5459
5460 if (pThis->pu32CorbBuf)
5461 RT_BZERO(pThis->pu32CorbBuf, pThis->cbCorbBuf);
5462 else
5463 pThis->pu32CorbBuf = (uint32_t *)RTMemAllocZ(pThis->cbCorbBuf);
5464
5465 pThis->cbRirbBuf = 256 * sizeof(uint64_t); /** @todo Use a define here. */
5466 if (pThis->pu64RirbBuf)
5467 RT_BZERO(pThis->pu64RirbBuf, pThis->cbRirbBuf);
5468 else
5469 pThis->pu64RirbBuf = (uint64_t *)RTMemAllocZ(pThis->cbRirbBuf);
5470
5471 pThis->u64BaseTS = PDMDevHlpTMTimeVirtGetNano(pDevIns);
5472
5473 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
5474 {
5475 /* Remove the RUN bit from SDnCTL in case the stream was in a running state before. */
5476 HDA_STREAM_REG(pThis, CTL, i) &= ~HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN);
5477 hdaStreamReset(pThis, &pThis->aStreams[i]);
5478 }
5479
5480 /* Clear stream tags <-> objects mapping table. */
5481 RT_ZERO(pThis->aTags);
5482
5483 /* Emulation of codec "wake up" (HDA spec 5.5.1 and 6.5). */
5484 HDA_REG(pThis, STATESTS) = 0x1;
5485
5486# ifndef VBOX_WITH_AUDIO_CALLBACKS
5487 hdaTimerMaybeStart(pThis);
5488# endif
5489
5490 LogFlowFuncLeave();
5491 LogRel(("HDA: Reset\n"));
5492}
5493
5494/**
5495 * @interface_method_impl{PDMDEVREG,pfnDestruct}
5496 */
5497static DECLCALLBACK(int) hdaDestruct(PPDMDEVINS pDevIns)
5498{
5499 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5500
5501 PHDADRIVER pDrv;
5502 while (!RTListIsEmpty(&pThis->lstDrv))
5503 {
5504 pDrv = RTListGetFirst(&pThis->lstDrv, HDADRIVER, Node);
5505
5506 RTListNodeRemove(&pDrv->Node);
5507 RTMemFree(pDrv);
5508 }
5509
5510 if (pThis->pCodec)
5511 {
5512 hdaCodecDestruct(pThis->pCodec);
5513
5514 RTMemFree(pThis->pCodec);
5515 pThis->pCodec = NULL;
5516 }
5517
5518 RTMemFree(pThis->pu32CorbBuf);
5519 pThis->pu32CorbBuf = NULL;
5520
5521 RTMemFree(pThis->pu64RirbBuf);
5522 pThis->pu64RirbBuf = NULL;
5523
5524 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
5525 hdaStreamDestroy(&pThis->aStreams[i]);
5526
5527 return VINF_SUCCESS;
5528}
5529
5530
5531/**
5532 * Attach command, internal version.
5533 *
5534 * This is called to let the device attach to a driver for a specified LUN
5535 * during runtime. This is not called during VM construction, the device
5536 * constructor has to attach to all the available drivers.
5537 *
5538 * @returns VBox status code.
5539 * @param pDevIns The device instance.
5540 * @param pDrv Driver to (re-)use for (re-)attaching to.
5541 * If NULL is specified, a new driver will be created and appended
5542 * to the driver list.
5543 * @param uLUN The logical unit which is being detached.
5544 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
5545 */
5546static int hdaAttachInternal(PPDMDEVINS pDevIns, PHDADRIVER pDrv, unsigned uLUN, uint32_t fFlags)
5547{
5548 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5549
5550 /*
5551 * Attach driver.
5552 */
5553 char *pszDesc = NULL;
5554 if (RTStrAPrintf(&pszDesc, "Audio driver port (HDA) for LUN#%u", uLUN) <= 0)
5555 AssertReleaseMsgReturn(pszDesc,
5556 ("Not enough memory for HDA driver port description of LUN #%u\n", uLUN),
5557 VERR_NO_MEMORY);
5558
5559 PPDMIBASE pDrvBase;
5560 int rc = PDMDevHlpDriverAttach(pDevIns, uLUN,
5561 &pThis->IBase, &pDrvBase, pszDesc);
5562 if (RT_SUCCESS(rc))
5563 {
5564 if (pDrv == NULL)
5565 pDrv = (PHDADRIVER)RTMemAllocZ(sizeof(HDADRIVER));
5566 if (pDrv)
5567 {
5568 pDrv->pDrvBase = pDrvBase;
5569 pDrv->pConnector = PDMIBASE_QUERY_INTERFACE(pDrvBase, PDMIAUDIOCONNECTOR);
5570 AssertMsg(pDrv->pConnector != NULL, ("Configuration error: LUN#%u has no host audio interface, rc=%Rrc\n", uLUN, rc));
5571 pDrv->pHDAState = pThis;
5572 pDrv->uLUN = uLUN;
5573
5574 /*
5575 * For now we always set the driver at LUN 0 as our primary
5576 * host backend. This might change in the future.
5577 */
5578 if (pDrv->uLUN == 0)
5579 pDrv->Flags |= PDMAUDIODRVFLAG_PRIMARY;
5580
5581 LogFunc(("LUN#%u: pCon=%p, drvFlags=0x%x\n", uLUN, pDrv->pConnector, pDrv->Flags));
5582
5583 /* Attach to driver list if not attached yet. */
5584 if (!pDrv->fAttached)
5585 {
5586 RTListAppend(&pThis->lstDrv, &pDrv->Node);
5587 pDrv->fAttached = true;
5588 }
5589 }
5590 else
5591 rc = VERR_NO_MEMORY;
5592 }
5593 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
5594 LogFunc(("No attached driver for LUN #%u\n", uLUN));
5595
5596 if (RT_FAILURE(rc))
5597 {
5598 /* Only free this string on failure;
5599 * must remain valid for the live of the driver instance. */
5600 RTStrFree(pszDesc);
5601 }
5602
5603 LogFunc(("uLUN=%u, fFlags=0x%x, rc=%Rrc\n", uLUN, fFlags, rc));
5604 return rc;
5605}
5606
5607/**
5608 * Attach command.
5609 *
5610 * This is called to let the device attach to a driver for a specified LUN
5611 * during runtime. This is not called during VM construction, the device
5612 * constructor has to attach to all the available drivers.
5613 *
5614 * @returns VBox status code.
5615 * @param pDevIns The device instance.
5616 * @param uLUN The logical unit which is being detached.
5617 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
5618 */
5619static DECLCALLBACK(int) hdaAttach(PPDMDEVINS pDevIns, unsigned uLUN, uint32_t fFlags)
5620{
5621 return hdaAttachInternal(pDevIns, NULL /* pDrv */, uLUN, fFlags);
5622}
5623
5624static DECLCALLBACK(void) hdaDetach(PPDMDEVINS pDevIns, unsigned uLUN, uint32_t fFlags)
5625{
5626 LogFunc(("iLUN=%u, fFlags=0x%x\n", uLUN, fFlags));
5627}
5628
5629/**
5630 * Powers off the device.
5631 *
5632 * @param pDevIns Device instance to power off.
5633 */
5634static DECLCALLBACK(void) hdaPowerOff(PPDMDEVINS pDevIns)
5635{
5636 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5637
5638 LogRel2(("HDA: Powering off ...\n"));
5639
5640 /* Ditto goes for the codec, which in turn uses the mixer. */
5641 hdaCodecPowerOff(pThis->pCodec);
5642
5643 /**
5644 * Note: Destroy the mixer while powering off and *not* in hdaDestruct,
5645 * giving the mixer the chance to release any references held to
5646 * PDM audio streams it maintains.
5647 */
5648 if (pThis->pMixer)
5649 {
5650 AudioMixerDestroy(pThis->pMixer);
5651 pThis->pMixer = NULL;
5652 }
5653}
5654
5655/**
5656 * Re-attaches a new driver to the device's driver chain.
5657 *
5658 * @returns VBox status code.
5659 * @param pThis Device instance to re-attach driver to.
5660 * @param pDrv Driver instance used for attaching to.
5661 * If NULL is specified, a new driver will be created and appended
5662 * to the driver list.
5663 * @param uLUN The logical unit which is being re-detached.
5664 * @param pszDriver Driver name.
5665 */
5666static int hdaReattach(PHDASTATE pThis, PHDADRIVER pDrv, uint8_t uLUN, const char *pszDriver)
5667{
5668 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
5669 AssertPtrReturn(pszDriver, VERR_INVALID_POINTER);
5670
5671 PVM pVM = PDMDevHlpGetVM(pThis->pDevInsR3);
5672 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
5673 PCFGMNODE pDev0 = CFGMR3GetChild(pRoot, "Devices/hda/0/");
5674
5675 /* Remove LUN branch. */
5676 CFGMR3RemoveNode(CFGMR3GetChildF(pDev0, "LUN#%u/", uLUN));
5677
5678 if (pDrv)
5679 {
5680 /* Re-use a driver instance => detach the driver before. */
5681 int rc = PDMDevHlpDriverDetach(pThis->pDevInsR3, PDMIBASE_2_PDMDRV(pDrv->pDrvBase), 0 /* fFlags */);
5682 if (RT_FAILURE(rc))
5683 return rc;
5684 }
5685
5686#define RC_CHECK() if (RT_FAILURE(rc)) { AssertReleaseRC(rc); break; }
5687
5688 int rc = VINF_SUCCESS;
5689 do
5690 {
5691 PCFGMNODE pLunL0;
5692 rc = CFGMR3InsertNodeF(pDev0, &pLunL0, "LUN#%u/", uLUN); RC_CHECK();
5693 rc = CFGMR3InsertString(pLunL0, "Driver", "AUDIO"); RC_CHECK();
5694 rc = CFGMR3InsertNode(pLunL0, "Config/", NULL); RC_CHECK();
5695
5696 PCFGMNODE pLunL1, pLunL2;
5697 rc = CFGMR3InsertNode (pLunL0, "AttachedDriver/", &pLunL1); RC_CHECK();
5698 rc = CFGMR3InsertNode (pLunL1, "Config/", &pLunL2); RC_CHECK();
5699 rc = CFGMR3InsertString(pLunL1, "Driver", pszDriver); RC_CHECK();
5700
5701 rc = CFGMR3InsertString(pLunL2, "AudioDriver", pszDriver); RC_CHECK();
5702
5703 } while (0);
5704
5705 if (RT_SUCCESS(rc))
5706 rc = hdaAttachInternal(pThis->pDevInsR3, pDrv, uLUN, 0 /* fFlags */);
5707
5708 LogFunc(("pThis=%p, uLUN=%u, pszDriver=%s, rc=%Rrc\n", pThis, uLUN, pszDriver, rc));
5709
5710#undef RC_CHECK
5711
5712 return rc;
5713}
5714
5715/**
5716 * @interface_method_impl{PDMDEVREG,pfnConstruct}
5717 */
5718static DECLCALLBACK(int) hdaConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
5719{
5720 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5721 Assert(iInstance == 0);
5722 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
5723
5724 /*
5725 * Validations.
5726 */
5727 if (!CFGMR3AreValuesValid(pCfg, "R0Enabled\0"
5728 "RCEnabled\0"
5729 "TimerHz\0"))
5730 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
5731 N_ ("Invalid configuration for the Intel HDA device"));
5732
5733 int rc = CFGMR3QueryBoolDef(pCfg, "RCEnabled", &pThis->fRCEnabled, false);
5734 if (RT_FAILURE(rc))
5735 return PDMDEV_SET_ERROR(pDevIns, rc,
5736 N_("HDA configuration error: failed to read RCEnabled as boolean"));
5737 rc = CFGMR3QueryBoolDef(pCfg, "R0Enabled", &pThis->fR0Enabled, false);
5738 if (RT_FAILURE(rc))
5739 return PDMDEV_SET_ERROR(pDevIns, rc,
5740 N_("HDA configuration error: failed to read R0Enabled as boolean"));
5741#ifndef VBOX_WITH_AUDIO_CALLBACKS
5742 uint16_t uTimerHz;
5743 rc = CFGMR3QueryU16Def(pCfg, "TimerHz", &uTimerHz, 200 /* Hz */);
5744 if (RT_FAILURE(rc))
5745 return PDMDEV_SET_ERROR(pDevIns, rc,
5746 N_("HDA configuration error: failed to read Hertz (Hz) rate as unsigned integer"));
5747#endif
5748
5749 /*
5750 * Initialize data (most of it anyway).
5751 */
5752 pThis->pDevInsR3 = pDevIns;
5753 pThis->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
5754 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
5755 /* IBase */
5756 pThis->IBase.pfnQueryInterface = hdaQueryInterface;
5757
5758 /* PCI Device */
5759 PCIDevSetVendorId (&pThis->PciDev, HDA_PCI_VENDOR_ID); /* nVidia */
5760 PCIDevSetDeviceId (&pThis->PciDev, HDA_PCI_DEVICE_ID); /* HDA */
5761
5762 PCIDevSetCommand (&pThis->PciDev, 0x0000); /* 04 rw,ro - pcicmd. */
5763 PCIDevSetStatus (&pThis->PciDev, VBOX_PCI_STATUS_CAP_LIST); /* 06 rwc?,ro? - pcists. */
5764 PCIDevSetRevisionId (&pThis->PciDev, 0x01); /* 08 ro - rid. */
5765 PCIDevSetClassProg (&pThis->PciDev, 0x00); /* 09 ro - pi. */
5766 PCIDevSetClassSub (&pThis->PciDev, 0x03); /* 0a ro - scc; 03 == HDA. */
5767 PCIDevSetClassBase (&pThis->PciDev, 0x04); /* 0b ro - bcc; 04 == multimedia. */
5768 PCIDevSetHeaderType (&pThis->PciDev, 0x00); /* 0e ro - headtyp. */
5769 PCIDevSetBaseAddress (&pThis->PciDev, 0, /* 10 rw - MMIO */
5770 false /* fIoSpace */, false /* fPrefetchable */, true /* f64Bit */, 0x00000000);
5771 PCIDevSetInterruptLine (&pThis->PciDev, 0x00); /* 3c rw. */
5772 PCIDevSetInterruptPin (&pThis->PciDev, 0x01); /* 3d ro - INTA#. */
5773
5774#if defined(HDA_AS_PCI_EXPRESS)
5775 PCIDevSetCapabilityList (&pThis->PciDev, 0x80);
5776#elif defined(VBOX_WITH_MSI_DEVICES)
5777 PCIDevSetCapabilityList (&pThis->PciDev, 0x60);
5778#else
5779 PCIDevSetCapabilityList (&pThis->PciDev, 0x50); /* ICH6 datasheet 18.1.16 */
5780#endif
5781
5782 /// @todo r=michaln: If there are really no PCIDevSetXx for these, the meaning
5783 /// of these values needs to be properly documented!
5784 /* HDCTL off 0x40 bit 0 selects signaling mode (1-HDA, 0 - Ac97) 18.1.19 */
5785 PCIDevSetByte(&pThis->PciDev, 0x40, 0x01);
5786
5787 /* Power Management */
5788 PCIDevSetByte(&pThis->PciDev, 0x50 + 0, VBOX_PCI_CAP_ID_PM);
5789 PCIDevSetByte(&pThis->PciDev, 0x50 + 1, 0x0); /* next */
5790 PCIDevSetWord(&pThis->PciDev, 0x50 + 2, VBOX_PCI_PM_CAP_DSI | 0x02 /* version, PM1.1 */ );
5791
5792#ifdef HDA_AS_PCI_EXPRESS
5793 /* PCI Express */
5794 PCIDevSetByte(&pThis->PciDev, 0x80 + 0, VBOX_PCI_CAP_ID_EXP); /* PCI_Express */
5795 PCIDevSetByte(&pThis->PciDev, 0x80 + 1, 0x60); /* next */
5796 /* Device flags */
5797 PCIDevSetWord(&pThis->PciDev, 0x80 + 2,
5798 /* version */ 0x1 |
5799 /* Root Complex Integrated Endpoint */ (VBOX_PCI_EXP_TYPE_ROOT_INT_EP << 4) |
5800 /* MSI */ (100) << 9 );
5801 /* Device capabilities */
5802 PCIDevSetDWord(&pThis->PciDev, 0x80 + 4, VBOX_PCI_EXP_DEVCAP_FLRESET);
5803 /* Device control */
5804 PCIDevSetWord( &pThis->PciDev, 0x80 + 8, 0);
5805 /* Device status */
5806 PCIDevSetWord( &pThis->PciDev, 0x80 + 10, 0);
5807 /* Link caps */
5808 PCIDevSetDWord(&pThis->PciDev, 0x80 + 12, 0);
5809 /* Link control */
5810 PCIDevSetWord( &pThis->PciDev, 0x80 + 16, 0);
5811 /* Link status */
5812 PCIDevSetWord( &pThis->PciDev, 0x80 + 18, 0);
5813 /* Slot capabilities */
5814 PCIDevSetDWord(&pThis->PciDev, 0x80 + 20, 0);
5815 /* Slot control */
5816 PCIDevSetWord( &pThis->PciDev, 0x80 + 24, 0);
5817 /* Slot status */
5818 PCIDevSetWord( &pThis->PciDev, 0x80 + 26, 0);
5819 /* Root control */
5820 PCIDevSetWord( &pThis->PciDev, 0x80 + 28, 0);
5821 /* Root capabilities */
5822 PCIDevSetWord( &pThis->PciDev, 0x80 + 30, 0);
5823 /* Root status */
5824 PCIDevSetDWord(&pThis->PciDev, 0x80 + 32, 0);
5825 /* Device capabilities 2 */
5826 PCIDevSetDWord(&pThis->PciDev, 0x80 + 36, 0);
5827 /* Device control 2 */
5828 PCIDevSetQWord(&pThis->PciDev, 0x80 + 40, 0);
5829 /* Link control 2 */
5830 PCIDevSetQWord(&pThis->PciDev, 0x80 + 48, 0);
5831 /* Slot control 2 */
5832 PCIDevSetWord( &pThis->PciDev, 0x80 + 56, 0);
5833#endif
5834
5835 /*
5836 * Register the PCI device.
5837 */
5838 rc = PDMDevHlpPCIRegister(pDevIns, &pThis->PciDev);
5839 if (RT_FAILURE(rc))
5840 return rc;
5841
5842 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 0, 0x4000, PCI_ADDRESS_SPACE_MEM, hdaPciIoRegionMap);
5843 if (RT_FAILURE(rc))
5844 return rc;
5845
5846#ifdef VBOX_WITH_MSI_DEVICES
5847 PDMMSIREG MsiReg;
5848 RT_ZERO(MsiReg);
5849 MsiReg.cMsiVectors = 1;
5850 MsiReg.iMsiCapOffset = 0x60;
5851 MsiReg.iMsiNextOffset = 0x50;
5852 rc = PDMDevHlpPCIRegisterMsi(pDevIns, &MsiReg);
5853 if (RT_FAILURE(rc))
5854 {
5855 /* That's OK, we can work without MSI */
5856 PCIDevSetCapabilityList(&pThis->PciDev, 0x50);
5857 }
5858#endif
5859
5860 rc = PDMDevHlpSSMRegister(pDevIns, HDA_SSM_VERSION, sizeof(*pThis), hdaSaveExec, hdaLoadExec);
5861 if (RT_FAILURE(rc))
5862 return rc;
5863
5864 RTListInit(&pThis->lstDrv);
5865
5866 uint8_t uLUN;
5867 for (uLUN = 0; uLUN < UINT8_MAX; ++uLUN)
5868 {
5869 LogFunc(("Trying to attach driver for LUN #%RU32 ...\n", uLUN));
5870 rc = hdaAttachInternal(pDevIns, NULL /* pDrv */, uLUN, 0 /* fFlags */);
5871 if (RT_FAILURE(rc))
5872 {
5873 if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
5874 rc = VINF_SUCCESS;
5875 else if (rc == VERR_AUDIO_BACKEND_INIT_FAILED)
5876 {
5877 hdaReattach(pThis, NULL /* pDrv */, uLUN, "NullAudio");
5878 PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "HostAudioNotResponding",
5879 N_("No audio devices could be opened. Selecting the NULL audio backend "
5880 "with the consequence that no sound is audible"));
5881 /* attaching to the NULL audio backend will never fail */
5882 rc = VINF_SUCCESS;
5883 }
5884 break;
5885 }
5886 }
5887
5888 LogFunc(("cLUNs=%RU8, rc=%Rrc\n", uLUN, rc));
5889
5890 if (RT_SUCCESS(rc))
5891 {
5892 rc = AudioMixerCreate("HDA Mixer", 0 /* uFlags */, &pThis->pMixer);
5893 if (RT_SUCCESS(rc))
5894 {
5895 /* Set a default audio format for our mixer. */
5896 PDMAUDIOSTREAMCFG streamCfg;
5897 streamCfg.uHz = 44100;
5898 streamCfg.cChannels = 2;
5899 streamCfg.enmFormat = PDMAUDIOFMT_S16;
5900 streamCfg.enmEndianness = PDMAUDIOHOSTENDIANNESS;
5901
5902 rc = AudioMixerSetDeviceFormat(pThis->pMixer, &streamCfg);
5903 AssertRC(rc);
5904
5905 /*
5906 * Add mixer output sinks.
5907 */
5908#ifdef VBOX_WITH_HDA_51_SURROUND
5909 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] Front",
5910 AUDMIXSINKDIR_OUTPUT, &pThis->SinkFront.pMixSink);
5911 AssertRC(rc);
5912 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] Center / Subwoofer",
5913 AUDMIXSINKDIR_OUTPUT, &pThis->SinkCenterLFE.pMixSink);
5914 AssertRC(rc);
5915 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] Rear",
5916 AUDMIXSINKDIR_OUTPUT, &pThis->SinkRear.pMixSink);
5917 AssertRC(rc);
5918#else
5919 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] PCM Output",
5920 AUDMIXSINKDIR_OUTPUT, &pThis->SinkFront.pMixSink);
5921 AssertRC(rc);
5922#endif
5923 /*
5924 * Add mixer input sinks.
5925 */
5926 rc = AudioMixerCreateSink(pThis->pMixer, "[Recording] Line In",
5927 AUDMIXSINKDIR_INPUT, &pThis->SinkLineIn.pMixSink);
5928 AssertRC(rc);
5929#ifdef VBOX_WITH_HDA_MIC_IN
5930 rc = AudioMixerCreateSink(pThis->pMixer, "[Recording] Microphone In",
5931 AUDMIXSINKDIR_INPUT, &pThis->SinkMicIn.pMixSink);
5932 AssertRC(rc);
5933#endif
5934 /* There is no master volume control. Set the master to max. */
5935 PDMAUDIOVOLUME vol = { false, 255, 255 };
5936 rc = AudioMixerSetMasterVolume(pThis->pMixer, &vol);
5937 AssertRC(rc);
5938 }
5939 }
5940
5941 if (RT_SUCCESS(rc))
5942 {
5943 /* Construct codec. */
5944 pThis->pCodec = (PHDACODEC)RTMemAllocZ(sizeof(HDACODEC));
5945 if (!pThis->pCodec)
5946 return PDMDEV_SET_ERROR(pDevIns, VERR_NO_MEMORY, N_("Out of memory allocating HDA codec state"));
5947
5948 /* Set codec callbacks. */
5949 pThis->pCodec->pfnMixerAddStream = hdaMixerAddStream;
5950 pThis->pCodec->pfnMixerRemoveStream = hdaMixerRemoveStream;
5951 pThis->pCodec->pfnMixerSetStream = hdaMixerSetStream;
5952 pThis->pCodec->pfnMixerSetVolume = hdaMixerSetVolume;
5953 pThis->pCodec->pfnReset = hdaCodecReset;
5954
5955 pThis->pCodec->pHDAState = pThis; /* Assign HDA controller state to codec. */
5956
5957 /* Construct the codec. */
5958 rc = hdaCodecConstruct(pDevIns, pThis->pCodec, 0 /* Codec index */, pCfg);
5959 if (RT_FAILURE(rc))
5960 AssertRCReturn(rc, rc);
5961
5962 /* ICH6 datasheet defines 0 values for SVID and SID (18.1.14-15), which together with values returned for
5963 verb F20 should provide device/codec recognition. */
5964 Assert(pThis->pCodec->u16VendorId);
5965 Assert(pThis->pCodec->u16DeviceId);
5966 PCIDevSetSubSystemVendorId(&pThis->PciDev, pThis->pCodec->u16VendorId); /* 2c ro - intel.) */
5967 PCIDevSetSubSystemId( &pThis->PciDev, pThis->pCodec->u16DeviceId); /* 2e ro. */
5968 }
5969
5970 if (RT_SUCCESS(rc))
5971 {
5972 /*
5973 * Create all hardware streams.
5974 */
5975 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
5976 {
5977 rc = hdaStreamCreate(&pThis->aStreams[i], i /* uSD */);
5978 AssertRC(rc);
5979 }
5980
5981 /*
5982 * Initialize the driver chain.
5983 */
5984 PHDADRIVER pDrv;
5985 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
5986 {
5987 /*
5988 * Only primary drivers are critical for the VM to run. Everything else
5989 * might not worth showing an own error message box in the GUI.
5990 */
5991 if (!(pDrv->Flags & PDMAUDIODRVFLAG_PRIMARY))
5992 continue;
5993
5994 PPDMIAUDIOCONNECTOR pCon = pDrv->pConnector;
5995 AssertPtr(pCon);
5996
5997 bool fValidLineIn = AudioMixerStreamIsValid(pDrv->LineIn.pMixStrm);
5998#ifdef VBOX_WITH_HDA_MIC_IN
5999 bool fValidMicIn = AudioMixerStreamIsValid(pDrv->MicIn.pMixStrm);
6000#endif
6001 bool fValidOut = AudioMixerStreamIsValid(pDrv->Front.pMixStrm);
6002#ifdef VBOX_WITH_HDA_51_SURROUND
6003 /** @todo Anything to do here? */
6004#endif
6005
6006 if ( !fValidLineIn
6007#ifdef VBOX_WITH_HDA_MIC_IN
6008 && !fValidMicIn
6009#endif
6010 && !fValidOut)
6011 {
6012 LogRel(("HDA: Falling back to NULL backend (no sound audible)\n"));
6013
6014 hdaReset(pDevIns);
6015 hdaReattach(pThis, pDrv, pDrv->uLUN, "NullAudio");
6016
6017 PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "HostAudioNotResponding",
6018 N_("No audio devices could be opened. Selecting the NULL audio backend "
6019 "with the consequence that no sound is audible"));
6020 }
6021 else
6022 {
6023 bool fWarn = false;
6024
6025 PDMAUDIOBACKENDCFG backendCfg;
6026 int rc2 = pCon->pfnGetConfig(pCon, &backendCfg);
6027 if (RT_SUCCESS(rc2))
6028 {
6029 if (backendCfg.cSources)
6030 {
6031#ifdef VBOX_WITH_HDA_MIC_IN
6032 /* If the audio backend supports two or more input streams at once,
6033 * warn if one of our two inputs (microphone-in and line-in) failed to initialize. */
6034 if (backendCfg.cMaxStreamsIn >= 2)
6035 fWarn = !fValidLineIn || !fValidMicIn;
6036 /* If the audio backend only supports one input stream at once (e.g. pure ALSA, and
6037 * *not* ALSA via PulseAudio plugin!), only warn if both of our inputs failed to initialize.
6038 * One of the two simply is not in use then. */
6039 else if (backendCfg.cMaxStreamsIn == 1)
6040 fWarn = !fValidLineIn && !fValidMicIn;
6041 /* Don't warn if our backend is not able of supporting any input streams at all. */
6042#else
6043 /* We only have line-in as input source. */
6044 fWarn = !fValidLineIn;
6045#endif
6046 }
6047
6048 if ( !fWarn
6049 && backendCfg.cSinks)
6050 {
6051 fWarn = !fValidOut;
6052 }
6053 }
6054 else
6055 {
6056 LogRel(("HDA: Unable to retrieve audio backend configuration for LUN #%RU8, rc=%Rrc\n", pDrv->uLUN, rc2));
6057 fWarn = true;
6058 }
6059
6060 if (fWarn)
6061 {
6062 char szMissingStreams[255];
6063 size_t len = 0;
6064 if (!fValidLineIn)
6065 {
6066 LogRel(("HDA: WARNING: Unable to open PCM line input for LUN #%RU8!\n", pDrv->uLUN));
6067 len = RTStrPrintf(szMissingStreams, sizeof(szMissingStreams), "PCM Input");
6068 }
6069#ifdef VBOX_WITH_HDA_MIC_IN
6070 if (!fValidMicIn)
6071 {
6072 LogRel(("HDA: WARNING: Unable to open PCM microphone input for LUN #%RU8!\n", pDrv->uLUN));
6073 len += RTStrPrintf(szMissingStreams + len,
6074 sizeof(szMissingStreams) - len, len ? ", PCM Microphone" : "PCM Microphone");
6075 }
6076#endif
6077 if (!fValidOut)
6078 {
6079 LogRel(("HDA: WARNING: Unable to open PCM output for LUN #%RU8!\n", pDrv->uLUN));
6080 len += RTStrPrintf(szMissingStreams + len,
6081 sizeof(szMissingStreams) - len, len ? ", PCM Output" : "PCM Output");
6082 }
6083
6084 PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "HostAudioNotResponding",
6085 N_("Some HDA audio streams (%s) could not be opened. Guest applications generating audio "
6086 "output or depending on audio input may hang. Make sure your host audio device "
6087 "is working properly. Check the logfile for error messages of the audio "
6088 "subsystem"), szMissingStreams);
6089 }
6090 }
6091 }
6092 }
6093
6094 if (RT_SUCCESS(rc))
6095 {
6096 hdaReset(pDevIns);
6097
6098 /*
6099 * 18.2.6,7 defines that values of this registers might be cleared on power on/reset
6100 * hdaReset shouldn't affects these registers.
6101 */
6102 HDA_REG(pThis, WAKEEN) = 0x0;
6103 HDA_REG(pThis, STATESTS) = 0x0;
6104
6105#ifdef DEBUG
6106 /*
6107 * Debug and string formatter types.
6108 */
6109 PDMDevHlpDBGFInfoRegister(pDevIns, "hda", "HDA info. (hda [register case-insensitive])", hdaDbgInfo);
6110 PDMDevHlpDBGFInfoRegister(pDevIns, "hdabdle", "HDA stream BDLE info. (hdabdle [stream number])", hdaDbgInfoBDLE);
6111 PDMDevHlpDBGFInfoRegister(pDevIns, "hdastrm", "HDA stream info. (hdastrm [stream number])", hdaDbgInfoStream);
6112 PDMDevHlpDBGFInfoRegister(pDevIns, "hdcnodes", "HDA codec nodes.", hdaDbgInfoCodecNodes);
6113 PDMDevHlpDBGFInfoRegister(pDevIns, "hdcselector", "HDA codec's selector states [node number].", hdaDbgInfoCodecSelector);
6114 PDMDevHlpDBGFInfoRegister(pDevIns, "hdamixer", "HDA mixer state.", hdaDbgInfoMixer);
6115
6116 rc = RTStrFormatTypeRegister("bdle", hdaDbgFmtBDLE, NULL);
6117 AssertRC(rc);
6118 rc = RTStrFormatTypeRegister("sdctl", hdaDbgFmtSDCTL, NULL);
6119 AssertRC(rc);
6120 rc = RTStrFormatTypeRegister("sdsts", hdaDbgFmtSDSTS, NULL);
6121 AssertRC(rc);
6122 rc = RTStrFormatTypeRegister("sdfifos", hdaDbgFmtSDFIFOS, NULL);
6123 AssertRC(rc);
6124 rc = RTStrFormatTypeRegister("sdfifow", hdaDbgFmtSDFIFOW, NULL);
6125 AssertRC(rc);
6126#endif /* DEBUG */
6127
6128 /*
6129 * Some debug assertions.
6130 */
6131 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegMap); i++)
6132 {
6133 struct HDAREGDESC const *pReg = &g_aHdaRegMap[i];
6134 struct HDAREGDESC const *pNextReg = i + 1 < RT_ELEMENTS(g_aHdaRegMap) ? &g_aHdaRegMap[i + 1] : NULL;
6135
6136 /* binary search order. */
6137 AssertReleaseMsg(!pNextReg || pReg->offset + pReg->size <= pNextReg->offset,
6138 ("[%#x] = {%#x LB %#x} vs. [%#x] = {%#x LB %#x}\n",
6139 i, pReg->offset, pReg->size, i + 1, pNextReg->offset, pNextReg->size));
6140
6141 /* alignment. */
6142 AssertReleaseMsg( pReg->size == 1
6143 || (pReg->size == 2 && (pReg->offset & 1) == 0)
6144 || (pReg->size == 3 && (pReg->offset & 3) == 0)
6145 || (pReg->size == 4 && (pReg->offset & 3) == 0),
6146 ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
6147
6148 /* registers are packed into dwords - with 3 exceptions with gaps at the end of the dword. */
6149 AssertRelease(((pReg->offset + pReg->size) & 3) == 0 || pNextReg);
6150 if (pReg->offset & 3)
6151 {
6152 struct HDAREGDESC const *pPrevReg = i > 0 ? &g_aHdaRegMap[i - 1] : NULL;
6153 AssertReleaseMsg(pPrevReg, ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
6154 if (pPrevReg)
6155 AssertReleaseMsg(pPrevReg->offset + pPrevReg->size == pReg->offset,
6156 ("[%#x] = {%#x LB %#x} vs. [%#x] = {%#x LB %#x}\n",
6157 i - 1, pPrevReg->offset, pPrevReg->size, i + 1, pReg->offset, pReg->size));
6158 }
6159#if 0
6160 if ((pReg->offset + pReg->size) & 3)
6161 {
6162 AssertReleaseMsg(pNextReg, ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
6163 if (pNextReg)
6164 AssertReleaseMsg(pReg->offset + pReg->size == pNextReg->offset,
6165 ("[%#x] = {%#x LB %#x} vs. [%#x] = {%#x LB %#x}\n",
6166 i, pReg->offset, pReg->size, i + 1, pNextReg->offset, pNextReg->size));
6167 }
6168#endif
6169 /* The final entry is a full DWORD, no gaps! Allows shortcuts. */
6170 AssertReleaseMsg(pNextReg || ((pReg->offset + pReg->size) & 3) == 0,
6171 ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
6172 }
6173 }
6174
6175# ifndef VBOX_WITH_AUDIO_CALLBACKS
6176 if (RT_SUCCESS(rc))
6177 {
6178 /* Start the emulation timer. */
6179 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, hdaTimer, pThis,
6180 TMTIMER_FLAGS_NO_CRIT_SECT, "DevIchHda", &pThis->pTimer);
6181 AssertRCReturn(rc, rc);
6182
6183 if (RT_SUCCESS(rc))
6184 {
6185 pThis->cTimerTicks = TMTimerGetFreq(pThis->pTimer) / uTimerHz;
6186 pThis->uTimerTS = TMTimerGet(pThis->pTimer);
6187 LogFunc(("Timer ticks=%RU64 (%RU16 Hz)\n", pThis->cTimerTicks, uTimerHz));
6188
6189 hdaTimerMaybeStart(pThis);
6190 }
6191 }
6192# else
6193 if (RT_SUCCESS(rc))
6194 {
6195 PHDADRIVER pDrv;
6196 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
6197 {
6198 /* Only register primary driver.
6199 * The device emulation does the output multiplexing then. */
6200 if (pDrv->Flags != PDMAUDIODRVFLAG_PRIMARY)
6201 continue;
6202
6203 PDMAUDIOCALLBACK AudioCallbacks[2];
6204
6205 HDACALLBACKCTX Ctx = { pThis, pDrv };
6206
6207 AudioCallbacks[0].enmType = PDMAUDIOCALLBACKTYPE_INPUT;
6208 AudioCallbacks[0].pfnCallback = hdaCallbackInput;
6209 AudioCallbacks[0].pvCtx = &Ctx;
6210 AudioCallbacks[0].cbCtx = sizeof(HDACALLBACKCTX);
6211
6212 AudioCallbacks[1].enmType = PDMAUDIOCALLBACKTYPE_OUTPUT;
6213 AudioCallbacks[1].pfnCallback = hdaCallbackOutput;
6214 AudioCallbacks[1].pvCtx = &Ctx;
6215 AudioCallbacks[1].cbCtx = sizeof(HDACALLBACKCTX);
6216
6217 rc = pDrv->pConnector->pfnRegisterCallbacks(pDrv->pConnector, AudioCallbacks, RT_ELEMENTS(AudioCallbacks));
6218 if (RT_FAILURE(rc))
6219 break;
6220 }
6221 }
6222# endif
6223
6224# ifdef VBOX_WITH_STATISTICS
6225 if (RT_SUCCESS(rc))
6226 {
6227 /*
6228 * Register statistics.
6229 */
6230# ifndef VBOX_WITH_AUDIO_CALLBACKS
6231 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTimer, STAMTYPE_PROFILE, "/Devices/HDA/Timer", STAMUNIT_TICKS_PER_CALL, "Profiling hdaTimer.");
6232# endif
6233 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, "/Devices/HDA/BytesRead" , STAMUNIT_BYTES, "Bytes read from HDA emulation.");
6234 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, "/Devices/HDA/BytesWritten", STAMUNIT_BYTES, "Bytes written to HDA emulation.");
6235 }
6236# endif
6237
6238 LogFlowFuncLeaveRC(rc);
6239 return rc;
6240}
6241
6242/**
6243 * The device registration structure.
6244 */
6245const PDMDEVREG g_DeviceICH6_HDA =
6246{
6247 /* u32Version */
6248 PDM_DEVREG_VERSION,
6249 /* szName */
6250 "hda",
6251 /* szRCMod */
6252 "VBoxDDRC.rc",
6253 /* szR0Mod */
6254 "VBoxDDR0.r0",
6255 /* pszDescription */
6256 "Intel HD Audio Controller",
6257 /* fFlags */
6258 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
6259 /* fClass */
6260 PDM_DEVREG_CLASS_AUDIO,
6261 /* cMaxInstances */
6262 1,
6263 /* cbInstance */
6264 sizeof(HDASTATE),
6265 /* pfnConstruct */
6266 hdaConstruct,
6267 /* pfnDestruct */
6268 hdaDestruct,
6269 /* pfnRelocate */
6270 NULL,
6271 /* pfnMemSetup */
6272 NULL,
6273 /* pfnPowerOn */
6274 NULL,
6275 /* pfnReset */
6276 hdaReset,
6277 /* pfnSuspend */
6278 NULL,
6279 /* pfnResume */
6280 NULL,
6281 /* pfnAttach */
6282 hdaAttach,
6283 /* pfnDetach */
6284 hdaDetach,
6285 /* pfnQueryInterface. */
6286 NULL,
6287 /* pfnInitComplete */
6288 NULL,
6289 /* pfnPowerOff */
6290 hdaPowerOff,
6291 /* pfnSoftReset */
6292 NULL,
6293 /* u32VersionEnd */
6294 PDM_DEVREG_VERSION
6295};
6296
6297#endif /* IN_RING3 */
6298#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
Note: See TracBrowser for help on using the repository browser.

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