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/>.
23 #include "qemu-timer.h"
25 #include "intel-hda.h"
26 #include "intel-hda-defs.h"
29 /* --------------------------------------------------------------------- */
32 static Property hda_props
[] = {
33 DEFINE_PROP_UINT32("cad", HDACodecDevice
, cad
, -1),
34 DEFINE_PROP_END_OF_LIST()
37 static const TypeInfo hda_codec_bus_info
= {
40 .instance_size
= sizeof(HDACodecBus
),
43 void hda_codec_bus_init(DeviceState
*dev
, HDACodecBus
*bus
,
44 hda_codec_response_func response
,
45 hda_codec_xfer_func xfer
)
47 qbus_create_inplace(&bus
->qbus
, TYPE_HDA_BUS
, dev
, NULL
);
48 bus
->response
= response
;
52 static int hda_codec_dev_init(DeviceState
*qdev
)
54 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, qdev
->parent_bus
);
55 HDACodecDevice
*dev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
56 HDACodecDeviceClass
*cdc
= HDA_CODEC_DEVICE_GET_CLASS(dev
);
59 dev
->cad
= bus
->next_cad
;
64 bus
->next_cad
= dev
->cad
+ 1;
65 return cdc
->init(dev
);
68 static int hda_codec_dev_exit(DeviceState
*qdev
)
70 HDACodecDevice
*dev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
71 HDACodecDeviceClass
*cdc
= HDA_CODEC_DEVICE_GET_CLASS(dev
);
79 HDACodecDevice
*hda_codec_find(HDACodecBus
*bus
, uint32_t cad
)
84 QTAILQ_FOREACH(kid
, &bus
->qbus
.children
, sibling
) {
85 DeviceState
*qdev
= kid
->child
;
86 cdev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
87 if (cdev
->cad
== cad
) {
94 void hda_codec_response(HDACodecDevice
*dev
, bool solicited
, uint32_t response
)
96 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
97 bus
->response(dev
, solicited
, response
);
100 bool hda_codec_xfer(HDACodecDevice
*dev
, uint32_t stnr
, bool output
,
101 uint8_t *buf
, uint32_t len
)
103 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
104 return bus
->xfer(dev
, stnr
, output
, buf
, len
);
107 /* --------------------------------------------------------------------- */
108 /* intel hda emulation */
110 typedef struct IntelHDAStream IntelHDAStream
;
111 typedef struct IntelHDAState IntelHDAState
;
112 typedef struct IntelHDAReg IntelHDAReg
;
120 struct IntelHDAStream
{
133 uint32_t bsize
, be
, bp
;
136 struct IntelHDAState
{
173 IntelHDAStream st
[8];
178 int64_t wall_base_ns
;
181 const IntelHDAReg
*last_reg
;
185 uint32_t repeat_count
;
193 const char *name
; /* register name */
194 uint32_t size
; /* size in bytes */
195 uint32_t reset
; /* reset value */
196 uint32_t wmask
; /* write mask */
197 uint32_t wclear
; /* write 1 to clear bits */
198 uint32_t offset
; /* location in IntelHDAState */
199 uint32_t shift
; /* byte access entries for dwords */
201 void (*whandler
)(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
);
202 void (*rhandler
)(IntelHDAState
*d
, const IntelHDAReg
*reg
);
205 static void intel_hda_reset(DeviceState
*dev
);
207 /* --------------------------------------------------------------------- */
209 static target_phys_addr_t
intel_hda_addr(uint32_t lbase
, uint32_t ubase
)
211 target_phys_addr_t addr
;
213 #if TARGET_PHYS_ADDR_BITS == 32
223 static void intel_hda_update_int_sts(IntelHDAState
*d
)
228 /* update controller status */
229 if (d
->rirb_sts
& ICH6_RBSTS_IRQ
) {
232 if (d
->rirb_sts
& ICH6_RBSTS_OVERRUN
) {
235 if (d
->state_sts
& d
->wake_en
) {
239 /* update stream status */
240 for (i
= 0; i
< 8; i
++) {
241 /* buffer completion interrupt */
242 if (d
->st
[i
].ctl
& (1 << 26)) {
247 /* update global status */
248 if (sts
& d
->int_ctl
) {
255 static void intel_hda_update_irq(IntelHDAState
*d
)
257 int msi
= d
->msi
&& msi_enabled(&d
->pci
);
260 intel_hda_update_int_sts(d
);
261 if (d
->int_sts
& (1 << 31) && d
->int_ctl
& (1 << 31)) {
266 dprint(d
, 2, "%s: level %d [%s]\n", __FUNCTION__
,
267 level
, msi
? "msi" : "intx");
270 msi_notify(&d
->pci
, 0);
273 qemu_set_irq(d
->pci
.irq
[0], level
);
277 static int intel_hda_send_command(IntelHDAState
*d
, uint32_t verb
)
279 uint32_t cad
, nid
, data
;
280 HDACodecDevice
*codec
;
281 HDACodecDeviceClass
*cdc
;
283 cad
= (verb
>> 28) & 0x0f;
284 if (verb
& (1 << 27)) {
285 /* indirect node addressing, not specified in HDA 1.0 */
286 dprint(d
, 1, "%s: indirect node addressing (guest bug?)\n", __FUNCTION__
);
289 nid
= (verb
>> 20) & 0x7f;
290 data
= verb
& 0xfffff;
292 codec
= hda_codec_find(&d
->codecs
, cad
);
294 dprint(d
, 1, "%s: addressed non-existing codec\n", __FUNCTION__
);
297 cdc
= HDA_CODEC_DEVICE_GET_CLASS(codec
);
298 cdc
->command(codec
, nid
, data
);
302 static void intel_hda_corb_run(IntelHDAState
*d
)
304 target_phys_addr_t addr
;
307 if (d
->ics
& ICH6_IRS_BUSY
) {
308 dprint(d
, 2, "%s: [icw] verb 0x%08x\n", __FUNCTION__
, d
->icw
);
309 intel_hda_send_command(d
, d
->icw
);
314 if (!(d
->corb_ctl
& ICH6_CORBCTL_RUN
)) {
315 dprint(d
, 2, "%s: !run\n", __FUNCTION__
);
318 if ((d
->corb_rp
& 0xff) == d
->corb_wp
) {
319 dprint(d
, 2, "%s: corb ring empty\n", __FUNCTION__
);
322 if (d
->rirb_count
== d
->rirb_cnt
) {
323 dprint(d
, 2, "%s: rirb count reached\n", __FUNCTION__
);
327 rp
= (d
->corb_rp
+ 1) & 0xff;
328 addr
= intel_hda_addr(d
->corb_lbase
, d
->corb_ubase
);
329 verb
= ldl_le_pci_dma(&d
->pci
, addr
+ 4*rp
);
332 dprint(d
, 2, "%s: [rp 0x%x] verb 0x%08x\n", __FUNCTION__
, rp
, verb
);
333 intel_hda_send_command(d
, verb
);
337 static void intel_hda_response(HDACodecDevice
*dev
, bool solicited
, uint32_t response
)
339 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
340 IntelHDAState
*d
= container_of(bus
, IntelHDAState
, codecs
);
341 target_phys_addr_t addr
;
344 if (d
->ics
& ICH6_IRS_BUSY
) {
345 dprint(d
, 2, "%s: [irr] response 0x%x, cad 0x%x\n",
346 __FUNCTION__
, response
, dev
->cad
);
348 d
->ics
&= ~(ICH6_IRS_BUSY
| 0xf0);
349 d
->ics
|= (ICH6_IRS_VALID
| (dev
->cad
<< 4));
353 if (!(d
->rirb_ctl
& ICH6_RBCTL_DMA_EN
)) {
354 dprint(d
, 1, "%s: rirb dma disabled, drop codec response\n", __FUNCTION__
);
358 ex
= (solicited
? 0 : (1 << 4)) | dev
->cad
;
359 wp
= (d
->rirb_wp
+ 1) & 0xff;
360 addr
= intel_hda_addr(d
->rirb_lbase
, d
->rirb_ubase
);
361 stl_le_pci_dma(&d
->pci
, addr
+ 8*wp
, response
);
362 stl_le_pci_dma(&d
->pci
, addr
+ 8*wp
+ 4, ex
);
365 dprint(d
, 2, "%s: [wp 0x%x] response 0x%x, extra 0x%x\n",
366 __FUNCTION__
, wp
, response
, ex
);
369 if (d
->rirb_count
== d
->rirb_cnt
) {
370 dprint(d
, 2, "%s: rirb count reached (%d)\n", __FUNCTION__
, d
->rirb_count
);
371 if (d
->rirb_ctl
& ICH6_RBCTL_IRQ_EN
) {
372 d
->rirb_sts
|= ICH6_RBSTS_IRQ
;
373 intel_hda_update_irq(d
);
375 } else if ((d
->corb_rp
& 0xff) == d
->corb_wp
) {
376 dprint(d
, 2, "%s: corb ring empty (%d/%d)\n", __FUNCTION__
,
377 d
->rirb_count
, d
->rirb_cnt
);
378 if (d
->rirb_ctl
& ICH6_RBCTL_IRQ_EN
) {
379 d
->rirb_sts
|= ICH6_RBSTS_IRQ
;
380 intel_hda_update_irq(d
);
385 static bool intel_hda_xfer(HDACodecDevice
*dev
, uint32_t stnr
, bool output
,
386 uint8_t *buf
, uint32_t len
)
388 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
389 IntelHDAState
*d
= container_of(bus
, IntelHDAState
, codecs
);
390 target_phys_addr_t addr
;
391 uint32_t s
, copy
, left
;
395 st
= output
? d
->st
+ 4 : d
->st
;
396 for (s
= 0; s
< 4; s
++) {
397 if (stnr
== ((st
[s
].ctl
>> 20) & 0x0f)) {
405 if (st
->bpl
== NULL
) {
408 if (st
->ctl
& (1 << 26)) {
410 * Wait with the next DMA xfer until the guest
411 * has acked the buffer completion interrupt
419 if (copy
> st
->bsize
- st
->lpib
)
420 copy
= st
->bsize
- st
->lpib
;
421 if (copy
> st
->bpl
[st
->be
].len
- st
->bp
)
422 copy
= st
->bpl
[st
->be
].len
- st
->bp
;
424 dprint(d
, 3, "dma: entry %d, pos %d/%d, copy %d\n",
425 st
->be
, st
->bp
, st
->bpl
[st
->be
].len
, copy
);
427 pci_dma_rw(&d
->pci
, st
->bpl
[st
->be
].addr
+ st
->bp
, buf
, copy
, !output
);
433 if (st
->bpl
[st
->be
].len
== st
->bp
) {
434 /* bpl entry filled */
435 if (st
->bpl
[st
->be
].flags
& 0x01) {
440 if (st
->be
== st
->bentries
) {
441 /* bpl wrap around */
447 if (d
->dp_lbase
& 0x01) {
448 addr
= intel_hda_addr(d
->dp_lbase
& ~0x01, d
->dp_ubase
);
449 stl_le_pci_dma(&d
->pci
, addr
+ 8*s
, st
->lpib
);
451 dprint(d
, 3, "dma: --\n");
454 st
->ctl
|= (1 << 26); /* buffer completion interrupt */
455 intel_hda_update_irq(d
);
460 static void intel_hda_parse_bdl(IntelHDAState
*d
, IntelHDAStream
*st
)
462 target_phys_addr_t addr
;
466 addr
= intel_hda_addr(st
->bdlp_lbase
, st
->bdlp_ubase
);
467 st
->bentries
= st
->lvi
+1;
469 st
->bpl
= g_malloc(sizeof(bpl
) * st
->bentries
);
470 for (i
= 0; i
< st
->bentries
; i
++, addr
+= 16) {
471 pci_dma_read(&d
->pci
, addr
, buf
, 16);
472 st
->bpl
[i
].addr
= le64_to_cpu(*(uint64_t *)buf
);
473 st
->bpl
[i
].len
= le32_to_cpu(*(uint32_t *)(buf
+ 8));
474 st
->bpl
[i
].flags
= le32_to_cpu(*(uint32_t *)(buf
+ 12));
475 dprint(d
, 1, "bdl/%d: 0x%" PRIx64
" +0x%x, 0x%x\n",
476 i
, st
->bpl
[i
].addr
, st
->bpl
[i
].len
, st
->bpl
[i
].flags
);
485 static void intel_hda_notify_codecs(IntelHDAState
*d
, uint32_t stream
, bool running
, bool output
)
488 HDACodecDevice
*cdev
;
490 QTAILQ_FOREACH(kid
, &d
->codecs
.qbus
.children
, sibling
) {
491 DeviceState
*qdev
= kid
->child
;
492 HDACodecDeviceClass
*cdc
;
494 cdev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
495 cdc
= HDA_CODEC_DEVICE_GET_CLASS(cdev
);
497 cdc
->stream(cdev
, stream
, running
, output
);
502 /* --------------------------------------------------------------------- */
504 static void intel_hda_set_g_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
506 if ((d
->g_ctl
& ICH6_GCTL_RESET
) == 0) {
507 intel_hda_reset(&d
->pci
.qdev
);
511 static void intel_hda_set_wake_en(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
513 intel_hda_update_irq(d
);
516 static void intel_hda_set_state_sts(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
518 intel_hda_update_irq(d
);
521 static void intel_hda_set_int_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
523 intel_hda_update_irq(d
);
526 static void intel_hda_get_wall_clk(IntelHDAState
*d
, const IntelHDAReg
*reg
)
530 ns
= qemu_get_clock_ns(vm_clock
) - d
->wall_base_ns
;
531 d
->wall_clk
= (uint32_t)(ns
* 24 / 1000); /* 24 MHz */
534 static void intel_hda_set_corb_wp(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
536 intel_hda_corb_run(d
);
539 static void intel_hda_set_corb_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
541 intel_hda_corb_run(d
);
544 static void intel_hda_set_rirb_wp(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
546 if (d
->rirb_wp
& ICH6_RIRBWP_RST
) {
551 static void intel_hda_set_rirb_sts(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
553 intel_hda_update_irq(d
);
555 if ((old
& ICH6_RBSTS_IRQ
) && !(d
->rirb_sts
& ICH6_RBSTS_IRQ
)) {
556 /* cleared ICH6_RBSTS_IRQ */
558 intel_hda_corb_run(d
);
562 static void intel_hda_set_ics(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
564 if (d
->ics
& ICH6_IRS_BUSY
) {
565 intel_hda_corb_run(d
);
569 static void intel_hda_set_st_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
571 bool output
= reg
->stream
>= 4;
572 IntelHDAStream
*st
= d
->st
+ reg
->stream
;
574 if (st
->ctl
& 0x01) {
576 dprint(d
, 1, "st #%d: reset\n", reg
->stream
);
579 if ((st
->ctl
& 0x02) != (old
& 0x02)) {
580 uint32_t stnr
= (st
->ctl
>> 20) & 0x0f;
581 /* run bit flipped */
582 if (st
->ctl
& 0x02) {
584 dprint(d
, 1, "st #%d: start %d (ring buf %d bytes)\n",
585 reg
->stream
, stnr
, st
->cbl
);
586 intel_hda_parse_bdl(d
, st
);
587 intel_hda_notify_codecs(d
, stnr
, true, output
);
590 dprint(d
, 1, "st #%d: stop %d\n", reg
->stream
, stnr
);
591 intel_hda_notify_codecs(d
, stnr
, false, output
);
594 intel_hda_update_irq(d
);
597 /* --------------------------------------------------------------------- */
599 #define ST_REG(_n, _o) (0x80 + (_n) * 0x20 + (_o))
601 static const struct IntelHDAReg regtab
[] = {
603 [ ICH6_REG_GCAP
] = {
608 [ ICH6_REG_VMIN
] = {
612 [ ICH6_REG_VMAJ
] = {
617 [ ICH6_REG_OUTPAY
] = {
622 [ ICH6_REG_INPAY
] = {
627 [ ICH6_REG_GCTL
] = {
631 .offset
= offsetof(IntelHDAState
, g_ctl
),
632 .whandler
= intel_hda_set_g_ctl
,
634 [ ICH6_REG_WAKEEN
] = {
638 .offset
= offsetof(IntelHDAState
, wake_en
),
639 .whandler
= intel_hda_set_wake_en
,
641 [ ICH6_REG_STATESTS
] = {
646 .offset
= offsetof(IntelHDAState
, state_sts
),
647 .whandler
= intel_hda_set_state_sts
,
651 [ ICH6_REG_INTCTL
] = {
655 .offset
= offsetof(IntelHDAState
, int_ctl
),
656 .whandler
= intel_hda_set_int_ctl
,
658 [ ICH6_REG_INTSTS
] = {
662 .wclear
= 0xc00000ff,
663 .offset
= offsetof(IntelHDAState
, int_sts
),
667 [ ICH6_REG_WALLCLK
] = {
670 .offset
= offsetof(IntelHDAState
, wall_clk
),
671 .rhandler
= intel_hda_get_wall_clk
,
673 [ ICH6_REG_WALLCLK
+ 0x2000 ] = {
674 .name
= "WALLCLK(alias)",
676 .offset
= offsetof(IntelHDAState
, wall_clk
),
677 .rhandler
= intel_hda_get_wall_clk
,
681 [ ICH6_REG_CORBLBASE
] = {
685 .offset
= offsetof(IntelHDAState
, corb_lbase
),
687 [ ICH6_REG_CORBUBASE
] = {
691 .offset
= offsetof(IntelHDAState
, corb_ubase
),
693 [ ICH6_REG_CORBWP
] = {
697 .offset
= offsetof(IntelHDAState
, corb_wp
),
698 .whandler
= intel_hda_set_corb_wp
,
700 [ ICH6_REG_CORBRP
] = {
704 .offset
= offsetof(IntelHDAState
, corb_rp
),
706 [ ICH6_REG_CORBCTL
] = {
710 .offset
= offsetof(IntelHDAState
, corb_ctl
),
711 .whandler
= intel_hda_set_corb_ctl
,
713 [ ICH6_REG_CORBSTS
] = {
718 .offset
= offsetof(IntelHDAState
, corb_sts
),
720 [ ICH6_REG_CORBSIZE
] = {
724 .offset
= offsetof(IntelHDAState
, corb_size
),
726 [ ICH6_REG_RIRBLBASE
] = {
730 .offset
= offsetof(IntelHDAState
, rirb_lbase
),
732 [ ICH6_REG_RIRBUBASE
] = {
736 .offset
= offsetof(IntelHDAState
, rirb_ubase
),
738 [ ICH6_REG_RIRBWP
] = {
742 .offset
= offsetof(IntelHDAState
, rirb_wp
),
743 .whandler
= intel_hda_set_rirb_wp
,
745 [ ICH6_REG_RINTCNT
] = {
749 .offset
= offsetof(IntelHDAState
, rirb_cnt
),
751 [ ICH6_REG_RIRBCTL
] = {
755 .offset
= offsetof(IntelHDAState
, rirb_ctl
),
757 [ ICH6_REG_RIRBSTS
] = {
762 .offset
= offsetof(IntelHDAState
, rirb_sts
),
763 .whandler
= intel_hda_set_rirb_sts
,
765 [ ICH6_REG_RIRBSIZE
] = {
769 .offset
= offsetof(IntelHDAState
, rirb_size
),
772 [ ICH6_REG_DPLBASE
] = {
776 .offset
= offsetof(IntelHDAState
, dp_lbase
),
778 [ ICH6_REG_DPUBASE
] = {
782 .offset
= offsetof(IntelHDAState
, dp_ubase
),
789 .offset
= offsetof(IntelHDAState
, icw
),
794 .offset
= offsetof(IntelHDAState
, irr
),
801 .offset
= offsetof(IntelHDAState
, ics
),
802 .whandler
= intel_hda_set_ics
,
805 #define HDA_STREAM(_t, _i) \
806 [ ST_REG(_i, ICH6_REG_SD_CTL) ] = { \
808 .name = _t stringify(_i) " CTL", \
810 .wmask = 0x1cff001f, \
811 .offset = offsetof(IntelHDAState, st[_i].ctl), \
812 .whandler = intel_hda_set_st_ctl, \
814 [ ST_REG(_i, ICH6_REG_SD_CTL) + 2] = { \
816 .name = _t stringify(_i) " CTL(stnr)", \
819 .wmask = 0x00ff0000, \
820 .offset = offsetof(IntelHDAState, st[_i].ctl), \
821 .whandler = intel_hda_set_st_ctl, \
823 [ ST_REG(_i, ICH6_REG_SD_STS)] = { \
825 .name = _t stringify(_i) " CTL(sts)", \
828 .wmask = 0x1c000000, \
829 .wclear = 0x1c000000, \
830 .offset = offsetof(IntelHDAState, st[_i].ctl), \
831 .whandler = intel_hda_set_st_ctl, \
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
, target_phys_addr_t addr
)
901 const IntelHDAReg
*reg
;
903 if (addr
>= sizeof(regtab
)/sizeof(regtab
[0])) {
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
,
936 time_t now
= time(NULL
);
937 if (d
->last_write
&& d
->last_reg
== reg
&& d
->last_val
== val
) {
939 if (d
->last_sec
!= now
) {
940 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
945 if (d
->repeat_count
) {
946 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
948 dprint(d
, 2, "write %-16s: 0x%x (%x)\n", reg
->name
, val
, wmask
);
956 assert(reg
->offset
!= 0);
958 addr
= intel_hda_reg_addr(d
, reg
);
963 wmask
<<= reg
->shift
;
967 *addr
|= wmask
& val
;
968 *addr
&= ~(val
& reg
->wclear
);
971 reg
->whandler(d
, reg
, old
);
975 static uint32_t intel_hda_reg_read(IntelHDAState
*d
, const IntelHDAReg
*reg
,
985 reg
->rhandler(d
, reg
);
988 if (reg
->offset
== 0) {
989 /* constant read-only register */
992 addr
= intel_hda_reg_addr(d
, reg
);
1000 time_t now
= time(NULL
);
1001 if (!d
->last_write
&& d
->last_reg
== reg
&& d
->last_val
== ret
) {
1003 if (d
->last_sec
!= now
) {
1004 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
1006 d
->repeat_count
= 0;
1009 if (d
->repeat_count
) {
1010 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
1012 dprint(d
, 2, "read %-16s: 0x%x (%x)\n", reg
->name
, ret
, rmask
);
1017 d
->repeat_count
= 0;
1023 static void intel_hda_regs_reset(IntelHDAState
*d
)
1028 for (i
= 0; i
< sizeof(regtab
)/sizeof(regtab
[0]); i
++) {
1029 if (regtab
[i
].name
== NULL
) {
1032 if (regtab
[i
].offset
== 0) {
1035 addr
= intel_hda_reg_addr(d
, regtab
+ i
);
1036 *addr
= regtab
[i
].reset
;
1040 /* --------------------------------------------------------------------- */
1042 static void intel_hda_mmio_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1044 IntelHDAState
*d
= opaque
;
1045 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1047 intel_hda_reg_write(d
, reg
, val
, 0xff);
1050 static void intel_hda_mmio_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1052 IntelHDAState
*d
= opaque
;
1053 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1055 intel_hda_reg_write(d
, reg
, val
, 0xffff);
1058 static void intel_hda_mmio_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1060 IntelHDAState
*d
= opaque
;
1061 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1063 intel_hda_reg_write(d
, reg
, val
, 0xffffffff);
1066 static uint32_t intel_hda_mmio_readb(void *opaque
, target_phys_addr_t addr
)
1068 IntelHDAState
*d
= opaque
;
1069 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1071 return intel_hda_reg_read(d
, reg
, 0xff);
1074 static uint32_t intel_hda_mmio_readw(void *opaque
, target_phys_addr_t addr
)
1076 IntelHDAState
*d
= opaque
;
1077 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1079 return intel_hda_reg_read(d
, reg
, 0xffff);
1082 static uint32_t intel_hda_mmio_readl(void *opaque
, target_phys_addr_t addr
)
1084 IntelHDAState
*d
= opaque
;
1085 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1087 return intel_hda_reg_read(d
, reg
, 0xffffffff);
1090 static const MemoryRegionOps intel_hda_mmio_ops
= {
1093 intel_hda_mmio_readb
,
1094 intel_hda_mmio_readw
,
1095 intel_hda_mmio_readl
,
1098 intel_hda_mmio_writeb
,
1099 intel_hda_mmio_writew
,
1100 intel_hda_mmio_writel
,
1103 .endianness
= DEVICE_NATIVE_ENDIAN
,
1106 /* --------------------------------------------------------------------- */
1108 static void intel_hda_reset(DeviceState
*dev
)
1111 IntelHDAState
*d
= DO_UPCAST(IntelHDAState
, pci
.qdev
, dev
);
1112 HDACodecDevice
*cdev
;
1114 intel_hda_regs_reset(d
);
1115 d
->wall_base_ns
= qemu_get_clock_ns(vm_clock
);
1118 QTAILQ_FOREACH(kid
, &d
->codecs
.qbus
.children
, sibling
) {
1119 DeviceState
*qdev
= kid
->child
;
1120 cdev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
1121 device_reset(DEVICE(cdev
));
1122 d
->state_sts
|= (1 << cdev
->cad
);
1124 intel_hda_update_irq(d
);
1127 static int intel_hda_init(PCIDevice
*pci
)
1129 IntelHDAState
*d
= DO_UPCAST(IntelHDAState
, pci
, pci
);
1130 uint8_t *conf
= d
->pci
.config
;
1132 d
->name
= object_get_typename(OBJECT(d
));
1134 pci_config_set_interrupt_pin(conf
, 1);
1136 /* HDCTL off 0x40 bit 0 selects signaling mode (1-HDA, 0 - Ac97) 18.1.19 */
1139 memory_region_init_io(&d
->mmio
, &intel_hda_mmio_ops
, d
,
1140 "intel-hda", 0x4000);
1141 pci_register_bar(&d
->pci
, 0, 0, &d
->mmio
);
1143 msi_init(&d
->pci
, 0x50, 1, true, false);
1146 hda_codec_bus_init(&d
->pci
.qdev
, &d
->codecs
,
1147 intel_hda_response
, intel_hda_xfer
);
1152 static void intel_hda_exit(PCIDevice
*pci
)
1154 IntelHDAState
*d
= DO_UPCAST(IntelHDAState
, pci
, pci
);
1156 msi_uninit(&d
->pci
);
1157 memory_region_destroy(&d
->mmio
);
1160 static int intel_hda_post_load(void *opaque
, int version
)
1162 IntelHDAState
* d
= opaque
;
1165 dprint(d
, 1, "%s\n", __FUNCTION__
);
1166 for (i
= 0; i
< ARRAY_SIZE(d
->st
); i
++) {
1167 if (d
->st
[i
].ctl
& 0x02) {
1168 intel_hda_parse_bdl(d
, &d
->st
[i
]);
1171 intel_hda_update_irq(d
);
1175 static const VMStateDescription vmstate_intel_hda_stream
= {
1176 .name
= "intel-hda-stream",
1178 .fields
= (VMStateField
[]) {
1179 VMSTATE_UINT32(ctl
, IntelHDAStream
),
1180 VMSTATE_UINT32(lpib
, IntelHDAStream
),
1181 VMSTATE_UINT32(cbl
, IntelHDAStream
),
1182 VMSTATE_UINT32(lvi
, IntelHDAStream
),
1183 VMSTATE_UINT32(fmt
, IntelHDAStream
),
1184 VMSTATE_UINT32(bdlp_lbase
, IntelHDAStream
),
1185 VMSTATE_UINT32(bdlp_ubase
, IntelHDAStream
),
1186 VMSTATE_END_OF_LIST()
1190 static const VMStateDescription vmstate_intel_hda
= {
1191 .name
= "intel-hda",
1193 .post_load
= intel_hda_post_load
,
1194 .fields
= (VMStateField
[]) {
1195 VMSTATE_PCI_DEVICE(pci
, IntelHDAState
),
1198 VMSTATE_UINT32(g_ctl
, IntelHDAState
),
1199 VMSTATE_UINT32(wake_en
, IntelHDAState
),
1200 VMSTATE_UINT32(state_sts
, IntelHDAState
),
1201 VMSTATE_UINT32(int_ctl
, IntelHDAState
),
1202 VMSTATE_UINT32(int_sts
, IntelHDAState
),
1203 VMSTATE_UINT32(wall_clk
, IntelHDAState
),
1204 VMSTATE_UINT32(corb_lbase
, IntelHDAState
),
1205 VMSTATE_UINT32(corb_ubase
, IntelHDAState
),
1206 VMSTATE_UINT32(corb_rp
, IntelHDAState
),
1207 VMSTATE_UINT32(corb_wp
, IntelHDAState
),
1208 VMSTATE_UINT32(corb_ctl
, IntelHDAState
),
1209 VMSTATE_UINT32(corb_sts
, IntelHDAState
),
1210 VMSTATE_UINT32(corb_size
, IntelHDAState
),
1211 VMSTATE_UINT32(rirb_lbase
, IntelHDAState
),
1212 VMSTATE_UINT32(rirb_ubase
, IntelHDAState
),
1213 VMSTATE_UINT32(rirb_wp
, IntelHDAState
),
1214 VMSTATE_UINT32(rirb_cnt
, IntelHDAState
),
1215 VMSTATE_UINT32(rirb_ctl
, IntelHDAState
),
1216 VMSTATE_UINT32(rirb_sts
, IntelHDAState
),
1217 VMSTATE_UINT32(rirb_size
, IntelHDAState
),
1218 VMSTATE_UINT32(dp_lbase
, IntelHDAState
),
1219 VMSTATE_UINT32(dp_ubase
, IntelHDAState
),
1220 VMSTATE_UINT32(icw
, IntelHDAState
),
1221 VMSTATE_UINT32(irr
, IntelHDAState
),
1222 VMSTATE_UINT32(ics
, IntelHDAState
),
1223 VMSTATE_STRUCT_ARRAY(st
, IntelHDAState
, 8, 0,
1224 vmstate_intel_hda_stream
,
1227 /* additional state info */
1228 VMSTATE_UINT32(rirb_count
, IntelHDAState
),
1229 VMSTATE_INT64(wall_base_ns
, IntelHDAState
),
1231 VMSTATE_END_OF_LIST()
1235 static Property intel_hda_properties
[] = {
1236 DEFINE_PROP_UINT32("debug", IntelHDAState
, debug
, 0),
1237 DEFINE_PROP_UINT32("msi", IntelHDAState
, msi
, 1),
1238 DEFINE_PROP_END_OF_LIST(),
1241 static void intel_hda_class_init(ObjectClass
*klass
, void *data
)
1243 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1244 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1246 k
->init
= intel_hda_init
;
1247 k
->exit
= intel_hda_exit
;
1248 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
1249 k
->device_id
= 0x2668;
1251 k
->class_id
= PCI_CLASS_MULTIMEDIA_HD_AUDIO
;
1252 dc
->desc
= "Intel HD Audio Controller";
1253 dc
->reset
= intel_hda_reset
;
1254 dc
->vmsd
= &vmstate_intel_hda
;
1255 dc
->props
= intel_hda_properties
;
1258 static TypeInfo intel_hda_info
= {
1259 .name
= "intel-hda",
1260 .parent
= TYPE_PCI_DEVICE
,
1261 .instance_size
= sizeof(IntelHDAState
),
1262 .class_init
= intel_hda_class_init
,
1265 static void hda_codec_device_class_init(ObjectClass
*klass
, void *data
)
1267 DeviceClass
*k
= DEVICE_CLASS(klass
);
1268 k
->init
= hda_codec_dev_init
;
1269 k
->exit
= hda_codec_dev_exit
;
1270 k
->bus_type
= TYPE_HDA_BUS
;
1271 k
->props
= hda_props
;
1274 static TypeInfo hda_codec_device_type_info
= {
1275 .name
= TYPE_HDA_CODEC_DEVICE
,
1276 .parent
= TYPE_DEVICE
,
1277 .instance_size
= sizeof(HDACodecDevice
),
1279 .class_size
= sizeof(HDACodecDeviceClass
),
1280 .class_init
= hda_codec_device_class_init
,
1283 static void intel_hda_register_types(void)
1285 type_register_static(&hda_codec_bus_info
);
1286 type_register_static(&intel_hda_info
);
1287 type_register_static(&hda_codec_device_type_info
);
1290 type_init(intel_hda_register_types
)
1293 * create intel hda controller with codec attached to it,
1294 * so '-soundhw hda' works.
1296 int intel_hda_and_codec_init(PCIBus
*bus
)
1298 PCIDevice
*controller
;
1302 controller
= pci_create_simple(bus
, -1, "intel-hda");
1303 hdabus
= QLIST_FIRST(&controller
->qdev
.child_bus
);
1304 codec
= qdev_create(hdabus
, "hda-duplex");
1305 qdev_init_nofail(codec
);