blob: 6918e23c5d07b5ac69b9f877f7e274b3edc85366 [file] [log] [blame]
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001/*
2 * Copyright (C) 2010 Red Hat, Inc.
3 *
4 * written by Gerd Hoffmann <kraxel@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 or
9 * (at your option) version 3 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
Peter Maydell6086a562016-01-18 17:33:52 +000020#include "qemu/osdep.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010021#include "hw/pci/pci.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020022#include "hw/qdev-properties.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010023#include "hw/pci/msi.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010024#include "qemu/timer.h"
Matt Parkera6b0bdc2017-08-27 20:20:38 +010025#include "qemu/bitops.h"
Gerd Hoffmann7ec91062018-11-23 07:39:57 +010026#include "qemu/log.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020027#include "qemu/module.h"
Gerd Hoffmannfcb541c2020-07-02 15:25:15 +020028#include "qemu/error-report.h"
Eduardo Habkost8a824e42017-05-08 17:57:35 -030029#include "hw/audio/soundhw.h"
Paolo Bonzini47b43a12013-03-18 17:36:02 +010030#include "intel-hda.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020031#include "migration/vmstate.h"
Paolo Bonzini47b43a12013-03-18 17:36:02 +010032#include "intel-hda-defs.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/dma.h"
xiaoqiang zhaobda8d9b2016-05-13 11:46:59 +080034#include "qapi/error.h"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040035#include "qom/object.h"
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +010036
37/* --------------------------------------------------------------------- */
38/* hda bus */
39
Paolo Bonzini3cb75a72012-03-28 18:01:36 +020040static Property hda_props[] = {
41 DEFINE_PROP_UINT32("cad", HDACodecDevice, cad, -1),
42 DEFINE_PROP_END_OF_LIST()
43};
44
Anthony Liguori0d936922012-05-02 09:00:20 +020045static const TypeInfo hda_codec_bus_info = {
46 .name = TYPE_HDA_BUS,
47 .parent = TYPE_BUS,
48 .instance_size = sizeof(HDACodecBus),
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +010049};
50
Andreas Färberab809e82013-08-23 20:05:16 +020051void hda_codec_bus_init(DeviceState *dev, HDACodecBus *bus, size_t bus_size,
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +010052 hda_codec_response_func response,
53 hda_codec_xfer_func xfer)
54{
Peter Maydelld637e1d2021-09-23 13:11:51 +010055 qbus_init(bus, bus_size, TYPE_HDA_BUS, dev, NULL);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +010056 bus->response = response;
57 bus->xfer = xfer;
58}
59
xiaoqiang zhaobda8d9b2016-05-13 11:46:59 +080060static void hda_codec_dev_realize(DeviceState *qdev, Error **errp)
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +010061{
xiaoqiang zhaoe19202a2016-05-13 11:46:58 +080062 HDACodecBus *bus = HDA_BUS(qdev->parent_bus);
63 HDACodecDevice *dev = HDA_CODEC_DEVICE(qdev);
Anthony Liguoridbaa7902011-12-16 13:39:51 -060064 HDACodecDeviceClass *cdc = HDA_CODEC_DEVICE_GET_CLASS(dev);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +010065
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +010066 if (dev->cad == -1) {
67 dev->cad = bus->next_cad;
68 }
Gerd Hoffmanndf0db222010-11-09 17:28:38 +010069 if (dev->cad >= 15) {
xiaoqiang zhaobda8d9b2016-05-13 11:46:59 +080070 error_setg(errp, "HDA audio codec address is full");
71 return;
Gerd Hoffmanndf0db222010-11-09 17:28:38 +010072 }
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +010073 bus->next_cad = dev->cad + 1;
Martin Kletzanderb7639b72022-04-25 10:21:46 +020074 cdc->init(dev, errp);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +010075}
76
Markus Armbrusterb69c3c22020-05-05 17:29:24 +020077static void hda_codec_dev_unrealize(DeviceState *qdev)
Gerd Hoffmanndc4b9242010-11-09 11:47:44 +010078{
xiaoqiang zhaoe19202a2016-05-13 11:46:58 +080079 HDACodecDevice *dev = HDA_CODEC_DEVICE(qdev);
Anthony Liguoridbaa7902011-12-16 13:39:51 -060080 HDACodecDeviceClass *cdc = HDA_CODEC_DEVICE_GET_CLASS(dev);
Gerd Hoffmanndc4b9242010-11-09 11:47:44 +010081
Anthony Liguoridbaa7902011-12-16 13:39:51 -060082 if (cdc->exit) {
83 cdc->exit(dev);
Gerd Hoffmanndc4b9242010-11-09 11:47:44 +010084 }
Gerd Hoffmanndc4b9242010-11-09 11:47:44 +010085}
86
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +010087HDACodecDevice *hda_codec_find(HDACodecBus *bus, uint32_t cad)
88{
Anthony Liguori0866aca2011-12-23 15:34:39 -060089 BusChild *kid;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +010090 HDACodecDevice *cdev;
91
Anthony Liguori0866aca2011-12-23 15:34:39 -060092 QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
93 DeviceState *qdev = kid->child;
xiaoqiang zhaoe19202a2016-05-13 11:46:58 +080094 cdev = HDA_CODEC_DEVICE(qdev);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +010095 if (cdev->cad == cad) {
96 return cdev;
97 }
98 }
99 return NULL;
100}
101
102void hda_codec_response(HDACodecDevice *dev, bool solicited, uint32_t response)
103{
xiaoqiang zhaoe19202a2016-05-13 11:46:58 +0800104 HDACodecBus *bus = HDA_BUS(dev->qdev.parent_bus);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100105 bus->response(dev, solicited, response);
106}
107
108bool hda_codec_xfer(HDACodecDevice *dev, uint32_t stnr, bool output,
109 uint8_t *buf, uint32_t len)
110{
xiaoqiang zhaoe19202a2016-05-13 11:46:58 +0800111 HDACodecBus *bus = HDA_BUS(dev->qdev.parent_bus);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100112 return bus->xfer(dev, stnr, output, buf, len);
113}
114
115/* --------------------------------------------------------------------- */
116/* intel hda emulation */
117
118typedef struct IntelHDAStream IntelHDAStream;
119typedef struct IntelHDAState IntelHDAState;
120typedef struct IntelHDAReg IntelHDAReg;
121
122typedef struct bpl {
123 uint64_t addr;
124 uint32_t len;
125 uint32_t flags;
126} bpl;
127
128struct IntelHDAStream {
129 /* registers */
130 uint32_t ctl;
131 uint32_t lpib;
132 uint32_t cbl;
133 uint32_t lvi;
134 uint32_t fmt;
135 uint32_t bdlp_lbase;
136 uint32_t bdlp_ubase;
137
138 /* state */
139 bpl *bpl;
140 uint32_t bentries;
141 uint32_t bsize, be, bp;
142};
143
144struct IntelHDAState {
145 PCIDevice pci;
146 const char *name;
147 HDACodecBus codecs;
148
149 /* registers */
150 uint32_t g_ctl;
151 uint32_t wake_en;
152 uint32_t state_sts;
153 uint32_t int_ctl;
154 uint32_t int_sts;
155 uint32_t wall_clk;
156
157 uint32_t corb_lbase;
158 uint32_t corb_ubase;
159 uint32_t corb_rp;
160 uint32_t corb_wp;
161 uint32_t corb_ctl;
162 uint32_t corb_sts;
163 uint32_t corb_size;
164
165 uint32_t rirb_lbase;
166 uint32_t rirb_ubase;
167 uint32_t rirb_wp;
168 uint32_t rirb_cnt;
169 uint32_t rirb_ctl;
170 uint32_t rirb_sts;
171 uint32_t rirb_size;
172
173 uint32_t dp_lbase;
174 uint32_t dp_ubase;
175
176 uint32_t icw;
177 uint32_t irr;
178 uint32_t ics;
179
180 /* streams */
181 IntelHDAStream st[8];
182
183 /* state */
Philippe Mathieu-Daudéa9d8ba22020-03-05 13:45:19 +0100184 MemoryRegion container;
Avi Kivity234bbdf2011-08-08 16:09:15 +0300185 MemoryRegion mmio;
Philippe Mathieu-Daudéa9d8ba22020-03-05 13:45:19 +0100186 MemoryRegion alias;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100187 uint32_t rirb_count;
188 int64_t wall_base_ns;
189
190 /* debug logging */
191 const IntelHDAReg *last_reg;
192 uint32_t last_val;
193 uint32_t last_write;
194 uint32_t last_sec;
195 uint32_t repeat_count;
196
197 /* properties */
198 uint32_t debug;
Cao jinc0f2abf2016-06-20 14:13:35 +0800199 OnOffAuto msi;
Jan Kiszkad209c742014-07-30 09:02:01 +0200200 bool old_msi_addr;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100201};
202
Peter Crosthwaite062db742013-06-06 15:34:08 +1000203#define TYPE_INTEL_HDA_GENERIC "intel-hda-generic"
204
Eduardo Habkost8110fa12020-08-31 17:07:33 -0400205DECLARE_INSTANCE_CHECKER(IntelHDAState, INTEL_HDA,
206 TYPE_INTEL_HDA_GENERIC)
Peter Crosthwaite52bb7c62013-06-06 15:34:52 +1000207
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100208struct IntelHDAReg {
209 const char *name; /* register name */
210 uint32_t size; /* size in bytes */
211 uint32_t reset; /* reset value */
212 uint32_t wmask; /* write mask */
213 uint32_t wclear; /* write 1 to clear bits */
214 uint32_t offset; /* location in IntelHDAState */
215 uint32_t shift; /* byte access entries for dwords */
216 uint32_t stream;
217 void (*whandler)(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old);
218 void (*rhandler)(IntelHDAState *d, const IntelHDAReg *reg);
219};
220
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100221/* --------------------------------------------------------------------- */
222
Avi Kivitya8170e52012-10-23 12:30:10 +0200223static hwaddr intel_hda_addr(uint32_t lbase, uint32_t ubase)
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100224{
Eduardo Habkost9be38592016-06-13 18:57:58 -0300225 return ((uint64_t)ubase << 32) | lbase;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100226}
227
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100228static void intel_hda_update_int_sts(IntelHDAState *d)
229{
230 uint32_t sts = 0;
231 uint32_t i;
232
233 /* update controller status */
234 if (d->rirb_sts & ICH6_RBSTS_IRQ) {
235 sts |= (1 << 30);
236 }
237 if (d->rirb_sts & ICH6_RBSTS_OVERRUN) {
238 sts |= (1 << 30);
239 }
François Revolaf934852010-11-09 11:47:46 +0100240 if (d->state_sts & d->wake_en) {
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100241 sts |= (1 << 30);
242 }
243
244 /* update stream status */
245 for (i = 0; i < 8; i++) {
246 /* buffer completion interrupt */
247 if (d->st[i].ctl & (1 << 26)) {
248 sts |= (1 << i);
249 }
250 }
251
252 /* update global status */
253 if (sts & d->int_ctl) {
Peter Maydellb1fe60c2014-05-09 14:22:53 +0100254 sts |= (1U << 31);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100255 }
256
257 d->int_sts = sts;
258}
259
260static void intel_hda_update_irq(IntelHDAState *d)
261{
Cao jinc0f2abf2016-06-20 14:13:35 +0800262 bool msi = msi_enabled(&d->pci);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100263 int level;
264
265 intel_hda_update_int_sts(d);
Peter Maydellb1fe60c2014-05-09 14:22:53 +0100266 if (d->int_sts & (1U << 31) && d->int_ctl & (1U << 31)) {
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100267 level = 1;
268 } else {
269 level = 0;
270 }
Alistair Francisa89f3642017-11-08 14:56:31 -0800271 dprint(d, 2, "%s: level %d [%s]\n", __func__,
Gerd Hoffmann17786d52010-11-09 11:47:48 +0100272 level, msi ? "msi" : "intx");
273 if (msi) {
274 if (level) {
275 msi_notify(&d->pci, 0);
276 }
277 } else {
Marcel Apfelbaum9e64f8a2013-10-07 10:36:39 +0300278 pci_set_irq(&d->pci, level);
Gerd Hoffmann17786d52010-11-09 11:47:48 +0100279 }
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100280}
281
282static int intel_hda_send_command(IntelHDAState *d, uint32_t verb)
283{
284 uint32_t cad, nid, data;
285 HDACodecDevice *codec;
Anthony Liguoridbaa7902011-12-16 13:39:51 -0600286 HDACodecDeviceClass *cdc;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100287
288 cad = (verb >> 28) & 0x0f;
289 if (verb & (1 << 27)) {
290 /* indirect node addressing, not specified in HDA 1.0 */
Alistair Francisa89f3642017-11-08 14:56:31 -0800291 dprint(d, 1, "%s: indirect node addressing (guest bug?)\n", __func__);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100292 return -1;
293 }
294 nid = (verb >> 20) & 0x7f;
295 data = verb & 0xfffff;
296
297 codec = hda_codec_find(&d->codecs, cad);
298 if (codec == NULL) {
Alistair Francisa89f3642017-11-08 14:56:31 -0800299 dprint(d, 1, "%s: addressed non-existing codec\n", __func__);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100300 return -1;
301 }
Anthony Liguoridbaa7902011-12-16 13:39:51 -0600302 cdc = HDA_CODEC_DEVICE_GET_CLASS(codec);
303 cdc->command(codec, nid, data);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100304 return 0;
305}
306
307static void intel_hda_corb_run(IntelHDAState *d)
308{
Avi Kivitya8170e52012-10-23 12:30:10 +0200309 hwaddr addr;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100310 uint32_t rp, verb;
311
312 if (d->ics & ICH6_IRS_BUSY) {
Alistair Francisa89f3642017-11-08 14:56:31 -0800313 dprint(d, 2, "%s: [icw] verb 0x%08x\n", __func__, d->icw);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100314 intel_hda_send_command(d, d->icw);
315 return;
316 }
317
318 for (;;) {
319 if (!(d->corb_ctl & ICH6_CORBCTL_RUN)) {
Alistair Francisa89f3642017-11-08 14:56:31 -0800320 dprint(d, 2, "%s: !run\n", __func__);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100321 return;
322 }
323 if ((d->corb_rp & 0xff) == d->corb_wp) {
Alistair Francisa89f3642017-11-08 14:56:31 -0800324 dprint(d, 2, "%s: corb ring empty\n", __func__);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100325 return;
326 }
327 if (d->rirb_count == d->rirb_cnt) {
Alistair Francisa89f3642017-11-08 14:56:31 -0800328 dprint(d, 2, "%s: rirb count reached\n", __func__);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100329 return;
330 }
331
332 rp = (d->corb_rp + 1) & 0xff;
333 addr = intel_hda_addr(d->corb_lbase, d->corb_ubase);
Philippe Mathieu-Daudé4a630542021-12-17 23:49:30 +0100334 ldl_le_pci_dma(&d->pci, addr + 4 * rp, &verb, MEMTXATTRS_UNSPECIFIED);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100335 d->corb_rp = rp;
336
Alistair Francisa89f3642017-11-08 14:56:31 -0800337 dprint(d, 2, "%s: [rp 0x%x] verb 0x%08x\n", __func__, rp, verb);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100338 intel_hda_send_command(d, verb);
339 }
340}
341
342static void intel_hda_response(HDACodecDevice *dev, bool solicited, uint32_t response)
343{
Philippe Mathieu-Daudé79fa9982021-12-18 17:09:11 +0100344 const MemTxAttrs attrs = { .memory = true };
xiaoqiang zhaoe19202a2016-05-13 11:46:58 +0800345 HDACodecBus *bus = HDA_BUS(dev->qdev.parent_bus);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100346 IntelHDAState *d = container_of(bus, IntelHDAState, codecs);
Avi Kivitya8170e52012-10-23 12:30:10 +0200347 hwaddr addr;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100348 uint32_t wp, ex;
Philippe Mathieu-Daudébe5a8cf2021-12-18 17:09:10 +0100349 MemTxResult res = MEMTX_OK;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100350
351 if (d->ics & ICH6_IRS_BUSY) {
352 dprint(d, 2, "%s: [irr] response 0x%x, cad 0x%x\n",
Alistair Francisa89f3642017-11-08 14:56:31 -0800353 __func__, response, dev->cad);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100354 d->irr = response;
355 d->ics &= ~(ICH6_IRS_BUSY | 0xf0);
356 d->ics |= (ICH6_IRS_VALID | (dev->cad << 4));
357 return;
358 }
359
360 if (!(d->rirb_ctl & ICH6_RBCTL_DMA_EN)) {
Alistair Francisa89f3642017-11-08 14:56:31 -0800361 dprint(d, 1, "%s: rirb dma disabled, drop codec response\n", __func__);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100362 return;
363 }
364
365 ex = (solicited ? 0 : (1 << 4)) | dev->cad;
366 wp = (d->rirb_wp + 1) & 0xff;
367 addr = intel_hda_addr(d->rirb_lbase, d->rirb_ubase);
Philippe Mathieu-Daudébe5a8cf2021-12-18 17:09:10 +0100368 res |= stl_le_pci_dma(&d->pci, addr + 8 * wp, response, attrs);
369 res |= stl_le_pci_dma(&d->pci, addr + 8 * wp + 4, ex, attrs);
370 if (res != MEMTX_OK && (d->rirb_ctl & ICH6_RBCTL_OVERRUN_EN)) {
371 d->rirb_sts |= ICH6_RBSTS_OVERRUN;
372 intel_hda_update_irq(d);
373 }
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100374 d->rirb_wp = wp;
375
376 dprint(d, 2, "%s: [wp 0x%x] response 0x%x, extra 0x%x\n",
Alistair Francisa89f3642017-11-08 14:56:31 -0800377 __func__, wp, response, ex);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100378
379 d->rirb_count++;
380 if (d->rirb_count == d->rirb_cnt) {
Alistair Francisa89f3642017-11-08 14:56:31 -0800381 dprint(d, 2, "%s: rirb count reached (%d)\n", __func__, d->rirb_count);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100382 if (d->rirb_ctl & ICH6_RBCTL_IRQ_EN) {
383 d->rirb_sts |= ICH6_RBSTS_IRQ;
384 intel_hda_update_irq(d);
385 }
386 } else if ((d->corb_rp & 0xff) == d->corb_wp) {
Alistair Francisa89f3642017-11-08 14:56:31 -0800387 dprint(d, 2, "%s: corb ring empty (%d/%d)\n", __func__,
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100388 d->rirb_count, d->rirb_cnt);
389 if (d->rirb_ctl & ICH6_RBCTL_IRQ_EN) {
390 d->rirb_sts |= ICH6_RBSTS_IRQ;
391 intel_hda_update_irq(d);
392 }
393 }
394}
395
396static bool intel_hda_xfer(HDACodecDevice *dev, uint32_t stnr, bool output,
397 uint8_t *buf, uint32_t len)
398{
Philippe Mathieu-Daudéa423a1b2021-12-17 22:39:42 +0100399 const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
xiaoqiang zhaoe19202a2016-05-13 11:46:58 +0800400 HDACodecBus *bus = HDA_BUS(dev->qdev.parent_bus);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100401 IntelHDAState *d = container_of(bus, IntelHDAState, codecs);
Avi Kivitya8170e52012-10-23 12:30:10 +0200402 hwaddr addr;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100403 uint32_t s, copy, left;
Marc-André Lureau36ac4ad2011-10-25 16:53:00 +0200404 IntelHDAStream *st;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100405 bool irq = false;
406
Marc-André Lureau36ac4ad2011-10-25 16:53:00 +0200407 st = output ? d->st + 4 : d->st;
408 for (s = 0; s < 4; s++) {
409 if (stnr == ((st[s].ctl >> 20) & 0x0f)) {
410 st = st + s;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100411 break;
412 }
413 }
Gerd Hoffmann18ebcc82011-11-02 12:56:14 +0100414 if (s == 4) {
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100415 return false;
416 }
417 if (st->bpl == NULL) {
418 return false;
419 }
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100420
421 left = len;
Prasad J Pandit0c0fc2b2016-10-20 13:10:24 +0530422 s = st->bentries;
423 while (left > 0 && s-- > 0) {
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100424 copy = left;
425 if (copy > st->bsize - st->lpib)
426 copy = st->bsize - st->lpib;
427 if (copy > st->bpl[st->be].len - st->bp)
428 copy = st->bpl[st->be].len - st->bp;
429
430 dprint(d, 3, "dma: entry %d, pos %d/%d, copy %d\n",
431 st->be, st->bp, st->bpl[st->be].len, copy);
432
Philippe Mathieu-Daudée2d784b2021-12-15 22:18:19 +0100433 pci_dma_rw(&d->pci, st->bpl[st->be].addr + st->bp, buf, copy, !output,
Philippe Mathieu-Daudéa423a1b2021-12-17 22:39:42 +0100434 attrs);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100435 st->lpib += copy;
436 st->bp += copy;
437 buf += copy;
438 left -= copy;
439
440 if (st->bpl[st->be].len == st->bp) {
441 /* bpl entry filled */
442 if (st->bpl[st->be].flags & 0x01) {
443 irq = true;
444 }
445 st->bp = 0;
446 st->be++;
447 if (st->be == st->bentries) {
448 /* bpl wrap around */
449 st->be = 0;
450 st->lpib = 0;
451 }
452 }
453 }
454 if (d->dp_lbase & 0x01) {
Gerd Hoffmannd58ce682013-11-29 14:25:33 +0100455 s = st - d->st;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100456 addr = intel_hda_addr(d->dp_lbase & ~0x01, d->dp_ubase);
Philippe Mathieu-Daudéa423a1b2021-12-17 22:39:42 +0100457 stl_le_pci_dma(&d->pci, addr + 8 * s, st->lpib, attrs);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100458 }
459 dprint(d, 3, "dma: --\n");
460
461 if (irq) {
462 st->ctl |= (1 << 26); /* buffer completion interrupt */
463 intel_hda_update_irq(d);
464 }
465 return true;
466}
467
468static void intel_hda_parse_bdl(IntelHDAState *d, IntelHDAStream *st)
469{
Avi Kivitya8170e52012-10-23 12:30:10 +0200470 hwaddr addr;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100471 uint8_t buf[16];
472 uint32_t i;
473
474 addr = intel_hda_addr(st->bdlp_lbase, st->bdlp_ubase);
475 st->bentries = st->lvi +1;
Anthony Liguori7267c092011-08-20 22:09:37 -0500476 g_free(st->bpl);
Markus Armbrusterb21e2382022-03-15 15:41:56 +0100477 st->bpl = g_new(bpl, st->bentries);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100478 for (i = 0; i < st->bentries; i++, addr += 16) {
David Gibsonfa0ce552011-10-31 17:06:55 +1100479 pci_dma_read(&d->pci, addr, buf, 16);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100480 st->bpl[i].addr = le64_to_cpu(*(uint64_t *)buf);
481 st->bpl[i].len = le32_to_cpu(*(uint32_t *)(buf + 8));
482 st->bpl[i].flags = le32_to_cpu(*(uint32_t *)(buf + 12));
483 dprint(d, 1, "bdl/%d: 0x%" PRIx64 " +0x%x, 0x%x\n",
484 i, st->bpl[i].addr, st->bpl[i].len, st->bpl[i].flags);
485 }
486
487 st->bsize = st->cbl;
488 st->lpib = 0;
489 st->be = 0;
490 st->bp = 0;
491}
492
Marc-André Lureauba43d282011-10-25 16:53:01 +0200493static void intel_hda_notify_codecs(IntelHDAState *d, uint32_t stream, bool running, bool output)
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100494{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600495 BusChild *kid;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100496 HDACodecDevice *cdev;
497
Anthony Liguori0866aca2011-12-23 15:34:39 -0600498 QTAILQ_FOREACH(kid, &d->codecs.qbus.children, sibling) {
499 DeviceState *qdev = kid->child;
Anthony Liguoridbaa7902011-12-16 13:39:51 -0600500 HDACodecDeviceClass *cdc;
501
xiaoqiang zhaoe19202a2016-05-13 11:46:58 +0800502 cdev = HDA_CODEC_DEVICE(qdev);
Anthony Liguoridbaa7902011-12-16 13:39:51 -0600503 cdc = HDA_CODEC_DEVICE_GET_CLASS(cdev);
504 if (cdc->stream) {
505 cdc->stream(cdev, stream, running, output);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100506 }
507 }
508}
509
510/* --------------------------------------------------------------------- */
511
512static void intel_hda_set_g_ctl(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
513{
514 if ((d->g_ctl & ICH6_GCTL_RESET) == 0) {
Peter Maydell3e95ef42022-10-14 15:26:31 +0100515 device_cold_reset(DEVICE(d));
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100516 }
517}
518
Gerd Hoffmann6a0d02f2010-11-09 11:47:47 +0100519static void intel_hda_set_wake_en(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
520{
521 intel_hda_update_irq(d);
522}
523
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100524static void intel_hda_set_state_sts(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
525{
526 intel_hda_update_irq(d);
527}
528
529static void intel_hda_set_int_ctl(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
530{
531 intel_hda_update_irq(d);
532}
533
534static void intel_hda_get_wall_clk(IntelHDAState *d, const IntelHDAReg *reg)
535{
536 int64_t ns;
537
Alex Blighbc72ad62013-08-21 16:03:08 +0100538 ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - d->wall_base_ns;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100539 d->wall_clk = (uint32_t)(ns * 24 / 1000); /* 24 MHz */
540}
541
542static void intel_hda_set_corb_wp(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
543{
544 intel_hda_corb_run(d);
545}
546
547static void intel_hda_set_corb_ctl(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
548{
549 intel_hda_corb_run(d);
550}
551
552static void intel_hda_set_rirb_wp(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
553{
554 if (d->rirb_wp & ICH6_RIRBWP_RST) {
555 d->rirb_wp = 0;
556 }
557}
558
559static void intel_hda_set_rirb_sts(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
560{
561 intel_hda_update_irq(d);
562
563 if ((old & ICH6_RBSTS_IRQ) && !(d->rirb_sts & ICH6_RBSTS_IRQ)) {
564 /* cleared ICH6_RBSTS_IRQ */
565 d->rirb_count = 0;
566 intel_hda_corb_run(d);
567 }
568}
569
570static void intel_hda_set_ics(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
571{
572 if (d->ics & ICH6_IRS_BUSY) {
573 intel_hda_corb_run(d);
574 }
575}
576
577static void intel_hda_set_st_ctl(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
578{
Marc-André Lureauba43d282011-10-25 16:53:01 +0200579 bool output = reg->stream >= 4;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100580 IntelHDAStream *st = d->st + reg->stream;
581
582 if (st->ctl & 0x01) {
583 /* reset */
584 dprint(d, 1, "st #%d: reset\n", reg->stream);
Volker Rümelinecd5f282021-12-26 16:40:17 +0100585 st->ctl = SD_STS_FIFO_READY << 24 | SD_CTL_STREAM_RESET;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100586 }
587 if ((st->ctl & 0x02) != (old & 0x02)) {
588 uint32_t stnr = (st->ctl >> 20) & 0x0f;
589 /* run bit flipped */
590 if (st->ctl & 0x02) {
591 /* start */
592 dprint(d, 1, "st #%d: start %d (ring buf %d bytes)\n",
593 reg->stream, stnr, st->cbl);
594 intel_hda_parse_bdl(d, st);
Marc-André Lureauba43d282011-10-25 16:53:01 +0200595 intel_hda_notify_codecs(d, stnr, true, output);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100596 } else {
597 /* stop */
598 dprint(d, 1, "st #%d: stop %d\n", reg->stream, stnr);
Marc-André Lureauba43d282011-10-25 16:53:01 +0200599 intel_hda_notify_codecs(d, stnr, false, output);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100600 }
601 }
602 intel_hda_update_irq(d);
603}
604
605/* --------------------------------------------------------------------- */
606
607#define ST_REG(_n, _o) (0x80 + (_n) * 0x20 + (_o))
608
609static const struct IntelHDAReg regtab[] = {
610 /* global */
611 [ ICH6_REG_GCAP ] = {
612 .name = "GCAP",
613 .size = 2,
614 .reset = 0x4401,
615 },
616 [ ICH6_REG_VMIN ] = {
617 .name = "VMIN",
618 .size = 1,
619 },
620 [ ICH6_REG_VMAJ ] = {
621 .name = "VMAJ",
622 .size = 1,
623 .reset = 1,
624 },
625 [ ICH6_REG_OUTPAY ] = {
626 .name = "OUTPAY",
627 .size = 2,
628 .reset = 0x3c,
629 },
630 [ ICH6_REG_INPAY ] = {
631 .name = "INPAY",
632 .size = 2,
633 .reset = 0x1d,
634 },
635 [ ICH6_REG_GCTL ] = {
636 .name = "GCTL",
637 .size = 4,
638 .wmask = 0x0103,
639 .offset = offsetof(IntelHDAState, g_ctl),
640 .whandler = intel_hda_set_g_ctl,
641 },
642 [ ICH6_REG_WAKEEN ] = {
643 .name = "WAKEEN",
644 .size = 2,
Gerd Hoffmanndf0db222010-11-09 17:28:38 +0100645 .wmask = 0x7fff,
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100646 .offset = offsetof(IntelHDAState, wake_en),
Gerd Hoffmann6a0d02f2010-11-09 11:47:47 +0100647 .whandler = intel_hda_set_wake_en,
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100648 },
649 [ ICH6_REG_STATESTS ] = {
650 .name = "STATESTS",
651 .size = 2,
Gerd Hoffmanndf0db222010-11-09 17:28:38 +0100652 .wmask = 0x7fff,
653 .wclear = 0x7fff,
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100654 .offset = offsetof(IntelHDAState, state_sts),
655 .whandler = intel_hda_set_state_sts,
656 },
657
658 /* interrupts */
659 [ ICH6_REG_INTCTL ] = {
660 .name = "INTCTL",
661 .size = 4,
662 .wmask = 0xc00000ff,
663 .offset = offsetof(IntelHDAState, int_ctl),
664 .whandler = intel_hda_set_int_ctl,
665 },
666 [ ICH6_REG_INTSTS ] = {
667 .name = "INTSTS",
668 .size = 4,
669 .wmask = 0xc00000ff,
670 .wclear = 0xc00000ff,
671 .offset = offsetof(IntelHDAState, int_sts),
672 },
673
674 /* misc */
675 [ ICH6_REG_WALLCLK ] = {
676 .name = "WALLCLK",
677 .size = 4,
678 .offset = offsetof(IntelHDAState, wall_clk),
679 .rhandler = intel_hda_get_wall_clk,
680 },
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100681
682 /* dma engine */
683 [ ICH6_REG_CORBLBASE ] = {
684 .name = "CORBLBASE",
685 .size = 4,
686 .wmask = 0xffffff80,
687 .offset = offsetof(IntelHDAState, corb_lbase),
688 },
689 [ ICH6_REG_CORBUBASE ] = {
690 .name = "CORBUBASE",
691 .size = 4,
692 .wmask = 0xffffffff,
693 .offset = offsetof(IntelHDAState, corb_ubase),
694 },
695 [ ICH6_REG_CORBWP ] = {
696 .name = "CORBWP",
697 .size = 2,
698 .wmask = 0xff,
699 .offset = offsetof(IntelHDAState, corb_wp),
700 .whandler = intel_hda_set_corb_wp,
701 },
702 [ ICH6_REG_CORBRP ] = {
703 .name = "CORBRP",
704 .size = 2,
705 .wmask = 0x80ff,
706 .offset = offsetof(IntelHDAState, corb_rp),
707 },
708 [ ICH6_REG_CORBCTL ] = {
709 .name = "CORBCTL",
710 .size = 1,
711 .wmask = 0x03,
712 .offset = offsetof(IntelHDAState, corb_ctl),
713 .whandler = intel_hda_set_corb_ctl,
714 },
715 [ ICH6_REG_CORBSTS ] = {
716 .name = "CORBSTS",
717 .size = 1,
718 .wmask = 0x01,
719 .wclear = 0x01,
720 .offset = offsetof(IntelHDAState, corb_sts),
721 },
722 [ ICH6_REG_CORBSIZE ] = {
723 .name = "CORBSIZE",
724 .size = 1,
725 .reset = 0x42,
726 .offset = offsetof(IntelHDAState, corb_size),
727 },
728 [ ICH6_REG_RIRBLBASE ] = {
729 .name = "RIRBLBASE",
730 .size = 4,
731 .wmask = 0xffffff80,
732 .offset = offsetof(IntelHDAState, rirb_lbase),
733 },
734 [ ICH6_REG_RIRBUBASE ] = {
735 .name = "RIRBUBASE",
736 .size = 4,
737 .wmask = 0xffffffff,
738 .offset = offsetof(IntelHDAState, rirb_ubase),
739 },
740 [ ICH6_REG_RIRBWP ] = {
741 .name = "RIRBWP",
742 .size = 2,
743 .wmask = 0x8000,
744 .offset = offsetof(IntelHDAState, rirb_wp),
745 .whandler = intel_hda_set_rirb_wp,
746 },
747 [ ICH6_REG_RINTCNT ] = {
748 .name = "RINTCNT",
749 .size = 2,
750 .wmask = 0xff,
751 .offset = offsetof(IntelHDAState, rirb_cnt),
752 },
753 [ ICH6_REG_RIRBCTL ] = {
754 .name = "RIRBCTL",
755 .size = 1,
756 .wmask = 0x07,
757 .offset = offsetof(IntelHDAState, rirb_ctl),
758 },
759 [ ICH6_REG_RIRBSTS ] = {
760 .name = "RIRBSTS",
761 .size = 1,
762 .wmask = 0x05,
763 .wclear = 0x05,
764 .offset = offsetof(IntelHDAState, rirb_sts),
765 .whandler = intel_hda_set_rirb_sts,
766 },
767 [ ICH6_REG_RIRBSIZE ] = {
768 .name = "RIRBSIZE",
769 .size = 1,
770 .reset = 0x42,
771 .offset = offsetof(IntelHDAState, rirb_size),
772 },
773
774 [ ICH6_REG_DPLBASE ] = {
775 .name = "DPLBASE",
776 .size = 4,
777 .wmask = 0xffffff81,
778 .offset = offsetof(IntelHDAState, dp_lbase),
779 },
780 [ ICH6_REG_DPUBASE ] = {
781 .name = "DPUBASE",
782 .size = 4,
783 .wmask = 0xffffffff,
784 .offset = offsetof(IntelHDAState, dp_ubase),
785 },
786
787 [ ICH6_REG_IC ] = {
788 .name = "ICW",
789 .size = 4,
790 .wmask = 0xffffffff,
791 .offset = offsetof(IntelHDAState, icw),
792 },
793 [ ICH6_REG_IR ] = {
794 .name = "IRR",
795 .size = 4,
796 .offset = offsetof(IntelHDAState, irr),
797 },
798 [ ICH6_REG_IRS ] = {
799 .name = "ICS",
800 .size = 2,
801 .wmask = 0x0003,
802 .wclear = 0x0002,
803 .offset = offsetof(IntelHDAState, ics),
804 .whandler = intel_hda_set_ics,
805 },
806
807#define HDA_STREAM(_t, _i) \
808 [ ST_REG(_i, ICH6_REG_SD_CTL) ] = { \
809 .stream = _i, \
810 .name = _t stringify(_i) " CTL", \
811 .size = 4, \
812 .wmask = 0x1cff001f, \
813 .offset = offsetof(IntelHDAState, st[_i].ctl), \
814 .whandler = intel_hda_set_st_ctl, \
815 }, \
816 [ ST_REG(_i, ICH6_REG_SD_CTL) + 2] = { \
817 .stream = _i, \
818 .name = _t stringify(_i) " CTL(stnr)", \
819 .size = 1, \
820 .shift = 16, \
821 .wmask = 0x00ff0000, \
822 .offset = offsetof(IntelHDAState, st[_i].ctl), \
823 .whandler = intel_hda_set_st_ctl, \
824 }, \
825 [ ST_REG(_i, ICH6_REG_SD_STS)] = { \
826 .stream = _i, \
827 .name = _t stringify(_i) " CTL(sts)", \
828 .size = 1, \
829 .shift = 24, \
830 .wmask = 0x1c000000, \
831 .wclear = 0x1c000000, \
832 .offset = offsetof(IntelHDAState, st[_i].ctl), \
833 .whandler = intel_hda_set_st_ctl, \
Stanislav Vorobiova2554a32014-04-29 16:48:47 +0400834 .reset = SD_STS_FIFO_READY << 24 \
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100835 }, \
836 [ ST_REG(_i, ICH6_REG_SD_LPIB) ] = { \
837 .stream = _i, \
838 .name = _t stringify(_i) " LPIB", \
839 .size = 4, \
840 .offset = offsetof(IntelHDAState, st[_i].lpib), \
841 }, \
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100842 [ ST_REG(_i, ICH6_REG_SD_CBL) ] = { \
843 .stream = _i, \
844 .name = _t stringify(_i) " CBL", \
845 .size = 4, \
846 .wmask = 0xffffffff, \
847 .offset = offsetof(IntelHDAState, st[_i].cbl), \
848 }, \
849 [ ST_REG(_i, ICH6_REG_SD_LVI) ] = { \
850 .stream = _i, \
851 .name = _t stringify(_i) " LVI", \
852 .size = 2, \
853 .wmask = 0x00ff, \
854 .offset = offsetof(IntelHDAState, st[_i].lvi), \
855 }, \
856 [ ST_REG(_i, ICH6_REG_SD_FIFOSIZE) ] = { \
857 .stream = _i, \
858 .name = _t stringify(_i) " FIFOS", \
859 .size = 2, \
860 .reset = HDA_BUFFER_SIZE, \
861 }, \
862 [ ST_REG(_i, ICH6_REG_SD_FORMAT) ] = { \
863 .stream = _i, \
864 .name = _t stringify(_i) " FMT", \
865 .size = 2, \
866 .wmask = 0x7f7f, \
867 .offset = offsetof(IntelHDAState, st[_i].fmt), \
868 }, \
869 [ ST_REG(_i, ICH6_REG_SD_BDLPL) ] = { \
870 .stream = _i, \
871 .name = _t stringify(_i) " BDLPL", \
872 .size = 4, \
873 .wmask = 0xffffff80, \
874 .offset = offsetof(IntelHDAState, st[_i].bdlp_lbase), \
875 }, \
876 [ ST_REG(_i, ICH6_REG_SD_BDLPU) ] = { \
877 .stream = _i, \
878 .name = _t stringify(_i) " BDLPU", \
879 .size = 4, \
880 .wmask = 0xffffffff, \
881 .offset = offsetof(IntelHDAState, st[_i].bdlp_ubase), \
882 }, \
883
884 HDA_STREAM("IN", 0)
885 HDA_STREAM("IN", 1)
886 HDA_STREAM("IN", 2)
887 HDA_STREAM("IN", 3)
888
889 HDA_STREAM("OUT", 4)
890 HDA_STREAM("OUT", 5)
891 HDA_STREAM("OUT", 6)
892 HDA_STREAM("OUT", 7)
893
894};
895
Avi Kivitya8170e52012-10-23 12:30:10 +0200896static const IntelHDAReg *intel_hda_reg_find(IntelHDAState *d, hwaddr addr)
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100897{
898 const IntelHDAReg *reg;
899
Stefan Weildff74242013-12-07 14:48:04 +0100900 if (addr >= ARRAY_SIZE(regtab)) {
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100901 goto noreg;
902 }
903 reg = regtab+addr;
904 if (reg->name == NULL) {
905 goto noreg;
906 }
907 return reg;
908
909noreg:
910 dprint(d, 1, "unknown register, addr 0x%x\n", (int) addr);
911 return NULL;
912}
913
914static uint32_t *intel_hda_reg_addr(IntelHDAState *d, const IntelHDAReg *reg)
915{
916 uint8_t *addr = (void*)d;
917
918 addr += reg->offset;
919 return (uint32_t*)addr;
920}
921
922static void intel_hda_reg_write(IntelHDAState *d, const IntelHDAReg *reg, uint32_t val,
923 uint32_t wmask)
924{
925 uint32_t *addr;
926 uint32_t old;
927
928 if (!reg) {
929 return;
930 }
Gerd Hoffmann7ec91062018-11-23 07:39:57 +0100931 if (!reg->wmask) {
932 qemu_log_mask(LOG_GUEST_ERROR, "intel-hda: write to r/o reg %s\n",
933 reg->name);
934 return;
935 }
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100936
937 if (d->debug) {
938 time_t now = time(NULL);
939 if (d->last_write && d->last_reg == reg && d->last_val == val) {
940 d->repeat_count++;
941 if (d->last_sec != now) {
942 dprint(d, 2, "previous register op repeated %d times\n", d->repeat_count);
943 d->last_sec = now;
944 d->repeat_count = 0;
945 }
946 } else {
947 if (d->repeat_count) {
948 dprint(d, 2, "previous register op repeated %d times\n", d->repeat_count);
949 }
950 dprint(d, 2, "write %-16s: 0x%x (%x)\n", reg->name, val, wmask);
951 d->last_write = 1;
952 d->last_reg = reg;
953 d->last_val = val;
954 d->last_sec = now;
955 d->repeat_count = 0;
956 }
957 }
958 assert(reg->offset != 0);
959
960 addr = intel_hda_reg_addr(d, reg);
961 old = *addr;
962
963 if (reg->shift) {
964 val <<= reg->shift;
965 wmask <<= reg->shift;
966 }
967 wmask &= reg->wmask;
968 *addr &= ~wmask;
969 *addr |= wmask & val;
970 *addr &= ~(val & reg->wclear);
971
972 if (reg->whandler) {
973 reg->whandler(d, reg, old);
974 }
975}
976
977static uint32_t intel_hda_reg_read(IntelHDAState *d, const IntelHDAReg *reg,
978 uint32_t rmask)
979{
980 uint32_t *addr, ret;
981
982 if (!reg) {
983 return 0;
984 }
985
986 if (reg->rhandler) {
987 reg->rhandler(d, reg);
988 }
989
990 if (reg->offset == 0) {
991 /* constant read-only register */
992 ret = reg->reset;
993 } else {
994 addr = intel_hda_reg_addr(d, reg);
995 ret = *addr;
996 if (reg->shift) {
997 ret >>= reg->shift;
998 }
999 ret &= rmask;
1000 }
1001 if (d->debug) {
1002 time_t now = time(NULL);
1003 if (!d->last_write && d->last_reg == reg && d->last_val == ret) {
1004 d->repeat_count++;
1005 if (d->last_sec != now) {
1006 dprint(d, 2, "previous register op repeated %d times\n", d->repeat_count);
1007 d->last_sec = now;
1008 d->repeat_count = 0;
1009 }
1010 } else {
1011 if (d->repeat_count) {
1012 dprint(d, 2, "previous register op repeated %d times\n", d->repeat_count);
1013 }
1014 dprint(d, 2, "read %-16s: 0x%x (%x)\n", reg->name, ret, rmask);
1015 d->last_write = 0;
1016 d->last_reg = reg;
1017 d->last_val = ret;
1018 d->last_sec = now;
1019 d->repeat_count = 0;
1020 }
1021 }
1022 return ret;
1023}
1024
1025static void intel_hda_regs_reset(IntelHDAState *d)
1026{
1027 uint32_t *addr;
1028 int i;
1029
Stefan Weildff74242013-12-07 14:48:04 +01001030 for (i = 0; i < ARRAY_SIZE(regtab); i++) {
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001031 if (regtab[i].name == NULL) {
1032 continue;
1033 }
1034 if (regtab[i].offset == 0) {
1035 continue;
1036 }
1037 addr = intel_hda_reg_addr(d, regtab + i);
1038 *addr = regtab[i].reset;
1039 }
1040}
1041
1042/* --------------------------------------------------------------------- */
1043
Matt Parkera6b0bdc2017-08-27 20:20:38 +01001044static void intel_hda_mmio_write(void *opaque, hwaddr addr, uint64_t val,
1045 unsigned size)
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001046{
1047 IntelHDAState *d = opaque;
1048 const IntelHDAReg *reg = intel_hda_reg_find(d, addr);
1049
Matt Parkera6b0bdc2017-08-27 20:20:38 +01001050 intel_hda_reg_write(d, reg, val, MAKE_64BIT_MASK(0, size * 8));
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001051}
1052
Matt Parkera6b0bdc2017-08-27 20:20:38 +01001053static uint64_t intel_hda_mmio_read(void *opaque, hwaddr addr, unsigned size)
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001054{
1055 IntelHDAState *d = opaque;
1056 const IntelHDAReg *reg = intel_hda_reg_find(d, addr);
1057
Matt Parkera6b0bdc2017-08-27 20:20:38 +01001058 return intel_hda_reg_read(d, reg, MAKE_64BIT_MASK(0, size * 8));
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001059}
1060
Avi Kivity234bbdf2011-08-08 16:09:15 +03001061static const MemoryRegionOps intel_hda_mmio_ops = {
Matt Parkera6b0bdc2017-08-27 20:20:38 +01001062 .read = intel_hda_mmio_read,
1063 .write = intel_hda_mmio_write,
1064 .impl = {
1065 .min_access_size = 1,
1066 .max_access_size = 4,
Avi Kivity234bbdf2011-08-08 16:09:15 +03001067 },
1068 .endianness = DEVICE_NATIVE_ENDIAN,
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001069};
1070
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001071/* --------------------------------------------------------------------- */
1072
1073static void intel_hda_reset(DeviceState *dev)
1074{
Anthony Liguori0866aca2011-12-23 15:34:39 -06001075 BusChild *kid;
Peter Crosthwaite52bb7c62013-06-06 15:34:52 +10001076 IntelHDAState *d = INTEL_HDA(dev);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001077 HDACodecDevice *cdev;
1078
1079 intel_hda_regs_reset(d);
Alex Blighbc72ad62013-08-21 16:03:08 +01001080 d->wall_base_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001081
Anthony Liguori0866aca2011-12-23 15:34:39 -06001082 QTAILQ_FOREACH(kid, &d->codecs.qbus.children, sibling) {
1083 DeviceState *qdev = kid->child;
xiaoqiang zhaoe19202a2016-05-13 11:46:58 +08001084 cdev = HDA_CODEC_DEVICE(qdev);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001085 d->state_sts |= (1 << cdev->cad);
1086 }
1087 intel_hda_update_irq(d);
1088}
1089
Markus Armbruster9af21db2015-01-19 15:52:30 +01001090static void intel_hda_realize(PCIDevice *pci, Error **errp)
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001091{
Peter Crosthwaite52bb7c62013-06-06 15:34:52 +10001092 IntelHDAState *d = INTEL_HDA(pci);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001093 uint8_t *conf = d->pci.config;
Cao jin1108b2f2016-06-20 14:13:39 +08001094 Error *err = NULL;
1095 int ret;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001096
Anthony Liguorif79f2bf2011-12-04 11:17:51 -06001097 d->name = object_get_typename(OBJECT(d));
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001098
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001099 pci_config_set_interrupt_pin(conf, 1);
1100
1101 /* HDCTL off 0x40 bit 0 selects signaling mode (1-HDA, 0 - Ac97) 18.1.19 */
1102 conf[0x40] = 0x01;
1103
Cao jin1108b2f2016-06-20 14:13:39 +08001104 if (d->msi != ON_OFF_AUTO_OFF) {
1105 ret = msi_init(&d->pci, d->old_msi_addr ? 0x50 : 0x60,
1106 1, true, false, &err);
1107 /* Any error other than -ENOTSUP(board's MSI support is broken)
1108 * is a programming error */
1109 assert(!ret || ret == -ENOTSUP);
1110 if (ret && d->msi == ON_OFF_AUTO_ON) {
1111 /* Can't satisfy user's explicit msi=on request, fail */
1112 error_append_hint(&err, "You have to use msi=auto (default) or "
1113 "msi=off with this machine type.\n");
1114 error_propagate(errp, err);
1115 return;
1116 }
1117 assert(!err || d->msi == ON_OFF_AUTO_AUTO);
1118 /* With msi=auto, we fall back to MSI off silently */
1119 error_free(err);
1120 }
1121
Philippe Mathieu-Daudéa9d8ba22020-03-05 13:45:19 +01001122 memory_region_init(&d->container, OBJECT(d),
1123 "intel-hda-container", 0x4000);
Paolo Bonzini64bde0f2013-06-06 21:25:08 -04001124 memory_region_init_io(&d->mmio, OBJECT(d), &intel_hda_mmio_ops, d,
Philippe Mathieu-Daudéa9d8ba22020-03-05 13:45:19 +01001125 "intel-hda", 0x2000);
1126 memory_region_add_subregion(&d->container, 0x0000, &d->mmio);
1127 memory_region_init_alias(&d->alias, OBJECT(d), "intel-hda-alias",
1128 &d->mmio, 0, 0x2000);
1129 memory_region_add_subregion(&d->container, 0x2000, &d->alias);
1130 pci_register_bar(&d->pci, 0, 0, &d->container);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001131
Andreas Färberab809e82013-08-23 20:05:16 +02001132 hda_codec_bus_init(DEVICE(pci), &d->codecs, sizeof(d->codecs),
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001133 intel_hda_response, intel_hda_xfer);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001134}
1135
Alex Williamsonf90c2bc2012-07-03 22:39:27 -06001136static void intel_hda_exit(PCIDevice *pci)
Gerd Hoffmanndc4b9242010-11-09 11:47:44 +01001137{
Peter Crosthwaite52bb7c62013-06-06 15:34:52 +10001138 IntelHDAState *d = INTEL_HDA(pci);
Gerd Hoffmanndc4b9242010-11-09 11:47:44 +01001139
Jan Kiszka45fe15c2011-05-02 20:00:47 +02001140 msi_uninit(&d->pci);
Gerd Hoffmanndc4b9242010-11-09 11:47:44 +01001141}
1142
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001143static int intel_hda_post_load(void *opaque, int version)
1144{
1145 IntelHDAState* d = opaque;
1146 int i;
1147
Alistair Francisa89f3642017-11-08 14:56:31 -08001148 dprint(d, 1, "%s\n", __func__);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001149 for (i = 0; i < ARRAY_SIZE(d->st); i++) {
1150 if (d->st[i].ctl & 0x02) {
1151 intel_hda_parse_bdl(d, &d->st[i]);
1152 }
1153 }
1154 intel_hda_update_irq(d);
1155 return 0;
1156}
1157
1158static const VMStateDescription vmstate_intel_hda_stream = {
1159 .name = "intel-hda-stream",
1160 .version_id = 1,
Richard Henderson856a6fe2023-12-21 14:16:04 +11001161 .fields = (const VMStateField[]) {
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001162 VMSTATE_UINT32(ctl, IntelHDAStream),
1163 VMSTATE_UINT32(lpib, IntelHDAStream),
1164 VMSTATE_UINT32(cbl, IntelHDAStream),
1165 VMSTATE_UINT32(lvi, IntelHDAStream),
1166 VMSTATE_UINT32(fmt, IntelHDAStream),
1167 VMSTATE_UINT32(bdlp_lbase, IntelHDAStream),
1168 VMSTATE_UINT32(bdlp_ubase, IntelHDAStream),
1169 VMSTATE_END_OF_LIST()
1170 }
1171};
1172
1173static const VMStateDescription vmstate_intel_hda = {
1174 .name = "intel-hda",
1175 .version_id = 1,
1176 .post_load = intel_hda_post_load,
Richard Henderson856a6fe2023-12-21 14:16:04 +11001177 .fields = (const VMStateField[]) {
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001178 VMSTATE_PCI_DEVICE(pci, IntelHDAState),
1179
1180 /* registers */
1181 VMSTATE_UINT32(g_ctl, IntelHDAState),
1182 VMSTATE_UINT32(wake_en, IntelHDAState),
1183 VMSTATE_UINT32(state_sts, IntelHDAState),
1184 VMSTATE_UINT32(int_ctl, IntelHDAState),
1185 VMSTATE_UINT32(int_sts, IntelHDAState),
1186 VMSTATE_UINT32(wall_clk, IntelHDAState),
1187 VMSTATE_UINT32(corb_lbase, IntelHDAState),
1188 VMSTATE_UINT32(corb_ubase, IntelHDAState),
1189 VMSTATE_UINT32(corb_rp, IntelHDAState),
1190 VMSTATE_UINT32(corb_wp, IntelHDAState),
1191 VMSTATE_UINT32(corb_ctl, IntelHDAState),
1192 VMSTATE_UINT32(corb_sts, IntelHDAState),
1193 VMSTATE_UINT32(corb_size, IntelHDAState),
1194 VMSTATE_UINT32(rirb_lbase, IntelHDAState),
1195 VMSTATE_UINT32(rirb_ubase, IntelHDAState),
1196 VMSTATE_UINT32(rirb_wp, IntelHDAState),
1197 VMSTATE_UINT32(rirb_cnt, IntelHDAState),
1198 VMSTATE_UINT32(rirb_ctl, IntelHDAState),
1199 VMSTATE_UINT32(rirb_sts, IntelHDAState),
1200 VMSTATE_UINT32(rirb_size, IntelHDAState),
1201 VMSTATE_UINT32(dp_lbase, IntelHDAState),
1202 VMSTATE_UINT32(dp_ubase, IntelHDAState),
1203 VMSTATE_UINT32(icw, IntelHDAState),
1204 VMSTATE_UINT32(irr, IntelHDAState),
1205 VMSTATE_UINT32(ics, IntelHDAState),
1206 VMSTATE_STRUCT_ARRAY(st, IntelHDAState, 8, 0,
1207 vmstate_intel_hda_stream,
1208 IntelHDAStream),
1209
1210 /* additional state info */
1211 VMSTATE_UINT32(rirb_count, IntelHDAState),
1212 VMSTATE_INT64(wall_base_ns, IntelHDAState),
1213
1214 VMSTATE_END_OF_LIST()
1215 }
1216};
1217
Anthony Liguori40021f02011-12-04 12:22:06 -06001218static Property intel_hda_properties[] = {
1219 DEFINE_PROP_UINT32("debug", IntelHDAState, debug, 0),
Cao jinc0f2abf2016-06-20 14:13:35 +08001220 DEFINE_PROP_ON_OFF_AUTO("msi", IntelHDAState, msi, ON_OFF_AUTO_AUTO),
Jan Kiszkad209c742014-07-30 09:02:01 +02001221 DEFINE_PROP_BOOL("old_msi_addr", IntelHDAState, old_msi_addr, false),
Anthony Liguori40021f02011-12-04 12:22:06 -06001222 DEFINE_PROP_END_OF_LIST(),
1223};
1224
Peter Crosthwaite062db742013-06-06 15:34:08 +10001225static void intel_hda_class_init(ObjectClass *klass, void *data)
Anthony Liguori40021f02011-12-04 12:22:06 -06001226{
Anthony Liguori39bffca2011-12-07 21:34:16 -06001227 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -06001228 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1229
Markus Armbruster9af21db2015-01-19 15:52:30 +01001230 k->realize = intel_hda_realize;
Anthony Liguori40021f02011-12-04 12:22:06 -06001231 k->exit = intel_hda_exit;
Anthony Liguori40021f02011-12-04 12:22:06 -06001232 k->vendor_id = PCI_VENDOR_ID_INTEL;
Anthony Liguori40021f02011-12-04 12:22:06 -06001233 k->class_id = PCI_CLASS_MULTIMEDIA_HD_AUDIO;
Peter Maydelle3d08142024-09-13 15:31:44 +01001234 device_class_set_legacy_reset(dc, intel_hda_reset);
Anthony Liguori39bffca2011-12-07 21:34:16 -06001235 dc->vmsd = &vmstate_intel_hda;
Marc-André Lureau4f67d302020-01-10 19:30:32 +04001236 device_class_set_props(dc, intel_hda_properties);
Anthony Liguori40021f02011-12-04 12:22:06 -06001237}
1238
Gerd Hoffmann8b07eaa2013-01-08 09:25:16 +01001239static void intel_hda_class_init_ich6(ObjectClass *klass, void *data)
1240{
1241 DeviceClass *dc = DEVICE_CLASS(klass);
1242 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1243
Gerd Hoffmann8b07eaa2013-01-08 09:25:16 +01001244 k->device_id = 0x2668;
1245 k->revision = 1;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +03001246 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
Gerd Hoffmann8b07eaa2013-01-08 09:25:16 +01001247 dc->desc = "Intel HD Audio Controller (ich6)";
1248}
1249
1250static void intel_hda_class_init_ich9(ObjectClass *klass, void *data)
1251{
1252 DeviceClass *dc = DEVICE_CLASS(klass);
1253 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1254
Gerd Hoffmann8b07eaa2013-01-08 09:25:16 +01001255 k->device_id = 0x293e;
1256 k->revision = 3;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +03001257 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
Gerd Hoffmann8b07eaa2013-01-08 09:25:16 +01001258 dc->desc = "Intel HD Audio Controller (ich9)";
1259}
1260
Peter Crosthwaite062db742013-06-06 15:34:08 +10001261static const TypeInfo intel_hda_info = {
1262 .name = TYPE_INTEL_HDA_GENERIC,
Anthony Liguori39bffca2011-12-07 21:34:16 -06001263 .parent = TYPE_PCI_DEVICE,
1264 .instance_size = sizeof(IntelHDAState),
Peter Crosthwaite062db742013-06-06 15:34:08 +10001265 .class_init = intel_hda_class_init,
1266 .abstract = true,
Eduardo Habkostfd3b02c2017-09-27 16:56:34 -03001267 .interfaces = (InterfaceInfo[]) {
1268 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1269 { },
1270 },
Peter Crosthwaite062db742013-06-06 15:34:08 +10001271};
1272
1273static const TypeInfo intel_hda_info_ich6 = {
1274 .name = "intel-hda",
1275 .parent = TYPE_INTEL_HDA_GENERIC,
Gerd Hoffmann8b07eaa2013-01-08 09:25:16 +01001276 .class_init = intel_hda_class_init_ich6,
1277};
1278
Anthony Liguorie2848a72013-01-11 08:36:52 -06001279static const TypeInfo intel_hda_info_ich9 = {
Gerd Hoffmann8b07eaa2013-01-08 09:25:16 +01001280 .name = "ich9-intel-hda",
Peter Crosthwaite062db742013-06-06 15:34:08 +10001281 .parent = TYPE_INTEL_HDA_GENERIC,
Gerd Hoffmann8b07eaa2013-01-08 09:25:16 +01001282 .class_init = intel_hda_class_init_ich9,
Anthony Liguori40021f02011-12-04 12:22:06 -06001283};
1284
Anthony Liguori39bffca2011-12-07 21:34:16 -06001285static void hda_codec_device_class_init(ObjectClass *klass, void *data)
1286{
1287 DeviceClass *k = DEVICE_CLASS(klass);
xiaoqiang zhaobda8d9b2016-05-13 11:46:59 +08001288 k->realize = hda_codec_dev_realize;
Zihan Yang8ac55352017-04-26 20:53:07 +08001289 k->unrealize = hda_codec_dev_unrealize;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +03001290 set_bit(DEVICE_CATEGORY_SOUND, k->categories);
Anthony Liguori0d936922012-05-02 09:00:20 +02001291 k->bus_type = TYPE_HDA_BUS;
Marc-André Lureau4f67d302020-01-10 19:30:32 +04001292 device_class_set_props(k, hda_props);
Anthony Liguori39bffca2011-12-07 21:34:16 -06001293}
1294
Andreas Färber8c43a6f2013-01-10 16:19:07 +01001295static const TypeInfo hda_codec_device_type_info = {
Anthony Liguori40021f02011-12-04 12:22:06 -06001296 .name = TYPE_HDA_CODEC_DEVICE,
1297 .parent = TYPE_DEVICE,
1298 .instance_size = sizeof(HDACodecDevice),
1299 .abstract = true,
1300 .class_size = sizeof(HDACodecDeviceClass),
Anthony Liguori39bffca2011-12-07 21:34:16 -06001301 .class_init = hda_codec_device_class_init,
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001302};
1303
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001304/*
1305 * create intel hda controller with codec attached to it,
1306 * so '-soundhw hda' works.
1307 */
Paolo Bonzini039a6832022-04-27 12:27:46 +02001308static int intel_hda_and_codec_init(PCIBus *bus, const char *audiodev)
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001309{
Peter Crosthwaite52bb7c62013-06-06 15:34:52 +10001310 DeviceState *controller;
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001311 BusState *hdabus;
1312 DeviceState *codec;
1313
Peter Crosthwaite52bb7c62013-06-06 15:34:52 +10001314 controller = DEVICE(pci_create_simple(bus, -1, "intel-hda"));
1315 hdabus = QLIST_FIRST(&controller->child_bus);
Markus Armbruster3e80f692020-06-10 07:31:58 +02001316 codec = qdev_new("hda-duplex");
Paolo Bonzini039a6832022-04-27 12:27:46 +02001317 qdev_prop_set_string(codec, "audiodev", audiodev);
Markus Armbruster3e80f692020-06-10 07:31:58 +02001318 qdev_realize_and_unref(codec, hdabus, &error_fatal);
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +01001319 return 0;
1320}
1321
Paolo Bonzini36cd6f62013-04-18 18:43:58 +02001322static void intel_hda_register_types(void)
1323{
1324 type_register_static(&hda_codec_bus_info);
Peter Crosthwaite062db742013-06-06 15:34:08 +10001325 type_register_static(&intel_hda_info);
Paolo Bonzini36cd6f62013-04-18 18:43:58 +02001326 type_register_static(&intel_hda_info_ich6);
1327 type_register_static(&intel_hda_info_ich9);
1328 type_register_static(&hda_codec_device_type_info);
1329 pci_register_soundhw("hda", "Intel HD Audio", intel_hda_and_codec_init);
1330}
1331
1332type_init(intel_hda_register_types)