2 * Copyright (c) 2006 Stephane E. Potvin <sepotvin@videotron.ca>
3 * Copyright (c) 2006 Ariff Abdullah <ariff@FreeBSD.org>
4 * Copyright (c) 2008-2012 Alexander Motin <mav@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Intel High Definition Audio (Controller) driver for FreeBSD.
33 #ifdef HAVE_KERNEL_OPTION_HEADERS
37 #include <dev/sound/pcm/sound.h>
38 #include <dev/pci/pcireg.h>
39 #include <dev/pci/pcivar.h>
41 #include <sys/ctype.h>
42 #include <sys/taskqueue.h>
44 #include <dev/sound/pci/hda/hdac_private.h>
45 #include <dev/sound/pci/hda/hdac_reg.h>
46 #include <dev/sound/pci/hda/hda_reg.h>
47 #include <dev/sound/pci/hda/hdac.h>
49 #define HDA_DRV_TEST_REV "20120126_0002"
51 SND_DECLARE_FILE("$FreeBSD$");
53 #define hdac_lock(sc) snd_mtxlock((sc)->lock)
54 #define hdac_unlock(sc) snd_mtxunlock((sc)->lock)
55 #define hdac_lockassert(sc) snd_mtxassert((sc)->lock)
56 #define hdac_lockowned(sc) mtx_owned((sc)->lock)
58 #define HDAC_QUIRK_64BIT (1 << 0)
59 #define HDAC_QUIRK_DMAPOS (1 << 1)
60 #define HDAC_QUIRK_MSI (1 << 2)
65 } hdac_quirks_tab[] = {
66 { "64bit", HDAC_QUIRK_DMAPOS },
67 { "dmapos", HDAC_QUIRK_DMAPOS },
68 { "msi", HDAC_QUIRK_MSI },
70 #define HDAC_QUIRKS_TAB_LEN \
71 (sizeof(hdac_quirks_tab) / sizeof(hdac_quirks_tab[0]))
73 MALLOC_DEFINE(M_HDAC, "hdac", "HDA Controller");
81 { HDA_INTEL_CPT, "Intel Cougar Point", 0, 0 },
82 { HDA_INTEL_PATSBURG,"Intel Patsburg", 0, 0 },
83 { HDA_INTEL_PPT1, "Intel Panther Point", 0, 0 },
84 { HDA_INTEL_LPT1, "Intel Lynx Point", 0, 0 },
85 { HDA_INTEL_LPT2, "Intel Lynx Point", 0, 0 },
86 { HDA_INTEL_82801F, "Intel 82801F", 0, 0 },
87 { HDA_INTEL_63XXESB, "Intel 631x/632xESB", 0, 0 },
88 { HDA_INTEL_82801G, "Intel 82801G", 0, 0 },
89 { HDA_INTEL_82801H, "Intel 82801H", 0, 0 },
90 { HDA_INTEL_82801I, "Intel 82801I", 0, 0 },
91 { HDA_INTEL_82801JI, "Intel 82801JI", 0, 0 },
92 { HDA_INTEL_82801JD, "Intel 82801JD", 0, 0 },
93 { HDA_INTEL_PCH, "Intel 5 Series/3400 Series", 0, 0 },
94 { HDA_INTEL_PCH2, "Intel 5 Series/3400 Series", 0, 0 },
95 { HDA_INTEL_SCH, "Intel SCH", 0, 0 },
96 { HDA_NVIDIA_MCP51, "NVIDIA MCP51", 0, HDAC_QUIRK_MSI },
97 { HDA_NVIDIA_MCP55, "NVIDIA MCP55", 0, HDAC_QUIRK_MSI },
98 { HDA_NVIDIA_MCP61_1, "NVIDIA MCP61", 0, 0 },
99 { HDA_NVIDIA_MCP61_2, "NVIDIA MCP61", 0, 0 },
100 { HDA_NVIDIA_MCP65_1, "NVIDIA MCP65", 0, 0 },
101 { HDA_NVIDIA_MCP65_2, "NVIDIA MCP65", 0, 0 },
102 { HDA_NVIDIA_MCP67_1, "NVIDIA MCP67", 0, 0 },
103 { HDA_NVIDIA_MCP67_2, "NVIDIA MCP67", 0, 0 },
104 { HDA_NVIDIA_MCP73_1, "NVIDIA MCP73", 0, 0 },
105 { HDA_NVIDIA_MCP73_2, "NVIDIA MCP73", 0, 0 },
106 { HDA_NVIDIA_MCP78_1, "NVIDIA MCP78", 0, HDAC_QUIRK_64BIT },
107 { HDA_NVIDIA_MCP78_2, "NVIDIA MCP78", 0, HDAC_QUIRK_64BIT },
108 { HDA_NVIDIA_MCP78_3, "NVIDIA MCP78", 0, HDAC_QUIRK_64BIT },
109 { HDA_NVIDIA_MCP78_4, "NVIDIA MCP78", 0, HDAC_QUIRK_64BIT },
110 { HDA_NVIDIA_MCP79_1, "NVIDIA MCP79", 0, 0 },
111 { HDA_NVIDIA_MCP79_2, "NVIDIA MCP79", 0, 0 },
112 { HDA_NVIDIA_MCP79_3, "NVIDIA MCP79", 0, 0 },
113 { HDA_NVIDIA_MCP79_4, "NVIDIA MCP79", 0, 0 },
114 { HDA_NVIDIA_MCP89_1, "NVIDIA MCP89", 0, 0 },
115 { HDA_NVIDIA_MCP89_2, "NVIDIA MCP89", 0, 0 },
116 { HDA_NVIDIA_MCP89_3, "NVIDIA MCP89", 0, 0 },
117 { HDA_NVIDIA_MCP89_4, "NVIDIA MCP89", 0, 0 },
118 { HDA_NVIDIA_0BE2, "NVIDIA (0x0be2)", 0, HDAC_QUIRK_MSI },
119 { HDA_NVIDIA_0BE3, "NVIDIA (0x0be3)", 0, HDAC_QUIRK_MSI },
120 { HDA_NVIDIA_0BE4, "NVIDIA (0x0be4)", 0, HDAC_QUIRK_MSI },
121 { HDA_NVIDIA_GT100, "NVIDIA GT100", 0, HDAC_QUIRK_MSI },
122 { HDA_NVIDIA_GT104, "NVIDIA GT104", 0, HDAC_QUIRK_MSI },
123 { HDA_NVIDIA_GT106, "NVIDIA GT106", 0, HDAC_QUIRK_MSI },
124 { HDA_NVIDIA_GT108, "NVIDIA GT108", 0, HDAC_QUIRK_MSI },
125 { HDA_NVIDIA_GT116, "NVIDIA GT116", 0, HDAC_QUIRK_MSI },
126 { HDA_NVIDIA_GF119, "NVIDIA GF119", 0, 0 },
127 { HDA_NVIDIA_GF110_1, "NVIDIA GF110", 0, HDAC_QUIRK_MSI },
128 { HDA_NVIDIA_GF110_2, "NVIDIA GF110", 0, HDAC_QUIRK_MSI },
129 { HDA_ATI_SB450, "ATI SB450", 0, 0 },
130 { HDA_ATI_SB600, "ATI SB600", 0, 0 },
131 { HDA_ATI_RS600, "ATI RS600", 0, 0 },
132 { HDA_ATI_RS690, "ATI RS690", 0, 0 },
133 { HDA_ATI_RS780, "ATI RS780", 0, 0 },
134 { HDA_ATI_R600, "ATI R600", 0, 0 },
135 { HDA_ATI_RV610, "ATI RV610", 0, 0 },
136 { HDA_ATI_RV620, "ATI RV620", 0, 0 },
137 { HDA_ATI_RV630, "ATI RV630", 0, 0 },
138 { HDA_ATI_RV635, "ATI RV635", 0, 0 },
139 { HDA_ATI_RV710, "ATI RV710", 0, 0 },
140 { HDA_ATI_RV730, "ATI RV730", 0, 0 },
141 { HDA_ATI_RV740, "ATI RV740", 0, 0 },
142 { HDA_ATI_RV770, "ATI RV770", 0, 0 },
143 { HDA_ATI_RV810, "ATI RV810", 0, 0 },
144 { HDA_ATI_RV830, "ATI RV830", 0, 0 },
145 { HDA_ATI_RV840, "ATI RV840", 0, 0 },
146 { HDA_ATI_RV870, "ATI RV870", 0, 0 },
147 { HDA_ATI_RV910, "ATI RV910", 0, 0 },
148 { HDA_ATI_RV930, "ATI RV930", 0, 0 },
149 { HDA_ATI_RV940, "ATI RV940", 0, 0 },
150 { HDA_ATI_RV970, "ATI RV970", 0, 0 },
151 { HDA_ATI_R1000, "ATI R1000", 0, 0 },
152 { HDA_RDC_M3010, "RDC M3010", 0, 0 },
153 { HDA_VIA_VT82XX, "VIA VT8251/8237A",0, 0 },
154 { HDA_SIS_966, "SiS 966", 0, 0 },
155 { HDA_ULI_M5461, "ULI M5461", 0, 0 },
157 { HDA_INTEL_ALL, "Intel", 0, 0 },
158 { HDA_NVIDIA_ALL, "NVIDIA", 0, 0 },
159 { HDA_ATI_ALL, "ATI", 0, 0 },
160 { HDA_VIA_ALL, "VIA", 0, 0 },
161 { HDA_SIS_ALL, "SiS", 0, 0 },
162 { HDA_ULI_ALL, "ULI", 0, 0 },
164 #define HDAC_DEVICES_LEN (sizeof(hdac_devices) / sizeof(hdac_devices[0]))
166 static const struct {
171 } hdac_pcie_snoop[] = {
172 { INTEL_VENDORID, 0x00, 0x00, 0x00 },
173 { ATI_VENDORID, 0x42, 0xf8, 0x02 },
174 { NVIDIA_VENDORID, 0x4e, 0xf0, 0x0f },
176 #define HDAC_PCIESNOOP_LEN \
177 (sizeof(hdac_pcie_snoop) / sizeof(hdac_pcie_snoop[0]))
179 /****************************************************************************
180 * Function prototypes
181 ****************************************************************************/
182 static void hdac_intr_handler(void *);
183 static int hdac_reset(struct hdac_softc *, int);
184 static int hdac_get_capabilities(struct hdac_softc *);
185 static void hdac_dma_cb(void *, bus_dma_segment_t *, int, int);
186 static int hdac_dma_alloc(struct hdac_softc *,
187 struct hdac_dma *, bus_size_t);
188 static void hdac_dma_free(struct hdac_softc *, struct hdac_dma *);
189 static int hdac_mem_alloc(struct hdac_softc *);
190 static void hdac_mem_free(struct hdac_softc *);
191 static int hdac_irq_alloc(struct hdac_softc *);
192 static void hdac_irq_free(struct hdac_softc *);
193 static void hdac_corb_init(struct hdac_softc *);
194 static void hdac_rirb_init(struct hdac_softc *);
195 static void hdac_corb_start(struct hdac_softc *);
196 static void hdac_rirb_start(struct hdac_softc *);
198 static void hdac_attach2(void *);
200 static uint32_t hdac_send_command(struct hdac_softc *, nid_t, uint32_t);
202 static int hdac_probe(device_t);
203 static int hdac_attach(device_t);
204 static int hdac_detach(device_t);
205 static int hdac_suspend(device_t);
206 static int hdac_resume(device_t);
208 static int hdac_rirb_flush(struct hdac_softc *sc);
209 static int hdac_unsolq_flush(struct hdac_softc *sc);
211 #define hdac_command(a1, a2, a3) \
212 hdac_send_command(a1, a3, a2)
214 /* This function surely going to make its way into upper level someday. */
216 hdac_config_fetch(struct hdac_softc *sc, uint32_t *on, uint32_t *off)
218 const char *res = NULL;
219 int i = 0, j, k, len, inv;
221 if (resource_string_value(device_get_name(sc->dev),
222 device_get_unit(sc->dev), "config", &res) != 0)
224 if (!(res != NULL && strlen(res) > 0))
227 device_printf(sc->dev, "Config options:");
230 while (res[i] != '\0' &&
231 (res[i] == ',' || isspace(res[i]) != 0))
233 if (res[i] == '\0') {
240 while (res[j] != '\0' &&
241 !(res[j] == ',' || isspace(res[j]) != 0))
244 if (len > 2 && strncmp(res + i, "no", 2) == 0)
248 for (k = 0; len > inv && k < HDAC_QUIRKS_TAB_LEN; k++) {
249 if (strncmp(res + i + inv,
250 hdac_quirks_tab[k].key, len - inv) != 0)
252 if (len - inv != strlen(hdac_quirks_tab[k].key))
255 printf(" %s%s", (inv != 0) ? "no" : "",
256 hdac_quirks_tab[k].key);
259 *on |= hdac_quirks_tab[k].value;
260 *on &= ~hdac_quirks_tab[k].value;
261 } else if (inv != 0) {
262 *off |= hdac_quirks_tab[k].value;
263 *off &= ~hdac_quirks_tab[k].value;
271 /****************************************************************************
272 * void hdac_intr_handler(void *)
274 * Interrupt handler. Processes interrupts received from the hdac.
275 ****************************************************************************/
277 hdac_intr_handler(void *context)
279 struct hdac_softc *sc;
285 sc = (struct hdac_softc *)context;
288 /* Do we have anything to do? */
289 intsts = HDAC_READ_4(&sc->mem, HDAC_INTSTS);
290 if ((intsts & HDAC_INTSTS_GIS) == 0) {
295 /* Was this a controller interrupt? */
296 if (intsts & HDAC_INTSTS_CIS) {
297 rirbsts = HDAC_READ_1(&sc->mem, HDAC_RIRBSTS);
298 /* Get as many responses that we can */
299 while (rirbsts & HDAC_RIRBSTS_RINTFL) {
300 HDAC_WRITE_1(&sc->mem,
301 HDAC_RIRBSTS, HDAC_RIRBSTS_RINTFL);
303 rirbsts = HDAC_READ_1(&sc->mem, HDAC_RIRBSTS);
305 if (sc->unsolq_rp != sc->unsolq_wp)
306 taskqueue_enqueue(taskqueue_thread, &sc->unsolq_task);
309 if (intsts & HDAC_INTSTS_SIS_MASK) {
310 for (i = 0; i < sc->num_ss; i++) {
311 if ((intsts & (1 << i)) == 0)
313 HDAC_WRITE_1(&sc->mem, (i << 5) + HDAC_SDSTS,
314 HDAC_SDSTS_DESE | HDAC_SDSTS_FIFOE | HDAC_SDSTS_BCIS );
315 if ((dev = sc->streams[i].dev) != NULL) {
316 HDAC_STREAM_INTR(dev,
317 sc->streams[i].dir, sc->streams[i].stream);
322 HDAC_WRITE_4(&sc->mem, HDAC_INTSTS, intsts);
327 hdac_poll_callback(void *arg)
329 struct hdac_softc *sc = arg;
335 if (sc->polling == 0) {
339 callout_reset(&sc->poll_callout, sc->poll_ival,
340 hdac_poll_callback, sc);
343 hdac_intr_handler(sc);
346 /****************************************************************************
347 * int hdac_reset(hdac_softc *, int)
349 * Reset the hdac to a quiescent and known state.
350 ****************************************************************************/
352 hdac_reset(struct hdac_softc *sc, int wakeup)
358 * Stop all Streams DMA engine
360 for (i = 0; i < sc->num_iss; i++)
361 HDAC_WRITE_4(&sc->mem, HDAC_ISDCTL(sc, i), 0x0);
362 for (i = 0; i < sc->num_oss; i++)
363 HDAC_WRITE_4(&sc->mem, HDAC_OSDCTL(sc, i), 0x0);
364 for (i = 0; i < sc->num_bss; i++)
365 HDAC_WRITE_4(&sc->mem, HDAC_BSDCTL(sc, i), 0x0);
368 * Stop Control DMA engines.
370 HDAC_WRITE_1(&sc->mem, HDAC_CORBCTL, 0x0);
371 HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, 0x0);
374 * Reset DMA position buffer.
376 HDAC_WRITE_4(&sc->mem, HDAC_DPIBLBASE, 0x0);
377 HDAC_WRITE_4(&sc->mem, HDAC_DPIBUBASE, 0x0);
380 * Reset the controller. The reset must remain asserted for
381 * a minimum of 100us.
383 gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
384 HDAC_WRITE_4(&sc->mem, HDAC_GCTL, gctl & ~HDAC_GCTL_CRST);
387 gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
388 if (!(gctl & HDAC_GCTL_CRST))
392 if (gctl & HDAC_GCTL_CRST) {
393 device_printf(sc->dev, "Unable to put hdac in reset\n");
397 /* If wakeup is not requested - leave the controller in reset state. */
402 gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
403 HDAC_WRITE_4(&sc->mem, HDAC_GCTL, gctl | HDAC_GCTL_CRST);
406 gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
407 if (gctl & HDAC_GCTL_CRST)
411 if (!(gctl & HDAC_GCTL_CRST)) {
412 device_printf(sc->dev, "Device stuck in reset\n");
417 * Wait for codecs to finish their own reset sequence. The delay here
418 * should be of 250us but for some reasons, on it's not enough on my
419 * computer. Let's use twice as much as necessary to make sure that
420 * it's reset properly.
428 /****************************************************************************
429 * int hdac_get_capabilities(struct hdac_softc *);
431 * Retreive the general capabilities of the hdac;
432 * Number of Input Streams
433 * Number of Output Streams
434 * Number of bidirectional Streams
436 * CORB and RIRB sizes
437 ****************************************************************************/
439 hdac_get_capabilities(struct hdac_softc *sc)
442 uint8_t corbsize, rirbsize;
444 gcap = HDAC_READ_2(&sc->mem, HDAC_GCAP);
445 sc->num_iss = HDAC_GCAP_ISS(gcap);
446 sc->num_oss = HDAC_GCAP_OSS(gcap);
447 sc->num_bss = HDAC_GCAP_BSS(gcap);
448 sc->num_ss = sc->num_iss + sc->num_oss + sc->num_bss;
449 sc->num_sdo = HDAC_GCAP_NSDO(gcap);
450 sc->support_64bit = (gcap & HDAC_GCAP_64OK) != 0;
451 if (sc->quirks_on & HDAC_QUIRK_64BIT)
452 sc->support_64bit = 1;
453 else if (sc->quirks_off & HDAC_QUIRK_64BIT)
454 sc->support_64bit = 0;
456 corbsize = HDAC_READ_1(&sc->mem, HDAC_CORBSIZE);
457 if ((corbsize & HDAC_CORBSIZE_CORBSZCAP_256) ==
458 HDAC_CORBSIZE_CORBSZCAP_256)
460 else if ((corbsize & HDAC_CORBSIZE_CORBSZCAP_16) ==
461 HDAC_CORBSIZE_CORBSZCAP_16)
463 else if ((corbsize & HDAC_CORBSIZE_CORBSZCAP_2) ==
464 HDAC_CORBSIZE_CORBSZCAP_2)
467 device_printf(sc->dev, "%s: Invalid corb size (%x)\n",
472 rirbsize = HDAC_READ_1(&sc->mem, HDAC_RIRBSIZE);
473 if ((rirbsize & HDAC_RIRBSIZE_RIRBSZCAP_256) ==
474 HDAC_RIRBSIZE_RIRBSZCAP_256)
476 else if ((rirbsize & HDAC_RIRBSIZE_RIRBSZCAP_16) ==
477 HDAC_RIRBSIZE_RIRBSZCAP_16)
479 else if ((rirbsize & HDAC_RIRBSIZE_RIRBSZCAP_2) ==
480 HDAC_RIRBSIZE_RIRBSZCAP_2)
483 device_printf(sc->dev, "%s: Invalid rirb size (%x)\n",
489 device_printf(sc->dev, "Caps: OSS %d, ISS %d, BSS %d, "
490 "NSDO %d%s, CORB %d, RIRB %d\n",
491 sc->num_oss, sc->num_iss, sc->num_bss, 1 << sc->num_sdo,
492 sc->support_64bit ? ", 64bit" : "",
493 sc->corb_size, sc->rirb_size);
500 /****************************************************************************
503 * This function is called by bus_dmamap_load when the mapping has been
504 * established. We just record the physical address of the mapping into
505 * the struct hdac_dma passed in.
506 ****************************************************************************/
508 hdac_dma_cb(void *callback_arg, bus_dma_segment_t *segs, int nseg, int error)
510 struct hdac_dma *dma;
513 dma = (struct hdac_dma *)callback_arg;
514 dma->dma_paddr = segs[0].ds_addr;
519 /****************************************************************************
522 * This function allocate and setup a dma region (struct hdac_dma).
523 * It must be freed by a corresponding hdac_dma_free.
524 ****************************************************************************/
526 hdac_dma_alloc(struct hdac_softc *sc, struct hdac_dma *dma, bus_size_t size)
531 roundsz = roundup2(size, HDA_DMA_ALIGNMENT);
532 bzero(dma, sizeof(*dma));
537 result = bus_dma_tag_create(
538 bus_get_dma_tag(sc->dev), /* parent */
539 HDA_DMA_ALIGNMENT, /* alignment */
541 (sc->support_64bit) ? BUS_SPACE_MAXADDR :
542 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
543 BUS_SPACE_MAXADDR, /* highaddr */
545 NULL, /* fistfuncarg */
546 roundsz, /* maxsize */
548 roundsz, /* maxsegsz */
551 NULL, /* lockfuncarg */
552 &dma->dma_tag); /* dmat */
554 device_printf(sc->dev, "%s: bus_dma_tag_create failed (%x)\n",
556 goto hdac_dma_alloc_fail;
560 * Allocate DMA memory
562 result = bus_dmamem_alloc(dma->dma_tag, (void **)&dma->dma_vaddr,
563 BUS_DMA_NOWAIT | BUS_DMA_ZERO |
564 ((sc->flags & HDAC_F_DMA_NOCACHE) ? BUS_DMA_NOCACHE : 0),
567 device_printf(sc->dev, "%s: bus_dmamem_alloc failed (%x)\n",
569 goto hdac_dma_alloc_fail;
572 dma->dma_size = roundsz;
577 result = bus_dmamap_load(dma->dma_tag, dma->dma_map,
578 (void *)dma->dma_vaddr, roundsz, hdac_dma_cb, (void *)dma, 0);
579 if (result != 0 || dma->dma_paddr == 0) {
582 device_printf(sc->dev, "%s: bus_dmamem_load failed (%x)\n",
584 goto hdac_dma_alloc_fail;
588 device_printf(sc->dev, "%s: size=%ju -> roundsz=%ju\n",
589 __func__, (uintmax_t)size, (uintmax_t)roundsz);
595 hdac_dma_free(sc, dma);
601 /****************************************************************************
602 * void hdac_dma_free(struct hdac_softc *, struct hdac_dma *)
604 * Free a struct dhac_dma that has been previously allocated via the
605 * hdac_dma_alloc function.
606 ****************************************************************************/
608 hdac_dma_free(struct hdac_softc *sc, struct hdac_dma *dma)
610 if (dma->dma_map != NULL) {
613 bus_dmamap_sync(dma->dma_tag, dma->dma_map,
614 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
616 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
618 if (dma->dma_vaddr != NULL) {
619 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
620 dma->dma_vaddr = NULL;
623 if (dma->dma_tag != NULL) {
624 bus_dma_tag_destroy(dma->dma_tag);
630 /****************************************************************************
631 * int hdac_mem_alloc(struct hdac_softc *)
633 * Allocate all the bus resources necessary to speak with the physical
635 ****************************************************************************/
637 hdac_mem_alloc(struct hdac_softc *sc)
639 struct hdac_mem *mem;
642 mem->mem_rid = PCIR_BAR(0);
643 mem->mem_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
644 &mem->mem_rid, RF_ACTIVE);
645 if (mem->mem_res == NULL) {
646 device_printf(sc->dev,
647 "%s: Unable to allocate memory resource\n", __func__);
650 mem->mem_tag = rman_get_bustag(mem->mem_res);
651 mem->mem_handle = rman_get_bushandle(mem->mem_res);
656 /****************************************************************************
657 * void hdac_mem_free(struct hdac_softc *)
659 * Free up resources previously allocated by hdac_mem_alloc.
660 ****************************************************************************/
662 hdac_mem_free(struct hdac_softc *sc)
664 struct hdac_mem *mem;
667 if (mem->mem_res != NULL)
668 bus_release_resource(sc->dev, SYS_RES_MEMORY, mem->mem_rid,
673 /****************************************************************************
674 * int hdac_irq_alloc(struct hdac_softc *)
676 * Allocate and setup the resources necessary for interrupt handling.
677 ****************************************************************************/
679 hdac_irq_alloc(struct hdac_softc *sc)
681 struct hdac_irq *irq;
687 if ((sc->quirks_off & HDAC_QUIRK_MSI) == 0 &&
688 (result = pci_msi_count(sc->dev)) == 1 &&
689 pci_alloc_msi(sc->dev, &result) == 0)
692 irq->irq_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ,
693 &irq->irq_rid, RF_SHAREABLE | RF_ACTIVE);
694 if (irq->irq_res == NULL) {
695 device_printf(sc->dev, "%s: Unable to allocate irq\n",
697 goto hdac_irq_alloc_fail;
699 result = bus_setup_intr(sc->dev, irq->irq_res, INTR_MPSAFE | INTR_TYPE_AV,
700 NULL, hdac_intr_handler, sc, &irq->irq_handle);
702 device_printf(sc->dev,
703 "%s: Unable to setup interrupt handler (%x)\n",
705 goto hdac_irq_alloc_fail;
716 /****************************************************************************
717 * void hdac_irq_free(struct hdac_softc *)
719 * Free up resources previously allocated by hdac_irq_alloc.
720 ****************************************************************************/
722 hdac_irq_free(struct hdac_softc *sc)
724 struct hdac_irq *irq;
727 if (irq->irq_res != NULL && irq->irq_handle != NULL)
728 bus_teardown_intr(sc->dev, irq->irq_res, irq->irq_handle);
729 if (irq->irq_res != NULL)
730 bus_release_resource(sc->dev, SYS_RES_IRQ, irq->irq_rid,
732 if (irq->irq_rid == 0x1)
733 pci_release_msi(sc->dev);
734 irq->irq_handle = NULL;
739 /****************************************************************************
740 * void hdac_corb_init(struct hdac_softc *)
742 * Initialize the corb registers for operations but do not start it up yet.
743 * The CORB engine must not be running when this function is called.
744 ****************************************************************************/
746 hdac_corb_init(struct hdac_softc *sc)
751 /* Setup the CORB size. */
752 switch (sc->corb_size) {
754 corbsize = HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_256);
757 corbsize = HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_16);
760 corbsize = HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_2);
763 panic("%s: Invalid CORB size (%x)\n", __func__, sc->corb_size);
765 HDAC_WRITE_1(&sc->mem, HDAC_CORBSIZE, corbsize);
767 /* Setup the CORB Address in the hdac */
768 corbpaddr = (uint64_t)sc->corb_dma.dma_paddr;
769 HDAC_WRITE_4(&sc->mem, HDAC_CORBLBASE, (uint32_t)corbpaddr);
770 HDAC_WRITE_4(&sc->mem, HDAC_CORBUBASE, (uint32_t)(corbpaddr >> 32));
772 /* Set the WP and RP */
774 HDAC_WRITE_2(&sc->mem, HDAC_CORBWP, sc->corb_wp);
775 HDAC_WRITE_2(&sc->mem, HDAC_CORBRP, HDAC_CORBRP_CORBRPRST);
777 * The HDA specification indicates that the CORBRPRST bit will always
778 * read as zero. Unfortunately, it seems that at least the 82801G
779 * doesn't reset the bit to zero, which stalls the corb engine.
780 * manually reset the bit to zero before continuing.
782 HDAC_WRITE_2(&sc->mem, HDAC_CORBRP, 0x0);
784 /* Enable CORB error reporting */
786 HDAC_WRITE_1(&sc->mem, HDAC_CORBCTL, HDAC_CORBCTL_CMEIE);
790 /****************************************************************************
791 * void hdac_rirb_init(struct hdac_softc *)
793 * Initialize the rirb registers for operations but do not start it up yet.
794 * The RIRB engine must not be running when this function is called.
795 ****************************************************************************/
797 hdac_rirb_init(struct hdac_softc *sc)
802 /* Setup the RIRB size. */
803 switch (sc->rirb_size) {
805 rirbsize = HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_256);
808 rirbsize = HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_16);
811 rirbsize = HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_2);
814 panic("%s: Invalid RIRB size (%x)\n", __func__, sc->rirb_size);
816 HDAC_WRITE_1(&sc->mem, HDAC_RIRBSIZE, rirbsize);
818 /* Setup the RIRB Address in the hdac */
819 rirbpaddr = (uint64_t)sc->rirb_dma.dma_paddr;
820 HDAC_WRITE_4(&sc->mem, HDAC_RIRBLBASE, (uint32_t)rirbpaddr);
821 HDAC_WRITE_4(&sc->mem, HDAC_RIRBUBASE, (uint32_t)(rirbpaddr >> 32));
823 /* Setup the WP and RP */
825 HDAC_WRITE_2(&sc->mem, HDAC_RIRBWP, HDAC_RIRBWP_RIRBWPRST);
827 /* Setup the interrupt threshold */
828 HDAC_WRITE_2(&sc->mem, HDAC_RINTCNT, sc->rirb_size / 2);
830 /* Enable Overrun and response received reporting */
832 HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL,
833 HDAC_RIRBCTL_RIRBOIC | HDAC_RIRBCTL_RINTCTL);
835 HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, HDAC_RIRBCTL_RINTCTL);
840 * Make sure that the Host CPU cache doesn't contain any dirty
841 * cache lines that falls in the rirb. If I understood correctly, it
842 * should be sufficient to do this only once as the rirb is purely
843 * read-only from now on.
845 bus_dmamap_sync(sc->rirb_dma.dma_tag, sc->rirb_dma.dma_map,
846 BUS_DMASYNC_PREREAD);
850 /****************************************************************************
851 * void hdac_corb_start(hdac_softc *)
853 * Startup the corb DMA engine
854 ****************************************************************************/
856 hdac_corb_start(struct hdac_softc *sc)
860 corbctl = HDAC_READ_1(&sc->mem, HDAC_CORBCTL);
861 corbctl |= HDAC_CORBCTL_CORBRUN;
862 HDAC_WRITE_1(&sc->mem, HDAC_CORBCTL, corbctl);
865 /****************************************************************************
866 * void hdac_rirb_start(hdac_softc *)
868 * Startup the rirb DMA engine
869 ****************************************************************************/
871 hdac_rirb_start(struct hdac_softc *sc)
875 rirbctl = HDAC_READ_1(&sc->mem, HDAC_RIRBCTL);
876 rirbctl |= HDAC_RIRBCTL_RIRBDMAEN;
877 HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, rirbctl);
881 hdac_rirb_flush(struct hdac_softc *sc)
883 struct hdac_rirb *rirb_base, *rirb;
889 rirb_base = (struct hdac_rirb *)sc->rirb_dma.dma_vaddr;
890 rirbwp = HDAC_READ_1(&sc->mem, HDAC_RIRBWP);
892 bus_dmamap_sync(sc->rirb_dma.dma_tag, sc->rirb_dma.dma_map,
893 BUS_DMASYNC_POSTREAD);
897 while (sc->rirb_rp != rirbwp) {
899 sc->rirb_rp %= sc->rirb_size;
900 rirb = &rirb_base[sc->rirb_rp];
901 cad = HDAC_RIRB_RESPONSE_EX_SDATA_IN(rirb->response_ex);
902 resp = rirb->response;
903 if (rirb->response_ex & HDAC_RIRB_RESPONSE_EX_UNSOLICITED) {
904 sc->unsolq[sc->unsolq_wp++] = resp;
905 sc->unsolq_wp %= HDAC_UNSOLQ_MAX;
906 sc->unsolq[sc->unsolq_wp++] = cad;
907 sc->unsolq_wp %= HDAC_UNSOLQ_MAX;
908 } else if (sc->codecs[cad].pending <= 0) {
909 device_printf(sc->dev, "Unexpected unsolicited "
910 "response from address %d: %08x\n", cad, resp);
912 sc->codecs[cad].response = resp;
913 sc->codecs[cad].pending--;
921 hdac_unsolq_flush(struct hdac_softc *sc)
928 if (sc->unsolq_st == HDAC_UNSOLQ_READY) {
929 sc->unsolq_st = HDAC_UNSOLQ_BUSY;
930 while (sc->unsolq_rp != sc->unsolq_wp) {
931 resp = sc->unsolq[sc->unsolq_rp++];
932 sc->unsolq_rp %= HDAC_UNSOLQ_MAX;
933 cad = sc->unsolq[sc->unsolq_rp++];
934 sc->unsolq_rp %= HDAC_UNSOLQ_MAX;
935 if ((child = sc->codecs[cad].dev) != NULL)
936 HDAC_UNSOL_INTR(child, resp);
939 sc->unsolq_st = HDAC_UNSOLQ_READY;
945 /****************************************************************************
946 * uint32_t hdac_command_sendone_internal
948 * Wrapper function that sends only one command to a given codec
949 ****************************************************************************/
951 hdac_send_command(struct hdac_softc *sc, nid_t cad, uint32_t verb)
956 if (!hdac_lockowned(sc))
957 device_printf(sc->dev, "WARNING!!!! mtx not owned!!!!\n");
958 verb &= ~HDA_CMD_CAD_MASK;
959 verb |= ((uint32_t)cad) << HDA_CMD_CAD_SHIFT;
960 sc->codecs[cad].response = HDA_INVALID;
962 sc->codecs[cad].pending++;
964 sc->corb_wp %= sc->corb_size;
965 corb = (uint32_t *)sc->corb_dma.dma_vaddr;
967 bus_dmamap_sync(sc->corb_dma.dma_tag,
968 sc->corb_dma.dma_map, BUS_DMASYNC_PREWRITE);
970 corb[sc->corb_wp] = verb;
972 bus_dmamap_sync(sc->corb_dma.dma_tag,
973 sc->corb_dma.dma_map, BUS_DMASYNC_POSTWRITE);
975 HDAC_WRITE_2(&sc->mem, HDAC_CORBWP, sc->corb_wp);
979 if (hdac_rirb_flush(sc) == 0)
981 } while (sc->codecs[cad].pending != 0 && --timeout);
983 if (sc->codecs[cad].pending != 0) {
984 device_printf(sc->dev, "Command timeout on address %d\n", cad);
985 sc->codecs[cad].pending = 0;
988 if (sc->unsolq_rp != sc->unsolq_wp)
989 taskqueue_enqueue(taskqueue_thread, &sc->unsolq_task);
990 return (sc->codecs[cad].response);
993 /****************************************************************************
995 ****************************************************************************/
997 /****************************************************************************
998 * int hdac_probe(device_t)
1000 * Probe for the presence of an hdac. If none is found, check for a generic
1001 * match using the subclass of the device.
1002 ****************************************************************************/
1004 hdac_probe(device_t dev)
1008 uint16_t class, subclass;
1011 model = (uint32_t)pci_get_device(dev) << 16;
1012 model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
1013 class = pci_get_class(dev);
1014 subclass = pci_get_subclass(dev);
1016 bzero(desc, sizeof(desc));
1018 for (i = 0; i < HDAC_DEVICES_LEN; i++) {
1019 if (hdac_devices[i].model == model) {
1020 strlcpy(desc, hdac_devices[i].desc, sizeof(desc));
1021 result = BUS_PROBE_DEFAULT;
1024 if (HDA_DEV_MATCH(hdac_devices[i].model, model) &&
1025 class == PCIC_MULTIMEDIA &&
1026 subclass == PCIS_MULTIMEDIA_HDA) {
1027 snprintf(desc, sizeof(desc),
1029 hdac_devices[i].desc, pci_get_device(dev));
1030 result = BUS_PROBE_GENERIC;
1034 if (result == ENXIO && class == PCIC_MULTIMEDIA &&
1035 subclass == PCIS_MULTIMEDIA_HDA) {
1036 snprintf(desc, sizeof(desc), "Generic (0x%08x)", model);
1037 result = BUS_PROBE_GENERIC;
1039 if (result != ENXIO) {
1040 strlcat(desc, " HDA Controller", sizeof(desc));
1041 device_set_desc_copy(dev, desc);
1048 hdac_unsolq_task(void *context, int pending)
1050 struct hdac_softc *sc;
1052 sc = (struct hdac_softc *)context;
1055 hdac_unsolq_flush(sc);
1059 /****************************************************************************
1060 * int hdac_attach(device_t)
1062 * Attach the device into the kernel. Interrupts usually won't be enabled
1063 * when this function is called. Setup everything that doesn't require
1064 * interrupts and defer probing of codecs until interrupts are enabled.
1065 ****************************************************************************/
1067 hdac_attach(device_t dev)
1069 struct hdac_softc *sc;
1073 uint16_t class, subclass;
1077 sc = device_get_softc(dev);
1079 device_printf(dev, "PCI card vendor: 0x%04x, device: 0x%04x\n",
1080 pci_get_subvendor(dev), pci_get_subdevice(dev));
1081 device_printf(dev, "HDA Driver Revision: %s\n",
1085 model = (uint32_t)pci_get_device(dev) << 16;
1086 model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
1087 class = pci_get_class(dev);
1088 subclass = pci_get_subclass(dev);
1090 for (i = 0; i < HDAC_DEVICES_LEN; i++) {
1091 if (hdac_devices[i].model == model) {
1095 if (HDA_DEV_MATCH(hdac_devices[i].model, model) &&
1096 class == PCIC_MULTIMEDIA &&
1097 subclass == PCIS_MULTIMEDIA_HDA) {
1103 sc->lock = snd_mtxcreate(device_get_nameunit(dev), "HDA driver mutex");
1105 TASK_INIT(&sc->unsolq_task, 0, hdac_unsolq_task, sc);
1106 callout_init(&sc->poll_callout, CALLOUT_MPSAFE);
1107 for (i = 0; i < HDAC_CODEC_MAX; i++)
1108 sc->codecs[i].dev = NULL;
1110 sc->quirks_on = hdac_devices[devid].quirks_on;
1111 sc->quirks_off = hdac_devices[devid].quirks_off;
1116 if (resource_int_value(device_get_name(dev),
1117 device_get_unit(dev), "msi", &i) == 0) {
1119 sc->quirks_off |= HDAC_QUIRK_MSI;
1121 sc->quirks_on |= HDAC_QUIRK_MSI;
1122 sc->quirks_off |= ~HDAC_QUIRK_MSI;
1125 hdac_config_fetch(sc, &sc->quirks_on, &sc->quirks_off);
1127 device_printf(sc->dev,
1128 "Config options: on=0x%08x off=0x%08x\n",
1129 sc->quirks_on, sc->quirks_off);
1132 if (resource_int_value(device_get_name(dev),
1133 device_get_unit(dev), "polling", &i) == 0 && i != 0)
1138 pci_enable_busmaster(dev);
1140 vendor = pci_get_vendor(dev);
1141 if (vendor == INTEL_VENDORID) {
1143 v = pci_read_config(dev, 0x44, 1);
1144 pci_write_config(dev, 0x44, v & 0xf8, 1);
1146 device_printf(dev, "TCSEL: 0x%02d -> 0x%02d\n", v,
1147 pci_read_config(dev, 0x44, 1));
1151 #if defined(__i386__) || defined(__amd64__)
1152 sc->flags |= HDAC_F_DMA_NOCACHE;
1154 if (resource_int_value(device_get_name(dev),
1155 device_get_unit(dev), "snoop", &i) == 0 && i != 0) {
1157 sc->flags &= ~HDAC_F_DMA_NOCACHE;
1160 * Try to enable PCIe snoop to avoid messing around with
1161 * uncacheable DMA attribute. Since PCIe snoop register
1162 * config is pretty much vendor specific, there are no
1163 * general solutions on how to enable it, forcing us (even
1164 * Microsoft) to enable uncacheable or write combined DMA
1167 * http://msdn2.microsoft.com/en-us/library/ms790324.aspx
1169 for (i = 0; i < HDAC_PCIESNOOP_LEN; i++) {
1170 if (hdac_pcie_snoop[i].vendor != vendor)
1172 sc->flags &= ~HDAC_F_DMA_NOCACHE;
1173 if (hdac_pcie_snoop[i].reg == 0x00)
1175 v = pci_read_config(dev, hdac_pcie_snoop[i].reg, 1);
1176 if ((v & hdac_pcie_snoop[i].enable) ==
1177 hdac_pcie_snoop[i].enable)
1179 v &= hdac_pcie_snoop[i].mask;
1180 v |= hdac_pcie_snoop[i].enable;
1181 pci_write_config(dev, hdac_pcie_snoop[i].reg, v, 1);
1182 v = pci_read_config(dev, hdac_pcie_snoop[i].reg, 1);
1183 if ((v & hdac_pcie_snoop[i].enable) !=
1184 hdac_pcie_snoop[i].enable) {
1187 "WARNING: Failed to enable PCIe "
1190 #if defined(__i386__) || defined(__amd64__)
1191 sc->flags |= HDAC_F_DMA_NOCACHE;
1196 #if defined(__i386__) || defined(__amd64__)
1201 device_printf(dev, "DMA Coherency: %s / vendor=0x%04x\n",
1202 (sc->flags & HDAC_F_DMA_NOCACHE) ?
1203 "Uncacheable" : "PCIe snoop", vendor);
1206 /* Allocate resources */
1207 result = hdac_mem_alloc(sc);
1209 goto hdac_attach_fail;
1210 result = hdac_irq_alloc(sc);
1212 goto hdac_attach_fail;
1214 /* Get Capabilities */
1215 result = hdac_get_capabilities(sc);
1217 goto hdac_attach_fail;
1219 /* Allocate CORB, RIRB, POS and BDLs dma memory */
1220 result = hdac_dma_alloc(sc, &sc->corb_dma,
1221 sc->corb_size * sizeof(uint32_t));
1223 goto hdac_attach_fail;
1224 result = hdac_dma_alloc(sc, &sc->rirb_dma,
1225 sc->rirb_size * sizeof(struct hdac_rirb));
1227 goto hdac_attach_fail;
1228 sc->streams = malloc(sizeof(struct hdac_stream) * sc->num_ss,
1229 M_HDAC, M_ZERO | M_WAITOK);
1230 for (i = 0; i < sc->num_ss; i++) {
1231 result = hdac_dma_alloc(sc, &sc->streams[i].bdl,
1232 sizeof(struct hdac_bdle) * HDA_BDL_MAX);
1234 goto hdac_attach_fail;
1236 if (sc->quirks_on & HDAC_QUIRK_DMAPOS) {
1237 if (hdac_dma_alloc(sc, &sc->pos_dma, (sc->num_ss) * 8) != 0) {
1239 device_printf(dev, "Failed to "
1240 "allocate DMA pos buffer "
1244 uint64_t addr = sc->pos_dma.dma_paddr;
1246 HDAC_WRITE_4(&sc->mem, HDAC_DPIBUBASE, addr >> 32);
1247 HDAC_WRITE_4(&sc->mem, HDAC_DPIBLBASE,
1248 (addr & HDAC_DPLBASE_DPLBASE_MASK) |
1249 HDAC_DPLBASE_DPLBASE_DMAPBE);
1253 result = bus_dma_tag_create(
1254 bus_get_dma_tag(sc->dev), /* parent */
1255 HDA_DMA_ALIGNMENT, /* alignment */
1257 (sc->support_64bit) ? BUS_SPACE_MAXADDR :
1258 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1259 BUS_SPACE_MAXADDR, /* highaddr */
1260 NULL, /* filtfunc */
1261 NULL, /* fistfuncarg */
1262 HDA_BUFSZ_MAX, /* maxsize */
1264 HDA_BUFSZ_MAX, /* maxsegsz */
1266 NULL, /* lockfunc */
1267 NULL, /* lockfuncarg */
1268 &sc->chan_dmat); /* dmat */
1270 device_printf(dev, "%s: bus_dma_tag_create failed (%x)\n",
1272 goto hdac_attach_fail;
1275 /* Quiesce everything */
1277 device_printf(dev, "Reset controller...\n");
1281 /* Initialize the CORB and RIRB */
1285 /* Defer remaining of initialization until interrupts are enabled */
1286 sc->intrhook.ich_func = hdac_attach2;
1287 sc->intrhook.ich_arg = (void *)sc;
1288 if (cold == 0 || config_intrhook_establish(&sc->intrhook) != 0) {
1289 sc->intrhook.ich_func = NULL;
1290 hdac_attach2((void *)sc);
1297 for (i = 0; i < sc->num_ss; i++)
1298 hdac_dma_free(sc, &sc->streams[i].bdl);
1299 free(sc->streams, M_HDAC);
1300 hdac_dma_free(sc, &sc->rirb_dma);
1301 hdac_dma_free(sc, &sc->corb_dma);
1303 snd_mtxfree(sc->lock);
1309 sysctl_hdac_pindump(SYSCTL_HANDLER_ARGS)
1311 struct hdac_softc *sc;
1314 int devcount, i, err, val;
1316 dev = oidp->oid_arg1;
1317 sc = device_get_softc(dev);
1321 err = sysctl_handle_int(oidp, &val, 0, req);
1322 if (err != 0 || req->newptr == NULL || val == 0)
1325 /* XXX: Temporary. For debugging. */
1329 } else if (val == 101) {
1334 if ((err = device_get_children(dev, &devlist, &devcount)) != 0)
1337 for (i = 0; i < devcount; i++)
1338 HDAC_PINDUMP(devlist[i]);
1340 free(devlist, M_TEMP);
1345 hdac_mdata_rate(uint16_t fmt)
1347 static const int mbits[8] = { 8, 16, 32, 32, 32, 32, 32, 32 };
1350 if (fmt & (1 << 14))
1354 rate *= ((fmt >> 11) & 0x07) + 1;
1355 rate /= ((fmt >> 8) & 0x07) + 1;
1356 bits = mbits[(fmt >> 4) & 0x03];
1357 bits *= (fmt & 0x0f) + 1;
1358 return (rate * bits);
1362 hdac_bdata_rate(uint16_t fmt, int output)
1364 static const int bbits[8] = { 8, 16, 20, 24, 32, 32, 32, 32 };
1368 rate *= ((fmt >> 11) & 0x07) + 1;
1369 bits = bbits[(fmt >> 4) & 0x03];
1370 bits *= (fmt & 0x0f) + 1;
1372 bits = ((bits + 7) & ~0x07) + 10;
1373 return (rate * bits);
1377 hdac_poll_reinit(struct hdac_softc *sc)
1379 int i, pollticks, min = 1000000;
1380 struct hdac_stream *s;
1382 if (sc->polling == 0)
1384 if (sc->unsol_registered > 0)
1386 for (i = 0; i < sc->num_ss; i++) {
1387 s = &sc->streams[i];
1388 if (s->running == 0)
1390 pollticks = ((uint64_t)hz * s->blksz) /
1391 (hdac_mdata_rate(s->format) / 8);
1395 if (pollticks < 1) {
1397 device_printf(sc->dev,
1398 "poll interval < 1 tick !\n");
1402 if (min > pollticks)
1406 device_printf(sc->dev,
1407 "poll interval %d -> %d ticks\n",
1408 sc->poll_ival, min);
1410 sc->poll_ival = min;
1412 callout_stop(&sc->poll_callout);
1414 callout_reset(&sc->poll_callout, 1, hdac_poll_callback, sc);
1418 sysctl_hdac_polling(SYSCTL_HANDLER_ARGS)
1420 struct hdac_softc *sc;
1425 dev = oidp->oid_arg1;
1426 sc = device_get_softc(dev);
1432 err = sysctl_handle_int(oidp, &val, 0, req);
1434 if (err != 0 || req->newptr == NULL)
1436 if (val < 0 || val > 1)
1440 if (val != sc->polling) {
1442 callout_stop(&sc->poll_callout);
1444 callout_drain(&sc->poll_callout);
1447 ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
1448 ctl |= HDAC_INTCTL_GIE;
1449 HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
1451 ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
1452 ctl &= ~HDAC_INTCTL_GIE;
1453 HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
1455 hdac_poll_reinit(sc);
1464 hdac_attach2(void *arg)
1466 struct hdac_softc *sc;
1468 uint32_t vendorid, revisionid;
1472 sc = (struct hdac_softc *)arg;
1476 /* Remove ourselves from the config hooks */
1477 if (sc->intrhook.ich_func != NULL) {
1478 config_intrhook_disestablish(&sc->intrhook);
1479 sc->intrhook.ich_func = NULL;
1483 device_printf(sc->dev, "Starting CORB Engine...\n");
1485 hdac_corb_start(sc);
1487 device_printf(sc->dev, "Starting RIRB Engine...\n");
1489 hdac_rirb_start(sc);
1491 device_printf(sc->dev,
1492 "Enabling controller interrupt...\n");
1494 HDAC_WRITE_4(&sc->mem, HDAC_GCTL, HDAC_READ_4(&sc->mem, HDAC_GCTL) |
1496 if (sc->polling == 0) {
1497 HDAC_WRITE_4(&sc->mem, HDAC_INTCTL,
1498 HDAC_INTCTL_CIE | HDAC_INTCTL_GIE);
1503 device_printf(sc->dev, "Scanning HDA codecs ...\n");
1505 statests = HDAC_READ_2(&sc->mem, HDAC_STATESTS);
1507 for (i = 0; i < HDAC_CODEC_MAX; i++) {
1508 if (HDAC_STATESTS_SDIWAKE(statests, i)) {
1510 device_printf(sc->dev,
1511 "Found CODEC at address %d\n", i);
1514 vendorid = hdac_send_command(sc, i,
1515 HDA_CMD_GET_PARAMETER(0, 0x0, HDA_PARAM_VENDOR_ID));
1516 revisionid = hdac_send_command(sc, i,
1517 HDA_CMD_GET_PARAMETER(0, 0x0, HDA_PARAM_REVISION_ID));
1519 if (vendorid == HDA_INVALID &&
1520 revisionid == HDA_INVALID) {
1521 device_printf(sc->dev,
1522 "CODEC is not responding!\n");
1525 sc->codecs[i].vendor_id =
1526 HDA_PARAM_VENDOR_ID_VENDOR_ID(vendorid);
1527 sc->codecs[i].device_id =
1528 HDA_PARAM_VENDOR_ID_DEVICE_ID(vendorid);
1529 sc->codecs[i].revision_id =
1530 HDA_PARAM_REVISION_ID_REVISION_ID(revisionid);
1531 sc->codecs[i].stepping_id =
1532 HDA_PARAM_REVISION_ID_STEPPING_ID(revisionid);
1533 child = device_add_child(sc->dev, "hdacc", -1);
1534 if (child == NULL) {
1535 device_printf(sc->dev,
1536 "Failed to add CODEC device\n");
1539 device_set_ivars(child, (void *)(intptr_t)i);
1540 sc->codecs[i].dev = child;
1543 bus_generic_attach(sc->dev);
1545 SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
1546 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
1547 "pindump", CTLTYPE_INT | CTLFLAG_RW, sc->dev, sizeof(sc->dev),
1548 sysctl_hdac_pindump, "I", "Dump pin states/data");
1549 SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
1550 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
1551 "polling", CTLTYPE_INT | CTLFLAG_RW, sc->dev, sizeof(sc->dev),
1552 sysctl_hdac_polling, "I", "Enable polling mode");
1555 /****************************************************************************
1556 * int hdac_suspend(device_t)
1558 * Suspend and power down HDA bus and codecs.
1559 ****************************************************************************/
1561 hdac_suspend(device_t dev)
1563 struct hdac_softc *sc = device_get_softc(dev);
1566 device_printf(dev, "Suspend...\n");
1568 bus_generic_suspend(dev);
1572 device_printf(dev, "Reset controller...\n");
1574 callout_stop(&sc->poll_callout);
1577 callout_drain(&sc->poll_callout);
1578 taskqueue_drain(taskqueue_thread, &sc->unsolq_task);
1580 device_printf(dev, "Suspend done\n");
1585 /****************************************************************************
1586 * int hdac_resume(device_t)
1588 * Powerup and restore HDA bus and codecs state.
1589 ****************************************************************************/
1591 hdac_resume(device_t dev)
1593 struct hdac_softc *sc = device_get_softc(dev);
1597 device_printf(dev, "Resume...\n");
1601 /* Quiesce everything */
1603 device_printf(dev, "Reset controller...\n");
1607 /* Initialize the CORB and RIRB */
1612 device_printf(dev, "Starting CORB Engine...\n");
1614 hdac_corb_start(sc);
1616 device_printf(dev, "Starting RIRB Engine...\n");
1618 hdac_rirb_start(sc);
1620 device_printf(dev, "Enabling controller interrupt...\n");
1622 HDAC_WRITE_4(&sc->mem, HDAC_GCTL, HDAC_READ_4(&sc->mem, HDAC_GCTL) |
1624 HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, HDAC_INTCTL_CIE | HDAC_INTCTL_GIE);
1626 hdac_poll_reinit(sc);
1629 error = bus_generic_resume(dev);
1631 device_printf(dev, "Resume done\n");
1636 /****************************************************************************
1637 * int hdac_detach(device_t)
1639 * Detach and free up resources utilized by the hdac device.
1640 ****************************************************************************/
1642 hdac_detach(device_t dev)
1644 struct hdac_softc *sc = device_get_softc(dev);
1646 int cad, i, devcount, error;
1648 if ((error = device_get_children(dev, &devlist, &devcount)) != 0)
1650 for (i = 0; i < devcount; i++) {
1651 cad = (intptr_t)device_get_ivars(devlist[i]);
1652 if ((error = device_delete_child(dev, devlist[i])) != 0) {
1653 free(devlist, M_TEMP);
1656 sc->codecs[cad].dev = NULL;
1658 free(devlist, M_TEMP);
1663 taskqueue_drain(taskqueue_thread, &sc->unsolq_task);
1666 for (i = 0; i < sc->num_ss; i++)
1667 hdac_dma_free(sc, &sc->streams[i].bdl);
1668 free(sc->streams, M_HDAC);
1669 hdac_dma_free(sc, &sc->pos_dma);
1670 hdac_dma_free(sc, &sc->rirb_dma);
1671 hdac_dma_free(sc, &sc->corb_dma);
1672 if (sc->chan_dmat != NULL) {
1673 bus_dma_tag_destroy(sc->chan_dmat);
1674 sc->chan_dmat = NULL;
1677 snd_mtxfree(sc->lock);
1681 static bus_dma_tag_t
1682 hdac_get_dma_tag(device_t dev, device_t child)
1684 struct hdac_softc *sc = device_get_softc(dev);
1686 return (sc->chan_dmat);
1690 hdac_print_child(device_t dev, device_t child)
1694 retval = bus_print_child_header(dev, child);
1695 retval += printf(" at cad %d",
1696 (int)(intptr_t)device_get_ivars(child));
1697 retval += bus_print_child_footer(dev, child);
1703 hdac_child_location_str(device_t dev, device_t child, char *buf,
1707 snprintf(buf, buflen, "cad=%d",
1708 (int)(intptr_t)device_get_ivars(child));
1713 hdac_child_pnpinfo_str_method(device_t dev, device_t child, char *buf,
1716 struct hdac_softc *sc = device_get_softc(dev);
1717 nid_t cad = (uintptr_t)device_get_ivars(child);
1719 snprintf(buf, buflen, "vendor=0x%04x device=0x%04x revision=0x%02x "
1721 sc->codecs[cad].vendor_id, sc->codecs[cad].device_id,
1722 sc->codecs[cad].revision_id, sc->codecs[cad].stepping_id);
1727 hdac_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1729 struct hdac_softc *sc = device_get_softc(dev);
1730 nid_t cad = (uintptr_t)device_get_ivars(child);
1733 case HDA_IVAR_CODEC_ID:
1736 case HDA_IVAR_VENDOR_ID:
1737 *result = sc->codecs[cad].vendor_id;
1739 case HDA_IVAR_DEVICE_ID:
1740 *result = sc->codecs[cad].device_id;
1742 case HDA_IVAR_REVISION_ID:
1743 *result = sc->codecs[cad].revision_id;
1745 case HDA_IVAR_STEPPING_ID:
1746 *result = sc->codecs[cad].stepping_id;
1748 case HDA_IVAR_SUBVENDOR_ID:
1749 *result = pci_get_subvendor(dev);
1751 case HDA_IVAR_SUBDEVICE_ID:
1752 *result = pci_get_subdevice(dev);
1754 case HDA_IVAR_DMA_NOCACHE:
1755 *result = (sc->flags & HDAC_F_DMA_NOCACHE) != 0;
1764 hdac_get_mtx(device_t dev, device_t child)
1766 struct hdac_softc *sc = device_get_softc(dev);
1772 hdac_codec_command(device_t dev, device_t child, uint32_t verb)
1775 return (hdac_send_command(device_get_softc(dev),
1776 (intptr_t)device_get_ivars(child), verb));
1780 hdac_find_stream(struct hdac_softc *sc, int dir, int stream)
1785 /* Allocate ISS/BSS first. */
1787 for (i = 0; i < sc->num_iss; i++) {
1788 if (sc->streams[i].stream == stream) {
1794 for (i = 0; i < sc->num_oss; i++) {
1795 if (sc->streams[i + sc->num_iss].stream == stream) {
1796 ss = i + sc->num_iss;
1801 /* Fallback to BSS. */
1803 for (i = 0; i < sc->num_bss; i++) {
1804 if (sc->streams[i + sc->num_iss + sc->num_oss].stream
1806 ss = i + sc->num_iss + sc->num_oss;
1815 hdac_stream_alloc(device_t dev, device_t child, int dir, int format, int stripe,
1818 struct hdac_softc *sc = device_get_softc(dev);
1819 nid_t cad = (uintptr_t)device_get_ivars(child);
1820 int stream, ss, bw, maxbw, prevbw;
1822 /* Look for empty stream. */
1823 ss = hdac_find_stream(sc, dir, 0);
1825 /* Return if found nothing. */
1829 /* Check bus bandwidth. */
1830 bw = hdac_bdata_rate(format, dir);
1832 bw *= 1 << (sc->num_sdo - stripe);
1833 prevbw = sc->sdo_bw_used;
1834 maxbw = 48000 * 960 * (1 << sc->num_sdo);
1836 prevbw = sc->codecs[cad].sdi_bw_used;
1837 maxbw = 48000 * 464;
1840 device_printf(dev, "%dKbps of %dKbps bandwidth used%s\n",
1841 (bw + prevbw) / 1000, maxbw / 1000,
1842 bw + prevbw > maxbw ? " -- OVERFLOW!" : "");
1844 if (bw + prevbw > maxbw)
1847 sc->sdo_bw_used += bw;
1849 sc->codecs[cad].sdi_bw_used += bw;
1851 /* Allocate stream number */
1852 if (ss >= sc->num_iss + sc->num_oss)
1853 stream = 15 - (ss - sc->num_iss + sc->num_oss);
1854 else if (ss >= sc->num_iss)
1855 stream = ss - sc->num_iss + 1;
1859 sc->streams[ss].dev = child;
1860 sc->streams[ss].dir = dir;
1861 sc->streams[ss].stream = stream;
1862 sc->streams[ss].bw = bw;
1863 sc->streams[ss].format = format;
1864 sc->streams[ss].stripe = stripe;
1865 if (dmapos != NULL) {
1866 if (sc->pos_dma.dma_vaddr != NULL)
1867 *dmapos = (uint32_t *)(sc->pos_dma.dma_vaddr + ss * 8);
1875 hdac_stream_free(device_t dev, device_t child, int dir, int stream)
1877 struct hdac_softc *sc = device_get_softc(dev);
1878 nid_t cad = (uintptr_t)device_get_ivars(child);
1881 ss = hdac_find_stream(sc, dir, stream);
1883 ("Free for not allocated stream (%d/%d)\n", dir, stream));
1885 sc->sdo_bw_used -= sc->streams[ss].bw;
1887 sc->codecs[cad].sdi_bw_used -= sc->streams[ss].bw;
1888 sc->streams[ss].stream = 0;
1889 sc->streams[ss].dev = NULL;
1893 hdac_stream_start(device_t dev, device_t child,
1894 int dir, int stream, bus_addr_t buf, int blksz, int blkcnt)
1896 struct hdac_softc *sc = device_get_softc(dev);
1897 struct hdac_bdle *bdle;
1902 ss = hdac_find_stream(sc, dir, stream);
1904 ("Start for not allocated stream (%d/%d)\n", dir, stream));
1906 addr = (uint64_t)buf;
1907 bdle = (struct hdac_bdle *)sc->streams[ss].bdl.dma_vaddr;
1908 for (i = 0; i < blkcnt; i++, bdle++) {
1909 bdle->addrl = (uint32_t)addr;
1910 bdle->addrh = (uint32_t)(addr >> 32);
1917 HDAC_WRITE_4(&sc->mem, off + HDAC_SDCBL, blksz * blkcnt);
1918 HDAC_WRITE_2(&sc->mem, off + HDAC_SDLVI, blkcnt - 1);
1919 addr = sc->streams[ss].bdl.dma_paddr;
1920 HDAC_WRITE_4(&sc->mem, off + HDAC_SDBDPL, (uint32_t)addr);
1921 HDAC_WRITE_4(&sc->mem, off + HDAC_SDBDPU, (uint32_t)(addr >> 32));
1923 ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL2);
1925 ctl |= HDAC_SDCTL2_DIR;
1927 ctl &= ~HDAC_SDCTL2_DIR;
1928 ctl &= ~HDAC_SDCTL2_STRM_MASK;
1929 ctl |= stream << HDAC_SDCTL2_STRM_SHIFT;
1930 ctl &= ~HDAC_SDCTL2_STRIPE_MASK;
1931 ctl |= sc->streams[ss].stripe << HDAC_SDCTL2_STRIPE_SHIFT;
1932 HDAC_WRITE_1(&sc->mem, off + HDAC_SDCTL2, ctl);
1934 HDAC_WRITE_2(&sc->mem, off + HDAC_SDFMT, sc->streams[ss].format);
1936 ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
1938 HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
1940 HDAC_WRITE_1(&sc->mem, off + HDAC_SDSTS,
1941 HDAC_SDSTS_DESE | HDAC_SDSTS_FIFOE | HDAC_SDSTS_BCIS);
1942 ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL0);
1943 ctl |= HDAC_SDCTL_IOCE | HDAC_SDCTL_FEIE | HDAC_SDCTL_DEIE |
1945 HDAC_WRITE_1(&sc->mem, off + HDAC_SDCTL0, ctl);
1947 sc->streams[ss].blksz = blksz;
1948 sc->streams[ss].running = 1;
1949 hdac_poll_reinit(sc);
1954 hdac_stream_stop(device_t dev, device_t child, int dir, int stream)
1956 struct hdac_softc *sc = device_get_softc(dev);
1960 ss = hdac_find_stream(sc, dir, stream);
1962 ("Stop for not allocated stream (%d/%d)\n", dir, stream));
1965 ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL0);
1966 ctl &= ~(HDAC_SDCTL_IOCE | HDAC_SDCTL_FEIE | HDAC_SDCTL_DEIE |
1968 HDAC_WRITE_1(&sc->mem, off + HDAC_SDCTL0, ctl);
1970 ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
1972 HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
1974 sc->streams[ss].running = 0;
1975 hdac_poll_reinit(sc);
1979 hdac_stream_reset(device_t dev, device_t child, int dir, int stream)
1981 struct hdac_softc *sc = device_get_softc(dev);
1987 ss = hdac_find_stream(sc, dir, stream);
1989 ("Reset for not allocated stream (%d/%d)\n", dir, stream));
1992 ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL0);
1993 ctl |= HDAC_SDCTL_SRST;
1994 HDAC_WRITE_1(&sc->mem, off + HDAC_SDCTL0, ctl);
1996 ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL0);
1997 if (ctl & HDAC_SDCTL_SRST)
2001 if (!(ctl & HDAC_SDCTL_SRST))
2002 device_printf(dev, "Reset setting timeout\n");
2003 ctl &= ~HDAC_SDCTL_SRST;
2004 HDAC_WRITE_1(&sc->mem, off + HDAC_SDCTL0, ctl);
2007 ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL0);
2008 if (!(ctl & HDAC_SDCTL_SRST))
2012 if (ctl & HDAC_SDCTL_SRST)
2013 device_printf(dev, "Reset timeout!\n");
2017 hdac_stream_getptr(device_t dev, device_t child, int dir, int stream)
2019 struct hdac_softc *sc = device_get_softc(dev);
2022 ss = hdac_find_stream(sc, dir, stream);
2024 ("Reset for not allocated stream (%d/%d)\n", dir, stream));
2027 return (HDAC_READ_4(&sc->mem, off + HDAC_SDLPIB));
2031 hdac_unsol_alloc(device_t dev, device_t child, int tag)
2033 struct hdac_softc *sc = device_get_softc(dev);
2035 sc->unsol_registered++;
2036 hdac_poll_reinit(sc);
2041 hdac_unsol_free(device_t dev, device_t child, int tag)
2043 struct hdac_softc *sc = device_get_softc(dev);
2045 sc->unsol_registered--;
2046 hdac_poll_reinit(sc);
2049 static device_method_t hdac_methods[] = {
2050 /* device interface */
2051 DEVMETHOD(device_probe, hdac_probe),
2052 DEVMETHOD(device_attach, hdac_attach),
2053 DEVMETHOD(device_detach, hdac_detach),
2054 DEVMETHOD(device_suspend, hdac_suspend),
2055 DEVMETHOD(device_resume, hdac_resume),
2057 DEVMETHOD(bus_get_dma_tag, hdac_get_dma_tag),
2058 DEVMETHOD(bus_print_child, hdac_print_child),
2059 DEVMETHOD(bus_child_location_str, hdac_child_location_str),
2060 DEVMETHOD(bus_child_pnpinfo_str, hdac_child_pnpinfo_str_method),
2061 DEVMETHOD(bus_read_ivar, hdac_read_ivar),
2062 DEVMETHOD(hdac_get_mtx, hdac_get_mtx),
2063 DEVMETHOD(hdac_codec_command, hdac_codec_command),
2064 DEVMETHOD(hdac_stream_alloc, hdac_stream_alloc),
2065 DEVMETHOD(hdac_stream_free, hdac_stream_free),
2066 DEVMETHOD(hdac_stream_start, hdac_stream_start),
2067 DEVMETHOD(hdac_stream_stop, hdac_stream_stop),
2068 DEVMETHOD(hdac_stream_reset, hdac_stream_reset),
2069 DEVMETHOD(hdac_stream_getptr, hdac_stream_getptr),
2070 DEVMETHOD(hdac_unsol_alloc, hdac_unsol_alloc),
2071 DEVMETHOD(hdac_unsol_free, hdac_unsol_free),
2075 static driver_t hdac_driver = {
2078 sizeof(struct hdac_softc),
2081 static devclass_t hdac_devclass;
2083 DRIVER_MODULE(snd_hda, pci, hdac_driver, hdac_devclass, 0, 0);