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 struct BusInfo hda_codec_bus_info
= {
34 .size
= sizeof(HDACodecBus
),
35 .props
= (Property
[]) {
36 DEFINE_PROP_UINT32("cad", HDACodecDevice
, cad
, -1),
37 DEFINE_PROP_END_OF_LIST()
41 void hda_codec_bus_init(DeviceState
*dev
, HDACodecBus
*bus
,
42 hda_codec_response_func response
,
43 hda_codec_xfer_func xfer
)
45 qbus_create_inplace(&bus
->qbus
, &hda_codec_bus_info
, dev
, NULL
);
46 bus
->response
= response
;
50 static int hda_codec_dev_init(DeviceState
*qdev
)
52 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, qdev
->parent_bus
);
53 HDACodecDevice
*dev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
54 HDACodecDeviceClass
*cdc
= HDA_CODEC_DEVICE_GET_CLASS(dev
);
57 dev
->cad
= bus
->next_cad
;
62 bus
->next_cad
= dev
->cad
+ 1;
63 return cdc
->init(dev
);
66 static int hda_codec_dev_exit(DeviceState
*qdev
)
68 HDACodecDevice
*dev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
69 HDACodecDeviceClass
*cdc
= HDA_CODEC_DEVICE_GET_CLASS(dev
);
77 HDACodecDevice
*hda_codec_find(HDACodecBus
*bus
, uint32_t cad
)
82 QTAILQ_FOREACH(qdev
, &bus
->qbus
.children
, sibling
) {
83 cdev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
84 if (cdev
->cad
== cad
) {
91 void hda_codec_response(HDACodecDevice
*dev
, bool solicited
, uint32_t response
)
93 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
94 bus
->response(dev
, solicited
, response
);
97 bool hda_codec_xfer(HDACodecDevice
*dev
, uint32_t stnr
, bool output
,
98 uint8_t *buf
, uint32_t len
)
100 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
101 return bus
->xfer(dev
, stnr
, output
, buf
, len
);
104 /* --------------------------------------------------------------------- */
105 /* intel hda emulation */
107 typedef struct IntelHDAStream IntelHDAStream
;
108 typedef struct IntelHDAState IntelHDAState
;
109 typedef struct IntelHDAReg IntelHDAReg
;
117 struct IntelHDAStream
{
130 uint32_t bsize
, be
, bp
;
133 struct IntelHDAState
{
170 IntelHDAStream st
[8];
175 int64_t wall_base_ns
;
178 const IntelHDAReg
*last_reg
;
182 uint32_t repeat_count
;
190 const char *name
; /* register name */
191 uint32_t size
; /* size in bytes */
192 uint32_t reset
; /* reset value */
193 uint32_t wmask
; /* write mask */
194 uint32_t wclear
; /* write 1 to clear bits */
195 uint32_t offset
; /* location in IntelHDAState */
196 uint32_t shift
; /* byte access entries for dwords */
198 void (*whandler
)(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
);
199 void (*rhandler
)(IntelHDAState
*d
, const IntelHDAReg
*reg
);
202 static void intel_hda_reset(DeviceState
*dev
);
204 /* --------------------------------------------------------------------- */
206 static target_phys_addr_t
intel_hda_addr(uint32_t lbase
, uint32_t ubase
)
208 target_phys_addr_t addr
;
210 #if TARGET_PHYS_ADDR_BITS == 32
220 static void intel_hda_update_int_sts(IntelHDAState
*d
)
225 /* update controller status */
226 if (d
->rirb_sts
& ICH6_RBSTS_IRQ
) {
229 if (d
->rirb_sts
& ICH6_RBSTS_OVERRUN
) {
232 if (d
->state_sts
& d
->wake_en
) {
236 /* update stream status */
237 for (i
= 0; i
< 8; i
++) {
238 /* buffer completion interrupt */
239 if (d
->st
[i
].ctl
& (1 << 26)) {
244 /* update global status */
245 if (sts
& d
->int_ctl
) {
252 static void intel_hda_update_irq(IntelHDAState
*d
)
254 int msi
= d
->msi
&& msi_enabled(&d
->pci
);
257 intel_hda_update_int_sts(d
);
258 if (d
->int_sts
& (1 << 31) && d
->int_ctl
& (1 << 31)) {
263 dprint(d
, 2, "%s: level %d [%s]\n", __FUNCTION__
,
264 level
, msi
? "msi" : "intx");
267 msi_notify(&d
->pci
, 0);
270 qemu_set_irq(d
->pci
.irq
[0], level
);
274 static int intel_hda_send_command(IntelHDAState
*d
, uint32_t verb
)
276 uint32_t cad
, nid
, data
;
277 HDACodecDevice
*codec
;
278 HDACodecDeviceClass
*cdc
;
280 cad
= (verb
>> 28) & 0x0f;
281 if (verb
& (1 << 27)) {
282 /* indirect node addressing, not specified in HDA 1.0 */
283 dprint(d
, 1, "%s: indirect node addressing (guest bug?)\n", __FUNCTION__
);
286 nid
= (verb
>> 20) & 0x7f;
287 data
= verb
& 0xfffff;
289 codec
= hda_codec_find(&d
->codecs
, cad
);
291 dprint(d
, 1, "%s: addressed non-existing codec\n", __FUNCTION__
);
294 cdc
= HDA_CODEC_DEVICE_GET_CLASS(codec
);
295 cdc
->command(codec
, nid
, data
);
299 static void intel_hda_corb_run(IntelHDAState
*d
)
301 target_phys_addr_t addr
;
304 if (d
->ics
& ICH6_IRS_BUSY
) {
305 dprint(d
, 2, "%s: [icw] verb 0x%08x\n", __FUNCTION__
, d
->icw
);
306 intel_hda_send_command(d
, d
->icw
);
311 if (!(d
->corb_ctl
& ICH6_CORBCTL_RUN
)) {
312 dprint(d
, 2, "%s: !run\n", __FUNCTION__
);
315 if ((d
->corb_rp
& 0xff) == d
->corb_wp
) {
316 dprint(d
, 2, "%s: corb ring empty\n", __FUNCTION__
);
319 if (d
->rirb_count
== d
->rirb_cnt
) {
320 dprint(d
, 2, "%s: rirb count reached\n", __FUNCTION__
);
324 rp
= (d
->corb_rp
+ 1) & 0xff;
325 addr
= intel_hda_addr(d
->corb_lbase
, d
->corb_ubase
);
326 verb
= ldl_le_pci_dma(&d
->pci
, addr
+ 4*rp
);
329 dprint(d
, 2, "%s: [rp 0x%x] verb 0x%08x\n", __FUNCTION__
, rp
, verb
);
330 intel_hda_send_command(d
, verb
);
334 static void intel_hda_response(HDACodecDevice
*dev
, bool solicited
, uint32_t response
)
336 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
337 IntelHDAState
*d
= container_of(bus
, IntelHDAState
, codecs
);
338 target_phys_addr_t addr
;
341 if (d
->ics
& ICH6_IRS_BUSY
) {
342 dprint(d
, 2, "%s: [irr] response 0x%x, cad 0x%x\n",
343 __FUNCTION__
, response
, dev
->cad
);
345 d
->ics
&= ~(ICH6_IRS_BUSY
| 0xf0);
346 d
->ics
|= (ICH6_IRS_VALID
| (dev
->cad
<< 4));
350 if (!(d
->rirb_ctl
& ICH6_RBCTL_DMA_EN
)) {
351 dprint(d
, 1, "%s: rirb dma disabled, drop codec response\n", __FUNCTION__
);
355 ex
= (solicited
? 0 : (1 << 4)) | dev
->cad
;
356 wp
= (d
->rirb_wp
+ 1) & 0xff;
357 addr
= intel_hda_addr(d
->rirb_lbase
, d
->rirb_ubase
);
358 stl_le_pci_dma(&d
->pci
, addr
+ 8*wp
, response
);
359 stl_le_pci_dma(&d
->pci
, addr
+ 8*wp
+ 4, ex
);
362 dprint(d
, 2, "%s: [wp 0x%x] response 0x%x, extra 0x%x\n",
363 __FUNCTION__
, wp
, response
, ex
);
366 if (d
->rirb_count
== d
->rirb_cnt
) {
367 dprint(d
, 2, "%s: rirb count reached (%d)\n", __FUNCTION__
, d
->rirb_count
);
368 if (d
->rirb_ctl
& ICH6_RBCTL_IRQ_EN
) {
369 d
->rirb_sts
|= ICH6_RBSTS_IRQ
;
370 intel_hda_update_irq(d
);
372 } else if ((d
->corb_rp
& 0xff) == d
->corb_wp
) {
373 dprint(d
, 2, "%s: corb ring empty (%d/%d)\n", __FUNCTION__
,
374 d
->rirb_count
, d
->rirb_cnt
);
375 if (d
->rirb_ctl
& ICH6_RBCTL_IRQ_EN
) {
376 d
->rirb_sts
|= ICH6_RBSTS_IRQ
;
377 intel_hda_update_irq(d
);
382 static bool intel_hda_xfer(HDACodecDevice
*dev
, uint32_t stnr
, bool output
,
383 uint8_t *buf
, uint32_t len
)
385 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
386 IntelHDAState
*d
= container_of(bus
, IntelHDAState
, codecs
);
387 target_phys_addr_t addr
;
388 uint32_t s
, copy
, left
;
392 st
= output
? d
->st
+ 4 : d
->st
;
393 for (s
= 0; s
< 4; s
++) {
394 if (stnr
== ((st
[s
].ctl
>> 20) & 0x0f)) {
402 if (st
->bpl
== NULL
) {
405 if (st
->ctl
& (1 << 26)) {
407 * Wait with the next DMA xfer until the guest
408 * has acked the buffer completion interrupt
416 if (copy
> st
->bsize
- st
->lpib
)
417 copy
= st
->bsize
- st
->lpib
;
418 if (copy
> st
->bpl
[st
->be
].len
- st
->bp
)
419 copy
= st
->bpl
[st
->be
].len
- st
->bp
;
421 dprint(d
, 3, "dma: entry %d, pos %d/%d, copy %d\n",
422 st
->be
, st
->bp
, st
->bpl
[st
->be
].len
, copy
);
424 pci_dma_rw(&d
->pci
, st
->bpl
[st
->be
].addr
+ st
->bp
, buf
, copy
, !output
);
430 if (st
->bpl
[st
->be
].len
== st
->bp
) {
431 /* bpl entry filled */
432 if (st
->bpl
[st
->be
].flags
& 0x01) {
437 if (st
->be
== st
->bentries
) {
438 /* bpl wrap around */
444 if (d
->dp_lbase
& 0x01) {
445 addr
= intel_hda_addr(d
->dp_lbase
& ~0x01, d
->dp_ubase
);
446 stl_le_pci_dma(&d
->pci
, addr
+ 8*s
, st
->lpib
);
448 dprint(d
, 3, "dma: --\n");
451 st
->ctl
|= (1 << 26); /* buffer completion interrupt */
452 intel_hda_update_irq(d
);
457 static void intel_hda_parse_bdl(IntelHDAState
*d
, IntelHDAStream
*st
)
459 target_phys_addr_t addr
;
463 addr
= intel_hda_addr(st
->bdlp_lbase
, st
->bdlp_ubase
);
464 st
->bentries
= st
->lvi
+1;
466 st
->bpl
= g_malloc(sizeof(bpl
) * st
->bentries
);
467 for (i
= 0; i
< st
->bentries
; i
++, addr
+= 16) {
468 pci_dma_read(&d
->pci
, addr
, buf
, 16);
469 st
->bpl
[i
].addr
= le64_to_cpu(*(uint64_t *)buf
);
470 st
->bpl
[i
].len
= le32_to_cpu(*(uint32_t *)(buf
+ 8));
471 st
->bpl
[i
].flags
= le32_to_cpu(*(uint32_t *)(buf
+ 12));
472 dprint(d
, 1, "bdl/%d: 0x%" PRIx64
" +0x%x, 0x%x\n",
473 i
, st
->bpl
[i
].addr
, st
->bpl
[i
].len
, st
->bpl
[i
].flags
);
482 static void intel_hda_notify_codecs(IntelHDAState
*d
, uint32_t stream
, bool running
, bool output
)
485 HDACodecDevice
*cdev
;
487 QTAILQ_FOREACH(qdev
, &d
->codecs
.qbus
.children
, sibling
) {
488 HDACodecDeviceClass
*cdc
;
490 cdev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
491 cdc
= HDA_CODEC_DEVICE_GET_CLASS(cdev
);
493 cdc
->stream(cdev
, stream
, running
, output
);
498 /* --------------------------------------------------------------------- */
500 static void intel_hda_set_g_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
502 if ((d
->g_ctl
& ICH6_GCTL_RESET
) == 0) {
503 intel_hda_reset(&d
->pci
.qdev
);
507 static void intel_hda_set_wake_en(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
509 intel_hda_update_irq(d
);
512 static void intel_hda_set_state_sts(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
514 intel_hda_update_irq(d
);
517 static void intel_hda_set_int_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
519 intel_hda_update_irq(d
);
522 static void intel_hda_get_wall_clk(IntelHDAState
*d
, const IntelHDAReg
*reg
)
526 ns
= qemu_get_clock_ns(vm_clock
) - d
->wall_base_ns
;
527 d
->wall_clk
= (uint32_t)(ns
* 24 / 1000); /* 24 MHz */
530 static void intel_hda_set_corb_wp(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
532 intel_hda_corb_run(d
);
535 static void intel_hda_set_corb_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
537 intel_hda_corb_run(d
);
540 static void intel_hda_set_rirb_wp(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
542 if (d
->rirb_wp
& ICH6_RIRBWP_RST
) {
547 static void intel_hda_set_rirb_sts(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
549 intel_hda_update_irq(d
);
551 if ((old
& ICH6_RBSTS_IRQ
) && !(d
->rirb_sts
& ICH6_RBSTS_IRQ
)) {
552 /* cleared ICH6_RBSTS_IRQ */
554 intel_hda_corb_run(d
);
558 static void intel_hda_set_ics(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
560 if (d
->ics
& ICH6_IRS_BUSY
) {
561 intel_hda_corb_run(d
);
565 static void intel_hda_set_st_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
567 bool output
= reg
->stream
>= 4;
568 IntelHDAStream
*st
= d
->st
+ reg
->stream
;
570 if (st
->ctl
& 0x01) {
572 dprint(d
, 1, "st #%d: reset\n", reg
->stream
);
575 if ((st
->ctl
& 0x02) != (old
& 0x02)) {
576 uint32_t stnr
= (st
->ctl
>> 20) & 0x0f;
577 /* run bit flipped */
578 if (st
->ctl
& 0x02) {
580 dprint(d
, 1, "st #%d: start %d (ring buf %d bytes)\n",
581 reg
->stream
, stnr
, st
->cbl
);
582 intel_hda_parse_bdl(d
, st
);
583 intel_hda_notify_codecs(d
, stnr
, true, output
);
586 dprint(d
, 1, "st #%d: stop %d\n", reg
->stream
, stnr
);
587 intel_hda_notify_codecs(d
, stnr
, false, output
);
590 intel_hda_update_irq(d
);
593 /* --------------------------------------------------------------------- */
595 #define ST_REG(_n, _o) (0x80 + (_n) * 0x20 + (_o))
597 static const struct IntelHDAReg regtab
[] = {
599 [ ICH6_REG_GCAP
] = {
604 [ ICH6_REG_VMIN
] = {
608 [ ICH6_REG_VMAJ
] = {
613 [ ICH6_REG_OUTPAY
] = {
618 [ ICH6_REG_INPAY
] = {
623 [ ICH6_REG_GCTL
] = {
627 .offset
= offsetof(IntelHDAState
, g_ctl
),
628 .whandler
= intel_hda_set_g_ctl
,
630 [ ICH6_REG_WAKEEN
] = {
634 .offset
= offsetof(IntelHDAState
, wake_en
),
635 .whandler
= intel_hda_set_wake_en
,
637 [ ICH6_REG_STATESTS
] = {
642 .offset
= offsetof(IntelHDAState
, state_sts
),
643 .whandler
= intel_hda_set_state_sts
,
647 [ ICH6_REG_INTCTL
] = {
651 .offset
= offsetof(IntelHDAState
, int_ctl
),
652 .whandler
= intel_hda_set_int_ctl
,
654 [ ICH6_REG_INTSTS
] = {
658 .wclear
= 0xc00000ff,
659 .offset
= offsetof(IntelHDAState
, int_sts
),
663 [ ICH6_REG_WALLCLK
] = {
666 .offset
= offsetof(IntelHDAState
, wall_clk
),
667 .rhandler
= intel_hda_get_wall_clk
,
669 [ ICH6_REG_WALLCLK
+ 0x2000 ] = {
670 .name
= "WALLCLK(alias)",
672 .offset
= offsetof(IntelHDAState
, wall_clk
),
673 .rhandler
= intel_hda_get_wall_clk
,
677 [ ICH6_REG_CORBLBASE
] = {
681 .offset
= offsetof(IntelHDAState
, corb_lbase
),
683 [ ICH6_REG_CORBUBASE
] = {
687 .offset
= offsetof(IntelHDAState
, corb_ubase
),
689 [ ICH6_REG_CORBWP
] = {
693 .offset
= offsetof(IntelHDAState
, corb_wp
),
694 .whandler
= intel_hda_set_corb_wp
,
696 [ ICH6_REG_CORBRP
] = {
700 .offset
= offsetof(IntelHDAState
, corb_rp
),
702 [ ICH6_REG_CORBCTL
] = {
706 .offset
= offsetof(IntelHDAState
, corb_ctl
),
707 .whandler
= intel_hda_set_corb_ctl
,
709 [ ICH6_REG_CORBSTS
] = {
714 .offset
= offsetof(IntelHDAState
, corb_sts
),
716 [ ICH6_REG_CORBSIZE
] = {
720 .offset
= offsetof(IntelHDAState
, corb_size
),
722 [ ICH6_REG_RIRBLBASE
] = {
726 .offset
= offsetof(IntelHDAState
, rirb_lbase
),
728 [ ICH6_REG_RIRBUBASE
] = {
732 .offset
= offsetof(IntelHDAState
, rirb_ubase
),
734 [ ICH6_REG_RIRBWP
] = {
738 .offset
= offsetof(IntelHDAState
, rirb_wp
),
739 .whandler
= intel_hda_set_rirb_wp
,
741 [ ICH6_REG_RINTCNT
] = {
745 .offset
= offsetof(IntelHDAState
, rirb_cnt
),
747 [ ICH6_REG_RIRBCTL
] = {
751 .offset
= offsetof(IntelHDAState
, rirb_ctl
),
753 [ ICH6_REG_RIRBSTS
] = {
758 .offset
= offsetof(IntelHDAState
, rirb_sts
),
759 .whandler
= intel_hda_set_rirb_sts
,
761 [ ICH6_REG_RIRBSIZE
] = {
765 .offset
= offsetof(IntelHDAState
, rirb_size
),
768 [ ICH6_REG_DPLBASE
] = {
772 .offset
= offsetof(IntelHDAState
, dp_lbase
),
774 [ ICH6_REG_DPUBASE
] = {
778 .offset
= offsetof(IntelHDAState
, dp_ubase
),
785 .offset
= offsetof(IntelHDAState
, icw
),
790 .offset
= offsetof(IntelHDAState
, irr
),
797 .offset
= offsetof(IntelHDAState
, ics
),
798 .whandler
= intel_hda_set_ics
,
801 #define HDA_STREAM(_t, _i) \
802 [ ST_REG(_i, ICH6_REG_SD_CTL) ] = { \
804 .name = _t stringify(_i) " CTL", \
806 .wmask = 0x1cff001f, \
807 .offset = offsetof(IntelHDAState, st[_i].ctl), \
808 .whandler = intel_hda_set_st_ctl, \
810 [ ST_REG(_i, ICH6_REG_SD_CTL) + 2] = { \
812 .name = _t stringify(_i) " CTL(stnr)", \
815 .wmask = 0x00ff0000, \
816 .offset = offsetof(IntelHDAState, st[_i].ctl), \
817 .whandler = intel_hda_set_st_ctl, \
819 [ ST_REG(_i, ICH6_REG_SD_STS)] = { \
821 .name = _t stringify(_i) " CTL(sts)", \
824 .wmask = 0x1c000000, \
825 .wclear = 0x1c000000, \
826 .offset = offsetof(IntelHDAState, st[_i].ctl), \
827 .whandler = intel_hda_set_st_ctl, \
829 [ ST_REG(_i, ICH6_REG_SD_LPIB) ] = { \
831 .name = _t stringify(_i) " LPIB", \
833 .offset = offsetof(IntelHDAState, st[_i].lpib), \
835 [ ST_REG(_i, ICH6_REG_SD_LPIB) + 0x2000 ] = { \
837 .name = _t stringify(_i) " LPIB(alias)", \
839 .offset = offsetof(IntelHDAState, st[_i].lpib), \
841 [ ST_REG(_i, ICH6_REG_SD_CBL) ] = { \
843 .name = _t stringify(_i) " CBL", \
845 .wmask = 0xffffffff, \
846 .offset = offsetof(IntelHDAState, st[_i].cbl), \
848 [ ST_REG(_i, ICH6_REG_SD_LVI) ] = { \
850 .name = _t stringify(_i) " LVI", \
853 .offset = offsetof(IntelHDAState, st[_i].lvi), \
855 [ ST_REG(_i, ICH6_REG_SD_FIFOSIZE) ] = { \
857 .name = _t stringify(_i) " FIFOS", \
859 .reset = HDA_BUFFER_SIZE, \
861 [ ST_REG(_i, ICH6_REG_SD_FORMAT) ] = { \
863 .name = _t stringify(_i) " FMT", \
866 .offset = offsetof(IntelHDAState, st[_i].fmt), \
868 [ ST_REG(_i, ICH6_REG_SD_BDLPL) ] = { \
870 .name = _t stringify(_i) " BDLPL", \
872 .wmask = 0xffffff80, \
873 .offset = offsetof(IntelHDAState, st[_i].bdlp_lbase), \
875 [ ST_REG(_i, ICH6_REG_SD_BDLPU) ] = { \
877 .name = _t stringify(_i) " BDLPU", \
879 .wmask = 0xffffffff, \
880 .offset = offsetof(IntelHDAState, st[_i].bdlp_ubase), \
895 static const IntelHDAReg
*intel_hda_reg_find(IntelHDAState
*d
, target_phys_addr_t addr
)
897 const IntelHDAReg
*reg
;
899 if (addr
>= sizeof(regtab
)/sizeof(regtab
[0])) {
903 if (reg
->name
== NULL
) {
909 dprint(d
, 1, "unknown register, addr 0x%x\n", (int) addr
);
913 static uint32_t *intel_hda_reg_addr(IntelHDAState
*d
, const IntelHDAReg
*reg
)
915 uint8_t *addr
= (void*)d
;
918 return (uint32_t*)addr
;
921 static void intel_hda_reg_write(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t val
,
932 time_t now
= time(NULL
);
933 if (d
->last_write
&& d
->last_reg
== reg
&& d
->last_val
== val
) {
935 if (d
->last_sec
!= now
) {
936 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
941 if (d
->repeat_count
) {
942 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
944 dprint(d
, 2, "write %-16s: 0x%x (%x)\n", reg
->name
, val
, wmask
);
952 assert(reg
->offset
!= 0);
954 addr
= intel_hda_reg_addr(d
, reg
);
959 wmask
<<= reg
->shift
;
963 *addr
|= wmask
& val
;
964 *addr
&= ~(val
& reg
->wclear
);
967 reg
->whandler(d
, reg
, old
);
971 static uint32_t intel_hda_reg_read(IntelHDAState
*d
, const IntelHDAReg
*reg
,
981 reg
->rhandler(d
, reg
);
984 if (reg
->offset
== 0) {
985 /* constant read-only register */
988 addr
= intel_hda_reg_addr(d
, reg
);
996 time_t now
= time(NULL
);
997 if (!d
->last_write
&& d
->last_reg
== reg
&& d
->last_val
== ret
) {
999 if (d
->last_sec
!= now
) {
1000 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
1002 d
->repeat_count
= 0;
1005 if (d
->repeat_count
) {
1006 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
1008 dprint(d
, 2, "read %-16s: 0x%x (%x)\n", reg
->name
, ret
, rmask
);
1013 d
->repeat_count
= 0;
1019 static void intel_hda_regs_reset(IntelHDAState
*d
)
1024 for (i
= 0; i
< sizeof(regtab
)/sizeof(regtab
[0]); i
++) {
1025 if (regtab
[i
].name
== NULL
) {
1028 if (regtab
[i
].offset
== 0) {
1031 addr
= intel_hda_reg_addr(d
, regtab
+ i
);
1032 *addr
= regtab
[i
].reset
;
1036 /* --------------------------------------------------------------------- */
1038 static void intel_hda_mmio_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1040 IntelHDAState
*d
= opaque
;
1041 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1043 intel_hda_reg_write(d
, reg
, val
, 0xff);
1046 static void intel_hda_mmio_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1048 IntelHDAState
*d
= opaque
;
1049 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1051 intel_hda_reg_write(d
, reg
, val
, 0xffff);
1054 static void intel_hda_mmio_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1056 IntelHDAState
*d
= opaque
;
1057 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1059 intel_hda_reg_write(d
, reg
, val
, 0xffffffff);
1062 static uint32_t intel_hda_mmio_readb(void *opaque
, target_phys_addr_t addr
)
1064 IntelHDAState
*d
= opaque
;
1065 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1067 return intel_hda_reg_read(d
, reg
, 0xff);
1070 static uint32_t intel_hda_mmio_readw(void *opaque
, target_phys_addr_t addr
)
1072 IntelHDAState
*d
= opaque
;
1073 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1075 return intel_hda_reg_read(d
, reg
, 0xffff);
1078 static uint32_t intel_hda_mmio_readl(void *opaque
, target_phys_addr_t addr
)
1080 IntelHDAState
*d
= opaque
;
1081 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1083 return intel_hda_reg_read(d
, reg
, 0xffffffff);
1086 static const MemoryRegionOps intel_hda_mmio_ops
= {
1089 intel_hda_mmio_readb
,
1090 intel_hda_mmio_readw
,
1091 intel_hda_mmio_readl
,
1094 intel_hda_mmio_writeb
,
1095 intel_hda_mmio_writew
,
1096 intel_hda_mmio_writel
,
1099 .endianness
= DEVICE_NATIVE_ENDIAN
,
1102 /* --------------------------------------------------------------------- */
1104 static void intel_hda_reset(DeviceState
*dev
)
1106 IntelHDAState
*d
= DO_UPCAST(IntelHDAState
, pci
.qdev
, dev
);
1108 HDACodecDevice
*cdev
;
1110 intel_hda_regs_reset(d
);
1111 d
->wall_base_ns
= qemu_get_clock_ns(vm_clock
);
1114 QTAILQ_FOREACH(qdev
, &d
->codecs
.qbus
.children
, sibling
) {
1115 cdev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
1116 device_reset(DEVICE(cdev
));
1117 d
->state_sts
|= (1 << cdev
->cad
);
1119 intel_hda_update_irq(d
);
1122 static int intel_hda_init(PCIDevice
*pci
)
1124 IntelHDAState
*d
= DO_UPCAST(IntelHDAState
, pci
, pci
);
1125 uint8_t *conf
= d
->pci
.config
;
1127 d
->name
= object_get_typename(OBJECT(d
));
1129 pci_config_set_interrupt_pin(conf
, 1);
1131 /* HDCTL off 0x40 bit 0 selects signaling mode (1-HDA, 0 - Ac97) 18.1.19 */
1134 memory_region_init_io(&d
->mmio
, &intel_hda_mmio_ops
, d
,
1135 "intel-hda", 0x4000);
1136 pci_register_bar(&d
->pci
, 0, 0, &d
->mmio
);
1138 msi_init(&d
->pci
, 0x50, 1, true, false);
1141 hda_codec_bus_init(&d
->pci
.qdev
, &d
->codecs
,
1142 intel_hda_response
, intel_hda_xfer
);
1147 static int intel_hda_exit(PCIDevice
*pci
)
1149 IntelHDAState
*d
= DO_UPCAST(IntelHDAState
, pci
, pci
);
1151 msi_uninit(&d
->pci
);
1152 memory_region_destroy(&d
->mmio
);
1156 static void intel_hda_write_config(PCIDevice
*pci
, uint32_t addr
,
1157 uint32_t val
, int len
)
1159 IntelHDAState
*d
= DO_UPCAST(IntelHDAState
, pci
, pci
);
1161 pci_default_write_config(pci
, addr
, val
, len
);
1163 msi_write_config(pci
, addr
, val
, len
);
1167 static int intel_hda_post_load(void *opaque
, int version
)
1169 IntelHDAState
* d
= opaque
;
1172 dprint(d
, 1, "%s\n", __FUNCTION__
);
1173 for (i
= 0; i
< ARRAY_SIZE(d
->st
); i
++) {
1174 if (d
->st
[i
].ctl
& 0x02) {
1175 intel_hda_parse_bdl(d
, &d
->st
[i
]);
1178 intel_hda_update_irq(d
);
1182 static const VMStateDescription vmstate_intel_hda_stream
= {
1183 .name
= "intel-hda-stream",
1185 .fields
= (VMStateField
[]) {
1186 VMSTATE_UINT32(ctl
, IntelHDAStream
),
1187 VMSTATE_UINT32(lpib
, IntelHDAStream
),
1188 VMSTATE_UINT32(cbl
, IntelHDAStream
),
1189 VMSTATE_UINT32(lvi
, IntelHDAStream
),
1190 VMSTATE_UINT32(fmt
, IntelHDAStream
),
1191 VMSTATE_UINT32(bdlp_lbase
, IntelHDAStream
),
1192 VMSTATE_UINT32(bdlp_ubase
, IntelHDAStream
),
1193 VMSTATE_END_OF_LIST()
1197 static const VMStateDescription vmstate_intel_hda
= {
1198 .name
= "intel-hda",
1200 .post_load
= intel_hda_post_load
,
1201 .fields
= (VMStateField
[]) {
1202 VMSTATE_PCI_DEVICE(pci
, IntelHDAState
),
1205 VMSTATE_UINT32(g_ctl
, IntelHDAState
),
1206 VMSTATE_UINT32(wake_en
, IntelHDAState
),
1207 VMSTATE_UINT32(state_sts
, IntelHDAState
),
1208 VMSTATE_UINT32(int_ctl
, IntelHDAState
),
1209 VMSTATE_UINT32(int_sts
, IntelHDAState
),
1210 VMSTATE_UINT32(wall_clk
, IntelHDAState
),
1211 VMSTATE_UINT32(corb_lbase
, IntelHDAState
),
1212 VMSTATE_UINT32(corb_ubase
, IntelHDAState
),
1213 VMSTATE_UINT32(corb_rp
, IntelHDAState
),
1214 VMSTATE_UINT32(corb_wp
, IntelHDAState
),
1215 VMSTATE_UINT32(corb_ctl
, IntelHDAState
),
1216 VMSTATE_UINT32(corb_sts
, IntelHDAState
),
1217 VMSTATE_UINT32(corb_size
, IntelHDAState
),
1218 VMSTATE_UINT32(rirb_lbase
, IntelHDAState
),
1219 VMSTATE_UINT32(rirb_ubase
, IntelHDAState
),
1220 VMSTATE_UINT32(rirb_wp
, IntelHDAState
),
1221 VMSTATE_UINT32(rirb_cnt
, IntelHDAState
),
1222 VMSTATE_UINT32(rirb_ctl
, IntelHDAState
),
1223 VMSTATE_UINT32(rirb_sts
, IntelHDAState
),
1224 VMSTATE_UINT32(rirb_size
, IntelHDAState
),
1225 VMSTATE_UINT32(dp_lbase
, IntelHDAState
),
1226 VMSTATE_UINT32(dp_ubase
, IntelHDAState
),
1227 VMSTATE_UINT32(icw
, IntelHDAState
),
1228 VMSTATE_UINT32(irr
, IntelHDAState
),
1229 VMSTATE_UINT32(ics
, IntelHDAState
),
1230 VMSTATE_STRUCT_ARRAY(st
, IntelHDAState
, 8, 0,
1231 vmstate_intel_hda_stream
,
1234 /* additional state info */
1235 VMSTATE_UINT32(rirb_count
, IntelHDAState
),
1236 VMSTATE_INT64(wall_base_ns
, IntelHDAState
),
1238 VMSTATE_END_OF_LIST()
1242 static Property intel_hda_properties
[] = {
1243 DEFINE_PROP_UINT32("debug", IntelHDAState
, debug
, 0),
1244 DEFINE_PROP_UINT32("msi", IntelHDAState
, msi
, 1),
1245 DEFINE_PROP_END_OF_LIST(),
1248 static void intel_hda_class_init(ObjectClass
*klass
, void *data
)
1250 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1251 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1253 k
->init
= intel_hda_init
;
1254 k
->exit
= intel_hda_exit
;
1255 k
->config_write
= intel_hda_write_config
;
1256 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
1257 k
->device_id
= 0x2668;
1259 k
->class_id
= PCI_CLASS_MULTIMEDIA_HD_AUDIO
;
1260 dc
->desc
= "Intel HD Audio Controller";
1261 dc
->reset
= intel_hda_reset
;
1262 dc
->vmsd
= &vmstate_intel_hda
;
1263 dc
->props
= intel_hda_properties
;
1266 static TypeInfo intel_hda_info
= {
1267 .name
= "intel-hda",
1268 .parent
= TYPE_PCI_DEVICE
,
1269 .instance_size
= sizeof(IntelHDAState
),
1270 .class_init
= intel_hda_class_init
,
1273 static void hda_codec_device_class_init(ObjectClass
*klass
, void *data
)
1275 DeviceClass
*k
= DEVICE_CLASS(klass
);
1276 k
->init
= hda_codec_dev_init
;
1277 k
->exit
= hda_codec_dev_exit
;
1278 k
->bus_info
= &hda_codec_bus_info
;
1281 static TypeInfo hda_codec_device_type_info
= {
1282 .name
= TYPE_HDA_CODEC_DEVICE
,
1283 .parent
= TYPE_DEVICE
,
1284 .instance_size
= sizeof(HDACodecDevice
),
1286 .class_size
= sizeof(HDACodecDeviceClass
),
1287 .class_init
= hda_codec_device_class_init
,
1290 static void intel_hda_register_types(void)
1292 type_register_static(&intel_hda_info
);
1293 type_register_static(&hda_codec_device_type_info
);
1296 type_init(intel_hda_register_types
)
1299 * create intel hda controller with codec attached to it,
1300 * so '-soundhw hda' works.
1302 int intel_hda_and_codec_init(PCIBus
*bus
)
1304 PCIDevice
*controller
;
1308 controller
= pci_create_simple(bus
, -1, "intel-hda");
1309 hdabus
= QLIST_FIRST(&controller
->qdev
.child_bus
);
1310 codec
= qdev_create(hdabus
, "hda-duplex");
1311 qdev_init_nofail(codec
);