xref: /dragonfly/sys/dev/sound/pci/hda/hdac.c (revision fb594d7a)
1 /*-
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>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Intel High Definition Audio (Controller) driver for FreeBSD.
31  */
32 
33 #ifdef HAVE_KERNEL_OPTION_HEADERS
34 #include "opt_snd.h"
35 #endif
36 
37 #include <dev/sound/pcm/sound.h>
38 #include <bus/pci/pcireg.h>
39 #include <bus/pci/pcivar.h>
40 
41 #include <sys/ctype.h>
42 #include <sys/taskqueue.h>
43 
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>
48 
49 #define HDA_DRV_TEST_REV	"20120126_0002"
50 
51 SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pci/hda/hdac.c 275101 2014-11-26 04:23:21Z mav $");
52 
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)	(lockstatus((sc)->lock, curthread) == LK_EXCLUSIVE)
57 
58 #define HDAC_QUIRK_64BIT	(1 << 0)
59 #define HDAC_QUIRK_DMAPOS	(1 << 1)
60 #define HDAC_QUIRK_MSI		(1 << 2)
61 
62 static const struct {
63 	const char *key;
64 	uint32_t value;
65 } hdac_quirks_tab[] = {
66 	{ "64bit", HDAC_QUIRK_DMAPOS },
67 	{ "dmapos", HDAC_QUIRK_DMAPOS },
68 	{ "msi", HDAC_QUIRK_MSI },
69 };
70 
71 MALLOC_DEFINE(M_HDAC, "hdac", "HDA Controller");
72 
73 static const struct {
74 	uint32_t	model;
75 	const char	*desc;
76 	char		quirks_on;
77 	char		quirks_off;
78 } hdac_devices[] = {
79 	{ HDA_INTEL_OAK,     "Intel Oaktrail",	0, 0 },
80 	{ HDA_INTEL_BAY,     "Intel BayTrail",	0, 0 },
81 	{ HDA_INTEL_HSW1,    "Intel Haswell",	0, 0 },
82 	{ HDA_INTEL_HSW2,    "Intel Haswell",	0, 0 },
83 	{ HDA_INTEL_HSW3,    "Intel Haswell",	0, 0 },
84 	{ HDA_INTEL_BDW1,    "Intel Broadwell",	0, 0 },
85 	{ HDA_INTEL_CPT,     "Intel Cougar Point",	0, 0 },
86 	{ HDA_INTEL_PATSBURG,"Intel Patsburg",  0, 0 },
87 	{ HDA_INTEL_PPT1,    "Intel Panther Point",	0, 0 },
88 	{ HDA_INTEL_LPT1,    "Intel Lynx Point",	0, 0 },
89 	{ HDA_INTEL_LPT2,    "Intel Lynx Point",	0, 0 },
90 	{ HDA_INTEL_WCPT,    "Intel Wildcat Point",	0, 0 },
91 	{ HDA_INTEL_WELLS1,  "Intel Wellsburg",	0, 0 },
92 	{ HDA_INTEL_WELLS2,  "Intel Wellsburg",	0, 0 },
93 	{ HDA_INTEL_LPTLP1,  "Intel Lynx Point-LP",	0, 0 },
94 	{ HDA_INTEL_LPTLP2,  "Intel Lynx Point-LP",	0, 0 },
95 	{ HDA_INTEL_WCPTLP,  "Intel Wildcat Point-LP",	0, 0 },
96 	{ HDA_INTEL_SRPTLP,  "Intel Sunrise Point-LP",	0, 0 },
97 	{ HDA_INTEL_KBLKLP,  "Intel Kabylake-LP",	0, 0 },
98 	{ HDA_INTEL_SRPT,    "Intel Sunrise Point",	0, 0 },
99 	{ HDA_INTEL_KBLK,    "Intel Kabylake",	0, 0 },
100 	{ HDA_INTEL_82801F,  "Intel 82801F",	0, 0 },
101 	{ HDA_INTEL_63XXESB, "Intel 631x/632xESB",	0, 0 },
102 	{ HDA_INTEL_82801G,  "Intel 82801G",	0, 0 },
103 	{ HDA_INTEL_82801H,  "Intel 82801H",	0, 0 },
104 	{ HDA_INTEL_82801I,  "Intel 82801I",	0, 0 },
105 	{ HDA_INTEL_82801JI, "Intel 82801JI",	0, 0 },
106 	{ HDA_INTEL_82801JD, "Intel 82801JD",	0, 0 },
107 	{ HDA_INTEL_PCH,     "Intel 5 Series/3400 Series",	0, 0 },
108 	{ HDA_INTEL_PCH2,    "Intel 5 Series/3400 Series",	0, 0 },
109 	{ HDA_INTEL_SCH,     "Intel SCH",	0, 0 },
110 	{ HDA_NVIDIA_MCP51,  "NVIDIA MCP51",	0, HDAC_QUIRK_MSI },
111 	{ HDA_NVIDIA_MCP55,  "NVIDIA MCP55",	0, HDAC_QUIRK_MSI },
112 	{ HDA_NVIDIA_MCP61_1, "NVIDIA MCP61",	0, 0 },
113 	{ HDA_NVIDIA_MCP61_2, "NVIDIA MCP61",	0, 0 },
114 	{ HDA_NVIDIA_MCP65_1, "NVIDIA MCP65",	0, 0 },
115 	{ HDA_NVIDIA_MCP65_2, "NVIDIA MCP65",	0, 0 },
116 	{ HDA_NVIDIA_MCP67_1, "NVIDIA MCP67",	0, 0 },
117 	{ HDA_NVIDIA_MCP67_2, "NVIDIA MCP67",	0, 0 },
118 	{ HDA_NVIDIA_MCP73_1, "NVIDIA MCP73",	0, 0 },
119 	{ HDA_NVIDIA_MCP73_2, "NVIDIA MCP73",	0, 0 },
120 	{ HDA_NVIDIA_MCP78_1, "NVIDIA MCP78",	0, HDAC_QUIRK_64BIT },
121 	{ HDA_NVIDIA_MCP78_2, "NVIDIA MCP78",	0, HDAC_QUIRK_64BIT },
122 	{ HDA_NVIDIA_MCP78_3, "NVIDIA MCP78",	0, HDAC_QUIRK_64BIT },
123 	{ HDA_NVIDIA_MCP78_4, "NVIDIA MCP78",	0, HDAC_QUIRK_64BIT },
124 	{ HDA_NVIDIA_MCP79_1, "NVIDIA MCP79",	0, 0 },
125 	{ HDA_NVIDIA_MCP79_2, "NVIDIA MCP79",	0, 0 },
126 	{ HDA_NVIDIA_MCP79_3, "NVIDIA MCP79",	0, 0 },
127 	{ HDA_NVIDIA_MCP79_4, "NVIDIA MCP79",	0, 0 },
128 	{ HDA_NVIDIA_MCP89_1, "NVIDIA MCP89",	0, 0 },
129 	{ HDA_NVIDIA_MCP89_2, "NVIDIA MCP89",	0, 0 },
130 	{ HDA_NVIDIA_MCP89_3, "NVIDIA MCP89",	0, 0 },
131 	{ HDA_NVIDIA_MCP89_4, "NVIDIA MCP89",	0, 0 },
132 	{ HDA_NVIDIA_0BE2,   "NVIDIA (0x0be2)",	0, HDAC_QUIRK_MSI },
133 	{ HDA_NVIDIA_0BE3,   "NVIDIA (0x0be3)",	0, HDAC_QUIRK_MSI },
134 	{ HDA_NVIDIA_0BE4,   "NVIDIA (0x0be4)",	0, HDAC_QUIRK_MSI },
135 	{ HDA_NVIDIA_GT100,  "NVIDIA GT100",	0, HDAC_QUIRK_MSI },
136 	{ HDA_NVIDIA_GT104,  "NVIDIA GT104",	0, HDAC_QUIRK_MSI },
137 	{ HDA_NVIDIA_GT106,  "NVIDIA GT106",	0, HDAC_QUIRK_MSI },
138 	{ HDA_NVIDIA_GT108,  "NVIDIA GT108",	0, HDAC_QUIRK_MSI },
139 	{ HDA_NVIDIA_GT116,  "NVIDIA GT116",	0, HDAC_QUIRK_MSI },
140 	{ HDA_NVIDIA_GF119,  "NVIDIA GF119",	0, 0 },
141 	{ HDA_NVIDIA_GF110_1, "NVIDIA GF110",	0, HDAC_QUIRK_MSI },
142 	{ HDA_NVIDIA_GF110_2, "NVIDIA GF110",	0, HDAC_QUIRK_MSI },
143 	{ HDA_ATI_SB450,     "ATI SB450",	0, 0 },
144 	{ HDA_ATI_SB600,     "ATI SB600",	0, 0 },
145 	{ HDA_ATI_RS600,     "ATI RS600",	0, 0 },
146 	{ HDA_ATI_RS690,     "ATI RS690",	0, 0 },
147 	{ HDA_ATI_RS780,     "ATI RS780",	0, 0 },
148 	{ HDA_ATI_R600,      "ATI R600",	0, 0 },
149 	{ HDA_ATI_RV610,     "ATI RV610",	0, 0 },
150 	{ HDA_ATI_RV620,     "ATI RV620",	0, 0 },
151 	{ HDA_ATI_RV630,     "ATI RV630",	0, 0 },
152 	{ HDA_ATI_RV635,     "ATI RV635",	0, 0 },
153 	{ HDA_ATI_RV710,     "ATI RV710",	0, 0 },
154 	{ HDA_ATI_RV730,     "ATI RV730",	0, 0 },
155 	{ HDA_ATI_RV740,     "ATI RV740",	0, 0 },
156 	{ HDA_ATI_RV770,     "ATI RV770",	0, 0 },
157 	{ HDA_ATI_RV810,     "ATI RV810",	0, 0 },
158 	{ HDA_ATI_RV830,     "ATI RV830",	0, 0 },
159 	{ HDA_ATI_RV840,     "ATI RV840",	0, 0 },
160 	{ HDA_ATI_RV870,     "ATI RV870",	0, 0 },
161 	{ HDA_ATI_RV910,     "ATI RV910",	0, 0 },
162 	{ HDA_ATI_RV930,     "ATI RV930",	0, 0 },
163 	{ HDA_ATI_RV940,     "ATI RV940",	0, 0 },
164 	{ HDA_ATI_RV970,     "ATI RV970",	0, 0 },
165 	{ HDA_ATI_R1000,     "ATI R1000",	0, 0 },
166 	{ HDA_AMD_HUDSON2,   "AMD Hudson-2",	0, 0 },
167 	{ HDA_RDC_M3010,     "RDC M3010",	0, 0 },
168 	{ HDA_VIA_VT82XX,    "VIA VT8251/8237A",0, 0 },
169 	{ HDA_SIS_966,       "SiS 966",		0, 0 },
170 	{ HDA_ULI_M5461,     "ULI M5461",	0, 0 },
171 	/* Unknown */
172 	{ HDA_INTEL_ALL,  "Intel",		0, 0 },
173 	{ HDA_NVIDIA_ALL, "NVIDIA",		0, 0 },
174 	{ HDA_ATI_ALL,    "ATI",		0, 0 },
175 	{ HDA_AMD_ALL,    "AMD",		0, 0 },
176 	{ HDA_VIA_ALL,    "VIA",		0, 0 },
177 	{ HDA_SIS_ALL,    "SiS",		0, 0 },
178 	{ HDA_ULI_ALL,    "ULI",		0, 0 },
179 };
180 
181 #if 0 /* unused */
182 static const struct {
183 	uint16_t vendor;
184 	uint8_t reg;
185 	uint8_t mask;
186 	uint8_t enable;
187 } hdac_pcie_snoop[] = {
188 	{  INTEL_VENDORID, 0x00, 0x00, 0x00 },
189 	{    ATI_VENDORID, 0x42, 0xf8, 0x02 },
190 	{ NVIDIA_VENDORID, 0x4e, 0xf0, 0x0f },
191 };
192 #endif
193 
194 /****************************************************************************
195  * Function prototypes
196  ****************************************************************************/
197 static void	hdac_intr_handler(void *);
198 static int	hdac_reset(struct hdac_softc *, int);
199 static int	hdac_get_capabilities(struct hdac_softc *);
200 static void	hdac_dma_cb(void *, bus_dma_segment_t *, int, int);
201 static int	hdac_dma_alloc(struct hdac_softc *,
202 					struct hdac_dma *, bus_size_t);
203 static void	hdac_dma_free(struct hdac_softc *, struct hdac_dma *);
204 static int	hdac_mem_alloc(struct hdac_softc *);
205 static void	hdac_mem_free(struct hdac_softc *);
206 static int	hdac_irq_alloc(struct hdac_softc *);
207 static void	hdac_irq_free(struct hdac_softc *);
208 static void	hdac_corb_init(struct hdac_softc *);
209 static void	hdac_rirb_init(struct hdac_softc *);
210 static void	hdac_corb_start(struct hdac_softc *);
211 static void	hdac_rirb_start(struct hdac_softc *);
212 
213 static void	hdac_attach2(void *);
214 
215 static uint32_t	hdac_send_command(struct hdac_softc *, nid_t, uint32_t);
216 
217 static int	hdac_probe(device_t);
218 static int	hdac_attach(device_t);
219 static int	hdac_detach(device_t);
220 static int	hdac_suspend(device_t);
221 static int	hdac_resume(device_t);
222 
223 static int	hdac_rirb_flush(struct hdac_softc *sc);
224 static int	hdac_unsolq_flush(struct hdac_softc *sc);
225 
226 #define hdac_command(a1, a2, a3)	\
227 		hdac_send_command(a1, a3, a2)
228 
229 /* This function surely going to make its way into upper level someday. */
230 static void
231 hdac_config_fetch(struct hdac_softc *sc, uint32_t *on, uint32_t *off)
232 {
233 	const char *res = NULL;
234 	int i = 0, j, k, len, inv;
235 
236 	if (resource_string_value(device_get_name(sc->dev),
237 	    device_get_unit(sc->dev), "config", &res) != 0)
238 		return;
239 	if (!(res != NULL && strlen(res) > 0))
240 		return;
241 	HDA_BOOTVERBOSE(
242 		device_printf(sc->dev, "Config options:");
243 	);
244 	for (;;) {
245 		while (res[i] != '\0' &&
246 		    (res[i] == ',' || isspace(res[i]) != 0))
247 			i++;
248 		if (res[i] == '\0') {
249 			HDA_BOOTVERBOSE(
250 				kprintf("\n");
251 			);
252 			return;
253 		}
254 		j = i;
255 		while (res[j] != '\0' &&
256 		    !(res[j] == ',' || isspace(res[j]) != 0))
257 			j++;
258 		len = j - i;
259 		if (len > 2 && strncmp(res + i, "no", 2) == 0)
260 			inv = 2;
261 		else
262 			inv = 0;
263 		for (k = 0; len > inv && k < nitems(hdac_quirks_tab); k++) {
264 			if (strncmp(res + i + inv,
265 			    hdac_quirks_tab[k].key, len - inv) != 0)
266 				continue;
267 			if (len - inv != strlen(hdac_quirks_tab[k].key))
268 				continue;
269 			HDA_BOOTVERBOSE(
270 				kprintf(" %s%s", (inv != 0) ? "no" : "",
271 				    hdac_quirks_tab[k].key);
272 			);
273 			if (inv == 0) {
274 				*on |= hdac_quirks_tab[k].value;
275 				*on &= ~hdac_quirks_tab[k].value;
276 			} else if (inv != 0) {
277 				*off |= hdac_quirks_tab[k].value;
278 				*off &= ~hdac_quirks_tab[k].value;
279 			}
280 			break;
281 		}
282 		i = j;
283 	}
284 }
285 
286 /****************************************************************************
287  * void hdac_intr_handler(void *)
288  *
289  * Interrupt handler. Processes interrupts received from the hdac.
290  ****************************************************************************/
291 static void
292 hdac_intr_handler(void *context)
293 {
294 	struct hdac_softc *sc;
295 	device_t dev;
296 	uint32_t intsts;
297 	uint8_t rirbsts;
298 	int i;
299 
300 	sc = (struct hdac_softc *)context;
301 	hdac_lock(sc);
302 
303 	/* Do we have anything to do? */
304 	intsts = HDAC_READ_4(&sc->mem, HDAC_INTSTS);
305 	if ((intsts & HDAC_INTSTS_GIS) == 0) {
306 		hdac_unlock(sc);
307 		return;
308 	}
309 
310 	/* Was this a controller interrupt? */
311 	if (intsts & HDAC_INTSTS_CIS) {
312 		rirbsts = HDAC_READ_1(&sc->mem, HDAC_RIRBSTS);
313 		/* Get as many responses that we can */
314 		while (rirbsts & HDAC_RIRBSTS_RINTFL) {
315 			HDAC_WRITE_1(&sc->mem,
316 			    HDAC_RIRBSTS, HDAC_RIRBSTS_RINTFL);
317 			hdac_rirb_flush(sc);
318 			rirbsts = HDAC_READ_1(&sc->mem, HDAC_RIRBSTS);
319 		}
320 		if (sc->unsolq_rp != sc->unsolq_wp)
321 			taskqueue_enqueue(taskqueue_thread[mycpuid], &sc->unsolq_task);
322 	}
323 
324 	if (intsts & HDAC_INTSTS_SIS_MASK) {
325 		for (i = 0; i < sc->num_ss; i++) {
326 			if ((intsts & (1 << i)) == 0)
327 				continue;
328 			HDAC_WRITE_1(&sc->mem, (i << 5) + HDAC_SDSTS,
329 			    HDAC_SDSTS_DESE | HDAC_SDSTS_FIFOE | HDAC_SDSTS_BCIS );
330 			if ((dev = sc->streams[i].dev) != NULL) {
331 				HDAC_STREAM_INTR(dev,
332 				    sc->streams[i].dir, sc->streams[i].stream);
333 			}
334 		}
335 	}
336 
337 	HDAC_WRITE_4(&sc->mem, HDAC_INTSTS, intsts);
338 	hdac_unlock(sc);
339 }
340 
341 static void
342 hdac_poll_callback(void *arg)
343 {
344 	struct hdac_softc *sc = arg;
345 
346 	if (sc == NULL)
347 		return;
348 
349 	hdac_lock(sc);
350 	if (sc->polling == 0) {
351 		hdac_unlock(sc);
352 		return;
353 	}
354 	callout_reset(&sc->poll_callout, sc->poll_ival,
355 	    hdac_poll_callback, sc);
356 	hdac_unlock(sc);
357 
358 	hdac_intr_handler(sc);
359 }
360 
361 /****************************************************************************
362  * int hdac_reset(hdac_softc *, int)
363  *
364  * Reset the hdac to a quiescent and known state.
365  ****************************************************************************/
366 static int
367 hdac_reset(struct hdac_softc *sc, int wakeup)
368 {
369 	uint32_t gctl;
370 	uint32_t wee;
371 	int count, i;
372 
373 	/*
374 	 * Stop all Streams DMA engine
375 	 */
376 	for (i = 0; i < sc->num_iss; i++)
377 		HDAC_WRITE_4(&sc->mem, HDAC_ISDCTL(sc, i), 0x0);
378 	for (i = 0; i < sc->num_oss; i++)
379 		HDAC_WRITE_4(&sc->mem, HDAC_OSDCTL(sc, i), 0x0);
380 	for (i = 0; i < sc->num_bss; i++)
381 		HDAC_WRITE_4(&sc->mem, HDAC_BSDCTL(sc, i), 0x0);
382 
383 	/*
384 	 * Stop Control DMA engines.
385 	 */
386 	HDAC_WRITE_1(&sc->mem, HDAC_CORBCTL, 0x0);
387 	HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, 0x0);
388 
389 	/*
390 	 * Reset DMA position buffer.
391 	 */
392 	HDAC_WRITE_4(&sc->mem, HDAC_DPIBLBASE, 0x0);
393 	HDAC_WRITE_4(&sc->mem, HDAC_DPIBUBASE, 0x0);
394 
395 	/*
396 	 * Reset the controller. The reset must remain asserted for
397 	 * a minimum of 100us.
398 	 */
399 	gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
400 	HDAC_WRITE_4(&sc->mem, HDAC_GCTL, gctl & ~HDAC_GCTL_CRST);
401 	count = 10000;
402 	do {
403 		gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
404 		if (!(gctl & HDAC_GCTL_CRST))
405 			break;
406 		DELAY(10);
407 	} while	(--count);
408 	if (gctl & HDAC_GCTL_CRST) {
409 		device_printf(sc->dev, "Unable to put hdac in reset\n");
410 		return (ENXIO);
411 	}
412 
413 	/* If wakeup is not requested - leave the controller in reset state. */
414 	if (!wakeup)
415 		return (0);
416 
417 	DELAY(100);
418 	gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
419 	HDAC_WRITE_4(&sc->mem, HDAC_GCTL, gctl | HDAC_GCTL_CRST);
420 	count = 10000;
421 	do {
422 		gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
423 		if (gctl & HDAC_GCTL_CRST)
424 			break;
425 		DELAY(10);
426 	} while (--count);
427 	if (!(gctl & HDAC_GCTL_CRST)) {
428 		device_printf(sc->dev, "Device stuck in reset\n");
429 		return (ENXIO);
430 	}
431 
432 	/*
433 	 * Wait for codecs to finish their own reset sequence. The delay here
434 	 * should be of 250us but for some reasons, on it's not enough on my
435 	 * computer. Let's use twice as much as necessary to make sure that
436 	 * it's reset properly.
437 	 */
438 	DELAY(1000);
439 
440 	/*
441 	 * BIOS May have left some wake bits enabled / pending, which can
442 	 *	force a continuous interrupt.  Make sure it is turned off.
443 	 */
444 	wee = HDAC_READ_2(&sc->mem, HDAC_WAKEEN);
445 	HDAC_WRITE_2(&sc->mem, HDAC_WAKEEN, wee & ~HDAC_WAKEEN_SDIWEN_MASK);
446 	/*HDAC_WRITE_2(&sc->mem, HDAC_STATESTS, HDAC_STATESTS_SDIWAKE_MASK);*/
447 
448 	return (0);
449 }
450 
451 
452 /****************************************************************************
453  * int hdac_get_capabilities(struct hdac_softc *);
454  *
455  * Retreive the general capabilities of the hdac;
456  *	Number of Input Streams
457  *	Number of Output Streams
458  *	Number of bidirectional Streams
459  *	64bit ready
460  *	CORB and RIRB sizes
461  ****************************************************************************/
462 static int
463 hdac_get_capabilities(struct hdac_softc *sc)
464 {
465 	uint16_t gcap;
466 	uint8_t corbsize, rirbsize;
467 
468 	gcap = HDAC_READ_2(&sc->mem, HDAC_GCAP);
469 	sc->num_iss = HDAC_GCAP_ISS(gcap);
470 	sc->num_oss = HDAC_GCAP_OSS(gcap);
471 	sc->num_bss = HDAC_GCAP_BSS(gcap);
472 	sc->num_ss = sc->num_iss + sc->num_oss + sc->num_bss;
473 	sc->num_sdo = HDAC_GCAP_NSDO(gcap);
474 	sc->support_64bit = (gcap & HDAC_GCAP_64OK) != 0;
475 	if (sc->quirks_on & HDAC_QUIRK_64BIT)
476 		sc->support_64bit = 1;
477 	else if (sc->quirks_off & HDAC_QUIRK_64BIT)
478 		sc->support_64bit = 0;
479 
480 	corbsize = HDAC_READ_1(&sc->mem, HDAC_CORBSIZE);
481 	if ((corbsize & HDAC_CORBSIZE_CORBSZCAP_256) ==
482 	    HDAC_CORBSIZE_CORBSZCAP_256)
483 		sc->corb_size = 256;
484 	else if ((corbsize & HDAC_CORBSIZE_CORBSZCAP_16) ==
485 	    HDAC_CORBSIZE_CORBSZCAP_16)
486 		sc->corb_size = 16;
487 	else if ((corbsize & HDAC_CORBSIZE_CORBSZCAP_2) ==
488 	    HDAC_CORBSIZE_CORBSZCAP_2)
489 		sc->corb_size = 2;
490 	else {
491 		device_printf(sc->dev, "%s: Invalid corb size (%x)\n",
492 		    __func__, corbsize);
493 		if (1) {
494 			device_printf(sc->dev, "Resetting corb size to 256\n");
495 			sc->corb_size = 256;
496 			corbsize =
497 			    HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_256);
498 			HDAC_WRITE_1(&sc->mem, HDAC_CORBSIZE, corbsize);
499 		}
500 		else
501 			return (ENXIO);
502 	}
503 
504 	rirbsize = HDAC_READ_1(&sc->mem, HDAC_RIRBSIZE);
505 	if ((rirbsize & HDAC_RIRBSIZE_RIRBSZCAP_256) ==
506 	    HDAC_RIRBSIZE_RIRBSZCAP_256)
507 		sc->rirb_size = 256;
508 	else if ((rirbsize & HDAC_RIRBSIZE_RIRBSZCAP_16) ==
509 	    HDAC_RIRBSIZE_RIRBSZCAP_16)
510 		sc->rirb_size = 16;
511 	else if ((rirbsize & HDAC_RIRBSIZE_RIRBSZCAP_2) ==
512 	    HDAC_RIRBSIZE_RIRBSZCAP_2)
513 		sc->rirb_size = 2;
514 	else {
515 		device_printf(sc->dev, "%s: Invalid rirb size (%x)\n",
516 		    __func__, rirbsize);
517 		if (1) {
518 			device_printf(sc->dev, "Resetting rirb size to 256\n");
519 			sc->rirb_size = 256;
520 			rirbsize =
521 			    HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_256);
522 			HDAC_WRITE_1(&sc->mem, HDAC_RIRBSIZE, rirbsize);
523 		}
524 		else
525 			return (ENXIO);
526 	}
527 
528 	HDA_BOOTVERBOSE(
529 		device_printf(sc->dev, "Caps: OSS %d, ISS %d, BSS %d, "
530 		    "NSDO %d%s, CORB %d, RIRB %d\n",
531 		    sc->num_oss, sc->num_iss, sc->num_bss, 1 << sc->num_sdo,
532 		    sc->support_64bit ? ", 64bit" : "",
533 		    sc->corb_size, sc->rirb_size);
534 	);
535 
536 	return (0);
537 }
538 
539 
540 /****************************************************************************
541  * void hdac_dma_cb
542  *
543  * This function is called by bus_dmamap_load when the mapping has been
544  * established. We just record the physical address of the mapping into
545  * the struct hdac_dma passed in.
546  ****************************************************************************/
547 static void
548 hdac_dma_cb(void *callback_arg, bus_dma_segment_t *segs, int nseg, int error)
549 {
550 	struct hdac_dma *dma;
551 
552 	if (error == 0) {
553 		dma = (struct hdac_dma *)callback_arg;
554 		dma->dma_paddr = segs[0].ds_addr;
555 	}
556 }
557 
558 
559 /****************************************************************************
560  * int hdac_dma_alloc
561  *
562  * This function allocate and setup a dma region (struct hdac_dma).
563  * It must be freed by a corresponding hdac_dma_free.
564  ****************************************************************************/
565 static int
566 hdac_dma_alloc(struct hdac_softc *sc, struct hdac_dma *dma, bus_size_t size)
567 {
568 	bus_size_t alignment, roundsz;
569 	int result;
570 
571 	if (sc->flags & HDAC_F_DMA_NOCACHE)
572 		alignment = roundup2(HDA_DMA_ALIGNMENT, PAGE_SIZE);
573 	else
574 		alignment = HDA_DMA_ALIGNMENT;
575 
576 	roundsz = roundup2(size, alignment);
577 	bzero(dma, sizeof(*dma));
578 
579 	/*
580 	 * Create a DMA tag
581 	 */
582 	result = bus_dma_tag_create(
583 	    bus_get_dma_tag(sc->dev),		/* parent */
584 	    alignment,				/* alignment */
585 	    0,					/* boundary */
586 	    (sc->support_64bit) ? BUS_SPACE_MAXADDR :
587 		BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
588 	    BUS_SPACE_MAXADDR,			/* highaddr */
589 	    NULL,				/* filtfunc */
590 	    NULL,				/* fistfuncarg */
591 	    roundsz, 				/* maxsize */
592 	    1,					/* nsegments */
593 	    roundsz, 				/* maxsegsz */
594 	    0,					/* flags */
595 	    &dma->dma_tag);			/* dmat */
596 	if (result != 0) {
597 		device_printf(sc->dev, "%s: bus_dma_tag_create failed (%x)\n",
598 		    __func__, result);
599 		goto hdac_dma_alloc_fail;
600 	}
601 
602 	/*
603 	 * Allocate DMA memory
604 	 */
605 	result = bus_dmamem_alloc(dma->dma_tag, (void **)&dma->dma_vaddr,
606 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO |
607 	    ((sc->flags & HDAC_F_DMA_NOCACHE) ? BUS_DMA_NOCACHE : 0),
608 	    &dma->dma_map);
609 	if (result != 0) {
610 		device_printf(sc->dev, "%s: bus_dmamem_alloc failed (%x)\n",
611 		    __func__, result);
612 		goto hdac_dma_alloc_fail;
613 	}
614 
615 	dma->dma_size = roundsz;
616 
617 	/*
618 	 * Map the memory
619 	 */
620 	result = bus_dmamap_load(dma->dma_tag, dma->dma_map,
621 	    (void *)dma->dma_vaddr, roundsz, hdac_dma_cb, (void *)dma, 0);
622 	if (result != 0 || dma->dma_paddr == 0) {
623 		if (result == 0)
624 			result = ENOMEM;
625 		device_printf(sc->dev, "%s: bus_dmamem_load failed (%x)\n",
626 		    __func__, result);
627 		goto hdac_dma_alloc_fail;
628 	}
629 
630 	HDA_BOOTHVERBOSE(
631 		device_printf(sc->dev, "%s: size=%ju -> roundsz=%ju\n",
632 		    __func__, (uintmax_t)size, (uintmax_t)roundsz);
633 	);
634 
635 	return (0);
636 
637 hdac_dma_alloc_fail:
638 	hdac_dma_free(sc, dma);
639 
640 	return (result);
641 }
642 
643 
644 /****************************************************************************
645  * void hdac_dma_free(struct hdac_softc *, struct hdac_dma *)
646  *
647  * Free a struct dhac_dma that has been previously allocated via the
648  * hdac_dma_alloc function.
649  ****************************************************************************/
650 static void
651 hdac_dma_free(struct hdac_softc *sc, struct hdac_dma *dma)
652 {
653 	if (dma->dma_paddr != 0) {
654 #if 0
655 		/* Flush caches */
656 		bus_dmamap_sync(dma->dma_tag, dma->dma_map,
657 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
658 #endif
659 		bus_dmamap_unload(dma->dma_tag, dma->dma_map);
660 		dma->dma_paddr = 0;
661 	}
662 	if (dma->dma_vaddr != NULL) {
663 		bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
664 		dma->dma_vaddr = NULL;
665 	}
666 	if (dma->dma_tag != NULL) {
667 		bus_dma_tag_destroy(dma->dma_tag);
668 		dma->dma_tag = NULL;
669 	}
670 	dma->dma_size = 0;
671 }
672 
673 /****************************************************************************
674  * int hdac_mem_alloc(struct hdac_softc *)
675  *
676  * Allocate all the bus resources necessary to speak with the physical
677  * controller.
678  ****************************************************************************/
679 static int
680 hdac_mem_alloc(struct hdac_softc *sc)
681 {
682 	struct hdac_mem *mem;
683 
684 	mem = &sc->mem;
685 	mem->mem_rid = PCIR_BAR(0);
686 	mem->mem_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
687 	    &mem->mem_rid, RF_ACTIVE);
688 	if (mem->mem_res == NULL) {
689 		device_printf(sc->dev,
690 		    "%s: Unable to allocate memory resource\n", __func__);
691 		return (ENOMEM);
692 	}
693 	mem->mem_tag = rman_get_bustag(mem->mem_res);
694 	mem->mem_handle = rman_get_bushandle(mem->mem_res);
695 
696 	return (0);
697 }
698 
699 /****************************************************************************
700  * void hdac_mem_free(struct hdac_softc *)
701  *
702  * Free up resources previously allocated by hdac_mem_alloc.
703  ****************************************************************************/
704 static void
705 hdac_mem_free(struct hdac_softc *sc)
706 {
707 	struct hdac_mem *mem;
708 
709 	mem = &sc->mem;
710 	if (mem->mem_res != NULL)
711 		bus_release_resource(sc->dev, SYS_RES_MEMORY, mem->mem_rid,
712 		    mem->mem_res);
713 	mem->mem_res = NULL;
714 }
715 
716 /****************************************************************************
717  * int hdac_irq_alloc(struct hdac_softc *)
718  *
719  * Allocate and setup the resources necessary for interrupt handling.
720  ****************************************************************************/
721 static int
722 hdac_irq_alloc(struct hdac_softc *sc)
723 {
724 	struct hdac_irq *irq;
725 	int result;
726 
727 	irq = &sc->irq;
728 	irq->irq_rid = 0x0;
729 
730 	if ((sc->quirks_off & HDAC_QUIRK_MSI) == 0 &&
731 	    (result = pci_msi_count(sc->dev)) == 1 &&
732 	    pci_alloc_msi(sc->dev, &result, 1, -1) == 0)
733 		irq->irq_rid = 0x1;
734 
735 	irq->irq_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ,
736 	    &irq->irq_rid, RF_SHAREABLE | RF_ACTIVE);
737 	if (irq->irq_res == NULL) {
738 		device_printf(sc->dev, "%s: Unable to allocate irq\n",
739 		    __func__);
740 		goto hdac_irq_alloc_fail;
741 	}
742 	result = bus_setup_intr(sc->dev, irq->irq_res, INTR_MPSAFE,
743 	    hdac_intr_handler, sc, &irq->irq_handle, NULL);
744 	if (result != 0) {
745 		device_printf(sc->dev,
746 		    "%s: Unable to setup interrupt handler (%x)\n",
747 		    __func__, result);
748 		goto hdac_irq_alloc_fail;
749 	}
750 
751 	return (0);
752 
753 hdac_irq_alloc_fail:
754 	hdac_irq_free(sc);
755 
756 	return (ENXIO);
757 }
758 
759 /****************************************************************************
760  * void hdac_irq_free(struct hdac_softc *)
761  *
762  * Free up resources previously allocated by hdac_irq_alloc.
763  ****************************************************************************/
764 static void
765 hdac_irq_free(struct hdac_softc *sc)
766 {
767 	struct hdac_irq *irq;
768 
769 	irq = &sc->irq;
770 	if (irq->irq_res != NULL && irq->irq_handle != NULL)
771 		bus_teardown_intr(sc->dev, irq->irq_res, irq->irq_handle);
772 	if (irq->irq_res != NULL)
773 		bus_release_resource(sc->dev, SYS_RES_IRQ, irq->irq_rid,
774 		    irq->irq_res);
775 	if (irq->irq_rid == 0x1)
776 		pci_release_msi(sc->dev);
777 	irq->irq_handle = NULL;
778 	irq->irq_res = NULL;
779 	irq->irq_rid = 0x0;
780 }
781 
782 /****************************************************************************
783  * void hdac_corb_init(struct hdac_softc *)
784  *
785  * Initialize the corb registers for operations but do not start it up yet.
786  * The CORB engine must not be running when this function is called.
787  ****************************************************************************/
788 static void
789 hdac_corb_init(struct hdac_softc *sc)
790 {
791 	uint8_t corbsize;
792 	uint64_t corbpaddr;
793 
794 	/* Setup the CORB size. */
795 	switch (sc->corb_size) {
796 	case 256:
797 		corbsize = HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_256);
798 		break;
799 	case 16:
800 		corbsize = HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_16);
801 		break;
802 	case 2:
803 		corbsize = HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_2);
804 		break;
805 	default:
806 		panic("%s: Invalid CORB size (%x)\n", __func__, sc->corb_size);
807 	}
808 	HDAC_WRITE_1(&sc->mem, HDAC_CORBSIZE, corbsize);
809 
810 	/* Setup the CORB Address in the hdac */
811 	corbpaddr = (uint64_t)sc->corb_dma.dma_paddr;
812 	HDAC_WRITE_4(&sc->mem, HDAC_CORBLBASE, (uint32_t)corbpaddr);
813 	HDAC_WRITE_4(&sc->mem, HDAC_CORBUBASE, (uint32_t)(corbpaddr >> 32));
814 
815 	/* Set the WP and RP */
816 	sc->corb_wp = 0;
817 	HDAC_WRITE_2(&sc->mem, HDAC_CORBWP, sc->corb_wp);
818 	HDAC_WRITE_2(&sc->mem, HDAC_CORBRP, HDAC_CORBRP_CORBRPRST);
819 	/*
820 	 * The HDA specification indicates that the CORBRPRST bit will always
821 	 * read as zero. Unfortunately, it seems that at least the 82801G
822 	 * doesn't reset the bit to zero, which stalls the corb engine.
823 	 * manually reset the bit to zero before continuing.
824 	 */
825 	HDAC_WRITE_2(&sc->mem, HDAC_CORBRP, 0x0);
826 
827 	/* Enable CORB error reporting */
828 #if 0
829 	HDAC_WRITE_1(&sc->mem, HDAC_CORBCTL, HDAC_CORBCTL_CMEIE);
830 #endif
831 }
832 
833 /****************************************************************************
834  * void hdac_rirb_init(struct hdac_softc *)
835  *
836  * Initialize the rirb registers for operations but do not start it up yet.
837  * The RIRB engine must not be running when this function is called.
838  ****************************************************************************/
839 static void
840 hdac_rirb_init(struct hdac_softc *sc)
841 {
842 	uint8_t rirbsize;
843 	uint64_t rirbpaddr;
844 
845 	/* Setup the RIRB size. */
846 	switch (sc->rirb_size) {
847 	case 256:
848 		rirbsize = HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_256);
849 		break;
850 	case 16:
851 		rirbsize = HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_16);
852 		break;
853 	case 2:
854 		rirbsize = HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_2);
855 		break;
856 	default:
857 		panic("%s: Invalid RIRB size (%x)\n", __func__, sc->rirb_size);
858 	}
859 	HDAC_WRITE_1(&sc->mem, HDAC_RIRBSIZE, rirbsize);
860 
861 	/* Setup the RIRB Address in the hdac */
862 	rirbpaddr = (uint64_t)sc->rirb_dma.dma_paddr;
863 	HDAC_WRITE_4(&sc->mem, HDAC_RIRBLBASE, (uint32_t)rirbpaddr);
864 	HDAC_WRITE_4(&sc->mem, HDAC_RIRBUBASE, (uint32_t)(rirbpaddr >> 32));
865 
866 	/* Setup the WP and RP */
867 	sc->rirb_rp = 0;
868 	HDAC_WRITE_2(&sc->mem, HDAC_RIRBWP, HDAC_RIRBWP_RIRBWPRST);
869 
870 	/* Setup the interrupt threshold */
871 	HDAC_WRITE_2(&sc->mem, HDAC_RINTCNT, sc->rirb_size / 2);
872 
873 	/* Enable Overrun and response received reporting */
874 #if 0
875 	HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL,
876 	    HDAC_RIRBCTL_RIRBOIC | HDAC_RIRBCTL_RINTCTL);
877 #else
878 	HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, HDAC_RIRBCTL_RINTCTL);
879 #endif
880 
881 #if 0
882 	/*
883 	 * Make sure that the Host CPU cache doesn't contain any dirty
884 	 * cache lines that falls in the rirb. If I understood correctly, it
885 	 * should be sufficient to do this only once as the rirb is purely
886 	 * read-only from now on.
887 	 */
888 	bus_dmamap_sync(sc->rirb_dma.dma_tag, sc->rirb_dma.dma_map,
889 	    BUS_DMASYNC_PREREAD);
890 #endif
891 }
892 
893 /****************************************************************************
894  * void hdac_corb_start(hdac_softc *)
895  *
896  * Startup the corb DMA engine
897  ****************************************************************************/
898 static void
899 hdac_corb_start(struct hdac_softc *sc)
900 {
901 	uint32_t corbctl;
902 
903 	corbctl = HDAC_READ_1(&sc->mem, HDAC_CORBCTL);
904 	corbctl |= HDAC_CORBCTL_CORBRUN;
905 	HDAC_WRITE_1(&sc->mem, HDAC_CORBCTL, corbctl);
906 }
907 
908 /****************************************************************************
909  * void hdac_rirb_start(hdac_softc *)
910  *
911  * Startup the rirb DMA engine
912  ****************************************************************************/
913 static void
914 hdac_rirb_start(struct hdac_softc *sc)
915 {
916 	uint32_t rirbctl;
917 
918 	rirbctl = HDAC_READ_1(&sc->mem, HDAC_RIRBCTL);
919 	rirbctl |= HDAC_RIRBCTL_RIRBDMAEN;
920 	HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, rirbctl);
921 }
922 
923 static int
924 hdac_rirb_flush(struct hdac_softc *sc)
925 {
926 	struct hdac_rirb *rirb_base, *rirb;
927 	nid_t cad;
928 	uint32_t resp;
929 	uint8_t rirbwp;
930 	int ret;
931 
932 	rirb_base = (struct hdac_rirb *)sc->rirb_dma.dma_vaddr;
933 	rirbwp = HDAC_READ_1(&sc->mem, HDAC_RIRBWP);
934 #if 0
935 	bus_dmamap_sync(sc->rirb_dma.dma_tag, sc->rirb_dma.dma_map,
936 	    BUS_DMASYNC_POSTREAD);
937 #endif
938 
939 	ret = 0;
940 	while (sc->rirb_rp != rirbwp) {
941 		sc->rirb_rp++;
942 		sc->rirb_rp %= sc->rirb_size;
943 		rirb = &rirb_base[sc->rirb_rp];
944 		cad = HDAC_RIRB_RESPONSE_EX_SDATA_IN(rirb->response_ex);
945 		resp = rirb->response;
946 		if (rirb->response_ex & HDAC_RIRB_RESPONSE_EX_UNSOLICITED) {
947 			sc->unsolq[sc->unsolq_wp++] = resp;
948 			sc->unsolq_wp %= HDAC_UNSOLQ_MAX;
949 			sc->unsolq[sc->unsolq_wp++] = cad;
950 			sc->unsolq_wp %= HDAC_UNSOLQ_MAX;
951 		} else if (sc->codecs[cad].pending <= 0) {
952 			device_printf(sc->dev, "Unexpected unsolicited "
953 			    "response from address %d: %08x\n", cad, resp);
954 		} else {
955 			sc->codecs[cad].response = resp;
956 			sc->codecs[cad].pending--;
957 		}
958 		ret++;
959 	}
960 	return (ret);
961 }
962 
963 static int
964 hdac_unsolq_flush(struct hdac_softc *sc)
965 {
966 	device_t child;
967 	nid_t cad;
968 	uint32_t resp;
969 	int ret = 0;
970 
971 	if (sc->unsolq_st == HDAC_UNSOLQ_READY) {
972 		sc->unsolq_st = HDAC_UNSOLQ_BUSY;
973 		while (sc->unsolq_rp != sc->unsolq_wp) {
974 			resp = sc->unsolq[sc->unsolq_rp++];
975 			sc->unsolq_rp %= HDAC_UNSOLQ_MAX;
976 			cad = sc->unsolq[sc->unsolq_rp++];
977 			sc->unsolq_rp %= HDAC_UNSOLQ_MAX;
978 			if ((child = sc->codecs[cad].dev) != NULL)
979 				HDAC_UNSOL_INTR(child, resp);
980 			ret++;
981 		}
982 		sc->unsolq_st = HDAC_UNSOLQ_READY;
983 	}
984 
985 	return (ret);
986 }
987 
988 /****************************************************************************
989  * uint32_t hdac_command_sendone_internal
990  *
991  * Wrapper function that sends only one command to a given codec
992  ****************************************************************************/
993 static uint32_t
994 hdac_send_command(struct hdac_softc *sc, nid_t cad, uint32_t verb)
995 {
996 	int timeout;
997 	uint32_t *corb;
998 
999 	if (!hdac_lockowned(sc))
1000 		device_printf(sc->dev, "WARNING!!!! mtx not owned!!!!\n");
1001 	verb &= ~HDA_CMD_CAD_MASK;
1002 	verb |= ((uint32_t)cad) << HDA_CMD_CAD_SHIFT;
1003 	sc->codecs[cad].response = HDA_INVALID;
1004 
1005 	sc->codecs[cad].pending++;
1006 	sc->corb_wp++;
1007 	sc->corb_wp %= sc->corb_size;
1008 	corb = (uint32_t *)sc->corb_dma.dma_vaddr;
1009 #if 0
1010 	bus_dmamap_sync(sc->corb_dma.dma_tag,
1011 	    sc->corb_dma.dma_map, BUS_DMASYNC_PREWRITE);
1012 #endif
1013 	corb[sc->corb_wp] = verb;
1014 #if 0
1015 	bus_dmamap_sync(sc->corb_dma.dma_tag,
1016 	    sc->corb_dma.dma_map, BUS_DMASYNC_POSTWRITE);
1017 #endif
1018 	HDAC_WRITE_2(&sc->mem, HDAC_CORBWP, sc->corb_wp);
1019 
1020 	timeout = 10000;
1021 	do {
1022 		if (hdac_rirb_flush(sc) == 0)
1023 			DELAY(10);
1024 	} while (sc->codecs[cad].pending != 0 && --timeout);
1025 
1026 	if (sc->codecs[cad].pending != 0) {
1027 		device_printf(sc->dev, "Command timeout on address %d\n", cad);
1028 		sc->codecs[cad].pending = 0;
1029 	}
1030 
1031 	if (sc->unsolq_rp != sc->unsolq_wp)
1032 		taskqueue_enqueue(taskqueue_thread[mycpuid], &sc->unsolq_task);
1033 	return (sc->codecs[cad].response);
1034 }
1035 
1036 /****************************************************************************
1037  * Device Methods
1038  ****************************************************************************/
1039 
1040 /****************************************************************************
1041  * int hdac_probe(device_t)
1042  *
1043  * Probe for the presence of an hdac. If none is found, check for a generic
1044  * match using the subclass of the device.
1045  ****************************************************************************/
1046 static int
1047 hdac_probe(device_t dev)
1048 {
1049 	int i, result;
1050 	uint32_t model;
1051 	uint16_t class, subclass;
1052 	char desc[64];
1053 
1054 	model = (uint32_t)pci_get_device(dev) << 16;
1055 	model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
1056 	class = pci_get_class(dev);
1057 	subclass = pci_get_subclass(dev);
1058 
1059 	bzero(desc, sizeof(desc));
1060 	result = ENXIO;
1061 	for (i = 0; i < nitems(hdac_devices); i++) {
1062 		if (hdac_devices[i].model == model) {
1063 			strlcpy(desc, hdac_devices[i].desc, sizeof(desc));
1064 			result = BUS_PROBE_DEFAULT;
1065 			break;
1066 		}
1067 		if (HDA_DEV_MATCH(hdac_devices[i].model, model) &&
1068 		    class == PCIC_MULTIMEDIA &&
1069 		    subclass == PCIS_MULTIMEDIA_HDA) {
1070 			ksnprintf(desc, sizeof(desc),
1071 			    "%s (0x%04x)",
1072 			    hdac_devices[i].desc, pci_get_device(dev));
1073 			result = BUS_PROBE_GENERIC;
1074 			break;
1075 		}
1076 	}
1077 	if (result == ENXIO && class == PCIC_MULTIMEDIA &&
1078 	    subclass == PCIS_MULTIMEDIA_HDA) {
1079 		ksnprintf(desc, sizeof(desc), "Generic (0x%08x)", model);
1080 		result = BUS_PROBE_GENERIC;
1081 	}
1082 	if (result != ENXIO) {
1083 		strlcat(desc, " HDA Controller", sizeof(desc));
1084 		device_set_desc_copy(dev, desc);
1085 	}
1086 
1087 	return (result);
1088 }
1089 
1090 static void
1091 hdac_unsolq_task(void *context, int pending)
1092 {
1093 	struct hdac_softc *sc;
1094 
1095 	sc = (struct hdac_softc *)context;
1096 
1097 	hdac_lock(sc);
1098 	hdac_unsolq_flush(sc);
1099 	hdac_unlock(sc);
1100 }
1101 
1102 /****************************************************************************
1103  * int hdac_attach(device_t)
1104  *
1105  * Attach the device into the kernel. Interrupts usually won't be enabled
1106  * when this function is called. Setup everything that doesn't require
1107  * interrupts and defer probing of codecs until interrupts are enabled.
1108  ****************************************************************************/
1109 static int
1110 hdac_attach(device_t dev)
1111 {
1112 	struct hdac_softc *sc;
1113 	int result;
1114 	int i, devid = -1;
1115 	uint32_t model;
1116 	uint16_t class, subclass;
1117 	uint16_t vendor;
1118 	uint8_t v;
1119 
1120 	sc = device_get_softc(dev);
1121 	HDA_BOOTVERBOSE(
1122 		device_printf(dev, "PCI card vendor: 0x%04x, device: 0x%04x\n",
1123 		    pci_get_subvendor(dev), pci_get_subdevice(dev));
1124 		device_printf(dev, "HDA Driver Revision: %s\n",
1125 		    HDA_DRV_TEST_REV);
1126 	);
1127 
1128 	model = (uint32_t)pci_get_device(dev) << 16;
1129 	model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
1130 	class = pci_get_class(dev);
1131 	subclass = pci_get_subclass(dev);
1132 
1133 	for (i = 0; i < nitems(hdac_devices); i++) {
1134 		if (hdac_devices[i].model == model) {
1135 			devid = i;
1136 			break;
1137 		}
1138 		if (HDA_DEV_MATCH(hdac_devices[i].model, model) &&
1139 		    class == PCIC_MULTIMEDIA &&
1140 		    subclass == PCIS_MULTIMEDIA_HDA) {
1141 			devid = i;
1142 			break;
1143 		}
1144 	}
1145 
1146 	sc->lock = snd_mtxcreate(device_get_nameunit(dev), "HDA driver mutex");
1147 	sc->dev = dev;
1148 	TASK_INIT(&sc->unsolq_task, 0, hdac_unsolq_task, sc);
1149 	callout_init_mp(&sc->poll_callout);
1150 	for (i = 0; i < HDAC_CODEC_MAX; i++)
1151 		sc->codecs[i].dev = NULL;
1152 	if (devid >= 0) {
1153 		sc->quirks_on = hdac_devices[devid].quirks_on;
1154 		sc->quirks_off = hdac_devices[devid].quirks_off;
1155 	} else {
1156 		sc->quirks_on = 0;
1157 		sc->quirks_off = 0;
1158 	}
1159 	hdac_config_fetch(sc, &sc->quirks_on, &sc->quirks_off);
1160 	if (resource_int_value(device_get_name(dev),
1161 			       device_get_unit(dev), "msi", &i) == 0) {
1162 		if (i == 0) {
1163 			sc->quirks_on &= ~HDAC_QUIRK_MSI;
1164 			sc->quirks_off |= HDAC_QUIRK_MSI;
1165 		} else {
1166 			sc->quirks_on |= HDAC_QUIRK_MSI;
1167 			sc->quirks_off &= ~HDAC_QUIRK_MSI;
1168 		}
1169 	}
1170 	HDA_BOOTVERBOSE(
1171 		device_printf(sc->dev,
1172 		    "Config options: on=0x%08x off=0x%08x\n",
1173 		    sc->quirks_on, sc->quirks_off);
1174 	);
1175 	sc->poll_ival = hz;
1176 	if (resource_int_value(device_get_name(dev),
1177 	    device_get_unit(dev), "polling", &i) == 0 && i != 0)
1178 		sc->polling = 1;
1179 	else
1180 		sc->polling = 0;
1181 
1182 	pci_enable_busmaster(dev);
1183 
1184 	vendor = pci_get_vendor(dev);
1185 	if (vendor == INTEL_VENDORID) {
1186 		/* TCSEL -> TC0 */
1187 		v = pci_read_config(dev, 0x44, 1);
1188 		pci_write_config(dev, 0x44, v & 0xf8, 1);
1189 		HDA_BOOTHVERBOSE(
1190 			device_printf(dev, "TCSEL: 0x%02d -> 0x%02d\n", v,
1191 			    pci_read_config(dev, 0x44, 1));
1192 		);
1193 	}
1194 
1195 	sc->flags |= HDAC_F_DMA_NOCACHE;
1196 	/*
1197 	 * Try to enable PCIe snoop to avoid messing around with
1198 	 * uncacheable DMA attribute.
1199 	 */
1200 	if (pci_is_pcie(dev)) {
1201 		int pcie_cap = pci_get_pciecap_ptr(dev);
1202 		uint16_t dev_ctl;
1203 
1204 		dev_ctl = pci_read_config(dev,
1205 		    pcie_cap + PCIER_DEVCTRL, 2);
1206 		device_printf(dev, "link ctrl %#x\n", dev_ctl);
1207 
1208 		if (dev_ctl & PCIEM_DEVCTL_NOSNOOP) {
1209 			dev_ctl &= ~PCIEM_DEVCTL_NOSNOOP;
1210 			pci_write_config(dev,
1211 			    pcie_cap + PCIER_DEVCTRL, dev_ctl, 2);
1212 
1213 			device_printf(dev, "disable nosnoop\n");
1214 		}
1215 		sc->flags &= ~HDAC_F_DMA_NOCACHE;
1216 	}
1217 
1218 	HDA_BOOTHVERBOSE(
1219 		device_printf(dev, "DMA Coherency: %s / vendor=0x%04x\n",
1220 		    (sc->flags & HDAC_F_DMA_NOCACHE) ?
1221 		    "Uncacheable" : "PCIe snoop", vendor);
1222 	);
1223 
1224 	/* Allocate resources */
1225 	result = hdac_mem_alloc(sc);
1226 	if (result != 0)
1227 		goto hdac_attach_fail;
1228 	result = hdac_irq_alloc(sc);
1229 	if (result != 0)
1230 		goto hdac_attach_fail;
1231 
1232 	hdac_reset(sc, 1);
1233 
1234 	/* Get Capabilities */
1235 	result = hdac_get_capabilities(sc);
1236 	if (result != 0)
1237 		goto hdac_attach_fail;
1238 
1239 	/* Allocate CORB, RIRB, POS and BDLs dma memory */
1240 	result = hdac_dma_alloc(sc, &sc->corb_dma,
1241 	    sc->corb_size * sizeof(uint32_t));
1242 	if (result != 0)
1243 		goto hdac_attach_fail;
1244 	result = hdac_dma_alloc(sc, &sc->rirb_dma,
1245 	    sc->rirb_size * sizeof(struct hdac_rirb));
1246 	if (result != 0)
1247 		goto hdac_attach_fail;
1248 	sc->streams = kmalloc(sizeof(struct hdac_stream) * sc->num_ss,
1249 	    M_HDAC, M_ZERO | M_WAITOK);
1250 	for (i = 0; i < sc->num_ss; i++) {
1251 		result = hdac_dma_alloc(sc, &sc->streams[i].bdl,
1252 		    sizeof(struct hdac_bdle) * HDA_BDL_MAX);
1253 		if (result != 0)
1254 			goto hdac_attach_fail;
1255 	}
1256 	if (sc->quirks_on & HDAC_QUIRK_DMAPOS) {
1257 		if (hdac_dma_alloc(sc, &sc->pos_dma, (sc->num_ss) * 8) != 0) {
1258 			HDA_BOOTVERBOSE(
1259 				device_printf(dev, "Failed to "
1260 				    "allocate DMA pos buffer "
1261 				    "(non-fatal)\n");
1262 			);
1263 		} else {
1264 			uint64_t addr = sc->pos_dma.dma_paddr;
1265 
1266 			HDAC_WRITE_4(&sc->mem, HDAC_DPIBUBASE, addr >> 32);
1267 			HDAC_WRITE_4(&sc->mem, HDAC_DPIBLBASE,
1268 			    (addr & HDAC_DPLBASE_DPLBASE_MASK) |
1269 			    HDAC_DPLBASE_DPLBASE_DMAPBE);
1270 		}
1271 	}
1272 
1273 	result = bus_dma_tag_create(
1274 	    bus_get_dma_tag(sc->dev),		/* parent */
1275 	    HDA_DMA_ALIGNMENT,			/* alignment */
1276 	    0,					/* boundary */
1277 	    (sc->support_64bit) ? BUS_SPACE_MAXADDR :
1278 		BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1279 	    BUS_SPACE_MAXADDR,			/* highaddr */
1280 	    NULL,				/* filtfunc */
1281 	    NULL,				/* fistfuncarg */
1282 	    HDA_BUFSZ_MAX, 			/* maxsize */
1283 	    1,					/* nsegments */
1284 	    HDA_BUFSZ_MAX, 			/* maxsegsz */
1285 	    0,					/* flags */
1286 	    &sc->chan_dmat);			/* dmat */
1287 	if (result != 0) {
1288 		device_printf(dev, "%s: bus_dma_tag_create failed (%x)\n",
1289 		     __func__, result);
1290 		goto hdac_attach_fail;
1291 	}
1292 
1293 	/* Quiesce everything */
1294 	HDA_BOOTHVERBOSE(
1295 		device_printf(dev, "Reset controller...\n");
1296 	);
1297 	hdac_reset(sc, 1);
1298 
1299 	/* Initialize the CORB and RIRB */
1300 	hdac_corb_init(sc);
1301 	hdac_rirb_init(sc);
1302 
1303 	/* Defer remaining of initialization until interrupts are enabled */
1304 	sc->intrhook.ich_func = hdac_attach2;
1305 	sc->intrhook.ich_arg = (void *)sc;
1306 	sc->intrhook.ich_desc = "snd_hda";
1307 	if (cold == 0 || config_intrhook_establish(&sc->intrhook) != 0) {
1308 		sc->intrhook.ich_func = NULL;
1309 		hdac_attach2((void *)sc);
1310 	}
1311 
1312 	return (0);
1313 
1314 hdac_attach_fail:
1315 	hdac_irq_free(sc);
1316 	for (i = 0; i < sc->num_ss; i++)
1317 		hdac_dma_free(sc, &sc->streams[i].bdl);
1318 	if (sc->streams != NULL)
1319 		kfree(sc->streams, M_HDAC);
1320 	hdac_dma_free(sc, &sc->rirb_dma);
1321 	hdac_dma_free(sc, &sc->corb_dma);
1322 	hdac_mem_free(sc);
1323 	snd_mtxfree(sc->lock);
1324 
1325 	return (ENXIO);
1326 }
1327 
1328 static int
1329 sysctl_hdac_pindump(SYSCTL_HANDLER_ARGS)
1330 {
1331 	struct hdac_softc *sc;
1332 	device_t *devlist;
1333 	device_t dev;
1334 	int devcount, i, err, val;
1335 
1336 	dev = oidp->oid_arg1;
1337 	sc = device_get_softc(dev);
1338 	if (sc == NULL)
1339 		return (EINVAL);
1340 	val = 0;
1341 	err = sysctl_handle_int(oidp, &val, 0, req);
1342 	if (err != 0 || req->newptr == NULL || val == 0)
1343 		return (err);
1344 
1345 	/* XXX: Temporary. For debugging. */
1346 	if (val == 100) {
1347 		hdac_suspend(dev);
1348 		return (0);
1349 	} else if (val == 101) {
1350 		hdac_resume(dev);
1351 		return (0);
1352 	}
1353 
1354 	if ((err = device_get_children(dev, &devlist, &devcount)) != 0)
1355 		return (err);
1356 	hdac_lock(sc);
1357 	for (i = 0; i < devcount; i++)
1358 		HDAC_PINDUMP(devlist[i]);
1359 	hdac_unlock(sc);
1360 	kfree(devlist, M_TEMP);
1361 	return (0);
1362 }
1363 
1364 static int
1365 hdac_mdata_rate(uint16_t fmt)
1366 {
1367 	static const int mbits[8] = { 8, 16, 32, 32, 32, 32, 32, 32 };
1368 	int rate, bits;
1369 
1370 	if (fmt & (1 << 14))
1371 		rate = 44100;
1372 	else
1373 		rate = 48000;
1374 	rate *= ((fmt >> 11) & 0x07) + 1;
1375 	rate /= ((fmt >> 8) & 0x07) + 1;
1376 	bits = mbits[(fmt >> 4) & 0x03];
1377 	bits *= (fmt & 0x0f) + 1;
1378 	return (rate * bits);
1379 }
1380 
1381 static int
1382 hdac_bdata_rate(uint16_t fmt, int output)
1383 {
1384 	static const int bbits[8] = { 8, 16, 20, 24, 32, 32, 32, 32 };
1385 	int rate, bits;
1386 
1387 	rate = 48000;
1388 	rate *= ((fmt >> 11) & 0x07) + 1;
1389 	bits = bbits[(fmt >> 4) & 0x03];
1390 	bits *= (fmt & 0x0f) + 1;
1391 	if (!output)
1392 		bits = ((bits + 7) & ~0x07) + 10;
1393 	return (rate * bits);
1394 }
1395 
1396 static void
1397 hdac_poll_reinit(struct hdac_softc *sc)
1398 {
1399 	int i, pollticks, min = 1000000;
1400 	struct hdac_stream *s;
1401 
1402 	if (sc->polling == 0)
1403 		return;
1404 	if (sc->unsol_registered > 0)
1405 		min = hz / 2;
1406 	for (i = 0; i < sc->num_ss; i++) {
1407 		s = &sc->streams[i];
1408 		if (s->running == 0)
1409 			continue;
1410 		pollticks = ((uint64_t)hz * s->blksz) /
1411 		    (hdac_mdata_rate(s->format) / 8);
1412 		pollticks >>= 1;
1413 		if (pollticks > hz)
1414 			pollticks = hz;
1415 		if (pollticks < 1) {
1416 			HDA_BOOTVERBOSE(
1417 				device_printf(sc->dev,
1418 				    "poll interval < 1 tick !\n");
1419 			);
1420 			pollticks = 1;
1421 		}
1422 		if (min > pollticks)
1423 			min = pollticks;
1424 	}
1425 	HDA_BOOTVERBOSE(
1426 		device_printf(sc->dev,
1427 		    "poll interval %d -> %d ticks\n",
1428 		    sc->poll_ival, min);
1429 	);
1430 	sc->poll_ival = min;
1431 	if (min == 1000000)
1432 		callout_stop(&sc->poll_callout);
1433 	else
1434 		callout_reset(&sc->poll_callout, 1, hdac_poll_callback, sc);
1435 }
1436 
1437 static int
1438 sysctl_hdac_polling(SYSCTL_HANDLER_ARGS)
1439 {
1440 	struct hdac_softc *sc;
1441 	device_t dev;
1442 	uint32_t ctl;
1443 	int err, val;
1444 
1445 	dev = oidp->oid_arg1;
1446 	sc = device_get_softc(dev);
1447 	if (sc == NULL)
1448 		return (EINVAL);
1449 	hdac_lock(sc);
1450 	val = sc->polling;
1451 	hdac_unlock(sc);
1452 	err = sysctl_handle_int(oidp, &val, 0, req);
1453 
1454 	if (err != 0 || req->newptr == NULL)
1455 		return (err);
1456 	if (val < 0 || val > 1)
1457 		return (EINVAL);
1458 
1459 	hdac_lock(sc);
1460 	if (val != sc->polling) {
1461 		if (val == 0) {
1462 			callout_stop(&sc->poll_callout);
1463 			hdac_unlock(sc);
1464 			callout_drain(&sc->poll_callout);
1465 			hdac_lock(sc);
1466 			sc->polling = 0;
1467 			ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
1468 			ctl |= HDAC_INTCTL_GIE;
1469 			HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
1470 		} else {
1471 			ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
1472 			ctl &= ~HDAC_INTCTL_GIE;
1473 			HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
1474 			sc->polling = 1;
1475 			hdac_poll_reinit(sc);
1476 		}
1477 	}
1478 	hdac_unlock(sc);
1479 
1480 	return (err);
1481 }
1482 
1483 static void
1484 hdac_attach2(void *arg)
1485 {
1486 	struct hdac_softc *sc;
1487 	device_t child;
1488 	uint32_t vendorid, revisionid;
1489 	int i;
1490 	uint16_t statests;
1491 
1492 	sc = (struct hdac_softc *)arg;
1493 
1494 	hdac_lock(sc);
1495 
1496 	/* Remove ourselves from the config hooks */
1497 	if (sc->intrhook.ich_func != NULL) {
1498 		config_intrhook_disestablish(&sc->intrhook);
1499 		sc->intrhook.ich_func = NULL;
1500 	}
1501 
1502 	HDA_BOOTHVERBOSE(
1503 		device_printf(sc->dev, "Starting CORB Engine...\n");
1504 	);
1505 	hdac_corb_start(sc);
1506 	HDA_BOOTHVERBOSE(
1507 		device_printf(sc->dev, "Starting RIRB Engine...\n");
1508 	);
1509 	hdac_rirb_start(sc);
1510 	HDA_BOOTHVERBOSE(
1511 		device_printf(sc->dev,
1512 		    "Enabling controller interrupt...\n");
1513 	);
1514 	HDAC_WRITE_4(&sc->mem, HDAC_GCTL, HDAC_READ_4(&sc->mem, HDAC_GCTL) |
1515 	    HDAC_GCTL_UNSOL);
1516 	if (sc->polling == 0) {
1517 		HDAC_WRITE_4(&sc->mem, HDAC_INTCTL,
1518 		    HDAC_INTCTL_CIE | HDAC_INTCTL_GIE);
1519 	}
1520 	DELAY(1000);
1521 
1522 	HDA_BOOTHVERBOSE(
1523 		device_printf(sc->dev, "Scanning HDA codecs ...\n");
1524 	);
1525 	statests = HDAC_READ_2(&sc->mem, HDAC_STATESTS);
1526 	hdac_unlock(sc);
1527 	for (i = 0; i < HDAC_CODEC_MAX; i++) {
1528 		if (HDAC_STATESTS_SDIWAKE(statests, i)) {
1529 			HDA_BOOTHVERBOSE(
1530 				device_printf(sc->dev,
1531 				    "Found CODEC at address %d\n", i);
1532 			);
1533 			hdac_lock(sc);
1534 			vendorid = hdac_send_command(sc, i,
1535 			    HDA_CMD_GET_PARAMETER(0, 0x0, HDA_PARAM_VENDOR_ID));
1536 			revisionid = hdac_send_command(sc, i,
1537 			    HDA_CMD_GET_PARAMETER(0, 0x0, HDA_PARAM_REVISION_ID));
1538 			hdac_unlock(sc);
1539 			if (vendorid == HDA_INVALID &&
1540 			    revisionid == HDA_INVALID) {
1541 				device_printf(sc->dev,
1542 				    "CODEC is not responding!\n");
1543 				continue;
1544 			}
1545 			sc->codecs[i].vendor_id =
1546 			    HDA_PARAM_VENDOR_ID_VENDOR_ID(vendorid);
1547 			sc->codecs[i].device_id =
1548 			    HDA_PARAM_VENDOR_ID_DEVICE_ID(vendorid);
1549 			sc->codecs[i].revision_id =
1550 			    HDA_PARAM_REVISION_ID_REVISION_ID(revisionid);
1551 			sc->codecs[i].stepping_id =
1552 			    HDA_PARAM_REVISION_ID_STEPPING_ID(revisionid);
1553 			child = device_add_child(sc->dev, "hdacc", -1);
1554 			if (child == NULL) {
1555 				device_printf(sc->dev,
1556 				    "Failed to add CODEC device\n");
1557 				continue;
1558 			}
1559 			device_set_ivars(child, (void *)(intptr_t)i);
1560 			sc->codecs[i].dev = child;
1561 		}
1562 	}
1563 	bus_generic_attach(sc->dev);
1564 
1565 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
1566 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
1567 	    "pindump", CTLTYPE_INT | CTLFLAG_RW, sc->dev, sizeof(sc->dev),
1568 	    sysctl_hdac_pindump, "I", "Dump pin states/data");
1569 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
1570 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
1571 	    "polling", CTLTYPE_INT | CTLFLAG_RW, sc->dev, sizeof(sc->dev),
1572 	    sysctl_hdac_polling, "I", "Enable polling mode");
1573 }
1574 
1575 /****************************************************************************
1576  * int hdac_suspend(device_t)
1577  *
1578  * Suspend and power down HDA bus and codecs.
1579  ****************************************************************************/
1580 static int
1581 hdac_suspend(device_t dev)
1582 {
1583 	struct hdac_softc *sc = device_get_softc(dev);
1584 
1585 	HDA_BOOTHVERBOSE(
1586 		device_printf(dev, "Suspend...\n");
1587 	);
1588 	bus_generic_suspend(dev);
1589 
1590 	hdac_lock(sc);
1591 	HDA_BOOTHVERBOSE(
1592 		device_printf(dev, "Reset controller...\n");
1593 	);
1594 	callout_stop(&sc->poll_callout);
1595 	hdac_reset(sc, 0);
1596 	hdac_unlock(sc);
1597 	callout_drain(&sc->poll_callout);
1598 	taskqueue_drain(taskqueue_thread[mycpuid], &sc->unsolq_task);
1599 	HDA_BOOTHVERBOSE(
1600 		device_printf(dev, "Suspend done\n");
1601 	);
1602 	return (0);
1603 }
1604 
1605 /****************************************************************************
1606  * int hdac_resume(device_t)
1607  *
1608  * Powerup and restore HDA bus and codecs state.
1609  ****************************************************************************/
1610 static int
1611 hdac_resume(device_t dev)
1612 {
1613 	struct hdac_softc *sc = device_get_softc(dev);
1614 	int error;
1615 
1616 	HDA_BOOTHVERBOSE(
1617 		device_printf(dev, "Resume...\n");
1618 	);
1619 	hdac_lock(sc);
1620 
1621 	/* Quiesce everything */
1622 	HDA_BOOTHVERBOSE(
1623 		device_printf(dev, "Reset controller...\n");
1624 	);
1625 	hdac_reset(sc, 1);
1626 
1627 	/* Initialize the CORB and RIRB */
1628 	hdac_corb_init(sc);
1629 	hdac_rirb_init(sc);
1630 
1631 	HDA_BOOTHVERBOSE(
1632 		device_printf(dev, "Starting CORB Engine...\n");
1633 	);
1634 	hdac_corb_start(sc);
1635 	HDA_BOOTHVERBOSE(
1636 		device_printf(dev, "Starting RIRB Engine...\n");
1637 	);
1638 	hdac_rirb_start(sc);
1639 	HDA_BOOTHVERBOSE(
1640 		device_printf(dev, "Enabling controller interrupt...\n");
1641 	);
1642 	HDAC_WRITE_4(&sc->mem, HDAC_GCTL, HDAC_READ_4(&sc->mem, HDAC_GCTL) |
1643 	    HDAC_GCTL_UNSOL);
1644 	HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, HDAC_INTCTL_CIE | HDAC_INTCTL_GIE);
1645 	DELAY(1000);
1646 	hdac_poll_reinit(sc);
1647 	hdac_unlock(sc);
1648 
1649 	error = bus_generic_resume(dev);
1650 	HDA_BOOTHVERBOSE(
1651 		device_printf(dev, "Resume done\n");
1652 	);
1653 	return (error);
1654 }
1655 
1656 /****************************************************************************
1657  * int hdac_detach(device_t)
1658  *
1659  * Detach and free up resources utilized by the hdac device.
1660  ****************************************************************************/
1661 static int
1662 hdac_detach(device_t dev)
1663 {
1664 	struct hdac_softc *sc = device_get_softc(dev);
1665 	device_t *devlist;
1666 	int cad, i, devcount, error;
1667 
1668 	if ((error = device_get_children(dev, &devlist, &devcount)) != 0)
1669 		return (error);
1670 	for (i = 0; i < devcount; i++) {
1671 		cad = (intptr_t)device_get_ivars(devlist[i]);
1672 		if ((error = device_delete_child(dev, devlist[i])) != 0) {
1673 			kfree(devlist, M_TEMP);
1674 			return (error);
1675 		}
1676 		sc->codecs[cad].dev = NULL;
1677 	}
1678 	kfree(devlist, M_TEMP);
1679 
1680 	hdac_lock(sc);
1681 	hdac_reset(sc, 0);
1682 	hdac_unlock(sc);
1683 	taskqueue_drain(taskqueue_thread[mycpuid], &sc->unsolq_task);
1684 	hdac_irq_free(sc);
1685 
1686 	/* give pending interrupts stuck on the lock a chance to clear */
1687 	/* bad hack */
1688 	tsleep(&sc->irq, 0, "hdaslp", hz / 10);
1689 
1690 	for (i = 0; i < sc->num_ss; i++)
1691 		hdac_dma_free(sc, &sc->streams[i].bdl);
1692 	kfree(sc->streams, M_HDAC);
1693 	hdac_dma_free(sc, &sc->pos_dma);
1694 	hdac_dma_free(sc, &sc->rirb_dma);
1695 	hdac_dma_free(sc, &sc->corb_dma);
1696 	if (sc->chan_dmat != NULL) {
1697 		bus_dma_tag_destroy(sc->chan_dmat);
1698 		sc->chan_dmat = NULL;
1699 	}
1700 	hdac_mem_free(sc);
1701 	snd_mtxfree(sc->lock);
1702 	return (0);
1703 }
1704 
1705 static bus_dma_tag_t
1706 hdac_get_dma_tag(device_t dev, device_t child)
1707 {
1708 	struct hdac_softc *sc = device_get_softc(dev);
1709 
1710 	return (sc->chan_dmat);
1711 }
1712 
1713 static int
1714 hdac_print_child(device_t dev, device_t child)
1715 {
1716 	int retval;
1717 
1718 	retval = bus_print_child_header(dev, child);
1719 	retval += kprintf(" at cad %d",
1720 	    (int)(intptr_t)device_get_ivars(child));
1721 	retval += bus_print_child_footer(dev, child);
1722 
1723 	return (retval);
1724 }
1725 
1726 static int
1727 hdac_child_location_str(device_t dev, device_t child, char *buf,
1728     size_t buflen)
1729 {
1730 
1731 	ksnprintf(buf, buflen, "cad=%d",
1732 	    (int)(intptr_t)device_get_ivars(child));
1733 	return (0);
1734 }
1735 
1736 static int
1737 hdac_child_pnpinfo_str_method(device_t dev, device_t child, char *buf,
1738     size_t buflen)
1739 {
1740 	struct hdac_softc *sc = device_get_softc(dev);
1741 	nid_t cad = (uintptr_t)device_get_ivars(child);
1742 
1743 	ksnprintf(buf, buflen, "vendor=0x%04x device=0x%04x revision=0x%02x "
1744 	    "stepping=0x%02x",
1745 	    sc->codecs[cad].vendor_id, sc->codecs[cad].device_id,
1746 	    sc->codecs[cad].revision_id, sc->codecs[cad].stepping_id);
1747 	return (0);
1748 }
1749 
1750 static int
1751 hdac_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1752 {
1753 	struct hdac_softc *sc = device_get_softc(dev);
1754 	nid_t cad = (uintptr_t)device_get_ivars(child);
1755 
1756 	switch (which) {
1757 	case HDA_IVAR_CODEC_ID:
1758 		*result = cad;
1759 		break;
1760 	case HDA_IVAR_VENDOR_ID:
1761 		*result = sc->codecs[cad].vendor_id;
1762 		break;
1763 	case HDA_IVAR_DEVICE_ID:
1764 		*result = sc->codecs[cad].device_id;
1765 		break;
1766 	case HDA_IVAR_REVISION_ID:
1767 		*result = sc->codecs[cad].revision_id;
1768 		break;
1769 	case HDA_IVAR_STEPPING_ID:
1770 		*result = sc->codecs[cad].stepping_id;
1771 		break;
1772 	case HDA_IVAR_SUBVENDOR_ID:
1773 		*result = pci_get_subvendor(dev);
1774 		break;
1775 	case HDA_IVAR_SUBDEVICE_ID:
1776 		*result = pci_get_subdevice(dev);
1777 		break;
1778 	case HDA_IVAR_DMA_NOCACHE:
1779 		*result = (sc->flags & HDAC_F_DMA_NOCACHE) != 0;
1780 		break;
1781 	default:
1782 		return (ENOENT);
1783 	}
1784 	return (0);
1785 }
1786 
1787 static struct lock *
1788 hdac_get_mtx(device_t dev, device_t child)
1789 {
1790 	struct hdac_softc *sc = device_get_softc(dev);
1791 
1792 	return (sc->lock);
1793 }
1794 
1795 static uint32_t
1796 hdac_codec_command(device_t dev, device_t child, uint32_t verb)
1797 {
1798 
1799 	return (hdac_send_command(device_get_softc(dev),
1800 	    (intptr_t)device_get_ivars(child), verb));
1801 }
1802 
1803 static int
1804 hdac_find_stream(struct hdac_softc *sc, int dir, int stream)
1805 {
1806 	int i, ss;
1807 
1808 	ss = -1;
1809 	/* Allocate ISS/BSS first. */
1810 	if (dir == 0) {
1811 		for (i = 0; i < sc->num_iss; i++) {
1812 			if (sc->streams[i].stream == stream) {
1813 				ss = i;
1814 				break;
1815 			}
1816 		}
1817 	} else {
1818 		for (i = 0; i < sc->num_oss; i++) {
1819 			if (sc->streams[i + sc->num_iss].stream == stream) {
1820 				ss = i + sc->num_iss;
1821 				break;
1822 			}
1823 		}
1824 	}
1825 	/* Fallback to BSS. */
1826 	if (ss == -1) {
1827 		for (i = 0; i < sc->num_bss; i++) {
1828 			if (sc->streams[i + sc->num_iss + sc->num_oss].stream
1829 			    == stream) {
1830 				ss = i + sc->num_iss + sc->num_oss;
1831 				break;
1832 			}
1833 		}
1834 	}
1835 	return (ss);
1836 }
1837 
1838 static int
1839 hdac_stream_alloc(device_t dev, device_t child, int dir, int format, int stripe,
1840     uint32_t **dmapos)
1841 {
1842 	struct hdac_softc *sc = device_get_softc(dev);
1843 	nid_t cad = (uintptr_t)device_get_ivars(child);
1844 	int stream, ss, bw, maxbw, prevbw;
1845 
1846 	/* Look for empty stream. */
1847 	ss = hdac_find_stream(sc, dir, 0);
1848 
1849 	/* Return if found nothing. */
1850 	if (ss < 0)
1851 		return (0);
1852 
1853 	/* Check bus bandwidth. */
1854 	bw = hdac_bdata_rate(format, dir);
1855 	if (dir == 1) {
1856 		bw *= 1 << (sc->num_sdo - stripe);
1857 		prevbw = sc->sdo_bw_used;
1858 		maxbw = 48000 * 960 * (1 << sc->num_sdo);
1859 	} else {
1860 		prevbw = sc->codecs[cad].sdi_bw_used;
1861 		maxbw = 48000 * 464;
1862 	}
1863 	HDA_BOOTHVERBOSE(
1864 		device_printf(dev, "%dKbps of %dKbps bandwidth used%s\n",
1865 		    (bw + prevbw) / 1000, maxbw / 1000,
1866 		    bw + prevbw > maxbw ? " -- OVERFLOW!" : "");
1867 	);
1868 	if (bw + prevbw > maxbw)
1869 		return (0);
1870 	if (dir == 1)
1871 		sc->sdo_bw_used += bw;
1872 	else
1873 		sc->codecs[cad].sdi_bw_used += bw;
1874 
1875 	/* Allocate stream number */
1876 	if (ss >= sc->num_iss + sc->num_oss)
1877 		stream = 15 - (ss - sc->num_iss + sc->num_oss);
1878 	else if (ss >= sc->num_iss)
1879 		stream = ss - sc->num_iss + 1;
1880 	else
1881 		stream = ss + 1;
1882 
1883 	sc->streams[ss].dev = child;
1884 	sc->streams[ss].dir = dir;
1885 	sc->streams[ss].stream = stream;
1886 	sc->streams[ss].bw = bw;
1887 	sc->streams[ss].format = format;
1888 	sc->streams[ss].stripe = stripe;
1889 	if (dmapos != NULL) {
1890 		if (sc->pos_dma.dma_vaddr != NULL)
1891 			*dmapos = (uint32_t *)(sc->pos_dma.dma_vaddr + ss * 8);
1892 		else
1893 			*dmapos = NULL;
1894 	}
1895 	return (stream);
1896 }
1897 
1898 static void
1899 hdac_stream_free(device_t dev, device_t child, int dir, int stream)
1900 {
1901 	struct hdac_softc *sc = device_get_softc(dev);
1902 	nid_t cad = (uintptr_t)device_get_ivars(child);
1903 	int ss;
1904 
1905 	ss = hdac_find_stream(sc, dir, stream);
1906 	KASSERT(ss >= 0,
1907 	    ("Free for not allocated stream (%d/%d)\n", dir, stream));
1908 	if (dir == 1)
1909 		sc->sdo_bw_used -= sc->streams[ss].bw;
1910 	else
1911 		sc->codecs[cad].sdi_bw_used -= sc->streams[ss].bw;
1912 	sc->streams[ss].stream = 0;
1913 	sc->streams[ss].dev = NULL;
1914 }
1915 
1916 static int
1917 hdac_stream_start(device_t dev, device_t child,
1918     int dir, int stream, bus_addr_t buf, int blksz, int blkcnt)
1919 {
1920 	struct hdac_softc *sc = device_get_softc(dev);
1921 	struct hdac_bdle *bdle;
1922 	uint64_t addr;
1923 	int i, ss, off;
1924 	uint32_t ctl;
1925 
1926 	ss = hdac_find_stream(sc, dir, stream);
1927 	KASSERT(ss >= 0,
1928 	    ("Start for not allocated stream (%d/%d)\n", dir, stream));
1929 
1930 	addr = (uint64_t)buf;
1931 	bdle = (struct hdac_bdle *)sc->streams[ss].bdl.dma_vaddr;
1932 	for (i = 0; i < blkcnt; i++, bdle++) {
1933 		bdle->addrl = (uint32_t)addr;
1934 		bdle->addrh = (uint32_t)(addr >> 32);
1935 		bdle->len = blksz;
1936 		bdle->ioc = 1;
1937 		addr += blksz;
1938 	}
1939 
1940 	off = ss << 5;
1941 	HDAC_WRITE_4(&sc->mem, off + HDAC_SDCBL, blksz * blkcnt);
1942 	HDAC_WRITE_2(&sc->mem, off + HDAC_SDLVI, blkcnt - 1);
1943 	addr = sc->streams[ss].bdl.dma_paddr;
1944 	HDAC_WRITE_4(&sc->mem, off + HDAC_SDBDPL, (uint32_t)addr);
1945 	HDAC_WRITE_4(&sc->mem, off + HDAC_SDBDPU, (uint32_t)(addr >> 32));
1946 
1947 	ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL2);
1948 	if (dir)
1949 		ctl |= HDAC_SDCTL2_DIR;
1950 	else
1951 		ctl &= ~HDAC_SDCTL2_DIR;
1952 	ctl &= ~HDAC_SDCTL2_STRM_MASK;
1953 	ctl |= stream << HDAC_SDCTL2_STRM_SHIFT;
1954 	ctl &= ~HDAC_SDCTL2_STRIPE_MASK;
1955 	ctl |= sc->streams[ss].stripe << HDAC_SDCTL2_STRIPE_SHIFT;
1956 	HDAC_WRITE_1(&sc->mem, off + HDAC_SDCTL2, ctl);
1957 
1958 	HDAC_WRITE_2(&sc->mem, off + HDAC_SDFMT, sc->streams[ss].format);
1959 
1960 	ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
1961 	ctl |= 1 << ss;
1962 	HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
1963 
1964 	HDAC_WRITE_1(&sc->mem, off + HDAC_SDSTS,
1965 	    HDAC_SDSTS_DESE | HDAC_SDSTS_FIFOE | HDAC_SDSTS_BCIS);
1966 	ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL0);
1967 	ctl |= HDAC_SDCTL_IOCE | HDAC_SDCTL_FEIE | HDAC_SDCTL_DEIE |
1968 	    HDAC_SDCTL_RUN;
1969 	HDAC_WRITE_1(&sc->mem, off + HDAC_SDCTL0, ctl);
1970 
1971 	sc->streams[ss].blksz = blksz;
1972 	sc->streams[ss].running = 1;
1973 	hdac_poll_reinit(sc);
1974 	return (0);
1975 }
1976 
1977 static void
1978 hdac_stream_stop(device_t dev, device_t child, int dir, int stream)
1979 {
1980 	struct hdac_softc *sc = device_get_softc(dev);
1981 	int ss, off;
1982 	uint32_t ctl;
1983 
1984 	ss = hdac_find_stream(sc, dir, stream);
1985 	KASSERT(ss >= 0,
1986 	    ("Stop for not allocated stream (%d/%d)\n", dir, stream));
1987 
1988 	off = ss << 5;
1989 	ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL0);
1990 	ctl &= ~(HDAC_SDCTL_IOCE | HDAC_SDCTL_FEIE | HDAC_SDCTL_DEIE |
1991 	    HDAC_SDCTL_RUN);
1992 	HDAC_WRITE_1(&sc->mem, off + HDAC_SDCTL0, ctl);
1993 
1994 	ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
1995 	ctl &= ~(1 << ss);
1996 	HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
1997 
1998 	sc->streams[ss].running = 0;
1999 	hdac_poll_reinit(sc);
2000 }
2001 
2002 static void
2003 hdac_stream_reset(device_t dev, device_t child, int dir, int stream)
2004 {
2005 	struct hdac_softc *sc = device_get_softc(dev);
2006 	int timeout = 1000;
2007 	int to = timeout;
2008 	int ss, off;
2009 	uint32_t ctl;
2010 
2011 	ss = hdac_find_stream(sc, dir, stream);
2012 	KASSERT(ss >= 0,
2013 	    ("Reset for not allocated stream (%d/%d)\n", dir, stream));
2014 
2015 	off = ss << 5;
2016 	ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL0);
2017 	ctl |= HDAC_SDCTL_SRST;
2018 	HDAC_WRITE_1(&sc->mem, off + HDAC_SDCTL0, ctl);
2019 	do {
2020 		ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL0);
2021 		if (ctl & HDAC_SDCTL_SRST)
2022 			break;
2023 		DELAY(10);
2024 	} while (--to);
2025 	if (!(ctl & HDAC_SDCTL_SRST))
2026 		device_printf(dev, "Reset setting timeout\n");
2027 	ctl &= ~HDAC_SDCTL_SRST;
2028 	HDAC_WRITE_1(&sc->mem, off + HDAC_SDCTL0, ctl);
2029 	to = timeout;
2030 	do {
2031 		ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL0);
2032 		if (!(ctl & HDAC_SDCTL_SRST))
2033 			break;
2034 		DELAY(10);
2035 	} while (--to);
2036 	if (ctl & HDAC_SDCTL_SRST)
2037 		device_printf(dev, "Reset timeout!\n");
2038 }
2039 
2040 static uint32_t
2041 hdac_stream_getptr(device_t dev, device_t child, int dir, int stream)
2042 {
2043 	struct hdac_softc *sc = device_get_softc(dev);
2044 	int ss, off;
2045 
2046 	ss = hdac_find_stream(sc, dir, stream);
2047 	KASSERT(ss >= 0,
2048 	    ("Reset for not allocated stream (%d/%d)\n", dir, stream));
2049 
2050 	off = ss << 5;
2051 	return (HDAC_READ_4(&sc->mem, off + HDAC_SDLPIB));
2052 }
2053 
2054 static int
2055 hdac_unsol_alloc(device_t dev, device_t child, int tag)
2056 {
2057 	struct hdac_softc *sc = device_get_softc(dev);
2058 
2059 	sc->unsol_registered++;
2060 	hdac_poll_reinit(sc);
2061 	return (tag);
2062 }
2063 
2064 static void
2065 hdac_unsol_free(device_t dev, device_t child, int tag)
2066 {
2067 	struct hdac_softc *sc = device_get_softc(dev);
2068 
2069 	sc->unsol_registered--;
2070 	hdac_poll_reinit(sc);
2071 }
2072 
2073 static device_method_t hdac_methods[] = {
2074 	/* device interface */
2075 	DEVMETHOD(device_probe,		hdac_probe),
2076 	DEVMETHOD(device_attach,	hdac_attach),
2077 	DEVMETHOD(device_detach,	hdac_detach),
2078 	DEVMETHOD(device_suspend,	hdac_suspend),
2079 	DEVMETHOD(device_resume,	hdac_resume),
2080 	/* Bus interface */
2081 	DEVMETHOD(bus_get_dma_tag,	hdac_get_dma_tag),
2082 	DEVMETHOD(bus_print_child,	hdac_print_child),
2083 	DEVMETHOD(bus_child_location_str, hdac_child_location_str),
2084 	DEVMETHOD(bus_child_pnpinfo_str, hdac_child_pnpinfo_str_method),
2085 	DEVMETHOD(bus_read_ivar,	hdac_read_ivar),
2086 	DEVMETHOD(hdac_get_mtx,		hdac_get_mtx),
2087 	DEVMETHOD(hdac_codec_command,	hdac_codec_command),
2088 	DEVMETHOD(hdac_stream_alloc,	hdac_stream_alloc),
2089 	DEVMETHOD(hdac_stream_free,	hdac_stream_free),
2090 	DEVMETHOD(hdac_stream_start,	hdac_stream_start),
2091 	DEVMETHOD(hdac_stream_stop,	hdac_stream_stop),
2092 	DEVMETHOD(hdac_stream_reset,	hdac_stream_reset),
2093 	DEVMETHOD(hdac_stream_getptr,	hdac_stream_getptr),
2094 	DEVMETHOD(hdac_unsol_alloc,	hdac_unsol_alloc),
2095 	DEVMETHOD(hdac_unsol_free,	hdac_unsol_free),
2096 	DEVMETHOD_END
2097 };
2098 
2099 static driver_t hdac_driver = {
2100 	"hdac",
2101 	hdac_methods,
2102 	sizeof(struct hdac_softc),
2103 };
2104 
2105 static devclass_t hdac_devclass;
2106 
2107 DRIVER_MODULE(snd_hda, pci, hdac_driver, hdac_devclass, NULL, NULL);
2108