VirtualBox

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

Last change on this file since 62142 was 62117, checked in by vboxsync, 9 years ago

Audio: Bugfixes, logging.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 224.3 KB
Line 
1/* $Id: DevIchHda.cpp 62117 2016-07-07 16:16:01Z 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 LogRel2(("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 PPDMAUDIOSTREAMCFG pStreamCfg = (PPDMAUDIOSTREAMCFG)RTMemDup(pCfg, sizeof(PDMAUDIOSTREAMCFG));
3781 if (!pStreamCfg)
3782 {
3783 rc = VERR_NO_MEMORY;
3784 break;
3785 }
3786
3787 /* Include the driver's LUN in the stream name for easier identification. */
3788 RTStrPrintf(pStreamCfg->szName, RT_ELEMENTS(pStreamCfg->szName), "[LUN#%RU8] %s", pDrv->uLUN, pCfg->szName);
3789
3790 if (pStreamCfg->enmDir == PDMAUDIODIR_IN)
3791 {
3792 LogFunc(("enmRecSource=%ld\n", pStreamCfg->DestSource.Source));
3793
3794 switch (pStreamCfg->DestSource.Source)
3795 {
3796 case PDMAUDIORECSOURCE_LINE:
3797 pStream = &pDrv->LineIn;
3798 break;
3799#ifdef VBOX_WITH_HDA_MIC_IN
3800 case PDMAUDIORECSOURCE_MIC:
3801 pStream = &pDrv->MicIn;
3802 break;
3803#endif
3804 default:
3805 rc2 = VERR_NOT_SUPPORTED;
3806 break;
3807 }
3808 }
3809 else if (pStreamCfg->enmDir == PDMAUDIODIR_OUT)
3810 {
3811 LogFunc(("enmPlaybackDest=%ld\n", pStreamCfg->DestSource.Dest));
3812
3813 switch (pStreamCfg->DestSource.Dest)
3814 {
3815 case PDMAUDIOPLAYBACKDEST_FRONT:
3816 pStream = &pDrv->Front;
3817 break;
3818#ifdef VBOX_WITH_HDA_51_SURROUND
3819 case PDMAUDIOPLAYBACKDEST_CENTER_LFE:
3820 pStream = &pDrv->CenterLFE;
3821 break;
3822 case PDMAUDIOPLAYBACKDEST_REAR:
3823 pStream = &pDrv->Rear;
3824 break;
3825#endif
3826 default:
3827 rc2 = VERR_NOT_SUPPORTED;
3828 break;
3829 }
3830 }
3831 else
3832 rc2 = VERR_NOT_SUPPORTED;
3833
3834 if (RT_SUCCESS(rc2))
3835 {
3836 AssertPtr(pStream);
3837
3838 AudioMixerSinkRemoveStream(pSink->pMixSink, pStream->pMixStrm);
3839
3840 AudioMixerStreamDestroy(pStream->pMixStrm);
3841 pStream->pMixStrm = NULL;
3842
3843 PAUDMIXSTREAM pMixStrm;
3844 rc2 = AudioMixerSinkCreateStream(pSink->pMixSink, pDrv->pConnector, pStreamCfg, 0 /* fFlags */, &pMixStrm);
3845 if (RT_SUCCESS(rc2))
3846 {
3847 rc2 = AudioMixerSinkAddStream(pSink->pMixSink, pMixStrm);
3848 LogFlowFunc(("LUN#%RU8: Added \"%s\" to sink, rc=%Rrc\n", pDrv->uLUN, pStreamCfg->szName , rc2));
3849 }
3850
3851 if (RT_SUCCESS(rc2))
3852 pStream->pMixStrm = pMixStrm;
3853 }
3854
3855 if (RT_SUCCESS(rc))
3856 rc = rc2;
3857
3858 if (pStreamCfg)
3859 {
3860 RTMemFree(pStreamCfg);
3861 pStreamCfg = NULL;
3862 }
3863 }
3864
3865 LogFlowFuncLeaveRC(rc);
3866 return rc;
3867}
3868
3869/**
3870 * Adds a new audio stream to a specific mixer control.
3871 * Depending on the mixer control the stream then gets assigned to one of the internal
3872 * mixer sinks, which in turn then handle the mixing of all connected streams to that sink.
3873 *
3874 * @return IPRT status code.
3875 * @param pThis HDA state.
3876 * @param enmMixerCtl Mixer control to assign new stream to.
3877 * @param pCfg Stream configuration for the new stream.
3878 */
3879static DECLCALLBACK(int) hdaMixerAddStream(PHDASTATE pThis, PDMAUDIOMIXERCTL enmMixerCtl, PPDMAUDIOSTREAMCFG pCfg)
3880{
3881 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
3882 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
3883
3884 int rc;
3885
3886 PHDAMIXERSINK pSink = hdaMixerControlToSink(pThis, enmMixerCtl);
3887 if (pSink)
3888 {
3889 rc = hdaMixerAddStream(pThis, pSink, pCfg);
3890
3891 AssertPtr(pSink->pMixSink);
3892 LogFlowFunc(("Sink=%s, enmMixerCtl=%ld\n", pSink->pMixSink->pszName, enmMixerCtl));
3893 }
3894 else
3895 rc = VERR_NOT_FOUND;
3896
3897 LogFlowFuncLeaveRC(rc);
3898 return rc;
3899}
3900
3901/**
3902 * Removes a specified mixer control from the HDA's mixer.
3903 *
3904 * @return IPRT status code.
3905 * @param pThis HDA state.
3906 * @param enmMixerCtl Mixer control to remove.
3907 */
3908static DECLCALLBACK(int) hdaMixerRemoveStream(PHDASTATE pThis, PDMAUDIOMIXERCTL enmMixerCtl)
3909{
3910 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
3911
3912 int rc;
3913
3914 PHDAMIXERSINK pSink = hdaMixerControlToSink(pThis, enmMixerCtl);
3915 if (pSink)
3916 {
3917 PHDADRIVER pDrv;
3918 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
3919 {
3920 PAUDMIXSTREAM pMixStream = NULL;
3921 switch (enmMixerCtl)
3922 {
3923 /*
3924 * Input.
3925 */
3926 case PDMAUDIOMIXERCTL_LINE_IN:
3927 pMixStream = pDrv->LineIn.pMixStrm;
3928 pDrv->LineIn.pMixStrm = NULL;
3929 break;
3930#ifdef VBOX_WITH_HDA_MIC_IN
3931 case PDMAUDIOMIXERCTL_MIC_IN:
3932 pMixStream = pDrv->MicIn.pMixStrm;
3933 pDrv->MicIn.pMixStrm = NULL;
3934 break;
3935#endif
3936 /*
3937 * Output.
3938 */
3939 case PDMAUDIOMIXERCTL_FRONT:
3940 pMixStream = pDrv->Front.pMixStrm;
3941 pDrv->Front.pMixStrm = NULL;
3942 break;
3943#ifdef VBOX_WITH_HDA_51_SURROUND
3944 case PDMAUDIOMIXERCTL_CENTER_LFE:
3945 pMixStream = pDrv->CenterLFE.pMixStrm;
3946 pDrv->CenterLFE.pMixStrm = NULL;
3947 break;
3948 case PDMAUDIOMIXERCTL_REAR:
3949 pMixStream = pDrv->Rear.pMixStrm;
3950 pDrv->Rear.pMixStrm = NULL;
3951 break;
3952#endif
3953 default:
3954 AssertMsgFailed(("Mixer control %ld not implemented\n", enmMixerCtl));
3955 break;
3956 }
3957
3958 if (pMixStream)
3959 {
3960 AudioMixerSinkRemoveStream(pSink->pMixSink, pMixStream);
3961 AudioMixerStreamDestroy(pMixStream);
3962
3963 pMixStream = NULL;
3964 }
3965 }
3966
3967 AudioMixerSinkRemoveAllStreams(pSink->pMixSink);
3968 rc = VINF_SUCCESS;
3969 }
3970 else
3971 rc = VERR_NOT_FOUND;
3972
3973 LogFlowFunc(("enmMixerCtl=%ld, rc=%Rrc\n", enmMixerCtl, rc));
3974 return rc;
3975}
3976
3977/**
3978 * Sets a SDn stream number and channel to a particular mixer control.
3979 *
3980 * @returns IPRT status code.
3981 * @param pThis HDA State.
3982 * @param enmMixerCtl Mixer control to set SD stream number and channel for.
3983 * @param uSD SD stream number (number + 1) to set. Set to 0 for unassign.
3984 * @param uChannel Channel to set. Only valid if a valid SD stream number is specified.
3985 */
3986static DECLCALLBACK(int) hdaMixerSetStream(PHDASTATE pThis,
3987 PDMAUDIOMIXERCTL enmMixerCtl, uint8_t uSD, uint8_t uChannel)
3988{
3989 LogFlowFunc(("enmMixerCtl=%RU32, uSD=%RU8, uChannel=%RU8\n", enmMixerCtl, uSD, uChannel));
3990
3991 if (uSD == 0) /* Stream number 0 is reserved. */
3992 {
3993 LogFlowFunc(("Invalid SDn (%RU8) number for mixer control %ld, ignoring\n", uSD, enmMixerCtl));
3994 return VINF_SUCCESS;
3995 }
3996 /* uChannel is optional. */
3997
3998 /* SDn0 starts as 1. */
3999 Assert(uSD);
4000 uSD--;
4001
4002 int rc;
4003
4004 PHDAMIXERSINK pSink = hdaMixerControlToSink(pThis, enmMixerCtl);
4005 if (pSink)
4006 {
4007 if ( (uSD < HDA_MAX_SDI)
4008 && AudioMixerSinkGetDir(pSink->pMixSink) == AUDMIXSINKDIR_OUTPUT)
4009 {
4010 uSD += HDA_MAX_SDI;
4011 }
4012
4013 LogFlowFunc(("%s: Setting to stream ID=%RU8, channel=%RU8, enmMixerCtl=%RU32\n",
4014 pSink->pMixSink->pszName, uSD, uChannel, enmMixerCtl));
4015
4016 Assert(uSD < HDA_MAX_STREAMS);
4017
4018 PHDASTREAM pStream = hdaStreamFromSD(pThis, uSD);
4019 if (pStream)
4020 {
4021 pSink->uSD = uSD;
4022 pSink->uChannel = uChannel;
4023
4024 /* Make sure that the stream also has this sink set. */
4025 hdaStreamAssignToSink(pStream, pSink);
4026
4027 rc = VINF_SUCCESS;
4028 }
4029 else
4030 {
4031 LogRel(("HDA: Guest wanted to assign invalid stream ID=%RU8 (channel %RU8) to mixer control %RU32, skipping\n",
4032 uSD, uChannel, enmMixerCtl));
4033 rc = VERR_INVALID_PARAMETER;
4034 }
4035 }
4036 else
4037 rc = VERR_NOT_FOUND;
4038
4039 LogFlowFuncLeaveRC(rc);
4040 return rc;
4041}
4042
4043/**
4044 * Sets the volume of a specified mixer control.
4045 *
4046 * @return IPRT status code.
4047 * @param pThis HDA State.
4048 * @param enmMixerCtl Mixer control to set volume for.
4049 * @param pVol Pointer to volume data to set.
4050 */
4051static DECLCALLBACK(int) hdaMixerSetVolume(PHDASTATE pThis,
4052 PDMAUDIOMIXERCTL enmMixerCtl, PPDMAUDIOVOLUME pVol)
4053{
4054 int rc;
4055
4056 PHDAMIXERSINK pSink = hdaMixerControlToSink(pThis, enmMixerCtl);
4057 if (pSink)
4058 {
4059 /* Set the volume.
4060 * We assume that the codec already converted it to the correct range. */
4061 rc = AudioMixerSinkSetVolume(pSink->pMixSink, pVol);
4062 }
4063 else
4064 rc = VERR_NOT_FOUND;
4065
4066 LogFlowFuncLeaveRC(rc);
4067 return rc;
4068}
4069
4070#ifndef VBOX_WITH_AUDIO_CALLBACKS
4071
4072static void hdaTimerMaybeStart(PHDASTATE pThis)
4073{
4074 if (pThis->cStreamsActive == 0) /* Only start the timer if there are no active streams. */
4075 return;
4076
4077 if (!pThis->pTimer)
4078 return;
4079
4080 LogFlowFuncEnter();
4081
4082 LogFlowFunc(("Starting timer\n"));
4083
4084 /* Set timer flag. */
4085 ASMAtomicXchgBool(&pThis->fTimerActive, true);
4086
4087 /* Update current time timestamp. */
4088 pThis->uTimerTS = TMTimerGet(pThis->pTimer);
4089
4090 /* Fire off timer. */
4091 TMTimerSet(pThis->pTimer, TMTimerGet(pThis->pTimer) + pThis->cTimerTicks);
4092}
4093
4094static void hdaTimerMaybeStop(PHDASTATE pThis)
4095{
4096 if (pThis->cStreamsActive) /* Some streams still active? Bail out. */
4097 return;
4098
4099 if (!pThis->pTimer)
4100 return;
4101
4102 LogFlowFunc(("Stopping timer\n"));
4103
4104 /* Set timer flag. */
4105 ASMAtomicXchgBool(&pThis->fTimerActive, false);
4106}
4107
4108static DECLCALLBACK(void) hdaTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
4109{
4110 PHDASTATE pThis = (PHDASTATE)pvUser;
4111 Assert(pThis == PDMINS_2_DATA(pDevIns, PHDASTATE));
4112 AssertPtr(pThis);
4113
4114 STAM_PROFILE_START(&pThis->StatTimer, a);
4115
4116 uint64_t cTicksNow = TMTimerGet(pTimer);
4117 uint64_t cTicksElapsed = cTicksNow - pThis->uTimerTS;
4118
4119 LogFlowFuncEnter();
4120
4121 /* Update current time timestamp. */
4122 pThis->uTimerTS = cTicksNow;
4123
4124 /* Flag indicating whether to kick the timer again for a
4125 * new data processing round. */
4126 bool fKickTimer = false;
4127
4128 PHDASTREAM pStreamLineIn = hdaGetStreamFromSink(pThis, &pThis->SinkLineIn);
4129#ifdef VBOX_WITH_HDA_MIC_IN
4130 PHDASTREAM pStreamMicIn = hdaGetStreamFromSink(pThis, &pThis->SinkMicIn);
4131#endif
4132 PHDASTREAM pStreamFront = hdaGetStreamFromSink(pThis, &pThis->SinkFront);
4133#ifdef VBOX_WITH_HDA_51_SURROUND
4134 /** @todo See note below. */
4135#endif
4136
4137 uint32_t cbToProcess;
4138 int rc = AudioMixerSinkUpdate(pThis->SinkLineIn.pMixSink);
4139 if (RT_SUCCESS(rc))
4140 {
4141 cbToProcess = AudioMixerSinkGetReadable(pThis->SinkLineIn.pMixSink);
4142 if (cbToProcess)
4143 {
4144 rc = hdaTransfer(pThis, pStreamLineIn, cbToProcess, NULL /* pcbProcessed */);
4145 fKickTimer |= RT_SUCCESS(rc);
4146 }
4147 }
4148
4149#ifdef VBOX_WITH_HDA_MIC_IN
4150 rc = AudioMixerSinkUpdate(pThis->SinkMicIn.pMixSink);
4151 if (RT_SUCCESS(rc))
4152 {
4153 cbToProcess = AudioMixerSinkGetReadable(pThis->SinkMicIn.pMixSink);
4154 if (cbToProcess)
4155 {
4156 rc = hdaTransfer(pThis, pStreamMicIn, cbToProcess, NULL /* pcbProcessed */);
4157 fKickTimer |= RT_SUCCESS(rc);
4158 }
4159 }
4160#endif
4161
4162#ifdef VBOX_WITH_HDA_51_SURROUND
4163 rc = AudioMixerSinkUpdate(pThis->SinkCenterLFE.pMixSink);
4164 if (RT_SUCCESS(rc))
4165 {
4166
4167 }
4168
4169 rc = AudioMixerSinkUpdate(pThis->SinkRear.pMixSink);
4170 if (RT_SUCCESS(rc))
4171 {
4172
4173 }
4174 /** @todo Check for stream interleaving and only call hdaTransfer() if required! */
4175
4176 /*
4177 * Only call hdaTransfer if CenterLFE and/or Rear are on different SDs,
4178 * otherwise we have to use the interleaved streams support for getting the data
4179 * out of the Front sink (depending on the mapping layout).
4180 */
4181#endif
4182 rc = AudioMixerSinkUpdate(pThis->SinkFront.pMixSink);
4183 if (RT_SUCCESS(rc))
4184 {
4185 cbToProcess = AudioMixerSinkGetWritable(pThis->SinkFront.pMixSink);
4186 if (cbToProcess)
4187 {
4188 rc = hdaTransfer(pThis, pStreamFront, cbToProcess, NULL /* pcbProcessed */);
4189 fKickTimer |= RT_SUCCESS(rc);
4190 }
4191 }
4192
4193 if ( ASMAtomicReadBool(&pThis->fTimerActive)
4194 || fKickTimer)
4195 {
4196 /* Kick the timer again. */
4197 uint64_t cTicks = pThis->cTimerTicks;
4198 /** @todo adjust cTicks down by now much cbOutMin represents. */
4199 TMTimerSet(pThis->pTimer, cTicksNow + cTicks);
4200 }
4201
4202 LogFlowFuncLeave();
4203
4204 STAM_PROFILE_STOP(&pThis->StatTimer, a);
4205}
4206
4207#else /* VBOX_WITH_AUDIO_CALLBACKS */
4208
4209static DECLCALLBACK(int) hdaCallbackInput(PDMAUDIOCALLBACKTYPE enmType, void *pvCtx, size_t cbCtx, void *pvUser, size_t cbUser)
4210{
4211 Assert(enmType == PDMAUDIOCALLBACKTYPE_INPUT);
4212 AssertPtrReturn(pvCtx, VERR_INVALID_POINTER);
4213 AssertReturn(cbCtx, VERR_INVALID_PARAMETER);
4214 AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
4215 AssertReturn(cbUser, VERR_INVALID_PARAMETER);
4216
4217 PHDACALLBACKCTX pCtx = (PHDACALLBACKCTX)pvCtx;
4218 AssertReturn(cbCtx == sizeof(HDACALLBACKCTX), VERR_INVALID_PARAMETER);
4219
4220 PPDMAUDIOCALLBACKDATAIN pData = (PPDMAUDIOCALLBACKDATAIN)pvUser;
4221 AssertReturn(cbUser == sizeof(PDMAUDIOCALLBACKDATAIN), VERR_INVALID_PARAMETER);
4222
4223 return hdaTransfer(pCtx->pThis, PI_INDEX, UINT32_MAX, &pData->cbOutRead);
4224}
4225
4226static DECLCALLBACK(int) hdaCallbackOutput(PDMAUDIOCALLBACKTYPE enmType, void *pvCtx, size_t cbCtx, void *pvUser, size_t cbUser)
4227{
4228 Assert(enmType == PDMAUDIOCALLBACKTYPE_OUTPUT);
4229 AssertPtrReturn(pvCtx, VERR_INVALID_POINTER);
4230 AssertReturn(cbCtx, VERR_INVALID_PARAMETER);
4231 AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
4232 AssertReturn(cbUser, VERR_INVALID_PARAMETER);
4233
4234 PHDACALLBACKCTX pCtx = (PHDACALLBACKCTX)pvCtx;
4235 AssertReturn(cbCtx == sizeof(HDACALLBACKCTX), VERR_INVALID_PARAMETER);
4236
4237 PPDMAUDIOCALLBACKDATAOUT pData = (PPDMAUDIOCALLBACKDATAOUT)pvUser;
4238 AssertReturn(cbUser == sizeof(PDMAUDIOCALLBACKDATAOUT), VERR_INVALID_PARAMETER);
4239
4240 PHDASTATE pThis = pCtx->pThis;
4241
4242 int rc = hdaTransfer(pCtx->pThis, PO_INDEX, UINT32_MAX, &pData->cbOutWritten);
4243 if ( RT_SUCCESS(rc)
4244 && pData->cbOutWritten)
4245 {
4246 PHDADRIVER pDrv;
4247 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
4248 {
4249 uint32_t cSamplesPlayed;
4250 int rc2 = pDrv->pConnector->pfnPlay(pDrv->pConnector, &cSamplesPlayed);
4251 LogFlowFunc(("LUN#%RU8: cSamplesPlayed=%RU32, rc=%Rrc\n", pDrv->uLUN, cSamplesPlayed, rc2));
4252 }
4253 }
4254}
4255#endif /* VBOX_WITH_AUDIO_CALLBACKS */
4256
4257static int hdaTransfer(PHDASTATE pThis, PHDASTREAM pStream, uint32_t cbToProcess, uint32_t *pcbProcessed)
4258{
4259 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
4260 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
4261 /* pcbProcessed is optional. */
4262
4263 if (ASMAtomicReadBool(&pThis->fInReset)) /* HDA controller in reset mode? Bail out. */
4264 {
4265 LogFlowFunc(("HDA in reset mode, skipping\n"));
4266
4267 if (pcbProcessed)
4268 *pcbProcessed = 0;
4269 return VINF_SUCCESS;
4270 }
4271
4272 bool fProceed = true;
4273 int rc = RTSemMutexRequest(pStream->State.hMtx, RT_INDEFINITE_WAIT);
4274 if (RT_FAILURE(rc))
4275 return rc;
4276
4277 Log3Func(("[SD%RU8] fActive=%RTbool, cbToProcess=%RU32\n", pStream->u8SD, pStream->State.fActive, cbToProcess));
4278
4279 /* Stop request received? */
4280 if ( !pStream->State.fActive
4281 || pStream->State.fDoStop)
4282 {
4283 pStream->State.fActive = false;
4284
4285 rc = RTSemEventSignal(pStream->State.hStateChangedEvent);
4286 AssertRC(rc);
4287
4288 fProceed = false;
4289 }
4290 /* Is the stream not in a running state currently? */
4291 else if (!(HDA_STREAM_REG(pThis, CTL, pStream->u8SD) & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN)))
4292 fProceed = false;
4293 /* Nothing to process? */
4294 else if (!cbToProcess)
4295 fProceed = false;
4296
4297 if ((HDA_STREAM_REG(pThis, STS, pStream->u8SD) & HDA_REG_FIELD_FLAG_MASK(SDSTS, BCIS)))
4298 {
4299 Log3Func(("[SD%RU8]: BCIS set\n", pStream->u8SD));
4300 fProceed = false;
4301 }
4302
4303 if (!fProceed)
4304 {
4305 Log3Func(("[SD%RU8]: Skipping\n", pStream->u8SD));
4306
4307 rc = RTSemMutexRelease(pStream->State.hMtx);
4308 AssertRC(rc);
4309
4310 if (pcbProcessed)
4311 *pcbProcessed = 0;
4312 return VINF_SUCCESS;
4313 }
4314
4315 /* Sanity checks. */
4316 Assert(pStream->u8SD <= HDA_MAX_STREAMS);
4317 Assert(pStream->u64BDLBase);
4318 Assert(pStream->u32CBL);
4319
4320 /* State sanity checks. */
4321 Assert(pStream->State.fInReset == false);
4322
4323 uint32_t u32LPIB = HDA_STREAM_REG(pThis, LPIB, pStream->u8SD);
4324 Assert(u32LPIB <= pStream->u32CBL);
4325
4326 bool fInterrupt = false;
4327
4328#ifdef DEBUG_andy
4329//# define DEBUG_SIMPLE
4330#endif
4331
4332#ifdef DEBUG_SIMPLE
4333 uint8_t u8FIFO[_16K+1];
4334 size_t u8FIFOff = 0;
4335#endif
4336
4337 uint32_t cbLeft = cbToProcess;
4338 uint32_t cbTotal = 0;
4339 uint32_t cbChunk = 0;
4340 uint32_t cbChunkProcessed = 0;
4341
4342 /* Set the FIFORDY bit on the stream while doing the transfer. */
4343 HDA_STREAM_REG(pThis, STS, pStream->u8SD) |= HDA_REG_FIELD_FLAG_MASK(SDSTS, FIFORDY);
4344
4345 while (cbLeft)
4346 {
4347 /* Do we need to fetch the next Buffer Descriptor Entry (BDLE)? */
4348 if (hdaStreamNeedsNextBDLE(pThis, pStream))
4349 {
4350 rc = hdaStreamGetNextBDLE(pThis, pStream);
4351 if (RT_FAILURE(rc))
4352 break;
4353 }
4354
4355 cbChunk = hdaStreamGetTransferSize(pThis, pStream, cbLeft);
4356 cbChunkProcessed = 0;
4357
4358 if (hdaGetDirFromSD(pStream->u8SD) == PDMAUDIODIR_IN)
4359 rc = hdaReadAudio(pThis, pStream, cbChunk, &cbChunkProcessed);
4360 else
4361 {
4362#ifndef DEBUG_SIMPLE
4363 rc = hdaWriteAudio(pThis, pStream, cbChunk, &cbChunkProcessed);
4364#else
4365 void *pvBuf = u8FIFO + u8FIFOff;
4366 int32_t cbBuf = cbChunk;
4367
4368 PHDABDLE pBDLE = &pStream->State.BDLE;
4369
4370 if (cbBuf)
4371 rc = PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns),
4372 pBDLE->u64BufAdr + pBDLE->State.u32BufOff,
4373 pvBuf, cbBuf);
4374
4375 cbChunkProcessed = cbChunk;
4376
4377 hdaBDLEUpdate(pBDLE, cbChunkProcessed, cbChunkProcessed);
4378
4379 u8FIFOff += cbChunkProcessed;
4380 Assert((u8FIFOff & 1) == 0);
4381 Assert(u8FIFOff <= sizeof(u8FIFO));
4382#endif
4383 }
4384
4385 if (RT_FAILURE(rc))
4386 break;
4387
4388 hdaStreamTransferUpdate(pThis, pStream, cbChunkProcessed);
4389
4390 Assert(cbLeft >= cbChunkProcessed);
4391 cbLeft -= cbChunkProcessed;
4392 cbTotal += cbChunkProcessed;
4393
4394 if (rc == VINF_EOF)
4395 break;
4396
4397 if (hdaStreamTransferIsComplete(pThis, pStream, &fInterrupt))
4398 break;
4399 }
4400
4401 /* Remove the FIFORDY bit again. */
4402 HDA_STREAM_REG(pThis, STS, pStream->u8SD) &= ~HDA_REG_FIELD_FLAG_MASK(SDSTS, FIFORDY);
4403
4404 LogFlowFunc(("[SD%RU8]: %RU32 / %RU32, rc=%Rrc\n", pStream->u8SD, cbTotal, cbToProcess, rc));
4405
4406#ifdef DEBUG_SIMPLE
4407# ifdef HDA_DEBUG_DUMP_PCM_DATA
4408 RTFILE fh;
4409 RTFileOpen(&fh, HDA_DEBUG_DUMP_PCM_DATA_PATH "hdaWriteAudio.pcm",
4410 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
4411 RTFileWrite(fh, u8FIFO, u8FIFOff, NULL);
4412 RTFileClose(fh);
4413# endif
4414
4415 AudioMixerSinkWrite(pThis->SinkFront.pMixSink, AUDMIXOP_COPY, u8FIFO, u8FIFOff,
4416 NULL /* pcbWritten */);
4417#endif /* DEBUG_SIMPLE */
4418
4419 if (fInterrupt)
4420 {
4421 /**
4422 * Set the BCIS (Buffer Completion Interrupt Status) flag as the
4423 * last byte of data for the current descriptor has been fetched
4424 * from memory and put into the DMA FIFO.
4425 *
4426 * Speech synthesis works fine on Mac Guest if this bit isn't set
4427 * but in general sound quality gets worse.
4428 *
4429 * This must be set in *any* case.
4430 */
4431 HDA_STREAM_REG(pThis, STS, pStream->u8SD) |= HDA_REG_FIELD_FLAG_MASK(SDSTS, BCIS);
4432 Log3Func(("[SD%RU8]: BCIS: Set\n", pStream->u8SD));
4433
4434 hdaProcessInterrupt(pThis);
4435 }
4436
4437 if (RT_SUCCESS(rc))
4438 {
4439 if (pcbProcessed)
4440 *pcbProcessed = cbTotal;
4441 }
4442
4443 int rc2 = RTSemMutexRelease(pStream->State.hMtx);
4444 if (RT_SUCCESS(rc))
4445 rc = rc2;
4446
4447 return rc;
4448}
4449#endif /* IN_RING3 */
4450
4451/* MMIO callbacks */
4452
4453/**
4454 * @callback_method_impl{FNIOMMMIOREAD, Looks up and calls the appropriate handler.}
4455 *
4456 * @note During implementation, we discovered so-called "forgotten" or "hole"
4457 * registers whose description is not listed in the RPM, datasheet, or
4458 * spec.
4459 */
4460PDMBOTHCBDECL(int) hdaMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
4461{
4462 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4463 int rc;
4464
4465 /*
4466 * Look up and log.
4467 */
4468 uint32_t offReg = GCPhysAddr - pThis->MMIOBaseAddr;
4469 int idxRegDsc = hdaRegLookup(pThis, offReg); /* Register descriptor index. */
4470#ifdef LOG_ENABLED
4471 unsigned const cbLog = cb;
4472 uint32_t offRegLog = offReg;
4473#endif
4474
4475 Log3Func(("offReg=%#x cb=%#x\n", offReg, cb));
4476 Assert(cb == 4); Assert((offReg & 3) == 0);
4477
4478 if (pThis->fInReset && idxRegDsc != HDA_REG_GCTL)
4479 LogFunc(("Access to registers except GCTL is blocked while reset\n"));
4480
4481 if (idxRegDsc == -1)
4482 LogRel(("HDA: Invalid read access @0x%x (bytes=%u)\n", offReg, cb));
4483
4484 if (idxRegDsc != -1)
4485 {
4486 /* ASSUMES gapless DWORD at end of map. */
4487 if (g_aHdaRegMap[idxRegDsc].size == 4)
4488 {
4489 /*
4490 * Straight forward DWORD access.
4491 */
4492 rc = g_aHdaRegMap[idxRegDsc].pfnRead(pThis, idxRegDsc, (uint32_t *)pv);
4493 Log3Func(("\tRead %s => %x (%Rrc)\n", g_aHdaRegMap[idxRegDsc].abbrev, *(uint32_t *)pv, rc));
4494 }
4495 else
4496 {
4497 /*
4498 * Multi register read (unless there are trailing gaps).
4499 * ASSUMES that only DWORD reads have sideeffects.
4500 */
4501 uint32_t u32Value = 0;
4502 unsigned cbLeft = 4;
4503 do
4504 {
4505 uint32_t const cbReg = g_aHdaRegMap[idxRegDsc].size;
4506 uint32_t u32Tmp = 0;
4507
4508 rc = g_aHdaRegMap[idxRegDsc].pfnRead(pThis, idxRegDsc, &u32Tmp);
4509 Log3Func(("\tRead %s[%db] => %x (%Rrc)*\n", g_aHdaRegMap[idxRegDsc].abbrev, cbReg, u32Tmp, rc));
4510 if (rc != VINF_SUCCESS)
4511 break;
4512 u32Value |= (u32Tmp & g_afMasks[cbReg]) << ((4 - cbLeft) * 8);
4513
4514 cbLeft -= cbReg;
4515 offReg += cbReg;
4516 idxRegDsc++;
4517 } while (cbLeft > 0 && g_aHdaRegMap[idxRegDsc].offset == offReg);
4518
4519 if (rc == VINF_SUCCESS)
4520 *(uint32_t *)pv = u32Value;
4521 else
4522 Assert(!IOM_SUCCESS(rc));
4523 }
4524 }
4525 else
4526 {
4527 rc = VINF_IOM_MMIO_UNUSED_FF;
4528 Log3Func(("\tHole at %x is accessed for read\n", offReg));
4529 }
4530
4531 /*
4532 * Log the outcome.
4533 */
4534#ifdef LOG_ENABLED
4535 if (cbLog == 4)
4536 Log3Func(("\tReturning @%#05x -> %#010x %Rrc\n", offRegLog, *(uint32_t *)pv, rc));
4537 else if (cbLog == 2)
4538 Log3Func(("\tReturning @%#05x -> %#06x %Rrc\n", offRegLog, *(uint16_t *)pv, rc));
4539 else if (cbLog == 1)
4540 Log3Func(("\tReturning @%#05x -> %#04x %Rrc\n", offRegLog, *(uint8_t *)pv, rc));
4541#endif
4542 return rc;
4543}
4544
4545
4546DECLINLINE(int) hdaWriteReg(PHDASTATE pThis, int idxRegDsc, uint32_t u32Value, char const *pszLog)
4547{
4548 if (pThis->fInReset && idxRegDsc != HDA_REG_GCTL)
4549 {
4550 LogRel2(("HDA: Warning: Access to register 0x%x is blocked while reset\n", idxRegDsc));
4551 return VINF_SUCCESS;
4552 }
4553
4554 uint32_t idxRegMem = g_aHdaRegMap[idxRegDsc].mem_idx;
4555#ifdef LOG_ENABLED
4556 uint32_t const u32CurValue = pThis->au32Regs[idxRegMem];
4557#endif
4558 int rc = g_aHdaRegMap[idxRegDsc].pfnWrite(pThis, idxRegDsc, u32Value);
4559 Log3Func(("Written value %#x to %s[%d byte]; %x => %x%s\n", u32Value, g_aHdaRegMap[idxRegDsc].abbrev,
4560 g_aHdaRegMap[idxRegDsc].size, u32CurValue, pThis->au32Regs[idxRegMem], pszLog));
4561 return rc;
4562}
4563
4564
4565/**
4566 * @callback_method_impl{FNIOMMMIOWRITE, Looks up and calls the appropriate handler.}
4567 */
4568PDMBOTHCBDECL(int) hdaMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
4569{
4570 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4571 int rc;
4572
4573 /*
4574 * The behavior of accesses that aren't aligned on natural boundraries is
4575 * undefined. Just reject them outright.
4576 */
4577 /** @todo IOM could check this, it could also split the 8 byte accesses for us. */
4578 Assert(cb == 1 || cb == 2 || cb == 4 || cb == 8);
4579 if (GCPhysAddr & (cb - 1))
4580 return PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "misaligned write access: GCPhysAddr=%RGp cb=%u\n", GCPhysAddr, cb);
4581
4582 /*
4583 * Look up and log the access.
4584 */
4585 uint32_t offReg = GCPhysAddr - pThis->MMIOBaseAddr;
4586 int idxRegDsc = hdaRegLookup(pThis, offReg);
4587 uint32_t idxRegMem = idxRegDsc != -1 ? g_aHdaRegMap[idxRegDsc].mem_idx : UINT32_MAX;
4588 uint64_t u64Value;
4589 if (cb == 4) u64Value = *(uint32_t const *)pv;
4590 else if (cb == 2) u64Value = *(uint16_t const *)pv;
4591 else if (cb == 1) u64Value = *(uint8_t const *)pv;
4592 else if (cb == 8) u64Value = *(uint64_t const *)pv;
4593 else
4594 {
4595 u64Value = 0; /* shut up gcc. */
4596 AssertReleaseMsgFailed(("%u\n", cb));
4597 }
4598
4599#ifdef LOG_ENABLED
4600 uint32_t const u32LogOldValue = idxRegDsc >= 0 ? pThis->au32Regs[idxRegMem] : UINT32_MAX;
4601 if (idxRegDsc == -1)
4602 Log3Func(("@%#05x u32=%#010x cb=%d\n", offReg, *(uint32_t const *)pv, cb));
4603 else if (cb == 4)
4604 Log3Func(("@%#05x u32=%#010x %s\n", offReg, *(uint32_t *)pv, g_aHdaRegMap[idxRegDsc].abbrev));
4605 else if (cb == 2)
4606 Log3Func(("@%#05x u16=%#06x (%#010x) %s\n", offReg, *(uint16_t *)pv, *(uint32_t *)pv, g_aHdaRegMap[idxRegDsc].abbrev));
4607 else if (cb == 1)
4608 Log3Func(("@%#05x u8=%#04x (%#010x) %s\n", offReg, *(uint8_t *)pv, *(uint32_t *)pv, g_aHdaRegMap[idxRegDsc].abbrev));
4609
4610 if (idxRegDsc >= 0 && g_aHdaRegMap[idxRegDsc].size != cb)
4611 Log3Func(("\tsize=%RU32 != cb=%u!!\n", g_aHdaRegMap[idxRegDsc].size, cb));
4612#endif
4613
4614 /*
4615 * Try for a direct hit first.
4616 */
4617 if (idxRegDsc != -1 && g_aHdaRegMap[idxRegDsc].size == cb)
4618 {
4619 rc = hdaWriteReg(pThis, idxRegDsc, u64Value, "");
4620 Log3Func(("\t%#x -> %#x\n", u32LogOldValue, idxRegMem != UINT32_MAX ? pThis->au32Regs[idxRegMem] : UINT32_MAX));
4621 }
4622 /*
4623 * Partial or multiple register access, loop thru the requested memory.
4624 */
4625 else
4626 {
4627 /*
4628 * If it's an access beyond the start of the register, shift the input
4629 * value and fill in missing bits. Natural alignment rules means we
4630 * will only see 1 or 2 byte accesses of this kind, so no risk of
4631 * shifting out input values.
4632 */
4633 if (idxRegDsc == -1 && (idxRegDsc = hdaRegLookupWithin(pThis, offReg)) != -1)
4634 {
4635 uint32_t const cbBefore = offReg - g_aHdaRegMap[idxRegDsc].offset; Assert(cbBefore > 0 && cbBefore < 4);
4636 offReg -= cbBefore;
4637 idxRegMem = g_aHdaRegMap[idxRegDsc].mem_idx;
4638 u64Value <<= cbBefore * 8;
4639 u64Value |= pThis->au32Regs[idxRegMem] & g_afMasks[cbBefore];
4640 Log3Func(("\tWithin register, supplied %u leading bits: %#llx -> %#llx ...\n",
4641 cbBefore * 8, ~g_afMasks[cbBefore] & u64Value, u64Value));
4642 }
4643
4644 /* Loop thru the write area, it may cover multiple registers. */
4645 rc = VINF_SUCCESS;
4646 for (;;)
4647 {
4648 uint32_t cbReg;
4649 if (idxRegDsc != -1)
4650 {
4651 idxRegMem = g_aHdaRegMap[idxRegDsc].mem_idx;
4652 cbReg = g_aHdaRegMap[idxRegDsc].size;
4653 if (cb < cbReg)
4654 {
4655 u64Value |= pThis->au32Regs[idxRegMem] & g_afMasks[cbReg] & ~g_afMasks[cb];
4656 Log3Func(("\tSupplying missing bits (%#x): %#llx -> %#llx ...\n",
4657 g_afMasks[cbReg] & ~g_afMasks[cb], u64Value & g_afMasks[cb], u64Value));
4658 }
4659 uint32_t u32LogOldVal = pThis->au32Regs[idxRegMem];
4660 rc = hdaWriteReg(pThis, idxRegDsc, u64Value, "*");
4661 Log3Func(("\t%#x -> %#x\n", u32LogOldVal, pThis->au32Regs[idxRegMem]));
4662 }
4663 else
4664 {
4665 LogRel(("HDA: Invalid write access @0x%x\n", offReg));
4666 cbReg = 1;
4667 }
4668 if (rc != VINF_SUCCESS)
4669 break;
4670 if (cbReg >= cb)
4671 break;
4672
4673 /* Advance. */
4674 offReg += cbReg;
4675 cb -= cbReg;
4676 u64Value >>= cbReg * 8;
4677 if (idxRegDsc == -1)
4678 idxRegDsc = hdaRegLookup(pThis, offReg);
4679 else
4680 {
4681 idxRegDsc++;
4682 if ( (unsigned)idxRegDsc >= RT_ELEMENTS(g_aHdaRegMap)
4683 || g_aHdaRegMap[idxRegDsc].offset != offReg)
4684 {
4685 idxRegDsc = -1;
4686 }
4687 }
4688 }
4689 }
4690
4691 return rc;
4692}
4693
4694
4695/* PCI callback. */
4696
4697#ifdef IN_RING3
4698/**
4699 * @callback_method_impl{FNPCIIOREGIONMAP}
4700 */
4701static DECLCALLBACK(int) hdaPciIoRegionMap(PPCIDEVICE pPciDev, int iRegion, RTGCPHYS GCPhysAddress, uint32_t cb,
4702 PCIADDRESSSPACE enmType)
4703{
4704 PPDMDEVINS pDevIns = pPciDev->pDevIns;
4705 PHDASTATE pThis = RT_FROM_MEMBER(pPciDev, HDASTATE, PciDev);
4706 RTIOPORT Port = (RTIOPORT)GCPhysAddress;
4707 int rc;
4708
4709 /*
4710 * 18.2 of the ICH6 datasheet defines the valid access widths as byte, word, and double word.
4711 *
4712 * Let IOM talk DWORDs when reading, saves a lot of complications. On
4713 * writing though, we have to do it all ourselves because of sideeffects.
4714 */
4715 Assert(enmType == PCI_ADDRESS_SPACE_MEM);
4716 rc = PDMDevHlpMMIORegister(pDevIns, GCPhysAddress, cb, NULL /*pvUser*/,
4717 IOMMMIO_FLAGS_READ_DWORD
4718 | IOMMMIO_FLAGS_WRITE_PASSTHRU,
4719 hdaMMIOWrite, hdaMMIORead, "HDA");
4720
4721 if (RT_FAILURE(rc))
4722 return rc;
4723
4724 if (pThis->fR0Enabled)
4725 {
4726 rc = PDMDevHlpMMIORegisterR0(pDevIns, GCPhysAddress, cb, NIL_RTR0PTR /*pvUser*/,
4727 "hdaMMIOWrite", "hdaMMIORead");
4728 if (RT_FAILURE(rc))
4729 return rc;
4730 }
4731
4732 if (pThis->fRCEnabled)
4733 {
4734 rc = PDMDevHlpMMIORegisterRC(pDevIns, GCPhysAddress, cb, NIL_RTRCPTR /*pvUser*/,
4735 "hdaMMIOWrite", "hdaMMIORead");
4736 if (RT_FAILURE(rc))
4737 return rc;
4738 }
4739
4740 pThis->MMIOBaseAddr = GCPhysAddress;
4741 return VINF_SUCCESS;
4742}
4743
4744
4745/* Saved state callbacks. */
4746
4747static int hdaSaveStream(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, PHDASTREAM pStrm)
4748{
4749 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4750
4751 LogFlowFunc(("[SD%RU8]\n", pStrm->u8SD));
4752
4753 /* Save stream ID. */
4754 int rc = SSMR3PutU8(pSSM, pStrm->u8SD);
4755 AssertRCReturn(rc, rc);
4756 Assert(pStrm->u8SD <= HDA_MAX_STREAMS);
4757
4758 rc = SSMR3PutStructEx(pSSM, &pStrm->State, sizeof(HDASTREAMSTATE), 0 /*fFlags*/, g_aSSMStreamStateFields6, NULL);
4759 AssertRCReturn(rc, rc);
4760
4761#ifdef DEBUG /* Sanity checks. */
4762 uint64_t u64BaseDMA = RT_MAKE_U64(HDA_STREAM_REG(pThis, BDPL, pStrm->u8SD),
4763 HDA_STREAM_REG(pThis, BDPU, pStrm->u8SD));
4764 uint16_t u16LVI = HDA_STREAM_REG(pThis, LVI, pStrm->u8SD);
4765 uint32_t u32CBL = HDA_STREAM_REG(pThis, CBL, pStrm->u8SD);
4766
4767 hdaBDLEDumpAll(pThis, u64BaseDMA, u16LVI + 1);
4768
4769 Assert(u64BaseDMA == pStrm->u64BDLBase);
4770 Assert(u16LVI == pStrm->u16LVI);
4771 Assert(u32CBL == pStrm->u32CBL);
4772#endif
4773
4774 rc = SSMR3PutStructEx(pSSM, &pStrm->State.BDLE, sizeof(HDABDLE),
4775 0 /*fFlags*/, g_aSSMBDLEFields6, NULL);
4776 AssertRCReturn(rc, rc);
4777
4778 rc = SSMR3PutStructEx(pSSM, &pStrm->State.BDLE.State, sizeof(HDABDLESTATE),
4779 0 /*fFlags*/, g_aSSMBDLEStateFields6, NULL);
4780 AssertRCReturn(rc, rc);
4781
4782#ifdef DEBUG /* Sanity checks. */
4783 PHDABDLE pBDLE = &pStrm->State.BDLE;
4784 if (u64BaseDMA)
4785 {
4786 Assert(pStrm->State.uCurBDLE <= u16LVI + 1);
4787
4788 HDABDLE curBDLE;
4789 rc = hdaBDLEFetch(pThis, &curBDLE, u64BaseDMA, pStrm->State.uCurBDLE);
4790 AssertRC(rc);
4791
4792 Assert(curBDLE.u32BufSize == pBDLE->u32BufSize);
4793 Assert(curBDLE.u64BufAdr == pBDLE->u64BufAdr);
4794 Assert(curBDLE.fIntOnCompletion == pBDLE->fIntOnCompletion);
4795 }
4796 else
4797 {
4798 Assert(pBDLE->u64BufAdr == 0);
4799 Assert(pBDLE->u32BufSize == 0);
4800 }
4801#endif
4802 return rc;
4803}
4804
4805/**
4806 * @callback_method_impl{FNSSMDEVSAVEEXEC}
4807 */
4808static DECLCALLBACK(int) hdaSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
4809{
4810 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4811
4812 /* Save Codec nodes states. */
4813 hdaCodecSaveState(pThis->pCodec, pSSM);
4814
4815 /* Save MMIO registers. */
4816 SSMR3PutU32(pSSM, RT_ELEMENTS(pThis->au32Regs));
4817 SSMR3PutMem(pSSM, pThis->au32Regs, sizeof(pThis->au32Regs));
4818
4819 /* Save number of streams. */
4820 SSMR3PutU32(pSSM, HDA_MAX_STREAMS);
4821
4822 /* Save stream states. */
4823 int rc;
4824 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
4825 {
4826 rc = hdaSaveStream(pDevIns, pSSM, &pThis->aStreams[i]);
4827 AssertRCReturn(rc, rc);
4828 }
4829
4830 return rc;
4831}
4832
4833
4834/**
4835 * @callback_method_impl{FNSSMDEVLOADEXEC}
4836 */
4837static DECLCALLBACK(int) hdaLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
4838{
4839 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
4840
4841 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
4842
4843 LogRel2(("hdaLoadExec: uVersion=%RU32, uPass=0x%x\n", uVersion, uPass));
4844
4845 /*
4846 * Load Codec nodes states.
4847 */
4848 int rc = hdaCodecLoadState(pThis->pCodec, pSSM, uVersion);
4849 if (RT_FAILURE(rc))
4850 {
4851 LogRel(("HDA: Failed loading codec state (version %RU32, pass 0x%x), rc=%Rrc\n", uVersion, uPass, rc));
4852 return rc;
4853 }
4854
4855 /*
4856 * Load MMIO registers.
4857 */
4858 uint32_t cRegs;
4859 switch (uVersion)
4860 {
4861 case HDA_SSM_VERSION_1:
4862 /* Starting with r71199, we would save 112 instead of 113
4863 registers due to some code cleanups. This only affected trunk
4864 builds in the 4.1 development period. */
4865 cRegs = 113;
4866 if (SSMR3HandleRevision(pSSM) >= 71199)
4867 {
4868 uint32_t uVer = SSMR3HandleVersion(pSSM);
4869 if ( VBOX_FULL_VERSION_GET_MAJOR(uVer) == 4
4870 && VBOX_FULL_VERSION_GET_MINOR(uVer) == 0
4871 && VBOX_FULL_VERSION_GET_BUILD(uVer) >= 51)
4872 cRegs = 112;
4873 }
4874 break;
4875
4876 case HDA_SSM_VERSION_2:
4877 case HDA_SSM_VERSION_3:
4878 cRegs = 112;
4879 AssertCompile(RT_ELEMENTS(pThis->au32Regs) >= 112);
4880 break;
4881
4882 /* Since version 4 we store the register count to stay flexible. */
4883 case HDA_SSM_VERSION_4:
4884 case HDA_SSM_VERSION_5:
4885 case HDA_SSM_VERSION:
4886 rc = SSMR3GetU32(pSSM, &cRegs); AssertRCReturn(rc, rc);
4887 if (cRegs != RT_ELEMENTS(pThis->au32Regs))
4888 LogRel(("HDA: SSM version cRegs is %RU32, expected %RU32\n", cRegs, RT_ELEMENTS(pThis->au32Regs)));
4889 break;
4890
4891 default:
4892 LogRel(("HDA: Unsupported / too new saved state version (%RU32)\n", uVersion));
4893 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
4894 }
4895
4896 if (cRegs >= RT_ELEMENTS(pThis->au32Regs))
4897 {
4898 SSMR3GetMem(pSSM, pThis->au32Regs, sizeof(pThis->au32Regs));
4899 SSMR3Skip(pSSM, sizeof(uint32_t) * (cRegs - RT_ELEMENTS(pThis->au32Regs)));
4900 }
4901 else
4902 SSMR3GetMem(pSSM, pThis->au32Regs, sizeof(uint32_t) * cRegs);
4903
4904 /*
4905 * Note: Saved states < v5 store LVI (u32BdleMaxCvi) for
4906 * *every* BDLE state, whereas it only needs to be stored
4907 * *once* for every stream. Most of the BDLE state we can
4908 * get out of the registers anyway, so just ignore those values.
4909 *
4910 * Also, only the current BDLE was saved, regardless whether
4911 * there were more than one (and there are at least two entries,
4912 * according to the spec).
4913 */
4914#define HDA_SSM_LOAD_BDLE_STATE_PRE_V5(v, x) \
4915 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Begin marker */ \
4916 AssertRCReturn(rc, rc); \
4917 rc = SSMR3GetU64(pSSM, &x.u64BufAdr); /* u64BdleCviAddr */ \
4918 AssertRCReturn(rc, rc); \
4919 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* u32BdleMaxCvi */ \
4920 AssertRCReturn(rc, rc); \
4921 rc = SSMR3GetU32(pSSM, &x.State.u32BDLIndex); /* u32BdleCvi */ \
4922 AssertRCReturn(rc, rc); \
4923 rc = SSMR3GetU32(pSSM, &x.u32BufSize); /* u32BdleCviLen */ \
4924 AssertRCReturn(rc, rc); \
4925 rc = SSMR3GetU32(pSSM, &x.State.u32BufOff); /* u32BdleCviPos */ \
4926 AssertRCReturn(rc, rc); \
4927 rc = SSMR3GetBool(pSSM, &x.fIntOnCompletion); /* fBdleCviIoc */ \
4928 AssertRCReturn(rc, rc); \
4929 rc = SSMR3GetU32(pSSM, &x.State.cbBelowFIFOW); /* cbUnderFifoW */ \
4930 AssertRCReturn(rc, rc); \
4931 rc = SSMR3GetMem(pSSM, &x.State.au8FIFO, sizeof(x.State.au8FIFO)); \
4932 AssertRCReturn(rc, rc); \
4933 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* End marker */ \
4934 AssertRCReturn(rc, rc); \
4935
4936 /*
4937 * Load BDLEs (Buffer Descriptor List Entries) and DMA counters.
4938 */
4939 switch (uVersion)
4940 {
4941 case HDA_SSM_VERSION_1:
4942 case HDA_SSM_VERSION_2:
4943 case HDA_SSM_VERSION_3:
4944 case HDA_SSM_VERSION_4:
4945 {
4946 /* Only load the internal states.
4947 * The rest will be initialized from the saved registers later. */
4948
4949 /* Note 1: Only the *current* BDLE for a stream was saved! */
4950 /* Note 2: The stream's saving order is/was fixed, so don't touch! */
4951
4952 /* Output */
4953 PHDASTREAM pStream = &pThis->aStreams[4];
4954 rc = hdaStreamInit(pThis, pStream, 4 /* 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
4960 /* Microphone-In */
4961 pStream = &pThis->aStreams[2];
4962 rc = hdaStreamInit(pThis, pStream, 2 /* Stream descriptor, hardcoded */);
4963 if (RT_FAILURE(rc))
4964 break;
4965 HDA_SSM_LOAD_BDLE_STATE_PRE_V5(uVersion, pStream->State.BDLE);
4966 pStream->State.uCurBDLE = pStream->State.BDLE.State.u32BDLIndex;
4967
4968 /* Line-In */
4969 pStream = &pThis->aStreams[0];
4970 rc = hdaStreamInit(pThis, pStream, 0 /* Stream descriptor, hardcoded */);
4971 if (RT_FAILURE(rc))
4972 break;
4973 HDA_SSM_LOAD_BDLE_STATE_PRE_V5(uVersion, pStream->State.BDLE);
4974 pStream->State.uCurBDLE = pStream->State.BDLE.State.u32BDLIndex;
4975 break;
4976 }
4977
4978 /* Since v5 we support flexible stream and BDLE counts. */
4979 case HDA_SSM_VERSION_5:
4980 case HDA_SSM_VERSION:
4981 {
4982 uint32_t cStreams;
4983 rc = SSMR3GetU32(pSSM, &cStreams);
4984 if (RT_FAILURE(rc))
4985 break;
4986
4987 LogRel2(("hdaLoadExec: cStreams=%RU32\n", cStreams));
4988
4989 /* Load stream states. */
4990 for (uint32_t i = 0; i < cStreams; i++)
4991 {
4992 uint8_t uSD;
4993 rc = SSMR3GetU8(pSSM, &uSD);
4994 if (RT_FAILURE(rc))
4995 break;
4996
4997 PHDASTREAM pStrm = hdaStreamFromSD(pThis, uSD);
4998 HDASTREAM StreamDummy;
4999
5000 if (!pStrm)
5001 {
5002 RT_ZERO(StreamDummy);
5003 pStrm = &StreamDummy;
5004 LogRel2(("HDA: Warning: Stream ID=%RU32 not supported, skipping to load ...\n", uSD));
5005 break;
5006 }
5007
5008 rc = hdaStreamInit(pThis, pStrm, uSD);
5009 if (RT_FAILURE(rc))
5010 {
5011 LogRel(("HDA: Stream #%RU32: Initialization of stream %RU8 failed, rc=%Rrc\n", i, uSD, rc));
5012 break;
5013 }
5014
5015 if (uVersion == HDA_SSM_VERSION_5)
5016 {
5017 /* Get the current BDLE entry and skip the rest. */
5018 uint16_t cBDLE;
5019
5020 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Begin marker */
5021 AssertRC(rc);
5022 rc = SSMR3GetU16(pSSM, &cBDLE); /* cBDLE */
5023 AssertRC(rc);
5024 rc = SSMR3GetU16(pSSM, &pStrm->State.uCurBDLE); /* uCurBDLE */
5025 AssertRC(rc);
5026 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* End marker */
5027 AssertRC(rc);
5028
5029 uint32_t u32BDLEIndex;
5030 for (uint16_t a = 0; a < cBDLE; a++)
5031 {
5032 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Begin marker */
5033 AssertRC(rc);
5034 rc = SSMR3GetU32(pSSM, &u32BDLEIndex); /* u32BDLIndex */
5035 AssertRC(rc);
5036
5037 /* Does the current BDLE index match the current BDLE to process? */
5038 if (u32BDLEIndex == pStrm->State.uCurBDLE)
5039 {
5040 rc = SSMR3GetU32(pSSM, &pStrm->State.BDLE.State.cbBelowFIFOW); /* cbBelowFIFOW */
5041 AssertRC(rc);
5042 rc = SSMR3GetMem(pSSM,
5043 &pStrm->State.BDLE.State.au8FIFO,
5044 sizeof(pStrm->State.BDLE.State.au8FIFO)); /* au8FIFO */
5045 AssertRC(rc);
5046 rc = SSMR3GetU32(pSSM, &pStrm->State.BDLE.State.u32BufOff); /* u32BufOff */
5047 AssertRC(rc);
5048 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* End marker */
5049 AssertRC(rc);
5050 }
5051 else /* Skip not current BDLEs. */
5052 {
5053 rc = SSMR3Skip(pSSM, sizeof(uint32_t) /* cbBelowFIFOW */
5054 + sizeof(uint8_t) * 256 /* au8FIFO */
5055 + sizeof(uint32_t) /* u32BufOff */
5056 + sizeof(uint32_t)); /* End marker */
5057 AssertRC(rc);
5058 }
5059 }
5060 }
5061 else
5062 {
5063 rc = SSMR3GetStructEx(pSSM, &pStrm->State, sizeof(HDASTREAMSTATE),
5064 0 /* fFlags */, g_aSSMStreamStateFields6, NULL);
5065 if (RT_FAILURE(rc))
5066 break;
5067
5068 rc = SSMR3GetStructEx(pSSM, &pStrm->State.BDLE, sizeof(HDABDLE),
5069 0 /* fFlags */, g_aSSMBDLEFields6, NULL);
5070 if (RT_FAILURE(rc))
5071 break;
5072
5073 rc = SSMR3GetStructEx(pSSM, &pStrm->State.BDLE.State, sizeof(HDABDLESTATE),
5074 0 /* fFlags */, g_aSSMBDLEStateFields6, NULL);
5075 if (RT_FAILURE(rc))
5076 break;
5077 }
5078 }
5079 break;
5080 }
5081
5082 default:
5083 AssertReleaseFailed(); /* Never reached. */
5084 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
5085 }
5086
5087#undef HDA_SSM_LOAD_BDLE_STATE_PRE_V5
5088
5089 if (RT_SUCCESS(rc))
5090 {
5091 pThis->u64CORBBase = RT_MAKE_U64(HDA_REG(pThis, CORBLBASE), HDA_REG(pThis, CORBUBASE));
5092 pThis->u64RIRBBase = RT_MAKE_U64(HDA_REG(pThis, RIRBLBASE), HDA_REG(pThis, RIRBUBASE));
5093 pThis->u64DPBase = RT_MAKE_U64(HDA_REG(pThis, DPLBASE), HDA_REG(pThis, DPUBASE));
5094
5095 /* Also make sure to update the DMA position bit if this was enabled when saving the state. */
5096 pThis->fDMAPosition = RT_BOOL(pThis->u64DPBase & RT_BIT_64(0));
5097 }
5098
5099 if (RT_SUCCESS(rc))
5100 {
5101 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
5102 {
5103 PHDASTREAM pStream = hdaStreamFromSD(pThis, i);
5104 if (pStream)
5105 {
5106 /* Deactive first. */
5107 int rc2 = hdaStreamSetActive(pThis, pStream, false);
5108 AssertRC(rc2);
5109
5110 bool fActive = RT_BOOL(HDA_STREAM_REG(pThis, CTL, i) & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN));
5111
5112 /* Activate, if needed. */
5113 rc2 = hdaStreamSetActive(pThis, pStream, fActive);
5114 AssertRC(rc2);
5115 }
5116 }
5117 }
5118
5119 if (RT_FAILURE(rc))
5120 LogRel(("HDA: Failed loading device state (version %RU32, pass 0x%x), rc=%Rrc\n", uVersion, uPass, rc));
5121
5122 LogFlowFuncLeaveRC(rc);
5123 return rc;
5124}
5125
5126#ifdef DEBUG
5127/* Debug and log type formatters. */
5128
5129/**
5130 * @callback_method_impl{FNRTSTRFORMATTYPE}
5131 */
5132static DECLCALLBACK(size_t) hdaDbgFmtBDLE(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
5133 const char *pszType, void const *pvValue,
5134 int cchWidth, int cchPrecision, unsigned fFlags,
5135 void *pvUser)
5136{
5137 PHDABDLE pBDLE = (PHDABDLE)pvValue;
5138 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
5139 "BDLE(idx:%RU32, off:%RU32, fifow:%RU32, IOC:%RTbool, DMA[%RU32 bytes @ 0x%x])",
5140 pBDLE->State.u32BDLIndex, pBDLE->State.u32BufOff, pBDLE->State.cbBelowFIFOW, pBDLE->fIntOnCompletion,
5141 pBDLE->u32BufSize, pBDLE->u64BufAdr);
5142}
5143
5144/**
5145 * @callback_method_impl{FNRTSTRFORMATTYPE}
5146 */
5147static DECLCALLBACK(size_t) hdaDbgFmtSDCTL(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
5148 const char *pszType, void const *pvValue,
5149 int cchWidth, int cchPrecision, unsigned fFlags,
5150 void *pvUser)
5151{
5152 uint32_t uSDCTL = (uint32_t)(uintptr_t)pvValue;
5153 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
5154 "SDCTL(raw:%#x, DIR:%s, TP:%RTbool, STRIPE:%x, DEIE:%RTbool, FEIE:%RTbool, IOCE:%RTbool, RUN:%RTbool, RESET:%RTbool)",
5155 uSDCTL,
5156 (uSDCTL & HDA_REG_FIELD_FLAG_MASK(SDCTL, DIR)) ? "OUT" : "IN",
5157 RT_BOOL(uSDCTL & HDA_REG_FIELD_FLAG_MASK(SDCTL, TP)),
5158 (uSDCTL & HDA_REG_FIELD_MASK(SDCTL, STRIPE)) >> HDA_SDCTL_STRIPE_SHIFT,
5159 RT_BOOL(uSDCTL & HDA_REG_FIELD_FLAG_MASK(SDCTL, DEIE)),
5160 RT_BOOL(uSDCTL & HDA_REG_FIELD_FLAG_MASK(SDCTL, FEIE)),
5161 RT_BOOL(uSDCTL & HDA_REG_FIELD_FLAG_MASK(SDCTL, ICE)),
5162 RT_BOOL(uSDCTL & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN)),
5163 RT_BOOL(uSDCTL & HDA_REG_FIELD_FLAG_MASK(SDCTL, SRST)));
5164}
5165
5166/**
5167 * @callback_method_impl{FNRTSTRFORMATTYPE}
5168 */
5169static DECLCALLBACK(size_t) hdaDbgFmtSDFIFOS(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
5170 const char *pszType, void const *pvValue,
5171 int cchWidth, int cchPrecision, unsigned fFlags,
5172 void *pvUser)
5173{
5174 uint32_t uSDFIFOS = (uint32_t)(uintptr_t)pvValue;
5175 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "SDFIFOS(raw:%#x, sdfifos:%RU8 B)", uSDFIFOS, hdaSDFIFOSToBytes(uSDFIFOS));
5176}
5177
5178/**
5179 * @callback_method_impl{FNRTSTRFORMATTYPE}
5180 */
5181static DECLCALLBACK(size_t) hdaDbgFmtSDFIFOW(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
5182 const char *pszType, void const *pvValue,
5183 int cchWidth, int cchPrecision, unsigned fFlags,
5184 void *pvUser)
5185{
5186 uint32_t uSDFIFOW = (uint32_t)(uintptr_t)pvValue;
5187 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "SDFIFOW(raw: %#0x, sdfifow:%d B)", uSDFIFOW, hdaSDFIFOWToBytes(uSDFIFOW));
5188}
5189
5190/**
5191 * @callback_method_impl{FNRTSTRFORMATTYPE}
5192 */
5193static DECLCALLBACK(size_t) hdaDbgFmtSDSTS(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
5194 const char *pszType, void const *pvValue,
5195 int cchWidth, int cchPrecision, unsigned fFlags,
5196 void *pvUser)
5197{
5198 uint32_t uSdSts = (uint32_t)(uintptr_t)pvValue;
5199 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
5200 "SDSTS(raw:%#0x, fifordy:%RTbool, dese:%RTbool, fifoe:%RTbool, bcis:%RTbool)",
5201 uSdSts,
5202 RT_BOOL(uSdSts & HDA_REG_FIELD_FLAG_MASK(SDSTS, FIFORDY)),
5203 RT_BOOL(uSdSts & HDA_REG_FIELD_FLAG_MASK(SDSTS, DE)),
5204 RT_BOOL(uSdSts & HDA_REG_FIELD_FLAG_MASK(SDSTS, FE)),
5205 RT_BOOL(uSdSts & HDA_REG_FIELD_FLAG_MASK(SDSTS, BCIS)));
5206}
5207
5208static int hdaDbgLookupRegByName(PHDASTATE pThis, const char *pszArgs)
5209{
5210 int iReg = 0;
5211 for (; iReg < HDA_NUM_REGS; ++iReg)
5212 if (!RTStrICmp(g_aHdaRegMap[iReg].abbrev, pszArgs))
5213 return iReg;
5214 return -1;
5215}
5216
5217
5218static void hdaDbgPrintRegister(PHDASTATE pThis, PCDBGFINFOHLP pHlp, int iHdaIndex)
5219{
5220 Assert( pThis
5221 && iHdaIndex >= 0
5222 && iHdaIndex < HDA_NUM_REGS);
5223 pHlp->pfnPrintf(pHlp, "%s: 0x%x\n", g_aHdaRegMap[iHdaIndex].abbrev, pThis->au32Regs[g_aHdaRegMap[iHdaIndex].mem_idx]);
5224}
5225
5226/**
5227 * @callback_method_impl{FNDBGFHANDLERDEV}
5228 */
5229static DECLCALLBACK(void) hdaDbgInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5230{
5231 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5232 int iHdaRegisterIndex = hdaDbgLookupRegByName(pThis, pszArgs);
5233 if (iHdaRegisterIndex != -1)
5234 hdaDbgPrintRegister(pThis, pHlp, iHdaRegisterIndex);
5235 else
5236 {
5237 for(iHdaRegisterIndex = 0; (unsigned int)iHdaRegisterIndex < HDA_NUM_REGS; ++iHdaRegisterIndex)
5238 hdaDbgPrintRegister(pThis, pHlp, iHdaRegisterIndex);
5239 }
5240}
5241
5242static void hdaDbgPrintStream(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
5250 pHlp->pfnPrintf(pHlp, "Stream #%d:\n", iIdx);
5251 pHlp->pfnPrintf(pHlp, "\tSD%dCTL : %R[sdctl]\n", iIdx, HDA_STREAM_REG(pThis, CTL, iIdx));
5252 pHlp->pfnPrintf(pHlp, "\tSD%dCTS : %R[sdsts]\n", iIdx, HDA_STREAM_REG(pThis, STS, iIdx));
5253 pHlp->pfnPrintf(pHlp, "\tSD%dFIFOS: %R[sdfifos]\n", iIdx, HDA_STREAM_REG(pThis, FIFOS, iIdx));
5254 pHlp->pfnPrintf(pHlp, "\tSD%dFIFOW: %R[sdfifow]\n", iIdx, HDA_STREAM_REG(pThis, FIFOW, iIdx));
5255 pHlp->pfnPrintf(pHlp, "\tBDLE : %R[bdle]\n", &pStrm->State.BDLE);
5256}
5257
5258static void hdaDbgPrintBDLE(PHDASTATE pThis, PCDBGFINFOHLP pHlp, int iIdx)
5259{
5260 Assert( pThis
5261 && iIdx >= 0
5262 && iIdx < HDA_MAX_STREAMS);
5263
5264 const PHDASTREAM pStrm = &pThis->aStreams[iIdx];
5265 const PHDABDLE pBDLE = &pStrm->State.BDLE;
5266
5267 pHlp->pfnPrintf(pHlp, "Stream #%d BDLE:\n", iIdx);
5268 pHlp->pfnPrintf(pHlp, "\t%R[bdle]\n", pBDLE);
5269
5270 uint64_t u64BaseDMA = RT_MAKE_U64(HDA_STREAM_REG(pThis, BDPL, iIdx),
5271 HDA_STREAM_REG(pThis, BDPU, iIdx));
5272 uint16_t u16LVI = HDA_STREAM_REG(pThis, LVI, iIdx);
5273 uint32_t u32CBL = HDA_STREAM_REG(pThis, CBL, iIdx);
5274
5275 if (!u64BaseDMA)
5276 return;
5277
5278 uint32_t cbBDLE = 0;
5279 for (uint16_t i = 0; i < u16LVI + 1; i++)
5280 {
5281 uint8_t bdle[16]; /** @todo Use a define. */
5282 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), u64BaseDMA + i * 16, bdle, 16); /** @todo Use a define. */
5283
5284 uint64_t addr = *(uint64_t *)bdle;
5285 uint32_t len = *(uint32_t *)&bdle[8];
5286 uint32_t ioc = *(uint32_t *)&bdle[12];
5287
5288 pHlp->pfnPrintf(pHlp, "\t#%03d BDLE(adr:0x%llx, size:%RU32, ioc:%RTbool)\n",
5289 i, addr, len, RT_BOOL(ioc & 0x1));
5290
5291 cbBDLE += len;
5292 }
5293
5294 pHlp->pfnPrintf(pHlp, "Total: %RU32 bytes\n", cbBDLE);
5295
5296 pHlp->pfnPrintf(pHlp, "DMA counters (base @ 0x%llx):\n", pThis->u64DPBase);
5297 if (!pThis->u64DPBase) /* No DMA base given? Bail out. */
5298 {
5299 pHlp->pfnPrintf(pHlp, "No counters found\n");
5300 return;
5301 }
5302
5303 for (int i = 0; i < u16LVI + 1; i++)
5304 {
5305 uint32_t uDMACnt;
5306 PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), (pThis->u64DPBase & DPBASE_ADDR_MASK) + (i * 2 * sizeof(uint32_t)),
5307 &uDMACnt, sizeof(uDMACnt));
5308
5309 pHlp->pfnPrintf(pHlp, "\t#%03d DMA @ 0x%x\n", i , uDMACnt);
5310 }
5311}
5312
5313static int hdaDbgLookupStrmIdx(PHDASTATE pThis, const char *pszArgs)
5314{
5315 /** @todo Add args parsing. */
5316 return -1;
5317}
5318
5319/**
5320 * @callback_method_impl{FNDBGFHANDLERDEV}
5321 */
5322static DECLCALLBACK(void) hdaDbgInfoStream(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5323{
5324 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5325 int iHdaStreamdex = hdaDbgLookupStrmIdx(pThis, pszArgs);
5326 if (iHdaStreamdex != -1)
5327 hdaDbgPrintStream(pThis, pHlp, iHdaStreamdex);
5328 else
5329 for(iHdaStreamdex = 0; iHdaStreamdex < HDA_MAX_STREAMS; ++iHdaStreamdex)
5330 hdaDbgPrintStream(pThis, pHlp, iHdaStreamdex);
5331}
5332
5333/**
5334 * @callback_method_impl{FNDBGFHANDLERDEV}
5335 */
5336static DECLCALLBACK(void) hdaDbgInfoBDLE(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5337{
5338 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5339 int iHdaStreamdex = hdaDbgLookupStrmIdx(pThis, pszArgs);
5340 if (iHdaStreamdex != -1)
5341 hdaDbgPrintBDLE(pThis, pHlp, iHdaStreamdex);
5342 else
5343 for(iHdaStreamdex = 0; iHdaStreamdex < HDA_MAX_STREAMS; ++iHdaStreamdex)
5344 hdaDbgPrintBDLE(pThis, pHlp, iHdaStreamdex);
5345}
5346
5347/**
5348 * @callback_method_impl{FNDBGFHANDLERDEV}
5349 */
5350static DECLCALLBACK(void) hdaDbgInfoCodecNodes(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5351{
5352 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5353
5354 if (pThis->pCodec->pfnDbgListNodes)
5355 pThis->pCodec->pfnDbgListNodes(pThis->pCodec, pHlp, pszArgs);
5356 else
5357 pHlp->pfnPrintf(pHlp, "Codec implementation doesn't provide corresponding callback\n");
5358}
5359
5360/**
5361 * @callback_method_impl{FNDBGFHANDLERDEV}
5362 */
5363static DECLCALLBACK(void) hdaDbgInfoCodecSelector(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5364{
5365 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5366
5367 if (pThis->pCodec->pfnDbgSelector)
5368 pThis->pCodec->pfnDbgSelector(pThis->pCodec, pHlp, pszArgs);
5369 else
5370 pHlp->pfnPrintf(pHlp, "Codec implementation doesn't provide corresponding callback\n");
5371}
5372
5373/**
5374 * @callback_method_impl{FNDBGFHANDLERDEV}
5375 */
5376static DECLCALLBACK(void) hdaDbgInfoMixer(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5377{
5378 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5379
5380 if (pThis->pMixer)
5381 AudioMixerDebug(pThis->pMixer, pHlp, pszArgs);
5382 else
5383 pHlp->pfnPrintf(pHlp, "Mixer not available\n");
5384}
5385#endif /* DEBUG */
5386
5387/* PDMIBASE */
5388
5389/**
5390 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
5391 */
5392static DECLCALLBACK(void *) hdaQueryInterface(struct PDMIBASE *pInterface, const char *pszIID)
5393{
5394 PHDASTATE pThis = RT_FROM_MEMBER(pInterface, HDASTATE, IBase);
5395 Assert(&pThis->IBase == pInterface);
5396
5397 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->IBase);
5398 return NULL;
5399}
5400
5401
5402/* PDMDEVREG */
5403
5404/**
5405 * Reset notification.
5406 *
5407 * @returns VBox status code.
5408 * @param pDevIns The device instance data.
5409 *
5410 * @remark The original sources didn't install a reset handler, but it seems to
5411 * make sense to me so we'll do it.
5412 */
5413static DECLCALLBACK(void) hdaReset(PPDMDEVINS pDevIns)
5414{
5415 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5416
5417 LogFlowFuncEnter();
5418
5419# ifndef VBOX_WITH_AUDIO_CALLBACKS
5420 /*
5421 * Stop the timer, if any.
5422 */
5423 hdaTimerMaybeStop(pThis);
5424# endif
5425
5426 /* See 6.2.1. */
5427 HDA_REG(pThis, GCAP) = HDA_MAKE_GCAP(HDA_MAX_SDO /* Ouput streams */,
5428 HDA_MAX_SDI /* Input streams */,
5429 0 /* Bidirectional output streams */,
5430 0 /* Serial data out signals */,
5431 1 /* 64-bit */);
5432 HDA_REG(pThis, VMIN) = 0x00; /* see 6.2.2 */
5433 HDA_REG(pThis, VMAJ) = 0x01; /* see 6.2.3 */
5434 /* Announce the full 60 words output payload. */
5435 HDA_REG(pThis, OUTPAY) = 0x003C; /* see 6.2.4 */
5436 /* Announce the full 29 words input payload. */
5437 HDA_REG(pThis, INPAY) = 0x001D; /* see 6.2.5 */
5438 HDA_REG(pThis, CORBSIZE) = 0x42; /* see 6.2.1 */
5439 HDA_REG(pThis, RIRBSIZE) = 0x42; /* see 6.2.1 */
5440 HDA_REG(pThis, CORBRP) = 0x0;
5441 HDA_REG(pThis, RIRBWP) = 0x0;
5442
5443 /*
5444 * Stop any audio currently playing and/or recording.
5445 */
5446 AudioMixerSinkCtl(pThis->SinkFront.pMixSink, AUDMIXSINKCMD_DISABLE);
5447# ifdef VBOX_WITH_HDA_MIC_IN
5448 AudioMixerSinkCtl(pThis->SinkMicIn.pMixSink, AUDMIXSINKCMD_DISABLE);
5449# endif
5450 AudioMixerSinkCtl(pThis->SinkLineIn.pMixSink, AUDMIXSINKCMD_DISABLE);
5451# ifdef VBOX_WITH_HDA_51_SURROUND
5452 AudioMixerSinkCtl(pThis->SinkCenterLFE.pMixSink, AUDMIXSINKCMD_DISABLE);
5453 AudioMixerSinkCtl(pThis->SinkRear.pMixSink, AUDMIXSINKCMD_DISABLE);
5454# endif
5455
5456 /*
5457 * Set some sensible defaults for which HDA sinks
5458 * are connected to which stream number.
5459 *
5460 * We use SD0 for input and SD4 for output by default.
5461 * These stream numbers can be changed by the guest dynamically lateron.
5462 */
5463#ifdef VBOX_WITH_HDA_MIC_IN
5464 hdaMixerSetStream(pThis, PDMAUDIOMIXERCTL_MIC_IN , 1 /* SD0 */, 0 /* Channel */);
5465#endif
5466 hdaMixerSetStream(pThis, PDMAUDIOMIXERCTL_LINE_IN , 1 /* SD0 */, 0 /* Channel */);
5467
5468 hdaMixerSetStream(pThis, PDMAUDIOMIXERCTL_FRONT , 5 /* SD4 */, 0 /* Channel */);
5469#ifdef VBOX_WITH_HDA_51_SURROUND
5470 hdaMixerSetStream(pThis, PDMAUDIOMIXERCTL_CENTER_LFE, 5 /* SD4 */, 0 /* Channel */);
5471 hdaMixerSetStream(pThis, PDMAUDIOMIXERCTL_REAR , 5 /* SD4 */, 0 /* Channel */);
5472#endif
5473
5474 pThis->cbCorbBuf = 256 * sizeof(uint32_t); /** @todo Use a define here. */
5475
5476 if (pThis->pu32CorbBuf)
5477 RT_BZERO(pThis->pu32CorbBuf, pThis->cbCorbBuf);
5478 else
5479 pThis->pu32CorbBuf = (uint32_t *)RTMemAllocZ(pThis->cbCorbBuf);
5480
5481 pThis->cbRirbBuf = 256 * sizeof(uint64_t); /** @todo Use a define here. */
5482 if (pThis->pu64RirbBuf)
5483 RT_BZERO(pThis->pu64RirbBuf, pThis->cbRirbBuf);
5484 else
5485 pThis->pu64RirbBuf = (uint64_t *)RTMemAllocZ(pThis->cbRirbBuf);
5486
5487 pThis->u64BaseTS = PDMDevHlpTMTimeVirtGetNano(pDevIns);
5488
5489 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
5490 {
5491 /* Remove the RUN bit from SDnCTL in case the stream was in a running state before. */
5492 HDA_STREAM_REG(pThis, CTL, i) &= ~HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN);
5493 hdaStreamReset(pThis, &pThis->aStreams[i]);
5494 }
5495
5496 /* Clear stream tags <-> objects mapping table. */
5497 RT_ZERO(pThis->aTags);
5498
5499 /* Emulation of codec "wake up" (HDA spec 5.5.1 and 6.5). */
5500 HDA_REG(pThis, STATESTS) = 0x1;
5501
5502# ifndef VBOX_WITH_AUDIO_CALLBACKS
5503 hdaTimerMaybeStart(pThis);
5504# endif
5505
5506 LogFlowFuncLeave();
5507 LogRel(("HDA: Reset\n"));
5508}
5509
5510/**
5511 * @interface_method_impl{PDMDEVREG,pfnDestruct}
5512 */
5513static DECLCALLBACK(int) hdaDestruct(PPDMDEVINS pDevIns)
5514{
5515 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5516
5517 PHDADRIVER pDrv;
5518 while (!RTListIsEmpty(&pThis->lstDrv))
5519 {
5520 pDrv = RTListGetFirst(&pThis->lstDrv, HDADRIVER, Node);
5521
5522 RTListNodeRemove(&pDrv->Node);
5523 RTMemFree(pDrv);
5524 }
5525
5526 if (pThis->pCodec)
5527 {
5528 hdaCodecDestruct(pThis->pCodec);
5529
5530 RTMemFree(pThis->pCodec);
5531 pThis->pCodec = NULL;
5532 }
5533
5534 RTMemFree(pThis->pu32CorbBuf);
5535 pThis->pu32CorbBuf = NULL;
5536
5537 RTMemFree(pThis->pu64RirbBuf);
5538 pThis->pu64RirbBuf = NULL;
5539
5540 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
5541 hdaStreamDestroy(&pThis->aStreams[i]);
5542
5543 return VINF_SUCCESS;
5544}
5545
5546
5547/**
5548 * Attach command, internal version.
5549 *
5550 * This is called to let the device attach to a driver for a specified LUN
5551 * during runtime. This is not called during VM construction, the device
5552 * constructor has to attach to all the available drivers.
5553 *
5554 * @returns VBox status code.
5555 * @param pDevIns The device instance.
5556 * @param pDrv Driver to (re-)use for (re-)attaching to.
5557 * If NULL is specified, a new driver will be created and appended
5558 * to the driver list.
5559 * @param uLUN The logical unit which is being detached.
5560 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
5561 */
5562static int hdaAttachInternal(PPDMDEVINS pDevIns, PHDADRIVER pDrv, unsigned uLUN, uint32_t fFlags)
5563{
5564 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5565
5566 /*
5567 * Attach driver.
5568 */
5569 char *pszDesc = NULL;
5570 if (RTStrAPrintf(&pszDesc, "Audio driver port (HDA) for LUN#%u", uLUN) <= 0)
5571 AssertReleaseMsgReturn(pszDesc,
5572 ("Not enough memory for HDA driver port description of LUN #%u\n", uLUN),
5573 VERR_NO_MEMORY);
5574
5575 PPDMIBASE pDrvBase;
5576 int rc = PDMDevHlpDriverAttach(pDevIns, uLUN,
5577 &pThis->IBase, &pDrvBase, pszDesc);
5578 if (RT_SUCCESS(rc))
5579 {
5580 if (pDrv == NULL)
5581 pDrv = (PHDADRIVER)RTMemAllocZ(sizeof(HDADRIVER));
5582 if (pDrv)
5583 {
5584 pDrv->pDrvBase = pDrvBase;
5585 pDrv->pConnector = PDMIBASE_QUERY_INTERFACE(pDrvBase, PDMIAUDIOCONNECTOR);
5586 AssertMsg(pDrv->pConnector != NULL, ("Configuration error: LUN#%u has no host audio interface, rc=%Rrc\n", uLUN, rc));
5587 pDrv->pHDAState = pThis;
5588 pDrv->uLUN = uLUN;
5589
5590 /*
5591 * For now we always set the driver at LUN 0 as our primary
5592 * host backend. This might change in the future.
5593 */
5594 if (pDrv->uLUN == 0)
5595 pDrv->Flags |= PDMAUDIODRVFLAG_PRIMARY;
5596
5597 LogFunc(("LUN#%u: pCon=%p, drvFlags=0x%x\n", uLUN, pDrv->pConnector, pDrv->Flags));
5598
5599 /* Attach to driver list if not attached yet. */
5600 if (!pDrv->fAttached)
5601 {
5602 RTListAppend(&pThis->lstDrv, &pDrv->Node);
5603 pDrv->fAttached = true;
5604 }
5605 }
5606 else
5607 rc = VERR_NO_MEMORY;
5608 }
5609 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
5610 LogFunc(("No attached driver for LUN #%u\n", uLUN));
5611
5612 if (RT_FAILURE(rc))
5613 {
5614 /* Only free this string on failure;
5615 * must remain valid for the live of the driver instance. */
5616 RTStrFree(pszDesc);
5617 }
5618
5619 LogFunc(("uLUN=%u, fFlags=0x%x, rc=%Rrc\n", uLUN, fFlags, rc));
5620 return rc;
5621}
5622
5623/**
5624 * Attach command.
5625 *
5626 * This is called to let the device attach to a driver for a specified LUN
5627 * during runtime. This is not called during VM construction, the device
5628 * constructor has to attach to all the available drivers.
5629 *
5630 * @returns VBox status code.
5631 * @param pDevIns The device instance.
5632 * @param uLUN The logical unit which is being detached.
5633 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
5634 */
5635static DECLCALLBACK(int) hdaAttach(PPDMDEVINS pDevIns, unsigned uLUN, uint32_t fFlags)
5636{
5637 return hdaAttachInternal(pDevIns, NULL /* pDrv */, uLUN, fFlags);
5638}
5639
5640static DECLCALLBACK(void) hdaDetach(PPDMDEVINS pDevIns, unsigned uLUN, uint32_t fFlags)
5641{
5642 LogFunc(("iLUN=%u, fFlags=0x%x\n", uLUN, fFlags));
5643}
5644
5645/**
5646 * Powers off the device.
5647 *
5648 * @param pDevIns Device instance to power off.
5649 */
5650static DECLCALLBACK(void) hdaPowerOff(PPDMDEVINS pDevIns)
5651{
5652 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5653
5654 LogRel2(("HDA: Powering off ...\n"));
5655
5656 /* Ditto goes for the codec, which in turn uses the mixer. */
5657 hdaCodecPowerOff(pThis->pCodec);
5658
5659 /**
5660 * Note: Destroy the mixer while powering off and *not* in hdaDestruct,
5661 * giving the mixer the chance to release any references held to
5662 * PDM audio streams it maintains.
5663 */
5664 if (pThis->pMixer)
5665 {
5666 AudioMixerDestroy(pThis->pMixer);
5667 pThis->pMixer = NULL;
5668 }
5669}
5670
5671/**
5672 * Re-attaches a new driver to the device's driver chain.
5673 *
5674 * @returns VBox status code.
5675 * @param pThis Device instance to re-attach driver to.
5676 * @param pDrv Driver instance used for attaching to.
5677 * If NULL is specified, a new driver will be created and appended
5678 * to the driver list.
5679 * @param uLUN The logical unit which is being re-detached.
5680 * @param pszDriver Driver name.
5681 */
5682static int hdaReattach(PHDASTATE pThis, PHDADRIVER pDrv, uint8_t uLUN, const char *pszDriver)
5683{
5684 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
5685 AssertPtrReturn(pszDriver, VERR_INVALID_POINTER);
5686
5687 PVM pVM = PDMDevHlpGetVM(pThis->pDevInsR3);
5688 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
5689 PCFGMNODE pDev0 = CFGMR3GetChild(pRoot, "Devices/hda/0/");
5690
5691 /* Remove LUN branch. */
5692 CFGMR3RemoveNode(CFGMR3GetChildF(pDev0, "LUN#%u/", uLUN));
5693
5694 if (pDrv)
5695 {
5696 /* Re-use a driver instance => detach the driver before. */
5697 int rc = PDMDevHlpDriverDetach(pThis->pDevInsR3, PDMIBASE_2_PDMDRV(pDrv->pDrvBase), 0 /* fFlags */);
5698 if (RT_FAILURE(rc))
5699 return rc;
5700 }
5701
5702#define RC_CHECK() if (RT_FAILURE(rc)) { AssertReleaseRC(rc); break; }
5703
5704 int rc = VINF_SUCCESS;
5705 do
5706 {
5707 PCFGMNODE pLunL0;
5708 rc = CFGMR3InsertNodeF(pDev0, &pLunL0, "LUN#%u/", uLUN); RC_CHECK();
5709 rc = CFGMR3InsertString(pLunL0, "Driver", "AUDIO"); RC_CHECK();
5710 rc = CFGMR3InsertNode(pLunL0, "Config/", NULL); RC_CHECK();
5711
5712 PCFGMNODE pLunL1, pLunL2;
5713 rc = CFGMR3InsertNode (pLunL0, "AttachedDriver/", &pLunL1); RC_CHECK();
5714 rc = CFGMR3InsertNode (pLunL1, "Config/", &pLunL2); RC_CHECK();
5715 rc = CFGMR3InsertString(pLunL1, "Driver", pszDriver); RC_CHECK();
5716
5717 rc = CFGMR3InsertString(pLunL2, "AudioDriver", pszDriver); RC_CHECK();
5718
5719 } while (0);
5720
5721 if (RT_SUCCESS(rc))
5722 rc = hdaAttachInternal(pThis->pDevInsR3, pDrv, uLUN, 0 /* fFlags */);
5723
5724 LogFunc(("pThis=%p, uLUN=%u, pszDriver=%s, rc=%Rrc\n", pThis, uLUN, pszDriver, rc));
5725
5726#undef RC_CHECK
5727
5728 return rc;
5729}
5730
5731/**
5732 * @interface_method_impl{PDMDEVREG,pfnConstruct}
5733 */
5734static DECLCALLBACK(int) hdaConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
5735{
5736 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
5737 Assert(iInstance == 0);
5738 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
5739
5740 /*
5741 * Validations.
5742 */
5743 if (!CFGMR3AreValuesValid(pCfg, "R0Enabled\0"
5744 "RCEnabled\0"
5745 "TimerHz\0"))
5746 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
5747 N_ ("Invalid configuration for the Intel HDA device"));
5748
5749 int rc = CFGMR3QueryBoolDef(pCfg, "RCEnabled", &pThis->fRCEnabled, false);
5750 if (RT_FAILURE(rc))
5751 return PDMDEV_SET_ERROR(pDevIns, rc,
5752 N_("HDA configuration error: failed to read RCEnabled as boolean"));
5753 rc = CFGMR3QueryBoolDef(pCfg, "R0Enabled", &pThis->fR0Enabled, false);
5754 if (RT_FAILURE(rc))
5755 return PDMDEV_SET_ERROR(pDevIns, rc,
5756 N_("HDA configuration error: failed to read R0Enabled as boolean"));
5757#ifndef VBOX_WITH_AUDIO_CALLBACKS
5758 uint16_t uTimerHz;
5759 rc = CFGMR3QueryU16Def(pCfg, "TimerHz", &uTimerHz, 200 /* Hz */);
5760 if (RT_FAILURE(rc))
5761 return PDMDEV_SET_ERROR(pDevIns, rc,
5762 N_("HDA configuration error: failed to read Hertz (Hz) rate as unsigned integer"));
5763#endif
5764
5765 /*
5766 * Initialize data (most of it anyway).
5767 */
5768 pThis->pDevInsR3 = pDevIns;
5769 pThis->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
5770 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
5771 /* IBase */
5772 pThis->IBase.pfnQueryInterface = hdaQueryInterface;
5773
5774 /* PCI Device */
5775 PCIDevSetVendorId (&pThis->PciDev, HDA_PCI_VENDOR_ID); /* nVidia */
5776 PCIDevSetDeviceId (&pThis->PciDev, HDA_PCI_DEVICE_ID); /* HDA */
5777
5778 PCIDevSetCommand (&pThis->PciDev, 0x0000); /* 04 rw,ro - pcicmd. */
5779 PCIDevSetStatus (&pThis->PciDev, VBOX_PCI_STATUS_CAP_LIST); /* 06 rwc?,ro? - pcists. */
5780 PCIDevSetRevisionId (&pThis->PciDev, 0x01); /* 08 ro - rid. */
5781 PCIDevSetClassProg (&pThis->PciDev, 0x00); /* 09 ro - pi. */
5782 PCIDevSetClassSub (&pThis->PciDev, 0x03); /* 0a ro - scc; 03 == HDA. */
5783 PCIDevSetClassBase (&pThis->PciDev, 0x04); /* 0b ro - bcc; 04 == multimedia. */
5784 PCIDevSetHeaderType (&pThis->PciDev, 0x00); /* 0e ro - headtyp. */
5785 PCIDevSetBaseAddress (&pThis->PciDev, 0, /* 10 rw - MMIO */
5786 false /* fIoSpace */, false /* fPrefetchable */, true /* f64Bit */, 0x00000000);
5787 PCIDevSetInterruptLine (&pThis->PciDev, 0x00); /* 3c rw. */
5788 PCIDevSetInterruptPin (&pThis->PciDev, 0x01); /* 3d ro - INTA#. */
5789
5790#if defined(HDA_AS_PCI_EXPRESS)
5791 PCIDevSetCapabilityList (&pThis->PciDev, 0x80);
5792#elif defined(VBOX_WITH_MSI_DEVICES)
5793 PCIDevSetCapabilityList (&pThis->PciDev, 0x60);
5794#else
5795 PCIDevSetCapabilityList (&pThis->PciDev, 0x50); /* ICH6 datasheet 18.1.16 */
5796#endif
5797
5798 /// @todo r=michaln: If there are really no PCIDevSetXx for these, the meaning
5799 /// of these values needs to be properly documented!
5800 /* HDCTL off 0x40 bit 0 selects signaling mode (1-HDA, 0 - Ac97) 18.1.19 */
5801 PCIDevSetByte(&pThis->PciDev, 0x40, 0x01);
5802
5803 /* Power Management */
5804 PCIDevSetByte(&pThis->PciDev, 0x50 + 0, VBOX_PCI_CAP_ID_PM);
5805 PCIDevSetByte(&pThis->PciDev, 0x50 + 1, 0x0); /* next */
5806 PCIDevSetWord(&pThis->PciDev, 0x50 + 2, VBOX_PCI_PM_CAP_DSI | 0x02 /* version, PM1.1 */ );
5807
5808#ifdef HDA_AS_PCI_EXPRESS
5809 /* PCI Express */
5810 PCIDevSetByte(&pThis->PciDev, 0x80 + 0, VBOX_PCI_CAP_ID_EXP); /* PCI_Express */
5811 PCIDevSetByte(&pThis->PciDev, 0x80 + 1, 0x60); /* next */
5812 /* Device flags */
5813 PCIDevSetWord(&pThis->PciDev, 0x80 + 2,
5814 /* version */ 0x1 |
5815 /* Root Complex Integrated Endpoint */ (VBOX_PCI_EXP_TYPE_ROOT_INT_EP << 4) |
5816 /* MSI */ (100) << 9 );
5817 /* Device capabilities */
5818 PCIDevSetDWord(&pThis->PciDev, 0x80 + 4, VBOX_PCI_EXP_DEVCAP_FLRESET);
5819 /* Device control */
5820 PCIDevSetWord( &pThis->PciDev, 0x80 + 8, 0);
5821 /* Device status */
5822 PCIDevSetWord( &pThis->PciDev, 0x80 + 10, 0);
5823 /* Link caps */
5824 PCIDevSetDWord(&pThis->PciDev, 0x80 + 12, 0);
5825 /* Link control */
5826 PCIDevSetWord( &pThis->PciDev, 0x80 + 16, 0);
5827 /* Link status */
5828 PCIDevSetWord( &pThis->PciDev, 0x80 + 18, 0);
5829 /* Slot capabilities */
5830 PCIDevSetDWord(&pThis->PciDev, 0x80 + 20, 0);
5831 /* Slot control */
5832 PCIDevSetWord( &pThis->PciDev, 0x80 + 24, 0);
5833 /* Slot status */
5834 PCIDevSetWord( &pThis->PciDev, 0x80 + 26, 0);
5835 /* Root control */
5836 PCIDevSetWord( &pThis->PciDev, 0x80 + 28, 0);
5837 /* Root capabilities */
5838 PCIDevSetWord( &pThis->PciDev, 0x80 + 30, 0);
5839 /* Root status */
5840 PCIDevSetDWord(&pThis->PciDev, 0x80 + 32, 0);
5841 /* Device capabilities 2 */
5842 PCIDevSetDWord(&pThis->PciDev, 0x80 + 36, 0);
5843 /* Device control 2 */
5844 PCIDevSetQWord(&pThis->PciDev, 0x80 + 40, 0);
5845 /* Link control 2 */
5846 PCIDevSetQWord(&pThis->PciDev, 0x80 + 48, 0);
5847 /* Slot control 2 */
5848 PCIDevSetWord( &pThis->PciDev, 0x80 + 56, 0);
5849#endif
5850
5851 /*
5852 * Register the PCI device.
5853 */
5854 rc = PDMDevHlpPCIRegister(pDevIns, &pThis->PciDev);
5855 if (RT_FAILURE(rc))
5856 return rc;
5857
5858 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 0, 0x4000, PCI_ADDRESS_SPACE_MEM, hdaPciIoRegionMap);
5859 if (RT_FAILURE(rc))
5860 return rc;
5861
5862#ifdef VBOX_WITH_MSI_DEVICES
5863 PDMMSIREG MsiReg;
5864 RT_ZERO(MsiReg);
5865 MsiReg.cMsiVectors = 1;
5866 MsiReg.iMsiCapOffset = 0x60;
5867 MsiReg.iMsiNextOffset = 0x50;
5868 rc = PDMDevHlpPCIRegisterMsi(pDevIns, &MsiReg);
5869 if (RT_FAILURE(rc))
5870 {
5871 /* That's OK, we can work without MSI */
5872 PCIDevSetCapabilityList(&pThis->PciDev, 0x50);
5873 }
5874#endif
5875
5876 rc = PDMDevHlpSSMRegister(pDevIns, HDA_SSM_VERSION, sizeof(*pThis), hdaSaveExec, hdaLoadExec);
5877 if (RT_FAILURE(rc))
5878 return rc;
5879
5880 RTListInit(&pThis->lstDrv);
5881
5882 uint8_t uLUN;
5883 for (uLUN = 0; uLUN < UINT8_MAX; ++uLUN)
5884 {
5885 LogFunc(("Trying to attach driver for LUN #%RU32 ...\n", uLUN));
5886 rc = hdaAttachInternal(pDevIns, NULL /* pDrv */, uLUN, 0 /* fFlags */);
5887 if (RT_FAILURE(rc))
5888 {
5889 if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
5890 rc = VINF_SUCCESS;
5891 else if (rc == VERR_AUDIO_BACKEND_INIT_FAILED)
5892 {
5893 hdaReattach(pThis, NULL /* pDrv */, uLUN, "NullAudio");
5894 PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "HostAudioNotResponding",
5895 N_("No audio devices could be opened. Selecting the NULL audio backend "
5896 "with the consequence that no sound is audible"));
5897 /* attaching to the NULL audio backend will never fail */
5898 rc = VINF_SUCCESS;
5899 }
5900 break;
5901 }
5902 }
5903
5904 LogFunc(("cLUNs=%RU8, rc=%Rrc\n", uLUN, rc));
5905
5906 if (RT_SUCCESS(rc))
5907 {
5908 rc = AudioMixerCreate("HDA Mixer", 0 /* uFlags */, &pThis->pMixer);
5909 if (RT_SUCCESS(rc))
5910 {
5911 /* Set a default audio format for our mixer. */
5912 PDMAUDIOSTREAMCFG streamCfg;
5913 streamCfg.uHz = 44100;
5914 streamCfg.cChannels = 2;
5915 streamCfg.enmFormat = PDMAUDIOFMT_S16;
5916 streamCfg.enmEndianness = PDMAUDIOHOSTENDIANNESS;
5917
5918 rc = AudioMixerSetDeviceFormat(pThis->pMixer, &streamCfg);
5919 AssertRC(rc);
5920
5921 /*
5922 * Add mixer output sinks.
5923 */
5924#ifdef VBOX_WITH_HDA_51_SURROUND
5925 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] Front",
5926 AUDMIXSINKDIR_OUTPUT, &pThis->SinkFront.pMixSink);
5927 AssertRC(rc);
5928 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] Center / Subwoofer",
5929 AUDMIXSINKDIR_OUTPUT, &pThis->SinkCenterLFE.pMixSink);
5930 AssertRC(rc);
5931 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] Rear",
5932 AUDMIXSINKDIR_OUTPUT, &pThis->SinkRear.pMixSink);
5933 AssertRC(rc);
5934#else
5935 rc = AudioMixerCreateSink(pThis->pMixer, "[Playback] PCM Output",
5936 AUDMIXSINKDIR_OUTPUT, &pThis->SinkFront.pMixSink);
5937 AssertRC(rc);
5938#endif
5939 /*
5940 * Add mixer input sinks.
5941 */
5942 rc = AudioMixerCreateSink(pThis->pMixer, "[Recording] Line In",
5943 AUDMIXSINKDIR_INPUT, &pThis->SinkLineIn.pMixSink);
5944 AssertRC(rc);
5945#ifdef VBOX_WITH_HDA_MIC_IN
5946 rc = AudioMixerCreateSink(pThis->pMixer, "[Recording] Microphone In",
5947 AUDMIXSINKDIR_INPUT, &pThis->SinkMicIn.pMixSink);
5948 AssertRC(rc);
5949#endif
5950 /* There is no master volume control. Set the master to max. */
5951 PDMAUDIOVOLUME vol = { false, 255, 255 };
5952 rc = AudioMixerSetMasterVolume(pThis->pMixer, &vol);
5953 AssertRC(rc);
5954 }
5955 }
5956
5957 if (RT_SUCCESS(rc))
5958 {
5959 /* Construct codec. */
5960 pThis->pCodec = (PHDACODEC)RTMemAllocZ(sizeof(HDACODEC));
5961 if (!pThis->pCodec)
5962 return PDMDEV_SET_ERROR(pDevIns, VERR_NO_MEMORY, N_("Out of memory allocating HDA codec state"));
5963
5964 /* Set codec callbacks. */
5965 pThis->pCodec->pfnMixerAddStream = hdaMixerAddStream;
5966 pThis->pCodec->pfnMixerRemoveStream = hdaMixerRemoveStream;
5967 pThis->pCodec->pfnMixerSetStream = hdaMixerSetStream;
5968 pThis->pCodec->pfnMixerSetVolume = hdaMixerSetVolume;
5969 pThis->pCodec->pfnReset = hdaCodecReset;
5970
5971 pThis->pCodec->pHDAState = pThis; /* Assign HDA controller state to codec. */
5972
5973 /* Construct the codec. */
5974 rc = hdaCodecConstruct(pDevIns, pThis->pCodec, 0 /* Codec index */, pCfg);
5975 if (RT_FAILURE(rc))
5976 AssertRCReturn(rc, rc);
5977
5978 /* ICH6 datasheet defines 0 values for SVID and SID (18.1.14-15), which together with values returned for
5979 verb F20 should provide device/codec recognition. */
5980 Assert(pThis->pCodec->u16VendorId);
5981 Assert(pThis->pCodec->u16DeviceId);
5982 PCIDevSetSubSystemVendorId(&pThis->PciDev, pThis->pCodec->u16VendorId); /* 2c ro - intel.) */
5983 PCIDevSetSubSystemId( &pThis->PciDev, pThis->pCodec->u16DeviceId); /* 2e ro. */
5984 }
5985
5986 if (RT_SUCCESS(rc))
5987 {
5988 /*
5989 * Create all hardware streams.
5990 */
5991 for (uint8_t i = 0; i < HDA_MAX_STREAMS; i++)
5992 {
5993 rc = hdaStreamCreate(&pThis->aStreams[i], i /* uSD */);
5994 AssertRC(rc);
5995 }
5996
5997 /*
5998 * Initialize the driver chain.
5999 */
6000 PHDADRIVER pDrv;
6001 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
6002 {
6003 /*
6004 * Only primary drivers are critical for the VM to run. Everything else
6005 * might not worth showing an own error message box in the GUI.
6006 */
6007 if (!(pDrv->Flags & PDMAUDIODRVFLAG_PRIMARY))
6008 continue;
6009
6010 PPDMIAUDIOCONNECTOR pCon = pDrv->pConnector;
6011 AssertPtr(pCon);
6012
6013 bool fValidLineIn = AudioMixerStreamIsValid(pDrv->LineIn.pMixStrm);
6014#ifdef VBOX_WITH_HDA_MIC_IN
6015 bool fValidMicIn = AudioMixerStreamIsValid(pDrv->MicIn.pMixStrm);
6016#endif
6017 bool fValidOut = AudioMixerStreamIsValid(pDrv->Front.pMixStrm);
6018#ifdef VBOX_WITH_HDA_51_SURROUND
6019 /** @todo Anything to do here? */
6020#endif
6021
6022 if ( !fValidLineIn
6023#ifdef VBOX_WITH_HDA_MIC_IN
6024 && !fValidMicIn
6025#endif
6026 && !fValidOut)
6027 {
6028 LogRel(("HDA: Falling back to NULL backend (no sound audible)\n"));
6029
6030 hdaReset(pDevIns);
6031 hdaReattach(pThis, pDrv, pDrv->uLUN, "NullAudio");
6032
6033 PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "HostAudioNotResponding",
6034 N_("No audio devices could be opened. Selecting the NULL audio backend "
6035 "with the consequence that no sound is audible"));
6036 }
6037 else
6038 {
6039 bool fWarn = false;
6040
6041 PDMAUDIOBACKENDCFG backendCfg;
6042 int rc2 = pCon->pfnGetConfig(pCon, &backendCfg);
6043 if (RT_SUCCESS(rc2))
6044 {
6045 if (backendCfg.cSources)
6046 {
6047#ifdef VBOX_WITH_HDA_MIC_IN
6048 /* If the audio backend supports two or more input streams at once,
6049 * warn if one of our two inputs (microphone-in and line-in) failed to initialize. */
6050 if (backendCfg.cMaxStreamsIn >= 2)
6051 fWarn = !fValidLineIn || !fValidMicIn;
6052 /* If the audio backend only supports one input stream at once (e.g. pure ALSA, and
6053 * *not* ALSA via PulseAudio plugin!), only warn if both of our inputs failed to initialize.
6054 * One of the two simply is not in use then. */
6055 else if (backendCfg.cMaxStreamsIn == 1)
6056 fWarn = !fValidLineIn && !fValidMicIn;
6057 /* Don't warn if our backend is not able of supporting any input streams at all. */
6058#else
6059 /* We only have line-in as input source. */
6060 fWarn = !fValidLineIn;
6061#endif
6062 }
6063
6064 if ( !fWarn
6065 && backendCfg.cSinks)
6066 {
6067 fWarn = !fValidOut;
6068 }
6069 }
6070 else
6071 {
6072 LogRel(("HDA: Unable to retrieve audio backend configuration for LUN #%RU8, rc=%Rrc\n", pDrv->uLUN, rc2));
6073 fWarn = true;
6074 }
6075
6076 if (fWarn)
6077 {
6078 char szMissingStreams[255];
6079 size_t len = 0;
6080 if (!fValidLineIn)
6081 {
6082 LogRel(("HDA: WARNING: Unable to open PCM line input for LUN #%RU8!\n", pDrv->uLUN));
6083 len = RTStrPrintf(szMissingStreams, sizeof(szMissingStreams), "PCM Input");
6084 }
6085#ifdef VBOX_WITH_HDA_MIC_IN
6086 if (!fValidMicIn)
6087 {
6088 LogRel(("HDA: WARNING: Unable to open PCM microphone input for LUN #%RU8!\n", pDrv->uLUN));
6089 len += RTStrPrintf(szMissingStreams + len,
6090 sizeof(szMissingStreams) - len, len ? ", PCM Microphone" : "PCM Microphone");
6091 }
6092#endif
6093 if (!fValidOut)
6094 {
6095 LogRel(("HDA: WARNING: Unable to open PCM output for LUN #%RU8!\n", pDrv->uLUN));
6096 len += RTStrPrintf(szMissingStreams + len,
6097 sizeof(szMissingStreams) - len, len ? ", PCM Output" : "PCM Output");
6098 }
6099
6100 PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "HostAudioNotResponding",
6101 N_("Some HDA audio streams (%s) could not be opened. Guest applications generating audio "
6102 "output or depending on audio input may hang. Make sure your host audio device "
6103 "is working properly. Check the logfile for error messages of the audio "
6104 "subsystem"), szMissingStreams);
6105 }
6106 }
6107 }
6108 }
6109
6110 if (RT_SUCCESS(rc))
6111 {
6112 hdaReset(pDevIns);
6113
6114 /*
6115 * 18.2.6,7 defines that values of this registers might be cleared on power on/reset
6116 * hdaReset shouldn't affects these registers.
6117 */
6118 HDA_REG(pThis, WAKEEN) = 0x0;
6119 HDA_REG(pThis, STATESTS) = 0x0;
6120
6121#ifdef DEBUG
6122 /*
6123 * Debug and string formatter types.
6124 */
6125 PDMDevHlpDBGFInfoRegister(pDevIns, "hda", "HDA info. (hda [register case-insensitive])", hdaDbgInfo);
6126 PDMDevHlpDBGFInfoRegister(pDevIns, "hdabdle", "HDA stream BDLE info. (hdabdle [stream number])", hdaDbgInfoBDLE);
6127 PDMDevHlpDBGFInfoRegister(pDevIns, "hdastrm", "HDA stream info. (hdastrm [stream number])", hdaDbgInfoStream);
6128 PDMDevHlpDBGFInfoRegister(pDevIns, "hdcnodes", "HDA codec nodes.", hdaDbgInfoCodecNodes);
6129 PDMDevHlpDBGFInfoRegister(pDevIns, "hdcselector", "HDA codec's selector states [node number].", hdaDbgInfoCodecSelector);
6130 PDMDevHlpDBGFInfoRegister(pDevIns, "hdamixer", "HDA mixer state.", hdaDbgInfoMixer);
6131
6132 rc = RTStrFormatTypeRegister("bdle", hdaDbgFmtBDLE, NULL);
6133 AssertRC(rc);
6134 rc = RTStrFormatTypeRegister("sdctl", hdaDbgFmtSDCTL, NULL);
6135 AssertRC(rc);
6136 rc = RTStrFormatTypeRegister("sdsts", hdaDbgFmtSDSTS, NULL);
6137 AssertRC(rc);
6138 rc = RTStrFormatTypeRegister("sdfifos", hdaDbgFmtSDFIFOS, NULL);
6139 AssertRC(rc);
6140 rc = RTStrFormatTypeRegister("sdfifow", hdaDbgFmtSDFIFOW, NULL);
6141 AssertRC(rc);
6142#endif /* DEBUG */
6143
6144 /*
6145 * Some debug assertions.
6146 */
6147 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegMap); i++)
6148 {
6149 struct HDAREGDESC const *pReg = &g_aHdaRegMap[i];
6150 struct HDAREGDESC const *pNextReg = i + 1 < RT_ELEMENTS(g_aHdaRegMap) ? &g_aHdaRegMap[i + 1] : NULL;
6151
6152 /* binary search order. */
6153 AssertReleaseMsg(!pNextReg || pReg->offset + pReg->size <= pNextReg->offset,
6154 ("[%#x] = {%#x LB %#x} vs. [%#x] = {%#x LB %#x}\n",
6155 i, pReg->offset, pReg->size, i + 1, pNextReg->offset, pNextReg->size));
6156
6157 /* alignment. */
6158 AssertReleaseMsg( pReg->size == 1
6159 || (pReg->size == 2 && (pReg->offset & 1) == 0)
6160 || (pReg->size == 3 && (pReg->offset & 3) == 0)
6161 || (pReg->size == 4 && (pReg->offset & 3) == 0),
6162 ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
6163
6164 /* registers are packed into dwords - with 3 exceptions with gaps at the end of the dword. */
6165 AssertRelease(((pReg->offset + pReg->size) & 3) == 0 || pNextReg);
6166 if (pReg->offset & 3)
6167 {
6168 struct HDAREGDESC const *pPrevReg = i > 0 ? &g_aHdaRegMap[i - 1] : NULL;
6169 AssertReleaseMsg(pPrevReg, ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
6170 if (pPrevReg)
6171 AssertReleaseMsg(pPrevReg->offset + pPrevReg->size == pReg->offset,
6172 ("[%#x] = {%#x LB %#x} vs. [%#x] = {%#x LB %#x}\n",
6173 i - 1, pPrevReg->offset, pPrevReg->size, i + 1, pReg->offset, pReg->size));
6174 }
6175#if 0
6176 if ((pReg->offset + pReg->size) & 3)
6177 {
6178 AssertReleaseMsg(pNextReg, ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
6179 if (pNextReg)
6180 AssertReleaseMsg(pReg->offset + pReg->size == pNextReg->offset,
6181 ("[%#x] = {%#x LB %#x} vs. [%#x] = {%#x LB %#x}\n",
6182 i, pReg->offset, pReg->size, i + 1, pNextReg->offset, pNextReg->size));
6183 }
6184#endif
6185 /* The final entry is a full DWORD, no gaps! Allows shortcuts. */
6186 AssertReleaseMsg(pNextReg || ((pReg->offset + pReg->size) & 3) == 0,
6187 ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
6188 }
6189 }
6190
6191# ifndef VBOX_WITH_AUDIO_CALLBACKS
6192 if (RT_SUCCESS(rc))
6193 {
6194 /* Start the emulation timer. */
6195 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, hdaTimer, pThis,
6196 TMTIMER_FLAGS_NO_CRIT_SECT, "DevIchHda", &pThis->pTimer);
6197 AssertRCReturn(rc, rc);
6198
6199 if (RT_SUCCESS(rc))
6200 {
6201 pThis->cTimerTicks = TMTimerGetFreq(pThis->pTimer) / uTimerHz;
6202 pThis->uTimerTS = TMTimerGet(pThis->pTimer);
6203 LogFunc(("Timer ticks=%RU64 (%RU16 Hz)\n", pThis->cTimerTicks, uTimerHz));
6204
6205 hdaTimerMaybeStart(pThis);
6206 }
6207 }
6208# else
6209 if (RT_SUCCESS(rc))
6210 {
6211 PHDADRIVER pDrv;
6212 RTListForEach(&pThis->lstDrv, pDrv, HDADRIVER, Node)
6213 {
6214 /* Only register primary driver.
6215 * The device emulation does the output multiplexing then. */
6216 if (pDrv->Flags != PDMAUDIODRVFLAG_PRIMARY)
6217 continue;
6218
6219 PDMAUDIOCALLBACK AudioCallbacks[2];
6220
6221 HDACALLBACKCTX Ctx = { pThis, pDrv };
6222
6223 AudioCallbacks[0].enmType = PDMAUDIOCALLBACKTYPE_INPUT;
6224 AudioCallbacks[0].pfnCallback = hdaCallbackInput;
6225 AudioCallbacks[0].pvCtx = &Ctx;
6226 AudioCallbacks[0].cbCtx = sizeof(HDACALLBACKCTX);
6227
6228 AudioCallbacks[1].enmType = PDMAUDIOCALLBACKTYPE_OUTPUT;
6229 AudioCallbacks[1].pfnCallback = hdaCallbackOutput;
6230 AudioCallbacks[1].pvCtx = &Ctx;
6231 AudioCallbacks[1].cbCtx = sizeof(HDACALLBACKCTX);
6232
6233 rc = pDrv->pConnector->pfnRegisterCallbacks(pDrv->pConnector, AudioCallbacks, RT_ELEMENTS(AudioCallbacks));
6234 if (RT_FAILURE(rc))
6235 break;
6236 }
6237 }
6238# endif
6239
6240# ifdef VBOX_WITH_STATISTICS
6241 if (RT_SUCCESS(rc))
6242 {
6243 /*
6244 * Register statistics.
6245 */
6246# ifndef VBOX_WITH_AUDIO_CALLBACKS
6247 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTimer, STAMTYPE_PROFILE, "/Devices/HDA/Timer", STAMUNIT_TICKS_PER_CALL, "Profiling hdaTimer.");
6248# endif
6249 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, "/Devices/HDA/BytesRead" , STAMUNIT_BYTES, "Bytes read from HDA emulation.");
6250 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, "/Devices/HDA/BytesWritten", STAMUNIT_BYTES, "Bytes written to HDA emulation.");
6251 }
6252# endif
6253
6254 LogFlowFuncLeaveRC(rc);
6255 return rc;
6256}
6257
6258/**
6259 * The device registration structure.
6260 */
6261const PDMDEVREG g_DeviceICH6_HDA =
6262{
6263 /* u32Version */
6264 PDM_DEVREG_VERSION,
6265 /* szName */
6266 "hda",
6267 /* szRCMod */
6268 "VBoxDDRC.rc",
6269 /* szR0Mod */
6270 "VBoxDDR0.r0",
6271 /* pszDescription */
6272 "Intel HD Audio Controller",
6273 /* fFlags */
6274 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
6275 /* fClass */
6276 PDM_DEVREG_CLASS_AUDIO,
6277 /* cMaxInstances */
6278 1,
6279 /* cbInstance */
6280 sizeof(HDASTATE),
6281 /* pfnConstruct */
6282 hdaConstruct,
6283 /* pfnDestruct */
6284 hdaDestruct,
6285 /* pfnRelocate */
6286 NULL,
6287 /* pfnMemSetup */
6288 NULL,
6289 /* pfnPowerOn */
6290 NULL,
6291 /* pfnReset */
6292 hdaReset,
6293 /* pfnSuspend */
6294 NULL,
6295 /* pfnResume */
6296 NULL,
6297 /* pfnAttach */
6298 hdaAttach,
6299 /* pfnDetach */
6300 hdaDetach,
6301 /* pfnQueryInterface. */
6302 NULL,
6303 /* pfnInitComplete */
6304 NULL,
6305 /* pfnPowerOff */
6306 hdaPowerOff,
6307 /* pfnSoftReset */
6308 NULL,
6309 /* u32VersionEnd */
6310 PDM_DEVREG_VERSION
6311};
6312
6313#endif /* IN_RING3 */
6314#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