1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // Copyright(c) 2021-2022 Intel Corporation 4 // 5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com> 6 // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com> 7 // 8 // Special thanks to: 9 // Krzysztof Hejmowski <krzysztof.hejmowski@intel.com> 10 // Michal Sienkiewicz <michal.sienkiewicz@intel.com> 11 // Filip Proborszcz 12 // 13 // for sharing Intel AudioDSP expertise and helping shape the very 14 // foundation of this driver 15 // 16 17 #include <linux/acpi.h> 18 #include <linux/module.h> 19 #include <linux/pci.h> 20 #include <acpi/nhlt.h> 21 #include <sound/hda_codec.h> 22 #include <sound/hda_i915.h> 23 #include <sound/hda_register.h> 24 #include <sound/hdaudio.h> 25 #include <sound/hdaudio_ext.h> 26 #include <sound/intel-dsp-config.h> 27 #include "../../codecs/hda.h" 28 #include "avs.h" 29 #include "cldma.h" 30 #include "messages.h" 31 32 static u32 pgctl_mask = AZX_PGCTL_LSRMD_MASK; 33 module_param(pgctl_mask, uint, 0444); 34 MODULE_PARM_DESC(pgctl_mask, "PCI PGCTL policy override"); 35 36 static u32 cgctl_mask = AZX_CGCTL_MISCBDCGE_MASK; 37 module_param(cgctl_mask, uint, 0444); 38 MODULE_PARM_DESC(cgctl_mask, "PCI CGCTL policy override"); 39 40 static void 41 avs_hda_update_config_dword(struct hdac_bus *bus, u32 reg, u32 mask, u32 value) 42 { 43 struct pci_dev *pci = to_pci_dev(bus->dev); 44 u32 data; 45 46 pci_read_config_dword(pci, reg, &data); 47 data &= ~mask; 48 data |= (value & mask); 49 pci_write_config_dword(pci, reg, data); 50 } 51 52 void avs_hda_power_gating_enable(struct avs_dev *adev, bool enable) 53 { 54 u32 value = enable ? 0 : pgctl_mask; 55 56 avs_hda_update_config_dword(&adev->base.core, AZX_PCIREG_PGCTL, pgctl_mask, value); 57 } 58 59 static void avs_hdac_clock_gating_enable(struct hdac_bus *bus, bool enable) 60 { 61 u32 value = enable ? cgctl_mask : 0; 62 63 avs_hda_update_config_dword(bus, AZX_PCIREG_CGCTL, cgctl_mask, value); 64 } 65 66 void avs_hda_clock_gating_enable(struct avs_dev *adev, bool enable) 67 { 68 avs_hdac_clock_gating_enable(&adev->base.core, enable); 69 } 70 71 void avs_hda_l1sen_enable(struct avs_dev *adev, bool enable) 72 { 73 if (enable) { 74 if (atomic_inc_and_test(&adev->l1sen_counter)) 75 snd_hdac_chip_updatel(&adev->base.core, VS_EM2, AZX_VS_EM2_L1SEN, 76 AZX_VS_EM2_L1SEN); 77 } else { 78 if (atomic_dec_return(&adev->l1sen_counter) == -1) 79 snd_hdac_chip_updatel(&adev->base.core, VS_EM2, AZX_VS_EM2_L1SEN, 0); 80 } 81 } 82 83 static int avs_hdac_bus_init_streams(struct hdac_bus *bus) 84 { 85 unsigned int cp_streams, pb_streams; 86 unsigned int gcap; 87 88 gcap = snd_hdac_chip_readw(bus, GCAP); 89 cp_streams = (gcap >> 8) & 0x0F; 90 pb_streams = (gcap >> 12) & 0x0F; 91 bus->num_streams = cp_streams + pb_streams; 92 93 snd_hdac_ext_stream_init_all(bus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE); 94 snd_hdac_ext_stream_init_all(bus, cp_streams, pb_streams, SNDRV_PCM_STREAM_PLAYBACK); 95 96 return snd_hdac_bus_alloc_stream_pages(bus); 97 } 98 99 static bool avs_hdac_bus_init_chip(struct hdac_bus *bus, bool full_reset) 100 { 101 struct hdac_ext_link *hlink; 102 bool ret; 103 104 avs_hdac_clock_gating_enable(bus, false); 105 ret = snd_hdac_bus_init_chip(bus, full_reset); 106 107 /* Reset stream-to-link mapping */ 108 list_for_each_entry(hlink, &bus->hlink_list, list) 109 writel(0, hlink->ml_addr + AZX_REG_ML_LOSIDV); 110 111 avs_hdac_clock_gating_enable(bus, true); 112 113 /* Set DUM bit to address incorrect position reporting for capture 114 * streams. In order to do so, CTRL needs to be out of reset state 115 */ 116 snd_hdac_chip_updatel(bus, VS_EM2, AZX_VS_EM2_DUM, AZX_VS_EM2_DUM); 117 118 return ret; 119 } 120 121 static int probe_codec(struct hdac_bus *bus, int addr) 122 { 123 struct hda_codec *codec; 124 unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) | 125 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID; 126 unsigned int res = -1; 127 int ret; 128 129 mutex_lock(&bus->cmd_mutex); 130 snd_hdac_bus_send_cmd(bus, cmd); 131 snd_hdac_bus_get_response(bus, addr, &res); 132 mutex_unlock(&bus->cmd_mutex); 133 if (res == -1) 134 return -EIO; 135 136 dev_dbg(bus->dev, "codec #%d probed OK: 0x%x\n", addr, res); 137 138 codec = snd_hda_codec_device_init(to_hda_bus(bus), addr, "hdaudioB%dD%d", bus->idx, addr); 139 if (IS_ERR(codec)) { 140 dev_err(bus->dev, "init codec failed: %ld\n", PTR_ERR(codec)); 141 return PTR_ERR(codec); 142 } 143 /* 144 * Allow avs_core suspend by forcing suspended state on all 145 * of its codec child devices. Component interested in 146 * dealing with hda codecs directly takes pm responsibilities 147 */ 148 pm_runtime_set_suspended(hda_codec_dev(codec)); 149 150 /* configure effectively creates new ASoC component */ 151 ret = snd_hda_codec_configure(codec); 152 if (ret < 0) { 153 dev_warn(bus->dev, "failed to config codec #%d: %d\n", addr, ret); 154 return ret; 155 } 156 157 return 0; 158 } 159 160 static void avs_hdac_bus_probe_codecs(struct hdac_bus *bus) 161 { 162 int ret, c; 163 164 /* First try to probe all given codec slots */ 165 for (c = 0; c < HDA_MAX_CODECS; c++) { 166 if (!(bus->codec_mask & BIT(c))) 167 continue; 168 169 ret = probe_codec(bus, c); 170 /* Ignore codecs with no supporting driver. */ 171 if (!ret || ret == -ENODEV) 172 continue; 173 174 /* 175 * Some BIOSen give you wrong codec addresses 176 * that don't exist 177 */ 178 dev_warn(bus->dev, "Codec #%d probe error; disabling it...\n", c); 179 bus->codec_mask &= ~BIT(c); 180 /* 181 * More badly, accessing to a non-existing 182 * codec often screws up the controller bus, 183 * and disturbs the further communications. 184 * Thus if an error occurs during probing, 185 * better to reset the controller bus to get 186 * back to the sanity state. 187 */ 188 snd_hdac_bus_stop_chip(bus); 189 avs_hdac_bus_init_chip(bus, true); 190 } 191 } 192 193 static void avs_hda_probe_work(struct work_struct *work) 194 { 195 struct avs_dev *adev = container_of(work, struct avs_dev, probe_work); 196 struct hdac_bus *bus = &adev->base.core; 197 struct hdac_ext_link *hlink; 198 int ret; 199 200 pm_runtime_set_active(bus->dev); /* clear runtime_error flag */ 201 202 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true); 203 avs_hdac_bus_init_chip(bus, true); 204 avs_hdac_bus_probe_codecs(bus); 205 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false); 206 207 /* with all codecs probed, links can be powered down */ 208 list_for_each_entry(hlink, &bus->hlink_list, list) 209 snd_hdac_ext_bus_link_put(bus, hlink); 210 211 snd_hdac_ext_bus_ppcap_enable(bus, true); 212 snd_hdac_ext_bus_ppcap_int_enable(bus, true); 213 avs_debugfs_init(adev); 214 215 ret = avs_dsp_first_boot_firmware(adev); 216 if (ret < 0) 217 return; 218 219 acpi_nhlt_get_gbl_table(); 220 221 avs_register_all_boards(adev); 222 223 /* configure PM */ 224 pm_runtime_set_autosuspend_delay(bus->dev, 2000); 225 pm_runtime_use_autosuspend(bus->dev); 226 pm_runtime_mark_last_busy(bus->dev); 227 pm_runtime_put_autosuspend(bus->dev); 228 pm_runtime_allow(bus->dev); 229 } 230 231 static void hdac_stream_update_pos(struct hdac_stream *stream, u64 buffer_size) 232 { 233 u64 prev_pos, pos, num_bytes; 234 235 div64_u64_rem(stream->curr_pos, buffer_size, &prev_pos); 236 pos = snd_hdac_stream_get_pos_posbuf(stream); 237 238 if (pos < prev_pos) 239 num_bytes = (buffer_size - prev_pos) + pos; 240 else 241 num_bytes = pos - prev_pos; 242 243 stream->curr_pos += num_bytes; 244 } 245 246 /* called from IRQ */ 247 static void hdac_update_stream(struct hdac_bus *bus, struct hdac_stream *stream) 248 { 249 if (stream->substream) { 250 snd_pcm_period_elapsed(stream->substream); 251 } else if (stream->cstream) { 252 u64 buffer_size = stream->cstream->runtime->buffer_size; 253 254 hdac_stream_update_pos(stream, buffer_size); 255 snd_compr_fragment_elapsed(stream->cstream); 256 } 257 } 258 259 static irqreturn_t avs_hda_interrupt(struct hdac_bus *bus) 260 { 261 irqreturn_t ret = IRQ_NONE; 262 u32 status; 263 264 status = snd_hdac_chip_readl(bus, INTSTS); 265 if (snd_hdac_bus_handle_stream_irq(bus, status, hdac_update_stream)) 266 ret = IRQ_HANDLED; 267 268 spin_lock_irq(&bus->reg_lock); 269 /* Clear RIRB interrupt. */ 270 status = snd_hdac_chip_readb(bus, RIRBSTS); 271 if (status & RIRB_INT_MASK) { 272 if (status & RIRB_INT_RESPONSE) 273 snd_hdac_bus_update_rirb(bus); 274 snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK); 275 ret = IRQ_HANDLED; 276 } 277 278 spin_unlock_irq(&bus->reg_lock); 279 return ret; 280 } 281 282 static irqreturn_t avs_hda_irq_handler(int irq, void *dev_id) 283 { 284 struct hdac_bus *bus = dev_id; 285 u32 intsts; 286 287 intsts = snd_hdac_chip_readl(bus, INTSTS); 288 if (intsts == UINT_MAX || !(intsts & AZX_INT_GLOBAL_EN)) 289 return IRQ_NONE; 290 291 /* Mask GIE, unmasked in irq_thread(). */ 292 snd_hdac_chip_updatel(bus, INTCTL, AZX_INT_GLOBAL_EN, 0); 293 294 return IRQ_WAKE_THREAD; 295 } 296 297 static irqreturn_t avs_hda_irq_thread(int irq, void *dev_id) 298 { 299 struct hdac_bus *bus = dev_id; 300 u32 status; 301 302 status = snd_hdac_chip_readl(bus, INTSTS); 303 if (status & ~AZX_INT_GLOBAL_EN) 304 avs_hda_interrupt(bus); 305 306 /* Unmask GIE, masked in irq_handler(). */ 307 snd_hdac_chip_updatel(bus, INTCTL, AZX_INT_GLOBAL_EN, AZX_INT_GLOBAL_EN); 308 309 return IRQ_HANDLED; 310 } 311 312 static irqreturn_t avs_dsp_irq_handler(int irq, void *dev_id) 313 { 314 struct avs_dev *adev = dev_id; 315 316 return avs_hda_irq_handler(irq, &adev->base.core); 317 } 318 319 static irqreturn_t avs_dsp_irq_thread(int irq, void *dev_id) 320 { 321 struct avs_dev *adev = dev_id; 322 struct hdac_bus *bus = &adev->base.core; 323 u32 status; 324 325 status = readl(bus->ppcap + AZX_REG_PP_PPSTS); 326 if (status & AZX_PPCTL_PIE) 327 avs_dsp_op(adev, dsp_interrupt); 328 329 /* Unmask GIE, masked in irq_handler(). */ 330 snd_hdac_chip_updatel(bus, INTCTL, AZX_INT_GLOBAL_EN, AZX_INT_GLOBAL_EN); 331 332 return IRQ_HANDLED; 333 } 334 335 static int avs_hdac_acquire_irq(struct avs_dev *adev) 336 { 337 struct hdac_bus *bus = &adev->base.core; 338 struct pci_dev *pci = to_pci_dev(bus->dev); 339 int ret; 340 341 /* request one and check that we only got one interrupt */ 342 ret = pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI | PCI_IRQ_INTX); 343 if (ret != 1) { 344 dev_err(adev->dev, "Failed to allocate IRQ vector: %d\n", ret); 345 return ret; 346 } 347 348 ret = pci_request_irq(pci, 0, avs_hda_irq_handler, avs_hda_irq_thread, bus, 349 KBUILD_MODNAME); 350 if (ret < 0) { 351 dev_err(adev->dev, "Failed to request stream IRQ handler: %d\n", ret); 352 goto free_vector; 353 } 354 355 ret = pci_request_irq(pci, 0, avs_dsp_irq_handler, avs_dsp_irq_thread, adev, 356 KBUILD_MODNAME); 357 if (ret < 0) { 358 dev_err(adev->dev, "Failed to request IPC IRQ handler: %d\n", ret); 359 goto free_stream_irq; 360 } 361 362 return 0; 363 364 free_stream_irq: 365 pci_free_irq(pci, 0, bus); 366 free_vector: 367 pci_free_irq_vectors(pci); 368 return ret; 369 } 370 371 static int avs_bus_init(struct avs_dev *adev, struct pci_dev *pci, const struct pci_device_id *id) 372 { 373 struct hda_bus *bus = &adev->base; 374 struct avs_ipc *ipc; 375 struct device *dev = &pci->dev; 376 int ret; 377 378 ret = snd_hdac_ext_bus_init(&bus->core, dev, NULL, &soc_hda_ext_bus_ops); 379 if (ret < 0) 380 return ret; 381 382 bus->core.use_posbuf = 1; 383 bus->core.bdl_pos_adj = 0; 384 bus->core.sync_write = 1; 385 bus->pci = pci; 386 bus->mixer_assigned = -1; 387 mutex_init(&bus->prepare_mutex); 388 389 ipc = devm_kzalloc(dev, sizeof(*ipc), GFP_KERNEL); 390 if (!ipc) 391 return -ENOMEM; 392 ret = avs_ipc_init(ipc, dev); 393 if (ret < 0) 394 return ret; 395 396 adev->modcfg_buf = devm_kzalloc(dev, AVS_MAILBOX_SIZE, GFP_KERNEL); 397 if (!adev->modcfg_buf) 398 return -ENOMEM; 399 400 adev->dev = dev; 401 adev->spec = (const struct avs_spec *)id->driver_data; 402 adev->ipc = ipc; 403 adev->hw_cfg.dsp_cores = hweight_long(AVS_MAIN_CORE_MASK); 404 INIT_WORK(&adev->probe_work, avs_hda_probe_work); 405 INIT_LIST_HEAD(&adev->comp_list); 406 INIT_LIST_HEAD(&adev->path_list); 407 INIT_LIST_HEAD(&adev->fw_list); 408 init_completion(&adev->fw_ready); 409 spin_lock_init(&adev->path_list_lock); 410 mutex_init(&adev->modres_mutex); 411 mutex_init(&adev->comp_list_mutex); 412 mutex_init(&adev->path_mutex); 413 414 return 0; 415 } 416 417 static int avs_pci_probe(struct pci_dev *pci, const struct pci_device_id *id) 418 { 419 struct hdac_bus *bus; 420 struct avs_dev *adev; 421 struct device *dev = &pci->dev; 422 int ret; 423 424 ret = snd_intel_dsp_driver_probe(pci); 425 switch (ret) { 426 case SND_INTEL_DSP_DRIVER_ANY: 427 case SND_INTEL_DSP_DRIVER_SST: 428 case SND_INTEL_DSP_DRIVER_AVS: 429 break; 430 default: 431 return -ENODEV; 432 } 433 434 ret = pcim_enable_device(pci); 435 if (ret < 0) 436 return ret; 437 438 adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL); 439 if (!adev) 440 return -ENOMEM; 441 ret = avs_bus_init(adev, pci, id); 442 if (ret < 0) { 443 dev_err(dev, "failed to init avs bus: %d\n", ret); 444 return ret; 445 } 446 447 ret = pci_request_regions(pci, "AVS HDAudio"); 448 if (ret < 0) 449 return ret; 450 451 bus = &adev->base.core; 452 bus->addr = pci_resource_start(pci, 0); 453 bus->remap_addr = pci_ioremap_bar(pci, 0); 454 if (!bus->remap_addr) { 455 dev_err(bus->dev, "ioremap error\n"); 456 ret = -ENXIO; 457 goto err_remap_bar0; 458 } 459 460 adev->dsp_ba = pci_ioremap_bar(pci, 4); 461 if (!adev->dsp_ba) { 462 dev_err(bus->dev, "ioremap error\n"); 463 ret = -ENXIO; 464 goto err_remap_bar4; 465 } 466 467 snd_hdac_bus_parse_capabilities(bus); 468 if (bus->mlcap) 469 snd_hdac_ext_bus_get_ml_capabilities(bus); 470 471 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) 472 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); 473 dma_set_max_seg_size(dev, UINT_MAX); 474 475 ret = avs_hdac_bus_init_streams(bus); 476 if (ret < 0) { 477 dev_err(dev, "failed to init streams: %d\n", ret); 478 goto err_init_streams; 479 } 480 481 ret = avs_hdac_acquire_irq(adev); 482 if (ret < 0) { 483 dev_err(bus->dev, "failed to acquire irq: %d\n", ret); 484 goto err_acquire_irq; 485 } 486 487 pci_set_master(pci); 488 pci_set_drvdata(pci, bus); 489 device_disable_async_suspend(dev); 490 491 ret = snd_hdac_i915_init(bus); 492 if (ret == -EPROBE_DEFER) 493 goto err_i915_init; 494 else if (ret < 0) 495 dev_info(bus->dev, "i915 init unsuccessful: %d\n", ret); 496 497 schedule_work(&adev->probe_work); 498 499 return 0; 500 501 err_i915_init: 502 pci_free_irq(pci, 0, adev); 503 pci_free_irq(pci, 0, bus); 504 pci_free_irq_vectors(pci); 505 pci_clear_master(pci); 506 pci_set_drvdata(pci, NULL); 507 err_acquire_irq: 508 snd_hdac_bus_free_stream_pages(bus); 509 snd_hdac_ext_stream_free_all(bus); 510 err_init_streams: 511 iounmap(adev->dsp_ba); 512 err_remap_bar4: 513 iounmap(bus->remap_addr); 514 err_remap_bar0: 515 pci_release_regions(pci); 516 return ret; 517 } 518 519 static void avs_pci_shutdown(struct pci_dev *pci) 520 { 521 struct hdac_bus *bus = pci_get_drvdata(pci); 522 struct avs_dev *adev = hdac_to_avs(bus); 523 524 cancel_work_sync(&adev->probe_work); 525 avs_ipc_block(adev->ipc); 526 527 snd_hdac_stop_streams(bus); 528 avs_dsp_op(adev, int_control, false); 529 snd_hdac_ext_bus_ppcap_int_enable(bus, false); 530 snd_hdac_ext_bus_link_power_down_all(bus); 531 532 snd_hdac_bus_stop_chip(bus); 533 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false); 534 535 pci_free_irq(pci, 0, adev); 536 pci_free_irq(pci, 0, bus); 537 pci_free_irq_vectors(pci); 538 } 539 540 static void avs_pci_remove(struct pci_dev *pci) 541 { 542 struct hdac_device *hdev, *save; 543 struct hdac_bus *bus = pci_get_drvdata(pci); 544 struct avs_dev *adev = hdac_to_avs(bus); 545 546 cancel_work_sync(&adev->probe_work); 547 avs_ipc_block(adev->ipc); 548 549 avs_unregister_all_boards(adev); 550 551 acpi_nhlt_put_gbl_table(); 552 avs_debugfs_exit(adev); 553 554 if (avs_platattr_test(adev, CLDMA)) 555 hda_cldma_free(&code_loader); 556 557 snd_hdac_stop_streams_and_chip(bus); 558 avs_dsp_op(adev, int_control, false); 559 snd_hdac_ext_bus_ppcap_int_enable(bus, false); 560 561 /* it is safe to remove all codecs from the system now */ 562 list_for_each_entry_safe(hdev, save, &bus->codec_list, list) 563 snd_hda_codec_unregister(hdac_to_hda_codec(hdev)); 564 565 snd_hdac_bus_free_stream_pages(bus); 566 snd_hdac_ext_stream_free_all(bus); 567 /* reverse ml_capabilities */ 568 snd_hdac_ext_link_free_all(bus); 569 snd_hdac_ext_bus_exit(bus); 570 571 avs_dsp_core_disable(adev, GENMASK(adev->hw_cfg.dsp_cores - 1, 0)); 572 snd_hdac_ext_bus_ppcap_enable(bus, false); 573 574 /* snd_hdac_stop_streams_and_chip does that already? */ 575 snd_hdac_bus_stop_chip(bus); 576 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false); 577 if (bus->audio_component) 578 snd_hdac_i915_exit(bus); 579 580 avs_module_info_free(adev); 581 pci_free_irq(pci, 0, adev); 582 pci_free_irq(pci, 0, bus); 583 pci_free_irq_vectors(pci); 584 iounmap(bus->remap_addr); 585 iounmap(adev->dsp_ba); 586 pci_release_regions(pci); 587 588 /* Firmware is not needed anymore */ 589 avs_release_firmwares(adev); 590 591 /* pm_runtime_forbid() can rpm_resume() which we do not want */ 592 pm_runtime_disable(&pci->dev); 593 pm_runtime_forbid(&pci->dev); 594 pm_runtime_enable(&pci->dev); 595 pm_runtime_get_noresume(&pci->dev); 596 } 597 598 static int avs_suspend_standby(struct avs_dev *adev) 599 { 600 struct hdac_bus *bus = &adev->base.core; 601 struct pci_dev *pci = adev->base.pci; 602 603 if (bus->cmd_dma_state) 604 snd_hdac_bus_stop_cmd_io(bus); 605 606 snd_hdac_ext_bus_link_power_down_all(bus); 607 608 enable_irq_wake(pci->irq); 609 pci_save_state(pci); 610 611 return 0; 612 } 613 614 static int __maybe_unused avs_suspend_common(struct avs_dev *adev, bool low_power) 615 { 616 struct hdac_bus *bus = &adev->base.core; 617 int ret; 618 619 flush_work(&adev->probe_work); 620 if (low_power && adev->num_lp_paths) 621 return avs_suspend_standby(adev); 622 623 snd_hdac_ext_bus_link_power_down_all(bus); 624 625 ret = avs_ipc_set_dx(adev, AVS_MAIN_CORE_MASK, false); 626 /* 627 * pm_runtime is blocked on DSP failure but system-wide suspend is not. 628 * Do not block entire system from suspending if that's the case. 629 */ 630 if (ret && ret != -EPERM) { 631 dev_err(adev->dev, "set dx failed: %d\n", ret); 632 return AVS_IPC_RET(ret); 633 } 634 635 avs_ipc_block(adev->ipc); 636 avs_dsp_op(adev, int_control, false); 637 snd_hdac_ext_bus_ppcap_int_enable(bus, false); 638 639 ret = avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK); 640 if (ret < 0) { 641 dev_err(adev->dev, "core_mask %ld disable failed: %d\n", AVS_MAIN_CORE_MASK, ret); 642 return ret; 643 } 644 645 snd_hdac_ext_bus_ppcap_enable(bus, false); 646 /* disable LP SRAM retention */ 647 avs_hda_power_gating_enable(adev, false); 648 snd_hdac_bus_stop_chip(bus); 649 /* disable CG when putting controller to reset */ 650 avs_hdac_clock_gating_enable(bus, false); 651 snd_hdac_bus_enter_link_reset(bus); 652 avs_hdac_clock_gating_enable(bus, true); 653 654 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false); 655 656 return 0; 657 } 658 659 static int avs_resume_standby(struct avs_dev *adev) 660 { 661 struct hdac_bus *bus = &adev->base.core; 662 struct pci_dev *pci = adev->base.pci; 663 664 pci_restore_state(pci); 665 disable_irq_wake(pci->irq); 666 667 snd_hdac_ext_bus_link_power_up_all(bus); 668 669 if (bus->cmd_dma_state) 670 snd_hdac_bus_init_cmd_io(bus); 671 672 return 0; 673 } 674 675 static int __maybe_unused avs_resume_common(struct avs_dev *adev, bool low_power, bool purge) 676 { 677 struct hdac_bus *bus = &adev->base.core; 678 int ret; 679 680 if (low_power && adev->num_lp_paths) 681 return avs_resume_standby(adev); 682 683 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true); 684 avs_hdac_bus_init_chip(bus, true); 685 686 snd_hdac_ext_bus_ppcap_enable(bus, true); 687 snd_hdac_ext_bus_ppcap_int_enable(bus, true); 688 689 ret = avs_dsp_boot_firmware(adev, purge); 690 if (ret < 0) { 691 dev_err(adev->dev, "firmware boot failed: %d\n", ret); 692 return ret; 693 } 694 695 return 0; 696 } 697 698 static int __maybe_unused avs_suspend(struct device *dev) 699 { 700 return avs_suspend_common(to_avs_dev(dev), true); 701 } 702 703 static int __maybe_unused avs_resume(struct device *dev) 704 { 705 return avs_resume_common(to_avs_dev(dev), true, true); 706 } 707 708 static int __maybe_unused avs_runtime_suspend(struct device *dev) 709 { 710 return avs_suspend_common(to_avs_dev(dev), true); 711 } 712 713 static int __maybe_unused avs_runtime_resume(struct device *dev) 714 { 715 return avs_resume_common(to_avs_dev(dev), true, false); 716 } 717 718 static int __maybe_unused avs_freeze(struct device *dev) 719 { 720 return avs_suspend_common(to_avs_dev(dev), false); 721 } 722 static int __maybe_unused avs_thaw(struct device *dev) 723 { 724 return avs_resume_common(to_avs_dev(dev), false, true); 725 } 726 727 static int __maybe_unused avs_poweroff(struct device *dev) 728 { 729 return avs_suspend_common(to_avs_dev(dev), false); 730 } 731 732 static int __maybe_unused avs_restore(struct device *dev) 733 { 734 return avs_resume_common(to_avs_dev(dev), false, true); 735 } 736 737 static const struct dev_pm_ops avs_dev_pm = { 738 .suspend = avs_suspend, 739 .resume = avs_resume, 740 .freeze = avs_freeze, 741 .thaw = avs_thaw, 742 .poweroff = avs_poweroff, 743 .restore = avs_restore, 744 SET_RUNTIME_PM_OPS(avs_runtime_suspend, avs_runtime_resume, NULL) 745 }; 746 747 static const struct avs_sram_spec skl_sram_spec = { 748 .base_offset = SKL_ADSP_SRAM_BASE_OFFSET, 749 .window_size = SKL_ADSP_SRAM_WINDOW_SIZE, 750 .rom_status_offset = SKL_ADSP_SRAM_BASE_OFFSET, 751 }; 752 753 static const struct avs_sram_spec apl_sram_spec = { 754 .base_offset = APL_ADSP_SRAM_BASE_OFFSET, 755 .window_size = APL_ADSP_SRAM_WINDOW_SIZE, 756 .rom_status_offset = APL_ADSP_SRAM_BASE_OFFSET, 757 }; 758 759 static const struct avs_hipc_spec skl_hipc_spec = { 760 .req_offset = SKL_ADSP_REG_HIPCI, 761 .req_ext_offset = SKL_ADSP_REG_HIPCIE, 762 .req_busy_mask = SKL_ADSP_HIPCI_BUSY, 763 .ack_offset = SKL_ADSP_REG_HIPCIE, 764 .ack_done_mask = SKL_ADSP_HIPCIE_DONE, 765 .rsp_offset = SKL_ADSP_REG_HIPCT, 766 .rsp_busy_mask = SKL_ADSP_HIPCT_BUSY, 767 .ctl_offset = SKL_ADSP_REG_HIPCCTL, 768 }; 769 770 static const struct avs_hipc_spec cnl_hipc_spec = { 771 .req_offset = CNL_ADSP_REG_HIPCIDR, 772 .req_ext_offset = CNL_ADSP_REG_HIPCIDD, 773 .req_busy_mask = CNL_ADSP_HIPCIDR_BUSY, 774 .ack_offset = CNL_ADSP_REG_HIPCIDA, 775 .ack_done_mask = CNL_ADSP_HIPCIDA_DONE, 776 .rsp_offset = CNL_ADSP_REG_HIPCTDR, 777 .rsp_busy_mask = CNL_ADSP_HIPCTDR_BUSY, 778 .ctl_offset = CNL_ADSP_REG_HIPCCTL, 779 }; 780 781 static const struct avs_spec skl_desc = { 782 .name = "skl", 783 .min_fw_version = { 9, 21, 0, 4732 }, 784 .dsp_ops = &avs_skl_dsp_ops, 785 .core_init_mask = 1, 786 .attributes = AVS_PLATATTR_CLDMA, 787 .sram = &skl_sram_spec, 788 .hipc = &skl_hipc_spec, 789 }; 790 791 static const struct avs_spec apl_desc = { 792 .name = "apl", 793 .min_fw_version = { 9, 22, 1, 4323 }, 794 .dsp_ops = &avs_apl_dsp_ops, 795 .core_init_mask = 3, 796 .attributes = AVS_PLATATTR_IMR, 797 .sram = &apl_sram_spec, 798 .hipc = &skl_hipc_spec, 799 }; 800 801 static const struct avs_spec cnl_desc = { 802 .name = "cnl", 803 .min_fw_version = { 10, 23, 0, 5314 }, 804 .dsp_ops = &avs_cnl_dsp_ops, 805 .core_init_mask = 1, 806 .attributes = AVS_PLATATTR_IMR, 807 .sram = &apl_sram_spec, 808 .hipc = &cnl_hipc_spec, 809 }; 810 811 static const struct avs_spec icl_desc = { 812 .name = "icl", 813 .min_fw_version = { 10, 23, 0, 5040 }, 814 .dsp_ops = &avs_icl_dsp_ops, 815 .core_init_mask = 1, 816 .attributes = AVS_PLATATTR_IMR, 817 .sram = &apl_sram_spec, 818 .hipc = &cnl_hipc_spec, 819 }; 820 821 static const struct avs_spec jsl_desc = { 822 .name = "jsl", 823 .min_fw_version = { 10, 26, 0, 5872 }, 824 .dsp_ops = &avs_icl_dsp_ops, 825 .core_init_mask = 1, 826 .attributes = AVS_PLATATTR_IMR, 827 .sram = &apl_sram_spec, 828 .hipc = &cnl_hipc_spec, 829 }; 830 831 #define AVS_TGL_BASED_SPEC(sname) \ 832 static const struct avs_spec sname##_desc = { \ 833 .name = #sname, \ 834 .min_fw_version = { 10, 29, 0, 5646 }, \ 835 .dsp_ops = &avs_tgl_dsp_ops, \ 836 .core_init_mask = 1, \ 837 .attributes = AVS_PLATATTR_IMR, \ 838 .sram = &apl_sram_spec, \ 839 .hipc = &cnl_hipc_spec, \ 840 } 841 842 AVS_TGL_BASED_SPEC(lkf); 843 AVS_TGL_BASED_SPEC(tgl); 844 AVS_TGL_BASED_SPEC(ehl); 845 AVS_TGL_BASED_SPEC(adl); 846 AVS_TGL_BASED_SPEC(adl_n); 847 848 static const struct pci_device_id avs_ids[] = { 849 { PCI_DEVICE_DATA(INTEL, HDA_SKL_LP, &skl_desc) }, 850 { PCI_DEVICE_DATA(INTEL, HDA_SKL, &skl_desc) }, 851 { PCI_DEVICE_DATA(INTEL, HDA_KBL_LP, &skl_desc) }, 852 { PCI_DEVICE_DATA(INTEL, HDA_KBL, &skl_desc) }, 853 { PCI_DEVICE_DATA(INTEL, HDA_KBL_H, &skl_desc) }, 854 { PCI_DEVICE_DATA(INTEL, HDA_CML_S, &skl_desc) }, 855 { PCI_DEVICE_DATA(INTEL, HDA_APL, &apl_desc) }, 856 { PCI_DEVICE_DATA(INTEL, HDA_GML, &apl_desc) }, 857 { PCI_DEVICE_DATA(INTEL, HDA_CNL_LP, &cnl_desc) }, 858 { PCI_DEVICE_DATA(INTEL, HDA_CNL_H, &cnl_desc) }, 859 { PCI_DEVICE_DATA(INTEL, HDA_CML_LP, &cnl_desc) }, 860 { PCI_DEVICE_DATA(INTEL, HDA_CML_H, &cnl_desc) }, 861 { PCI_DEVICE_DATA(INTEL, HDA_RKL_S, &cnl_desc) }, 862 { PCI_DEVICE_DATA(INTEL, HDA_ICL_LP, &icl_desc) }, 863 { PCI_DEVICE_DATA(INTEL, HDA_ICL_N, &icl_desc) }, 864 { PCI_DEVICE_DATA(INTEL, HDA_ICL_H, &icl_desc) }, 865 { PCI_DEVICE_DATA(INTEL, HDA_JSL_N, &jsl_desc) }, 866 { PCI_DEVICE_DATA(INTEL, HDA_LKF, &lkf_desc) }, 867 { PCI_DEVICE_DATA(INTEL, HDA_TGL_LP, &tgl_desc) }, 868 { PCI_DEVICE_DATA(INTEL, HDA_TGL_H, &tgl_desc) }, 869 { PCI_DEVICE_DATA(INTEL, HDA_CML_R, &tgl_desc) }, 870 { PCI_DEVICE_DATA(INTEL, HDA_EHL_0, &ehl_desc) }, 871 { PCI_DEVICE_DATA(INTEL, HDA_EHL_3, &ehl_desc) }, 872 { PCI_DEVICE_DATA(INTEL, HDA_ADL_S, &adl_desc) }, 873 { PCI_DEVICE_DATA(INTEL, HDA_ADL_P, &adl_desc) }, 874 { PCI_DEVICE_DATA(INTEL, HDA_ADL_PS, &adl_desc) }, 875 { PCI_DEVICE_DATA(INTEL, HDA_ADL_M, &adl_desc) }, 876 { PCI_DEVICE_DATA(INTEL, HDA_ADL_PX, &adl_desc) }, 877 { PCI_DEVICE_DATA(INTEL, HDA_ADL_N, &adl_n_desc) }, 878 { PCI_DEVICE_DATA(INTEL, HDA_RPL_S, &adl_desc) }, 879 { PCI_DEVICE_DATA(INTEL, HDA_RPL_P_0, &adl_desc) }, 880 { PCI_DEVICE_DATA(INTEL, HDA_RPL_P_1, &adl_desc) }, 881 { PCI_DEVICE_DATA(INTEL, HDA_RPL_M, &adl_desc) }, 882 { PCI_DEVICE_DATA(INTEL, HDA_RPL_PX, &adl_desc) }, 883 { 0 } 884 }; 885 MODULE_DEVICE_TABLE(pci, avs_ids); 886 887 static struct pci_driver avs_pci_driver = { 888 .name = KBUILD_MODNAME, 889 .id_table = avs_ids, 890 .probe = avs_pci_probe, 891 .remove = avs_pci_remove, 892 .shutdown = avs_pci_shutdown, 893 .dev_groups = avs_attr_groups, 894 .driver = { 895 .pm = &avs_dev_pm, 896 }, 897 }; 898 module_pci_driver(avs_pci_driver); 899 900 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>"); 901 MODULE_AUTHOR("Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>"); 902 MODULE_DESCRIPTION("Intel cAVS sound driver"); 903 MODULE_LICENSE("GPL"); 904