2 * Copyright (C) 2010 Red Hat, Inc.
4 * written by Gerd Hoffmann <kraxel@redhat.com>
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.
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.
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/>.
20 #include "qemu/osdep.h"
21 #include "hw/pci/pci.h"
22 #include "hw/pci/msi.h"
23 #include "qemu/timer.h"
24 #include "qemu/bitops.h"
26 #include "qemu/module.h"
27 #include "hw/audio/soundhw.h"
28 #include "intel-hda.h"
29 #include "migration/vmstate.h"
30 #include "intel-hda-defs.h"
31 #include "sysemu/dma.h"
32 #include "qapi/error.h"
34 /* --------------------------------------------------------------------- */
37 static Property hda_props
[] = {
38 DEFINE_PROP_UINT32("cad", HDACodecDevice
, cad
, -1),
39 DEFINE_PROP_END_OF_LIST()
42 static const TypeInfo hda_codec_bus_info
= {
45 .instance_size
= sizeof(HDACodecBus
),
48 void hda_codec_bus_init(DeviceState
*dev
, HDACodecBus
*bus
, size_t bus_size
,
49 hda_codec_response_func response
,
50 hda_codec_xfer_func xfer
)
52 qbus_create_inplace(bus
, bus_size
, TYPE_HDA_BUS
, dev
, NULL
);
53 bus
->response
= response
;
57 static void hda_codec_dev_realize(DeviceState
*qdev
, Error
**errp
)
59 HDACodecBus
*bus
= HDA_BUS(qdev
->parent_bus
);
60 HDACodecDevice
*dev
= HDA_CODEC_DEVICE(qdev
);
61 HDACodecDeviceClass
*cdc
= HDA_CODEC_DEVICE_GET_CLASS(dev
);
64 dev
->cad
= bus
->next_cad
;
67 error_setg(errp
, "HDA audio codec address is full");
70 bus
->next_cad
= dev
->cad
+ 1;
71 if (cdc
->init(dev
) != 0) {
72 error_setg(errp
, "HDA audio init failed");
76 static void hda_codec_dev_unrealize(DeviceState
*qdev
, Error
**errp
)
78 HDACodecDevice
*dev
= HDA_CODEC_DEVICE(qdev
);
79 HDACodecDeviceClass
*cdc
= HDA_CODEC_DEVICE_GET_CLASS(dev
);
86 HDACodecDevice
*hda_codec_find(HDACodecBus
*bus
, uint32_t cad
)
91 QTAILQ_FOREACH(kid
, &bus
->qbus
.children
, sibling
) {
92 DeviceState
*qdev
= kid
->child
;
93 cdev
= HDA_CODEC_DEVICE(qdev
);
94 if (cdev
->cad
== cad
) {
101 void hda_codec_response(HDACodecDevice
*dev
, bool solicited
, uint32_t response
)
103 HDACodecBus
*bus
= HDA_BUS(dev
->qdev
.parent_bus
);
104 bus
->response(dev
, solicited
, response
);
107 bool hda_codec_xfer(HDACodecDevice
*dev
, uint32_t stnr
, bool output
,
108 uint8_t *buf
, uint32_t len
)
110 HDACodecBus
*bus
= HDA_BUS(dev
->qdev
.parent_bus
);
111 return bus
->xfer(dev
, stnr
, output
, buf
, len
);
114 /* --------------------------------------------------------------------- */
115 /* intel hda emulation */
117 typedef struct IntelHDAStream IntelHDAStream
;
118 typedef struct IntelHDAState IntelHDAState
;
119 typedef struct IntelHDAReg IntelHDAReg
;
127 struct IntelHDAStream
{
140 uint32_t bsize
, be
, bp
;
143 struct IntelHDAState
{
180 IntelHDAStream st
[8];
185 int64_t wall_base_ns
;
188 const IntelHDAReg
*last_reg
;
192 uint32_t repeat_count
;
200 #define TYPE_INTEL_HDA_GENERIC "intel-hda-generic"
202 #define INTEL_HDA(obj) \
203 OBJECT_CHECK(IntelHDAState, (obj), TYPE_INTEL_HDA_GENERIC)
206 const char *name
; /* register name */
207 uint32_t size
; /* size in bytes */
208 uint32_t reset
; /* reset value */
209 uint32_t wmask
; /* write mask */
210 uint32_t wclear
; /* write 1 to clear bits */
211 uint32_t offset
; /* location in IntelHDAState */
212 uint32_t shift
; /* byte access entries for dwords */
214 void (*whandler
)(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
);
215 void (*rhandler
)(IntelHDAState
*d
, const IntelHDAReg
*reg
);
218 static void intel_hda_reset(DeviceState
*dev
);
220 /* --------------------------------------------------------------------- */
222 static hwaddr
intel_hda_addr(uint32_t lbase
, uint32_t ubase
)
224 return ((uint64_t)ubase
<< 32) | lbase
;
227 static void intel_hda_update_int_sts(IntelHDAState
*d
)
232 /* update controller status */
233 if (d
->rirb_sts
& ICH6_RBSTS_IRQ
) {
236 if (d
->rirb_sts
& ICH6_RBSTS_OVERRUN
) {
239 if (d
->state_sts
& d
->wake_en
) {
243 /* update stream status */
244 for (i
= 0; i
< 8; i
++) {
245 /* buffer completion interrupt */
246 if (d
->st
[i
].ctl
& (1 << 26)) {
251 /* update global status */
252 if (sts
& d
->int_ctl
) {
259 static void intel_hda_update_irq(IntelHDAState
*d
)
261 bool msi
= msi_enabled(&d
->pci
);
264 intel_hda_update_int_sts(d
);
265 if (d
->int_sts
& (1U << 31) && d
->int_ctl
& (1U << 31)) {
270 dprint(d
, 2, "%s: level %d [%s]\n", __func__
,
271 level
, msi
? "msi" : "intx");
274 msi_notify(&d
->pci
, 0);
277 pci_set_irq(&d
->pci
, level
);
281 static int intel_hda_send_command(IntelHDAState
*d
, uint32_t verb
)
283 uint32_t cad
, nid
, data
;
284 HDACodecDevice
*codec
;
285 HDACodecDeviceClass
*cdc
;
287 cad
= (verb
>> 28) & 0x0f;
288 if (verb
& (1 << 27)) {
289 /* indirect node addressing, not specified in HDA 1.0 */
290 dprint(d
, 1, "%s: indirect node addressing (guest bug?)\n", __func__
);
293 nid
= (verb
>> 20) & 0x7f;
294 data
= verb
& 0xfffff;
296 codec
= hda_codec_find(&d
->codecs
, cad
);
298 dprint(d
, 1, "%s: addressed non-existing codec\n", __func__
);
301 cdc
= HDA_CODEC_DEVICE_GET_CLASS(codec
);
302 cdc
->command(codec
, nid
, data
);
306 static void intel_hda_corb_run(IntelHDAState
*d
)
311 if (d
->ics
& ICH6_IRS_BUSY
) {
312 dprint(d
, 2, "%s: [icw] verb 0x%08x\n", __func__
, d
->icw
);
313 intel_hda_send_command(d
, d
->icw
);
318 if (!(d
->corb_ctl
& ICH6_CORBCTL_RUN
)) {
319 dprint(d
, 2, "%s: !run\n", __func__
);
322 if ((d
->corb_rp
& 0xff) == d
->corb_wp
) {
323 dprint(d
, 2, "%s: corb ring empty\n", __func__
);
326 if (d
->rirb_count
== d
->rirb_cnt
) {
327 dprint(d
, 2, "%s: rirb count reached\n", __func__
);
331 rp
= (d
->corb_rp
+ 1) & 0xff;
332 addr
= intel_hda_addr(d
->corb_lbase
, d
->corb_ubase
);
333 verb
= ldl_le_pci_dma(&d
->pci
, addr
+ 4*rp
);
336 dprint(d
, 2, "%s: [rp 0x%x] verb 0x%08x\n", __func__
, rp
, verb
);
337 intel_hda_send_command(d
, verb
);
341 static void intel_hda_response(HDACodecDevice
*dev
, bool solicited
, uint32_t response
)
343 HDACodecBus
*bus
= HDA_BUS(dev
->qdev
.parent_bus
);
344 IntelHDAState
*d
= container_of(bus
, IntelHDAState
, codecs
);
348 if (d
->ics
& ICH6_IRS_BUSY
) {
349 dprint(d
, 2, "%s: [irr] response 0x%x, cad 0x%x\n",
350 __func__
, response
, dev
->cad
);
352 d
->ics
&= ~(ICH6_IRS_BUSY
| 0xf0);
353 d
->ics
|= (ICH6_IRS_VALID
| (dev
->cad
<< 4));
357 if (!(d
->rirb_ctl
& ICH6_RBCTL_DMA_EN
)) {
358 dprint(d
, 1, "%s: rirb dma disabled, drop codec response\n", __func__
);
362 ex
= (solicited
? 0 : (1 << 4)) | dev
->cad
;
363 wp
= (d
->rirb_wp
+ 1) & 0xff;
364 addr
= intel_hda_addr(d
->rirb_lbase
, d
->rirb_ubase
);
365 stl_le_pci_dma(&d
->pci
, addr
+ 8*wp
, response
);
366 stl_le_pci_dma(&d
->pci
, addr
+ 8*wp
+ 4, ex
);
369 dprint(d
, 2, "%s: [wp 0x%x] response 0x%x, extra 0x%x\n",
370 __func__
, wp
, response
, ex
);
373 if (d
->rirb_count
== d
->rirb_cnt
) {
374 dprint(d
, 2, "%s: rirb count reached (%d)\n", __func__
, d
->rirb_count
);
375 if (d
->rirb_ctl
& ICH6_RBCTL_IRQ_EN
) {
376 d
->rirb_sts
|= ICH6_RBSTS_IRQ
;
377 intel_hda_update_irq(d
);
379 } else if ((d
->corb_rp
& 0xff) == d
->corb_wp
) {
380 dprint(d
, 2, "%s: corb ring empty (%d/%d)\n", __func__
,
381 d
->rirb_count
, d
->rirb_cnt
);
382 if (d
->rirb_ctl
& ICH6_RBCTL_IRQ_EN
) {
383 d
->rirb_sts
|= ICH6_RBSTS_IRQ
;
384 intel_hda_update_irq(d
);
389 static bool intel_hda_xfer(HDACodecDevice
*dev
, uint32_t stnr
, bool output
,
390 uint8_t *buf
, uint32_t len
)
392 HDACodecBus
*bus
= HDA_BUS(dev
->qdev
.parent_bus
);
393 IntelHDAState
*d
= container_of(bus
, IntelHDAState
, codecs
);
395 uint32_t s
, copy
, left
;
399 st
= output
? d
->st
+ 4 : d
->st
;
400 for (s
= 0; s
< 4; s
++) {
401 if (stnr
== ((st
[s
].ctl
>> 20) & 0x0f)) {
409 if (st
->bpl
== NULL
) {
415 while (left
> 0 && s
-- > 0) {
417 if (copy
> st
->bsize
- st
->lpib
)
418 copy
= st
->bsize
- st
->lpib
;
419 if (copy
> st
->bpl
[st
->be
].len
- st
->bp
)
420 copy
= st
->bpl
[st
->be
].len
- st
->bp
;
422 dprint(d
, 3, "dma: entry %d, pos %d/%d, copy %d\n",
423 st
->be
, st
->bp
, st
->bpl
[st
->be
].len
, copy
);
425 pci_dma_rw(&d
->pci
, st
->bpl
[st
->be
].addr
+ st
->bp
, buf
, copy
, !output
);
431 if (st
->bpl
[st
->be
].len
== st
->bp
) {
432 /* bpl entry filled */
433 if (st
->bpl
[st
->be
].flags
& 0x01) {
438 if (st
->be
== st
->bentries
) {
439 /* bpl wrap around */
445 if (d
->dp_lbase
& 0x01) {
447 addr
= intel_hda_addr(d
->dp_lbase
& ~0x01, d
->dp_ubase
);
448 stl_le_pci_dma(&d
->pci
, addr
+ 8*s
, st
->lpib
);
450 dprint(d
, 3, "dma: --\n");
453 st
->ctl
|= (1 << 26); /* buffer completion interrupt */
454 intel_hda_update_irq(d
);
459 static void intel_hda_parse_bdl(IntelHDAState
*d
, IntelHDAStream
*st
)
465 addr
= intel_hda_addr(st
->bdlp_lbase
, st
->bdlp_ubase
);
466 st
->bentries
= st
->lvi
+1;
468 st
->bpl
= g_malloc(sizeof(bpl
) * st
->bentries
);
469 for (i
= 0; i
< st
->bentries
; i
++, addr
+= 16) {
470 pci_dma_read(&d
->pci
, addr
, buf
, 16);
471 st
->bpl
[i
].addr
= le64_to_cpu(*(uint64_t *)buf
);
472 st
->bpl
[i
].len
= le32_to_cpu(*(uint32_t *)(buf
+ 8));
473 st
->bpl
[i
].flags
= le32_to_cpu(*(uint32_t *)(buf
+ 12));
474 dprint(d
, 1, "bdl/%d: 0x%" PRIx64
" +0x%x, 0x%x\n",
475 i
, st
->bpl
[i
].addr
, st
->bpl
[i
].len
, st
->bpl
[i
].flags
);
484 static void intel_hda_notify_codecs(IntelHDAState
*d
, uint32_t stream
, bool running
, bool output
)
487 HDACodecDevice
*cdev
;
489 QTAILQ_FOREACH(kid
, &d
->codecs
.qbus
.children
, sibling
) {
490 DeviceState
*qdev
= kid
->child
;
491 HDACodecDeviceClass
*cdc
;
493 cdev
= HDA_CODEC_DEVICE(qdev
);
494 cdc
= HDA_CODEC_DEVICE_GET_CLASS(cdev
);
496 cdc
->stream(cdev
, stream
, running
, output
);
501 /* --------------------------------------------------------------------- */
503 static void intel_hda_set_g_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
505 if ((d
->g_ctl
& ICH6_GCTL_RESET
) == 0) {
506 intel_hda_reset(DEVICE(d
));
510 static void intel_hda_set_wake_en(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
512 intel_hda_update_irq(d
);
515 static void intel_hda_set_state_sts(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
517 intel_hda_update_irq(d
);
520 static void intel_hda_set_int_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
522 intel_hda_update_irq(d
);
525 static void intel_hda_get_wall_clk(IntelHDAState
*d
, const IntelHDAReg
*reg
)
529 ns
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - d
->wall_base_ns
;
530 d
->wall_clk
= (uint32_t)(ns
* 24 / 1000); /* 24 MHz */
533 static void intel_hda_set_corb_wp(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
535 intel_hda_corb_run(d
);
538 static void intel_hda_set_corb_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
540 intel_hda_corb_run(d
);
543 static void intel_hda_set_rirb_wp(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
545 if (d
->rirb_wp
& ICH6_RIRBWP_RST
) {
550 static void intel_hda_set_rirb_sts(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
552 intel_hda_update_irq(d
);
554 if ((old
& ICH6_RBSTS_IRQ
) && !(d
->rirb_sts
& ICH6_RBSTS_IRQ
)) {
555 /* cleared ICH6_RBSTS_IRQ */
557 intel_hda_corb_run(d
);
561 static void intel_hda_set_ics(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
563 if (d
->ics
& ICH6_IRS_BUSY
) {
564 intel_hda_corb_run(d
);
568 static void intel_hda_set_st_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
570 bool output
= reg
->stream
>= 4;
571 IntelHDAStream
*st
= d
->st
+ reg
->stream
;
573 if (st
->ctl
& 0x01) {
575 dprint(d
, 1, "st #%d: reset\n", reg
->stream
);
576 st
->ctl
= SD_STS_FIFO_READY
<< 24;
578 if ((st
->ctl
& 0x02) != (old
& 0x02)) {
579 uint32_t stnr
= (st
->ctl
>> 20) & 0x0f;
580 /* run bit flipped */
581 if (st
->ctl
& 0x02) {
583 dprint(d
, 1, "st #%d: start %d (ring buf %d bytes)\n",
584 reg
->stream
, stnr
, st
->cbl
);
585 intel_hda_parse_bdl(d
, st
);
586 intel_hda_notify_codecs(d
, stnr
, true, output
);
589 dprint(d
, 1, "st #%d: stop %d\n", reg
->stream
, stnr
);
590 intel_hda_notify_codecs(d
, stnr
, false, output
);
593 intel_hda_update_irq(d
);
596 /* --------------------------------------------------------------------- */
598 #define ST_REG(_n, _o) (0x80 + (_n) * 0x20 + (_o))
600 static const struct IntelHDAReg regtab
[] = {
602 [ ICH6_REG_GCAP
] = {
607 [ ICH6_REG_VMIN
] = {
611 [ ICH6_REG_VMAJ
] = {
616 [ ICH6_REG_OUTPAY
] = {
621 [ ICH6_REG_INPAY
] = {
626 [ ICH6_REG_GCTL
] = {
630 .offset
= offsetof(IntelHDAState
, g_ctl
),
631 .whandler
= intel_hda_set_g_ctl
,
633 [ ICH6_REG_WAKEEN
] = {
637 .offset
= offsetof(IntelHDAState
, wake_en
),
638 .whandler
= intel_hda_set_wake_en
,
640 [ ICH6_REG_STATESTS
] = {
645 .offset
= offsetof(IntelHDAState
, state_sts
),
646 .whandler
= intel_hda_set_state_sts
,
650 [ ICH6_REG_INTCTL
] = {
654 .offset
= offsetof(IntelHDAState
, int_ctl
),
655 .whandler
= intel_hda_set_int_ctl
,
657 [ ICH6_REG_INTSTS
] = {
661 .wclear
= 0xc00000ff,
662 .offset
= offsetof(IntelHDAState
, int_sts
),
666 [ ICH6_REG_WALLCLK
] = {
669 .offset
= offsetof(IntelHDAState
, wall_clk
),
670 .rhandler
= intel_hda_get_wall_clk
,
672 [ ICH6_REG_WALLCLK
+ 0x2000 ] = {
673 .name
= "WALLCLK(alias)",
675 .offset
= offsetof(IntelHDAState
, wall_clk
),
676 .rhandler
= intel_hda_get_wall_clk
,
680 [ ICH6_REG_CORBLBASE
] = {
684 .offset
= offsetof(IntelHDAState
, corb_lbase
),
686 [ ICH6_REG_CORBUBASE
] = {
690 .offset
= offsetof(IntelHDAState
, corb_ubase
),
692 [ ICH6_REG_CORBWP
] = {
696 .offset
= offsetof(IntelHDAState
, corb_wp
),
697 .whandler
= intel_hda_set_corb_wp
,
699 [ ICH6_REG_CORBRP
] = {
703 .offset
= offsetof(IntelHDAState
, corb_rp
),
705 [ ICH6_REG_CORBCTL
] = {
709 .offset
= offsetof(IntelHDAState
, corb_ctl
),
710 .whandler
= intel_hda_set_corb_ctl
,
712 [ ICH6_REG_CORBSTS
] = {
717 .offset
= offsetof(IntelHDAState
, corb_sts
),
719 [ ICH6_REG_CORBSIZE
] = {
723 .offset
= offsetof(IntelHDAState
, corb_size
),
725 [ ICH6_REG_RIRBLBASE
] = {
729 .offset
= offsetof(IntelHDAState
, rirb_lbase
),
731 [ ICH6_REG_RIRBUBASE
] = {
735 .offset
= offsetof(IntelHDAState
, rirb_ubase
),
737 [ ICH6_REG_RIRBWP
] = {
741 .offset
= offsetof(IntelHDAState
, rirb_wp
),
742 .whandler
= intel_hda_set_rirb_wp
,
744 [ ICH6_REG_RINTCNT
] = {
748 .offset
= offsetof(IntelHDAState
, rirb_cnt
),
750 [ ICH6_REG_RIRBCTL
] = {
754 .offset
= offsetof(IntelHDAState
, rirb_ctl
),
756 [ ICH6_REG_RIRBSTS
] = {
761 .offset
= offsetof(IntelHDAState
, rirb_sts
),
762 .whandler
= intel_hda_set_rirb_sts
,
764 [ ICH6_REG_RIRBSIZE
] = {
768 .offset
= offsetof(IntelHDAState
, rirb_size
),
771 [ ICH6_REG_DPLBASE
] = {
775 .offset
= offsetof(IntelHDAState
, dp_lbase
),
777 [ ICH6_REG_DPUBASE
] = {
781 .offset
= offsetof(IntelHDAState
, dp_ubase
),
788 .offset
= offsetof(IntelHDAState
, icw
),
793 .offset
= offsetof(IntelHDAState
, irr
),
800 .offset
= offsetof(IntelHDAState
, ics
),
801 .whandler
= intel_hda_set_ics
,
804 #define HDA_STREAM(_t, _i) \
805 [ ST_REG(_i, ICH6_REG_SD_CTL) ] = { \
807 .name = _t stringify(_i) " CTL", \
809 .wmask = 0x1cff001f, \
810 .offset = offsetof(IntelHDAState, st[_i].ctl), \
811 .whandler = intel_hda_set_st_ctl, \
813 [ ST_REG(_i, ICH6_REG_SD_CTL) + 2] = { \
815 .name = _t stringify(_i) " CTL(stnr)", \
818 .wmask = 0x00ff0000, \
819 .offset = offsetof(IntelHDAState, st[_i].ctl), \
820 .whandler = intel_hda_set_st_ctl, \
822 [ ST_REG(_i, ICH6_REG_SD_STS)] = { \
824 .name = _t stringify(_i) " CTL(sts)", \
827 .wmask = 0x1c000000, \
828 .wclear = 0x1c000000, \
829 .offset = offsetof(IntelHDAState, st[_i].ctl), \
830 .whandler = intel_hda_set_st_ctl, \
831 .reset = SD_STS_FIFO_READY << 24 \
833 [ ST_REG(_i, ICH6_REG_SD_LPIB) ] = { \
835 .name = _t stringify(_i) " LPIB", \
837 .offset = offsetof(IntelHDAState, st[_i].lpib), \
839 [ ST_REG(_i, ICH6_REG_SD_LPIB) + 0x2000 ] = { \
841 .name = _t stringify(_i) " LPIB(alias)", \
843 .offset = offsetof(IntelHDAState, st[_i].lpib), \
845 [ ST_REG(_i, ICH6_REG_SD_CBL) ] = { \
847 .name = _t stringify(_i) " CBL", \
849 .wmask = 0xffffffff, \
850 .offset = offsetof(IntelHDAState, st[_i].cbl), \
852 [ ST_REG(_i, ICH6_REG_SD_LVI) ] = { \
854 .name = _t stringify(_i) " LVI", \
857 .offset = offsetof(IntelHDAState, st[_i].lvi), \
859 [ ST_REG(_i, ICH6_REG_SD_FIFOSIZE) ] = { \
861 .name = _t stringify(_i) " FIFOS", \
863 .reset = HDA_BUFFER_SIZE, \
865 [ ST_REG(_i, ICH6_REG_SD_FORMAT) ] = { \
867 .name = _t stringify(_i) " FMT", \
870 .offset = offsetof(IntelHDAState, st[_i].fmt), \
872 [ ST_REG(_i, ICH6_REG_SD_BDLPL) ] = { \
874 .name = _t stringify(_i) " BDLPL", \
876 .wmask = 0xffffff80, \
877 .offset = offsetof(IntelHDAState, st[_i].bdlp_lbase), \
879 [ ST_REG(_i, ICH6_REG_SD_BDLPU) ] = { \
881 .name = _t stringify(_i) " BDLPU", \
883 .wmask = 0xffffffff, \
884 .offset = offsetof(IntelHDAState, st[_i].bdlp_ubase), \
899 static const IntelHDAReg
*intel_hda_reg_find(IntelHDAState
*d
, hwaddr addr
)
901 const IntelHDAReg
*reg
;
903 if (addr
>= ARRAY_SIZE(regtab
)) {
907 if (reg
->name
== NULL
) {
913 dprint(d
, 1, "unknown register, addr 0x%x\n", (int) addr
);
917 static uint32_t *intel_hda_reg_addr(IntelHDAState
*d
, const IntelHDAReg
*reg
)
919 uint8_t *addr
= (void*)d
;
922 return (uint32_t*)addr
;
925 static void intel_hda_reg_write(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t val
,
935 qemu_log_mask(LOG_GUEST_ERROR
, "intel-hda: write to r/o reg %s\n",
941 time_t now
= time(NULL
);
942 if (d
->last_write
&& d
->last_reg
== reg
&& d
->last_val
== val
) {
944 if (d
->last_sec
!= now
) {
945 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
950 if (d
->repeat_count
) {
951 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
953 dprint(d
, 2, "write %-16s: 0x%x (%x)\n", reg
->name
, val
, wmask
);
961 assert(reg
->offset
!= 0);
963 addr
= intel_hda_reg_addr(d
, reg
);
968 wmask
<<= reg
->shift
;
972 *addr
|= wmask
& val
;
973 *addr
&= ~(val
& reg
->wclear
);
976 reg
->whandler(d
, reg
, old
);
980 static uint32_t intel_hda_reg_read(IntelHDAState
*d
, const IntelHDAReg
*reg
,
990 reg
->rhandler(d
, reg
);
993 if (reg
->offset
== 0) {
994 /* constant read-only register */
997 addr
= intel_hda_reg_addr(d
, reg
);
1005 time_t now
= time(NULL
);
1006 if (!d
->last_write
&& d
->last_reg
== reg
&& d
->last_val
== ret
) {
1008 if (d
->last_sec
!= now
) {
1009 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
1011 d
->repeat_count
= 0;
1014 if (d
->repeat_count
) {
1015 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
1017 dprint(d
, 2, "read %-16s: 0x%x (%x)\n", reg
->name
, ret
, rmask
);
1022 d
->repeat_count
= 0;
1028 static void intel_hda_regs_reset(IntelHDAState
*d
)
1033 for (i
= 0; i
< ARRAY_SIZE(regtab
); i
++) {
1034 if (regtab
[i
].name
== NULL
) {
1037 if (regtab
[i
].offset
== 0) {
1040 addr
= intel_hda_reg_addr(d
, regtab
+ i
);
1041 *addr
= regtab
[i
].reset
;
1045 /* --------------------------------------------------------------------- */
1047 static void intel_hda_mmio_write(void *opaque
, hwaddr addr
, uint64_t val
,
1050 IntelHDAState
*d
= opaque
;
1051 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1053 intel_hda_reg_write(d
, reg
, val
, MAKE_64BIT_MASK(0, size
* 8));
1056 static uint64_t intel_hda_mmio_read(void *opaque
, hwaddr addr
, unsigned size
)
1058 IntelHDAState
*d
= opaque
;
1059 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1061 return intel_hda_reg_read(d
, reg
, MAKE_64BIT_MASK(0, size
* 8));
1064 static const MemoryRegionOps intel_hda_mmio_ops
= {
1065 .read
= intel_hda_mmio_read
,
1066 .write
= intel_hda_mmio_write
,
1068 .min_access_size
= 1,
1069 .max_access_size
= 4,
1071 .endianness
= DEVICE_NATIVE_ENDIAN
,
1074 /* --------------------------------------------------------------------- */
1076 static void intel_hda_reset(DeviceState
*dev
)
1079 IntelHDAState
*d
= INTEL_HDA(dev
);
1080 HDACodecDevice
*cdev
;
1082 intel_hda_regs_reset(d
);
1083 d
->wall_base_ns
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1086 QTAILQ_FOREACH(kid
, &d
->codecs
.qbus
.children
, sibling
) {
1087 DeviceState
*qdev
= kid
->child
;
1088 cdev
= HDA_CODEC_DEVICE(qdev
);
1089 device_reset(DEVICE(cdev
));
1090 d
->state_sts
|= (1 << cdev
->cad
);
1092 intel_hda_update_irq(d
);
1095 static void intel_hda_realize(PCIDevice
*pci
, Error
**errp
)
1097 IntelHDAState
*d
= INTEL_HDA(pci
);
1098 uint8_t *conf
= d
->pci
.config
;
1102 d
->name
= object_get_typename(OBJECT(d
));
1104 pci_config_set_interrupt_pin(conf
, 1);
1106 /* HDCTL off 0x40 bit 0 selects signaling mode (1-HDA, 0 - Ac97) 18.1.19 */
1109 if (d
->msi
!= ON_OFF_AUTO_OFF
) {
1110 ret
= msi_init(&d
->pci
, d
->old_msi_addr
? 0x50 : 0x60,
1111 1, true, false, &err
);
1112 /* Any error other than -ENOTSUP(board's MSI support is broken)
1113 * is a programming error */
1114 assert(!ret
|| ret
== -ENOTSUP
);
1115 if (ret
&& d
->msi
== ON_OFF_AUTO_ON
) {
1116 /* Can't satisfy user's explicit msi=on request, fail */
1117 error_append_hint(&err
, "You have to use msi=auto (default) or "
1118 "msi=off with this machine type.\n");
1119 error_propagate(errp
, err
);
1122 assert(!err
|| d
->msi
== ON_OFF_AUTO_AUTO
);
1123 /* With msi=auto, we fall back to MSI off silently */
1127 memory_region_init_io(&d
->mmio
, OBJECT(d
), &intel_hda_mmio_ops
, d
,
1128 "intel-hda", 0x4000);
1129 pci_register_bar(&d
->pci
, 0, 0, &d
->mmio
);
1131 hda_codec_bus_init(DEVICE(pci
), &d
->codecs
, sizeof(d
->codecs
),
1132 intel_hda_response
, intel_hda_xfer
);
1135 static void intel_hda_exit(PCIDevice
*pci
)
1137 IntelHDAState
*d
= INTEL_HDA(pci
);
1139 msi_uninit(&d
->pci
);
1142 static int intel_hda_post_load(void *opaque
, int version
)
1144 IntelHDAState
* d
= opaque
;
1147 dprint(d
, 1, "%s\n", __func__
);
1148 for (i
= 0; i
< ARRAY_SIZE(d
->st
); i
++) {
1149 if (d
->st
[i
].ctl
& 0x02) {
1150 intel_hda_parse_bdl(d
, &d
->st
[i
]);
1153 intel_hda_update_irq(d
);
1157 static const VMStateDescription vmstate_intel_hda_stream
= {
1158 .name
= "intel-hda-stream",
1160 .fields
= (VMStateField
[]) {
1161 VMSTATE_UINT32(ctl
, IntelHDAStream
),
1162 VMSTATE_UINT32(lpib
, IntelHDAStream
),
1163 VMSTATE_UINT32(cbl
, IntelHDAStream
),
1164 VMSTATE_UINT32(lvi
, IntelHDAStream
),
1165 VMSTATE_UINT32(fmt
, IntelHDAStream
),
1166 VMSTATE_UINT32(bdlp_lbase
, IntelHDAStream
),
1167 VMSTATE_UINT32(bdlp_ubase
, IntelHDAStream
),
1168 VMSTATE_END_OF_LIST()
1172 static const VMStateDescription vmstate_intel_hda
= {
1173 .name
= "intel-hda",
1175 .post_load
= intel_hda_post_load
,
1176 .fields
= (VMStateField
[]) {
1177 VMSTATE_PCI_DEVICE(pci
, IntelHDAState
),
1180 VMSTATE_UINT32(g_ctl
, IntelHDAState
),
1181 VMSTATE_UINT32(wake_en
, IntelHDAState
),
1182 VMSTATE_UINT32(state_sts
, IntelHDAState
),
1183 VMSTATE_UINT32(int_ctl
, IntelHDAState
),
1184 VMSTATE_UINT32(int_sts
, IntelHDAState
),
1185 VMSTATE_UINT32(wall_clk
, IntelHDAState
),
1186 VMSTATE_UINT32(corb_lbase
, IntelHDAState
),
1187 VMSTATE_UINT32(corb_ubase
, IntelHDAState
),
1188 VMSTATE_UINT32(corb_rp
, IntelHDAState
),
1189 VMSTATE_UINT32(corb_wp
, IntelHDAState
),
1190 VMSTATE_UINT32(corb_ctl
, IntelHDAState
),
1191 VMSTATE_UINT32(corb_sts
, IntelHDAState
),
1192 VMSTATE_UINT32(corb_size
, IntelHDAState
),
1193 VMSTATE_UINT32(rirb_lbase
, IntelHDAState
),
1194 VMSTATE_UINT32(rirb_ubase
, IntelHDAState
),
1195 VMSTATE_UINT32(rirb_wp
, IntelHDAState
),
1196 VMSTATE_UINT32(rirb_cnt
, IntelHDAState
),
1197 VMSTATE_UINT32(rirb_ctl
, IntelHDAState
),
1198 VMSTATE_UINT32(rirb_sts
, IntelHDAState
),
1199 VMSTATE_UINT32(rirb_size
, IntelHDAState
),
1200 VMSTATE_UINT32(dp_lbase
, IntelHDAState
),
1201 VMSTATE_UINT32(dp_ubase
, IntelHDAState
),
1202 VMSTATE_UINT32(icw
, IntelHDAState
),
1203 VMSTATE_UINT32(irr
, IntelHDAState
),
1204 VMSTATE_UINT32(ics
, IntelHDAState
),
1205 VMSTATE_STRUCT_ARRAY(st
, IntelHDAState
, 8, 0,
1206 vmstate_intel_hda_stream
,
1209 /* additional state info */
1210 VMSTATE_UINT32(rirb_count
, IntelHDAState
),
1211 VMSTATE_INT64(wall_base_ns
, IntelHDAState
),
1213 VMSTATE_END_OF_LIST()
1217 static Property intel_hda_properties
[] = {
1218 DEFINE_PROP_UINT32("debug", IntelHDAState
, debug
, 0),
1219 DEFINE_PROP_ON_OFF_AUTO("msi", IntelHDAState
, msi
, ON_OFF_AUTO_AUTO
),
1220 DEFINE_PROP_BOOL("old_msi_addr", IntelHDAState
, old_msi_addr
, false),
1221 DEFINE_PROP_END_OF_LIST(),
1224 static void intel_hda_class_init(ObjectClass
*klass
, void *data
)
1226 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1227 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1229 k
->realize
= intel_hda_realize
;
1230 k
->exit
= intel_hda_exit
;
1231 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
1232 k
->class_id
= PCI_CLASS_MULTIMEDIA_HD_AUDIO
;
1233 dc
->reset
= intel_hda_reset
;
1234 dc
->vmsd
= &vmstate_intel_hda
;
1235 dc
->props
= intel_hda_properties
;
1238 static void intel_hda_class_init_ich6(ObjectClass
*klass
, void *data
)
1240 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1241 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1243 k
->device_id
= 0x2668;
1245 set_bit(DEVICE_CATEGORY_SOUND
, dc
->categories
);
1246 dc
->desc
= "Intel HD Audio Controller (ich6)";
1249 static void intel_hda_class_init_ich9(ObjectClass
*klass
, void *data
)
1251 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1252 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1254 k
->device_id
= 0x293e;
1256 set_bit(DEVICE_CATEGORY_SOUND
, dc
->categories
);
1257 dc
->desc
= "Intel HD Audio Controller (ich9)";
1260 static const TypeInfo intel_hda_info
= {
1261 .name
= TYPE_INTEL_HDA_GENERIC
,
1262 .parent
= TYPE_PCI_DEVICE
,
1263 .instance_size
= sizeof(IntelHDAState
),
1264 .class_init
= intel_hda_class_init
,
1266 .interfaces
= (InterfaceInfo
[]) {
1267 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
1272 static const TypeInfo intel_hda_info_ich6
= {
1273 .name
= "intel-hda",
1274 .parent
= TYPE_INTEL_HDA_GENERIC
,
1275 .class_init
= intel_hda_class_init_ich6
,
1278 static const TypeInfo intel_hda_info_ich9
= {
1279 .name
= "ich9-intel-hda",
1280 .parent
= TYPE_INTEL_HDA_GENERIC
,
1281 .class_init
= intel_hda_class_init_ich9
,
1284 static void hda_codec_device_class_init(ObjectClass
*klass
, void *data
)
1286 DeviceClass
*k
= DEVICE_CLASS(klass
);
1287 k
->realize
= hda_codec_dev_realize
;
1288 k
->unrealize
= hda_codec_dev_unrealize
;
1289 set_bit(DEVICE_CATEGORY_SOUND
, k
->categories
);
1290 k
->bus_type
= TYPE_HDA_BUS
;
1291 k
->props
= hda_props
;
1294 static const TypeInfo hda_codec_device_type_info
= {
1295 .name
= TYPE_HDA_CODEC_DEVICE
,
1296 .parent
= TYPE_DEVICE
,
1297 .instance_size
= sizeof(HDACodecDevice
),
1299 .class_size
= sizeof(HDACodecDeviceClass
),
1300 .class_init
= hda_codec_device_class_init
,
1304 * create intel hda controller with codec attached to it,
1305 * so '-soundhw hda' works.
1307 static int intel_hda_and_codec_init(PCIBus
*bus
)
1309 DeviceState
*controller
;
1313 controller
= DEVICE(pci_create_simple(bus
, -1, "intel-hda"));
1314 hdabus
= QLIST_FIRST(&controller
->child_bus
);
1315 codec
= qdev_create(hdabus
, "hda-duplex");
1316 qdev_init_nofail(codec
);
1320 static void intel_hda_register_types(void)
1322 type_register_static(&hda_codec_bus_info
);
1323 type_register_static(&intel_hda_info
);
1324 type_register_static(&intel_hda_info_ich6
);
1325 type_register_static(&intel_hda_info_ich9
);
1326 type_register_static(&hda_codec_device_type_info
);
1327 pci_register_soundhw("hda", "Intel HD Audio", intel_hda_and_codec_init
);
1330 type_init(intel_hda_register_types
)