VirtualBox

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

Last change on this file since 44717 was 44717, checked in by vboxsync, 12 years ago

DevIchHda.cpp: Du'h!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 127.0 KB
Line 
1/* $Id: DevIchHda.cpp 44717 2013-02-15 15:28:07Z 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-2013 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* Header Files *
24*******************************************************************************/
25#define LOG_GROUP LOG_GROUP_DEV_AUDIO
26#include <VBox/vmm/pdmdev.h>
27#include <VBox/version.h>
28
29#include <iprt/assert.h>
30#include <iprt/uuid.h>
31#include <iprt/string.h>
32#include <iprt/mem.h>
33#include <iprt/asm.h>
34#include <iprt/asm-math.h>
35
36#include "VBoxDD.h"
37
38extern "C" {
39#include "audio.h"
40}
41#include "DevIchHdaCodec.h"
42
43
44/*******************************************************************************
45* Defined Constants And Macros *
46*******************************************************************************/
47//#define HDA_AS_PCI_EXPRESS
48#define VBOX_WITH_INTEL_HDA
49
50#if defined(VBOX_WITH_HP_HDA)
51/* HP Pavilion dv4t-1300 */
52# define HDA_PCI_VENDOR_ID 0x103c
53# define HDA_PCI_DEICE_ID 0x30f7
54#elif defined(VBOX_WITH_INTEL_HDA)
55/* Intel HDA controller */
56# define HDA_PCI_VENDOR_ID 0x8086
57# define HDA_PCI_DEICE_ID 0x2668
58#elif defined(VBOX_WITH_NVIDIA_HDA)
59/* nVidia HDA controller */
60# define HDA_PCI_VENDOR_ID 0x10de
61# define HDA_PCI_DEICE_ID 0x0ac0
62#else
63# error "Please specify your HDA device vendor/device IDs"
64#endif
65
66/** @todo r=bird: Looking at what the linux driver (accidentally?) does when
67 * updating CORBWP, I belive that the ICH6 datahsheet is wrong and that CORBRP
68 * is read only except for bit 15 like the HDA spec states.
69 *
70 * Btw. the CORBRPRST implementation is incomplete according to both docs (sw
71 * writes 1, hw sets it to 1 (after completion), sw reads 1, sw writes 0). */
72#define BIRD_THINKS_CORBRP_IS_MOSTLY_RO
73
74#define HDA_NREGS 112
75/* Registers */
76#define HDA_REG_IND_NAME(x) ICH6_HDA_REG_##x
77#define HDA_REG_FIELD_NAME(reg, x) ICH6_HDA_##reg##_##x
78#define HDA_REG_FIELD_MASK(reg, x) ICH6_HDA_##reg##_##x##_MASK
79#define HDA_REG_FIELD_FLAG_MASK(reg, x) RT_BIT(ICH6_HDA_##reg##_##x##_SHIFT)
80#define HDA_REG_FIELD_SHIFT(reg, x) ICH6_HDA_##reg##_##x##_SHIFT
81#define HDA_REG_IND(pThis, x) ((pThis)->au32Regs[(x)])
82#define HDA_REG(pThis, x) (HDA_REG_IND((pThis), HDA_REG_IND_NAME(x)))
83#define HDA_REG_VALUE(pThis, reg, val) (HDA_REG((pThis),reg) & (((HDA_REG_FIELD_MASK(reg, val))) << (HDA_REG_FIELD_SHIFT(reg, val))))
84#define HDA_REG_FLAG_VALUE(pThis, reg, val) (HDA_REG((pThis),reg) & (((HDA_REG_FIELD_FLAG_MASK(reg, val)))))
85#define HDA_REG_SVALUE(pThis, reg, val) (HDA_REG_VALUE(pThis, reg, val) >> (HDA_REG_FIELD_SHIFT(reg, val)))
86
87#define ICH6_HDA_REG_GCAP 0 /* range 0x00-0x01*/
88#define GCAP(pThis) (HDA_REG((pThis), GCAP))
89/* GCAP HDASpec 3.3.2 This macro encodes the following information about HDA in a compact manner:
90 * oss (15:12) - number of output streams supported
91 * iss (11:8) - number of input streams supported
92 * bss (7:3) - number of bidirectional streams supported
93 * bds (2:1) - number of serial data out signals supported
94 * b64sup (0) - 64 bit addressing supported.
95 */
96#define HDA_MAKE_GCAP(oss, iss, bss, bds, b64sup) \
97 ( (((oss) & 0xF) << 12) \
98 | (((iss) & 0xF) << 8) \
99 | (((bss) & 0x1F) << 3) \
100 | (((bds) & 0x3) << 2) \
101 | ((b64sup) & 1))
102#define ICH6_HDA_REG_VMIN 1 /* range 0x02 */
103#define VMIN(pThis) (HDA_REG((pThis), VMIN))
104
105#define ICH6_HDA_REG_VMAJ 2 /* range 0x03 */
106#define VMAJ(pThis) (HDA_REG((pThis), VMAJ))
107
108#define ICH6_HDA_REG_OUTPAY 3 /* range 0x04-0x05 */
109#define OUTPAY(pThis) (HDA_REG((pThis), OUTPAY))
110
111#define ICH6_HDA_REG_INPAY 4 /* range 0x06-0x07 */
112#define INPAY(pThis) (HDA_REG((pThis), INPAY))
113
114#define ICH6_HDA_REG_GCTL (5)
115#define ICH6_HDA_GCTL_RST_SHIFT (0)
116#define ICH6_HDA_GCTL_FSH_SHIFT (1)
117#define ICH6_HDA_GCTL_UR_SHIFT (8)
118#define GCTL(pThis) (HDA_REG((pThis), GCTL))
119
120#define ICH6_HDA_REG_WAKEEN 6 /* 0x0C */
121#define WAKEEN(pThis) (HDA_REG((pThis), WAKEEN))
122
123#define ICH6_HDA_REG_STATESTS 7 /* range 0x0E */
124#define STATESTS(pThis) (HDA_REG((pThis), STATESTS))
125#define ICH6_HDA_STATES_SCSF 0x7
126
127#define ICH6_HDA_REG_GSTS 8 /* range 0x10-0x11*/
128#define ICH6_HDA_GSTS_FSH_SHIFT (1)
129#define GSTS(pThis) (HDA_REG(pThis, GSTS))
130
131#define ICH6_HDA_REG_INTCTL 9 /* 0x20 */
132#define ICH6_HDA_INTCTL_GIE_SHIFT 31
133#define ICH6_HDA_INTCTL_CIE_SHIFT 30
134#define ICH6_HDA_INTCTL_S0_SHIFT (0)
135#define ICH6_HDA_INTCTL_S1_SHIFT (1)
136#define ICH6_HDA_INTCTL_S2_SHIFT (2)
137#define ICH6_HDA_INTCTL_S3_SHIFT (3)
138#define ICH6_HDA_INTCTL_S4_SHIFT (4)
139#define ICH6_HDA_INTCTL_S5_SHIFT (5)
140#define ICH6_HDA_INTCTL_S6_SHIFT (6)
141#define ICH6_HDA_INTCTL_S7_SHIFT (7)
142#define INTCTL(pThis) (HDA_REG((pThis), INTCTL))
143#define INTCTL_GIE(pThis) (HDA_REG_FLAG_VALUE(pThis, INTCTL, GIE))
144#define INTCTL_CIE(pThis) (HDA_REG_FLAG_VALUE(pThis, INTCTL, CIE))
145#define INTCTL_SX(pThis, X) (HDA_REG_FLAG_VALUE((pThis), INTCTL, S##X))
146#define INTCTL_SALL(pThis) (INTCTL((pThis)) & 0xFF)
147
148/* Note: The HDA specification defines a SSYNC register at offset 0x38. The
149 * ICH6/ICH9 datahseet defines SSYNC at offset 0x34. The Linux HDA driver matches
150 * the datasheet.
151 */
152#define ICH6_HDA_REG_SSYNC 12 /* 0x34 */
153#define SSYNC(pThis) (HDA_REG((pThis), SSYNC))
154
155#define ICH6_HDA_REG_INTSTS 10 /* 0x24 */
156#define ICH6_HDA_INTSTS_GIS_SHIFT (31)
157#define ICH6_HDA_INTSTS_CIS_SHIFT (30)
158#define ICH6_HDA_INTSTS_S0_SHIFT (0)
159#define ICH6_HDA_INTSTS_S1_SHIFT (1)
160#define ICH6_HDA_INTSTS_S2_SHIFT (2)
161#define ICH6_HDA_INTSTS_S3_SHIFT (3)
162#define ICH6_HDA_INTSTS_S4_SHIFT (4)
163#define ICH6_HDA_INTSTS_S5_SHIFT (5)
164#define ICH6_HDA_INTSTS_S6_SHIFT (6)
165#define ICH6_HDA_INTSTS_S7_SHIFT (7)
166#define ICH6_HDA_INTSTS_S_MASK(num) RT_BIT(HDA_REG_FIELD_SHIFT(S##num))
167#define INTSTS(pThis) (HDA_REG((pThis), INTSTS))
168#define INTSTS_GIS(pThis) (HDA_REG_FLAG_VALUE((pThis), INTSTS, GIS)
169#define INTSTS_CIS(pThis) (HDA_REG_FLAG_VALUE((pThis), INTSTS, CIS)
170#define INTSTS_SX(pThis, X) (HDA_REG_FLAG_VALUE(pThis), INTSTS, S##X)
171#define INTSTS_SANY(pThis) (INTSTS((pThis)) & 0xFF)
172
173#define ICH6_HDA_REG_CORBLBASE 13 /* 0x40 */
174#define CORBLBASE(pThis) (HDA_REG((pThis), CORBLBASE))
175#define ICH6_HDA_REG_CORBUBASE 14 /* 0x44 */
176#define CORBUBASE(pThis) (HDA_REG((pThis), CORBUBASE))
177#define ICH6_HDA_REG_CORBWP 15 /* 48 */
178#define ICH6_HDA_REG_CORBRP 16 /* 4A */
179#define ICH6_HDA_CORBRP_RST_SHIFT 15
180#define ICH6_HDA_CORBRP_WP_SHIFT 0
181#define ICH6_HDA_CORBRP_WP_MASK 0xFF
182
183#define CORBRP(pThis) (HDA_REG(pThis, CORBRP))
184#define CORBWP(pThis) (HDA_REG(pThis, CORBWP))
185
186#define ICH6_HDA_REG_CORBCTL 17 /* 0x4C */
187#define ICH6_HDA_CORBCTL_DMA_SHIFT (1)
188#define ICH6_HDA_CORBCTL_CMEIE_SHIFT (0)
189
190#define CORBCTL(pThis) (HDA_REG(pThis, CORBCTL))
191
192
193#define ICH6_HDA_REG_CORBSTS 18 /* 0x4D */
194#define CORBSTS(pThis) (HDA_REG(pThis, CORBSTS))
195#define ICH6_HDA_CORBSTS_CMEI_SHIFT (0)
196
197#define ICH6_HDA_REG_CORBSIZE 19 /* 0x4E */
198#define ICH6_HDA_CORBSIZE_SZ_CAP 0xF0
199#define ICH6_HDA_CORBSIZE_SZ 0x3
200#define CORBSIZE_SZ(pThis) (HDA_REG(pThis, ICH6_HDA_REG_CORBSIZE) & ICH6_HDA_CORBSIZE_SZ)
201#define CORBSIZE_SZ_CAP(pThis) (HDA_REG(pThis, ICH6_HDA_REG_CORBSIZE) & ICH6_HDA_CORBSIZE_SZ_CAP)
202/* till ich 10 sizes of CORB and RIRB are hardcoded to 256 in real hw */
203
204#define ICH6_HDA_REG_RIRLBASE 20 /* 0x50 */
205#define RIRLBASE(pThis) (HDA_REG((pThis), RIRLBASE))
206
207#define ICH6_HDA_REG_RIRUBASE 21 /* 0x54 */
208#define RIRUBASE(pThis) (HDA_REG((pThis), RIRUBASE))
209
210#define ICH6_HDA_REG_RIRBWP 22 /* 0x58 */
211#define ICH6_HDA_RIRBWP_RST_SHIFT (15)
212#define ICH6_HDA_RIRBWP_WP_MASK 0xFF
213#define RIRBWP(pThis) (HDA_REG(pThis, RIRBWP))
214
215#define ICH6_HDA_REG_RINTCNT 23 /* 0x5A */
216#define RINTCNT(pThis) (HDA_REG((pThis), RINTCNT))
217#define RINTCNT_N(pThis) (RINTCNT((pThis)) & 0xff)
218
219#define ICH6_HDA_REG_RIRBCTL 24 /* 0x5C */
220#define ICH6_HDA_RIRBCTL_RIC_SHIFT (0)
221#define ICH6_HDA_RIRBCTL_DMA_SHIFT (1)
222#define ICH6_HDA_ROI_DMA_SHIFT (2)
223#define RIRBCTL(pThis) (HDA_REG((pThis), RIRBCTL))
224#define RIRBCTL_RIRB_RIC(pThis) (HDA_REG_FLAG_VALUE(pThis, RIRBCTL, RIC))
225#define RIRBCTL_RIRB_DMA(pThis) (HDA_REG_FLAG_VALUE((pThis), RIRBCTL, DMA)
226#define RIRBCTL_ROI(pThis) (HDA_REG_FLAG_VALUE((pThis), RIRBCTL, ROI))
227
228#define ICH6_HDA_REG_RIRBSTS 25 /* 0x5D */
229#define ICH6_HDA_RIRBSTS_RINTFL_SHIFT (0)
230#define ICH6_HDA_RIRBSTS_RIRBOIS_SHIFT (2)
231#define RIRBSTS(pThis) (HDA_REG(pThis, RIRBSTS))
232#define RIRBSTS_RINTFL(pThis) (HDA_REG_FLAG_VALUE(pThis, RIRBSTS, RINTFL))
233#define RIRBSTS_RIRBOIS(pThis) (HDA_REG_FLAG_VALUE(pThis, RIRBSTS, RIRBOIS))
234
235#define ICH6_HDA_REG_RIRBSIZE 26 /* 0x5E */
236#define ICH6_HDA_RIRBSIZE_SZ_CAP 0xF0
237#define ICH6_HDA_RIRBSIZE_SZ 0x3
238
239#define RIRBSIZE_SZ(pThis) (HDA_REG(pThis, ICH6_HDA_REG_RIRBSIZE) & ICH6_HDA_RIRBSIZE_SZ)
240#define RIRBSIZE_SZ_CAP(pThis) (HDA_REG(pThis, ICH6_HDA_REG_RIRBSIZE) & ICH6_HDA_RIRBSIZE_SZ_CAP)
241
242
243#define ICH6_HDA_REG_IC 27 /* 0x60 */
244#define IC(pThis) (HDA_REG(pThis, IC))
245#define ICH6_HDA_REG_IR 28 /* 0x64 */
246#define IR(pThis) (HDA_REG(pThis, IR))
247#define ICH6_HDA_REG_IRS 29 /* 0x68 */
248#define ICH6_HDA_IRS_ICB_SHIFT (0)
249#define ICH6_HDA_IRS_IRV_SHIFT (1)
250#define IRS(pThis) (HDA_REG(pThis, IRS))
251#define IRS_ICB(pThis) (HDA_REG_FLAG_VALUE(pThis, IRS, ICB))
252#define IRS_IRV(pThis) (HDA_REG_FLAG_VALUE(pThis, IRS, IRV))
253
254#define ICH6_HDA_REG_DPLBASE 30 /* 0x70 */
255#define DPLBASE(pThis) (HDA_REG((pThis), DPLBASE))
256#define ICH6_HDA_REG_DPUBASE 31 /* 0x74 */
257#define DPUBASE(pThis) (HDA_REG((pThis), DPUBASE))
258#define DPBASE_ENABLED 1
259#define DPBASE_ADDR_MASK (~(uint64_t)0x7f)
260
261#define HDA_STREAM_REG_DEF(name, num) (ICH6_HDA_REG_SD##num##name)
262#define HDA_STREAM_REG(pThis, name, num) (HDA_REG((pThis), N_(HDA_STREAM_REG_DEF(name, num))))
263/* Note: sdnum here _MUST_ be stream reg number [0,7] */
264#define HDA_STREAM_REG2(pThis, name, sdnum) (HDA_REG_IND((pThis), ICH6_HDA_REG_SD0##name + (sdnum) * 10))
265
266#define ICH6_HDA_REG_SD0CTL 32 /* 0x80 */
267#define ICH6_HDA_REG_SD1CTL (HDA_STREAM_REG_DEF(CTL, 0) + 10) /* 0xA0 */
268#define ICH6_HDA_REG_SD2CTL (HDA_STREAM_REG_DEF(CTL, 0) + 20) /* 0xC0 */
269#define ICH6_HDA_REG_SD3CTL (HDA_STREAM_REG_DEF(CTL, 0) + 30) /* 0xE0 */
270#define ICH6_HDA_REG_SD4CTL (HDA_STREAM_REG_DEF(CTL, 0) + 40) /* 0x100 */
271#define ICH6_HDA_REG_SD5CTL (HDA_STREAM_REG_DEF(CTL, 0) + 50) /* 0x120 */
272#define ICH6_HDA_REG_SD6CTL (HDA_STREAM_REG_DEF(CTL, 0) + 60) /* 0x140 */
273#define ICH6_HDA_REG_SD7CTL (HDA_STREAM_REG_DEF(CTL, 0) + 70) /* 0x160 */
274
275#define SD(func, num) SD##num##func
276#define SDCTL(pThis, num) HDA_REG((pThis), SD(CTL, num))
277#define SDCTL_NUM(pThis, num) ((SDCTL((pThis), num) & HDA_REG_FIELD_MASK(SDCTL,NUM)) >> HDA_REG_FIELD_SHIFT(SDCTL, NUM))
278#define ICH6_HDA_SDCTL_NUM_MASK (0xF)
279#define ICH6_HDA_SDCTL_NUM_SHIFT (20)
280#define ICH6_HDA_SDCTL_DIR_SHIFT (19)
281#define ICH6_HDA_SDCTL_TP_SHIFT (18)
282#define ICH6_HDA_SDCTL_STRIPE_MASK (0x3)
283#define ICH6_HDA_SDCTL_STRIPE_SHIFT (16)
284#define ICH6_HDA_SDCTL_DEIE_SHIFT (4)
285#define ICH6_HDA_SDCTL_FEIE_SHIFT (3)
286#define ICH6_HDA_SDCTL_ICE_SHIFT (2)
287#define ICH6_HDA_SDCTL_RUN_SHIFT (1)
288#define ICH6_HDA_SDCTL_SRST_SHIFT (0)
289
290#define ICH6_HDA_REG_SD0STS 33 /* 0x83 */
291#define ICH6_HDA_REG_SD1STS (HDA_STREAM_REG_DEF(STS, 0) + 10) /* 0xA3 */
292#define ICH6_HDA_REG_SD2STS (HDA_STREAM_REG_DEF(STS, 0) + 20) /* 0xC3 */
293#define ICH6_HDA_REG_SD3STS (HDA_STREAM_REG_DEF(STS, 0) + 30) /* 0xE3 */
294#define ICH6_HDA_REG_SD4STS (HDA_STREAM_REG_DEF(STS, 0) + 40) /* 0x103 */
295#define ICH6_HDA_REG_SD5STS (HDA_STREAM_REG_DEF(STS, 0) + 50) /* 0x123 */
296#define ICH6_HDA_REG_SD6STS (HDA_STREAM_REG_DEF(STS, 0) + 60) /* 0x143 */
297#define ICH6_HDA_REG_SD7STS (HDA_STREAM_REG_DEF(STS, 0) + 70) /* 0x163 */
298
299#define SDSTS(pThis, num) HDA_REG((pThis), SD(STS, num))
300#define ICH6_HDA_SDSTS_FIFORDY_SHIFT (5)
301#define ICH6_HDA_SDSTS_DE_SHIFT (4)
302#define ICH6_HDA_SDSTS_FE_SHIFT (3)
303#define ICH6_HDA_SDSTS_BCIS_SHIFT (2)
304
305#define ICH6_HDA_REG_SD0LPIB 34 /* 0x84 */
306#define ICH6_HDA_REG_SD1LPIB (HDA_STREAM_REG_DEF(LPIB, 0) + 10) /* 0xA4 */
307#define ICH6_HDA_REG_SD2LPIB (HDA_STREAM_REG_DEF(LPIB, 0) + 20) /* 0xC4 */
308#define ICH6_HDA_REG_SD3LPIB (HDA_STREAM_REG_DEF(LPIB, 0) + 30) /* 0xE4 */
309#define ICH6_HDA_REG_SD4LPIB (HDA_STREAM_REG_DEF(LPIB, 0) + 40) /* 0x104 */
310#define ICH6_HDA_REG_SD5LPIB (HDA_STREAM_REG_DEF(LPIB, 0) + 50) /* 0x124 */
311#define ICH6_HDA_REG_SD6LPIB (HDA_STREAM_REG_DEF(LPIB, 0) + 60) /* 0x144 */
312#define ICH6_HDA_REG_SD7LPIB (HDA_STREAM_REG_DEF(LPIB, 0) + 70) /* 0x164 */
313
314#define SDLPIB(pThis, num) HDA_REG((pThis), SD(LPIB, num))
315
316#define ICH6_HDA_REG_SD0CBL 35 /* 0x88 */
317#define ICH6_HDA_REG_SD1CBL (HDA_STREAM_REG_DEF(CBL, 0) + 10) /* 0xA8 */
318#define ICH6_HDA_REG_SD2CBL (HDA_STREAM_REG_DEF(CBL, 0) + 20) /* 0xC8 */
319#define ICH6_HDA_REG_SD3CBL (HDA_STREAM_REG_DEF(CBL, 0) + 30) /* 0xE8 */
320#define ICH6_HDA_REG_SD4CBL (HDA_STREAM_REG_DEF(CBL, 0) + 40) /* 0x108 */
321#define ICH6_HDA_REG_SD5CBL (HDA_STREAM_REG_DEF(CBL, 0) + 50) /* 0x128 */
322#define ICH6_HDA_REG_SD6CBL (HDA_STREAM_REG_DEF(CBL, 0) + 60) /* 0x148 */
323#define ICH6_HDA_REG_SD7CBL (HDA_STREAM_REG_DEF(CBL, 0) + 70) /* 0x168 */
324
325#define SDLCBL(pThis, num) HDA_REG((pThis), SD(CBL, num))
326
327#define ICH6_HDA_REG_SD0LVI 36 /* 0x8C */
328#define ICH6_HDA_REG_SD1LVI (HDA_STREAM_REG_DEF(LVI, 0) + 10) /* 0xAC */
329#define ICH6_HDA_REG_SD2LVI (HDA_STREAM_REG_DEF(LVI, 0) + 20) /* 0xCC */
330#define ICH6_HDA_REG_SD3LVI (HDA_STREAM_REG_DEF(LVI, 0) + 30) /* 0xEC */
331#define ICH6_HDA_REG_SD4LVI (HDA_STREAM_REG_DEF(LVI, 0) + 40) /* 0x10C */
332#define ICH6_HDA_REG_SD5LVI (HDA_STREAM_REG_DEF(LVI, 0) + 50) /* 0x12C */
333#define ICH6_HDA_REG_SD6LVI (HDA_STREAM_REG_DEF(LVI, 0) + 60) /* 0x14C */
334#define ICH6_HDA_REG_SD7LVI (HDA_STREAM_REG_DEF(LVI, 0) + 70) /* 0x16C */
335
336#define SDLVI(pThis, num) HDA_REG((pThis), SD(LVI, num))
337
338#define ICH6_HDA_REG_SD0FIFOW 37 /* 0x8E */
339#define ICH6_HDA_REG_SD1FIFOW (HDA_STREAM_REG_DEF(FIFOW, 0) + 10) /* 0xAE */
340#define ICH6_HDA_REG_SD2FIFOW (HDA_STREAM_REG_DEF(FIFOW, 0) + 20) /* 0xCE */
341#define ICH6_HDA_REG_SD3FIFOW (HDA_STREAM_REG_DEF(FIFOW, 0) + 30) /* 0xEE */
342#define ICH6_HDA_REG_SD4FIFOW (HDA_STREAM_REG_DEF(FIFOW, 0) + 40) /* 0x10E */
343#define ICH6_HDA_REG_SD5FIFOW (HDA_STREAM_REG_DEF(FIFOW, 0) + 50) /* 0x12E */
344#define ICH6_HDA_REG_SD6FIFOW (HDA_STREAM_REG_DEF(FIFOW, 0) + 60) /* 0x14E */
345#define ICH6_HDA_REG_SD7FIFOW (HDA_STREAM_REG_DEF(FIFOW, 0) + 70) /* 0x16E */
346
347/*
348 * ICH6 datasheet defined limits for FIFOW values (18.2.38)
349 */
350#define HDA_SDFIFOW_8B (0x2)
351#define HDA_SDFIFOW_16B (0x3)
352#define HDA_SDFIFOW_32B (0x4)
353#define SDFIFOW(pThis, num) HDA_REG((pThis), SD(FIFOW, num))
354
355#define ICH6_HDA_REG_SD0FIFOS 38 /* 0x90 */
356#define ICH6_HDA_REG_SD1FIFOS (HDA_STREAM_REG_DEF(FIFOS, 0) + 10) /* 0xB0 */
357#define ICH6_HDA_REG_SD2FIFOS (HDA_STREAM_REG_DEF(FIFOS, 0) + 20) /* 0xD0 */
358#define ICH6_HDA_REG_SD3FIFOS (HDA_STREAM_REG_DEF(FIFOS, 0) + 30) /* 0xF0 */
359#define ICH6_HDA_REG_SD4FIFOS (HDA_STREAM_REG_DEF(FIFOS, 0) + 40) /* 0x110 */
360#define ICH6_HDA_REG_SD5FIFOS (HDA_STREAM_REG_DEF(FIFOS, 0) + 50) /* 0x130 */
361#define ICH6_HDA_REG_SD6FIFOS (HDA_STREAM_REG_DEF(FIFOS, 0) + 60) /* 0x150 */
362#define ICH6_HDA_REG_SD7FIFOS (HDA_STREAM_REG_DEF(FIFOS, 0) + 70) /* 0x170 */
363
364/*
365 * ICH6 datasheet defines limits for FIFOS registers (18.2.39)
366 * formula: size - 1
367 * Other values not listed are not supported.
368 */
369#define HDA_SDONFIFO_16B (0x0F) /* 8-, 16-, 20-, 24-, 32-bit Output Streams */
370#define HDA_SDONFIFO_32B (0x1F) /* 8-, 16-, 20-, 24-, 32-bit Output Streams */
371#define HDA_SDONFIFO_64B (0x3F) /* 8-, 16-, 20-, 24-, 32-bit Output Streams */
372#define HDA_SDONFIFO_128B (0x7F) /* 8-, 16-, 20-, 24-, 32-bit Output Streams */
373#define HDA_SDONFIFO_192B (0xBF) /* 8-, 16-, 20-, 24-, 32-bit Output Streams */
374#define HDA_SDONFIFO_256B (0xFF) /* 20-, 24-bit Output Streams */
375#define HDA_SDINFIFO_120B (0x77) /* 8-, 16-, 20-, 24-, 32-bit Input Streams */
376#define HDA_SDINFIFO_160B (0x9F) /* 20-, 24-bit Input Streams Streams */
377#define SDFIFOS(pThis, num) HDA_REG((pThis), SD(FIFOS, num))
378
379#define ICH6_HDA_REG_SD0FMT 39 /* 0x92 */
380#define ICH6_HDA_REG_SD1FMT (HDA_STREAM_REG_DEF(FMT, 0) + 10) /* 0xB2 */
381#define ICH6_HDA_REG_SD2FMT (HDA_STREAM_REG_DEF(FMT, 0) + 20) /* 0xD2 */
382#define ICH6_HDA_REG_SD3FMT (HDA_STREAM_REG_DEF(FMT, 0) + 30) /* 0xF2 */
383#define ICH6_HDA_REG_SD4FMT (HDA_STREAM_REG_DEF(FMT, 0) + 40) /* 0x112 */
384#define ICH6_HDA_REG_SD5FMT (HDA_STREAM_REG_DEF(FMT, 0) + 50) /* 0x132 */
385#define ICH6_HDA_REG_SD6FMT (HDA_STREAM_REG_DEF(FMT, 0) + 60) /* 0x152 */
386#define ICH6_HDA_REG_SD7FMT (HDA_STREAM_REG_DEF(FMT, 0) + 70) /* 0x172 */
387
388#define SDFMT(pThis, num) (HDA_REG((pThis), SD(FMT, num)))
389#define ICH6_HDA_SDFMT_BASE_RATE_SHIFT (14)
390#define ICH6_HDA_SDFMT_MULT_SHIFT (11)
391#define ICH6_HDA_SDFMT_MULT_MASK (0x7)
392#define ICH6_HDA_SDFMT_DIV_SHIFT (8)
393#define ICH6_HDA_SDFMT_DIV_MASK (0x7)
394#define ICH6_HDA_SDFMT_BITS_SHIFT (4)
395#define ICH6_HDA_SDFMT_BITS_MASK (0x7)
396#define SDFMT_BASE_RATE(pThis, num) ((SDFMT(pThis, num) & HDA_REG_FIELD_FLAG_MASK(SDFMT, BASE_RATE)) >> HDA_REG_FIELD_SHIFT(SDFMT, BASE_RATE))
397#define SDFMT_MULT(pThis, num) ((SDFMT((pThis), num) & HDA_REG_FIELD_MASK(SDFMT,MULT)) >> HDA_REG_FIELD_SHIFT(SDFMT, MULT))
398#define SDFMT_DIV(pThis, num) ((SDFMT((pThis), num) & HDA_REG_FIELD_MASK(SDFMT,DIV)) >> HDA_REG_FIELD_SHIFT(SDFMT, DIV))
399
400#define ICH6_HDA_REG_SD0BDPL 40 /* 0x98 */
401#define ICH6_HDA_REG_SD1BDPL (HDA_STREAM_REG_DEF(BDPL, 0) + 10) /* 0xB8 */
402#define ICH6_HDA_REG_SD2BDPL (HDA_STREAM_REG_DEF(BDPL, 0) + 20) /* 0xD8 */
403#define ICH6_HDA_REG_SD3BDPL (HDA_STREAM_REG_DEF(BDPL, 0) + 30) /* 0xF8 */
404#define ICH6_HDA_REG_SD4BDPL (HDA_STREAM_REG_DEF(BDPL, 0) + 40) /* 0x118 */
405#define ICH6_HDA_REG_SD5BDPL (HDA_STREAM_REG_DEF(BDPL, 0) + 50) /* 0x138 */
406#define ICH6_HDA_REG_SD6BDPL (HDA_STREAM_REG_DEF(BDPL, 0) + 60) /* 0x158 */
407#define ICH6_HDA_REG_SD7BDPL (HDA_STREAM_REG_DEF(BDPL, 0) + 70) /* 0x178 */
408
409#define SDBDPL(pThis, num) HDA_REG((pThis), SD(BDPL, num))
410
411#define ICH6_HDA_REG_SD0BDPU 41 /* 0x9C */
412#define ICH6_HDA_REG_SD1BDPU (HDA_STREAM_REG_DEF(BDPU, 0) + 10) /* 0xBC */
413#define ICH6_HDA_REG_SD2BDPU (HDA_STREAM_REG_DEF(BDPU, 0) + 20) /* 0xDC */
414#define ICH6_HDA_REG_SD3BDPU (HDA_STREAM_REG_DEF(BDPU, 0) + 30) /* 0xFC */
415#define ICH6_HDA_REG_SD4BDPU (HDA_STREAM_REG_DEF(BDPU, 0) + 40) /* 0x11C */
416#define ICH6_HDA_REG_SD5BDPU (HDA_STREAM_REG_DEF(BDPU, 0) + 50) /* 0x13C */
417#define ICH6_HDA_REG_SD6BDPU (HDA_STREAM_REG_DEF(BDPU, 0) + 60) /* 0x15C */
418#define ICH6_HDA_REG_SD7BDPU (HDA_STREAM_REG_DEF(BDPU, 0) + 70) /* 0x17C */
419
420#define SDBDPU(pThis, num) HDA_REG((pThis), SD(BDPU, num))
421
422
423/*******************************************************************************
424* Structures and Typedefs *
425*******************************************************************************/
426typedef struct HDABDLEDESC
427{
428 uint64_t u64BdleCviAddr;
429 uint32_t u32BdleMaxCvi;
430 uint32_t u32BdleCvi;
431 uint32_t u32BdleCviLen;
432 uint32_t u32BdleCviPos;
433 bool fBdleCviIoc;
434 uint32_t cbUnderFifoW;
435 uint8_t au8HdaBuffer[HDA_SDONFIFO_256B + 1];
436} HDABDLEDESC, *PHDABDLEDESC;
437
438typedef struct HDASTREAMTRANSFERDESC
439{
440 uint64_t u64BaseDMA;
441 uint32_t u32Ctl;
442 uint32_t *pu32Sts;
443 uint8_t u8Strm;
444 uint32_t *pu32Lpib;
445 uint32_t u32Cbl;
446 uint32_t u32Fifos;
447} HDASTREAMTRANSFERDESC, *PHDASTREAMTRANSFERDESC;
448
449/**
450 * ICH Intel HD Audio Controller state.
451 */
452typedef struct HDASTATE
453{
454 /** The PCI device structure. */
455 PCIDevice PciDev;
456 /** Pointer to the device instance. */
457 PPDMDEVINSR3 pDevIns;
458 /** Pointer to the connector of the attached audio driver. */
459 PPDMIAUDIOCONNECTOR pDrv;
460 /** Pointer to the attached audio driver. */
461 PPDMIBASE pDrvBase;
462 /** The base interface for LUN\#0. */
463 PDMIBASE IBase;
464 RTGCPHYS MMIOBaseAddr;
465 uint32_t au32Regs[HDA_NREGS];
466 HDABDLEDESC StInBdle;
467 HDABDLEDESC StOutBdle;
468 HDABDLEDESC StMicBdle;
469 /** Interrupt on completion */
470 bool fCviIoc;
471 uint64_t u64CORBBase;
472 uint64_t u64RIRBBase;
473 uint64_t u64DPBase;
474 /** pointer to CORB buf */
475 uint32_t *pu32CorbBuf;
476 /** size in bytes of CORB buf */
477 uint32_t cbCorbBuf;
478 /** pointer on RIRB buf */
479 uint64_t *pu64RirbBuf;
480 /** size in bytes of RIRB buf */
481 uint32_t cbRirbBuf;
482 /** indicates if HDA in reset. */
483 bool fInReset;
484 /** The HDA codec state. */
485 HDACODEC Codec;
486 /** 1.2.3.4.5.6.7. - someone please tell me what I'm counting! - .8.9.10... */
487 uint8_t u8Counter;
488 uint64_t u64BaseTS;
489} HDASTATE;
490/** Pointer to the ICH Intel HD Audio Controller state. */
491typedef HDASTATE *PHDASTATE;
492
493#define ISD0FMT_TO_AUDIO_SELECTOR(pThis) \
494 ( AUDIO_FORMAT_SELECTOR(&(pThis)->Codec, In, SDFMT_BASE_RATE(pThis, 0), SDFMT_MULT(pThis, 0), SDFMT_DIV(pThis, 0)) )
495#define OSD0FMT_TO_AUDIO_SELECTOR(pThis) \
496 ( AUDIO_FORMAT_SELECTOR(&(pThis)->Codec, Out, SDFMT_BASE_RATE(pThis, 4), SDFMT_MULT(pThis, 4), SDFMT_DIV(pThis, 4)) )
497
498
499/*******************************************************************************
500* Internal Functions *
501*******************************************************************************/
502static FNPDMDEVRESET hdaReset;
503
504static int hdaRegReadUnimplemented(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
505static int hdaRegWriteUnimplemented(PHDASTATE pThis, uint32_t iReg, uint32_t pu32Value);
506static int hdaRegReadGCTL(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
507static int hdaRegWriteGCTL(PHDASTATE pThis, uint32_t iReg, uint32_t pu32Value);
508static int hdaRegReadSTATESTS(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
509static int hdaRegWriteSTATESTS(PHDASTATE pThis, uint32_t iReg, uint32_t pu32Value);
510static int hdaRegReadGCAP(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
511static int hdaRegReadINTSTS(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
512static int hdaRegReadWALCLK(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
513static int hdaRegWriteINTSTS(PHDASTATE pThis, uint32_t iReg, uint32_t pu32Value);
514static int hdaRegWriteCORBWP(PHDASTATE pThis, uint32_t iReg, uint32_t pu32Value);
515static int hdaRegWriteCORBRP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
516static int hdaRegWriteCORBCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
517static int hdaRegWriteCORBSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
518static int hdaRegWriteRIRBWP(PHDASTATE pThis, uint32_t iReg, uint32_t pu32Value);
519static int hdaRegWriteRIRBSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
520static int hdaRegWriteIRS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
521static int hdaRegReadIRS(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
522static int hdaRegWriteSDCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
523static int hdaRegReadSDCTL(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
524
525static int hdaRegWriteSDSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
526static int hdaRegWriteSDLVI(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
527static int hdaRegWriteSDFIFOW(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
528static int hdaRegWriteSDFIFOS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
529static int hdaRegWriteSDFMT(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
530static int hdaRegWriteSDBDPL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
531static int hdaRegWriteSDBDPU(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
532static int hdaRegWriteBase(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
533static int hdaRegReadU32(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
534static int hdaRegWriteU32(PHDASTATE pThis, uint32_t iReg, uint32_t pu32Value);
535static int hdaRegReadU24(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
536static int hdaRegWriteU24(PHDASTATE pThis, uint32_t iReg, uint32_t pu32Value);
537static int hdaRegReadU16(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
538static int hdaRegWriteU16(PHDASTATE pThis, uint32_t iReg, uint32_t pu32Value);
539static int hdaRegReadU8(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
540static int hdaRegWriteU8(PHDASTATE pThis, uint32_t iReg, uint32_t pu32Value);
541
542DECLINLINE(void) hdaInitTransferDescriptor(PHDASTATE pThis, PHDABDLEDESC pBdle, uint8_t u8Strm,
543 PHDASTREAMTRANSFERDESC pStreamDesc);
544static void hdaFetchBdle(PHDASTATE pThis, PHDABDLEDESC pBdle, PHDASTREAMTRANSFERDESC pStreamDesc);
545#ifdef LOG_ENABLED
546static void dump_bd(PHDASTATE pThis, PHDABDLEDESC pBdle, uint64_t u64BaseDMA);
547#endif
548
549
550/*******************************************************************************
551* Global Variables *
552*******************************************************************************/
553/* see 302349 p 6.2*/
554static const struct HDAREGDESC
555{
556 /** Register offset in the register space. */
557 uint32_t offset;
558 /** Size in bytes. Registers of size > 4 are in fact tables. */
559 uint32_t size;
560 /** Readable bits. */
561 uint32_t readable;
562 /** Writable bits. */
563 uint32_t writable;
564 /** Read callback. */
565 int (*pfnRead)(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value);
566 /** Write callback. */
567 int (*pfnWrite)(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value);
568 /** Abbreviated name. */
569 const char *abbrev;
570 /** Full name. */
571 const char *name;
572} g_aHdaRegMap[HDA_NREGS] =
573{
574 /* offset size read mask write mask read callback write callback abbrev full name */
575 /*------- ------- ---------- ---------- ----------------------- ------------------------ ---------- ------------------------------*/
576 { 0x00000, 0x00002, 0x0000FFFB, 0x00000000, hdaRegReadGCAP , hdaRegWriteUnimplemented, "GCAP" , "Global Capabilities" },
577 { 0x00002, 0x00001, 0x000000FF, 0x00000000, hdaRegReadU8 , hdaRegWriteUnimplemented, "VMIN" , "Minor Version" },
578 { 0x00003, 0x00001, 0x000000FF, 0x00000000, hdaRegReadU8 , hdaRegWriteUnimplemented, "VMAJ" , "Major Version" },
579 { 0x00004, 0x00002, 0x0000FFFF, 0x00000000, hdaRegReadU16 , hdaRegWriteUnimplemented, "OUTPAY" , "Output Payload Capabilities" },
580 { 0x00006, 0x00002, 0x0000FFFF, 0x00000000, hdaRegReadU16 , hdaRegWriteUnimplemented, "INPAY" , "Input Payload Capabilities" },
581 { 0x00008, 0x00004, 0x00000103, 0x00000103, hdaRegReadGCTL , hdaRegWriteGCTL , "GCTL" , "Global Control" },
582 { 0x0000c, 0x00002, 0x00007FFF, 0x00007FFF, hdaRegReadU16 , hdaRegWriteU16 , "WAKEEN" , "Wake Enable" },
583 { 0x0000e, 0x00002, 0x00000007, 0x00000007, hdaRegReadU8 , hdaRegWriteSTATESTS , "STATESTS" , "State Change Status" },
584 { 0x00010, 0x00002, 0xFFFFFFFF, 0x00000000, hdaRegReadUnimplemented, hdaRegWriteUnimplemented, "GSTS" , "Global Status" },
585 { 0x00020, 0x00004, 0xC00000FF, 0xC00000FF, hdaRegReadU32 , hdaRegWriteU32 , "INTCTL" , "Interrupt Control" },
586 { 0x00024, 0x00004, 0xC00000FF, 0x00000000, hdaRegReadINTSTS , hdaRegWriteUnimplemented, "INTSTS" , "Interrupt Status" },
587 { 0x00030, 0x00004, 0xFFFFFFFF, 0x00000000, hdaRegReadWALCLK , hdaRegWriteUnimplemented, "WALCLK" , "Wall Clock Counter" },
588 /// @todo r=michaln: Doesn't the SSYNC register need to actually stop the stream(s)?
589 { 0x00034, 0x00004, 0x000000FF, 0x000000FF, hdaRegReadU32 , hdaRegWriteU32 , "SSYNC" , "Stream Synchronization" },
590 { 0x00040, 0x00004, 0xFFFFFF80, 0xFFFFFF80, hdaRegReadU32 , hdaRegWriteBase , "CORBLBASE" , "CORB Lower Base Address" },
591 { 0x00044, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteBase , "CORBUBASE" , "CORB Upper Base Address" },
592 { 0x00048, 0x00002, 0x000000FF, 0x000000FF, hdaRegReadU16 , hdaRegWriteCORBWP , "CORBWP" , "CORB Write Pointer" },
593#ifdef OLD_REGISTER_TABLE
594 { 0x0004A, 0x00002, 0x000000FF, 0x000080FF, hdaRegReadU8 , hdaRegWriteCORBRP , "CORBRP" , "CORB Read Pointer" },
595#else /** @todo 18.2.17 indicates that the 15th bit can be read as well as and written. hdaRegReadU8 is wrong, a special reader should be used. */
596 { 0x0004A, 0x00002, 0x000080FF, 0x000080FF, hdaRegReadU16 , hdaRegWriteCORBRP , "CORBRP" , "CORB Read Pointer" },
597#endif
598 { 0x0004C, 0x00001, 0x00000003, 0x00000003, hdaRegReadU8 , hdaRegWriteCORBCTL , "CORBCTL" , "CORB Control" },
599 { 0x0004D, 0x00001, 0x00000001, 0x00000001, hdaRegReadU8 , hdaRegWriteCORBSTS , "CORBSTS" , "CORB Status" },
600 { 0x0004E, 0x00001, 0x000000F3, 0x00000000, hdaRegReadU8 , hdaRegWriteUnimplemented, "CORBSIZE" , "CORB Size" },
601 { 0x00050, 0x00004, 0xFFFFFF80, 0xFFFFFF80, hdaRegReadU32 , hdaRegWriteBase , "RIRBLBASE" , "RIRB Lower Base Address" },
602 { 0x00054, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteBase , "RIRBUBASE" , "RIRB Upper Base Address" },
603 { 0x00058, 0x00002, 0x000000FF, 0x00008000, hdaRegReadU8 , hdaRegWriteRIRBWP , "RIRBWP" , "RIRB Write Pointer" },
604 { 0x0005A, 0x00002, 0x000000FF, 0x000000FF, hdaRegReadU16 , hdaRegWriteU16 , "RINTCNT" , "Response Interrupt Count" },
605 { 0x0005C, 0x00001, 0x00000007, 0x00000007, hdaRegReadU8 , hdaRegWriteU8 , "RIRBCTL" , "RIRB Control" },
606 { 0x0005D, 0x00001, 0x00000005, 0x00000005, hdaRegReadU8 , hdaRegWriteRIRBSTS , "RIRBSTS" , "RIRB Status" },
607 { 0x0005E, 0x00001, 0x000000F3, 0x00000000, hdaRegReadU8 , hdaRegWriteUnimplemented, "RIRBSIZE" , "RIRB Size" },
608 { 0x00060, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteU32 , "IC" , "Immediate Command" },
609 { 0x00064, 0x00004, 0x00000000, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteUnimplemented, "IR" , "Immediate Response" },
610#ifdef OLD_REGISTER_TABLE
611 { 0x00068, 0x00004, 0x00000002, 0x00000002, hdaRegReadIRS , hdaRegWriteIRS , "IRS" , "Immediate Command Status" },
612#else /* 18.2.30 as well as the table says 16-bit. Linux accesses it as a 16-bit register. */
613 { 0x00068, 0x00002, 0x00000002, 0x00000002, hdaRegReadIRS , hdaRegWriteIRS , "IRS" , "Immediate Command Status" },
614#endif
615 { 0x00070, 0x00004, 0xFFFFFFFF, 0xFFFFFF81, hdaRegReadU32 , hdaRegWriteBase , "DPLBASE" , "DMA Position Lower Base" },
616 { 0x00074, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteBase , "DPUBASE" , "DMA Position Upper Base" },
617
618 { 0x00080, 0x00003, 0x00FF001F, 0x00F0001F, hdaRegReadU24 , hdaRegWriteSDCTL , "ISD0CTL" , "Input Stream Descriptor 0 (ICD0) Control" },
619 { 0x00083, 0x00001, 0x0000001C, 0x0000003C, hdaRegReadU8 , hdaRegWriteSDSTS , "ISD0STS" , "ISD0 Status" },
620 { 0x00084, 0x00004, 0xFFFFFFFF, 0x00000000, hdaRegReadU32 , hdaRegWriteU32 , "ISD0LPIB" , "ISD0 Link Position In Buffer" },
621 { 0x00088, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteU32 , "ISD0CBL" , "ISD0 Cyclic Buffer Length" },
622 { 0x0008C, 0x00002, 0x0000FFFF, 0x0000FFFF, hdaRegReadU16 , hdaRegWriteSDLVI , "ISD0LVI" , "ISD0 Last Valid Index" },
623 { 0x0008E, 0x00002, 0x00000007, 0x00000007, hdaRegReadU16 , hdaRegWriteSDFIFOW , "ISD0FIFOW", "ISD0 FIFO Watermark" },
624 { 0x00090, 0x00002, 0x000000FF, 0x00000000, hdaRegReadU16 , hdaRegWriteU16 , "ISD0FIFOS", "ISD0 FIFO Size" },
625 { 0x00092, 0x00002, 0x00007F7F, 0x00007F7F, hdaRegReadU16 , hdaRegWriteSDFMT , "ISD0FMT" , "ISD0 Format" },
626 { 0x00098, 0x00004, 0xFFFFFF80, 0xFFFFFF80, hdaRegReadU32 , hdaRegWriteSDBDPL , "ISD0BDPL" , "ISD0 Buffer Descriptor List Pointer-Lower Base Address" },
627 { 0x0009C, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteSDBDPU , "ISD0BDPU" , "ISD0 Buffer Descriptor List Pointer-Upper Base Address" },
628
629 { 0x000A0, 0x00003, 0x00FF001F, 0x00F0001F, hdaRegReadU24 , hdaRegWriteSDCTL , "ISD1CTL" , "Input Stream Descriptor 1 (ISD1) Control" },
630 { 0x000A3, 0x00001, 0x0000001C, 0x0000003C, hdaRegReadU8 , hdaRegWriteSDSTS , "ISD1STS" , "ISD1 Status" },
631 { 0x000A4, 0x00004, 0xFFFFFFFF, 0x00000000, hdaRegReadU32 , hdaRegWriteU32 , "ISD1LPIB" , "ISD1 Link Position In Buffer" },
632 { 0x000A8, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteU32 , "ISD1CBL" , "ISD1 Cyclic Buffer Length" },
633 { 0x000AC, 0x00002, 0x0000FFFF, 0x0000FFFF, hdaRegReadU16 , hdaRegWriteSDLVI , "ISD1LVI" , "ISD1 Last Valid Index" },
634 { 0x000AE, 0x00002, 0x00000007, 0x00000007, hdaRegReadU16 , hdaRegWriteSDFIFOW , "ISD1FIFOW", "ISD1 FIFO Watermark" },
635 { 0x000B0, 0x00002, 0x000000FF, 0x00000000, hdaRegReadU16 , hdaRegWriteU16 , "ISD1FIFOS", "ISD1 FIFO Size" },
636 { 0x000B2, 0x00002, 0x00007F7F, 0x00007F7F, hdaRegReadU16 , hdaRegWriteSDFMT , "ISD1FMT" , "ISD1 Format" },
637 { 0x000B8, 0x00004, 0xFFFFFF80, 0xFFFFFF80, hdaRegReadU32 , hdaRegWriteSDBDPL , "ISD1BDPL" , "ISD1 Buffer Descriptor List Pointer-Lower Base Address" },
638 { 0x000BC, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteSDBDPU , "ISD1BDPU" , "ISD1 Buffer Descriptor List Pointer-Upper Base Address" },
639
640 { 0x000C0, 0x00003, 0x00FF001F, 0x00F0001F, hdaRegReadU24 , hdaRegWriteSDCTL , "ISD2CTL" , "Input Stream Descriptor 2 (ISD2) Control" },
641 { 0x000C3, 0x00001, 0x0000001C, 0x0000003C, hdaRegReadU8 , hdaRegWriteSDSTS , "ISD2STS" , "ISD2 Status" },
642 { 0x000C4, 0x00004, 0xFFFFFFFF, 0x00000000, hdaRegReadU32 , hdaRegWriteU32 , "ISD2LPIB" , "ISD2 Link Position In Buffer" },
643 { 0x000C8, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteU32 , "ISD2CBL" , "ISD2 Cyclic Buffer Length" },
644 { 0x000CC, 0x00002, 0x0000FFFF, 0x0000FFFF, hdaRegReadU16 , hdaRegWriteSDLVI , "ISD2LVI" , "ISD2 Last Valid Index" },
645 { 0x000CE, 0x00002, 0x00000007, 0x00000007, hdaRegReadU16 , hdaRegWriteSDFIFOW , "ISD2FIFOW", "ISD2 FIFO Watermark" },
646 { 0x000D0, 0x00002, 0x000000FF, 0x00000000, hdaRegReadU16 , hdaRegWriteU16 , "ISD2FIFOS", "ISD2 FIFO Size" },
647 { 0x000D2, 0x00002, 0x00007F7F, 0x00007F7F, hdaRegReadU16 , hdaRegWriteSDFMT , "ISD2FMT" , "ISD2 Format" },
648 { 0x000D8, 0x00004, 0xFFFFFF80, 0xFFFFFF80, hdaRegReadU32 , hdaRegWriteSDBDPL , "ISD2BDPL" , "ISD2 Buffer Descriptor List Pointer-Lower Base Address" },
649 { 0x000DC, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteSDBDPU , "ISD2BDPU" , "ISD2 Buffer Descriptor List Pointer-Upper Base Address" },
650
651 { 0x000E0, 0x00003, 0x00FF001F, 0x00F0001F, hdaRegReadU24 , hdaRegWriteSDCTL , "ISD3CTL" , "Input Stream Descriptor 3 (ISD3) Control" },
652 { 0x000E3, 0x00001, 0x0000001C, 0x0000003C, hdaRegReadU8 , hdaRegWriteSDSTS , "ISD3STS" , "ISD3 Status" },
653 { 0x000E4, 0x00004, 0xFFFFFFFF, 0x00000000, hdaRegReadU32 , hdaRegWriteU32 , "ISD3LPIB" , "ISD3 Link Position In Buffer" },
654 { 0x000E8, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteU32 , "ISD3CBL" , "ISD3 Cyclic Buffer Length" },
655 { 0x000EC, 0x00002, 0x0000FFFF, 0x0000FFFF, hdaRegReadU16 , hdaRegWriteSDLVI , "ISD3LVI" , "ISD3 Last Valid Index" },
656 { 0x000EE, 0x00002, 0x00000005, 0x00000005, hdaRegReadU16 , hdaRegWriteU16 , "ISD3FIFOW", "ISD3 FIFO Watermark" },
657 { 0x000F0, 0x00002, 0x000000FF, 0x00000000, hdaRegReadU16 , hdaRegWriteU16 , "ISD3FIFOS", "ISD3 FIFO Size" },
658 { 0x000F2, 0x00002, 0x00007F7F, 0x00007F7F, hdaRegReadU16 , hdaRegWriteSDFMT , "ISD3FMT" , "ISD3 Format" },
659 { 0x000F8, 0x00004, 0xFFFFFF80, 0xFFFFFF80, hdaRegReadU32 , hdaRegWriteSDBDPL , "ISD3BDPL" , "ISD3 Buffer Descriptor List Pointer-Lower Base Address" },
660 { 0x000FC, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteSDBDPU , "ISD3BDPU" , "ISD3 Buffer Descriptor List Pointer-Upper Base Address" },
661
662 { 0x00100, 0x00003, 0x00FF001F, 0x00F0001F, hdaRegReadSDCTL , hdaRegWriteSDCTL , "OSD0CTL" , "Input Stream Descriptor 0 (OSD0) Control" },
663 { 0x00103, 0x00001, 0x0000001C, 0x0000003C, hdaRegReadU8 , hdaRegWriteSDSTS , "OSD0STS" , "OSD0 Status" },
664 { 0x00104, 0x00004, 0xFFFFFFFF, 0x00000000, hdaRegReadU32 , hdaRegWriteU32 , "OSD0LPIB" , "OSD0 Link Position In Buffer" },
665 { 0x00108, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteU32 , "OSD0CBL" , "OSD0 Cyclic Buffer Length" },
666 { 0x0010C, 0x00002, 0x0000FFFF, 0x0000FFFF, hdaRegReadU16 , hdaRegWriteSDLVI , "OSD0LVI" , "OSD0 Last Valid Index" },
667 { 0x0010E, 0x00002, 0x00000007, 0x00000007, hdaRegReadU16 , hdaRegWriteSDFIFOW , "OSD0FIFOW", "OSD0 FIFO Watermark" },
668 { 0x00110, 0x00002, 0x000000FF, 0x000000FF, hdaRegReadU16 , hdaRegWriteSDFIFOS , "OSD0FIFOS", "OSD0 FIFO Size" },
669 { 0x00112, 0x00002, 0x00007F7F, 0x00007F7F, hdaRegReadU16 , hdaRegWriteSDFMT , "OSD0FMT" , "OSD0 Format" },
670 { 0x00118, 0x00004, 0xFFFFFF80, 0xFFFFFF80, hdaRegReadU32 , hdaRegWriteSDBDPL , "OSD0BDPL" , "OSD0 Buffer Descriptor List Pointer-Lower Base Address" },
671 { 0x0011C, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteSDBDPU , "OSD0BDPU" , "OSD0 Buffer Descriptor List Pointer-Upper Base Address" },
672
673 { 0x00120, 0x00003, 0x00FF001F, 0x00F0001F, hdaRegReadU24 , hdaRegWriteSDCTL , "OSD1CTL" , "Input Stream Descriptor 0 (OSD1) Control" },
674 { 0x00123, 0x00001, 0x0000001C, 0x0000003C, hdaRegReadU8 , hdaRegWriteSDSTS , "OSD1STS" , "OSD1 Status" },
675 { 0x00124, 0x00004, 0xFFFFFFFF, 0x00000000, hdaRegReadU32 , hdaRegWriteU32 , "OSD1LPIB" , "OSD1 Link Position In Buffer" },
676 { 0x00128, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteU32 , "OSD1CBL" , "OSD1 Cyclic Buffer Length" },
677 { 0x0012C, 0x00002, 0x0000FFFF, 0x0000FFFF, hdaRegReadU16 , hdaRegWriteSDLVI , "OSD1LVI" , "OSD1 Last Valid Index" },
678 { 0x0012E, 0x00002, 0x00000007, 0x00000007, hdaRegReadU16 , hdaRegWriteSDFIFOW , "OSD1FIFOW", "OSD1 FIFO Watermark" },
679 { 0x00130, 0x00002, 0x000000FF, 0x000000FF, hdaRegReadU16 , hdaRegWriteSDFIFOS , "OSD1FIFOS", "OSD1 FIFO Size" },
680 { 0x00132, 0x00002, 0x00007F7F, 0x00007F7F, hdaRegReadU16 , hdaRegWriteSDFMT , "OSD1FMT" , "OSD1 Format" },
681 { 0x00138, 0x00004, 0xFFFFFF80, 0xFFFFFF80, hdaRegReadU32 , hdaRegWriteSDBDPL , "OSD1BDPL" , "OSD1 Buffer Descriptor List Pointer-Lower Base Address" },
682 { 0x0013C, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteSDBDPU , "OSD1BDPU" , "OSD1 Buffer Descriptor List Pointer-Upper Base Address" },
683
684 { 0x00140, 0x00003, 0x00FF001F, 0x00F0001F, hdaRegReadU24 , hdaRegWriteSDCTL , "OSD2CTL" , "Input Stream Descriptor 0 (OSD2) Control" },
685 { 0x00143, 0x00001, 0x0000001C, 0x0000003C, hdaRegReadU8 , hdaRegWriteSDSTS , "OSD2STS" , "OSD2 Status" },
686 { 0x00144, 0x00004, 0xFFFFFFFF, 0x00000000, hdaRegReadU32 , hdaRegWriteU32 , "OSD2LPIB" , "OSD2 Link Position In Buffer" },
687 { 0x00148, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteU32 , "OSD2CBL" , "OSD2 Cyclic Buffer Length" },
688 { 0x0014C, 0x00002, 0x0000FFFF, 0x0000FFFF, hdaRegReadU16 , hdaRegWriteSDLVI , "OSD2LVI" , "OSD2 Last Valid Index" },
689 { 0x0014E, 0x00002, 0x00000007, 0x00000007, hdaRegReadU16 , hdaRegWriteSDFIFOW , "OSD2FIFOW", "OSD2 FIFO Watermark" },
690 { 0x00150, 0x00002, 0x000000FF, 0x000000FF, hdaRegReadU16 , hdaRegWriteSDFIFOS , "OSD2FIFOS", "OSD2 FIFO Size" },
691 { 0x00152, 0x00002, 0x00007F7F, 0x00007F7F, hdaRegReadU16 , hdaRegWriteSDFMT , "OSD2FMT" , "OSD2 Format" },
692 { 0x00158, 0x00004, 0xFFFFFF80, 0xFFFFFF80, hdaRegReadU32 , hdaRegWriteSDBDPL , "OSD2BDPL" , "OSD2 Buffer Descriptor List Pointer-Lower Base Address" },
693 { 0x0015C, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteSDBDPU , "OSD2BDPU" , "OSD2 Buffer Descriptor List Pointer-Upper Base Address" },
694
695 { 0x00160, 0x00003, 0x00FF001F, 0x00F0001F, hdaRegReadU24 , hdaRegWriteSDCTL , "OSD3CTL" , "Input Stream Descriptor 0 (OSD3) Control" },
696 { 0x00163, 0x00001, 0x0000001C, 0x0000003C, hdaRegReadU8 , hdaRegWriteSDSTS , "OSD3STS" , "OSD3 Status" },
697 { 0x00164, 0x00004, 0xFFFFFFFF, 0x00000000, hdaRegReadU32 , hdaRegWriteU32 , "OSD3LPIB" , "OSD3 Link Position In Buffer" },
698 { 0x00168, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteU32 , "OSD3CBL" , "OSD3 Cyclic Buffer Length" },
699 { 0x0016C, 0x00002, 0x0000FFFF, 0x0000FFFF, hdaRegReadU16 , hdaRegWriteSDLVI , "OSD3LVI" , "OSD3 Last Valid Index" },
700 { 0x0016E, 0x00002, 0x00000007, 0x00000007, hdaRegReadU16 , hdaRegWriteSDFIFOW , "OSD3FIFOW", "OSD3 FIFO Watermark" },
701 { 0x00170, 0x00002, 0x000000FF, 0x000000FF, hdaRegReadU16 , hdaRegWriteSDFIFOS , "OSD3FIFOS", "OSD3 FIFO Size" },
702 { 0x00172, 0x00002, 0x00007F7F, 0x00007F7F, hdaRegReadU16 , hdaRegWriteSDFMT , "OSD3FMT" , "OSD3 Format" },
703 { 0x00178, 0x00004, 0xFFFFFF80, 0xFFFFFF80, hdaRegReadU32 , hdaRegWriteSDBDPL , "OSD3BDPL" , "OSD3 Buffer Descriptor List Pointer-Lower Base Address" },
704 { 0x0017C, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, hdaRegReadU32 , hdaRegWriteSDBDPU , "OSD3BDPU" , "OSD3 Buffer Descriptor List Pointer-Upper Base Address" },
705};
706
707/**
708 * HDA register aliases (HDA spec 3.3.45).
709 * @remarks Sorted by offReg.
710 */
711static const struct
712{
713 /** The alias register offset. */
714 uint32_t offReg;
715 /** The register index. */
716 int idxAlias;
717} g_aHdaRegAliases[] =
718{
719 { 0x2084, HDA_REG_IND_NAME(SD0LPIB) },
720 { 0x20a4, HDA_REG_IND_NAME(SD1LPIB) },
721 { 0x20c4, HDA_REG_IND_NAME(SD2LPIB) },
722 { 0x20e4, HDA_REG_IND_NAME(SD3LPIB) },
723 { 0x2104, HDA_REG_IND_NAME(SD4LPIB) },
724 { 0x2124, HDA_REG_IND_NAME(SD5LPIB) },
725 { 0x2144, HDA_REG_IND_NAME(SD6LPIB) },
726 { 0x2164, HDA_REG_IND_NAME(SD7LPIB) },
727};
728
729
730/** HDABDLEDESC field descriptors the v3+ saved state. */
731static SSMFIELD const g_aHdaBDLEDescFields[] =
732{
733 SSMFIELD_ENTRY( HDABDLEDESC, u64BdleCviAddr),
734 SSMFIELD_ENTRY( HDABDLEDESC, u32BdleMaxCvi),
735 SSMFIELD_ENTRY( HDABDLEDESC, u32BdleCvi),
736 SSMFIELD_ENTRY( HDABDLEDESC, u32BdleCviLen),
737 SSMFIELD_ENTRY( HDABDLEDESC, u32BdleCviPos),
738 SSMFIELD_ENTRY( HDABDLEDESC, fBdleCviIoc),
739 SSMFIELD_ENTRY( HDABDLEDESC, cbUnderFifoW),
740 SSMFIELD_ENTRY( HDABDLEDESC, au8HdaBuffer),
741 SSMFIELD_ENTRY_TERM()
742};
743
744/** HDABDLEDESC field descriptors the v1 and v2 saved state. */
745static SSMFIELD const g_aHdaBDLEDescFieldsOld[] =
746{
747 SSMFIELD_ENTRY( HDABDLEDESC, u64BdleCviAddr),
748 SSMFIELD_ENTRY( HDABDLEDESC, u32BdleMaxCvi),
749 SSMFIELD_ENTRY( HDABDLEDESC, u32BdleCvi),
750 SSMFIELD_ENTRY( HDABDLEDESC, u32BdleCviLen),
751 SSMFIELD_ENTRY( HDABDLEDESC, u32BdleCviPos),
752 SSMFIELD_ENTRY( HDABDLEDESC, fBdleCviIoc),
753 SSMFIELD_ENTRY_PAD_HC_AUTO(3, 3),
754 SSMFIELD_ENTRY( HDABDLEDESC, cbUnderFifoW),
755 SSMFIELD_ENTRY( HDABDLEDESC, au8HdaBuffer),
756 SSMFIELD_ENTRY_TERM()
757};
758
759/**
760 * 32-bit size indexed masks, i.e. g_afMasks[2 bytes] = 0xffff.
761 */
762static uint32_t const g_afMasks[5] =
763{
764 UINT32_C(0), UINT32_C(0x000000ff), UINT32_C(0x0000ffff), UINT32_C(0xffffff00), UINT32_C(0xffffffff)
765};
766
767
768DECLINLINE(void) hdaUpdatePosBuf(PHDASTATE pThis, PHDASTREAMTRANSFERDESC pStreamDesc)
769{
770 if (pThis->u64DPBase & DPBASE_ENABLED)
771 PDMDevHlpPhysWrite(pThis->pDevIns,
772 (pThis->u64DPBase & DPBASE_ADDR_MASK) + pStreamDesc->u8Strm * 8,
773 pStreamDesc->pu32Lpib, sizeof(uint32_t));
774}
775DECLINLINE(uint32_t) hdaFifoWToSz(PHDASTATE pThis, PHDASTREAMTRANSFERDESC pStreamDesc)
776{
777#if 0
778 switch(HDA_STREAM_REG2(pThis, FIFOW, pStreamDesc->u8Strm))
779 {
780 case HDA_SDFIFOW_8B: return 8;
781 case HDA_SDFIFOW_16B: return 16;
782 case HDA_SDFIFOW_32B: return 32;
783 default:
784 AssertMsgFailed(("hda: unsupported value (%x) in SDFIFOW(,%d)\n", HDA_REG_IND(pThis, pStreamDesc->u8Strm), pStreamDesc->u8Strm));
785 }
786#endif
787 return 0;
788}
789
790static int hdaProcessInterrupt(PHDASTATE pThis)
791{
792#define IS_INTERRUPT_OCCURED_AND_ENABLED(pThis, num) \
793 ( INTCTL_SX((pThis), num) \
794 && (SDSTS(pThis, num) & HDA_REG_FIELD_FLAG_MASK(SDSTS, BCIS)))
795 bool fIrq = false;
796 if ( INTCTL_CIE(pThis)
797 && ( RIRBSTS_RINTFL(pThis)
798 || RIRBSTS_RIRBOIS(pThis)
799 || (STATESTS(pThis) & WAKEEN(pThis))))
800 fIrq = true;
801
802 if ( IS_INTERRUPT_OCCURED_AND_ENABLED(pThis, 0)
803 || IS_INTERRUPT_OCCURED_AND_ENABLED(pThis, 4))
804 fIrq = true;
805
806 if (INTCTL_GIE(pThis))
807 {
808 Log(("hda: irq %s\n", fIrq ? "asserted" : "deasserted"));
809 PDMDevHlpPCISetIrq(pThis->pDevIns, 0 , fIrq);
810 }
811 return VINF_SUCCESS;
812}
813
814/**
815 * Looks up a register at the exact offset given by @a offReg.
816 *
817 * @returns Register index on success, -1 if not found.
818 * @param pThis The HDA device state.
819 * @param offReg The register offset.
820 */
821static int hdaRegLookup(PHDASTATE pThis, uint32_t offReg)
822{
823 /*
824 * Aliases.
825 */
826 if (offReg >= g_aHdaRegAliases[0].offReg)
827 {
828 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegAliases); i++)
829 if (offReg == g_aHdaRegAliases[i].offReg)
830 return g_aHdaRegAliases[i].idxAlias;
831 Assert(g_aHdaRegMap[RT_ELEMENTS(g_aHdaRegMap) - 1].offset < offReg);
832 return -1;
833 }
834
835 /*
836 * Binary search the
837 */
838 int idxEnd = RT_ELEMENTS(g_aHdaRegMap);
839 int idxLow = 0;
840 for (;;)
841 {
842 int idxMiddle = idxLow + (idxEnd - idxLow) / 2;
843 if (offReg < g_aHdaRegMap[idxMiddle].offset)
844 {
845 if (idxLow == idxMiddle)
846 break;
847 idxEnd = idxMiddle;
848 }
849 else if (offReg > g_aHdaRegMap[idxMiddle].offset)
850 {
851 idxLow = idxMiddle + 1;
852 if (idxLow >= idxEnd)
853 break;
854 }
855 else
856 return idxMiddle;
857 }
858
859#ifdef RT_STRICT
860 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegMap); i++)
861 Assert(g_aHdaRegMap[i].offset != offReg);
862#endif
863 return -1;
864}
865
866/**
867 * Looks up a register covering the offset given by @a offReg.
868 *
869 * @returns Register index on success, -1 if not found.
870 * @param pThis The HDA device state.
871 * @param offReg The register offset.
872 */
873static int hdaRegLookupWithin(PHDASTATE pThis, uint32_t offReg)
874{
875 /*
876 * Aliases.
877 */
878 if (offReg >= g_aHdaRegAliases[0].offReg)
879 {
880 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegAliases); i++)
881 {
882 uint32_t off = offReg - g_aHdaRegAliases[i].offReg;
883 if (off < 4 && off < g_aHdaRegMap[g_aHdaRegAliases[i].idxAlias].size)
884 return g_aHdaRegAliases[i].idxAlias;
885 }
886 Assert(g_aHdaRegMap[RT_ELEMENTS(g_aHdaRegMap) - 1].offset < offReg);
887 return -1;
888 }
889
890 /*
891 * Binary search the
892 */
893 int idxEnd = RT_ELEMENTS(g_aHdaRegMap);
894 int idxLow = 0;
895 for (;;)
896 {
897 int idxMiddle = idxLow + (idxEnd - idxLow) / 2;
898 if (offReg < g_aHdaRegMap[idxMiddle].offset)
899 {
900 if (idxLow == idxMiddle)
901 break;
902 idxEnd = idxMiddle;
903 }
904 else if (offReg >= g_aHdaRegMap[idxMiddle].offset + g_aHdaRegMap[idxMiddle].size)
905 {
906 idxLow = idxMiddle + 1;
907 if (idxLow >= idxEnd)
908 break;
909 }
910 else
911 return idxMiddle;
912 }
913
914#ifdef RT_STRICT
915 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegMap); i++)
916 Assert(offReg - g_aHdaRegMap[i].offset >= g_aHdaRegMap[i].size);
917#endif
918 return -1;
919}
920
921
922static int hdaCmdSync(PHDASTATE pThis, bool fLocal)
923{
924 int rc = VINF_SUCCESS;
925 if (fLocal)
926 {
927 Assert((HDA_REG_FLAG_VALUE(pThis, CORBCTL, DMA)));
928 rc = PDMDevHlpPhysRead(pThis->pDevIns, pThis->u64CORBBase, pThis->pu32CorbBuf, pThis->cbCorbBuf);
929 if (RT_FAILURE(rc))
930 AssertRCReturn(rc, rc);
931#ifdef DEBUG_CMD_BUFFER
932 uint8_t i = 0;
933 do
934 {
935 Log(("hda: corb%02x: ", i));
936 uint8_t j = 0;
937 do
938 {
939 const char *prefix;
940 if ((i + j) == CORBRP(pThis))
941 prefix = "[R]";
942 else if ((i + j) == CORBWP(pThis))
943 prefix = "[W]";
944 else
945 prefix = " "; /* three spaces */
946 Log(("%s%08x", prefix, pThis->pu32CorbBuf[i + j]));
947 j++;
948 } while (j < 8);
949 Log(("\n"));
950 i += 8;
951 } while(i != 0);
952#endif
953 }
954 else
955 {
956 Assert((HDA_REG_FLAG_VALUE(pThis, RIRBCTL, DMA)));
957 rc = PDMDevHlpPhysWrite(pThis->pDevIns, pThis->u64RIRBBase, pThis->pu64RirbBuf, pThis->cbRirbBuf);
958 if (RT_FAILURE(rc))
959 AssertRCReturn(rc, rc);
960#ifdef DEBUG_CMD_BUFFER
961 uint8_t i = 0;
962 do {
963 Log(("hda: rirb%02x: ", i));
964 uint8_t j = 0;
965 do {
966 const char *prefix;
967 if ((i + j) == RIRBWP(pThis))
968 prefix = "[W]";
969 else
970 prefix = " ";
971 Log((" %s%016lx", prefix, pThis->pu64RirbBuf[i + j]));
972 } while (++j < 8);
973 Log(("\n"));
974 i += 8;
975 } while (i != 0);
976#endif
977 }
978 return rc;
979}
980
981static int hdaCORBCmdProcess(PHDASTATE pThis)
982{
983 int rc;
984 uint8_t corbRp;
985 uint8_t corbWp;
986 uint8_t rirbWp;
987
988 PFNHDACODECVERBPROCESSOR pfn = (PFNHDACODECVERBPROCESSOR)NULL;
989
990 rc = hdaCmdSync(pThis, true);
991 if (RT_FAILURE(rc))
992 AssertRCReturn(rc, rc);
993 corbRp = CORBRP(pThis);
994 corbWp = CORBWP(pThis);
995 rirbWp = RIRBWP(pThis);
996 Assert((corbWp != corbRp));
997 Log(("hda: CORB(RP:%x, WP:%x) RIRBWP:%x\n", CORBRP(pThis), CORBWP(pThis), RIRBWP(pThis)));
998 while (corbRp != corbWp)
999 {
1000 uint32_t cmd;
1001 uint64_t resp;
1002 pfn = NULL;
1003 corbRp++;
1004 cmd = pThis->pu32CorbBuf[corbRp];
1005 rc = pThis->Codec.pfnLookup(&pThis->Codec, cmd, &pfn);
1006 if (RT_FAILURE(rc))
1007 AssertRCReturn(rc, rc);
1008 Assert(pfn);
1009 (rirbWp)++;
1010
1011 if (RT_LIKELY(pfn))
1012 rc = pfn(&pThis->Codec, cmd, &resp);
1013 else
1014 rc = VERR_INVALID_FUNCTION;
1015
1016 if (RT_FAILURE(rc))
1017 AssertRCReturn(rc, rc);
1018 Log(("hda: verb:%08x->%016lx\n", cmd, resp));
1019 if ( (resp & CODEC_RESPONSE_UNSOLICITED)
1020 && !HDA_REG_FLAG_VALUE(pThis, GCTL, UR))
1021 {
1022 Log(("hda: unexpected unsolicited response.\n"));
1023 pThis->au32Regs[ICH6_HDA_REG_CORBRP] = corbRp;
1024 return rc;
1025 }
1026 pThis->pu64RirbBuf[rirbWp] = resp;
1027 pThis->u8Counter++;
1028 if (pThis->u8Counter == RINTCNT_N(pThis))
1029 break;
1030 }
1031 pThis->au32Regs[ICH6_HDA_REG_CORBRP] = corbRp;
1032 pThis->au32Regs[ICH6_HDA_REG_RIRBWP] = rirbWp;
1033 rc = hdaCmdSync(pThis, false);
1034 Log(("hda: CORB(RP:%x, WP:%x) RIRBWP:%x\n", CORBRP(pThis), CORBWP(pThis), RIRBWP(pThis)));
1035 if (RIRBCTL_RIRB_RIC(pThis))
1036 {
1037 RIRBSTS((pThis)) |= HDA_REG_FIELD_FLAG_MASK(RIRBSTS,RINTFL);
1038 pThis->u8Counter = 0;
1039 rc = hdaProcessInterrupt(pThis);
1040 }
1041 if (RT_FAILURE(rc))
1042 AssertRCReturn(rc, rc);
1043 return rc;
1044}
1045
1046static void hdaStreamReset(PHDASTATE pThis, PHDABDLEDESC pBdle, PHDASTREAMTRANSFERDESC pStreamDesc, uint8_t u8Strm)
1047{
1048 Log(("hda: reset of stream (%d) started\n", u8Strm));
1049 Assert(( pThis
1050 && pBdle
1051 && pStreamDesc
1052 && u8Strm <= 7));
1053 memset(pBdle, 0, sizeof(HDABDLEDESC));
1054 *pStreamDesc->pu32Lpib = 0;
1055 *pStreamDesc->pu32Sts = 0;
1056 /* According to the ICH6 datasheet, 0x40000 is the default value for stream descriptor register 23:20
1057 * bits are reserved for stream number 18.2.33, resets SDnCTL except SRCT bit */
1058 HDA_STREAM_REG2(pThis, CTL, u8Strm) = 0x40000 | (HDA_STREAM_REG2(pThis, CTL, u8Strm) & HDA_REG_FIELD_FLAG_MASK(SDCTL, SRST));
1059
1060 /* ICH6 defines default values (0x77 for input and 0xBF for output descriptors) of FIFO size. 18.2.39 */
1061 HDA_STREAM_REG2(pThis, FIFOS, u8Strm) = u8Strm < 4 ? HDA_SDINFIFO_120B : HDA_SDONFIFO_192B;
1062 HDA_STREAM_REG2(pThis, FIFOW, u8Strm) = u8Strm < 4 ? HDA_SDFIFOW_8B : HDA_SDFIFOW_32B;
1063 HDA_STREAM_REG2(pThis, CBL, u8Strm) = 0;
1064 HDA_STREAM_REG2(pThis, LVI, u8Strm) = 0;
1065 HDA_STREAM_REG2(pThis, FMT, u8Strm) = 0;
1066 HDA_STREAM_REG2(pThis, BDPU, u8Strm) = 0;
1067 HDA_STREAM_REG2(pThis, BDPL, u8Strm) = 0;
1068 Log(("hda: reset of stream (%d) finished\n", u8Strm));
1069}
1070
1071
1072/* Register access handlers. */
1073
1074static int hdaRegReadUnimplemented(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
1075{
1076 *pu32Value = 0;
1077 return VINF_SUCCESS;
1078}
1079
1080static int hdaRegWriteUnimplemented(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1081{
1082 return VINF_SUCCESS;
1083}
1084
1085/* U8 */
1086static int hdaRegReadU8(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
1087{
1088 Assert(((pThis->au32Regs[iReg] & g_aHdaRegMap[iReg].readable) & 0xffffff00) == 0);
1089 return hdaRegReadU32(pThis, iReg, pu32Value);
1090}
1091
1092static int hdaRegWriteU8(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1093{
1094 Assert((u32Value & 0xffffff00) == 0);
1095 return hdaRegWriteU32(pThis, iReg, u32Value);
1096}
1097
1098/* U16 */
1099static int hdaRegReadU16(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
1100{
1101 Assert(((pThis->au32Regs[iReg] & g_aHdaRegMap[iReg].readable) & 0xffff0000) == 0);
1102 return hdaRegReadU32(pThis, iReg, pu32Value);
1103}
1104
1105static int hdaRegWriteU16(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1106{
1107 Assert((u32Value & 0xffff0000) == 0);
1108 return hdaRegWriteU32(pThis, iReg, u32Value);
1109}
1110
1111/* U24 */
1112static int hdaRegReadU24(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
1113{
1114 Assert(((pThis->au32Regs[iReg] & g_aHdaRegMap[iReg].readable) & 0xff000000) == 0);
1115 return hdaRegReadU32(pThis, iReg, pu32Value);
1116}
1117
1118static int hdaRegWriteU24(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1119{
1120 Assert((u32Value & 0xff000000) == 0);
1121 return hdaRegWriteU32(pThis, iReg, u32Value);
1122}
1123
1124/* U32 */
1125static int hdaRegReadU32(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
1126{
1127 *pu32Value = pThis->au32Regs[iReg] & g_aHdaRegMap[iReg].readable;
1128 return VINF_SUCCESS;
1129}
1130
1131static int hdaRegWriteU32(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1132{
1133 pThis->au32Regs[iReg] = (u32Value & g_aHdaRegMap[iReg].writable)
1134 | (pThis->au32Regs[iReg] & ~g_aHdaRegMap[iReg].writable);
1135 return VINF_SUCCESS;
1136}
1137
1138static int hdaRegReadGCTL(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
1139{
1140 return hdaRegReadU32(pThis, iReg, pu32Value);
1141}
1142
1143static int hdaRegWriteGCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1144{
1145 if (u32Value & HDA_REG_FIELD_FLAG_MASK(GCTL, RST))
1146 {
1147 /* exit reset state */
1148 GCTL(pThis) |= HDA_REG_FIELD_FLAG_MASK(GCTL, RST);
1149 pThis->fInReset = false;
1150 }
1151 else
1152 {
1153 /* enter reset state*/
1154 if ( HDA_REG_FLAG_VALUE(pThis, CORBCTL, DMA)
1155 || HDA_REG_FLAG_VALUE(pThis, RIRBCTL, DMA))
1156 {
1157 Log(("hda: HDA enters in reset with DMA(RIRB:%s, CORB:%s)\n",
1158 HDA_REG_FLAG_VALUE(pThis, CORBCTL, DMA) ? "on" : "off",
1159 HDA_REG_FLAG_VALUE(pThis, RIRBCTL, DMA) ? "on" : "off"));
1160 }
1161 hdaReset(pThis->pDevIns);
1162 GCTL(pThis) &= ~HDA_REG_FIELD_FLAG_MASK(GCTL, RST);
1163 pThis->fInReset = true;
1164 }
1165 if (u32Value & HDA_REG_FIELD_FLAG_MASK(GCTL, FSH))
1166 {
1167 /* Flush: GSTS:1 set, see 6.2.6*/
1168 GSTS(pThis) |= HDA_REG_FIELD_FLAG_MASK(GSTS, FSH); /* set the flush state */
1169 /* DPLBASE and DPUBASE should be initialized with initial value (see 6.2.6)*/
1170 }
1171 return VINF_SUCCESS;
1172}
1173
1174static int hdaRegWriteSTATESTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1175{
1176 uint32_t v = pThis->au32Regs[iReg];
1177 uint32_t nv = u32Value & ICH6_HDA_STATES_SCSF;
1178 pThis->au32Regs[iReg] &= ~(v & nv); /* write of 1 clears corresponding bit */
1179 return VINF_SUCCESS;
1180}
1181
1182static int hdaRegReadINTSTS(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
1183{
1184 uint32_t v = 0;
1185 if ( RIRBSTS_RIRBOIS(pThis)
1186 || RIRBSTS_RINTFL(pThis)
1187 || HDA_REG_FLAG_VALUE(pThis, CORBSTS, CMEI)
1188 || STATESTS(pThis))
1189 v |= RT_BIT(30);
1190#define HDA_IS_STREAM_EVENT(pThis, stream) \
1191 ( (SDSTS((pThis),stream) & HDA_REG_FIELD_FLAG_MASK(SDSTS, DE)) \
1192 || (SDSTS((pThis),stream) & HDA_REG_FIELD_FLAG_MASK(SDSTS, FE)) \
1193 || (SDSTS((pThis),stream) & HDA_REG_FIELD_FLAG_MASK(SDSTS, BCIS)))
1194#define MARK_STREAM(pThis, stream, v) do { (v) |= HDA_IS_STREAM_EVENT((pThis),stream) ? RT_BIT((stream)) : 0; } while(0)
1195 MARK_STREAM(pThis, 0, v);
1196 MARK_STREAM(pThis, 1, v);
1197 MARK_STREAM(pThis, 2, v);
1198 MARK_STREAM(pThis, 3, v);
1199 MARK_STREAM(pThis, 4, v);
1200 MARK_STREAM(pThis, 5, v);
1201 MARK_STREAM(pThis, 6, v);
1202 MARK_STREAM(pThis, 7, v);
1203 v |= v ? RT_BIT(31) : 0;
1204 *pu32Value = v;
1205 return VINF_SUCCESS;
1206}
1207
1208static int hdaRegReadWALCLK(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
1209{
1210 /* HDA spec (1a): 3.3.16 WALCLK counter ticks with 24Mhz bitclock rate. */
1211 *pu32Value = (uint32_t)ASMMultU64ByU32DivByU32(PDMDevHlpTMTimeVirtGetNano(pThis->pDevIns)
1212 - pThis->u64BaseTS, 24, 1000);
1213 return VINF_SUCCESS;
1214}
1215
1216static int hdaRegReadGCAP(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
1217{
1218 return hdaRegReadU16(pThis, iReg, pu32Value);
1219}
1220
1221static int hdaRegWriteCORBRP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1222{
1223 if (u32Value & HDA_REG_FIELD_FLAG_MASK(CORBRP, RST))
1224 CORBRP(pThis) = 0;
1225#ifndef BIRD_THINKS_CORBRP_IS_MOSTLY_RO
1226 else
1227 return hdaRegWriteU8(pThis, iReg, u32Value);
1228#endif
1229 return VINF_SUCCESS;
1230}
1231
1232static int hdaRegWriteCORBCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1233{
1234 int rc = hdaRegWriteU8(pThis, iReg, u32Value);
1235 AssertRC(rc);
1236 if ( CORBWP(pThis) != CORBRP(pThis)
1237 && HDA_REG_FLAG_VALUE(pThis, CORBCTL, DMA) != 0)
1238 return hdaCORBCmdProcess(pThis);
1239 return rc;
1240}
1241
1242static int hdaRegWriteCORBSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1243{
1244 uint32_t v = CORBSTS(pThis);
1245 CORBSTS(pThis) &= ~(v & u32Value);
1246 return VINF_SUCCESS;
1247}
1248
1249static int hdaRegWriteCORBWP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1250{
1251 int rc;
1252 rc = hdaRegWriteU16(pThis, iReg, u32Value);
1253 if (RT_FAILURE(rc))
1254 AssertRCReturn(rc, rc);
1255 if (CORBWP(pThis) == CORBRP(pThis))
1256 return VINF_SUCCESS;
1257 if (!HDA_REG_FLAG_VALUE(pThis, CORBCTL, DMA))
1258 return VINF_SUCCESS;
1259 rc = hdaCORBCmdProcess(pThis);
1260 return rc;
1261}
1262
1263static int hdaRegReadSDCTL(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
1264{
1265 return hdaRegReadU24(pThis, iReg, pu32Value);
1266}
1267
1268static int hdaRegWriteSDCTL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1269{
1270 bool fRun = RT_BOOL(u32Value & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN));
1271 bool fInRun = RT_BOOL(HDA_REG_IND(pThis, iReg) & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN));
1272 bool fReset = RT_BOOL(u32Value & HDA_REG_FIELD_FLAG_MASK(SDCTL, SRST));
1273 bool fInReset = RT_BOOL(HDA_REG_IND(pThis, iReg) & HDA_REG_FIELD_FLAG_MASK(SDCTL, SRST));
1274 int rc = VINF_SUCCESS;
1275 if (fInReset)
1276 {
1277 /* Assert!!! Guest is resetting HDA's stream, we're expecting guest will mark stream as exit
1278 * from reset
1279 */
1280 Assert((!fReset));
1281 Log(("hda: guest initiated exit of stream reset.\n"));
1282 goto l_done;
1283 }
1284 else if (fReset)
1285 {
1286 /*
1287 * Assert!!! ICH6 datasheet 18.2.33 says that RUN bit should be cleared before initiation of reset.
1288 */
1289 uint8_t u8Strm = 0;
1290 PHDABDLEDESC pBdle = NULL;
1291 HDASTREAMTRANSFERDESC StreamDesc;
1292 Assert((!fInRun && !fRun));
1293 switch (iReg)
1294 {
1295 case ICH6_HDA_REG_SD0CTL:
1296 u8Strm = 0;
1297 pBdle = &pThis->StInBdle;
1298 break;
1299 case ICH6_HDA_REG_SD4CTL:
1300 u8Strm = 4;
1301 pBdle = &pThis->StOutBdle;
1302 break;
1303 default:
1304 Log(("hda: changing SRST bit on non-attached stream\n"));
1305 goto l_done;
1306 }
1307 Log(("hda: guest initiated enter to stream reset.\n"));
1308 hdaInitTransferDescriptor(pThis, pBdle, u8Strm, &StreamDesc);
1309 hdaStreamReset(pThis, pBdle, &StreamDesc, u8Strm);
1310 goto l_done;
1311 }
1312
1313 /* we enter here to change DMA states only */
1314 if ( (fInRun && !fRun)
1315 || (fRun && !fInRun))
1316 {
1317 Assert((!fReset && !fInReset));
1318 switch (iReg)
1319 {
1320 case ICH6_HDA_REG_SD0CTL:
1321 AUD_set_active_in(pThis->Codec.SwVoiceIn, fRun);
1322 break;
1323 case ICH6_HDA_REG_SD4CTL:
1324 AUD_set_active_out(pThis->Codec.SwVoiceOut, fRun);
1325 break;
1326 default:
1327 Log(("hda: changing RUN bit on non-attached stream\n"));
1328 goto l_done;
1329 }
1330 }
1331
1332l_done:
1333 rc = hdaRegWriteU24(pThis, iReg, u32Value);
1334 if (RT_FAILURE(rc))
1335 AssertRCReturn(rc, VINF_SUCCESS);
1336 return rc;
1337}
1338
1339static int hdaRegWriteSDSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1340{
1341 uint32_t v = HDA_REG_IND(pThis, iReg);
1342 v &= ~(u32Value & v);
1343 HDA_REG_IND(pThis, iReg) = v;
1344 hdaProcessInterrupt(pThis);
1345 return VINF_SUCCESS;
1346}
1347
1348static int hdaRegWriteSDLVI(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1349{
1350 int rc = hdaRegWriteU32(pThis, iReg, u32Value);
1351 if (RT_FAILURE(rc))
1352 AssertRCReturn(rc, VINF_SUCCESS);
1353 return rc;
1354}
1355
1356static int hdaRegWriteSDFIFOW(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1357{
1358 switch (u32Value)
1359 {
1360 case HDA_SDFIFOW_8B:
1361 case HDA_SDFIFOW_16B:
1362 case HDA_SDFIFOW_32B:
1363 return hdaRegWriteU16(pThis, iReg, u32Value);
1364 default:
1365 Log(("hda: Attempt to store unsupported value(%x) in SDFIFOW\n", u32Value));
1366 return hdaRegWriteU16(pThis, iReg, HDA_SDFIFOW_32B);
1367 }
1368 return VINF_SUCCESS;
1369}
1370
1371/**
1372 * @note This method could be called for changing value on Output Streams
1373 * only (ICH6 datasheet 18.2.39)
1374 */
1375static int hdaRegWriteSDFIFOS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1376{
1377 switch (iReg)
1378 {
1379 /* SDInFIFOS is RO, n=0-3 */
1380 case ICH6_HDA_REG_SD0FIFOS:
1381 case ICH6_HDA_REG_SD1FIFOS:
1382 case ICH6_HDA_REG_SD2FIFOS:
1383 case ICH6_HDA_REG_SD3FIFOS:
1384 Log(("hda: Guest tries change value of FIFO size of Input Stream\n"));
1385 return VINF_SUCCESS;
1386 case ICH6_HDA_REG_SD4FIFOS:
1387 case ICH6_HDA_REG_SD5FIFOS:
1388 case ICH6_HDA_REG_SD6FIFOS:
1389 case ICH6_HDA_REG_SD7FIFOS:
1390 switch(u32Value)
1391 {
1392 case HDA_SDONFIFO_16B:
1393 case HDA_SDONFIFO_32B:
1394 case HDA_SDONFIFO_64B:
1395 case HDA_SDONFIFO_128B:
1396 case HDA_SDONFIFO_192B:
1397 return hdaRegWriteU16(pThis, iReg, u32Value);
1398
1399 case HDA_SDONFIFO_256B:
1400 Log(("hda: 256-bit is unsupported, HDA is switched into 192-bit mode\n"));
1401 default:
1402 return hdaRegWriteU16(pThis, iReg, HDA_SDONFIFO_192B);
1403 }
1404 return VINF_SUCCESS;
1405 default:
1406 AssertMsgFailed(("Something weird happened with register lookup routine"));
1407 }
1408 return VINF_SUCCESS;
1409}
1410
1411static void hdaSdFmtToAudSettings(uint32_t u32SdFmt, audsettings_t *pAudSetting)
1412{
1413 Assert((pAudSetting));
1414#define EXTRACT_VALUE(v, mask, shift) ((v & ((mask) << (shift))) >> (shift))
1415 uint32_t u32Hz = (u32SdFmt & ICH6_HDA_SDFMT_BASE_RATE_SHIFT) ? 44100 : 48000;
1416 uint32_t u32HzMult = 1;
1417 uint32_t u32HzDiv = 1;
1418 switch (EXTRACT_VALUE(u32SdFmt, ICH6_HDA_SDFMT_MULT_MASK, ICH6_HDA_SDFMT_MULT_SHIFT))
1419 {
1420 case 0: u32HzMult = 1; break;
1421 case 1: u32HzMult = 2; break;
1422 case 2: u32HzMult = 3; break;
1423 case 3: u32HzMult = 4; break;
1424 default:
1425 Log(("hda: unsupported multiplier %x\n", u32SdFmt));
1426 }
1427 switch (EXTRACT_VALUE(u32SdFmt, ICH6_HDA_SDFMT_DIV_MASK, ICH6_HDA_SDFMT_DIV_SHIFT))
1428 {
1429 case 0: u32HzDiv = 1; break;
1430 case 1: u32HzDiv = 2; break;
1431 case 2: u32HzDiv = 3; break;
1432 case 3: u32HzDiv = 4; break;
1433 case 4: u32HzDiv = 5; break;
1434 case 5: u32HzDiv = 6; break;
1435 case 6: u32HzDiv = 7; break;
1436 case 7: u32HzDiv = 8; break;
1437 }
1438 pAudSetting->freq = u32Hz * u32HzMult / u32HzDiv;
1439
1440 switch (EXTRACT_VALUE(u32SdFmt, ICH6_HDA_SDFMT_BITS_MASK, ICH6_HDA_SDFMT_BITS_SHIFT))
1441 {
1442 case 0:
1443 Log(("hda: %s requested 8-bit\n", __FUNCTION__));
1444 pAudSetting->fmt = AUD_FMT_S8;
1445 break;
1446 case 1:
1447 Log(("hda: %s requested 16-bit\n", __FUNCTION__));
1448 pAudSetting->fmt = AUD_FMT_S16;
1449 break;
1450 case 2:
1451 Log(("hda: %s requested 20-bit\n", __FUNCTION__));
1452 break;
1453 case 3:
1454 Log(("hda: %s requested 24-bit\n", __FUNCTION__));
1455 break;
1456 case 4:
1457 Log(("hda: %s requested 32-bit\n", __FUNCTION__));
1458 pAudSetting->fmt = AUD_FMT_S32;
1459 break;
1460 default:
1461 AssertMsgFailed(("Unsupported"));
1462 }
1463 pAudSetting->nchannels = (u32SdFmt & 0xf) + 1;
1464 pAudSetting->fmt = AUD_FMT_S16;
1465 pAudSetting->endianness = 0;
1466#undef EXTRACT_VALUE
1467}
1468
1469static int hdaRegWriteSDFMT(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1470{
1471#ifdef VBOX_WITH_HDA_CODEC_EMU
1472 /** @todo a bit more investigation is required here. */
1473 int rc = 0;
1474 audsettings_t as;
1475 /* no reason to reopen voice with same settings */
1476 if (u32Value == HDA_REG_IND(pThis, iReg))
1477 return VINF_SUCCESS;
1478 hdaSdFmtToAudSettings(u32Value, &as);
1479 switch (iReg)
1480 {
1481 case ICH6_HDA_REG_SD0FMT:
1482 rc = hdaCodecOpenVoice(&pThis->Codec, PI_INDEX, &as);
1483 break;
1484 case ICH6_HDA_REG_SD4FMT:
1485 rc = hdaCodecOpenVoice(&pThis->Codec, PO_INDEX, &as);
1486 break;
1487 default:
1488 Log(("HDA: attempt to change format on %d\n", iReg));
1489 rc = 0;
1490 }
1491 return hdaRegWriteU16(pThis, iReg, u32Value);
1492#else
1493 return hdaRegWriteU16(pThis, iReg, u32Value);
1494#endif
1495}
1496
1497static int hdaRegWriteSDBDPL(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1498{
1499 int rc = hdaRegWriteU32(pThis, iReg, u32Value);
1500 if (RT_FAILURE(rc))
1501 AssertRCReturn(rc, VINF_SUCCESS);
1502 return rc;
1503}
1504
1505static int hdaRegWriteSDBDPU(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1506{
1507 int rc = hdaRegWriteU32(pThis, iReg, u32Value);
1508 if (RT_FAILURE(rc))
1509 AssertRCReturn(rc, VINF_SUCCESS);
1510 return rc;
1511}
1512
1513static int hdaRegReadIRS(PHDASTATE pThis, uint32_t iReg, uint32_t *pu32Value)
1514{
1515 int rc = VINF_SUCCESS;
1516 /* regarding 3.4.3 we should mark IRS as busy in case CORB is active */
1517 if ( CORBWP(pThis) != CORBRP(pThis)
1518 || HDA_REG_FLAG_VALUE(pThis, CORBCTL, DMA))
1519 IRS(pThis) = HDA_REG_FIELD_FLAG_MASK(IRS, ICB); /* busy */
1520
1521 rc = hdaRegReadU32(pThis, iReg, pu32Value);
1522 return rc;
1523}
1524
1525static int hdaRegWriteIRS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1526{
1527 int rc = VINF_SUCCESS;
1528 PFNHDACODECVERBPROCESSOR pfn = NULL;
1529 uint64_t resp;
1530
1531 /*
1532 * if guest set the ICB bit of IRS register, HDA should process the verb in IC register,
1533 * write the response to IR register, and set the IRV (valid in case of success) bit of IRS register.
1534 */
1535 if ( u32Value & HDA_REG_FIELD_FLAG_MASK(IRS, ICB)
1536 && !IRS_ICB(pThis))
1537 {
1538 uint32_t cmd = IC(pThis);
1539 if (CORBWP(pThis) != CORBRP(pThis))
1540 {
1541 /*
1542 * 3.4.3 defines behavior of immediate Command status register.
1543 */
1544 LogRel(("hda: guest attempted process immediate verb (%x) with active CORB\n", cmd));
1545 return rc;
1546 }
1547 IRS(pThis) = HDA_REG_FIELD_FLAG_MASK(IRS, ICB); /* busy */
1548 Log(("hda: IC:%x\n", cmd));
1549 rc = pThis->Codec.pfnLookup(&pThis->Codec, cmd, &pfn);
1550 if (RT_FAILURE(rc))
1551 AssertRCReturn(rc, rc);
1552 rc = pfn(&pThis->Codec, cmd, &resp);
1553 if (RT_FAILURE(rc))
1554 AssertRCReturn(rc, rc);
1555 IR(pThis) = (uint32_t)resp;
1556 Log(("hda: IR:%x\n", IR(pThis)));
1557 IRS(pThis) = HDA_REG_FIELD_FLAG_MASK(IRS, IRV); /* result is ready */
1558 IRS(pThis) &= ~HDA_REG_FIELD_FLAG_MASK(IRS, ICB); /* busy is clear */
1559 return rc;
1560 }
1561 /*
1562 * Once the guest read the response, it should clean the IRV bit of the IRS register.
1563 */
1564 if ( u32Value & HDA_REG_FIELD_FLAG_MASK(IRS, IRV)
1565 && IRS_IRV(pThis))
1566 IRS(pThis) &= ~HDA_REG_FIELD_FLAG_MASK(IRS, IRV);
1567 return rc;
1568}
1569
1570static int hdaRegWriteRIRBWP(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1571{
1572 if (u32Value & HDA_REG_FIELD_FLAG_MASK(RIRBWP, RST))
1573 {
1574 RIRBWP(pThis) = 0;
1575 }
1576 /* The remaining bits are O, see 6.2.22 */
1577 return VINF_SUCCESS;
1578}
1579
1580static int hdaRegWriteBase(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1581{
1582 int rc = hdaRegWriteU32(pThis, iReg, u32Value);
1583 if (RT_FAILURE(rc))
1584 AssertRCReturn(rc, rc);
1585 switch(iReg)
1586 {
1587 case ICH6_HDA_REG_CORBLBASE:
1588 pThis->u64CORBBase &= UINT64_C(0xFFFFFFFF00000000);
1589 pThis->u64CORBBase |= pThis->au32Regs[iReg];
1590 break;
1591 case ICH6_HDA_REG_CORBUBASE:
1592 pThis->u64CORBBase &= UINT64_C(0x00000000FFFFFFFF);
1593 pThis->u64CORBBase |= ((uint64_t)pThis->au32Regs[iReg] << 32);
1594 break;
1595 case ICH6_HDA_REG_RIRLBASE:
1596 pThis->u64RIRBBase &= UINT64_C(0xFFFFFFFF00000000);
1597 pThis->u64RIRBBase |= pThis->au32Regs[iReg];
1598 break;
1599 case ICH6_HDA_REG_RIRUBASE:
1600 pThis->u64RIRBBase &= UINT64_C(0x00000000FFFFFFFF);
1601 pThis->u64RIRBBase |= ((uint64_t)pThis->au32Regs[iReg] << 32);
1602 break;
1603 case ICH6_HDA_REG_DPLBASE:
1604 /** @todo: first bit has special meaning */
1605 pThis->u64DPBase &= UINT64_C(0xFFFFFFFF00000000);
1606 pThis->u64DPBase |= pThis->au32Regs[iReg];
1607 break;
1608 case ICH6_HDA_REG_DPUBASE:
1609 pThis->u64DPBase &= UINT64_C(0x00000000FFFFFFFF);
1610 pThis->u64DPBase |= ((uint64_t)pThis->au32Regs[iReg] << 32);
1611 break;
1612 default:
1613 AssertMsgFailed(("Invalid index"));
1614 }
1615 Log(("hda: CORB base:%llx RIRB base: %llx DP base: %llx\n", pThis->u64CORBBase, pThis->u64RIRBBase, pThis->u64DPBase));
1616 return rc;
1617}
1618
1619static int hdaRegWriteRIRBSTS(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
1620{
1621 uint8_t v = RIRBSTS(pThis);
1622 RIRBSTS(pThis) &= ~(v & u32Value);
1623
1624 return hdaProcessInterrupt(pThis);
1625}
1626
1627#ifdef LOG_ENABLED
1628static void dump_bd(PHDASTATE pThis, PHDABDLEDESC pBdle, uint64_t u64BaseDMA)
1629{
1630#if 0
1631 uint64_t addr;
1632 uint32_t len;
1633 uint32_t ioc;
1634 uint8_t bdle[16];
1635 uint32_t counter;
1636 uint32_t i;
1637 uint32_t sum = 0;
1638 Assert(pBdle && pBdle->u32BdleMaxCvi);
1639 for (i = 0; i <= pBdle->u32BdleMaxCvi; ++i)
1640 {
1641 PDMDevHlpPhysRead(pThis->pDevIns, u64BaseDMA + i*16, bdle, 16);
1642 addr = *(uint64_t *)bdle;
1643 len = *(uint32_t *)&bdle[8];
1644 ioc = *(uint32_t *)&bdle[12];
1645 Log(("hda: %s bdle[%d] a:%llx, len:%d, ioc:%d\n", (i == pBdle->u32BdleCvi? "[C]": " "), i, addr, len, ioc & 0x1));
1646 sum += len;
1647 }
1648 Log(("hda: sum: %d\n", sum));
1649 for (i = 0; i < 8; ++i)
1650 {
1651 PDMDevHlpPhysRead(pThis->pDevIns, (pThis->u64DPBase & DPBASE_ADDR_MASK) + i*8, &counter, sizeof(&counter));
1652 Log(("hda: %s stream[%d] counter=%x\n", i == SDCTL_NUM(pThis, 4) || i == SDCTL_NUM(pThis, 0)? "[C]": " ",
1653 i , counter));
1654 }
1655#endif
1656}
1657#endif
1658
1659static void hdaFetchBdle(PHDASTATE pThis, PHDABDLEDESC pBdle, PHDASTREAMTRANSFERDESC pStreamDesc)
1660{
1661 uint8_t bdle[16];
1662 Assert(( pStreamDesc->u64BaseDMA
1663 && pBdle
1664 && pBdle->u32BdleMaxCvi));
1665 PDMDevHlpPhysRead(pThis->pDevIns, pStreamDesc->u64BaseDMA + pBdle->u32BdleCvi*16, bdle, 16);
1666 pBdle->u64BdleCviAddr = *(uint64_t *)bdle;
1667 pBdle->u32BdleCviLen = *(uint32_t *)&bdle[8];
1668 pBdle->fBdleCviIoc = (*(uint32_t *)&bdle[12]) & 0x1;
1669#ifdef LOG_ENABLED
1670 dump_bd(pThis, pBdle, pStreamDesc->u64BaseDMA);
1671#endif
1672}
1673
1674DECLINLINE(uint32_t) hdaCalculateTransferBufferLength(PHDABDLEDESC pBdle, PHDASTREAMTRANSFERDESC pStreamDesc,
1675 uint32_t u32SoundBackendBufferBytesAvail, uint32_t u32CblLimit)
1676{
1677 uint32_t cb2Copy;
1678 /*
1679 * Number of bytes depends on the current position in buffer (u32BdleCviLen-u32BdleCviPos)
1680 */
1681 Assert((pBdle->u32BdleCviLen >= pBdle->u32BdleCviPos)); /* sanity */
1682 cb2Copy = pBdle->u32BdleCviLen - pBdle->u32BdleCviPos;
1683 /*
1684 * we may increase the counter in range of [0, FIFOS + 1]
1685 */
1686 cb2Copy = RT_MIN(cb2Copy, pStreamDesc->u32Fifos + 1);
1687 Assert((u32SoundBackendBufferBytesAvail > 0));
1688
1689 /* sanity check to avoid overriding the backend audio buffer */
1690 cb2Copy = RT_MIN(cb2Copy, u32SoundBackendBufferBytesAvail);
1691 cb2Copy = RT_MIN(cb2Copy, u32CblLimit);
1692
1693 if (cb2Copy <= pBdle->cbUnderFifoW)
1694 return 0;
1695 cb2Copy -= pBdle->cbUnderFifoW; /* forcibly reserve the amount of unreported bytes to copy */
1696 return cb2Copy;
1697}
1698
1699DECLINLINE(void) hdaBackendWriteTransferReported(PHDABDLEDESC pBdle, uint32_t cbArranged2Copy, uint32_t cbCopied,
1700 uint32_t *pu32DMACursor, uint32_t *pu32BackendBufferCapacity)
1701{
1702 Log(("hda:hdaBackendWriteTransferReported: cbArranged2Copy: %d, cbCopied: %d, pu32DMACursor: %d, pu32BackendBufferCapacity:%d\n",
1703 cbArranged2Copy, cbCopied, pu32DMACursor ? *pu32DMACursor : 0, pu32BackendBufferCapacity ? *pu32BackendBufferCapacity : 0));
1704 Assert((cbCopied));
1705 Assert((pu32BackendBufferCapacity && *pu32BackendBufferCapacity));
1706 /* Assertion!!! Fewer than cbUnderFifoW bytes were copied.
1707 * Probably we need to move the buffer, but it is rather hard to imagine a situation
1708 * where it might happen.
1709 */
1710 Assert((cbCopied == pBdle->cbUnderFifoW + cbArranged2Copy)); /* we assume that we write the entire buffer including unreported bytes */
1711 if ( pBdle->cbUnderFifoW
1712 && pBdle->cbUnderFifoW <= cbCopied)
1713 Log(("hda:hdaBackendWriteTransferReported: CVI resetting cbUnderFifoW:%d(pos:%d, len:%d)\n", pBdle->cbUnderFifoW, pBdle->u32BdleCviPos, pBdle->u32BdleCviLen));
1714
1715 pBdle->cbUnderFifoW -= RT_MIN(pBdle->cbUnderFifoW, cbCopied);
1716 Assert((!pBdle->cbUnderFifoW)); /* Assert!!! Incorrect assumption */
1717
1718 /* We always increment the position of DMA buffer counter because we're always reading into an intermediate buffer */
1719 pBdle->u32BdleCviPos += cbArranged2Copy;
1720
1721 Assert((pBdle->u32BdleCviLen >= pBdle->u32BdleCviPos && *pu32BackendBufferCapacity >= cbCopied)); /* sanity */
1722 /* We report all bytes (including previously unreported bytes) */
1723 *pu32DMACursor += cbCopied;
1724 /* Decrease the backend counter by the number of bytes we copied to the backend */
1725 *pu32BackendBufferCapacity -= cbCopied;
1726 Log(("hda:hdaBackendWriteTransferReported: CVI(pos:%d, len:%d), pu32DMACursor: %d, pu32BackendBufferCapacity:%d\n",
1727 pBdle->u32BdleCviPos, pBdle->u32BdleCviLen, *pu32DMACursor, *pu32BackendBufferCapacity));
1728}
1729
1730DECLINLINE(void) hdaBackendReadTransferReported(PHDABDLEDESC pBdle, uint32_t cbArranged2Copy, uint32_t cbCopied,
1731 uint32_t *pu32DMACursor, uint32_t *pu32BackendBufferCapacity)
1732{
1733 Assert((cbCopied, cbArranged2Copy));
1734 *pu32BackendBufferCapacity -= cbCopied;
1735 pBdle->u32BdleCviPos += cbCopied;
1736 Log(("hda:hdaBackendReadTransferReported: CVI resetting cbUnderFifoW:%d(pos:%d, len:%d)\n", pBdle->cbUnderFifoW, pBdle->u32BdleCviPos, pBdle->u32BdleCviLen));
1737 *pu32DMACursor += cbCopied + pBdle->cbUnderFifoW;
1738 pBdle->cbUnderFifoW = 0;
1739 Log(("hda:hdaBackendReadTransferReported: CVI(pos:%d, len:%d), pu32DMACursor: %d, pu32BackendBufferCapacity:%d\n",
1740 pBdle->u32BdleCviPos, pBdle->u32BdleCviLen, pu32DMACursor ? *pu32DMACursor : 0, pu32BackendBufferCapacity ? *pu32BackendBufferCapacity : 0));
1741}
1742
1743DECLINLINE(void) hdaBackendTransferUnreported(PHDASTATE pThis, PHDABDLEDESC pBdle, PHDASTREAMTRANSFERDESC pStreamDesc,
1744 uint32_t cbCopied, uint32_t *pu32BackendBufferCapacity)
1745{
1746 Log(("hda:hdaBackendTransferUnreported: CVI (cbUnderFifoW:%d, pos:%d, len:%d)\n", pBdle->cbUnderFifoW, pBdle->u32BdleCviPos, pBdle->u32BdleCviLen));
1747 pBdle->u32BdleCviPos += cbCopied;
1748 pBdle->cbUnderFifoW += cbCopied;
1749 /* In case of a read transaction we're always copying from the backend buffer */
1750 if (pu32BackendBufferCapacity)
1751 *pu32BackendBufferCapacity -= cbCopied;
1752 Log(("hda:hdaBackendTransferUnreported: CVI (cbUnderFifoW:%d, pos:%d, len:%d)\n", pBdle->cbUnderFifoW, pBdle->u32BdleCviPos, pBdle->u32BdleCviLen));
1753 Assert((pBdle->cbUnderFifoW <= hdaFifoWToSz(pThis, pStreamDesc)));
1754}
1755
1756DECLINLINE(bool) hdaIsTransferCountersOverlapped(PHDASTATE pThis, PHDABDLEDESC pBdle, PHDASTREAMTRANSFERDESC pStreamDesc)
1757{
1758 bool fOnBufferEdge = ( *pStreamDesc->pu32Lpib == pStreamDesc->u32Cbl
1759 || pBdle->u32BdleCviPos == pBdle->u32BdleCviLen);
1760
1761 Assert((*pStreamDesc->pu32Lpib <= pStreamDesc->u32Cbl));
1762
1763 if (*pStreamDesc->pu32Lpib == pStreamDesc->u32Cbl)
1764 *pStreamDesc->pu32Lpib -= pStreamDesc->u32Cbl;
1765 hdaUpdatePosBuf(pThis, pStreamDesc);
1766
1767 /* don't touch BdleCvi counter on uninitialized descriptor */
1768 if ( pBdle->u32BdleCviPos
1769 && pBdle->u32BdleCviPos == pBdle->u32BdleCviLen)
1770 {
1771 pBdle->u32BdleCviPos = 0;
1772 pBdle->u32BdleCvi++;
1773 if (pBdle->u32BdleCvi == pBdle->u32BdleMaxCvi + 1)
1774 pBdle->u32BdleCvi = 0;
1775 }
1776 return fOnBufferEdge;
1777}
1778
1779DECLINLINE(void) hdaStreamCounterUpdate(PHDASTATE pThis, PHDABDLEDESC pBdle, PHDASTREAMTRANSFERDESC pStreamDesc,
1780 uint32_t cbInc)
1781{
1782 /*
1783 * if we're below the FIFO Watermark, it's expected that HDA doesn't fetch anything.
1784 * (ICH6 datasheet 18.2.38)
1785 */
1786 if (!pBdle->cbUnderFifoW)
1787 {
1788 *pStreamDesc->pu32Lpib += cbInc;
1789
1790 /*
1791 * Assert. The buffer counters should never overlap.
1792 */
1793 Assert((*pStreamDesc->pu32Lpib <= pStreamDesc->u32Cbl));
1794
1795 hdaUpdatePosBuf(pThis, pStreamDesc);
1796
1797 }
1798}
1799
1800static bool hdaDoNextTransferCycle(PHDASTATE pThis, PHDABDLEDESC pBdle, PHDASTREAMTRANSFERDESC pStreamDesc)
1801{
1802 bool fDoNextTransferLoop = true;
1803 if ( pBdle->u32BdleCviPos == pBdle->u32BdleCviLen
1804 || *pStreamDesc->pu32Lpib == pStreamDesc->u32Cbl)
1805 {
1806 if ( !pBdle->cbUnderFifoW
1807 && pBdle->fBdleCviIoc)
1808 {
1809 /**
1810 * @todo - more carefully investigate BCIS flag.
1811 * Speech synthesis works fine on Mac Guest if this bit isn't set
1812 * but in general sound quality gets worse.
1813 */
1814 *pStreamDesc->pu32Sts |= HDA_REG_FIELD_FLAG_MASK(SDSTS, BCIS);
1815
1816 /*
1817 * we should generate the interrupt if ICE bit of SDCTL register is set.
1818 */
1819 if (pStreamDesc->u32Ctl & HDA_REG_FIELD_FLAG_MASK(SDCTL, ICE))
1820 hdaProcessInterrupt(pThis);
1821 }
1822 fDoNextTransferLoop = false;
1823 }
1824 return fDoNextTransferLoop;
1825}
1826
1827/*
1828 * hdaReadAudio - copies samples from audio backend to DMA.
1829 * Note: this function writes to the DMA buffer immediately, but "reports bytes" when all conditions are met (FIFOW)
1830 */
1831static uint32_t hdaReadAudio(PHDASTATE pThis, PHDASTREAMTRANSFERDESC pStreamDesc, uint32_t *pu32Avail, bool *fStop, uint32_t u32CblLimit)
1832{
1833 PHDABDLEDESC pBdle = &pThis->StInBdle;
1834 uint32_t cbTransferred = 0;
1835 uint32_t cb2Copy = 0;
1836 uint32_t cbBackendCopy = 0;
1837
1838 Log(("hda:ra: CVI(pos:%d, len:%d)\n", pBdle->u32BdleCviPos, pBdle->u32BdleCviLen));
1839
1840 cb2Copy = hdaCalculateTransferBufferLength(pBdle, pStreamDesc, *pu32Avail, u32CblLimit);
1841 if (!cb2Copy)
1842 {
1843 /* if we enter here we can't report "unreported bits" */
1844 *fStop = true;
1845 goto l_done;
1846 }
1847
1848
1849 /*
1850 * read from backend input line to the last unreported position or at the begining.
1851 */
1852 cbBackendCopy = AUD_read(pThis->Codec.SwVoiceIn, pBdle->au8HdaBuffer, cb2Copy);
1853 /*
1854 * write the HDA DMA buffer
1855 */
1856 PDMDevHlpPhysWrite(pThis->pDevIns, pBdle->u64BdleCviAddr + pBdle->u32BdleCviPos, pBdle->au8HdaBuffer, cbBackendCopy);
1857
1858 /* Don't see any reason why cb2Copy would differ from cbBackendCopy */
1859 Assert((cbBackendCopy == cb2Copy && (*pu32Avail) >= cb2Copy)); /* sanity */
1860
1861 if (pBdle->cbUnderFifoW + cbBackendCopy > hdaFifoWToSz(pThis, 0))
1862 hdaBackendReadTransferReported(pBdle, cb2Copy, cbBackendCopy, &cbTransferred, pu32Avail);
1863 else
1864 {
1865 hdaBackendTransferUnreported(pThis, pBdle, pStreamDesc, cbBackendCopy, pu32Avail);
1866 *fStop = true;
1867 }
1868l_done:
1869 Assert((cbTransferred <= (SDFIFOS(pThis, 0) + 1)));
1870 Log(("hda:ra: CVI(pos:%d, len:%d) cbTransferred: %d\n", pBdle->u32BdleCviPos, pBdle->u32BdleCviLen, cbTransferred));
1871 return cbTransferred;
1872}
1873
1874static uint32_t hdaWriteAudio(PHDASTATE pThis, PHDASTREAMTRANSFERDESC pStreamDesc, uint32_t *pu32Avail, bool *fStop, uint32_t u32CblLimit)
1875{
1876 PHDABDLEDESC pBdle = &pThis->StOutBdle;
1877 uint32_t cbTransferred = 0;
1878 uint32_t cb2Copy = 0; /* local byte counter (on local buffer) */
1879 uint32_t cbBackendCopy = 0; /* local byte counter, how many bytes copied to backend */
1880
1881 Log(("hda:wa: CVI(cvi:%d, pos:%d, len:%d)\n", pBdle->u32BdleCvi, pBdle->u32BdleCviPos, pBdle->u32BdleCviLen));
1882
1883 cb2Copy = hdaCalculateTransferBufferLength(pBdle, pStreamDesc, *pu32Avail, u32CblLimit);
1884
1885 /*
1886 * Copy from DMA to the corresponding hdaBuffer (if there are any bytes from the
1887 * previous unreported transfer we write at offset 'pBdle->cbUnderFifoW').
1888 */
1889 if (!cb2Copy)
1890 {
1891 *fStop = true;
1892 goto l_done;
1893 }
1894
1895 PDMDevHlpPhysRead(pThis->pDevIns, pBdle->u64BdleCviAddr + pBdle->u32BdleCviPos, pBdle->au8HdaBuffer + pBdle->cbUnderFifoW, cb2Copy);
1896 /*
1897 * Write to audio backend. we should ensure that we have enough bytes to copy to the backend.
1898 */
1899 if (cb2Copy + pBdle->cbUnderFifoW >= hdaFifoWToSz(pThis, pStreamDesc))
1900 {
1901 /*
1902 * Feed the newly fetched samples, including unreported ones, to the backend.
1903 */
1904 cbBackendCopy = AUD_write (pThis->Codec.SwVoiceOut, pBdle->au8HdaBuffer, cb2Copy + pBdle->cbUnderFifoW);
1905 hdaBackendWriteTransferReported(pBdle, cb2Copy, cbBackendCopy, &cbTransferred, pu32Avail);
1906 }
1907 else
1908 {
1909 /* Not enough bytes to be processed and reported, we'll try our luck next time around */
1910 hdaBackendTransferUnreported(pThis, pBdle, pStreamDesc, cb2Copy, NULL);
1911 *fStop = true;
1912 }
1913
1914l_done:
1915 Assert(cbTransferred <= SDFIFOS(pThis, 4) + 1);
1916 Log(("hda:wa: CVI(pos:%d, len:%d, cbTransferred:%d)\n", pBdle->u32BdleCviPos, pBdle->u32BdleCviLen, cbTransferred));
1917 return cbTransferred;
1918}
1919
1920/**
1921 * @interface_method_impl{HDACODEC,pfnReset}
1922 */
1923DECLCALLBACK(int) hdaCodecReset(PHDACODEC pCodec)
1924{
1925 PHDASTATE pThis = (PHDASTATE)pCodec->pvHDAState;
1926 NOREF(pThis);
1927 return VINF_SUCCESS;
1928}
1929
1930DECLINLINE(void) hdaInitTransferDescriptor(PHDASTATE pThis, PHDABDLEDESC pBdle, uint8_t u8Strm,
1931 PHDASTREAMTRANSFERDESC pStreamDesc)
1932{
1933 Assert(pThis); Assert(pBdle); Assert(pStreamDesc); Assert(u8Strm <= 7);
1934
1935 memset(pStreamDesc, 0, sizeof(HDASTREAMTRANSFERDESC));
1936 pStreamDesc->u8Strm = u8Strm;
1937 pStreamDesc->u32Ctl = HDA_STREAM_REG2(pThis, CTL, u8Strm);
1938 pStreamDesc->u64BaseDMA = RT_MAKE_U64(HDA_STREAM_REG2(pThis, BDPL, u8Strm),
1939 HDA_STREAM_REG2(pThis, BDPU, u8Strm));
1940 pStreamDesc->pu32Lpib = &HDA_STREAM_REG2(pThis, LPIB, u8Strm);
1941 pStreamDesc->pu32Sts = &HDA_STREAM_REG2(pThis, STS, u8Strm);
1942 pStreamDesc->u32Cbl = HDA_STREAM_REG2(pThis, CBL, u8Strm);
1943 pStreamDesc->u32Fifos = HDA_STREAM_REG2(pThis, FIFOS, u8Strm);
1944
1945 pBdle->u32BdleMaxCvi = HDA_STREAM_REG2(pThis, LVI, u8Strm);
1946
1947#ifdef LOG_ENABLED
1948 if ( pBdle
1949 && pBdle->u32BdleMaxCvi)
1950 {
1951 Log(("Initialization of transfer descriptor:\n"));
1952 dump_bd(pThis, pBdle, pStreamDesc->u64BaseDMA);
1953 }
1954#endif
1955}
1956
1957
1958/**
1959 * @interface_method_impl{HDACODEC,pfnTransfer}
1960 */
1961static DECLCALLBACK(void) hdaTransfer(PHDACODEC pCodec, ENMSOUNDSOURCE src, int avail)
1962{
1963 PHDASTATE pThis = (PHDASTATE)pCodec->pvHDAState;
1964 uint8_t u8Strm = 0;
1965 PHDABDLEDESC pBdle = NULL;
1966
1967 switch (src)
1968 {
1969 case PO_INDEX:
1970 {
1971 u8Strm = 4;
1972 pBdle = &pThis->StOutBdle;
1973 break;
1974 }
1975 case PI_INDEX:
1976 {
1977 u8Strm = 0;
1978 pBdle = &pThis->StInBdle;
1979 break;
1980 }
1981 default:
1982 return;
1983 }
1984
1985 HDASTREAMTRANSFERDESC StreamDesc;
1986 hdaInitTransferDescriptor(pThis, pBdle, u8Strm, &StreamDesc);
1987
1988 bool fStop = false;
1989 while (avail && !fStop)
1990 {
1991 Assert( (StreamDesc.u32Ctl & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN))
1992 && avail
1993 && StreamDesc.u64BaseDMA);
1994
1995 /* Fetch the Buffer Descriptor Entry (BDE). */
1996
1997 if (hdaIsTransferCountersOverlapped(pThis, pBdle, &StreamDesc))
1998 hdaFetchBdle(pThis, pBdle, &StreamDesc);
1999 *StreamDesc.pu32Sts |= HDA_REG_FIELD_FLAG_MASK(SDSTS, FIFORDY);
2000 Assert((avail >= 0 && (StreamDesc.u32Cbl >= (*StreamDesc.pu32Lpib)))); /* sanity */
2001 uint32_t u32CblLimit = StreamDesc.u32Cbl - (*StreamDesc.pu32Lpib);
2002 Assert((u32CblLimit > hdaFifoWToSz(pThis, &StreamDesc)));
2003 Log(("hda: CBL=%d, LPIB=%d\n", StreamDesc.u32Cbl, *StreamDesc.pu32Lpib));
2004 uint32_t cb;
2005 switch (src)
2006 {
2007 case PO_INDEX:
2008 cb = hdaWriteAudio(pThis, &StreamDesc, (uint32_t *)&avail, &fStop, u32CblLimit);
2009 break;
2010 case PI_INDEX:
2011 cb = hdaReadAudio(pThis, &StreamDesc, (uint32_t *)&avail, &fStop, u32CblLimit);
2012 break;
2013 default:
2014 cb = 0;
2015 fStop = true;
2016 AssertMsgFailed(("Unsupported"));
2017 }
2018 Assert(cb <= StreamDesc.u32Fifos + 1);
2019 *StreamDesc.pu32Sts &= ~HDA_REG_FIELD_FLAG_MASK(SDSTS, FIFORDY);
2020
2021 /* Process end of buffer condition. */
2022 hdaStreamCounterUpdate(pThis, pBdle, &StreamDesc, cb);
2023 fStop = !fStop ? !hdaDoNextTransferCycle(pThis, pBdle, &StreamDesc) : fStop;
2024 }
2025}
2026
2027
2028/* MMIO callbacks */
2029
2030/**
2031 * @callback_method_impl{FNIOMMMIOREAD, Looks up and calls the appropriate handler.}
2032 *
2033 * @note During implementation, we discovered so-called "forgotten" or "hole"
2034 * registers whose description is not listed in the RPM, datasheet, or
2035 * spec.
2036 */
2037PDMBOTHCBDECL(int) hdaMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
2038{
2039 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
2040 int rc;
2041
2042 /*
2043 * Look up and log.
2044 */
2045 uint32_t offReg = GCPhysAddr - pThis->MMIOBaseAddr;
2046 int idxReg = hdaRegLookup(pThis, offReg);
2047#ifdef LOG_ENABLED
2048 unsigned const cbLog = cb;
2049 uint32_t offRegLog = offReg;
2050#endif
2051
2052 Log(("hdaMMIORead: offReg=%#x cb=%#x\n", offReg, cb));
2053#define NEW_READ_CODE
2054#ifdef NEW_READ_CODE
2055 Assert(cb == 4); Assert((offReg & 3) == 0);
2056
2057 if (pThis->fInReset && idxReg != ICH6_HDA_REG_GCTL)
2058 Log(("hda: access to registers except GCTL is blocked while reset\n"));
2059
2060 if (idxReg == -1)
2061 LogRel(("hda: Invalid read access @0x%x(of bytes:%d)\n", offReg, cb));
2062
2063 if (idxReg != -1)
2064 {
2065 /* ASSUMES gapless DWORD at end of map. */
2066 if (g_aHdaRegMap[idxReg].size == 4)
2067 {
2068 /*
2069 * Straight forward DWORD access.
2070 */
2071 rc = g_aHdaRegMap[idxReg].pfnRead(pThis, idxReg, (uint32_t *)pv);
2072 Log(("hda: read %s => %x (%Rrc)\n", g_aHdaRegMap[idxReg].abbrev, *(uint32_t *)pv, rc));
2073 }
2074 else
2075 {
2076 /*
2077 * Multi register read (unless there are trailing gaps).
2078 * ASSUMES that only DWORD reads have sideeffects.
2079 */
2080 uint32_t u32Value = 0;
2081 unsigned cbLeft = 4;
2082 do
2083 {
2084 uint32_t const cbReg = g_aHdaRegMap[idxReg].size;
2085 uint32_t u32Tmp = 0;
2086
2087 rc = g_aHdaRegMap[idxReg].pfnRead(pThis, idxReg, &u32Tmp);
2088 Log(("hda: read %s[%db] => %x (%Rrc)*\n", g_aHdaRegMap[idxReg].abbrev, cbReg, *(uint32_t *)pv, rc));
2089 if (rc != VINF_SUCCESS)
2090 break;
2091 u32Value |= (u32Tmp & g_afMasks[cbReg]) << ((4 - cbLeft) * 8);
2092
2093 cbLeft -= cbReg;
2094 offReg += cbReg;
2095 idxReg++;
2096 } while (cbLeft > 0 && g_aHdaRegMap[idxReg].offset == offReg);
2097
2098 if (rc == VINF_SUCCESS)
2099 *(uint32_t *)pv = u32Value;
2100 else
2101 Assert(!IOM_SUCCESS(rc));
2102 }
2103 }
2104 else
2105 {
2106 rc = VINF_IOM_MMIO_UNUSED_FF;
2107 Log(("hda: hole at %x is accessed for read\n", offReg));
2108 }
2109#else
2110 if (idxReg != -1)
2111 {
2112 /** @todo r=bird: Accesses crossing register boundraries aren't handled
2113 * right from what I can tell? If they are, please explain
2114 * what the rules are. */
2115 uint32_t mask = 0;
2116 uint32_t shift = (g_aHdaRegMap[idxReg].offset - offReg) % sizeof(uint32_t) * 8;
2117 uint32_t u32Value = 0;
2118 switch(cb)
2119 {
2120 case 1: mask = 0x000000ff; break;
2121 case 2: mask = 0x0000ffff; break;
2122 case 4:
2123 /* 18.2 of the ICH6 datasheet defines the valid access widths as byte, word, and double word */
2124 case 8:
2125 mask = 0xffffffff;
2126 cb = 4;
2127 break;
2128 }
2129#if 0
2130 /* Cross-register access. Mac guest hits this assert doing assumption 4 byte access to 3 byte registers e.g. {I,O}SDnCTL
2131 */
2132 //Assert((cb <= g_aHdaRegMap[idxReg].size - (offReg - g_aHdaRegMap[idxReg].offset)));
2133 if (cb > g_aHdaRegMap[idxReg].size - (offReg - g_aHdaRegMap[idxReg].offset))
2134 {
2135 int off = cb - (g_aHdaRegMap[idxReg].size - (offReg - g_aHdaRegMap[idxReg].offset));
2136 rc = hdaMMIORead(pDevIns, pvUser, GCPhysAddr + cb - off, (char *)pv + cb - off, off);
2137 if (RT_FAILURE(rc))
2138 AssertRCReturn (rc, rc);
2139 }
2140 //Assert(((offReg - g_aHdaRegMap[idxReg].offset) == 0));
2141#endif
2142 mask <<= shift;
2143 rc = g_aHdaRegMap[idxReg].pfnRead(pThis, idxReg, &u32Value);
2144 *(uint32_t *)pv |= (u32Value & mask);
2145 Log(("hda: read %s[%x/%x]\n", g_aHdaRegMap[idxReg].abbrev, u32Value, *(uint32_t *)pv));
2146 }
2147 else
2148 {
2149 *(uint32_t *)pv = 0xFF;
2150 Log(("hda: hole at %x is accessed for read\n", offReg));
2151 rc = VINF_SUCCESS;
2152 }
2153#endif
2154
2155 /*
2156 * Log the outcome.
2157 */
2158#ifdef LOG_ENABLED
2159 if (cbLog == 4)
2160 Log(("hdaMMIORead: @%#05x -> %#010x %Rrc\n", offRegLog, *(uint32_t *)pv, rc));
2161 else if (cbLog == 2)
2162 Log(("hdaMMIORead: @%#05x -> %#06x %Rrc\n", offRegLog, *(uint16_t *)pv, rc));
2163 else if (cbLog == 1)
2164 Log(("hdaMMIORead: @%#05x -> %#04x %Rrc\n", offRegLog, *(uint8_t *)pv, rc));
2165#endif
2166 return rc;
2167}
2168
2169
2170DECLINLINE(int) hdaWriteReg(PHDASTATE pThis, int idxReg, uint32_t u32Value, char const *pszLog)
2171{
2172 if (pThis->fInReset && idxReg != ICH6_HDA_REG_GCTL)
2173 Log(("hda: access to registers except GCTL is blocked while reset\n")); /** @todo where is this enforced? */
2174
2175#ifdef LOG_ENABLED
2176 uint32_t const u32CurValue = pThis->au32Regs[idxReg];
2177#endif
2178 int rc = g_aHdaRegMap[idxReg].pfnWrite(pThis, idxReg, u32Value);
2179 Log(("hda: write %#x -> %s[%db]; %x => %x%s\n", u32Value, g_aHdaRegMap[idxReg].abbrev,
2180 g_aHdaRegMap[idxReg].size, u32CurValue, pThis->au32Regs[idxReg], pszLog));
2181 return rc;
2182}
2183
2184
2185/**
2186 * @callback_method_impl{FNIOMMMIOWRITE, Looks up and calls the appropriate handler.}
2187 */
2188PDMBOTHCBDECL(int) hdaMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
2189{
2190 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
2191 int rc;
2192
2193 /*
2194 * The behavior of accesses that aren't aligned on natural boundraries is
2195 * undefined. Just reject them out right.
2196 */
2197 /** @todo IOM could check this, it could also split the 8 byte accesses for us. */
2198 Assert(cb == 1 || cb == 2 || cb == 4 || cb == 8);
2199 if (GCPhysAddr & (cb - 1))
2200 return PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "misaligned write access: GCPhysAddr=%RGp cb=%u\n", GCPhysAddr, cb);
2201
2202 /*
2203 * Lookup and log the access.
2204 */
2205 uint32_t offReg = GCPhysAddr - pThis->MMIOBaseAddr;
2206 int idxReg = hdaRegLookup(pThis, offReg);
2207 uint64_t u64Value;
2208 if (cb == 4) u64Value = *(uint32_t const *)pv;
2209 else if (cb == 2) u64Value = *(uint16_t const *)pv;
2210 else if (cb == 1) u64Value = *(uint8_t const *)pv;
2211 else if (cb == 8) u64Value = *(uint64_t const *)pv;
2212 else
2213 AssertReleaseMsgFailed(("%d\n", cb));
2214
2215#ifdef LOG_ENABLED
2216 uint32_t const u32LogOldValue = idxReg != -1 ? pThis->au32Regs[idxReg] : UINT32_MAX;
2217 uint32_t const offRegLog = offReg;
2218 int const idxRegLog = idxReg;
2219 if (idxReg == -1)
2220 Log(("hdaMMIOWrite: @%#05x u32=%#010x cb=%d\n", offReg, *(uint32_t const *)pv, cb));
2221 else if (cb == 4)
2222 Log(("hdaMMIOWrite: @%#05x u32=%#010x %s\n", offReg, *(uint32_t *)pv, g_aHdaRegMap[idxReg].abbrev));
2223 else if (cb == 2)
2224 Log(("hdaMMIOWrite: @%#05x u16=%#06x (%#010x) %s\n", offReg, *(uint16_t *)pv, *(uint32_t *)pv, g_aHdaRegMap[idxReg].abbrev));
2225 else if (cb == 1)
2226 Log(("hdaMMIOWrite: @%#05x u8=%#04x (%#010x) %s\n", offReg, *(uint8_t *)pv, *(uint32_t *)pv, g_aHdaRegMap[idxReg].abbrev));
2227 if (idxReg != -1 && g_aHdaRegMap[idxReg].size != cb)
2228 Log(("hdaMMIOWrite: size=%d != cb=%d!!\n", g_aHdaRegMap[idxReg].size, cb));
2229#endif
2230
2231#define NEW_WRITE_CODE
2232#ifdef NEW_WRITE_CODE
2233 /*
2234 * Try for a direct hit first.
2235 */
2236 if (idxReg != -1 && g_aHdaRegMap[idxReg].size == cb)
2237 rc = hdaWriteReg(pThis, idxReg, u64Value, "");
2238 /*
2239 * Partial or multiple register access, loop thru the requested memory.
2240 */
2241 else
2242 {
2243 /* If it's an access beyond the start of the register, shift the input
2244 value and fill in missing bits. Natural alignment rules means we
2245 will only see 1 or 2 byte accesses of this kind, so no risk of
2246 shifting out input values. */
2247 if (idxReg == -1 && (idxReg = hdaRegLookupWithin(pThis, offReg)) != -1)
2248 {
2249 uint32_t const cbBefore = offReg - g_aHdaRegMap[idxReg].offset; Assert(cbBefore > 0 && cbBefore < 4);
2250 offReg -= cbBefore;
2251 u64Value <<= cbBefore * 8;
2252 u64Value |= pThis->au32Regs[idxReg] & g_afMasks[cbBefore];
2253 Log(("hdaMMIOWrite: Within register, supplied %u leading bits: %#llx -> %#llx ...\n",
2254 cbBefore * 8, ~g_afMasks[cbBefore] & u64Value, u64Value));
2255 }
2256
2257 /* Loop thru the write area, it may covert multiple registers. */
2258 rc = VINF_SUCCESS;
2259 for (;;)
2260 {
2261 uint32_t cbReg;
2262 if (idxReg != -1)
2263 {
2264 cbReg = g_aHdaRegMap[idxReg].size;
2265 if (cb < cbReg)
2266 {
2267 u64Value |= pThis->au32Regs[idxReg] & g_afMasks[cbReg] & ~g_afMasks[cb];
2268 Log(("hdaMMIOWrite: Supplying missing bits (%#x): %#llx -> %#llx ...\n",
2269 g_afMasks[cbReg] & ~g_afMasks[cb], u64Value & g_afMasks[cb], u64Value));
2270 }
2271 rc = hdaWriteReg(pThis, idxReg, u64Value, "*");
2272 }
2273 else
2274 {
2275 LogRel(("hda: Invalid write access @0x%x!\n", offReg));
2276 cbReg = 1;
2277 }
2278 if (cbReg >= cb)
2279 break;
2280
2281 /* advance */
2282 offReg += cbReg;
2283 cb -= cbReg;
2284 u64Value >>= cbReg * 8;
2285 if (idxReg == -1)
2286 idxReg = hdaRegLookup(pThis, offReg);
2287 else
2288 {
2289 idxReg++;
2290 if ( (unsigned)idxReg >= RT_ELEMENTS(g_aHdaRegMap)
2291 || g_aHdaRegMap[idxReg].offset != offReg)
2292 idxReg = -1;
2293 }
2294 }
2295 }
2296#else
2297 if (idxReg != -1)
2298 {
2299 /** @todo r=bird: This looks like code for handling unaligned register
2300 * accesses. If it isn't, then add a comment explaining what you're
2301 * trying to do here. OTOH, if it is then it has the following
2302 * issues:
2303 * -# You're calculating the wrong new value for the register.
2304 * -# You're not handling cross register accesses. Imagine a
2305 * 4-byte write starting at CORBCTL, or a 8-byte write.
2306 *
2307 * PS! consider dropping the 'offset' argument to pfnWrite/pfnRead as
2308 * nobody seems to be using it and it just adds complexity when reading
2309 * the code.
2310 *
2311 */
2312 uint32_t u32CurValue = pThis->au32Regs[idxReg];
2313 uint32_t u32NewValue;
2314 uint32_t mask;
2315 switch (cb)
2316 {
2317 case 1:
2318 u32NewValue = *(uint8_t const *)pv;
2319 mask = 0xff;
2320 break;
2321 case 2:
2322 u32NewValue = *(uint16_t const *)pv;
2323 mask = 0xffff;
2324 break;
2325 case 4:
2326 case 8:
2327 /* 18.2 of the ICH6 datasheet defines the valid access widths as byte, word, and double word */
2328 u32NewValue = *(uint32_t const *)pv;
2329 mask = 0xffffffff;
2330 cb = 4;
2331 break;
2332 default:
2333 AssertFailedReturn(VERR_INTERNAL_ERROR_4); /* shall not happen. */
2334 }
2335 /* cross-register access, see corresponding comment in hdaMMIORead */
2336 uint32_t shift = (g_aHdaRegMap[idxReg].offset - offReg) % sizeof(uint32_t) * 8;
2337 mask <<= shift;
2338 u32NewValue <<= shift;
2339 u32NewValue &= mask;
2340 u32NewValue |= (u32CurValue & ~mask);
2341
2342 rc = g_aHdaRegMap[idxReg].pfnWrite(pThis, idxReg, u32NewValue);
2343 Log(("hda: write %s:(%x) %x => %x\n", g_aHdaRegMap[idxReg].abbrev, u32NewValue,
2344 u32CurValue, pThis->au32Regs[idxReg]));
2345 }
2346 else
2347 rc = VINF_SUCCESS;
2348#endif
2349 Log(("hdaMMIOWrite: @%#05x %#x -> %#x\n", offRegLog, u32LogOldValue,
2350 idxRegLog != -1 ? pThis->au32Regs[idxRegLog] : UINT32_MAX));
2351 return rc;
2352}
2353
2354
2355/* PCI callback. */
2356
2357/**
2358 * @callback_method_impl{FNPCIIOREGIONMAP}
2359 */
2360static DECLCALLBACK(int) hdaPciIoRegionMap(PPCIDEVICE pPciDev, int iRegion, RTGCPHYS GCPhysAddress, uint32_t cb,
2361 PCIADDRESSSPACE enmType)
2362{
2363 PPDMDEVINS pDevIns = pPciDev->pDevIns;
2364 PHDASTATE pThis = RT_FROM_MEMBER(pPciDev, HDASTATE, PciDev);
2365 RTIOPORT Port = (RTIOPORT)GCPhysAddress;
2366 int rc;
2367
2368 /*
2369 * 18.2 of the ICH6 datasheet defines the valid access widths as byte, word, and double word.
2370 *
2371 * Let IOM talk DWORDs when reading, saves a lot of complications. On
2372 * writing though, we have to do it all ourselves because of sideeffects.
2373 */
2374 Assert(enmType == PCI_ADDRESS_SPACE_MEM);
2375 rc = PDMDevHlpMMIORegister(pPciDev->pDevIns, GCPhysAddress, cb, NULL /*pvUser*/,
2376#ifdef NEW_READ_CODE
2377 IOMMMIO_FLAGS_READ_DWORD |
2378#else
2379 IOMMMIO_FLAGS_READ_PASSTHRU |
2380#endif
2381 IOMMMIO_FLAGS_WRITE_PASSTHRU,
2382 hdaMMIOWrite, hdaMMIORead, "ICH6_HDA");
2383
2384 if (RT_FAILURE(rc))
2385 return rc;
2386
2387 pThis->MMIOBaseAddr = GCPhysAddress;
2388 return VINF_SUCCESS;
2389}
2390
2391
2392/* Saved state callbacks. */
2393
2394/**
2395 * @callback_method_impl{FNSSMDEVSAVEEXEC}
2396 */
2397static DECLCALLBACK(int) hdaSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
2398{
2399 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
2400 /* Save Codec nodes states */
2401 hdaCodecSaveState(&pThis->Codec, pSSM);
2402
2403 /* Save MMIO registers */
2404 AssertCompile(RT_ELEMENTS(pThis->au32Regs) == 112);
2405 SSMR3PutU32(pSSM, RT_ELEMENTS(pThis->au32Regs));
2406 SSMR3PutMem(pSSM, pThis->au32Regs, sizeof(pThis->au32Regs));
2407
2408 /* Save HDA dma counters */
2409 SSMR3PutStructEx(pSSM, &pThis->StOutBdle, sizeof(pThis->StOutBdle), 0 /*fFlags*/, g_aHdaBDLEDescFields, NULL);
2410 SSMR3PutStructEx(pSSM, &pThis->StMicBdle, sizeof(pThis->StMicBdle), 0 /*fFlags*/, g_aHdaBDLEDescFields, NULL);
2411 SSMR3PutStructEx(pSSM, &pThis->StInBdle, sizeof(pThis->StInBdle), 0 /*fFlags*/, g_aHdaBDLEDescFields, NULL);
2412 return VINF_SUCCESS;
2413}
2414
2415
2416/**
2417 * @callback_method_impl{FNSSMDEVLOADEXEC}
2418 */
2419static DECLCALLBACK(int) hdaLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
2420{
2421 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
2422
2423 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
2424
2425 /*
2426 * Load Codec nodes states.
2427 */
2428 int rc = hdaCodecLoadState(&pThis->Codec, pSSM, uVersion);
2429 if (RT_FAILURE(rc))
2430 return rc;
2431
2432 /*
2433 * Load MMIO registers.
2434 */
2435 uint32_t cRegs;
2436 switch (uVersion)
2437 {
2438 case HDA_SSM_VERSION_1:
2439 /* Starting with r71199, we would save 112 instead of 113
2440 registers due to some code cleanups. This only affected trunk
2441 builds in the 4.1 development period. */
2442 cRegs = 113;
2443 if (SSMR3HandleRevision(pSSM) >= 71199)
2444 {
2445 uint32_t uVer = SSMR3HandleVersion(pSSM);
2446 if ( VBOX_FULL_VERSION_GET_MAJOR(uVer) == 4
2447 && VBOX_FULL_VERSION_GET_MINOR(uVer) == 0
2448 && VBOX_FULL_VERSION_GET_BUILD(uVer) >= 51)
2449 cRegs = 112;
2450 }
2451 break;
2452
2453 case HDA_SSM_VERSION_2:
2454 case HDA_SSM_VERSION_3:
2455 cRegs = 112;
2456 AssertCompile(RT_ELEMENTS(pThis->au32Regs) == 112);
2457 break;
2458
2459 case HDA_SSM_VERSION:
2460 rc = SSMR3GetU32(pSSM, &cRegs); AssertRCReturn(rc, rc);
2461 AssertLogRelMsgReturn(cRegs == RT_ELEMENTS(pThis->au32Regs),
2462 ("cRegs is %d, expected %d\n", cRegs, RT_ELEMENTS(pThis->au32Regs)),
2463 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2464 break;
2465
2466 default:
2467 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
2468 }
2469
2470 if (cRegs >= RT_ELEMENTS(pThis->au32Regs))
2471 {
2472 SSMR3GetMem(pSSM, pThis->au32Regs, sizeof(pThis->au32Regs));
2473 SSMR3Skip(pSSM, sizeof(uint32_t) * (cRegs - RT_ELEMENTS(pThis->au32Regs)));
2474 }
2475 else
2476 {
2477 RT_ZERO(pThis->au32Regs);
2478 SSMR3GetMem(pSSM, pThis->au32Regs, sizeof(uint32_t) * cRegs);
2479 }
2480
2481 /*
2482 * Load HDA dma counters.
2483 */
2484 uint32_t fFlags = uVersion <= HDA_SSM_VERSION_2 ? SSMSTRUCT_FLAGS_MEM_BAND_AID_RELAXED : 0;
2485 PCSSMFIELD paFields = uVersion <= HDA_SSM_VERSION_2 ? g_aHdaBDLEDescFieldsOld : g_aHdaBDLEDescFields;
2486 SSMR3GetStructEx(pSSM, &pThis->StOutBdle, sizeof(pThis->StOutBdle), fFlags, paFields, NULL);
2487 SSMR3GetStructEx(pSSM, &pThis->StMicBdle, sizeof(pThis->StMicBdle), fFlags, paFields, NULL);
2488 rc = SSMR3GetStructEx(pSSM, &pThis->StInBdle, sizeof(pThis->StInBdle), fFlags, paFields, NULL);
2489 AssertRCReturn(rc, rc);
2490
2491 /*
2492 * Update stuff after the state changes.
2493 */
2494 AUD_set_active_in(pThis->Codec.SwVoiceIn, SDCTL(pThis, 0) & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN));
2495 AUD_set_active_out(pThis->Codec.SwVoiceOut, SDCTL(pThis, 4) & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN));
2496
2497 pThis->u64CORBBase = RT_MAKE_U64(CORBLBASE(pThis), CORBUBASE(pThis));
2498 pThis->u64RIRBBase = RT_MAKE_U64(RIRLBASE(pThis), RIRUBASE(pThis));
2499 pThis->u64DPBase = RT_MAKE_U64(DPLBASE(pThis), DPUBASE(pThis));
2500 return VINF_SUCCESS;
2501}
2502
2503
2504/* Debug and log type formatters. */
2505
2506/**
2507 * @callback_method_impl{FNRTSTRFORMATTYPE}
2508 */
2509static DECLCALLBACK(size_t)
2510hdaFormatStrmCtl(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
2511 const char *pszType, void const *pvValue,
2512 int cchWidth, int cchPrecision, unsigned fFlags,
2513 void *pvUser)
2514{
2515 uint32_t sdCtl = (uint32_t)(uintptr_t)pvValue;
2516 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
2517 "SDCTL(raw: %#x, strm:%#x, dir:%RTbool, tp:%RTbool strip:%x, deie:%RTbool, ioce:%RTbool, run:%RTbool, srst:%RTbool)",
2518 sdCtl,
2519 (sdCtl & HDA_REG_FIELD_MASK(SDCTL, NUM)) >> ICH6_HDA_SDCTL_NUM_SHIFT,
2520 RT_BOOL(sdCtl & HDA_REG_FIELD_FLAG_MASK(SDCTL, DIR)),
2521 RT_BOOL(sdCtl & HDA_REG_FIELD_FLAG_MASK(SDCTL, TP)),
2522 (sdCtl & HDA_REG_FIELD_MASK(SDCTL, STRIPE)) >> ICH6_HDA_SDCTL_STRIPE_SHIFT,
2523 RT_BOOL(sdCtl & HDA_REG_FIELD_FLAG_MASK(SDCTL, DEIE)),
2524 RT_BOOL(sdCtl & HDA_REG_FIELD_FLAG_MASK(SDCTL, ICE)),
2525 RT_BOOL(sdCtl & HDA_REG_FIELD_FLAG_MASK(SDCTL, RUN)),
2526 RT_BOOL(sdCtl & HDA_REG_FIELD_FLAG_MASK(SDCTL, SRST)));
2527}
2528
2529/**
2530 * @callback_method_impl{FNRTSTRFORMATTYPE}
2531 */
2532static DECLCALLBACK(size_t)
2533hdaFormatStrmFifos(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
2534 const char *pszType, void const *pvValue,
2535 int cchWidth, int cchPrecision, unsigned fFlags,
2536 void *pvUser)
2537{
2538 uint32_t uSdFifos = (uint32_t)(uintptr_t)pvValue;
2539 uint32_t cb;
2540 switch (uSdFifos)
2541 {
2542 case HDA_SDONFIFO_16B: cb = 16; break;
2543 case HDA_SDONFIFO_32B: cb = 32; break;
2544 case HDA_SDONFIFO_64B: cb = 64; break;
2545 case HDA_SDONFIFO_128B: cb = 128; break;
2546 case HDA_SDONFIFO_192B: cb = 192; break;
2547 case HDA_SDONFIFO_256B: cb = 256; break;
2548 case HDA_SDINFIFO_120B: cb = 120; break;
2549 case HDA_SDINFIFO_160B: cb = 160; break;
2550 default: cb = 0; break;
2551 }
2552 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "SDFIFOS(raw: %#x, sdfifos:%u B)", uSdFifos, cb);
2553}
2554
2555/**
2556 * @callback_method_impl{FNRTSTRFORMATTYPE}
2557 */
2558static DECLCALLBACK(size_t)
2559hdaFormatStrmFifow(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
2560 const char *pszType, void const *pvValue,
2561 int cchWidth, int cchPrecision, unsigned fFlags,
2562 void *pvUser)
2563{
2564 uint32_t uSdFifos = (uint32_t)(uintptr_t)pvValue;
2565 uint32_t cb;
2566 switch (uSdFifos)
2567 {
2568 case HDA_SDFIFOW_8B: cb = 8; break;
2569 case HDA_SDFIFOW_16B: cb = 16; break;
2570 case HDA_SDFIFOW_32B: cb = 32; break;
2571 default: cb = 0; break;
2572 }
2573 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "SDFIFOW(raw: %#0x, sdfifow:%d B)", uSdFifos, cb);
2574}
2575
2576/**
2577 * @callback_method_impl{FNRTSTRFORMATTYPE}
2578 */
2579static DECLCALLBACK(size_t)
2580hdaFormatStrmSts(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
2581 const char *pszType, void const *pvValue,
2582 int cchWidth, int cchPrecision, unsigned fFlags,
2583 void *pvUser)
2584{
2585 uint32_t uSdSts = (uint32_t)(uintptr_t)pvValue;
2586 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
2587 "SDSTS(raw: %#0x, fifordy:%RTbool, dese:%RTbool, fifoe:%RTbool, bcis:%RTbool)",
2588 uSdSts,
2589 RT_BOOL(uSdSts & HDA_REG_FIELD_FLAG_MASK(SDSTS, FIFORDY)),
2590 RT_BOOL(uSdSts & HDA_REG_FIELD_FLAG_MASK(SDSTS, DE)),
2591 RT_BOOL(uSdSts & HDA_REG_FIELD_FLAG_MASK(SDSTS, FE)),
2592 RT_BOOL(uSdSts & HDA_REG_FIELD_FLAG_MASK(SDSTS, BCIS)));
2593}
2594
2595
2596static int hdaLookUpRegisterByName(PHDASTATE pThis, const char *pszArgs)
2597{
2598 int iReg = 0;
2599 for (; iReg < HDA_NREGS; ++iReg)
2600 if (!RTStrICmp(g_aHdaRegMap[iReg].abbrev, pszArgs))
2601 return iReg;
2602 return -1;
2603}
2604
2605
2606static void hdaDbgPrintRegister(PHDASTATE pThis, PCDBGFINFOHLP pHlp, int iHdaIndex)
2607{
2608 Assert( pThis
2609 && iHdaIndex >= 0
2610 && iHdaIndex < HDA_NREGS);
2611 pHlp->pfnPrintf(pHlp, "hda: %s: 0x%x\n", g_aHdaRegMap[iHdaIndex].abbrev, pThis->au32Regs[iHdaIndex]);
2612}
2613
2614
2615/**
2616 * @callback_method_impl{FNDBGFHANDLERDEV}
2617 */
2618static DECLCALLBACK(void) hdaInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
2619{
2620 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
2621 int iHdaRegisterIndex = hdaLookUpRegisterByName(pThis, pszArgs);
2622 if (iHdaRegisterIndex != -1)
2623 hdaDbgPrintRegister(pThis, pHlp, iHdaRegisterIndex);
2624 else
2625 for(iHdaRegisterIndex = 0; (unsigned int)iHdaRegisterIndex < HDA_NREGS; ++iHdaRegisterIndex)
2626 hdaDbgPrintRegister(pThis, pHlp, iHdaRegisterIndex);
2627}
2628
2629
2630static void hdaDbgPrintStream(PHDASTATE pThis, PCDBGFINFOHLP pHlp, int iHdaStrmIndex)
2631{
2632 Assert( pThis
2633 && iHdaStrmIndex >= 0
2634 && iHdaStrmIndex < 7);
2635 pHlp->pfnPrintf(pHlp, "Dump of %d HDA Stream:\n", iHdaStrmIndex);
2636 pHlp->pfnPrintf(pHlp, "SD%dCTL: %R[sdctl]\n", iHdaStrmIndex, HDA_STREAM_REG2(pThis, CTL, iHdaStrmIndex));
2637 pHlp->pfnPrintf(pHlp, "SD%dCTS: %R[sdsts]\n", iHdaStrmIndex, HDA_STREAM_REG2(pThis, STS, iHdaStrmIndex));
2638 pHlp->pfnPrintf(pHlp, "SD%dFIFOS: %R[sdfifos]\n", iHdaStrmIndex, HDA_STREAM_REG2(pThis, FIFOS, iHdaStrmIndex));
2639 pHlp->pfnPrintf(pHlp, "SD%dFIFOW: %R[sdfifow]\n", iHdaStrmIndex, HDA_STREAM_REG2(pThis, FIFOW, iHdaStrmIndex));
2640}
2641
2642
2643static int hdaLookUpStreamIndex(PHDASTATE pThis, const char *pszArgs)
2644{
2645 /* todo: add args parsing */
2646 return -1;
2647}
2648
2649
2650/**
2651 * @callback_method_impl{FNDBGFHANDLERDEV}
2652 */
2653static DECLCALLBACK(void) hdaInfoStream(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
2654{
2655 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
2656 int iHdaStrmIndex = hdaLookUpStreamIndex(pThis, pszArgs);
2657 if (iHdaStrmIndex != -1)
2658 hdaDbgPrintStream(pThis, pHlp, iHdaStrmIndex);
2659 else
2660 for(iHdaStrmIndex = 0; iHdaStrmIndex < 7; ++iHdaStrmIndex)
2661 hdaDbgPrintStream(pThis, pHlp, iHdaStrmIndex);
2662}
2663
2664/**
2665 * @callback_method_impl{FNDBGFHANDLERDEV}
2666 */
2667static DECLCALLBACK(void) hdaInfoCodecNodes(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
2668{
2669 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
2670 if (pThis->Codec.pfnCodecDbgListNodes)
2671 pThis->Codec.pfnCodecDbgListNodes(&pThis->Codec, pHlp, pszArgs);
2672 else
2673 pHlp->pfnPrintf(pHlp, "Codec implementation doesn't provide corresponding callback.\n");
2674}
2675
2676
2677/**
2678 * @callback_method_impl{FNDBGFHANDLERDEV}
2679 */
2680static DECLCALLBACK(void) hdaInfoCodecSelector(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
2681{
2682 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
2683 if (pThis->Codec.pfnCodecDbgSelector)
2684 pThis->Codec.pfnCodecDbgSelector(&pThis->Codec, pHlp, pszArgs);
2685 else
2686 pHlp->pfnPrintf(pHlp, "Codec implementation doesn't provide corresponding callback.\n");
2687}
2688
2689
2690/* PDMIBASE */
2691
2692/**
2693 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
2694 */
2695static DECLCALLBACK(void *) hdaQueryInterface(struct PDMIBASE *pInterface, const char *pszIID)
2696{
2697 PHDASTATE pThis = RT_FROM_MEMBER(pInterface, HDASTATE, IBase);
2698 Assert(&pThis->IBase == pInterface);
2699
2700 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->IBase);
2701 return NULL;
2702}
2703
2704
2705/* PDMDEVREG */
2706
2707/**
2708 * Reset notification.
2709 *
2710 * @returns VBox status.
2711 * @param pDevIns The device instance data.
2712 *
2713 * @remark The original sources didn't install a reset handler, but it seems to
2714 * make sense to me so we'll do it.
2715 */
2716static DECLCALLBACK(void) hdaReset(PPDMDEVINS pDevIns)
2717{
2718 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
2719 GCAP(pThis) = HDA_MAKE_GCAP(4,4,0,0,1); /* see 6.2.1 */
2720 VMIN(pThis) = 0x00; /* see 6.2.2 */
2721 VMAJ(pThis) = 0x01; /* see 6.2.3 */
2722 VMAJ(pThis) = 0x01; /* see 6.2.3 */
2723 OUTPAY(pThis) = 0x003C; /* see 6.2.4 */
2724 INPAY(pThis) = 0x001D; /* see 6.2.5 */
2725 pThis->au32Regs[ICH6_HDA_REG_CORBSIZE] = 0x42; /* see 6.2.1 */
2726 pThis->au32Regs[ICH6_HDA_REG_RIRBSIZE] = 0x42; /* see 6.2.1 */
2727 CORBRP(pThis) = 0x0;
2728 RIRBWP(pThis) = 0x0;
2729
2730 Log(("hda: inter HDA reset.\n"));
2731 pThis->cbCorbBuf = 256 * sizeof(uint32_t);
2732
2733 if (pThis->pu32CorbBuf)
2734 memset(pThis->pu32CorbBuf, 0, pThis->cbCorbBuf);
2735 else
2736 pThis->pu32CorbBuf = (uint32_t *)RTMemAllocZ(pThis->cbCorbBuf);
2737
2738 pThis->cbRirbBuf = 256 * sizeof(uint64_t);
2739 if (pThis->pu64RirbBuf)
2740 memset(pThis->pu64RirbBuf, 0, pThis->cbRirbBuf);
2741 else
2742 pThis->pu64RirbBuf = (uint64_t *)RTMemAllocZ(pThis->cbRirbBuf);
2743
2744 pThis->u64BaseTS = PDMDevHlpTMTimeVirtGetNano(pDevIns);
2745
2746 HDABDLEDESC StEmptyBdle;
2747 for (uint8_t u8Strm = 0; u8Strm < 8; ++u8Strm)
2748 {
2749 HDASTREAMTRANSFERDESC StreamDesc;
2750 PHDABDLEDESC pBdle = NULL;
2751 if (u8Strm == 0)
2752 pBdle = &pThis->StInBdle;
2753 else if(u8Strm == 4)
2754 pBdle = &pThis->StOutBdle;
2755 else
2756 {
2757 memset(&StEmptyBdle, 0, sizeof(HDABDLEDESC));
2758 pBdle = &StEmptyBdle;
2759 }
2760 hdaInitTransferDescriptor(pThis, pBdle, u8Strm, &StreamDesc);
2761 /* hdaStreamReset prevents changing the SRST bit, so we force it to zero here. */
2762 HDA_STREAM_REG2(pThis, CTL, u8Strm) = 0;
2763 hdaStreamReset(pThis, pBdle, &StreamDesc, u8Strm);
2764 }
2765
2766 /* emulation of codec "wake up" (HDA spec 5.5.1 and 6.5)*/
2767 STATESTS(pThis) = 0x1;
2768
2769 Log(("hda: reset finished\n"));
2770}
2771
2772
2773/**
2774 * @interface_method_impl{PDMDEVREG,pfnDestruct}
2775 */
2776static DECLCALLBACK(int) hdaDestruct(PPDMDEVINS pDevIns)
2777{
2778 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
2779
2780 int rc = hdaCodecDestruct(&pThis->Codec);
2781 AssertRC(rc);
2782
2783 RTMemFree(pThis->pu32CorbBuf);
2784 pThis->pu32CorbBuf = NULL;
2785
2786 RTMemFree(pThis->pu64RirbBuf);
2787 pThis->pu64RirbBuf = NULL;
2788
2789 return VINF_SUCCESS;
2790}
2791
2792/**
2793 * @interface_method_impl{PDMDEVREG,pfnConstruct}
2794 */
2795static DECLCALLBACK(int) hdaConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
2796{
2797 PHDASTATE pThis = PDMINS_2_DATA(pDevIns, PHDASTATE);
2798 int rc;
2799
2800 Assert(iInstance == 0);
2801 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
2802
2803 /*
2804 * Validations.
2805 */
2806 if (!CFGMR3AreValuesValid(pCfgHandle, "\0"))
2807 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
2808 N_ ("Invalid configuration for the Intel HDA device"));
2809
2810 /// @todo r=michaln: This device may need R0/RC enabling, especially if guests
2811 /// poll some register(pThis).
2812
2813 /*
2814 * Initialize data (most of it anyway).
2815 */
2816 pThis->pDevIns = pDevIns;
2817 /* IBase */
2818 pThis->IBase.pfnQueryInterface = hdaQueryInterface;
2819
2820 /* PCI Device */
2821 PCIDevSetVendorId (&pThis->PciDev, HDA_PCI_VENDOR_ID); /* nVidia */
2822 PCIDevSetDeviceId (&pThis->PciDev, HDA_PCI_DEICE_ID); /* HDA */
2823
2824 PCIDevSetCommand (&pThis->PciDev, 0x0000); /* 04 rw,ro - pcicmd. */
2825 PCIDevSetStatus (&pThis->PciDev, VBOX_PCI_STATUS_CAP_LIST); /* 06 rwc?,ro? - pcists. */
2826 PCIDevSetRevisionId (&pThis->PciDev, 0x01); /* 08 ro - rid. */
2827 PCIDevSetClassProg (&pThis->PciDev, 0x00); /* 09 ro - pi. */
2828 PCIDevSetClassSub (&pThis->PciDev, 0x03); /* 0a ro - scc; 03 == HDA. */
2829 PCIDevSetClassBase (&pThis->PciDev, 0x04); /* 0b ro - bcc; 04 == multimedia. */
2830 PCIDevSetHeaderType (&pThis->PciDev, 0x00); /* 0e ro - headtyp. */
2831 PCIDevSetBaseAddress (&pThis->PciDev, 0, /* 10 rw - MMIO */
2832 false /* fIoSpace */, false /* fPrefetchable */, true /* f64Bit */, 0x00000000);
2833 PCIDevSetInterruptLine (&pThis->PciDev, 0x00); /* 3c rw. */
2834 PCIDevSetInterruptPin (&pThis->PciDev, 0x01); /* 3d ro - INTA#. */
2835
2836#if defined(HDA_AS_PCI_EXPRESS)
2837 PCIDevSetCapabilityList (&pThis->PciDev, 0x80);
2838#elif defined(VBOX_WITH_MSI_DEVICES)
2839 PCIDevSetCapabilityList (&pThis->PciDev, 0x60);
2840#else
2841 PCIDevSetCapabilityList (&pThis->PciDev, 0x50); /* ICH6 datasheet 18.1.16 */
2842#endif
2843
2844 /// @todo r=michaln: If there are really no PCIDevSetXx for these, the meaning
2845 /// of these values needs to be properly documented!
2846 /* HDCTL off 0x40 bit 0 selects signaling mode (1-HDA, 0 - Ac97) 18.1.19 */
2847 PCIDevSetByte(&pThis->PciDev, 0x40, 0x01);
2848
2849 /* Power Management */
2850 PCIDevSetByte(&pThis->PciDev, 0x50 + 0, VBOX_PCI_CAP_ID_PM);
2851 PCIDevSetByte(&pThis->PciDev, 0x50 + 1, 0x0); /* next */
2852 PCIDevSetWord(&pThis->PciDev, 0x50 + 2, VBOX_PCI_PM_CAP_DSI | 0x02 /* version, PM1.1 */ );
2853
2854#ifdef HDA_AS_PCI_EXPRESS
2855 /* PCI Express */
2856 PCIDevSetByte(&pThis->PciDev, 0x80 + 0, VBOX_PCI_CAP_ID_EXP); /* PCI_Express */
2857 PCIDevSetByte(&pThis->PciDev, 0x80 + 1, 0x60); /* next */
2858 /* Device flags */
2859 PCIDevSetWord(&pThis->PciDev, 0x80 + 2,
2860 /* version */ 0x1 |
2861 /* Root Complex Integrated Endpoint */ (VBOX_PCI_EXP_TYPE_ROOT_INT_EP << 4) |
2862 /* MSI */ (100) << 9 );
2863 /* Device capabilities */
2864 PCIDevSetDWord(&pThis->PciDev, 0x80 + 4, VBOX_PCI_EXP_DEVCAP_FLRESET);
2865 /* Device control */
2866 PCIDevSetWord( &pThis->PciDev, 0x80 + 8, 0);
2867 /* Device status */
2868 PCIDevSetWord( &pThis->PciDev, 0x80 + 10, 0);
2869 /* Link caps */
2870 PCIDevSetDWord(&pThis->PciDev, 0x80 + 12, 0);
2871 /* Link control */
2872 PCIDevSetWord( &pThis->PciDev, 0x80 + 16, 0);
2873 /* Link status */
2874 PCIDevSetWord( &pThis->PciDev, 0x80 + 18, 0);
2875 /* Slot capabilities */
2876 PCIDevSetDWord(&pThis->PciDev, 0x80 + 20, 0);
2877 /* Slot control */
2878 PCIDevSetWord( &pThis->PciDev, 0x80 + 24, 0);
2879 /* Slot status */
2880 PCIDevSetWord( &pThis->PciDev, 0x80 + 26, 0);
2881 /* Root control */
2882 PCIDevSetWord( &pThis->PciDev, 0x80 + 28, 0);
2883 /* Root capabilities */
2884 PCIDevSetWord( &pThis->PciDev, 0x80 + 30, 0);
2885 /* Root status */
2886 PCIDevSetDWord(&pThis->PciDev, 0x80 + 32, 0);
2887 /* Device capabilities 2 */
2888 PCIDevSetDWord(&pThis->PciDev, 0x80 + 36, 0);
2889 /* Device control 2 */
2890 PCIDevSetQWord(&pThis->PciDev, 0x80 + 40, 0);
2891 /* Link control 2 */
2892 PCIDevSetQWord(&pThis->PciDev, 0x80 + 48, 0);
2893 /* Slot control 2 */
2894 PCIDevSetWord( &pThis->PciDev, 0x80 + 56, 0);
2895#endif
2896
2897 /*
2898 * Register the PCI device.
2899 */
2900 rc = PDMDevHlpPCIRegister(pDevIns, &pThis->PciDev);
2901 if (RT_FAILURE(rc))
2902 return rc;
2903
2904 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 0, 0x4000, PCI_ADDRESS_SPACE_MEM, hdaPciIoRegionMap);
2905 if (RT_FAILURE(rc))
2906 return rc;
2907
2908#ifdef VBOX_WITH_MSI_DEVICES
2909 PDMMSIREG MsiReg;
2910 RT_ZERO(MsiReg);
2911 MsiReg.cMsiVectors = 1;
2912 MsiReg.iMsiCapOffset = 0x60;
2913 MsiReg.iMsiNextOffset = 0x50;
2914 rc = PDMDevHlpPCIRegisterMsi(pDevIns, &MsiReg);
2915 if (RT_FAILURE(rc))
2916 {
2917 LogRel(("Chipset cannot do MSI: %Rrc\n", rc));
2918 PCIDevSetCapabilityList(&pThis->PciDev, 0x50);
2919 }
2920#endif
2921
2922 rc = PDMDevHlpSSMRegister(pDevIns, HDA_SSM_VERSION, sizeof(*pThis), hdaSaveExec, hdaLoadExec);
2923 if (RT_FAILURE(rc))
2924 return rc;
2925
2926 /*
2927 * Attach driver.
2928 */
2929 rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThis->IBase, &pThis->pDrvBase, "Audio Driver Port");
2930 if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
2931 Log(("hda: No attached driver!\n"));
2932 else if (RT_FAILURE(rc))
2933 {
2934 AssertMsgFailed(("Failed to attach Intel HDA LUN #0! rc=%Rrc\n", rc));
2935 return rc;
2936 }
2937
2938 pThis->Codec.pvHDAState = pThis;
2939 rc = hdaCodecConstruct(pDevIns, &pThis->Codec, pCfgHandle);
2940 if (RT_FAILURE(rc))
2941 AssertRCReturn(rc, rc);
2942
2943 /* ICH6 datasheet defines 0 values for SVID and SID (18.1.14-15), which together with values returned for
2944 verb F20 should provide device/codec recognition. */
2945 Assert(pThis->Codec.u16VendorId);
2946 Assert(pThis->Codec.u16DeviceId);
2947 PCIDevSetSubSystemVendorId(&pThis->PciDev, pThis->Codec.u16VendorId); /* 2c ro - intel.) */
2948 PCIDevSetSubSystemId( &pThis->PciDev, pThis->Codec.u16DeviceId); /* 2e ro. */
2949
2950 hdaReset(pDevIns);
2951 pThis->Codec.id = 0;
2952 pThis->Codec.pfnTransfer = hdaTransfer;
2953 pThis->Codec.pfnReset = hdaCodecReset;
2954
2955 /*
2956 * 18.2.6,7 defines that values of this registers might be cleared on power on/reset
2957 * hdaReset shouldn't affects these registers.
2958 */
2959 WAKEEN(pThis) = 0x0;
2960 STATESTS(pThis) = 0x0;
2961
2962 /*
2963 * Debug and string formatter types.
2964 */
2965 PDMDevHlpDBGFInfoRegister(pDevIns, "hda", "HDA info. (hda [register case-insensitive])", hdaInfo);
2966 PDMDevHlpDBGFInfoRegister(pDevIns, "hdastrm", "HDA stream info. (hdastrm [stream number])", hdaInfoStream);
2967 PDMDevHlpDBGFInfoRegister(pDevIns, "hdcnodes", "HDA codec nodes.", hdaInfoCodecNodes);
2968 PDMDevHlpDBGFInfoRegister(pDevIns, "hdcselector", "HDA codec's selector states [node number].", hdaInfoCodecSelector);
2969
2970 rc = RTStrFormatTypeRegister("sdctl", hdaFormatStrmCtl, NULL);
2971 AssertRC(rc);
2972 rc = RTStrFormatTypeRegister("sdsts", hdaFormatStrmSts, NULL);
2973 AssertRC(rc);
2974 rc = RTStrFormatTypeRegister("sdfifos", hdaFormatStrmFifos, NULL);
2975 AssertRC(rc);
2976 rc = RTStrFormatTypeRegister("sdfifow", hdaFormatStrmFifow, NULL);
2977 AssertRC(rc);
2978#if 0
2979 rc = RTStrFormatTypeRegister("sdfmt", printHdaStrmFmt, NULL);
2980 AssertRC(rc);
2981#endif
2982
2983 /*
2984 * Some debug assertions.
2985 */
2986 for (unsigned i = 0; i < RT_ELEMENTS(g_aHdaRegMap); i++)
2987 {
2988 struct HDAREGDESC const *pReg = &g_aHdaRegMap[i];
2989 struct HDAREGDESC const *pNextReg = i + 1 < RT_ELEMENTS(g_aHdaRegMap) ? &g_aHdaRegMap[i + 1] : NULL;
2990
2991 /* binary search order. */
2992 AssertReleaseMsg(!pNextReg || pReg->offset + pReg->size <= pNextReg->offset,
2993 ("[%#x] = {%#x LB %#x} vs. [%#x] = {%#x LB %#x}\n",
2994 i, pReg->offset, pReg->size, i + 1, pNextReg->offset, pNextReg->size));
2995
2996 /* alignment. */
2997 AssertReleaseMsg( pReg->size == 1
2998 || (pReg->size == 2 && (pReg->offset & 1) == 0)
2999 || (pReg->size == 3 && (pReg->offset & 3) == 0)
3000 || (pReg->size == 4 && (pReg->offset & 3) == 0),
3001 ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
3002
3003 /* registers are packed into dwords - with 3 exceptions with gaps at the end of the dword. */
3004 AssertRelease(((pReg->offset + pReg->size) & 3) == 0 || pNextReg);
3005 if (pReg->offset & 3)
3006 {
3007 struct HDAREGDESC const *pPrevReg = i > 0 ? &g_aHdaRegMap[i - 1] : NULL;
3008 AssertReleaseMsg(pPrevReg, ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
3009 if (pPrevReg)
3010 AssertReleaseMsg(pPrevReg->offset + pPrevReg->size == pReg->offset,
3011 ("[%#x] = {%#x LB %#x} vs. [%#x] = {%#x LB %#x}\n",
3012 i - 1, pPrevReg->offset, pPrevReg->size, i + 1, pReg->offset, pReg->size));
3013 }
3014#if 0
3015 if ((pReg->offset + pReg->size) & 3)
3016 {
3017 AssertReleaseMsg(pNextReg, ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
3018 if (pNextReg)
3019 AssertReleaseMsg(pReg->offset + pReg->size == pNextReg->offset,
3020 ("[%#x] = {%#x LB %#x} vs. [%#x] = {%#x LB %#x}\n",
3021 i, pReg->offset, pReg->size, i + 1, pNextReg->offset, pNextReg->size));
3022 }
3023#endif
3024
3025 /* The final entry is a full dword, no gaps! Allows shortcuts. */
3026 AssertReleaseMsg(pNextReg || ((pReg->offset + pReg->size) & 3) == 0,
3027 ("[%#x] = {%#x LB %#x}\n", i, pReg->offset, pReg->size));
3028 }
3029
3030 return VINF_SUCCESS;
3031}
3032
3033/**
3034 * The device registration structure.
3035 */
3036const PDMDEVREG g_DeviceICH6_HDA =
3037{
3038 /* u32Version */
3039 PDM_DEVREG_VERSION,
3040 /* szName */
3041 "hda",
3042 /* szRCMod */
3043 "",
3044 /* szR0Mod */
3045 "",
3046 /* pszDescription */
3047 "Intel HD Audio Controller",
3048 /* fFlags */
3049 PDM_DEVREG_FLAGS_DEFAULT_BITS,
3050 /* fClass */
3051 PDM_DEVREG_CLASS_AUDIO,
3052 /* cMaxInstances */
3053 1,
3054 /* cbInstance */
3055 sizeof(HDASTATE),
3056 /* pfnConstruct */
3057 hdaConstruct,
3058 /* pfnDestruct */
3059 hdaDestruct,
3060 /* pfnRelocate */
3061 NULL,
3062 /* pfnIOCtl */
3063 NULL,
3064 /* pfnPowerOn */
3065 NULL,
3066 /* pfnReset */
3067 hdaReset,
3068 /* pfnSuspend */
3069 NULL,
3070 /* pfnResume */
3071 NULL,
3072 /* pfnAttach */
3073 NULL,
3074 /* pfnDetach */
3075 NULL,
3076 /* pfnQueryInterface. */
3077 NULL,
3078 /* pfnInitComplete */
3079 NULL,
3080 /* pfnPowerOff */
3081 NULL,
3082 /* pfnSoftReset */
3083 NULL,
3084 /* u32VersionEnd */
3085 PDM_DEVREG_VERSION
3086};
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