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/qdev-properties.h"
23 #include "hw/pci/msi.h"
24 #include "qemu/timer.h"
25 #include "qemu/bitops.h"
27 #include "qemu/module.h"
28 #include "qemu/error-report.h"
29 #include "hw/audio/soundhw.h"
30 #include "intel-hda.h"
31 #include "migration/vmstate.h"
32 #include "intel-hda-defs.h"
33 #include "sysemu/dma.h"
34 #include "qapi/error.h"
35 #include "qom/object.h"
37 /* --------------------------------------------------------------------- */
40 static Property hda_props
[] = {
41 DEFINE_PROP_UINT32("cad", HDACodecDevice
, cad
, -1),
42 DEFINE_PROP_END_OF_LIST()
45 static const TypeInfo hda_codec_bus_info
= {
48 .instance_size
= sizeof(HDACodecBus
),
51 void hda_codec_bus_init(DeviceState
*dev
, HDACodecBus
*bus
, size_t bus_size
,
52 hda_codec_response_func response
,
53 hda_codec_xfer_func xfer
)
55 qbus_init(bus
, bus_size
, TYPE_HDA_BUS
, dev
, NULL
);
56 bus
->response
= response
;
60 static void hda_codec_dev_realize(DeviceState
*qdev
, Error
**errp
)
62 HDACodecBus
*bus
= HDA_BUS(qdev
->parent_bus
);
63 HDACodecDevice
*dev
= HDA_CODEC_DEVICE(qdev
);
64 HDACodecDeviceClass
*cdc
= HDA_CODEC_DEVICE_GET_CLASS(dev
);
67 dev
->cad
= bus
->next_cad
;
70 error_setg(errp
, "HDA audio codec address is full");
73 bus
->next_cad
= dev
->cad
+ 1;
77 static void hda_codec_dev_unrealize(DeviceState
*qdev
)
79 HDACodecDevice
*dev
= HDA_CODEC_DEVICE(qdev
);
80 HDACodecDeviceClass
*cdc
= HDA_CODEC_DEVICE_GET_CLASS(dev
);
87 HDACodecDevice
*hda_codec_find(HDACodecBus
*bus
, uint32_t cad
)
92 QTAILQ_FOREACH(kid
, &bus
->qbus
.children
, sibling
) {
93 DeviceState
*qdev
= kid
->child
;
94 cdev
= HDA_CODEC_DEVICE(qdev
);
95 if (cdev
->cad
== cad
) {
102 void hda_codec_response(HDACodecDevice
*dev
, bool solicited
, uint32_t response
)
104 HDACodecBus
*bus
= HDA_BUS(dev
->qdev
.parent_bus
);
105 bus
->response(dev
, solicited
, response
);
108 bool hda_codec_xfer(HDACodecDevice
*dev
, uint32_t stnr
, bool output
,
109 uint8_t *buf
, uint32_t len
)
111 HDACodecBus
*bus
= HDA_BUS(dev
->qdev
.parent_bus
);
112 return bus
->xfer(dev
, stnr
, output
, buf
, len
);
115 /* --------------------------------------------------------------------- */
116 /* intel hda emulation */
118 typedef struct IntelHDAStream IntelHDAStream
;
119 typedef struct IntelHDAState IntelHDAState
;
120 typedef struct IntelHDAReg IntelHDAReg
;
128 struct IntelHDAStream
{
141 uint32_t bsize
, be
, bp
;
144 struct IntelHDAState
{
181 IntelHDAStream st
[8];
184 MemoryRegion container
;
188 int64_t wall_base_ns
;
191 const IntelHDAReg
*last_reg
;
195 uint32_t repeat_count
;
203 #define TYPE_INTEL_HDA_GENERIC "intel-hda-generic"
205 DECLARE_INSTANCE_CHECKER(IntelHDAState
, INTEL_HDA
,
206 TYPE_INTEL_HDA_GENERIC
)
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 */
217 void (*whandler
)(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
);
218 void (*rhandler
)(IntelHDAState
*d
, const IntelHDAReg
*reg
);
221 /* --------------------------------------------------------------------- */
223 static hwaddr
intel_hda_addr(uint32_t lbase
, uint32_t ubase
)
225 return ((uint64_t)ubase
<< 32) | lbase
;
228 static void intel_hda_update_int_sts(IntelHDAState
*d
)
233 /* update controller status */
234 if (d
->rirb_sts
& ICH6_RBSTS_IRQ
) {
237 if (d
->rirb_sts
& ICH6_RBSTS_OVERRUN
) {
240 if (d
->state_sts
& d
->wake_en
) {
244 /* update stream status */
245 for (i
= 0; i
< 8; i
++) {
246 /* buffer completion interrupt */
247 if (d
->st
[i
].ctl
& (1 << 26)) {
252 /* update global status */
253 if (sts
& d
->int_ctl
) {
260 static void intel_hda_update_irq(IntelHDAState
*d
)
262 bool msi
= msi_enabled(&d
->pci
);
265 intel_hda_update_int_sts(d
);
266 if (d
->int_sts
& (1U << 31) && d
->int_ctl
& (1U << 31)) {
271 dprint(d
, 2, "%s: level %d [%s]\n", __func__
,
272 level
, msi
? "msi" : "intx");
275 msi_notify(&d
->pci
, 0);
278 pci_set_irq(&d
->pci
, level
);
282 static int intel_hda_send_command(IntelHDAState
*d
, uint32_t verb
)
284 uint32_t cad
, nid
, data
;
285 HDACodecDevice
*codec
;
286 HDACodecDeviceClass
*cdc
;
288 cad
= (verb
>> 28) & 0x0f;
289 if (verb
& (1 << 27)) {
290 /* indirect node addressing, not specified in HDA 1.0 */
291 dprint(d
, 1, "%s: indirect node addressing (guest bug?)\n", __func__
);
294 nid
= (verb
>> 20) & 0x7f;
295 data
= verb
& 0xfffff;
297 codec
= hda_codec_find(&d
->codecs
, cad
);
299 dprint(d
, 1, "%s: addressed non-existing codec\n", __func__
);
302 cdc
= HDA_CODEC_DEVICE_GET_CLASS(codec
);
303 cdc
->command(codec
, nid
, data
);
307 static void intel_hda_corb_run(IntelHDAState
*d
)
312 if (d
->ics
& ICH6_IRS_BUSY
) {
313 dprint(d
, 2, "%s: [icw] verb 0x%08x\n", __func__
, d
->icw
);
314 intel_hda_send_command(d
, d
->icw
);
319 if (!(d
->corb_ctl
& ICH6_CORBCTL_RUN
)) {
320 dprint(d
, 2, "%s: !run\n", __func__
);
323 if ((d
->corb_rp
& 0xff) == d
->corb_wp
) {
324 dprint(d
, 2, "%s: corb ring empty\n", __func__
);
327 if (d
->rirb_count
== d
->rirb_cnt
) {
328 dprint(d
, 2, "%s: rirb count reached\n", __func__
);
332 rp
= (d
->corb_rp
+ 1) & 0xff;
333 addr
= intel_hda_addr(d
->corb_lbase
, d
->corb_ubase
);
334 ldl_le_pci_dma(&d
->pci
, addr
+ 4 * rp
, &verb
, MEMTXATTRS_UNSPECIFIED
);
337 dprint(d
, 2, "%s: [rp 0x%x] verb 0x%08x\n", __func__
, rp
, verb
);
338 intel_hda_send_command(d
, verb
);
342 static void intel_hda_response(HDACodecDevice
*dev
, bool solicited
, uint32_t response
)
344 const MemTxAttrs attrs
= { .memory
= true };
345 HDACodecBus
*bus
= HDA_BUS(dev
->qdev
.parent_bus
);
346 IntelHDAState
*d
= container_of(bus
, IntelHDAState
, codecs
);
349 MemTxResult res
= MEMTX_OK
;
351 if (d
->ics
& ICH6_IRS_BUSY
) {
352 dprint(d
, 2, "%s: [irr] response 0x%x, cad 0x%x\n",
353 __func__
, response
, dev
->cad
);
355 d
->ics
&= ~(ICH6_IRS_BUSY
| 0xf0);
356 d
->ics
|= (ICH6_IRS_VALID
| (dev
->cad
<< 4));
360 if (!(d
->rirb_ctl
& ICH6_RBCTL_DMA_EN
)) {
361 dprint(d
, 1, "%s: rirb dma disabled, drop codec response\n", __func__
);
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
);
368 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
);
376 dprint(d
, 2, "%s: [wp 0x%x] response 0x%x, extra 0x%x\n",
377 __func__
, wp
, response
, ex
);
380 if (d
->rirb_count
== d
->rirb_cnt
) {
381 dprint(d
, 2, "%s: rirb count reached (%d)\n", __func__
, d
->rirb_count
);
382 if (d
->rirb_ctl
& ICH6_RBCTL_IRQ_EN
) {
383 d
->rirb_sts
|= ICH6_RBSTS_IRQ
;
384 intel_hda_update_irq(d
);
386 } else if ((d
->corb_rp
& 0xff) == d
->corb_wp
) {
387 dprint(d
, 2, "%s: corb ring empty (%d/%d)\n", __func__
,
388 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
);
396 static bool intel_hda_xfer(HDACodecDevice
*dev
, uint32_t stnr
, bool output
,
397 uint8_t *buf
, uint32_t len
)
399 const MemTxAttrs attrs
= MEMTXATTRS_UNSPECIFIED
;
400 HDACodecBus
*bus
= HDA_BUS(dev
->qdev
.parent_bus
);
401 IntelHDAState
*d
= container_of(bus
, IntelHDAState
, codecs
);
403 uint32_t s
, copy
, left
;
407 st
= output
? d
->st
+ 4 : d
->st
;
408 for (s
= 0; s
< 4; s
++) {
409 if (stnr
== ((st
[s
].ctl
>> 20) & 0x0f)) {
417 if (st
->bpl
== NULL
) {
423 while (left
> 0 && s
-- > 0) {
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
;
430 dprint(d
, 3, "dma: entry %d, pos %d/%d, copy %d\n",
431 st
->be
, st
->bp
, st
->bpl
[st
->be
].len
, copy
);
433 pci_dma_rw(&d
->pci
, st
->bpl
[st
->be
].addr
+ st
->bp
, buf
, copy
, !output
,
440 if (st
->bpl
[st
->be
].len
== st
->bp
) {
441 /* bpl entry filled */
442 if (st
->bpl
[st
->be
].flags
& 0x01) {
447 if (st
->be
== st
->bentries
) {
448 /* bpl wrap around */
454 if (d
->dp_lbase
& 0x01) {
456 addr
= intel_hda_addr(d
->dp_lbase
& ~0x01, d
->dp_ubase
);
457 stl_le_pci_dma(&d
->pci
, addr
+ 8 * s
, st
->lpib
, attrs
);
459 dprint(d
, 3, "dma: --\n");
462 st
->ctl
|= (1 << 26); /* buffer completion interrupt */
463 intel_hda_update_irq(d
);
468 static void intel_hda_parse_bdl(IntelHDAState
*d
, IntelHDAStream
*st
)
474 addr
= intel_hda_addr(st
->bdlp_lbase
, st
->bdlp_ubase
);
475 st
->bentries
= st
->lvi
+1;
477 st
->bpl
= g_new(bpl
, st
->bentries
);
478 for (i
= 0; i
< st
->bentries
; i
++, addr
+= 16) {
479 pci_dma_read(&d
->pci
, addr
, buf
, 16);
480 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
);
493 static void intel_hda_notify_codecs(IntelHDAState
*d
, uint32_t stream
, bool running
, bool output
)
496 HDACodecDevice
*cdev
;
498 QTAILQ_FOREACH(kid
, &d
->codecs
.qbus
.children
, sibling
) {
499 DeviceState
*qdev
= kid
->child
;
500 HDACodecDeviceClass
*cdc
;
502 cdev
= HDA_CODEC_DEVICE(qdev
);
503 cdc
= HDA_CODEC_DEVICE_GET_CLASS(cdev
);
505 cdc
->stream(cdev
, stream
, running
, output
);
510 /* --------------------------------------------------------------------- */
512 static void intel_hda_set_g_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
514 if ((d
->g_ctl
& ICH6_GCTL_RESET
) == 0) {
515 device_cold_reset(DEVICE(d
));
519 static void intel_hda_set_wake_en(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
521 intel_hda_update_irq(d
);
524 static void intel_hda_set_state_sts(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
526 intel_hda_update_irq(d
);
529 static void intel_hda_set_int_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
531 intel_hda_update_irq(d
);
534 static void intel_hda_get_wall_clk(IntelHDAState
*d
, const IntelHDAReg
*reg
)
538 ns
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - d
->wall_base_ns
;
539 d
->wall_clk
= (uint32_t)(ns
* 24 / 1000); /* 24 MHz */
542 static void intel_hda_set_corb_wp(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
544 intel_hda_corb_run(d
);
547 static void intel_hda_set_corb_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
549 intel_hda_corb_run(d
);
552 static void intel_hda_set_rirb_wp(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
554 if (d
->rirb_wp
& ICH6_RIRBWP_RST
) {
559 static void intel_hda_set_rirb_sts(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
561 intel_hda_update_irq(d
);
563 if ((old
& ICH6_RBSTS_IRQ
) && !(d
->rirb_sts
& ICH6_RBSTS_IRQ
)) {
564 /* cleared ICH6_RBSTS_IRQ */
566 intel_hda_corb_run(d
);
570 static void intel_hda_set_ics(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
572 if (d
->ics
& ICH6_IRS_BUSY
) {
573 intel_hda_corb_run(d
);
577 static void intel_hda_set_st_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
579 bool output
= reg
->stream
>= 4;
580 IntelHDAStream
*st
= d
->st
+ reg
->stream
;
582 if (st
->ctl
& 0x01) {
584 dprint(d
, 1, "st #%d: reset\n", reg
->stream
);
585 st
->ctl
= SD_STS_FIFO_READY
<< 24 | SD_CTL_STREAM_RESET
;
587 if ((st
->ctl
& 0x02) != (old
& 0x02)) {
588 uint32_t stnr
= (st
->ctl
>> 20) & 0x0f;
589 /* run bit flipped */
590 if (st
->ctl
& 0x02) {
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
);
595 intel_hda_notify_codecs(d
, stnr
, true, output
);
598 dprint(d
, 1, "st #%d: stop %d\n", reg
->stream
, stnr
);
599 intel_hda_notify_codecs(d
, stnr
, false, output
);
602 intel_hda_update_irq(d
);
605 /* --------------------------------------------------------------------- */
607 #define ST_REG(_n, _o) (0x80 + (_n) * 0x20 + (_o))
609 static const struct IntelHDAReg regtab
[] = {
611 [ ICH6_REG_GCAP
] = {
616 [ ICH6_REG_VMIN
] = {
620 [ ICH6_REG_VMAJ
] = {
625 [ ICH6_REG_OUTPAY
] = {
630 [ ICH6_REG_INPAY
] = {
635 [ ICH6_REG_GCTL
] = {
639 .offset
= offsetof(IntelHDAState
, g_ctl
),
640 .whandler
= intel_hda_set_g_ctl
,
642 [ ICH6_REG_WAKEEN
] = {
646 .offset
= offsetof(IntelHDAState
, wake_en
),
647 .whandler
= intel_hda_set_wake_en
,
649 [ ICH6_REG_STATESTS
] = {
654 .offset
= offsetof(IntelHDAState
, state_sts
),
655 .whandler
= intel_hda_set_state_sts
,
659 [ ICH6_REG_INTCTL
] = {
663 .offset
= offsetof(IntelHDAState
, int_ctl
),
664 .whandler
= intel_hda_set_int_ctl
,
666 [ ICH6_REG_INTSTS
] = {
670 .wclear
= 0xc00000ff,
671 .offset
= offsetof(IntelHDAState
, int_sts
),
675 [ ICH6_REG_WALLCLK
] = {
678 .offset
= offsetof(IntelHDAState
, wall_clk
),
679 .rhandler
= intel_hda_get_wall_clk
,
683 [ ICH6_REG_CORBLBASE
] = {
687 .offset
= offsetof(IntelHDAState
, corb_lbase
),
689 [ ICH6_REG_CORBUBASE
] = {
693 .offset
= offsetof(IntelHDAState
, corb_ubase
),
695 [ ICH6_REG_CORBWP
] = {
699 .offset
= offsetof(IntelHDAState
, corb_wp
),
700 .whandler
= intel_hda_set_corb_wp
,
702 [ ICH6_REG_CORBRP
] = {
706 .offset
= offsetof(IntelHDAState
, corb_rp
),
708 [ ICH6_REG_CORBCTL
] = {
712 .offset
= offsetof(IntelHDAState
, corb_ctl
),
713 .whandler
= intel_hda_set_corb_ctl
,
715 [ ICH6_REG_CORBSTS
] = {
720 .offset
= offsetof(IntelHDAState
, corb_sts
),
722 [ ICH6_REG_CORBSIZE
] = {
726 .offset
= offsetof(IntelHDAState
, corb_size
),
728 [ ICH6_REG_RIRBLBASE
] = {
732 .offset
= offsetof(IntelHDAState
, rirb_lbase
),
734 [ ICH6_REG_RIRBUBASE
] = {
738 .offset
= offsetof(IntelHDAState
, rirb_ubase
),
740 [ ICH6_REG_RIRBWP
] = {
744 .offset
= offsetof(IntelHDAState
, rirb_wp
),
745 .whandler
= intel_hda_set_rirb_wp
,
747 [ ICH6_REG_RINTCNT
] = {
751 .offset
= offsetof(IntelHDAState
, rirb_cnt
),
753 [ ICH6_REG_RIRBCTL
] = {
757 .offset
= offsetof(IntelHDAState
, rirb_ctl
),
759 [ ICH6_REG_RIRBSTS
] = {
764 .offset
= offsetof(IntelHDAState
, rirb_sts
),
765 .whandler
= intel_hda_set_rirb_sts
,
767 [ ICH6_REG_RIRBSIZE
] = {
771 .offset
= offsetof(IntelHDAState
, rirb_size
),
774 [ ICH6_REG_DPLBASE
] = {
778 .offset
= offsetof(IntelHDAState
, dp_lbase
),
780 [ ICH6_REG_DPUBASE
] = {
784 .offset
= offsetof(IntelHDAState
, dp_ubase
),
791 .offset
= offsetof(IntelHDAState
, icw
),
796 .offset
= offsetof(IntelHDAState
, irr
),
803 .offset
= offsetof(IntelHDAState
, ics
),
804 .whandler
= intel_hda_set_ics
,
807 #define HDA_STREAM(_t, _i) \
808 [ ST_REG(_i, ICH6_REG_SD_CTL) ] = { \
810 .name = _t stringify(_i) " CTL", \
812 .wmask = 0x1cff001f, \
813 .offset = offsetof(IntelHDAState, st[_i].ctl), \
814 .whandler = intel_hda_set_st_ctl, \
816 [ ST_REG(_i, ICH6_REG_SD_CTL) + 2] = { \
818 .name = _t stringify(_i) " CTL(stnr)", \
821 .wmask = 0x00ff0000, \
822 .offset = offsetof(IntelHDAState, st[_i].ctl), \
823 .whandler = intel_hda_set_st_ctl, \
825 [ ST_REG(_i, ICH6_REG_SD_STS)] = { \
827 .name = _t stringify(_i) " CTL(sts)", \
830 .wmask = 0x1c000000, \
831 .wclear = 0x1c000000, \
832 .offset = offsetof(IntelHDAState, st[_i].ctl), \
833 .whandler = intel_hda_set_st_ctl, \
834 .reset = SD_STS_FIFO_READY << 24 \
836 [ ST_REG(_i, ICH6_REG_SD_LPIB) ] = { \
838 .name = _t stringify(_i) " LPIB", \
840 .offset = offsetof(IntelHDAState, st[_i].lpib), \
842 [ ST_REG(_i, ICH6_REG_SD_CBL) ] = { \
844 .name = _t stringify(_i) " CBL", \
846 .wmask = 0xffffffff, \
847 .offset = offsetof(IntelHDAState, st[_i].cbl), \
849 [ ST_REG(_i, ICH6_REG_SD_LVI) ] = { \
851 .name = _t stringify(_i) " LVI", \
854 .offset = offsetof(IntelHDAState, st[_i].lvi), \
856 [ ST_REG(_i, ICH6_REG_SD_FIFOSIZE) ] = { \
858 .name = _t stringify(_i) " FIFOS", \
860 .reset = HDA_BUFFER_SIZE, \
862 [ ST_REG(_i, ICH6_REG_SD_FORMAT) ] = { \
864 .name = _t stringify(_i) " FMT", \
867 .offset = offsetof(IntelHDAState, st[_i].fmt), \
869 [ ST_REG(_i, ICH6_REG_SD_BDLPL) ] = { \
871 .name = _t stringify(_i) " BDLPL", \
873 .wmask = 0xffffff80, \
874 .offset = offsetof(IntelHDAState, st[_i].bdlp_lbase), \
876 [ ST_REG(_i, ICH6_REG_SD_BDLPU) ] = { \
878 .name = _t stringify(_i) " BDLPU", \
880 .wmask = 0xffffffff, \
881 .offset = offsetof(IntelHDAState, st[_i].bdlp_ubase), \
896 static const IntelHDAReg
*intel_hda_reg_find(IntelHDAState
*d
, hwaddr addr
)
898 const IntelHDAReg
*reg
;
900 if (addr
>= ARRAY_SIZE(regtab
)) {
904 if (reg
->name
== NULL
) {
910 dprint(d
, 1, "unknown register, addr 0x%x\n", (int) addr
);
914 static uint32_t *intel_hda_reg_addr(IntelHDAState
*d
, const IntelHDAReg
*reg
)
916 uint8_t *addr
= (void*)d
;
919 return (uint32_t*)addr
;
922 static void intel_hda_reg_write(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t val
,
932 qemu_log_mask(LOG_GUEST_ERROR
, "intel-hda: write to r/o reg %s\n",
938 time_t now
= time(NULL
);
939 if (d
->last_write
&& d
->last_reg
== reg
&& d
->last_val
== val
) {
941 if (d
->last_sec
!= now
) {
942 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
947 if (d
->repeat_count
) {
948 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
950 dprint(d
, 2, "write %-16s: 0x%x (%x)\n", reg
->name
, val
, wmask
);
958 assert(reg
->offset
!= 0);
960 addr
= intel_hda_reg_addr(d
, reg
);
965 wmask
<<= reg
->shift
;
969 *addr
|= wmask
& val
;
970 *addr
&= ~(val
& reg
->wclear
);
973 reg
->whandler(d
, reg
, old
);
977 static uint32_t intel_hda_reg_read(IntelHDAState
*d
, const IntelHDAReg
*reg
,
987 reg
->rhandler(d
, reg
);
990 if (reg
->offset
== 0) {
991 /* constant read-only register */
994 addr
= intel_hda_reg_addr(d
, reg
);
1002 time_t now
= time(NULL
);
1003 if (!d
->last_write
&& d
->last_reg
== reg
&& d
->last_val
== ret
) {
1005 if (d
->last_sec
!= now
) {
1006 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
1008 d
->repeat_count
= 0;
1011 if (d
->repeat_count
) {
1012 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
1014 dprint(d
, 2, "read %-16s: 0x%x (%x)\n", reg
->name
, ret
, rmask
);
1019 d
->repeat_count
= 0;
1025 static void intel_hda_regs_reset(IntelHDAState
*d
)
1030 for (i
= 0; i
< ARRAY_SIZE(regtab
); i
++) {
1031 if (regtab
[i
].name
== NULL
) {
1034 if (regtab
[i
].offset
== 0) {
1037 addr
= intel_hda_reg_addr(d
, regtab
+ i
);
1038 *addr
= regtab
[i
].reset
;
1042 /* --------------------------------------------------------------------- */
1044 static void intel_hda_mmio_write(void *opaque
, hwaddr addr
, uint64_t val
,
1047 IntelHDAState
*d
= opaque
;
1048 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1050 intel_hda_reg_write(d
, reg
, val
, MAKE_64BIT_MASK(0, size
* 8));
1053 static uint64_t intel_hda_mmio_read(void *opaque
, hwaddr addr
, unsigned size
)
1055 IntelHDAState
*d
= opaque
;
1056 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1058 return intel_hda_reg_read(d
, reg
, MAKE_64BIT_MASK(0, size
* 8));
1061 static const MemoryRegionOps intel_hda_mmio_ops
= {
1062 .read
= intel_hda_mmio_read
,
1063 .write
= intel_hda_mmio_write
,
1065 .min_access_size
= 1,
1066 .max_access_size
= 4,
1068 .endianness
= DEVICE_NATIVE_ENDIAN
,
1071 /* --------------------------------------------------------------------- */
1073 static void intel_hda_reset(DeviceState
*dev
)
1076 IntelHDAState
*d
= INTEL_HDA(dev
);
1077 HDACodecDevice
*cdev
;
1079 intel_hda_regs_reset(d
);
1080 d
->wall_base_ns
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1082 QTAILQ_FOREACH(kid
, &d
->codecs
.qbus
.children
, sibling
) {
1083 DeviceState
*qdev
= kid
->child
;
1084 cdev
= HDA_CODEC_DEVICE(qdev
);
1085 d
->state_sts
|= (1 << cdev
->cad
);
1087 intel_hda_update_irq(d
);
1090 static void intel_hda_realize(PCIDevice
*pci
, Error
**errp
)
1092 IntelHDAState
*d
= INTEL_HDA(pci
);
1093 uint8_t *conf
= d
->pci
.config
;
1097 d
->name
= object_get_typename(OBJECT(d
));
1099 pci_config_set_interrupt_pin(conf
, 1);
1101 /* HDCTL off 0x40 bit 0 selects signaling mode (1-HDA, 0 - Ac97) 18.1.19 */
1104 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
);
1117 assert(!err
|| d
->msi
== ON_OFF_AUTO_AUTO
);
1118 /* With msi=auto, we fall back to MSI off silently */
1122 memory_region_init(&d
->container
, OBJECT(d
),
1123 "intel-hda-container", 0x4000);
1124 memory_region_init_io(&d
->mmio
, OBJECT(d
), &intel_hda_mmio_ops
, d
,
1125 "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
);
1132 hda_codec_bus_init(DEVICE(pci
), &d
->codecs
, sizeof(d
->codecs
),
1133 intel_hda_response
, intel_hda_xfer
);
1136 static void intel_hda_exit(PCIDevice
*pci
)
1138 IntelHDAState
*d
= INTEL_HDA(pci
);
1140 msi_uninit(&d
->pci
);
1143 static int intel_hda_post_load(void *opaque
, int version
)
1145 IntelHDAState
* d
= opaque
;
1148 dprint(d
, 1, "%s\n", __func__
);
1149 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
]);
1154 intel_hda_update_irq(d
);
1158 static const VMStateDescription vmstate_intel_hda_stream
= {
1159 .name
= "intel-hda-stream",
1161 .fields
= (const VMStateField
[]) {
1162 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()
1173 static const VMStateDescription vmstate_intel_hda
= {
1174 .name
= "intel-hda",
1176 .post_load
= intel_hda_post_load
,
1177 .fields
= (const VMStateField
[]) {
1178 VMSTATE_PCI_DEVICE(pci
, IntelHDAState
),
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
,
1210 /* additional state info */
1211 VMSTATE_UINT32(rirb_count
, IntelHDAState
),
1212 VMSTATE_INT64(wall_base_ns
, IntelHDAState
),
1214 VMSTATE_END_OF_LIST()
1218 static Property intel_hda_properties
[] = {
1219 DEFINE_PROP_UINT32("debug", IntelHDAState
, debug
, 0),
1220 DEFINE_PROP_ON_OFF_AUTO("msi", IntelHDAState
, msi
, ON_OFF_AUTO_AUTO
),
1221 DEFINE_PROP_BOOL("old_msi_addr", IntelHDAState
, old_msi_addr
, false),
1222 DEFINE_PROP_END_OF_LIST(),
1225 static void intel_hda_class_init(ObjectClass
*klass
, void *data
)
1227 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1228 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1230 k
->realize
= intel_hda_realize
;
1231 k
->exit
= intel_hda_exit
;
1232 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
1233 k
->class_id
= PCI_CLASS_MULTIMEDIA_HD_AUDIO
;
1234 device_class_set_legacy_reset(dc
, intel_hda_reset
);
1235 dc
->vmsd
= &vmstate_intel_hda
;
1236 device_class_set_props(dc
, intel_hda_properties
);
1239 static void intel_hda_class_init_ich6(ObjectClass
*klass
, void *data
)
1241 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1242 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1244 k
->device_id
= 0x2668;
1246 set_bit(DEVICE_CATEGORY_SOUND
, dc
->categories
);
1247 dc
->desc
= "Intel HD Audio Controller (ich6)";
1250 static void intel_hda_class_init_ich9(ObjectClass
*klass
, void *data
)
1252 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1253 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1255 k
->device_id
= 0x293e;
1257 set_bit(DEVICE_CATEGORY_SOUND
, dc
->categories
);
1258 dc
->desc
= "Intel HD Audio Controller (ich9)";
1261 static const TypeInfo intel_hda_info
= {
1262 .name
= TYPE_INTEL_HDA_GENERIC
,
1263 .parent
= TYPE_PCI_DEVICE
,
1264 .instance_size
= sizeof(IntelHDAState
),
1265 .class_init
= intel_hda_class_init
,
1267 .interfaces
= (InterfaceInfo
[]) {
1268 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
1273 static const TypeInfo intel_hda_info_ich6
= {
1274 .name
= "intel-hda",
1275 .parent
= TYPE_INTEL_HDA_GENERIC
,
1276 .class_init
= intel_hda_class_init_ich6
,
1279 static const TypeInfo intel_hda_info_ich9
= {
1280 .name
= "ich9-intel-hda",
1281 .parent
= TYPE_INTEL_HDA_GENERIC
,
1282 .class_init
= intel_hda_class_init_ich9
,
1285 static void hda_codec_device_class_init(ObjectClass
*klass
, void *data
)
1287 DeviceClass
*k
= DEVICE_CLASS(klass
);
1288 k
->realize
= hda_codec_dev_realize
;
1289 k
->unrealize
= hda_codec_dev_unrealize
;
1290 set_bit(DEVICE_CATEGORY_SOUND
, k
->categories
);
1291 k
->bus_type
= TYPE_HDA_BUS
;
1292 device_class_set_props(k
, hda_props
);
1295 static const TypeInfo hda_codec_device_type_info
= {
1296 .name
= TYPE_HDA_CODEC_DEVICE
,
1297 .parent
= TYPE_DEVICE
,
1298 .instance_size
= sizeof(HDACodecDevice
),
1300 .class_size
= sizeof(HDACodecDeviceClass
),
1301 .class_init
= hda_codec_device_class_init
,
1305 * create intel hda controller with codec attached to it,
1306 * so '-soundhw hda' works.
1308 static int intel_hda_and_codec_init(PCIBus
*bus
, const char *audiodev
)
1310 DeviceState
*controller
;
1314 controller
= DEVICE(pci_create_simple(bus
, -1, "intel-hda"));
1315 hdabus
= QLIST_FIRST(&controller
->child_bus
);
1316 codec
= qdev_new("hda-duplex");
1317 qdev_prop_set_string(codec
, "audiodev", audiodev
);
1318 qdev_realize_and_unref(codec
, hdabus
, &error_fatal
);
1322 static void intel_hda_register_types(void)
1324 type_register_static(&hda_codec_bus_info
);
1325 type_register_static(&intel_hda_info
);
1326 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
);
1332 type_init(intel_hda_register_types
)