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
, DeviceInfo
*base
)
52 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, qdev
->parent_bus
);
53 HDACodecDevice
*dev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
54 HDACodecDeviceInfo
*info
= DO_UPCAST(HDACodecDeviceInfo
, qdev
, base
);
58 dev
->cad
= bus
->next_cad
;
63 bus
->next_cad
= dev
->cad
+ 1;
64 return info
->init(dev
);
67 static int hda_codec_dev_exit(DeviceState
*qdev
)
69 HDACodecDevice
*dev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
71 if (dev
->info
->exit
) {
77 void hda_codec_register(HDACodecDeviceInfo
*info
)
79 info
->qdev
.init
= hda_codec_dev_init
;
80 info
->qdev
.exit
= hda_codec_dev_exit
;
81 info
->qdev
.bus_info
= &hda_codec_bus_info
;
82 qdev_register(&info
->qdev
);
85 HDACodecDevice
*hda_codec_find(HDACodecBus
*bus
, uint32_t cad
)
90 QTAILQ_FOREACH(qdev
, &bus
->qbus
.children
, sibling
) {
91 cdev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
92 if (cdev
->cad
== cad
) {
99 void hda_codec_response(HDACodecDevice
*dev
, bool solicited
, uint32_t response
)
101 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
102 bus
->response(dev
, solicited
, response
);
105 bool hda_codec_xfer(HDACodecDevice
*dev
, uint32_t stnr
, bool output
,
106 uint8_t *buf
, uint32_t len
)
108 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
109 return bus
->xfer(dev
, stnr
, output
, buf
, len
);
112 /* --------------------------------------------------------------------- */
113 /* intel hda emulation */
115 typedef struct IntelHDAStream IntelHDAStream
;
116 typedef struct IntelHDAState IntelHDAState
;
117 typedef struct IntelHDAReg IntelHDAReg
;
125 struct IntelHDAStream
{
138 uint32_t bsize
, be
, bp
;
141 struct IntelHDAState
{
178 IntelHDAStream st
[8];
183 int64_t wall_base_ns
;
186 const IntelHDAReg
*last_reg
;
190 uint32_t repeat_count
;
198 const char *name
; /* register name */
199 uint32_t size
; /* size in bytes */
200 uint32_t reset
; /* reset value */
201 uint32_t wmask
; /* write mask */
202 uint32_t wclear
; /* write 1 to clear bits */
203 uint32_t offset
; /* location in IntelHDAState */
204 uint32_t shift
; /* byte access entries for dwords */
206 void (*whandler
)(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
);
207 void (*rhandler
)(IntelHDAState
*d
, const IntelHDAReg
*reg
);
210 static void intel_hda_reset(DeviceState
*dev
);
212 /* --------------------------------------------------------------------- */
214 static target_phys_addr_t
intel_hda_addr(uint32_t lbase
, uint32_t ubase
)
216 target_phys_addr_t addr
;
218 #if TARGET_PHYS_ADDR_BITS == 32
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 int msi
= d
->msi
&& msi_enabled(&d
->pci
);
265 intel_hda_update_int_sts(d
);
266 if (d
->int_sts
& (1 << 31) && d
->int_ctl
& (1 << 31)) {
271 dprint(d
, 2, "%s: level %d [%s]\n", __FUNCTION__
,
272 level
, msi
? "msi" : "intx");
275 msi_notify(&d
->pci
, 0);
278 qemu_set_irq(d
->pci
.irq
[0], level
);
282 static int intel_hda_send_command(IntelHDAState
*d
, uint32_t verb
)
284 uint32_t cad
, nid
, data
;
285 HDACodecDevice
*codec
;
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", __FUNCTION__
);
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", __FUNCTION__
);
301 codec
->info
->command(codec
, nid
, data
);
305 static void intel_hda_corb_run(IntelHDAState
*d
)
307 target_phys_addr_t addr
;
310 if (d
->ics
& ICH6_IRS_BUSY
) {
311 dprint(d
, 2, "%s: [icw] verb 0x%08x\n", __FUNCTION__
, d
->icw
);
312 intel_hda_send_command(d
, d
->icw
);
317 if (!(d
->corb_ctl
& ICH6_CORBCTL_RUN
)) {
318 dprint(d
, 2, "%s: !run\n", __FUNCTION__
);
321 if ((d
->corb_rp
& 0xff) == d
->corb_wp
) {
322 dprint(d
, 2, "%s: corb ring empty\n", __FUNCTION__
);
325 if (d
->rirb_count
== d
->rirb_cnt
) {
326 dprint(d
, 2, "%s: rirb count reached\n", __FUNCTION__
);
330 rp
= (d
->corb_rp
+ 1) & 0xff;
331 addr
= intel_hda_addr(d
->corb_lbase
, d
->corb_ubase
);
332 verb
= ldl_le_pci_dma(&d
->pci
, addr
+ 4*rp
);
335 dprint(d
, 2, "%s: [rp 0x%x] verb 0x%08x\n", __FUNCTION__
, rp
, verb
);
336 intel_hda_send_command(d
, verb
);
340 static void intel_hda_response(HDACodecDevice
*dev
, bool solicited
, uint32_t response
)
342 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
343 IntelHDAState
*d
= container_of(bus
, IntelHDAState
, codecs
);
344 target_phys_addr_t addr
;
347 if (d
->ics
& ICH6_IRS_BUSY
) {
348 dprint(d
, 2, "%s: [irr] response 0x%x, cad 0x%x\n",
349 __FUNCTION__
, response
, dev
->cad
);
351 d
->ics
&= ~(ICH6_IRS_BUSY
| 0xf0);
352 d
->ics
|= (ICH6_IRS_VALID
| (dev
->cad
<< 4));
356 if (!(d
->rirb_ctl
& ICH6_RBCTL_DMA_EN
)) {
357 dprint(d
, 1, "%s: rirb dma disabled, drop codec response\n", __FUNCTION__
);
361 ex
= (solicited
? 0 : (1 << 4)) | dev
->cad
;
362 wp
= (d
->rirb_wp
+ 1) & 0xff;
363 addr
= intel_hda_addr(d
->rirb_lbase
, d
->rirb_ubase
);
364 stl_le_pci_dma(&d
->pci
, addr
+ 8*wp
, response
);
365 stl_le_pci_dma(&d
->pci
, addr
+ 8*wp
+ 4, ex
);
368 dprint(d
, 2, "%s: [wp 0x%x] response 0x%x, extra 0x%x\n",
369 __FUNCTION__
, wp
, response
, ex
);
372 if (d
->rirb_count
== d
->rirb_cnt
) {
373 dprint(d
, 2, "%s: rirb count reached (%d)\n", __FUNCTION__
, d
->rirb_count
);
374 if (d
->rirb_ctl
& ICH6_RBCTL_IRQ_EN
) {
375 d
->rirb_sts
|= ICH6_RBSTS_IRQ
;
376 intel_hda_update_irq(d
);
378 } else if ((d
->corb_rp
& 0xff) == d
->corb_wp
) {
379 dprint(d
, 2, "%s: corb ring empty (%d/%d)\n", __FUNCTION__
,
380 d
->rirb_count
, d
->rirb_cnt
);
381 if (d
->rirb_ctl
& ICH6_RBCTL_IRQ_EN
) {
382 d
->rirb_sts
|= ICH6_RBSTS_IRQ
;
383 intel_hda_update_irq(d
);
388 static bool intel_hda_xfer(HDACodecDevice
*dev
, uint32_t stnr
, bool output
,
389 uint8_t *buf
, uint32_t len
)
391 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
392 IntelHDAState
*d
= container_of(bus
, IntelHDAState
, codecs
);
393 target_phys_addr_t addr
;
394 uint32_t s
, copy
, left
;
398 st
= output
? d
->st
+ 4 : d
->st
;
399 for (s
= 0; s
< 4; s
++) {
400 if (stnr
== ((st
[s
].ctl
>> 20) & 0x0f)) {
408 if (st
->bpl
== NULL
) {
411 if (st
->ctl
& (1 << 26)) {
413 * Wait with the next DMA xfer until the guest
414 * has acked the buffer completion interrupt
422 if (copy
> st
->bsize
- st
->lpib
)
423 copy
= st
->bsize
- st
->lpib
;
424 if (copy
> st
->bpl
[st
->be
].len
- st
->bp
)
425 copy
= st
->bpl
[st
->be
].len
- st
->bp
;
427 dprint(d
, 3, "dma: entry %d, pos %d/%d, copy %d\n",
428 st
->be
, st
->bp
, st
->bpl
[st
->be
].len
, copy
);
430 pci_dma_rw(&d
->pci
, st
->bpl
[st
->be
].addr
+ st
->bp
, buf
, copy
, !output
);
436 if (st
->bpl
[st
->be
].len
== st
->bp
) {
437 /* bpl entry filled */
438 if (st
->bpl
[st
->be
].flags
& 0x01) {
443 if (st
->be
== st
->bentries
) {
444 /* bpl wrap around */
450 if (d
->dp_lbase
& 0x01) {
451 addr
= intel_hda_addr(d
->dp_lbase
& ~0x01, d
->dp_ubase
);
452 stl_le_pci_dma(&d
->pci
, addr
+ 8*s
, st
->lpib
);
454 dprint(d
, 3, "dma: --\n");
457 st
->ctl
|= (1 << 26); /* buffer completion interrupt */
458 intel_hda_update_irq(d
);
463 static void intel_hda_parse_bdl(IntelHDAState
*d
, IntelHDAStream
*st
)
465 target_phys_addr_t addr
;
469 addr
= intel_hda_addr(st
->bdlp_lbase
, st
->bdlp_ubase
);
470 st
->bentries
= st
->lvi
+1;
472 st
->bpl
= g_malloc(sizeof(bpl
) * st
->bentries
);
473 for (i
= 0; i
< st
->bentries
; i
++, addr
+= 16) {
474 pci_dma_read(&d
->pci
, addr
, buf
, 16);
475 st
->bpl
[i
].addr
= le64_to_cpu(*(uint64_t *)buf
);
476 st
->bpl
[i
].len
= le32_to_cpu(*(uint32_t *)(buf
+ 8));
477 st
->bpl
[i
].flags
= le32_to_cpu(*(uint32_t *)(buf
+ 12));
478 dprint(d
, 1, "bdl/%d: 0x%" PRIx64
" +0x%x, 0x%x\n",
479 i
, st
->bpl
[i
].addr
, st
->bpl
[i
].len
, st
->bpl
[i
].flags
);
488 static void intel_hda_notify_codecs(IntelHDAState
*d
, uint32_t stream
, bool running
, bool output
)
491 HDACodecDevice
*cdev
;
493 QTAILQ_FOREACH(qdev
, &d
->codecs
.qbus
.children
, sibling
) {
494 cdev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
495 if (cdev
->info
->stream
) {
496 cdev
->info
->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(&d
->pci
.qdev
);
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_get_clock_ns(vm_clock
) - 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
);
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, \
832 [ ST_REG(_i, ICH6_REG_SD_LPIB) ] = { \
834 .name = _t stringify(_i) " LPIB", \
836 .offset = offsetof(IntelHDAState, st[_i].lpib), \
838 [ ST_REG(_i, ICH6_REG_SD_LPIB) + 0x2000 ] = { \
840 .name = _t stringify(_i) " LPIB(alias)", \
842 .offset = offsetof(IntelHDAState, st[_i].lpib), \
844 [ ST_REG(_i, ICH6_REG_SD_CBL) ] = { \
846 .name = _t stringify(_i) " CBL", \
848 .wmask = 0xffffffff, \
849 .offset = offsetof(IntelHDAState, st[_i].cbl), \
851 [ ST_REG(_i, ICH6_REG_SD_LVI) ] = { \
853 .name = _t stringify(_i) " LVI", \
856 .offset = offsetof(IntelHDAState, st[_i].lvi), \
858 [ ST_REG(_i, ICH6_REG_SD_FIFOSIZE) ] = { \
860 .name = _t stringify(_i) " FIFOS", \
862 .reset = HDA_BUFFER_SIZE, \
864 [ ST_REG(_i, ICH6_REG_SD_FORMAT) ] = { \
866 .name = _t stringify(_i) " FMT", \
869 .offset = offsetof(IntelHDAState, st[_i].fmt), \
871 [ ST_REG(_i, ICH6_REG_SD_BDLPL) ] = { \
873 .name = _t stringify(_i) " BDLPL", \
875 .wmask = 0xffffff80, \
876 .offset = offsetof(IntelHDAState, st[_i].bdlp_lbase), \
878 [ ST_REG(_i, ICH6_REG_SD_BDLPU) ] = { \
880 .name = _t stringify(_i) " BDLPU", \
882 .wmask = 0xffffffff, \
883 .offset = offsetof(IntelHDAState, st[_i].bdlp_ubase), \
898 static const IntelHDAReg
*intel_hda_reg_find(IntelHDAState
*d
, target_phys_addr_t addr
)
900 const IntelHDAReg
*reg
;
902 if (addr
>= sizeof(regtab
)/sizeof(regtab
[0])) {
906 if (reg
->name
== NULL
) {
912 dprint(d
, 1, "unknown register, addr 0x%x\n", (int) addr
);
916 static uint32_t *intel_hda_reg_addr(IntelHDAState
*d
, const IntelHDAReg
*reg
)
918 uint8_t *addr
= (void*)d
;
921 return (uint32_t*)addr
;
924 static void intel_hda_reg_write(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t val
,
935 time_t now
= time(NULL
);
936 if (d
->last_write
&& d
->last_reg
== reg
&& d
->last_val
== val
) {
938 if (d
->last_sec
!= now
) {
939 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
944 if (d
->repeat_count
) {
945 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
947 dprint(d
, 2, "write %-16s: 0x%x (%x)\n", reg
->name
, val
, wmask
);
955 assert(reg
->offset
!= 0);
957 addr
= intel_hda_reg_addr(d
, reg
);
962 wmask
<<= reg
->shift
;
966 *addr
|= wmask
& val
;
967 *addr
&= ~(val
& reg
->wclear
);
970 reg
->whandler(d
, reg
, old
);
974 static uint32_t intel_hda_reg_read(IntelHDAState
*d
, const IntelHDAReg
*reg
,
984 reg
->rhandler(d
, reg
);
987 if (reg
->offset
== 0) {
988 /* constant read-only register */
991 addr
= intel_hda_reg_addr(d
, reg
);
999 time_t now
= time(NULL
);
1000 if (!d
->last_write
&& d
->last_reg
== reg
&& d
->last_val
== ret
) {
1002 if (d
->last_sec
!= now
) {
1003 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
1005 d
->repeat_count
= 0;
1008 if (d
->repeat_count
) {
1009 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
1011 dprint(d
, 2, "read %-16s: 0x%x (%x)\n", reg
->name
, ret
, rmask
);
1016 d
->repeat_count
= 0;
1022 static void intel_hda_regs_reset(IntelHDAState
*d
)
1027 for (i
= 0; i
< sizeof(regtab
)/sizeof(regtab
[0]); i
++) {
1028 if (regtab
[i
].name
== NULL
) {
1031 if (regtab
[i
].offset
== 0) {
1034 addr
= intel_hda_reg_addr(d
, regtab
+ i
);
1035 *addr
= regtab
[i
].reset
;
1039 /* --------------------------------------------------------------------- */
1041 static void intel_hda_mmio_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1043 IntelHDAState
*d
= opaque
;
1044 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1046 intel_hda_reg_write(d
, reg
, val
, 0xff);
1049 static void intel_hda_mmio_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1051 IntelHDAState
*d
= opaque
;
1052 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1054 intel_hda_reg_write(d
, reg
, val
, 0xffff);
1057 static void intel_hda_mmio_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1059 IntelHDAState
*d
= opaque
;
1060 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1062 intel_hda_reg_write(d
, reg
, val
, 0xffffffff);
1065 static uint32_t intel_hda_mmio_readb(void *opaque
, target_phys_addr_t addr
)
1067 IntelHDAState
*d
= opaque
;
1068 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1070 return intel_hda_reg_read(d
, reg
, 0xff);
1073 static uint32_t intel_hda_mmio_readw(void *opaque
, target_phys_addr_t addr
)
1075 IntelHDAState
*d
= opaque
;
1076 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1078 return intel_hda_reg_read(d
, reg
, 0xffff);
1081 static uint32_t intel_hda_mmio_readl(void *opaque
, target_phys_addr_t addr
)
1083 IntelHDAState
*d
= opaque
;
1084 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1086 return intel_hda_reg_read(d
, reg
, 0xffffffff);
1089 static const MemoryRegionOps intel_hda_mmio_ops
= {
1092 intel_hda_mmio_readb
,
1093 intel_hda_mmio_readw
,
1094 intel_hda_mmio_readl
,
1097 intel_hda_mmio_writeb
,
1098 intel_hda_mmio_writew
,
1099 intel_hda_mmio_writel
,
1102 .endianness
= DEVICE_NATIVE_ENDIAN
,
1105 /* --------------------------------------------------------------------- */
1107 static void intel_hda_reset(DeviceState
*dev
)
1109 IntelHDAState
*d
= DO_UPCAST(IntelHDAState
, pci
.qdev
, dev
);
1111 HDACodecDevice
*cdev
;
1113 intel_hda_regs_reset(d
);
1114 d
->wall_base_ns
= qemu_get_clock_ns(vm_clock
);
1117 QTAILQ_FOREACH(qdev
, &d
->codecs
.qbus
.children
, sibling
) {
1118 cdev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
1119 if (qdev
->info
->reset
) {
1120 qdev
->info
->reset(qdev
);
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
= d
->pci
.qdev
.info
->name
;
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 int intel_hda_exit(PCIDevice
*pci
)
1154 IntelHDAState
*d
= DO_UPCAST(IntelHDAState
, pci
, pci
);
1156 msi_uninit(&d
->pci
);
1157 memory_region_destroy(&d
->mmio
);
1161 static void intel_hda_write_config(PCIDevice
*pci
, uint32_t addr
,
1162 uint32_t val
, int len
)
1164 IntelHDAState
*d
= DO_UPCAST(IntelHDAState
, pci
, pci
);
1166 pci_default_write_config(pci
, addr
, val
, len
);
1168 msi_write_config(pci
, addr
, val
, len
);
1172 static int intel_hda_post_load(void *opaque
, int version
)
1174 IntelHDAState
* d
= opaque
;
1177 dprint(d
, 1, "%s\n", __FUNCTION__
);
1178 for (i
= 0; i
< ARRAY_SIZE(d
->st
); i
++) {
1179 if (d
->st
[i
].ctl
& 0x02) {
1180 intel_hda_parse_bdl(d
, &d
->st
[i
]);
1183 intel_hda_update_irq(d
);
1187 static const VMStateDescription vmstate_intel_hda_stream
= {
1188 .name
= "intel-hda-stream",
1190 .fields
= (VMStateField
[]) {
1191 VMSTATE_UINT32(ctl
, IntelHDAStream
),
1192 VMSTATE_UINT32(lpib
, IntelHDAStream
),
1193 VMSTATE_UINT32(cbl
, IntelHDAStream
),
1194 VMSTATE_UINT32(lvi
, IntelHDAStream
),
1195 VMSTATE_UINT32(fmt
, IntelHDAStream
),
1196 VMSTATE_UINT32(bdlp_lbase
, IntelHDAStream
),
1197 VMSTATE_UINT32(bdlp_ubase
, IntelHDAStream
),
1198 VMSTATE_END_OF_LIST()
1202 static const VMStateDescription vmstate_intel_hda
= {
1203 .name
= "intel-hda",
1205 .post_load
= intel_hda_post_load
,
1206 .fields
= (VMStateField
[]) {
1207 VMSTATE_PCI_DEVICE(pci
, IntelHDAState
),
1210 VMSTATE_UINT32(g_ctl
, IntelHDAState
),
1211 VMSTATE_UINT32(wake_en
, IntelHDAState
),
1212 VMSTATE_UINT32(state_sts
, IntelHDAState
),
1213 VMSTATE_UINT32(int_ctl
, IntelHDAState
),
1214 VMSTATE_UINT32(int_sts
, IntelHDAState
),
1215 VMSTATE_UINT32(wall_clk
, IntelHDAState
),
1216 VMSTATE_UINT32(corb_lbase
, IntelHDAState
),
1217 VMSTATE_UINT32(corb_ubase
, IntelHDAState
),
1218 VMSTATE_UINT32(corb_rp
, IntelHDAState
),
1219 VMSTATE_UINT32(corb_wp
, IntelHDAState
),
1220 VMSTATE_UINT32(corb_ctl
, IntelHDAState
),
1221 VMSTATE_UINT32(corb_sts
, IntelHDAState
),
1222 VMSTATE_UINT32(corb_size
, IntelHDAState
),
1223 VMSTATE_UINT32(rirb_lbase
, IntelHDAState
),
1224 VMSTATE_UINT32(rirb_ubase
, IntelHDAState
),
1225 VMSTATE_UINT32(rirb_wp
, IntelHDAState
),
1226 VMSTATE_UINT32(rirb_cnt
, IntelHDAState
),
1227 VMSTATE_UINT32(rirb_ctl
, IntelHDAState
),
1228 VMSTATE_UINT32(rirb_sts
, IntelHDAState
),
1229 VMSTATE_UINT32(rirb_size
, IntelHDAState
),
1230 VMSTATE_UINT32(dp_lbase
, IntelHDAState
),
1231 VMSTATE_UINT32(dp_ubase
, IntelHDAState
),
1232 VMSTATE_UINT32(icw
, IntelHDAState
),
1233 VMSTATE_UINT32(irr
, IntelHDAState
),
1234 VMSTATE_UINT32(ics
, IntelHDAState
),
1235 VMSTATE_STRUCT_ARRAY(st
, IntelHDAState
, 8, 0,
1236 vmstate_intel_hda_stream
,
1239 /* additional state info */
1240 VMSTATE_UINT32(rirb_count
, IntelHDAState
),
1241 VMSTATE_INT64(wall_base_ns
, IntelHDAState
),
1243 VMSTATE_END_OF_LIST()
1247 static PCIDeviceInfo intel_hda_info
= {
1248 .qdev
.name
= "intel-hda",
1249 .qdev
.desc
= "Intel HD Audio Controller",
1250 .qdev
.size
= sizeof(IntelHDAState
),
1251 .qdev
.vmsd
= &vmstate_intel_hda
,
1252 .qdev
.reset
= intel_hda_reset
,
1253 .init
= intel_hda_init
,
1254 .exit
= intel_hda_exit
,
1255 .config_write
= intel_hda_write_config
,
1256 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1257 .device_id
= 0x2668,
1259 .class_id
= PCI_CLASS_MULTIMEDIA_HD_AUDIO
,
1260 .qdev
.props
= (Property
[]) {
1261 DEFINE_PROP_UINT32("debug", IntelHDAState
, debug
, 0),
1262 DEFINE_PROP_UINT32("msi", IntelHDAState
, msi
, 1),
1263 DEFINE_PROP_END_OF_LIST(),
1267 static void intel_hda_register(void)
1269 pci_qdev_register(&intel_hda_info
);
1271 device_init(intel_hda_register
);
1274 * create intel hda controller with codec attached to it,
1275 * so '-soundhw hda' works.
1277 int intel_hda_and_codec_init(PCIBus
*bus
)
1279 PCIDevice
*controller
;
1283 controller
= pci_create_simple(bus
, -1, "intel-hda");
1284 hdabus
= QLIST_FIRST(&controller
->qdev
.child_bus
);
1285 codec
= qdev_create(hdabus
, "hda-duplex");
1286 qdev_init_nofail(codec
);