2 * Copyright (C) 2010 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 or
9 * (at your option) version 3 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "hw/pci/pci.h"
23 #include "hw/pci/msi.h"
24 #include "qemu/timer.h"
25 #include "qemu/bitops.h"
26 #include "hw/audio/soundhw.h"
27 #include "intel-hda.h"
28 #include "intel-hda-defs.h"
29 #include "sysemu/dma.h"
30 #include "qapi/error.h"
32 /* --------------------------------------------------------------------- */
35 static Property hda_props[] = {
36 DEFINE_PROP_UINT32("cad", HDACodecDevice, cad, -1),
37 DEFINE_PROP_END_OF_LIST()
40 static const TypeInfo hda_codec_bus_info = {
43 .instance_size = sizeof(HDACodecBus),
46 void hda_codec_bus_init(DeviceState *dev, HDACodecBus *bus, size_t bus_size,
47 hda_codec_response_func response,
48 hda_codec_xfer_func xfer)
50 qbus_create_inplace(bus, bus_size, TYPE_HDA_BUS, dev, NULL);
51 bus->response = response;
55 static void hda_codec_dev_realize(DeviceState *qdev, Error **errp)
57 HDACodecBus *bus = HDA_BUS(qdev->parent_bus);
58 HDACodecDevice *dev = HDA_CODEC_DEVICE(qdev);
59 HDACodecDeviceClass *cdc = HDA_CODEC_DEVICE_GET_CLASS(dev);
62 dev->cad = bus->next_cad;
65 error_setg(errp, "HDA audio codec address is full");
68 bus->next_cad = dev->cad + 1;
69 if (cdc->init(dev) != 0) {
70 error_setg(errp, "HDA audio init failed");
74 static void hda_codec_dev_unrealize(DeviceState *qdev, Error **errp)
76 HDACodecDevice *dev = HDA_CODEC_DEVICE(qdev);
77 HDACodecDeviceClass *cdc = HDA_CODEC_DEVICE_GET_CLASS(dev);
84 HDACodecDevice *hda_codec_find(HDACodecBus *bus, uint32_t cad)
89 QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
90 DeviceState *qdev = kid->child;
91 cdev = HDA_CODEC_DEVICE(qdev);
92 if (cdev->cad == cad) {
99 void hda_codec_response(HDACodecDevice *dev, bool solicited, uint32_t response)
101 HDACodecBus *bus = HDA_BUS(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 = HDA_BUS(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 #define TYPE_INTEL_HDA_GENERIC "intel-hda-generic"
200 #define INTEL_HDA(obj) \
201 OBJECT_CHECK(IntelHDAState, (obj), TYPE_INTEL_HDA_GENERIC)
204 const char *name; /* register name */
205 uint32_t size; /* size in bytes */
206 uint32_t reset; /* reset value */
207 uint32_t wmask; /* write mask */
208 uint32_t wclear; /* write 1 to clear bits */
209 uint32_t offset; /* location in IntelHDAState */
210 uint32_t shift; /* byte access entries for dwords */
212 void (*whandler)(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old);
213 void (*rhandler)(IntelHDAState *d, const IntelHDAReg *reg);
216 static void intel_hda_reset(DeviceState *dev);
218 /* --------------------------------------------------------------------- */
220 static hwaddr intel_hda_addr(uint32_t lbase, uint32_t ubase)
222 return ((uint64_t)ubase << 32) | lbase;
225 static void intel_hda_update_int_sts(IntelHDAState *d)
230 /* update controller status */
231 if (d->rirb_sts & ICH6_RBSTS_IRQ) {
234 if (d->rirb_sts & ICH6_RBSTS_OVERRUN) {
237 if (d->state_sts & d->wake_en) {
241 /* update stream status */
242 for (i = 0; i < 8; i++) {
243 /* buffer completion interrupt */
244 if (d->st[i].ctl & (1 << 26)) {
249 /* update global status */
250 if (sts & d->int_ctl) {
257 static void intel_hda_update_irq(IntelHDAState *d)
259 bool msi = msi_enabled(&d->pci);
262 intel_hda_update_int_sts(d);
263 if (d->int_sts & (1U << 31) && d->int_ctl & (1U << 31)) {
268 dprint(d, 2, "%s: level %d [%s]\n", __func__,
269 level, msi ? "msi" : "intx");
272 msi_notify(&d->pci, 0);
275 pci_set_irq(&d->pci, level);
279 static int intel_hda_send_command(IntelHDAState *d, uint32_t verb)
281 uint32_t cad, nid, data;
282 HDACodecDevice *codec;
283 HDACodecDeviceClass *cdc;
285 cad = (verb >> 28) & 0x0f;
286 if (verb & (1 << 27)) {
287 /* indirect node addressing, not specified in HDA 1.0 */
288 dprint(d, 1, "%s: indirect node addressing (guest bug?)\n", __func__);
291 nid = (verb >> 20) & 0x7f;
292 data = verb & 0xfffff;
294 codec = hda_codec_find(&d->codecs, cad);
296 dprint(d, 1, "%s: addressed non-existing codec\n", __func__);
299 cdc = HDA_CODEC_DEVICE_GET_CLASS(codec);
300 cdc->command(codec, nid, data);
304 static void intel_hda_corb_run(IntelHDAState *d)
309 if (d->ics & ICH6_IRS_BUSY) {
310 dprint(d, 2, "%s: [icw] verb 0x%08x\n", __func__, d->icw);
311 intel_hda_send_command(d, d->icw);
316 if (!(d->corb_ctl & ICH6_CORBCTL_RUN)) {
317 dprint(d, 2, "%s: !run\n", __func__);
320 if ((d->corb_rp & 0xff) == d->corb_wp) {
321 dprint(d, 2, "%s: corb ring empty\n", __func__);
324 if (d->rirb_count == d->rirb_cnt) {
325 dprint(d, 2, "%s: rirb count reached\n", __func__);
329 rp = (d->corb_rp + 1) & 0xff;
330 addr = intel_hda_addr(d->corb_lbase, d->corb_ubase);
331 verb = ldl_le_pci_dma(&d->pci, addr + 4*rp);
334 dprint(d, 2, "%s: [rp 0x%x] verb 0x%08x\n", __func__, rp, verb);
335 intel_hda_send_command(d, verb);
339 static void intel_hda_response(HDACodecDevice *dev, bool solicited, uint32_t response)
341 HDACodecBus *bus = HDA_BUS(dev->qdev.parent_bus);
342 IntelHDAState *d = container_of(bus, IntelHDAState, codecs);
346 if (d->ics & ICH6_IRS_BUSY) {
347 dprint(d, 2, "%s: [irr] response 0x%x, cad 0x%x\n",
348 __func__, response, dev->cad);
350 d->ics &= ~(ICH6_IRS_BUSY | 0xf0);
351 d->ics |= (ICH6_IRS_VALID | (dev->cad << 4));
355 if (!(d->rirb_ctl & ICH6_RBCTL_DMA_EN)) {
356 dprint(d, 1, "%s: rirb dma disabled, drop codec response\n", __func__);
360 ex = (solicited ? 0 : (1 << 4)) | dev->cad;
361 wp = (d->rirb_wp + 1) & 0xff;
362 addr = intel_hda_addr(d->rirb_lbase, d->rirb_ubase);
363 stl_le_pci_dma(&d->pci, addr + 8*wp, response);
364 stl_le_pci_dma(&d->pci, addr + 8*wp + 4, ex);
367 dprint(d, 2, "%s: [wp 0x%x] response 0x%x, extra 0x%x\n",
368 __func__, wp, response, ex);
371 if (d->rirb_count == d->rirb_cnt) {
372 dprint(d, 2, "%s: rirb count reached (%d)\n", __func__, d->rirb_count);
373 if (d->rirb_ctl & ICH6_RBCTL_IRQ_EN) {
374 d->rirb_sts |= ICH6_RBSTS_IRQ;
375 intel_hda_update_irq(d);
377 } else if ((d->corb_rp & 0xff) == d->corb_wp) {
378 dprint(d, 2, "%s: corb ring empty (%d/%d)\n", __func__,
379 d->rirb_count, d->rirb_cnt);
380 if (d->rirb_ctl & ICH6_RBCTL_IRQ_EN) {
381 d->rirb_sts |= ICH6_RBSTS_IRQ;
382 intel_hda_update_irq(d);
387 static bool intel_hda_xfer(HDACodecDevice *dev, uint32_t stnr, bool output,
388 uint8_t *buf, uint32_t len)
390 HDACodecBus *bus = HDA_BUS(dev->qdev.parent_bus);
391 IntelHDAState *d = container_of(bus, IntelHDAState, codecs);
393 uint32_t s, copy, left;
397 st = output ? d->st + 4 : d->st;
398 for (s = 0; s < 4; s++) {
399 if (stnr == ((st[s].ctl >> 20) & 0x0f)) {
407 if (st->bpl == NULL) {
413 while (left > 0 && s-- > 0) {
415 if (copy > st->bsize - st->lpib)
416 copy = st->bsize - st->lpib;
417 if (copy > st->bpl[st->be].len - st->bp)
418 copy = st->bpl[st->be].len - st->bp;
420 dprint(d, 3, "dma: entry %d, pos %d/%d, copy %d\n",
421 st->be, st->bp, st->bpl[st->be].len, copy);
423 pci_dma_rw(&d->pci, st->bpl[st->be].addr + st->bp, buf, copy, !output);
429 if (st->bpl[st->be].len == st->bp) {
430 /* bpl entry filled */
431 if (st->bpl[st->be].flags & 0x01) {
436 if (st->be == st->bentries) {
437 /* bpl wrap around */
443 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)
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(kid, &d->codecs.qbus.children, sibling) {
488 DeviceState *qdev = kid->child;
489 HDACodecDeviceClass *cdc;
491 cdev = HDA_CODEC_DEVICE(qdev);
492 cdc = HDA_CODEC_DEVICE_GET_CLASS(cdev);
494 cdc->stream(cdev, stream, running, output);
499 /* --------------------------------------------------------------------- */
501 static void intel_hda_set_g_ctl(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
503 if ((d->g_ctl & ICH6_GCTL_RESET) == 0) {
504 intel_hda_reset(DEVICE(d));
508 static void intel_hda_set_wake_en(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
510 intel_hda_update_irq(d);
513 static void intel_hda_set_state_sts(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
515 intel_hda_update_irq(d);
518 static void intel_hda_set_int_ctl(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
520 intel_hda_update_irq(d);
523 static void intel_hda_get_wall_clk(IntelHDAState *d, const IntelHDAReg *reg)
527 ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - d->wall_base_ns;
528 d->wall_clk = (uint32_t)(ns * 24 / 1000); /* 24 MHz */
531 static void intel_hda_set_corb_wp(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
533 intel_hda_corb_run(d);
536 static void intel_hda_set_corb_ctl(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
538 intel_hda_corb_run(d);
541 static void intel_hda_set_rirb_wp(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
543 if (d->rirb_wp & ICH6_RIRBWP_RST) {
548 static void intel_hda_set_rirb_sts(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
550 intel_hda_update_irq(d);
552 if ((old & ICH6_RBSTS_IRQ) && !(d->rirb_sts & ICH6_RBSTS_IRQ)) {
553 /* cleared ICH6_RBSTS_IRQ */
555 intel_hda_corb_run(d);
559 static void intel_hda_set_ics(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
561 if (d->ics & ICH6_IRS_BUSY) {
562 intel_hda_corb_run(d);
566 static void intel_hda_set_st_ctl(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
568 bool output = reg->stream >= 4;
569 IntelHDAStream *st = d->st + reg->stream;
571 if (st->ctl & 0x01) {
573 dprint(d, 1, "st #%d: reset\n", reg->stream);
574 st->ctl = SD_STS_FIFO_READY << 24;
576 if ((st->ctl & 0x02) != (old & 0x02)) {
577 uint32_t stnr = (st->ctl >> 20) & 0x0f;
578 /* run bit flipped */
579 if (st->ctl & 0x02) {
581 dprint(d, 1, "st #%d: start %d (ring buf %d bytes)\n",
582 reg->stream, stnr, st->cbl);
583 intel_hda_parse_bdl(d, st);
584 intel_hda_notify_codecs(d, stnr, true, output);
587 dprint(d, 1, "st #%d: stop %d\n", reg->stream, stnr);
588 intel_hda_notify_codecs(d, stnr, false, output);
591 intel_hda_update_irq(d);
594 /* --------------------------------------------------------------------- */
596 #define ST_REG(_n, _o) (0x80 + (_n) * 0x20 + (_o))
598 static const struct IntelHDAReg regtab[] = {
600 [ ICH6_REG_GCAP ] = {
605 [ ICH6_REG_VMIN ] = {
609 [ ICH6_REG_VMAJ ] = {
614 [ ICH6_REG_OUTPAY ] = {
619 [ ICH6_REG_INPAY ] = {
624 [ ICH6_REG_GCTL ] = {
628 .offset = offsetof(IntelHDAState, g_ctl),
629 .whandler = intel_hda_set_g_ctl,
631 [ ICH6_REG_WAKEEN ] = {
635 .offset = offsetof(IntelHDAState, wake_en),
636 .whandler = intel_hda_set_wake_en,
638 [ ICH6_REG_STATESTS ] = {
643 .offset = offsetof(IntelHDAState, state_sts),
644 .whandler = intel_hda_set_state_sts,
648 [ ICH6_REG_INTCTL ] = {
652 .offset = offsetof(IntelHDAState, int_ctl),
653 .whandler = intel_hda_set_int_ctl,
655 [ ICH6_REG_INTSTS ] = {
659 .wclear = 0xc00000ff,
660 .offset = offsetof(IntelHDAState, int_sts),
664 [ ICH6_REG_WALLCLK ] = {
667 .offset = offsetof(IntelHDAState, wall_clk),
668 .rhandler = intel_hda_get_wall_clk,
670 [ ICH6_REG_WALLCLK + 0x2000 ] = {
671 .name = "WALLCLK(alias)",
673 .offset = offsetof(IntelHDAState, wall_clk),
674 .rhandler = intel_hda_get_wall_clk,
678 [ ICH6_REG_CORBLBASE ] = {
682 .offset = offsetof(IntelHDAState, corb_lbase),
684 [ ICH6_REG_CORBUBASE ] = {
688 .offset = offsetof(IntelHDAState, corb_ubase),
690 [ ICH6_REG_CORBWP ] = {
694 .offset = offsetof(IntelHDAState, corb_wp),
695 .whandler = intel_hda_set_corb_wp,
697 [ ICH6_REG_CORBRP ] = {
701 .offset = offsetof(IntelHDAState, corb_rp),
703 [ ICH6_REG_CORBCTL ] = {
707 .offset = offsetof(IntelHDAState, corb_ctl),
708 .whandler = intel_hda_set_corb_ctl,
710 [ ICH6_REG_CORBSTS ] = {
715 .offset = offsetof(IntelHDAState, corb_sts),
717 [ ICH6_REG_CORBSIZE ] = {
721 .offset = offsetof(IntelHDAState, corb_size),
723 [ ICH6_REG_RIRBLBASE ] = {
727 .offset = offsetof(IntelHDAState, rirb_lbase),
729 [ ICH6_REG_RIRBUBASE ] = {
733 .offset = offsetof(IntelHDAState, rirb_ubase),
735 [ ICH6_REG_RIRBWP ] = {
739 .offset = offsetof(IntelHDAState, rirb_wp),
740 .whandler = intel_hda_set_rirb_wp,
742 [ ICH6_REG_RINTCNT ] = {
746 .offset = offsetof(IntelHDAState, rirb_cnt),
748 [ ICH6_REG_RIRBCTL ] = {
752 .offset = offsetof(IntelHDAState, rirb_ctl),
754 [ ICH6_REG_RIRBSTS ] = {
759 .offset = offsetof(IntelHDAState, rirb_sts),
760 .whandler = intel_hda_set_rirb_sts,
762 [ ICH6_REG_RIRBSIZE ] = {
766 .offset = offsetof(IntelHDAState, rirb_size),
769 [ ICH6_REG_DPLBASE ] = {
773 .offset = offsetof(IntelHDAState, dp_lbase),
775 [ ICH6_REG_DPUBASE ] = {
779 .offset = offsetof(IntelHDAState, dp_ubase),
786 .offset = offsetof(IntelHDAState, icw),
791 .offset = offsetof(IntelHDAState, irr),
798 .offset = offsetof(IntelHDAState, ics),
799 .whandler = intel_hda_set_ics,
802 #define HDA_STREAM(_t, _i) \
803 [ ST_REG(_i, ICH6_REG_SD_CTL) ] = { \
805 .name = _t stringify(_i) " CTL", \
807 .wmask = 0x1cff001f, \
808 .offset = offsetof(IntelHDAState, st[_i].ctl), \
809 .whandler = intel_hda_set_st_ctl, \
811 [ ST_REG(_i, ICH6_REG_SD_CTL) + 2] = { \
813 .name = _t stringify(_i) " CTL(stnr)", \
816 .wmask = 0x00ff0000, \
817 .offset = offsetof(IntelHDAState, st[_i].ctl), \
818 .whandler = intel_hda_set_st_ctl, \
820 [ ST_REG(_i, ICH6_REG_SD_STS)] = { \
822 .name = _t stringify(_i) " CTL(sts)", \
825 .wmask = 0x1c000000, \
826 .wclear = 0x1c000000, \
827 .offset = offsetof(IntelHDAState, st[_i].ctl), \
828 .whandler = intel_hda_set_st_ctl, \
829 .reset = SD_STS_FIFO_READY << 24 \
831 [ ST_REG(_i, ICH6_REG_SD_LPIB) ] = { \
833 .name = _t stringify(_i) " LPIB", \
835 .offset = offsetof(IntelHDAState, st[_i].lpib), \
837 [ ST_REG(_i, ICH6_REG_SD_LPIB) + 0x2000 ] = { \
839 .name = _t stringify(_i) " LPIB(alias)", \
841 .offset = offsetof(IntelHDAState, st[_i].lpib), \
843 [ ST_REG(_i, ICH6_REG_SD_CBL) ] = { \
845 .name = _t stringify(_i) " CBL", \
847 .wmask = 0xffffffff, \
848 .offset = offsetof(IntelHDAState, st[_i].cbl), \
850 [ ST_REG(_i, ICH6_REG_SD_LVI) ] = { \
852 .name = _t stringify(_i) " LVI", \
855 .offset = offsetof(IntelHDAState, st[_i].lvi), \
857 [ ST_REG(_i, ICH6_REG_SD_FIFOSIZE) ] = { \
859 .name = _t stringify(_i) " FIFOS", \
861 .reset = HDA_BUFFER_SIZE, \
863 [ ST_REG(_i, ICH6_REG_SD_FORMAT) ] = { \
865 .name = _t stringify(_i) " FMT", \
868 .offset = offsetof(IntelHDAState, st[_i].fmt), \
870 [ ST_REG(_i, ICH6_REG_SD_BDLPL) ] = { \
872 .name = _t stringify(_i) " BDLPL", \
874 .wmask = 0xffffff80, \
875 .offset = offsetof(IntelHDAState, st[_i].bdlp_lbase), \
877 [ ST_REG(_i, ICH6_REG_SD_BDLPU) ] = { \
879 .name = _t stringify(_i) " BDLPU", \
881 .wmask = 0xffffffff, \
882 .offset = offsetof(IntelHDAState, st[_i].bdlp_ubase), \
897 static const IntelHDAReg *intel_hda_reg_find(IntelHDAState *d, hwaddr addr)
899 const IntelHDAReg *reg;
901 if (addr >= ARRAY_SIZE(regtab)) {
905 if (reg->name == NULL) {
911 dprint(d, 1, "unknown register, addr 0x%x\n", (int) addr);
915 static uint32_t *intel_hda_reg_addr(IntelHDAState *d, const IntelHDAReg *reg)
917 uint8_t *addr = (void*)d;
920 return (uint32_t*)addr;
923 static void intel_hda_reg_write(IntelHDAState *d, const IntelHDAReg *reg, uint32_t val,
934 time_t now = time(NULL);
935 if (d->last_write && d->last_reg == reg && d->last_val == val) {
937 if (d->last_sec != now) {
938 dprint(d, 2, "previous register op repeated %d times\n", d->repeat_count);
943 if (d->repeat_count) {
944 dprint(d, 2, "previous register op repeated %d times\n", d->repeat_count);
946 dprint(d, 2, "write %-16s: 0x%x (%x)\n", reg->name, val, wmask);
954 assert(reg->offset != 0);
956 addr = intel_hda_reg_addr(d, reg);
961 wmask <<= reg->shift;
965 *addr |= wmask & val;
966 *addr &= ~(val & reg->wclear);
969 reg->whandler(d, reg, old);
973 static uint32_t intel_hda_reg_read(IntelHDAState *d, const IntelHDAReg *reg,
983 reg->rhandler(d, reg);
986 if (reg->offset == 0) {
987 /* constant read-only register */
990 addr = intel_hda_reg_addr(d, reg);
998 time_t now = time(NULL);
999 if (!d->last_write && d->last_reg == reg && d->last_val == ret) {
1001 if (d->last_sec != now) {
1002 dprint(d, 2, "previous register op repeated %d times\n", d->repeat_count);
1004 d->repeat_count = 0;
1007 if (d->repeat_count) {
1008 dprint(d, 2, "previous register op repeated %d times\n", d->repeat_count);
1010 dprint(d, 2, "read %-16s: 0x%x (%x)\n", reg->name, ret, rmask);
1015 d->repeat_count = 0;
1021 static void intel_hda_regs_reset(IntelHDAState *d)
1026 for (i = 0; i < ARRAY_SIZE(regtab); i++) {
1027 if (regtab[i].name == NULL) {
1030 if (regtab[i].offset == 0) {
1033 addr = intel_hda_reg_addr(d, regtab + i);
1034 *addr = regtab[i].reset;
1038 /* --------------------------------------------------------------------- */
1040 static void intel_hda_mmio_write(void *opaque, hwaddr addr, uint64_t val,
1043 IntelHDAState *d = opaque;
1044 const IntelHDAReg *reg = intel_hda_reg_find(d, addr);
1046 intel_hda_reg_write(d, reg, val, MAKE_64BIT_MASK(0, size * 8));
1049 static uint64_t intel_hda_mmio_read(void *opaque, hwaddr addr, unsigned size)
1051 IntelHDAState *d = opaque;
1052 const IntelHDAReg *reg = intel_hda_reg_find(d, addr);
1054 return intel_hda_reg_read(d, reg, MAKE_64BIT_MASK(0, size * 8));
1057 static const MemoryRegionOps intel_hda_mmio_ops = {
1058 .read = intel_hda_mmio_read,
1059 .write = intel_hda_mmio_write,
1061 .min_access_size = 1,
1062 .max_access_size = 4,
1064 .endianness = DEVICE_NATIVE_ENDIAN,
1067 /* --------------------------------------------------------------------- */
1069 static void intel_hda_reset(DeviceState *dev)
1072 IntelHDAState *d = INTEL_HDA(dev);
1073 HDACodecDevice *cdev;
1075 intel_hda_regs_reset(d);
1076 d->wall_base_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1079 QTAILQ_FOREACH(kid, &d->codecs.qbus.children, sibling) {
1080 DeviceState *qdev = kid->child;
1081 cdev = HDA_CODEC_DEVICE(qdev);
1082 device_reset(DEVICE(cdev));
1083 d->state_sts |= (1 << cdev->cad);
1085 intel_hda_update_irq(d);
1088 static void intel_hda_realize(PCIDevice *pci, Error **errp)
1090 IntelHDAState *d = INTEL_HDA(pci);
1091 uint8_t *conf = d->pci.config;
1095 d->name = object_get_typename(OBJECT(d));
1097 pci_config_set_interrupt_pin(conf, 1);
1099 /* HDCTL off 0x40 bit 0 selects signaling mode (1-HDA, 0 - Ac97) 18.1.19 */
1102 if (d->msi != ON_OFF_AUTO_OFF) {
1103 ret = msi_init(&d->pci, d->old_msi_addr ? 0x50 : 0x60,
1104 1, true, false, &err);
1105 /* Any error other than -ENOTSUP(board's MSI support is broken)
1106 * is a programming error */
1107 assert(!ret || ret == -ENOTSUP);
1108 if (ret && d->msi == ON_OFF_AUTO_ON) {
1109 /* Can't satisfy user's explicit msi=on request, fail */
1110 error_append_hint(&err, "You have to use msi=auto (default) or "
1111 "msi=off with this machine type.\n");
1112 error_propagate(errp, err);
1115 assert(!err || d->msi == ON_OFF_AUTO_AUTO);
1116 /* With msi=auto, we fall back to MSI off silently */
1120 memory_region_init_io(&d->mmio, OBJECT(d), &intel_hda_mmio_ops, d,
1121 "intel-hda", 0x4000);
1122 pci_register_bar(&d->pci, 0, 0, &d->mmio);
1124 hda_codec_bus_init(DEVICE(pci), &d->codecs, sizeof(d->codecs),
1125 intel_hda_response, intel_hda_xfer);
1128 static void intel_hda_exit(PCIDevice *pci)
1130 IntelHDAState *d = INTEL_HDA(pci);
1132 msi_uninit(&d->pci);
1135 static int intel_hda_post_load(void *opaque, int version)
1137 IntelHDAState* d = opaque;
1140 dprint(d, 1, "%s\n", __func__);
1141 for (i = 0; i < ARRAY_SIZE(d->st); i++) {
1142 if (d->st[i].ctl & 0x02) {
1143 intel_hda_parse_bdl(d, &d->st[i]);
1146 intel_hda_update_irq(d);
1150 static const VMStateDescription vmstate_intel_hda_stream = {
1151 .name = "intel-hda-stream",
1153 .fields = (VMStateField[]) {
1154 VMSTATE_UINT32(ctl, IntelHDAStream),
1155 VMSTATE_UINT32(lpib, IntelHDAStream),
1156 VMSTATE_UINT32(cbl, IntelHDAStream),
1157 VMSTATE_UINT32(lvi, IntelHDAStream),
1158 VMSTATE_UINT32(fmt, IntelHDAStream),
1159 VMSTATE_UINT32(bdlp_lbase, IntelHDAStream),
1160 VMSTATE_UINT32(bdlp_ubase, IntelHDAStream),
1161 VMSTATE_END_OF_LIST()
1165 static const VMStateDescription vmstate_intel_hda = {
1166 .name = "intel-hda",
1168 .post_load = intel_hda_post_load,
1169 .fields = (VMStateField[]) {
1170 VMSTATE_PCI_DEVICE(pci, IntelHDAState),
1173 VMSTATE_UINT32(g_ctl, IntelHDAState),
1174 VMSTATE_UINT32(wake_en, IntelHDAState),
1175 VMSTATE_UINT32(state_sts, IntelHDAState),
1176 VMSTATE_UINT32(int_ctl, IntelHDAState),
1177 VMSTATE_UINT32(int_sts, IntelHDAState),
1178 VMSTATE_UINT32(wall_clk, IntelHDAState),
1179 VMSTATE_UINT32(corb_lbase, IntelHDAState),
1180 VMSTATE_UINT32(corb_ubase, IntelHDAState),
1181 VMSTATE_UINT32(corb_rp, IntelHDAState),
1182 VMSTATE_UINT32(corb_wp, IntelHDAState),
1183 VMSTATE_UINT32(corb_ctl, IntelHDAState),
1184 VMSTATE_UINT32(corb_sts, IntelHDAState),
1185 VMSTATE_UINT32(corb_size, IntelHDAState),
1186 VMSTATE_UINT32(rirb_lbase, IntelHDAState),
1187 VMSTATE_UINT32(rirb_ubase, IntelHDAState),
1188 VMSTATE_UINT32(rirb_wp, IntelHDAState),
1189 VMSTATE_UINT32(rirb_cnt, IntelHDAState),
1190 VMSTATE_UINT32(rirb_ctl, IntelHDAState),
1191 VMSTATE_UINT32(rirb_sts, IntelHDAState),
1192 VMSTATE_UINT32(rirb_size, IntelHDAState),
1193 VMSTATE_UINT32(dp_lbase, IntelHDAState),
1194 VMSTATE_UINT32(dp_ubase, IntelHDAState),
1195 VMSTATE_UINT32(icw, IntelHDAState),
1196 VMSTATE_UINT32(irr, IntelHDAState),
1197 VMSTATE_UINT32(ics, IntelHDAState),
1198 VMSTATE_STRUCT_ARRAY(st, IntelHDAState, 8, 0,
1199 vmstate_intel_hda_stream,
1202 /* additional state info */
1203 VMSTATE_UINT32(rirb_count, IntelHDAState),
1204 VMSTATE_INT64(wall_base_ns, IntelHDAState),
1206 VMSTATE_END_OF_LIST()
1210 static Property intel_hda_properties[] = {
1211 DEFINE_PROP_UINT32("debug", IntelHDAState, debug, 0),
1212 DEFINE_PROP_ON_OFF_AUTO("msi", IntelHDAState, msi, ON_OFF_AUTO_AUTO),
1213 DEFINE_PROP_BOOL("old_msi_addr", IntelHDAState, old_msi_addr, false),
1214 DEFINE_PROP_END_OF_LIST(),
1217 static void intel_hda_class_init(ObjectClass *klass, void *data)
1219 DeviceClass *dc = DEVICE_CLASS(klass);
1220 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1222 k->realize = intel_hda_realize;
1223 k->exit = intel_hda_exit;
1224 k->vendor_id = PCI_VENDOR_ID_INTEL;
1225 k->class_id = PCI_CLASS_MULTIMEDIA_HD_AUDIO;
1226 dc->reset = intel_hda_reset;
1227 dc->vmsd = &vmstate_intel_hda;
1228 dc->props = intel_hda_properties;
1231 static void intel_hda_class_init_ich6(ObjectClass *klass, void *data)
1233 DeviceClass *dc = DEVICE_CLASS(klass);
1234 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1236 k->device_id = 0x2668;
1238 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
1239 dc->desc = "Intel HD Audio Controller (ich6)";
1242 static void intel_hda_class_init_ich9(ObjectClass *klass, void *data)
1244 DeviceClass *dc = DEVICE_CLASS(klass);
1245 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1247 k->device_id = 0x293e;
1249 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
1250 dc->desc = "Intel HD Audio Controller (ich9)";
1253 static const TypeInfo intel_hda_info = {
1254 .name = TYPE_INTEL_HDA_GENERIC,
1255 .parent = TYPE_PCI_DEVICE,
1256 .instance_size = sizeof(IntelHDAState),
1257 .class_init = intel_hda_class_init,
1259 .interfaces = (InterfaceInfo[]) {
1260 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1265 static const TypeInfo intel_hda_info_ich6 = {
1266 .name = "intel-hda",
1267 .parent = TYPE_INTEL_HDA_GENERIC,
1268 .class_init = intel_hda_class_init_ich6,
1271 static const TypeInfo intel_hda_info_ich9 = {
1272 .name = "ich9-intel-hda",
1273 .parent = TYPE_INTEL_HDA_GENERIC,
1274 .class_init = intel_hda_class_init_ich9,
1277 static void hda_codec_device_class_init(ObjectClass *klass, void *data)
1279 DeviceClass *k = DEVICE_CLASS(klass);
1280 k->realize = hda_codec_dev_realize;
1281 k->unrealize = hda_codec_dev_unrealize;
1282 set_bit(DEVICE_CATEGORY_SOUND, k->categories);
1283 k->bus_type = TYPE_HDA_BUS;
1284 k->props = hda_props;
1287 static const TypeInfo hda_codec_device_type_info = {
1288 .name = TYPE_HDA_CODEC_DEVICE,
1289 .parent = TYPE_DEVICE,
1290 .instance_size = sizeof(HDACodecDevice),
1292 .class_size = sizeof(HDACodecDeviceClass),
1293 .class_init = hda_codec_device_class_init,
1297 * create intel hda controller with codec attached to it,
1298 * so '-soundhw hda' works.
1300 static int intel_hda_and_codec_init(PCIBus *bus)
1302 DeviceState *controller;
1306 controller = DEVICE(pci_create_simple(bus, -1, "intel-hda"));
1307 hdabus = QLIST_FIRST(&controller->child_bus);
1308 codec = qdev_create(hdabus, "hda-duplex");
1309 qdev_init_nofail(codec);
1313 static void intel_hda_register_types(void)
1315 type_register_static(&hda_codec_bus_info);
1316 type_register_static(&intel_hda_info);
1317 type_register_static(&intel_hda_info_ich6);
1318 type_register_static(&intel_hda_info_ich9);
1319 type_register_static(&hda_codec_device_type_info);
1320 pci_register_soundhw("hda", "Intel HD Audio", intel_hda_and_codec_init);
1323 type_init(intel_hda_register_types)