1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
3 // This file is provided under a dual BSD/GPLv2 license. When using or
4 // redistributing this file, you may do so under either license.
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
8 // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 // Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
10 // Rander Wang <rander.wang@intel.com>
11 // Keyon Jie <yang.jie@linux.intel.com>
15 * Hardware interface for generic Intel audio DSP HDA IP
18 #include <sound/hdaudio_ext.h>
19 #include <sound/hda_register.h>
21 #include <linux/module.h>
22 #include <sound/sof.h>
23 #include <sound/sof/xtensa.h>
26 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
27 #include "../../codecs/hdac_hda.h"
30 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
31 #include <sound/soc-acpi-intel-match.h>
34 /* platform specific devices */
37 #define IS_CFL(pci) ((pci)->vendor == 0x8086 && (pci)->device == 0xa348)
38 #define IS_CNL(pci) ((pci)->vendor == 0x8086 && (pci)->device == 0x9dc8)
44 struct hda_dsp_msg_code {
49 static const struct hda_dsp_msg_code hda_dsp_rom_msg[] = {
50 {HDA_DSP_ROM_FW_MANIFEST_LOADED, "status: manifest loaded"},
51 {HDA_DSP_ROM_FW_FW_LOADED, "status: fw loaded"},
52 {HDA_DSP_ROM_FW_ENTERED, "status: fw entered"},
53 {HDA_DSP_ROM_CSE_ERROR, "error: cse error"},
54 {HDA_DSP_ROM_CSE_WRONG_RESPONSE, "error: cse wrong response"},
55 {HDA_DSP_ROM_IMR_TO_SMALL, "error: IMR too small"},
56 {HDA_DSP_ROM_BASE_FW_NOT_FOUND, "error: base fw not found"},
57 {HDA_DSP_ROM_CSE_VALIDATION_FAILED, "error: signature verification failed"},
58 {HDA_DSP_ROM_IPC_FATAL_ERROR, "error: ipc fatal error"},
59 {HDA_DSP_ROM_L2_CACHE_ERROR, "error: L2 cache error"},
60 {HDA_DSP_ROM_LOAD_OFFSET_TO_SMALL, "error: load offset too small"},
61 {HDA_DSP_ROM_API_PTR_INVALID, "error: API ptr invalid"},
62 {HDA_DSP_ROM_BASEFW_INCOMPAT, "error: base fw incompatible"},
63 {HDA_DSP_ROM_UNHANDLED_INTERRUPT, "error: unhandled interrupt"},
64 {HDA_DSP_ROM_MEMORY_HOLE_ECC, "error: ECC memory hole"},
65 {HDA_DSP_ROM_KERNEL_EXCEPTION, "error: kernel exception"},
66 {HDA_DSP_ROM_USER_EXCEPTION, "error: user exception"},
67 {HDA_DSP_ROM_UNEXPECTED_RESET, "error: unexpected reset"},
68 {HDA_DSP_ROM_NULL_FW_ENTRY, "error: null FW entry point"},
71 static void hda_dsp_get_status_skl(struct snd_sof_dev *sdev)
76 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
77 HDA_ADSP_FW_STATUS_SKL);
79 for (i = 0; i < ARRAY_SIZE(hda_dsp_rom_msg); i++) {
80 if (status == hda_dsp_rom_msg[i].code) {
81 dev_err(sdev->dev, "%s - code %8.8x\n",
82 hda_dsp_rom_msg[i].msg, status);
87 /* not for us, must be generic sof message */
88 dev_dbg(sdev->dev, "unknown ROM status value %8.8x\n", status);
91 static void hda_dsp_get_status(struct snd_sof_dev *sdev)
96 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
97 HDA_DSP_SRAM_REG_ROM_STATUS);
99 for (i = 0; i < ARRAY_SIZE(hda_dsp_rom_msg); i++) {
100 if (status == hda_dsp_rom_msg[i].code) {
101 dev_err(sdev->dev, "%s - code %8.8x\n",
102 hda_dsp_rom_msg[i].msg, status);
107 /* not for us, must be generic sof message */
108 dev_dbg(sdev->dev, "unknown ROM status value %8.8x\n", status);
111 static void hda_dsp_get_registers(struct snd_sof_dev *sdev,
112 struct sof_ipc_dsp_oops_xtensa *xoops,
113 struct sof_ipc_panic_info *panic_info,
114 u32 *stack, size_t stack_words)
116 u32 offset = sdev->dsp_oops_offset;
118 /* first read registers */
119 sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
121 /* note: variable AR register array is not read */
123 /* then get panic info */
124 offset += xoops->arch_hdr.totalsize;
125 sof_block_read(sdev, sdev->mmio_bar, offset,
126 panic_info, sizeof(*panic_info));
128 /* then get the stack */
129 offset += sizeof(*panic_info);
130 sof_block_read(sdev, sdev->mmio_bar, offset, stack,
131 stack_words * sizeof(u32));
134 void hda_dsp_dump_skl(struct snd_sof_dev *sdev, u32 flags)
136 struct sof_ipc_dsp_oops_xtensa xoops;
137 struct sof_ipc_panic_info panic_info;
138 u32 stack[HDA_DSP_STACK_DUMP_SIZE];
141 /* try APL specific status message types first */
142 hda_dsp_get_status_skl(sdev);
144 /* now try generic SOF status messages */
145 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
146 HDA_ADSP_ERROR_CODE_SKL);
148 /*TODO: Check: there is no define in spec, but it is used in the code*/
149 panic = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
150 HDA_ADSP_ERROR_CODE_SKL + 0x4);
152 if (sdev->boot_complete) {
153 hda_dsp_get_registers(sdev, &xoops, &panic_info, stack,
154 HDA_DSP_STACK_DUMP_SIZE);
155 snd_sof_get_status(sdev, status, panic, &xoops, &panic_info,
156 stack, HDA_DSP_STACK_DUMP_SIZE);
158 dev_err(sdev->dev, "error: status = 0x%8.8x panic = 0x%8.8x\n",
160 hda_dsp_get_status_skl(sdev);
164 void hda_dsp_dump(struct snd_sof_dev *sdev, u32 flags)
166 struct sof_ipc_dsp_oops_xtensa xoops;
167 struct sof_ipc_panic_info panic_info;
168 u32 stack[HDA_DSP_STACK_DUMP_SIZE];
171 /* try APL specific status message types first */
172 hda_dsp_get_status(sdev);
174 /* now try generic SOF status messages */
175 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
176 HDA_DSP_SRAM_REG_FW_STATUS);
177 panic = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_TRACEP);
179 if (sdev->boot_complete) {
180 hda_dsp_get_registers(sdev, &xoops, &panic_info, stack,
181 HDA_DSP_STACK_DUMP_SIZE);
182 snd_sof_get_status(sdev, status, panic, &xoops, &panic_info,
183 stack, HDA_DSP_STACK_DUMP_SIZE);
185 dev_err(sdev->dev, "error: status = 0x%8.8x panic = 0x%8.8x\n",
187 hda_dsp_get_status(sdev);
191 void hda_ipc_irq_dump(struct snd_sof_dev *sdev)
193 struct hdac_bus *bus = sof_to_bus(sdev);
200 /* read key IRQ stats and config registers */
201 adspis = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS);
202 intsts = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS);
203 intctl = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL);
204 ppsts = snd_sof_dsp_read(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPSTS);
205 rirbsts = snd_hdac_chip_readb(bus, RIRBSTS);
208 "error: hda irq intsts 0x%8.8x intlctl 0x%8.8x rirb %2.2x\n",
209 intsts, intctl, rirbsts);
211 "error: dsp irq ppsts 0x%8.8x adspis 0x%8.8x\n",
215 void hda_ipc_dump(struct snd_sof_dev *sdev)
221 hda_ipc_irq_dump(sdev);
223 /* read IPC status */
224 hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE);
225 hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
226 hipcctl = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL);
228 /* dump the IPC regs */
229 /* TODO: parse the raw msg */
231 "error: host status 0x%8.8x dsp status 0x%8.8x mask 0x%8.8x\n",
232 hipcie, hipct, hipcctl);
235 static int hda_init(struct snd_sof_dev *sdev)
237 struct hda_bus *hbus;
238 struct hdac_bus *bus;
239 struct hdac_ext_bus_ops *ext_ops = NULL;
240 struct pci_dev *pci = to_pci_dev(sdev->dev);
243 hbus = sof_to_hbus(sdev);
244 bus = sof_to_bus(sdev);
247 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
248 ext_ops = snd_soc_hdac_hda_get_ops();
250 sof_hda_bus_init(bus, &pci->dev, ext_ops);
252 /* Workaround for a communication error on CFL (bko#199007) and CNL */
253 if (IS_CFL(pci) || IS_CNL(pci))
254 bus->polling_mode = 1;
257 bus->bdl_pos_adj = 0;
259 mutex_init(&hbus->prepare_mutex);
261 hbus->mixer_assigned = -1;
262 hbus->modelname = "sofbus";
264 /* initialise hdac bus */
265 bus->addr = pci_resource_start(pci, 0);
266 #if IS_ENABLED(CONFIG_PCI)
267 bus->remap_addr = pci_ioremap_bar(pci, 0);
269 if (!bus->remap_addr) {
270 dev_err(bus->dev, "error: ioremap error\n");
275 sdev->bar[HDA_DSP_HDA_BAR] = bus->remap_addr;
277 /* get controller capabilities */
278 ret = hda_dsp_ctrl_get_caps(sdev);
280 dev_err(sdev->dev, "error: get caps error\n");
285 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
287 static const char *fixup_tplg_name(struct snd_sof_dev *sdev,
288 const char *sof_tplg_filename)
290 const char *tplg_filename = NULL;
294 filename = devm_kstrdup(sdev->dev, sof_tplg_filename, GFP_KERNEL);
298 /* this assumes a .tplg extension */
299 split_ext = strsep(&filename, ".");
301 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
302 "%s-idisp.tplg", split_ext);
306 return tplg_filename;
311 static int hda_init_caps(struct snd_sof_dev *sdev)
313 struct hdac_bus *bus = sof_to_bus(sdev);
314 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
315 struct hdac_ext_link *hlink;
316 struct snd_soc_acpi_mach_params *mach_params;
317 struct snd_soc_acpi_mach *hda_mach;
318 struct snd_sof_pdata *pdata = sdev->pdata;
319 struct snd_soc_acpi_mach *mach;
320 const char *tplg_filename;
326 device_disable_async_suspend(bus->dev);
328 /* check if dsp is there */
330 dev_dbg(sdev->dev, "PP capability, will probe DSP later.\n");
332 ret = hda_dsp_ctrl_init_chip(sdev, true);
334 dev_err(bus->dev, "error: init chip failed with ret: %d\n",
339 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
341 snd_hdac_ext_bus_get_ml_capabilities(bus);
343 /* init i915 and HDMI codecs */
344 ret = hda_codec_i915_init(sdev);
346 dev_err(sdev->dev, "error: no HDMI audio devices found\n");
350 /* codec detection */
351 if (!bus->codec_mask) {
352 dev_info(bus->dev, "no hda codecs found!\n");
354 dev_info(bus->dev, "hda codecs found, mask %lx\n",
357 for (i = 0; i < HDA_MAX_CODECS; i++) {
358 if (bus->codec_mask & (1 << i))
363 * If no machine driver is found, then:
365 * hda machine driver is used if :
366 * 1. there is one HDMI codec and one external HDAudio codec
369 if (!pdata->machine && codec_num <= 2 &&
370 HDA_IDISP_CODEC(bus->codec_mask)) {
371 hda_mach = snd_soc_acpi_intel_hda_machines;
372 pdata->machine = hda_mach;
374 /* topology: use the info from hda_machines */
375 pdata->tplg_filename =
376 hda_mach->sof_tplg_filename;
378 /* firmware: pick the first in machine list */
379 mach = pdata->desc->machines;
380 pdata->fw_filename = mach->sof_fw_filename;
382 dev_info(bus->dev, "using HDA machine driver %s now\n",
385 /* fixup topology file for HDMI only platforms */
386 if (codec_num == 1) {
387 /* use local variable for readability */
388 tplg_filename = pdata->tplg_filename;
389 tplg_filename = fixup_tplg_name(sdev, tplg_filename);
390 if (!tplg_filename) {
391 hda_codec_i915_exit(sdev);
394 pdata->tplg_filename = tplg_filename;
399 /* used by hda machine driver to create dai links */
400 if (pdata->machine) {
401 mach_params = (struct snd_soc_acpi_mach_params *)
402 &pdata->machine->mach_params;
403 mach_params->codec_mask = bus->codec_mask;
404 mach_params->platform = dev_name(sdev->dev);
407 /* create codec instances */
408 hda_codec_probe_bus(sdev);
410 hda_codec_i915_put(sdev);
413 * we are done probing so decrement link counts
415 list_for_each_entry(hlink, &bus->hlink_list, list)
416 snd_hdac_ext_bus_link_put(bus, hlink);
421 static const struct sof_intel_dsp_desc
422 *get_chip_info(struct snd_sof_pdata *pdata)
424 const struct sof_dev_desc *desc = pdata->desc;
425 const struct sof_intel_dsp_desc *chip_info;
427 chip_info = desc->chip_info;
432 int hda_dsp_probe(struct snd_sof_dev *sdev)
434 struct pci_dev *pci = to_pci_dev(sdev->dev);
435 struct sof_intel_hda_dev *hdev;
436 struct hdac_bus *bus;
437 const struct sof_intel_dsp_desc *chip;
441 * detect DSP by checking class/subclass/prog-id information
442 * class=04 subclass 03 prog-if 00: no DSP, legacy driver is required
443 * class=04 subclass 01 prog-if 00: DSP is present
444 * (and may be required e.g. for DMIC or SSP support)
445 * class=04 subclass 03 prog-if 80: either of DSP or legacy mode works
447 if (pci->class == 0x040300) {
448 dev_err(sdev->dev, "error: the DSP is not enabled on this platform, aborting probe\n");
450 } else if (pci->class != 0x040100 && pci->class != 0x040380) {
451 dev_err(sdev->dev, "error: unknown PCI class/subclass/prog-if 0x%06x found, aborting probe\n", pci->class);
454 dev_info(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n", pci->class);
456 chip = get_chip_info(sdev->pdata);
458 dev_err(sdev->dev, "error: no such device supported, chip id:%x\n",
464 hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
467 sdev->pdata->hw_pdata = hdev;
470 hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
473 if (IS_ERR(hdev->dmic_dev)) {
474 dev_err(sdev->dev, "error: failed to create DMIC device\n");
475 return PTR_ERR(hdev->dmic_dev);
479 * use position update IPC if either it is forced
480 * or we don't have other choice
482 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION)
483 hdev->no_ipc_position = 0;
485 hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0;
488 /* set up HDA base */
489 bus = sof_to_bus(sdev);
490 ret = hda_init(sdev);
495 #if IS_ENABLED(CONFIG_PCI)
496 sdev->bar[HDA_DSP_BAR] = pci_ioremap_bar(pci, HDA_DSP_BAR);
498 if (!sdev->bar[HDA_DSP_BAR]) {
499 dev_err(sdev->dev, "error: ioremap error\n");
504 sdev->mmio_bar = HDA_DSP_BAR;
505 sdev->mailbox_bar = HDA_DSP_BAR;
507 /* allow 64bit DMA address if supported by H/W */
508 if (!dma_set_mask(&pci->dev, DMA_BIT_MASK(64))) {
509 dev_dbg(sdev->dev, "DMA mask is 64 bit\n");
510 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(64));
512 dev_dbg(sdev->dev, "DMA mask is 32 bit\n");
513 dma_set_mask(&pci->dev, DMA_BIT_MASK(32));
514 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(32));
518 ret = hda_dsp_stream_init(sdev);
520 dev_err(sdev->dev, "error: failed to init streams\n");
522 * not all errors are due to memory issues, but trying
523 * to free everything does not harm
530 * let's try to enable msi firstly
531 * if it fails, use legacy interrupt mode
532 * TODO: support interrupt mode selection with kernel parameter
533 * support msi multiple vectors
535 ret = pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI);
537 dev_info(sdev->dev, "use legacy interrupt mode\n");
539 * in IO-APIC mode, hda->irq and ipc_irq are using the same
540 * irq number of pci->irq
542 hdev->irq = pci->irq;
543 sdev->ipc_irq = pci->irq;
544 sdev->msi_enabled = 0;
546 dev_info(sdev->dev, "use msi interrupt mode\n");
547 hdev->irq = pci_irq_vector(pci, 0);
548 /* ipc irq number is the same of hda irq */
549 sdev->ipc_irq = hdev->irq;
550 sdev->msi_enabled = 1;
553 dev_dbg(sdev->dev, "using HDA IRQ %d\n", hdev->irq);
554 ret = request_threaded_irq(hdev->irq, hda_dsp_stream_interrupt,
555 hda_dsp_stream_threaded_handler,
556 IRQF_SHARED, "AudioHDA", bus);
558 dev_err(sdev->dev, "error: failed to register HDA IRQ %d\n",
560 goto free_irq_vector;
563 dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq);
564 ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_ipc_irq_handler,
565 sof_ops(sdev)->irq_thread, IRQF_SHARED,
568 dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
574 synchronize_irq(pci->irq);
577 * clear TCSEL to clear playback on some HD Audio
578 * codecs. PCI TCSEL is defined in the Intel manuals.
580 snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
582 /* init HDA capabilities */
583 ret = hda_init_caps(sdev);
587 /* enable ppcap interrupt */
588 hda_dsp_ctrl_ppcap_enable(sdev, true);
589 hda_dsp_ctrl_ppcap_int_enable(sdev, true);
591 /* initialize waitq for code loading */
592 init_waitqueue_head(&sdev->waitq);
594 /* set default mailbox offset for FW ready message */
595 sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
600 free_irq(sdev->ipc_irq, sdev);
602 free_irq(hdev->irq, bus);
604 if (sdev->msi_enabled)
605 pci_free_irq_vectors(pci);
607 hda_dsp_stream_free(sdev);
608 /* dsp_unmap: not currently used */
609 iounmap(sdev->bar[HDA_DSP_BAR]);
611 iounmap(bus->remap_addr);
616 int hda_dsp_remove(struct snd_sof_dev *sdev)
618 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
619 struct hdac_bus *bus = sof_to_bus(sdev);
620 struct pci_dev *pci = to_pci_dev(sdev->dev);
621 const struct sof_intel_dsp_desc *chip = hda->desc;
623 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
624 /* codec removal, invoke bus_device_remove */
625 snd_hdac_ext_bus_device_remove(bus);
628 if (!IS_ERR_OR_NULL(hda->dmic_dev))
629 platform_device_unregister(hda->dmic_dev);
631 /* disable DSP IRQ */
632 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
633 SOF_HDA_PPCTL_PIE, 0);
635 /* disable CIE and GIE interrupts */
636 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
637 SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
641 hda_dsp_core_reset_power_down(sdev, chip->cores_mask);
644 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
645 SOF_HDA_PPCTL_GPROCEN, 0);
647 free_irq(sdev->ipc_irq, sdev);
648 free_irq(hda->irq, bus);
649 if (sdev->msi_enabled)
650 pci_free_irq_vectors(pci);
652 hda_dsp_stream_free(sdev);
653 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
654 snd_hdac_link_free_all(bus);
657 iounmap(sdev->bar[HDA_DSP_BAR]);
658 iounmap(bus->remap_addr);
660 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
661 snd_hdac_ext_bus_exit(bus);
663 hda_codec_i915_exit(sdev);
668 MODULE_LICENSE("Dual BSD/GPL");