xref: /openbmc/qemu/hw/audio/intel-hda.c (revision 8692aa29)
1 /*
2  * Copyright (C) 2010 Red Hat, Inc.
3  *
4  * written by Gerd Hoffmann <kraxel@redhat.com>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "hw/hw.h"
22 #include "hw/pci/pci.h"
23 #include "hw/pci/msi.h"
24 #include "qemu/timer.h"
25 #include "hw/audio/audio.h"
26 #include "intel-hda.h"
27 #include "intel-hda-defs.h"
28 #include "sysemu/dma.h"
29 #include "qapi/error.h"
30 
31 /* --------------------------------------------------------------------- */
32 /* hda bus                                                               */
33 
34 static Property hda_props[] = {
35     DEFINE_PROP_UINT32("cad", HDACodecDevice, cad, -1),
36     DEFINE_PROP_END_OF_LIST()
37 };
38 
39 static const TypeInfo hda_codec_bus_info = {
40     .name = TYPE_HDA_BUS,
41     .parent = TYPE_BUS,
42     .instance_size = sizeof(HDACodecBus),
43 };
44 
45 void hda_codec_bus_init(DeviceState *dev, HDACodecBus *bus, size_t bus_size,
46                         hda_codec_response_func response,
47                         hda_codec_xfer_func xfer)
48 {
49     qbus_create_inplace(bus, bus_size, TYPE_HDA_BUS, dev, NULL);
50     bus->response = response;
51     bus->xfer = xfer;
52 }
53 
54 static void hda_codec_dev_realize(DeviceState *qdev, Error **errp)
55 {
56     HDACodecBus *bus = HDA_BUS(qdev->parent_bus);
57     HDACodecDevice *dev = HDA_CODEC_DEVICE(qdev);
58     HDACodecDeviceClass *cdc = HDA_CODEC_DEVICE_GET_CLASS(dev);
59 
60     if (dev->cad == -1) {
61         dev->cad = bus->next_cad;
62     }
63     if (dev->cad >= 15) {
64         error_setg(errp, "HDA audio codec address is full");
65         return;
66     }
67     bus->next_cad = dev->cad + 1;
68     if (cdc->init(dev) != 0) {
69         error_setg(errp, "HDA audio init failed");
70     }
71 }
72 
73 static int hda_codec_dev_exit(DeviceState *qdev)
74 {
75     HDACodecDevice *dev = HDA_CODEC_DEVICE(qdev);
76     HDACodecDeviceClass *cdc = HDA_CODEC_DEVICE_GET_CLASS(dev);
77 
78     if (cdc->exit) {
79         cdc->exit(dev);
80     }
81     return 0;
82 }
83 
84 HDACodecDevice *hda_codec_find(HDACodecBus *bus, uint32_t cad)
85 {
86     BusChild *kid;
87     HDACodecDevice *cdev;
88 
89     QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
90         DeviceState *qdev = kid->child;
91         cdev = HDA_CODEC_DEVICE(qdev);
92         if (cdev->cad == cad) {
93             return cdev;
94         }
95     }
96     return NULL;
97 }
98 
99 void hda_codec_response(HDACodecDevice *dev, bool solicited, uint32_t response)
100 {
101     HDACodecBus *bus = HDA_BUS(dev->qdev.parent_bus);
102     bus->response(dev, solicited, response);
103 }
104 
105 bool hda_codec_xfer(HDACodecDevice *dev, uint32_t stnr, bool output,
106                     uint8_t *buf, uint32_t len)
107 {
108     HDACodecBus *bus = HDA_BUS(dev->qdev.parent_bus);
109     return bus->xfer(dev, stnr, output, buf, len);
110 }
111 
112 /* --------------------------------------------------------------------- */
113 /* intel hda emulation                                                   */
114 
115 typedef struct IntelHDAStream IntelHDAStream;
116 typedef struct IntelHDAState IntelHDAState;
117 typedef struct IntelHDAReg IntelHDAReg;
118 
119 typedef struct bpl {
120     uint64_t addr;
121     uint32_t len;
122     uint32_t flags;
123 } bpl;
124 
125 struct IntelHDAStream {
126     /* registers */
127     uint32_t ctl;
128     uint32_t lpib;
129     uint32_t cbl;
130     uint32_t lvi;
131     uint32_t fmt;
132     uint32_t bdlp_lbase;
133     uint32_t bdlp_ubase;
134 
135     /* state */
136     bpl      *bpl;
137     uint32_t bentries;
138     uint32_t bsize, be, bp;
139 };
140 
141 struct IntelHDAState {
142     PCIDevice pci;
143     const char *name;
144     HDACodecBus codecs;
145 
146     /* registers */
147     uint32_t g_ctl;
148     uint32_t wake_en;
149     uint32_t state_sts;
150     uint32_t int_ctl;
151     uint32_t int_sts;
152     uint32_t wall_clk;
153 
154     uint32_t corb_lbase;
155     uint32_t corb_ubase;
156     uint32_t corb_rp;
157     uint32_t corb_wp;
158     uint32_t corb_ctl;
159     uint32_t corb_sts;
160     uint32_t corb_size;
161 
162     uint32_t rirb_lbase;
163     uint32_t rirb_ubase;
164     uint32_t rirb_wp;
165     uint32_t rirb_cnt;
166     uint32_t rirb_ctl;
167     uint32_t rirb_sts;
168     uint32_t rirb_size;
169 
170     uint32_t dp_lbase;
171     uint32_t dp_ubase;
172 
173     uint32_t icw;
174     uint32_t irr;
175     uint32_t ics;
176 
177     /* streams */
178     IntelHDAStream st[8];
179 
180     /* state */
181     MemoryRegion mmio;
182     uint32_t rirb_count;
183     int64_t wall_base_ns;
184 
185     /* debug logging */
186     const IntelHDAReg *last_reg;
187     uint32_t last_val;
188     uint32_t last_write;
189     uint32_t last_sec;
190     uint32_t repeat_count;
191 
192     /* properties */
193     uint32_t debug;
194     uint32_t msi;
195     bool old_msi_addr;
196 };
197 
198 #define TYPE_INTEL_HDA_GENERIC "intel-hda-generic"
199 
200 #define INTEL_HDA(obj) \
201     OBJECT_CHECK(IntelHDAState, (obj), TYPE_INTEL_HDA_GENERIC)
202 
203 struct IntelHDAReg {
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 */
211     uint32_t   stream;
212     void       (*whandler)(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old);
213     void       (*rhandler)(IntelHDAState *d, const IntelHDAReg *reg);
214 };
215 
216 static void intel_hda_reset(DeviceState *dev);
217 
218 /* --------------------------------------------------------------------- */
219 
220 static hwaddr intel_hda_addr(uint32_t lbase, uint32_t ubase)
221 {
222     return ((uint64_t)ubase << 32) | lbase;
223 }
224 
225 static void intel_hda_update_int_sts(IntelHDAState *d)
226 {
227     uint32_t sts = 0;
228     uint32_t i;
229 
230     /* update controller status */
231     if (d->rirb_sts & ICH6_RBSTS_IRQ) {
232         sts |= (1 << 30);
233     }
234     if (d->rirb_sts & ICH6_RBSTS_OVERRUN) {
235         sts |= (1 << 30);
236     }
237     if (d->state_sts & d->wake_en) {
238         sts |= (1 << 30);
239     }
240 
241     /* update stream status */
242     for (i = 0; i < 8; i++) {
243         /* buffer completion interrupt */
244         if (d->st[i].ctl & (1 << 26)) {
245             sts |= (1 << i);
246         }
247     }
248 
249     /* update global status */
250     if (sts & d->int_ctl) {
251         sts |= (1U << 31);
252     }
253 
254     d->int_sts = sts;
255 }
256 
257 static void intel_hda_update_irq(IntelHDAState *d)
258 {
259     int msi = d->msi && msi_enabled(&d->pci);
260     int level;
261 
262     intel_hda_update_int_sts(d);
263     if (d->int_sts & (1U << 31) && d->int_ctl & (1U << 31)) {
264         level = 1;
265     } else {
266         level = 0;
267     }
268     dprint(d, 2, "%s: level %d [%s]\n", __FUNCTION__,
269            level, msi ? "msi" : "intx");
270     if (msi) {
271         if (level) {
272             msi_notify(&d->pci, 0);
273         }
274     } else {
275         pci_set_irq(&d->pci, level);
276     }
277 }
278 
279 static int intel_hda_send_command(IntelHDAState *d, uint32_t verb)
280 {
281     uint32_t cad, nid, data;
282     HDACodecDevice *codec;
283     HDACodecDeviceClass *cdc;
284 
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", __FUNCTION__);
289         return -1;
290     }
291     nid = (verb >> 20) & 0x7f;
292     data = verb & 0xfffff;
293 
294     codec = hda_codec_find(&d->codecs, cad);
295     if (codec == NULL) {
296         dprint(d, 1, "%s: addressed non-existing codec\n", __FUNCTION__);
297         return -1;
298     }
299     cdc = HDA_CODEC_DEVICE_GET_CLASS(codec);
300     cdc->command(codec, nid, data);
301     return 0;
302 }
303 
304 static void intel_hda_corb_run(IntelHDAState *d)
305 {
306     hwaddr addr;
307     uint32_t rp, verb;
308 
309     if (d->ics & ICH6_IRS_BUSY) {
310         dprint(d, 2, "%s: [icw] verb 0x%08x\n", __FUNCTION__, d->icw);
311         intel_hda_send_command(d, d->icw);
312         return;
313     }
314 
315     for (;;) {
316         if (!(d->corb_ctl & ICH6_CORBCTL_RUN)) {
317             dprint(d, 2, "%s: !run\n", __FUNCTION__);
318             return;
319         }
320         if ((d->corb_rp & 0xff) == d->corb_wp) {
321             dprint(d, 2, "%s: corb ring empty\n", __FUNCTION__);
322             return;
323         }
324         if (d->rirb_count == d->rirb_cnt) {
325             dprint(d, 2, "%s: rirb count reached\n", __FUNCTION__);
326             return;
327         }
328 
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);
332         d->corb_rp = rp;
333 
334         dprint(d, 2, "%s: [rp 0x%x] verb 0x%08x\n", __FUNCTION__, rp, verb);
335         intel_hda_send_command(d, verb);
336     }
337 }
338 
339 static void intel_hda_response(HDACodecDevice *dev, bool solicited, uint32_t response)
340 {
341     HDACodecBus *bus = HDA_BUS(dev->qdev.parent_bus);
342     IntelHDAState *d = container_of(bus, IntelHDAState, codecs);
343     hwaddr addr;
344     uint32_t wp, ex;
345 
346     if (d->ics & ICH6_IRS_BUSY) {
347         dprint(d, 2, "%s: [irr] response 0x%x, cad 0x%x\n",
348                __FUNCTION__, response, dev->cad);
349         d->irr = response;
350         d->ics &= ~(ICH6_IRS_BUSY | 0xf0);
351         d->ics |= (ICH6_IRS_VALID | (dev->cad << 4));
352         return;
353     }
354 
355     if (!(d->rirb_ctl & ICH6_RBCTL_DMA_EN)) {
356         dprint(d, 1, "%s: rirb dma disabled, drop codec response\n", __FUNCTION__);
357         return;
358     }
359 
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);
365     d->rirb_wp = wp;
366 
367     dprint(d, 2, "%s: [wp 0x%x] response 0x%x, extra 0x%x\n",
368            __FUNCTION__, wp, response, ex);
369 
370     d->rirb_count++;
371     if (d->rirb_count == d->rirb_cnt) {
372         dprint(d, 2, "%s: rirb count reached (%d)\n", __FUNCTION__, 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);
376         }
377     } else if ((d->corb_rp & 0xff) == d->corb_wp) {
378         dprint(d, 2, "%s: corb ring empty (%d/%d)\n", __FUNCTION__,
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);
383         }
384     }
385 }
386 
387 static bool intel_hda_xfer(HDACodecDevice *dev, uint32_t stnr, bool output,
388                            uint8_t *buf, uint32_t len)
389 {
390     HDACodecBus *bus = HDA_BUS(dev->qdev.parent_bus);
391     IntelHDAState *d = container_of(bus, IntelHDAState, codecs);
392     hwaddr addr;
393     uint32_t s, copy, left;
394     IntelHDAStream *st;
395     bool irq = false;
396 
397     st = output ? d->st + 4 : d->st;
398     for (s = 0; s < 4; s++) {
399         if (stnr == ((st[s].ctl >> 20) & 0x0f)) {
400             st = st + s;
401             break;
402         }
403     }
404     if (s == 4) {
405         return false;
406     }
407     if (st->bpl == NULL) {
408         return false;
409     }
410     if (st->ctl & (1 << 26)) {
411         /*
412          * Wait with the next DMA xfer until the guest
413          * has acked the buffer completion interrupt
414          */
415         return false;
416     }
417 
418     left = len;
419     while (left > 0) {
420         copy = left;
421         if (copy > st->bsize - st->lpib)
422             copy = st->bsize - st->lpib;
423         if (copy > st->bpl[st->be].len - st->bp)
424             copy = st->bpl[st->be].len - st->bp;
425 
426         dprint(d, 3, "dma: entry %d, pos %d/%d, copy %d\n",
427                st->be, st->bp, st->bpl[st->be].len, copy);
428 
429         pci_dma_rw(&d->pci, st->bpl[st->be].addr + st->bp, buf, copy, !output);
430         st->lpib += copy;
431         st->bp += copy;
432         buf += copy;
433         left -= copy;
434 
435         if (st->bpl[st->be].len == st->bp) {
436             /* bpl entry filled */
437             if (st->bpl[st->be].flags & 0x01) {
438                 irq = true;
439             }
440             st->bp = 0;
441             st->be++;
442             if (st->be == st->bentries) {
443                 /* bpl wrap around */
444                 st->be = 0;
445                 st->lpib = 0;
446             }
447         }
448     }
449     if (d->dp_lbase & 0x01) {
450         s = st - d->st;
451         addr = intel_hda_addr(d->dp_lbase & ~0x01, d->dp_ubase);
452         stl_le_pci_dma(&d->pci, addr + 8*s, st->lpib);
453     }
454     dprint(d, 3, "dma: --\n");
455 
456     if (irq) {
457         st->ctl |= (1 << 26); /* buffer completion interrupt */
458         intel_hda_update_irq(d);
459     }
460     return true;
461 }
462 
463 static void intel_hda_parse_bdl(IntelHDAState *d, IntelHDAStream *st)
464 {
465     hwaddr addr;
466     uint8_t buf[16];
467     uint32_t i;
468 
469     addr = intel_hda_addr(st->bdlp_lbase, st->bdlp_ubase);
470     st->bentries = st->lvi +1;
471     g_free(st->bpl);
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);
480     }
481 
482     st->bsize = st->cbl;
483     st->lpib  = 0;
484     st->be    = 0;
485     st->bp    = 0;
486 }
487 
488 static void intel_hda_notify_codecs(IntelHDAState *d, uint32_t stream, bool running, bool output)
489 {
490     BusChild *kid;
491     HDACodecDevice *cdev;
492 
493     QTAILQ_FOREACH(kid, &d->codecs.qbus.children, sibling) {
494         DeviceState *qdev = kid->child;
495         HDACodecDeviceClass *cdc;
496 
497         cdev = HDA_CODEC_DEVICE(qdev);
498         cdc = HDA_CODEC_DEVICE_GET_CLASS(cdev);
499         if (cdc->stream) {
500             cdc->stream(cdev, stream, running, output);
501         }
502     }
503 }
504 
505 /* --------------------------------------------------------------------- */
506 
507 static void intel_hda_set_g_ctl(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
508 {
509     if ((d->g_ctl & ICH6_GCTL_RESET) == 0) {
510         intel_hda_reset(DEVICE(d));
511     }
512 }
513 
514 static void intel_hda_set_wake_en(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
515 {
516     intel_hda_update_irq(d);
517 }
518 
519 static void intel_hda_set_state_sts(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
520 {
521     intel_hda_update_irq(d);
522 }
523 
524 static void intel_hda_set_int_ctl(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
525 {
526     intel_hda_update_irq(d);
527 }
528 
529 static void intel_hda_get_wall_clk(IntelHDAState *d, const IntelHDAReg *reg)
530 {
531     int64_t ns;
532 
533     ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - d->wall_base_ns;
534     d->wall_clk = (uint32_t)(ns * 24 / 1000);  /* 24 MHz */
535 }
536 
537 static void intel_hda_set_corb_wp(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
538 {
539     intel_hda_corb_run(d);
540 }
541 
542 static void intel_hda_set_corb_ctl(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
543 {
544     intel_hda_corb_run(d);
545 }
546 
547 static void intel_hda_set_rirb_wp(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
548 {
549     if (d->rirb_wp & ICH6_RIRBWP_RST) {
550         d->rirb_wp = 0;
551     }
552 }
553 
554 static void intel_hda_set_rirb_sts(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
555 {
556     intel_hda_update_irq(d);
557 
558     if ((old & ICH6_RBSTS_IRQ) && !(d->rirb_sts & ICH6_RBSTS_IRQ)) {
559         /* cleared ICH6_RBSTS_IRQ */
560         d->rirb_count = 0;
561         intel_hda_corb_run(d);
562     }
563 }
564 
565 static void intel_hda_set_ics(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
566 {
567     if (d->ics & ICH6_IRS_BUSY) {
568         intel_hda_corb_run(d);
569     }
570 }
571 
572 static void intel_hda_set_st_ctl(IntelHDAState *d, const IntelHDAReg *reg, uint32_t old)
573 {
574     bool output = reg->stream >= 4;
575     IntelHDAStream *st = d->st + reg->stream;
576 
577     if (st->ctl & 0x01) {
578         /* reset */
579         dprint(d, 1, "st #%d: reset\n", reg->stream);
580         st->ctl = SD_STS_FIFO_READY << 24;
581     }
582     if ((st->ctl & 0x02) != (old & 0x02)) {
583         uint32_t stnr = (st->ctl >> 20) & 0x0f;
584         /* run bit flipped */
585         if (st->ctl & 0x02) {
586             /* start */
587             dprint(d, 1, "st #%d: start %d (ring buf %d bytes)\n",
588                    reg->stream, stnr, st->cbl);
589             intel_hda_parse_bdl(d, st);
590             intel_hda_notify_codecs(d, stnr, true, output);
591         } else {
592             /* stop */
593             dprint(d, 1, "st #%d: stop %d\n", reg->stream, stnr);
594             intel_hda_notify_codecs(d, stnr, false, output);
595         }
596     }
597     intel_hda_update_irq(d);
598 }
599 
600 /* --------------------------------------------------------------------- */
601 
602 #define ST_REG(_n, _o) (0x80 + (_n) * 0x20 + (_o))
603 
604 static const struct IntelHDAReg regtab[] = {
605     /* global */
606     [ ICH6_REG_GCAP ] = {
607         .name     = "GCAP",
608         .size     = 2,
609         .reset    = 0x4401,
610     },
611     [ ICH6_REG_VMIN ] = {
612         .name     = "VMIN",
613         .size     = 1,
614     },
615     [ ICH6_REG_VMAJ ] = {
616         .name     = "VMAJ",
617         .size     = 1,
618         .reset    = 1,
619     },
620     [ ICH6_REG_OUTPAY ] = {
621         .name     = "OUTPAY",
622         .size     = 2,
623         .reset    = 0x3c,
624     },
625     [ ICH6_REG_INPAY ] = {
626         .name     = "INPAY",
627         .size     = 2,
628         .reset    = 0x1d,
629     },
630     [ ICH6_REG_GCTL ] = {
631         .name     = "GCTL",
632         .size     = 4,
633         .wmask    = 0x0103,
634         .offset   = offsetof(IntelHDAState, g_ctl),
635         .whandler = intel_hda_set_g_ctl,
636     },
637     [ ICH6_REG_WAKEEN ] = {
638         .name     = "WAKEEN",
639         .size     = 2,
640         .wmask    = 0x7fff,
641         .offset   = offsetof(IntelHDAState, wake_en),
642         .whandler = intel_hda_set_wake_en,
643     },
644     [ ICH6_REG_STATESTS ] = {
645         .name     = "STATESTS",
646         .size     = 2,
647         .wmask    = 0x7fff,
648         .wclear   = 0x7fff,
649         .offset   = offsetof(IntelHDAState, state_sts),
650         .whandler = intel_hda_set_state_sts,
651     },
652 
653     /* interrupts */
654     [ ICH6_REG_INTCTL ] = {
655         .name     = "INTCTL",
656         .size     = 4,
657         .wmask    = 0xc00000ff,
658         .offset   = offsetof(IntelHDAState, int_ctl),
659         .whandler = intel_hda_set_int_ctl,
660     },
661     [ ICH6_REG_INTSTS ] = {
662         .name     = "INTSTS",
663         .size     = 4,
664         .wmask    = 0xc00000ff,
665         .wclear   = 0xc00000ff,
666         .offset   = offsetof(IntelHDAState, int_sts),
667     },
668 
669     /* misc */
670     [ ICH6_REG_WALLCLK ] = {
671         .name     = "WALLCLK",
672         .size     = 4,
673         .offset   = offsetof(IntelHDAState, wall_clk),
674         .rhandler = intel_hda_get_wall_clk,
675     },
676     [ ICH6_REG_WALLCLK + 0x2000 ] = {
677         .name     = "WALLCLK(alias)",
678         .size     = 4,
679         .offset   = offsetof(IntelHDAState, wall_clk),
680         .rhandler = intel_hda_get_wall_clk,
681     },
682 
683     /* dma engine */
684     [ ICH6_REG_CORBLBASE ] = {
685         .name     = "CORBLBASE",
686         .size     = 4,
687         .wmask    = 0xffffff80,
688         .offset   = offsetof(IntelHDAState, corb_lbase),
689     },
690     [ ICH6_REG_CORBUBASE ] = {
691         .name     = "CORBUBASE",
692         .size     = 4,
693         .wmask    = 0xffffffff,
694         .offset   = offsetof(IntelHDAState, corb_ubase),
695     },
696     [ ICH6_REG_CORBWP ] = {
697         .name     = "CORBWP",
698         .size     = 2,
699         .wmask    = 0xff,
700         .offset   = offsetof(IntelHDAState, corb_wp),
701         .whandler = intel_hda_set_corb_wp,
702     },
703     [ ICH6_REG_CORBRP ] = {
704         .name     = "CORBRP",
705         .size     = 2,
706         .wmask    = 0x80ff,
707         .offset   = offsetof(IntelHDAState, corb_rp),
708     },
709     [ ICH6_REG_CORBCTL ] = {
710         .name     = "CORBCTL",
711         .size     = 1,
712         .wmask    = 0x03,
713         .offset   = offsetof(IntelHDAState, corb_ctl),
714         .whandler = intel_hda_set_corb_ctl,
715     },
716     [ ICH6_REG_CORBSTS ] = {
717         .name     = "CORBSTS",
718         .size     = 1,
719         .wmask    = 0x01,
720         .wclear   = 0x01,
721         .offset   = offsetof(IntelHDAState, corb_sts),
722     },
723     [ ICH6_REG_CORBSIZE ] = {
724         .name     = "CORBSIZE",
725         .size     = 1,
726         .reset    = 0x42,
727         .offset   = offsetof(IntelHDAState, corb_size),
728     },
729     [ ICH6_REG_RIRBLBASE ] = {
730         .name     = "RIRBLBASE",
731         .size     = 4,
732         .wmask    = 0xffffff80,
733         .offset   = offsetof(IntelHDAState, rirb_lbase),
734     },
735     [ ICH6_REG_RIRBUBASE ] = {
736         .name     = "RIRBUBASE",
737         .size     = 4,
738         .wmask    = 0xffffffff,
739         .offset   = offsetof(IntelHDAState, rirb_ubase),
740     },
741     [ ICH6_REG_RIRBWP ] = {
742         .name     = "RIRBWP",
743         .size     = 2,
744         .wmask    = 0x8000,
745         .offset   = offsetof(IntelHDAState, rirb_wp),
746         .whandler = intel_hda_set_rirb_wp,
747     },
748     [ ICH6_REG_RINTCNT ] = {
749         .name     = "RINTCNT",
750         .size     = 2,
751         .wmask    = 0xff,
752         .offset   = offsetof(IntelHDAState, rirb_cnt),
753     },
754     [ ICH6_REG_RIRBCTL ] = {
755         .name     = "RIRBCTL",
756         .size     = 1,
757         .wmask    = 0x07,
758         .offset   = offsetof(IntelHDAState, rirb_ctl),
759     },
760     [ ICH6_REG_RIRBSTS ] = {
761         .name     = "RIRBSTS",
762         .size     = 1,
763         .wmask    = 0x05,
764         .wclear   = 0x05,
765         .offset   = offsetof(IntelHDAState, rirb_sts),
766         .whandler = intel_hda_set_rirb_sts,
767     },
768     [ ICH6_REG_RIRBSIZE ] = {
769         .name     = "RIRBSIZE",
770         .size     = 1,
771         .reset    = 0x42,
772         .offset   = offsetof(IntelHDAState, rirb_size),
773     },
774 
775     [ ICH6_REG_DPLBASE ] = {
776         .name     = "DPLBASE",
777         .size     = 4,
778         .wmask    = 0xffffff81,
779         .offset   = offsetof(IntelHDAState, dp_lbase),
780     },
781     [ ICH6_REG_DPUBASE ] = {
782         .name     = "DPUBASE",
783         .size     = 4,
784         .wmask    = 0xffffffff,
785         .offset   = offsetof(IntelHDAState, dp_ubase),
786     },
787 
788     [ ICH6_REG_IC ] = {
789         .name     = "ICW",
790         .size     = 4,
791         .wmask    = 0xffffffff,
792         .offset   = offsetof(IntelHDAState, icw),
793     },
794     [ ICH6_REG_IR ] = {
795         .name     = "IRR",
796         .size     = 4,
797         .offset   = offsetof(IntelHDAState, irr),
798     },
799     [ ICH6_REG_IRS ] = {
800         .name     = "ICS",
801         .size     = 2,
802         .wmask    = 0x0003,
803         .wclear   = 0x0002,
804         .offset   = offsetof(IntelHDAState, ics),
805         .whandler = intel_hda_set_ics,
806     },
807 
808 #define HDA_STREAM(_t, _i)                                            \
809     [ ST_REG(_i, ICH6_REG_SD_CTL) ] = {                               \
810         .stream   = _i,                                               \
811         .name     = _t stringify(_i) " CTL",                          \
812         .size     = 4,                                                \
813         .wmask    = 0x1cff001f,                                       \
814         .offset   = offsetof(IntelHDAState, st[_i].ctl),              \
815         .whandler = intel_hda_set_st_ctl,                             \
816     },                                                                \
817     [ ST_REG(_i, ICH6_REG_SD_CTL) + 2] = {                            \
818         .stream   = _i,                                               \
819         .name     = _t stringify(_i) " CTL(stnr)",                    \
820         .size     = 1,                                                \
821         .shift    = 16,                                               \
822         .wmask    = 0x00ff0000,                                       \
823         .offset   = offsetof(IntelHDAState, st[_i].ctl),              \
824         .whandler = intel_hda_set_st_ctl,                             \
825     },                                                                \
826     [ ST_REG(_i, ICH6_REG_SD_STS)] = {                                \
827         .stream   = _i,                                               \
828         .name     = _t stringify(_i) " CTL(sts)",                     \
829         .size     = 1,                                                \
830         .shift    = 24,                                               \
831         .wmask    = 0x1c000000,                                       \
832         .wclear   = 0x1c000000,                                       \
833         .offset   = offsetof(IntelHDAState, st[_i].ctl),              \
834         .whandler = intel_hda_set_st_ctl,                             \
835         .reset    = SD_STS_FIFO_READY << 24                           \
836     },                                                                \
837     [ ST_REG(_i, ICH6_REG_SD_LPIB) ] = {                              \
838         .stream   = _i,                                               \
839         .name     = _t stringify(_i) " LPIB",                         \
840         .size     = 4,                                                \
841         .offset   = offsetof(IntelHDAState, st[_i].lpib),             \
842     },                                                                \
843     [ ST_REG(_i, ICH6_REG_SD_LPIB) + 0x2000 ] = {                     \
844         .stream   = _i,                                               \
845         .name     = _t stringify(_i) " LPIB(alias)",                  \
846         .size     = 4,                                                \
847         .offset   = offsetof(IntelHDAState, st[_i].lpib),             \
848     },                                                                \
849     [ ST_REG(_i, ICH6_REG_SD_CBL) ] = {                               \
850         .stream   = _i,                                               \
851         .name     = _t stringify(_i) " CBL",                          \
852         .size     = 4,                                                \
853         .wmask    = 0xffffffff,                                       \
854         .offset   = offsetof(IntelHDAState, st[_i].cbl),              \
855     },                                                                \
856     [ ST_REG(_i, ICH6_REG_SD_LVI) ] = {                               \
857         .stream   = _i,                                               \
858         .name     = _t stringify(_i) " LVI",                          \
859         .size     = 2,                                                \
860         .wmask    = 0x00ff,                                           \
861         .offset   = offsetof(IntelHDAState, st[_i].lvi),              \
862     },                                                                \
863     [ ST_REG(_i, ICH6_REG_SD_FIFOSIZE) ] = {                          \
864         .stream   = _i,                                               \
865         .name     = _t stringify(_i) " FIFOS",                        \
866         .size     = 2,                                                \
867         .reset    = HDA_BUFFER_SIZE,                                  \
868     },                                                                \
869     [ ST_REG(_i, ICH6_REG_SD_FORMAT) ] = {                            \
870         .stream   = _i,                                               \
871         .name     = _t stringify(_i) " FMT",                          \
872         .size     = 2,                                                \
873         .wmask    = 0x7f7f,                                           \
874         .offset   = offsetof(IntelHDAState, st[_i].fmt),              \
875     },                                                                \
876     [ ST_REG(_i, ICH6_REG_SD_BDLPL) ] = {                             \
877         .stream   = _i,                                               \
878         .name     = _t stringify(_i) " BDLPL",                        \
879         .size     = 4,                                                \
880         .wmask    = 0xffffff80,                                       \
881         .offset   = offsetof(IntelHDAState, st[_i].bdlp_lbase),       \
882     },                                                                \
883     [ ST_REG(_i, ICH6_REG_SD_BDLPU) ] = {                             \
884         .stream   = _i,                                               \
885         .name     = _t stringify(_i) " BDLPU",                        \
886         .size     = 4,                                                \
887         .wmask    = 0xffffffff,                                       \
888         .offset   = offsetof(IntelHDAState, st[_i].bdlp_ubase),       \
889     },                                                                \
890 
891     HDA_STREAM("IN", 0)
892     HDA_STREAM("IN", 1)
893     HDA_STREAM("IN", 2)
894     HDA_STREAM("IN", 3)
895 
896     HDA_STREAM("OUT", 4)
897     HDA_STREAM("OUT", 5)
898     HDA_STREAM("OUT", 6)
899     HDA_STREAM("OUT", 7)
900 
901 };
902 
903 static const IntelHDAReg *intel_hda_reg_find(IntelHDAState *d, hwaddr addr)
904 {
905     const IntelHDAReg *reg;
906 
907     if (addr >= ARRAY_SIZE(regtab)) {
908         goto noreg;
909     }
910     reg = regtab+addr;
911     if (reg->name == NULL) {
912         goto noreg;
913     }
914     return reg;
915 
916 noreg:
917     dprint(d, 1, "unknown register, addr 0x%x\n", (int) addr);
918     return NULL;
919 }
920 
921 static uint32_t *intel_hda_reg_addr(IntelHDAState *d, const IntelHDAReg *reg)
922 {
923     uint8_t *addr = (void*)d;
924 
925     addr += reg->offset;
926     return (uint32_t*)addr;
927 }
928 
929 static void intel_hda_reg_write(IntelHDAState *d, const IntelHDAReg *reg, uint32_t val,
930                                 uint32_t wmask)
931 {
932     uint32_t *addr;
933     uint32_t old;
934 
935     if (!reg) {
936         return;
937     }
938 
939     if (d->debug) {
940         time_t now = time(NULL);
941         if (d->last_write && d->last_reg == reg && d->last_val == val) {
942             d->repeat_count++;
943             if (d->last_sec != now) {
944                 dprint(d, 2, "previous register op repeated %d times\n", d->repeat_count);
945                 d->last_sec = now;
946                 d->repeat_count = 0;
947             }
948         } else {
949             if (d->repeat_count) {
950                 dprint(d, 2, "previous register op repeated %d times\n", d->repeat_count);
951             }
952             dprint(d, 2, "write %-16s: 0x%x (%x)\n", reg->name, val, wmask);
953             d->last_write = 1;
954             d->last_reg   = reg;
955             d->last_val   = val;
956             d->last_sec   = now;
957             d->repeat_count = 0;
958         }
959     }
960     assert(reg->offset != 0);
961 
962     addr = intel_hda_reg_addr(d, reg);
963     old = *addr;
964 
965     if (reg->shift) {
966         val <<= reg->shift;
967         wmask <<= reg->shift;
968     }
969     wmask &= reg->wmask;
970     *addr &= ~wmask;
971     *addr |= wmask & val;
972     *addr &= ~(val & reg->wclear);
973 
974     if (reg->whandler) {
975         reg->whandler(d, reg, old);
976     }
977 }
978 
979 static uint32_t intel_hda_reg_read(IntelHDAState *d, const IntelHDAReg *reg,
980                                    uint32_t rmask)
981 {
982     uint32_t *addr, ret;
983 
984     if (!reg) {
985         return 0;
986     }
987 
988     if (reg->rhandler) {
989         reg->rhandler(d, reg);
990     }
991 
992     if (reg->offset == 0) {
993         /* constant read-only register */
994         ret = reg->reset;
995     } else {
996         addr = intel_hda_reg_addr(d, reg);
997         ret = *addr;
998         if (reg->shift) {
999             ret >>= reg->shift;
1000         }
1001         ret &= rmask;
1002     }
1003     if (d->debug) {
1004         time_t now = time(NULL);
1005         if (!d->last_write && d->last_reg == reg && d->last_val == ret) {
1006             d->repeat_count++;
1007             if (d->last_sec != now) {
1008                 dprint(d, 2, "previous register op repeated %d times\n", d->repeat_count);
1009                 d->last_sec = now;
1010                 d->repeat_count = 0;
1011             }
1012         } else {
1013             if (d->repeat_count) {
1014                 dprint(d, 2, "previous register op repeated %d times\n", d->repeat_count);
1015             }
1016             dprint(d, 2, "read  %-16s: 0x%x (%x)\n", reg->name, ret, rmask);
1017             d->last_write = 0;
1018             d->last_reg   = reg;
1019             d->last_val   = ret;
1020             d->last_sec   = now;
1021             d->repeat_count = 0;
1022         }
1023     }
1024     return ret;
1025 }
1026 
1027 static void intel_hda_regs_reset(IntelHDAState *d)
1028 {
1029     uint32_t *addr;
1030     int i;
1031 
1032     for (i = 0; i < ARRAY_SIZE(regtab); i++) {
1033         if (regtab[i].name == NULL) {
1034             continue;
1035         }
1036         if (regtab[i].offset == 0) {
1037             continue;
1038         }
1039         addr = intel_hda_reg_addr(d, regtab + i);
1040         *addr = regtab[i].reset;
1041     }
1042 }
1043 
1044 /* --------------------------------------------------------------------- */
1045 
1046 static void intel_hda_mmio_writeb(void *opaque, hwaddr addr, uint32_t val)
1047 {
1048     IntelHDAState *d = opaque;
1049     const IntelHDAReg *reg = intel_hda_reg_find(d, addr);
1050 
1051     intel_hda_reg_write(d, reg, val, 0xff);
1052 }
1053 
1054 static void intel_hda_mmio_writew(void *opaque, hwaddr addr, uint32_t val)
1055 {
1056     IntelHDAState *d = opaque;
1057     const IntelHDAReg *reg = intel_hda_reg_find(d, addr);
1058 
1059     intel_hda_reg_write(d, reg, val, 0xffff);
1060 }
1061 
1062 static void intel_hda_mmio_writel(void *opaque, hwaddr addr, uint32_t val)
1063 {
1064     IntelHDAState *d = opaque;
1065     const IntelHDAReg *reg = intel_hda_reg_find(d, addr);
1066 
1067     intel_hda_reg_write(d, reg, val, 0xffffffff);
1068 }
1069 
1070 static uint32_t intel_hda_mmio_readb(void *opaque, hwaddr addr)
1071 {
1072     IntelHDAState *d = opaque;
1073     const IntelHDAReg *reg = intel_hda_reg_find(d, addr);
1074 
1075     return intel_hda_reg_read(d, reg, 0xff);
1076 }
1077 
1078 static uint32_t intel_hda_mmio_readw(void *opaque, hwaddr addr)
1079 {
1080     IntelHDAState *d = opaque;
1081     const IntelHDAReg *reg = intel_hda_reg_find(d, addr);
1082 
1083     return intel_hda_reg_read(d, reg, 0xffff);
1084 }
1085 
1086 static uint32_t intel_hda_mmio_readl(void *opaque, hwaddr addr)
1087 {
1088     IntelHDAState *d = opaque;
1089     const IntelHDAReg *reg = intel_hda_reg_find(d, addr);
1090 
1091     return intel_hda_reg_read(d, reg, 0xffffffff);
1092 }
1093 
1094 static const MemoryRegionOps intel_hda_mmio_ops = {
1095     .old_mmio = {
1096         .read = {
1097             intel_hda_mmio_readb,
1098             intel_hda_mmio_readw,
1099             intel_hda_mmio_readl,
1100         },
1101         .write = {
1102             intel_hda_mmio_writeb,
1103             intel_hda_mmio_writew,
1104             intel_hda_mmio_writel,
1105         },
1106     },
1107     .endianness = DEVICE_NATIVE_ENDIAN,
1108 };
1109 
1110 /* --------------------------------------------------------------------- */
1111 
1112 static void intel_hda_reset(DeviceState *dev)
1113 {
1114     BusChild *kid;
1115     IntelHDAState *d = INTEL_HDA(dev);
1116     HDACodecDevice *cdev;
1117 
1118     intel_hda_regs_reset(d);
1119     d->wall_base_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1120 
1121     /* reset codecs */
1122     QTAILQ_FOREACH(kid, &d->codecs.qbus.children, sibling) {
1123         DeviceState *qdev = kid->child;
1124         cdev = HDA_CODEC_DEVICE(qdev);
1125         device_reset(DEVICE(cdev));
1126         d->state_sts |= (1 << cdev->cad);
1127     }
1128     intel_hda_update_irq(d);
1129 }
1130 
1131 static void intel_hda_realize(PCIDevice *pci, Error **errp)
1132 {
1133     IntelHDAState *d = INTEL_HDA(pci);
1134     uint8_t *conf = d->pci.config;
1135 
1136     d->name = object_get_typename(OBJECT(d));
1137 
1138     pci_config_set_interrupt_pin(conf, 1);
1139 
1140     /* HDCTL off 0x40 bit 0 selects signaling mode (1-HDA, 0 - Ac97) 18.1.19 */
1141     conf[0x40] = 0x01;
1142 
1143     memory_region_init_io(&d->mmio, OBJECT(d), &intel_hda_mmio_ops, d,
1144                           "intel-hda", 0x4000);
1145     pci_register_bar(&d->pci, 0, 0, &d->mmio);
1146     if (d->msi) {
1147         msi_init(&d->pci, d->old_msi_addr ? 0x50 : 0x60, 1, true, false);
1148     }
1149 
1150     hda_codec_bus_init(DEVICE(pci), &d->codecs, sizeof(d->codecs),
1151                        intel_hda_response, intel_hda_xfer);
1152 }
1153 
1154 static void intel_hda_exit(PCIDevice *pci)
1155 {
1156     IntelHDAState *d = INTEL_HDA(pci);
1157 
1158     msi_uninit(&d->pci);
1159 }
1160 
1161 static int intel_hda_post_load(void *opaque, int version)
1162 {
1163     IntelHDAState* d = opaque;
1164     int i;
1165 
1166     dprint(d, 1, "%s\n", __FUNCTION__);
1167     for (i = 0; i < ARRAY_SIZE(d->st); i++) {
1168         if (d->st[i].ctl & 0x02) {
1169             intel_hda_parse_bdl(d, &d->st[i]);
1170         }
1171     }
1172     intel_hda_update_irq(d);
1173     return 0;
1174 }
1175 
1176 static const VMStateDescription vmstate_intel_hda_stream = {
1177     .name = "intel-hda-stream",
1178     .version_id = 1,
1179     .fields = (VMStateField[]) {
1180         VMSTATE_UINT32(ctl, IntelHDAStream),
1181         VMSTATE_UINT32(lpib, IntelHDAStream),
1182         VMSTATE_UINT32(cbl, IntelHDAStream),
1183         VMSTATE_UINT32(lvi, IntelHDAStream),
1184         VMSTATE_UINT32(fmt, IntelHDAStream),
1185         VMSTATE_UINT32(bdlp_lbase, IntelHDAStream),
1186         VMSTATE_UINT32(bdlp_ubase, IntelHDAStream),
1187         VMSTATE_END_OF_LIST()
1188     }
1189 };
1190 
1191 static const VMStateDescription vmstate_intel_hda = {
1192     .name = "intel-hda",
1193     .version_id = 1,
1194     .post_load = intel_hda_post_load,
1195     .fields = (VMStateField[]) {
1196         VMSTATE_PCI_DEVICE(pci, IntelHDAState),
1197 
1198         /* registers */
1199         VMSTATE_UINT32(g_ctl, IntelHDAState),
1200         VMSTATE_UINT32(wake_en, IntelHDAState),
1201         VMSTATE_UINT32(state_sts, IntelHDAState),
1202         VMSTATE_UINT32(int_ctl, IntelHDAState),
1203         VMSTATE_UINT32(int_sts, IntelHDAState),
1204         VMSTATE_UINT32(wall_clk, IntelHDAState),
1205         VMSTATE_UINT32(corb_lbase, IntelHDAState),
1206         VMSTATE_UINT32(corb_ubase, IntelHDAState),
1207         VMSTATE_UINT32(corb_rp, IntelHDAState),
1208         VMSTATE_UINT32(corb_wp, IntelHDAState),
1209         VMSTATE_UINT32(corb_ctl, IntelHDAState),
1210         VMSTATE_UINT32(corb_sts, IntelHDAState),
1211         VMSTATE_UINT32(corb_size, IntelHDAState),
1212         VMSTATE_UINT32(rirb_lbase, IntelHDAState),
1213         VMSTATE_UINT32(rirb_ubase, IntelHDAState),
1214         VMSTATE_UINT32(rirb_wp, IntelHDAState),
1215         VMSTATE_UINT32(rirb_cnt, IntelHDAState),
1216         VMSTATE_UINT32(rirb_ctl, IntelHDAState),
1217         VMSTATE_UINT32(rirb_sts, IntelHDAState),
1218         VMSTATE_UINT32(rirb_size, IntelHDAState),
1219         VMSTATE_UINT32(dp_lbase, IntelHDAState),
1220         VMSTATE_UINT32(dp_ubase, IntelHDAState),
1221         VMSTATE_UINT32(icw, IntelHDAState),
1222         VMSTATE_UINT32(irr, IntelHDAState),
1223         VMSTATE_UINT32(ics, IntelHDAState),
1224         VMSTATE_STRUCT_ARRAY(st, IntelHDAState, 8, 0,
1225                              vmstate_intel_hda_stream,
1226                              IntelHDAStream),
1227 
1228         /* additional state info */
1229         VMSTATE_UINT32(rirb_count, IntelHDAState),
1230         VMSTATE_INT64(wall_base_ns, IntelHDAState),
1231 
1232         VMSTATE_END_OF_LIST()
1233     }
1234 };
1235 
1236 static Property intel_hda_properties[] = {
1237     DEFINE_PROP_UINT32("debug", IntelHDAState, debug, 0),
1238     DEFINE_PROP_UINT32("msi", IntelHDAState, msi, 1),
1239     DEFINE_PROP_BOOL("old_msi_addr", IntelHDAState, old_msi_addr, false),
1240     DEFINE_PROP_END_OF_LIST(),
1241 };
1242 
1243 static void intel_hda_class_init(ObjectClass *klass, void *data)
1244 {
1245     DeviceClass *dc = DEVICE_CLASS(klass);
1246     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1247 
1248     k->realize = intel_hda_realize;
1249     k->exit = intel_hda_exit;
1250     k->vendor_id = PCI_VENDOR_ID_INTEL;
1251     k->class_id = PCI_CLASS_MULTIMEDIA_HD_AUDIO;
1252     dc->reset = intel_hda_reset;
1253     dc->vmsd = &vmstate_intel_hda;
1254     dc->props = intel_hda_properties;
1255 }
1256 
1257 static void intel_hda_class_init_ich6(ObjectClass *klass, void *data)
1258 {
1259     DeviceClass *dc = DEVICE_CLASS(klass);
1260     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1261 
1262     k->device_id = 0x2668;
1263     k->revision = 1;
1264     set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
1265     dc->desc = "Intel HD Audio Controller (ich6)";
1266 }
1267 
1268 static void intel_hda_class_init_ich9(ObjectClass *klass, void *data)
1269 {
1270     DeviceClass *dc = DEVICE_CLASS(klass);
1271     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1272 
1273     k->device_id = 0x293e;
1274     k->revision = 3;
1275     set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
1276     dc->desc = "Intel HD Audio Controller (ich9)";
1277 }
1278 
1279 static const TypeInfo intel_hda_info = {
1280     .name          = TYPE_INTEL_HDA_GENERIC,
1281     .parent        = TYPE_PCI_DEVICE,
1282     .instance_size = sizeof(IntelHDAState),
1283     .class_init    = intel_hda_class_init,
1284     .abstract      = true,
1285 };
1286 
1287 static const TypeInfo intel_hda_info_ich6 = {
1288     .name          = "intel-hda",
1289     .parent        = TYPE_INTEL_HDA_GENERIC,
1290     .class_init    = intel_hda_class_init_ich6,
1291 };
1292 
1293 static const TypeInfo intel_hda_info_ich9 = {
1294     .name          = "ich9-intel-hda",
1295     .parent        = TYPE_INTEL_HDA_GENERIC,
1296     .class_init    = intel_hda_class_init_ich9,
1297 };
1298 
1299 static void hda_codec_device_class_init(ObjectClass *klass, void *data)
1300 {
1301     DeviceClass *k = DEVICE_CLASS(klass);
1302     k->realize = hda_codec_dev_realize;
1303     k->exit = hda_codec_dev_exit;
1304     set_bit(DEVICE_CATEGORY_SOUND, k->categories);
1305     k->bus_type = TYPE_HDA_BUS;
1306     k->props = hda_props;
1307 }
1308 
1309 static const TypeInfo hda_codec_device_type_info = {
1310     .name = TYPE_HDA_CODEC_DEVICE,
1311     .parent = TYPE_DEVICE,
1312     .instance_size = sizeof(HDACodecDevice),
1313     .abstract = true,
1314     .class_size = sizeof(HDACodecDeviceClass),
1315     .class_init = hda_codec_device_class_init,
1316 };
1317 
1318 /*
1319  * create intel hda controller with codec attached to it,
1320  * so '-soundhw hda' works.
1321  */
1322 static int intel_hda_and_codec_init(PCIBus *bus)
1323 {
1324     DeviceState *controller;
1325     BusState *hdabus;
1326     DeviceState *codec;
1327 
1328     controller = DEVICE(pci_create_simple(bus, -1, "intel-hda"));
1329     hdabus = QLIST_FIRST(&controller->child_bus);
1330     codec = qdev_create(hdabus, "hda-duplex");
1331     qdev_init_nofail(codec);
1332     return 0;
1333 }
1334 
1335 static void intel_hda_register_types(void)
1336 {
1337     type_register_static(&hda_codec_bus_info);
1338     type_register_static(&intel_hda_info);
1339     type_register_static(&intel_hda_info_ich6);
1340     type_register_static(&intel_hda_info_ich9);
1341     type_register_static(&hda_codec_device_type_info);
1342     pci_register_soundhw("hda", "Intel HD Audio", intel_hda_and_codec_init);
1343 }
1344 
1345 type_init(intel_hda_register_types)
1346