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