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"
28 /* --------------------------------------------------------------------- */
31 static struct BusInfo hda_codec_bus_info
= {
33 .size
= sizeof(HDACodecBus
),
34 .props
= (Property
[]) {
35 DEFINE_PROP_UINT32("cad", HDACodecDevice
, cad
, -1),
36 DEFINE_PROP_END_OF_LIST()
40 void hda_codec_bus_init(DeviceState
*dev
, HDACodecBus
*bus
,
41 hda_codec_response_func response
,
42 hda_codec_xfer_func xfer
)
44 qbus_create_inplace(&bus
->qbus
, &hda_codec_bus_info
, dev
, NULL
);
45 bus
->response
= response
;
49 static int hda_codec_dev_init(DeviceState
*qdev
, DeviceInfo
*base
)
51 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, qdev
->parent_bus
);
52 HDACodecDevice
*dev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
53 HDACodecDeviceInfo
*info
= DO_UPCAST(HDACodecDeviceInfo
, qdev
, base
);
57 dev
->cad
= bus
->next_cad
;
62 bus
->next_cad
= dev
->cad
+ 1;
63 return info
->init(dev
);
66 static int hda_codec_dev_exit(DeviceState
*qdev
)
68 HDACodecDevice
*dev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
70 if (dev
->info
->exit
) {
76 void hda_codec_register(HDACodecDeviceInfo
*info
)
78 info
->qdev
.init
= hda_codec_dev_init
;
79 info
->qdev
.exit
= hda_codec_dev_exit
;
80 info
->qdev
.bus_info
= &hda_codec_bus_info
;
81 qdev_register(&info
->qdev
);
84 HDACodecDevice
*hda_codec_find(HDACodecBus
*bus
, uint32_t cad
)
89 QLIST_FOREACH(qdev
, &bus
->qbus
.children
, sibling
) {
90 cdev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
91 if (cdev
->cad
== cad
) {
98 void hda_codec_response(HDACodecDevice
*dev
, bool solicited
, uint32_t response
)
100 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
101 bus
->response(dev
, solicited
, response
);
104 bool hda_codec_xfer(HDACodecDevice
*dev
, uint32_t stnr
, bool output
,
105 uint8_t *buf
, uint32_t len
)
107 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
108 return bus
->xfer(dev
, stnr
, output
, buf
, len
);
111 /* --------------------------------------------------------------------- */
112 /* intel hda emulation */
114 typedef struct IntelHDAStream IntelHDAStream
;
115 typedef struct IntelHDAState IntelHDAState
;
116 typedef struct IntelHDAReg IntelHDAReg
;
124 struct IntelHDAStream
{
137 uint32_t bsize
, be
, bp
;
140 struct IntelHDAState
{
177 IntelHDAStream st
[8];
182 int64_t wall_base_ns
;
185 const IntelHDAReg
*last_reg
;
189 uint32_t repeat_count
;
197 const char *name
; /* register name */
198 uint32_t size
; /* size in bytes */
199 uint32_t reset
; /* reset value */
200 uint32_t wmask
; /* write mask */
201 uint32_t wclear
; /* write 1 to clear bits */
202 uint32_t offset
; /* location in IntelHDAState */
203 uint32_t shift
; /* byte access entries for dwords */
205 void (*whandler
)(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
);
206 void (*rhandler
)(IntelHDAState
*d
, const IntelHDAReg
*reg
);
209 static void intel_hda_reset(DeviceState
*dev
);
211 /* --------------------------------------------------------------------- */
213 static target_phys_addr_t
intel_hda_addr(uint32_t lbase
, uint32_t ubase
)
215 target_phys_addr_t addr
;
217 #if TARGET_PHYS_ADDR_BITS == 32
227 static void stl_phys_le(target_phys_addr_t addr
, uint32_t value
)
229 uint32_t value_le
= cpu_to_le32(value
);
230 cpu_physical_memory_write(addr
, (uint8_t*)(&value_le
), sizeof(value_le
));
233 static uint32_t ldl_phys_le(target_phys_addr_t addr
)
236 cpu_physical_memory_read(addr
, (uint8_t*)(&value_le
), sizeof(value_le
));
237 return le32_to_cpu(value_le
);
240 static void intel_hda_update_int_sts(IntelHDAState
*d
)
245 /* update controller status */
246 if (d
->rirb_sts
& ICH6_RBSTS_IRQ
) {
249 if (d
->rirb_sts
& ICH6_RBSTS_OVERRUN
) {
252 if (d
->state_sts
& d
->wake_en
) {
256 /* update stream status */
257 for (i
= 0; i
< 8; i
++) {
258 /* buffer completion interrupt */
259 if (d
->st
[i
].ctl
& (1 << 26)) {
264 /* update global status */
265 if (sts
& d
->int_ctl
) {
272 static void intel_hda_update_irq(IntelHDAState
*d
)
274 int msi
= d
->msi
&& msi_enabled(&d
->pci
);
277 intel_hda_update_int_sts(d
);
278 if (d
->int_sts
& (1 << 31) && d
->int_ctl
& (1 << 31)) {
283 dprint(d
, 2, "%s: level %d [%s]\n", __FUNCTION__
,
284 level
, msi
? "msi" : "intx");
287 msi_notify(&d
->pci
, 0);
290 qemu_set_irq(d
->pci
.irq
[0], level
);
294 static int intel_hda_send_command(IntelHDAState
*d
, uint32_t verb
)
296 uint32_t cad
, nid
, data
;
297 HDACodecDevice
*codec
;
299 cad
= (verb
>> 28) & 0x0f;
300 if (verb
& (1 << 27)) {
301 /* indirect node addressing, not specified in HDA 1.0 */
302 dprint(d
, 1, "%s: indirect node addressing (guest bug?)\n", __FUNCTION__
);
305 nid
= (verb
>> 20) & 0x7f;
306 data
= verb
& 0xfffff;
308 codec
= hda_codec_find(&d
->codecs
, cad
);
310 dprint(d
, 1, "%s: addressed non-existing codec\n", __FUNCTION__
);
313 codec
->info
->command(codec
, nid
, data
);
317 static void intel_hda_corb_run(IntelHDAState
*d
)
319 target_phys_addr_t addr
;
322 if (d
->ics
& ICH6_IRS_BUSY
) {
323 dprint(d
, 2, "%s: [icw] verb 0x%08x\n", __FUNCTION__
, d
->icw
);
324 intel_hda_send_command(d
, d
->icw
);
329 if (!(d
->corb_ctl
& ICH6_CORBCTL_RUN
)) {
330 dprint(d
, 2, "%s: !run\n", __FUNCTION__
);
333 if ((d
->corb_rp
& 0xff) == d
->corb_wp
) {
334 dprint(d
, 2, "%s: corb ring empty\n", __FUNCTION__
);
337 if (d
->rirb_count
== d
->rirb_cnt
) {
338 dprint(d
, 2, "%s: rirb count reached\n", __FUNCTION__
);
342 rp
= (d
->corb_rp
+ 1) & 0xff;
343 addr
= intel_hda_addr(d
->corb_lbase
, d
->corb_ubase
);
344 verb
= ldl_phys_le(addr
+ 4*rp
);
347 dprint(d
, 2, "%s: [rp 0x%x] verb 0x%08x\n", __FUNCTION__
, rp
, verb
);
348 intel_hda_send_command(d
, verb
);
352 static void intel_hda_response(HDACodecDevice
*dev
, bool solicited
, uint32_t response
)
354 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
355 IntelHDAState
*d
= container_of(bus
, IntelHDAState
, codecs
);
356 target_phys_addr_t addr
;
359 if (d
->ics
& ICH6_IRS_BUSY
) {
360 dprint(d
, 2, "%s: [irr] response 0x%x, cad 0x%x\n",
361 __FUNCTION__
, response
, dev
->cad
);
363 d
->ics
&= ~(ICH6_IRS_BUSY
| 0xf0);
364 d
->ics
|= (ICH6_IRS_VALID
| (dev
->cad
<< 4));
368 if (!(d
->rirb_ctl
& ICH6_RBCTL_DMA_EN
)) {
369 dprint(d
, 1, "%s: rirb dma disabled, drop codec response\n", __FUNCTION__
);
373 ex
= (solicited
? 0 : (1 << 4)) | dev
->cad
;
374 wp
= (d
->rirb_wp
+ 1) & 0xff;
375 addr
= intel_hda_addr(d
->rirb_lbase
, d
->rirb_ubase
);
376 stl_phys_le(addr
+ 8*wp
, response
);
377 stl_phys_le(addr
+ 8*wp
+ 4, ex
);
380 dprint(d
, 2, "%s: [wp 0x%x] response 0x%x, extra 0x%x\n",
381 __FUNCTION__
, wp
, response
, ex
);
384 if (d
->rirb_count
== d
->rirb_cnt
) {
385 dprint(d
, 2, "%s: rirb count reached (%d)\n", __FUNCTION__
, d
->rirb_count
);
386 if (d
->rirb_ctl
& ICH6_RBCTL_IRQ_EN
) {
387 d
->rirb_sts
|= ICH6_RBSTS_IRQ
;
388 intel_hda_update_irq(d
);
390 } else if ((d
->corb_rp
& 0xff) == d
->corb_wp
) {
391 dprint(d
, 2, "%s: corb ring empty (%d/%d)\n", __FUNCTION__
,
392 d
->rirb_count
, d
->rirb_cnt
);
393 if (d
->rirb_ctl
& ICH6_RBCTL_IRQ_EN
) {
394 d
->rirb_sts
|= ICH6_RBSTS_IRQ
;
395 intel_hda_update_irq(d
);
400 static bool intel_hda_xfer(HDACodecDevice
*dev
, uint32_t stnr
, bool output
,
401 uint8_t *buf
, uint32_t len
)
403 HDACodecBus
*bus
= DO_UPCAST(HDACodecBus
, qbus
, dev
->qdev
.parent_bus
);
404 IntelHDAState
*d
= container_of(bus
, IntelHDAState
, codecs
);
405 IntelHDAStream
*st
= NULL
;
406 target_phys_addr_t addr
;
407 uint32_t s
, copy
, left
;
410 for (s
= 0; s
< ARRAY_SIZE(d
->st
); s
++) {
411 if (stnr
== ((d
->st
[s
].ctl
>> 20) & 0x0f)) {
419 if (st
->bpl
== NULL
) {
422 if (st
->ctl
& (1 << 26)) {
424 * Wait with the next DMA xfer until the guest
425 * has acked the buffer completion interrupt
433 if (copy
> st
->bsize
- st
->lpib
)
434 copy
= st
->bsize
- st
->lpib
;
435 if (copy
> st
->bpl
[st
->be
].len
- st
->bp
)
436 copy
= st
->bpl
[st
->be
].len
- st
->bp
;
438 dprint(d
, 3, "dma: entry %d, pos %d/%d, copy %d\n",
439 st
->be
, st
->bp
, st
->bpl
[st
->be
].len
, copy
);
441 cpu_physical_memory_rw(st
->bpl
[st
->be
].addr
+ st
->bp
,
448 if (st
->bpl
[st
->be
].len
== st
->bp
) {
449 /* bpl entry filled */
450 if (st
->bpl
[st
->be
].flags
& 0x01) {
455 if (st
->be
== st
->bentries
) {
456 /* bpl wrap around */
462 if (d
->dp_lbase
& 0x01) {
463 addr
= intel_hda_addr(d
->dp_lbase
& ~0x01, d
->dp_ubase
);
464 stl_phys_le(addr
+ 8*s
, st
->lpib
);
466 dprint(d
, 3, "dma: --\n");
469 st
->ctl
|= (1 << 26); /* buffer completion interrupt */
470 intel_hda_update_irq(d
);
475 static void intel_hda_parse_bdl(IntelHDAState
*d
, IntelHDAStream
*st
)
477 target_phys_addr_t addr
;
481 addr
= intel_hda_addr(st
->bdlp_lbase
, st
->bdlp_ubase
);
482 st
->bentries
= st
->lvi
+1;
484 st
->bpl
= qemu_malloc(sizeof(bpl
) * st
->bentries
);
485 for (i
= 0; i
< st
->bentries
; i
++, addr
+= 16) {
486 cpu_physical_memory_read(addr
, buf
, 16);
487 st
->bpl
[i
].addr
= le64_to_cpu(*(uint64_t *)buf
);
488 st
->bpl
[i
].len
= le32_to_cpu(*(uint32_t *)(buf
+ 8));
489 st
->bpl
[i
].flags
= le32_to_cpu(*(uint32_t *)(buf
+ 12));
490 dprint(d
, 1, "bdl/%d: 0x%" PRIx64
" +0x%x, 0x%x\n",
491 i
, st
->bpl
[i
].addr
, st
->bpl
[i
].len
, st
->bpl
[i
].flags
);
500 static void intel_hda_notify_codecs(IntelHDAState
*d
, uint32_t stream
, bool running
)
503 HDACodecDevice
*cdev
;
505 QLIST_FOREACH(qdev
, &d
->codecs
.qbus
.children
, sibling
) {
506 cdev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
507 if (cdev
->info
->stream
) {
508 cdev
->info
->stream(cdev
, stream
, running
);
513 /* --------------------------------------------------------------------- */
515 static void intel_hda_set_g_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
517 if ((d
->g_ctl
& ICH6_GCTL_RESET
) == 0) {
518 intel_hda_reset(&d
->pci
.qdev
);
522 static void intel_hda_set_wake_en(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
524 intel_hda_update_irq(d
);
527 static void intel_hda_set_state_sts(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
529 intel_hda_update_irq(d
);
532 static void intel_hda_set_int_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
534 intel_hda_update_irq(d
);
537 static void intel_hda_get_wall_clk(IntelHDAState
*d
, const IntelHDAReg
*reg
)
541 ns
= qemu_get_clock_ns(vm_clock
) - d
->wall_base_ns
;
542 d
->wall_clk
= (uint32_t)(ns
* 24 / 1000); /* 24 MHz */
545 static void intel_hda_set_corb_wp(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
547 intel_hda_corb_run(d
);
550 static void intel_hda_set_corb_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
552 intel_hda_corb_run(d
);
555 static void intel_hda_set_rirb_wp(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
557 if (d
->rirb_wp
& ICH6_RIRBWP_RST
) {
562 static void intel_hda_set_rirb_sts(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
564 intel_hda_update_irq(d
);
566 if ((old
& ICH6_RBSTS_IRQ
) && !(d
->rirb_sts
& ICH6_RBSTS_IRQ
)) {
567 /* cleared ICH6_RBSTS_IRQ */
569 intel_hda_corb_run(d
);
573 static void intel_hda_set_ics(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
575 if (d
->ics
& ICH6_IRS_BUSY
) {
576 intel_hda_corb_run(d
);
580 static void intel_hda_set_st_ctl(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t old
)
582 IntelHDAStream
*st
= d
->st
+ reg
->stream
;
584 if (st
->ctl
& 0x01) {
586 dprint(d
, 1, "st #%d: reset\n", reg
->stream
);
589 if ((st
->ctl
& 0x02) != (old
& 0x02)) {
590 uint32_t stnr
= (st
->ctl
>> 20) & 0x0f;
591 /* run bit flipped */
592 if (st
->ctl
& 0x02) {
594 dprint(d
, 1, "st #%d: start %d (ring buf %d bytes)\n",
595 reg
->stream
, stnr
, st
->cbl
);
596 intel_hda_parse_bdl(d
, st
);
597 intel_hda_notify_codecs(d
, stnr
, true);
600 dprint(d
, 1, "st #%d: stop %d\n", reg
->stream
, stnr
);
601 intel_hda_notify_codecs(d
, stnr
, false);
604 intel_hda_update_irq(d
);
607 /* --------------------------------------------------------------------- */
609 #define ST_REG(_n, _o) (0x80 + (_n) * 0x20 + (_o))
611 static const struct IntelHDAReg regtab
[] = {
613 [ ICH6_REG_GCAP
] = {
618 [ ICH6_REG_VMIN
] = {
622 [ ICH6_REG_VMAJ
] = {
627 [ ICH6_REG_OUTPAY
] = {
632 [ ICH6_REG_INPAY
] = {
637 [ ICH6_REG_GCTL
] = {
641 .offset
= offsetof(IntelHDAState
, g_ctl
),
642 .whandler
= intel_hda_set_g_ctl
,
644 [ ICH6_REG_WAKEEN
] = {
648 .offset
= offsetof(IntelHDAState
, wake_en
),
649 .whandler
= intel_hda_set_wake_en
,
651 [ ICH6_REG_STATESTS
] = {
656 .offset
= offsetof(IntelHDAState
, state_sts
),
657 .whandler
= intel_hda_set_state_sts
,
661 [ ICH6_REG_INTCTL
] = {
665 .offset
= offsetof(IntelHDAState
, int_ctl
),
666 .whandler
= intel_hda_set_int_ctl
,
668 [ ICH6_REG_INTSTS
] = {
672 .wclear
= 0xc00000ff,
673 .offset
= offsetof(IntelHDAState
, int_sts
),
677 [ ICH6_REG_WALLCLK
] = {
680 .offset
= offsetof(IntelHDAState
, wall_clk
),
681 .rhandler
= intel_hda_get_wall_clk
,
683 [ ICH6_REG_WALLCLK
+ 0x2000 ] = {
684 .name
= "WALLCLK(alias)",
686 .offset
= offsetof(IntelHDAState
, wall_clk
),
687 .rhandler
= intel_hda_get_wall_clk
,
691 [ ICH6_REG_CORBLBASE
] = {
695 .offset
= offsetof(IntelHDAState
, corb_lbase
),
697 [ ICH6_REG_CORBUBASE
] = {
701 .offset
= offsetof(IntelHDAState
, corb_ubase
),
703 [ ICH6_REG_CORBWP
] = {
707 .offset
= offsetof(IntelHDAState
, corb_wp
),
708 .whandler
= intel_hda_set_corb_wp
,
710 [ ICH6_REG_CORBRP
] = {
714 .offset
= offsetof(IntelHDAState
, corb_rp
),
716 [ ICH6_REG_CORBCTL
] = {
720 .offset
= offsetof(IntelHDAState
, corb_ctl
),
721 .whandler
= intel_hda_set_corb_ctl
,
723 [ ICH6_REG_CORBSTS
] = {
728 .offset
= offsetof(IntelHDAState
, corb_sts
),
730 [ ICH6_REG_CORBSIZE
] = {
734 .offset
= offsetof(IntelHDAState
, corb_size
),
736 [ ICH6_REG_RIRBLBASE
] = {
740 .offset
= offsetof(IntelHDAState
, rirb_lbase
),
742 [ ICH6_REG_RIRBUBASE
] = {
746 .offset
= offsetof(IntelHDAState
, rirb_ubase
),
748 [ ICH6_REG_RIRBWP
] = {
752 .offset
= offsetof(IntelHDAState
, rirb_wp
),
753 .whandler
= intel_hda_set_rirb_wp
,
755 [ ICH6_REG_RINTCNT
] = {
759 .offset
= offsetof(IntelHDAState
, rirb_cnt
),
761 [ ICH6_REG_RIRBCTL
] = {
765 .offset
= offsetof(IntelHDAState
, rirb_ctl
),
767 [ ICH6_REG_RIRBSTS
] = {
772 .offset
= offsetof(IntelHDAState
, rirb_sts
),
773 .whandler
= intel_hda_set_rirb_sts
,
775 [ ICH6_REG_RIRBSIZE
] = {
779 .offset
= offsetof(IntelHDAState
, rirb_size
),
782 [ ICH6_REG_DPLBASE
] = {
786 .offset
= offsetof(IntelHDAState
, dp_lbase
),
788 [ ICH6_REG_DPUBASE
] = {
792 .offset
= offsetof(IntelHDAState
, dp_ubase
),
799 .offset
= offsetof(IntelHDAState
, icw
),
804 .offset
= offsetof(IntelHDAState
, irr
),
811 .offset
= offsetof(IntelHDAState
, ics
),
812 .whandler
= intel_hda_set_ics
,
815 #define HDA_STREAM(_t, _i) \
816 [ ST_REG(_i, ICH6_REG_SD_CTL) ] = { \
818 .name = _t stringify(_i) " CTL", \
820 .wmask = 0x1cff001f, \
821 .offset = offsetof(IntelHDAState, st[_i].ctl), \
822 .whandler = intel_hda_set_st_ctl, \
824 [ ST_REG(_i, ICH6_REG_SD_CTL) + 2] = { \
826 .name = _t stringify(_i) " CTL(stnr)", \
829 .wmask = 0x00ff0000, \
830 .offset = offsetof(IntelHDAState, st[_i].ctl), \
831 .whandler = intel_hda_set_st_ctl, \
833 [ ST_REG(_i, ICH6_REG_SD_STS)] = { \
835 .name = _t stringify(_i) " CTL(sts)", \
838 .wmask = 0x1c000000, \
839 .wclear = 0x1c000000, \
840 .offset = offsetof(IntelHDAState, st[_i].ctl), \
841 .whandler = intel_hda_set_st_ctl, \
843 [ ST_REG(_i, ICH6_REG_SD_LPIB) ] = { \
845 .name = _t stringify(_i) " LPIB", \
847 .offset = offsetof(IntelHDAState, st[_i].lpib), \
849 [ ST_REG(_i, ICH6_REG_SD_LPIB) + 0x2000 ] = { \
851 .name = _t stringify(_i) " LPIB(alias)", \
853 .offset = offsetof(IntelHDAState, st[_i].lpib), \
855 [ ST_REG(_i, ICH6_REG_SD_CBL) ] = { \
857 .name = _t stringify(_i) " CBL", \
859 .wmask = 0xffffffff, \
860 .offset = offsetof(IntelHDAState, st[_i].cbl), \
862 [ ST_REG(_i, ICH6_REG_SD_LVI) ] = { \
864 .name = _t stringify(_i) " LVI", \
867 .offset = offsetof(IntelHDAState, st[_i].lvi), \
869 [ ST_REG(_i, ICH6_REG_SD_FIFOSIZE) ] = { \
871 .name = _t stringify(_i) " FIFOS", \
873 .reset = HDA_BUFFER_SIZE, \
875 [ ST_REG(_i, ICH6_REG_SD_FORMAT) ] = { \
877 .name = _t stringify(_i) " FMT", \
880 .offset = offsetof(IntelHDAState, st[_i].fmt), \
882 [ ST_REG(_i, ICH6_REG_SD_BDLPL) ] = { \
884 .name = _t stringify(_i) " BDLPL", \
886 .wmask = 0xffffff80, \
887 .offset = offsetof(IntelHDAState, st[_i].bdlp_lbase), \
889 [ ST_REG(_i, ICH6_REG_SD_BDLPU) ] = { \
891 .name = _t stringify(_i) " BDLPU", \
893 .wmask = 0xffffffff, \
894 .offset = offsetof(IntelHDAState, st[_i].bdlp_ubase), \
909 static const IntelHDAReg
*intel_hda_reg_find(IntelHDAState
*d
, target_phys_addr_t addr
)
911 const IntelHDAReg
*reg
;
913 if (addr
>= sizeof(regtab
)/sizeof(regtab
[0])) {
917 if (reg
->name
== NULL
) {
923 dprint(d
, 1, "unknown register, addr 0x%x\n", (int) addr
);
927 static uint32_t *intel_hda_reg_addr(IntelHDAState
*d
, const IntelHDAReg
*reg
)
929 uint8_t *addr
= (void*)d
;
932 return (uint32_t*)addr
;
935 static void intel_hda_reg_write(IntelHDAState
*d
, const IntelHDAReg
*reg
, uint32_t val
,
946 time_t now
= time(NULL
);
947 if (d
->last_write
&& d
->last_reg
== reg
&& d
->last_val
== val
) {
949 if (d
->last_sec
!= now
) {
950 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
955 if (d
->repeat_count
) {
956 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
958 dprint(d
, 2, "write %-16s: 0x%x (%x)\n", reg
->name
, val
, wmask
);
966 assert(reg
->offset
!= 0);
968 addr
= intel_hda_reg_addr(d
, reg
);
973 wmask
<<= reg
->shift
;
977 *addr
|= wmask
& val
;
978 *addr
&= ~(val
& reg
->wclear
);
981 reg
->whandler(d
, reg
, old
);
985 static uint32_t intel_hda_reg_read(IntelHDAState
*d
, const IntelHDAReg
*reg
,
995 reg
->rhandler(d
, reg
);
998 if (reg
->offset
== 0) {
999 /* constant read-only register */
1002 addr
= intel_hda_reg_addr(d
, reg
);
1010 time_t now
= time(NULL
);
1011 if (!d
->last_write
&& d
->last_reg
== reg
&& d
->last_val
== ret
) {
1013 if (d
->last_sec
!= now
) {
1014 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
1016 d
->repeat_count
= 0;
1019 if (d
->repeat_count
) {
1020 dprint(d
, 2, "previous register op repeated %d times\n", d
->repeat_count
);
1022 dprint(d
, 2, "read %-16s: 0x%x (%x)\n", reg
->name
, ret
, rmask
);
1027 d
->repeat_count
= 0;
1033 static void intel_hda_regs_reset(IntelHDAState
*d
)
1038 for (i
= 0; i
< sizeof(regtab
)/sizeof(regtab
[0]); i
++) {
1039 if (regtab
[i
].name
== NULL
) {
1042 if (regtab
[i
].offset
== 0) {
1045 addr
= intel_hda_reg_addr(d
, regtab
+ i
);
1046 *addr
= regtab
[i
].reset
;
1050 /* --------------------------------------------------------------------- */
1052 static void intel_hda_mmio_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1054 IntelHDAState
*d
= opaque
;
1055 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1057 intel_hda_reg_write(d
, reg
, val
, 0xff);
1060 static void intel_hda_mmio_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1062 IntelHDAState
*d
= opaque
;
1063 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1065 intel_hda_reg_write(d
, reg
, val
, 0xffff);
1068 static void intel_hda_mmio_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1070 IntelHDAState
*d
= opaque
;
1071 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1073 intel_hda_reg_write(d
, reg
, val
, 0xffffffff);
1076 static uint32_t intel_hda_mmio_readb(void *opaque
, target_phys_addr_t addr
)
1078 IntelHDAState
*d
= opaque
;
1079 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1081 return intel_hda_reg_read(d
, reg
, 0xff);
1084 static uint32_t intel_hda_mmio_readw(void *opaque
, target_phys_addr_t addr
)
1086 IntelHDAState
*d
= opaque
;
1087 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1089 return intel_hda_reg_read(d
, reg
, 0xffff);
1092 static uint32_t intel_hda_mmio_readl(void *opaque
, target_phys_addr_t addr
)
1094 IntelHDAState
*d
= opaque
;
1095 const IntelHDAReg
*reg
= intel_hda_reg_find(d
, addr
);
1097 return intel_hda_reg_read(d
, reg
, 0xffffffff);
1100 static CPUReadMemoryFunc
* const intel_hda_mmio_read
[3] = {
1101 intel_hda_mmio_readb
,
1102 intel_hda_mmio_readw
,
1103 intel_hda_mmio_readl
,
1106 static CPUWriteMemoryFunc
* const intel_hda_mmio_write
[3] = {
1107 intel_hda_mmio_writeb
,
1108 intel_hda_mmio_writew
,
1109 intel_hda_mmio_writel
,
1112 static void intel_hda_map(PCIDevice
*pci
, int region_num
,
1113 pcibus_t addr
, pcibus_t size
, int type
)
1115 IntelHDAState
*d
= DO_UPCAST(IntelHDAState
, pci
, pci
);
1117 cpu_register_physical_memory(addr
, 0x4000, d
->mmio_addr
);
1120 /* --------------------------------------------------------------------- */
1122 static void intel_hda_reset(DeviceState
*dev
)
1124 IntelHDAState
*d
= DO_UPCAST(IntelHDAState
, pci
.qdev
, dev
);
1126 HDACodecDevice
*cdev
;
1128 intel_hda_regs_reset(d
);
1129 d
->wall_base_ns
= qemu_get_clock(vm_clock
);
1132 QLIST_FOREACH(qdev
, &d
->codecs
.qbus
.children
, sibling
) {
1133 cdev
= DO_UPCAST(HDACodecDevice
, qdev
, qdev
);
1134 if (qdev
->info
->reset
) {
1135 qdev
->info
->reset(qdev
);
1137 d
->state_sts
|= (1 << cdev
->cad
);
1139 intel_hda_update_irq(d
);
1142 static int intel_hda_init(PCIDevice
*pci
)
1144 IntelHDAState
*d
= DO_UPCAST(IntelHDAState
, pci
, pci
);
1145 uint8_t *conf
= d
->pci
.config
;
1147 d
->name
= d
->pci
.qdev
.info
->name
;
1149 pci_config_set_vendor_id(conf
, PCI_VENDOR_ID_INTEL
);
1150 pci_config_set_device_id(conf
, 0x2668);
1151 pci_config_set_revision(conf
, 1);
1152 pci_config_set_class(conf
, PCI_CLASS_MULTIMEDIA_HD_AUDIO
);
1153 pci_config_set_interrupt_pin(conf
, 1);
1155 /* HDCTL off 0x40 bit 0 selects signaling mode (1-HDA, 0 - Ac97) 18.1.19 */
1158 d
->mmio_addr
= cpu_register_io_memory(intel_hda_mmio_read
,
1159 intel_hda_mmio_write
, d
,
1160 DEVICE_NATIVE_ENDIAN
);
1161 pci_register_bar(&d
->pci
, 0, 0x4000, PCI_BASE_ADDRESS_SPACE_MEMORY
,
1164 msi_init(&d
->pci
, 0x50, 1, true, false);
1167 hda_codec_bus_init(&d
->pci
.qdev
, &d
->codecs
,
1168 intel_hda_response
, intel_hda_xfer
);
1173 static int intel_hda_exit(PCIDevice
*pci
)
1175 IntelHDAState
*d
= DO_UPCAST(IntelHDAState
, pci
, pci
);
1178 msi_uninit(&d
->pci
);
1180 cpu_unregister_io_memory(d
->mmio_addr
);
1184 static void intel_hda_write_config(PCIDevice
*pci
, uint32_t addr
,
1185 uint32_t val
, int len
)
1187 IntelHDAState
*d
= DO_UPCAST(IntelHDAState
, pci
, pci
);
1189 pci_default_write_config(pci
, addr
, val
, len
);
1191 msi_write_config(pci
, addr
, val
, len
);
1195 static int intel_hda_post_load(void *opaque
, int version
)
1197 IntelHDAState
* d
= opaque
;
1200 dprint(d
, 1, "%s\n", __FUNCTION__
);
1201 for (i
= 0; i
< ARRAY_SIZE(d
->st
); i
++) {
1202 if (d
->st
[i
].ctl
& 0x02) {
1203 intel_hda_parse_bdl(d
, &d
->st
[i
]);
1206 intel_hda_update_irq(d
);
1210 static const VMStateDescription vmstate_intel_hda_stream
= {
1211 .name
= "intel-hda-stream",
1213 .fields
= (VMStateField
[]) {
1214 VMSTATE_UINT32(ctl
, IntelHDAStream
),
1215 VMSTATE_UINT32(lpib
, IntelHDAStream
),
1216 VMSTATE_UINT32(cbl
, IntelHDAStream
),
1217 VMSTATE_UINT32(lvi
, IntelHDAStream
),
1218 VMSTATE_UINT32(fmt
, IntelHDAStream
),
1219 VMSTATE_UINT32(bdlp_lbase
, IntelHDAStream
),
1220 VMSTATE_UINT32(bdlp_ubase
, IntelHDAStream
),
1221 VMSTATE_END_OF_LIST()
1225 static const VMStateDescription vmstate_intel_hda
= {
1226 .name
= "intel-hda",
1228 .post_load
= intel_hda_post_load
,
1229 .fields
= (VMStateField
[]) {
1230 VMSTATE_PCI_DEVICE(pci
, IntelHDAState
),
1233 VMSTATE_UINT32(g_ctl
, IntelHDAState
),
1234 VMSTATE_UINT32(wake_en
, IntelHDAState
),
1235 VMSTATE_UINT32(state_sts
, IntelHDAState
),
1236 VMSTATE_UINT32(int_ctl
, IntelHDAState
),
1237 VMSTATE_UINT32(int_sts
, IntelHDAState
),
1238 VMSTATE_UINT32(wall_clk
, IntelHDAState
),
1239 VMSTATE_UINT32(corb_lbase
, IntelHDAState
),
1240 VMSTATE_UINT32(corb_ubase
, IntelHDAState
),
1241 VMSTATE_UINT32(corb_rp
, IntelHDAState
),
1242 VMSTATE_UINT32(corb_wp
, IntelHDAState
),
1243 VMSTATE_UINT32(corb_ctl
, IntelHDAState
),
1244 VMSTATE_UINT32(corb_sts
, IntelHDAState
),
1245 VMSTATE_UINT32(corb_size
, IntelHDAState
),
1246 VMSTATE_UINT32(rirb_lbase
, IntelHDAState
),
1247 VMSTATE_UINT32(rirb_ubase
, IntelHDAState
),
1248 VMSTATE_UINT32(rirb_wp
, IntelHDAState
),
1249 VMSTATE_UINT32(rirb_cnt
, IntelHDAState
),
1250 VMSTATE_UINT32(rirb_ctl
, IntelHDAState
),
1251 VMSTATE_UINT32(rirb_sts
, IntelHDAState
),
1252 VMSTATE_UINT32(rirb_size
, IntelHDAState
),
1253 VMSTATE_UINT32(dp_lbase
, IntelHDAState
),
1254 VMSTATE_UINT32(dp_ubase
, IntelHDAState
),
1255 VMSTATE_UINT32(icw
, IntelHDAState
),
1256 VMSTATE_UINT32(irr
, IntelHDAState
),
1257 VMSTATE_UINT32(ics
, IntelHDAState
),
1258 VMSTATE_STRUCT_ARRAY(st
, IntelHDAState
, 8, 0,
1259 vmstate_intel_hda_stream
,
1262 /* additional state info */
1263 VMSTATE_UINT32(rirb_count
, IntelHDAState
),
1264 VMSTATE_INT64(wall_base_ns
, IntelHDAState
),
1266 VMSTATE_END_OF_LIST()
1270 static PCIDeviceInfo intel_hda_info
= {
1271 .qdev
.name
= "intel-hda",
1272 .qdev
.desc
= "Intel HD Audio Controller",
1273 .qdev
.size
= sizeof(IntelHDAState
),
1274 .qdev
.vmsd
= &vmstate_intel_hda
,
1275 .qdev
.reset
= intel_hda_reset
,
1276 .init
= intel_hda_init
,
1277 .exit
= intel_hda_exit
,
1278 .config_write
= intel_hda_write_config
,
1279 .qdev
.props
= (Property
[]) {
1280 DEFINE_PROP_UINT32("debug", IntelHDAState
, debug
, 0),
1281 DEFINE_PROP_UINT32("msi", IntelHDAState
, msi
, 1),
1282 DEFINE_PROP_END_OF_LIST(),
1286 static void intel_hda_register(void)
1288 pci_qdev_register(&intel_hda_info
);
1290 device_init(intel_hda_register
);
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
);